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
346
347
348
use hashbrown::{HashMap, HashSet};

use crate::{ndarray, BlockUtils, ChunkUtils, LightColor, LightUtils, Ndarray, Vec2, Vec3};

use super::{
    access::VoxelAccess,
    block::{BlockRotation, PY_ROTATION, Y_000_ROTATION},
    chunks::Chunks,
};

/// What kind of data does this space have/need?
#[derive(Default)]
pub struct SpaceData {
    pub needs_lights: bool,
    pub needs_voxels: bool,
    pub needs_height_maps: bool,
}

/// Parameters of constructing a Space data structure.
#[derive(Default, Clone)]
pub struct SpaceParams {
    /// By how many blocks does the space extend from the center chunk.
    pub margin: usize,

    /// The horizontal dimension of each chunk.
    pub chunk_size: usize,

    /// The number of sub-chunks of each chunk.
    pub sub_chunks: usize,

    /// Maximum height of the chunk/space.
    pub max_height: usize,

    /// Maximum light of the voxelize world.
    pub max_light_level: u32,
}

/// A data structure used in Voxelize to access voxel data of multiple chunks at
/// the same time. Centered with one chunk, a Space allows developers to know what's
/// around a chunk.
///
/// Construct a space by calling `chunks.make_space`.
#[derive(Default, Clone)]
pub struct Space {
    /// Chunk coordinate of the center chunk of the space.
    pub coords: Vec2<i32>,

    /// Width of the space.
    pub width: usize,

    /// Shape of the space.
    pub shape: Vec3<usize>,

    /// Minimum voxel coordinate of the space.
    pub min: Vec3<i32>,

    /// Parameters to construct the space.
    pub params: SpaceParams,

    /// A set of sub-chunks that have been updated.
    pub updated_levels: HashSet<u32>,

    /// A map of voxels, chunk coordinates -> n-dims array of voxels.
    voxels: HashMap<Vec2<i32>, Ndarray<u32>>,

    /// A map of lights, chunk coordinates -> n-dims array of lights.
    lights: HashMap<Vec2<i32>, Ndarray<u32>>,

    /// A map of height maps, chunk coordinates -> n-dims array of height maps.
    height_maps: HashMap<Vec2<i32>, Ndarray<u32>>,
}

impl Space {
    /// Check if space contains this coordinate
    pub fn contains(&self, vx: i32, vy: i32, vz: i32) -> bool {
        let (coords, _) = self.to_local(vx, vy, vz);

        vy >= 0
            && vy < self.params.max_height as i32
            && (self.lights.contains_key(&coords)
                || self.voxels.contains_key(&coords)
                || self.height_maps.contains_key(&coords))
    }

    /// Converts a voxel position to a chunk coordinate and a chunk local coordinate.
    fn to_local(&self, vx: i32, vy: i32, vz: i32) -> (Vec2<i32>, Vec3<usize>) {
        let SpaceParams { chunk_size, .. } = self.params;

        let coords = ChunkUtils::map_voxel_to_chunk(vx, vy, vz, chunk_size);
        let local = ChunkUtils::map_voxel_to_chunk_local(vx, vy, vz, chunk_size);

        (coords, local)
    }
}

/// A data structure to build a space.
pub struct SpaceBuilder<'a> {
    pub chunks: &'a Chunks,
    pub coords: Vec2<i32>,
    pub params: SpaceParams,

    pub needs_voxels: bool,
    pub needs_lights: bool,
    pub needs_height_maps: bool,

    pub strict: bool,
}

impl SpaceBuilder<'_> {
    /// Set this space to load in voxel data.
    pub fn needs_voxels(mut self) -> Self {
        self.needs_voxels = true;
        self
    }

    /// Set this space to load in lighting data.
    pub fn needs_lights(mut self) -> Self {
        self.needs_lights = true;
        self
    }

    /// Set this space to load in height map data.
    pub fn needs_height_maps(mut self) -> Self {
        self.needs_height_maps = true;
        self
    }

    /// Set this space to load in all voxel, lighting, and height map data.
    pub fn needs_all(mut self) -> Self {
        self.needs_voxels = true;
        self.needs_lights = true;
        self.needs_height_maps = true;
        self
    }

    /// Sets if this space is strict. If strict, space panics if one of the chunks DNE.
    pub fn strict(mut self) -> Self {
        self.strict = true;
        self
    }

    /// Create a `Space` instance with the instructed data loaded in.
    pub fn build(self) -> Space {
        let SpaceParams {
            margin,
            chunk_size,
            max_height,
            ..
        } = self.params;

        let Vec2(cx, cz) = self.coords;

        if margin == 0 {
            panic!("Margin of 0 on Space is wasteful.");
        }

        let width = chunk_size + margin * 2;

        let mut voxels = HashMap::<Vec2<i32>, Ndarray<u32>>::new();
        let mut lights = HashMap::<Vec2<i32>, Ndarray<u32>>::new();
        let mut height_maps = HashMap::<Vec2<i32>, Ndarray<u32>>::new();

        self.chunks
            .light_traversed_chunks(&self.coords)
            .into_iter()
            .for_each(|n_coords| {
                if !self.chunks.is_within_world(&n_coords) {
                    return;
                }

                if let Some(chunk) = self.chunks.raw(&n_coords) {
                    if self.needs_voxels {
                        voxels.insert(n_coords.to_owned(), chunk.voxels.clone());
                    }

                    if self.needs_lights {
                        lights.insert(n_coords.to_owned(), chunk.lights.clone());
                    } else {
                        lights.insert(n_coords.to_owned(), ndarray(&chunk.lights.shape, 0));
                    }

                    if self.needs_height_maps {
                        height_maps.insert(n_coords.to_owned(), chunk.height_map.clone());
                    }
                } else if self.strict {
                    panic!("Space incomplete in strict mode: {:?}", n_coords);
                }
            });

        let min = Vec3(
            cx * chunk_size as i32 - margin as i32,
            0,
            cz * chunk_size as i32 - margin as i32,
        );

        let shape = Vec3(width, max_height, width);

        Space {
            coords: self.coords.to_owned(),
            params: self.params.to_owned(),

            width,
            shape,
            min,

            voxels,
            lights,
            height_maps,

            ..Default::default()
        }
    }
}

impl VoxelAccess for Space {
    /// Get the raw voxel data at the voxel position. Zero is returned if chunk doesn't exist.
    /// Panics if space does not contain voxel data.
    #[inline]
    fn get_raw_voxel(&self, vx: i32, vy: i32, vz: i32) -> u32 {
        if self.voxels.is_empty() {
            panic!("Space does not contain voxel data.");
        }

        let (coords, Vec3(lx, ly, lz)) = self.to_local(vx, vy, vz);

        if let Some(voxels) = self.voxels.get(&coords) {
            if !voxels.contains(&[lx, ly, lz]) {
                return 0;
            }

            return voxels[&[lx, ly, lz]];
        }

        0
    }

    /// Get the voxel type at the voxel position. Zero is returned if chunk doesn't exist.
    /// Panics if space does not contain voxel data.
    fn get_voxel(&self, vx: i32, vy: i32, vz: i32) -> u32 {
        if !self.contains(vx, vy, vz) {
            return 0;
        }

        BlockUtils::extract_id(self.get_raw_voxel(vx, vy, vz))
    }

    /// Get the voxel rotation at the voxel position. Zero is returned if chunk doesn't exist.
    /// Panics if space does not contain voxel data.
    fn get_voxel_rotation(&self, vx: i32, vy: i32, vz: i32) -> BlockRotation {
        if !self.contains(vx, vy, vz) {
            return BlockRotation::encode(PY_ROTATION, Y_000_ROTATION);
        }

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

    /// Get the voxel stage at the voxel position. Zero is returned if chunk doesn't exist.
    /// Panics if space does not contain voxel data.
    fn get_voxel_stage(&self, vx: i32, vy: i32, vz: i32) -> u32 {
        if !self.contains(vx, vy, vz) {
            return 0;
        }

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

    /// Get the raw light level at the voxel position. Zero is returned if chunk doesn't exist.
    /// Panics if space does not contain lighting data.
    #[inline]
    fn get_raw_light(&self, vx: i32, vy: i32, vz: i32) -> u32 {
        if self.lights.is_empty() {
            panic!("Space does not contain voxel data.");
        }

        let (coords, Vec3(lx, ly, lz)) = self.to_local(vx, vy, vz);

        if let Some(lights) = self.lights.get(&coords) {
            if !lights.contains(&[lx, ly, lz]) {
                return 0;
            }

            return lights[&[lx, ly, lz]];
        }

        0
    }

    /// Set the raw light level at the voxel position. Returns false if chunk doesn't exist.
    #[inline]
    fn set_raw_light(&mut self, vx: i32, vy: i32, vz: i32, level: u32) -> bool {
        if self.lights.is_empty() {
            panic!("Space does not contain light data.");
        }

        if !self.contains(vx, vy, vz) {
            return false;
        }

        let (coords, Vec3(lx, ly, lz)) = self.to_local(vx, vy, vz);

        if let Some(lights) = self.lights.get_mut(&coords) {
            let chunk_level = vy as u32 / (self.params.max_height / self.params.sub_chunks) as u32;
            self.updated_levels.insert(chunk_level);

            lights[&[lx, ly, lz]] = level;
            return true;
        }

        false
    }

    /// Get the sunlight level at the voxel position. Zero is returned if chunk doesn't exist.
    fn get_sunlight(&self, vx: i32, vy: i32, vz: i32) -> u32 {
        if !self.contains(vx, vy, vz) {
            return if vy < 0 {
                0
            } else {
                self.params.max_light_level
            };
        }

        LightUtils::extract_sunlight(self.get_raw_light(vx, vy, vz))
    }

    /// Get the max height at the voxel column. Zero is returned if column doesn't exist.
    fn get_max_height(&self, vx: i32, vz: i32) -> u32 {
        if self.height_maps.is_empty() {
            panic!("Space does not contain height map data.");
        }

        if !self.contains(vx, 0, vz) {
            return 0;
        }

        let (coords, Vec3(lx, _, lz)) = self.to_local(vx, 0, vz);

        if let Some(height_map) = self.height_maps.get(&coords) {
            return height_map[&[lx, lz]];
        }

        0
    }

    /// Get a reference of lighting n-dimensional array.
    fn get_lights(&self, cx: i32, cz: i32) -> Option<&Ndarray<u32>> {
        self.lights.get(&Vec2(cx, cz))
    }
}