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
|
use azalea_block::BlockState;
use azalea_core::{heightmap_kind::HeightmapKind as HeightmapKind_, math, position::ChunkBlockPos};
use azalea_registry::tags::blocks::LEAVES;
use tracing::warn;
use crate::{BitStorage, Section, chunk::get_block_state_from_sections};
// TODO: when removing this deprecated marker, also rename `HeightmapKind_` back
// to `HeightmapKind`.
#[deprecated = "moved to `azalea_core::heightmap_kind::HeightmapKind`."]
pub type HeightmapKind = azalea_core::heightmap_kind::HeightmapKind;
#[derive(Clone, Debug)]
pub struct Heightmap {
pub data: BitStorage,
pub min_y: i32,
pub kind: HeightmapKind_,
}
fn blocks_motion(block_state: BlockState) -> bool {
// TODO
!block_state.is_air()
}
fn motion_blocking(block_state: BlockState) -> bool {
// TODO
!block_state.is_air()
|| block_state
.property::<azalea_block::properties::Waterlogged>()
.unwrap_or_default()
}
pub fn is_heightmap_opaque(heightmap: HeightmapKind_, block_state: BlockState) -> bool {
let registry_block = block_state.as_block_kind();
type K = HeightmapKind_;
match heightmap {
K::WorldSurfaceWg => !block_state.is_air(),
K::WorldSurface => !block_state.is_air(),
K::OceanFloorWg => blocks_motion(block_state),
K::OceanFloor => blocks_motion(block_state),
K::MotionBlocking => motion_blocking(block_state),
K::MotionBlockingNoLeaves => {
motion_blocking(block_state) && !LEAVES.contains(®istry_block)
}
}
}
impl Heightmap {
pub fn new(kind: HeightmapKind_, dimension_height: u32, min_y: i32, data: Box<[u64]>) -> Self {
let bits = math::ceil_log2(dimension_height + 1);
let mut bit_storage = BitStorage::new(bits as usize, 16 * 16, None)
.expect("data is empty, so this can't fail");
if bit_storage.data.len() != data.len() {
warn!(
"Ignoring heightmap data, size does not match; expected: {}, got: {}",
bit_storage.data.len(),
data.len()
);
} else {
bit_storage.data.copy_from_slice(&data);
}
Self {
kind,
data: bit_storage,
min_y,
}
}
pub fn get_index(x: u8, z: u8) -> usize {
(x as usize) + (z as usize) * 16
}
pub fn get_first_available_at_index(&self, index: usize) -> i32 {
self.data.get(index) as i32 + self.min_y
}
pub fn get_first_available(&self, x: u8, z: u8) -> i32 {
self.get_first_available_at_index(Self::get_index(x, z))
}
pub fn get_highest_taken(&self, x: u8, z: u8) -> i32 {
self.get_first_available(x, z) - 1
}
pub fn set_height(&mut self, x: u8, z: u8, height: i32) {
self.data
.set(Self::get_index(x, z), (height - self.min_y) as u64);
}
/// Updates the heightmap with the given block state at the given position.
pub fn update(
&mut self,
pos: &ChunkBlockPos,
block_state: BlockState,
sections: &[Section],
) -> bool {
let first_available_y = self.get_first_available(pos.x, pos.z);
if pos.y <= first_available_y - 2 {
return false;
}
if is_heightmap_opaque(self.kind, block_state) {
// increase y
if pos.y >= first_available_y {
self.set_height(pos.x, pos.z, pos.y + 1);
return true;
}
} else if first_available_y - 1 == pos.y {
// decrease y
for y in (self.min_y..pos.y).rev() {
if is_heightmap_opaque(
self.kind,
get_block_state_from_sections(
sections,
&ChunkBlockPos::new(pos.x, y, pos.z),
self.min_y,
)
.unwrap_or_default(),
) {
self.set_height(pos.x, pos.z, y + 1);
return true;
}
}
self.set_height(pos.x, pos.z, self.min_y);
return true;
}
false
}
/// Get an iterator over the top available block positions in this
/// heightmap.
pub fn iter_first_available(&self) -> impl Iterator<Item = ChunkBlockPos> + '_ {
self.data.iter().enumerate().map(move |(index, height)| {
let x = (index % 16) as u8;
let z = (index / 16) as u8;
ChunkBlockPos::new(x, height as i32 + self.min_y, z)
})
}
/// Get an iterator over the top block positions in this heightmap.
pub fn iter_highest_taken(&self) -> impl Iterator<Item = ChunkBlockPos> + '_ {
self.data.iter().enumerate().map(move |(index, height)| {
let x = (index % 16) as u8;
let z = (index / 16) as u8;
ChunkBlockPos::new(x, height as i32 + self.min_y - 1, z)
})
}
}
|