cleans up raws/mod.rs with trait impls for json parsing
This commit is contained in:
parent
875e6bfee7
commit
dc1cb0562f
5 changed files with 60 additions and 21 deletions
9
raws/loot_tables.json
Normal file
9
raws/loot_tables.json
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"id": "animal",
|
||||||
|
"table": [
|
||||||
|
{ "id": "animal_hide", "weight": 1},
|
||||||
|
{ "id": "animal_meat", "weight": 1}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
@ -85,7 +85,8 @@
|
||||||
"flags": ["MONSTER", "BLOCKS_TILE"],
|
"flags": ["MONSTER", "BLOCKS_TILE"],
|
||||||
"bac": 6,
|
"bac": 6,
|
||||||
"vision_range": 8,
|
"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",
|
"id": "chicken",
|
||||||
|
|
|
||||||
13
src/raws/loot_table_structs.rs
Normal file
13
src/raws/loot_table_structs.rs
Normal 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,
|
||||||
|
}
|
||||||
|
|
@ -9,6 +9,8 @@ mod prop_structs;
|
||||||
use prop_structs::Prop;
|
use prop_structs::Prop;
|
||||||
mod spawn_table_structs;
|
mod spawn_table_structs;
|
||||||
use spawn_table_structs::*;
|
use spawn_table_structs::*;
|
||||||
|
mod loot_table_structs;
|
||||||
|
use loot_table_structs::*;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
|
|
@ -21,42 +23,48 @@ pub struct Raws {
|
||||||
pub mobs: Vec<Mob>,
|
pub mobs: Vec<Mob>,
|
||||||
pub props: Vec<Prop>,
|
pub props: Vec<Prop>,
|
||||||
pub spawn_tables: Vec<SpawnTable>,
|
pub spawn_tables: Vec<SpawnTable>,
|
||||||
|
pub loot_tables: Vec<LootTable>,
|
||||||
}
|
}
|
||||||
|
|
||||||
rltk::embedded_resource!(RAW_ITEMS, "../../raws/items.json");
|
rltk::embedded_resource!(RAW_ITEMS, "../../raws/items.json");
|
||||||
rltk::embedded_resource!(RAW_MOBS, "../../raws/mobs.json");
|
rltk::embedded_resource!(RAW_MOBS, "../../raws/mobs.json");
|
||||||
rltk::embedded_resource!(RAW_PROPS, "../../raws/props.json");
|
rltk::embedded_resource!(RAW_PROPS, "../../raws/props.json");
|
||||||
rltk::embedded_resource!(RAW_SPAWN_TABLES, "../../raws/spawn_tables.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() {
|
pub fn load_raws() {
|
||||||
rltk::link_resource!(RAW_ITEMS, "../../raws/items.json");
|
rltk::link_resource!(RAW_ITEMS, "../../raws/items.json");
|
||||||
rltk::link_resource!(RAW_MOBS, "../../raws/mobs.json");
|
rltk::link_resource!(RAW_MOBS, "../../raws/mobs.json");
|
||||||
rltk::link_resource!(RAW_PROPS, "../../raws/props.json");
|
rltk::link_resource!(RAW_PROPS, "../../raws/props.json");
|
||||||
rltk::link_resource!(RAW_SPAWN_TABLES, "../../raws/spawn_tables.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();
|
let decoded_raws = get_decoded_raws();
|
||||||
RAWS.lock().unwrap().load(decoded_raws);
|
RAWS.lock().unwrap().load(decoded_raws);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_decoded_raws() -> Raws {
|
pub fn get_decoded_raws() -> Raws {
|
||||||
// Get items from file
|
let items: Vec<Item> = ParseJson::parse_raws_into_vector("../../raws/items.json".to_string());
|
||||||
let mut raw_data = rltk::embedding::EMBED.lock().get_resource("../../raws/items.json".to_string()).unwrap();
|
let mobs: Vec<Mob> = ParseJson::parse_raws_into_vector("../../raws/mobs.json".to_string());
|
||||||
let mut raw_string = std::str::from_utf8(&raw_data).expect("Unable to convert to a valid UTF-8 string.");
|
let props: Vec<Prop> = ParseJson::parse_raws_into_vector("../../raws/props.json".to_string());
|
||||||
let items: Vec<Item> = serde_json::from_str(&raw_string).expect("Unable to parse items.json");
|
let spawn_tables: Vec<SpawnTable> = ParseJson::parse_raws_into_vector("../../raws/spawn_tables.json".to_string());
|
||||||
// Get mobs from file
|
let loot_tables: Vec<LootTable> = ParseJson::parse_raws_into_vector("../../raws/loot_tables.json".to_string());
|
||||||
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");
|
|
||||||
|
|
||||||
// Create combined raws
|
return Raws { items, mobs, props, spawn_tables, loot_tables };
|
||||||
let raws = Raws { items, mobs, props, spawn_tables };
|
|
||||||
return raws;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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>);
|
||||||
|
|
|
||||||
|
|
@ -21,16 +21,24 @@ pub struct RawMaster {
|
||||||
mob_index: HashMap<String, usize>,
|
mob_index: HashMap<String, usize>,
|
||||||
prop_index: HashMap<String, usize>,
|
prop_index: HashMap<String, usize>,
|
||||||
table_index: HashMap<String, usize>,
|
table_index: HashMap<String, usize>,
|
||||||
|
loot_index: HashMap<String, usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RawMaster {
|
impl RawMaster {
|
||||||
pub fn empty() -> RawMaster {
|
pub fn empty() -> RawMaster {
|
||||||
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(),
|
item_index: HashMap::new(),
|
||||||
mob_index: HashMap::new(),
|
mob_index: HashMap::new(),
|
||||||
prop_index: HashMap::new(),
|
prop_index: HashMap::new(),
|
||||||
table_index: HashMap::new(),
|
table_index: HashMap::new(),
|
||||||
|
loot_index: HashMap::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue