summaryrefslogtreecommitdiff
path: root/src/sim
diff options
context:
space:
mode:
Diffstat (limited to 'src/sim')
-rw-r--r--src/sim/chunk.rs2
-rw-r--r--src/sim/materials/fire.rs33
-rw-r--r--src/sim/materials/smoke.rs24
-rw-r--r--src/sim/sim.rs52
-rw-r--r--src/sim/world.rs10
5 files changed, 53 insertions, 68 deletions
diff --git a/src/sim/chunk.rs b/src/sim/chunk.rs
index 1980932..478af12 100644
--- a/src/sim/chunk.rs
+++ b/src/sim/chunk.rs
@@ -4,7 +4,7 @@ use crate::{
};
pub struct Chunk {
- pub cells: Box<[Cell; CELLS_IN_CHUNK as usize]>,
+ pub cells: Box<[Cell; CELLS_IN_CHUNK]>,
pub sleeping: bool,
pub needs_texture_update: bool,
}
diff --git a/src/sim/materials/fire.rs b/src/sim/materials/fire.rs
index 3c686fe..97027d5 100644
--- a/src/sim/materials/fire.rs
+++ b/src/sim/materials/fire.rs
@@ -12,7 +12,7 @@ impl FireCellView for Cell {
fn get_ticks_lived(self) -> u16 {
self.data
}
- fn set_ticks_lived(&mut self, ticks: u16) -> () {
+ 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
@@ -52,22 +52,21 @@ pub fn sim_update(ctx: &mut UpdateCtx) {
}
// 8 times in our lifespan, emit smoke
- if ticks_lived % 30 == 0 {
- if 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;
- }
+ 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;
}
}
}
diff --git a/src/sim/materials/smoke.rs b/src/sim/materials/smoke.rs
index 68f77ca..5240bc9 100644
--- a/src/sim/materials/smoke.rs
+++ b/src/sim/materials/smoke.rs
@@ -1,24 +1,20 @@
use rand::RngExt;
-use crate::sim::{cell::Cell, materials::MaterialId, sim::UpdateCtx};
+use crate::sim::sim::UpdateCtx;
#[inline]
pub fn sim_update(ctx: &mut UpdateCtx) {
// only allow upward movement some of the time to limit movement speed
- if ctx.rng.random_range(0.0..1.0) > 0.8 {
- if ctx.candidates_swap(&[(0, -1)]) {
- return;
- }
+ if ctx.rng.random_range(0.0..1.0) > 0.8 && ctx.candidates_swap(&[(0, -1)]) {
+ return;
}
// same for each horizontal direction
- if ctx.rng.random_range(0.0..1.0) > 0.8 {
- if ctx.candidates_swap(&[(1 - ctx.seqno_parity as i32 * 2, 0)]) {
- return;
- }
- }
- if ctx.rng.random_range(0.0..1.0) > 0.8 {
- if ctx.candidates_swap(&[(-1 + ctx.seqno_parity as i32 * 2, 0)]) {
- return;
- }
+ if ctx.rng.random_range(0.0..1.0) > 0.8
+ && ctx.candidates_swap(&[(1 - ctx.seqno_parity as i32 * 2, 0)])
+ {
+ return;
}
+ if ctx.rng.random_range(0.0..1.0) > 0.8
+ && ctx.candidates_swap(&[(-1 + ctx.seqno_parity as i32 * 2, 0)])
+ {}
}
diff --git a/src/sim/sim.rs b/src/sim/sim.rs
index 84c3126..b9f4b76 100644
--- a/src/sim/sim.rs
+++ b/src/sim/sim.rs
@@ -39,17 +39,13 @@ fn get_cell(chunks: &[Option<&mut Chunk>; 9], x: i32, y: i32) -> Option<Cell> {
let nc_x = x.rem_euclid(CHUNK_SIZE) as u8;
let nc_y = y.rem_euclid(CHUNK_SIZE) as u8;
- return if let Some(chunk) = &chunks[(dcx + 1 + (dcy + 1) * 3) as usize] {
- Some(chunk.get_cell_at_local_position(nc_x, nc_y))
- } else {
- None
- };
+ chunks[(dcx + 1 + (dcy + 1) * 3) as usize]
+ .as_ref()
+ .map(|chunk| chunk.get_cell_at_local_position(nc_x, nc_y))
} else {
- if let Some(target) = &chunks[4] {
- Some(target.get_cell_at_local_position(x as u8, y as u8))
- } else {
- None
- }
+ chunks[4]
+ .as_ref()
+ .map(|target| target.get_cell_at_local_position(x as u8, y as u8))
}
}
@@ -138,28 +134,28 @@ pub fn sim_tick_chunk(chunks: &mut [Option<&mut Chunk>; 9], seqno: u64) {
let mut cell = get_cell(chunks, x, y).unwrap();
let material = cell.material.def();
- if let Some(update) = material.sim_update {
- if cell.parity() == seqno_parity {
- cell.flip_parity();
- // apply the flipped parity in case the sim target doesn't
- set_cell(chunks, x, y, cell);
+ if let Some(update) = material.sim_update
+ && cell.parity() == seqno_parity
+ {
+ cell.flip_parity();
+ // apply the flipped parity in case the sim target doesn't
+ set_cell(chunks, x, y, cell);
- let mut update_ctx = UpdateCtx {
- chunks,
- seqno,
- seqno_parity,
+ let mut update_ctx = UpdateCtx {
+ chunks,
+ seqno,
+ seqno_parity,
- x,
- y,
- cell: &mut cell,
- material,
+ x,
+ y,
+ cell: &mut cell,
+ material,
- // TODO this is platform-dependent, will break for multiplayer
- rng: &mut rng,
- };
+ // TODO this is platform-dependent, will break for multiplayer
+ rng: &mut rng,
+ };
- update(&mut update_ctx);
- }
+ update(&mut update_ctx);
}
}
}
diff --git a/src/sim/world.rs b/src/sim/world.rs
index 5993b10..d54906c 100644
--- a/src/sim/world.rs
+++ b/src/sim/world.rs
@@ -36,13 +36,7 @@ impl World {
}
// VERY EXPENSIVE
- pub fn set_cell_from_game_position(
- &mut self,
- x: i32,
- y: i32,
- cell: Cell,
- sleeping: bool,
- ) -> () {
+ pub fn set_cell_from_game_position(&mut self, x: i32, y: i32, cell: Cell, sleeping: bool) {
let ((cx, cy), (dx, dy)) = World::split_game_position(x, y);
if let Some(&idx) = self.chunk_position_to_chunk_idx.get(&(cx, cy)) {
self.chunks[idx].set_cell_at_local_position(dx, dy, cell);
@@ -52,7 +46,7 @@ impl World {
}
}
- pub fn insert(&mut self, x: i32, y: i32, chunk: Chunk) -> () {
+ pub fn insert(&mut self, x: i32, y: i32, chunk: Chunk) {
self.chunk_position_to_chunk_idx
.insert((x, y), self.chunks.len());
self.chunks.push(chunk);