Skip to content

Commit aa8f4eb

Browse files
committed
make chunk gen use ChunkPos
1 parent 1ecfd64 commit aa8f4eb

File tree

9 files changed

+56
-49
lines changed

9 files changed

+56
-49
lines changed

src/bin/src/launch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub fn generate_spawn_chunks(state: GlobalState) -> Result<(), BinaryError> {
4747
batch.execute(move || {
4848
let chunk = state_clone
4949
.terrain_generator
50-
.generate_chunk(x, z)
50+
.generate_chunk(ChunkPos::new(x, z))
5151
.map(Arc::new);
5252

5353
match chunk {

src/bin/src/packet_handlers/play_packets/player_action.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@ pub fn handle(
5353
.0
5454
.clone()
5555
.terrain_generator
56-
.generate_chunk(
57-
event.location.x.div_euclid(16),
58-
event.location.z.div_euclid(16),
59-
)
56+
.generate_chunk(BlockPos::from(event.location.clone()).chunk())
6057
.map_err(BinaryError::WorldGen)?
6158
}
6259
};

src/bin/src/systems/chunk_sending.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub fn handle(
8282
state
8383
.0
8484
.terrain_generator
85-
.generate_chunk(coordinates.x(), coordinates.z())
85+
.generate_chunk(coordinates)
8686
.expect("Could not generate chunk")
8787
.into(),
8888
);

src/bin/src/systems/listeners/digging_system.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ fn break_block(
285285
.0
286286
.clone()
287287
.terrain_generator
288-
.generate_chunk(position.x >> 4, position.z >> 4)
288+
.generate_chunk(BlockPos::from(position.clone()).chunk())
289289
.map_err(BinaryError::WorldGen)?
290290
}
291291
};

src/lib/net/benches/packets.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub fn bench_packets(c: &mut criterion::BenchmarkGroup<WallTime>) {
99

1010
fn bench_chunk_packet(c: &mut criterion::BenchmarkGroup<WallTime>) {
1111
let chunk = ferrumc_world_gen::WorldGenerator::new(0)
12-
.generate_chunk(0, 0)
12+
.generate_chunk(ChunkPos::new(0, 0))
1313
.unwrap();
1414
let chunk_packet = black_box(
1515
ferrumc_net::packets::outgoing::chunk_and_light_data::ChunkAndLightData::from_chunk(

src/lib/net/src/conn_init/login.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ fn send_initial_chunks(
472472
let chunk = state.world.load_chunk(ChunkPos::new(x,z), "overworld").unwrap_or(
473473
state
474474
.terrain_generator
475-
.generate_chunk(x, z)
475+
.generate_chunk(ChunkPos::new(x, z))
476476
.expect("Could not generate chunk")
477477
.into(),
478478
);

src/lib/world/src/pos.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,13 @@ impl ChunkHeight {
108108

109109
#[derive(Hash, Debug, Clone, Copy, PartialEq, Eq)]
110110
pub struct ChunkPos {
111-
pos: IVec2,
111+
pub pos: IVec2,
112112
}
113113

114114
impl ChunkPos {
115-
pub fn new(x: i32, z: i32) -> Self {
115+
pub const fn new(x: i32, z: i32) -> Self {
116+
assert!(x < 1 << 22);
117+
assert!(z < 1 << 22);
116118
Self {
117119
pos: IVec2::new(x * 16, z * 16),
118120
}
@@ -259,6 +261,14 @@ impl ColumnPos {
259261
pub fn chunk(self) -> ChunkPos {
260262
ChunkPos::new(self.pos.x, self.pos.y)
261263
}
264+
265+
pub fn x(&self) -> i32 {
266+
self.pos.x
267+
}
268+
269+
pub fn z(&self) -> i32 {
270+
self.pos.y
271+
}
262272
}
263273

264274
impl From<IVec2> for ColumnPos {

src/lib/world_gen/src/biomes/plains.rs

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use ferrumc_macros::block;
44
use ferrumc_world::block_state_id::BlockStateId;
55
use ferrumc_world::chunk_format::Chunk;
66
use ferrumc_world::edit_batch::EditBatch;
7-
use ferrumc_world::pos::{BlockPos, ChunkHeight};
7+
use ferrumc_world::pos::{BlockPos, ChunkColumnPos, ChunkHeight, ChunkPos};
88

99
pub(crate) struct PlainsBiome;
1010

@@ -19,8 +19,7 @@ impl BiomeGenerator for PlainsBiome {
1919

2020
fn generate_chunk(
2121
&self,
22-
x: i32,
23-
z: i32,
22+
pos: ChunkPos,
2423
noise: &NoiseGenerator,
2524
) -> Result<Chunk, WorldGenError> {
2625
let mut chunk = Chunk::new(ChunkHeight::new(-64, 384));
@@ -33,11 +32,12 @@ impl BiomeGenerator for PlainsBiome {
3332
}
3433

3534
// Then generate some heights
36-
for chunk_x in 0..16i64 {
37-
for chunk_z in 0..16i64 {
38-
let global_x = i64::from(x) * 16 + chunk_x;
39-
let global_z = i64::from(z) * 16 + chunk_z;
40-
let height = noise.get_noise(global_x as f64, global_z as f64);
35+
for chunk_x in 0..16 {
36+
for chunk_z in 0..16 {
37+
let curr_pos = pos.column_pos(ChunkColumnPos::new(chunk_x, chunk_z));
38+
let global_x = curr_pos.x();
39+
let global_z = curr_pos.z();
40+
let height = noise.get_noise(f64::from(global_x), f64::from(global_z));
4141
let height = (height * 64.0) as i32 + 64;
4242
heights.push((global_x, global_z, height));
4343
}
@@ -58,22 +58,14 @@ impl BiomeGenerator for PlainsBiome {
5858
for y in 0..height {
5959
if y + above_filled_sections <= 64 {
6060
batch.set_block(
61-
BlockPos::of(
62-
global_x as i32,
63-
y + above_filled_sections,
64-
global_z as i32,
65-
)
66-
.chunk_block_pos(),
61+
BlockPos::of(global_x, y + above_filled_sections, global_z)
62+
.chunk_block_pos(),
6763
block!("sand"),
6864
);
6965
} else {
7066
batch.set_block(
71-
BlockPos::of(
72-
global_x as i32,
73-
y + above_filled_sections,
74-
global_z as i32,
75-
)
76-
.chunk_block_pos(),
67+
BlockPos::of(global_x, y + above_filled_sections, global_z)
68+
.chunk_block_pos(),
7769
block!("grass_block", {snowy: false}),
7870
);
7971
}
@@ -95,17 +87,25 @@ mod test {
9587
fn test_is_ok() {
9688
let generator = PlainsBiome {};
9789
let noise = NoiseGenerator::new(0);
98-
assert!(generator.generate_chunk(0, 0, &noise).is_ok());
90+
assert!(
91+
generator
92+
.generate_chunk(ChunkPos::new(0, 0), &noise)
93+
.is_ok()
94+
);
9995
}
10096

10197
#[test]
10298
fn test_random_chunk_generation() {
10399
let generator = PlainsBiome {};
104100
let noise = NoiseGenerator::new(0);
105101
for _ in 0..100 {
106-
let x = rand::random::<i32>();
107-
let z = rand::random::<i32>();
108-
assert!(generator.generate_chunk(x, z, &noise).is_ok());
102+
let x = rand::random::<i32>() & ((1 << 22) - 1);
103+
let z = rand::random::<i32>() & ((1 << 22) - 1);
104+
assert!(
105+
generator
106+
.generate_chunk(ChunkPos::new(x, z), &noise)
107+
.is_ok()
108+
);
109109
}
110110
}
111111

@@ -115,12 +115,12 @@ mod test {
115115
let noise = NoiseGenerator::new(0);
116116
assert!(
117117
generator
118-
.generate_chunk(1610612735, 1610612735, &noise)
118+
.generate_chunk(ChunkPos::new((1 << 22) - 1, (1 << 22) - 1), &noise)
119119
.is_ok()
120120
);
121121
assert!(
122122
generator
123-
.generate_chunk(-1610612735, -1610612735, &noise)
123+
.generate_chunk(ChunkPos::new(-((1 << 22) - 1), -((1 << 22) - 1)), &noise)
124124
.is_ok()
125125
);
126126
}
@@ -131,7 +131,11 @@ mod test {
131131
let generator = PlainsBiome {};
132132
let seed = rand::random::<u64>();
133133
let noise = NoiseGenerator::new(seed);
134-
assert!(generator.generate_chunk(0, 0, &noise).is_ok());
134+
assert!(
135+
generator
136+
.generate_chunk(ChunkPos::new(0, 0), &noise)
137+
.is_ok()
138+
);
135139
}
136140
}
137141
}

src/lib/world_gen/src/lib.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod biomes;
22
pub mod errors;
33

44
use crate::errors::WorldGenError;
5-
use ferrumc_world::chunk_format::Chunk;
5+
use ferrumc_world::{chunk_format::Chunk, pos::ChunkPos};
66
use noise::{Clamp, NoiseFn, OpenSimplex};
77

88
/// Trait for generating a biome
@@ -11,12 +11,8 @@ use noise::{Clamp, NoiseFn, OpenSimplex};
1111
pub(crate) trait BiomeGenerator {
1212
fn _biome_id(&self) -> u8;
1313
fn _biome_name(&self) -> String;
14-
fn generate_chunk(
15-
&self,
16-
x: i32,
17-
z: i32,
18-
noise: &NoiseGenerator,
19-
) -> Result<Chunk, WorldGenError>;
14+
fn generate_chunk(&self, pos: ChunkPos, noise: &NoiseGenerator)
15+
-> Result<Chunk, WorldGenError>;
2016
}
2117

2218
pub(crate) struct NoiseGenerator {
@@ -57,13 +53,13 @@ impl WorldGenerator {
5753
}
5854
}
5955

60-
fn get_biome(&self, _x: i32, _z: i32) -> Box<dyn BiomeGenerator> {
56+
fn get_biome(&self, _pos: ChunkPos) -> Box<dyn BiomeGenerator> {
6157
// Implement biome selection here
6258
Box::new(biomes::plains::PlainsBiome)
6359
}
6460

65-
pub fn generate_chunk(&self, x: i32, z: i32) -> Result<Chunk, WorldGenError> {
66-
let biome = self.get_biome(x, z);
67-
biome.generate_chunk(x, z, &self.noise_generator)
61+
pub fn generate_chunk(&self, pos: ChunkPos) -> Result<Chunk, WorldGenError> {
62+
let biome = self.get_biome(pos);
63+
biome.generate_chunk(pos, &self.noise_generator)
6864
}
6965
}

0 commit comments

Comments
 (0)