version 1.4.56b
[polyglot.git] / uci.c
diff --git a/uci.c b/uci.c
index 9326ec7..65d4d2a 100644 (file)
--- a/uci.c
+++ b/uci.c
@@ -10,6 +10,7 @@
 \r
 #include "board.h"\r
 #include "engine.h"\r
+#include "gui.h"\r
 #include "move.h"\r
 #include "move_do.h"\r
 #include "move_legal.h"\r
@@ -18,6 +19,7 @@
 #include "line.h"\r
 #include "uci.h"\r
 \r
+\r
 // constants\r
 \r
 static const bool UseDebug = FALSE;\r
@@ -148,6 +150,11 @@ void uci_open(uci_t * uci, engine_t * engine) {
 \r
    do {\r
       engine_get(uci->engine,string);\r
+      // Handle the case that the engine is really a WB engine somewhat gracefully.\r
+      if((strstr(string,"Illegal") || strstr(string,"Error"))\r
+         &&strstr(string,"uci")){\r
+          my_fatal("uci_open(): Not an UCI engine.\n");\r
+      }\r
       event = uci_parse(uci,string);\r
    } while (!engine_eof(Engine) && (event & EVENT_UCI) == 0);\r
 }\r
@@ -196,6 +203,8 @@ void uci_clear(uci_t * uci) {
    uci->root_move = MoveNone;\r
    uci->root_move_pos = 0;\r
    uci->root_move_nb = board_mobility(uci->board);\r
+\r
+   uci->multipvSP=0;\r
 }\r
 \r
 // uci_send_isready()\r
@@ -331,7 +340,7 @@ int uci_parse(uci_t * uci, const char string[]) {
    parse_open(parse,string);\r
 \r
    if (parse_get_word(parse,command,StringSize)) {\r
-\r
+       \r
       parse_get_string(parse,argument,StringSize);\r
       if (UseDebug) my_log("POLYGLOT COMMAND \"%s\" ARGUMENT \"%s\"\n",command,argument);\r
 \r
@@ -633,7 +642,7 @@ static int parse_info(uci_t * uci, const char string[]) {
          ASSERT(!my_string_empty(argument));\r
 \r
          n = atoi(argument);\r
-                if(Uci->multipv_mode) multipvline=n;\r
+        multipvline=n;\r
         \r
          ASSERT(n>=1);\r
 \r
@@ -726,16 +735,43 @@ static int parse_info(uci_t * uci, const char string[]) {
 \r
    parse_close(parse);\r
 \r
-   // update display\r
-   //lousy uci,filter out lower depth multipv lines that have been repeated from the engine \r
-   if(multipvline>1 && uci->depth<uci->best_depth) event &= ~EVENT_PV;\r
+\r
+   // code by HGM\r
    if ((event & EVENT_PV) != 0) {\r
       uci->best_score = uci->score; \r
-         uci->best_depth = uci->depth;\r
-         if(multipvline==1)uci->depth=-1; //HACK ,clears the engine outpout window,see send_pv in adapter.cpp \r
       uci->best_sel_depth = uci->sel_depth;\r
       line_copy(uci->best_pv,uci->pv);\r
    }\r
+   if(uci->depth < uci->best_depth){\r
+     // ignore lines of lower depth\r
+     event &= ~EVENT_PV;\r
+   } else {\r
+     if(uci->depth > uci->best_depth) {\r
+       // clear stack when we start new depth\r
+       uci->multipvSP = 0; \r
+     }\r
+     uci->best_depth = uci->depth;\r
+     if(multipvline >= 1) {\r
+       int i;\r
+       for(i=0; i<uci->multipvSP; i++) {\r
+        if(uci->score == uci->multipvScore[i] && uci->pv[0] == uci->multipvMove[i]) {\r
+          event &= ~EVENT_PV; // ignore duplicates\r
+        }\r
+       }\r
+       if(event & EVENT_PV){\r
+        // line is new, try to add to stack\r
+        if(uci->multipvSP<MultiPVStackSize){\r
+          uci->multipvMove[uci->multipvSP] = uci->pv[0];\r
+          uci->multipvScore[uci->multipvSP] = uci->score;\r
+          uci->multipvSP++;\r
+        }else{\r
+          my_fatal("parse_info(): multipv stack overflow.");\r
+        }\r
+       }\r
+     }\r
+   }\r
+\r
+\r
    return event;\r
 }\r
 \r