Documentation
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
use std::{
    collections::HashMap,
    ops::{Index, IndexMut},
};

use fmc::{
    blocks::{BlockFace, BlockId, BlockPosition, BlockRotation, BlockState, Blocks},
    prelude::*,
    world::{BlockUpdate, ChangedBlockEvent},
};

pub(super) struct WaterPlugin;
impl Plugin for WaterPlugin {
    fn build(&self, app: &mut App) {
        app.insert_resource(WaterUpdateTimer(Timer::new(
            std::time::Duration::from_millis(200),
            TimerMode::Repeating,
        )))
        .add_systems(Startup, setup)
        .add_systems(Update, spread_water);
    }
}

fn setup(mut commands: Commands, blocks: Res<Blocks>) {
    let mut water = Water::default();
    water.add(
        WaterBlock {
            corners: [
                WaterLevel::Nine,
                WaterLevel::Nine,
                WaterLevel::Nine,
                WaterLevel::Nine,
            ],
            is_source: false,
        },
        vec![
            blocks.get_id("still_water_9"),
            blocks.get_id("still_water_8"),
            blocks.get_id("still_water_7"),
            blocks.get_id("still_water_6"),
            blocks.get_id("still_water_5"),
            blocks.get_id("still_water_4"),
            blocks.get_id("still_water_3"),
            blocks.get_id("still_water_2"),
            blocks.get_id("still_water_1"),
        ],
    );
    water.add(
        WaterBlock {
            corners: [
                WaterLevel::Eight,
                WaterLevel::Eight,
                WaterLevel::Nine,
                WaterLevel::Nine,
            ],
            is_source: false,
        },
        vec![
            blocks.get_id("straight_water_8"),
            blocks.get_id("straight_water_7"),
            blocks.get_id("straight_water_6"),
            blocks.get_id("straight_water_5"),
            blocks.get_id("straight_water_4"),
            blocks.get_id("straight_water_3"),
            blocks.get_id("straight_water_2"),
            blocks.get_id("straight_water_1"),
        ],
    );

    water.add(
        WaterBlock {
            corners: [
                WaterLevel::Seven,
                WaterLevel::Eight,
                WaterLevel::Nine,
                WaterLevel::Eight,
            ],
            is_source: false,
        },
        vec![
            blocks.get_id("diagonal_water_7"),
            blocks.get_id("diagonal_water_6"),
            blocks.get_id("diagonal_water_5"),
            blocks.get_id("diagonal_water_4"),
            blocks.get_id("diagonal_water_3"),
            blocks.get_id("diagonal_water_2"),
            blocks.get_id("diagonal_water_1"),
        ],
    );

    water.add(
        WaterBlock {
            corners: [
                WaterLevel::Eight,
                WaterLevel::Eight,
                WaterLevel::Nine,
                WaterLevel::Eight,
            ],
            is_source: false,
        },
        vec![
            blocks.get_id("diagonal_water_corner_up_8"),
            blocks.get_id("diagonal_water_corner_up_7"),
            blocks.get_id("diagonal_water_corner_up_6"),
            blocks.get_id("diagonal_water_corner_up_5"),
            blocks.get_id("diagonal_water_corner_up_4"),
            blocks.get_id("diagonal_water_corner_up_3"),
            blocks.get_id("diagonal_water_corner_up_2"),
            blocks.get_id("diagonal_water_corner_up_1"),
        ],
    );

    water.add(
        WaterBlock {
            corners: [
                WaterLevel::Eight,
                WaterLevel::Nine,
                WaterLevel::Nine,
                WaterLevel::Nine,
            ],
            is_source: false,
        },
        vec![
            blocks.get_id("diagonal_water_corner_down_8"),
            blocks.get_id("diagonal_water_corner_down_7"),
            blocks.get_id("diagonal_water_corner_down_6"),
            blocks.get_id("diagonal_water_corner_down_5"),
            blocks.get_id("diagonal_water_corner_down_4"),
            blocks.get_id("diagonal_water_corner_down_3"),
            blocks.get_id("diagonal_water_corner_down_2"),
            blocks.get_id("diagonal_water_corner_down_1"),
        ],
    );

    water.add(
        WaterBlock {
            corners: [
                WaterLevel::Nine,
                WaterLevel::Eight,
                WaterLevel::Nine,
                WaterLevel::Eight,
            ],
            is_source: false,
        },
        vec![
            blocks.get_id("tilted_water_8"),
            blocks.get_id("tilted_water_7"),
            blocks.get_id("tilted_water_6"),
            blocks.get_id("tilted_water_5"),
            blocks.get_id("tilted_water_4"),
            blocks.get_id("tilted_water_3"),
            blocks.get_id("tilted_water_2"),
            blocks.get_id("tilted_water_1"),
        ],
    );

    // Have to add the two source block types manually, as well as still_water_10 because it works
    // the same way as subsurface.
    water.water_to_block.insert(
        WaterBlock {
            corners: [
                WaterLevel::Nine,
                WaterLevel::Nine,
                WaterLevel::Nine,
                WaterLevel::Nine,
            ],
            is_source: true,
        },
        (blocks.get_id("surface_water"), None),
    );
    water.block_to_water.insert(
        (blocks.get_id("surface_water"), None),
        WaterBlock {
            corners: [
                WaterLevel::Nine,
                WaterLevel::Nine,
                WaterLevel::Nine,
                WaterLevel::Nine,
            ],
            is_source: true,
        },
    );

    water.water_to_block.insert(
        WaterBlock {
            corners: [
                WaterLevel::Ten,
                WaterLevel::Ten,
                WaterLevel::Ten,
                WaterLevel::Ten,
            ],
            is_source: true,
        },
        (blocks.get_id("subsurface_water"), None),
    );
    // This is one level lower to mimic surface water, since nothing can spread from level 10
    water.block_to_water.insert(
        (blocks.get_id("subsurface_water"), None),
        WaterBlock {
            corners: [
                WaterLevel::Ten,
                WaterLevel::Ten,
                WaterLevel::Ten,
                WaterLevel::Ten,
            ],
            is_source: true,
        },
    );

    water.water_to_block.insert(
        WaterBlock {
            corners: [
                WaterLevel::Ten,
                WaterLevel::Ten,
                WaterLevel::Ten,
                WaterLevel::Ten,
            ],
            is_source: false,
        },
        (blocks.get_id("still_water_10"), None),
    );
    water.block_to_water.insert(
        (blocks.get_id("still_water_10"), None),
        WaterBlock {
            corners: [
                WaterLevel::Ten,
                WaterLevel::Ten,
                WaterLevel::Ten,
                WaterLevel::Ten,
            ],
            is_source: false,
        },
    );

    // This is for removal of water
    water.water_to_block.insert(
        WaterBlock {
            corners: [
                WaterLevel::Zero,
                WaterLevel::Zero,
                WaterLevel::Zero,
                WaterLevel::Zero,
            ],
            is_source: false,
        },
        (blocks.get_id("air"), None),
    );

    commands.insert_resource(water);
}

#[derive(Resource, Default)]
struct Water {
    water_to_block: HashMap<WaterBlock, (BlockId, Option<BlockState>)>,
    block_to_water: HashMap<(BlockId, Option<BlockState>), WaterBlock>,
}

impl Water {
    #[track_caller]
    fn add(&mut self, mut water_block: WaterBlock, block_ids: Vec<BlockId>) {
        for block_id in block_ids {
            self.water_to_block
                .insert(water_block.clone(), (block_id, None));
            self.block_to_water
                .insert((block_id, None), water_block.clone());

            if water_block[Corner::Left] == water_block[Corner::Right]
                && water_block[Corner::Left] == water_block[Corner::FarRight]
                && water_block[Corner::Left] == water_block[Corner::FarLeft]
            {
                water_block[Corner::Left] = water_block[Corner::Left].decrement();
                water_block[Corner::Right] = water_block[Corner::Right].decrement();
                water_block[Corner::FarRight] = water_block[Corner::FarRight].decrement();
                water_block[Corner::FarLeft] = water_block[Corner::FarLeft].decrement();

                continue;
            }

            for i in 1..4 {
                let rotation = BlockRotation::from(i);
                self.water_to_block.insert(
                    water_block.rotate(rotation),
                    (block_id, Some(BlockState::new().with_rotation(rotation))),
                );
                self.block_to_water.insert(
                    (block_id, Some(BlockState::new().with_rotation(rotation))),
                    water_block.rotate(rotation),
                );
            }

            water_block[Corner::Left] = water_block[Corner::Left].decrement();
            water_block[Corner::Right] = water_block[Corner::Right].decrement();
            water_block[Corner::FarRight] = water_block[Corner::FarRight].decrement();
            water_block[Corner::FarLeft] = water_block[Corner::FarLeft].decrement();
        }
    }
}

#[derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug)]
enum WaterLevel {
    #[default]
    Zero,
    One,
    Two,
    Three,
    Four,
    Five,
    Six,
    Seven,
    Eight,
    Nine,
    Ten,
}

impl WaterLevel {
    #[track_caller]
    fn decrement(self) -> Self {
        match self {
            Self::Zero => unreachable!(),
            Self::One => Self::Zero,
            Self::Two => Self::One,
            Self::Three => Self::Two,
            Self::Four => Self::Three,
            Self::Five => Self::Four,
            Self::Six => Self::Five,
            Self::Seven => Self::Six,
            Self::Eight => Self::Seven,
            Self::Nine => Self::Eight,
            Self::Ten => Self::Nine,
        }
    }
}

#[derive(Debug, Clone, Copy)]
enum Corner {
    Left = 0,
    Right,
    FarRight,
    FarLeft,
}

impl Corner {
    fn rotate(&self, rotation: BlockRotation) -> Self {
        let new = (*self as usize + rotation as usize) % 4;
        match new {
            0 => Corner::Left,
            1 => Corner::Right,
            2 => Corner::FarRight,
            3 => Corner::FarLeft,
            _ => unreachable!(),
        }
    }
}

#[derive(Default, PartialEq, Eq, Hash, Clone, Debug)]
struct WaterBlock {
    corners: [WaterLevel; 4],
    is_source: bool,
}

impl WaterBlock {
    fn rotate(&self, rotation: BlockRotation) -> Self {
        match rotation {
            BlockRotation::Right => WaterBlock {
                corners: [
                    self.corners[Corner::FarLeft as usize],
                    self.corners[Corner::Left as usize],
                    self.corners[Corner::Right as usize],
                    self.corners[Corner::FarRight as usize],
                ],
                is_source: self.is_source,
            },
            BlockRotation::Back => WaterBlock {
                corners: [
                    self.corners[Corner::FarRight as usize],
                    self.corners[Corner::FarLeft as usize],
                    self.corners[Corner::Left as usize],
                    self.corners[Corner::Right as usize],
                ],
                is_source: self.is_source,
            },
            BlockRotation::Left => WaterBlock {
                corners: [
                    self.corners[Corner::Right as usize],
                    self.corners[Corner::FarRight as usize],
                    self.corners[Corner::FarLeft as usize],
                    self.corners[Corner::Left as usize],
                ],
                is_source: self.is_source,
            },
            _ => unreachable!(),
        }
    }

    fn update_corner(&mut self, corner: Corner, water_level: WaterLevel) {
        if self.corners[corner as usize] == water_level
            || self.corners[corner as usize] == WaterLevel::Ten
        {
            return;
        }

        self.corners[corner as usize] = water_level;

        if self[corner.rotate(BlockRotation::Right)] < self[corner] {
            self[corner.rotate(BlockRotation::Right)] = self[corner].decrement();
        }

        if self[corner.rotate(BlockRotation::Left)] < self[corner] {
            self[corner.rotate(BlockRotation::Left)] = self[corner].decrement();
        }

        if self[corner.rotate(BlockRotation::Back)] < self[corner.rotate(BlockRotation::Right)]
            || self[corner.rotate(BlockRotation::Back)] < self[corner.rotate(BlockRotation::Left)]
        {
            self[corner.rotate(BlockRotation::Back)] = self[corner.rotate(BlockRotation::Right)]
                .max(self[corner.rotate(BlockRotation::Left)])
                .decrement();
        }
    }
}

impl Index<Corner> for WaterBlock {
    type Output = WaterLevel;
    fn index(&self, index: Corner) -> &Self::Output {
        &self.corners[index as usize]
    }
}

impl IndexMut<Corner> for WaterBlock {
    fn index_mut(&mut self, index: Corner) -> &mut Self::Output {
        &mut self.corners[index as usize]
    }
}

const TEN: WaterBlock = WaterBlock {
    corners: [
        WaterLevel::Ten,
        WaterLevel::Ten,
        WaterLevel::Ten,
        WaterLevel::Ten,
    ],
    is_source: false,
};

#[derive(Debug)]
struct ChangedBlockAsWater {
    pub to: Option<WaterBlock>,
    pub top: Option<WaterBlock>,
    pub bottom: Option<WaterBlock>,
    pub back: Option<WaterBlock>,
    pub back_right: Option<WaterBlock>,
    pub back_left: Option<WaterBlock>,
    pub right: Option<WaterBlock>,
    pub left: Option<WaterBlock>,
    pub front: Option<WaterBlock>,
    pub front_right: Option<WaterBlock>,
    pub front_left: Option<WaterBlock>,
}

impl ChangedBlockAsWater {
    fn new(changed_block: &ChangedBlockEvent, water: &Res<Water>) -> Self {
        Self {
            to: water.block_to_water.get(&changed_block.to).cloned(),
            top: changed_block
                .top
                .as_ref()
                .and_then(|top| water.block_to_water.get(top).cloned()),
            bottom: changed_block
                .bottom
                .as_ref()
                .and_then(|bottom| water.block_to_water.get(bottom).cloned()),
            back: changed_block
                .back
                .as_ref()
                .and_then(|back| water.block_to_water.get(back).cloned()),
            back_right: changed_block
                .back_right
                .as_ref()
                .and_then(|back_right| water.block_to_water.get(back_right).cloned()),
            back_left: changed_block
                .back_left
                .as_ref()
                .and_then(|back_left| water.block_to_water.get(back_left).cloned()),
            right: changed_block
                .right
                .as_ref()
                .and_then(|right| water.block_to_water.get(right).cloned()),
            left: changed_block
                .left
                .as_ref()
                .and_then(|left| water.block_to_water.get(left).cloned()),
            front: changed_block
                .front
                .as_ref()
                .and_then(|front| water.block_to_water.get(front).cloned()),
            front_right: changed_block
                .front_right
                .as_ref()
                .and_then(|front_right| water.block_to_water.get(front_right).cloned()),
            front_left: changed_block
                .front_left
                .as_ref()
                .and_then(|front_left| water.block_to_water.get(front_left).cloned()),
        }
    }
}

impl Index<BlockFace> for ChangedBlockAsWater {
    type Output = Option<WaterBlock>;
    fn index(&self, index: BlockFace) -> &Self::Output {
        match index {
            BlockFace::Front => &self.front,
            BlockFace::Back => &self.back,
            BlockFace::Right => &self.right,
            BlockFace::Left => &self.left,
            BlockFace::Top => &self.top,
            BlockFace::Bottom => &self.bottom,
        }
    }
}

impl Index<[BlockFace; 2]> for ChangedBlockAsWater {
    type Output = Option<WaterBlock>;
    #[track_caller]
    fn index(&self, index: [BlockFace; 2]) -> &Self::Output {
        match index {
            [BlockFace::Front, BlockFace::Left] => &self.front_left,
            [BlockFace::Left, BlockFace::Front] => &self.front_left,
            [BlockFace::Front, BlockFace::Right] => &self.front_right,
            [BlockFace::Right, BlockFace::Front] => &self.front_right,
            [BlockFace::Back, BlockFace::Left] => &self.back_left,
            [BlockFace::Left, BlockFace::Back] => &self.back_left,
            [BlockFace::Back, BlockFace::Right] => &self.back_right,
            [BlockFace::Right, BlockFace::Back] => &self.back_right,
            _ => panic!("Tried to index with non-horizontal blockfaces."),
        }
    }
}

#[derive(Resource, DerefMut, Deref)]
struct WaterUpdateTimer(Timer);

//// TODO: I want waterfalls, but there is currently no way to know which water blocks should spread
//// in a new chunk, and checking all of them would be too expensive... Maybe generate with a dummy
//// block that can use it's spawn function to trigger something.
//// This also makes for silly looking reverse moon pools when caves generate into a body of water.
fn spread_water(
    water: Res<Water>,
    time: Res<Time>,
    mut update_timer: ResMut<WaterUpdateTimer>,
    mut changed_blocks: EventReader<ChangedBlockEvent>,
    mut block_updates: EventWriter<BlockUpdate>,
    mut updates: Local<HashMap<BlockPosition, WaterBlock>>,
) {
    let blocks = Blocks::get();
    let air = blocks.get_id("air");

    for changed_block in changed_blocks.read() {
        // If there's an update waiting to be sent, but the block is changed, the update is stale
        updates.remove(&changed_block.position);

        let change_as_water = ChangedBlockAsWater::new(changed_block, &water);

        let mut water_block = if let Some(to) = &change_as_water.to {
            if to.is_source {
                to.clone()
            } else {
                let new_max = to.corners.iter().max().unwrap().decrement();
                WaterBlock {
                    corners: [
                        to[Corner::Left].min(new_max),
                        to[Corner::Right].min(new_max),
                        to[Corner::FarRight].min(new_max),
                        to[Corner::FarLeft].min(new_max),
                    ],
                    is_source: false,
                }
            }
        } else {
            WaterBlock::default()
        };

        if change_as_water.top.is_some() {
            water_block.corners = [
                WaterLevel::Nine,
                WaterLevel::Nine,
                WaterLevel::Nine,
                WaterLevel::Nine,
            ]
        } else if !water_block.is_source {
            for (corner, block_faces) in [
                (Corner::Left, [BlockFace::Left, BlockFace::Front]),
                (Corner::Left, [BlockFace::Front, BlockFace::Left]),
                (Corner::Right, [BlockFace::Right, BlockFace::Front]),
                (Corner::Right, [BlockFace::Front, BlockFace::Right]),
                (Corner::FarRight, [BlockFace::Right, BlockFace::Back]),
                (Corner::FarRight, [BlockFace::Back, BlockFace::Right]),
                (Corner::FarLeft, [BlockFace::Left, BlockFace::Back]),
                (Corner::FarLeft, [BlockFace::Back, BlockFace::Left]),
            ] {
                if let Some(adjacent_water_block) = &change_as_water[block_faces[0]] {
                    let (corner_one, corner_two) = match (corner, block_faces[0]) {
                        (Corner::Left, BlockFace::Left) => (Corner::Right, Corner::Left),
                        (Corner::Left, BlockFace::Front) => (Corner::FarLeft, Corner::Left),
                        (Corner::Right, BlockFace::Right) => (Corner::Left, Corner::Right),
                        (Corner::Right, BlockFace::Front) => (Corner::FarRight, Corner::Right),
                        (Corner::FarRight, BlockFace::Right) => (Corner::FarLeft, Corner::FarRight),
                        (Corner::FarRight, BlockFace::Back) => (Corner::Right, Corner::FarRight),
                        (Corner::FarLeft, BlockFace::Left) => (Corner::FarRight, Corner::FarLeft),
                        (Corner::FarLeft, BlockFace::Back) => (Corner::Left, Corner::FarLeft),
                        _ => unreachable!(),
                    };
                    if adjacent_water_block.is_source {
                        water_block.update_corner(corner, WaterLevel::Nine);
                    } else {
                        water_block.update_corner(
                            corner,
                            water_block[corner]
                                .max(adjacent_water_block[corner_one].decrement())
                                .max(adjacent_water_block[corner_two].decrement()),
                        );
                    }
                }

                if change_as_water[block_faces[0]].is_some()
                    && change_as_water[block_faces].is_some()
                {
                    let diagonal_water_block = change_as_water[block_faces].as_ref().unwrap();
                    let (corner_near, corner_far) = match block_faces {
                        // left corner
                        [BlockFace::Left, BlockFace::Front] => (Corner::FarRight, Corner::Right),
                        [BlockFace::Front, BlockFace::Left] => (Corner::FarLeft, Corner::FarRight),
                        // right corner
                        [BlockFace::Right, BlockFace::Front] => (Corner::FarLeft, Corner::Left),
                        [BlockFace::Front, BlockFace::Right] => (Corner::FarLeft, Corner::FarRight),
                        // far right corner
                        [BlockFace::Right, BlockFace::Back] => (Corner::FarLeft, Corner::Left),
                        [BlockFace::Back, BlockFace::Right] => (Corner::Left, Corner::Right),
                        // far left corner
                        [BlockFace::Left, BlockFace::Back] => (Corner::FarRight, Corner::Right),
                        [BlockFace::Back, BlockFace::Left] => (Corner::Left, Corner::Right),
                        _ => unreachable!(),
                    };
                    if diagonal_water_block.is_source {
                        water_block.update_corner(corner, WaterLevel::Nine);
                    } else {
                        water_block.update_corner(
                            corner,
                            water_block[corner]
                                .max(diagonal_water_block[corner_near].decrement())
                                .max(diagonal_water_block[corner_far].decrement()),
                        );
                    }
                }
            }
        }

        if water_block != WaterBlock::default()
            && water_block.corners.iter().any(|c| c == &WaterLevel::Zero)
        {
            water_block.corners = WaterBlock::default().corners;
        }

        if water_block == WaterBlock::default() && change_as_water.to.is_none() {
            continue;
        }

        for block_face in [
            BlockFace::Front,
            BlockFace::Right,
            BlockFace::Back,
            BlockFace::Left,
        ] {
            //let (orthogonal_one, orthogonal_two) = match block_face {
            //    BlockFace::Front | BlockFace::Back => (BlockFace::Right, BlockFace::Left),
            //    BlockFace::Left | BlockFace::Right => (BlockFace::Front, BlockFace::Back),
            //    _ => unreachable!(),
            //};
            let to_corners = match block_face {
                BlockFace::Left => [Corner::Right, Corner::FarRight],
                BlockFace::Right => [Corner::Left, Corner::FarLeft],
                BlockFace::Front => [Corner::FarLeft, Corner::FarRight],
                BlockFace::Back => [Corner::Left, Corner::Right],
                _ => unreachable!(),
            };
            let from_corners = match block_face {
                BlockFace::Left => [Corner::Left, Corner::FarLeft],
                BlockFace::Right => [Corner::Right, Corner::FarRight],
                BlockFace::Front => [Corner::Left, Corner::Right],
                BlockFace::Back => [Corner::FarLeft, Corner::FarRight],
                _ => unreachable!(),
            };
            let position = block_face.shift_position(changed_block.position);

            if let Some(adjacent_water_block) = &change_as_water[block_face] {
                //if change_as_water.to.is_some()
                //    || (change_as_water[orthogonal_one].is_some()
                //        && change_as_water[[orthogonal_one, block_face]].is_some())
                //    || (change_as_water[orthogonal_two].is_some()
                //        && change_as_water[[orthogonal_two, block_face]].is_some())
                //{
                let mut update = updates
                    .get(&position)
                    .unwrap_or(adjacent_water_block)
                    .clone();
                update.update_corner(to_corners[0], water_block[from_corners[0]]);
                update.update_corner(to_corners[1], water_block[from_corners[1]]);

                if &update != adjacent_water_block {
                    updates.insert(position, update);
                }
                //}
            } else if changed_block[block_face].is_some_and(|b| b.0 == air)
                && changed_block.bottom.is_some_and(|b| b.0 != air)
                && change_as_water.bottom.is_none()
                && water_block[from_corners[0]] > WaterLevel::One
                && water_block[from_corners[1]] > WaterLevel::One
            {
                let update = updates.entry(position).or_insert(WaterBlock::default());
                if update != &TEN {
                    update.update_corner(to_corners[0], water_block[from_corners[0]]);
                    update.update_corner(to_corners[1], water_block[from_corners[1]]);
                }
            }
        }

        for (block_faces, corner_to, corner_from) in [
            (
                [BlockFace::Front, BlockFace::Left],
                Corner::FarRight,
                Corner::Left,
            ),
            (
                [BlockFace::Front, BlockFace::Right],
                Corner::FarLeft,
                Corner::Right,
            ),
            (
                [BlockFace::Back, BlockFace::Right],
                Corner::Left,
                Corner::FarRight,
            ),
            (
                [BlockFace::Back, BlockFace::Left],
                Corner::Right,
                Corner::FarLeft,
            ),
        ] {
            if let Some(diagonal_water_block) = &change_as_water[block_faces] {
                if change_as_water[block_faces[0]].is_some()
                    || change_as_water[block_faces[1]].is_some()
                {
                    let position = block_faces[0]
                        .shift_position(block_faces[1].shift_position(changed_block.position));
                    let mut update = updates
                        .get(&position)
                        .unwrap_or(diagonal_water_block)
                        .clone();
                    update.update_corner(corner_to, water_block[corner_from]);
                    if &update != diagonal_water_block {
                        updates.insert(position, update);
                    }
                }
            }
        }

        if let Some(bottom) = &change_as_water.bottom {
            if water_block == WaterBlock::default() {
                updates.insert(
                    changed_block.position - IVec3::Y,
                    WaterBlock {
                        corners: [
                            WaterLevel::Nine,
                            WaterLevel::Nine,
                            WaterLevel::Nine,
                            WaterLevel::Nine,
                        ],
                        is_source: bottom.is_source,
                    },
                );
            }
        }

        if changed_block.bottom.is_some_and(|block| block.0 == air) {
            updates.insert(changed_block.position - IVec3::Y, TEN.clone());
        } else if let Some(bottom) = change_as_water.bottom {
            if bottom.corners != TEN.corners {
                let mut new = TEN.clone();
                new.is_source = bottom.is_source;
                updates.insert(changed_block.position - IVec3::Y, new);
            }
        }

        if changed_block.to.0 == air
            || change_as_water
                .to
                .is_some_and(|to| to != water_block && to.corners != TEN.corners)
        {
            if change_as_water.top.is_some() {
                updates.insert(changed_block.position, TEN.clone());
            } else {
                updates.insert(changed_block.position, water_block);
            }
        }
    }

    update_timer.tick(time.delta());
    if update_timer.just_finished() {
        block_updates.send_batch(updates.drain().filter_map(|(position, water_block)| {
            // TODO: The idea is that it's not supposed to generate invalid water states, but it
            // does often when trying to remove the water at edges. Ending up with states like
            // [Zero, Zero, One, One] and variations. This is probably what introduces the
            // flickering that sometimes happen.
            //let (block_id, block_state) = water.water_to_block[&water_block];
            let (block_id, block_state) = match water.water_to_block.get(&water_block) {
                Some(k) => k.clone(),
                None => (air, None),
            };
            Some(BlockUpdate::Replace {
                position,
                block_id,
                block_state,
                block_data: None,
            })
        }));
    }
}