cleans up raws/mod.rs with trait impls for json parsing

This commit is contained in:
Llywelwyn 2023-07-30 09:27:47 +01:00
parent 875e6bfee7
commit dc1cb0562f
5 changed files with 60 additions and 21 deletions

9
raws/loot_tables.json Normal file
View file

@ -0,0 +1,9 @@
[
{
"id": "animal",
"table": [
{ "id": "animal_hide", "weight": 1},
{ "id": "animal_meat", "weight": 1}
]
}
]

View file

@ -85,7 +85,8 @@
"flags": ["MONSTER", "BLOCKS_TILE"],
"bac": 6,
"vision_range": 8,
"attacks": [{ "name": "bites", "hit_bonus": 0, "damage": "1d2" }]
"attacks": [{ "name": "bites", "hit_bonus": 0, "damage": "1d2" }],
"equipped": ["equip_shortsword", "equip_body_leather", "equip_head_leather"]
},
{
"id": "chicken",

View file

@ -0,0 +1,13 @@
use serde::Deserialize;
#[derive(Deserialize, Debug)]
pub struct LootTable {
pub id: String,
pub table: Vec<LootTableEntry>,
}
#[derive(Deserialize, Debug)]
pub struct LootTableEntry {
pub id: String,
pub weight: i32,
}

View file

@ -9,6 +9,8 @@ mod prop_structs;
use prop_structs::Prop;
mod spawn_table_structs;
use spawn_table_structs::*;
mod loot_table_structs;
use loot_table_structs::*;
use std::sync::Mutex;
lazy_static! {
@ -21,42 +23,48 @@ pub struct Raws {
pub mobs: Vec<Mob>,
pub props: Vec<Prop>,
pub spawn_tables: Vec<SpawnTable>,
pub loot_tables: Vec<LootTable>,
}
rltk::embedded_resource!(RAW_ITEMS, "../../raws/items.json");
rltk::embedded_resource!(RAW_MOBS, "../../raws/mobs.json");
rltk::embedded_resource!(RAW_PROPS, "../../raws/props.json");
rltk::embedded_resource!(RAW_SPAWN_TABLES, "../../raws/spawn_tables.json");
rltk::embedded_resource!(RAW_LOOT_TABLES, "../../raws/loot_tables.json");
pub fn load_raws() {
rltk::link_resource!(RAW_ITEMS, "../../raws/items.json");
rltk::link_resource!(RAW_MOBS, "../../raws/mobs.json");
rltk::link_resource!(RAW_PROPS, "../../raws/props.json");
rltk::link_resource!(RAW_SPAWN_TABLES, "../../raws/spawn_tables.json");
rltk::link_resource!(RAW_LOOT_TABLES, "../../raws/loot_tables.json");
let decoded_raws = get_decoded_raws();
RAWS.lock().unwrap().load(decoded_raws);
}
pub fn get_decoded_raws() -> Raws {
// Get items from file
let mut raw_data = rltk::embedding::EMBED.lock().get_resource("../../raws/items.json".to_string()).unwrap();
let mut raw_string = std::str::from_utf8(&raw_data).expect("Unable to convert to a valid UTF-8 string.");
let items: Vec<Item> = serde_json::from_str(&raw_string).expect("Unable to parse items.json");
// Get mobs from file
raw_data = rltk::embedding::EMBED.lock().get_resource("../../raws/mobs.json".to_string()).unwrap();
raw_string = std::str::from_utf8(&raw_data).expect("Unable to convert to a valid UTF-8 string.");
let mobs: Vec<Mob> = serde_json::from_str(&raw_string).expect("Unable to parse mobs.json");
// Get props from file
raw_data = rltk::embedding::EMBED.lock().get_resource("../../raws/props.json".to_string()).unwrap();
raw_string = std::str::from_utf8(&raw_data).expect("Unable to convert to a valid UTF-8 string.");
let props: Vec<Prop> = serde_json::from_str(&raw_string).expect("Unable to parse props.json");
// Get spawntables from file
raw_data = rltk::embedding::EMBED.lock().get_resource("../../raws/spawn_tables.json".to_string()).unwrap();
raw_string = std::str::from_utf8(&raw_data).expect("Unable to convert to a valid UTF-8 string.");
let spawn_tables: Vec<SpawnTable> = serde_json::from_str(&raw_string).expect("Unable to parse spawn_tables.json");
let items: Vec<Item> = ParseJson::parse_raws_into_vector("../../raws/items.json".to_string());
let mobs: Vec<Mob> = ParseJson::parse_raws_into_vector("../../raws/mobs.json".to_string());
let props: Vec<Prop> = ParseJson::parse_raws_into_vector("../../raws/props.json".to_string());
let spawn_tables: Vec<SpawnTable> = ParseJson::parse_raws_into_vector("../../raws/spawn_tables.json".to_string());
let loot_tables: Vec<LootTable> = ParseJson::parse_raws_into_vector("../../raws/loot_tables.json".to_string());
// Create combined raws
let raws = Raws { items, mobs, props, spawn_tables };
return raws;
return Raws { items, mobs, props, spawn_tables, loot_tables };
}
trait ParseJson {
fn parse_raws_into_vector(path: String) -> Self;
}
macro_rules! impl_ParseJson {
(for $($t:ty),+) => {
$(impl ParseJson for $t {
fn parse_raws_into_vector(path: String) -> $t {
let raw_data = rltk::embedding::EMBED.lock().get_resource(path).unwrap();
let raw_string = std::str::from_utf8(&raw_data).expect("Unable to convert to a valid UTF-8 string.");
return serde_json::from_str(&raw_string).expect("Unable to parse items.json");
}
})*
}
}
impl_ParseJson!(for Vec<Item>, Vec<Mob>, Vec<Prop>, Vec<SpawnTable>, Vec<LootTable>);

View file

@ -21,16 +21,24 @@ pub struct RawMaster {
mob_index: HashMap<String, usize>,
prop_index: HashMap<String, usize>,
table_index: HashMap<String, usize>,
loot_index: HashMap<String, usize>,
}
impl RawMaster {
pub fn empty() -> RawMaster {
RawMaster {
raws: Raws { items: Vec::new(), mobs: Vec::new(), props: Vec::new(), spawn_tables: Vec::new() },
raws: Raws {
items: Vec::new(),
mobs: Vec::new(),
props: Vec::new(),
spawn_tables: Vec::new(),
loot_tables: Vec::new(),
},
item_index: HashMap::new(),
mob_index: HashMap::new(),
prop_index: HashMap::new(),
table_index: HashMap::new(),
loot_index: HashMap::new(),
}
}