diff options
| author | Kai Stevenson <kai@kaistevenson.com> | 2026-08-11 20:41:41 -0700 |
|---|---|---|
| committer | Kai Stevenson <kai@kaistevenson.com> | 2026-08-11 20:41:41 -0700 |
| commit | 7ce9781f86c56f932dabc3bc84b8d21d7be8fecc (patch) | |
| tree | 25bd371970910934a1957a09e2e84936021d73af /src/sim/materials | |
| parent | 6f67586b8fc6efdb86d0c9a9744c546da97c4dab (diff) | |
refactor sim loop
Diffstat (limited to 'src/sim/materials')
| -rw-r--r-- | src/sim/materials/mod.rs | 60 | ||||
| -rw-r--r-- | src/sim/materials/sand.rs | 10 | ||||
| -rw-r--r-- | src/sim/materials/water.rs | 78 |
3 files changed, 148 insertions, 0 deletions
diff --git a/src/sim/materials/mod.rs b/src/sim/materials/mod.rs new file mode 100644 index 0000000..410236d --- /dev/null +++ b/src/sim/materials/mod.rs @@ -0,0 +1,60 @@ +use crate::sim::sim::UpdateCtx; + +mod sand; +mod water; + +#[repr(u8)] +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub enum MaterialId { + Void = 0, + Sand, + Wood, + Water, +} + +pub struct MaterialDef { + pub name: &'static str, + pub color: [u8; 4], + pub density: u8, + pub sim_update: Option<fn(ctx: &mut UpdateCtx) -> ()>, +} + +static MATERIALS: [MaterialDef; 4] = [ + MaterialDef { + name: "Void", + color: [0x00, 0x00, 0x00, 0xFF], + density: 0, + sim_update: None, + }, + MaterialDef { + name: "Sand", + color: [0xDE, 0xCB, 0x85, 0xFF], + density: 50, + sim_update: Some(sand::sim_update), + }, + MaterialDef { + name: "Wood", + color: [0x85, 0x56, 0x1D, 0xFF], + density: 50, + sim_update: None, + }, + MaterialDef { + name: "Water", + color: [0x38, 0xA9, 0xFF, 0xFF], + density: 40, + sim_update: Some(water::sim_update), + }, +]; + +impl MaterialId { + pub const ALL: [MaterialId; 4] = [ + MaterialId::Void, + MaterialId::Sand, + MaterialId::Wood, + MaterialId::Water, + ]; + #[inline] + pub fn def(self) -> &'static MaterialDef { + &MATERIALS[self as usize] + } +} diff --git a/src/sim/materials/sand.rs b/src/sim/materials/sand.rs new file mode 100644 index 0000000..6a780f3 --- /dev/null +++ b/src/sim/materials/sand.rs @@ -0,0 +1,10 @@ +use crate::sim::sim::UpdateCtx; + +#[inline] +pub fn sim_update(ctx: &mut UpdateCtx) { + 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), + ]); +} diff --git a/src/sim/materials/water.rs b/src/sim/materials/water.rs new file mode 100644 index 0000000..d875aee --- /dev/null +++ b/src/sim/materials/water.rs @@ -0,0 +1,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); +} |
