-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathheightmap.rs
More file actions
202 lines (177 loc) · 5.36 KB
/
heightmap.rs
File metadata and controls
202 lines (177 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use std::marker::PhantomData;
use blocks::{BlockId, SimplifiedBlockKind};
use crate::{CHUNK_HEIGHT, CHUNK_WIDTH};
use super::PackedArray;
/// Stores heightmaps for a chunk.
#[derive(Debug, Clone)]
pub struct HeightmapStore {
pub motion_blocking: Heightmap<MotionBlocking>,
pub motion_blocking_no_leaves: Heightmap<MotionBlockingNoLeaves>,
pub light_blocking: Heightmap<LightBlocking>,
pub ocean_floor: Heightmap<OceanFloor>,
pub world_surface: Heightmap<WorldSurface>,
}
impl Default for HeightmapStore {
fn default() -> Self {
Self::new()
}
}
impl HeightmapStore {
pub fn new() -> Self {
Self {
motion_blocking: Heightmap::new(),
motion_blocking_no_leaves: Heightmap::new(),
light_blocking: Heightmap::new(),
ocean_floor: Heightmap::new(),
world_surface: Heightmap::new(),
}
}
pub fn update(
&mut self,
x: usize,
y: usize,
z: usize,
old_block: BlockId,
new_block: BlockId,
get_block: impl Fn(usize, usize, usize) -> BlockId,
) {
self.motion_blocking
.update(x, y, z, old_block, new_block, &get_block);
self.motion_blocking_no_leaves
.update(x, y, z, old_block, new_block, &get_block);
self.light_blocking
.update(x, y, z, old_block, new_block, &get_block);
self.ocean_floor
.update(x, y, z, old_block, new_block, &get_block);
self.world_surface
.update(x, y, z, old_block, new_block, &get_block);
}
pub fn recalculate(&mut self, get_block: impl Fn(usize, usize, usize) -> BlockId) {
self.motion_blocking.recalculate(&get_block);
self.motion_blocking_no_leaves.recalculate(&get_block);
self.light_blocking.recalculate(&get_block);
self.ocean_floor.recalculate(&get_block);
self.world_surface.recalculate(&get_block);
}
}
/// A function used to compute heightmaps.
pub trait HeightmapFunction {
/// Returns whether a block should be considered
/// "solid" during the heightmap computation.
fn is_solid(block: BlockId) -> bool;
}
#[derive(Debug, Clone)]
pub struct LightBlocking;
impl HeightmapFunction for LightBlocking {
fn is_solid(block: BlockId) -> bool {
block.is_opaque()
}
}
#[derive(Debug, Clone)]
pub struct MotionBlocking;
impl HeightmapFunction for MotionBlocking {
fn is_solid(block: BlockId) -> bool {
block.is_solid() || block.is_fluid()
}
}
#[derive(Debug, Clone)]
pub struct MotionBlockingNoLeaves;
impl HeightmapFunction for MotionBlockingNoLeaves {
fn is_solid(block: BlockId) -> bool {
(block.is_solid() || block.is_fluid())
&& block.simplified_kind() != SimplifiedBlockKind::Leaves
}
}
#[derive(Debug, Clone)]
pub struct OceanFloor;
impl HeightmapFunction for OceanFloor {
fn is_solid(block: BlockId) -> bool {
block.is_solid()
}
}
#[derive(Debug, Clone)]
pub struct WorldSurface;
impl HeightmapFunction for WorldSurface {
fn is_solid(block: BlockId) -> bool {
!block.is_air()
}
}
#[derive(Debug, Clone)]
pub struct Heightmap<F> {
heights: PackedArray,
_marker: PhantomData<F>,
}
impl<F> Default for Heightmap<F>
where
F: HeightmapFunction,
{
fn default() -> Self {
Self::new()
}
}
impl<F> Heightmap<F>
where
F: HeightmapFunction,
{
pub fn new() -> Self {
Self {
heights: PackedArray::new(256, 9),
_marker: PhantomData,
}
}
pub fn set_height(&mut self, x: usize, z: usize, height: usize) {
let index = self.index(x, z);
self.heights.set(index, height as u64);
}
pub fn set_height_index(&mut self, index: usize, height: i64) {
self.heights.as_u64_mut_vec()[index] = height as u64;
}
pub fn height(&self, x: usize, z: usize) -> Option<usize> {
let index = self.index(x, z);
self.heights.get(index).map(|x| x as usize)
}
pub fn as_u64_slice(&self) -> &[u64] {
self.heights.as_u64_slice()
}
fn index(&self, x: usize, z: usize) -> usize {
(z << 4) | x
}
/// Updates this height map after a block has been updated.
pub fn update(
&mut self,
x: usize,
y: usize,
z: usize,
old_block: BlockId,
new_block: BlockId,
get_block: impl Fn(usize, usize, usize) -> BlockId,
) {
if F::is_solid(old_block) && self.height(x, z) == Some(y) {
// This was old the highest block
for i in (0..y).rev() {
let block = get_block(x, i, z);
if F::is_solid(block) {
self.set_height(x, z, i + 1);
break;
}
}
}
if F::is_solid(new_block) && self.height(x, z).unwrap() < y {
// This is the new highest block
self.set_height(x, z, y);
}
}
/// Recalculates this entire heightmap.
pub fn recalculate(&mut self, get_block: impl Fn(usize, usize, usize) -> BlockId) {
for x in 0..CHUNK_WIDTH {
for z in 0..CHUNK_WIDTH {
for y in (0..CHUNK_HEIGHT).rev() {
if F::is_solid(get_block(x, y, z)) {
self.set_height(x, z, y + 1);
break;
}
}
}
}
}
}