From 7fc150e1efe22f4e438915cee63933b6b24d7c3e Mon Sep 17 00:00:00 2001 From: blockmath Date: Thu, 20 Jun 2024 22:35:17 -0700 Subject: [PATCH 01/18] Tickers gonna tick! Basic ticker proof-of-concept added here. --- client/Cargo.toml | 1 + client/src/.sim.rs.swp | Bin 0 -> 4096 bytes client/src/graphics/window.rs | 5 +++ client/src/local_character_controller.rs | 2 +- client/src/sim.rs | 49 +++++++++++++++++++++++ common/src/lib.rs | 1 + common/src/proto.rs | 3 +- common/src/ticker.rs | 46 +++++++++++++++++++++ 8 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 client/src/.sim.rs.swp create mode 100644 common/src/ticker.rs diff --git a/client/Cargo.toml b/client/Cargo.toml index b67bfecf..f7f81955 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -29,6 +29,7 @@ anyhow = "1.0.26" whoami = "1.2.1" serde = { version = "1.0.104", features = ["derive", "rc"] } toml = { workspace = true } +rand = { version = "0.8.5", features = ["small_rng"] } fxhash = "0.2.1" downcast-rs = "1.1.1" quinn = { workspace = true } diff --git a/client/src/.sim.rs.swp b/client/src/.sim.rs.swp new file mode 100644 index 0000000000000000000000000000000000000000..0f535e95c05fb885d95a683ce25837c4d2a96cf0 GIT binary patch literal 4096 zcmYc?2=nw+u+%eT00IF91~;zKG!XNHF9SnLYFTC;h(iE4dpHL9`w>tCly4CoGz3ONfb { + if let Some(sim) = self.sim.as_mut() { + sim.debug_spawn_blinker(); + } + } KeyCode::F1 if state == ElementState::Pressed => { self.gui_state.toggle_gui(); } diff --git a/client/src/local_character_controller.rs b/client/src/local_character_controller.rs index a4197d7a..6cc7e62a 100644 --- a/client/src/local_character_controller.rs +++ b/client/src/local_character_controller.rs @@ -3,7 +3,7 @@ use common::{math, proto::Position}; pub struct LocalCharacterController { /// The last extrapolated inter-frame view position, used for rendering and gravity-specific /// orientation computations - position: Position, + pub position: Position, /// The up vector relative to position, ignoring orientation up: na::UnitVector3, diff --git a/client/src/sim.rs b/client/src/sim.rs index 9f59582f..e30f0ade 100644 --- a/client/src/sim.rs +++ b/client/src/sim.rs @@ -3,6 +3,8 @@ use std::time::Duration; use fxhash::FxHashMap; use hecs::Entity; use tracing::{debug, error, trace}; +use rand::rngs::SmallRng; +use rand::{Rng, SeedableRng}; use crate::{ local_character_controller::LocalCharacterController, metrics, net, @@ -18,6 +20,7 @@ use common::{ self, BlockUpdate, Character, CharacterInput, CharacterState, Command, Component, Position, }, sanitize_motion_input, + ticker::{TickerEntity, Ticker}, world::Material, EntityId, GraphEntities, SimConfig, Step, }; @@ -48,6 +51,7 @@ pub struct Sim { pub local_character_id: EntityId, pub local_character: Option, step: Option, + rng: SmallRng, // Input state since_input_sent: Duration, @@ -94,6 +98,7 @@ impl Sim { local_character_id, local_character: None, step: None, + rng: SmallRng::from_entropy(), since_input_sent: Duration::new(0, 0), movement_input: na::zero(), @@ -144,6 +149,20 @@ impl Sim { self.toggle_no_clip = true; } + pub fn debug_spawn_blinker(&mut self) { // Note: the blinker currently does nothing but update internal state + let mut components: Vec = Vec::::new(); + let position: Position = self.local_character_controller.position; + let ticker: TickerEntity = TickerEntity { + lastTicked: 0, + tickerID: 1, + tickerUID: 0.into(), + ticker: Ticker::new(), + }; + components.push(common::proto::Component::Position(position)); + components.push(common::proto::Component::TickerEntity(ticker)); + self.request_server_spawn(components); + } + pub fn set_jump_held(&mut self, jump_held: bool) { self.jump_held = jump_held; self.jump_pressed = jump_held || self.jump_pressed; @@ -223,6 +242,13 @@ impl Sim { if !self.no_clip { self.local_character_controller.align_to_gravity(); } + + // This is just for testing the blinker. Remove this once we actually sync it to the server + let mut blinkerQuery = self.world.query::<&mut TickerEntity>(); + let blinkerIter = blinkerQuery.iter(); + for (_entity, mut ticker_entity) in blinkerIter { + ticker_entity.ticker.tick(Option::expect(self.step, "missing step")); + } } pub fn handle_net(&mut self, msg: net::Message) { @@ -367,6 +393,10 @@ impl Sim { node = Some(x.node); builder.add(x); } + TickerEntity(mut x) => { + x.setUID(id); + builder.add(x); + } }; } let entity = self.world.spawn(builder.build()); @@ -382,6 +412,16 @@ impl Sim { } } + fn request_server_spawn( + &mut self, + components: Vec, + ) { + // TODO: actually tell the server to spawn the entity, rather than spawning it ourselves. (We should wait for the server to confirm the spawn and send the data back to us rather than instantiating it right away.) Also get rid of the blinker debug code + let mut builder = hecs::EntityBuilder::new(); + let id = self.new_id(); + self.spawn(&mut builder, id, components); + } + fn send_input(&mut self, net: &mut Net) { let orientation = if self.no_clip { self.local_character_controller.orientation() @@ -521,4 +561,13 @@ impl Sim { new_material: material, }) } + + fn new_id(&mut self) -> EntityId { + loop { + let id = self.rng.gen(); + if !self.entity_ids.contains_key(&id) { + return id; + } + } + } } diff --git a/common/src/lib.rs b/common/src/lib.rs index a8d8e097..89f8d468 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -29,6 +29,7 @@ mod plane; pub mod proto; mod sim_config; pub mod terraingen; +pub mod ticker; pub mod traversal; pub mod voxel_math; pub mod world; diff --git a/common/src/proto.rs b/common/src/proto.rs index d051f5eb..91f3ccfb 100644 --- a/common/src/proto.rs +++ b/common/src/proto.rs @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize}; use crate::{ dodeca, graph::NodeId, node::ChunkId, voxel_math::Coords, world::Material, EntityId, SimConfig, - Step, + Step, ticker::TickerEntity }; #[derive(Debug, Serialize, Deserialize)] @@ -90,6 +90,7 @@ pub struct SerializedVoxelData { pub enum Component { Character(Character), Position(Position), + TickerEntity(TickerEntity), } #[derive(Debug, Serialize, Deserialize)] diff --git a/common/src/ticker.rs b/common/src/ticker.rs new file mode 100644 index 00000000..19bd2b38 --- /dev/null +++ b/common/src/ticker.rs @@ -0,0 +1,46 @@ +use serde::{Deserialize, Serialize}; +use hecs::Entity; +use crate::{Step, EntityId}; + +#[derive(Debug, Serialize, Deserialize)] +pub struct TickerEntity { + pub lastTicked: Step, + pub tickerID: u16, // This ID determines what *type* of ticker this is (blinker, sapling, etc) + pub tickerUID: EntityId, + pub ticker: Ticker, +} + +impl TickerEntity { + pub fn setUID(&mut self, id: EntityId) { + self.tickerUID = id; + } + pub fn setLastTicked(&mut self, step: Step) { + self.lastTicked = step; + } + pub fn setID(&mut self, id: u16) { + self.tickerID = id; + } +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct Ticker { + on: bool, +} + +impl Ticker { + pub fn new() -> Self { + Self { + on: false, + } + } + pub fn tick(&mut self, _step: Step) { + self.on = !self.on; + + // Just for testing + if self.on { + println!("Ticked ON!"); + } else { + println!("Ticked OFF!"); + } + } +} \ No newline at end of file From 084b2c7c9c36452e260e4415d65711a3d711c8a1 Mon Sep 17 00:00:00 2001 From: blockmath Date: Fri, 21 Jun 2024 21:52:15 -0700 Subject: [PATCH 02/18] Fixed some style issues, moved blinker handling to the server, moved ID generation and tracking used IDs to id_generator.rs --- client/src/.sim.rs.swp | Bin 4096 -> 0 bytes client/src/prediction.rs | 1 + client/src/sim.rs | 52 ++++++++--------------------------- common/src/id_generator.rs | 28 +++++++++++++++++++ common/src/lib.rs | 1 + common/src/proto.rs | 1 + common/src/ticker.rs | 19 ++----------- server/src/sim.rs | 54 ++++++++++++++++++++++++------------- 8 files changed, 80 insertions(+), 76 deletions(-) delete mode 100644 client/src/.sim.rs.swp create mode 100644 common/src/id_generator.rs diff --git a/client/src/.sim.rs.swp b/client/src/.sim.rs.swp deleted file mode 100644 index 0f535e95c05fb885d95a683ce25837c4d2a96cf0..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4096 zcmYc?2=nw+u+%eT00IF91~;zKG!XNHF9SnLYFTC;h(iE4dpHL9`w>tCly4CoGz3ONfb, step: Option, - rng: SmallRng, // Input state since_input_sent: Duration, @@ -67,6 +64,7 @@ pub struct Sim { no_clip: bool, /// Whether no_clip will be toggled next step toggle_no_clip: bool, + debug_spawn_blinker: bool, /// Whether the current step starts with a jump is_jumping: bool, /// Whether the jump button has been pressed since the last step @@ -98,13 +96,13 @@ impl Sim { local_character_id, local_character: None, step: None, - rng: SmallRng::from_entropy(), since_input_sent: Duration::new(0, 0), movement_input: na::zero(), average_movement_input: na::zero(), no_clip: true, toggle_no_clip: false, + debug_spawn_blinker: false, is_jumping: false, jump_pressed: false, jump_held: false, @@ -150,17 +148,7 @@ impl Sim { } pub fn debug_spawn_blinker(&mut self) { // Note: the blinker currently does nothing but update internal state - let mut components: Vec = Vec::::new(); - let position: Position = self.local_character_controller.position; - let ticker: TickerEntity = TickerEntity { - lastTicked: 0, - tickerID: 1, - tickerUID: 0.into(), - ticker: Ticker::new(), - }; - components.push(common::proto::Component::Position(position)); - components.push(common::proto::Component::TickerEntity(ticker)); - self.request_server_spawn(components); + self.debug_spawn_blinker = true; } pub fn set_jump_held(&mut self, jump_held: bool) { @@ -244,10 +232,10 @@ impl Sim { } // This is just for testing the blinker. Remove this once we actually sync it to the server - let mut blinkerQuery = self.world.query::<&mut TickerEntity>(); - let blinkerIter = blinkerQuery.iter(); - for (_entity, mut ticker_entity) in blinkerIter { - ticker_entity.ticker.tick(Option::expect(self.step, "missing step")); + let mut blinker_query = self.world.query::<&mut TickerEntity>(); + let blinker_iter = blinker_query.iter(); + for (_entity, ticker_entity) in blinker_iter { + ticker_entity.ticker.tick(self.step.unwrap()); } } @@ -393,8 +381,7 @@ impl Sim { node = Some(x.node); builder.add(x); } - TickerEntity(mut x) => { - x.setUID(id); + TickerEntity(x) => { builder.add(x); } }; @@ -412,16 +399,6 @@ impl Sim { } } - fn request_server_spawn( - &mut self, - components: Vec, - ) { - // TODO: actually tell the server to spawn the entity, rather than spawning it ourselves. (We should wait for the server to confirm the spawn and send the data back to us rather than instantiating it right away.) Also get rid of the blinker debug code - let mut builder = hecs::EntityBuilder::new(); - let id = self.new_id(); - self.spawn(&mut builder, id, components); - } - fn send_input(&mut self, net: &mut Net) { let orientation = if self.no_clip { self.local_character_controller.orientation() @@ -432,6 +409,7 @@ impl Sim { movement: sanitize_motion_input(orientation * self.average_movement_input), jump: self.is_jumping, no_clip: self.no_clip, + debug_spawn_blinker: self.debug_spawn_blinker, block_update: self.get_local_character_block_update(), }; let generation = self @@ -465,6 +443,7 @@ impl Sim { / (self.since_input_sent.as_secs_f32() / self.cfg.step_interval.as_secs_f32()), jump: self.is_jumping, no_clip: self.no_clip, + debug_spawn_blinker: self.debug_spawn_blinker, block_update: None, }; character_controller::run_character_step( @@ -561,13 +540,4 @@ impl Sim { new_material: material, }) } - - fn new_id(&mut self) -> EntityId { - loop { - let id = self.rng.gen(); - if !self.entity_ids.contains_key(&id) { - return id; - } - } - } } diff --git a/common/src/id_generator.rs b/common/src/id_generator.rs new file mode 100644 index 00000000..c51f8caa --- /dev/null +++ b/common/src/id_generator.rs @@ -0,0 +1,28 @@ +use hecs::Entity; +use crate::EntityId; +use rand::rngs::SmallRng; +use rand::{Rng, SeedableRng}; +use fxhash::FxHashMap; + +pub struct IDGenerator { + pub rng: SmallRng, + pub entity_ids: FxHashMap, +} + + +impl IDGenerator { + pub fn new() -> Self { + Self { + rng: SmallRng::from_entropy(), + entity_ids: FxHashMap::default(), + } + } + pub fn new_id(&mut self) -> EntityId { + loop { + let id = self.rng.gen(); + if !self.entity_ids.contains_key(&id) { + return id; + } + } + } +} \ No newline at end of file diff --git a/common/src/lib.rs b/common/src/lib.rs index 89f8d468..8fec681b 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -21,6 +21,7 @@ pub mod graph; pub mod graph_collision; mod graph_entities; pub mod graph_ray_casting; +pub mod id_generator; pub mod lru_slab; mod margins; pub mod math; diff --git a/common/src/proto.rs b/common/src/proto.rs index 91f3ccfb..94b22f78 100644 --- a/common/src/proto.rs +++ b/common/src/proto.rs @@ -70,6 +70,7 @@ pub struct CharacterInput { pub movement: na::Vector3, pub jump: bool, pub no_clip: bool, + pub debug_spawn_blinker: bool, pub block_update: Option, } diff --git a/common/src/ticker.rs b/common/src/ticker.rs index 19bd2b38..41c0be5c 100644 --- a/common/src/ticker.rs +++ b/common/src/ticker.rs @@ -1,27 +1,12 @@ use serde::{Deserialize, Serialize}; -use hecs::Entity; -use crate::{Step, EntityId}; +use crate::Step; #[derive(Debug, Serialize, Deserialize)] pub struct TickerEntity { - pub lastTicked: Step, - pub tickerID: u16, // This ID determines what *type* of ticker this is (blinker, sapling, etc) - pub tickerUID: EntityId, + pub last_ticked: Step, pub ticker: Ticker, } -impl TickerEntity { - pub fn setUID(&mut self, id: EntityId) { - self.tickerUID = id; - } - pub fn setLastTicked(&mut self, step: Step) { - self.lastTicked = step; - } - pub fn setID(&mut self, id: u16) { - self.tickerID = id; - } -} - #[derive(Debug, Serialize, Deserialize)] pub struct Ticker { on: bool, diff --git a/server/src/sim.rs b/server/src/sim.rs index 72e938dd..a57e50f0 100644 --- a/server/src/sim.rs +++ b/server/src/sim.rs @@ -7,20 +7,20 @@ use common::proto::{BlockUpdate, SerializedVoxelData}; use common::{node::ChunkId, GraphEntities}; use fxhash::{FxHashMap, FxHashSet}; use hecs::Entity; -use rand::rngs::SmallRng; -use rand::{Rng, SeedableRng}; use save::ComponentType; use tracing::{error, error_span, info, trace}; use common::{ character_controller, dodeca, graph::{Graph, NodeId}, + id_generator::IDGenerator, math, node::{populate_fresh_nodes, Chunk}, proto::{ Character, CharacterInput, CharacterState, ClientHello, Command, Component, FreshNode, Position, Spawns, StateDelta, }, + ticker::{Ticker, TickerEntity}, traversal::{ensure_nearby, nearby_nodes}, worldgen::ChunkParams, EntityId, SimConfig, Step, @@ -30,14 +30,14 @@ use crate::postcard_helpers::{self, SaveEntity}; pub struct Sim { cfg: Arc, - rng: SmallRng, step: Step, - entity_ids: FxHashMap, + id_generator: IDGenerator, world: hecs::World, graph: Graph, /// Voxel data that has been fetched from a savefile but not yet introduced to the graph preloaded_voxel_data: FxHashMap, spawns: Vec, + pending_ticker_spawns: Vec<(Position, TickerEntity)>, despawns: Vec, graph_entities: GraphEntities, /// All nodes that have entity-related information yet to be saved @@ -51,13 +51,13 @@ pub struct Sim { impl Sim { pub fn new(cfg: Arc, save: &save::Save) -> Self { let mut result = Self { - rng: SmallRng::from_entropy(), step: 0, - entity_ids: FxHashMap::default(), + id_generator: IDGenerator::new(), world: hecs::World::new(), graph: Graph::new(cfg.chunk_size), preloaded_voxel_data: FxHashMap::default(), spawns: Vec::new(), + pending_ticker_spawns: Vec::new(), despawns: Vec::new(), graph_entities: GraphEntities::new(), dirty_nodes: FxHashSet::default(), @@ -187,7 +187,7 @@ impl Sim { } pub fn spawn_character(&mut self, hello: ClientHello) -> (EntityId, Entity) { - let id = self.new_id(); + let id = self.id_generator.new_id(); info!(%id, name = %hello.name, "spawning character"); let position = Position { node: NodeId::ROOT, @@ -205,11 +205,12 @@ impl Sim { movement: na::Vector3::zeros(), jump: false, no_clip: true, + debug_spawn_blinker: false, block_update: None, }; let entity = self.world.spawn((id, position, character, initial_input)); self.graph_entities.insert(position.node, entity); - self.entity_ids.insert(id, entity); + self.id_generator.entity_ids.insert(id, entity); self.spawns.push(entity); self.dirty_nodes.insert(position.node); (id, entity) @@ -229,7 +230,7 @@ impl Sim { pub fn destroy(&mut self, entity: Entity) { let id = *self.world.get::<&EntityId>(entity).unwrap(); - self.entity_ids.remove(&id); + self.id_generator.entity_ids.remove(&id); if let Ok(position) = self.world.get::<&Position>(entity) { self.graph_entities.remove(position.node, entity); } @@ -272,8 +273,17 @@ impl Sim { let span = error_span!("step", step = self.step); let _guard = span.enter(); + { + let mut ticker_query = self.world.query::<&mut TickerEntity>(); + let ticker_iter = ticker_query.iter(); + for (_entity, ticker_entity) in ticker_iter { + ticker_entity.ticker.tick(self.step); + } + } + let mut pending_block_updates: Vec = vec![]; + // Extend graph structure for (_, (position, _)) in self.world.query::<(&mut Position, &mut Character)>().iter() { ensure_nearby(&mut self.graph, position, self.cfg.view_distance); @@ -332,6 +342,13 @@ impl Sim { .iter() { let prev_node = position.node; + if input.debug_spawn_blinker { + let ticker: TickerEntity = TickerEntity { + last_ticked: 0, + ticker: Ticker::new(), + }; + self.pending_ticker_spawns.push((*position, ticker)); + } character_controller::run_character_step( &self.cfg, &self.graph, @@ -350,6 +367,16 @@ impl Sim { self.dirty_nodes.insert(position.node); } + let pending_ticker_spawns = std::mem::take(&mut self.pending_ticker_spawns); + for (position, ticker) in pending_ticker_spawns { + let id = self.id_generator.new_id(); + let entity = self.world.spawn((id, position, ticker)); + self.graph_entities.insert(position.node, entity); + self.id_generator.entity_ids.insert(id, entity); + self.spawns.push(entity); + self.dirty_nodes.insert(position.node); + } + let mut accepted_block_updates: Vec = vec![]; for block_update in pending_block_updates.into_iter() { @@ -411,15 +438,6 @@ impl Sim { self.step += 1; (spawns, delta) } - - fn new_id(&mut self) -> EntityId { - loop { - let id = self.rng.gen(); - if !self.entity_ids.contains_key(&id) { - return id; - } - } - } } fn dump_entity(world: &hecs::World, entity: Entity) -> Vec { From a0b80984979e131f95566d5e22396852fd16ad7a Mon Sep 17 00:00:00 2001 From: blockmath Date: Fri, 21 Jun 2024 22:18:08 -0700 Subject: [PATCH 03/18] i am stupid help me how do i no longer stupid syndrome --- client/src/sim.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/client/src/sim.rs b/client/src/sim.rs index 099e78b5..d93f364f 100644 --- a/client/src/sim.rs +++ b/client/src/sim.rs @@ -422,6 +422,7 @@ impl Sim { character_input, orientation: self.local_character_controller.orientation(), }); + self.debug_spawn_blinker = false; } fn update_view_position(&mut self) { From d9a8222c929503efb4a0e0802d4fa73c6bada667 Mon Sep 17 00:00:00 2001 From: blockmath Date: Sat, 22 Jun 2024 11:05:08 -0700 Subject: [PATCH 04/18] Removed some unused/shouldn't be used code. --- client/Cargo.toml | 1 - client/src/sim.rs | 8 -------- 2 files changed, 9 deletions(-) diff --git a/client/Cargo.toml b/client/Cargo.toml index f7f81955..b67bfecf 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -29,7 +29,6 @@ anyhow = "1.0.26" whoami = "1.2.1" serde = { version = "1.0.104", features = ["derive", "rc"] } toml = { workspace = true } -rand = { version = "0.8.5", features = ["small_rng"] } fxhash = "0.2.1" downcast-rs = "1.1.1" quinn = { workspace = true } diff --git a/client/src/sim.rs b/client/src/sim.rs index d93f364f..5300a4c5 100644 --- a/client/src/sim.rs +++ b/client/src/sim.rs @@ -18,7 +18,6 @@ use common::{ self, BlockUpdate, Character, CharacterInput, CharacterState, Command, Component, Position, }, sanitize_motion_input, - ticker::TickerEntity, world::Material, EntityId, GraphEntities, SimConfig, Step, }; @@ -230,13 +229,6 @@ impl Sim { if !self.no_clip { self.local_character_controller.align_to_gravity(); } - - // This is just for testing the blinker. Remove this once we actually sync it to the server - let mut blinker_query = self.world.query::<&mut TickerEntity>(); - let blinker_iter = blinker_query.iter(); - for (_entity, ticker_entity) in blinker_iter { - ticker_entity.ticker.tick(self.step.unwrap()); - } } pub fn handle_net(&mut self, msg: net::Message) { From 38a9f71fd721d41705e4333db11e030b39ecc170 Mon Sep 17 00:00:00 2001 From: blockmath Date: Sun, 23 Jun 2024 15:14:12 -0700 Subject: [PATCH 05/18] Fixed clippy issues, formatted crate --- client/src/sim.rs | 3 ++- common/src/id_generator.rs | 13 +++++++++---- common/src/proto.rs | 4 ++-- common/src/ticker.rs | 14 +++++++++----- server/src/sim.rs | 1 - 5 files changed, 22 insertions(+), 13 deletions(-) diff --git a/client/src/sim.rs b/client/src/sim.rs index 5300a4c5..a47f59b4 100644 --- a/client/src/sim.rs +++ b/client/src/sim.rs @@ -146,7 +146,8 @@ impl Sim { self.toggle_no_clip = true; } - pub fn debug_spawn_blinker(&mut self) { // Note: the blinker currently does nothing but update internal state + pub fn debug_spawn_blinker(&mut self) { + // Note: the blinker currently does nothing but update internal state self.debug_spawn_blinker = true; } diff --git a/common/src/id_generator.rs b/common/src/id_generator.rs index c51f8caa..78ff1ca8 100644 --- a/common/src/id_generator.rs +++ b/common/src/id_generator.rs @@ -1,15 +1,14 @@ -use hecs::Entity; use crate::EntityId; +use fxhash::FxHashMap; +use hecs::Entity; use rand::rngs::SmallRng; use rand::{Rng, SeedableRng}; -use fxhash::FxHashMap; pub struct IDGenerator { pub rng: SmallRng, pub entity_ids: FxHashMap, } - impl IDGenerator { pub fn new() -> Self { Self { @@ -25,4 +24,10 @@ impl IDGenerator { } } } -} \ No newline at end of file +} + +impl Default for IDGenerator { + fn default() -> Self { + IDGenerator::new() + } +} diff --git a/common/src/proto.rs b/common/src/proto.rs index 94b22f78..d6a70409 100644 --- a/common/src/proto.rs +++ b/common/src/proto.rs @@ -1,8 +1,8 @@ use serde::{Deserialize, Serialize}; use crate::{ - dodeca, graph::NodeId, node::ChunkId, voxel_math::Coords, world::Material, EntityId, SimConfig, - Step, ticker::TickerEntity + dodeca, graph::NodeId, node::ChunkId, ticker::TickerEntity, voxel_math::Coords, + world::Material, EntityId, SimConfig, Step, }; #[derive(Debug, Serialize, Deserialize)] diff --git a/common/src/ticker.rs b/common/src/ticker.rs index 41c0be5c..f0219f54 100644 --- a/common/src/ticker.rs +++ b/common/src/ticker.rs @@ -1,5 +1,5 @@ -use serde::{Deserialize, Serialize}; use crate::Step; +use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize)] pub struct TickerEntity { @@ -14,9 +14,7 @@ pub struct Ticker { impl Ticker { pub fn new() -> Self { - Self { - on: false, - } + Self { on: false } } pub fn tick(&mut self, _step: Step) { self.on = !self.on; @@ -28,4 +26,10 @@ impl Ticker { println!("Ticked OFF!"); } } -} \ No newline at end of file +} + +impl Default for Ticker { + fn default() -> Self { + Ticker::new() + } +} diff --git a/server/src/sim.rs b/server/src/sim.rs index a57e50f0..def0e1ef 100644 --- a/server/src/sim.rs +++ b/server/src/sim.rs @@ -283,7 +283,6 @@ impl Sim { let mut pending_block_updates: Vec = vec![]; - // Extend graph structure for (_, (position, _)) in self.world.query::<(&mut Position, &mut Character)>().iter() { ensure_nearby(&mut self.graph, position, self.cfg.view_distance); From 524a4f376ebb33c6d9ab4e129e724ef05fd2570b Mon Sep 17 00:00:00 2001 From: blockmath Date: Sun, 23 Jun 2024 15:39:31 -0700 Subject: [PATCH 06/18] Reverted the creation of id_generator --- common/src/id_generator.rs | 33 --------------------------------- common/src/lib.rs | 1 - server/src/sim.rs | 28 ++++++++++++++++++++-------- 3 files changed, 20 insertions(+), 42 deletions(-) delete mode 100644 common/src/id_generator.rs diff --git a/common/src/id_generator.rs b/common/src/id_generator.rs deleted file mode 100644 index 78ff1ca8..00000000 --- a/common/src/id_generator.rs +++ /dev/null @@ -1,33 +0,0 @@ -use crate::EntityId; -use fxhash::FxHashMap; -use hecs::Entity; -use rand::rngs::SmallRng; -use rand::{Rng, SeedableRng}; - -pub struct IDGenerator { - pub rng: SmallRng, - pub entity_ids: FxHashMap, -} - -impl IDGenerator { - pub fn new() -> Self { - Self { - rng: SmallRng::from_entropy(), - entity_ids: FxHashMap::default(), - } - } - pub fn new_id(&mut self) -> EntityId { - loop { - let id = self.rng.gen(); - if !self.entity_ids.contains_key(&id) { - return id; - } - } - } -} - -impl Default for IDGenerator { - fn default() -> Self { - IDGenerator::new() - } -} diff --git a/common/src/lib.rs b/common/src/lib.rs index 8fec681b..89f8d468 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -21,7 +21,6 @@ pub mod graph; pub mod graph_collision; mod graph_entities; pub mod graph_ray_casting; -pub mod id_generator; pub mod lru_slab; mod margins; pub mod math; diff --git a/server/src/sim.rs b/server/src/sim.rs index def0e1ef..c3bf9c14 100644 --- a/server/src/sim.rs +++ b/server/src/sim.rs @@ -7,13 +7,14 @@ use common::proto::{BlockUpdate, SerializedVoxelData}; use common::{node::ChunkId, GraphEntities}; use fxhash::{FxHashMap, FxHashSet}; use hecs::Entity; +use rand::rngs::SmallRng; +use rand::{Rng, SeedableRng}; use save::ComponentType; use tracing::{error, error_span, info, trace}; use common::{ character_controller, dodeca, graph::{Graph, NodeId}, - id_generator::IDGenerator, math, node::{populate_fresh_nodes, Chunk}, proto::{ @@ -31,7 +32,8 @@ use crate::postcard_helpers::{self, SaveEntity}; pub struct Sim { cfg: Arc, step: Step, - id_generator: IDGenerator, + pub rng: SmallRng, + pub entity_ids: FxHashMap, world: hecs::World, graph: Graph, /// Voxel data that has been fetched from a savefile but not yet introduced to the graph @@ -52,7 +54,8 @@ impl Sim { pub fn new(cfg: Arc, save: &save::Save) -> Self { let mut result = Self { step: 0, - id_generator: IDGenerator::new(), + rng: SmallRng::from_entropy(), + entity_ids: FxHashMap::default(), world: hecs::World::new(), graph: Graph::new(cfg.chunk_size), preloaded_voxel_data: FxHashMap::default(), @@ -187,7 +190,7 @@ impl Sim { } pub fn spawn_character(&mut self, hello: ClientHello) -> (EntityId, Entity) { - let id = self.id_generator.new_id(); + let id = self.new_id(); info!(%id, name = %hello.name, "spawning character"); let position = Position { node: NodeId::ROOT, @@ -210,7 +213,7 @@ impl Sim { }; let entity = self.world.spawn((id, position, character, initial_input)); self.graph_entities.insert(position.node, entity); - self.id_generator.entity_ids.insert(id, entity); + self.entity_ids.insert(id, entity); self.spawns.push(entity); self.dirty_nodes.insert(position.node); (id, entity) @@ -230,7 +233,7 @@ impl Sim { pub fn destroy(&mut self, entity: Entity) { let id = *self.world.get::<&EntityId>(entity).unwrap(); - self.id_generator.entity_ids.remove(&id); + self.entity_ids.remove(&id); if let Ok(position) = self.world.get::<&Position>(entity) { self.graph_entities.remove(position.node, entity); } @@ -368,10 +371,10 @@ impl Sim { let pending_ticker_spawns = std::mem::take(&mut self.pending_ticker_spawns); for (position, ticker) in pending_ticker_spawns { - let id = self.id_generator.new_id(); + let id = self.new_id(); let entity = self.world.spawn((id, position, ticker)); self.graph_entities.insert(position.node, entity); - self.id_generator.entity_ids.insert(id, entity); + self.entity_ids.insert(id, entity); self.spawns.push(entity); self.dirty_nodes.insert(position.node); } @@ -437,6 +440,15 @@ impl Sim { self.step += 1; (spawns, delta) } + + pub fn new_id(&mut self) -> EntityId { + loop { + let id = self.rng.gen(); + if !self.entity_ids.contains_key(&id) { + return id; + } + } + } } fn dump_entity(world: &hecs::World, entity: Entity) -> Vec { From c0bd1140230e889bb9fff7abd93375da09973b2b Mon Sep 17 00:00:00 2001 From: blockmath Date: Sun, 23 Jun 2024 16:34:43 -0700 Subject: [PATCH 07/18] I thought Rust complained about this. Guess not --- server/src/sim.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/server/src/sim.rs b/server/src/sim.rs index c3bf9c14..f53b5968 100644 --- a/server/src/sim.rs +++ b/server/src/sim.rs @@ -277,9 +277,7 @@ impl Sim { let _guard = span.enter(); { - let mut ticker_query = self.world.query::<&mut TickerEntity>(); - let ticker_iter = ticker_query.iter(); - for (_entity, ticker_entity) in ticker_iter { + for (_entity, ticker_entity) in self.world.query::<&mut TickerEntity>().iter() { ticker_entity.ticker.tick(self.step); } } From bd7f9955de11b6112696da4d98d87934c39eaf78 Mon Sep 17 00:00:00 2001 From: blockmath Date: Sun, 23 Jun 2024 16:35:42 -0700 Subject: [PATCH 08/18] I thought Rust complained about this. Guess not --- server/src/sim.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/server/src/sim.rs b/server/src/sim.rs index f53b5968..40cf54e9 100644 --- a/server/src/sim.rs +++ b/server/src/sim.rs @@ -276,10 +276,8 @@ impl Sim { let span = error_span!("step", step = self.step); let _guard = span.enter(); - { - for (_entity, ticker_entity) in self.world.query::<&mut TickerEntity>().iter() { - ticker_entity.ticker.tick(self.step); - } + for (_entity, ticker_entity) in self.world.query::<&mut TickerEntity>().iter() { + ticker_entity.ticker.tick(self.step); } let mut pending_block_updates: Vec = vec![]; From 3bfcabd9fba838a12082980e9132ce2ed139a152 Mon Sep 17 00:00:00 2001 From: blockmath Date: Sun, 23 Jun 2024 17:26:47 -0700 Subject: [PATCH 09/18] Fixed issues brought up in review. --- client/src/local_character_controller.rs | 2 +- client/src/sim.rs | 2 +- common/src/proto.rs | 4 ++-- common/src/ticker.rs | 29 +++++------------------ server/src/sim.rs | 30 +++++++++++++----------- 5 files changed, 26 insertions(+), 41 deletions(-) diff --git a/client/src/local_character_controller.rs b/client/src/local_character_controller.rs index 6cc7e62a..a4197d7a 100644 --- a/client/src/local_character_controller.rs +++ b/client/src/local_character_controller.rs @@ -3,7 +3,7 @@ use common::{math, proto::Position}; pub struct LocalCharacterController { /// The last extrapolated inter-frame view position, used for rendering and gravity-specific /// orientation computations - pub position: Position, + position: Position, /// The up vector relative to position, ignoring orientation up: na::UnitVector3, diff --git a/client/src/sim.rs b/client/src/sim.rs index a47f59b4..8df31028 100644 --- a/client/src/sim.rs +++ b/client/src/sim.rs @@ -374,7 +374,7 @@ impl Sim { node = Some(x.node); builder.add(x); } - TickerEntity(x) => { + Blinker(x) => { builder.add(x); } }; diff --git a/common/src/proto.rs b/common/src/proto.rs index d6a70409..f219f610 100644 --- a/common/src/proto.rs +++ b/common/src/proto.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use crate::{ - dodeca, graph::NodeId, node::ChunkId, ticker::TickerEntity, voxel_math::Coords, + dodeca, graph::NodeId, node::ChunkId, ticker::Blinker, voxel_math::Coords, world::Material, EntityId, SimConfig, Step, }; @@ -91,7 +91,7 @@ pub struct SerializedVoxelData { pub enum Component { Character(Character), Position(Position), - TickerEntity(TickerEntity), + Blinker(Blinker), } #[derive(Debug, Serialize, Deserialize)] diff --git a/common/src/ticker.rs b/common/src/ticker.rs index f0219f54..9e0b6d59 100644 --- a/common/src/ticker.rs +++ b/common/src/ticker.rs @@ -1,35 +1,18 @@ -use crate::Step; use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize)] -pub struct TickerEntity { - pub last_ticked: Step, - pub ticker: Ticker, +pub struct Blinker { + pub on: bool, } -#[derive(Debug, Serialize, Deserialize)] -pub struct Ticker { - on: bool, -} - -impl Ticker { +impl Blinker { pub fn new() -> Self { Self { on: false } } - pub fn tick(&mut self, _step: Step) { - self.on = !self.on; - - // Just for testing - if self.on { - println!("Ticked ON!"); - } else { - println!("Ticked OFF!"); - } - } } -impl Default for Ticker { +impl Default for Blinker { fn default() -> Self { - Ticker::new() + Blinker::new() } -} +} \ No newline at end of file diff --git a/server/src/sim.rs b/server/src/sim.rs index 40cf54e9..b5b1d476 100644 --- a/server/src/sim.rs +++ b/server/src/sim.rs @@ -21,7 +21,7 @@ use common::{ Character, CharacterInput, CharacterState, ClientHello, Command, Component, FreshNode, Position, Spawns, StateDelta, }, - ticker::{Ticker, TickerEntity}, + ticker::Blinker, traversal::{ensure_nearby, nearby_nodes}, worldgen::ChunkParams, EntityId, SimConfig, Step, @@ -32,14 +32,13 @@ use crate::postcard_helpers::{self, SaveEntity}; pub struct Sim { cfg: Arc, step: Step, - pub rng: SmallRng, - pub entity_ids: FxHashMap, + rng: SmallRng, + entity_ids: FxHashMap, world: hecs::World, graph: Graph, /// Voxel data that has been fetched from a savefile but not yet introduced to the graph preloaded_voxel_data: FxHashMap, spawns: Vec, - pending_ticker_spawns: Vec<(Position, TickerEntity)>, despawns: Vec, graph_entities: GraphEntities, /// All nodes that have entity-related information yet to be saved @@ -60,7 +59,6 @@ impl Sim { graph: Graph::new(cfg.chunk_size), preloaded_voxel_data: FxHashMap::default(), spawns: Vec::new(), - pending_ticker_spawns: Vec::new(), despawns: Vec::new(), graph_entities: GraphEntities::new(), dirty_nodes: FxHashSet::default(), @@ -276,8 +274,14 @@ impl Sim { let span = error_span!("step", step = self.step); let _guard = span.enter(); - for (_entity, ticker_entity) in self.world.query::<&mut TickerEntity>().iter() { - ticker_entity.ticker.tick(self.step); + for (_entity, blinker) in self.world.query::<&mut Blinker>().iter() { + blinker.on = !blinker.on; + + if blinker.on { + tracing::info!("Blinked ON"); + } else { + tracing::info!("Blinked OFF"); + } } let mut pending_block_updates: Vec = vec![]; @@ -333,6 +337,8 @@ impl Sim { } } + let mut pending_blinker_spawns: Vec<(Position, Blinker)> = Vec::new(); + // Simulate for (entity, (position, character, input)) in self .world @@ -341,11 +347,8 @@ impl Sim { { let prev_node = position.node; if input.debug_spawn_blinker { - let ticker: TickerEntity = TickerEntity { - last_ticked: 0, - ticker: Ticker::new(), - }; - self.pending_ticker_spawns.push((*position, ticker)); + let blinker: Blinker = Blinker::new(); + pending_blinker_spawns.push((*position, blinker)); } character_controller::run_character_step( &self.cfg, @@ -365,8 +368,7 @@ impl Sim { self.dirty_nodes.insert(position.node); } - let pending_ticker_spawns = std::mem::take(&mut self.pending_ticker_spawns); - for (position, ticker) in pending_ticker_spawns { + for (position, ticker) in pending_blinker_spawns { let id = self.new_id(); let entity = self.world.spawn((id, position, ticker)); self.graph_entities.insert(position.node, entity); From bd3589aa1da70314196f00648c9856b6f05b93f4 Mon Sep 17 00:00:00 2001 From: blockmath Date: Sun, 23 Jun 2024 17:39:04 -0700 Subject: [PATCH 10/18] The blinker now syncs to client. --- client/src/sim.rs | 18 ++++++++++++++++++ common/src/proto.rs | 1 + common/src/ticker.rs | 2 +- server/src/sim.rs | 9 +++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/client/src/sim.rs b/client/src/sim.rs index 8df31028..27ebf542 100644 --- a/client/src/sim.rs +++ b/client/src/sim.rs @@ -18,6 +18,7 @@ use common::{ self, BlockUpdate, Character, CharacterInput, CharacterState, Command, Component, Position, }, sanitize_motion_input, + ticker::Blinker, world::Material, EntityId, GraphEntities, SimConfig, Step, }; @@ -251,6 +252,9 @@ impl Sim { for &(id, ref new_state) in &msg.character_states { self.update_character_state(id, new_state); } + for &(id, ref new_blinker) in &msg.blinker_states { + self.update_blinker_state(id, new_blinker); + } self.reconcile_prediction(msg.latest_input); } } @@ -286,6 +290,20 @@ impl Sim { } } + fn update_blinker_state(&mut self, id: EntityId, new_blinker: &Blinker) { + match self.entity_ids.get(&id) { + None => debug!(%id, "blinker state update for unknown entity"), + Some(&entity) => match self.world.get::<&mut Blinker>(entity) { + Ok(mut blinker) => { + blinker.on = new_blinker.on; + } + Err(e) => { + error!(%id, "blinker state update error: {}", e) + } + }, + } + } + fn reconcile_prediction(&mut self, latest_input: u16) { let id = self.local_character_id; let Some(&entity) = self.entity_ids.get(&id) else { diff --git a/common/src/proto.rs b/common/src/proto.rs index f219f610..af064c1a 100644 --- a/common/src/proto.rs +++ b/common/src/proto.rs @@ -38,6 +38,7 @@ pub struct StateDelta { pub latest_input: u16, pub positions: Vec<(EntityId, Position)>, pub character_states: Vec<(EntityId, CharacterState)>, + pub blinker_states: Vec<(EntityId, Blinker)>, } #[derive(Debug, Clone, Serialize, Deserialize)] diff --git a/common/src/ticker.rs b/common/src/ticker.rs index 9e0b6d59..056354ba 100644 --- a/common/src/ticker.rs +++ b/common/src/ticker.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Blinker { pub on: bool, } diff --git a/server/src/sim.rs b/server/src/sim.rs index b5b1d476..1afce4f3 100644 --- a/server/src/sim.rs +++ b/server/src/sim.rs @@ -433,6 +433,12 @@ impl Sim { .iter() .map(|(_, (&id, ch))| (id, ch.state.clone())) .collect(), + blinker_states: self + .world + .query::<(&EntityId, &Blinker)>() + .iter() + .map(|(_, (&id, blinker))| (id, blinker.clone())) + .collect(), }; self.step += 1; @@ -457,5 +463,8 @@ fn dump_entity(world: &hecs::World, entity: Entity) -> Vec { if let Ok(x) = world.get::<&Character>(entity) { components.push(Component::Character((*x).clone())); } + if let Ok(x) = world.get::<&Blinker>(entity) { + components.push(Component::Blinker((*x).clone())); + } components } From 8c8d39df875025d79b27e0fd984a3555e1bd5591 Mon Sep 17 00:00:00 2001 From: blockmath Date: Sun, 23 Jun 2024 17:42:57 -0700 Subject: [PATCH 11/18] formatting --- common/src/proto.rs | 4 ++-- common/src/ticker.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/common/src/proto.rs b/common/src/proto.rs index af064c1a..be73542e 100644 --- a/common/src/proto.rs +++ b/common/src/proto.rs @@ -1,8 +1,8 @@ use serde::{Deserialize, Serialize}; use crate::{ - dodeca, graph::NodeId, node::ChunkId, ticker::Blinker, voxel_math::Coords, - world::Material, EntityId, SimConfig, Step, + dodeca, graph::NodeId, node::ChunkId, ticker::Blinker, voxel_math::Coords, world::Material, + EntityId, SimConfig, Step, }; #[derive(Debug, Serialize, Deserialize)] diff --git a/common/src/ticker.rs b/common/src/ticker.rs index 056354ba..88641b44 100644 --- a/common/src/ticker.rs +++ b/common/src/ticker.rs @@ -15,4 +15,4 @@ impl Default for Blinker { fn default() -> Self { Blinker::new() } -} \ No newline at end of file +} From f4c5e364b184b246acba96e86e3835162ffa101f Mon Sep 17 00:00:00 2001 From: blockmath Date: Sun, 23 Jun 2024 17:53:46 -0700 Subject: [PATCH 12/18] Fixed nits. --- client/src/sim.rs | 2 +- common/src/{ticker.rs => blinker.rs} | 0 common/src/lib.rs | 2 +- common/src/proto.rs | 2 +- server/src/sim.rs | 6 +++--- 5 files changed, 6 insertions(+), 6 deletions(-) rename common/src/{ticker.rs => blinker.rs} (100%) diff --git a/client/src/sim.rs b/client/src/sim.rs index 27ebf542..f06b4c56 100644 --- a/client/src/sim.rs +++ b/client/src/sim.rs @@ -9,6 +9,7 @@ use crate::{ prediction::PredictedMotion, Net, }; use common::{ + blinker::Blinker, character_controller, collision_math::Ray, graph::{Graph, NodeId}, @@ -18,7 +19,6 @@ use common::{ self, BlockUpdate, Character, CharacterInput, CharacterState, Command, Component, Position, }, sanitize_motion_input, - ticker::Blinker, world::Material, EntityId, GraphEntities, SimConfig, Step, }; diff --git a/common/src/ticker.rs b/common/src/blinker.rs similarity index 100% rename from common/src/ticker.rs rename to common/src/blinker.rs diff --git a/common/src/lib.rs b/common/src/lib.rs index 89f8d468..a9776652 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -9,6 +9,7 @@ use rand::{ mod id; extern crate nalgebra as na; +pub mod blinker; pub mod character_controller; pub mod chunk_collision; mod chunk_ray_casting; @@ -29,7 +30,6 @@ mod plane; pub mod proto; mod sim_config; pub mod terraingen; -pub mod ticker; pub mod traversal; pub mod voxel_math; pub mod world; diff --git a/common/src/proto.rs b/common/src/proto.rs index be73542e..1d69afa6 100644 --- a/common/src/proto.rs +++ b/common/src/proto.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use crate::{ - dodeca, graph::NodeId, node::ChunkId, ticker::Blinker, voxel_math::Coords, world::Material, + blinker::Blinker, dodeca, graph::NodeId, node::ChunkId, voxel_math::Coords, world::Material, EntityId, SimConfig, Step, }; diff --git a/server/src/sim.rs b/server/src/sim.rs index 1afce4f3..c072209f 100644 --- a/server/src/sim.rs +++ b/server/src/sim.rs @@ -13,6 +13,7 @@ use save::ComponentType; use tracing::{error, error_span, info, trace}; use common::{ + blinker::Blinker, character_controller, dodeca, graph::{Graph, NodeId}, math, @@ -21,7 +22,6 @@ use common::{ Character, CharacterInput, CharacterState, ClientHello, Command, Component, FreshNode, Position, Spawns, StateDelta, }, - ticker::Blinker, traversal::{ensure_nearby, nearby_nodes}, worldgen::ChunkParams, EntityId, SimConfig, Step, @@ -31,8 +31,8 @@ use crate::postcard_helpers::{self, SaveEntity}; pub struct Sim { cfg: Arc, - step: Step, rng: SmallRng, + step: Step, entity_ids: FxHashMap, world: hecs::World, graph: Graph, @@ -52,8 +52,8 @@ pub struct Sim { impl Sim { pub fn new(cfg: Arc, save: &save::Save) -> Self { let mut result = Self { - step: 0, rng: SmallRng::from_entropy(), + step: 0, entity_ids: FxHashMap::default(), world: hecs::World::new(), graph: Graph::new(cfg.chunk_size), From ebb79ad69708c303a396e6b92c26efc9c46c60ba Mon Sep 17 00:00:00 2001 From: blockmath Date: Sun, 23 Jun 2024 17:59:02 -0700 Subject: [PATCH 13/18] I thought I unpubbed new_id, guess not --- server/src/sim.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/sim.rs b/server/src/sim.rs index c072209f..61f01da9 100644 --- a/server/src/sim.rs +++ b/server/src/sim.rs @@ -445,7 +445,7 @@ impl Sim { (spawns, delta) } - pub fn new_id(&mut self) -> EntityId { + fn new_id(&mut self) -> EntityId { loop { let id = self.rng.gen(); if !self.entity_ids.contains_key(&id) { From 7817d3a6d598a484bee8473280dec13fdc5aa0e4 Mon Sep 17 00:00:00 2001 From: blockmath Date: Sun, 23 Jun 2024 18:02:20 -0700 Subject: [PATCH 14/18] Also removed the last instance of ticker that I forgot about. --- server/src/sim.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/src/sim.rs b/server/src/sim.rs index 61f01da9..b6fa4ca2 100644 --- a/server/src/sim.rs +++ b/server/src/sim.rs @@ -368,9 +368,9 @@ impl Sim { self.dirty_nodes.insert(position.node); } - for (position, ticker) in pending_blinker_spawns { + for (position, blinker) in pending_blinker_spawns { let id = self.new_id(); - let entity = self.world.spawn((id, position, ticker)); + let entity = self.world.spawn((id, position, blinker)); self.graph_entities.insert(position.node, entity); self.entity_ids.insert(id, entity); self.spawns.push(entity); From a59bfd34e25f7b2977a12cb9e5b14fd44c91e71d Mon Sep 17 00:00:00 2001 From: blockmath Date: Sun, 23 Jun 2024 18:11:44 -0700 Subject: [PATCH 15/18] Merged the blinker into proto.rs --- client/src/sim.rs | 4 ++-- common/src/blinker.rs | 18 ------------------ common/src/lib.rs | 1 - common/src/proto.rs | 9 +++++++-- server/src/sim.rs | 7 +++---- 5 files changed, 12 insertions(+), 27 deletions(-) delete mode 100644 common/src/blinker.rs diff --git a/client/src/sim.rs b/client/src/sim.rs index f06b4c56..68db9c99 100644 --- a/client/src/sim.rs +++ b/client/src/sim.rs @@ -9,14 +9,14 @@ use crate::{ prediction::PredictedMotion, Net, }; use common::{ - blinker::Blinker, character_controller, collision_math::Ray, graph::{Graph, NodeId}, graph_ray_casting, node::{populate_fresh_nodes, ChunkId, VoxelData}, proto::{ - self, BlockUpdate, Character, CharacterInput, CharacterState, Command, Component, Position, + self, Blinker, BlockUpdate, Character, CharacterInput, CharacterState, Command, Component, + Position, }, sanitize_motion_input, world::Material, diff --git a/common/src/blinker.rs b/common/src/blinker.rs deleted file mode 100644 index 88641b44..00000000 --- a/common/src/blinker.rs +++ /dev/null @@ -1,18 +0,0 @@ -use serde::{Deserialize, Serialize}; - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct Blinker { - pub on: bool, -} - -impl Blinker { - pub fn new() -> Self { - Self { on: false } - } -} - -impl Default for Blinker { - fn default() -> Self { - Blinker::new() - } -} diff --git a/common/src/lib.rs b/common/src/lib.rs index a9776652..a8d8e097 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -9,7 +9,6 @@ use rand::{ mod id; extern crate nalgebra as na; -pub mod blinker; pub mod character_controller; pub mod chunk_collision; mod chunk_ray_casting; diff --git a/common/src/proto.rs b/common/src/proto.rs index 1d69afa6..1905d359 100644 --- a/common/src/proto.rs +++ b/common/src/proto.rs @@ -1,8 +1,8 @@ use serde::{Deserialize, Serialize}; use crate::{ - blinker::Blinker, dodeca, graph::NodeId, node::ChunkId, voxel_math::Coords, world::Material, - EntityId, SimConfig, Step, + dodeca, graph::NodeId, node::ChunkId, voxel_math::Coords, world::Material, EntityId, SimConfig, + Step, }; #[derive(Debug, Serialize, Deserialize)] @@ -107,3 +107,8 @@ pub struct Character { pub name: String, pub state: CharacterState, } + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Blinker { + pub on: bool, +} diff --git a/server/src/sim.rs b/server/src/sim.rs index b6fa4ca2..8975988c 100644 --- a/server/src/sim.rs +++ b/server/src/sim.rs @@ -13,14 +13,13 @@ use save::ComponentType; use tracing::{error, error_span, info, trace}; use common::{ - blinker::Blinker, character_controller, dodeca, graph::{Graph, NodeId}, math, node::{populate_fresh_nodes, Chunk}, proto::{ - Character, CharacterInput, CharacterState, ClientHello, Command, Component, FreshNode, - Position, Spawns, StateDelta, + Blinker, Character, CharacterInput, CharacterState, ClientHello, Command, Component, + FreshNode, Position, Spawns, StateDelta, }, traversal::{ensure_nearby, nearby_nodes}, worldgen::ChunkParams, @@ -347,7 +346,7 @@ impl Sim { { let prev_node = position.node; if input.debug_spawn_blinker { - let blinker: Blinker = Blinker::new(); + let blinker: Blinker = Blinker { on: false }; pending_blinker_spawns.push((*position, blinker)); } character_controller::run_character_step( From ac4deacfc6b524df11ecaa6c78cd6b18988b77df Mon Sep 17 00:00:00 2001 From: blockmath Date: Sun, 23 Jun 2024 18:13:21 -0700 Subject: [PATCH 16/18] Fixed nit. --- client/src/sim.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/sim.rs b/client/src/sim.rs index 68db9c99..a6209f8f 100644 --- a/client/src/sim.rs +++ b/client/src/sim.rs @@ -295,7 +295,7 @@ impl Sim { None => debug!(%id, "blinker state update for unknown entity"), Some(&entity) => match self.world.get::<&mut Blinker>(entity) { Ok(mut blinker) => { - blinker.on = new_blinker.on; + *blinker = new_blinker.clone(); } Err(e) => { error!(%id, "blinker state update error: {}", e) From 2b709c4a2fc449dea8971fe1276d26634ff92ce6 Mon Sep 17 00:00:00 2001 From: blockmath Date: Mon, 24 Jun 2024 15:04:12 -0700 Subject: [PATCH 17/18] Fixed lingering issues with merge/merge conflict. --- server/src/sim.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/server/src/sim.rs b/server/src/sim.rs index afd12dac..d60f355c 100644 --- a/server/src/sim.rs +++ b/server/src/sim.rs @@ -300,8 +300,6 @@ impl Sim { } } - let mut pending_block_updates: Vec = vec![]; - // Extend graph structure for (_, (position, _)) in self.world.query::<(&mut Position, &mut Character)>().iter() { ensure_nearby(&mut self.graph, position, self.cfg.view_distance); @@ -387,16 +385,16 @@ impl Sim { } self.dirty_nodes.insert(position.node); } - + for (position, blinker) in pending_blinker_spawns { let id = self.new_id(); let entity = self.world.spawn((id, position, blinker)); self.graph_entities.insert(position.node, entity); self.entity_ids.insert(id, entity); - self.spawns.push(entity); + self.accumulated_changes.spawns.push(entity); self.dirty_nodes.insert(position.node); } - + for (entity, block_update) in pending_block_updates { let id = *self.world.get::<&EntityId>(entity).unwrap(); self.attempt_block_update(id, block_update); From c3f0a3ee4d4b94e95c5f33382aba94439e44314e Mon Sep 17 00:00:00 2001 From: blockmath Date: Mon, 24 Jun 2024 17:54:30 -0700 Subject: [PATCH 18/18] Used the cool new spawn construct. --- server/src/sim.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/server/src/sim.rs b/server/src/sim.rs index d60f355c..42f33e3b 100644 --- a/server/src/sim.rs +++ b/server/src/sim.rs @@ -387,12 +387,7 @@ impl Sim { } for (position, blinker) in pending_blinker_spawns { - let id = self.new_id(); - let entity = self.world.spawn((id, position, blinker)); - self.graph_entities.insert(position.node, entity); - self.entity_ids.insert(id, entity); - self.accumulated_changes.spawns.push(entity); - self.dirty_nodes.insert(position.node); + self.spawn((position, blinker)); } for (entity, block_update) in pending_block_updates {