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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
use std::collections::VecDeque;

use log::info;

use crate::{
    Block, ChunkUtils, LightColor, Ndarray, Registry, Vec2, Vec3, VoxelAccess, WorldConfig,
};

pub const VOXEL_NEIGHBORS: [[i32; 3]; 6] = [
    [1, 0, 0],
    [-1, 0, 0],
    [0, 0, 1],
    [0, 0, -1],
    [0, 1, 0],
    [0, -1, 0],
];

/// Node of a light propagation queue.
#[derive(Debug)]
pub struct LightNode {
    pub voxel: [i32; 3],
    pub level: u32,
}

const RED: LightColor = LightColor::Red;
const GREEN: LightColor = LightColor::Green;
const BLUE: LightColor = LightColor::Blue;
const SUNLIGHT: LightColor = LightColor::Sunlight;

/// A set of utility functions to simulate global illumination in a Voxelize world.
pub struct Lights;

// TODO: RIGHT NOW, A TOP SLAB WILL STILL LET LIGHT TRAVEL INTO A BOTTOM SLAB...

impl Lights {
    /// Propagate a specific queue of `LightNode`s in a depth-first-search fashion. If the propagation
    /// is for sunlight, light value does not decrease going downwards to simulate sunshine.
    pub fn flood_light(
        space: &mut dyn VoxelAccess,
        mut queue: VecDeque<LightNode>,
        color: &LightColor,
        registry: &Registry,
        config: &WorldConfig,
        min: Option<&Vec3<i32>>,
        shape: Option<&Vec3<usize>>,
    ) {
        let &WorldConfig {
            max_height,
            min_chunk,
            max_chunk,
            max_light_level,
            ..
        } = config;

        let [start_cx, start_cz] = min_chunk;
        let [end_cx, end_cz] = max_chunk;

        let max_height = max_height as i32;
        let is_sunlight = *color == LightColor::Sunlight;

        while !queue.is_empty() {
            let LightNode { voxel, level } = queue.pop_front().unwrap();
            let [vx, vy, vz] = voxel;

            if level == 0 {
                continue;
            }

            let source_block = registry.get_block_by_id(space.get_voxel(vx, vy, vz));
            let source_transparency =
                source_block.get_rotated_transparency(&space.get_voxel_rotation(vx, vy, vz));

            for [ox, oy, oz] in VOXEL_NEIGHBORS.into_iter() {
                let nvy = vy + oy;

                if nvy < 0 || nvy >= max_height {
                    continue;
                }

                let nvx = vx + ox;
                let nvz = vz + oz;

                let Vec2(ncx, ncz) =
                    ChunkUtils::map_voxel_to_chunk(nvx, nvy, nvz, config.chunk_size);

                // If neighbor is out of this chunk, or if voxel is out of the specified range, continue to next neighbor.
                if ncx < start_cx
                    || ncz < start_cz
                    || ncx > end_cx
                    || ncz > end_cz
                    || if let Some(&Vec3(start_x, _, start_z)) = min {
                        nvx < start_x
                            || nvz < start_z
                            || if let Some(&Vec3(shape0, _, shape2)) = shape {
                                nvx >= start_x + shape0 as i32 || nvz >= start_z + shape2 as i32
                            } else {
                                false
                            }
                    } else {
                        false
                    }
                {
                    continue;
                }

                let next_level = level
                    - if is_sunlight && oy == -1 && level == max_light_level {
                        0
                    } else {
                        1
                    };
                let next_voxel = [nvx, nvy, nvz];
                let n_block = registry.get_block_by_id(space.get_voxel(nvx, nvy, nvz));
                let n_transparency =
                    n_block.get_rotated_transparency(&space.get_voxel_rotation(nvx, nvy, nvz));

                // To not continue:
                // (1) Light cannot be flooded from source block to neighbor.
                // (2) Neighbor light level is greater or equal to self.
                if !Lights::can_enter(&source_transparency, &n_transparency, ox, oy, oz)
                    || (if is_sunlight {
                        space.get_sunlight(nvx, nvy, nvz)
                    } else {
                        space.get_torch_light(nvx, nvy, nvz, color)
                    } >= next_level)
                {
                    continue;
                }

                if is_sunlight {
                    space.set_sunlight(nvx, nvy, nvz, next_level);
                } else {
                    space.set_torch_light(nvx, nvy, nvz, next_level, color);
                }

                queue.push_back(LightNode {
                    voxel: next_voxel,
                    level: next_level,
                });
            }
        }
    }

    pub fn remove_light(
        space: &mut dyn VoxelAccess,
        voxel: &Vec3<i32>,
        color: &LightColor,
        config: &WorldConfig,
        registry: &Registry,
    ) {
        let max_height = config.max_height as i32;
        let max_light_level = config.max_light_level;

        let mut fill = VecDeque::<LightNode>::new();
        let mut queue = VecDeque::<LightNode>::new();

        let is_sunlight = *color == LightColor::Sunlight;
        let &Vec3(vx, vy, vz) = voxel;

        queue.push_back(LightNode {
            voxel: [vx, vy, vz],
            level: if is_sunlight {
                space.get_sunlight(vx, vy, vz)
            } else {
                space.get_torch_light(vx, vy, vz, color)
            },
        });

        if is_sunlight {
            space.set_sunlight(vx, vy, vz, 0);
        } else {
            space.set_torch_light(vx, vy, vz, 0, color);
        }

        while !queue.is_empty() {
            let LightNode { voxel, level } = queue.pop_front().unwrap();
            let [vx, vy, vz] = voxel;

            for [ox, oy, oz] in VOXEL_NEIGHBORS.into_iter() {
                let nvy = vy + oy;

                if nvy < 0 || nvy >= max_height {
                    continue;
                }

                let nvx = vx + ox;
                let nvz = vz + oz;
                let n_block = registry.get_block_by_id(space.get_voxel(nvx, nvy, nvz));
                let rotation = space.get_voxel_rotation(nvx, nvy, nvz);
                let n_transparency = n_block.get_rotated_transparency(&rotation);

                // if the neighboring block doesn't allow light, then it wouldn't be a potential light entrance.
                if !Lights::can_enter_into(&n_transparency, ox, oy, oz) {
                    continue;
                }

                let n_voxel = [nvx, nvy, nvz];
                let nl = if is_sunlight {
                    space.get_sunlight(nvx, nvy, nvz)
                } else {
                    space.get_torch_light(nvx, nvy, nvz, color)
                };

                if nl == 0 {
                    continue;
                }

                // if level is less, or if sunlight is propagating downwards without stopping
                if nl < level
                    || (is_sunlight
                        && oy == -1
                        && level == max_light_level
                        && nl == max_light_level)
                {
                    queue.push_back(LightNode {
                        voxel: n_voxel,
                        level: nl,
                    });

                    if is_sunlight {
                        space.set_sunlight(nvx, nvy, nvz, 0);
                    } else {
                        space.set_torch_light(nvx, nvy, nvz, 0, color);
                    }
                } else if if is_sunlight && oy == -1 {
                    nl > level
                } else {
                    nl >= level
                } {
                    fill.push_back(LightNode {
                        voxel: n_voxel,
                        level: nl,
                    })
                }
            }
        }

        Lights::flood_light(space, fill, color, registry, config, None, None);
    }

    pub fn propagate(
        space: &mut dyn VoxelAccess,
        min: &Vec3<i32>,
        center: &Vec2<i32>,
        shape: &Vec3<usize>,
        registry: &Registry,
        config: &WorldConfig,
    ) -> Ndarray<u32> {
        let &WorldConfig {
            max_height,
            max_light_level,
            ..
        } = config;

        let mut red_light_queue = VecDeque::<LightNode>::new();
        let mut green_light_queue = VecDeque::<LightNode>::new();
        let mut blue_light_queue = VecDeque::<LightNode>::new();
        let mut sunlight_queue = VecDeque::<LightNode>::new();

        let Vec3(start_x, _, start_z) = min;
        let shape = Vec3(shape.0 as i32, shape.1 as i32, shape.2 as i32);

        let mut mask = vec![];
        for _ in 0..(shape.0 * shape.2) {
            mask.push(max_light_level);
        }

        for y in (0..max_height as i32).rev() {
            for x in 0..shape.0 {
                for z in 0..shape.2 {
                    let index = (x + z * shape.2) as usize;

                    let id = space.get_voxel(x + start_x, y, z + start_z);
                    let &Block {
                        is_px_transparent,
                        is_py_transparent,
                        is_pz_transparent,
                        is_nx_transparent,
                        is_ny_transparent,
                        is_nz_transparent,
                        is_opaque,
                        is_light,
                        red_light_level,
                        green_light_level,
                        blue_light_level,
                        ..
                    } = registry.get_block_by_id(id);

                    let [px, py, pz, nx, ny, nz] = space
                        .get_voxel_rotation(x + start_x, y, z + start_z)
                        .rotate_transparency([
                            is_px_transparent,
                            is_py_transparent,
                            is_pz_transparent,
                            is_nx_transparent,
                            is_ny_transparent,
                            is_nz_transparent,
                        ]);

                    if is_opaque {
                        mask[index] = 0;
                    } else {
                        // Let sunlight pass through if it can.
                        if py && ny {
                            space.set_sunlight(x + start_x, y, z + start_z, mask[index]);

                            if mask[index] == max_light_level {
                                if (x < shape.0 - 1
                                    && mask[(x + 1 + z * shape.2) as usize] == 0
                                    && px)
                                    || (x > 0 && mask[(x - 1 + z * shape.2) as usize] == 0 && nx)
                                    || (z < shape.2 - 1
                                        && mask[(x + (z + 1) * shape.2) as usize] == 0
                                        && pz)
                                    || (z > 0 && mask[(x + (z - 1) * shape.2) as usize] == 0 && nz)
                                {
                                    space.set_sunlight(
                                        x + start_x,
                                        y,
                                        z + start_z,
                                        max_light_level - 1,
                                    );
                                    sunlight_queue.push_back(LightNode {
                                        level: max_light_level - 1,
                                        voxel: [start_x + x, y, start_z + z],
                                    });
                                }
                            }
                        } else {
                            mask[index] = 0;
                        }
                    }

                    if is_light {
                        if red_light_level > 0 {
                            space.set_red_light(x + start_x, y, z + start_z, red_light_level);
                            red_light_queue.push_back(LightNode {
                                voxel: [x + start_x, y, z + start_z],
                                level: red_light_level,
                            });
                        }
                        if green_light_level > 0 {
                            space.set_green_light(x + start_x, y, z + start_z, green_light_level);
                            green_light_queue.push_back(LightNode {
                                voxel: [x + start_x, y, z + start_z],
                                level: green_light_level,
                            });
                        }
                        if blue_light_level > 0 {
                            space.set_blue_light(x + start_x, y, z + start_z, blue_light_level);
                            blue_light_queue.push_back(LightNode {
                                voxel: [x + start_x, y, z + start_z],
                                level: blue_light_level,
                            });
                        }
                    }
                }
            }
        }

        let shape = Vec3(shape.0 as usize, shape.1 as usize, shape.2 as usize);

        if !red_light_queue.is_empty() {
            Lights::flood_light(
                space,
                red_light_queue,
                &RED,
                registry,
                config,
                Some(min),
                Some(&shape),
            );
        }

        if !green_light_queue.is_empty() {
            Lights::flood_light(
                space,
                green_light_queue,
                &GREEN,
                registry,
                config,
                Some(min),
                Some(&shape),
            );
        }

        if !blue_light_queue.is_empty() {
            Lights::flood_light(
                space,
                blue_light_queue,
                &BLUE,
                registry,
                config,
                Some(min),
                Some(&shape),
            );
        }

        if !sunlight_queue.is_empty() {
            Lights::flood_light(
                space,
                sunlight_queue,
                &SUNLIGHT,
                registry,
                config,
                Some(min),
                Some(&shape),
            );
        }

        space.get_lights(center.0, center.1).unwrap().to_owned()
    }

    /// Check to see if light can go "into" one block, disregarding the source.
    pub fn can_enter_into(target: &[bool; 6], dx: i32, dy: i32, dz: i32) -> bool {
        if (dx + dy + dz).abs() != 1 {
            panic!("This isn't supposed to happen. Light neighboring direction should be on 1 axis only.");
        }

        let &[px, py, pz, nx, ny, nz] = target;

        // Going into the NX of the target.
        if dx == 1 {
            return nx;
        }

        // Going into the PX of the target.
        if dx == -1 {
            return px;
        }

        // Going into the NY of the target.
        if dy == 1 {
            return ny;
        }

        // Going into the PY of the target.
        if dy == -1 {
            return py;
        }

        // Going into the NZ of the target.
        if dz == 1 {
            return nz;
        }

        // Going into the PZ of the target.
        pz
    }

    /// Check to see if light can enter from one block to another.
    pub fn can_enter(source: &[bool; 6], target: &[bool; 6], dx: i32, dy: i32, dz: i32) -> bool {
        if (dx + dy + dz).abs() != 1 {
            panic!("This isn't supposed to happen. Light neighboring direction should be on 1 axis only.");
        }

        let &[spx, spy, spz, snx, sny, snz] = source;
        let &[tpx, tpy, tpz, tnx, tny, tnz] = target;

        // Going from PX of source to NX of target
        if dx == 1 {
            return spx && tnx;
        }

        // Going from NX of source to PX of target
        if dx == -1 {
            return snx && tpx;
        }

        // Going from PY of source to NY of target
        if dy == 1 {
            return spy && tny;
        }

        // Going from NY of source to PY of target
        if dy == -1 {
            return sny && tpy;
        }

        // Going from PZ of source to NZ of target
        if dz == 1 {
            return spz && tnz;
        }

        // Going from NZ of source to PZ of target
        snz && tpz
    }
}