tmaze 1.15.1

Simple multiplatform maze solving game for terminal written entirely in Rust
Documentation
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
use cmaze::{game::GeneratorFn, gameboard::algorithms::MazeAlgorithm};
use crossterm::style::{Color, ContentStyle};
use derivative::Derivative;
use ron::{self, extensions::Extensions};
use serde::{Deserialize, Serialize};
use std::{
    fs, io,
    path::PathBuf,
    sync::{Arc, RwLock},
};

use crate::constants::base_path;

const DEFAULT_SETTINGS: &str = include_str!("./default_settings.ron");

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Offset {
    Abs(i32),
    Rel(f32),
}

impl Offset {
    pub fn to_abs(self, size: i32) -> i32 {
        match self {
            Offset::Rel(ratio) => (size as f32 * ratio.clamp(0., 0.5)).round() as i32,
            Offset::Abs(chars) => chars,
        }
    }
}

impl Default for Offset {
    fn default() -> Self {
        Offset::Rel(0.25)
    }
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
pub enum CameraMode {
    #[default]
    CloseFollow,
    EdgeFollow(Offset, Offset),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MazePreset {
    pub title: String,
    pub width: u16,
    pub height: u16,
    #[serde(default = "default_depth")]
    pub depth: u16,
    #[serde(default)]
    pub tower: bool,
    #[serde(default)]
    pub default: bool,
}

fn default_depth() -> u16 {
    1
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ColorScheme {
    pub normal: Color,
    pub player: Color,
    pub goal: Color,
    pub text: Color,
}

#[allow(dead_code)]
impl ColorScheme {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn normal(mut self, value: Color) -> Self {
        self.normal = value;
        self
    }

    pub fn player(mut self, value: Color) -> Self {
        self.player = value;
        self
    }

    pub fn goal(mut self, value: Color) -> Self {
        self.goal = value;
        self
    }

    pub fn text(mut self, value: Color) -> Self {
        self.text = value;
        self
    }

    pub fn normals(&self) -> ContentStyle {
        ContentStyle {
            foreground_color: Some(self.normal),
            background_color: None,
            ..Default::default()
        }
    }

    pub fn players(&self) -> ContentStyle {
        ContentStyle {
            foreground_color: Some(self.player),
            background_color: None,
            ..Default::default()
        }
    }

    pub fn goals(&self) -> ContentStyle {
        ContentStyle {
            foreground_color: Some(self.goal),
            background_color: None,
            ..Default::default()
        }
    }

    pub fn texts(&self) -> ContentStyle {
        ContentStyle {
            foreground_color: Some(self.text),
            background_color: None,
            ..Default::default()
        }
    }
}

impl Default for ColorScheme {
    fn default() -> Self {
        ColorScheme {
            normal: Color::White,
            player: Color::White,
            goal: Color::White,
            text: Color::White,
        }
    }
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
pub enum MazeGenAlgo {
    #[default]
    RandomKruskals,
    DepthFirstSearch,
}

impl MazeGenAlgo {
    pub fn to_fn(&self) -> GeneratorFn {
        match self {
            MazeGenAlgo::RandomKruskals => cmaze::gameboard::algorithms::RndKruskals::generate,
            MazeGenAlgo::DepthFirstSearch => {
                cmaze::gameboard::algorithms::DepthFirstSearch::generate
            }
        }
    }
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
pub enum UpdateCheckInterval {
    Never,
    #[default]
    Daily,
    Weekly,
    Monthly,
    Yearly,
    Always,
}

#[derive(Debug, Derivative, Clone, Serialize, Deserialize)]
#[derivative(Default)]
#[serde(rename = "Settings")]
pub struct SettingsInner {
    // general
    #[serde(default)]
    pub color_scheme: Option<ColorScheme>,
    #[serde(default)]
    // motion
    pub slow: Option<bool>,
    #[serde(default)]
    pub disable_tower_auto_up: Option<bool>,
    #[serde(default)]
    pub camera_mode: Option<CameraMode>,
    #[serde(default)]
    pub camera_smoothing: Option<f32>,
    #[serde[default]]
    pub player_smoothing: Option<f32>,

    // game config
    #[serde(default)]
    pub default_maze_gen_algo: Option<MazeGenAlgo>,
    #[serde(default)]
    pub dont_ask_for_maze_algo: Option<bool>,
    #[serde(default)]
    // update check
    pub update_check_interval: Option<UpdateCheckInterval>,
    #[serde(default)]
    pub display_update_check_errors: Option<bool>,

    // audio
    #[serde(default)]
    pub enable_audio: Option<bool>,
    #[serde(default)]
    pub audio_volume: Option<f32>,
    #[serde(default)]
    pub enable_music: Option<bool>,
    #[serde(default)]
    pub music_volume: Option<f32>,

    // mazes
    #[serde(default)]
    pub mazes: Option<Vec<MazePreset>>,

    // other
    #[serde(skip)]
    #[derivative(Default(value = "Settings::default_path()"))]
    pub path: PathBuf,
    // TODO: it's not possible in RON to have a HashMap with flattened keys,
    // so we will support it in different way formats
    // once we support them - this would mean dropping RON support
    // https://github.com/ron-rs/ron/issues/115
    // pub unknown_fields: HashMap<String, Value>,
}

#[derive(Debug, Clone)]
pub struct Settings(Arc<RwLock<SettingsInner>>);

impl Default for Settings {
    fn default() -> Self {
        let settings = SettingsInner::default();
        Self(Arc::new(RwLock::new(settings)))
    }
}

#[allow(dead_code)]
impl Settings {
    pub fn default_path() -> PathBuf {
        base_path().join("settings.ron")
    }

    pub fn new() -> Self {
        Self::default()
    }

    pub fn path(&self) -> PathBuf {
        self.0.read().unwrap().path.clone()
    }

    pub fn read(&self) -> std::sync::RwLockReadGuard<SettingsInner> {
        self.0.read().unwrap()
    }

    pub fn write(&mut self) -> std::sync::RwLockWriteGuard<SettingsInner> {
        self.0.write().unwrap()
    }

    pub fn get_color_scheme(&self) -> ColorScheme {
        self.read().color_scheme.clone().unwrap_or_default()
    }

    pub fn set_color_scheme(mut self, value: ColorScheme) -> Self {
        self.write().color_scheme = Some(value);
        self
    }

    pub fn set_slow(mut self, value: bool) -> Self {
        self.write().slow = Some(value);
        self
    }

    pub fn get_slow(&self) -> bool {
        self.read().slow.unwrap_or_default()
    }

    pub fn set_disable_tower_auto_up(mut self, value: bool) -> Self {
        self.write().disable_tower_auto_up = Some(value);
        self
    }

    pub fn get_disable_tower_auto_up(&self) -> bool {
        self.read().disable_tower_auto_up.unwrap_or_default()
    }

    pub fn set_camera_mode(mut self, value: CameraMode) -> Self {
        self.write().camera_mode = Some(value);
        self
    }

    pub fn get_camera_mode(&self) -> CameraMode {
        self.read().camera_mode.unwrap_or_default()
    }

    pub fn set_camera_smoothing(mut self, value: f32) -> Self {
        self.write().camera_smoothing = Some(value.clamp(0.5, 1.0));
        self
    }

    pub fn get_camera_smoothing(&self) -> f32 {
        self.read().camera_smoothing.unwrap_or(0.5).clamp(0.5, 1.0)
    }

    pub fn set_player_smoothing(mut self, value: f32) -> Self {
        self.write().player_smoothing = Some(value.clamp(0.5, 1.0));
        self
    }

    pub fn get_player_smoothing(&self) -> f32 {
        self.read().player_smoothing.unwrap_or(0.8).clamp(0.5, 1.0)
    }

    pub fn set_default_maze_gen_algo(mut self, value: MazeGenAlgo) -> Self {
        self.write().default_maze_gen_algo = Some(value);
        self
    }

    pub fn get_default_maze_gen_algo(&self) -> MazeGenAlgo {
        self.read().default_maze_gen_algo.unwrap_or_default()
    }

    pub fn set_dont_ask_for_maze_algo(mut self, value: bool) -> Self {
        self.write().dont_ask_for_maze_algo = Some(value);
        self
    }

    pub fn get_dont_ask_for_maze_algo(&self) -> bool {
        self.read().dont_ask_for_maze_algo.unwrap_or_default()
    }

    pub fn set_check_interval(mut self, value: UpdateCheckInterval) -> Self {
        self.write().update_check_interval = Some(value);
        self
    }

    pub fn get_check_interval(&self) -> UpdateCheckInterval {
        self.read().update_check_interval.unwrap_or_default()
    }

    pub fn get_display_update_check_errors(&self) -> bool {
        self.read().display_update_check_errors.unwrap_or(true)
    }

    pub fn set_display_update_check_errors(mut self, value: bool) -> Self {
        self.write().display_update_check_errors = Some(value);
        self
    }

    pub fn get_enable_audio(&self) -> bool {
        self.read().enable_audio.unwrap_or_default()
    }

    pub fn set_enable_audio(mut self, value: bool) -> Self {
        self.write().enable_audio = Some(value);
        self
    }

    pub fn get_audio_volume(&self) -> f32 {
        self.read().audio_volume.unwrap_or_default().clamp(0., 1.)
    }

    pub fn set_audio_volume(mut self, value: f32) -> Self {
        self.write().audio_volume = Some(value.clamp(0., 1.));
        self
    }

    pub fn get_enable_music(&self) -> bool {
        self.read().enable_music.unwrap_or_default()
    }

    pub fn set_enable_music(mut self, value: bool) -> Self {
        self.write().enable_music = Some(value);
        self
    }

    pub fn get_music_volume(&self) -> f32 {
        self.read().music_volume.unwrap_or_default().clamp(0., 1.)
    }

    pub fn set_music_volume(mut self, value: f32) -> Self {
        self.write().music_volume = Some(value.clamp(0., 1.));
        self
    }

    pub fn set_mazes(mut self, value: Vec<MazePreset>) -> Self {
        self.write().mazes = Some(value);
        self
    }

    pub fn get_mazes(&self) -> Vec<MazePreset> {
        self.read().mazes.clone().unwrap_or_default()
    }
}

impl Settings {
    pub fn load(path: PathBuf) -> io::Result<Self> {
        let default_settings_string = DEFAULT_SETTINGS;

        let settings_string = fs::read_to_string(&path);
        let options = ron::Options::default().with_default_extension(Extensions::IMPLICIT_SOME);
        let mut settings: SettingsInner = if let Ok(settings_string) = settings_string {
            options
                .from_str(&settings_string)
                .expect("Could not parse settings file")
        } else {
            fs::create_dir_all(path.parent().unwrap())?;
            fs::write(&path, default_settings_string)?;
            options.from_str(default_settings_string).unwrap()
        };

        settings.path = path;

        Ok(Self(Arc::new(RwLock::new(settings))))
    }

    pub fn reset(&mut self) {
        let default_settings_string = DEFAULT_SETTINGS;
        let options = ron::Options::default().with_default_extension(Extensions::IMPLICIT_SOME);
        *self.write() = options.from_str(default_settings_string).unwrap();

        let path = Settings::default_path();
        fs::write(&path, default_settings_string).unwrap();

        self.write().path = path;
    }

    pub fn reset_config(path: PathBuf) {
        let default_settings_string = DEFAULT_SETTINGS;
        fs::write(path, default_settings_string).unwrap();
    }
}