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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use std::{collections::HashMap, convert::TryFrom};
use byteorder::{BigEndian, ReadBytesExt};
use serde::Deserialize;
use crate::{bits_per_block, PackedBits};
use super::biome::Biome;
#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
pub struct Chunk<'a> {
pub data_version: i32,
#[serde(borrow)]
pub level: Level<'a>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
pub struct Level<'a> {
#[serde(rename = "xPos")]
pub x_pos: i32,
#[serde(rename = "zPos")]
pub z_pos: i32,
pub biomes: Option<&'a [u8]>,
#[serde(borrow)]
pub heightmaps: Option<Heightmaps<'a>>,
#[serde(rename = "HeightMap")]
pub old_heightmap: Option<Vec<i32>>,
pub sections: Option<Vec<Section<'a>>>,
pub status: &'a str,
#[serde(skip)]
sec_map: Option<HashMap<i8, usize>>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub struct Heightmaps<'a> {
#[serde(borrow)]
pub motion_blocking: Option<PackedBits<'a>>,
pub motion_blocking_no_leaves: Option<PackedBits<'a>>,
pub ocean_floor: Option<PackedBits<'a>>,
pub world_surface: Option<PackedBits<'a>>,
#[serde(skip)]
unpacked_motion_blocking: Option<[u16; 16 * 16]>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
pub struct Section<'a> {
pub y: i8,
#[serde(borrow)]
pub block_states: Option<PackedBits<'a>>,
pub palette: Option<Vec<Block<'a>>>,
#[serde(skip)]
unpacked_states: Option<[u16; 16 * 16 * 16]>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
pub struct Block<'a> {
pub name: &'a str,
#[serde(default)]
pub properties: HashMap<&'a str, &'a str>,
}
impl<'a> Chunk<'a> {
pub fn block(&mut self, x: usize, y: usize, z: usize) -> Option<&Block> {
let sec = self.get_section_for_y(y)?;
if (sec.y as usize) * 16 > y {}
let sec_y = y - sec.y as usize * 16;
let state_index = (sec_y as usize * 16 * 16) + z * 16 + x;
if sec.unpacked_states == None {
let bits_per_item = bits_per_block(sec.palette.as_ref()?.len());
sec.unpacked_states = Some([0; 16 * 16 * 16]);
let buf = sec.unpacked_states.as_mut()?;
sec.block_states
.as_ref()?
.unpack_into(bits_per_item, &mut buf[..]);
}
let pal_index = sec.unpacked_states.as_ref()?[state_index] as usize;
sec.palette.as_ref()?.get(pal_index)
}
pub fn height_of(&mut self, x: usize, z: usize) -> Option<usize> {
let ref mut maps = self.level.heightmaps;
match maps {
Some(maps) => {
if maps.unpacked_motion_blocking == None {
maps.unpacked_motion_blocking = Some([0; 256]);
let buf = maps.unpacked_motion_blocking.as_mut()?;
maps.motion_blocking
.as_ref()?
.unpack_heights_into(&mut buf[..]);
}
Some(maps.unpacked_motion_blocking.as_ref()?[z * 16 + x] as usize)
}
None => self
.level
.old_heightmap
.as_ref()
.map(|v| v[x * 16 + z] as usize),
}
}
pub fn biome_of(&self, x: usize, _y: usize, z: usize) -> Option<Biome> {
let biomes = self.level.biomes?;
if biomes.len() == 1024 * 4 {
let i = 4 * ((z / 4) * 4 + (x / 4));
let biome = (&biomes[i..]).read_i32::<BigEndian>().ok()?;
Biome::try_from(biome).ok()
} else if biomes.len() == 256 * 4 {
let i = 4 * (z * 16 + x);
let biome = (&biomes[i..]).read_i32::<BigEndian>().ok()?;
Biome::try_from(biome).ok()
} else {
None
}
}
fn calculate_sec_map(&mut self) {
self.level.sec_map = Some(HashMap::new());
let map = self.level.sec_map.as_mut().unwrap();
for (i, sec) in self.level.sections.iter().flatten().enumerate() {
map.insert(sec.y, i);
}
}
fn get_section_for_y(&mut self, y: usize) -> Option<&mut Section<'a>> {
if self.level.sections.as_ref()?.is_empty() {
return None;
}
if self.level.sec_map.is_none() {
self.calculate_sec_map();
}
let containing_section_y = y / 16;
let section_index = self
.level
.sec_map
.as_ref()
.unwrap()
.get(&(containing_section_y as i8))?;
let sec = self.level.sections.as_mut()?.get_mut(*section_index);
sec
}
}
impl<'a> Block<'a> {
pub fn encoded_description(&self) -> String {
let mut id = self.name.to_string() + "|";
let mut sep = "";
let mut props = self
.properties
.iter()
.filter(|(k, _)| **k != "waterlogged")
.collect::<Vec<_>>();
props.sort();
for (k, v) in props {
id = id + sep + k + "=" + v;
sep = ",";
}
id
}
}