version 1.4.39b
[polyglot.git] / xboard2uci.c
1 \r
2 // xboard2uci.c\r
3 \r
4 // includes\r
5 \r
6 #include <errno.h>\r
7 #include <stdio.h>\r
8 #include <stdlib.h>\r
9 #include <string.h>\r
10 #include <time.h>\r
11 \r
12 #include "board.h"\r
13 #include "book.h"\r
14 #include "colour.h"\r
15 #include "engine.h"\r
16 #include "fen.h"\r
17 #include "game.h"\r
18 #include "gui.h"\r
19 #include "line.h"\r
20 #include "main.h"\r
21 #include "move.h"\r
22 #include "move_do.h"\r
23 #include "move_legal.h"\r
24 #include "option.h"\r
25 #include "parse.h"\r
26 #include "san.h"\r
27 #include "uci.h"\r
28 #include "uci2uci.h"\r
29 #include "util.h"\r
30 \r
31 // defines\r
32 \r
33 #define StringSize 4096\r
34 \r
35 // constants\r
36 \r
37 static const bool UseDebug = FALSE;\r
38 static const bool DelayPong = FALSE;\r
39 \r
40 // types\r
41 \r
42 typedef struct {\r
43    int state;\r
44    bool computer[ColourNb];\r
45    int exp_move;\r
46    int resign_nb;\r
47    my_timer_t timer[1];\r
48 } state_t;\r
49 \r
50 typedef struct {\r
51     bool has_feature_memory;\r
52     bool has_feature_smp;\r
53     bool has_feature_egt;\r
54     bool analyse;\r
55     bool computer;\r
56     const char * name;\r
57     bool ics;\r
58     bool new_hack; // "new" is a C++ keyword\r
59     bool ponder;\r
60     int ping;\r
61     bool post;\r
62     int proto_ver;\r
63     bool result;\r
64 \r
65     int mps;\r
66     double base;\r
67     double inc;\r
68     \r
69     bool time_limit;\r
70     double time_max;\r
71     \r
72     bool depth_limit;\r
73     int depth_max;\r
74     \r
75     double my_time;\r
76     double opp_time;\r
77 } xb_t;\r
78 \r
79 typedef enum { WAIT, THINK, PONDER, ANALYSE } dummy_state_t;\r
80 \r
81 // variables\r
82 \r
83 static state_t State[1];\r
84 static xb_t XB[1];\r
85 \r
86 // prototypes\r
87 \r
88 static void comp_move      (int move);\r
89 static void move_step      (int move);\r
90 static void board_update   ();\r
91 \r
92 static void mess           ();\r
93 static void no_mess        (int move);\r
94 \r
95 static void search_update  ();\r
96 static void search_clear   ();\r
97 static void update_remaining_time();\r
98 static int  report_best_score();\r
99 static bool kibitz_throttle (bool searching);\r
100 static void start_protected_command();\r
101 static void end_protected_command();\r
102 \r
103 static bool active         ();\r
104 static bool ponder         ();\r
105 static bool ponder_ok      (int ponder_move);\r
106 \r
107 static void stop_search    ();\r
108 \r
109 static void send_board     (int extra_move);\r
110 static void send_pv        ();\r
111 static void send_info      ();\r
112 \r
113 static void send_xboard_options ();\r
114 \r
115 static void learn          (int result);\r
116 \r
117 \r
118 // functions\r
119 \r
120 // xboard2uci_init()\r
121 \r
122 void xboard2uci_init() {\r
123    // init\r
124 \r
125    game_clear(Game);\r
126 \r
127    // state\r
128 \r
129    State->state = WAIT;\r
130 \r
131    State->computer[White] = FALSE;\r
132    State->computer[Black] = TRUE;\r
133 \r
134    State->exp_move = MoveNone;\r
135    State->resign_nb = 0;\r
136    my_timer_reset(State->timer);\r
137 \r
138    // yes there are engines that do not have the "Hash" option....\r
139    XB->has_feature_memory= (option_find(Uci->option,"Hash")!=NULL);\r
140    XB->has_feature_smp = (uci_thread_option(Uci)!=NULL);\r
141    // TODO: support for other types of table bases\r
142    XB->has_feature_egt = (option_find(Uci->option,"NalimovPath")!=NULL);\r
143    XB->analyse = FALSE;\r
144    XB->computer = FALSE;\r
145    XB->name = NULL;\r
146    my_string_set(&XB->name,"<empty>");\r
147    XB->ics = FALSE;\r
148    XB->new_hack = TRUE;\r
149    XB->ping = -1;\r
150    XB->ponder = FALSE;\r
151    XB->post = FALSE;\r
152    XB->proto_ver = 1;\r
153    XB->result = FALSE;\r
154 \r
155    XB->mps = 0;\r
156    XB->base = 300.0;\r
157    XB->inc = 0.0;\r
158 \r
159    XB->time_limit = FALSE;\r
160    XB->time_max = 5.0;\r
161 \r
162    XB->depth_limit = FALSE;\r
163    XB->depth_max = 127;\r
164 \r
165    XB->my_time = 300.0;\r
166    XB->opp_time = 300.0;\r
167 }\r
168 \r
169 // xboard2uci_gui_step()\r
170 \r
171 void xboard2uci_gui_step(char string[]) {\r
172 \r
173         int move;\r
174         char move_string[256];\r
175         board_t board[1];\r
176 \r
177                 if (FALSE) {\r
178          \r
179                 } else if (match(string,"accepted *")) {\r
180 \r
181                         // ignore\r
182 \r
183                 } else if (match(string,"analyze")) {\r
184 \r
185                         State->computer[White] = FALSE;\r
186                         State->computer[Black] = FALSE;\r
187 \r
188                         XB->analyse = TRUE;\r
189                         XB->new_hack = FALSE;\r
190                         ASSERT(!XB->result);\r
191                         XB->result = FALSE;\r
192 \r
193                         mess();\r
194 \r
195                 } else if (match(string,"bk")) {\r
196 \r
197                         if (option_get_bool(Option,"Book")) {\r
198                                 game_get_board(Game,board);\r
199                                 book_disp(board);\r
200                         }\r
201 \r
202                 } else if (match(string,"black")) {\r
203 \r
204                         if (colour_is_black(game_turn(Game))) {\r
205 \r
206                                 State->computer[White] = TRUE;\r
207                                 State->computer[Black] = FALSE;\r
208 \r
209                                 XB->new_hack = TRUE;\r
210                                 XB->result = FALSE;\r
211 \r
212                                 mess();\r
213                         }\r
214 \r
215                 } else if (match(string,"computer")) {\r
216 \r
217                         XB->computer = TRUE;\r
218 \r
219                 } else if (match(string,"draw")) {\r
220                         if(option_find(Uci->option,"UCI_DrawOffers")){\r
221                             my_log("POLYGLOT draw from XB received");\r
222                                 uci_send_option(Uci,"DrawOffer","%s","draw");}\r
223                 } else if (match(string,"easy")) {\r
224 \r
225                         XB->ponder = FALSE;\r
226 \r
227                         mess();\r
228 \r
229                 } else if (match(string,"edit")) {\r
230 \r
231                         // refuse\r
232 \r
233                         gui_send(GUI,"Error (unknown command): %s",string);\r
234 \r
235                 } else if (match(string,"exit")) {\r
236 \r
237                         State->computer[White] = FALSE;\r
238                         State->computer[Black] = FALSE;\r
239 \r
240                         XB->analyse = FALSE;\r
241 \r
242                         mess();\r
243 \r
244                 } else if (match(string,"force")) {\r
245 \r
246                         State->computer[White] = FALSE;\r
247                         State->computer[Black] = FALSE;\r
248 \r
249                         mess();\r
250 \r
251                 } else if (match(string,"go")) {\r
252 \r
253                         State->computer[game_turn(Game)] = TRUE;\r
254                         State->computer[colour_opp(game_turn(Game))] = FALSE;\r
255 \r
256                         XB->new_hack = FALSE;\r
257                         ASSERT(!XB->result);\r
258                         XB->result = FALSE;\r
259 \r
260                         mess();\r
261 \r
262                 } else if (match(string,"hard")) {\r
263 \r
264                         XB->ponder = TRUE;\r
265 \r
266                         mess();\r
267 \r
268                 } else if (match(string,"hint")) {\r
269 \r
270                         if (option_get_bool(Option,"Book")) {\r
271 \r
272                                 game_get_board(Game,board);\r
273                                 move = book_move(board,FALSE);\r
274 \r
275                                 if (move != MoveNone && move_is_legal(move,board)) {\r
276                                         move_to_san(move,board,move_string,256);\r
277                                         gui_send(GUI,"Hint: %s",move_string);\r
278                                 }\r
279                         }\r
280 \r
281                 } else if (match(string,"ics *")) {\r
282 \r
283                         XB->ics = TRUE;\r
284 \r
285                 } else if (match(string,"level * *:* *")) {\r
286 \r
287                         XB->mps  = atoi(Star[0]);\r
288                         XB->base = ((double)atoi(Star[1])) * 60.0 + ((double)atoi(Star[2]));\r
289                         XB->inc  = ((double)atoi(Star[3]));\r
290 \r
291                 } else if (match(string,"level * * *")) {\r
292 \r
293                         XB->mps  = atoi(Star[0]);\r
294                         XB->base = ((double)atoi(Star[1])) * 60.0;\r
295                         XB->inc  = ((double)atoi(Star[2]));\r
296 \r
297                 } else if (match(string,"name *")) {\r
298 \r
299                         my_string_set(&XB->name,Star[0]);\r
300 \r
301                 } else if (match(string,"new")) {\r
302 \r
303             uci_send_isready(Uci);\r
304                         my_log("POLYGLOT NEW GAME\n");\r
305 \r
306                         option_set(Option,"Chess960","false");\r
307 \r
308                         game_clear(Game);\r
309 \r
310                         if (XB->analyse) {\r
311                                 State->computer[White] = FALSE;\r
312                                 State->computer[Black] = FALSE;\r
313                         } else {\r
314                                 State->computer[White] = FALSE;\r
315                                 State->computer[Black] = TRUE;\r
316                         }\r
317 \r
318                         XB->new_hack = TRUE;\r
319                         XB->result = FALSE;\r
320 \r
321                         XB->depth_limit = FALSE;\r
322 \r
323                         XB->computer = FALSE;\r
324                         my_string_set(&XB->name,"<empty>");\r
325 \r
326                         board_update();\r
327                         mess();\r
328 \r
329                         uci_send_ucinewgame(Uci);\r
330 \r
331                 } else if (match(string,"nopost")) {\r
332 \r
333                         XB->post = FALSE;\r
334 \r
335                 } else if (match(string,"otim *")) {\r
336 \r
337                         XB->opp_time = ((double)atoi(Star[0])) / 100.0;\r
338                         if (XB->opp_time < 0.0) XB->opp_time = 0.0;\r
339 \r
340                 } else if (match(string,"pause")) {\r
341 \r
342                         // refuse\r
343 \r
344                         gui_send(GUI,"Error (unknown command): %s",string);\r
345 \r
346                 } else if (match(string,"ping *")) {\r
347 \r
348                         // HACK; TODO: answer only after an engine move\r
349 \r
350                         if (DelayPong) {\r
351                                 if (XB->ping >= 0) gui_send(GUI,"pong %d",XB->ping); // HACK: get rid of old ping\r
352                                 XB->ping = atoi(Star[0]);\r
353                                 uci_send_isready(Uci);\r
354                         } else {\r
355                                 ASSERT(XB->ping==-1);\r
356                                 gui_send(GUI,"pong %s",Star[0]);\r
357                         }\r
358 \r
359                 } else if (match(string,"playother")) {\r
360 \r
361                         State->computer[game_turn(Game)] = FALSE;\r
362                         State->computer[colour_opp(game_turn(Game))] = TRUE;\r
363 \r
364                         XB->new_hack = FALSE;\r
365                         ASSERT(!XB->result);\r
366                         XB->result = FALSE;\r
367 \r
368                         mess();\r
369 \r
370                 } else if (match(string,"post")) {\r
371 \r
372                         XB->post = TRUE;\r
373 \r
374                 } else if (match(string,"protover *")) {\r
375             XB->proto_ver = atoi(Star[0]);\r
376             ASSERT(XB->proto_ver>=2);\r
377             send_xboard_options();\r
378 \r
379                 } else if (match(string,"quit")) {\r
380                         my_log("POLYGLOT *** \"quit\" from GUI ***\n");\r
381                         quit();\r
382                 } else if (match(string,"random")) {\r
383 \r
384                         // ignore\r
385 \r
386                 } else if (match(string,"rating * *")) {\r
387 \r
388                         // ignore\r
389 \r
390                 } else if (match(string,"remove")) {\r
391 \r
392                         if (game_pos(Game) >= 2) {\r
393 \r
394                                 game_goto(Game,game_pos(Game)-2);\r
395 \r
396                                 ASSERT(!XB->new_hack);\r
397                                 XB->new_hack = FALSE; // HACK?\r
398                                 XB->result = FALSE;\r
399 \r
400                                 board_update();\r
401                                 mess();\r
402                         }\r
403 \r
404                 } else if (match(string,"rejected *")) {\r
405 \r
406                         // ignore\r
407 \r
408                 } else if (match(string,"reset")) { // protover 3?\r
409 \r
410                         // refuse\r
411 \r
412                         gui_send(GUI,"Error (unknown command): %s",string);\r
413 \r
414                 } else if (FALSE\r
415                         || match(string,"result * {*}")\r
416                         || match(string,"result * {* }")\r
417                         || match(string,"result * { *}")\r
418                         || match(string,"result * { * }")) {\r
419 \r
420                                 my_log("POLYGLOT GAME END\n");\r
421 \r
422                                 XB->result = TRUE;\r
423 \r
424                                 mess();\r
425 \r
426                                 // book learning\r
427 \r
428                                 if (option_get_bool(Option,"Book") &&\r
429                     option_get_bool(Option,"BookLearn")) {\r
430 \r
431                                         if (FALSE) {\r
432                                         } else if (my_string_equal(Star[0],"1-0")) {\r
433                                                 learn(+1);\r
434                                         } else if (my_string_equal(Star[0],"0-1")) {\r
435                                                 learn(-1);\r
436                                         } else if (my_string_equal(Star[0],"1/2-1/2")) {\r
437                                                 learn(0);\r
438                                         }\r
439                                 }\r
440                 } else if (match(string,"resume")) {\r
441 \r
442                         // refuse\r
443 \r
444                         gui_send(GUI,"Error (unknown command): %s",string);\r
445 \r
446         } else if (match(string,"option *=*")   ||\r
447                    match(string,"option * =*") ||\r
448                    match(string,"option *= *") ||\r
449                    match(string,"option * = *")\r
450                    ){\r
451             char *name=Star[0];\r
452             char *value=Star[1];\r
453             if(match(name, "Polyglot *")){\r
454                 char *pg_name=Star[0];\r
455                 polyglot_set_option(pg_name,value);\r
456             }else{\r
457                 option_t *opt=option_find(Uci->option,name);\r
458                 if(opt){\r
459                     if(my_string_case_equal(opt->type,"check")){\r
460                        value=my_string_equal(value,"1")?"true":"false";\r
461                     }\r
462                     start_protected_command();\r
463                     uci_send_option(Uci, name, "%s", value);\r
464                     end_protected_command();\r
465                 }else{\r
466                     gui_send(GUI,"Error (unknown option): %s",name); \r
467                 }\r
468             }\r
469         } else if (match(string,"option *")){\r
470             char *name=Star[0];\r
471             start_protected_command();\r
472                 // value is ignored\r
473             if(!uci_send_option(Uci, name, "%s", "<empty>")){\r
474                gui_send(GUI,"Error (unknown option): %s",name); \r
475             }; \r
476             end_protected_command();\r
477         } else if (XB->has_feature_smp && match(string,"cores *")){\r
478                 int cores=atoi(Star[0]);\r
479                 if(cores>=1){\r
480                     // updating the number of cores\r
481                     my_log("POLYGLOT setting the number of cores to %d\n",cores);\r
482                     start_protected_command();\r
483                     uci_set_threads(Uci,cores); \r
484                     end_protected_command();\r
485                 } else{\r
486                    // refuse\r
487                     gui_send(GUI,"Error (unknown command): %s",string);\r
488                 }\r
489         } else if (XB->has_feature_egt && match(string,"egtpath * *")){\r
490                 char *type=Star[0];\r
491                 char *path=Star[1];\r
492                 if(!my_string_case_equal(type,"nalimov")){\r
493                    // refuse\r
494                     gui_send(GUI,"Error (unsupported table base format): %s",string);\r
495                 }else if(my_string_empty(path)){\r
496                     // refuse\r
497                     gui_send(GUI,"Error (unknown command): %s",string);\r
498                 }else{\r
499                     // updating NalimovPath\r
500                     my_log("POLYGLOT setting the Nalimov path to %s\n",path);\r
501                     start_protected_command();\r
502                     uci_send_option(Uci,"NalimovPath","%s",path);\r
503                     end_protected_command();\r
504                 }\r
505         } else if (XB->has_feature_memory && match(string,"memory *")){\r
506             int memory = atoi(Star[0]);\r
507             int nalimov_cache;\r
508             int real_memory;\r
509             if(memory>=1){\r
510                 // updating the available memory\r
511                 option_t *opt;\r
512                 my_log("POLYGLOT setting the amount of memory to %dMb\n",memory);\r
513                 if((opt=option_find(Uci->option,"NalimovCache"))){\r
514                     nalimov_cache=atoi(opt->value);\r
515                 }else{\r
516                     nalimov_cache=0;\r
517                 }\r
518                 my_log("POLYGLOT Nalimov Cache is %dMb\n",nalimov_cache);\r
519                 real_memory=memory-nalimov_cache;\r
520                 if(real_memory>0){\r
521                     start_protected_command();\r
522                     uci_send_option(Uci,"Hash", "%d", real_memory);\r
523                     end_protected_command();\r
524                 }\r
525             }else{\r
526                 // refuse\r
527                 gui_send(GUI,"Error (unknown command): %s",string);\r
528             }\r
529 \r
530                 } else if (match(string,"sd *")) {\r
531 \r
532                         XB->depth_limit = TRUE;\r
533                         XB->depth_max = atoi(Star[0]);\r
534 \r
535                 } else if (match(string,"setboard *")) {\r
536 \r
537                         my_log("POLYGLOT FEN %s\n",Star[0]);\r
538 \r
539                         if (!game_init(Game,Star[0])) my_fatal("xboard_step(): bad FEN \"%s\"\n",Star[0]);\r
540 \r
541                         State->computer[White] = FALSE;\r
542                         State->computer[Black] = FALSE;\r
543 \r
544                         XB->new_hack = TRUE; // HACK?\r
545                         XB->result = FALSE;\r
546 \r
547                         board_update();\r
548                         mess();\r
549 \r
550                 } else if (match(string,"st *")) {\r
551 \r
552                         XB->time_limit = TRUE;\r
553                         XB->time_max = ((double)atoi(Star[0]));\r
554 \r
555                 } else if (match(string,"time *")) {\r
556 \r
557                         XB->my_time = ((double)atoi(Star[0])) / 100.0;\r
558                         if (XB->my_time < 0.0) XB->my_time = 0.0;\r
559 \r
560                 } else if (match(string,"undo")) {\r
561 \r
562                         if (game_pos(Game) >= 1) {\r
563 \r
564                                 game_goto(Game,game_pos(Game)-1);\r
565 \r
566                                 ASSERT(!XB->new_hack);\r
567                                 XB->new_hack = FALSE; // HACK?\r
568                                 XB->result = FALSE;\r
569 \r
570                                 board_update();\r
571                                 mess();\r
572                         }\r
573 \r
574                 } else if (match(string,"usermove *")) {\r
575 \r
576                         game_get_board(Game,board);\r
577                         move = move_from_san(Star[0],board);\r
578 \r
579                         if (move != MoveNone && move_is_legal(move,board)) {\r
580 \r
581                                 XB->new_hack = FALSE;\r
582                                 ASSERT(!XB->result);\r
583                                 XB->result = FALSE;\r
584 \r
585                                 move_step(move);\r
586                                 no_mess(move);\r
587 \r
588                         } else {\r
589 \r
590                                 gui_send(GUI,"Illegal move: %s",Star[0]);\r
591                         }\r
592 \r
593                 } else if (match(string,"variant *")) {\r
594 \r
595                         if (my_string_equal(Star[0],"fischerandom")) {\r
596                                 option_set(Option,"Chess960","true");\r
597                         } else {\r
598                                 option_set(Option,"Chess960","false");\r
599                         }\r
600 \r
601                 } else if (match(string,"white")) {\r
602 \r
603                         if (colour_is_white(game_turn(Game))) {\r
604 \r
605                                 State->computer[White] = FALSE;\r
606                                 State->computer[Black] = TRUE;\r
607 \r
608                                 XB->new_hack = TRUE;\r
609                                 XB->result = FALSE;\r
610 \r
611                                 mess();\r
612                         }\r
613 \r
614                 } else if (match(string,"xboard")) {\r
615 \r
616                         // ignore\r
617 \r
618                 } else if (match(string,".")) { // analyse info\r
619 \r
620                         if (State->state == ANALYSE) {\r
621                                 int depth=Uci->best_depth;//HACK: don't clear engine-output window...\r
622 \r
623                                 ASSERT(Uci->searching);\r
624                                 ASSERT(Uci->pending_nb>=1);\r
625 \r
626                                 if (Uci->root_move != MoveNone && move_is_legal(Uci->root_move,Uci->board)) {\r
627                                         move_to_san(Uci->root_move,Uci->board,move_string,256);\r
628                                         gui_send(GUI,"stat01: %.0f "S64_FORMAT" %d %d %d %s",Uci->time*100.0,Uci->node_nb,/*Uci->*/depth,Uci->root_move_nb-(Uci->root_move_pos+1),Uci->root_move_nb,move_string);\r
629                                 } else {\r
630                                         gui_send(GUI,"stat01: %.0f "S64_FORMAT" %d %d %d",Uci->time*100.0,Uci->node_nb,/*Uci->*/depth,0,0); // HACK\r
631                                 }\r
632                         }\r
633 \r
634                 } else if (match(string,"?")) { // move now\r
635 \r
636                         if (State->state == THINK) {\r
637 \r
638                                 ASSERT(Uci->searching);\r
639                                 ASSERT(Uci->pending_nb>=1);\r
640 \r
641                                 // HACK: just send "stop" to the engine\r
642 \r
643                                 if (Uci->searching) {\r
644                                         my_log("POLYGLOT STOP SEARCH\n");\r
645                                         engine_send(Engine,"stop");\r
646                                 }\r
647                         }\r
648 \r
649                 } else { // unknown command, maybe a move?\r
650 \r
651                         game_get_board(Game,board);\r
652                         move = move_from_san(string,board);\r
653 \r
654                         if (move != MoveNone && move_is_legal(move,board)) {\r
655 \r
656                                 XB->new_hack = FALSE;\r
657                                 ASSERT(!XB->result);\r
658                                 XB->result = FALSE;\r
659 \r
660                                 move_step(move);\r
661                                 no_mess(move);\r
662 \r
663                         } else if (move != MoveNone) {\r
664 \r
665                                 gui_send(GUI,"Illegal move: %s",string);\r
666 \r
667                         } else {\r
668 \r
669                                 gui_send(GUI,"Error (unknown command): %s",string);\r
670                         }\r
671                 }\r
672         return;\r
673 }\r
674 \r
675 // xboard2uci_engine_step()\r
676 \r
677 void xboard2uci_engine_step(char string[]) {\r
678 \r
679         int event;\r
680     board_t board[1];\r
681                 event = uci_parse(Uci,string);\r
682 \r
683                 // react to events\r
684 \r
685                 if ((event & EVENT_READY) != 0) {\r
686 \r
687                         // the engine is now ready\r
688 \r
689                         if (!Uci->ready) {\r
690                                 Uci->ready = TRUE;\r
691                     //  if (XB->proto_ver >= 2) xboard_send(XBoard,"feature done=1");\r
692                         }\r
693 \r
694                         if (!DelayPong && XB->ping >= 0) {\r
695                                 gui_send(GUI,"pong %d",XB->ping);\r
696                                 XB->ping = -1;\r
697                         }\r
698                 }\r
699 \r
700                 if ((event & EVENT_MOVE) != 0 && State->state == THINK) {\r
701 \r
702                         // the engine is playing a move\r
703 \r
704                         // MEGA HACK: estimate remaining time because XBoard won't send it!\r
705 \r
706                         my_timer_stop(State->timer);\r
707 \r
708                         XB->my_time -= my_timer_elapsed_real(State->timer);\r
709                         XB->my_time += XB->inc;\r
710                         if (XB->mps != 0 && (game_move_nb(Game) + 1) % XB->mps == 0) XB->my_time += XB->base;\r
711 \r
712                         if (XB->my_time < 0.0) XB->my_time = 0.0;\r
713 \r
714                         // play the engine move\r
715 \r
716                         comp_move(Uci->best_move);\r
717                 }\r
718 \r
719                 if ((event & EVENT_PV) != 0) {\r
720 \r
721                         // the engine has sent a new PV\r
722 \r
723                         send_pv();\r
724                 }\r
725                 if ((event & EVENT_INFO) != 0) {\r
726 \r
727                         // the engine has sent info\r
728 \r
729                         send_info();\r
730                 }\r
731                 if((event & (EVENT_DRAW|EVENT_RESIGN))!=0){\r
732                         my_log("POYGLOT draw offer/resign from engine\n");\r
733                         if(option_find(Uci->option,"UCI_DrawOffers")){\r
734                                 if(event & EVENT_DRAW)\r
735                                         gui_send(GUI,"offer draw");\r
736                                 else\r
737                                         gui_send(GUI,"resign");\r
738                         }\r
739                 }\r
740                 if(((event & EVENT_ILLEGAL_MOVE)!=0) && (State->state == THINK)){\r
741             game_get_board(Game,board);\r
742             if(board->turn==White){\r
743                 gui_send(GUI,"0-1 {polyglot: resign"\r
744                             " (illegal engine move white)}");\r
745             }else{\r
746                 gui_send(GUI,"1-0 {polyglot: resign"\r
747                          " (illegal engine move black)}");\r
748             }\r
749             board_disp(board);\r
750             XB->result = TRUE;\r
751             mess();\r
752         }\r
753 }\r
754 \r
755 // format_xboard_option_line\r
756 \r
757 void format_xboard_option_line(char * option_line, option_t *opt){\r
758     int j;\r
759     char option_string[StringSize];\r
760     char *tmp;\r
761     strcpy(option_line,"");\r
762         // buffer overflow alert\r
763     strcat(option_line,"feature option=\"");\r
764     if(opt->mode&PG){\r
765         strcat(option_line,"Polyglot ");\r
766     }\r
767     sprintf(option_string,"%s",opt->name);\r
768     strcat(option_line,option_string);\r
769     sprintf(option_string," -%s",opt->type);\r
770     strcat(option_line,option_string);\r
771     if(strcmp(opt->type,"button") && strcmp(opt->type,"combo")){\r
772         if(strcmp(opt->type,"check")){\r
773             sprintf(option_string," %s",opt->default_);\r
774         }else{\r
775             sprintf(option_string," %d",\r
776                     my_string_case_equal(opt->default_,"true")?1:0);\r
777         }\r
778         strcat(option_line,option_string);\r
779     }\r
780     if(!strcmp(opt->type,"spin")){\r
781         sprintf(option_string," %s",opt->min);\r
782             strcat(option_line,option_string);\r
783     }\r
784     if(!strcmp(opt->type,"spin")){\r
785         sprintf(option_string," %s",opt->max);\r
786         strcat(option_line,option_string);\r
787     }\r
788     for(j=0;j<opt->var_nb;j++){\r
789         if(!strcmp(opt->var[j],opt->default_)){\r
790             sprintf(option_string," *%s",opt->var[j]);\r
791         }else{\r
792             sprintf(option_string," %s",opt->var[j]);\r
793         }\r
794         strcat(option_line,option_string);\r
795         if(j!=opt->var_nb-1){\r
796             strcat(option_line," ///");\r
797         }\r
798     }\r
799     strcat(option_line,"\"");\r
800     if(option_get_bool(Option,"WbWorkArounds") &&\r
801        (tmp=strstr(option_line,"Draw"))){\r
802         *tmp='d';\r
803         my_log("POLYGLOT Decapitalizing \"Draw\" in option \"%s\"\n",\r
804                opt->name);\r
805     }\r
806 }\r
807 \r
808 // send_xboard_options()\r
809 \r
810 static void send_xboard_options(){\r
811 \r
812     char option_line[StringSize]="";\r
813     const char * name;\r
814     option_t *opt;\r
815     \r
816     gui_send(GUI,"feature done=0");\r
817     \r
818     gui_send(GUI,"feature analyze=1");\r
819     gui_send(GUI,"feature colors=0");\r
820     gui_send(GUI,"feature draw=1");\r
821     gui_send(GUI,"feature ics=1");\r
822     gui_send(GUI,"feature myname=\"%s\"",\r
823              option_get_string(Option,"EngineName"));\r
824     gui_send(GUI,"feature name=1");\r
825     gui_send(GUI,"feature pause=0");\r
826     gui_send(GUI,"feature ping=1");\r
827     gui_send(GUI,"feature playother=1");\r
828     gui_send(GUI,"feature reuse=1");\r
829     gui_send(GUI,"feature san=0");\r
830     gui_send(GUI,"feature setboard=1");\r
831     gui_send(GUI,"feature sigint=0");\r
832     gui_send(GUI,"feature sigterm=0");\r
833     gui_send(GUI,"feature time=1");\r
834     gui_send(GUI,"feature usermove=1");\r
835     if (XB->has_feature_memory){\r
836         gui_send(GUI,"feature memory=1");\r
837     }else{\r
838         gui_send(GUI,"feature memory=0");\r
839     }\r
840     if (XB->has_feature_smp){\r
841         gui_send(GUI,"feature smp=1");\r
842     }else{\r
843         gui_send(GUI,"feature smp=0");\r
844     }\r
845     if (XB->has_feature_egt){\r
846             // TODO: support for other types of table bases\r
847         gui_send(GUI,"feature egt=\"nalimov\"");\r
848     }else{\r
849         gui_send(GUI,"feature egt=\"\"");\r
850     }\r
851     \r
852     if (option_find(Uci->option,"UCI_Chess960")) {\r
853         gui_send(GUI,"feature variants=\"normal,fischerandom\"");\r
854     } else {\r
855         gui_send(GUI,"feature variants=\"normal\"");\r
856     }\r
857 \r
858     option_start_iter(Uci->option);\r
859     while((opt=option_next(Uci->option))){\r
860         if(my_string_case_equal(opt->name,"UCI_AnalyseMode")) continue;\r
861         if(my_string_case_equal(opt->name,"UCI_Opponent")) continue;\r
862         if(my_string_case_equal(opt->name,"UCI_Chess960")) continue;\r
863         if(my_string_case_equal(opt->name,"UCI_ShowCurrLine")) continue;\r
864         if(my_string_case_equal(opt->name,"UCI_ShowRefutations")) continue;\r
865         if(my_string_case_equal(opt->name,"UCI_ShredderbasesPath")) continue;\r
866         if(my_string_case_equal(opt->name,"UCI_SetPositionValue")) continue;\r
867         if(my_string_case_equal(opt->name,"UCI_DrawOffers")) continue;\r
868         if(my_string_case_equal(opt->name,"Ponder")) continue;\r
869         if(my_string_case_equal(opt->name,"Hash")) continue;\r
870         if(my_string_case_equal(opt->name,"NalimovPath")) continue;\r
871         if((name=uci_thread_option(Uci))!=NULL &&\r
872            my_string_case_equal(opt->name,name)) continue;\r
873         \r
874         format_xboard_option_line(option_line,opt);\r
875         \r
876         gui_send(GUI,"%s",option_line);\r
877     }\r
878 \r
879 \r
880     option_start_iter(Option);\r
881     while((opt=option_next(Option))){\r
882         if(opt->mode &XBOARD){\r
883             format_xboard_option_line(option_line,opt);\r
884             gui_send(GUI,"%s",option_line);\r
885         }\r
886     }       \r
887     gui_send(GUI,"feature done=1"); \r
888     \r
889 }\r
890 \r
891 // report_best_score()\r
892 \r
893 static int report_best_score(){\r
894     if(!option_get_bool(Option,"ScoreWhite") ||\r
895        colour_is_white(Uci->board->turn)){\r
896         return Uci->best_score;\r
897     }else{\r
898         return -Uci->best_score;\r
899     }\r
900 }\r
901 \r
902 // comp_move()\r
903 \r
904 static void comp_move(int move) {\r
905 \r
906    board_t board[1];\r
907    char string[256];\r
908 \r
909    ASSERT(move_is_ok(move));\r
910 \r
911    ASSERT(State->state==THINK);\r
912    ASSERT(!XB->analyse);\r
913 \r
914    if(option_get_bool(Option,"RepeatPV"))\r
915            send_pv(); // to update time and nodes\r
916 \r
917    // send the move\r
918 \r
919    game_get_board(Game,board);\r
920 \r
921    if (move_is_castle(move,board) && option_get_bool(Option,"Chess960")) {\r
922       if (!move_to_san(move,board,string,256)) my_fatal("comp_move(): move_to_san() failed\n"); // O-O/O-O-O\r
923    } else {\r
924       if (!move_to_can(move,board,string,256)) my_fatal("comp_move(): move_to_can() failed\n");\r
925    }\r
926 \r
927    gui_send(GUI,"move %s",string);\r
928 \r
929    // resign?\r
930 \r
931    if (option_get_bool(Option,"Resign") && Uci->root_move_nb > 1) {\r
932 \r
933        if (Uci->best_score <= -abs(option_get_int(Option,"ResignScore"))) {\r
934 \r
935          State->resign_nb++;\r
936          my_log("POLYGLOT %d move%s with resign score\n",State->resign_nb,(State->resign_nb>1)?"s":"");\r
937 \r
938          if (State->resign_nb >= option_get_int(Option,"ResignMoves")) {\r
939             my_log("POLYGLOT *** RESIGN ***\n");\r
940             gui_send(GUI,"resign");\r
941          }\r
942 \r
943       } else {\r
944 \r
945          if (State->resign_nb > 0) my_log("POLYGLOT resign reset (State->resign_nb=%d)\n",State->resign_nb);\r
946          State->resign_nb = 0;\r
947       }\r
948    }\r
949 \r
950    // play the move\r
951 \r
952    move_step(move);\r
953    no_mess(move);\r
954 }\r
955 \r
956 // move_step()\r
957 \r
958 static void move_step(int move) {\r
959 \r
960    board_t board[1];\r
961    char move_string[256];\r
962 \r
963    ASSERT(move_is_ok(move));\r
964 \r
965    // log\r
966 \r
967    game_get_board(Game,board);\r
968 \r
969    if (move != MoveNone && move_is_legal(move,board)) {\r
970 \r
971       move_to_san(move,board,move_string,256);\r
972       my_log("POLYGLOT MOVE %s\n",move_string);\r
973 \r
974    } else {\r
975 \r
976       move_to_can(move,board,move_string,256);\r
977       my_log("POLYGLOT ILLEGAL MOVE \"%s\"\n",move_string);\r
978       board_disp(board);\r
979 \r
980       my_fatal("move_step(): illegal move \"%s\"\n",move_string);\r
981    }\r
982 \r
983    // play the move\r
984 \r
985    game_add_move(Game,move);\r
986    board_update();\r
987 }\r
988 \r
989 // board_update()\r
990 \r
991 static void board_update() {\r
992 \r
993    // handle game end\r
994 \r
995    ASSERT(!XB->result);\r
996 \r
997    switch (game_status(Game)) {\r
998    case PLAYING:\r
999       break;\r
1000    case WHITE_MATES:\r
1001       gui_send(GUI,"1-0 {White mates}");\r
1002       break;\r
1003    case BLACK_MATES:\r
1004       gui_send(GUI,"0-1 {Black mates}");\r
1005       break;\r
1006    case STALEMATE:\r
1007       gui_send(GUI,"1/2-1/2 {Stalemate}");\r
1008       break;\r
1009    case DRAW_MATERIAL:\r
1010       gui_send(GUI,"1/2-1/2 {Draw by insufficient material}");\r
1011       break;\r
1012    case DRAW_FIFTY:\r
1013       gui_send(GUI,"1/2-1/2 {Draw by fifty-move rule}");\r
1014       break;\r
1015    case DRAW_REPETITION:\r
1016       gui_send(GUI,"1/2-1/2 {Draw by repetition}");\r
1017       break;\r
1018    default:\r
1019       ASSERT(FALSE);\r
1020       break;\r
1021    }\r
1022 }\r
1023 \r
1024 // mess()\r
1025 \r
1026 static void mess() {\r
1027 \r
1028    // clear state variables\r
1029 \r
1030    State->resign_nb = 0;\r
1031    State->exp_move = MoveNone;\r
1032    my_timer_reset(State->timer);\r
1033 \r
1034    // abort a possible search\r
1035 \r
1036    stop_search();\r
1037 \r
1038    // calculate the new state\r
1039 \r
1040    if (FALSE) {\r
1041    } else if (!active()) {\r
1042       State->state = WAIT;\r
1043       my_log("POLYGLOT WAIT\n");\r
1044    } else if (XB->analyse) {\r
1045       State->state = ANALYSE;\r
1046       my_log("POLYGLOT ANALYSE\n");\r
1047    } else if (State->computer[game_turn(Game)]) {\r
1048       State->state = THINK;\r
1049       my_log("POLYGLOT THINK\n");\r
1050    } else {\r
1051       State->state = WAIT;\r
1052       my_log("POLYGLOT WAIT\n");\r
1053    }\r
1054 \r
1055    search_update();\r
1056 }\r
1057 \r
1058 // no_mess()\r
1059 \r
1060 static void no_mess(int move) {\r
1061 \r
1062    ASSERT(move_is_ok(move));\r
1063 \r
1064    // just received a move, calculate the new state\r
1065 \r
1066    if (FALSE) {\r
1067 \r
1068    } else if (!active()) {\r
1069 \r
1070       stop_search(); // abort a possible search\r
1071 \r
1072       State->state = WAIT;\r
1073       State->exp_move = MoveNone;\r
1074 \r
1075       my_log("POLYGLOT WAIT\n");\r
1076 \r
1077    } else if (State->state == WAIT) {\r
1078 \r
1079       ASSERT(State->computer[game_turn(Game)]);\r
1080       ASSERT(!State->computer[colour_opp(game_turn(Game))]);\r
1081       ASSERT(!XB->analyse);\r
1082 \r
1083       my_log("POLYGLOT WAIT -> THINK\n");\r
1084 \r
1085       State->state = THINK;\r
1086       State->exp_move = MoveNone;\r
1087 \r
1088    } else if (State->state == THINK) {\r
1089 \r
1090       ASSERT(!State->computer[game_turn(Game)]);\r
1091       ASSERT(State->computer[colour_opp(game_turn(Game))]);\r
1092       ASSERT(!XB->analyse);\r
1093 \r
1094       if (ponder() && ponder_ok(Uci->ponder_move)) {\r
1095 \r
1096          my_log("POLYGLOT THINK -> PONDER\n");\r
1097 \r
1098          State->state = PONDER;\r
1099          State->exp_move = Uci->ponder_move;\r
1100 \r
1101       } else {\r
1102 \r
1103          my_log("POLYGLOT THINK -> WAIT\n");\r
1104 \r
1105          State->state = WAIT;\r
1106          State->exp_move = MoveNone;\r
1107       }\r
1108 \r
1109    } else if (State->state == PONDER) {\r
1110 \r
1111       ASSERT(State->computer[game_turn(Game)]);\r
1112       ASSERT(!State->computer[colour_opp(game_turn(Game))]);\r
1113       ASSERT(!XB->analyse);\r
1114 \r
1115       if (move == State->exp_move && Uci->searching) {\r
1116 \r
1117          ASSERT(Uci->searching);\r
1118          ASSERT(Uci->pending_nb>=1);\r
1119 \r
1120          my_timer_start(State->timer);//also resets\r
1121 \r
1122          my_log("POLYGLOT PONDER -> THINK (*** HIT ***)\n");\r
1123          engine_send(Engine,"ponderhit");\r
1124 \r
1125          State->state = THINK;\r
1126          State->exp_move = MoveNone;\r
1127 \r
1128          send_pv(); // update display\r
1129 \r
1130          return; // do not launch a new search\r
1131 \r
1132       } else {\r
1133 \r
1134          my_log("POLYGLOT PONDER -> THINK (miss)\n");\r
1135 \r
1136          stop_search();\r
1137 \r
1138          State->state = THINK;\r
1139          State->exp_move = MoveNone;\r
1140       }\r
1141 \r
1142    } else if (State->state == ANALYSE) {\r
1143 \r
1144       ASSERT(XB->analyse);\r
1145 \r
1146       my_log("POLYGLOT ANALYSE -> ANALYSE\n");\r
1147 \r
1148       stop_search();\r
1149 \r
1150    } else {\r
1151 \r
1152       ASSERT(FALSE);\r
1153    }\r
1154 \r
1155    search_update();\r
1156 }\r
1157 \r
1158 // start_protected_command()\r
1159 \r
1160 static void start_protected_command(){\r
1161     stop_search();\r
1162 }\r
1163 \r
1164 static void end_protected_command(){\r
1165     if(Uci->ready){ // not init faze\r
1166         uci_send_isready_sync(Uci); // gobble up spurious "bestmove"\r
1167     }\r
1168     update_remaining_time();\r
1169     search_update();   // relaunch search if necessary\r
1170 }\r
1171 \r
1172 // update_remaining_time()\r
1173 \r
1174 static void update_remaining_time(){\r
1175    double reduce;\r
1176    if(State->timer->running){\r
1177        my_timer_stop(State->timer);\r
1178        reduce = my_timer_elapsed_real(State->timer);\r
1179        my_log("POLYGLOT reducing remaing time by %f seconds\n",reduce);\r
1180        XB->my_time -= reduce;\r
1181        if(XB->my_time<0.0){\r
1182            XB->my_time=0.0;\r
1183        }\r
1184    }\r
1185 }\r
1186 \r
1187 \r
1188 // search_update()\r
1189 \r
1190 static void search_update() {\r
1191 \r
1192    int move;\r
1193    int move_nb;\r
1194    board_t board[1];\r
1195 \r
1196    ASSERT(!Uci->searching);\r
1197 \r
1198 \r
1199 \r
1200    \r
1201    // launch a new search if needed\r
1202 \r
1203    \r
1204 \r
1205    if (State->state == THINK || State->state == PONDER || State->state == ANALYSE) {\r
1206 \r
1207       // [VdB] moved up as we need the move number\r
1208 \r
1209        game_get_board(Game,Uci->board);\r
1210 \r
1211       // opening book\r
1212 \r
1213        if (State->state == THINK &&\r
1214            option_get_bool(Option,"Book") &&\r
1215            Uci->board->move_nb<option_get_int(Option,"BookDepth")\r
1216            ) {\r
1217 \r
1218 \r
1219          move = book_move(Uci->board,option_get_bool(Option,"BookRandom"));\r
1220 \r
1221          if (move != MoveNone && move_is_legal(move,Uci->board)) {\r
1222 \r
1223             my_log("POLYGLOT *BOOK MOVE*\n");\r
1224 \r
1225             search_clear(); // clears Uci->ponder_move\r
1226             Uci->best_move = move;\r
1227 \r
1228             board_copy(board,Uci->board);\r
1229             move_do(board,move);\r
1230             Uci->ponder_move = book_move(board,FALSE); // expected move = best book move\r
1231 \r
1232             Uci->best_pv[0] = Uci->best_move;\r
1233             Uci->best_pv[1] = Uci->ponder_move; // can be MoveNone\r
1234             Uci->best_pv[2] = MoveNone;\r
1235 \r
1236             comp_move(Uci->best_move);\r
1237 \r
1238             return;\r
1239          }\r
1240       }\r
1241 \r
1242       // engine search\r
1243 \r
1244       my_log("POLYGLOT START SEARCH\n");\r
1245 \r
1246       // options\r
1247 \r
1248       uci_send_option(Uci,"UCI_Chess960","%s",\r
1249                       option_get_bool(Option,"Chess960")?"true":"false");\r
1250 \r
1251       if (option_get_int(Option,"UCIVersion") >= 2) {\r
1252          uci_send_option(Uci,"UCI_Opponent","none none %s %s",(XB->computer)?"computer":"human",XB->name);\r
1253          uci_send_option(Uci,"UCI_AnalyseMode","%s",(XB->analyse)?"true":"false");\r
1254       }\r
1255 \r
1256       uci_send_option(Uci,"Ponder","%s",ponder()?"true":"false");\r
1257 \r
1258       // position\r
1259 \r
1260       move = (State->state == PONDER) ? State->exp_move : MoveNone;\r
1261       send_board(move); // updates Uci->board global variable\r
1262 \r
1263       // search\r
1264 \r
1265       if (State->state == THINK || State->state == PONDER) {\r
1266 \r
1267          engine_send_queue(Engine,"go");\r
1268 \r
1269          if (XB->time_limit) {\r
1270 \r
1271             // fixed time per move\r
1272 \r
1273             engine_send_queue(Engine," movetime %.0f",XB->time_max*1000.0);\r
1274 \r
1275          } else {\r
1276 \r
1277             // time controls\r
1278 \r
1279             if (colour_is_white(Uci->board->turn)) {\r
1280                engine_send_queue(Engine," wtime %.0f btime %.0f",XB->my_time*1000.0,XB->opp_time*1000.0);\r
1281             } else {\r
1282                engine_send_queue(Engine," wtime %.0f btime %.0f",XB->opp_time*1000.0,XB->my_time*1000.0);\r
1283             }\r
1284 \r
1285             if (XB->inc != 0.0) engine_send_queue(Engine," winc %.0f binc %.0f",XB->inc*1000.0,XB->inc*1000.0);\r
1286 \r
1287             if (XB->mps != 0) {\r
1288 \r
1289                move_nb = XB->mps - (Uci->board->move_nb % XB->mps);\r
1290                ASSERT(move_nb>=1&&move_nb<=XB->mps);\r
1291 \r
1292                engine_send_queue(Engine," movestogo %d",move_nb);\r
1293             }\r
1294          }\r
1295 \r
1296          if (XB->depth_limit) engine_send_queue(Engine," depth %d",XB->depth_max);\r
1297 \r
1298          if (State->state == PONDER) engine_send_queue(Engine," ponder");\r
1299 \r
1300          engine_send(Engine,""); // newline\r
1301 \r
1302       } else if (State->state == ANALYSE) {\r
1303 \r
1304          engine_send(Engine,"go infinite");\r
1305 \r
1306       } else {\r
1307 \r
1308          ASSERT(FALSE);\r
1309       }\r
1310 \r
1311       // init search info\r
1312 \r
1313       ASSERT(!Uci->searching);\r
1314 \r
1315       search_clear();\r
1316 \r
1317       Uci->searching = TRUE;\r
1318       Uci->pending_nb++;\r
1319    }\r
1320 }\r
1321 \r
1322 // search_clear()\r
1323 \r
1324 static void search_clear() {\r
1325 \r
1326    uci_clear(Uci);\r
1327 \r
1328    // TODO: MOVE ME\r
1329 \r
1330    my_timer_start(State->timer);//also resets\r
1331 }\r
1332 \r
1333 // active()\r
1334 \r
1335 static bool active() {\r
1336 \r
1337    // position state\r
1338 \r
1339    if (game_status(Game) != PLAYING) return FALSE; // game ended\r
1340 \r
1341    // xboard state\r
1342 \r
1343    if (XB->analyse) return TRUE; // analysing\r
1344    if (!State->computer[White] && !State->computer[Black]) return FALSE; // force mode\r
1345    if (XB->new_hack || XB->result) return FALSE; // unstarted or ended game\r
1346 \r
1347    return TRUE; // playing\r
1348 }\r
1349 \r
1350 // ponder()\r
1351 \r
1352 static bool ponder() {\r
1353 \r
1354     return XB->ponder && (option_get_bool(Option,"CanPonder") ||\r
1355                           option_find(Uci->option,"Ponder"));\r
1356 }\r
1357 // ponder_ok()\r
1358 \r
1359 static bool ponder_ok(int move) {\r
1360    int status;\r
1361    board_t board[1];\r
1362 \r
1363    ASSERT(move==MoveNone||move_is_ok(move));\r
1364 \r
1365    // legal ponder move?\r
1366 \r
1367    if (move == MoveNone) return FALSE;\r
1368 \r
1369    game_get_board(Game,board);\r
1370    if (!move_is_legal(move,board)) return FALSE;\r
1371 \r
1372    // UCI-legal resulting position?\r
1373 \r
1374    game_add_move(Game,move);\r
1375 \r
1376    game_get_board(Game,board);\r
1377    status = game_status(Game);\r
1378 \r
1379    game_rem_move(Game);\r
1380 \r
1381    if (status != PLAYING) return FALSE; // game ended\r
1382 \r
1383    if (option_get_bool(Option,"Book") && is_in_book(board)) {\r
1384       return FALSE;\r
1385    }\r
1386 \r
1387    return TRUE;\r
1388 }\r
1389 \r
1390 // stop_search()\r
1391 \r
1392 static void stop_search() {\r
1393 \r
1394    if (Uci->searching) {\r
1395 \r
1396       ASSERT(Uci->searching);\r
1397       ASSERT(Uci->pending_nb>=1);\r
1398 \r
1399       my_log("POLYGLOT STOP SEARCH\n");\r
1400 \r
1401 /*\r
1402       engine_send(Engine,"stop");\r
1403       Uci->searching = FALSE;\r
1404 */\r
1405 \r
1406       if (option_get_bool(Option,"SyncStop")) {\r
1407          uci_send_stop_sync(Uci);\r
1408       } else {\r
1409          uci_send_stop(Uci);\r
1410       }\r
1411         }\r
1412 }\r
1413 \r
1414 // send_board()\r
1415 \r
1416 static void send_board(int extra_move) {\r
1417 \r
1418    char fen[256];\r
1419    int start, end;\r
1420    board_t board[1];\r
1421    int pos;\r
1422    int move;\r
1423    char string[256];\r
1424 \r
1425    ASSERT(extra_move==MoveNone||move_is_ok(extra_move));\r
1426 \r
1427    ASSERT(!Uci->searching);\r
1428 \r
1429    // init\r
1430 \r
1431    game_get_board(Game,Uci->board);\r
1432    if (extra_move != MoveNone) move_do(Uci->board,extra_move);\r
1433 \r
1434    board_to_fen(Uci->board,fen,256);\r
1435    my_log("POLYGLOT FEN %s\n",fen);\r
1436 \r
1437    ASSERT(board_can_play(Uci->board));\r
1438 \r
1439    // more init\r
1440 \r
1441    start = 0;\r
1442    end = game_pos(Game);\r
1443    ASSERT(end>=start);\r
1444 \r
1445    // position\r
1446 \r
1447    game_get_board_ex(Game,board,start);\r
1448    board_to_fen(board,string,256);\r
1449 \r
1450    engine_send_queue(Engine,"position");\r
1451 \r
1452    if (my_string_equal(string,StartFen)) {\r
1453       engine_send_queue(Engine," startpos");\r
1454    } else {\r
1455       engine_send_queue(Engine," fen %s",string);\r
1456    }\r
1457 \r
1458    // move list\r
1459 \r
1460    if (end > start || extra_move != MoveNone) engine_send_queue(Engine," moves");\r
1461 \r
1462    for (pos = start; pos < end; pos++) { // game moves\r
1463 \r
1464       move = game_move(Game,pos);\r
1465 \r
1466       move_to_can(move,board,string,256);\r
1467       engine_send_queue(Engine," %s",string);\r
1468 \r
1469       move_do(board,move);\r
1470    }\r
1471 \r
1472    if (extra_move != MoveNone) { // move to ponder on\r
1473       move_to_can(extra_move,board,string,256);\r
1474       engine_send_queue(Engine," %s",string);\r
1475    }\r
1476 \r
1477    // end\r
1478 \r
1479    engine_send(Engine,""); // newline\r
1480 }\r
1481 \r
1482 // send_info()\r
1483 \r
1484 static void send_info() {\r
1485     int min_depth;\r
1486     if(option_get_bool(Option,"WbWorkArounds2")){\r
1487             // Silly bug in some versions of WinBoard.\r
1488             // depth <=1 clears the engine output window.\r
1489             // Why shouldn't an engine be allowed to send info at depth 1?\r
1490         min_depth=2;\r
1491     }else{\r
1492         min_depth=1;\r
1493     }\r
1494     gui_send(GUI,"%d %+d %.0f "S64_FORMAT" %s",Uci->best_depth>min_depth?Uci->best_depth:min_depth,\r
1495                  0,0,0.0,0,Uci->info);  \r
1496 }\r
1497 \r
1498 // send_pv()\r
1499 \r
1500 static void send_pv() {\r
1501 \r
1502    char pv_string[StringSize];\r
1503    board_t board[1];\r
1504    int move;\r
1505    char move_string[StringSize];\r
1506 \r
1507    ASSERT(State->state!=WAIT);\r
1508 \r
1509    if (Uci->best_depth == 0) return;\r
1510 \r
1511    // xboard search information\r
1512 \r
1513    if (XB->post) {\r
1514 \r
1515       if (State->state == THINK || State->state == ANALYSE) {\r
1516 \r
1517          line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);\r
1518 \r
1519                  if(Uci->depth==-1) //hack to clear the engine output window\r
1520              gui_send(GUI,"%d %+d %.0f "S64_FORMAT" ",0,report_best_score(),Uci->time*100.0,Uci->node_nb);\r
1521 \r
1522                  gui_send(GUI,"%d %+d %.0f "S64_FORMAT" %s",Uci->best_depth,report_best_score(),Uci->time*100.0,Uci->node_nb,pv_string);\r
1523 \r
1524       } else if (State->state == PONDER &&\r
1525                  option_get_bool(Option,"ShowPonder")) {\r
1526 \r
1527          game_get_board(Game,board);\r
1528          move = State->exp_move;\r
1529 \r
1530          if (move != MoveNone && move_is_legal(move,board)) {\r
1531             move_to_san(move,board,move_string,256);\r
1532             line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);\r
1533             gui_send(GUI,"%d %+d %.0f "S64_FORMAT" (%s) %s",Uci->best_depth,report_best_score(),Uci->time*100.0,Uci->node_nb,move_string,pv_string);\r
1534          }\r
1535       }\r
1536    }\r
1537 \r
1538    // kibitz\r
1539 \r
1540    if ((Uci->searching &&\r
1541         option_get_bool(Option,"KibitzPV") &&\r
1542         Uci->time >= option_get_double(Option,"KibitzDelay"))\r
1543        || (!Uci->searching && option_get_bool(Option,"KibitzMove"))) {\r
1544 \r
1545       if (State->state == THINK || State->state == ANALYSE) {\r
1546 \r
1547          line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);\r
1548          if(kibitz_throttle(Uci->searching)){\r
1549              gui_send(GUI,"%s depth=%d time=%.2f node="S64_FORMAT" speed=%.0f score=%+.2f pv=\"%s\"",option_get_string(Option,"KibitzCommand"),Uci->best_depth,Uci->time,Uci->node_nb,Uci->speed,((double)report_best_score())/100.0,pv_string);\r
1550          }\r
1551       } else if (State->state == PONDER) {\r
1552 \r
1553          game_get_board(Game,board);\r
1554          move = State->exp_move;\r
1555 \r
1556          if (move != MoveNone && move_is_legal(move,board)) {\r
1557             move_to_san(move,board,move_string,256);\r
1558             line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);\r
1559             if(kibitz_throttle(Uci->searching)){\r
1560                 gui_send(GUI,"%s depth=%d time=%.2f node="S64_FORMAT" speed=%.0f score=%+.2f pv=\"(%s) %s\"",option_get_string(Option,"KibitzCommand"),Uci->best_depth,Uci->time,Uci->node_nb,Uci->speed,((double)report_best_score())/100.0,move_string,pv_string);\r
1561             }\r
1562          }\r
1563       }\r
1564    }\r
1565 }\r
1566 \r
1567 // kibitz_throttle()\r
1568 \r
1569 static bool kibitz_throttle(bool searching){\r
1570     time_t curr_time;\r
1571     static time_t lastKibitzMove=0;\r
1572     static time_t lastKibitzPV=0;\r
1573     curr_time = time(NULL);\r
1574     if(searching){   // KibitzPV\r
1575         if(curr_time >=\r
1576            (option_get_int(Option,"KibitzInterval") + lastKibitzPV)){\r
1577             lastKibitzPV=curr_time;\r
1578             return TRUE;\r
1579         }\r
1580     }else{       // KibitzMove\r
1581         if(curr_time >=\r
1582            (option_get_int(Option,"KibitzInterval") + lastKibitzMove)){\r
1583             lastKibitzPV=curr_time;\r
1584             lastKibitzMove=curr_time;\r
1585             return TRUE;\r
1586         }        \r
1587     }\r
1588     return FALSE;\r
1589 }\r
1590 \r
1591 // learn()\r
1592 \r
1593 static void learn(int result) {\r
1594 \r
1595    int pos;\r
1596    board_t board[1];\r
1597    int move;\r
1598 \r
1599    ASSERT(result>=-1&&result<=+1);\r
1600 \r
1601    ASSERT(XB->result);\r
1602    ASSERT(State->computer[White]||State->computer[Black]);\r
1603 \r
1604    // init\r
1605 \r
1606    pos = 0;\r
1607 \r
1608    if (FALSE) {\r
1609    } else if (State->computer[White]) {\r
1610       pos = 0;\r
1611    } else if (State->computer[Black]) {\r
1612       pos = 1;\r
1613       result = -result;\r
1614    } else {\r
1615       my_fatal("learn(): unknown side\n");\r
1616    }\r
1617 \r
1618    if (FALSE) {\r
1619    } else if (result > 0) {\r
1620       my_log("POLYGLOT *LEARN WIN*\n");\r
1621    } else if (result < 0) {\r
1622       my_log("POLYGLOT *LEARN LOSS*\n");\r
1623    } else {\r
1624       my_log("POLYGLOT *LEARN DRAW*\n");\r
1625    }\r
1626 \r
1627    // loop\r
1628 \r
1629    for (; pos < Game->size; pos += 2) {\r
1630 \r
1631       game_get_board_ex(Game,board,pos);\r
1632       move = game_move(Game,pos);\r
1633 \r
1634       book_learn_move(board,move,result);\r
1635    }\r
1636 \r
1637    book_flush();\r
1638 }\r
1639 \r
1640 // end of xboard2uci.c\r