-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathmodel.rs
More file actions
195 lines (177 loc) · 5.67 KB
/
Copy pathmodel.rs
File metadata and controls
195 lines (177 loc) · 5.67 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
use anyhow::Context;
use libcraft::biome::BiomeList;
use libcraft::chunk::LightStore;
use libcraft::{
chunk::{
paletted_container::{Paletteable, PalettedContainer},
PackedArray,
},
BlockState, ChunkPosition,
};
use libcraft::{Chunk, ChunkSection, Sections};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChunkModel {
pub sections: Vec<ChunkSectionModel>,
}
impl ChunkModel {
pub fn from_chunk(chunk: &Chunk, biomes: &BiomeList) -> Self {
Self {
sections: chunk
.sections()
.iter()
.map(|section| ChunkSectionModel::from_chunk_section(section, biomes))
.collect(),
}
}
pub fn to_chunk(
&self,
biomes: &BiomeList,
pos: ChunkPosition,
sections: Sections,
min_y: i32,
) -> Result<Chunk, anyhow::Error> {
let mut chunk = Chunk::new(pos, sections, min_y);
for (model, section) in self.sections.iter().zip(chunk.sections_mut()) {
*section = model.to_chunk_section(biomes)?;
}
Ok(chunk)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChunkSectionModel {
pub sky_light: Option<PackedArrayModel>,
pub block_light: Option<PackedArrayModel>,
pub blocks: PalettedContainerModel<BlockStateModel>,
pub biomes: PalettedContainerModel<String>,
pub air_block_count: u32,
}
impl ChunkSectionModel {
pub fn from_chunk_section(section: &ChunkSection, biomes: &BiomeList) -> Self {
Self {
sky_light: section.light().sky_light().map(Into::into),
block_light: section.light().block_light().map(Into::into),
blocks: PalettedContainerModel::from(section.blocks(), |state| {
BlockStateModel::from(*state)
}),
biomes: PalettedContainerModel::from(section.biomes(), |biome_id| {
biomes.get_by_id(biome_id).unwrap().0.clone()
}),
air_block_count: section.air_blocks(),
}
}
pub fn to_chunk_section(&self, biomes: &BiomeList) -> Result<ChunkSection, anyhow::Error> {
Ok(ChunkSection::new(
self.blocks
.clone()
.into(|block| (&block).try_into().unwrap()),
self.biomes
.clone()
.into(|biome| biomes.get_id(&biome).unwrap()),
self.air_block_count,
LightStore::from_packed_arrays(
self.sky_light.clone().map(Into::into),
self.block_light.clone().map(Into::into),
)
.unwrap(),
))
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BlockStateModel {
pub kind: String,
pub properties: Vec<(String, String)>,
}
impl From<BlockState> for BlockStateModel {
fn from(state: BlockState) -> Self {
Self {
kind: state.namespaced_id().to_owned(),
properties: state
.property_values()
.map(|(a, b)| (a.to_owned(), b.to_owned()))
.collect(),
}
}
}
impl TryFrom<&BlockStateModel> for BlockState {
type Error = anyhow::Error;
fn try_from(model: &BlockStateModel) -> Result<Self, anyhow::Error> {
BlockState::from_namespaced_id_and_property_values(
&model.kind,
model
.properties
.iter()
.map(|(a, b)| (a.as_str(), b.as_str())),
)
.context("invalid block state")
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PalettedContainerModel<T> {
SingleValue(T),
MultipleValues {
data: PackedArrayModel,
palette: Vec<T>,
},
GlobalPalette {
data: PackedArrayModel,
},
}
impl<T> PalettedContainerModel<T> {
pub fn from<U>(container: &PalettedContainer<U>, convert: impl Fn(&U) -> T) -> Self
where
U: Paletteable,
{
match container {
PalettedContainer::SingleValue(val) => {
PalettedContainerModel::SingleValue(convert(val))
}
PalettedContainer::MultipleValues { data, palette } => {
PalettedContainerModel::MultipleValues {
data: data.into(),
palette: palette.iter().map(convert).collect(),
}
}
PalettedContainer::GlobalPalette { data } => {
PalettedContainerModel::GlobalPalette { data: data.into() }
}
}
}
pub fn into<U>(self, convert: impl Fn(T) -> U) -> PalettedContainer<U>
where
U: Paletteable,
{
match self {
PalettedContainerModel::SingleValue(val) => {
PalettedContainer::SingleValue(convert(val))
}
PalettedContainerModel::MultipleValues { data, palette } => {
PalettedContainer::MultipleValues {
data: data.into(),
palette: palette.into_iter().map(convert).collect(),
}
}
PalettedContainerModel::GlobalPalette { data } => {
PalettedContainer::GlobalPalette { data: data.into() }
}
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PackedArrayModel {
pub length: usize,
pub bits: Vec<u64>,
}
impl<'a> From<&'a PackedArray> for PackedArrayModel {
fn from(packed: &'a PackedArray) -> Self {
Self {
length: packed.len(),
bits: packed.as_u64_slice().to_vec(),
}
}
}
impl From<PackedArrayModel> for PackedArray {
fn from(model: PackedArrayModel) -> Self {
Self::from_u64_vec(model.bits, model.length)
}
}