diff options
| author | Kai Stevenson <kai@kaistevenson.com> | 2026-08-18 22:22:27 -0700 |
|---|---|---|
| committer | Kai Stevenson <kai@kaistevenson.com> | 2026-08-18 22:22:27 -0700 |
| commit | 2640f91e36558650bb988dcc82b5148ee71f3eb0 (patch) | |
| tree | 5f67c648569109c84736b11b39cb6d2cd6b67e4d /src/sim/cell/materials/gas.rs | |
| parent | 92dd02d7cc4d9fc930760d26d81edbe2197a3fc0 (diff) | |
optimize with settlement counter
Diffstat (limited to 'src/sim/cell/materials/gas.rs')
| -rw-r--r-- | src/sim/cell/materials/gas.rs | 78 |
1 files changed, 11 insertions, 67 deletions
diff --git a/src/sim/cell/materials/gas.rs b/src/sim/cell/materials/gas.rs index c0cd9f9..2ee7c00 100644 --- a/src/sim/cell/materials/gas.rs +++ b/src/sim/cell/materials/gas.rs @@ -1,76 +1,20 @@ +use rand::RngExt; + use crate::sim::cell_sim::sim::UpdateCtx; #[inline] pub fn sim_update(ctx: &mut UpdateCtx) { - // if the water can fall, do so - if ctx.candidates_swap(&[ - (0, -1), - (-1 + 2 * ctx.seqno_parity as i32, -1), - (1 - 2 * ctx.seqno_parity as i32, -1), - ]) { + // 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 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.get_cell(-1, 0); - let can_move_left = - left_target.is_some_and(|c| c.material.def().density < ctx.material.density); - let right_target = ctx.get_cell(1, 0); - let can_move_right = - right_target.is_some_and(|c| c.material.def().density < ctx.material.density); - - // we can't move down or to other side, so we're stuck - if !can_move_left && !can_move_right { + // 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)]) + { return; } - - // if we can't move left, just move right - if !can_move_left { - ctx.candidates_swap(&[(1, 0)]); - return; - } - // and vice versa - if !can_move_right { - ctx.candidates_swap(&[(-1, 0)]); - 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 - }; - - let offset = side * (1 + i / 2); - let hole_target = ctx.get_cell(offset, -1); - if let Some(target) = hole_target - && target.material.def().density < ctx.material.density - { - // 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; - } - } - - // 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 { - 1 - } else if !can_move_right { - -1 - } else if ctx.seqno_parity % 2 == 1 { - 1 - } else { - -1 - }; - - ctx.candidates_swap(&[(target_x, 0)]); + if ctx.rng.random_range(0.0..1.0) > 0.8 + && ctx.candidates_swap(&[(-1 + ctx.seqno_parity as i32 * 2, 0)]) + {} } |
