version 1.4.36b
[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,"Ponder")) continue;\r
862         if(my_string_case_equal(opt->name,"Hash")) continue;\r
863         if(my_string_case_equal(opt->name,"NalimovPath")) continue;\r
864         if((name=uci_thread_option(Uci))!=NULL &&\r
865            my_string_case_equal(opt->name,name)) continue;\r
866         \r
867         format_xboard_option_line(option_line,opt);\r
868         \r
869         gui_send(GUI,"%s",option_line);\r
870     }\r
871 \r
872 \r
873     option_start_iter(Option);\r
874     while((opt=option_next(Option))){\r
875         if(opt->mode &XBOARD){\r
876             format_xboard_option_line(option_line,opt);\r
877             gui_send(GUI,"%s",option_line);\r
878         }\r
879     }       \r
880     gui_send(GUI,"feature done=1"); \r
881     \r
882 }\r
883 \r
884 // report_best_score()\r
885 \r
886 static int report_best_score(){\r
887     if(!option_get_bool(Option,"ScoreWhite") ||\r
888        colour_is_white(Uci->board->turn)){\r
889         return Uci->best_score;\r
890     }else{\r
891         return -Uci->best_score;\r
892     }\r
893 }\r
894 \r
895 // comp_move()\r
896 \r
897 static void comp_move(int move) {\r
898 \r
899    board_t board[1];\r
900    char string[256];\r
901 \r
902    ASSERT(move_is_ok(move));\r
903 \r
904    ASSERT(State->state==THINK);\r
905    ASSERT(!XB->analyse);\r
906 \r
907    if(option_get_bool(Option,"RepeatPV"))\r
908            send_pv(); // to update time and nodes\r
909 \r
910    // send the move\r
911 \r
912    game_get_board(Game,board);\r
913 \r
914    if (move_is_castle(move,board) && option_get_bool(Option,"Chess960")) {\r
915       if (!move_to_san(move,board,string,256)) my_fatal("comp_move(): move_to_san() failed\n"); // O-O/O-O-O\r
916    } else {\r
917       if (!move_to_can(move,board,string,256)) my_fatal("comp_move(): move_to_can() failed\n");\r
918    }\r
919 \r
920    gui_send(GUI,"move %s",string);\r
921 \r
922    // resign?\r
923 \r
924    if (option_get_bool(Option,"Resign") && Uci->root_move_nb > 1) {\r
925 \r
926        if (Uci->best_score <= -abs(option_get_int(Option,"ResignScore"))) {\r
927 \r
928          State->resign_nb++;\r
929          my_log("POLYGLOT %d move%s with resign score\n",State->resign_nb,(State->resign_nb>1)?"s":"");\r
930 \r
931          if (State->resign_nb >= option_get_int(Option,"ResignMoves")) {\r
932             my_log("POLYGLOT *** RESIGN ***\n");\r
933             gui_send(GUI,"resign");\r
934          }\r
935 \r
936       } else {\r
937 \r
938          if (State->resign_nb > 0) my_log("POLYGLOT resign reset (State->resign_nb=%d)\n",State->resign_nb);\r
939          State->resign_nb = 0;\r
940       }\r
941    }\r
942 \r
943    // play the move\r
944 \r
945    move_step(move);\r
946    no_mess(move);\r
947 }\r
948 \r
949 // move_step()\r
950 \r
951 static void move_step(int move) {\r
952 \r
953    board_t board[1];\r
954    char move_string[256];\r
955 \r
956    ASSERT(move_is_ok(move));\r
957 \r
958    // log\r
959 \r
960    game_get_board(Game,board);\r
961 \r
962    if (move != MoveNone && move_is_legal(move,board)) {\r
963 \r
964       move_to_san(move,board,move_string,256);\r
965       my_log("POLYGLOT MOVE %s\n",move_string);\r
966 \r
967    } else {\r
968 \r
969       move_to_can(move,board,move_string,256);\r
970       my_log("POLYGLOT ILLEGAL MOVE \"%s\"\n",move_string);\r
971       board_disp(board);\r
972 \r
973       my_fatal("move_step(): illegal move \"%s\"\n",move_string);\r
974    }\r
975 \r
976    // play the move\r
977 \r
978    game_add_move(Game,move);\r
979    board_update();\r
980 }\r
981 \r
982 // board_update()\r
983 \r
984 static void board_update() {\r
985 \r
986    // handle game end\r
987 \r
988    ASSERT(!XB->result);\r
989 \r
990    switch (game_status(Game)) {\r
991    case PLAYING:\r
992       break;\r
993    case WHITE_MATES:\r
994       gui_send(GUI,"1-0 {White mates}");\r
995       break;\r
996    case BLACK_MATES:\r
997       gui_send(GUI,"0-1 {Black mates}");\r
998       break;\r
999    case STALEMATE:\r
1000       gui_send(GUI,"1/2-1/2 {Stalemate}");\r
1001       break;\r
1002    case DRAW_MATERIAL:\r
1003       gui_send(GUI,"1/2-1/2 {Draw by insufficient material}");\r
1004       break;\r
1005    case DRAW_FIFTY:\r
1006       gui_send(GUI,"1/2-1/2 {Draw by fifty-move rule}");\r
1007       break;\r
1008    case DRAW_REPETITION:\r
1009       gui_send(GUI,"1/2-1/2 {Draw by repetition}");\r
1010       break;\r
1011    default:\r
1012       ASSERT(FALSE);\r
1013       break;\r
1014    }\r
1015 }\r
1016 \r
1017 // mess()\r
1018 \r
1019 static void mess() {\r
1020 \r
1021    // clear state variables\r
1022 \r
1023    State->resign_nb = 0;\r
1024    State->exp_move = MoveNone;\r
1025    my_timer_reset(State->timer);\r
1026 \r
1027    // abort a possible search\r
1028 \r
1029    stop_search();\r
1030 \r
1031    // calculate the new state\r
1032 \r
1033    if (FALSE) {\r
1034    } else if (!active()) {\r
1035       State->state = WAIT;\r
1036       my_log("POLYGLOT WAIT\n");\r
1037    } else if (XB->analyse) {\r
1038       State->state = ANALYSE;\r
1039       my_log("POLYGLOT ANALYSE\n");\r
1040    } else if (State->computer[game_turn(Game)]) {\r
1041       State->state = THINK;\r
1042       my_log("POLYGLOT THINK\n");\r
1043    } else {\r
1044       State->state = WAIT;\r
1045       my_log("POLYGLOT WAIT\n");\r
1046    }\r
1047 \r
1048    search_update();\r
1049 }\r
1050 \r
1051 // no_mess()\r
1052 \r
1053 static void no_mess(int move) {\r
1054 \r
1055    ASSERT(move_is_ok(move));\r
1056 \r
1057    // just received a move, calculate the new state\r
1058 \r
1059    if (FALSE) {\r
1060 \r
1061    } else if (!active()) {\r
1062 \r
1063       stop_search(); // abort a possible search\r
1064 \r
1065       State->state = WAIT;\r
1066       State->exp_move = MoveNone;\r
1067 \r
1068       my_log("POLYGLOT WAIT\n");\r
1069 \r
1070    } else if (State->state == WAIT) {\r
1071 \r
1072       ASSERT(State->computer[game_turn(Game)]);\r
1073       ASSERT(!State->computer[colour_opp(game_turn(Game))]);\r
1074       ASSERT(!XB->analyse);\r
1075 \r
1076       my_log("POLYGLOT WAIT -> THINK\n");\r
1077 \r
1078       State->state = THINK;\r
1079       State->exp_move = MoveNone;\r
1080 \r
1081    } else if (State->state == THINK) {\r
1082 \r
1083       ASSERT(!State->computer[game_turn(Game)]);\r
1084       ASSERT(State->computer[colour_opp(game_turn(Game))]);\r
1085       ASSERT(!XB->analyse);\r
1086 \r
1087       if (ponder() && ponder_ok(Uci->ponder_move)) {\r
1088 \r
1089          my_log("POLYGLOT THINK -> PONDER\n");\r
1090 \r
1091          State->state = PONDER;\r
1092          State->exp_move = Uci->ponder_move;\r
1093 \r
1094       } else {\r
1095 \r
1096          my_log("POLYGLOT THINK -> WAIT\n");\r
1097 \r
1098          State->state = WAIT;\r
1099          State->exp_move = MoveNone;\r
1100       }\r
1101 \r
1102    } else if (State->state == PONDER) {\r
1103 \r
1104       ASSERT(State->computer[game_turn(Game)]);\r
1105       ASSERT(!State->computer[colour_opp(game_turn(Game))]);\r
1106       ASSERT(!XB->analyse);\r
1107 \r
1108       if (move == State->exp_move && Uci->searching) {\r
1109 \r
1110          ASSERT(Uci->searching);\r
1111          ASSERT(Uci->pending_nb>=1);\r
1112 \r
1113          my_timer_start(State->timer);//also resets\r
1114 \r
1115          my_log("POLYGLOT PONDER -> THINK (*** HIT ***)\n");\r
1116          engine_send(Engine,"ponderhit");\r
1117 \r
1118          State->state = THINK;\r
1119          State->exp_move = MoveNone;\r
1120 \r
1121          send_pv(); // update display\r
1122 \r
1123          return; // do not launch a new search\r
1124 \r
1125       } else {\r
1126 \r
1127          my_log("POLYGLOT PONDER -> THINK (miss)\n");\r
1128 \r
1129          stop_search();\r
1130 \r
1131          State->state = THINK;\r
1132          State->exp_move = MoveNone;\r
1133       }\r
1134 \r
1135    } else if (State->state == ANALYSE) {\r
1136 \r
1137       ASSERT(XB->analyse);\r
1138 \r
1139       my_log("POLYGLOT ANALYSE -> ANALYSE\n");\r
1140 \r
1141       stop_search();\r
1142 \r
1143    } else {\r
1144 \r
1145       ASSERT(FALSE);\r
1146    }\r
1147 \r
1148    search_update();\r
1149 }\r
1150 \r
1151 // start_protected_command()\r
1152 \r
1153 static void start_protected_command(){\r
1154     stop_search();\r
1155 }\r
1156 \r
1157 static void end_protected_command(){\r
1158     if(Uci->ready){ // not init faze\r
1159         uci_send_isready_sync(Uci); // gobble up spurious "bestmove"\r
1160     }\r
1161     update_remaining_time();\r
1162     search_update();   // relaunch search if necessary\r
1163 }\r
1164 \r
1165 // update_remaining_time()\r
1166 \r
1167 static void update_remaining_time(){\r
1168    double reduce;\r
1169    if(State->timer->running){\r
1170        my_timer_stop(State->timer);\r
1171        reduce = my_timer_elapsed_real(State->timer);\r
1172        my_log("POLYGLOT reducing remaing time by %f seconds\n",reduce);\r
1173        XB->my_time -= reduce;\r
1174        if(XB->my_time<0.0){\r
1175            XB->my_time=0.0;\r
1176        }\r
1177    }\r
1178 }\r
1179 \r
1180 \r
1181 // search_update()\r
1182 \r
1183 static void search_update() {\r
1184 \r
1185    int move;\r
1186    int move_nb;\r
1187    board_t board[1];\r
1188 \r
1189    ASSERT(!Uci->searching);\r
1190 \r
1191 \r
1192 \r
1193    \r
1194    // launch a new search if needed\r
1195 \r
1196    \r
1197 \r
1198    if (State->state == THINK || State->state == PONDER || State->state == ANALYSE) {\r
1199 \r
1200       // opening book\r
1201 \r
1202        if (State->state == THINK && option_get_bool(Option,"Book")) {\r
1203 \r
1204          game_get_board(Game,Uci->board);\r
1205 \r
1206          move = book_move(Uci->board,option_get_bool(Option,"BookRandom"));\r
1207 \r
1208          if (move != MoveNone && move_is_legal(move,Uci->board)) {\r
1209 \r
1210             my_log("POLYGLOT *BOOK MOVE*\n");\r
1211 \r
1212             search_clear(); // clears Uci->ponder_move\r
1213             Uci->best_move = move;\r
1214 \r
1215             board_copy(board,Uci->board);\r
1216             move_do(board,move);\r
1217             Uci->ponder_move = book_move(board,FALSE); // expected move = best book move\r
1218 \r
1219             Uci->best_pv[0] = Uci->best_move;\r
1220             Uci->best_pv[1] = Uci->ponder_move; // can be MoveNone\r
1221             Uci->best_pv[2] = MoveNone;\r
1222 \r
1223             comp_move(Uci->best_move);\r
1224 \r
1225             return;\r
1226          }\r
1227       }\r
1228 \r
1229       // engine search\r
1230 \r
1231       my_log("POLYGLOT START SEARCH\n");\r
1232 \r
1233       // options\r
1234 \r
1235       uci_send_option(Uci,"UCI_Chess960","%s",\r
1236                       option_get_bool(Option,"Chess960")?"true":"false");\r
1237 \r
1238       if (option_get_int(Option,"UCIVersion") >= 2) {\r
1239          uci_send_option(Uci,"UCI_Opponent","none none %s %s",(XB->computer)?"computer":"human",XB->name);\r
1240          uci_send_option(Uci,"UCI_AnalyseMode","%s",(XB->analyse)?"true":"false");\r
1241       }\r
1242 \r
1243       uci_send_option(Uci,"Ponder","%s",ponder()?"true":"false");\r
1244 \r
1245       // position\r
1246 \r
1247       move = (State->state == PONDER) ? State->exp_move : MoveNone;\r
1248       send_board(move); // updates Uci->board global variable\r
1249 \r
1250       // search\r
1251 \r
1252       if (State->state == THINK || State->state == PONDER) {\r
1253 \r
1254          engine_send_queue(Engine,"go");\r
1255 \r
1256          if (XB->time_limit) {\r
1257 \r
1258             // fixed time per move\r
1259 \r
1260             engine_send_queue(Engine," movetime %.0f",XB->time_max*1000.0);\r
1261 \r
1262          } else {\r
1263 \r
1264             // time controls\r
1265 \r
1266             if (colour_is_white(Uci->board->turn)) {\r
1267                engine_send_queue(Engine," wtime %.0f btime %.0f",XB->my_time*1000.0,XB->opp_time*1000.0);\r
1268             } else {\r
1269                engine_send_queue(Engine," wtime %.0f btime %.0f",XB->opp_time*1000.0,XB->my_time*1000.0);\r
1270             }\r
1271 \r
1272             if (XB->inc != 0.0) engine_send_queue(Engine," winc %.0f binc %.0f",XB->inc*1000.0,XB->inc*1000.0);\r
1273 \r
1274             if (XB->mps != 0) {\r
1275 \r
1276                move_nb = XB->mps - (Uci->board->move_nb % XB->mps);\r
1277                ASSERT(move_nb>=1&&move_nb<=XB->mps);\r
1278 \r
1279                engine_send_queue(Engine," movestogo %d",move_nb);\r
1280             }\r
1281          }\r
1282 \r
1283          if (XB->depth_limit) engine_send_queue(Engine," depth %d",XB->depth_max);\r
1284 \r
1285          if (State->state == PONDER) engine_send_queue(Engine," ponder");\r
1286 \r
1287          engine_send(Engine,""); // newline\r
1288 \r
1289       } else if (State->state == ANALYSE) {\r
1290 \r
1291          engine_send(Engine,"go infinite");\r
1292 \r
1293       } else {\r
1294 \r
1295          ASSERT(FALSE);\r
1296       }\r
1297 \r
1298       // init search info\r
1299 \r
1300       ASSERT(!Uci->searching);\r
1301 \r
1302       search_clear();\r
1303 \r
1304       Uci->searching = TRUE;\r
1305       Uci->pending_nb++;\r
1306    }\r
1307 }\r
1308 \r
1309 // search_clear()\r
1310 \r
1311 static void search_clear() {\r
1312 \r
1313    uci_clear(Uci);\r
1314 \r
1315    // TODO: MOVE ME\r
1316 \r
1317    my_timer_start(State->timer);//also resets\r
1318 }\r
1319 \r
1320 // active()\r
1321 \r
1322 static bool active() {\r
1323 \r
1324    // position state\r
1325 \r
1326    if (game_status(Game) != PLAYING) return FALSE; // game ended\r
1327 \r
1328    // xboard state\r
1329 \r
1330    if (XB->analyse) return TRUE; // analysing\r
1331    if (!State->computer[White] && !State->computer[Black]) return FALSE; // force mode\r
1332    if (XB->new_hack || XB->result) return FALSE; // unstarted or ended game\r
1333 \r
1334    return TRUE; // playing\r
1335 }\r
1336 \r
1337 // ponder()\r
1338 \r
1339 static bool ponder() {\r
1340 \r
1341     return XB->ponder && (option_get_bool(Option,"CanPonder") ||\r
1342                           option_find(Uci->option,"Ponder"));\r
1343 }\r
1344 // ponder_ok()\r
1345 \r
1346 static bool ponder_ok(int move) {\r
1347    int status;\r
1348    board_t board[1];\r
1349 \r
1350    ASSERT(move==MoveNone||move_is_ok(move));\r
1351 \r
1352    // legal ponder move?\r
1353 \r
1354    if (move == MoveNone) return FALSE;\r
1355 \r
1356    game_get_board(Game,board);\r
1357    if (!move_is_legal(move,board)) return FALSE;\r
1358 \r
1359    // UCI-legal resulting position?\r
1360 \r
1361    game_add_move(Game,move);\r
1362 \r
1363    game_get_board(Game,board);\r
1364    status = game_status(Game);\r
1365 \r
1366    game_rem_move(Game);\r
1367 \r
1368    if (status != PLAYING) return FALSE; // game ended\r
1369 \r
1370    if (option_get_bool(Option,"Book") && is_in_book(board)) {\r
1371       return FALSE;\r
1372    }\r
1373 \r
1374    return TRUE;\r
1375 }\r
1376 \r
1377 // stop_search()\r
1378 \r
1379 static void stop_search() {\r
1380 \r
1381    if (Uci->searching) {\r
1382 \r
1383       ASSERT(Uci->searching);\r
1384       ASSERT(Uci->pending_nb>=1);\r
1385 \r
1386       my_log("POLYGLOT STOP SEARCH\n");\r
1387 \r
1388 /*\r
1389       engine_send(Engine,"stop");\r
1390       Uci->searching = FALSE;\r
1391 */\r
1392 \r
1393       if (option_get_bool(Option,"SyncStop")) {\r
1394          uci_send_stop_sync(Uci);\r
1395       } else {\r
1396          uci_send_stop(Uci);\r
1397       }\r
1398         }\r
1399 }\r
1400 \r
1401 // send_board()\r
1402 \r
1403 static void send_board(int extra_move) {\r
1404 \r
1405    char fen[256];\r
1406    int start, end;\r
1407    board_t board[1];\r
1408    int pos;\r
1409    int move;\r
1410    char string[256];\r
1411 \r
1412    ASSERT(extra_move==MoveNone||move_is_ok(extra_move));\r
1413 \r
1414    ASSERT(!Uci->searching);\r
1415 \r
1416    // init\r
1417 \r
1418    game_get_board(Game,Uci->board);\r
1419    if (extra_move != MoveNone) move_do(Uci->board,extra_move);\r
1420 \r
1421    board_to_fen(Uci->board,fen,256);\r
1422    my_log("POLYGLOT FEN %s\n",fen);\r
1423 \r
1424    ASSERT(board_can_play(Uci->board));\r
1425 \r
1426    // more init\r
1427 \r
1428    start = 0;\r
1429    end = game_pos(Game);\r
1430    ASSERT(end>=start);\r
1431 \r
1432    // position\r
1433 \r
1434    game_get_board_ex(Game,board,start);\r
1435    board_to_fen(board,string,256);\r
1436 \r
1437    engine_send_queue(Engine,"position");\r
1438 \r
1439    if (my_string_equal(string,StartFen)) {\r
1440       engine_send_queue(Engine," startpos");\r
1441    } else {\r
1442       engine_send_queue(Engine," fen %s",string);\r
1443    }\r
1444 \r
1445    // move list\r
1446 \r
1447    if (end > start || extra_move != MoveNone) engine_send_queue(Engine," moves");\r
1448 \r
1449    for (pos = start; pos < end; pos++) { // game moves\r
1450 \r
1451       move = game_move(Game,pos);\r
1452 \r
1453       move_to_can(move,board,string,256);\r
1454       engine_send_queue(Engine," %s",string);\r
1455 \r
1456       move_do(board,move);\r
1457    }\r
1458 \r
1459    if (extra_move != MoveNone) { // move to ponder on\r
1460       move_to_can(extra_move,board,string,256);\r
1461       engine_send_queue(Engine," %s",string);\r
1462    }\r
1463 \r
1464    // end\r
1465 \r
1466    engine_send(Engine,""); // newline\r
1467 }\r
1468 \r
1469 // send_info()\r
1470 \r
1471 static void send_info() {\r
1472     int min_depth;\r
1473     if(option_get_bool(Option,"WbWorkArounds2")){\r
1474             // Silly bug in some versions of WinBoard.\r
1475             // depth <=1 clears the engine output window.\r
1476             // Why shouldn't an engine be allowed to send info at depth 1?\r
1477         min_depth=2;\r
1478     }else{\r
1479         min_depth=1;\r
1480     }\r
1481     gui_send(GUI,"%d %+d %.0f "S64_FORMAT" %s",Uci->best_depth>min_depth?Uci->best_depth:min_depth,\r
1482                  0,0,0.0,0,Uci->info);  \r
1483 }\r
1484 \r
1485 // send_pv()\r
1486 \r
1487 static void send_pv() {\r
1488 \r
1489    char pv_string[StringSize];\r
1490    board_t board[1];\r
1491    int move;\r
1492    char move_string[StringSize];\r
1493 \r
1494    ASSERT(State->state!=WAIT);\r
1495 \r
1496    if (Uci->best_depth == 0) return;\r
1497 \r
1498    // xboard search information\r
1499 \r
1500    if (XB->post) {\r
1501 \r
1502       if (State->state == THINK || State->state == ANALYSE) {\r
1503 \r
1504          line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);\r
1505 \r
1506                  if(Uci->depth==-1) //hack to clear the engine output window\r
1507              gui_send(GUI,"%d %+d %.0f "S64_FORMAT" ",0,report_best_score(),Uci->time*100.0,Uci->node_nb);\r
1508 \r
1509                  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
1510 \r
1511       } else if (State->state == PONDER &&\r
1512                  option_get_bool(Option,"ShowPonder")) {\r
1513 \r
1514          game_get_board(Game,board);\r
1515          move = State->exp_move;\r
1516 \r
1517          if (move != MoveNone && move_is_legal(move,board)) {\r
1518             move_to_san(move,board,move_string,256);\r
1519             line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);\r
1520             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
1521          }\r
1522       }\r
1523    }\r
1524 \r
1525    // kibitz\r
1526 \r
1527    if ((Uci->searching &&\r
1528         option_get_bool(Option,"KibitzPV") &&\r
1529         Uci->time >= option_get_double(Option,"KibitzDelay"))\r
1530        || (!Uci->searching && option_get_bool(Option,"KibitzMove"))) {\r
1531 \r
1532       if (State->state == THINK || State->state == ANALYSE) {\r
1533 \r
1534          line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);\r
1535          if(kibitz_throttle(Uci->searching)){\r
1536              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
1537          }\r
1538       } else if (State->state == PONDER) {\r
1539 \r
1540          game_get_board(Game,board);\r
1541          move = State->exp_move;\r
1542 \r
1543          if (move != MoveNone && move_is_legal(move,board)) {\r
1544             move_to_san(move,board,move_string,256);\r
1545             line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);\r
1546             if(kibitz_throttle(Uci->searching)){\r
1547                 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
1548             }\r
1549          }\r
1550       }\r
1551    }\r
1552 }\r
1553 \r
1554 // kibitz_throttle()\r
1555 \r
1556 static bool kibitz_throttle(bool searching){\r
1557     time_t curr_time;\r
1558     static time_t lastKibitzMove=0;\r
1559     static time_t lastKibitzPV=0;\r
1560     curr_time = time(NULL);\r
1561     if(searching){   // KibitzPV\r
1562         if(curr_time >=\r
1563            (option_get_int(Option,"KibitzInterval") + lastKibitzPV)){\r
1564             lastKibitzPV=curr_time;\r
1565             return TRUE;\r
1566         }\r
1567     }else{       // KibitzMove\r
1568         if(curr_time >=\r
1569            (option_get_int(Option,"KibitzInterval") + lastKibitzMove)){\r
1570             lastKibitzPV=curr_time;\r
1571             lastKibitzMove=curr_time;\r
1572             return TRUE;\r
1573         }        \r
1574     }\r
1575     return FALSE;\r
1576 }\r
1577 \r
1578 // learn()\r
1579 \r
1580 static void learn(int result) {\r
1581 \r
1582    int pos;\r
1583    board_t board[1];\r
1584    int move;\r
1585 \r
1586    ASSERT(result>=-1&&result<=+1);\r
1587 \r
1588    ASSERT(XB->result);\r
1589    ASSERT(State->computer[White]||State->computer[Black]);\r
1590 \r
1591    // init\r
1592 \r
1593    pos = 0;\r
1594 \r
1595    if (FALSE) {\r
1596    } else if (State->computer[White]) {\r
1597       pos = 0;\r
1598    } else if (State->computer[Black]) {\r
1599       pos = 1;\r
1600       result = -result;\r
1601    } else {\r
1602       my_fatal("learn(): unknown side\n");\r
1603    }\r
1604 \r
1605    if (FALSE) {\r
1606    } else if (result > 0) {\r
1607       my_log("POLYGLOT *LEARN WIN*\n");\r
1608    } else if (result < 0) {\r
1609       my_log("POLYGLOT *LEARN LOSS*\n");\r
1610    } else {\r
1611       my_log("POLYGLOT *LEARN DRAW*\n");\r
1612    }\r
1613 \r
1614    // loop\r
1615 \r
1616    for (; pos < Game->size; pos += 2) {\r
1617 \r
1618       game_get_board_ex(Game,board,pos);\r
1619       move = game_move(Game,pos);\r
1620 \r
1621       book_learn_move(board,move,result);\r
1622    }\r
1623 \r
1624    book_flush();\r
1625 }\r
1626 \r
1627 // end of xboard2uci.c\r