summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--content/encyclopediae/npcs.json13
-rw-r--r--src/assemblage.rs2
-rw-r--r--src/componentparameter.rs6
-rw-r--r--src/components/interactable.rs29
-rw-r--r--src/components/mod.rs2
-rw-r--r--src/componentwrapper.rs12
-rw-r--r--src/encyclopedia.rs2
-rw-r--r--src/parameter.rs32
-rw-r--r--src/resources/ground.rs16
-rw-r--r--src/room.rs8
-rw-r--r--src/systems/exchange.rs82
-rw-r--r--src/systems/interact.rs39
-rw-r--r--src/systems/mod.rs4
-rw-r--r--src/systems/talk.rs14
-rw-r--r--src/template.rs4
15 files changed, 168 insertions, 97 deletions
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<Self> {
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<String, Exchange>),
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<String, (Vec<ItemId>, Vec<ItemId>)>)
- >(arg.clone()).ok()?;
- Exchange(
- prefix,
- change.into_iter().map(
- |(id, (cost, offer))| (id, Exchange{cost, offer})
- ).collect::<HashMap<String, Exchange>>()
- )
- },
"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<String>) -> 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<String, Exchange>
+}
+
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<ItemId>, Vec<ItemId>)>) {
+ 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<Parameter>{
+ pub fn from_typed_json(typ: ParameterType, val: &Value) -> PResult<Parameter>{
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<Parameter>) list, v
({
v
- .as_array()?
+ .as_array().ok_or(perr!("{:?} not an array", v))?
.iter()
.map(|item| Parameter::guess_from_json(item))
- .collect::<Option<Vec<Parameter>>>()?
+ .collect::<PResult<Vec<Parameter>>>()?
})
(json!(["list", v.iter().map(Parameter::to_json).collect::<Vec<Value>>()]));
);
@@ -88,11 +90,11 @@ impl Parameter {
Self::String(string.to_string())
}
- pub fn guess_from_json(val: &Value) -> Option<Parameter> {
+ pub fn guess_from_json(val: &Value) -> PResult<Parameter> {
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<C>) -> 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<Visible>, ignore: &Entity) -> Vec<Entity> {
let mut entities: Vec<Entity> = 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") {