-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructs.rs
316 lines (304 loc) · 9.51 KB
/
structs.rs
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
use crate::structs::CharId;
use quicksilver::graphics::Image;
use rand::seq::{IteratorRandom, SliceRandom};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, path::Path};
#[derive(Default)]
pub struct Module {
features: HashMap<String, TileFeatureRaw>,
species: HashMap<SpeciesType, SpeciesConf>,
pub images: HashMap<String, Image>,
tiles: Option<TilesConf>,
categories: HashMap<String, Vec<String>>,
}
impl Module {
pub fn new() -> Self {
Self {
features: HashMap::new(),
species: HashMap::new(),
images: HashMap::new(),
categories: HashMap::new(),
tiles: None,
}
}
pub fn add_image(&mut self, path: &Path, img: Image) {
let path = path.to_path_buf().into_os_string().into_string();
match path {
Ok(path) => {
self.images.insert(path, img);
}
Err(err) => println!("Could not convert path to String : {:?}", err),
}
}
pub fn set_species(&mut self, name: SpeciesType, species: SpeciesConf) {
self.species.insert(name, species);
}
pub fn set_tiles(&mut self, tiles: TilesConf) {
self.tiles = Some(tiles);
}
pub fn set_features(&mut self, name: String, features: TileFeatureRaw) {
self.categories
.entry(features.category.clone())
.or_insert_with(Vec::new)
.push(name.clone());
self.features.insert(name, features);
}
pub fn add_to_all_mods(mut self, all_mods: &mut ModulesContainer) -> HashMap<String, Image> {
let all_images = self.images.drain().collect();
all_mods.add_module(self);
all_images
}
}
#[derive(serde::Serialize, serde::Deserialize, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SpeciesKinds {
Land,
Water,
}
#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SpeciesConf {
kind: SpeciesKinds,
speeds: HashMap<String, usize>,
base_speeds: Vec<usize>,
pub name: SpeciesType,
possible_names: Vec<String>,
images: Vec<ImageName>,
}
#[derive(serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
pub enum Tile {
BaseTile {
image: String,
end: f64,
speeds: HashMap<SpeciesKinds, usize>,
},
ExtendingTile {
end: f64,
image: String,
extend: TileType,
speeds: Option<HashMap<SpeciesKinds, usize>>,
},
}
impl Tile {
pub fn get_end(&self) -> f64 {
match &self {
Tile::BaseTile { end, .. } | Tile::ExtendingTile { end, .. } => *end,
}
}
pub fn get_image(&self) -> &str {
match &self {
Tile::BaseTile { image, .. } | Tile::ExtendingTile { image, .. } => image,
}
}
pub fn get_speed(
&self,
tile: &str,
kind: SpeciesKinds,
overwrites: &HashMap<String, usize>,
tiles: &HashMap<String, Tile>,
) -> usize {
overwrites.get(tile).copied().unwrap_or_else(|| match self {
Tile::BaseTile { speeds, .. } => *speeds.get(&kind).unwrap(),
Tile::ExtendingTile { extend, speeds, .. } => speeds
.as_ref()
.and_then(|v| v.get(&kind).copied())
.unwrap_or_else(|| {
tiles
.get(extend)
.unwrap()
.get_speed(extend, kind, overwrites, tiles)
}),
})
}
}
fn def_false() -> bool {
false
}
#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TileFeatureRaw {
pub category: String,
pub image: String,
pub name: String,
pub speed_penalty: Option<usize>,
#[serde(default = "def_false")]
pub is_transparent: bool,
pub is_ownable: bool,
pub is_bed: bool,
pub can_walk_on: bool,
pub is_drawable: bool,
}
#[derive(Clone, Serialize, Deserialize)]
pub enum TileFeatures {
Ownable { owner: Option<CharId>, tile: String },
NotOwnable(String),
}
impl TileFeatures {
pub fn get_feature_name(&self) -> &str {
match self {
TileFeatures::Ownable { tile, .. } | TileFeatures::NotOwnable(tile) => &tile,
}
}
pub fn set_owned(&mut self, id: Option<CharId>) {
if let TileFeatures::Ownable { owner, .. } = self {
*owner = id
}
}
pub fn can_walk(&self, mods: &ModulesContainer) -> bool {
match self {
TileFeatures::NotOwnable(tile) | TileFeatures::Ownable { tile, .. } => {
mods.get_feature(tile).can_walk_on
}
}
}
pub fn is_owned_by(&self, id: CharId) -> bool {
match self {
TileFeatures::NotOwnable(_) => false,
TileFeatures::Ownable {
owner: Some(owner), ..
} => *owner == id,
TileFeatures::Ownable { owner: None, .. } => false,
}
}
pub fn can_sleep(&self, id: CharId, mods: &ModulesContainer) -> bool {
match self {
TileFeatures::NotOwnable(tile) => mods.get_feature(tile).is_bed,
TileFeatures::Ownable { tile, owner } => {
let tile = mods.get_feature(tile);
match (tile, owner) {
(TileFeatureRaw { is_bed: true, .. }, Some(owner)) => *owner == id,
(TileFeatureRaw { is_bed: false, .. }, _) => false,
(TileFeatureRaw { is_bed: true, .. }, None) => true,
}
}
}
}
pub fn get_speed_penalty(&self, mods: &ModulesContainer) -> Option<usize> {
match self {
TileFeatures::NotOwnable(tile) | TileFeatures::Ownable { tile, .. } => {
mods.get_feature(tile).speed_penalty
}
}
}
pub fn get_image<'a>(&self, mods: &'a ModulesContainer) -> &'a str {
match self {
TileFeatures::NotOwnable(tile) | TileFeatures::Ownable { tile, .. } => {
&mods.get_feature(tile).image
}
}
}
}
#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TilesConf {
generate_chances: HashMap<String, Tile>,
}
#[derive(PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize, Clone)]
pub struct SpeciesType(String);
impl SpeciesType {
pub fn get_speed_on_tile(
&self,
species: &HashMap<SpeciesType, SpeciesConf>,
tiles: &HashMap<String, Tile>,
tile: &str,
) -> usize {
let species = species.get(self).unwrap();
tiles
.get(tile)
.unwrap()
.get_speed(tile, species.kind, &species.speeds, &tiles)
}
}
impl<'a> From<&'a SpeciesType> for &'a str {
fn from(from: &'a SpeciesType) -> &'a str {
&from.0
}
}
impl From<SpeciesType> for String {
fn from(from: SpeciesType) -> String {
from.0
}
}
pub type ImageName = String;
pub type TileType = String;
#[derive(Default)]
pub struct ModulesContainer {
//modules : Vec<Module>,
pub all_species: HashMap<SpeciesType, SpeciesConf>,
pub all_tiles: HashMap<String, Tile>,
pub all_features: HashMap<String, TileFeatureRaw>,
pub all_categories: HashMap<String, Vec<String>>,
}
impl ModulesContainer {
pub fn get_species(&self, species: &SpeciesType) -> &SpeciesConf {
self.all_species.get(species).unwrap()
}
pub fn get_tile(&self, tile: &str) -> &Tile {
self.all_tiles.get(tile).unwrap()
}
pub fn get_feature(&self, feature: &str) -> &TileFeatureRaw {
self.all_features
.get(feature)
.unwrap_or_else(|| panic!("{:?} is not a loaded feature", feature))
}
pub fn add_module(&mut self, module: Module) {
self.all_species.extend(module.species);
self.all_features.extend(module.features);
self.all_categories.extend(module.categories);
if let Some(tiles) = module.tiles {
self.all_tiles.extend(tiles.generate_chances)
}
}
pub fn get_random_image_for_species(&self, species: &SpeciesType) -> ImageName {
let mut rng = rand::thread_rng();
self.all_species
.get(species)
.unwrap()
.images
.choose(&mut rng)
.unwrap()
.clone()
}
pub fn get_random_base_speed(&self, species: &SpeciesType) -> usize {
let mut rng = rand::thread_rng();
*self
.all_species
.get(species)
.unwrap()
.base_speeds
.choose(&mut rng)
.unwrap()
}
pub fn f64_to_tile(&self, num: f64) -> Option<String> {
let found: Option<(&str, &Tile)> = None;
let num = num + 1.0;
self.all_tiles
.iter()
.fold(found, |found, (name, tile)| {
if tile.get_end() > num {
if found.is_none() || found.unwrap().1.get_end() - num > tile.get_end() - num {
Some((name, tile))
} else {
found
}
} else {
found
}
})
.map(|v| v.0.into())
}
pub fn get_random_name_for_species(&self, species: &SpeciesType) -> String {
let mut rng = rand::thread_rng();
self.all_species
.get(species)
.unwrap()
.possible_names
.choose(&mut rng)
.unwrap()
.clone()
}
pub fn get_random_species(&self) -> SpeciesType {
let mut rng = rand::thread_rng();
self.all_species.keys().choose(&mut rng).unwrap().clone()
}
}