diff options
Diffstat (limited to 'server/src/recipes.rs')
| -rw-r--r-- | server/src/recipes.rs | 104 | 
1 files changed, 0 insertions, 104 deletions
| diff --git a/server/src/recipes.rs b/server/src/recipes.rs deleted file mode 100644 index 2dcb215c..00000000 --- a/server/src/recipes.rs +++ /dev/null @@ -1,104 +0,0 @@ -use crate::protocol::{ItemIndex, TileIndex}; -use glam::IVec2; -use log::debug; -use serde::{Deserialize, Serialize}; -use std::collections::HashMap; - -#[derive(Debug, Deserialize, Serialize, Clone, Copy, Default)] -#[serde(rename_all = "snake_case")] -pub enum Action { -    #[default] -    Never, -    Passive(f32), -    Active(f32), -    Instant, -} - -#[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>, -    #[serde(default)] -    pub action: Action, -} - -#[derive(Debug, Clone, Deserialize)] -pub struct InitialMap { -    map: Vec<String>, -    tiles: HashMap<String, String>, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct Gamedata { -    pub recipes: Vec<Recipe>, -    pub item_names: Vec<String>, -    pub tile_names: Vec<String>, -    #[serde(skip)] -    pub initial_map: HashMap<IVec2, TileIndex>, -} -pub fn build_gamedata(recipes_in: Vec<Recipe<String, String>>, map_in: InitialMap) -> 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(), -        }) -    } - -    let mut initial_map = HashMap::new(); -    for (y, line) in map_in.map.iter().enumerate() { -        for (x, tile) in line.trim().char_indices() { -            debug!("{tile:?}"); -            let tile = register(&mut tile_names, map_in.tiles[&tile.to_string()].clone()); -            initial_map.insert(IVec2::new(x as i32, y as i32), tile); -        } -    } - -    Gamedata { -        recipes, -        initial_map, -        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) -    } -} -impl Action { -    pub fn duration(&self) -> f32 { -        match self { -            Action::Instant | Action::Never => 0., -            Action::Passive(x) | Action::Active(x) => *x, -        } -    } -} | 
