From 9864344cfe72f98f9ac0698f364e4118b91220a0 Mon Sep 17 00:00:00 2001 From: Knightress Paladin Date: Wed, 14 Jul 2021 22:17:05 -0700 Subject: [PATCH 1/2] Implemented setting blocks with the wiring system --- server/src/cmd.rs | 15 ++++++++- server/src/sys/wiring.rs | 4 ++- server/src/sys/wiring/dispatch_actions.rs | 38 ++++++++++++++++++++--- server/src/wiring.rs | 7 ++--- 4 files changed, 54 insertions(+), 10 deletions(-) diff --git a/server/src/cmd.rs b/server/src/cmd.rs index 239a73b95e..14bf15ab67 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -1944,6 +1944,7 @@ fn handle_spawn_wiring( inputs: HashMap::new(), outputs: outputs1, }) + .with(comp::Density(100_f32)) .with(comp::Sticky); let ent1 = builder1.build(); @@ -1965,6 +1966,16 @@ fn handle_spawn_wiring( }, }], }, + WiringAction { + formula: wiring::OutputFormula::Input { + name: String::from("deaths_accumulated"), + }, + threshold: 1.0, + effects: vec![WiringActionEffect::SetBlock { + coords: vek::Vec3::new(0, 0, pos.0.z as i32), + block: Block::new(BlockKind::Water, vek::Rgb::new(0, 0, 0)), + }], + }, WiringAction { formula: wiring::OutputFormula::Constant { value: 1.0 }, threshold: 1.0, @@ -1983,7 +1994,8 @@ fn handle_spawn_wiring( ], inputs: HashMap::new(), outputs: HashMap::new(), - }); + }) + .with(comp::Density(100_f32)); let ent2 = builder2.build(); pos.0.x += 3.0; @@ -1994,6 +2006,7 @@ fn handle_spawn_wiring( inputs: HashMap::new(), outputs: HashMap::new(), }) + .with(comp::Density(comp::object::Body::TrainingDummy.density().0)) .with(Circuit { wires: vec![ Wire { diff --git a/server/src/sys/wiring.rs b/server/src/sys/wiring.rs index 0f0407e7e0..28da0afbb2 100644 --- a/server/src/sys/wiring.rs +++ b/server/src/sys/wiring.rs @@ -5,9 +5,10 @@ use common::{ resources::EntitiesDiedLastTick, }; use common_ecs::{Job, Origin, Phase, System}; +use common_state::BlockChange; use hashbrown::HashMap; use specs::{ - join::Join, shred::ResourceId, Entities, Entity, Read, ReadStorage, SystemData, World, + join::Join, shred::ResourceId, Entities, Entity, Read, ReadStorage, SystemData, World, Write, WriteStorage, }; mod compute_outputs; @@ -28,6 +29,7 @@ pub struct WiringData<'a> { pub event_bus: Read<'a, EventBus>, pub entities_died_last_tick: Read<'a, EntitiesDiedLastTick>, + pub block_change: Write<'a, BlockChange>, } /// This system is responsible for handling wiring (signals and wiring systems) diff --git a/server/src/sys/wiring/dispatch_actions.rs b/server/src/sys/wiring/dispatch_actions.rs index 28730134b6..782b7bba62 100644 --- a/server/src/sys/wiring/dispatch_actions.rs +++ b/server/src/sys/wiring/dispatch_actions.rs @@ -6,11 +6,13 @@ use common::{ comp::{object, Body, LightEmitter, PhysicsState, Pos, ProjectileConstructor}, event::{Emitter, ServerEvent}, resources::EntitiesDiedLastTick, + terrain::{block::Block, TerrainChunkSize}, util::Dir, + vol::RectVolSize, }; +use common_state::BlockChange; use hashbrown::HashMap; -use specs::{join::Join, Entity, Read}; -use tracing::warn; +use specs::{join::Join, Entity, Read, Write}; use vek::Rgb; pub fn dispatch_actions(system_data: &mut WiringData) { @@ -21,6 +23,7 @@ pub fn dispatch_actions(system_data: &mut WiringData) { physics_states, light_emitters, entities_died_last_tick, + block_change, pos, .. } = system_data; @@ -57,6 +60,7 @@ pub fn dispatch_actions(system_data: &mut WiringData) { &mut light_emitter, physics_state, entities_died_last_tick, + block_change, pos, ); }) @@ -75,13 +79,14 @@ fn dispatch_action( light_emitter: &mut Option>, physics_state: Option<&PhysicsState>, entities_died_last_tick: &Read, + block_change: &mut Write, pos: Option<&Pos>, ) { action_effects .iter() .for_each(|action_effect| match action_effect { - WiringActionEffect::SetBlockCollidability { .. } => { - warn!("Not implemented WiringActionEffect::SetBlockCollidability") + WiringActionEffect::SetBlock { coords, block } => { + dispatch_action_set_block(*coords, *block, block_change, pos); }, WiringActionEffect::SpawnProjectile { constr } => { dispatch_action_spawn_projectile(entity, constr, server_emitter) @@ -140,3 +145,28 @@ fn dispatch_action_set_light( light_emitter.col = Rgb::new(computed_r, computed_g, computed_b); } } + +#[allow(clippy::too_many_arguments)] +fn dispatch_action_set_block( + coord: vek::Vec3, + block: Block, + block_change: &mut Write, + pos: Option<&Pos>, +) { + let chunk_origin = match pos { + Some(opos) => vek::Vec3::new( + (opos.0.x as i32 / TerrainChunkSize::RECT_SIZE.x as i32) + * TerrainChunkSize::RECT_SIZE.x as i32, + (opos.0.y as i32 / TerrainChunkSize::RECT_SIZE.y as i32) + * TerrainChunkSize::RECT_SIZE.y as i32, + 0, + ), + None => vek::Vec3::new(0, 0, 0), + }; + let offset_pos = chunk_origin + .iter() + .zip(coord.iter()) + .map(|(p, c)| p + c) + .collect(); + block_change.set(offset_pos, block); +} diff --git a/server/src/wiring.rs b/server/src/wiring.rs index 620dd38ff1..9759a83e30 100644 --- a/server/src/wiring.rs +++ b/server/src/wiring.rs @@ -1,5 +1,4 @@ -use common::comp::ProjectileConstructor; -use core::f32; +use common::{comp::ProjectileConstructor, terrain::Block}; use hashbrown::HashMap; use specs::{Component, Entity}; use specs_idvs::IdvStorage; @@ -50,9 +49,9 @@ pub enum WiringActionEffect { SpawnProjectile { constr: ProjectileConstructor, }, - SetBlockCollidability { + SetBlock { coords: Vec3, - collidable: bool, + block: Block, }, SetLight { r: OutputFormula, From 0ded44d635610383c55e04f9bd21f9eaea46b1ff Mon Sep 17 00:00:00 2001 From: Knightress Paladin Date: Thu, 15 Jul 2021 12:35:29 -0700 Subject: [PATCH 2/2] Cleaned up chunk-relative block setting math --- server/src/sys/wiring/dispatch_actions.rs | 25 +++++++++-------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/server/src/sys/wiring/dispatch_actions.rs b/server/src/sys/wiring/dispatch_actions.rs index 782b7bba62..d5ae4dc6ad 100644 --- a/server/src/sys/wiring/dispatch_actions.rs +++ b/server/src/sys/wiring/dispatch_actions.rs @@ -153,20 +153,15 @@ fn dispatch_action_set_block( block_change: &mut Write, pos: Option<&Pos>, ) { - let chunk_origin = match pos { - Some(opos) => vek::Vec3::new( - (opos.0.x as i32 / TerrainChunkSize::RECT_SIZE.x as i32) - * TerrainChunkSize::RECT_SIZE.x as i32, - (opos.0.y as i32 / TerrainChunkSize::RECT_SIZE.y as i32) - * TerrainChunkSize::RECT_SIZE.y as i32, - 0, - ), - None => vek::Vec3::new(0, 0, 0), - }; - let offset_pos = chunk_origin - .iter() - .zip(coord.iter()) - .map(|(p, c)| p + c) - .collect(); + let chunk_origin = pos + .map(|opos| { + opos.0 + .xy() + .as_::() + .map2(TerrainChunkSize::RECT_SIZE.as_::(), |a, b| (a / b) * b) + .with_z(0) + }) + .unwrap_or_else(vek::Vec3::zero); + let offset_pos = chunk_origin + coord; block_change.set(offset_pos, block); }