File size: 983 Bytes
3014f14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// timeman.rs
use std::time::{Instant, Duration};

pub struct TimeManager {
    start_time: Instant,
    time_limit: Duration,
    nodes_searched: u64,
}

impl TimeManager {
    pub fn new(wtime: u64, btime: u64, winc: u64, binc: u64, side: crate::types::Color) -> Self {
        let (time, inc) = if side == crate::types::Color::White {
            (wtime, winc)
        } else {
            (btime, binc)
        };

        // Basic time allocation: 1/40th of remaining time + half increment
        let allocated = (time / 40) + (inc / 2);
        
        Self {
            start_time: Instant::now(),
            time_limit: Duration::from_millis(allocated),
            nodes_searched: 0,
        }
    }

    pub fn tick(&mut self) -> bool {
        self.nodes_searched += 1;
        if self.nodes_searched & 2047 == 0 {
            if self.start_time.elapsed() >= self.time_limit {
                return false; // Out of time
            }
        }
        true
    }
}