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
337
338
339
340
341
342
343
344
345
use crate::{
    server::models::Mesh,
    utils::{
        block_utils::BlockUtils,
        chunk_utils::ChunkUtils,
        light_utils::{LightColor, LightUtils},
        ndarray::Ndarray,
        vec::{Vec2, Vec3},
    },
};

use super::block::BlockRotation;

#[derive(Default, Clone)]
pub struct ChunkParams {
    pub size: usize,
    pub max_height: usize,
}

#[derive(Default, Clone)]
pub struct Chunk {
    pub id: String,
    pub name: String,
    pub coords: Vec2<i32>,
    pub stage: Option<usize>,

    pub voxels: Ndarray<u32>,
    pub lights: Ndarray<u32>,
    pub height_map: Ndarray<u32>,

    pub mesh: Option<Mesh>,

    pub min: Vec3<i32>,
    pub max: Vec3<i32>,

    pub params: ChunkParams,
}

impl Chunk {
    pub fn new(id: &str, cx: i32, cz: i32, params: &ChunkParams) -> Self {
        let ChunkParams { size, max_height } = *params;

        let voxels = Ndarray::new(&[size, max_height, size], 0);
        let lights = Ndarray::new(&[size, max_height, size], 0);
        let height_map = Ndarray::new(&[size, size], 0);

        let min = Vec3(cx * size as i32, 0, cz * size as i32);
        let max = Vec3(
            (cx + 1) * size as i32,
            max_height as i32,
            (cz + 1) * size as i32,
        );

        Self {
            id: id.to_owned(),
            name: ChunkUtils::get_chunk_name(cx, cz),
            coords: Vec2(cx, cz),
            stage: None,

            mesh: None,

            voxels,
            lights,
            height_map,

            min,
            max,

            params: params.to_owned(),
        }
    }

    /// Get the raw value of voxel.
    ///
    /// Returns 0 if it's outside of the chunk.
    pub fn get_raw_value(&self, vx: i32, vy: i32, vz: i32) -> u32 {
        if !self.contains(vx, vy, vz) {
            return 0;
        }

        let Vec3(lx, ly, lz) = self.to_local(vx, vy, vz);
        self.voxels[&[lx, ly, lz]]
    }

    /// Set the raw value of voxel.
    ///
    /// Panics if the coordinates are outside of chunk.
    pub fn set_raw_value(&mut self, vx: i32, vy: i32, vz: i32, val: u32) {
        assert!(self.contains(vx, vy, vz));

        let Vec3(lx, ly, lz) = self.to_local(vx, vy, vz);
        self.voxels[&[lx, ly, lz]] = val;
    }

    /// Get a voxel type within chunk by voxel coordinates.
    ///
    /// Returns 0 if it's outside of the chunk.
    pub fn get_voxel(&self, vx: i32, vy: i32, vz: i32) -> u32 {
        BlockUtils::extract_id(self.get_raw_value(vx, vy, vz))
    }

    /// Set a voxel to type within chunk by voxel coordinates.
    ///
    /// Note: This clears the rotation and stage.
    ///
    /// Panics if the coordinates are outside of chunk.
    pub fn set_voxel(&mut self, vx: i32, vy: i32, vz: i32, id: u32) {
        let value = BlockUtils::insert_id(0, id);
        self.set_raw_value(vx, vy, vz, value);
    }

    /// Get a voxel rotation within chunk by voxel coordinates.
    ///
    /// Panics if it's outside of chunk.
    pub fn get_voxel_rotation(&self, vx: i32, vy: i32, vz: i32) -> BlockRotation {
        assert!(self.contains(vx, vy, vz,));

        BlockUtils::extract_rotation(self.get_raw_value(vx, vy, vz))
    }

    /// Set a voxel to rotation within chunk by voxel coordinates.
    ///
    /// Panics if the coordinates are outside of chunk.
    pub fn set_voxel_rotation(&mut self, vx: i32, vy: i32, vz: i32, rotation: &BlockRotation) {
        let value = BlockUtils::insert_rotation(self.get_raw_value(vx, vy, vz), rotation);
        self.set_raw_value(vx, vy, vz, value);
    }

    /// Get a voxel stage within chunk by voxel coordinates.
    ///
    /// Panics if it's outside of chunk.
    pub fn get_voxel_stage(&self, vx: i32, vy: i32, vz: i32) -> u32 {
        assert!(self.contains(vx, vy, vz));

        BlockUtils::extract_stage(self.get_raw_value(vx, vy, vz))
    }

    /// Set a voxel stage within chunk by voxel coordinates.
    ///
    /// Panics if it's outside of chunk.
    pub fn set_voxel_stage(&mut self, vx: i32, vy: i32, vz: i32, stage: u32) {
        let value = BlockUtils::insert_stage(self.get_raw_value(vx, vy, vz), stage);
        self.set_raw_value(vx, vy, vz, value);
    }

    /// Get the red light value for voxel by voxel coordinates.
    ///
    /// Returns 0 if it's outside of the chunk.
    pub fn get_red_light(&self, vx: i32, vy: i32, vz: i32) -> u32 {
        if !self.contains(vx, vy, vz) {
            return 0;
        }

        let Vec3(lx, ly, lz) = self.to_local(vx, vy, vz);
        self.get_local_red_light(lx as usize, ly as usize, lz as usize)
    }

    /// Set the red light value for voxel by voxel coordinates.
    ///
    /// Panics if it's outside of the chunk.
    pub fn set_red_light(&mut self, vx: i32, vy: i32, vz: i32, level: u32) {
        assert!(self.contains(vx, vy, vz,));

        let Vec3(lx, ly, lz) = self.to_local(vx, vy, vz);
        self.set_local_red_light(lx as usize, ly as usize, lz as usize, level);
    }

    /// Get the green light value for voxel by voxel coordinates.
    ///
    /// Returns 0 if it's outside of the chunk.
    pub fn get_green_light(&self, vx: i32, vy: i32, vz: i32) -> u32 {
        if !self.contains(vx, vy, vz) {
            return 0;
        }

        let Vec3(lx, ly, lz) = self.to_local(vx, vy, vz);
        self.get_local_green_light(lx as usize, ly as usize, lz as usize)
    }

    /// Set the green light value for voxel by voxel coordinates.
    ///
    /// Panics if it's outside of the chunk.
    pub fn set_green_light(&mut self, vx: i32, vy: i32, vz: i32, level: u32) {
        assert!(self.contains(vx, vy, vz,));

        let Vec3(lx, ly, lz) = self.to_local(vx, vy, vz);
        self.set_local_green_light(lx as usize, ly as usize, lz as usize, level);
    }

    /// Get the blue light value for voxel by voxel coordinates.
    ///
    /// Returns 0 if it's outside of the chunk.
    pub fn get_blue_light(&self, vx: i32, vy: i32, vz: i32) -> u32 {
        if !self.contains(vx, vy, vz) {
            return 0;
        }

        let Vec3(lx, ly, lz) = self.to_local(vx, vy, vz);
        self.get_local_blue_light(lx as usize, ly as usize, lz as usize)
    }

    /// Set the blue light value for voxel by voxel coordinates.
    ///
    /// Panics if it's outside of the chunk.
    pub fn set_blue_light(&mut self, vx: i32, vy: i32, vz: i32, level: u32) {
        assert!(self.contains(vx, vy, vz,));

        let Vec3(lx, ly, lz) = self.to_local(vx, vy, vz);
        self.set_local_blue_light(lx as usize, ly as usize, lz as usize, level);
    }

    /// Get the torch light value for voxel by voxel coordinates by color.
    ///
    /// Returns 0 if it's outside of the chunk.
    #[inline]
    pub fn get_torch_light(&self, vx: i32, vy: i32, vz: i32, color: &LightColor) -> u32 {
        match color {
            LightColor::Red => self.get_red_light(vx, vy, vz),
            LightColor::Green => self.get_green_light(vx, vy, vz),
            LightColor::Blue => self.get_blue_light(vx, vy, vz),
            LightColor::Sunlight => panic!("Getting torch light of Sunlight!"),
        }
    }

    /// Set the torch light value for voxel by voxel coordinates by color>
    ///
    /// Panics if it's outside of the chunk.
    #[inline]
    pub fn set_torch_light(&mut self, vx: i32, vy: i32, vz: i32, level: u32, color: &LightColor) {
        match color {
            LightColor::Red => self.set_red_light(vx, vy, vz, level),
            LightColor::Green => self.set_green_light(vx, vy, vz, level),
            LightColor::Blue => self.set_blue_light(vx, vy, vz, level),
            LightColor::Sunlight => panic!("Setting torch light of Sunlight!"),
        }
    }

    /// Get the sunlight value for voxel by voxel coordinates.
    ///
    /// Returns 0 if it's not within the chunk.
    pub fn get_sunlight(&self, vx: i32, vy: i32, vz: i32) -> u32 {
        if !self.contains(vx, vy, vz) {
            return 0;
        }

        let Vec3(lx, ly, lz) = self.to_local(vx, vy, vz);
        self.get_local_sunlight(lx as usize, ly as usize, lz as usize)
    }

    /// Set the sunlight value for voxel by voxel coordinates.
    ///
    /// Panics if it's outside of the chunk.
    pub fn set_sunlight(&mut self, vx: i32, vy: i32, vz: i32, level: u32) {
        assert!(self.contains(vx, vy, vz,));

        let Vec3(lx, ly, lz) = self.to_local(vx, vy, vz);
        self.set_local_sunlight(lx as usize, ly as usize, lz as usize, level)
    }

    /// Get the max height of a voxel column.
    ///
    /// Returns `max_height` if it's not within the chunk.
    pub fn get_max_height(&self, vx: i32, vz: i32) -> u32 {
        if !self.contains(vx, 0, vz) {
            return self.params.max_height as u32;
        }

        let Vec3(lx, _, lz) = self.to_local(vx, 0, vz);
        self.height_map[&[lx as usize, lz as usize]]
    }

    /// Set the max height of a voxel column.
    ///
    /// Panics if it's not within the chunk.
    pub fn set_max_height(&mut self, vx: i32, vz: i32, height: u32) {
        assert!(self.contains(vx, 0, vz,));

        let Vec3(lx, _, lz) = self.to_local(vx, 0, vz);
        self.height_map[&[lx as usize, lz as usize]] = height;
    }

    /// Get the red light value locally.
    #[inline]
    fn get_local_red_light(&self, lx: usize, ly: usize, lz: usize) -> u32 {
        LightUtils::extract_red_light(self.lights[&[lx, ly, lz]])
    }

    /// Set the red light value locally.
    #[inline]
    fn set_local_red_light(&mut self, lx: usize, ly: usize, lz: usize, level: u32) {
        self.lights[&[lx, ly, lz]] =
            LightUtils::insert_red_light(self.lights[&[lx, ly, lz]], level);
    }

    /// Get the green light value locally.
    #[inline]
    fn get_local_green_light(&self, lx: usize, ly: usize, lz: usize) -> u32 {
        LightUtils::extract_green_light(self.lights[&[lx, ly, lz]])
    }

    /// Set the green light value locally.
    #[inline]
    fn set_local_green_light(&mut self, lx: usize, ly: usize, lz: usize, level: u32) {
        self.lights[&[lx, ly, lz]] =
            LightUtils::insert_green_light(self.lights[&[lx, ly, lz]], level);
    }

    /// Get the blue light value locally.
    #[inline]
    fn get_local_blue_light(&self, lx: usize, ly: usize, lz: usize) -> u32 {
        LightUtils::extract_blue_light(self.lights[&[lx, ly, lz]])
    }

    /// Set the blue light value locally.
    #[inline]
    fn set_local_blue_light(&mut self, lx: usize, ly: usize, lz: usize, level: u32) {
        self.lights[&[lx, ly, lz]] =
            LightUtils::insert_blue_light(self.lights[&[lx, ly, lz]], level);
    }

    /// Get the sunlight value locally.
    #[inline]
    fn get_local_sunlight(&self, lx: usize, ly: usize, lz: usize) -> u32 {
        LightUtils::extract_sunlight(self.lights[&[lx, ly, lz]])
    }

    /// Set the sunlight value locally.
    #[inline]
    fn set_local_sunlight(&mut self, lx: usize, ly: usize, lz: usize, level: u32) {
        self.lights[&[lx, ly, lz]] = LightUtils::insert_sunlight(self.lights[&[lx, ly, lz]], level);
    }

    /// Convert voxel coordinates to local chunk coordinates.
    fn to_local(&self, vx: i32, vy: i32, vz: i32) -> Vec3<usize> {
        let Vec3(mx, my, mz) = self.min;
        Vec3((vx - mx) as usize, (vy - my) as usize, (vz - mz) as usize)
    }

    fn contains(&self, vx: i32, vy: i32, vz: i32) -> bool {
        let ChunkParams { size, max_height } = self.params;
        let Vec3(lx, ly, lz) = self.to_local(vx, vy, vz);

        return lx < size && ly < max_height && lz < size;
    }
}