aboutsummaryrefslogtreecommitdiff
path: root/server/protocol/src/helpers.rs
blob: 31e68d0220dcf637c0c3216a1aee391db5e8b8cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
    Hurry Curry! - a game about cooking
    Copyright (C) 2025 Hurry Curry! Contributors

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, version 3 of the License only.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

*/
use crate::{Gamedata, Hand, ItemIndex, ItemLocation, PlayerID, Recipe, RecipeIndex, TileIndex};
use std::fmt::Display;

impl Gamedata {
    pub fn tile_name(&self, index: TileIndex) -> &str {
        &self.tile_names[index.0]
    }
    pub fn item_name(&self, index: ItemIndex) -> &str {
        &self.item_names[index.0]
    }
    pub fn recipe(&self, index: RecipeIndex) -> &Recipe {
        &self.recipes[index.0]
    }
    pub fn get_tile_by_name(&self, name: &str) -> Option<TileIndex> {
        self.tile_names
            .iter()
            .position(|t| t == name)
            .map(TileIndex)
    }
    pub fn get_item_by_name(&self, name: &str) -> Option<ItemIndex> {
        self.item_names
            .iter()
            .position(|t| t == name)
            .map(ItemIndex)
    }
    pub fn recipes(&self) -> impl Iterator<Item = (RecipeIndex, &Recipe)> {
        self.recipes
            .iter()
            .enumerate()
            .map(|(i, e)| (RecipeIndex(i), e))
    }
}

impl Recipe {
    pub fn tile(&self) -> Option<TileIndex> {
        match self {
            Recipe::Passive { tile, .. } => *tile,
            Recipe::Active { tile, .. } => *tile,
            Recipe::Instant { tile, .. } => *tile,
        }
    }
    pub fn speed(&self) -> Option<f32> {
        match self {
            Recipe::Passive { speed, .. } => Some(*speed),
            Recipe::Active { speed, .. } => Some(*speed),
            _ => None,
        }
    }
    pub fn revert_speed(&self) -> Option<f32> {
        match self {
            Recipe::Passive { revert_speed, .. } => *revert_speed,
            _ => 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
        }
    }
}

impl Display for ItemLocation {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ItemLocation::Tile(pos) => write!(f, "tile({}, {})", pos.x, pos.y),
            ItemLocation::Player(player, hand) => write!(f, "{player}-{hand}"),
        }
    }
}

impl Display for Hand {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "hand#{}", self.0)
    }
}
impl Display for PlayerID {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "player#{}", self.0)
    }
}
impl Display for TileIndex {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "tile#{}", self.0)
    }
}
impl Display for ItemIndex {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "item#{}", self.0)
    }
}
impl Display for RecipeIndex {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "recipe#{}", self.0)
    }
}