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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
/*
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/>.
*/
pub mod book;
pub mod entities;
pub mod filter_demands;
pub mod recipes;
pub mod registry;
use crate::{
book::book,
entities::EntityDecl,
recipes::{RecipeDecl, load_recipes},
registry::{ItemTileRegistry, filter_unused_tiles_and_items},
};
use anyhow::{Context, Result, anyhow, bail};
use clap::Parser;
use filter_demands::filter_demands_and_recipes;
use hurrycurry_protocol::{
Gamedata, GamedataFlags, ItemIndex, MapMetadata, Recipe, TileIndex,
book::Book,
glam::{IVec2, Vec2},
};
use log::debug;
use serde::Deserialize;
use std::{
collections::{BTreeMap, BTreeSet, HashMap, HashSet},
fs::read_to_string,
path::Path,
time::Duration,
};
#[derive(Debug, Deserialize)]
pub struct DataIndex {
pub maps: HashMap<String, MapMetadata>,
pub recipes: HashSet<String>,
}
#[rustfmt::skip]
#[derive(Debug, Clone, Deserialize)]
pub struct MapDecl {
map: Vec<String>,
#[serde(default)] tiles: HashMap<char, String>,
#[serde(default)] use_palettes: Vec<String>,
#[serde(default = "default_recipes")] recipes: String,
#[serde(default)] hand_count: Option<usize>,
#[serde(default)] entities: Vec<EntityDecl>,
#[serde(default)] score_baseline: i64,
#[serde(default)] default_timer: Option<u64>,
#[serde(default)] flags: GamedataFlags,
}
fn default_recipes() -> String {
"default".to_string()
}
#[derive(Parser)]
struct TileArgs {
tiles: Vec<String>,
#[clap(long)]
book: bool,
#[clap(long)]
chef_spawn: bool,
#[clap(long)]
customer_spawn: bool,
#[clap(short = 'i', long)]
item: Option<String>,
#[clap(long)]
conveyor: Option<String>,
#[clap(long)]
demand_sink: bool,
}
#[derive(Debug, Clone, Default)]
#[rustfmt::skip]
pub struct Serverdata {
pub initial_map: HashMap<IVec2, (Vec<TileIndex>, Option<ItemIndex>)>,
pub chef_spawn: Vec2,
pub customer_spawn: Option<Vec2>,
pub score_baseline: i64,
pub default_timer: Option<Duration>,
pub book: Book,
pub entity_decls: Vec<EntityDecl>,
pub recipe_groups: BTreeMap<String, BTreeSet<ItemIndex>>,
}
pub fn build_data(
data_path: &Path,
map_name: &str,
generate_book: bool,
) -> Result<(Gamedata, Serverdata)> {
debug!("Preparing gamedata for {map_name}");
// Load index
let index =
read_to_string(data_path.join("index.yaml")).context("Failed reading data index")?;
let index = serde_yaml_ng::from_str::<DataIndex>(&index)?;
// Load map
if map_name.contains("..") || map_name.starts_with("/") || map_name.contains("//") {
bail!("illegal map path");
}
let map_in = read_to_string(data_path.join(format!("maps/{map_name}.yaml")))
.context("Failed reading map file")?;
let map_in = serde_yaml_ng::from_str::<MapDecl>(&map_in)?;
// Load recipes
if !index.recipes.contains(&map_in.recipes) {
bail!("unknown recipes: {:?}", map_in.recipes);
}
let recipes_in = read_to_string(data_path.join(format!("recipes/{}.yaml", map_in.recipes)))?;
let recipes_in = serde_yaml_ng::from_str::<Vec<RecipeDecl>>(&recipes_in)?;
// Load tile flags
let tile_flags =
read_to_string(data_path.join("tiles.yaml")).context("Failed reading tile flags")?;
let tile_flags = serde_yaml_ng::from_str::<HashMap<String, String>>(&tile_flags)?;
let palette = load_palette(data_path, &map_in)?;
let reg = ItemTileRegistry::default();
let (mut recipes, mut demands, recipe_groups) = load_recipes(recipes_in, ®)?;
let mut entities = Vec::new();
let mut chef_spawn = None;
let mut customer_spawn = None;
let mut initial_map = HashMap::new();
for (y, line) in map_in.map.iter().enumerate() {
for (x, char) in line.chars().enumerate() {
if char == ' ' {
continue; // space is empty space
}
let pos = IVec2::new(x as i32, y as i32);
let ts = palette
.get(&char)
.ok_or(anyhow!("tile {char} is undefined"))?;
let tiles = ts
.tiles
.iter()
.cloned()
.map(|t| reg.register_tile(t))
.collect();
let item = ts.item.clone().map(|i| reg.register_item(i));
initial_map.insert(pos, (tiles, item));
if ts.chef_spawn {
chef_spawn = Some(pos.as_vec2() + Vec2::splat(0.5));
}
if ts.customer_spawn {
customer_spawn = Some(pos.as_vec2() + Vec2::splat(0.5));
}
if ts.book {
entities.push(EntityDecl::Book { pos });
}
if ts.demand_sink {
entities.push(EntityDecl::DemandSink { pos });
}
if let Some(off) = &ts.conveyor {
let (x, y) = off
.split_once(",")
.ok_or(anyhow!("conveyor offset invalid format"))?;
let dir = IVec2::new(x.parse()?, y.parse()?);
entities.push(EntityDecl::Conveyor {
from: pos,
speed: None,
to: pos + dir,
});
}
}
}
let chef_spawn = chef_spawn.ok_or(anyhow!("map has no chef spawn"))?;
for mut e in map_in.entities.clone() {
match &mut e {
EntityDecl::Customers { unknown_order, .. } => {
*unknown_order = reg.register_item("unknown-order".to_owned())
}
EntityDecl::TagMinigame {
tag_item,
blocker_tile,
} => {
*tag_item = reg.register_item("lettuce".to_owned());
*blocker_tile = reg.register_tile("conveyor".to_owned());
}
EntityDecl::PlayerPortalPair {
in_tile,
out_tile,
neutral_tile,
..
} => {
*in_tile = reg.register_tile("black-hole".to_owned());
*neutral_tile = reg.register_tile("grey-hole".to_owned());
*out_tile = reg.register_tile("white-hole".to_owned());
}
EntityDecl::CtfMinigame {
items,
item_indices,
..
} => {
item_indices.extend(items.iter().cloned().map(|name| reg.register_item(name)));
}
_ => (),
}
entities.push(e);
}
debug!("{} entities created", entities.len());
filter_demands_and_recipes(&initial_map, &mut demands, &mut recipes);
let (items, tiles) = reg.finish();
let default_timer = if map_name.ends_with("lobby") {
None
} else {
Some(Duration::from_secs(map_in.default_timer.unwrap_or(420)))
};
let mut data = Gamedata {
current_map: map_name.to_string(),
maps: map_listing(&index),
tile_collide: tiles_flagged(&tile_flags, &tiles, 'c'),
tile_placeable_items: tile_placeable_items(
&initial_map,
&recipes,
tiles_flagged(&tile_flags, &tiles, 'x'),
),
tile_placeable_any: tiles_flagged(&tile_flags, &tiles, 'a'),
tile_interactable_empty: tiles_flagged(&tile_flags, &tiles, 'e')
.union(&tile_interactable_empty_bc_recipe(&recipes))
.copied()
.collect(),
flags: map_in.flags,
recipes,
item_names: items,
demands,
tile_names: tiles,
bot_algos: vec![
"waiter".to_string(),
"simple".to_string(),
"dishwasher".to_string(),
"frank".to_string(),
],
hand_count: map_in.hand_count.unwrap_or(1),
};
let mut serverdata = Serverdata {
initial_map,
chef_spawn,
customer_spawn,
default_timer,
book: Book::default(),
score_baseline: map_in.score_baseline,
entity_decls: entities,
recipe_groups,
};
filter_unused_tiles_and_items(&mut data, &mut serverdata);
if generate_book {
serverdata.book = book(&data, &serverdata).context("within book")?;
}
Ok((data, serverdata))
}
fn tile_placeable_items(
initial_map: &HashMap<IVec2, (Vec<TileIndex>, Option<ItemIndex>)>,
recipes: &[Recipe],
extiles: HashSet<TileIndex>,
) -> BTreeMap<TileIndex, HashSet<ItemIndex>> {
let mut tile_placeable_items = BTreeMap::new();
for tile in extiles {
let initially_placed = initial_map
.values()
.filter(|(t, _)| t.contains(&tile))
.flat_map(|(_, i)| i)
.copied();
let used_in_recipe = recipes
.iter()
.filter(|r| r.tile() == Some(tile))
.flat_map(|e| e.inputs());
tile_placeable_items.insert(tile, used_in_recipe.chain(initially_placed).collect());
}
tile_placeable_items
}
fn tile_interactable_empty_bc_recipe(recipes: &[Recipe]) -> HashSet<TileIndex> {
recipes
.iter()
.filter(|r| r.inputs().next().is_none())
.flat_map(|r| r.tile())
.collect()
}
fn map_listing(index: &DataIndex) -> Vec<(String, MapMetadata)> {
let mut maps = index
.maps
.iter()
.filter(|(_, v)| v.players > 0)
.map(|(k, v)| (k.to_owned(), v.to_owned()))
.collect::<Vec<(String, MapMetadata)>>();
maps.sort_unstable_by_key(|(_, m)| m.difficulty);
maps.sort_by_key(|(_, m)| m.players);
maps
}
fn load_palette(data_path: &Path, map_in: &MapDecl) -> Result<HashMap<char, TileArgs>> {
// Load palette
let palettes =
read_to_string(data_path.join("palettes.yaml")).context("Failed reading palettes")?;
let palettes = serde_yaml_ng::from_str::<HashMap<String, HashMap<char, String>>>(&palettes)?;
let mut raw_args = HashMap::new();
for p in &map_in.use_palettes {
raw_args.extend(
palettes
.get(p)
.cloned()
.ok_or(anyhow!("palette {p:?} is undefined"))?,
)
}
raw_args.extend(map_in.tiles.clone());
let mut palette = HashMap::new();
for (k, raw) in raw_args {
let mut toks = shlex::split(&raw).ok_or(anyhow!("tile stack quoting invalid"))?;
toks.insert(0, "tilestack".to_string()); // exe name
let args = TileArgs::try_parse_from(toks)
.context(anyhow!("tile declaration for {k:?} is invalid"))?;
palette.insert(k, args);
}
Ok(palette)
}
fn tiles_flagged(
tile_flags: &HashMap<String, String>,
tiles: &[String],
flag: char,
) -> HashSet<TileIndex> {
let mut out = HashSet::new();
for (i, tile) in tiles.iter().enumerate() {
if let Some(flags) = tile_flags.get(tile) {
if flags.contains(flag) {
out.insert(TileIndex(i));
}
}
}
out
}
|