Rev 153 | Rev 159 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 153 | Rev 154 | ||
---|---|---|---|
Line 6... | Line 6... | ||
6 | // global variables used in this module only |
6 | // global variables used in this module only |
7 | static wchar_t chessengine_path[MAX_PATH]; |
7 | static wchar_t chessengine_path[MAX_PATH]; |
8 | static wchar_t chessengine_shellcommand[MAX_PATH]; |
8 | static wchar_t chessengine_shellcommand[MAX_PATH]; |
9 | static wchar_t chessengineinitfile_pathname[MAX_PATH]; |
9 | static wchar_t chessengineinitfile_pathname[MAX_PATH]; |
10 | static int current_obstinacy; |
10 | static int current_obstinacy; |
- | 11 | static int max_cores; |
|
11 | static bool is_hint_pending; |
12 | static bool is_hint_pending; |
- | 13 | static bool should_initialize; |
|
12 | FILE *fpipe; |
14 | FILE *fpipe; |
13 | 15 | ||
14 | // prototypes of local functions |
16 | // prototypes of local functions |
15 | static void PlayerEngine_Recv (player_t *player); |
17 | static void PlayerEngine_Recv (player_t *player); |
16 | static void PlayerEngine_Send (player_t *player); |
18 | static void PlayerEngine_Send (player_t *player); |
17 | static wchar_t *Move_BuildString (boardmove_t *move); |
19 | static wchar_t *Move_BuildString (boardmove_t *move); |
18 | static bool SendEngineHistoryToAuthor (void); |
- | |
19 | 20 | ||
20 | 21 | ||
21 | bool PlayerEngine_Init (player_t *player) |
22 | bool PlayerEngine_Init (player_t *player) |
22 | { |
23 | { |
23 | // this function starts a chess engine as a child process. This process's stdin and |
24 | // this function starts a chess engine as a child process. This process's stdin and |
Line 25... | Line 26... | ||
25 | 26 | ||
26 | engineprogram_t *program; |
27 | engineprogram_t *program; |
27 | wchar_t current_path[MAX_PATH]; |
28 | wchar_t current_path[MAX_PATH]; |
28 | wchar_t widechar_buffer[256]; |
29 | wchar_t widechar_buffer[256]; |
29 | SYSTEM_INFO sysinfo; |
30 | SYSTEM_INFO sysinfo; |
30 | int try_index; |
- | |
31 | FILE *fp; |
31 | FILE *fp; |
32 | 32 | ||
33 | // reset stuff first |
33 | // reset stuff first |
34 | player->wants_hint = false; |
34 | player->wants_hint = false; |
35 | 35 | ||
Line 55... | Line 55... | ||
55 | 55 | ||
56 | PlayerEngine_Shutdown (player); // on error, shutdown the engine |
56 | PlayerEngine_Shutdown (player); // on error, shutdown the engine |
57 | return (false); |
57 | return (false); |
58 | } |
58 | } |
59 | 59 | ||
60 | // |
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 | for (try_index = 0; try_index < 50; try_index++) |
- | |
62 | { |
- | |
63 | // read from pipe (non-blocking) |
- | |
64 | PlayerEngine_Recv (player); |
- | |
65 | if (player->recvbuffer[0] != 0) |
- | |
66 | break; // break as soon as we get something |
- | |
67 |
|
61 | GetSystemInfo (&sysinfo); // get the number of cores |
68 | } |
- | |
69 | - | ||
70 |
|
62 | max_cores = max (1, sysinfo.dwNumberOfProcessors - 1); |
71 | if (player->recvbuffer[0] == 0) |
- | |
72 | { |
- | |
73 | messagebox.hWndParent = hMainWnd; |
- | |
74 | wcscpy_s (messagebox.title, WCHAR_SIZEOF (messagebox.title), LOCALIZE (L"ImportantMessage")); |
- | |
75 | swprintf_s (messagebox.text, WCHAR_SIZEOF (messagebox.text), LOCALIZE (L"Error_ChessEngineInitializationFailed"), program->folder); |
- | |
76 | messagebox.flags = MB_ICONWARNING | MB_OK; |
- | |
77 | DialogBox_Message (&messagebox); // display a modeless error message box |
- | |
78 | - | ||
79 | PlayerEngine_Shutdown (player); // on error, shutdown the engine |
- | |
80 | return (false); |
- | |
81 | } |
- | |
82 | 63 | ||
83 | // optionally initialize the debug log file |
64 | // optionally initialize the debug log file |
84 | Debug_Init (L"Chess engine output.txt"); |
65 | Debug_Init (L"Chess engine output.txt"); |
85 | Debug_Log (L"===Using engine: %s [%s]===\n", program->friendly_name, chessengine_shellcommand); |
66 | Debug_Log (L"===Using engine: %s [%s]===\n", program->friendly_name, chessengine_shellcommand); |
86 | 67 | ||
Line 90... | Line 71... | ||
90 | 71 | ||
91 | // could the init file be opened ? |
72 | // could the init file be opened ? |
92 | if (fp != NULL) |
73 | if (fp != NULL) |
93 | { |
74 | { |
94 | Debug_Log (L"===Found initialization file, parsing...===\n", chessengineinitfile_pathname); |
75 | Debug_Log (L"===Found initialization file, parsing...===\n", chessengineinitfile_pathname); |
95 | - | ||
96 | // SMP HACK -- assume our engine is CECP compatible and set the max CPUs to use to core max - 1 |
- | |
97 | // (the computer will look like hung if all CPU is taken) |
- | |
98 | GetSystemInfo (&sysinfo); // get the number of cores and build the corresponding engine initialization order |
- | |
99 | Player_SendBuffer_Add (player, 1000, L"mt %d\n", max (1, sysinfo.dwNumberOfProcessors - 1)); |
- | |
100 | 76 | ||
101 | // read line per line |
77 | // read line per line |
102 | while (fgetws (widechar_buffer, WCHAR_SIZEOF (widechar_buffer), fp) != NULL) |
78 | while (fgetws (widechar_buffer, WCHAR_SIZEOF (widechar_buffer), fp) != NULL) |
103 | { |
79 | { |
104 | if ((widechar_buffer[0] == L'#') || (widechar_buffer[0] == L'\n')) |
80 | if ((widechar_buffer[0] == L'#') || (widechar_buffer[0] == L'\n')) |
Line 112... | Line 88... | ||
112 | } |
88 | } |
113 | 89 | ||
114 | // everything's okay, set engine name as the player's name |
90 | // everything's okay, set engine name as the player's name |
115 | wcscpy_s (player->name, WCHAR_SIZEOF (player->name), program->friendly_name); |
91 | wcscpy_s (player->name, WCHAR_SIZEOF (player->name), program->friendly_name); |
116 | 92 | ||
- | 93 | should_initialize = true; // remember the engine should be initialized |
|
117 | return (true); // finished |
94 | return (true); // finished |
118 | } |
95 | } |
119 | 96 | ||
120 | 97 | ||
121 | void PlayerEngine_Shutdown (player_t *player) |
98 | void PlayerEngine_Shutdown (player_t *player) |
Line 179... | Line 156... | ||
179 | // if opponent player is human, play the victory sound, else, play defeat sound at the center of the board |
156 | // if opponent player is human, play the victory sound, else, play defeat sound at the center of the board |
180 | Audio_PlaySound (opposite_player->type == PLAYER_HUMAN ? SOUNDTYPE_VICTORY : SOUNDTYPE_DEFEAT, 0.0f, 0.0f, 0.04f); |
157 | Audio_PlaySound (opposite_player->type == PLAYER_HUMAN ? SOUNDTYPE_VICTORY : SOUNDTYPE_DEFEAT, 0.0f, 0.0f, 0.04f); |
181 | 158 | ||
182 | // display a crash notification dialog box |
159 | // display a crash notification dialog box |
183 | if (MessageBox (hMainWnd, LOCALIZE (L"Error_EngineCrashed"), LOCALIZE (L"ImportantMessage"), MB_ICONWARNING | MB_YESNO) == IDYES) |
160 | if (MessageBox (hMainWnd, LOCALIZE (L"Error_EngineCrashed"), LOCALIZE (L"ImportantMessage"), MB_ICONWARNING | MB_YESNO) == IDYES) |
184 | if ( |
161 | if (Debug_SendLogToAuthor ()) |
- | 162 | MessageBox (hMainWnd, LOCALIZE (L"MessageSent"), PROGRAM_NAME, MB_ICONINFORMATION | MB_OK); // send the game engine history to the author |
|
- | 163 | else |
|
185 | MessageBox (hMainWnd, LOCALIZE (L"NoInternetConnection"), LOCALIZE (L"FatalError"), MB_ICONWARNING | MB_OK); // |
164 | MessageBox (hMainWnd, LOCALIZE (L"NoInternetConnection"), LOCALIZE (L"FatalError"), MB_ICONWARNING | MB_OK); // and display an error message if failed |
186 | 165 | ||
187 | // and display the game over dialog box |
166 | // and display the game over dialog box |
188 | the_board.game_state = (opposite_player->color == COLOR_BLACK ? STATE_WHITEWIN_RESIGNORFORFEIT : STATE_BLACKWIN_RESIGNORFORFEIT); |
167 | the_board.game_state = (opposite_player->color == COLOR_BLACK ? STATE_WHITEWIN_RESIGNORFORFEIT : STATE_BLACKWIN_RESIGNORFORFEIT); |
189 | DialogBox_EndGame (); |
168 | DialogBox_EndGame (); |
190 | 169 | ||
Line 290... | Line 269... | ||
290 | } |
269 | } |
291 | 270 | ||
292 | // has the game ended already ? |
271 | // has the game ended already ? |
293 | if (the_board.game_state > STATE_PLAYING) |
272 | if (the_board.game_state > STATE_PLAYING) |
294 | continue; // ignore all that the engine tells us. Game is over already. |
273 | continue; // ignore all that the engine tells us. Game is over already. |
295 | - | ||
296 | // else is it a normal move AND not a polyglot error ? |
- | |
297 | else if (((move_string = wcsstr (line_buffer, program->replystring_move)) != NULL) && (wcsstr (line_buffer, L"illegal") == NULL)) |
- | |
298 | move_string += wcslen (program->replystring_move); // go to the parsable data |
- | |
299 | 274 | ||
300 | // else is it an unrecoverable engine error ? |
275 | // else is it an unrecoverable engine error ? |
301 | else if (wcsstr (line_buffer, L"illegal") != NULL) |
276 | else if ((wcsstr (line_buffer, L"illegal") != NULL) || (wcsstr (line_buffer, L"invalid") != NULL)) |
302 | { |
277 | { |
303 | Debug_Log (L"===Unrecoverable engine error: opponent wins!===\n"); |
278 | Debug_Log (L"===Unrecoverable engine error: opponent wins!===\n"); |
304 | 279 | ||
305 | // if opponent player is human, play the victory sound, else, play defeat sound at the center of the board |
280 | // if opponent player is human, play the victory sound, else, play defeat sound at the center of the board |
306 | Audio_PlaySound (opposite_player->type == PLAYER_HUMAN ? SOUNDTYPE_VICTORY : SOUNDTYPE_DEFEAT, 0.0f, 0.0f, 0.04f); |
281 | Audio_PlaySound (opposite_player->type == PLAYER_HUMAN ? SOUNDTYPE_VICTORY : SOUNDTYPE_DEFEAT, 0.0f, 0.0f, 0.04f); |
307 | 282 | ||
308 | // display a crash notification dialog box |
283 | // display a crash notification dialog box |
309 | if (MessageBox (hMainWnd, LOCALIZE (L"Error_EngineCrashed"), LOCALIZE (L"ImportantMessage"), MB_ICONWARNING | MB_YESNO) == IDYES) |
284 | if (MessageBox (hMainWnd, LOCALIZE (L"Error_EngineCrashed"), LOCALIZE (L"ImportantMessage"), MB_ICONWARNING | MB_YESNO) == IDYES) |
310 | if ( |
285 | if (Debug_SendLogToAuthor ()) |
- | 286 | MessageBox (hMainWnd, LOCALIZE (L"MessageSent"), PROGRAM_NAME, MB_ICONINFORMATION | MB_OK); // send the game engine history to the author |
|
- | 287 | else |
|
311 | MessageBox (hMainWnd, LOCALIZE (L"NoInternetConnection"), LOCALIZE (L"FatalError"), MB_ICONWARNING | MB_OK); // |
288 | MessageBox (hMainWnd, LOCALIZE (L"NoInternetConnection"), LOCALIZE (L"FatalError"), MB_ICONWARNING | MB_OK); // and display an error message if failed |
312 | 289 | ||
313 | // and display the game over dialog box |
290 | // and display the game over dialog box |
314 | the_board.game_state = (opposite_player->color == COLOR_BLACK ? STATE_WHITEWIN_RESIGNORFORFEIT : STATE_BLACKWIN_RESIGNORFORFEIT); |
291 | the_board.game_state = (opposite_player->color == COLOR_BLACK ? STATE_WHITEWIN_RESIGNORFORFEIT : STATE_BLACKWIN_RESIGNORFORFEIT); |
315 | DialogBox_EndGame (); |
292 | DialogBox_EndGame (); |
316 | 293 | ||
317 | do_update = true; // remember to update the 3D scene |
294 | do_update = true; // remember to update the 3D scene |
318 | } |
295 | } |
- | 296 | ||
- | 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 |
|
319 | 300 | ||
320 | // else it's any other sort of line |
301 | // else it's any other sort of line |
321 | else |
302 | else |
322 | continue; // skip lines that don't contain any valid move data |
303 | continue; // skip lines that don't contain any valid move data |
323 | 304 | ||
324 | // now we are sure it's either a hint or a move (or some pondering) |
305 | // now we are sure it's either a hint or a move (or some pondering) |
325 | 306 | ||
326 | length = wcslen (move_string); |
307 | length = wcslen (move_string); |
327 | if |
308 | if (length < 2) |
328 | continue; // if string is too |
309 | continue; // if string is too short to be a move, skip it |
329 | 310 | ||
330 | // there must be valid move data on that line. |
311 | // there must be valid move data on that line. |
331 | 312 | ||
332 | // evaluate the engine move string |
313 | // evaluate the engine move string |
333 | memcpy (move.slots, the_board.moves[the_board.move_count - 1].slots, sizeof (move.slots)); |
314 | memcpy (move.slots, the_board.moves[the_board.move_count - 1].slots, sizeof (move.slots)); |
- | 315 | char_index = 0; |
|
- | 316 | while ((move_string[char_index] != 0) && !iswspace (move_string[char_index])) |
|
- | 317 | { |
|
334 |
|
318 | move.pgntext[char_index] = move_string[char_index]; |
- | 319 | char_index++; |
|
- | 320 | } |
|
- | 321 | move.pgntext[char_index] = 0; |
|
335 | if (!Move_SetupFromSAN (&the_board.moves[the_board.move_count - 1], &move, Board_ColorToMove (&the_board))) |
322 | if (!Move_SetupFromSAN (&the_board.moves[the_board.move_count - 1], &move, Board_ColorToMove (&the_board))) |
336 | { |
323 | { |
337 | Debug_Log (L"===Skipping line (invalid move syntax)===\n%s", move_string); |
324 | Debug_Log (L"===Skipping line (invalid move syntax)===\n%s", move_string); |
338 | 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 |
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 |
339 | } |
326 | } |
340 | 327 | ||
341 | // is it NOT a hint, are blunders allowed, should we do one now AND can we do one now ? |
328 | // is it NOT a hint, are blunders allowed, should we do one now AND can we do one now ? |
342 | if (!is_hint_pending && (options.engine.blunder_chances > 0) && (rand () % 100 < options.engine.blunder_chances) |
329 | if (!is_hint_pending && (options.engine.blunder_chances > 0) && (rand () % 100 < options.engine.blunder_chances) |
343 | && Move_FindRandomMove (&the_board.moves[the_board.move_count - 1], player->color, &move)) |
330 | && Move_FindRandomMove (&the_board.moves[the_board.move_count - 1], player->color, &move)) |
344 | { |
331 | { |
- | 332 | if (program->command_force[0] != 0) |
|
- | 333 | { |
|
345 | Player_SendBuffer_Add (player, 1000, program->command_force, Move_BuildString (&move)); // send the blunderous move to the engine |
334 | Player_SendBuffer_Add (player, 1000, program->command_force, Move_BuildString (&move)); // send the blunderous move to the engine |
346 | 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 |
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 |
347 | if (_wcsnicmp (program->friendly_name, L"Crafty", 6) == 0) |
336 | if (_wcsnicmp (program->friendly_name, L"Crafty", 6) == 0) |
348 | Player_SendBuffer_Add (player, 1000, L"disp\n"); // FIXME: we no longer need this Crafty-specific hack, do we? |
337 | Player_SendBuffer_Add (player, 1000, L"disp\n"); // FIXME: we no longer need this Crafty-specific hack, do we? |
- | 338 | } |
|
349 | Debug_Log (L"===Discarding engine move, forcing a blunderous move (%s) instead===\n", Move_BuildString (&move)); // blunder |
339 | Debug_Log (L"===Discarding engine move, forcing a blunderous move (%s) instead===\n", Move_BuildString (&move)); // blunder |
350 | } |
340 | } |
351 | 341 | ||
352 | // mark the engine's selected and hovered squares |
342 | // 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]); |
343 | Board_SetSelectedAndHovered (&the_board, move.source[0], move.source[1], move.target[0], move.target[1]); |
Line 377... | Line 367... | ||
377 | 367 | ||
378 | // we must now restore the board to its last state. Just set up the board again from its current Forsyth-Edwards notation |
368 | // 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"); |
369 | 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"); |
370 | Debug_Log (L"===setting up board using FEN string===\n"); |
381 | 371 | ||
382 | // instruct it about its allowed search depth BEFORE each table set (this ensures engine will be "ready" to handle the command) |
- | |
383 | // |
372 | // get the current game state in FEN format and feed it to the engine |
384 | Player_SendBuffer_Add (player, 1000, program->command_sd, options.engine.depth); |
- | |
385 | 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 |
- | |
386 | Player_SendBuffer_Add (player, 1000, program->command_setboard, the_board.moves[the_board.move_count - 1].fen_string); |
373 | Player_SendBuffer_Add (player, 1000, program->command_setboard, the_board.moves[the_board.move_count - 1].fen_string); |
387 | 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 |
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 |
388 | 375 | ||
389 | if (_wcsnicmp (program->friendly_name, L"Crafty", 6) == 0) |
376 | if (_wcsnicmp (program->friendly_name, L"Crafty", 6) == 0) |
390 | Player_SendBuffer_Add (player, 1000, L"disp\n"); // FIXME: we no longer need this Crafty-specific hack, do we? |
377 | Player_SendBuffer_Add (player, 1000, L"disp\n"); // FIXME: we no longer need this Crafty-specific hack, do we? |
Line 420... | Line 407... | ||
420 | 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 |
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 |
421 | 408 | ||
422 | // just set up the board from its Forsyth-Edwards notation |
409 | // just set up the board from its Forsyth-Edwards notation |
423 | Debug_Log (L"===setting up board using FEN string===\n"); |
410 | Debug_Log (L"===setting up board using FEN string===\n"); |
424 | 411 | ||
425 | // instruct it about its allowed search depth BEFORE each table set (this ensures engine will be "ready" to handle the command) |
- | |
426 | // |
412 | // get the current game state in FEN format and feed it to the engine |
427 | Player_SendBuffer_Add (player, 1000, program->command_sd, options.engine.depth); |
- | |
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 |
- | |
429 | Player_SendBuffer_Add (player, 1000, program->command_setboard, the_board.moves[the_board.move_count - 1].fen_string); |
413 | Player_SendBuffer_Add (player, 1000, program->command_setboard, the_board.moves[the_board.move_count - 1].fen_string); |
430 | 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 |
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 |
431 | 415 | ||
432 | current_obstinacy = 0; // reset current obstinacy |
416 | current_obstinacy = 0; // reset current obstinacy |
433 | is_hint_pending = false; // no hint was requested so far |
417 | is_hint_pending = false; // no hint was requested so far |
Line 457... | Line 441... | ||
457 | { |
441 | { |
458 | Debug_Log (L"===Player just played, sending the chosen move to engine===\n"); |
442 | Debug_Log (L"===Player just played, sending the chosen move to engine===\n"); |
459 | if (_wcsnicmp (program->friendly_name, L"Crafty", 6) == 0) |
443 | if (_wcsnicmp (program->friendly_name, L"Crafty", 6) == 0) |
460 | Player_SendBuffer_Add (player, 1000, L"disp\n"); // FIXME: we no longer need this Crafty-specific hack, do we? |
444 | Player_SendBuffer_Add (player, 1000, L"disp\n"); // FIXME: we no longer need this Crafty-specific hack, do we? |
461 | 445 | ||
462 | // instruct it about its allowed search depth BEFORE each move (this ensures engine will be "ready" to handle the command) |
- | |
463 | // |
446 | // build the move string, and send the move string to the engine |
464 | Player_SendBuffer_Add (player, 1000, program->command_sd, options.engine.depth); |
- | |
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 | Player_SendBuffer_Add (player, 1000, program->command_move, Move_BuildString (&the_board.moves[the_board.move_count - 1])); |
447 | Player_SendBuffer_Add (player, 1000, program->command_move, Move_BuildString (&the_board.moves[the_board.move_count - 1])); |
467 | Player_SendBuffer_Add (player, 1000, L"\n"); // end the send buffer with a carriage return |
448 | Player_SendBuffer_Add (player, 1000, L"\n"); // end the send buffer with a carriage return |
468 | } |
449 | } |
469 | 450 | ||
470 | // else game has not started yet, but it's our turn |
451 | // else game has not started yet, but it's our turn |
Line 510... | Line 491... | ||
510 | 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) |
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) |
511 | 492 | ||
512 | // just set up the board from its Forsyth-Edwards notation |
493 | // just set up the board from its Forsyth-Edwards notation |
513 | Debug_Log (L"===setting up board using FEN string===\n"); |
494 | Debug_Log (L"===setting up board using FEN string===\n"); |
514 | 495 | ||
515 | // instruct it about its allowed search depth BEFORE each table set (this ensures engine will be "ready" to handle the command) |
- | |
516 | // |
496 | // get the current game state in FEN format and feed it to the engine |
517 | Player_SendBuffer_Add (player, 1000, program->command_sd, options.engine.depth); |
- | |
518 | 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 |
- | |
519 | Player_SendBuffer_Add (player, 1000, program->command_setboard, the_board.moves[the_board.move_count - 1].fen_string); |
497 | Player_SendBuffer_Add (player, 1000, program->command_setboard, the_board.moves[the_board.move_count - 1].fen_string); |
520 | 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 |
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 |
521 | 499 | ||
522 | if (_wcsnicmp (program->friendly_name, L"Crafty", 6) == 0) |
500 | if (_wcsnicmp (program->friendly_name, L"Crafty", 6) == 0) |
523 | Player_SendBuffer_Add (player, 1000, L"disp\n"); // FIXME: we no longer need this Crafty-specific hack, do we? |
501 | Player_SendBuffer_Add (player, 1000, L"disp\n"); // FIXME: we no longer need this Crafty-specific hack, do we? |
Line 565... | Line 543... | ||
565 | // write what we received (if we received anything) |
543 | // write what we received (if we received anything) |
566 | if (wcslen (player->recvbuffer) > initial_pos) |
544 | if (wcslen (player->recvbuffer) > initial_pos) |
567 | Debug_Log (L"===================================RECEIVED:===================================\n%s\n", &player->recvbuffer[initial_pos]); |
545 | Debug_Log (L"===================================RECEIVED:===================================\n%s\n", &player->recvbuffer[initial_pos]); |
568 | 546 | ||
569 | return; // finished |
547 | return; // finished |
- | 548 | } |
|
- | 549 | ||
- | 550 | ||
- | 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); |
|
570 | } |
569 | } |
571 | 570 | ||
572 | 571 | ||
573 | static void PlayerEngine_Send (player_t *player) |
572 | static void PlayerEngine_Send (player_t *player) |
574 | { |
573 | { |
575 | // helper function to send data through the pipe communicating with the game engine process |
574 | // helper function to send data through the pipe communicating with the game engine process |
576 | 575 | ||
- | 576 | static wchar_t replacement_string[65536]; |
|
577 | wchar_t widechar_buffer[256]; |
577 | wchar_t widechar_buffer[256]; |
578 | wchar_t *line_pointer; |
578 | wchar_t *line_pointer; |
579 | char ascii_buffer[256]; |
579 | char ascii_buffer[256]; |
580 | unsigned long amount_written; |
580 | unsigned long amount_written; |
- | 581 | int move_index; |
|
- | 582 | int try_index; |
|
581 | 583 | ||
582 | player->sendbuffer_locked = true; // lock the buffer |
584 | player->sendbuffer_locked = true; // lock the buffer |
- | 585 | ||
- | 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 | } |
|
583 | 613 | ||
584 | // write what we're sending (if we're sending anything) |
614 | // write what we're sending (if we're sending anything) |
585 | if (player->sendbuffer[0] != 0) |
615 | if (player->sendbuffer[0] != 0) |
586 | Debug_Log (L"====================================SENDING:===================================\n%s\n", player->sendbuffer); |
616 | Debug_Log (L"====================================SENDING:===================================\n%s\n", player->sendbuffer); |
587 | 617 | ||
Line 594... | Line 624... | ||
594 | amount_written = pipe_write (fpipe, ascii_buffer, strlen (ascii_buffer)); // send data |
624 | amount_written = pipe_write (fpipe, ascii_buffer, strlen (ascii_buffer)); // send data |
595 | } |
625 | } |
596 | 626 | ||
597 | player->sendbuffer[0] = 0; // what we had to send has been sent, reset the send buffer |
627 | player->sendbuffer[0] = 0; // what we had to send has been sent, reset the send buffer |
598 | player->sendbuffer_locked = false; // and unlock it |
628 | player->sendbuffer_locked = false; // and unlock it |
- | 629 | ||
- | 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 | } |
|
599 | 657 | ||
600 | return; // finished |
658 | return; // finished |
601 | } |
659 | } |
602 | 660 | ||
603 | 661 | ||
Line 621... | Line 679... | ||
621 | wcscat_s (output_string, WCHAR_SIZEOF (output_string), L"b"); |
679 | wcscat_s (output_string, WCHAR_SIZEOF (output_string), L"b"); |
622 | else if (move->promotion_type == PART_QUEEN) |
680 | else if (move->promotion_type == PART_QUEEN) |
623 | wcscat_s (output_string, WCHAR_SIZEOF (output_string), L"q"); |
681 | wcscat_s (output_string, WCHAR_SIZEOF (output_string), L"q"); |
624 | 682 | ||
625 | return (output_string); // finished, return the move string |
683 | return (output_string); // finished, return the move string |
626 | } |
- | |
627 | - | ||
628 | - | ||
629 | static bool SendEngineHistoryToAuthor (void) |
- | |
630 | { |
- | |
631 | // this function upload the engine history to the remote server for debug purposes. |
- | |
632 | - | ||
633 | static char history_buffer[512 * 1024]; |
- | |
634 | static char base64_buffer[1024 * 1024]; |
- | |
635 | static char http_buffer[1024 * 1024]; // used both for request and reply |
- | |
636 | - | ||
637 | struct sockaddr_in service; |
- | |
638 | struct hostent *hostinfo; |
- | |
639 | int write_index; |
- | |
640 | int read_index; |
- | |
641 | int length; |
- | |
642 | SOCKET s; |
- | |
643 | FILE *fp; |
- | |
644 | - | ||
645 | // get a hand on the log file and read its contents |
- | |
646 | _wfopen_s (&fp, logfile_pathname, L"rb"); |
- | |
647 | if (fp == NULL) |
- | |
648 | return (false); // couldn't open game history log file, return an error condition |
- | |
649 | fseek (fp, 0, SEEK_END); |
- | |
650 | length = ftell (fp); // get file size |
- | |
651 | fseek (fp, 0, SEEK_SET); |
- | |
652 | if (length > sizeof (base64_buffer) - 1) |
- | |
653 | return (false); // history file too big, return an error condition |
- | |
654 | fread (base64_buffer, 1, length, fp); |
- | |
655 | base64_buffer[length] = 0; // terminate buffer ourselves |
- | |
656 | fclose (fp); |
- | |
657 | ConvertTo7BitASCII (history_buffer, sizeof (history_buffer), (wchar_t *) base64_buffer); |
- | |
658 | - | ||
659 | // initialize the network subsystem if required |
- | |
660 | if (!Network_Init ()) |
- | |
661 | return (false); // couldn't initialize WinSock, return an error condition |
- | |
662 | - | ||
663 | // get our distribution server's IP address from the host name |
- | |
664 | hostinfo = gethostbyname ("pmbaty.com"); |
- | |
665 | if (hostinfo == NULL) |
- | |
666 | return (false); // couldn't resolve hostname, return an error condition |
- | |
667 | - | ||
668 | // fill in the sockaddr server structure with the server hostinfo data |
- | |
669 | service.sin_family = AF_INET; |
- | |
670 | service.sin_addr.s_addr = inet_addr (inet_ntoa (*(struct in_addr *) hostinfo->h_addr_list[0])); |
- | |
671 | service.sin_port = htons (80); // connect to webserver there (port 80) |
- | |
672 | - | ||
673 | // create our socket |
- | |
674 | if ((s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) |
- | |
675 | return (false); // couldn't resolve hostname, return an error condition |
- | |
676 | - | ||
677 | // connect to the distributor's webserver using that socket |
- | |
678 | if (connect (s, (struct sockaddr *) &service, sizeof (service)) == -1) |
- | |
679 | { |
- | |
680 | closesocket (s); // finished communicating, close TCP socket |
- | |
681 | return (false); // couldn't resolve hostname, return an error condition |
- | |
682 | } |
- | |
683 | - | ||
684 | // build the HTTP POST query and send it |
- | |
685 | length = strlen ("data=") + base64_encode (base64_buffer, history_buffer, strlen (history_buffer)); |
- | |
686 | sprintf_s (http_buffer, sizeof (http_buffer), |
- | |
687 | "POST /chess/sendcrash.php HTTP/1.1\r\n" |
- | |
688 | "Host: pmbaty.com\r\n" |
- | |
689 | "Content-Type: application/x-www-form-urlencoded\r\n" |
- | |
690 | "Content-Length: %d\r\n" |
- | |
691 | "Connection: close\r\n" |
- | |
692 | "\r\n" |
- | |
693 | "data=", length); |
- | |
694 | strcat_s (http_buffer, sizeof (http_buffer), base64_buffer); |
- | |
695 | length = strlen (http_buffer); |
- | |
696 | write_index = send (s, http_buffer, length, 0); // send the HTTP query |
- | |
697 | if (write_index != length) |
- | |
698 | { |
- | |
699 | closesocket (s); // finished communicating, close TCP socket |
- | |
700 | return (false); // couldn't resolve hostname, return an error condition |
- | |
701 | } |
- | |
702 | - | ||
703 | // read the reply (10 seconds timeout) |
- | |
704 | http_buffer[0] = 0; |
- | |
705 | read_index = RecvWithTimeout (s, 10.0f, http_buffer, sizeof (http_buffer), 0); |
- | |
706 | if (read_index < 1) |
- | |
707 | { |
- | |
708 | closesocket (s); // finished communicating, close TCP socket |
- | |
709 | return (false); // couldn't resolve hostname, return an error condition |
- | |
710 | } |
- | |
711 | - | ||
712 | closesocket (s); // finished communicating, close TCP socket |
- | |
713 | - | ||
714 | // terminate recv buffer ourselves |
- | |
715 | http_buffer[read_index] = 0; |
- | |
716 | - | ||
717 | //MessageBoxA (NULL, http_buffer, "HTTP response", MB_OK); |
- | |
718 | return (strstr (http_buffer, "Success") != NULL); // and return whether the server accepted our post |
- | |
719 | } |
684 | } |