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