Rev 174 | Rev 177 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | pmbaty | 1 | // chessengine.cpp |
| 2 | |||
| 3 | #include "common.h" |
||
| 4 | |||
| 5 | |||
| 6 | // global variables used in this module only |
||
| 150 | pmbaty | 7 | static wchar_t chessengine_path[MAX_PATH]; |
| 8 | static wchar_t chessengine_shellcommand[MAX_PATH]; |
||
| 1 | pmbaty | 9 | static wchar_t chessengineinitfile_pathname[MAX_PATH]; |
| 10 | static int current_obstinacy; |
||
| 154 | pmbaty | 11 | static int max_cores; |
| 50 | pmbaty | 12 | static bool is_hint_pending; |
| 154 | pmbaty | 13 | static bool should_initialize; |
| 150 | pmbaty | 14 | FILE *fpipe; |
| 1 | pmbaty | 15 | |
| 16 | // prototypes of local functions |
||
| 17 | static void PlayerEngine_Recv (player_t *player); |
||
| 18 | static void PlayerEngine_Send (player_t *player); |
||
| 12 | pmbaty | 19 | static wchar_t *Move_BuildString (boardmove_t *move); |
| 1 | pmbaty | 20 | |
| 21 | |||
| 22 | bool PlayerEngine_Init (player_t *player) |
||
| 23 | { |
||
| 24 | // this function starts a chess engine as a child process. This process's stdin and |
||
| 25 | // stdout are redirected to the handles we give it, so that we may read/write to it. |
||
| 26 | |||
| 119 | pmbaty | 27 | engineprogram_t *program; |
| 150 | pmbaty | 28 | wchar_t current_path[MAX_PATH]; |
| 159 | pmbaty | 29 | wchar_t widechar_buffer[1024]; |
| 7 | pmbaty | 30 | SYSTEM_INFO sysinfo; |
| 174 | pmbaty | 31 | int attempt_index; |
| 1 | pmbaty | 32 | FILE *fp; |
| 33 | |||
| 34 | // reset stuff first |
||
| 35 | player->wants_hint = false; |
||
| 36 | |||
| 119 | pmbaty | 37 | // quick access to engine program |
| 38 | program = &options.engine.programs[options.engine.selected_program]; |
||
| 40 | pmbaty | 39 | |
| 1 | pmbaty | 40 | // build the chess engine module path and pathname |
| 150 | pmbaty | 41 | swprintf_s (chessengine_path, WCHAR_SIZEOF (chessengine_path), L"%s/engines/%s", app_path, program->folder); |
| 42 | swprintf_s (chessengine_shellcommand, WCHAR_SIZEOF (chessengine_shellcommand), L"\"%s/%s\" %s", chessengine_path, program->executable_name, program->executable_arguments); |
||
| 1 | pmbaty | 43 | |
| 150 | pmbaty | 44 | // start the engine behind a bidirectional I/O pipe |
| 45 | GetCurrentDirectory (WCHAR_SIZEOF (current_path), current_path); // save current directory |
||
| 46 | SetCurrentDirectory (chessengine_path); // temporarily set chess engine's startup directory as current directory |
||
| 47 | fpipe = pipe_open (chessengine_shellcommand, L"r+"); |
||
| 48 | SetCurrentDirectory (current_path); // restore current directory |
||
| 174 | pmbaty | 49 | for (attempt_index = 0; attempt_index < 10; attempt_index++) |
| 50 | if (!pipe_isalive (fpipe)) |
||
| 51 | Sleep (100); // wait for the pipe to come to life, stop as soon as it's alive |
||
| 52 | else |
||
| 53 | break; // wait for the pipe to come to life |
||
| 54 | if ((fpipe == NULL) || (attempt_index == 10)) |
||
| 1 | pmbaty | 55 | { |
| 175 | pmbaty | 56 | dont_nag = true; // remember NOT to nag the user if he experienced any bug |
| 57 | |||
| 1 | pmbaty | 58 | messagebox.hWndParent = hMainWnd; |
| 59 | wcscpy_s (messagebox.title, WCHAR_SIZEOF (messagebox.title), LOCALIZE (L"ImportantMessage")); |
||
| 60 | wcscpy_s (messagebox.text, WCHAR_SIZEOF (messagebox.text), LOCALIZE (L"Error_ChessEngineInitializationFailed")); |
||
| 61 | messagebox.flags = MB_ICONWARNING | MB_OK; |
||
| 62 | DialogBox_Message (&messagebox); // display a modeless error message box |
||
| 63 | |||
| 64 | PlayerEngine_Shutdown (player); // on error, shutdown the engine |
||
| 65 | return (false); |
||
| 66 | } |
||
| 67 | |||
| 154 | pmbaty | 68 | // SMP HACK -- save the max CPUs to use to core max - 1 (the computer will look like hung if all CPU is taken) |
| 69 | GetSystemInfo (&sysinfo); // get the number of cores |
||
| 70 | max_cores = max (1, sysinfo.dwNumberOfProcessors - 1); |
||
| 77 | pmbaty | 71 | |
| 150 | pmbaty | 72 | // optionally initialize the debug log file |
| 1 | pmbaty | 73 | Debug_Init (L"Chess engine output.txt"); |
| 153 | pmbaty | 74 | Debug_Log (L"===Using engine: %s [%s]===\n", program->friendly_name, chessengine_shellcommand); |
| 1 | pmbaty | 75 | |
| 32 | pmbaty | 76 | // build the init file full qualified path name and try to open it |
| 119 | pmbaty | 77 | swprintf_s (chessengineinitfile_pathname, WCHAR_SIZEOF (chessengineinitfile_pathname), L"%s/engines/%s/init.txt", app_path, program->folder); |
| 32 | pmbaty | 78 | _wfopen_s (&fp, chessengineinitfile_pathname, L"r"); |
| 79 | |||
| 80 | // could the init file be opened ? |
||
| 81 | if (fp != NULL) |
||
| 1 | pmbaty | 82 | { |
| 153 | pmbaty | 83 | Debug_Log (L"===Found initialization file, parsing...===\n", chessengineinitfile_pathname); |
| 1 | pmbaty | 84 | |
| 85 | // read line per line |
||
| 86 | while (fgetws (widechar_buffer, WCHAR_SIZEOF (widechar_buffer), fp) != NULL) |
||
| 87 | { |
||
| 88 | if ((widechar_buffer[0] == L'#') || (widechar_buffer[0] == L'\n')) |
||
| 89 | continue; // skip comments and empty lines |
||
| 90 | |||
| 91 | // new command line found, append it to the send buffer |
||
| 92 | Player_SendBuffer_Add (player, 1000, widechar_buffer); |
||
| 93 | } |
||
| 94 | |||
| 95 | fclose (fp); // finished, close the file |
||
| 96 | } |
||
| 97 | |||
| 98 | // everything's okay, set engine name as the player's name |
||
| 150 | pmbaty | 99 | wcscpy_s (player->name, WCHAR_SIZEOF (player->name), program->friendly_name); |
| 1 | pmbaty | 100 | |
| 154 | pmbaty | 101 | should_initialize = true; // remember the engine should be initialized |
| 1 | pmbaty | 102 | return (true); // finished |
| 103 | } |
||
| 104 | |||
| 105 | |||
| 106 | void PlayerEngine_Shutdown (player_t *player) |
||
| 107 | { |
||
| 108 | // this function terminates the chess engine process and releases all handles attached to it |
||
| 109 | |||
| 110 | int attempt_index; |
||
| 111 | |||
| 112 | // send the engine a quit command |
||
| 119 | pmbaty | 113 | Player_SendBuffer_Add (player, 1000, options.engine.programs[options.engine.selected_program].command_quit); |
| 39 | pmbaty | 114 | Player_SendBuffer_Add (player, 1000, L"\n"); // since the format string was read from the options, don't forget to end it with a carriage return |
| 1 | pmbaty | 115 | PlayerEngine_Send (player); |
| 116 | |||
| 117 | // check 10 times if the engine process has ended cleanly |
||
| 118 | for (attempt_index = 0; attempt_index < 10; attempt_index++) |
||
| 119 | { |
||
| 150 | pmbaty | 120 | if (!pipe_isalive (fpipe)) |
| 1 | pmbaty | 121 | break; // break as soon as we've told the process exited cleanly |
| 122 | |||
| 123 | Sleep (100); // else wait one tenth second |
||
| 124 | } |
||
| 125 | |||
| 150 | pmbaty | 126 | // close the pipe (this will terminate the child process if it hasn't terminated yet) |
| 127 | pipe_close (fpipe); |
||
| 1 | pmbaty | 128 | |
| 150 | pmbaty | 129 | Debug_Log (L"===Engine closed===\n"); |
| 1 | pmbaty | 130 | return; // finished |
| 131 | } |
||
| 132 | |||
| 133 | |||
| 134 | bool PlayerEngine_Think (player_t *player) |
||
| 135 | { |
||
| 136 | // this function reads and writes any necessary data from and to the chess engine. Returns TRUE if scene needs to be updated. |
||
| 137 | |||
| 119 | pmbaty | 138 | engineprogram_t *program; |
| 159 | pmbaty | 139 | wchar_t line_buffer[1024]; |
| 1 | pmbaty | 140 | wchar_t *line_pointer; |
| 141 | wchar_t *move_string; |
||
| 142 | int char_index; |
||
| 143 | int length; |
||
| 144 | boardmove_t move; |
||
| 145 | player_t *current_player; |
||
| 146 | player_t *opposite_player; |
||
| 147 | bool do_update; |
||
| 148 | |||
| 119 | pmbaty | 149 | // quick access to chess engine program |
| 150 | program = &options.engine.programs[options.engine.selected_program]; |
||
| 151 | |||
| 1 | pmbaty | 152 | // don't update the scene until told otherwise |
| 153 | do_update = false; |
||
| 154 | |||
| 155 | // get current and opposite players |
||
| 156 | current_player = Player_GetCurrent (); |
||
| 157 | opposite_player = Player_GetOpposite (); |
||
| 158 | |||
| 153 | pmbaty | 159 | // is the game still alive AND has the engine just died ? |
| 160 | if ((the_board.game_state == STATE_PLAYING) && !pipe_isalive (fpipe)) |
||
| 161 | { |
||
| 162 | Debug_Log (L"===Unrecoverable engine error: opponent wins!===\n"); |
||
| 175 | pmbaty | 163 | dont_nag = true; // remember NOT to nag the user if he experienced any bug |
| 153 | pmbaty | 164 | |
| 165 | // if opponent player is human, play the victory sound, else, play defeat sound at the center of the board |
||
| 166 | Audio_PlaySound (opposite_player->type == PLAYER_HUMAN ? SOUNDTYPE_VICTORY : SOUNDTYPE_DEFEAT, 0.0f, 0.0f, 0.04f); |
||
| 167 | |||
| 168 | // display a crash notification dialog box |
||
| 169 | if (MessageBox (hMainWnd, LOCALIZE (L"Error_EngineCrashed"), LOCALIZE (L"ImportantMessage"), MB_ICONWARNING | MB_YESNO) == IDYES) |
||
| 160 | pmbaty | 170 | if (Debug_SendLogToAuthor (true)) |
| 154 | pmbaty | 171 | MessageBox (hMainWnd, LOCALIZE (L"MessageSent"), PROGRAM_NAME, MB_ICONINFORMATION | MB_OK); // send the game engine history to the author |
| 172 | else |
||
| 173 | MessageBox (hMainWnd, LOCALIZE (L"NoInternetConnection"), LOCALIZE (L"FatalError"), MB_ICONWARNING | MB_OK); // and display an error message if failed |
||
| 153 | pmbaty | 174 | |
| 175 | // and display the game over dialog box |
||
| 176 | the_board.game_state = (opposite_player->color == COLOR_BLACK ? STATE_WHITEWIN_RESIGNORFORFEIT : STATE_BLACKWIN_RESIGNORFORFEIT); |
||
| 177 | DialogBox_EndGame (); |
||
| 178 | |||
| 179 | do_update = true; // remember to update the 3D scene |
||
| 180 | } |
||
| 181 | |||
| 1 | pmbaty | 182 | // read from pipe (non-blocking) |
| 183 | PlayerEngine_Recv (player); |
||
| 184 | |||
| 185 | //////////////// |
||
| 186 | // START PARSING |
||
| 187 | |||
| 188 | // read line per line... |
||
| 189 | line_pointer = player->recvbuffer; // start at the first character |
||
| 190 | while ((line_pointer = ReadACompleteLine (line_buffer, WCHAR_SIZEOF (line_buffer), line_pointer)) != NULL) |
||
| 191 | { |
||
| 192 | // is it an empty line or engine noise ? |
||
| 193 | if ((line_buffer[0] == 0) || iswspace (line_buffer[0])) |
||
| 194 | continue; // skip empty lines and engine noise |
||
| 195 | |||
| 86 | pmbaty | 196 | // is the engine offering us a draw ? |
| 197 | if (wcsncmp (line_buffer, L"offer draw", 10) == 0) |
||
| 198 | continue; // if so, discard its offer (engine draws are unsupported) and proceed to the next line |
||
| 199 | |||
| 200 | // else is it an *irrevocable* draw ? (at this point there's nothing we can do but accept it) |
||
| 201 | else if (wcsstr (line_buffer, L"1/2-1/2") != NULL) |
||
| 53 | pmbaty | 202 | { |
| 86 | pmbaty | 203 | Debug_Log (L"===Engine tells us that it's a draw: nobody wins!===\n"); |
| 53 | pmbaty | 204 | |
| 116 | pmbaty | 205 | // play defeat sound (a draw is always a sort of defeat...) at the center of the board |
| 206 | Audio_PlaySound (SOUNDTYPE_DEFEAT, 0.0f, 0.0f, 0.04f); |
||
| 86 | pmbaty | 207 | |
| 208 | // display the game over dialog box |
||
| 209 | the_board.game_state = STATE_DRAW_OTHER; |
||
| 210 | DialogBox_EndGame (); |
||
| 211 | |||
| 212 | do_update = true; // remember to update the 3D scene |
||
| 213 | continue; // we processed that line |
||
| 214 | } |
||
| 215 | |||
| 216 | // else is the engine *irrevocably* resigning ? (at this point there's nothing we can do but accept it) |
||
| 217 | else if (wcsncmp (line_buffer, L"resign", 6) == 0) |
||
| 218 | { |
||
| 219 | Debug_Log (L"===Engine tells us that %s resigns: %s wins!===\n", (current_player->color == COLOR_BLACK ? "black" : "white"), (current_player->color == COLOR_BLACK ? "white" : "black")); |
||
| 220 | |||
| 116 | pmbaty | 221 | // if opponent player is human, play the victory sound, else, play defeat sound at the center of the board |
| 222 | Audio_PlaySound (opposite_player->type == PLAYER_HUMAN ? SOUNDTYPE_VICTORY : SOUNDTYPE_DEFEAT, 0.0f, 0.0f, 0.04f); |
||
| 53 | pmbaty | 223 | |
| 224 | // display the game over dialog box |
||
| 86 | pmbaty | 225 | the_board.game_state = (current_player->color == COLOR_BLACK ? STATE_WHITEWIN_RESIGNORFORFEIT : STATE_BLACKWIN_RESIGNORFORFEIT); |
| 53 | pmbaty | 226 | DialogBox_EndGame (); |
| 227 | |||
| 228 | do_update = true; // remember to update the 3D scene |
||
| 229 | continue; // we processed that line |
||
| 230 | } |
||
| 231 | |||
| 1 | pmbaty | 232 | // is engine allowed to resign ? if so, check if it's a game results |
| 233 | if (options.engine.obstinacy_level >= 0) |
||
| 234 | { |
||
| 235 | // is it a game result AND are we still in play ? |
||
| 236 | if ((wcsstr (line_buffer, L"1-0") != NULL) && (the_board.game_state == STATE_PLAYING)) |
||
| 237 | { |
||
| 238 | // have we NOT been obstinate enough ? |
||
| 239 | if (current_obstinacy < options.engine.obstinacy_level) |
||
| 240 | current_obstinacy++; // if so, discard this resign and go on |
||
| 241 | else |
||
| 242 | { |
||
| 243 | Debug_Log (L"===Engine tells us that black resigns: white wins!===\n"); |
||
| 244 | |||
| 116 | pmbaty | 245 | // if opponent player is human, play the victory sound, else, play defeat sound at the center of the board |
| 246 | Audio_PlaySound (opposite_player->type == PLAYER_HUMAN ? SOUNDTYPE_VICTORY : SOUNDTYPE_DEFEAT, 0.0f, 0.0f, 0.04f); |
||
| 1 | pmbaty | 247 | |
| 248 | // display the game over dialog box |
||
| 249 | the_board.game_state = STATE_WHITEWIN_RESIGNORFORFEIT; |
||
| 250 | DialogBox_EndGame (); |
||
| 251 | |||
| 252 | do_update = true; // remember to update the 3D scene |
||
| 253 | } |
||
| 254 | |||
| 255 | continue; // we processed that line |
||
| 256 | } |
||
| 257 | else if ((wcsstr (line_buffer, L"0-1") != NULL) && (the_board.game_state == STATE_PLAYING)) |
||
| 258 | { |
||
| 259 | // have we NOT been obstinate enough ? |
||
| 260 | if (current_obstinacy < options.engine.obstinacy_level) |
||
| 261 | current_obstinacy++; // if so, discard this resign and go on |
||
| 262 | else |
||
| 263 | { |
||
| 264 | Debug_Log (L"===Engine tells us that white resigns: black wins!===\n"); |
||
| 265 | |||
| 116 | pmbaty | 266 | // if opponent player is human, play the victory sound, else, play defeat sound at the center of the board |
| 267 | Audio_PlaySound (opposite_player->type == PLAYER_HUMAN ? SOUNDTYPE_VICTORY : SOUNDTYPE_DEFEAT, 0.0f, 0.0f, 0.04f); |
||
| 1 | pmbaty | 268 | |
| 269 | // display the game over dialog box |
||
| 270 | the_board.game_state = STATE_BLACKWIN_RESIGNORFORFEIT; |
||
| 271 | DialogBox_EndGame (); |
||
| 272 | |||
| 273 | do_update = true; // remember to update the 3D scene |
||
| 274 | } |
||
| 275 | |||
| 276 | continue; // we processed that line |
||
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | // has the game ended already ? |
||
| 281 | if (the_board.game_state > STATE_PLAYING) |
||
| 282 | continue; // ignore all that the engine tells us. Game is over already. |
||
| 283 | |||
| 153 | pmbaty | 284 | // else is it an unrecoverable engine error ? |
| 154 | pmbaty | 285 | else if ((wcsstr (line_buffer, L"illegal") != NULL) || (wcsstr (line_buffer, L"invalid") != NULL)) |
| 153 | pmbaty | 286 | { |
| 287 | Debug_Log (L"===Unrecoverable engine error: opponent wins!===\n"); |
||
| 175 | pmbaty | 288 | dont_nag = true; // remember NOT to nag the user if he experienced any bug |
| 153 | pmbaty | 289 | |
| 290 | // if opponent player is human, play the victory sound, else, play defeat sound at the center of the board |
||
| 291 | Audio_PlaySound (opposite_player->type == PLAYER_HUMAN ? SOUNDTYPE_VICTORY : SOUNDTYPE_DEFEAT, 0.0f, 0.0f, 0.04f); |
||
| 292 | |||
| 293 | // display a crash notification dialog box |
||
| 294 | if (MessageBox (hMainWnd, LOCALIZE (L"Error_EngineCrashed"), LOCALIZE (L"ImportantMessage"), MB_ICONWARNING | MB_YESNO) == IDYES) |
||
| 160 | pmbaty | 295 | if (Debug_SendLogToAuthor (true)) |
| 154 | pmbaty | 296 | MessageBox (hMainWnd, LOCALIZE (L"MessageSent"), PROGRAM_NAME, MB_ICONINFORMATION | MB_OK); // send the game engine history to the author |
| 297 | else |
||
| 298 | MessageBox (hMainWnd, LOCALIZE (L"NoInternetConnection"), LOCALIZE (L"FatalError"), MB_ICONWARNING | MB_OK); // and display an error message if failed |
||
| 153 | pmbaty | 299 | |
| 300 | // and display the game over dialog box |
||
| 301 | the_board.game_state = (opposite_player->color == COLOR_BLACK ? STATE_WHITEWIN_RESIGNORFORFEIT : STATE_BLACKWIN_RESIGNORFORFEIT); |
||
| 302 | DialogBox_EndGame (); |
||
| 303 | |||
| 304 | do_update = true; // remember to update the 3D scene |
||
| 305 | } |
||
| 306 | |||
| 154 | pmbaty | 307 | // else is it a normal move ? |
| 308 | else if ((move_string = wcsstr (line_buffer, program->replystring_move)) != NULL) |
||
| 309 | move_string += wcslen (program->replystring_move); // go to the parsable data |
||
| 310 | |||
| 1 | pmbaty | 311 | // else it's any other sort of line |
| 312 | else |
||
| 313 | continue; // skip lines that don't contain any valid move data |
||
| 314 | |||
| 315 | // now we are sure it's either a hint or a move (or some pondering) |
||
| 316 | |||
| 317 | length = wcslen (move_string); |
||
| 154 | pmbaty | 318 | if (length < 2) |
| 319 | continue; // if string is too short to be a move, skip it |
||
| 1 | pmbaty | 320 | |
| 321 | // there must be valid move data on that line. |
||
| 322 | |||
| 323 | // evaluate the engine move string |
||
| 324 | memcpy (move.slots, the_board.moves[the_board.move_count - 1].slots, sizeof (move.slots)); |
||
| 154 | pmbaty | 325 | char_index = 0; |
| 326 | while ((move_string[char_index] != 0) && !iswspace (move_string[char_index])) |
||
| 327 | { |
||
| 328 | move.pgntext[char_index] = move_string[char_index]; |
||
| 329 | char_index++; |
||
| 330 | } |
||
| 331 | move.pgntext[char_index] = 0; |
||
| 1 | pmbaty | 332 | if (!Move_SetupFromSAN (&the_board.moves[the_board.move_count - 1], &move, Board_ColorToMove (&the_board))) |
| 333 | { |
||
| 334 | Debug_Log (L"===Skipping line (invalid move syntax)===\n%s", move_string); |
||
| 335 | continue; // so now, if there are NOT two digits AND it's not a kind of castle, it can't be a move so skip that line |
||
| 336 | } |
||
| 337 | |||
| 338 | // is it NOT a hint, are blunders allowed, should we do one now AND can we do one now ? |
||
| 50 | pmbaty | 339 | if (!is_hint_pending && (options.engine.blunder_chances > 0) && (rand () % 100 < options.engine.blunder_chances) |
| 1 | pmbaty | 340 | && Move_FindRandomMove (&the_board.moves[the_board.move_count - 1], player->color, &move)) |
| 341 | { |
||
| 154 | pmbaty | 342 | if (program->command_force[0] != 0) |
| 343 | { |
||
| 344 | Player_SendBuffer_Add (player, 1000, program->command_force, Move_BuildString (&move)); // send the blunderous move to the engine |
||
| 345 | Player_SendBuffer_Add (player, 1000, L"\n"); // since the format string was read from the options, don't forget to end it with a carriage return |
||
| 346 | if (_wcsnicmp (program->friendly_name, L"Crafty", 6) == 0) |
||
| 347 | Player_SendBuffer_Add (player, 1000, L"disp\n"); // FIXME: we no longer need this Crafty-specific hack, do we? |
||
| 348 | } |
||
| 12 | pmbaty | 349 | Debug_Log (L"===Discarding engine move, forcing a blunderous move (%s) instead===\n", Move_BuildString (&move)); // blunder |
| 1 | pmbaty | 350 | } |
| 351 | |||
| 352 | // mark the engine's selected and hovered squares |
||
| 353 | Board_SetSelectedAndHovered (&the_board, move.source[0], move.source[1], move.target[0], move.target[1]); |
||
| 354 | |||
| 355 | // was it NOT a hint ? |
||
| 50 | pmbaty | 356 | if (!is_hint_pending) |
| 1 | pmbaty | 357 | { |
| 50 | pmbaty | 358 | the_scene.gui.central_text.disappear_time = current_time + 1.0f; // fade the "thinking" phrase out now (FIXME: ugly) |
| 359 | the_scene.gui.want_spinwheel = false; // stop spinning wheel |
||
| 360 | |||
| 1 | pmbaty | 361 | Board_AppendMove (&the_board, the_board.selected_position[0], the_board.selected_position[1], the_board.hovered_position[0], the_board.hovered_position[1], move.promotion_type, NULL); |
| 171 | pmbaty | 362 | Player_GetOpposite ()->should_wakeup = true; // do the movement and switch players |
| 1 | pmbaty | 363 | |
| 364 | // forget the hovered and selected positions |
||
| 365 | Board_SetSelectedAndHovered (&the_board, -1, -1, -1, -1); |
||
| 366 | animation_endtime = current_time + ANIMATION_DURATION; // wait for animation time seconds |
||
| 367 | } |
||
| 368 | |||
| 369 | // else it was a hint |
||
| 370 | else |
||
| 50 | pmbaty | 371 | { |
| 372 | the_scene.gui.central_text.disappear_time = current_time + 0.1f; // fade the "thinking" phrase out now quickly (FIXME: ugly) |
||
| 373 | the_scene.gui.want_spinwheel = false; // stop spinning wheel |
||
| 374 | |||
| 1 | pmbaty | 375 | highlight_endtime = current_time + 2.0f; // just highlight this part for a little more than one second |
| 50 | pmbaty | 376 | is_hint_pending = false; // remember no hint is pending any longer |
| 1 | pmbaty | 377 | |
| 50 | pmbaty | 378 | // we must now restore the board to its last state. Just set up the board again from its current Forsyth-Edwards notation |
| 379 | Debug_Log (L"===Hint received, rebuilding board and telling engine to backup 1 move===\n"); |
||
| 380 | Debug_Log (L"===setting up board using FEN string===\n"); |
||
| 381 | |||
| 154 | pmbaty | 382 | // get the current game state in FEN format and feed it to the engine |
| 119 | pmbaty | 383 | Player_SendBuffer_Add (player, 1000, program->command_setboard, the_board.moves[the_board.move_count - 1].fen_string); |
| 50 | pmbaty | 384 | Player_SendBuffer_Add (player, 1000, L"\n"); // since the format string was read from the options, don't forget to end it with a carriage return |
| 385 | |||
| 150 | pmbaty | 386 | if (_wcsnicmp (program->friendly_name, L"Crafty", 6) == 0) |
| 387 | Player_SendBuffer_Add (player, 1000, L"disp\n"); // FIXME: we no longer need this Crafty-specific hack, do we? |
||
| 50 | pmbaty | 388 | } |
| 389 | |||
| 1 | pmbaty | 390 | do_update = true; // remember to update the 3D scene |
| 391 | } |
||
| 392 | |||
| 393 | // now clean the input buffer of all the lines we parsed |
||
| 394 | line_pointer = wcsrchr (player->recvbuffer, L'\n'); // reach the last carriage return |
||
| 395 | if (line_pointer != NULL) |
||
| 396 | { |
||
| 397 | line_pointer++; // skip the carriage return |
||
| 398 | length = wcslen (line_pointer); // get the remaining string length |
||
| 399 | for (char_index = 0; char_index < length; char_index++) |
||
| 400 | player->recvbuffer[char_index] = line_pointer[char_index]; // and recopy the remaining string at the beginning of the buffer |
||
| 401 | player->recvbuffer[char_index] = 0; // finish the string ourselves |
||
| 402 | } |
||
| 403 | |||
| 404 | // END PARSING |
||
| 405 | ////////////// |
||
| 406 | |||
| 407 | ///////////////////////////////// |
||
| 408 | // START NOTIFICATIONS PROCESSING |
||
| 409 | |||
| 410 | // have we been notified that the board was just set up ? |
||
| 411 | if (the_board.was_setup) |
||
| 412 | { |
||
| 413 | Debug_Log (L"===Got board setup notification from interface===\n"); |
||
| 414 | |||
| 415 | // send a new game command to the chess engine |
||
| 119 | pmbaty | 416 | Player_SendBuffer_Add (player, 1000, program->command_new); |
| 39 | pmbaty | 417 | Player_SendBuffer_Add (player, 1000, L"\n"); // since the format string was read from the options, don't forget to end it with a carriage return |
| 1 | pmbaty | 418 | |
| 419 | // just set up the board from its Forsyth-Edwards notation |
||
| 420 | Debug_Log (L"===setting up board using FEN string===\n"); |
||
| 421 | |||
| 154 | pmbaty | 422 | // get the current game state in FEN format and feed it to the engine |
| 119 | pmbaty | 423 | Player_SendBuffer_Add (player, 1000, program->command_setboard, the_board.moves[the_board.move_count - 1].fen_string); |
| 39 | pmbaty | 424 | Player_SendBuffer_Add (player, 1000, L"\n"); // since the format string was read from the options, don't forget to end it with a carriage return |
| 1 | pmbaty | 425 | |
| 50 | pmbaty | 426 | current_obstinacy = 0; // reset current obstinacy |
| 427 | is_hint_pending = false; // no hint was requested so far |
||
| 1 | pmbaty | 428 | |
| 150 | pmbaty | 429 | if (_wcsnicmp (program->friendly_name, L"Crafty", 6) == 0) |
| 430 | Player_SendBuffer_Add (player, 1000, L"disp\n"); // FIXME: we no longer need this Crafty-specific hack, do we? |
||
| 1 | pmbaty | 431 | } |
| 432 | |||
| 433 | // have we been notified that players are swapping colors ? (N.B. when this happens in human vs. computer mode, it's always that the human player is *GIVING* his turn) |
||
| 434 | if (the_board.want_playerswap) |
||
| 435 | { |
||
| 436 | Debug_Log (L"===Got player SWAP notification from interface===\n"); |
||
| 119 | pmbaty | 437 | Player_SendBuffer_Add (player, 1000, program->command_go); // tell engine it's now the current player |
| 39 | pmbaty | 438 | Player_SendBuffer_Add (player, 1000, L"\n"); // since the format string was read from the options, don't forget to end it with a carriage return |
| 1 | pmbaty | 439 | } |
| 440 | |||
| 171 | pmbaty | 441 | // have we been notified that the current player just changed AND is the animation finished ? |
| 442 | if (player->should_wakeup && (animation_endtime < current_time)) |
||
| 1 | pmbaty | 443 | { |
| 444 | Debug_Log (L"===Got player change notification from interface===\n"); |
||
| 445 | |||
| 446 | // is the current player our color ? (meaning is it engine's turn to play) ? |
||
| 447 | if (Board_ColorToMove (&the_board) == player->color) |
||
| 448 | { |
||
| 449 | // is it NOT a board setup AND has at least one move been played (meaning it was just the other's turn before) ? |
||
| 450 | if (!the_board.was_setup && (the_board.move_count > 1)) |
||
| 451 | { |
||
| 39 | pmbaty | 452 | Debug_Log (L"===Player just played, sending the chosen move to engine===\n"); |
| 150 | pmbaty | 453 | if (_wcsnicmp (program->friendly_name, L"Crafty", 6) == 0) |
| 454 | Player_SendBuffer_Add (player, 1000, L"disp\n"); // FIXME: we no longer need this Crafty-specific hack, do we? |
||
| 1 | pmbaty | 455 | |
| 154 | pmbaty | 456 | // build the move string, and send the move string to the engine |
| 119 | pmbaty | 457 | Player_SendBuffer_Add (player, 1000, program->command_move, Move_BuildString (&the_board.moves[the_board.move_count - 1])); |
| 39 | pmbaty | 458 | Player_SendBuffer_Add (player, 1000, L"\n"); // end the send buffer with a carriage return |
| 1 | pmbaty | 459 | } |
| 460 | |||
| 461 | // else game has not started yet, but it's our turn |
||
| 462 | else |
||
| 39 | pmbaty | 463 | { |
| 119 | pmbaty | 464 | Player_SendBuffer_Add (player, 1000, program->command_go); // so let's start the game |
| 39 | pmbaty | 465 | Player_SendBuffer_Add (player, 1000, L"\n"); // since the format string was read from the options, don't forget to end it with a carriage return |
| 466 | } |
||
| 1 | pmbaty | 467 | } |
| 171 | pmbaty | 468 | |
| 469 | player->should_wakeup = false; // remember we taken this notification in account |
||
| 1 | pmbaty | 470 | } |
| 471 | |||
| 472 | // END NOTIFICATIONS PROCESSING |
||
| 473 | /////////////////////////////// |
||
| 474 | |||
| 475 | // is it NOT our turn ? |
||
| 476 | if (current_player != player) |
||
| 477 | { |
||
| 478 | // does our opponent want a hint ? |
||
| 479 | if (current_player->wants_hint) |
||
| 480 | { |
||
| 481 | current_player->wants_hint = false; // don't ask twice |
||
| 39 | pmbaty | 482 | Debug_Log (L"===Hint requested, asking engine for it===\n"); |
| 119 | pmbaty | 483 | Player_SendBuffer_Add (player, 1000, program->command_go); // tell the computer to analyze this position |
| 39 | pmbaty | 484 | Player_SendBuffer_Add (player, 1000, L"\n"); // since the format string was read from the options, don't forget to end it with a carriage return |
| 50 | pmbaty | 485 | is_hint_pending = true; // remember a hint is pending |
| 486 | |||
| 487 | // FIXME: move to scene.cpp |
||
| 140 | pmbaty | 488 | Scene_UpdateText (&the_scene.gui.central_text, RGBA_TO_RGBACOLOR (255, 255, 255, 191), DURATION_INFINITE, true, LOCALIZE (L"Thinking")); // display the "thinking" phrase in the middle of the screen |
| 50 | pmbaty | 489 | the_scene.gui.want_spinwheel = true; // start spinning wheel |
| 490 | do_update = true; // remember to update the 3D scene |
||
| 1 | pmbaty | 491 | } |
| 492 | |||
| 493 | // does our opponent want to cancel a move ? |
||
| 494 | if (current_player->wants_cancel) |
||
| 495 | { |
||
| 496 | current_player->wants_cancel = false; // don't ask twice (remember now before we switch players) |
||
| 39 | pmbaty | 497 | Debug_Log (L"===Move cancellation requested, rebuilding board and telling engine to backup 2 moves===\n"); |
| 1 | pmbaty | 498 | |
| 499 | // rewind game 2 moves back |
||
| 500 | the_board.moves = (boardmove_t *) SAFE_realloc (the_board.moves, the_board.move_count, max (1, the_board.move_count - 2), sizeof (boardmove_t), false); |
||
| 501 | the_board.move_count = max (1, the_board.move_count - 2); // figure out how many moves we have now |
||
| 502 | the_board.viewed_move = the_board.move_count - 1; // take us back to the last move |
||
| 136 | pmbaty | 503 | the_board.game_state = STATE_PLAYING; // remember the game is now playing (in case we wanted to cancel the closing move of a finished game, this opens the game again) |
| 1 | pmbaty | 504 | |
| 505 | // just set up the board from its Forsyth-Edwards notation |
||
| 506 | Debug_Log (L"===setting up board using FEN string===\n"); |
||
| 507 | |||
| 154 | pmbaty | 508 | // get the current game state in FEN format and feed it to the engine |
| 119 | pmbaty | 509 | Player_SendBuffer_Add (player, 1000, program->command_setboard, the_board.moves[the_board.move_count - 1].fen_string); |
| 39 | pmbaty | 510 | Player_SendBuffer_Add (player, 1000, L"\n"); // since the format string was read from the options, don't forget to end it with a carriage return |
| 1 | pmbaty | 511 | |
| 150 | pmbaty | 512 | if (_wcsnicmp (program->friendly_name, L"Crafty", 6) == 0) |
| 513 | Player_SendBuffer_Add (player, 1000, L"disp\n"); // FIXME: we no longer need this Crafty-specific hack, do we? |
||
| 1 | pmbaty | 514 | |
| 515 | do_update = true; // remember to update the 3D scene |
||
| 516 | } |
||
| 517 | } |
||
| 518 | |||
| 519 | // write to pipe (when we're allowed to) |
||
| 520 | if (!player->sendbuffer_locked && (animation_endtime + 1.0f < current_time)) |
||
| 521 | PlayerEngine_Send (player); |
||
| 522 | |||
| 523 | return (do_update); // finished, return whether we should update the scene or not |
||
| 524 | } |
||
| 525 | |||
| 526 | |||
| 527 | static void PlayerEngine_Recv (player_t *player) |
||
| 528 | { |
||
| 529 | // helper function to read data from the pipe communicating with the game engine process |
||
| 530 | |||
| 149 | pmbaty | 531 | //unsigned long amount_to_read; |
| 1 | pmbaty | 532 | unsigned long read_count; |
| 533 | int length; |
||
| 534 | unsigned int initial_pos; |
||
| 535 | |||
| 536 | // get reception buffer's initial end position |
||
| 537 | initial_pos = wcslen (player->recvbuffer); |
||
| 149 | pmbaty | 538 | length = initial_pos; |
| 1 | pmbaty | 539 | |
| 149 | pmbaty | 540 | // as long as the pipe reports us there is data to read, read one byte at a time |
| 150 | pmbaty | 541 | while (pipe_hasdata (fpipe)) |
| 1 | pmbaty | 542 | { |
| 150 | pmbaty | 543 | read_count = pipe_read (fpipe, player->ascii_recvbuffer, 1); |
| 149 | pmbaty | 544 | if (read_count == 0) |
| 1 | pmbaty | 545 | break; // read the pipe; if it fails, stop trying |
| 149 | pmbaty | 546 | if ((player->ascii_recvbuffer[0] == '\r') || (player->ascii_recvbuffer[0] == '%')) |
| 547 | continue; // ignore carriage returns and percent signs |
||
| 1 | pmbaty | 548 | |
| 149 | pmbaty | 549 | // convert the received character to wide char and append it to recvbuffer |
| 550 | player->ascii_recvbuffer[1] = 0; // terminate the received string ourselves |
||
| 171 | pmbaty | 551 | player->recvbuffer[length] = (wchar_t) player->ascii_recvbuffer[0]; |
| 552 | //ConvertToWideChar (&player->recvbuffer[length], player->recvbuffer_size - length, player->ascii_recvbuffer); |
||
| 149 | pmbaty | 553 | length++; // there's one more character in the player's recv buffer |
| 171 | pmbaty | 554 | player->recvbuffer[length] = 0; // terminate the string ourselves |
| 1 | pmbaty | 555 | } |
| 556 | |||
| 557 | // write what we received (if we received anything) |
||
| 558 | if (wcslen (player->recvbuffer) > initial_pos) |
||
| 559 | Debug_Log (L"===================================RECEIVED:===================================\n%s\n", &player->recvbuffer[initial_pos]); |
||
| 560 | |||
| 561 | return; // finished |
||
| 562 | } |
||
| 563 | |||
| 564 | |||
| 154 | pmbaty | 565 | int wcs_replace (wchar_t *haystack, wchar_t *needle, wchar_t *replacement) |
| 566 | { |
||
| 567 | size_t replacement_length; |
||
| 568 | size_t replacement_count; |
||
| 569 | size_t pattern_length; |
||
| 570 | wchar_t *line_pointer; |
||
| 571 | |||
| 572 | replacement_count = 0; |
||
| 573 | while ((line_pointer = wcsstr (haystack, needle)) != NULL) |
||
| 574 | { |
||
| 575 | pattern_length = wcslen (needle); |
||
| 576 | replacement_length = wcslen (replacement); |
||
| 577 | memmove (&line_pointer[replacement_length], &line_pointer[pattern_length], (wcslen (&line_pointer[pattern_length]) + 1) * sizeof (wchar_t)); |
||
| 578 | memcpy (line_pointer, replacement, replacement_length * sizeof (wchar_t)); |
||
| 579 | replacement_count++; |
||
| 580 | } |
||
| 581 | |||
| 582 | return (replacement_count); |
||
| 583 | } |
||
| 584 | |||
| 585 | |||
| 1 | pmbaty | 586 | static void PlayerEngine_Send (player_t *player) |
| 587 | { |
||
| 588 | // helper function to send data through the pipe communicating with the game engine process |
||
| 589 | |||
| 154 | pmbaty | 590 | static wchar_t replacement_string[65536]; |
| 159 | pmbaty | 591 | static wchar_t widechar_buffer[65536]; // needs to be big enough to hold game history |
| 592 | static char ascii_buffer[65536]; // needs to be big enough to hold game history |
||
| 1 | pmbaty | 593 | wchar_t *line_pointer; |
| 594 | unsigned long amount_written; |
||
| 154 | pmbaty | 595 | int move_index; |
| 596 | int try_index; |
||
| 1 | pmbaty | 597 | |
| 598 | player->sendbuffer_locked = true; // lock the buffer |
||
| 599 | |||
| 154 | pmbaty | 600 | // perform variable replacements on sendbuffer |
| 601 | if (the_board.move_count > 0) |
||
| 602 | { |
||
| 603 | wcs_replace (player->sendbuffer, L"${CURRENT_POS}", (/*the_board.move_count == 2 ? L"startpos" : */the_board.moves[the_board.move_count - 1].fen_string)); |
||
| 604 | wcs_replace (player->sendbuffer, L"${LAST_MOVE}", Move_BuildString (&the_board.moves[the_board.move_count - 1])); |
||
| 605 | while (wcsstr (player->sendbuffer, L"${MAX_CORES}") != NULL) |
||
| 606 | { |
||
| 607 | swprintf_s (replacement_string, WCHAR_SIZEOF (replacement_string), L"%d", max_cores); |
||
| 608 | wcs_replace (player->sendbuffer, L"${MAX_CORES}", replacement_string); |
||
| 609 | } |
||
| 610 | while (wcsstr (player->sendbuffer, L"${SEARCH_DEPTH}") != NULL) |
||
| 611 | { |
||
| 612 | swprintf_s (replacement_string, WCHAR_SIZEOF (replacement_string), L"%d", options.engine.depth); |
||
| 613 | wcs_replace (player->sendbuffer, L"${SEARCH_DEPTH}", replacement_string); |
||
| 614 | } |
||
| 615 | while (wcsstr (player->sendbuffer, L"${GAME_HISTORY}") != NULL) |
||
| 616 | { |
||
| 617 | replacement_string[0] = 0; |
||
| 618 | for (move_index = 1; move_index < the_board.move_count; move_index++) |
||
| 619 | { |
||
| 620 | if (move_index > 1) |
||
| 621 | wcscat_s (replacement_string, WCHAR_SIZEOF (replacement_string), L" "); |
||
| 622 | wcscat_s (replacement_string, WCHAR_SIZEOF (replacement_string), Move_BuildString (&the_board.moves[move_index])); |
||
| 623 | } |
||
| 624 | wcs_replace (player->sendbuffer, L"${GAME_HISTORY}", replacement_string); |
||
| 625 | } |
||
| 626 | } |
||
| 627 | |||
| 1 | pmbaty | 628 | // write what we're sending (if we're sending anything) |
| 629 | if (player->sendbuffer[0] != 0) |
||
| 630 | Debug_Log (L"====================================SENDING:===================================\n%s\n", player->sendbuffer); |
||
| 631 | |||
| 632 | // now read line per line... |
||
| 633 | line_pointer = player->sendbuffer; // start at the first character |
||
| 634 | while ((line_pointer = wcsgets (widechar_buffer, WCHAR_SIZEOF (widechar_buffer), line_pointer)) != NULL) |
||
| 635 | { |
||
| 636 | wcscat_s (widechar_buffer, WCHAR_SIZEOF (widechar_buffer), L"\n"); // put the carriage return back |
||
| 637 | ConvertTo7BitASCII (ascii_buffer, sizeof (ascii_buffer), widechar_buffer); // convert to ASCII |
||
| 150 | pmbaty | 638 | amount_written = pipe_write (fpipe, ascii_buffer, strlen (ascii_buffer)); // send data |
| 1 | pmbaty | 639 | } |
| 640 | |||
| 641 | player->sendbuffer[0] = 0; // what we had to send has been sent, reset the send buffer |
||
| 642 | player->sendbuffer_locked = false; // and unlock it |
||
| 643 | |||
| 154 | pmbaty | 644 | // should the engine be initialized ? |
| 645 | if (should_initialize) |
||
| 646 | { |
||
| 647 | // wait for the engine process to display something (which will mean it's ready). Try for 5 seconds. |
||
| 648 | for (try_index = 0; try_index < 50; try_index++) |
||
| 649 | { |
||
| 650 | // read from pipe (non-blocking) |
||
| 651 | PlayerEngine_Recv (player); |
||
| 652 | if (player->recvbuffer[0] != 0) |
||
| 653 | break; // break as soon as we get something |
||
| 654 | Sleep (100); // next try in 100 milliseconds |
||
| 655 | } |
||
| 656 | |||
| 657 | // has the engine process not spoken yet ? |
||
| 658 | if (player->recvbuffer[0] == 0) |
||
| 659 | { |
||
| 660 | messagebox.hWndParent = hMainWnd; |
||
| 661 | wcscpy_s (messagebox.title, WCHAR_SIZEOF (messagebox.title), LOCALIZE (L"ImportantMessage")); |
||
| 662 | swprintf_s (messagebox.text, WCHAR_SIZEOF (messagebox.text), LOCALIZE (L"Error_ChessEngineInitializationFailed"), options.engine.programs[options.engine.selected_program].folder); |
||
| 663 | messagebox.flags = MB_ICONWARNING | MB_OK; |
||
| 664 | DialogBox_Message (&messagebox); // display a modeless error message box |
||
| 665 | |||
| 666 | PlayerEngine_Shutdown (player); // on error, shutdown the engine |
||
| 667 | } |
||
| 668 | |||
| 669 | should_initialize = false; // remember this is no longer to be done (until the next call to PlayerEngine_Init()) |
||
| 670 | } |
||
| 671 | |||
| 1 | pmbaty | 672 | return; // finished |
| 673 | } |
||
| 674 | |||
| 675 | |||
| 12 | pmbaty | 676 | static wchar_t *Move_BuildString (boardmove_t *move) |
| 1 | pmbaty | 677 | { |
| 12 | pmbaty | 678 | // helper function to build a move string to send to the engine from a particular board move. NOT THREAD SAFE. |
| 1 | pmbaty | 679 | |
| 12 | pmbaty | 680 | static wchar_t output_string[8]; |
| 681 | |||
| 1 | pmbaty | 682 | // construct the four first characters |
| 12 | pmbaty | 683 | swprintf_s (output_string, WCHAR_SIZEOF (output_string), L"%c%c%c%c", |
| 1 | pmbaty | 684 | L'a' + move->source[1], L'1' + move->source[0], |
| 685 | L'a' + move->target[1], L'1' + move->target[0]); |
||
| 686 | |||
| 687 | // append any eventual promotion |
||
| 688 | if (move->promotion_type == PART_ROOK) |
||
| 12 | pmbaty | 689 | wcscat_s (output_string, WCHAR_SIZEOF (output_string), L"r"); |
| 1 | pmbaty | 690 | else if (move->promotion_type == PART_KNIGHT) |
| 12 | pmbaty | 691 | wcscat_s (output_string, WCHAR_SIZEOF (output_string), L"n"); |
| 1 | pmbaty | 692 | else if (move->promotion_type == PART_BISHOP) |
| 12 | pmbaty | 693 | wcscat_s (output_string, WCHAR_SIZEOF (output_string), L"b"); |
| 1 | pmbaty | 694 | else if (move->promotion_type == PART_QUEEN) |
| 12 | pmbaty | 695 | wcscat_s (output_string, WCHAR_SIZEOF (output_string), L"q"); |
| 1 | pmbaty | 696 | |
| 12 | pmbaty | 697 | return (output_string); // finished, return the move string |
| 1 | pmbaty | 698 | } |