diff --git a/pumpkin-world/src/lib.rs b/pumpkin-world/src/lib.rs index b99c06691..30051a517 100644 --- a/pumpkin-world/src/lib.rs +++ b/pumpkin-world/src/lib.rs @@ -5,3 +5,4 @@ pub const WORLD_Y_START_AT: i32 = -64; pub const DIRECT_PALETTE_BITS: u32 = 15; mod block_registry; mod world; +pub mod radial_chunk_iterator; \ No newline at end of file diff --git a/pumpkin-world/src/radial_chunk_iterator.rs b/pumpkin-world/src/radial_chunk_iterator.rs new file mode 100644 index 000000000..650a55253 --- /dev/null +++ b/pumpkin-world/src/radial_chunk_iterator.rs @@ -0,0 +1,59 @@ +use std::collections::VecDeque; + +pub struct RadialIterator { + radius: i32, + direction: usize, + current: (i32, i32), + step_size: i32, + steps_taken: i32, + steps_in_direction: i32, +} + +impl RadialIterator { + pub fn new(radius: i32) -> Self { + RadialIterator { + radius, + direction: 0, + current: (0, 0), + step_size: 1, + steps_taken: 0, + steps_in_direction: 0, + } + } +} + +impl Iterator for RadialIterator { + type Item = (i32, i32); + + fn next(&mut self) -> Option { + if self.steps_taken >= self.radius * self.radius * 4 { + return None; + } + + let result = self.current; + + self.steps_in_direction += 1; + + // Move in the current direction + match self.direction { + 0 => self.current.0 += 1, // Right + 1 => self.current.1 += 1, // Up + 2 => self.current.0 -= 1, // Left + 3 => self.current.1 -= 1, // Down + _ => {} + } + + if self.steps_in_direction >= self.step_size { + self.direction = (self.direction + 1) % 4; + self.steps_in_direction = 0; + + // Increase step size after completing two directions + if self.direction == 0 || self.direction == 2 { + self.step_size += 1; + } + } + + self.steps_taken += 1; + Some(result) + } +} \ No newline at end of file diff --git a/pumpkin/src/server.rs b/pumpkin/src/server.rs index e8234ca9f..4c2820204 100644 --- a/pumpkin/src/server.rs +++ b/pumpkin/src/server.rs @@ -25,7 +25,7 @@ use pumpkin_protocol::{ uuid::UUID, ClientPacket, Players, Sample, StatusResponse, VarInt, Version, CURRENT_MC_PROTOCOL, }; -use pumpkin_world::dimension::Dimension; +use pumpkin_world::{dimension::Dimension, radial_chunk_iterator::RadialIterator}; use pumpkin_registry::Registry; use rsa::{traits::PublicKeyParts, RsaPrivateKey, RsaPublicKey}; @@ -330,19 +330,12 @@ impl Server { // TODO: do this in a world async fn spawn_test_chunk(client: &mut Client) { - let mut wanted_chunks = Vec::new(); - - for i in -32i32..32 { - for j in -32i32..32 { - wanted_chunks.push((i, j)) - } - } let chunks = Dimension::OverWorld .into_level( // TODO: load form config "./world".parse().unwrap(), ) - .read_chunks(wanted_chunks) + .read_chunks(RadialIterator::new(32).collect()) .await; client.send_packet(&CCenterChunk {