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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//! This module provides data structures to store all the data about a chunk
use nbt_lib::{
    convert_list_to,
    traits::{AsNbtValue, FromNbtValue, IntoNbt},
    NbtValue,
};

use std::{collections::HashMap, str::FromStr};

use crate::anvil::region::chunk::section::ChunkSection;
use nbt_lib::{create_compound_map, unwrap_to, unwrap_to_empty};

use super::block_entity::BlockEntity;

mod generation_status;

pub use generation_status::*;

mod heightmaps;

pub use heightmaps::*;

/// A struct holding all chunk data
///
/// # Source
/// - [minecraft.fandom.com](https://minecraft.fandom.com/wiki/Chunk_format#NBT_structure)
///
/// # Info
/// Everything, taged as "Not confirmed for 1.18 format" is not implemented, but will be if it
/// turnes out to be neccessarry
#[derive(PartialEq, Debug)]
pub struct ChunkData {
    /// The real x position of the chunk
    pub x_pos: i32,
    /// The read z position of the chunk
    pub z_pos: i32,
    /// The lowest y sector position in the chunk
    pub y_pos: i32,
    /// The generation status of the chunk
    pub status: GenerationStatus,
    /// The last tick that the chunk was updated
    pub last_update: i64,
    /// A list of all sections of the chunk
    pub sections: Vec<super::section::ChunkSection>,
    /// A list of all block entities in a chunk
    pub block_entities: Vec<BlockEntity>,
    // Info: CarvingMasks are only used for proto chunk and will probably be added to this the
    // moment i write the world generation and learn, how and what exactly it stores
    /// All heightmaps of the section
    pub heightmaps: Heightmaps,
    /// A list of "active" liquid tiles in the chunk
    pub fluid_ticks: Vec<TileTick>,
    /// A list of "active" blocks in the chunk
    pub block_ticks: Vec<TileTick>,
    /// This variable increases tick by the amount of players inside of it
    pub inhabited_time: i64,
    ///// A list of 24 (the amount of sections) lists. Each one of them holding data for blocks that
    ///// have to be updated, this is only used in proto chunks
    // pub post_processing: [Vec<ToBeTicked>; 24],
    /// A list of all structures and their data
    pub structure_data_list: StructureDataList,
}
impl ChunkData {
    const X_POS: &'static str = "xPos";
    const Y_POS: &'static str = "yPos";
    const Z_POS: &'static str = "zPos";
    const STATUS: &'static str = "Status";
    const LAST_UPDATE: &'static str = "LastUpdate";
    const SECTIONS: &'static str = "sections";
    const BLOCK_ENTITIES: &'static str = "block_entities";
    const HEIGHTMAPS: &'static str = "Heightmaps";
    const FLUID_TICKS: &'static str = "fluid_ticks";
    const BLOCK_TICKS: &'static str = "block_ticks";
    const INHABITED_TIME: &'static str = "InhabitedTime";
    const STRUCTURES: &'static str = "structures";
    /// creates a new instance
    pub fn new(
        heightmap: [[u16; 16]; 16],
        filler: &str,
        x: i64,
        z: i64,
        current_tick: i64,
    ) -> Self {
        let mut s = Self {
            x_pos: x as i32,
            y_pos: -4,
            z_pos: z as i32,
            status: GenerationStatus::Empty,
            last_update: current_tick,
            sections: Vec::new(),
            block_entities: Vec::new(),
            block_ticks: Vec::new(),
            heightmaps: dbg!(Heightmaps::default()),
            fluid_ticks: Vec::new(),
            inhabited_time: 0,
            structure_data_list: StructureDataList::new(),
        };
        s.create_pregen(heightmap, filler);
        s
    }
    /// creates a new instance of [`ChunkData`]
    ///
    /// [`ChunkData`]: `ChunkData`
    fn create_pregen(&mut self, heightmap: [[u16; 16]; 16], filler: &str) {
        let heightmap = heightmap.map(|map| map.map(|e| e.min(384)));
        let heightmap_max = *heightmap
            .iter()
            .map(|map| map.iter().min().unwrap_or(&384))
            .min()
            .unwrap_or(&384) as i16
            - 64;
        let lowest_full_section = dbg!(heightmap_max / 16);
        for y in -4..=lowest_full_section {
            let section = ChunkSection::new_filled(y as i8, filler, false);
            self.sections.push(section);
        }
        self.sections.push(ChunkSection::new_with_height_map(
            lowest_full_section as i8 + 1,
            filler,
            true,
            heightmap.map(|e| e.map(|e| (e as i16) - 64)),
        ));
        for y in lowest_full_section + 2..=19 {
            let section = ChunkSection::new_filled(y as i8, filler, true);
            self.sections.push(section);
        }
        let motion_blocking_vec: Vec<u16> = heightmap.to_vec().into_iter().flatten().collect();
        let motion_blocking = Some(
            motion_blocking_vec
                .chunks(7)
                .map(|chunk| {
                    if chunk.len() == 7 {
                        ((chunk[0] as u64) << (64 - 9))
                            | ((chunk[1] as u64) << (64 - (9 * 2)))
                            | ((chunk[2] as u64) << (64 - (9 * 3)))
                            | ((chunk[3] as u64) << (64 - (9 * 4)))
                            | ((chunk[4] as u64) << (64 - (9 * 5)))
                            | ((chunk[5] as u64) << (64 - (9 * 6)))
                            | ((chunk[6] as u64) << (64 - (9 * 7)))
                    } else {
                        (chunk[0] as u64) << (64 - 9)
                    }
                })
                .collect::<Vec<_>>()
                .try_into()
                .unwrap(),
        );
        self.heightmaps = Heightmaps {
            motion_blocking,
            motion_blocking_no_leaves: None,
            ocean_floor: None,
            ocean_floor_wg: None,
            world_surface: motion_blocking,
            world_surface_wg: None,
        };
        /*
                Self {
                    x_pos: x as i32,
                    y_pos: -4,
                    z_pos: z as i32,
                    status: GenerationStatus::Full,
                    last_update: current_tick,
                    sections,
                    block_entities: Vec::new(),
                    fluid_ticks: Vec::new(),
                    block_ticks: Vec::new(),
                    inhabited_time: 0,
                    structure_data_list: StructureDataList { structure_references: Vec::new(), starts: Vec::new() },
                    heightmaps,
                }
        */
    }
}

mod structure_data_list;

pub use structure_data_list::*;

impl FromNbtValue for ChunkData {
    fn from_nbt_value(value: NbtValue) -> Result<Self, ()>
    where
        Self: Sized,
    {
        use nbt_lib::NbtValue::*;
        // use std::string::String as Str;
        if let Compound(_, data) = value {
            let (name, structure_data) = data
                .get(Self::STRUCTURES)
                .ok_or(())?
                .as_compound()
                .map_err(|_| ())?;
            let structure_data_list_nbt =
                NbtValue::Compound(name.map(|s| s.clone()), structure_data);
            let structure_data_list = StructureDataList::from_nbt_value(structure_data_list_nbt)?;
            return Ok(Self {
                x_pos: unwrap_to_empty!(data.get(Self::X_POS), i32),
                y_pos: unwrap_to_empty!(data.get(Self::Y_POS), i32),
                z_pos: unwrap_to_empty!(data.get(Self::Z_POS), i32),
                status: GenerationStatus::from_str(unwrap_to_empty!(data.get(Self::STATUS), str))?,
                last_update: unwrap_to_empty!(data.get(Self::LAST_UPDATE), i64),
                sections: convert_list_to!(data.get(Self::SECTIONS), ChunkSection),
                block_entities: convert_list_to!(data.get(Self::BLOCK_ENTITIES), BlockEntity),
                heightmaps: unwrap_to!(data.get(Self::HEIGHTMAPS), Heightmaps),
                fluid_ticks: convert_list_to!(data.get(Self::FLUID_TICKS), TileTick),
                block_ticks: convert_list_to!(data.get(Self::BLOCK_TICKS), TileTick),
                inhabited_time: unwrap_to_empty!(data.get(Self::INHABITED_TIME), i64),
                structure_data_list,
            });
        }
        Err(())
    }
}
pub(crate) fn list_to_nbt_value_list<T>(data: &Vec<T>) -> Result<NbtValue, ()>
where
    T: AsNbtValue,
{
    Ok(NbtValue::List(
        data.iter()
            .map(|i| i.as_nbt_value())
            .collect::<Result<Vec<NbtValue>, _>>()?,
    ))
}
impl AsNbtValue for ChunkData {
    fn as_nbt_value(&self) -> Result<NbtValue, ()> {
        use nbt_lib::NbtValue::*;
        use std::string::String as Str;
        // name is `Some("")` not `None`, because ChunkData is the outer most NbtValue
        Ok(Compound(
            Some(Str::new()),
            create_compound_map!(
                DataVersion: Int(nbt_lib::NBT_VERSION),
                xPos: Int(self.x_pos),
                zPos: Int(self.z_pos),
                yPos: Int(self.y_pos),
                Status: String(self.status.to_string()),
                LastUpdate: Long(self.last_update),
                sections: list_to_nbt_value_list(&self.sections)?,
                block_entities: list_to_nbt_value_list(&self.block_entities)?,
                Heightmaps: self.heightmaps.as_nbt_value()?,
                fluid_ticks: list_to_nbt_value_list(&self.fluid_ticks)?,
                block_ticks: list_to_nbt_value_list(&self.block_ticks)?,
                InhabitedTime: Long(self.inhabited_time),
                // PostProcessing: list_to_nbt_value_list(&self.po)
                structures: self.structure_data_list.as_nbt_value()?
            ),
        ))
    }
}
/// This enum is to determine, if a structure is in a chunk or not
#[derive(PartialEq, Debug)]
pub enum ChunkDataHolder {
    /// This option is used if the structure is in the chunk
    Data(structure::StructureData),
    /// This optiojn is used, if the structure is marked as abscent
    Empty {
        /// The id is INVALID, if it is absent, its only for parsing reasons included
        id: String,
    },
}
impl ChunkDataHolder {
    /// creates a `ChunkDataHolder` from its `NbtValue`
    pub fn from(name: String, value: NbtValue) -> Result<Self, ()>
    where
        Self: Sized,
    {
        let (_, data) = unwrap_to_empty!(Some(value), compound);
        if unwrap_to_empty!(data.get("id"), str) == "INVALID" {
            return Ok(Self::Empty {
                id: String::from("INVALID"),
            });
        }
        Ok(Self::Data(structure::StructureData::from_nbt(name, data)?))
    }
}
impl AsNbtValue for ChunkDataHolder {
    fn as_nbt_value(&self) -> Result<NbtValue, ()> {
        todo!()
    }
}
pub mod structure;
/// This is data about ticks, that have been scheduled but not happened
#[derive(PartialEq, Debug)]
pub struct TileTick {
    /// The id of the block
    pub i: String,
    /// The priority
    ///
    /// # Note
    /// If multiple tile ticks are schedules, the ones with lower `p` are processed first,
    /// the order of tile ticks with same `p` is not regulated and assumed to be random,
    /// this is used for updates that do not interact with each other
    pub p: i32,
    /// the amount of ticks, until the processing should occur, this can be negativ if the
    /// processing is overdue
    pub t: i32,
    /// The x coordinate of the tile that should be ticked
    pub x: i32,
    /// The y coordinate of the tile that should be ticked
    pub y: i32,
    /// The z coordinate of the tile that should be ticked
    pub z: i32,
}
impl FromNbtValue for TileTick {
    fn from_nbt_value(value: NbtValue) -> Result<Self, ()>
    where
        Self: Sized,
    {
        let (_, data) = unwrap_to_empty!(Some(value), compound);
        Ok(Self {
            i: unwrap_to_empty!(data.get("i"), string),
            p: unwrap_to_empty!(data.get("p"), i32),
            t: unwrap_to_empty!(data.get("t"), i32),
            x: unwrap_to_empty!(data.get("x"), i32),
            y: unwrap_to_empty!(data.get("y"), i32),
            z: unwrap_to_empty!(data.get("z"), i32),
        })
    }
}
impl AsNbtValue for TileTick {
    fn as_nbt_value(&self) -> Result<NbtValue, ()> {
        todo!()
    }
}
#[deprecated]
pub(crate) struct ToBeTicked;
impl AsNbtValue for ToBeTicked {
    fn as_nbt_value(&self) -> Result<NbtValue, ()> {
        todo!()
    }
}
impl IntoNbt for ChunkData {
    fn to_nbt(&self) -> nbt_lib::NbtValue {
        let data_version = NbtValue::Int(nbt_lib::NBT_VERSION);
        todo!()
    }
}