summaryrefslogtreecommitdiff
path: root/src/sim/materials/water.rs
blob: d875aee393b7c31c37287454afc83601be037658 (plain)
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
use crate::sim::sim::UpdateCtx;

#[inline]
pub fn sim_update(ctx: &mut UpdateCtx) {
    // if the water can fall, do so
    if ctx.candidates_swap(&[
        (ctx.self_x, ctx.self_y + 1),
        (ctx.self_x - 1 + 2 * ctx.seqno_parity as i32, ctx.self_y + 1),
        (ctx.self_x + 1 - 2 * ctx.seqno_parity as i32, ctx.self_y + 1),
    ]) {
        return;
    }

    // if the water can't fall, check if we can move left or right
    // these are inverted on parity so that we don't preference a direction
    let left_target = ctx.board.cell_at_position(ctx.self_x - 1, ctx.self_y);
    let can_move_left = left_target
        .is_some_and(|c| c.material.def().density < ctx.self_cell.material.def().density);
    let right_target = ctx.board.cell_at_position(ctx.self_x + 1, ctx.self_y);
    let can_move_right = right_target
        .is_some_and(|c| c.material.def().density < ctx.self_cell.material.def().density);

    // we can't move down or to other side, so we're stuck
    if !can_move_left && !can_move_right {
        return;
    }

    // find the closest hole within 20 pixels (TODO optimize)
    // a hole is any space below us with a lesser density
    // prevents equidistance stuck state
    let starting_side = if ctx.seqno_parity == 0 { 1 } else { -1 };
    for i in 0..20 {
        let side = if i % 2 == 0 {
            starting_side
        } else {
            -starting_side
        };
        if (side == 1 && !can_move_right) || (side == -1 && !can_move_left) {
            continue;
        }

        let offset = side * (1 + i / 2);
        let hole_target = ctx
            .board
            .cell_at_position(ctx.self_x + offset, ctx.self_y + 1);
        if let Some(target) = hole_target
            && target.material.def().density < ctx.self_cell.material.def().density
        {
            // we identified a hole and we know that the space on this side is open
            // move toward the hole
            let move_target = if side == 1 { right_target } else { left_target }.clone();
            // new_target.flags = new_target.flags ^ 0b1;
            // safe to unwrap
            ctx.board
                .set_cell_at_position(ctx.self_x, ctx.self_y, move_target.unwrap());
            ctx.board
                .set_cell_at_position(ctx.self_x + side, ctx.self_y, ctx.self_cell);
            return;
        }
    }

    // we didn't find a hole, so just move "randomly" on the same surface
    // TODO when to settle?
    let (target, target_x) = if !can_move_left {
        (right_target, 1)
    } else if !can_move_right {
        (left_target, -1)
    } else if ctx.seqno_parity % 2 == 1 {
        (right_target, 1)
    } else {
        (left_target, -1)
    };

    ctx.board
        .set_cell_at_position(ctx.self_x, ctx.self_y, target.unwrap());
    ctx.board
        .set_cell_at_position(ctx.self_x + target_x, ctx.self_y, ctx.self_cell);
}