diff --git a/src/main.rs b/src/main.rs index 091cf4a..8dcba10 100644 --- a/src/main.rs +++ b/src/main.rs @@ -111,51 +111,56 @@ fn cmd_position(mut tokens: std::str::SplitWhitespace<'_>, state: &mut MainState /// Play the game. fn cmd_go(mut tokens: std::str::SplitWhitespace<'_>, state: &mut MainState) { - let mut wtime = 0; - let mut btime = 0; + let mut wtime: Option = None; + let mut btime: Option = None; + let mut movetime: Option = None; macro_rules! set_time { - ($color: expr, $var: ident) => { + ($var: ident) => { if let Some(time) = tokens.next() { - if let Ok(time) = time.parse::() { - $var = time; - } + $var = time.parse::().ok(); } }; } + while let Some(token) = tokens.next() { match token { "wtime" => { - set_time!(Color::White, wtime) + set_time!(wtime) } "btime" => { - set_time!(Color::Black, btime) + set_time!(btime) + } + "movetime" => { + set_time!(movetime) } _ => ignore!(), } } - let (mut ourtime_ms, theirtime_ms) = if state.board.get_turn() == Color::White { + let (ourtime_ms, theirtime_ms) = if state.board.get_turn() == Color::White { (wtime, btime) } else { (btime, wtime) }; - if ourtime_ms == 0 { - ourtime_ms = 300_000 - } + let time_lims = if let Some(movetime) = movetime { + TimeLimits::from_movetime(movetime) + } else { + TimeLimits::from_ourtime_theirtime( + ourtime_ms.unwrap_or(300_000), + theirtime_ms.unwrap_or(300_000), + eval_metrics(&state.board), + ) + }; state .tx_engine .send(MsgToEngine::Go(Box::new(GoMessage { board: state.board, config: state.config, - time_lims: TimeLimits::from_ourtime_theirtime( - ourtime_ms, - theirtime_ms, - eval_metrics(&state.board), - ), + time_lims, }))) .unwrap(); } diff --git a/src/search.rs b/src/search.rs index a0b85f1..ddb9c76 100644 --- a/src/search.rs +++ b/src/search.rs @@ -454,7 +454,7 @@ impl TimeLimits { ourtime_ms: u64, _theirtime_ms: u64, eval: EvalMetrics, - ) -> TimeLimits { + ) -> Self { // hard timeout (max) let mut hard_ms = 100_000; // soft timeout (default max) @@ -488,6 +488,16 @@ impl TimeLimits { soft: Some(soft_limit), } } + + /// Make time limit based on an exact hard limit. + pub fn from_movetime(movetime_ms: u64) -> Self { + let hard_limit = Instant::now() + Duration::from_millis(movetime_ms); + + TimeLimits { + hard: Some(hard_limit), + soft: None, + } + } } /// Helper type to avoid retyping the same arguments into every function prototype.