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