diff options
| author | Kai Stevenson <kai@kaistevenson.com> | 2026-08-23 00:38:32 -0700 |
|---|---|---|
| committer | Kai Stevenson <kai@kaistevenson.com> | 2026-08-23 00:38:32 -0700 |
| commit | 350c74bc8ef61833be11bbb2bab6747245e3ae0d (patch) | |
| tree | b9286d694eaedaef93cf36434a5c80e6aa9e8dce /src/sim/cell/materials | |
| parent | a0ff76837ca7ed6bd90cedcfee3832c1d9ad2287 (diff) | |
refactor out content
Diffstat (limited to 'src/sim/cell/materials')
| -rw-r--r-- | src/sim/cell/materials/fire.rs | 77 | ||||
| -rw-r--r-- | src/sim/cell/materials/gas.rs | 24 | ||||
| -rw-r--r-- | src/sim/cell/materials/liquid.rs | 75 | ||||
| -rw-r--r-- | src/sim/cell/materials/mod.rs | 111 | ||||
| -rw-r--r-- | src/sim/cell/materials/powder.rs | 10 |
5 files changed, 0 insertions, 297 deletions
diff --git a/src/sim/cell/materials/fire.rs b/src/sim/cell/materials/fire.rs deleted file mode 100644 index 8cabe30..0000000 --- a/src/sim/cell/materials/fire.rs +++ /dev/null @@ -1,77 +0,0 @@ -use rand::RngExt; - -use crate::sim::{ - cell::{cell::Cell, materials::MaterialId}, - cell_manager::sim::{PostUpdateAction, UpdateCtx}, -}; - -pub trait FireCellView { - fn get_ticks_lived(self) -> u16; - fn set_ticks_lived(&mut self, ticks: u16) -> (); - fn is_flammable(self) -> bool; -} - -impl FireCellView for Cell { - fn get_ticks_lived(self) -> u16 { - self.data - } - fn set_ticks_lived(&mut self, ticks: u16) { - self.data = ticks; - } - // this could be a property of the material def, I think it's better here for now - fn is_flammable(self) -> bool { - [MaterialId::Wood].contains(&self.material) - } -} - -// TODO optimize number of rng calls? -// TODO wtf is fire -#[inline] -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 - ctx.set_cell(0, 0, Cell::void()); - // the updater should take no action since we already killed ourselves - return PostUpdateAction::None; - } - } - - // at an average of 4 times per lifespan, try to spread - // 1/60 * 240 = 4 - if ctx.rng.random_range(0.0..1.0) > (59.0 / 60.0) { - let (dx, dy) = (ctx.rng.random_range(-1..=1), ctx.rng.random_range(-1..=1)); - if let Some(target) = ctx.get_cell(dx, dy) - && target.is_flammable() - { - ctx.set_cell(dx, dy, Cell::from_material(MaterialId::Fire)); - } - } - - // 8 times in our lifespan, emit smoke - if ticks_lived.is_multiple_of(30) - && let Some(target) = ctx.get_cell(0, -1) - && target.material == MaterialId::Void - { - for (dx, dy) in [ - (0, -1), - (1 - ctx.seqno_parity as i32 * 2, 0), - (-1 + ctx.seqno_parity as i32 * 2, 0), - (0, 1), - ] { - if let Some(target) = ctx.get_cell(dx, dy) - && target.material == MaterialId::Void - { - ctx.set_cell(dx, dy, Cell::from_material(MaterialId::Smoke)); - break; - } - } - } - - 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 deleted file mode 100644 index 229d0e9..0000000 --- a/src/sim/cell/materials/gas.rs +++ /dev/null @@ -1,24 +0,0 @@ -use rand::RngExt; - -use crate::sim::cell_manager::sim::{PostUpdateAction, UpdateCtx}; - -#[inline] -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.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.swap_or_settle(&[(1 - ctx.seqno_parity as i32 * 2, 0)]) == PostUpdateAction::None - { - return PostUpdateAction::None; - } - 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 deleted file mode 100644 index 6c3bdcd..0000000 --- a/src/sim/cell/materials/liquid.rs +++ /dev/null @@ -1,75 +0,0 @@ -use crate::sim::cell_manager::sim::{PostUpdateAction, UpdateCtx}; - -#[inline] -pub fn sim_update(ctx: &mut UpdateCtx) -> PostUpdateAction { - // if the water can fall, do so - if ctx.swap_or_settle(&[ - (0, 1), - (-1 + 2 * ctx.seqno_parity as i32, 1), - (1 - 2 * ctx.seqno_parity as i32, 1), - ]) == PostUpdateAction::None - { - return PostUpdateAction::None; - } - - // 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 { - return PostUpdateAction::Settle; - } - - // if we can't move left, just move right - if !can_move_left { - return ctx.swap_or_settle(&[(1, 0)]); - } - // and vice versa - if !can_move_right { - return ctx.swap_or_settle(&[(-1, 0)]); - } - - // 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 - 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 { - // 1 - // } else if !can_move_right { - // -1 - // } else if ctx.seqno_parity % 2 == 1 { - // 1 - // } else { - // -1 - // }; - - // ctx.candidates_swap(&[(target_x, 0)]); -} diff --git a/src/sim/cell/materials/mod.rs b/src/sim/cell/materials/mod.rs deleted file mode 100644 index 8116565..0000000 --- a/src/sim/cell/materials/mod.rs +++ /dev/null @@ -1,111 +0,0 @@ -use crate::sim::cell_manager::sim::{PostUpdateAction, UpdateCtx}; - -pub mod fire; -pub mod gas; -pub mod liquid; -pub mod powder; - -#[repr(u8)] -#[derive(Clone, Copy, PartialEq, Eq, Debug)] -pub enum MaterialId { - Void = 0, - Sand, - Wood, - Water, - Fire, - Smoke, - Steel, -} - -#[repr(u8)] -#[derive(Clone, Copy, PartialEq, Eq, Debug)] -pub enum MaterialForm { - Void, - Gas, - Liquid, - Powder, - Solid, -} - -pub struct MaterialDef { - pub name: &'static str, - pub color: (u8, u8, u8, u8), - pub density: u8, - pub form: MaterialForm, - pub sim_update: Option<fn(ctx: &mut UpdateCtx) -> PostUpdateAction>, -} - -static MATERIALS: [MaterialDef; 7] = [ - MaterialDef { - name: "Void", - color: (0x00, 0x00, 0x00, 0x00), - density: 0, - // form usually but not always determines the update fn - form: MaterialForm::Void, - sim_update: None, - }, - MaterialDef { - name: "Sand", - color: (0xDE, 0xCB, 0x85, 0xFF), - density: 50, - form: MaterialForm::Powder, - sim_update: Some(powder::sim_update), - }, - MaterialDef { - name: "Wood", - color: (0x85, 0x56, 0x1D, 0xFF), - density: 50, - form: MaterialForm::Solid, - sim_update: None, - }, - MaterialDef { - name: "Water", - color: (0x38, 0xA9, 0xFF, 0xFF), - density: 40, - form: MaterialForm::Liquid, - sim_update: Some(liquid::sim_update), - }, - // TODO this is not a material - MaterialDef { - name: "Fire", - color: (0xFC, 0x66, 0x00, 0xAA), - // for now this matches wood - density: 50, - form: MaterialForm::Gas, - sim_update: Some(fire::sim_update), - }, - MaterialDef { - name: "Smoke", - color: (0x32, 0x35, 0x36, 0xAA), - density: 15, - form: MaterialForm::Gas, - sim_update: Some(gas::sim_update), - }, - MaterialDef { - name: "Steel", - color: (0x60, 0x60, 0x60, 0xFF), - density: 150, - form: MaterialForm::Solid, - sim_update: None, - }, -]; - -impl MaterialId { - pub const ALL: [MaterialId; 7] = [ - MaterialId::Void, - MaterialId::Sand, - MaterialId::Wood, - MaterialId::Water, - MaterialId::Fire, - MaterialId::Smoke, - MaterialId::Steel, - ]; - #[inline] - pub fn def(self) -> &'static MaterialDef { - &MATERIALS[self as usize] - } - - pub fn from_index(idx: u8) -> MaterialId { - MaterialId::ALL[idx as usize] - } -} diff --git a/src/sim/cell/materials/powder.rs b/src/sim/cell/materials/powder.rs deleted file mode 100644 index 8c916e4..0000000 --- a/src/sim/cell/materials/powder.rs +++ /dev/null @@ -1,10 +0,0 @@ -use crate::sim::cell_manager::sim::{PostUpdateAction, UpdateCtx}; - -#[inline] -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), - ]) -} |
