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
|
use crate::{
interaction::Recipe,
protocol::{DemandIndex, ItemIndex, RecipeIndex, TileIndex},
};
use glam::{IVec2, Vec2};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, sync::RwLock};
#[derive(Debug, Deserialize, Serialize, Clone, Copy, Default)]
#[serde(rename_all = "snake_case")]
pub enum Action {
#[default]
Never,
Passive(f32),
Active(f32),
Instant,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RecipeDecl {
#[serde(default)]
pub tile: Option<String>,
#[serde(default)]
pub inputs: Vec<String>,
#[serde(default)]
pub outputs: Vec<String>,
#[serde(default)]
pub action: Action,
}
#[derive(Debug, Clone, Deserialize)]
pub struct InitialMap {
map: Vec<String>,
tiles: HashMap<String, String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DemandDecl {
from: String,
to: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Demand {
pub from: ItemIndex,
pub to: ItemIndex,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Gamedata {
recipes: Vec<Recipe>,
pub demands: Vec<Demand>,
item_names: Vec<String>,
tile_names: Vec<String>,
#[serde(skip)]
pub initial_map: HashMap<IVec2, TileIndex>,
pub chef_spawn: Vec2,
pub customer_spawn: Vec2,
}
pub fn build_gamedata(
recipes_in: Vec<RecipeDecl>,
map_in: InitialMap,
demands_in: Vec<DemandDecl>,
) -> Gamedata {
let item_names = RwLock::new(Vec::new());
let tile_names = RwLock::new(Vec::new());
let mut recipes = Vec::new();
let mut demands = Vec::new();
for r in recipes_in {
let r2 = r.clone();
let mut inputs = r
.inputs
.into_iter()
.map(|i| ItemIndex(register(&item_names, i)));
let mut outputs = r
.outputs
.into_iter()
.map(|o| ItemIndex(register(&item_names, o)));
let tile = r.tile.map(|t| TileIndex(register(&tile_names, t)));
match r.action {
Action::Never => {}
Action::Passive(duration) => recipes.push(Recipe::Passive {
duration,
tile,
input: inputs.next().expect("passive recipe without input"),
output: outputs.next(),
}),
Action::Active(duration) => recipes.push(Recipe::Active {
duration,
tile,
input: inputs.next().expect("active recipe without input"),
outputs: [outputs.next(), outputs.next()],
}),
Action::Instant => {
recipes.push(Recipe::Instant {
tile,
inputs: [inputs.next(), inputs.next()],
outputs: [outputs.next(), outputs.next()],
});
}
}
assert_eq!(inputs.next(), None, "{r2:?}");
assert_eq!(outputs.next(), None, "{r2:?}");
}
for d in demands_in {
demands.push(Demand {
from: ItemIndex(register(&item_names, d.from)),
to: ItemIndex(register(&item_names, d.to)),
})
}
let mut chef_spawn = Vec2::new(0., 0.);
let mut customer_spawn = Vec2::new(0., 0.);
let mut initial_map = HashMap::new();
for (y, line) in map_in.map.iter().enumerate() {
for (x, tile) in line.trim().char_indices() {
let pos = IVec2::new(x as i32, y as i32);
let mut tilename = map_in.tiles[&tile.to_string()].clone();
if tilename == "chef-spawn" {
chef_spawn = pos.as_vec2();
tilename = "floor".to_owned();
}
if tilename == "customer-spawn" {
customer_spawn = pos.as_vec2();
tilename = "floor".to_owned();
}
let tile = TileIndex(register(&tile_names, tilename));
initial_map.insert(pos, tile);
}
}
Gamedata {
demands,
recipes,
initial_map,
item_names: item_names.into_inner().unwrap(),
tile_names: tile_names.into_inner().unwrap(),
chef_spawn,
customer_spawn,
}
}
fn register(db: &RwLock<Vec<String>>, name: String) -> usize {
let mut db = db.write().unwrap();
if let Some(index) = db.iter().position(|e| e == &name) {
index
} else {
let index = db.len();
db.push(name);
index
}
}
impl Gamedata {
pub fn tile_name(&self, index: TileIndex) -> &String {
&self.tile_names[index.0]
}
pub fn item_name(&self, index: ItemIndex) -> &String {
&self.item_names[index.0]
}
pub fn recipe(&self, index: RecipeIndex) -> &Recipe {
&self.recipes[index.0]
}
pub fn demand(&self, index: DemandIndex) -> &Demand {
&self.demands[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 Action {
pub fn duration(&self) -> f32 {
match self {
Action::Instant | Action::Never => 0.,
Action::Passive(x) | Action::Active(x) => *x,
}
}
}
|