From 6350e4ffca8ce1e46465284ef3d7559e0f40229b Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Sat, 22 Aug 2026 13:54:00 -0700 Subject: fixes and refactors --- src/sim/cell/cell.rs | 4 ++-- src/sim/cell/materials/fire.rs | 17 +++++++---------- src/sim/cell/materials/gas.rs | 22 +++++++++++++--------- src/sim/cell/materials/liquid.rs | 25 ++++++++++++------------- src/sim/cell/materials/mod.rs | 4 ++-- src/sim/cell/materials/powder.rs | 8 ++++---- 6 files changed, 40 insertions(+), 40 deletions(-) (limited to 'src/sim/cell') diff --git a/src/sim/cell/cell.rs b/src/sim/cell/cell.rs index edb2402..054ac62 100644 --- a/src/sim/cell/cell.rs +++ b/src/sim/cell/cell.rs @@ -1,4 +1,4 @@ -use crate::sim::cell::materials::MaterialId; +use crate::{config::SETTLED_THRESOHLD, sim::cell::materials::MaterialId}; #[derive(Clone, Copy)] pub struct Cell { @@ -60,7 +60,7 @@ impl Cell { #[inline] pub fn increment_settled(&mut self) { let s = self.settled(); - if s < 7 { + if s < SETTLED_THRESOHLD { self.set_settled(s + 1); } } diff --git a/src/sim/cell/materials/fire.rs b/src/sim/cell/materials/fire.rs index 2a1225e..8667cea 100644 --- a/src/sim/cell/materials/fire.rs +++ b/src/sim/cell/materials/fire.rs @@ -2,7 +2,7 @@ use rand::RngExt; use crate::sim::{ cell::{cell::Cell, materials::MaterialId}, - cell_sim::sim::UpdateCtx, + cell_sim::sim::{PostUpdateAction, UpdateCtx}, }; pub trait FireCellView { @@ -27,21 +27,16 @@ impl FireCellView for Cell { // TODO optimize number of rng calls? // TODO wtf is fire #[inline] -pub fn sim_update(ctx: &mut UpdateCtx) { +pub fn sim_update(ctx: &mut UpdateCtx) -> PostUpdateAction { let ticks_lived = ctx.cell.get_ticks_lived(); // 2 seconds if ticks_lived > 240 { // we have a chance to live longer--roughly ?% chance of living an extra second if ctx.rng.random_range(0.0..1.0) > 0.9 { // kill ourselves, with a chance to turn into ash - if ctx.rng.random_range(0.0..1.0) > 0.8 { - // TODO ash material - // ctx.set_cell(0, 0, Cell::from_material(MaterialId::Sand)); - } else { - ctx.do_rewrite = false; - ctx.set_cell(0, 0, Cell::void()); - } - return; + ctx.set_cell(0, 0, Cell::void()); + // the updater should take no action since we already killed ourselves + return PostUpdateAction::None; } } @@ -77,4 +72,6 @@ pub fn sim_update(ctx: &mut UpdateCtx) { } ctx.cell.set_ticks_lived(ticks_lived + 1); + // apply our new lifespan + PostUpdateAction::Apply } diff --git a/src/sim/cell/materials/gas.rs b/src/sim/cell/materials/gas.rs index 2ee7c00..af8f4fd 100644 --- a/src/sim/cell/materials/gas.rs +++ b/src/sim/cell/materials/gas.rs @@ -1,20 +1,24 @@ use rand::RngExt; -use crate::sim::cell_sim::sim::UpdateCtx; +use crate::sim::cell_sim::sim::{PostUpdateAction, UpdateCtx}; #[inline] -pub fn sim_update(ctx: &mut UpdateCtx) { +pub fn sim_update(ctx: &mut UpdateCtx) -> PostUpdateAction { // only allow upward movement some of the time to limit movement speed - if ctx.rng.random_range(0.0..1.0) > 0.8 && ctx.candidates_swap(&[(0, -1)]) { - return; + if ctx.rng.random_range(0.0..1.0) > 0.8 + && ctx.swap_or_settle(&[(0, -1)]) == PostUpdateAction::None + { + return PostUpdateAction::None; } // same for each horizontal direction if ctx.rng.random_range(0.0..1.0) > 0.8 - && ctx.candidates_swap(&[(1 - ctx.seqno_parity as i32 * 2, 0)]) + && ctx.swap_or_settle(&[(1 - ctx.seqno_parity as i32 * 2, 0)]) == PostUpdateAction::None { - return; + return PostUpdateAction::None; } - if ctx.rng.random_range(0.0..1.0) > 0.8 - && ctx.candidates_swap(&[(-1 + ctx.seqno_parity as i32 * 2, 0)]) - {} + if ctx.rng.random_range(0.0..1.0) > 0.8 { + return ctx.swap_or_settle(&[(-1 + ctx.seqno_parity as i32 * 2, 0)]); + } + + PostUpdateAction::Apply } diff --git a/src/sim/cell/materials/liquid.rs b/src/sim/cell/materials/liquid.rs index 4b848fb..bff0ac0 100644 --- a/src/sim/cell/materials/liquid.rs +++ b/src/sim/cell/materials/liquid.rs @@ -1,14 +1,15 @@ -use crate::sim::cell_sim::sim::UpdateCtx; +use crate::sim::cell_sim::sim::{PostUpdateAction, UpdateCtx}; #[inline] -pub fn sim_update(ctx: &mut UpdateCtx) { +pub fn sim_update(ctx: &mut UpdateCtx) -> PostUpdateAction { // if the water can fall, do so - if ctx.candidates_swap(&[ + if ctx.swap_or_settle(&[ (0, 1), (-1 + 2 * ctx.seqno_parity as i32, 1), (1 - 2 * ctx.seqno_parity as i32, 1), - ]) { - return; + ]) == PostUpdateAction::None + { + return PostUpdateAction::None; } // if the water can't fall, check if we can move left or right @@ -22,18 +23,16 @@ pub fn sim_update(ctx: &mut UpdateCtx) { // we can't move down or to other side, so we're stuck if !can_move_left && !can_move_right { - return; + return PostUpdateAction::Settle; } // if we can't move left, just move right if !can_move_left { - ctx.candidates_swap(&[(1, 0)]); - return; + return ctx.swap_or_settle(&[(1, 0)]); } // and vice versa if !can_move_right { - ctx.candidates_swap(&[(-1, 0)]); - return; + return ctx.swap_or_settle(&[(-1, 0)]); } // find the closest hole within 20 pixels (TODO optimize) @@ -54,12 +53,12 @@ pub fn sim_update(ctx: &mut UpdateCtx) { { // we identified a hole and we know that the space on this side is open // move toward the hole - // new_target.flags = new_target.flags ^ 0b1; - ctx.candidates_swap(&[(side, 0)]); - return; + return ctx.swap_or_settle(&[(side, 0)]); } } + return PostUpdateAction::Settle; + // we didn't find a hole, so just move "randomly" on the same surface // TODO when to settle? // let target_x = if !can_move_left { diff --git a/src/sim/cell/materials/mod.rs b/src/sim/cell/materials/mod.rs index e2b5fc6..ee2b4d0 100644 --- a/src/sim/cell/materials/mod.rs +++ b/src/sim/cell/materials/mod.rs @@ -1,4 +1,4 @@ -use crate::sim::cell_sim::sim::UpdateCtx; +use crate::sim::cell_sim::sim::{PostUpdateAction, UpdateCtx}; pub mod fire; pub mod gas; @@ -31,7 +31,7 @@ pub struct MaterialDef { pub color: (u8, u8, u8, u8), pub density: u8, pub form: MaterialForm, - pub sim_update: Option ()>, + pub sim_update: Option PostUpdateAction>, } static MATERIALS: [MaterialDef; 6] = [ diff --git a/src/sim/cell/materials/powder.rs b/src/sim/cell/materials/powder.rs index d2d1265..e08ea1f 100644 --- a/src/sim/cell/materials/powder.rs +++ b/src/sim/cell/materials/powder.rs @@ -1,10 +1,10 @@ -use crate::sim::cell_sim::sim::UpdateCtx; +use crate::sim::cell_sim::sim::{PostUpdateAction, UpdateCtx}; #[inline] -pub fn sim_update(ctx: &mut UpdateCtx) { - ctx.candidates_swap(&[ +pub fn sim_update(ctx: &mut UpdateCtx) -> PostUpdateAction { + ctx.swap_or_settle(&[ (0, 1), (-1 + 2 * ctx.seqno_parity as i32, 1), (1 - 2 * ctx.seqno_parity as i32, 1), - ]); + ]) } -- cgit v1.3.1