intfic 0.3.8

An interactive fiction framework written 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
use text_io::read;

use crate::game_state::GameState;
use crate::print_debug;
use crate::write_out::{type_text, Color};

/// Yes, No, or Unsure. Corresponds with a dictionary of responses that indicate one of these three answers.
#[derive(Debug, PartialEq)]
pub enum Answer {
    /// Yes, y, yeah, sure, etc.
    Yes,
    /// No, n, nah, nope, etc.
    No,
    /// Not sure, idk, maybe, etc.
    Unsure,
}

/// Cardinal directions, as well as Up, Down, and Return. Corresponds with a dictionary of responses.
#[derive(Debug, PartialEq)]
pub enum Direction {
    /// North, n, forward, etc.
    North,
    /// East, e, right, etc.
    East,
    /// South, s, backward, etc.
    South,
    /// West, w, left, etc.
    West,
    /// Up, u, ascend, etc.
    Up,
    /// Down, d, descend, etc.
    Down,
    /// Return, r, go back, etc.
    Return,
}

// Dictionary for Answer::Yes
const AFFIRMATIVES: &[&str] = &[
    "10-4",
    "affirmative",
    "alright",
    "aye",
    "fuck yeah",
    "fuck yes",
    "hell yeah",
    "hell yes",
    "ok",
    "okay",
    "please",
    "positive",
    "sure",
    "y",
    "yay",
    "ye",
    "yeah",
    "yeah ok",
    "yeah sure",
    "yep",
    "yes",
    "yes please",
    "yup",
];

// Dictionary for Answer::No
const NEGATIVES: &[&str] = &[
    "fuck nah",
    "fuck no",
    "hell nah",
    "hell no",
    "n",
    "nah",
    "nay",
    "negative",
    "never",
    "no",
    "nope",
    "no please",
    "not ok",
    "not okay",
    "no way",
];

// Dictionary for Answer::Unsure
const UNSURATIVES: &[&str] = &[
    "dunno",
    "huh",
    "idk",
    "i dont know",
    "i dunno",
    "i guess",
    "maybe",
    "no clue",
    "no idea",
    "not sure",
    "que",
    "shrug",
    "unsure",
    "what",
];

// Dictionary for Direction::North
const NORTHS: &[&str] = &[
    "forward",
    "go forward",
    "go north",
    "n",
    "north",
    "northbound",
    "northward",
];

// Dictionary for Direction::East
const EASTS: &[&str] = &[
    "e",
    "east",
    "eastbound",
    "eastward",
    "go east",
    "go right",
    "right",
];

// Dictionary for Direction::South
const SOUTHS: &[&str] = &[
    "backward",
    "go backward",
    "go south",
    "s",
    "south",
    "southbound",
    "southward",
];

// Dictionary for Direction::West
const WESTS: &[&str] = &[
    "go left",
    "go west",
    "left",
    "w",
    "west",
    "westbound",
    "westward",
];

// Dictionary for Direction::Up
const UPS: &[&str] = &[
    "ascend",
    "climb",
    "climb up",
    "fly",
    "fly up",
    "go up",
    "rise",
    "u",
    "up",
];

// Dictionary for Direction::Down
const DOWNS: &[&str] = &[
    "climb down",
    "d",
    "descend",
    "down",
    "fall",
    "glide",
    "go down",
];

// Dictionary for Direction::Return
const RETURNS: &[&str] = &[
    "b",
    "back",
    "fall back",
    "go back",
    "r",
    "retreat",
    "return",
    "run",
    "run away",
];

// Dictionary for game.quit()
const EXITS: &[&str] = &[
    "exit",
    "exit game",
    "quit",
    "quit game",
];

// Dictionary for game.save()
const SAVES: &[&str] = &[
    "save",
    "save game",
];

// Dictionary for game.load()
const LOADS: &[&str] = &[
    "load",
    "load game",
];

/// Returns true if the given dictionary contains the given input string.
/// 
/// ```
/// # use intfic::parse_input::query;
/// assert_eq!(query("@AFFIRMATIVES", "sure"), true);
/// ```
/// Dictionaries are specified with an @ sign in front of them, and can be referenced this way in the "typed" field of a Choice.
/// ```text
///   Isn't that neat?
/// *- Yeah I guess. -> @AFFIRMATIVES -> neat
/// *- Not really. -> @NEGATIVES -> not_neat
/// ```
pub fn query(dict: &str, name: &str) -> bool {
    match dict {
        "@AFFIRMATIVES" => AFFIRMATIVES.contains(&name),
        "@NEGATIVES" => NEGATIVES.contains(&name),
        "@UNSURATIVES" => UNSURATIVES.contains(&name),
        "@NORTHS" => NORTHS.contains(&name),
        "@EASTS" => EASTS.contains(&name),
        "@SOUTHS" => SOUTHS.contains(&name),
        "@WESTS" => WESTS.contains(&name),
        "@UPS" => UPS.contains(&name),
        "@DOWNS" => DOWNS.contains(&name),
        "@RETURNS" => RETURNS.contains(&name),
        "@SAVES" => SAVES.contains(&name),
        "@LOADS" => LOADS.contains(&name),
        "@EXITS" => EXITS.contains(&name),
        _ => false,
    }
}

/// Returns a lowercase string containing only alphanumeric characters and spaces.
/// 
/// ```
/// # use intfic::parse_input::sanitize;
/// 
/// assert_eq!(sanitize(String::from("OH mOst DefiniTEly!?!")), String::from("oh most definitely"));
/// ```
pub fn sanitize(input: String) -> String {
    input
        .chars()
        .filter(|c| c.is_alphanumeric() || *c == ' ')
        .collect::<String>()
        .trim()
        .to_lowercase()
}

/// Gets input from the user and checks if it matches a keyword (returns None) or else returns Some(String).
/// 
/// Keywords are the following:
/// * exit - asks to save if you haven't recently, then quits the game. See [game_state::GameState::quit()](../game_state/struct.GameState.html#method.quit)
/// * save - saves the game. See [game_state::GameState::save()](../game_state/struct.GameState.html#method.save)
/// * load - loads the game. See [game_state::GameState::load()](../game_state/struct.GameState.html#method.load)
/// 
/// ```no_run
/// # use intfic::parse_input::get_input;
/// # use intfic::game_state::GameState;
/// let mut game: GameState = GameState::new("Test GameState");
/// 
/// assert_eq!(get_input(&mut game), Some(String::from("open door"))); // If the user typed "open door"
/// assert_eq!(get_input(&mut game), None); // If the user typed "load" or "save"
/// ```
pub fn get_input(game: &mut GameState) -> Option<String> {
    let input: String = sanitize(read!("{}\n"));

    if EXITS.contains(&&input[..]) {
        if game.get_flag("saved") {
            game.quit();
            None
        } else {
            match ask_question("Do you want to save first?", game) {
                Some(Answer::Yes) => {
                    game.save();
                    game.quit();
                    None
                }
                Some(Answer::No) => {
                    game.quit();
                    None
                }
                Some(Answer::Unsure) => {
                    type_text("I'll just save for you...", Color::White, false);
                    game.save();
                    game.quit();
                    None
                }
                _ => None,
            }
        }
    } else if SAVES.contains(&&input[..]) {
        game.save();
        game.start();
        None
    } else if LOADS.contains(&&input[..]) {
        game.load();
        game.start();
        None
    } else {
        Some(input)
    }
}

/// Asks a given yes-no question and returns Some(Answer) if the user doesn't type a keyword.
/// 
/// If the user types something that does not correspond to any of the Answer dictionaries, 
/// the question will repeat until a proper response or keyword is given.
/// 
/// ```no_run
/// # use intfic::parse_input::{ask_question, Answer};
/// # use intfic::game_state::GameState;
/// let mut game: GameState = GameState::new("Test GameState");
/// 
/// assert_eq!(ask_question("Continue?", &mut game), Some(Answer::Yes)); // If the user typed "y"
/// assert_eq!(ask_question("Continue?", &mut game), None); // If the user typed "load" or "save"
/// ```
pub fn ask_question(question: &str, game: &mut GameState) -> Option<Answer> {
    loop {
        type_text(question, Color::Cyan, true);
        if let Some(input) = get_input(game) {
            if input.is_empty() {
                continue;
            }

            if let Some(answer) = parse_answer(&input[..]) {
                return Some(answer);
            }

            type_text("I didn't understand that.", Color::White, false);
        } else {
            return None;
        }
    }
}

// Searches Answer dictionaries for given input
fn parse_answer(input: &str) -> Option<Answer> {
    if AFFIRMATIVES.contains(&input) {
        print_parse_result(input, "Answer->Yes");
        Some(Answer::Yes)
    } else if NEGATIVES.contains(&input) {
        print_parse_result(input, "Answer->No");
        Some(Answer::No)
    } else if UNSURATIVES.contains(&input) {
        print_parse_result(input, "Answer->Unsure");
        Some(Answer::Unsure)
    } else {
        print_parse_result(input, "Answer->None");
        None
    }
}

/// Asks for a direction and returns Some(Answer) if the user doesn't type a keyword.
/// 
/// If the user types something that does not correspond to any of the Direction dictionaries, 
/// the question will repeat until a proper response or keyword is given.
/// 
/// ```no_run
/// # use intfic::parse_input::{ask_direction, Direction};
/// # use intfic::game_state::GameState;
/// let mut game: GameState = GameState::new("Test GameState");
/// 
/// assert_eq!(ask_direction("Which Way?", &mut game), Some(Direction::North)); // If the user typed "Northward!"
/// assert_eq!(ask_direction("Which Way??", &mut game), None); // If the user typed "load" or "save"
/// ```
pub fn ask_direction(question: &str, game: &mut GameState) -> Option<Direction> {
    loop {
        type_text(question, Color::Cyan, true);
        if let Some(input) = get_input(game) {
            if input.is_empty() {
                continue;
            }

            if let Some(direction) = parse_direction(&input[..]) {
                return Some(direction);
            }

            type_text("I didn't understand that.", Color::White, false);
        } else {
            return None;
        }
    }
}

// Searches Direction dictionaries for given input
fn parse_direction(input: &str) -> Option<Direction> {
    if NORTHS.contains(&input) {
        print_parse_result(input, "Direction->North");
        Some(Direction::North)
    } else if EASTS.contains(&input) {
        print_parse_result(input, "Direction->East");
        Some(Direction::East)
    } else if SOUTHS.contains(&input) {
        print_parse_result(input, "Direction->South");
        Some(Direction::South)
    } else if WESTS.contains(&input) {
        print_parse_result(input, "Direction->West");
        Some(Direction::West)
    } else if UPS.contains(&input) {
        print_parse_result(input, "Direction->Up");
        Some(Direction::Up)
    } else if DOWNS.contains(&input) {
        print_parse_result(input, "Direction->Down");
        Some(Direction::Down)
    } else if RETURNS.contains(&input) {
        print_parse_result(input, "Direction->Return");
        Some(Direction::Return)
    } else {
        print_parse_result(input, "Direction->None");
        None
    }
}

// Helper to print the input and parsed result if DEBUG is enabled
fn print_parse_result(input: &str, parsed: &str) {
    print_debug(format!("Input: {}, Parsed: {}", input, parsed));
}