From 4adb963c8e830e30c27e0385c70e544c9af2c814 Mon Sep 17 00:00:00 2001 From: Daniel Mizerski Date: Fri, 7 May 2021 16:32:52 +0200 Subject: [PATCH] (Wiring) Parametrize OutputFormulas with OutputFormulas --- server/src/cmd.rs | 69 ++++++++--------------- server/src/sys/wiring/dispatch_actions.rs | 8 ++- server/src/wiring.rs | 44 ++++++++++++--- 3 files changed, 66 insertions(+), 55 deletions(-) diff --git a/server/src/cmd.rs b/server/src/cmd.rs index 61fc9c55af..16cc9ed219 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -4,7 +4,7 @@ use crate::{ settings::{BanRecord, EditableSetting}, - wiring::{Logic, OutputFormula}, + wiring::{BoxConst, Const, Input, Logic, OutputFormula}, Server, SpawnPoint, StateExt, }; use assets::AssetExt; @@ -1723,57 +1723,40 @@ fn handle_spawn_wiring( pos.0.x += 3.0; let mut outputs1 = HashMap::new(); - outputs1.insert( - "deaths_last_tick".to_string(), - wiring::OutputFormula::OnDeath { - value: 1.0, - radius: 30.0, - }, - ); + outputs1.insert("deaths_last_tick".to_string(), OutputFormula::OnDeath { + value: BoxConst(1.0), + radius: BoxConst(30.0), + }); outputs1.insert( "deaths_accumulated".to_string(), OutputFormula::Logic(Box::new(Logic { kind: wiring::LogicKind::Sum, left: OutputFormula::Logic(Box::new(Logic { kind: wiring::LogicKind::Sub, - left: OutputFormula::Input { - name: "deaths_accumulated".to_string(), - }, + left: Input("deaths_accumulated"), right: OutputFormula::Logic(Box::new(Logic { kind: wiring::LogicKind::Min, - left: OutputFormula::Input { - name: "pressed".to_string(), - }, - right: OutputFormula::Input { - name: "deaths_accumulated".to_string(), - }, + left: Input("pressed"), + right: Input("deaths_accumulated"), })), })), - right: OutputFormula::Input { - name: "deaths_last_tick".to_string(), - }, + right: Input("deaths_last_tick"), })), ); outputs1.insert("pressed".to_string(), OutputFormula::OnCollide { - value: f32::MAX, + value: Box::new(OutputFormula::Constant { value: f32::MAX }), }); let builder1 = server .state .create_wiring(pos, comp::object::Body::Coins, WiringElement { actions: vec![WiringAction { - formula: wiring::OutputFormula::Constant { value: 1.0 }, - threshold: 1.0, + formula: Const(1.0), + threshold: Const(1.0), effects: vec![WiringActionEffect::SetLight { - r: wiring::OutputFormula::Input { - name: String::from("color"), - }, - g: wiring::OutputFormula::Input { - name: String::from("color"), - }, - b: wiring::OutputFormula::Input { - name: String::from("color"), - }, + r: Input("color"), + g: Input("color"), + b: Input("color"), }], }], inputs: HashMap::new(), @@ -1788,10 +1771,8 @@ fn handle_spawn_wiring( .create_wiring(pos, comp::object::Body::Coins, WiringElement { actions: vec![ WiringAction { - formula: wiring::OutputFormula::Input { - name: String::from("deaths_accumulated"), - }, - threshold: 5.0, + formula: Input("deaths_accumulated"), + threshold: Const(5.0), effects: vec![WiringActionEffect::SpawnProjectile { constr: comp::ProjectileConstructor::Arrow { damage: 1.0, @@ -1801,18 +1782,12 @@ fn handle_spawn_wiring( }], }, WiringAction { - formula: wiring::OutputFormula::Constant { value: 1.0 }, - threshold: 1.0, + formula: Const(1.0), + threshold: Const(1.0), effects: vec![WiringActionEffect::SetLight { - r: wiring::OutputFormula::Input { - name: String::from("color"), - }, - g: wiring::OutputFormula::Input { - name: String::from("color"), - }, - b: wiring::OutputFormula::Input { - name: String::from("color"), - }, + r: Input("color"), + g: Input("color"), + b: Input("color"), }], }, ], diff --git a/server/src/sys/wiring/dispatch_actions.rs b/server/src/sys/wiring/dispatch_actions.rs index 641275a006..bce31b0491 100644 --- a/server/src/sys/wiring/dispatch_actions.rs +++ b/server/src/sys/wiring/dispatch_actions.rs @@ -46,7 +46,13 @@ pub fn dispatch_actions(system_data: &mut WiringData) { physics_state, entities_died_last_tick, pos, - ) >= wiring_action.threshold + ) >= compute_output( + &wiring_action.threshold, + &wiring_element.inputs, + physics_state, + entities_died_last_tick, + pos, + ) }) .for_each(|wiring_action| { dispatch_action( diff --git a/server/src/wiring.rs b/server/src/wiring.rs index 620dd38ff1..f1e34526b3 100644 --- a/server/src/wiring.rs +++ b/server/src/wiring.rs @@ -22,14 +22,28 @@ pub struct Circuit { } pub enum OutputFormula { - Constant { value: f32 }, - Input { name: String }, + Constant { + value: f32, + }, + Input { + name: String, + }, Logic(Box), - SineWave { amplitude: f32, frequency: f32 }, - OnCollide { value: f32 }, - OnInteract { value: f32 }, - OnDeath { value: f32, radius: f32 }, + SineWave { + amplitude: Box, + frequency: Box, + }, + OnCollide { + value: Box, + }, + OnInteract { + value: Box, + }, + OnDeath { + value: Box, + radius: Box, + }, } pub enum LogicKind { @@ -42,7 +56,7 @@ pub enum LogicKind { pub struct WiringAction { pub formula: OutputFormula, - pub threshold: f32, + pub threshold: OutputFormula, pub effects: Vec, } @@ -75,3 +89,19 @@ impl Component for WiringElement { impl Component for Circuit { type Storage = IdvStorage; } + +pub fn BoxConst(constant_value: f32) -> Box { Box::new(Const(constant_value)) } + +pub fn Const(constant_value: f32) -> OutputFormula { + OutputFormula::Constant { + value: constant_value, + } +} + +pub fn BoxInput(name: &str) -> Box { Box::new(Input(name)) } + +pub fn Input(name: &str) -> OutputFormula { + OutputFormula::Input { + name: name.to_string(), + } +}