Debian Codegraph @

src/game_config.rs

raw symbols SCIP
1 /*
2 * Copyright 2020-2026 Andrey Kutejko <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 *
17 * For more details see the file COPYING.
18 */
19
20 use gettextrs::gettext;
21 use std::num::NonZeroU32;
22 use std::rc::Rc;
23
24 #[derive(PartialEq, Eq)]
25 pub struct GameConfig {
26 pub name: &'static str,
27 pub display_name: String,
28 pub robot_type1: RobotTypeConfig,
29 pub robot_type2: RobotTypeConfig,
30 pub safe_teleports: Option<SafeTeleportsConfig>,
31 pub moveable_heaps: bool,
32 }
33
34 #[derive(PartialEq, Eq)]
35 pub struct RobotTypeConfig {
36 pub initial: u32,
37 pub increment: u32,
38 pub maximum: u32,
39 pub score: u32,
40 pub score_waiting: u32,
41 pub score_splatted: u32,
42 }
43
44 #[derive(PartialEq, Eq)]
45 pub struct SafeTeleportsConfig {
46 pub initial: u32,
47 pub free: u32,
48 pub max: u32,
49 pub kill_reward_price: Option<NonZeroU32>,
50 }
51
52 pub struct Reward {
53 pub price: u32,
54 pub safe_teleports_reward: u32,
55 }
56
57 impl RobotTypeConfig {
58 pub fn robots_on_level(&self, level: u32) -> u32 {
59 (self.initial + self.increment * level).clamp(0, self.maximum)
60 }
61 }
62
63 impl SafeTeleportsConfig {
64 pub fn free(&self, current: u32) -> u32 {
65 u32::min(self.free, self.max.saturating_sub(current))
66 }
67
68 pub fn kills_reward(&self, current: u32, kills: u32) -> Option<Reward> {
69 let kill_reward_price: u32 = self.kill_reward_price?.into();
70 let safe_teleports_reward =
71 u32::min(kills / kill_reward_price, self.max.saturating_sub(current));
72 (safe_teleports_reward > 0).then_some({
73 Reward {
74 price: safe_teleports_reward * kill_reward_price,
75 safe_teleports_reward,
76 }
77 })
78 }
79 }
80
81 fn classic_robots() -> GameConfig {
82 GameConfig {
83 name: "classic_robots",
84 display_name: gettext("Classic robots"),
85 robot_type1: RobotTypeConfig {
86 initial: 10,
87 increment: 10,
88 maximum: 9999,
89 score: 10,
90 score_waiting: 10,
91 score_splatted: 10,
92 },
93 robot_type2: RobotTypeConfig {
94 initial: 0,
95 increment: 0,
96 maximum: 0,
97 score: 0,
98 score_waiting: 0,
99 score_splatted: 0,
100 },
101 safe_teleports: None,
102 moveable_heaps: false,
103 }
104 }
105
106 fn robots_with_safe_teleport() -> GameConfig {
107 GameConfig {
108 name: "robots_with_safe_teleport",
109 display_name: gettext("Robots with safe teleport"),
110 safe_teleports: Some(SafeTeleportsConfig {
111 initial: 0,
112 free: 0,
113 max: 10,
114 kill_reward_price: NonZeroU32::new(1),
115 }),
116 ..classic_robots()
117 }
118 }
119
120 fn robots2() -> GameConfig {
121 GameConfig {
122 name: "robots2",
123 display_name: gettext("Robots2"),
124 robot_type1: RobotTypeConfig {
125 initial: 8,
126 increment: 8,
127 maximum: 9999,
128 score: 10,
129 score_waiting: 10,
130 score_splatted: 20,
131 },
132 robot_type2: RobotTypeConfig {
133 initial: 2,
134 increment: 2,
135 maximum: 9999,
136 score: 20,
137 score_waiting: 20,
138 score_splatted: 40,
139 },
140 safe_teleports: Some(SafeTeleportsConfig {
141 initial: 1,
142 free: 0,
143 max: 10,
144 kill_reward_price: NonZeroU32::new(1),
145 }),
146 moveable_heaps: true,
147 }
148 }
149
150 fn robots2_easy() -> GameConfig {
151 let robots2 = robots2();
152 GameConfig {
153 name: "robots2_easy",
154 display_name: gettext("Robots2 easy"),
155 safe_teleports: Some(SafeTeleportsConfig {
156 initial: 2,
157 free: 1,
158 ..robots2.safe_teleports.unwrap()
159 }),
160 ..robots2
161 }
162 }
163
164 fn nightmare() -> GameConfig {
165 GameConfig {
166 name: "nightmare",
167 display_name: gettext("Nightmare"),
168 robot_type1: RobotTypeConfig {
169 initial: 2,
170 increment: 2,
171 maximum: 9999,
172 score: 10,
173 score_waiting: 10,
174 score_splatted: 20,
175 },
176 robot_type2: RobotTypeConfig {
177 initial: 8,
178 increment: 8,
179 maximum: 9999,
180 score: 20,
181 score_waiting: 20,
182 score_splatted: 40,
183 },
184 safe_teleports: Some(SafeTeleportsConfig {
185 initial: 1,
186 free: 1,
187 max: 10,
188 kill_reward_price: NonZeroU32::new(2),
189 }),
190 moveable_heaps: true,
191 }
192 }
193
194 #[derive(Clone)]
195 pub struct GameConfigs {
196 pub game_configs: Vec<Rc<GameConfig>>,
197 }
198
199 impl GameConfigs {
200 pub fn new() -> Self {
201 Self {
202 game_configs: vec![
203 Rc::new(classic_robots()),
204 Rc::new(robots_with_safe_teleport()),
205 Rc::new(robots2()),
206 Rc::new(robots2_easy()),
207 Rc::new(nightmare()),
208 ],
209 }
210 }
211
212 pub fn find_by_name(&self, name: &str) -> Option<&Rc<GameConfig>> {
213 self.game_configs
214 .iter()
215 .find(|gc| gc.name == name || gc.name.replace('_', " ") == name)
216 }
217
218 pub fn best_match(&self, name: &str) -> &Rc<GameConfig> {
219 self.find_by_name(name)
220 .unwrap_or_else(|| &self.game_configs[0])
221 }
222 }