version 1.4.35b
[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                 start_protected_command();\r
458                 if(!uci_send_option(Uci, name, "%s", value)){\r
459                     gui_send(GUI,"Error (unknown option): %s",name); \r
460                 }\r
461                 end_protected_command();\r
462             }\r
463         } else if (match(string,"option *")){\r
464             char *name=Star[0];\r
465             start_protected_command();\r
466                 // value is ignored\r
467             if(!uci_send_option(Uci, name, "%s", "<empty>")){\r
468                gui_send(GUI,"Error (unknown option): %s",name); \r
469             }; \r
470             end_protected_command();\r
471         } else if (XB->has_feature_smp && match(string,"cores *")){\r
472                 int cores=atoi(Star[0]);\r
473                 if(cores>=1){\r
474                     // updating the number of cores\r
475                     my_log("POLYGLOT setting the number of cores to %d\n",cores);\r
476                     start_protected_command();\r
477                     uci_set_threads(Uci,cores); \r
478                     end_protected_command();\r
479                 } else{\r
480                    // refuse\r
481                     gui_send(GUI,"Error (unknown command): %s",string);\r
482                 }\r
483         } else if (XB->has_feature_egt && match(string,"egtpath * *")){\r
484                 char *type=Star[0];\r
485                 char *path=Star[1];\r
486                 if(!my_string_case_equal(type,"nalimov")){\r
487                    // refuse\r
488                     gui_send(GUI,"Error (unsupported table base format): %s",string);\r
489                 }else if(my_string_empty(path)){\r
490                     // refuse\r
491                     gui_send(GUI,"Error (unknown command): %s",string);\r
492                 }else{\r
493                     // updating NalimovPath\r
494                     my_log("POLYGLOT setting the Nalimov path to %s\n",path);\r
495                     start_protected_command();\r
496                     uci_send_option(Uci,"NalimovPath","%s",path);\r
497                     end_protected_command();\r
498                 }\r
499         } else if (XB->has_feature_memory && match(string,"memory *")){\r
500             int memory = atoi(Star[0]);\r
501             int nalimov_cache;\r
502             int real_memory;\r
503             if(memory>=1){\r
504                 // updating the available memory\r
505                 option_t *opt;\r
506                 my_log("POLYGLOT setting the amount of memory to %dMb\n",memory);\r
507                 if((opt=option_find(Uci->option,"NalimovCache"))){\r
508                     nalimov_cache=atoi(opt->value);\r
509                 }else{\r
510                     nalimov_cache=0;\r
511                 }\r
512                 my_log("POLYGLOT Nalimov Cache is %dMb\n",nalimov_cache);\r
513                 real_memory=memory-nalimov_cache;\r
514                 if(real_memory>0){\r
515                     start_protected_command();\r
516                     uci_send_option(Uci,"Hash", "%d", real_memory);\r
517                     end_protected_command();\r
518                 }\r
519             }else{\r
520                 // refuse\r
521                 gui_send(GUI,"Error (unknown command): %s",string);\r
522             }\r
523 \r
524                 } else if (match(string,"sd *")) {\r
525 \r
526                         XB->depth_limit = TRUE;\r
527                         XB->depth_max = atoi(Star[0]);\r
528 \r
529                 } else if (match(string,"setboard *")) {\r
530 \r
531                         my_log("POLYGLOT FEN %s\n",Star[0]);\r
532 \r
533                         if (!game_init(Game,Star[0])) my_fatal("xboard_step(): bad FEN \"%s\"\n",Star[0]);\r
534 \r
535                         State->computer[White] = FALSE;\r
536                         State->computer[Black] = FALSE;\r
537 \r
538                         XB->new_hack = TRUE; // HACK?\r
539                         XB->result = FALSE;\r
540 \r
541                         board_update();\r
542                         mess();\r
543 \r
544                 } else if (match(string,"st *")) {\r
545 \r
546                         XB->time_limit = TRUE;\r
547                         XB->time_max = ((double)atoi(Star[0]));\r
548 \r
549                 } else if (match(string,"time *")) {\r
550 \r
551                         XB->my_time = ((double)atoi(Star[0])) / 100.0;\r
552                         if (XB->my_time < 0.0) XB->my_time = 0.0;\r
553 \r
554                 } else if (match(string,"undo")) {\r
555 \r
556                         if (game_pos(Game) >= 1) {\r
557 \r
558                                 game_goto(Game,game_pos(Game)-1);\r
559 \r
560                                 ASSERT(!XB->new_hack);\r
561                                 XB->new_hack = FALSE; // HACK?\r
562                                 XB->result = FALSE;\r
563 \r
564                                 board_update();\r
565                                 mess();\r
566                         }\r
567 \r
568                 } else if (match(string,"usermove *")) {\r
569 \r
570                         game_get_board(Game,board);\r
571                         move = move_from_san(Star[0],board);\r
572 \r
573                         if (move != MoveNone && move_is_legal(move,board)) {\r
574 \r
575                                 XB->new_hack = FALSE;\r
576                                 ASSERT(!XB->result);\r
577                                 XB->result = FALSE;\r
578 \r
579                                 move_step(move);\r
580                                 no_mess(move);\r
581 \r
582                         } else {\r
583 \r
584                                 gui_send(GUI,"Illegal move: %s",Star[0]);\r
585                         }\r
586 \r
587                 } else if (match(string,"variant *")) {\r
588 \r
589                         if (my_string_equal(Star[0],"fischerandom")) {\r
590                                 option_set(Option,"Chess960","true");\r
591                         } else {\r
592                                 option_set(Option,"Chess960","false");\r
593                         }\r
594 \r
595                 } else if (match(string,"white")) {\r
596 \r
597                         if (colour_is_white(game_turn(Game))) {\r
598 \r
599                                 State->computer[White] = FALSE;\r
600                                 State->computer[Black] = TRUE;\r
601 \r
602                                 XB->new_hack = TRUE;\r
603                                 XB->result = FALSE;\r
604 \r
605                                 mess();\r
606                         }\r
607 \r
608                 } else if (match(string,"xboard")) {\r
609 \r
610                         // ignore\r
611 \r
612                 } else if (match(string,".")) { // analyse info\r
613 \r
614                         if (State->state == ANALYSE) {\r
615                                 int depth=Uci->best_depth;//HACK: don't clear engine-output window...\r
616 \r
617                                 ASSERT(Uci->searching);\r
618                                 ASSERT(Uci->pending_nb>=1);\r
619 \r
620                                 if (Uci->root_move != MoveNone && move_is_legal(Uci->root_move,Uci->board)) {\r
621                                         move_to_san(Uci->root_move,Uci->board,move_string,256);\r
622                                         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
623                                 } else {\r
624                                         gui_send(GUI,"stat01: %.0f "S64_FORMAT" %d %d %d",Uci->time*100.0,Uci->node_nb,/*Uci->*/depth,0,0); // HACK\r
625                                 }\r
626                         }\r
627 \r
628                 } else if (match(string,"?")) { // move now\r
629 \r
630                         if (State->state == THINK) {\r
631 \r
632                                 ASSERT(Uci->searching);\r
633                                 ASSERT(Uci->pending_nb>=1);\r
634 \r
635                                 // HACK: just send "stop" to the engine\r
636 \r
637                                 if (Uci->searching) {\r
638                                         my_log("POLYGLOT STOP SEARCH\n");\r
639                                         engine_send(Engine,"stop");\r
640                                 }\r
641                         }\r
642 \r
643                 } else { // unknown command, maybe a move?\r
644 \r
645                         game_get_board(Game,board);\r
646                         move = move_from_san(string,board);\r
647 \r
648                         if (move != MoveNone && move_is_legal(move,board)) {\r
649 \r
650                                 XB->new_hack = FALSE;\r
651                                 ASSERT(!XB->result);\r
652                                 XB->result = FALSE;\r
653 \r
654                                 move_step(move);\r
655                                 no_mess(move);\r
656 \r
657                         } else if (move != MoveNone) {\r
658 \r
659                                 gui_send(GUI,"Illegal move: %s",string);\r
660 \r
661                         } else {\r
662 \r
663                                 gui_send(GUI,"Error (unknown command): %s",string);\r
664                         }\r
665                 }\r
666         return;\r
667 }\r
668 \r
669 // xboard2uci_engine_step()\r
670 \r
671 void xboard2uci_engine_step(char string[]) {\r
672 \r
673         int event;\r
674     board_t board[1];\r
675                 event = uci_parse(Uci,string);\r
676 \r
677                 // react to events\r
678 \r
679                 if ((event & EVENT_READY) != 0) {\r
680 \r
681                         // the engine is now ready\r
682 \r
683                         if (!Uci->ready) {\r
684                                 Uci->ready = TRUE;\r
685                     //  if (XB->proto_ver >= 2) xboard_send(XBoard,"feature done=1");\r
686                         }\r
687 \r
688                         if (!DelayPong && XB->ping >= 0) {\r
689                                 gui_send(GUI,"pong %d",XB->ping);\r
690                                 XB->ping = -1;\r
691                         }\r
692                 }\r
693 \r
694                 if ((event & EVENT_MOVE) != 0 && State->state == THINK) {\r
695 \r
696                         // the engine is playing a move\r
697 \r
698                         // MEGA HACK: estimate remaining time because XBoard won't send it!\r
699 \r
700                         my_timer_stop(State->timer);\r
701 \r
702                         XB->my_time -= my_timer_elapsed_real(State->timer);\r
703                         XB->my_time += XB->inc;\r
704                         if (XB->mps != 0 && (game_move_nb(Game) + 1) % XB->mps == 0) XB->my_time += XB->base;\r
705 \r
706                         if (XB->my_time < 0.0) XB->my_time = 0.0;\r
707 \r
708                         // play the engine move\r
709 \r
710                         comp_move(Uci->best_move);\r
711                 }\r
712 \r
713                 if ((event & EVENT_PV) != 0) {\r
714 \r
715                         // the engine has sent a new PV\r
716 \r
717                         send_pv();\r
718                 }\r
719                 if ((event & EVENT_INFO) != 0) {\r
720 \r
721                         // the engine has sent info\r
722 \r
723                         send_info();\r
724                 }\r
725                 if((event & (EVENT_DRAW|EVENT_RESIGN))!=0){\r
726                         my_log("POYGLOT draw offer/resign from engine\n");\r
727                         if(option_find(Uci->option,"UCI_DrawOffers")){\r
728                                 if(event & EVENT_DRAW)\r
729                                         gui_send(GUI,"offer draw");\r
730                                 else\r
731                                         gui_send(GUI,"resign");\r
732                         }\r
733                 }\r
734                 if(((event & EVENT_ILLEGAL_MOVE)!=0) && (State->state == THINK)){\r
735             game_get_board(Game,board);\r
736             if(board->turn==White){\r
737                 gui_send(GUI,"0-1 {polyglot: resign"\r
738                             " (illegal engine move white)}");\r
739             }else{\r
740                 gui_send(GUI,"1-0 {polyglot: resign"\r
741                          " (illegal engine move black)}");\r
742             }\r
743             XB->result = TRUE;\r
744             mess();\r
745         }\r
746 }\r
747 \r
748 // format_xboard_option_line\r
749 \r
750 void format_xboard_option_line(char * option_line, option_t *opt){\r
751     int j;\r
752     char option_string[StringSize];\r
753     char *tmp;\r
754     strcpy(option_line,"");\r
755         // buffer overflow alert\r
756     strcat(option_line,"feature option=\"");\r
757     if(opt->mode&PG){\r
758         strcat(option_line,"Polyglot ");\r
759     }\r
760     sprintf(option_string,"%s",opt->name);\r
761     strcat(option_line,option_string);\r
762     sprintf(option_string," -%s",opt->type);\r
763     strcat(option_line,option_string);\r
764     if(strcmp(opt->type,"button") && strcmp(opt->type,"combo")){\r
765         if(strcmp(opt->type,"check")){\r
766             sprintf(option_string," %s",opt->default_);\r
767         }else{\r
768             sprintf(option_string," %d",\r
769                     my_string_case_equal(opt->default_,"true")?1:0);\r
770         }\r
771         strcat(option_line,option_string);\r
772     }\r
773     if(!strcmp(opt->type,"spin")){\r
774         sprintf(option_string," %s",opt->min);\r
775             strcat(option_line,option_string);\r
776     }\r
777     if(!strcmp(opt->type,"spin")){\r
778         sprintf(option_string," %s",opt->max);\r
779         strcat(option_line,option_string);\r
780     }\r
781     for(j=0;j<opt->var_nb;j++){\r
782         if(!strcmp(opt->var[j],opt->default_)){\r
783             sprintf(option_string," *%s",opt->var[j]);\r
784         }else{\r
785             sprintf(option_string," %s",opt->var[j]);\r
786         }\r
787         strcat(option_line,option_string);\r
788         if(j!=opt->var_nb-1){\r
789             strcat(option_line," ///");\r
790         }\r
791     }\r
792     strcat(option_line,"\"");\r
793     if(option_get_bool(Option,"WbWorkArounds") &&\r
794        (tmp=strstr(option_line,"Draw"))){\r
795         *tmp='d';\r
796         my_log("POLYGLOT Decapitalizing \"Draw\" in option \"%s\"\n",\r
797                opt->name);\r
798     }\r
799 }\r
800 \r
801 // send_xboard_options()\r
802 \r
803 static void send_xboard_options(){\r
804 \r
805     char option_line[StringSize]="";\r
806     const char * name;\r
807     option_t *opt;\r
808     \r
809     gui_send(GUI,"feature done=0");\r
810     \r
811     gui_send(GUI,"feature analyze=1");\r
812     gui_send(GUI,"feature colors=0");\r
813     gui_send(GUI,"feature draw=1");\r
814     gui_send(GUI,"feature ics=1");\r
815     gui_send(GUI,"feature myname=\"%s\"",\r
816              option_get_string(Option,"EngineName"));\r
817     gui_send(GUI,"feature name=1");\r
818     gui_send(GUI,"feature pause=0");\r
819     gui_send(GUI,"feature ping=1");\r
820     gui_send(GUI,"feature playother=1");\r
821     gui_send(GUI,"feature reuse=1");\r
822     gui_send(GUI,"feature san=0");\r
823     gui_send(GUI,"feature setboard=1");\r
824     gui_send(GUI,"feature sigint=0");\r
825     gui_send(GUI,"feature sigterm=0");\r
826     gui_send(GUI,"feature time=1");\r
827     gui_send(GUI,"feature usermove=1");\r
828     if (XB->has_feature_memory){\r
829         gui_send(GUI,"feature memory=1");\r
830     }else{\r
831         gui_send(GUI,"feature memory=0");\r
832     }\r
833     if (XB->has_feature_smp){\r
834         gui_send(GUI,"feature smp=1");\r
835     }else{\r
836         gui_send(GUI,"feature smp=0");\r
837     }\r
838     if (XB->has_feature_egt){\r
839             // TODO: support for other types of table bases\r
840         gui_send(GUI,"feature egt=\"nalimov\"");\r
841     }else{\r
842         gui_send(GUI,"feature egt=\"\"");\r
843     }\r
844     \r
845     if (option_find(Uci->option,"UCI_Chess960")) {\r
846         gui_send(GUI,"feature variants=\"normal,fischerandom\"");\r
847     } else {\r
848         gui_send(GUI,"feature variants=\"normal\"");\r
849     }\r
850 \r
851     option_start_iter(Uci->option);\r
852     while((opt=option_next(Uci->option))){\r
853         if(my_string_case_equal(opt->name,"UCI_AnalyseMode")) continue;\r
854         if(my_string_case_equal(opt->name,"Ponder")) continue;\r
855         if(my_string_case_equal(opt->name,"Hash")) continue;\r
856         if(my_string_case_equal(opt->name,"NalimovPath")) continue;\r
857         if((name=uci_thread_option(Uci))!=NULL &&\r
858            my_string_case_equal(opt->name,name)) continue;\r
859         \r
860         format_xboard_option_line(option_line,opt);\r
861         \r
862         gui_send(GUI,"%s",option_line);\r
863     }\r
864 \r
865 \r
866     option_start_iter(Option);\r
867     while((opt=option_next(Option))){\r
868         if(opt->mode &XBOARD){\r
869             format_xboard_option_line(option_line,opt);\r
870             gui_send(GUI,"%s",option_line);\r
871         }\r
872     }       \r
873     gui_send(GUI,"feature done=1"); \r
874     \r
875 }\r
876 \r
877 // report_best_score()\r
878 \r
879 static int report_best_score(){\r
880     if(!option_get_bool(Option,"ScoreWhite") ||\r
881        colour_is_white(Uci->board->turn)){\r
882         return Uci->best_score;\r
883     }else{\r
884         return -Uci->best_score;\r
885     }\r
886 }\r
887 \r
888 // comp_move()\r
889 \r
890 static void comp_move(int move) {\r
891 \r
892    board_t board[1];\r
893    char string[256];\r
894 \r
895    ASSERT(move_is_ok(move));\r
896 \r
897    ASSERT(State->state==THINK);\r
898    ASSERT(!XB->analyse);\r
899 \r
900    if(option_get_bool(Option,"RepeatPV"))\r
901            send_pv(); // to update time and nodes\r
902 \r
903    // send the move\r
904 \r
905    game_get_board(Game,board);\r
906 \r
907    if (move_is_castle(move,board) && option_get_bool(Option,"Chess960")) {\r
908       if (!move_to_san(move,board,string,256)) my_fatal("comp_move(): move_to_san() failed\n"); // O-O/O-O-O\r
909    } else {\r
910       if (!move_to_can(move,board,string,256)) my_fatal("comp_move(): move_to_can() failed\n");\r
911    }\r
912 \r
913    gui_send(GUI,"move %s",string);\r
914 \r
915    // resign?\r
916 \r
917    if (option_get_bool(Option,"Resign") && Uci->root_move_nb > 1) {\r
918 \r
919        if (Uci->best_score <= -abs(option_get_int(Option,"ResignScore"))) {\r
920 \r
921          State->resign_nb++;\r
922          my_log("POLYGLOT %d move%s with resign score\n",State->resign_nb,(State->resign_nb>1)?"s":"");\r
923 \r
924          if (State->resign_nb >= option_get_int(Option,"ResignMoves")) {\r
925             my_log("POLYGLOT *** RESIGN ***\n");\r
926             gui_send(GUI,"resign");\r
927          }\r
928 \r
929       } else {\r
930 \r
931          if (State->resign_nb > 0) my_log("POLYGLOT resign reset (State->resign_nb=%d)\n",State->resign_nb);\r
932          State->resign_nb = 0;\r
933       }\r
934    }\r
935 \r
936    // play the move\r
937 \r
938    move_step(move);\r
939    no_mess(move);\r
940 }\r
941 \r
942 // move_step()\r
943 \r
944 static void move_step(int move) {\r
945 \r
946    board_t board[1];\r
947    char move_string[256];\r
948 \r
949    ASSERT(move_is_ok(move));\r
950 \r
951    // log\r
952 \r
953    game_get_board(Game,board);\r
954 \r
955    if (move != MoveNone && move_is_legal(move,board)) {\r
956 \r
957       move_to_san(move,board,move_string,256);\r
958       my_log("POLYGLOT MOVE %s\n",move_string);\r
959 \r
960    } else {\r
961 \r
962       move_to_can(move,board,move_string,256);\r
963       my_log("POLYGLOT ILLEGAL MOVE \"%s\"\n",move_string);\r
964       board_disp(board);\r
965 \r
966       my_fatal("move_step(): illegal move \"%s\"\n",move_string);\r
967    }\r
968 \r
969    // play the move\r
970 \r
971    game_add_move(Game,move);\r
972    board_update();\r
973 }\r
974 \r
975 // board_update()\r
976 \r
977 static void board_update() {\r
978 \r
979    // handle game end\r
980 \r
981    ASSERT(!XB->result);\r
982 \r
983    switch (game_status(Game)) {\r
984    case PLAYING:\r
985       break;\r
986    case WHITE_MATES:\r
987       gui_send(GUI,"1-0 {White mates}");\r
988       break;\r
989    case BLACK_MATES:\r
990       gui_send(GUI,"0-1 {Black mates}");\r
991       break;\r
992    case STALEMATE:\r
993       gui_send(GUI,"1/2-1/2 {Stalemate}");\r
994       break;\r
995    case DRAW_MATERIAL:\r
996       gui_send(GUI,"1/2-1/2 {Draw by insufficient material}");\r
997       break;\r
998    case DRAW_FIFTY:\r
999       gui_send(GUI,"1/2-1/2 {Draw by fifty-move rule}");\r
1000       break;\r
1001    case DRAW_REPETITION:\r
1002       gui_send(GUI,"1/2-1/2 {Draw by repetition}");\r
1003       break;\r
1004    default:\r
1005       ASSERT(FALSE);\r
1006       break;\r
1007    }\r
1008 }\r
1009 \r
1010 // mess()\r
1011 \r
1012 static void mess() {\r
1013 \r
1014    // clear state variables\r
1015 \r
1016    State->resign_nb = 0;\r
1017    State->exp_move = MoveNone;\r
1018    my_timer_reset(State->timer);\r
1019 \r
1020    // abort a possible search\r
1021 \r
1022    stop_search();\r
1023 \r
1024    // calculate the new state\r
1025 \r
1026    if (FALSE) {\r
1027    } else if (!active()) {\r
1028       State->state = WAIT;\r
1029       my_log("POLYGLOT WAIT\n");\r
1030    } else if (XB->analyse) {\r
1031       State->state = ANALYSE;\r
1032       my_log("POLYGLOT ANALYSE\n");\r
1033    } else if (State->computer[game_turn(Game)]) {\r
1034       State->state = THINK;\r
1035       my_log("POLYGLOT THINK\n");\r
1036    } else {\r
1037       State->state = WAIT;\r
1038       my_log("POLYGLOT WAIT\n");\r
1039    }\r
1040 \r
1041    search_update();\r
1042 }\r
1043 \r
1044 // no_mess()\r
1045 \r
1046 static void no_mess(int move) {\r
1047 \r
1048    ASSERT(move_is_ok(move));\r
1049 \r
1050    // just received a move, calculate the new state\r
1051 \r
1052    if (FALSE) {\r
1053 \r
1054    } else if (!active()) {\r
1055 \r
1056       stop_search(); // abort a possible search\r
1057 \r
1058       State->state = WAIT;\r
1059       State->exp_move = MoveNone;\r
1060 \r
1061       my_log("POLYGLOT WAIT\n");\r
1062 \r
1063    } else if (State->state == WAIT) {\r
1064 \r
1065       ASSERT(State->computer[game_turn(Game)]);\r
1066       ASSERT(!State->computer[colour_opp(game_turn(Game))]);\r
1067       ASSERT(!XB->analyse);\r
1068 \r
1069       my_log("POLYGLOT WAIT -> THINK\n");\r
1070 \r
1071       State->state = THINK;\r
1072       State->exp_move = MoveNone;\r
1073 \r
1074    } else if (State->state == THINK) {\r
1075 \r
1076       ASSERT(!State->computer[game_turn(Game)]);\r
1077       ASSERT(State->computer[colour_opp(game_turn(Game))]);\r
1078       ASSERT(!XB->analyse);\r
1079 \r
1080       if (ponder() && ponder_ok(Uci->ponder_move)) {\r
1081 \r
1082          my_log("POLYGLOT THINK -> PONDER\n");\r
1083 \r
1084          State->state = PONDER;\r
1085          State->exp_move = Uci->ponder_move;\r
1086 \r
1087       } else {\r
1088 \r
1089          my_log("POLYGLOT THINK -> WAIT\n");\r
1090 \r
1091          State->state = WAIT;\r
1092          State->exp_move = MoveNone;\r
1093       }\r
1094 \r
1095    } else if (State->state == PONDER) {\r
1096 \r
1097       ASSERT(State->computer[game_turn(Game)]);\r
1098       ASSERT(!State->computer[colour_opp(game_turn(Game))]);\r
1099       ASSERT(!XB->analyse);\r
1100 \r
1101       if (move == State->exp_move && Uci->searching) {\r
1102 \r
1103          ASSERT(Uci->searching);\r
1104          ASSERT(Uci->pending_nb>=1);\r
1105 \r
1106          my_timer_start(State->timer);//also resets\r
1107 \r
1108          my_log("POLYGLOT PONDER -> THINK (*** HIT ***)\n");\r
1109          engine_send(Engine,"ponderhit");\r
1110 \r
1111          State->state = THINK;\r
1112          State->exp_move = MoveNone;\r
1113 \r
1114          send_pv(); // update display\r
1115 \r
1116          return; // do not launch a new search\r
1117 \r
1118       } else {\r
1119 \r
1120          my_log("POLYGLOT PONDER -> THINK (miss)\n");\r
1121 \r
1122          stop_search();\r
1123 \r
1124          State->state = THINK;\r
1125          State->exp_move = MoveNone;\r
1126       }\r
1127 \r
1128    } else if (State->state == ANALYSE) {\r
1129 \r
1130       ASSERT(XB->analyse);\r
1131 \r
1132       my_log("POLYGLOT ANALYSE -> ANALYSE\n");\r
1133 \r
1134       stop_search();\r
1135 \r
1136    } else {\r
1137 \r
1138       ASSERT(FALSE);\r
1139    }\r
1140 \r
1141    search_update();\r
1142 }\r
1143 \r
1144 // start_protected_command()\r
1145 \r
1146 static void start_protected_command(){\r
1147     stop_search();\r
1148 }\r
1149 \r
1150 static void end_protected_command(){\r
1151     if(Uci->ready){ // not init faze\r
1152         uci_send_isready_sync(Uci); // gobble up spurious "bestmove"\r
1153     }\r
1154     update_remaining_time();\r
1155     search_update();   // relaunch search if necessary\r
1156 }\r
1157 \r
1158 // update_remaining_time()\r
1159 \r
1160 static void update_remaining_time(){\r
1161    double reduce;\r
1162    if(State->timer->running){\r
1163        my_timer_stop(State->timer);\r
1164        reduce = my_timer_elapsed_real(State->timer);\r
1165        my_log("POLYGLOT reducing remaing time by %f seconds\n",reduce);\r
1166        XB->my_time -= reduce;\r
1167        if(XB->my_time<0.0){\r
1168            XB->my_time=0.0;\r
1169        }\r
1170    }\r
1171 }\r
1172 \r
1173 \r
1174 // search_update()\r
1175 \r
1176 static void search_update() {\r
1177 \r
1178    int move;\r
1179    int move_nb;\r
1180    board_t board[1];\r
1181 \r
1182    ASSERT(!Uci->searching);\r
1183 \r
1184 \r
1185 \r
1186    \r
1187    // launch a new search if needed\r
1188 \r
1189    \r
1190 \r
1191    if (State->state == THINK || State->state == PONDER || State->state == ANALYSE) {\r
1192 \r
1193       // opening book\r
1194 \r
1195        if (State->state == THINK && option_get_bool(Option,"Book")) {\r
1196 \r
1197          game_get_board(Game,Uci->board);\r
1198 \r
1199          move = book_move(Uci->board,option_get_bool(Option,"BookRandom"));\r
1200 \r
1201          if (move != MoveNone && move_is_legal(move,Uci->board)) {\r
1202 \r
1203             my_log("POLYGLOT *BOOK MOVE*\n");\r
1204 \r
1205             search_clear(); // clears Uci->ponder_move\r
1206             Uci->best_move = move;\r
1207 \r
1208             board_copy(board,Uci->board);\r
1209             move_do(board,move);\r
1210             Uci->ponder_move = book_move(board,FALSE); // expected move = best book move\r
1211 \r
1212             Uci->best_pv[0] = Uci->best_move;\r
1213             Uci->best_pv[1] = Uci->ponder_move; // can be MoveNone\r
1214             Uci->best_pv[2] = MoveNone;\r
1215 \r
1216             comp_move(Uci->best_move);\r
1217 \r
1218             return;\r
1219          }\r
1220       }\r
1221 \r
1222       // engine search\r
1223 \r
1224       my_log("POLYGLOT START SEARCH\n");\r
1225 \r
1226       // options\r
1227 \r
1228       uci_send_option(Uci,"UCI_Chess960","%s",\r
1229                       option_get_bool(Option,"Chess960")?"true":"false");\r
1230 \r
1231       if (option_get_int(Option,"UCIVersion") >= 2) {\r
1232          uci_send_option(Uci,"UCI_Opponent","none none %s %s",(XB->computer)?"computer":"human",XB->name);\r
1233          uci_send_option(Uci,"UCI_AnalyseMode","%s",(XB->analyse)?"true":"false");\r
1234       }\r
1235 \r
1236       uci_send_option(Uci,"Ponder","%s",ponder()?"true":"false");\r
1237 \r
1238       // position\r
1239 \r
1240       move = (State->state == PONDER) ? State->exp_move : MoveNone;\r
1241       send_board(move); // updates Uci->board global variable\r
1242 \r
1243       // search\r
1244 \r
1245       if (State->state == THINK || State->state == PONDER) {\r
1246 \r
1247          engine_send_queue(Engine,"go");\r
1248 \r
1249          if (XB->time_limit) {\r
1250 \r
1251             // fixed time per move\r
1252 \r
1253             engine_send_queue(Engine," movetime %.0f",XB->time_max*1000.0);\r
1254 \r
1255          } else {\r
1256 \r
1257             // time controls\r
1258 \r
1259             if (colour_is_white(Uci->board->turn)) {\r
1260                engine_send_queue(Engine," wtime %.0f btime %.0f",XB->my_time*1000.0,XB->opp_time*1000.0);\r
1261             } else {\r
1262                engine_send_queue(Engine," wtime %.0f btime %.0f",XB->opp_time*1000.0,XB->my_time*1000.0);\r
1263             }\r
1264 \r
1265             if (XB->inc != 0.0) engine_send_queue(Engine," winc %.0f binc %.0f",XB->inc*1000.0,XB->inc*1000.0);\r
1266 \r
1267             if (XB->mps != 0) {\r
1268 \r
1269                move_nb = XB->mps - (Uci->board->move_nb % XB->mps);\r
1270                ASSERT(move_nb>=1&&move_nb<=XB->mps);\r
1271 \r
1272                engine_send_queue(Engine," movestogo %d",move_nb);\r
1273             }\r
1274          }\r
1275 \r
1276          if (XB->depth_limit) engine_send_queue(Engine," depth %d",XB->depth_max);\r
1277 \r
1278          if (State->state == PONDER) engine_send_queue(Engine," ponder");\r
1279 \r
1280          engine_send(Engine,""); // newline\r
1281 \r
1282       } else if (State->state == ANALYSE) {\r
1283 \r
1284          engine_send(Engine,"go infinite");\r
1285 \r
1286       } else {\r
1287 \r
1288          ASSERT(FALSE);\r
1289       }\r
1290 \r
1291       // init search info\r
1292 \r
1293       ASSERT(!Uci->searching);\r
1294 \r
1295       search_clear();\r
1296 \r
1297       Uci->searching = TRUE;\r
1298       Uci->pending_nb++;\r
1299    }\r
1300 }\r
1301 \r
1302 // search_clear()\r
1303 \r
1304 static void search_clear() {\r
1305 \r
1306    uci_clear(Uci);\r
1307 \r
1308    // TODO: MOVE ME\r
1309 \r
1310    my_timer_start(State->timer);//also resets\r
1311 }\r
1312 \r
1313 // active()\r
1314 \r
1315 static bool active() {\r
1316 \r
1317    // position state\r
1318 \r
1319    if (game_status(Game) != PLAYING) return FALSE; // game ended\r
1320 \r
1321    // xboard state\r
1322 \r
1323    if (XB->analyse) return TRUE; // analysing\r
1324    if (!State->computer[White] && !State->computer[Black]) return FALSE; // force mode\r
1325    if (XB->new_hack || XB->result) return FALSE; // unstarted or ended game\r
1326 \r
1327    return TRUE; // playing\r
1328 }\r
1329 \r
1330 // ponder()\r
1331 \r
1332 static bool ponder() {\r
1333 \r
1334     return XB->ponder && (option_get_bool(Option,"CanPonder") ||\r
1335                           option_find(Uci->option,"Ponder"));\r
1336 }\r
1337 // ponder_ok()\r
1338 \r
1339 static bool ponder_ok(int move) {\r
1340    int status;\r
1341    board_t board[1];\r
1342 \r
1343    ASSERT(move==MoveNone||move_is_ok(move));\r
1344 \r
1345    // legal ponder move?\r
1346 \r
1347    if (move == MoveNone) return FALSE;\r
1348 \r
1349    game_get_board(Game,board);\r
1350    if (!move_is_legal(move,board)) return FALSE;\r
1351 \r
1352    // UCI-legal resulting position?\r
1353 \r
1354    game_add_move(Game,move);\r
1355 \r
1356    game_get_board(Game,board);\r
1357    status = game_status(Game);\r
1358 \r
1359    game_rem_move(Game);\r
1360 \r
1361    if (status != PLAYING) return FALSE; // game ended\r
1362 \r
1363    if (option_get_bool(Option,"Book") && is_in_book(board)) {\r
1364       return FALSE;\r
1365    }\r
1366 \r
1367    return TRUE;\r
1368 }\r
1369 \r
1370 // stop_search()\r
1371 \r
1372 static void stop_search() {\r
1373 \r
1374    if (Uci->searching) {\r
1375 \r
1376       ASSERT(Uci->searching);\r
1377       ASSERT(Uci->pending_nb>=1);\r
1378 \r
1379       my_log("POLYGLOT STOP SEARCH\n");\r
1380 \r
1381 /*\r
1382       engine_send(Engine,"stop");\r
1383       Uci->searching = FALSE;\r
1384 */\r
1385 \r
1386       if (option_get_bool(Option,"SyncStop")) {\r
1387          uci_send_stop_sync(Uci);\r
1388       } else {\r
1389          uci_send_stop(Uci);\r
1390       }\r
1391         }\r
1392 }\r
1393 \r
1394 // send_board()\r
1395 \r
1396 static void send_board(int extra_move) {\r
1397 \r
1398    char fen[256];\r
1399    int start, end;\r
1400    board_t board[1];\r
1401    int pos;\r
1402    int move;\r
1403    char string[256];\r
1404 \r
1405    ASSERT(extra_move==MoveNone||move_is_ok(extra_move));\r
1406 \r
1407    ASSERT(!Uci->searching);\r
1408 \r
1409    // init\r
1410 \r
1411    game_get_board(Game,Uci->board);\r
1412    if (extra_move != MoveNone) move_do(Uci->board,extra_move);\r
1413 \r
1414    board_to_fen(Uci->board,fen,256);\r
1415    my_log("POLYGLOT FEN %s\n",fen);\r
1416 \r
1417    ASSERT(board_can_play(Uci->board));\r
1418 \r
1419    // more init\r
1420 \r
1421    start = 0;\r
1422    end = game_pos(Game);\r
1423    ASSERT(end>=start);\r
1424 \r
1425    // position\r
1426 \r
1427    game_get_board_ex(Game,board,start);\r
1428    board_to_fen(board,string,256);\r
1429 \r
1430    engine_send_queue(Engine,"position");\r
1431 \r
1432    if (my_string_equal(string,StartFen)) {\r
1433       engine_send_queue(Engine," startpos");\r
1434    } else {\r
1435       engine_send_queue(Engine," fen %s",string);\r
1436    }\r
1437 \r
1438    // move list\r
1439 \r
1440    if (end > start || extra_move != MoveNone) engine_send_queue(Engine," moves");\r
1441 \r
1442    for (pos = start; pos < end; pos++) { // game moves\r
1443 \r
1444       move = game_move(Game,pos);\r
1445 \r
1446       move_to_can(move,board,string,256);\r
1447       engine_send_queue(Engine," %s",string);\r
1448 \r
1449       move_do(board,move);\r
1450    }\r
1451 \r
1452    if (extra_move != MoveNone) { // move to ponder on\r
1453       move_to_can(extra_move,board,string,256);\r
1454       engine_send_queue(Engine," %s",string);\r
1455    }\r
1456 \r
1457    // end\r
1458 \r
1459    engine_send(Engine,""); // newline\r
1460 }\r
1461 \r
1462 // send_info()\r
1463 \r
1464 static void send_info() {\r
1465     int min_depth;\r
1466     if(option_get_bool(Option,"WbWorkArounds")){\r
1467             // Silly bug in some versions of WinBoard.\r
1468             // depth <=1 clears the engine output window.\r
1469             // Why shouldn't an engine be allowed to send info at depth 1?\r
1470         min_depth=2;\r
1471     }else{\r
1472         min_depth=1;\r
1473     }\r
1474     gui_send(GUI,"%d %+d %.0f "S64_FORMAT" %s",Uci->best_depth>min_depth?Uci->best_depth:min_depth,\r
1475                  0,0,0.0,0,Uci->info);  \r
1476 }\r
1477 \r
1478 // send_pv()\r
1479 \r
1480 static void send_pv() {\r
1481 \r
1482    char pv_string[StringSize];\r
1483    board_t board[1];\r
1484    int move;\r
1485    char move_string[StringSize];\r
1486 \r
1487    ASSERT(State->state!=WAIT);\r
1488 \r
1489    if (Uci->best_depth == 0) return;\r
1490 \r
1491    // xboard search information\r
1492 \r
1493    if (XB->post) {\r
1494 \r
1495       if (State->state == THINK || State->state == ANALYSE) {\r
1496 \r
1497          line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);\r
1498 \r
1499                  if(Uci->depth==-1) //hack to clear the engine output window\r
1500              gui_send(GUI,"%d %+d %.0f "S64_FORMAT" ",0,report_best_score(),Uci->time*100.0,Uci->node_nb);\r
1501 \r
1502                  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
1503 \r
1504       } else if (State->state == PONDER &&\r
1505                  option_get_bool(Option,"ShowPonder")) {\r
1506 \r
1507          game_get_board(Game,board);\r
1508          move = State->exp_move;\r
1509 \r
1510          if (move != MoveNone && move_is_legal(move,board)) {\r
1511             move_to_san(move,board,move_string,256);\r
1512             line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);\r
1513             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
1514          }\r
1515       }\r
1516    }\r
1517 \r
1518    // kibitz\r
1519 \r
1520    if ((Uci->searching &&\r
1521         option_get_bool(Option,"KibitzPV") &&\r
1522         Uci->time >= option_get_double(Option,"KibitzDelay"))\r
1523        || (!Uci->searching && option_get_bool(Option,"KibitzMove"))) {\r
1524 \r
1525       if (State->state == THINK || State->state == ANALYSE) {\r
1526 \r
1527          line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);\r
1528          if(kibitz_throttle(Uci->searching)){\r
1529              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
1530          }\r
1531       } else if (State->state == PONDER) {\r
1532 \r
1533          game_get_board(Game,board);\r
1534          move = State->exp_move;\r
1535 \r
1536          if (move != MoveNone && move_is_legal(move,board)) {\r
1537             move_to_san(move,board,move_string,256);\r
1538             line_to_san(Uci->best_pv,Uci->board,pv_string,StringSize);\r
1539             if(kibitz_throttle(Uci->searching)){\r
1540                 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
1541             }\r
1542          }\r
1543       }\r
1544    }\r
1545 }\r
1546 \r
1547 // kibitz_throttle()\r
1548 \r
1549 static bool kibitz_throttle(bool searching){\r
1550     time_t curr_time;\r
1551     static time_t lastKibitzMove=0;\r
1552     static time_t lastKibitzPV=0;\r
1553     curr_time = time(NULL);\r
1554     if(searching){   // KibitzPV\r
1555         if(curr_time >=\r
1556            (option_get_int(Option,"KibitzInterval") + lastKibitzPV)){\r
1557             lastKibitzPV=curr_time;\r
1558             return TRUE;\r
1559         }\r
1560     }else{       // KibitzMove\r
1561         if(curr_time >=\r
1562            (option_get_int(Option,"KibitzInterval") + lastKibitzMove)){\r
1563             lastKibitzPV=curr_time;\r
1564             lastKibitzMove=curr_time;\r
1565             return TRUE;\r
1566         }        \r
1567     }\r
1568     return FALSE;\r
1569 }\r
1570 \r
1571 // learn()\r
1572 \r
1573 static void learn(int result) {\r
1574 \r
1575    int pos;\r
1576    board_t board[1];\r
1577    int move;\r
1578 \r
1579    ASSERT(result>=-1&&result<=+1);\r
1580 \r
1581    ASSERT(XB->result);\r
1582    ASSERT(State->computer[White]||State->computer[Black]);\r
1583 \r
1584    // init\r
1585 \r
1586    pos = 0;\r
1587 \r
1588    if (FALSE) {\r
1589    } else if (State->computer[White]) {\r
1590       pos = 0;\r
1591    } else if (State->computer[Black]) {\r
1592       pos = 1;\r
1593       result = -result;\r
1594    } else {\r
1595       my_fatal("learn(): unknown side\n");\r
1596    }\r
1597 \r
1598    if (FALSE) {\r
1599    } else if (result > 0) {\r
1600       my_log("POLYGLOT *LEARN WIN*\n");\r
1601    } else if (result < 0) {\r
1602       my_log("POLYGLOT *LEARN LOSS*\n");\r
1603    } else {\r
1604       my_log("POLYGLOT *LEARN DRAW*\n");\r
1605    }\r
1606 \r
1607    // loop\r
1608 \r
1609    for (; pos < Game->size; pos += 2) {\r
1610 \r
1611       game_get_board_ex(Game,board,pos);\r
1612       move = game_move(Game,pos);\r
1613 \r
1614       book_learn_move(board,move,result);\r
1615    }\r
1616 \r
1617    book_flush();\r
1618 }\r
1619 \r
1620 // end of xboard2uci.c\r