Skip to content

Commit

Permalink
Load the world radially
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas0008 committed Aug 13, 2024
1 parent 85785c9 commit 5075c97
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
1 change: 1 addition & 0 deletions pumpkin-world/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
59 changes: 59 additions & 0 deletions pumpkin-world/src/radial_chunk_iterator.rs
Original file line number Diff line number Diff line change
@@ -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<Self::Item> {
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)
}
}
11 changes: 2 additions & 9 deletions pumpkin/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 5075c97

Please sign in to comment.