diff options
| author | troido <troido@protonmail.com> | 2020-04-09 12:57:24 +0200 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-04-09 12:57:24 +0200 |
| commit | 53e2db880893a1a2ddf797c7e991665c2cb05e29 (patch) | |
| tree | 3e5139737487a223552e1d02574c387e3bee8f58 | |
| parent | 72ac784ed8409676a04da5b3af159bac1724f383 (diff) | |
encyclopedia can substitute names
| -rw-r--r-- | content/encyclopediae/default_encyclopedia.json | 6 | ||||
| -rw-r--r-- | src/encyclopedia.rs | 61 |
2 files changed, 62 insertions, 5 deletions
diff --git a/content/encyclopediae/default_encyclopedia.json b/content/encyclopediae/default_encyclopedia.json index cfb55df..c1dcc89 100644 --- a/content/encyclopediae/default_encyclopedia.json +++ b/content/encyclopediae/default_encyclopedia.json @@ -416,7 +416,9 @@ "templates":{ "plantedcarrotseed": ["plantedseed", {"delay": 60, "next": "carrotseedling"}], "carrotseedling": ["seedling", {"delay": 60, "next": "youngcarrotplant"}], - "youngcarrotplant": ["youngplant", {"crop": "carrot", "delay": 60, "next": "carrotplant"}], - "radishes": ["radish", {}] + "youngcarrotplant": ["youngplant", {"crop": "carrot", "delay": 60, "next": "carrotplant"}] + }, + "substitute": { + "radishes": "radish" } } diff --git a/src/encyclopedia.rs b/src/encyclopedia.rs index d396e0f..be05959 100644 --- a/src/encyclopedia.rs +++ b/src/encyclopedia.rs @@ -19,7 +19,9 @@ use crate::{ #[derive(Default, Clone)] pub struct Encyclopedia { assemblages: HashMap<EntityType, Assemblage>, - items: HashMap<ItemId, Item> + items: HashMap<ItemId, Item>, + assemblage_substitute: HashMap<EntityType, EntityType>, + item_substitute: HashMap<ItemId, ItemId>, } impl Encyclopedia { @@ -101,9 +103,51 @@ impl Encyclopedia { assemblages.insert(EntityType(name.to_string()), assemblage); } + let assemblage_substitute = val + .get("assemblage_substitute") + .unwrap_or(&json!({})) + .as_object().ok_or(perr!("assemblage_subtitutions not a json dict"))? + .iter() + .chain( + val + .get("substitute") + .unwrap_or(&json!({})) + .as_object().ok_or(perr!("substitutions not a json dict"))? + .iter() + ) + .map(|(from, into)| { + Ok(( + EntityType(from.to_string()), + EntityType(into.as_str().ok_or(perr!("substitution value not a string: {:?}", into))?.to_string()) + )) + }) + .collect::<PResult<HashMap<EntityType, EntityType>>>()?; + + let item_substitute = val + .get("assemblage_substitute") + .unwrap_or(&json!({})) + .as_object().ok_or(perr!("assemblage_subtitutions not a json dict"))? + .iter() + .chain( + val + .get("substitute") + .unwrap_or(&json!({})) + .as_object().ok_or(perr!("substitutions not a json dict"))? + .iter() + ) + .map(|(from, into)| { + Ok(( + ItemId(from.to_string()), + ItemId(into.as_str().ok_or(perr!("substitution value not a string: {:?}", into))?.to_string()) + )) + }) + .collect::<PResult<HashMap<ItemId, ItemId>>>()?; + Ok(Encyclopedia{ assemblages, - items + items, + assemblage_substitute, + item_substitute }) } @@ -115,6 +159,12 @@ impl Encyclopedia { } pub fn construct(&self, template: &Template) -> Result<PreEntity> { + if let Some(new_name) = self.assemblage_substitute.get(&template.name){ + println!("substituting {:?} with {:?}", template.name, new_name); + let mut into = template.clone(); + into.name = new_name.clone(); + return self.construct(&into); + } let assemblage = self.assemblages .get(&template.name) .ok_or(aerr!("unknown assemblage name: '{:?}' in {:?}", template.name, template))?; @@ -122,7 +172,12 @@ impl Encyclopedia { } pub fn get_item(&self, id: &ItemId) -> Option<Item> { - self.items.get(id).map(|item| item.clone()) + let actual_id = if let Some(into) = self.item_substitute.get(id) { + into + } else { + id + }; + self.items.get(actual_id).map(|item| item.clone()) } } |
