From bd1da23cf18960b36f8683c09899044d64b4bd83 Mon Sep 17 00:00:00 2001 From: troido Date: Thu, 21 May 2020 12:25:36 +0200 Subject: made Talk its own component/system instead of part of interact --- content/encyclopediae/default_encyclopedia.json | 4 ++-- src/componentparameter.rs | 1 + src/components/interactable.rs | 6 ++++++ src/components/mod.rs | 2 +- src/componentwrapper.rs | 12 +++++++----- src/room.rs | 8 +++++--- src/systems/mod.rs | 4 +++- 7 files changed, 25 insertions(+), 12 deletions(-) diff --git a/content/encyclopediae/default_encyclopedia.json b/content/encyclopediae/default_encyclopedia.json index 18cf967..fdaa42f 100644 --- a/content/encyclopediae/default_encyclopedia.json +++ b/content/encyclopediae/default_encyclopedia.json @@ -22,7 +22,7 @@ "arguments": [["health", "int", 100]], "components": [ ["Health", {"health": ["arg", "health"], "maxhealth": 100}], - ["Loot", {"loot": ["list", [{"type": "stone"}]]}] + ["Loot", {"loot": ["list", [["list", [{"type": "stone"}, 1.0]]]]}] ], "sprite": "builtwall", "height": 2, @@ -87,7 +87,7 @@ "height": 1, "flags": ["Occupied"], "components": [ - ["Interactable", {"action": ["interaction", ["say", "Good morning there, World"]]}] + ["Talkable", {"text": "Good morning there, World"}] ] }, "quarry": { diff --git a/src/componentparameter.rs b/src/componentparameter.rs index 333c6e1..30a7d91 100644 --- a/src/componentparameter.rs +++ b/src/componentparameter.rs @@ -120,6 +120,7 @@ impl ComponentParameter { } } + #[allow(dead_code)] pub fn get_type(&self, arguments: &[(String, ParameterType, Option)]) -> Result{ Ok(match self { Self::Constant(param) => param.paramtype(), diff --git a/src/components/interactable.rs b/src/components/interactable.rs index d97b742..636953f 100644 --- a/src/components/interactable.rs +++ b/src/components/interactable.rs @@ -74,3 +74,9 @@ impl Interactable { } } } + +#[derive(Component, Debug, Clone, PartialEq)] +#[storage(HashMapStorage)] +pub struct Talkable { + pub text: String +} diff --git a/src/components/mod.rs b/src/components/mod.rs index ee5176e..23f5488 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -16,7 +16,7 @@ pub use messages::{ TriggerBox }; pub use faction::Faction; -pub use interactable::Interactable; +pub use interactable::{Interactable, Talkable}; pub use equipment::Equipment; pub use inventory::Inventory; pub use serialise::Serialise; diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs index 2619d9c..e1b5bb8 100644 --- a/src/componentwrapper.rs +++ b/src/componentwrapper.rs @@ -50,12 +50,13 @@ macro_rules! components { ComponentType::$comp => Ok(Self::$comp({ use crate::components::$comp; $( - let $paramname = <$paramtype>::from_parameter( - parameters + let $paramname = { + let param = parameters .remove(stringify!($paramname)) - .ok_or(aerr!("required parameter '{}'not found", stringify!($paramname)))? - ) - .ok_or(aerr!("parameter {} is invalid type", stringify!($paramname)))?; + .ok_or(aerr!("required parameter '{}'not found", stringify!($paramname)))?; + <$paramtype>::from_parameter(param.clone()) + .ok_or(aerr!("parameter {} is invalid type: {:?} is not of type {}", stringify!($paramname), param, stringify!($paramtype)))? + }; )* $creation @@ -233,6 +234,7 @@ components!(all: } }; Substitute (into: Template); + Talkable (text: String); ); diff --git a/src/room.rs b/src/room.rs index 0b8ee3b..d39bdb1 100644 --- a/src/room.rs +++ b/src/room.rs @@ -70,7 +70,8 @@ use crate::{ Building, Deduplicate, SpawnTrigger, - Replace + Replace, + Talk } }; @@ -86,12 +87,13 @@ pub fn default_dispatcher<'a, 'b>() -> Dispatcher<'a, 'b> { .with(Take, "take", &["controlinput", "controlai"]) .with(Use, "use", &["controlinput", "controlai"]) .with(Interact, "interact", &["controlinput", "controlai"]) + .with(Talk, "talk", &["controlinput", "controlai"]) .with(SpawnTrigger, "spawntrigger", &["spawn", "deduplicate", "replace"]) .with(Move, "move", &["controlinput", "controlai"]) .with(Trapping, "trapping", &["move"]) .with(Fight, "fight", &["move"]) .with(Heal, "heal", &[]) - .with(Attacking, "attacking", &["use", "trapping", "fight", "heal", "interact", "spawntrigger"]) + .with(Attacking, "attacking", &["use", "trapping", "fight", "heal", "interact", "talk", "spawntrigger"]) .with(Die, "die", &["attacking"]) .with(DropLoot, "droploot", &["attacking"]) .with(Building, "building", &["attacking"]) @@ -125,7 +127,7 @@ impl <'a, 'b>Room<'a, 'b> { world.insert(NewEntities::new(encyclopedia)); register_insert!( world, - (Position, Visible, Controller, Movable, New, Removed, Moved, Player, Inventory, Health, Serialise, RoomExit, Entered, TriggerBox, Trap, Fighter, Healing, ControlCooldown, Autofight, MonsterAI, Home, AttackInbox, Item, Spawner, Clan, Faction, Interactable, Loot, Timer, Equipment, TimeOffset, Flags, Ear, Build, Whitelist, Dedup, Minable, LootHolder, OnSpawn, Substitute), + (Position, Visible, Controller, Movable, New, Removed, Moved, Player, Inventory, Health, Serialise, RoomExit, Entered, TriggerBox, Trap, Fighter, Healing, ControlCooldown, Autofight, MonsterAI, Home, AttackInbox, Item, Spawner, Clan, Faction, Interactable, Loot, Timer, Equipment, TimeOffset, Flags, Ear, Build, Whitelist, Dedup, Minable, LootHolder, OnSpawn, Substitute, Talkable), (Ground, Input, Output, Size, Spawn, Players, Emigration, Time, RoomPermissions) ); diff --git a/src/systems/mod.rs b/src/systems/mod.rs index 7323dee..56606e7 100644 --- a/src/systems/mod.rs +++ b/src/systems/mod.rs @@ -24,6 +24,7 @@ mod building; mod deduplicate; mod spawntrigger; mod replace; +mod talk; pub use self::{ controlinput::ControlInput, @@ -50,5 +51,6 @@ pub use self::{ building::Building, deduplicate::Deduplicate, spawntrigger::SpawnTrigger, - replace::Replace + replace::Replace, + talk::Talk }; -- cgit From f47034bdf86e7ddc831ecb8f50689b9b07a0f6ca Mon Sep 17 00:00:00 2001 From: troido Date: Thu, 21 May 2020 12:45:44 +0200 Subject: actually added talk system and removed reply interaction --- content/encyclopediae/npcs.json | 2 +- content/maps/room.json | 3 +-- src/components/interactable.rs | 6 ----- src/systems/interact.rs | 6 ----- src/systems/talk.rs | 56 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 15 deletions(-) create mode 100644 src/systems/talk.rs diff --git a/content/encyclopediae/npcs.json b/content/encyclopediae/npcs.json index bfaa6fe..4b8fd39 100644 --- a/content/encyclopediae/npcs.json +++ b/content/encyclopediae/npcs.json @@ -79,7 +79,7 @@ "height": 1.5, "flags": ["Occupied"], "components": [ - ["Interactable", {"action": ["interaction", ["reply", "did you say '{}'?"]]}] + ["Talkable", {"text": "Hey there, welcome to Asciifarm"}] ] }, "trader": { diff --git a/content/maps/room.json b/content/maps/room.json index 74cfcc0..173f79e 100644 --- a/content/maps/room.json +++ b/content/maps/room.json @@ -13,7 +13,7 @@ " ~~,,,,.,,,,,,,,,,,~~~,,,,,,,,,,,,,,,,,,,,X", " X,,,,,.,,,,,,,,,,,~~~~,,,,,,T,,,,,,,,,,,,X", " X,,,,,.,,,,,,,,,,,,~~~,,,,,,,,,,,,,,,,,,,X", - " X,,,,,.,,,,,,,,,,,,~~~,,,,,T,,,,######,,,X", + " X,,,,,.,u,,,,,,,,,,~~~,,,,,T,,,,######,,,X", " X,,,,,.,,,,,,,,,,,,bbb,,,,,,,,,,#++++#,,,X", " X,,,t..............bbb..........D++++#,,,X", " X,,,,,.,,,,,,,,,,,,bbb,,,,,,,,,,#++++#,,,X", @@ -52,7 +52,6 @@ "u": ["ground", "dude"], "t": ["ground", "trader"], "P": ["ground", "pickaxe"], - "u": ["ground", "radishes"], "Q": "quarry", " ": [] } diff --git a/src/components/interactable.rs b/src/components/interactable.rs index 636953f..ab1ac29 100644 --- a/src/components/interactable.rs +++ b/src/components/interactable.rs @@ -17,8 +17,6 @@ use crate::{ #[storage(HashMapStorage)] pub enum Interactable { Trigger(Trigger), - Say(String), - Reply(String), Exchange(String, HashMap), Visit(RoomId), Mine(Stat) @@ -32,8 +30,6 @@ impl Interactable { let arg = val.get(1)?; Some(match typ.as_str()? { "trigger" => Trigger(Trigger::from_str(arg.as_str()?)?), - "say" => Say(arg.as_str()?.to_string()), - "reply" => Reply(arg.as_str()?.to_string()), "exchange" => { let (prefix, change) = serde_json::value::from_value::< (String, HashMap, Vec)>) @@ -54,8 +50,6 @@ impl Interactable { pub fn accepts_arg(&self, arg: &Option) -> bool { match self { Trigger(_) => arg.is_none(), - Say(_) => arg.is_none(), - Reply(_) => arg.is_some(), Exchange(prefix, _exchanges) => { if let Some(txt) = arg { txt.starts_with(prefix) diff --git a/src/systems/interact.rs b/src/systems/interact.rs index b377a7a..2090f6f 100644 --- a/src/systems/interact.rs +++ b/src/systems/interact.rs @@ -82,12 +82,6 @@ impl <'a> System<'a> for Interact { Interactable::Trigger(trigger) => { TriggerBox::add_message(&mut triggerbox, ent, *trigger); } - Interactable::Say(text) => { - say(ear, text.clone(), name); - } - Interactable::Reply(text) => { - say(ear, text.replace("{}", &arg.unwrap()), name); - } Interactable::Exchange(prefix, exchanges) => { if let Some(txt) = arg { if let (Some(inventory), Some(action)) = (inventories.get_mut(actor), strip_prefix(&txt, prefix)) { diff --git a/src/systems/talk.rs b/src/systems/talk.rs new file mode 100644 index 0000000..4bb898a --- /dev/null +++ b/src/systems/talk.rs @@ -0,0 +1,56 @@ + +use std::collections::HashSet; + +use specs::{ + ReadStorage, + WriteStorage, + System, + Join, + Read +}; + +use crate::{ + components::{ + Controller, + Position, + Talkable, + Notification, + Ear, + Visible + }, + controls::{Control}, + resources::{Ground}, +}; + +pub struct Talk; +impl <'a> System<'a> for Talk { + type SystemData = ( + ReadStorage<'a, Controller>, + ReadStorage<'a, Position>, + Read<'a, Ground>, + ReadStorage<'a, Talkable>, + WriteStorage<'a, Ear>, + ReadStorage<'a, Visible> + ); + + fn run(&mut self, (controllers, positions, ground, talkables, mut ears, visibles): Self::SystemData) { + for (controller, position, ear) in (&controllers, &positions, &mut ears).join(){ + match &controller.control { + Control::Interact(directions, None) => { + 'targets: for direction in directions { + let pos = position.pos + direction.to_position(); + for ent in ground.cells.get(&pos).unwrap_or(&HashSet::new()) { + if let Some(Talkable{text}) = talkables.get(*ent) { + let name = visibles.get(*ent).map(|v| v.name.clone()); + ear.sounds.push(Notification::Sound{text: text.clone(), source: name}); + break 'targets; + } + } + } + } + _ => {} + } + } + } +} + -- cgit From 1899b27b791734a6b72e28cfb1420536c6035ee4 Mon Sep 17 00:00:00 2001 From: troido Date: Thu, 21 May 2020 15:26:12 +0200 Subject: added exchanger as seperate component/system; refactored other interactions; parameter parsing returns result instead of option --- content/encyclopediae/npcs.json | 13 ++++--- src/assemblage.rs | 2 +- src/componentparameter.rs | 6 +-- src/components/interactable.rs | 29 +++++---------- src/components/mod.rs | 2 +- src/componentwrapper.rs | 12 ++++++ src/encyclopedia.rs | 2 +- src/parameter.rs | 32 ++++++++-------- src/resources/ground.rs | 16 +++++++- src/room.rs | 8 ++-- src/systems/exchange.rs | 82 +++++++++++++++++++++++++++++++++++++++++ src/systems/interact.rs | 39 +++----------------- src/systems/mod.rs | 4 +- src/systems/talk.rs | 14 ++----- src/template.rs | 4 +- 15 files changed, 168 insertions(+), 97 deletions(-) create mode 100644 src/systems/exchange.rs diff --git a/content/encyclopediae/npcs.json b/content/encyclopediae/npcs.json index 4b8fd39..78dda29 100644 --- a/content/encyclopediae/npcs.json +++ b/content/encyclopediae/npcs.json @@ -86,11 +86,14 @@ "sprite": "human", "height": 1.5, "components": [ - ["Interactable", {"action": ["interaction", ["exchange", ["buy ", { - "pebble": [["radish", "radish"], ["pebble"]], - "radishseed": [["radish"], ["radishseed", "radishseed"]], - "carrotseed": [["radish"], ["carrotseed"]] - }]]]}] + ["Exchanger", { + "prefix": "buy", + "exchanges": ["list", [ + ["list", ["pebble", ["list", ["radish", "radish"]], ["list", ["pebble"]]]], + ["list", ["radishseed", ["list", ["radish"]], ["list", ["radishseed", "radishseed"]]]], + ["list", ["carrotseed", ["list", ["radish"]], ["list", ["carrotseed"]]]] + ]] + }] ] } } diff --git a/src/assemblage.rs b/src/assemblage.rs index 2b60ea3..c88b39b 100644 --- a/src/assemblage.rs +++ b/src/assemblage.rs @@ -37,7 +37,7 @@ impl Assemblage { ( key.clone(), typ, - Some(Parameter::from_typed_json(typ, def).ok_or(perr!("invalid argument default {:?} {:?}", typ, def))?) + Some(Parameter::from_typed_json(typ, def)?) ) ); } else { diff --git a/src/componentparameter.rs b/src/componentparameter.rs index 30a7d91..08c3244 100644 --- a/src/componentparameter.rs +++ b/src/componentparameter.rs @@ -76,14 +76,12 @@ impl ComponentParameter { pub fn from_json(value: &Value) -> PResult { if !value.is_array() { - return Ok(Self::Constant(Parameter::guess_from_json(value).ok_or(perr!("invalid component parameter {:?}", value))?)); + return Ok(Self::Constant(Parameter::guess_from_json(value)?)); } let paramvalue = value.get(1).ok_or(perr!("index 1 not in component parameter"))?; let typename = value.get(0).ok_or(perr!("index 0 not in component parameter"))?.as_str().ok_or(perr!("compparam type not a string"))?; if let Some(paramtype) = ParameterType::from_str(typename) { - Ok(Self::Constant(Parameter::from_typed_json(paramtype, paramvalue).ok_or_else(|| - perr!("failed to parse parameter constant: {:?} {:?}", paramtype, paramvalue) - )?)) + Ok(Self::Constant(Parameter::from_typed_json(paramtype, paramvalue)?)) } else { match typename { "A" | "arg" => { diff --git a/src/components/interactable.rs b/src/components/interactable.rs index ab1ac29..63e89e3 100644 --- a/src/components/interactable.rs +++ b/src/components/interactable.rs @@ -8,7 +8,6 @@ use specs::{ }; use crate::{ exchange::Exchange, - ItemId, components::{Trigger, equipment::Stat}, RoomId }; @@ -17,7 +16,6 @@ use crate::{ #[storage(HashMapStorage)] pub enum Interactable { Trigger(Trigger), - Exchange(String, HashMap), Visit(RoomId), Mine(Stat) } @@ -30,17 +28,6 @@ impl Interactable { let arg = val.get(1)?; Some(match typ.as_str()? { "trigger" => Trigger(Trigger::from_str(arg.as_str()?)?), - "exchange" => { - let (prefix, change) = serde_json::value::from_value::< - (String, HashMap, Vec)>) - >(arg.clone()).ok()?; - Exchange( - prefix, - change.into_iter().map( - |(id, (cost, offer))| (id, Exchange{cost, offer}) - ).collect::>() - ) - }, "visit" => Visit(RoomId::from_str(arg.as_str()?)), "mine" => Mine(Stat::from_str(arg.as_str()?)?), _ => None? @@ -50,13 +37,6 @@ impl Interactable { pub fn accepts_arg(&self, arg: &Option) -> bool { match self { Trigger(_) => arg.is_none(), - Exchange(prefix, _exchanges) => { - if let Some(txt) = arg { - txt.starts_with(prefix) - } else { - true - } - }, Visit(_) => { if let Some(txt) = arg { txt.starts_with("visit ") || txt.starts_with("disallow ") || txt.starts_with("allow ") || txt.starts_with("whitelist") @@ -74,3 +54,12 @@ impl Interactable { pub struct Talkable { pub text: String } + + +#[derive(Component, Debug, Clone, PartialEq)] +#[storage(HashMapStorage)] +pub struct Exchanger { + pub prefix: String, + pub exchanges: HashMap +} + diff --git a/src/components/mod.rs b/src/components/mod.rs index 23f5488..68e666c 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -16,7 +16,7 @@ pub use messages::{ TriggerBox }; pub use faction::Faction; -pub use interactable::{Interactable, Talkable}; +pub use interactable::{Interactable, Talkable, Exchanger}; pub use equipment::Equipment; pub use inventory::Inventory; pub use serialise::Serialise; diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs index e1b5bb8..638d156 100644 --- a/src/componentwrapper.rs +++ b/src/componentwrapper.rs @@ -21,6 +21,7 @@ use crate::{ fromtoparameter::FromToParameter, Timestamp, Template, + exchange::Exchange, Pos, Result, aerr @@ -235,6 +236,17 @@ components!(all: }; Substitute (into: Template); Talkable (text: String); + Exchanger (prefix: String, exchanges: Vec<(String, Vec, Vec)>) { + Exchanger { + prefix, + exchanges: exchanges + .into_iter() + .map(|(key, cost, offer)| + (key, Exchange{cost, offer}) + ) + .collect() + } + }; ); diff --git a/src/encyclopedia.rs b/src/encyclopedia.rs index e100cad..70f0ac6 100644 --- a/src/encyclopedia.rs +++ b/src/encyclopedia.rs @@ -94,7 +94,7 @@ impl Encyclopedia { let mut assemblage = assemblages.get(&enttype).ok_or(perr!("template name '{:?}' does not point to not an assemblage", enttype))?.clone(); for arg in assemblage.arguments.iter_mut() { if let Some(x) = values.get(&arg.0) { - let param = Parameter::from_typed_json(arg.1, x).ok_or(perr!("subtitution parameter has wrong type"))?; + let param = Parameter::from_typed_json(arg.1, x)?; arg.2 = Some(param); } } diff --git a/src/parameter.rs b/src/parameter.rs index cee6342..e2d48dd 100644 --- a/src/parameter.rs +++ b/src/parameter.rs @@ -3,7 +3,9 @@ use serde_json::{Value, json}; use crate::{ Template, components::interactable::Interactable, - Pos + Pos, + PResult, + perr }; @@ -17,10 +19,10 @@ macro_rules! parameters { )* } impl Parameter { - pub fn from_typed_json(typ: ParameterType, val: &Value) -> Option{ + pub fn from_typed_json(typ: ParameterType, val: &Value) -> PResult{ match typ { $( - ParameterType::$name => Some(Self::$name({ + ParameterType::$name => Ok(Self::$name({ let $v = val; $fromjson })), @@ -63,20 +65,20 @@ macro_rules! parameters { } parameters!( - String (String) string, v (v.as_str()?.to_string()) (json!(v)); - Int (i64) int, v (v.as_i64()?) (json!(v)); - Pos (Pos) pos, v (Pos::from_json(v)?) (json!(v)); - Float (f64) float, v (v.as_f64()?) (json!(v)); - Template (Template) template, v (Template::from_json(v).ok()?) (json!(["template", v.to_json()])); - Interaction (Interactable) interaction, _v (Interactable::from_json(_v)?) (panic!("interactions can't be serialized")); - Bool (bool) bool, v (v.as_bool()?) (json!(v)); + String (String) string, v (v.as_str().ok_or(perr!("{:?} not a string", v))?.to_string()) (json!(v)); + Int (i64) int, v (v.as_i64().ok_or(perr!("{:?} not an int", v))?) (json!(v)); + Pos (Pos) pos, v (Pos::from_json(v).ok_or(perr!("{:?} not a pos", v))?) (json!(v)); + Float (f64) float, v (v.as_f64().ok_or(perr!("{:?} not an float", v))?) (json!(v)); + Template (Template) template, v (Template::from_json(v)?) (json!(["template", v.to_json()])); + Interaction (Interactable) interaction, _v (Interactable::from_json(_v).ok_or(perr!("{:?} not an interactable", _v))?) (panic!("interactions can't be serialized")); + Bool (bool) bool, v (v.as_bool().ok_or(perr!("{:?} not a bool", v))?) (json!(v)); List (Vec) list, v ({ v - .as_array()? + .as_array().ok_or(perr!("{:?} not an array", v))? .iter() .map(|item| Parameter::guess_from_json(item)) - .collect::>>()? + .collect::>>()? }) (json!(["list", v.iter().map(Parameter::to_json).collect::>()])); ); @@ -88,11 +90,11 @@ impl Parameter { Self::String(string.to_string()) } - pub fn guess_from_json(val: &Value) -> Option { + pub fn guess_from_json(val: &Value) -> PResult { if let Some(arr) = val.as_array() { if arr.len() == 2 && arr[0].is_string() { let typestr = arr[0].as_str().unwrap(); - let typ = ParameterType::from_str(typestr)?; + let typ = ParameterType::from_str(typestr).ok_or(perr!("invalid parameter type {}", typestr))?; return Self::from_typed_json(typ, &arr[1]); } } @@ -108,7 +110,7 @@ impl Parameter { } else if val.is_object(){ ParameterType::Template } else { - return None + return Err(perr!("can't guess the type of parameter {:?}", val)); }; Self::from_typed_json(typ, val) } diff --git a/src/resources/ground.rs b/src/resources/ground.rs index 7411a15..7868be9 100644 --- a/src/resources/ground.rs +++ b/src/resources/ground.rs @@ -9,7 +9,8 @@ use specs::{ use crate::{ components::{Visible, Flags, Flag}, - Pos + Pos, + controls::Direction }; #[derive(Default)] @@ -43,6 +44,19 @@ impl Ground { .collect() } + pub fn components_near<'a, C: Component>(&self, pos: Pos, directions: &[Direction], component_type: &'a ReadStorage) -> Vec<(Entity, &'a C)> { + let mut nearby_components: Vec<(Entity, &'a C)> = Vec::new(); + for direction in directions { + let pos = pos + direction.to_position(); + for ent in self.cells.get(&pos).unwrap_or(&HashSet::new()) { + if let Some(comp) = component_type.get(*ent) { + nearby_components.push((*ent, comp)); + } + } + } + nearby_components + } + pub fn by_height(&self, pos: &Pos, visibles: &ReadStorage, ignore: &Entity) -> Vec { let mut entities: Vec = self.cells .get(&pos).unwrap_or(&HashSet::new()) diff --git a/src/room.rs b/src/room.rs index d39bdb1..9034e9e 100644 --- a/src/room.rs +++ b/src/room.rs @@ -71,7 +71,8 @@ use crate::{ Deduplicate, SpawnTrigger, Replace, - Talk + Talk, + Exchange } }; @@ -88,12 +89,13 @@ pub fn default_dispatcher<'a, 'b>() -> Dispatcher<'a, 'b> { .with(Use, "use", &["controlinput", "controlai"]) .with(Interact, "interact", &["controlinput", "controlai"]) .with(Talk, "talk", &["controlinput", "controlai"]) + .with(Exchange, "exchange", &["controlinput", "controlai"]) .with(SpawnTrigger, "spawntrigger", &["spawn", "deduplicate", "replace"]) .with(Move, "move", &["controlinput", "controlai"]) .with(Trapping, "trapping", &["move"]) .with(Fight, "fight", &["move"]) .with(Heal, "heal", &[]) - .with(Attacking, "attacking", &["use", "trapping", "fight", "heal", "interact", "talk", "spawntrigger"]) + .with(Attacking, "attacking", &["use", "trapping", "fight", "heal", "interact", "talk", "exchange", "spawntrigger"]) .with(Die, "die", &["attacking"]) .with(DropLoot, "droploot", &["attacking"]) .with(Building, "building", &["attacking"]) @@ -127,7 +129,7 @@ impl <'a, 'b>Room<'a, 'b> { world.insert(NewEntities::new(encyclopedia)); register_insert!( world, - (Position, Visible, Controller, Movable, New, Removed, Moved, Player, Inventory, Health, Serialise, RoomExit, Entered, TriggerBox, Trap, Fighter, Healing, ControlCooldown, Autofight, MonsterAI, Home, AttackInbox, Item, Spawner, Clan, Faction, Interactable, Loot, Timer, Equipment, TimeOffset, Flags, Ear, Build, Whitelist, Dedup, Minable, LootHolder, OnSpawn, Substitute, Talkable), + (Position, Visible, Controller, Movable, New, Removed, Moved, Player, Inventory, Health, Serialise, RoomExit, Entered, TriggerBox, Trap, Fighter, Healing, ControlCooldown, Autofight, MonsterAI, Home, AttackInbox, Item, Spawner, Clan, Faction, Interactable, Loot, Timer, Equipment, TimeOffset, Flags, Ear, Build, Whitelist, Dedup, Minable, LootHolder, OnSpawn, Substitute, Talkable, Exchanger), (Ground, Input, Output, Size, Spawn, Players, Emigration, Time, RoomPermissions) ); diff --git a/src/systems/exchange.rs b/src/systems/exchange.rs new file mode 100644 index 0000000..9931bdd --- /dev/null +++ b/src/systems/exchange.rs @@ -0,0 +1,82 @@ + +use specs::{ + Entities, + ReadStorage, + WriteStorage, + System, + Join, + Read, + Write +}; + +use crate::{ + components::{ + Controller, + Position, + Exchanger, + Notification, + Ear, + Inventory, + Visible + }, + controls::{Control}, + resources::{Ground, NewEntities}, + util::strip_prefix +}; + +pub struct Exchange; +impl <'a> System<'a> for Exchange { + type SystemData = ( + Entities<'a>, + ReadStorage<'a, Controller>, + ReadStorage<'a, Position>, + Read<'a, Ground>, + ReadStorage<'a, Exchanger>, + Write<'a, NewEntities>, + WriteStorage<'a, Ear>, + WriteStorage<'a, Inventory>, + ReadStorage<'a, Visible> + ); + + fn run(&mut self, (entities, controllers, positions, ground, exchangers, new, mut ears, mut inventories, visibles): Self::SystemData) { + for (actor, controller, position) in (&entities, &controllers, &positions).join(){ + let ear = ears.get_mut(actor); + match &controller.control { + Control::Interact(directions, arg) => { + for (ent, exchanger) in ground.components_near(position.pos, directions, &exchangers) { + let prefix = exchanger.prefix.as_str(); + let name = visibles.get(ent).map(|v| v.name.as_str()); + if let Some(txt) = arg { + if let (Some(inventory), Some(action)) = (inventories.get_mut(actor), strip_prefix(&txt, prefix)) { + if let Some(exchange) = exchanger.exchanges.get(action) { + if exchange.can_trade(inventory){ + exchange.trade(inventory, &new.encyclopedia); + say(ear, format!("Success! '{}' ({})", txt, exchange.show()), name); + } else { + say(ear, format!("You do not have the required items or inventory space for '{}' ({})", txt, exchange.show()), name); + } + } else { + say(ear, format!("Invalid option: {}", action), name); + } + break; + } + } else if let Some(ear) = ear { + ear.sounds.push(Notification::Options{ + description: "".to_string(), + options: exchanger.exchanges.iter().map(|(id, exchange)| (format!("{}{}", prefix, id), exchange.show())).collect() + }); + break; + } + } + } + _ => {} + } + } + } +} + +fn say(maybe_ear: Option<&mut Ear>, text: String, source: Option<&str>){ + if let Some(ear) = maybe_ear { + ear.sounds.push(Notification::Sound{text, source: source.map(|s| s.to_string())}); + } +} diff --git a/src/systems/interact.rs b/src/systems/interact.rs index 2090f6f..bce2afc 100644 --- a/src/systems/interact.rs +++ b/src/systems/interact.rs @@ -28,7 +28,7 @@ use crate::{ Minable }, controls::{Control}, - resources::{Ground, NewEntities, Emigration}, + resources::{Ground, Emigration}, hashmap, playerstate::RoomPos, PlayerId, @@ -45,7 +45,6 @@ impl <'a> System<'a> for Interact { WriteStorage<'a, ControlCooldown>, ReadStorage<'a, Interactable>, WriteStorage<'a, TriggerBox>, - Write<'a, NewEntities>, WriteStorage<'a, Ear>, WriteStorage<'a, Inventory>, ReadStorage<'a, Visible>, @@ -55,21 +54,16 @@ impl <'a> System<'a> for Interact { WriteStorage<'a, Minable> ); - fn run(&mut self, (entities, controllers, positions, ground, mut cooldowns, interactables, mut triggerbox, new, mut ears, mut inventories, visibles, players, mut emigration, mut whitelists, mut minables): Self::SystemData) { + fn run(&mut self, (entities, controllers, positions, ground, mut cooldowns, interactables, mut triggerbox, mut ears, inventories, visibles, players, mut emigration, mut whitelists, mut minables): Self::SystemData) { for (actor, controller, position) in (&entities, &controllers, &positions).join(){ let mut target = None; let ear = ears.get_mut(actor); match &controller.control { Control::Interact(directions, arg) => { - 'targets: for direction in directions { - let pos = position.pos + direction.to_position(); - for ent in ground.cells.get(&pos).unwrap_or(&HashSet::new()) { - if let Some(interactable) = interactables.get(*ent) { - if interactable.accepts_arg(arg){ - target = Some((*ent, interactable, arg.clone())); - break 'targets; - } - } + for (ent, interactable) in ground.components_near(position.pos, directions, &interactables) { + if interactable.accepts_arg(arg){ + target = Some((ent, interactable, arg.clone())); + break; } } } @@ -82,27 +76,6 @@ impl <'a> System<'a> for Interact { Interactable::Trigger(trigger) => { TriggerBox::add_message(&mut triggerbox, ent, *trigger); } - Interactable::Exchange(prefix, exchanges) => { - if let Some(txt) = arg { - if let (Some(inventory), Some(action)) = (inventories.get_mut(actor), strip_prefix(&txt, prefix)) { - if let Some(exchange) = exchanges.get(action) { - if exchange.can_trade(inventory){ - exchange.trade(inventory, &new.encyclopedia); - say(ear, format!("Success! '{}' ({})", txt, exchange.show()), name); - } else { - say(ear, format!("You do not have the required items or inventory space for '{}' ({})", txt, exchange.show()), name); - } - } else { - say(ear, format!("Invalid option: {}", action), name); - } - } - } else if let Some(ear) = ear { - ear.sounds.push(Notification::Options{ - description: "".to_string(), - options: exchanges.iter().map(|(id, exchange)| (format!("{}{}", prefix, id), exchange.show())).collect() - }) - } - } Interactable::Visit(dest) => { if let Some(argument) = arg { if let (Some(player), Some(whitelist)) = (players.get(actor), whitelists.get_mut(ent)){ diff --git a/src/systems/mod.rs b/src/systems/mod.rs index 56606e7..76e39ce 100644 --- a/src/systems/mod.rs +++ b/src/systems/mod.rs @@ -25,6 +25,7 @@ mod deduplicate; mod spawntrigger; mod replace; mod talk; +mod exchange; pub use self::{ controlinput::ControlInput, @@ -52,5 +53,6 @@ pub use self::{ deduplicate::Deduplicate, spawntrigger::SpawnTrigger, replace::Replace, - talk::Talk + talk::Talk, + exchange::Exchange }; diff --git a/src/systems/talk.rs b/src/systems/talk.rs index 4bb898a..50e491f 100644 --- a/src/systems/talk.rs +++ b/src/systems/talk.rs @@ -1,5 +1,4 @@ -use std::collections::HashSet; use specs::{ ReadStorage, @@ -37,15 +36,10 @@ impl <'a> System<'a> for Talk { for (controller, position, ear) in (&controllers, &positions, &mut ears).join(){ match &controller.control { Control::Interact(directions, None) => { - 'targets: for direction in directions { - let pos = position.pos + direction.to_position(); - for ent in ground.cells.get(&pos).unwrap_or(&HashSet::new()) { - if let Some(Talkable{text}) = talkables.get(*ent) { - let name = visibles.get(*ent).map(|v| v.name.clone()); - ear.sounds.push(Notification::Sound{text: text.clone(), source: name}); - break 'targets; - } - } + for (ent, Talkable{text}) in ground.components_near(position.pos, directions, &talkables) { + let name = visibles.get(ent).map(|v| v.name.clone()); + ear.sounds.push(Notification::Sound{text: text.clone(), source: name}); + break; } } _ => {} diff --git a/src/template.rs b/src/template.rs index 0ac03cd..7422465 100644 --- a/src/template.rs +++ b/src/template.rs @@ -89,11 +89,11 @@ impl Template { let name = EntityType(val.get("type").ok_or(perr!("template doesn't have 'type'"))?.as_str().ok_or(perr!("template type not a string"))?.to_string()); let mut args = Vec::new(); for arg in val.get("args").unwrap_or(&json!([])).as_array().ok_or(perr!("template args not an array"))? { - args.push(Parameter::guess_from_json(arg).ok_or(perr!("template arg {:?} not a parameter", arg))?); + args.push(Parameter::guess_from_json(arg)?); } let mut kwargs = HashMap::new(); for (key, arg) in val.get("kwargs").unwrap_or(&json!({})).as_object().ok_or(perr!("template kwargs not a json object"))? { - kwargs.insert(key.to_string(), Parameter::guess_from_json(arg).ok_or(perr!("template kwarg {}: {:?} not a parameter", key, arg))?); + kwargs.insert(key.to_string(), Parameter::guess_from_json(arg)?); } let save = if let Some(saveval) = val.get("save") { -- cgit From 39d7f4a123171a1dc5d5a8ec1c512599d4bec0f0 Mon Sep 17 00:00:00 2001 From: troido Date: Sun, 20 Sep 2020 12:55:40 +0200 Subject: renamed ComponentParameter to ParemeterExpression; List and Template variant for ParameterExpression --- src/assemblage.rs | 22 ++--- src/componentparameter.rs | 150 --------------------------------- src/main.rs | 2 +- src/parameterexpression.rs | 204 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 216 insertions(+), 162 deletions(-) delete mode 100644 src/componentparameter.rs create mode 100644 src/parameterexpression.rs diff --git a/src/assemblage.rs b/src/assemblage.rs index c88b39b..c706ed0 100644 --- a/src/assemblage.rs +++ b/src/assemblage.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use serde_json::{Value, json, value}; use crate::{ - componentparameter::ComponentParameter, + parameterexpression::ParameterExpression, parameter::{Parameter, ParameterType}, componentwrapper::{ComponentWrapper, ComponentType}, components::Serialise, @@ -18,7 +18,7 @@ type ArgumentDef = (String, ParameterType, Option); #[derive(Debug, PartialEq, Clone)] pub struct Assemblage { pub arguments: Vec, - pub components: Vec<(ComponentType, HashMap)>, + pub components: Vec<(ComponentType, HashMap)>, pub save: bool, pub extract: Vec<(String, ComponentType, String)> } @@ -47,7 +47,7 @@ impl Assemblage { Ok(arguments) } - fn parse_definition_components(comps: &[Value]) -> PResult)>> { + fn parse_definition_components(comps: &[Value]) -> PResult)>> { let mut components = Vec::new(); for tup in comps { if let Some(name) = tup.as_str() { @@ -55,9 +55,9 @@ impl Assemblage { } else { let (name, params) = value::from_value::<(String, HashMap)>(tup.clone()).map_err(|e| perr!("invalid component definition: {:?}", e))?; let comptype = ComponentType::from_str(&name).ok_or(perr!("{} not a valid componenttype", name))?; - let mut parameters: HashMap = HashMap::new(); + let mut parameters: HashMap = HashMap::new(); for (key, value) in params.into_iter() { - let param = ComponentParameter::from_json(&value)?; + let param = ParameterExpression::from_json(&value)?; parameters.insert(key, param); } components.push((comptype, parameters)); @@ -227,9 +227,9 @@ mod tests { arguments: vec![("sprite".to_string(), ParameterType::String, Some(Parameter::String("grass1".to_string())))], components: vec![ (ComponentType::Visible, hashmap!( - "sprite".to_string() => ComponentParameter::Argument("sprite".to_string()), - "height".to_string() => ComponentParameter::Constant(Parameter::Float(0.1)), - "name".to_string() => ComponentParameter::Constant(Parameter::String("grass".to_string())) + "sprite".to_string() => ParameterExpression::Argument("sprite".to_string()), + "height".to_string() => ParameterExpression::Constant(Parameter::Float(0.1)), + "name".to_string() => ParameterExpression::Constant(Parameter::String("grass".to_string())) )) ], save: true, @@ -346,9 +346,9 @@ mod tests { arguments: vec![("sprite".to_string(), ParameterType::String, None)], components: vec![ (ComponentType::Visible, hashmap!( - "sprite".to_string() => ComponentParameter::Argument("sprite".to_string()), - "height".to_string() => ComponentParameter::Constant(Parameter::Float(0.1)), - "name".to_string() => ComponentParameter::Argument("sprite".to_string()) + "sprite".to_string() => ParameterExpression::Argument("sprite".to_string()), + "height".to_string() => ParameterExpression::Constant(Parameter::Float(0.1)), + "name".to_string() => ParameterExpression::Argument("sprite".to_string()) )) ], save: true, diff --git a/src/componentparameter.rs b/src/componentparameter.rs deleted file mode 100644 index 08c3244..0000000 --- a/src/componentparameter.rs +++ /dev/null @@ -1,150 +0,0 @@ - -use std::collections::HashMap; -use rand::Rng; -use serde_json::Value; -use crate::{ - parameter::{Parameter, ParameterType}, - Template, - Result, - aerr, - PResult, - perr -}; - -const MAX_NESTING: usize = 5; - - -#[derive(Debug, PartialEq, Clone)] -pub enum ComponentParameter { - Constant(Parameter), - Argument(String), - Random(Vec), - Concat(Vec), - If(Box, Box, Box), - TemplateSelf, - TemplateName -} - -impl ComponentParameter { - - pub fn evaluate(&self, arguments: &HashMap<&str, Parameter>, template: &Template) -> Option { - self.evaluate_(arguments, template, 0) - } - - fn evaluate_(&self, arguments: &HashMap<&str, Parameter>, template: &Template, nesting: usize) -> Option { - if nesting > MAX_NESTING { - return None; - } - match self { - Self::Constant(val) => { - Some(val.clone()) - } - Self::Argument(argname) => { - Some(arguments.get(argname.as_str())?.clone()) - } - Self::Random(options) => { - let r = rand::thread_rng().gen_range(0, options.len()); - options[r].evaluate_(arguments, template, nesting + 1) - } - Self::Concat(options) => { - let mut string = String::new(); - for option in options { - if let Parameter::String(s) = option.evaluate_(arguments, template, nesting+1)? { - string.push_str(&s); - } else { - return None; - } - } - Some(Parameter::String(string)) - } - Self::If(condition, thenval, elseval) => { - if let Parameter::Bool(b) = condition.evaluate_(arguments, template, nesting+1)? { - if b { - thenval.evaluate_(arguments, template, nesting+1) - } else { - elseval.evaluate_(arguments, template, nesting+1) - } - } else { - None - } - } - Self::TemplateSelf => Some(Parameter::Template(template.clone())), - Self::TemplateName => Some(Parameter::String(template.name.0.clone())), - - } - } - - pub fn from_json(value: &Value) -> PResult { - if !value.is_array() { - return Ok(Self::Constant(Parameter::guess_from_json(value)?)); - } - let paramvalue = value.get(1).ok_or(perr!("index 1 not in component parameter"))?; - let typename = value.get(0).ok_or(perr!("index 0 not in component parameter"))?.as_str().ok_or(perr!("compparam type not a string"))?; - if let Some(paramtype) = ParameterType::from_str(typename) { - Ok(Self::Constant(Parameter::from_typed_json(paramtype, paramvalue)?)) - } else { - match typename { - "A" | "arg" => { - let argname = paramvalue.as_str().ok_or(perr!("argument parameter not a string"))?.to_string(); - Ok(Self::Argument(argname)) - }, - "random" => { - let optionvalues = paramvalue.as_array().ok_or(perr!("random argument not an array"))?; - let mut options = Vec::new(); - for option in optionvalues { - options.push(Self::from_json(option)?) - } - Ok(Self::Random(options)) - }, - "concat" => { - let values = paramvalue.as_array().ok_or(perr!("concat argument not an array"))?; - let mut options = Vec::new(); - for option in values { - options.push(Self::from_json(option)?) - } - Ok(Self::Concat(options)) - }, - "if" => { - Ok(Self::If( - Box::new(Self::from_json(paramvalue.get(0).ok_or(perr!("if does not have condition"))?)?), - Box::new(Self::from_json(paramvalue.get(1).ok_or(perr!("if does not have then value"))?)?), - Box::new(Self::from_json(paramvalue.get(2).ok_or(perr!("if does not have else value"))?)?) - )) - } - "self" => Ok(Self::TemplateSelf), - "name" => Ok(Self::TemplateName), - _ => Err(perr!("unknown compparam type '{}'", typename)) - } - } - } - - #[allow(dead_code)] - pub fn get_type(&self, arguments: &[(String, ParameterType, Option)]) -> Result{ - Ok(match self { - Self::Constant(param) => param.paramtype(), - Self::Argument(argname) => arguments.iter().find(|(n, _t, _d)| n == argname).ok_or(aerr!("unknown argument name {} in {:?}", argname, arguments))?.1, - Self::Random(options) => { - let typ: ParameterType = options.get(0).ok_or(aerr!("random has no options"))?.get_type(arguments)?; - for param in options { - if param.get_type(arguments)? != typ { - return Err(aerr!("inconsistent parameter types in random")); - } - } - typ - }, - Self::If(condition, thenval, elseval) => { - if condition.get_type(arguments)? != ParameterType::Bool { - return Err(aerr!("if condition is not a bool")); - } - let typ: ParameterType = thenval.get_type(arguments)?; - if elseval.get_type(arguments)? != typ { - return Err(aerr!("inconsistent parameter types in if")); - } - typ - }, - Self::Concat(_s) => ParameterType::String, - Self::TemplateSelf => ParameterType::Template, - Self::TemplateName => ParameterType::String - }) - } -} diff --git a/src/main.rs b/src/main.rs index cbc92a6..9020e3c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,7 @@ mod pos; mod componentwrapper; mod parameter; mod assemblage; -mod componentparameter; +mod parameterexpression; mod encyclopedia; mod template; mod roomtemplate; diff --git a/src/parameterexpression.rs b/src/parameterexpression.rs new file mode 100644 index 0000000..7e2236e --- /dev/null +++ b/src/parameterexpression.rs @@ -0,0 +1,204 @@ + +use std::collections::HashMap; +use rand::Rng; +use serde_json::{Value, json}; +use crate::{ + parameter::{Parameter, ParameterType}, + Template, + template::{SaveOption, EntityType}, + Result, + aerr, + PResult, + perr +}; + +const MAX_NESTING: usize = 5; + + +#[derive(Debug, PartialEq, Clone)] +pub enum ParameterExpression { + Constant(Parameter), + List(Vec), + #[allow(dead_code)] // rustc bug does not know that this variant is used: https://github.com/rust-lang/rust/issues/68408 + Template{name: EntityType, kwargs: HashMap, save: SaveOption}, + Argument(String), + Random(Vec), + Concat(Vec), + If(Box, Box, Box), + TemplateSelf, + TemplateName +} + +impl ParameterExpression { + + pub fn evaluate(&self, arguments: &HashMap<&str, Parameter>, template: &Template) -> Option { + self.evaluate_(arguments, template, 0) + } + + fn evaluate_(&self, arguments: &HashMap<&str, Parameter>, template: &Template, nesting: usize) -> Option { + if nesting > MAX_NESTING { + return None; + } + match self { + Self::Constant(val) => { + Some(val.clone()) + } + Self::List(values) => { + Some(Parameter::List(values.iter().map(|v| v.evaluate_(arguments, template, nesting+1)).collect::>>()?)) + } + Self::Template{name, kwargs, save} => { + Some(Parameter::Template(Template{ + name: name.clone(), + args: Vec::new(), + save: *save, + kwargs: kwargs + .iter() + .map( + |(k, v)| + Some((k.clone(), v.evaluate_(arguments, template, nesting+1)?))) + .collect::>>()? + })) + } + Self::Argument(argname) => { + Some(arguments.get(argname.as_str())?.clone()) + } + Self::Random(options) => { + let r = rand::thread_rng().gen_range(0, options.len()); + options[r].evaluate_(arguments, template, nesting + 1) + } + Self::Concat(options) => { + let mut string = String::new(); + for option in options { + if let Parameter::String(s) = option.evaluate_(arguments, template, nesting+1)? { + string.push_str(&s); + } else { + return None; + } + } + Some(Parameter::String(string)) + } + Self::If(condition, thenval, elseval) => { + if let Parameter::Bool(b) = condition.evaluate_(arguments, template, nesting+1)? { + if b { + thenval.evaluate_(arguments, template, nesting+1) + } else { + elseval.evaluate_(arguments, template, nesting+1) + } + } else { + None + } + } + Self::TemplateSelf => Some(Parameter::Template(template.clone())), + Self::TemplateName => Some(Parameter::String(template.name.0.clone())), + + } + } + + pub fn from_json(value: &Value) -> PResult { + if !value.is_array() { + return Ok(Self::Constant(Parameter::guess_from_json(value)?)); + } + let paramvalue = value.get(1).ok_or(perr!("index 1 not in component parameter"))?; + let typename = value.get(0).ok_or(perr!("index 0 not in component parameter"))?.as_str().ok_or(perr!("compparam type not a string"))?; + match typename { + "string" | "int" | "float" | "bool" | "pos" | "interaction" => { + let paramtype = ParameterType::from_str(typename).expect(&format!("unknown parameter type {:?}", typename)); + Ok(Self::Constant(Parameter::from_typed_json(paramtype, paramvalue)?)) + } + "list" => { + let values = paramvalue.as_array().ok_or(perr!("random argument not an array"))?; + let mut entries = Vec::new(); + for entry in values { + entries.push(Self::from_json(entry)?) + } + Ok(Self::List(entries)) + } + "template" => { + match paramvalue { + Value::String(s) => Ok(Self::Template{ + name: EntityType(s.clone()), + kwargs: HashMap::new(), + save: SaveOption::Default + }), + Value::Object(o) => { + let name = EntityType(o.get("type").ok_or(perr!("template doesn't have 'type'"))?.as_str().ok_or(perr!("template type not a string"))?.to_string()); + let mut kwargs = HashMap::new(); + for (key, arg) in o.get("kwargs").unwrap_or(&json!({})).as_object().ok_or(perr!("template kwargs not a json object"))? { + kwargs.insert(key.to_string(), Self::from_json(arg)?); + } + let save = match o.get("save") { + Some(Value::Bool(b)) if *b => SaveOption::Always, + Some(Value::Bool(_b)) => SaveOption::False, + None => SaveOption::Default, + _ => {return Err(perr!("save not a bool"))} + }; + Ok(Self::Template{name, kwargs, save}) + } + _ => return Err(perr!("invalid template {:?}", paramvalue)) + } + } + "A" | "arg" => { + let argname = paramvalue.as_str().ok_or(perr!("argument parameter not a string"))?.to_string(); + Ok(Self::Argument(argname)) + } + "random" => { + let optionvalues = paramvalue.as_array().ok_or(perr!("random argument not an array"))?; + let mut options = Vec::new(); + for option in optionvalues { + options.push(Self::from_json(option)?) + } + Ok(Self::Random(options)) + } + "concat" => { + let values = paramvalue.as_array().ok_or(perr!("concat argument not an array"))?; + let mut options = Vec::new(); + for option in values { + options.push(Self::from_json(option)?) + } + Ok(Self::Concat(options)) + } + "if" => { + Ok(Self::If( + Box::new(Self::from_json(paramvalue.get(0).ok_or(perr!("if does not have condition"))?)?), + Box::new(Self::from_json(paramvalue.get(1).ok_or(perr!("if does not have then value"))?)?), + Box::new(Self::from_json(paramvalue.get(2).ok_or(perr!("if does not have else value"))?)?) + )) + } + "self" => Ok(Self::TemplateSelf), + "name" => Ok(Self::TemplateName), + _ => Err(perr!("unknown compparam type '{}'", typename)) + } + } + + #[allow(dead_code)] + pub fn get_type(&self, arguments: &[(String, ParameterType, Option)]) -> Result{ + Ok(match self { + Self::Constant(param) => param.paramtype(), + Self::List(_) => ParameterType::List, + Self::Template{name: _, kwargs: _, save: _} => ParameterType::Template, + Self::Argument(argname) => arguments.iter().find(|(n, _t, _d)| n == argname).ok_or(aerr!("unknown argument name {} in {:?}", argname, arguments))?.1, + Self::Random(options) => { + let typ: ParameterType = options.get(0).ok_or(aerr!("random has no options"))?.get_type(arguments)?; + for param in options { + if param.get_type(arguments)? != typ { + return Err(aerr!("inconsistent parameter types in random")); + } + } + typ + }, + Self::If(condition, thenval, elseval) => { + if condition.get_type(arguments)? != ParameterType::Bool { + return Err(aerr!("if condition is not a bool")); + } + let typ: ParameterType = thenval.get_type(arguments)?; + if elseval.get_type(arguments)? != typ { + return Err(aerr!("inconsistent parameter types in if")); + } + typ + }, + Self::Concat(_s) => ParameterType::String, + Self::TemplateSelf => ParameterType::Template, + Self::TemplateName => ParameterType::String + }) + } +} -- cgit From 5eda37efbd1b34851364923069c0c3effdc32ca8 Mon Sep 17 00:00:00 2001 From: troido Date: Sun, 20 Sep 2020 23:33:13 +0200 Subject: create interactions from parameter instead of json --- content/encyclopediae/crops.json | 4 ++-- content/encyclopediae/default_encyclopedia.json | 8 ++++---- src/components/interactable.rs | 15 +++++++++++++-- src/componentwrapper.rs | 2 +- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/content/encyclopediae/crops.json b/content/encyclopediae/crops.json index 2d2b5ce..29901d9 100644 --- a/content/encyclopediae/crops.json +++ b/content/encyclopediae/crops.json @@ -5,7 +5,7 @@ "name": "radishplant", "height": 0.5, "components": [ - ["Interactable", {"action": ["interaction", ["trigger", "die"]]}], + ["Interactable", {"typ": "trigger", "arg": "die"}], ["Loot", {"loot": ["list", [ ["list", [{"type": "radishseed"}, 0.92]], ["list", [{"type": "radishseed"}, 0.20]], @@ -136,7 +136,7 @@ "name": "carrotplant", "height": 1.0, "components": [ - ["Interactable", {"action": ["interaction", ["trigger", "die"]]}], + ["Interactable", {"typ": "trigger", "arg": "die"}], ["Loot", {"loot": ["list", [ ["list", [{"type": "carrotseed"}, 1.0]], ["list", [{"type": "carrot"}, 1.0]] diff --git a/content/encyclopediae/default_encyclopedia.json b/content/encyclopediae/default_encyclopedia.json index fdaa42f..9a9b526 100644 --- a/content/encyclopediae/default_encyclopedia.json +++ b/content/encyclopediae/default_encyclopedia.json @@ -12,7 +12,7 @@ "extract": {"allowed": ["Whitelist", "allowed"], "dedup_priority": ["Dedup", "priority"]}, "components": [ ["RoomExit", {"destination": "_home+{player}", "dest_pos": ""}], - ["Interactable", {"action": ["interaction", ["visit", "_home+{player}"]]}], + ["Interactable", {"typ": "visit", "arg": "_home+{player}"}], ["Whitelist", {"allowed": ["arg", "allowed"]}], ["Dedup", {"id": ["arg", "dedup_id"], "priority": ["arg", "dedup_priority"]}] ], @@ -69,7 +69,7 @@ "height": 2, "flags": ["Blocking"], "components": [ - ["Interactable", {"action": ["interaction", ["trigger", "change"]]}], + ["Interactable", {"typ": "trigger", "arg": "change"}], ["Build", {"obj": {"type": "opendoor", "save": false}}] ] }, @@ -78,7 +78,7 @@ "height": 0.8, "flags": ["Occupied"], "components": [ - ["Interactable", {"action": ["interaction", ["trigger", "change"]]}], + ["Interactable", {"typ": "trigger", "arg": "change"}], ["Build", {"obj": {"type": "closeddoor", "save": false}}] ] }, @@ -94,7 +94,7 @@ "sprite": "quarry", "height": 2, "components": [ - ["Interactable", {"action": ["interaction", ["mine", "mining"]]}], + ["Interactable", {"typ": "mine", "arg": "mining"}], ["Minable", {"total": 20, "trigger": "loot"}], ["Loot", {"loot": ["list", [ ["list", [{"type": "stone"}, 1.0]] diff --git a/src/components/interactable.rs b/src/components/interactable.rs index 63e89e3..d5a6926 100644 --- a/src/components/interactable.rs +++ b/src/components/interactable.rs @@ -4,12 +4,13 @@ use serde_json; use serde_json::{Value}; use specs::{ Component, - HashMapStorage + HashMapStorage, }; use crate::{ exchange::Exchange, components::{Trigger, equipment::Stat}, - RoomId + RoomId, + parameter::Parameter }; #[derive(Component, Debug, Clone, PartialEq)] @@ -23,6 +24,16 @@ pub enum Interactable { use Interactable::*; impl Interactable { + + pub fn parse_from_parameter(typ: &str, arg: &Parameter) -> Option { + Some(match (typ, arg) { + ("trigger", Parameter::String(s)) => Trigger(Trigger::from_str(s)?), + ("visit", Parameter::String(s)) => Visit(RoomId::from_str(s)), + ("mine", Parameter::String(s)) => Mine(Stat::from_str(s)?), + _ => None? + }) + } + pub fn from_json(val: &Value) -> Option { let typ = val.get(0)?; let arg = val.get(1)?; diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs index 638d156..f3c2db0 100644 --- a/src/componentwrapper.rs +++ b/src/componentwrapper.rs @@ -185,7 +185,7 @@ components!(all: Clan (name: String); Home (home: Pos); Faction (faction: String) {Faction::from_str(faction.as_str()).ok_or(aerr!("invalid faction name"))?}; - Interactable (action: Interactable) {action}; + Interactable (typ: String, arg: Parameter) {Interactable::parse_from_parameter(&typ, &arg).ok_or(aerr!("invalid interaction"))?}; Loot (loot: Vec<(Template, f64)>); Timer ( trigger: String, (panic!("can't turn trigger to string")), -- cgit From 1e7c665728eb638cdf921824f4c22f54b81f29de Mon Sep 17 00:00:00 2001 From: troido Date: Sun, 20 Sep 2020 23:37:28 +0200 Subject: removed interactable as Parameter type --- src/components/interactable.rs | 13 ------------- src/componentwrapper.rs | 3 +-- src/fromtoparameter.rs | 2 -- src/parameter.rs | 2 -- src/parameterexpression.rs | 2 +- 5 files changed, 2 insertions(+), 20 deletions(-) diff --git a/src/components/interactable.rs b/src/components/interactable.rs index d5a6926..ec8c1db 100644 --- a/src/components/interactable.rs +++ b/src/components/interactable.rs @@ -1,7 +1,5 @@ use std::collections::HashMap; -use serde_json; -use serde_json::{Value}; use specs::{ Component, HashMapStorage, @@ -33,17 +31,6 @@ impl Interactable { _ => None? }) } - - pub fn from_json(val: &Value) -> Option { - let typ = val.get(0)?; - let arg = val.get(1)?; - Some(match typ.as_str()? { - "trigger" => Trigger(Trigger::from_str(arg.as_str()?)?), - "visit" => Visit(RoomId::from_str(arg.as_str()?)), - "mine" => Mine(Stat::from_str(arg.as_str()?)?), - _ => None? - }) - } pub fn accepts_arg(&self, arg: &Option) -> bool { match self { diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs index f3c2db0..62e090e 100644 --- a/src/componentwrapper.rs +++ b/src/componentwrapper.rs @@ -14,8 +14,7 @@ use crate::{ AttackType, Clan, Flag, - Trigger, - interactable::Interactable + Trigger }, parameter::{Parameter}, fromtoparameter::FromToParameter, diff --git a/src/fromtoparameter.rs b/src/fromtoparameter.rs index f201a2a..75912ae 100644 --- a/src/fromtoparameter.rs +++ b/src/fromtoparameter.rs @@ -5,7 +5,6 @@ use crate::{ parameter::Parameter, Template, Pos, - components::interactable::Interactable, PlayerId, Sprite, ItemId @@ -78,7 +77,6 @@ tofrom!(bool:Bool); tofrom!(String: String); tofrom!(Pos: Pos); tofrom!(Template: Template); -tofrom!(Interactable: Interaction); tofrom!(PlayerId{name: String}); tofrom!(Sprite{name: String}); diff --git a/src/parameter.rs b/src/parameter.rs index e2d48dd..e303f76 100644 --- a/src/parameter.rs +++ b/src/parameter.rs @@ -2,7 +2,6 @@ use serde_json::{Value, json}; use crate::{ Template, - components::interactable::Interactable, Pos, PResult, perr @@ -70,7 +69,6 @@ parameters!( Pos (Pos) pos, v (Pos::from_json(v).ok_or(perr!("{:?} not a pos", v))?) (json!(v)); Float (f64) float, v (v.as_f64().ok_or(perr!("{:?} not an float", v))?) (json!(v)); Template (Template) template, v (Template::from_json(v)?) (json!(["template", v.to_json()])); - Interaction (Interactable) interaction, _v (Interactable::from_json(_v).ok_or(perr!("{:?} not an interactable", _v))?) (panic!("interactions can't be serialized")); Bool (bool) bool, v (v.as_bool().ok_or(perr!("{:?} not a bool", v))?) (json!(v)); List (Vec) list, v ({ diff --git a/src/parameterexpression.rs b/src/parameterexpression.rs index 7e2236e..65ad4c8 100644 --- a/src/parameterexpression.rs +++ b/src/parameterexpression.rs @@ -101,7 +101,7 @@ impl ParameterExpression { let paramvalue = value.get(1).ok_or(perr!("index 1 not in component parameter"))?; let typename = value.get(0).ok_or(perr!("index 0 not in component parameter"))?.as_str().ok_or(perr!("compparam type not a string"))?; match typename { - "string" | "int" | "float" | "bool" | "pos" | "interaction" => { + "string" | "int" | "float" | "bool" | "pos" => { let paramtype = ParameterType::from_str(typename).expect(&format!("unknown parameter type {:?}", typename)); Ok(Self::Constant(Parameter::from_typed_json(paramtype, paramvalue)?)) } -- cgit From 92e437e50498f7705e33a556535ba39a2b918f9d Mon Sep 17 00:00:00 2001 From: troido Date: Mon, 21 Sep 2020 00:59:38 +0200 Subject: made talk and reply a form of interact again --- content/encyclopediae/default_encyclopedia.json | 2 +- content/encyclopediae/npcs.json | 2 +- src/components/interactable.rs | 16 ++++++++-------- src/components/mod.rs | 2 +- src/componentwrapper.rs | 1 - src/room.rs | 6 ++---- src/systems/interact.rs | 6 ++++++ src/systems/mod.rs | 2 -- 8 files changed, 19 insertions(+), 18 deletions(-) diff --git a/content/encyclopediae/default_encyclopedia.json b/content/encyclopediae/default_encyclopedia.json index 9a9b526..d4f7094 100644 --- a/content/encyclopediae/default_encyclopedia.json +++ b/content/encyclopediae/default_encyclopedia.json @@ -87,7 +87,7 @@ "height": 1, "flags": ["Occupied"], "components": [ - ["Talkable", {"text": "Good morning there, World"}] + ["Interactable", {"typ": "say", "arg": "Good morning there, World"}] ] }, "quarry": { diff --git a/content/encyclopediae/npcs.json b/content/encyclopediae/npcs.json index 78dda29..661c80d 100644 --- a/content/encyclopediae/npcs.json +++ b/content/encyclopediae/npcs.json @@ -79,7 +79,7 @@ "height": 1.5, "flags": ["Occupied"], "components": [ - ["Talkable", {"text": "Hey there, welcome to Asciifarm"}] + ["Interactable", {"typ": "say", "arg": "Hey there, welcome to Asciifarm"}] ] }, "trader": { diff --git a/src/components/interactable.rs b/src/components/interactable.rs index ec8c1db..2e286cf 100644 --- a/src/components/interactable.rs +++ b/src/components/interactable.rs @@ -16,7 +16,9 @@ use crate::{ pub enum Interactable { Trigger(Trigger), Visit(RoomId), - Mine(Stat) + Mine(Stat), + Say(String), + Reply(String) } use Interactable::*; @@ -28,6 +30,8 @@ impl Interactable { ("trigger", Parameter::String(s)) => Trigger(Trigger::from_str(s)?), ("visit", Parameter::String(s)) => Visit(RoomId::from_str(s)), ("mine", Parameter::String(s)) => Mine(Stat::from_str(s)?), + ("say", Parameter::String(s)) => Say(s.clone()), + ("reply", Parameter::String(s)) => Reply(s.clone()), _ => None? }) } @@ -42,17 +46,13 @@ impl Interactable { true } } - Mine(_) => arg.is_none() + Mine(_) => arg.is_none(), + Say(_) => arg.is_none(), + Reply(_) => arg.is_some(), } } } -#[derive(Component, Debug, Clone, PartialEq)] -#[storage(HashMapStorage)] -pub struct Talkable { - pub text: String -} - #[derive(Component, Debug, Clone, PartialEq)] #[storage(HashMapStorage)] diff --git a/src/components/mod.rs b/src/components/mod.rs index 68e666c..f315060 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -16,7 +16,7 @@ pub use messages::{ TriggerBox }; pub use faction::Faction; -pub use interactable::{Interactable, Talkable, Exchanger}; +pub use interactable::{Interactable, Exchanger}; pub use equipment::Equipment; pub use inventory::Inventory; pub use serialise::Serialise; diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs index 62e090e..96cedd3 100644 --- a/src/componentwrapper.rs +++ b/src/componentwrapper.rs @@ -234,7 +234,6 @@ components!(all: } }; Substitute (into: Template); - Talkable (text: String); Exchanger (prefix: String, exchanges: Vec<(String, Vec, Vec)>) { Exchanger { prefix, diff --git a/src/room.rs b/src/room.rs index 9034e9e..04e3044 100644 --- a/src/room.rs +++ b/src/room.rs @@ -71,7 +71,6 @@ use crate::{ Deduplicate, SpawnTrigger, Replace, - Talk, Exchange } }; @@ -88,14 +87,13 @@ pub fn default_dispatcher<'a, 'b>() -> Dispatcher<'a, 'b> { .with(Take, "take", &["controlinput", "controlai"]) .with(Use, "use", &["controlinput", "controlai"]) .with(Interact, "interact", &["controlinput", "controlai"]) - .with(Talk, "talk", &["controlinput", "controlai"]) .with(Exchange, "exchange", &["controlinput", "controlai"]) .with(SpawnTrigger, "spawntrigger", &["spawn", "deduplicate", "replace"]) .with(Move, "move", &["controlinput", "controlai"]) .with(Trapping, "trapping", &["move"]) .with(Fight, "fight", &["move"]) .with(Heal, "heal", &[]) - .with(Attacking, "attacking", &["use", "trapping", "fight", "heal", "interact", "talk", "exchange", "spawntrigger"]) + .with(Attacking, "attacking", &["use", "trapping", "fight", "heal", "interact", "exchange", "spawntrigger"]) .with(Die, "die", &["attacking"]) .with(DropLoot, "droploot", &["attacking"]) .with(Building, "building", &["attacking"]) @@ -129,7 +127,7 @@ impl <'a, 'b>Room<'a, 'b> { world.insert(NewEntities::new(encyclopedia)); register_insert!( world, - (Position, Visible, Controller, Movable, New, Removed, Moved, Player, Inventory, Health, Serialise, RoomExit, Entered, TriggerBox, Trap, Fighter, Healing, ControlCooldown, Autofight, MonsterAI, Home, AttackInbox, Item, Spawner, Clan, Faction, Interactable, Loot, Timer, Equipment, TimeOffset, Flags, Ear, Build, Whitelist, Dedup, Minable, LootHolder, OnSpawn, Substitute, Talkable, Exchanger), + (Position, Visible, Controller, Movable, New, Removed, Moved, Player, Inventory, Health, Serialise, RoomExit, Entered, TriggerBox, Trap, Fighter, Healing, ControlCooldown, Autofight, MonsterAI, Home, AttackInbox, Item, Spawner, Clan, Faction, Interactable, Loot, Timer, Equipment, TimeOffset, Flags, Ear, Build, Whitelist, Dedup, Minable, LootHolder, OnSpawn, Substitute, Exchanger), (Ground, Input, Output, Size, Spawn, Players, Emigration, Time, RoomPermissions) ); diff --git a/src/systems/interact.rs b/src/systems/interact.rs index bce2afc..35c30b6 100644 --- a/src/systems/interact.rs +++ b/src/systems/interact.rs @@ -73,6 +73,12 @@ impl <'a> System<'a> for Interact { let mut cooldown = 2; let name = visibles.get(ent).map(|v| v.name.as_str()); match interactable { + Interactable::Say(text) => { + say(ear, text.clone(), name); + } + Interactable::Reply(text) => { + say(ear, text.replace("{}", &arg.unwrap()), name); + } Interactable::Trigger(trigger) => { TriggerBox::add_message(&mut triggerbox, ent, *trigger); } diff --git a/src/systems/mod.rs b/src/systems/mod.rs index 76e39ce..963cfa6 100644 --- a/src/systems/mod.rs +++ b/src/systems/mod.rs @@ -24,7 +24,6 @@ mod building; mod deduplicate; mod spawntrigger; mod replace; -mod talk; mod exchange; pub use self::{ @@ -53,6 +52,5 @@ pub use self::{ deduplicate::Deduplicate, spawntrigger::SpawnTrigger, replace::Replace, - talk::Talk, exchange::Exchange }; -- cgit From e2281d8c6293b311ccc187e3503093a1120e6215 Mon Sep 17 00:00:00 2001 From: troido Date: Mon, 21 Sep 2020 02:32:22 +0200 Subject: exchange is now an interaction again --- content/encyclopediae/npcs.json | 8 ++++---- src/components/interactable.rs | 28 ++++++++++++++++++---------- src/components/mod.rs | 2 +- src/componentwrapper.rs | 16 +++------------- src/room.rs | 6 ++---- src/systems/interact.rs | 28 +++++++++++++++++++++++++--- src/systems/mod.rs | 2 -- 7 files changed, 53 insertions(+), 37 deletions(-) diff --git a/content/encyclopediae/npcs.json b/content/encyclopediae/npcs.json index 661c80d..00c45d6 100644 --- a/content/encyclopediae/npcs.json +++ b/content/encyclopediae/npcs.json @@ -86,14 +86,14 @@ "sprite": "human", "height": 1.5, "components": [ - ["Exchanger", { - "prefix": "buy", - "exchanges": ["list", [ + ["Interactable", {"typ": "exchange", "arg": ["list", [ + "buy ", + ["list", [ ["list", ["pebble", ["list", ["radish", "radish"]], ["list", ["pebble"]]]], ["list", ["radishseed", ["list", ["radish"]], ["list", ["radishseed", "radishseed"]]]], ["list", ["carrotseed", ["list", ["radish"]], ["list", ["carrotseed"]]]] ]] - }] + ]]}] ] } } diff --git a/src/components/interactable.rs b/src/components/interactable.rs index 2e286cf..2f20966 100644 --- a/src/components/interactable.rs +++ b/src/components/interactable.rs @@ -8,7 +8,9 @@ use crate::{ exchange::Exchange, components::{Trigger, equipment::Stat}, RoomId, - parameter::Parameter + parameter::Parameter, + fromtoparameter::FromToParameter, + ItemId, }; #[derive(Component, Debug, Clone, PartialEq)] @@ -18,7 +20,8 @@ pub enum Interactable { Visit(RoomId), Mine(Stat), Say(String), - Reply(String) + Reply(String), + Exchange(String, HashMap), } use Interactable::*; @@ -32,7 +35,12 @@ impl Interactable { ("mine", Parameter::String(s)) => Mine(Stat::from_str(s)?), ("say", Parameter::String(s)) => Say(s.clone()), ("reply", Parameter::String(s)) => Reply(s.clone()), - _ => None? + ("exchange", p) => { + let (prefix, trades) = <(String, Vec<(String, Vec, Vec)>)>::from_parameter(p.clone())?; + let exchanges = trades.into_iter().map(|(k, cost, offer)| (k, Exchange{cost, offer})).collect(); + Exchange(prefix, exchanges) + } + _ => {return None} }) } @@ -49,15 +57,15 @@ impl Interactable { Mine(_) => arg.is_none(), Say(_) => arg.is_none(), Reply(_) => arg.is_some(), + Exchange(prefix, _exchanges) => { + if let Some(txt) = arg { + txt.starts_with(prefix) + } else { + true + } + }, } } } -#[derive(Component, Debug, Clone, PartialEq)] -#[storage(HashMapStorage)] -pub struct Exchanger { - pub prefix: String, - pub exchanges: HashMap -} - diff --git a/src/components/mod.rs b/src/components/mod.rs index f315060..38a14bd 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -16,7 +16,7 @@ pub use messages::{ TriggerBox }; pub use faction::Faction; -pub use interactable::{Interactable, Exchanger}; +pub use interactable::{Interactable}; pub use equipment::Equipment; pub use inventory::Inventory; pub use serialise::Serialise; diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs index 96cedd3..08d95ee 100644 --- a/src/componentwrapper.rs +++ b/src/componentwrapper.rs @@ -20,7 +20,6 @@ use crate::{ fromtoparameter::FromToParameter, Timestamp, Template, - exchange::Exchange, Pos, Result, aerr @@ -184,7 +183,9 @@ components!(all: Clan (name: String); Home (home: Pos); Faction (faction: String) {Faction::from_str(faction.as_str()).ok_or(aerr!("invalid faction name"))?}; - Interactable (typ: String, arg: Parameter) {Interactable::parse_from_parameter(&typ, &arg).ok_or(aerr!("invalid interaction"))?}; + Interactable (typ: String, arg: Parameter) { + Interactable::parse_from_parameter(&typ, &arg).ok_or(aerr!("invalid interaction {:?} {:?}", typ, arg))? + }; Loot (loot: Vec<(Template, f64)>); Timer ( trigger: String, (panic!("can't turn trigger to string")), @@ -234,17 +235,6 @@ components!(all: } }; Substitute (into: Template); - Exchanger (prefix: String, exchanges: Vec<(String, Vec, Vec)>) { - Exchanger { - prefix, - exchanges: exchanges - .into_iter() - .map(|(key, cost, offer)| - (key, Exchange{cost, offer}) - ) - .collect() - } - }; ); diff --git a/src/room.rs b/src/room.rs index 04e3044..56fe2e6 100644 --- a/src/room.rs +++ b/src/room.rs @@ -71,7 +71,6 @@ use crate::{ Deduplicate, SpawnTrigger, Replace, - Exchange } }; @@ -87,13 +86,12 @@ pub fn default_dispatcher<'a, 'b>() -> Dispatcher<'a, 'b> { .with(Take, "take", &["controlinput", "controlai"]) .with(Use, "use", &["controlinput", "controlai"]) .with(Interact, "interact", &["controlinput", "controlai"]) - .with(Exchange, "exchange", &["controlinput", "controlai"]) .with(SpawnTrigger, "spawntrigger", &["spawn", "deduplicate", "replace"]) .with(Move, "move", &["controlinput", "controlai"]) .with(Trapping, "trapping", &["move"]) .with(Fight, "fight", &["move"]) .with(Heal, "heal", &[]) - .with(Attacking, "attacking", &["use", "trapping", "fight", "heal", "interact", "exchange", "spawntrigger"]) + .with(Attacking, "attacking", &["use", "trapping", "fight", "heal", "interact", "spawntrigger"]) .with(Die, "die", &["attacking"]) .with(DropLoot, "droploot", &["attacking"]) .with(Building, "building", &["attacking"]) @@ -127,7 +125,7 @@ impl <'a, 'b>Room<'a, 'b> { world.insert(NewEntities::new(encyclopedia)); register_insert!( world, - (Position, Visible, Controller, Movable, New, Removed, Moved, Player, Inventory, Health, Serialise, RoomExit, Entered, TriggerBox, Trap, Fighter, Healing, ControlCooldown, Autofight, MonsterAI, Home, AttackInbox, Item, Spawner, Clan, Faction, Interactable, Loot, Timer, Equipment, TimeOffset, Flags, Ear, Build, Whitelist, Dedup, Minable, LootHolder, OnSpawn, Substitute, Exchanger), + (Position, Visible, Controller, Movable, New, Removed, Moved, Player, Inventory, Health, Serialise, RoomExit, Entered, TriggerBox, Trap, Fighter, Healing, ControlCooldown, Autofight, MonsterAI, Home, AttackInbox, Item, Spawner, Clan, Faction, Interactable, Loot, Timer, Equipment, TimeOffset, Flags, Ear, Build, Whitelist, Dedup, Minable, LootHolder, OnSpawn, Substitute), (Ground, Input, Output, Size, Spawn, Players, Emigration, Time, RoomPermissions) ); diff --git a/src/systems/interact.rs b/src/systems/interact.rs index 35c30b6..1eb4589 100644 --- a/src/systems/interact.rs +++ b/src/systems/interact.rs @@ -28,7 +28,7 @@ use crate::{ Minable }, controls::{Control}, - resources::{Ground, Emigration}, + resources::{Ground, Emigration, NewEntities}, hashmap, playerstate::RoomPos, PlayerId, @@ -51,10 +51,11 @@ impl <'a> System<'a> for Interact { ReadStorage<'a, Player>, Write<'a, Emigration>, WriteStorage<'a, Whitelist>, - WriteStorage<'a, Minable> + WriteStorage<'a, Minable>, + Read<'a, NewEntities> ); - fn run(&mut self, (entities, controllers, positions, ground, mut cooldowns, interactables, mut triggerbox, mut ears, inventories, visibles, players, mut emigration, mut whitelists, mut minables): Self::SystemData) { + fn run(&mut self, (entities, controllers, positions, ground, mut cooldowns, interactables, mut triggerbox, mut ears, mut inventories, visibles, players, mut emigration, mut whitelists, mut minables, new): Self::SystemData) { for (actor, controller, position) in (&entities, &controllers, &positions).join(){ let mut target = None; let ear = ears.get_mut(actor); @@ -136,6 +137,27 @@ impl <'a> System<'a> for Interact { } } } + Interactable::Exchange(prefix, exchanges) => { + if let Some(txt) = arg { + if let (Some(inventory), Some(action)) = (inventories.get_mut(actor), strip_prefix(&txt, prefix)) { + if let Some(exchange) = exchanges.get(action) { + if exchange.can_trade(inventory){ + exchange.trade(inventory, &new.encyclopedia); + say(ear, format!("Success! '{}' ({})", txt, exchange.show()), name); + } else { + say(ear, format!("You do not have the required items or inventory space for '{}' ({})", txt, exchange.show()), name); + } + } else { + say(ear, format!("Invalid option: {}", action), name); + } + } + } else if let Some(ear) = ear { + ear.sounds.push(Notification::Options{ + description: "".to_string(), + options: exchanges.iter().map(|(id, exchange)| (format!("{}{}", prefix, id), exchange.show())).collect() + }) + } + } } cooldowns.insert(actor, ControlCooldown{amount: cooldown}).unwrap(); } diff --git a/src/systems/mod.rs b/src/systems/mod.rs index 963cfa6..7f93bb9 100644 --- a/src/systems/mod.rs +++ b/src/systems/mod.rs @@ -24,7 +24,6 @@ mod building; mod deduplicate; mod spawntrigger; mod replace; -mod exchange; pub use self::{ controlinput::ControlInput, @@ -52,5 +51,4 @@ pub use self::{ deduplicate::Deduplicate, spawntrigger::SpawnTrigger, replace::Replace, - exchange::Exchange }; -- cgit From 455867294cc849bff2c0829a7464e71e79a0dcae Mon Sep 17 00:00:00 2001 From: troido Date: Mon, 21 Sep 2020 02:33:09 +0200 Subject: removed unused systems for talk and exchange --- src/systems/exchange.rs | 82 ------------------------------------------------- src/systems/talk.rs | 50 ------------------------------ 2 files changed, 132 deletions(-) delete mode 100644 src/systems/exchange.rs delete mode 100644 src/systems/talk.rs diff --git a/src/systems/exchange.rs b/src/systems/exchange.rs deleted file mode 100644 index 9931bdd..0000000 --- a/src/systems/exchange.rs +++ /dev/null @@ -1,82 +0,0 @@ - -use specs::{ - Entities, - ReadStorage, - WriteStorage, - System, - Join, - Read, - Write -}; - -use crate::{ - components::{ - Controller, - Position, - Exchanger, - Notification, - Ear, - Inventory, - Visible - }, - controls::{Control}, - resources::{Ground, NewEntities}, - util::strip_prefix -}; - -pub struct Exchange; -impl <'a> System<'a> for Exchange { - type SystemData = ( - Entities<'a>, - ReadStorage<'a, Controller>, - ReadStorage<'a, Position>, - Read<'a, Ground>, - ReadStorage<'a, Exchanger>, - Write<'a, NewEntities>, - WriteStorage<'a, Ear>, - WriteStorage<'a, Inventory>, - ReadStorage<'a, Visible> - ); - - fn run(&mut self, (entities, controllers, positions, ground, exchangers, new, mut ears, mut inventories, visibles): Self::SystemData) { - for (actor, controller, position) in (&entities, &controllers, &positions).join(){ - let ear = ears.get_mut(actor); - match &controller.control { - Control::Interact(directions, arg) => { - for (ent, exchanger) in ground.components_near(position.pos, directions, &exchangers) { - let prefix = exchanger.prefix.as_str(); - let name = visibles.get(ent).map(|v| v.name.as_str()); - if let Some(txt) = arg { - if let (Some(inventory), Some(action)) = (inventories.get_mut(actor), strip_prefix(&txt, prefix)) { - if let Some(exchange) = exchanger.exchanges.get(action) { - if exchange.can_trade(inventory){ - exchange.trade(inventory, &new.encyclopedia); - say(ear, format!("Success! '{}' ({})", txt, exchange.show()), name); - } else { - say(ear, format!("You do not have the required items or inventory space for '{}' ({})", txt, exchange.show()), name); - } - } else { - say(ear, format!("Invalid option: {}", action), name); - } - break; - } - } else if let Some(ear) = ear { - ear.sounds.push(Notification::Options{ - description: "".to_string(), - options: exchanger.exchanges.iter().map(|(id, exchange)| (format!("{}{}", prefix, id), exchange.show())).collect() - }); - break; - } - } - } - _ => {} - } - } - } -} - -fn say(maybe_ear: Option<&mut Ear>, text: String, source: Option<&str>){ - if let Some(ear) = maybe_ear { - ear.sounds.push(Notification::Sound{text, source: source.map(|s| s.to_string())}); - } -} diff --git a/src/systems/talk.rs b/src/systems/talk.rs deleted file mode 100644 index 50e491f..0000000 --- a/src/systems/talk.rs +++ /dev/null @@ -1,50 +0,0 @@ - - -use specs::{ - ReadStorage, - WriteStorage, - System, - Join, - Read -}; - -use crate::{ - components::{ - Controller, - Position, - Talkable, - Notification, - Ear, - Visible - }, - controls::{Control}, - resources::{Ground}, -}; - -pub struct Talk; -impl <'a> System<'a> for Talk { - type SystemData = ( - ReadStorage<'a, Controller>, - ReadStorage<'a, Position>, - Read<'a, Ground>, - ReadStorage<'a, Talkable>, - WriteStorage<'a, Ear>, - ReadStorage<'a, Visible> - ); - - fn run(&mut self, (controllers, positions, ground, talkables, mut ears, visibles): Self::SystemData) { - for (controller, position, ear) in (&controllers, &positions, &mut ears).join(){ - match &controller.control { - Control::Interact(directions, None) => { - for (ent, Talkable{text}) in ground.components_near(position.pos, directions, &talkables) { - let name = visibles.get(ent).map(|v| v.name.clone()); - ear.sounds.push(Notification::Sound{text: text.clone(), source: name}); - break; - } - } - _ => {} - } - } - } -} - -- cgit