diff options
author | metamuffin <metamuffin@disroot.org> | 2024-08-11 13:35:15 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-08-11 13:35:15 +0200 |
commit | 52d99b16534631e293a23ddbc18c4ea70b71392f (patch) | |
tree | f596887a976540ab553e69105ab192cbbb2dd753 /server/protocol | |
parent | 63d5a3ff37d1e3972d34ccc9e1d26c3b4bc05efb (diff) | |
download | hurrycurry-52d99b16534631e293a23ddbc18c4ea70b71392f.tar hurrycurry-52d99b16534631e293a23ddbc18c4ea70b71392f.tar.bz2 hurrycurry-52d99b16534631e293a23ddbc18c4ea70b71392f.tar.zst |
add recipes back to protocol
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 { |