|
| 1 | +use tetka::games::isolation::{self, Position}; |
| 2 | +use tetka::uxi::{error, Bundle, Command, Flag, RunError}; |
| 3 | + |
| 4 | +use crate::search; |
| 5 | + |
| 6 | +use super::Context; |
| 7 | + |
| 8 | +// TODO: Move these macros into UXI |
| 9 | + |
| 10 | +macro_rules! lock { |
| 11 | + ($bundle:ident > $ctx:ident => $($stmt:stmt;)*) => { |
| 12 | + let $ctx = $bundle.lock(); |
| 13 | + $( |
| 14 | + $stmt |
| 15 | + )* |
| 16 | + drop($ctx); |
| 17 | + }; |
| 18 | + |
| 19 | + ($bundle:ident > mut $ctx:ident => $($stmt:stmt;)*) => { |
| 20 | + let mut $ctx = $bundle.lock(); |
| 21 | + $( |
| 22 | + $stmt |
| 23 | + )* |
| 24 | + drop($ctx); |
| 25 | + }; |
| 26 | +} |
| 27 | + |
| 28 | +pub fn go() -> Command<Context> { |
| 29 | + Command::new(|bundle: Bundle<Context>| { |
| 30 | + lock! { |
| 31 | + bundle > ctx => |
| 32 | + let position = ctx.position; // Get the position to search |
| 33 | + let mut searcher = ctx.searcher.clone(); // Get the previous search state |
| 34 | + } |
| 35 | + |
| 36 | + let mut nodes = 0; |
| 37 | + |
| 38 | + // Update the searcher with the new position and start searching. |
| 39 | + searcher.update_position(position); |
| 40 | + let limits = parse_limits(&bundle, &position)?; |
| 41 | + let bestmove = searcher.search(limits, &mut nodes); |
| 42 | + |
| 43 | + println!("bestmove {}", bestmove); |
| 44 | + |
| 45 | + lock! { |
| 46 | + bundle > mut ctx => |
| 47 | + // Push the new search state to the context. |
| 48 | + ctx.searcher = searcher; |
| 49 | + } |
| 50 | + |
| 51 | + Ok(()) |
| 52 | + }) |
| 53 | + // Flags for reporting the current time situation. |
| 54 | + .flag("p2inc", Flag::Single) |
| 55 | + .flag("p1inc", Flag::Single) |
| 56 | + .flag("p2time", Flag::Single) |
| 57 | + .flag("p1time", Flag::Single) |
| 58 | + .flag("movestogo", Flag::Single) |
| 59 | + // Flags for imposing other search limits. |
| 60 | + .flag("depth", Flag::Single) |
| 61 | + .flag("nodes", Flag::Single) |
| 62 | + .flag("movetime", Flag::Single) |
| 63 | + // Flags for setting the search type. |
| 64 | + // .flag("ponder", Flag::Single) |
| 65 | + .flag("infinite", Flag::Single) |
| 66 | + // This command should be run in a separate thread so that the Client |
| 67 | + // can still respond to and run other Commands while this one is running. |
| 68 | + .parallelize(true) |
| 69 | +} |
| 70 | + |
| 71 | +fn parse_limits(bundle: &Bundle<Context>, position: &Position) -> Result<search::Limits, RunError> { |
| 72 | + //////////////////////////////////////////// |
| 73 | + // Check which of the limit flags are set // |
| 74 | + //////////////////////////////////////////// |
| 75 | + |
| 76 | + // Standard Time Control flags |
| 77 | + let p1inc = bundle.is_flag_set("p1inc"); |
| 78 | + let p2inc = bundle.is_flag_set("p2inc"); |
| 79 | + let p1time = bundle.is_flag_set("p1time"); |
| 80 | + let p2time = bundle.is_flag_set("p2time"); |
| 81 | + |
| 82 | + // Other limit flags |
| 83 | + let depth = bundle.is_flag_set("depth"); |
| 84 | + let nodes = bundle.is_flag_set("nodes"); |
| 85 | + let movetime = bundle.is_flag_set("movetime"); |
| 86 | + |
| 87 | + // Infinite flag |
| 88 | + let infinite = bundle.is_flag_set("infinite"); |
| 89 | + |
| 90 | + /////////////////////////////////////////////////////// |
| 91 | + // Ensure that the given flag configuration is valid // |
| 92 | + /////////////////////////////////////////////////////// |
| 93 | + |
| 94 | + let std_tc = p2time || p1time || p2inc || p1inc; |
| 95 | + let oth_tc = depth || nodes || movetime; |
| 96 | + |
| 97 | + // No other time control flags may be set alongside 'infinite'. |
| 98 | + if infinite && (std_tc || oth_tc) { |
| 99 | + return error!("bad flag set: time control flags set alongside infinite"); |
| 100 | + } |
| 101 | + |
| 102 | + // A little utility macro to parse the given flag into the required type. |
| 103 | + macro_rules! get_flag { |
| 104 | + ($name:expr) => { |
| 105 | + match bundle.get_single_flag($name) { |
| 106 | + Some(value) => Some(value.parse()?), |
| 107 | + None => None, |
| 108 | + } |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | + //////////////////////////////////////////// |
| 113 | + // Parse the provided search/perft limits // |
| 114 | + //////////////////////////////////////////// |
| 115 | + |
| 116 | + Ok(search::Limits { |
| 117 | + maxnodes: get_flag!("nodes"), |
| 118 | + maxdepth: get_flag!("depth"), |
| 119 | + movetime: if std_tc { |
| 120 | + let (time, incr) = match position.side_to_move { |
| 121 | + isolation::Color::White => ("p1time", "p1inc"), |
| 122 | + isolation::Color::Black => ("p2time", "p2inc"), |
| 123 | + }; |
| 124 | + |
| 125 | + let time: u128 = get_flag!(time).unwrap_or(0); |
| 126 | + let incr: u128 = get_flag!(incr).unwrap_or(0); |
| 127 | + |
| 128 | + Some((time / 20 + incr / 2).max(1)) |
| 129 | + } else { |
| 130 | + get_flag!("movetime") |
| 131 | + }, |
| 132 | + movestogo: get_flag!("movestogo"), |
| 133 | + }) |
| 134 | +} |
0 commit comments