Skip to main content

bladeink/
state_patch.rs

1use std::{
2    collections::{HashMap, HashSet},
3    rc::Rc,
4};
5
6use crate::{container::Container, object::Object, value::Value};
7
8#[derive(Clone)]
9pub struct StatePatch {
10    pub globals: HashMap<String, Rc<Value>>,
11    pub changed_variables: HashSet<String>,
12    pub visit_counts: HashMap<String, i32>,
13    pub turn_indices: HashMap<String, i32>,
14}
15
16impl StatePatch {
17    pub fn new() -> StatePatch {
18        StatePatch {
19            globals: HashMap::new(),
20            changed_variables: HashSet::new(),
21            visit_counts: HashMap::new(),
22            turn_indices: HashMap::new(),
23        }
24    }
25
26    pub fn get_visit_count(&self, container: &Rc<Container>) -> Option<i32> {
27        let key = Object::get_path(container.as_ref()).to_string();
28        self.visit_counts.get(&key).copied()
29    }
30
31    pub fn set_visit_count(&mut self, container: &Rc<Container>, count: i32) {
32        let key = Object::get_path(container.as_ref()).to_string();
33        self.visit_counts.insert(key, count);
34    }
35
36    pub fn get_global(&self, name: &str) -> Option<Rc<Value>> {
37        self.globals.get(name).cloned()
38    }
39
40    pub fn set_global(&mut self, name: &str, value: Rc<Value>) {
41        self.globals.insert(name.to_string(), value);
42    }
43
44    pub(crate) fn add_changed_variable(&mut self, name: &str) {
45        self.changed_variables.insert(name.to_string());
46    }
47
48    pub(crate) fn set_turn_index(&mut self, container: &Container, index: i32) {
49        let key = Object::get_path(container).to_string();
50        self.turn_indices.insert(key, index);
51    }
52
53    pub(crate) fn get_turn_index(&self, container: &Container) -> Option<&i32> {
54        let key = Object::get_path(container).to_string();
55        return self.turn_indices.get(&key);
56    }
57}