From d7d17f777987047999d09d196417d3f8e8241ba5 Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Sat, 29 Aug 2026 14:12:01 -0700 Subject: fix sleeping, disable sleeping --- src/sim/cell_manager/chunk.rs | 13 +++++++++++-- src/sim/cell_manager/sim.rs | 12 ++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) (limited to 'src/sim/cell_manager') 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, } 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 { } } -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); } } } -- cgit v1.3.1