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