summaryrefslogtreecommitdiff
path: root/src/sim/cell_manager
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2026-08-29 14:12:01 -0700
committerKai Stevenson <kai@kaistevenson.com>2026-08-29 14:12:01 -0700
commitd7d17f777987047999d09d196417d3f8e8241ba5 (patch)
tree8241ef82c7b5591d33a029e1f4b2c0ae9397a840 /src/sim/cell_manager
parentd577b97d4817762a1a77349ffbf08e4b7ce33902 (diff)
fix sleeping, disable sleeping
Diffstat (limited to 'src/sim/cell_manager')
-rw-r--r--src/sim/cell_manager/chunk.rs13
-rw-r--r--src/sim/cell_manager/sim.rs12
2 files changed, 19 insertions, 6 deletions
diff --git a/src/sim/cell_manager/chunk.rs b/src/sim/cell_manager/chunk.rs
index 17e1551..48e9d16 100644
--- a/src/sim/cell_manager/chunk.rs
+++ b/src/sim/cell_manager/chunk.rs
@@ -1,7 +1,7 @@
use glam::IVec2;
use crate::{
- config::{CELLS_IN_CHUNK, CHUNK_SIZE},
+ config::{CELLS_IN_CHUNK, CHUNK_SIZE, SETTLED_THRESOHLD},
content::materials::{MaterialForm, MaterialId},
sim::{cell::Cell, lib::marching_squares::Marchable},
};
@@ -10,6 +10,7 @@ pub struct Chunk {
pub cells: Box<[Cell; CELLS_IN_CHUNK]>,
pub sleeping: bool,
pub needs_texture_update: bool,
+ pub collider_dirty_seqno: Option<u64>,
}
impl Chunk {
@@ -22,11 +23,18 @@ impl Chunk {
self.cells[x as usize + y as usize * CHUNK_SIZE as usize] = cell;
}
+ pub fn mark_collider_dirty(&mut self, seqno: u64) {
+ if self.collider_dirty_seqno.is_none() {
+ self.collider_dirty_seqno = Some(seqno);
+ }
+ }
+
pub fn void() -> Self {
Chunk {
cells: Box::new([Cell::void(); CELLS_IN_CHUNK]),
sleeping: true,
needs_texture_update: true,
+ collider_dirty_seqno: None,
}
}
@@ -35,6 +43,7 @@ impl Chunk {
cells: Box::new([Cell::void(); CELLS_IN_CHUNK]),
sleeping: true,
needs_texture_update: true,
+ collider_dirty_seqno: Some(0),
};
for dy in 1..5 {
@@ -60,7 +69,7 @@ impl Marchable for Chunk {
let cell = self.get_cell_at_local_position(pos.x as u8, pos.y as u8);
let material = cell.material.def();
material.form == MaterialForm::Solid
- || material.form == MaterialForm::Powder && cell.settled() > 4
+ || material.form == MaterialForm::Powder && cell.settled() >= SETTLED_THRESOHLD
}
}
fn size(&self) -> IVec2 {
diff --git a/src/sim/cell_manager/sim.rs b/src/sim/cell_manager/sim.rs
index ffa253b..6b3a0de 100644
--- a/src/sim/cell_manager/sim.rs
+++ b/src/sim/cell_manager/sim.rs
@@ -128,7 +128,7 @@ fn adjacent_chunks(x: u8, y: u8) -> Vec<usize> {
}
}
-fn internal_set_cell(chunks: &mut [Option<&mut Chunk>; 9], x: i32, y: i32, cell: Cell) {
+fn internal_set_cell(chunks: &mut [Option<&mut Chunk>; 9], x: i32, y: i32, cell: Cell, seqno: u64) {
let cx = x.div_euclid(CHUNK_SIZE);
let cy = y.div_euclid(CHUNK_SIZE);
let lx = x.rem_euclid(CHUNK_SIZE) as u8;
@@ -139,10 +139,12 @@ fn internal_set_cell(chunks: &mut [Option<&mut Chunk>; 9], x: i32, y: i32, cell:
chunk.set_cell_at_local_position(lx, ly, cell);
chunk.needs_texture_update = true;
chunk.sleeping = false;
+ chunk.mark_collider_dirty(seqno);
// if we're at the boundaries of the chunk, wake the adjacent chunk(s)
for idx in adjacent_chunks(lx, ly) {
if let Some(chunk) = chunks[idx].as_mut() {
chunk.sleeping = false;
+ chunk.mark_collider_dirty(seqno);
}
}
}
@@ -151,10 +153,12 @@ fn internal_set_cell(chunks: &mut [Option<&mut Chunk>; 9], x: i32, y: i32, cell:
target.set_cell_at_local_position(x as u8, y as u8, cell);
target.needs_texture_update = true;
target.sleeping = false;
+ target.mark_collider_dirty(seqno);
// if we're at the boundaries of the chunk, wake the adjacent chunk(s)
for idx in adjacent_chunks(lx, ly) {
if let Some(chunk) = chunks[idx].as_mut() {
chunk.sleeping = false;
+ chunk.mark_collider_dirty(seqno);
}
}
}
@@ -200,7 +204,7 @@ impl UpdateCtx<'_, '_, '_> {
debug_assert!(dy > -CHUNK_SIZE + 1 && dy < CHUNK_SIZE - 1);
let x = self.x + dx;
let y = self.y + dy;
- internal_set_cell(self.chunks, x, y, cell);
+ internal_set_cell(self.chunks, x, y, cell, self.seqno);
}
pub fn swap_or_settle(&mut self, candidates: &[(i32, i32)]) -> PostUpdateAction {
@@ -267,12 +271,12 @@ fn sim_tick_chunk(chunks: &mut [Option<&mut Chunk>; 9], seqno: u64) {
if cell.settled() < SETTLED_THRESOHLD {
cell.match_parity(seqno + 1);
cell.increment_settled();
- internal_set_cell(chunks, x, y, cell);
+ internal_set_cell(chunks, x, y, cell, seqno);
}
}
PostUpdateAction::Apply => {
cell.match_parity(seqno + 1);
- internal_set_cell(chunks, x, y, cell);
+ internal_set_cell(chunks, x, y, cell, seqno);
}
}
}