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