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