From 52d99b16534631e293a23ddbc18c4ea70b71392f Mon Sep 17 00:00:00 2001 From: metamuffin Date: Sun, 11 Aug 2024 13:35:15 +0200 Subject: add recipes back to protocol --- server/protocol/src/lib.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) (limited to 'server/protocol/src/lib.rs') 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, pub map_names: HashSet, // for compat with game jam version pub maps: HashMap, + pub recipes: Vec, } #[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, + tile: Option, + input: ItemIndex, + output: Option, + warn: bool, + }, + Active { + duration: f32, + tile: Option, + input: ItemIndex, + outputs: [Option; 2], + }, + Instant { + tile: Option, + inputs: [Option; 2], + outputs: [Option; 2], + points: i64, + }, +} + +impl Recipe { + pub fn tile(&self) -> Option { + match self { + Recipe::Passive { tile, .. } => *tile, + Recipe::Active { tile, .. } => *tile, + Recipe::Instant { tile, .. } => *tile, + } + } + pub fn duration(&self) -> Option { + match self { + Recipe::Passive { duration, .. } => Some(*duration), + Recipe::Active { duration, .. } => Some(*duration), + _ => None, + } + } + pub fn revert_duration(&self) -> Option { + 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 { + 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 { + 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) -> 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 { -- cgit v1.2.3-70-g09d2