diff options
| author | metamuffin <metamuffin@disroot.org> | 2024-06-17 17:39:39 +0200 | 
|---|---|---|
| committer | metamuffin <metamuffin@disroot.org> | 2024-06-23 19:20:50 +0200 | 
| commit | 6f0424b9b4cddc0495eb673d314c570e27e61e83 (patch) | |
| tree | 3ca2f5c8f1d16020dfa432d8a93fb1f53be93c4b /server/src/recipes.rs | |
| parent | 428fa6fb8dac18c541c0c231f1b640ba172e52b9 (diff) | |
| download | hurrycurry-6f0424b9b4cddc0495eb673d314c570e27e61e83.tar hurrycurry-6f0424b9b4cddc0495eb673d314c570e27e61e83.tar.bz2 hurrycurry-6f0424b9b4cddc0495eb673d314c570e27e61e83.tar.zst | |
everything indexed
Diffstat (limited to 'server/src/recipes.rs')
| -rw-r--r-- | server/src/recipes.rs | 73 | 
1 files changed, 73 insertions, 0 deletions
| diff --git a/server/src/recipes.rs b/server/src/recipes.rs new file mode 100644 index 00000000..25a4be98 --- /dev/null +++ b/server/src/recipes.rs @@ -0,0 +1,73 @@ +use crate::protocol::{ItemIndex, TileIndex}; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Deserialize, Serialize, Clone, Copy)] +#[serde(rename_all = "snake_case")] +pub enum Action { +    Passive(f32), +    Active(f32), +    Instant, +    Never, +} + +#[derive(Debug, Clone, Deserialize, Serialize)] +pub struct Recipe<T = TileIndex, I = ItemIndex> { +    pub tile: T, +    #[serde(default)] +    pub inputs: Vec<I>, +    #[serde(default)] +    pub outputs: Vec<I>, +    pub action: Action, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Gamedata { +    pub recipes: Vec<Recipe>, +    pub item_names: Vec<String>, +    pub tile_names: Vec<String>, +} +pub fn build_gamedata(recipes_in: Vec<Recipe<String, String>>) -> Gamedata { +    let mut item_names = Vec::new(); +    let mut tile_names = Vec::new(); +    let mut recipes = Vec::new(); + +    for r in recipes_in { +        recipes.push(Recipe { +            action: r.action, +            tile: register(&mut tile_names, r.tile.clone()), +            inputs: r +                .inputs +                .clone() +                .into_iter() +                .map(|e| register(&mut item_names, e)) +                .collect(), +            outputs: r +                .outputs +                .clone() +                .into_iter() +                .map(|e| register(&mut item_names, e)) +                .collect(), +        }) +    } + +    Gamedata { +        recipes, +        item_names, +        tile_names, +    } +} +fn register(db: &mut Vec<String>, name: String) -> usize { +    if let Some(index) = db.iter().position(|e| e == &name) { +        index +    } else { +        let index = db.len(); +        db.push(name); +        index +    } +} + +impl Gamedata { +    pub fn get_tile(&self, name: &str) -> Option<TileIndex> { +        self.tile_names.iter().position(|t| t == name) +    } +} | 
