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
use super::*;

pub fn identity_function<T>(object: T) -> T {
    object
}

fn print_info<T: fmt::Display>(message: &str, info: impl Into<Option<T>>) {
    if !GLOBAL_UCI_STATE.is_in_debug_mode() {
        return;
    }
    let mut to_print = if let Some(info_message) = info.into() {
        format!(
            "{} {}",
            message.colorize(SUCCESS_MESSAGE_STYLE),
            info_message.colorize(INFO_MESSAGE_STYLE),
        )
    } else {
        message.colorize(SUCCESS_MESSAGE_STYLE)
    };
    if GLOBAL_UCI_STATE.is_in_uci_mode() {
        to_print = format!("{} {to_print}", "info string".colorize(INFO_MESSAGE_STYLE))
    }
    println!("{to_print}");
}

#[derive(Debug)]
pub struct GlobalUCIState {
    _terminate_engine: AtomicBool,
    #[cfg(feature = "colored_output")]
    _colored_output: AtomicBool,
    _console_mode: AtomicBool,
    _t_table_size: Mutex<CacheTableSize>,
    _long_algebraic_notation: AtomicBool,
    _num_threads: AtomicUsize,
    _move_overhead: Mutex<Duration>,
    _use_own_book: AtomicBool,
    _debug_mode: AtomicBool,
    _chess960_mode: AtomicBool,
}

impl Default for GlobalUCIState {
    fn default() -> Self {
        Self::new()
    }
}

impl GlobalUCIState {
    pub const fn new() -> Self {
        GlobalUCIState {
            _terminate_engine: AtomicBool::new(false),
            #[cfg(feature = "colored_output")]
            _colored_output: AtomicBool::new(true),
            _console_mode: AtomicBool::new(true),
            _t_table_size: Mutex::new(CacheTableSize::Exact(16)),
            _long_algebraic_notation: AtomicBool::new(false),
            _num_threads: AtomicUsize::new(1),
            _move_overhead: Mutex::new(Duration::ZERO),
            _use_own_book: AtomicBool::new(false),
            _debug_mode: AtomicBool::new(true),
            _chess960_mode: AtomicBool::new(false),
        }
    }

    #[inline(always)]
    pub fn terminate_engine(&self) -> bool {
        self._terminate_engine.load(MEMORY_ORDERING)
    }

    #[inline(always)]
    pub fn set_engine_termination(&self, b: bool) {
        self._terminate_engine.store(b, MEMORY_ORDERING);
    }

    #[cfg(feature = "colored_output")]
    #[inline(always)]
    pub fn is_colored_output(&self) -> bool {
        self._colored_output.load(MEMORY_ORDERING)
    }

    #[cfg(not(feature = "colored_output"))]
    #[inline(always)]
    pub fn is_colored_output(&self) -> bool {
        false
    }

    #[cfg(feature = "colored_output")]
    pub fn set_colored_output(&self, b: bool, print: bool) {
        self._colored_output.store(b, MEMORY_ORDERING);
        if print {
            print_info("Colored output is set to", b);
        }
    }

    #[inline(always)]
    pub fn is_in_console_mode(&self) -> bool {
        self._console_mode.load(MEMORY_ORDERING)
    }

    #[inline(always)]
    pub fn is_in_uci_mode(&self) -> bool {
        !self.is_in_console_mode()
    }

    pub fn set_console_mode(&self, b: bool, print: bool) {
        self._console_mode.store(b, MEMORY_ORDERING);
        self._debug_mode.store(b, MEMORY_ORDERING);
        if print {
            print_info("UCI mode is set to", b);
        }
    }

    #[inline(always)]
    pub fn set_uci_mode(&self, b: bool, print: bool) {
        self.set_console_mode(!b, print);
    }

    #[inline(always)]
    pub fn set_to_uci_mode(&self) {
        self.set_uci_mode(true, false);
    }

    #[inline(always)]
    pub fn set_to_console_mode(&self) {
        self.set_console_mode(true, false);
    }

    #[inline(always)]
    pub fn get_t_table_size(&self) -> CacheTableSize {
        self._t_table_size.lock().unwrap().to_owned()
    }

    pub fn set_t_table_size(&self, transposition_table: &TranspositionTable, size: CacheTableSize) {
        //TODO: modify such that T Table and evaluation function takes same amount of space
        *self._t_table_size.lock().unwrap() = size;
        transposition_table.reset_size();
        if GLOBAL_UCI_STATE.is_in_debug_mode() {
            transposition_table.print_info();
        }
        print_info(
            "Transposition table is set to size to",
            size.to_cache_table_memory_size::<TranspositionTableEntry>(),
        );
    }

    #[inline(always)]
    pub fn use_long_algebraic_notation(&self) -> bool {
        self._long_algebraic_notation.load(MEMORY_ORDERING)
    }

    pub fn set_long_algebraic_notation(&self, b: bool) {
        self._long_algebraic_notation.store(b, MEMORY_ORDERING);
        print_info("Long algebraic notation is set to", b);
    }

    #[inline(always)]
    pub fn get_num_threads(&self) -> usize {
        self._num_threads.load(MEMORY_ORDERING)
    }

    pub fn set_num_threads(&self, num_threads: usize, print: bool) {
        self._num_threads.store(num_threads, MEMORY_ORDERING);
        if print {
            print_info("Number of threads is set to", num_threads);
        }
    }

    #[inline(always)]
    pub fn get_move_overhead(&self) -> Duration {
        self._move_overhead.lock().unwrap().to_owned()
    }

    pub fn set_move_overhead(&self, duration: Duration) {
        *self._move_overhead.lock().unwrap() = duration;
        print_info("Move Overhead is set to", duration.stringify());
    }

    #[inline(always)]
    pub fn use_own_book(&self) -> bool {
        self._use_own_book.load(MEMORY_ORDERING)
    }

    pub fn set_using_own_book(&self, b: bool) {
        self._use_own_book.store(b, MEMORY_ORDERING);
        print_info("Own Book Usage is set to", b);
    }

    #[inline(always)]
    pub fn is_in_debug_mode(&self) -> bool {
        self._debug_mode.load(MEMORY_ORDERING)
    }

    pub fn set_debug_mode(&self, b: bool) {
        self._debug_mode.store(b, MEMORY_ORDERING);
        print_info("Debug Mode is set to", b);
    }

    #[inline(always)]
    pub fn is_in_console_and_debug_mode(&self) -> bool {
        self.is_in_console_mode() && self.is_in_debug_mode()
    }

    #[inline(always)]
    pub fn is_in_chess960_mode(&self) -> bool {
        self._chess960_mode.load(MEMORY_ORDERING)
    }

    pub fn set_chess960_mode(&self, b: bool) {
        self._chess960_mode.store(b, MEMORY_ORDERING);
        print_info("Chess 960 mode is set to", b);
    }
}

#[cfg(feature = "engine")]
pub fn clear_all_cache_tables(transposition_table: &TranspositionTable) {
    transposition_table.clear();
    #[cfg(feature = "nnue")]
    EVALUATOR.clear();
    print_info::<&str>("All hash tables are cleared!", None);
}