Fill in gaps in cliffs

Description

Commands

git clone git@github.com:atsheehan/iridium
cd iridium
git checkout d270a1870aa574c5bf53d1743458f9877f0ab50e
cargo run --release

Code Changes

Modified src/world.rsGitHub

@@ -7,7 +7,7 @@
77 x_width: u32,
88 z_depth: u32,
99 camera: Camera,
10- heights: Vec<u32>,
10+ heights: Vec<i32>,
1111 }
1212
1313 impl World {
@@ -34,7 +34,7 @@
3434 let xz_position = coordinate.center().xz();
3535
3636 let height = heightmap.height_at(&xz_position);
37- let scaled_height = (height * height_range as f32) as u32 + min_height;
37+ let scaled_height = (height * height_range as f32) as i32 + min_height as i32;
3838
3939 heights.push(scaled_height);
4040 }
@@ -54,16 +54,39 @@
5454 }
5555
5656 pub(crate) fn visible_block_positions(&self) -> impl Iterator<Item = Vec3> + '_ {
57- (0..self.z_depth).flat_map(move |z| {
58- (0..self.x_width).map(move |x| {
59- let index = ((self.x_width * z) + x) as usize;
60- let y = self.heights[index];
57+ let x_width = self.x_width as i32;
58+ let z_depth = self.z_depth as i32;
6159
62- Vec3(x as f32, y as f32, z as f32)
60+ (0..z_depth).flat_map(move |z| {
61+ (0..x_width).flat_map(move |x| {
62+ let y_max = self.height_at(x, z);
63+
64+ let min_neighbor_height = [
65+ self.height_at(x + 1, z),
66+ self.height_at(x - 1, z),
67+ self.height_at(x, z + 1),
68+ self.height_at(x, z - 1),
69+ ]
70+ .into_iter()
71+ .min()
72+ .unwrap();
73+
74+ let y_min = y_max.min(min_neighbor_height);
75+
76+ (y_min..=y_max).map(move |y| Vec3(x as f32, y as f32, z as f32))
6377 })
6478 })
6579 }
6680
81+ fn height_at(&self, x: i32, z: i32) -> i32 {
82+ if x < 0 || x >= self.x_width as i32 || z < 0 || z >= self.z_depth as i32 {
83+ 0
84+ } else {
85+ let index = ((self.x_width as i32 * z) + x) as usize;
86+ self.heights[index]
87+ }
88+ }
89+
6790 pub(crate) fn start_moving_forward(&mut self) {
6891 self.camera.velocity = self.camera.velocity.set_z(MOVE_SPEED);
6992 }
@@ -7,7 +7,7 @@
7 x_width: u32,
8 z_depth: u32,
9 camera: Camera,
10- heights: Vec<u32>,
11 }
12
13 impl World {
@@ -34,7 +34,7 @@
34 let xz_position = coordinate.center().xz();
35
36 let height = heightmap.height_at(&xz_position);
37- let scaled_height = (height * height_range as f32) as u32 + min_height;
38
39 heights.push(scaled_height);
40 }
@@ -54,16 +54,39 @@
54 }
55
56 pub(crate) fn visible_block_positions(&self) -> impl Iterator<Item = Vec3> + '_ {
57- (0..self.z_depth).flat_map(move |z| {
58- (0..self.x_width).map(move |x| {
59- let index = ((self.x_width * z) + x) as usize;
60- let y = self.heights[index];
61
62- Vec3(x as f32, y as f32, z as f32)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63 })
64 })
65 }
66
 
 
 
 
 
 
 
 
 
67 pub(crate) fn start_moving_forward(&mut self) {
68 self.camera.velocity = self.camera.velocity.set_z(MOVE_SPEED);
69 }
@@ -7,7 +7,7 @@
7 x_width: u32,
8 z_depth: u32,
9 camera: Camera,
10+ heights: Vec<i32>,
11 }
12
13 impl World {
@@ -34,7 +34,7 @@
34 let xz_position = coordinate.center().xz();
35
36 let height = heightmap.height_at(&xz_position);
37+ let scaled_height = (height * height_range as f32) as i32 + min_height as i32;
38
39 heights.push(scaled_height);
40 }
@@ -54,16 +54,39 @@
54 }
55
56 pub(crate) fn visible_block_positions(&self) -> impl Iterator<Item = Vec3> + '_ {
57+ let x_width = self.x_width as i32;
58+ let z_depth = self.z_depth as i32;
 
 
59
60+ (0..z_depth).flat_map(move |z| {
61+ (0..x_width).flat_map(move |x| {
62+ let y_max = self.height_at(x, z);
63+
64+ let min_neighbor_height = [
65+ self.height_at(x + 1, z),
66+ self.height_at(x - 1, z),
67+ self.height_at(x, z + 1),
68+ self.height_at(x, z - 1),
69+ ]
70+ .into_iter()
71+ .min()
72+ .unwrap();
73+
74+ let y_min = y_max.min(min_neighbor_height);
75+
76+ (y_min..=y_max).map(move |y| Vec3(x as f32, y as f32, z as f32))
77 })
78 })
79 }
80
81+ fn height_at(&self, x: i32, z: i32) -> i32 {
82+ if x < 0 || x >= self.x_width as i32 || z < 0 || z >= self.z_depth as i32 {
83+ 0
84+ } else {
85+ let index = ((self.x_width as i32 * z) + x) as usize;
86+ self.heights[index]
87+ }
88+ }
89+
90 pub(crate) fn start_moving_forward(&mut self) {
91 self.camera.velocity = self.camera.velocity.set_z(MOVE_SPEED);
92 }