diff options
Diffstat (limited to 'server/protocol')
| -rw-r--r-- | server/protocol/src/lib.rs | 82 | 
1 files changed, 82 insertions, 0 deletions
| diff --git a/server/protocol/src/lib.rs b/server/protocol/src/lib.rs index 8f8e9784..2c165a92 100644 --- a/server/protocol/src/lib.rs +++ b/server/protocol/src/lib.rs @@ -79,6 +79,7 @@ pub struct ClientGamedata {      pub tile_interact: Vec<bool>,      pub map_names: HashSet<String>, // for compat with game jam version      pub maps: HashMap<String, MapMetadata>, +    pub recipes: Vec<Recipe>,  }  #[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)] @@ -231,6 +232,87 @@ pub struct Score {      pub instant_recipes: usize,  } +#[derive(Debug, Clone, Serialize, Encode, Decode, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum Recipe { +    Passive { +        duration: f32, +        revert_duration: Option<f32>, +        tile: Option<TileIndex>, +        input: ItemIndex, +        output: Option<ItemIndex>, +        warn: bool, +    }, +    Active { +        duration: f32, +        tile: Option<TileIndex>, +        input: ItemIndex, +        outputs: [Option<ItemIndex>; 2], +    }, +    Instant { +        tile: Option<TileIndex>, +        inputs: [Option<ItemIndex>; 2], +        outputs: [Option<ItemIndex>; 2], +        points: i64, +    }, +} + +impl Recipe { +    pub fn tile(&self) -> Option<TileIndex> { +        match self { +            Recipe::Passive { tile, .. } => *tile, +            Recipe::Active { tile, .. } => *tile, +            Recipe::Instant { tile, .. } => *tile, +        } +    } +    pub fn duration(&self) -> Option<f32> { +        match self { +            Recipe::Passive { duration, .. } => Some(*duration), +            Recipe::Active { duration, .. } => Some(*duration), +            _ => None, +        } +    } +    pub fn revert_duration(&self) -> Option<f32> { +        match self { +            Recipe::Passive { +                revert_duration, .. +            } => *revert_duration, +            _ => None, +        } +    } +    pub fn warn(&self) -> bool { +        match self { +            Recipe::Passive { warn, .. } => *warn, +            _ => false, +        } +    } +    pub fn inputs(&self) -> Vec<ItemIndex> { +        match self { +            Recipe::Passive { input, .. } => vec![*input], +            Recipe::Active { input, .. } => vec![*input], +            Recipe::Instant { inputs, .. } => inputs.iter().flat_map(|e| e.to_owned()).collect(), +        } +    } +    pub fn outputs(&self) -> Vec<ItemIndex> { +        match self { +            Recipe::Passive { output, .. } => output.iter().copied().collect(), +            Recipe::Active { outputs, .. } => outputs.iter().flat_map(|e| e.to_owned()).collect(), +            Recipe::Instant { outputs, .. } => outputs.iter().flat_map(|e| e.to_owned()).collect(), +        } +    } +    pub fn supports_tile(&self, tile: Option<TileIndex>) -> bool { +        if let Some(tile_constraint) = self.tile() { +            if let Some(tile) = tile { +                tile == tile_constraint +            } else { +                false +            } +        } else { +            true +        } +    } +} +  #[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode, Copy, PartialEq, Eq, Hash)]  #[serde(rename_all = "snake_case")]  pub enum ItemLocation { | 
