summaryrefslogtreecommitdiff
path: root/src/sim
diff options
context:
space:
mode:
Diffstat (limited to 'src/sim')
-rw-r--r--src/sim/materials/gas.rs76
-rw-r--r--src/sim/materials/mod.rs13
2 files changed, 87 insertions, 2 deletions
diff --git a/src/sim/materials/gas.rs b/src/sim/materials/gas.rs
new file mode 100644
index 0000000..8d9f42b
--- /dev/null
+++ b/src/sim/materials/gas.rs
@@ -0,0 +1,76 @@
+use crate::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),
+ ]) {
+ 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 {
+ 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)]);
+}
diff --git a/src/sim/materials/mod.rs b/src/sim/materials/mod.rs
index 86ae7d5..0cba5ff 100644
--- a/src/sim/materials/mod.rs
+++ b/src/sim/materials/mod.rs
@@ -1,5 +1,6 @@
use crate::sim::sim::UpdateCtx;
+mod gas;
mod sand;
mod water;
@@ -10,6 +11,7 @@ pub enum MaterialId {
Sand,
Wood,
Water,
+ Gas,
}
pub struct MaterialDef {
@@ -19,7 +21,7 @@ pub struct MaterialDef {
pub sim_update: Option<fn(ctx: &mut UpdateCtx) -> ()>,
}
-static MATERIALS: [MaterialDef; 4] = [
+static MATERIALS: [MaterialDef; 5] = [
MaterialDef {
name: "Void",
color: (0x00, 0x00, 0x00, 0xFF),
@@ -44,14 +46,21 @@ static MATERIALS: [MaterialDef; 4] = [
density: 40,
sim_update: Some(water::sim_update),
},
+ MaterialDef {
+ name: "Gas",
+ color: (0xBD, 0xFF, 0xE4, 0xAA),
+ density: 10,
+ sim_update: Some(gas::sim_update),
+ },
];
impl MaterialId {
- pub const ALL: [MaterialId; 4] = [
+ pub const ALL: [MaterialId; 5] = [
MaterialId::Void,
MaterialId::Sand,
MaterialId::Wood,
MaterialId::Water,
+ MaterialId::Gas,
];
#[inline]
pub fn def(self) -> &'static MaterialDef {