Add option to allow Polyglot to accept draw offers
[polyglot.git] / xboard2uci.c
index 8f4614e..f0288d7 100644 (file)
@@ -43,6 +43,7 @@ static const bool DelayPong = FALSE;
 typedef struct {
    int state;
    bool computer[ColourNb];
+   bool playedAllMoves[ColourNb];
    int exp_move;
    int hint_move;
    int resign_nb;
@@ -230,6 +231,10 @@ void xboard2uci_gui_step(char string[]) {
                        if(option_find(Uci->option,"UCI_DrawOffers")){
                            my_log("POLYGLOT draw from XB received");
                                uci_send_option(Uci,"DrawOffer","%s","draw");}
+                       else if (option_get_bool(Option,"HandleDraws") && Uci->root_move_nb > 20) { // [HGM] PG draw handling
+                           my_log("POLYGLOT draw from XB received");
+                           if (Uci->best_score <= -option_get_int(Option,"ContemptScore")) 
+                               gui_send(GUI,"offer draw");}
                } else if (match(string,"easy")) {
 
                        XB->ponder = FALSE;
@@ -327,6 +332,8 @@ void xboard2uci_gui_step(char string[]) {
                        } else {
                                State->computer[White] = FALSE;
                                State->computer[Black] = TRUE;
+                               State->playedAllMoves[White] = TRUE; // [HGM]
+                               State->playedAllMoves[Black] = TRUE;
                        }
 
                        XB->new_hack = TRUE;
@@ -616,6 +623,9 @@ void xboard2uci_gui_step(char string[]) {
                                ASSERT(!XB->result);
                                XB->result = FALSE;
 
+                               // [HGM] externally supplied move means we did not fully play the current stm
+                               State->playedAllMoves[colour_is_white(game_turn(Game)) ? White : Black] = FALSE;
+
                                move_step(move);
                                no_mess(move);
 
@@ -846,6 +856,19 @@ void format_xboard_option_line(char * option_line, option_t *opt){
     }
 }
 
+// disarm() // [HGM] cleanse a string of offending double-quotes
+
+static char*disarm(const char *s){
+    static char buf[25];
+    char *p = buf, *q;
+    strncpy(buf, s, 24);
+    q = buf + strlen(buf) - 1;
+    while(*q == '"') *q-- = '\0';          // strip trailing quotes
+    while(*p == '"') p++;                  // strip leading quotes
+    while((q = strchr(p, '"'))) *q = '\''; // replace internal quotes
+    return p;
+}
+
 // send_xboard_options()
 
 static void send_xboard_options(){
@@ -860,7 +883,7 @@ static void send_xboard_options(){
     gui_send(GUI,"feature draw=1");
     gui_send(GUI,"feature ics=1");
     gui_send(GUI,"feature myname=\"%s\"",
-             option_get_string(Option,"EngineName"));
+             disarm(option_get_string(Option,"EngineName")));
     gui_send(GUI,"feature name=1");
     gui_send(GUI,"feature pause=0");
     gui_send(GUI,"feature ping=1");
@@ -995,8 +1018,10 @@ static void comp_move(int move) {
          my_log("POLYGLOT %d move%s with resign score\n",State->resign_nb,(State->resign_nb>1)?"s":"");
 
          if (State->resign_nb >= option_get_int(Option,"ResignMoves")) {
-            my_log("POLYGLOT *** RESIGN ***\n");
-            gui_send(GUI,"resign");
+           if (!option_get_bool(Option,"QueenNeverResigns") || !board_has_queen(board, board->turn)) { // [HGM] suppress resignig with Queen
+                my_log("POLYGLOT *** RESIGN ***\n");
+                gui_send(GUI,"resign");
+           }
          }
 
       } else {
@@ -1591,9 +1616,8 @@ static void send_info() {
     }else{
         min_depth=1;
     }
-        
     gui_send(GUI,"%d %+d %.0f "S64_FORMAT" %s",Uci->best_depth>min_depth?Uci->best_depth:min_depth,
-            0,0.0,0,Uci->info);  
+            0,0.0,U64(0),Uci->info);  
 }
 
 // send_pv()
@@ -1700,20 +1724,20 @@ static void learn(int result) {
    ASSERT(result>=-1&&result<=+1);
 
    ASSERT(XB->result);
-   ASSERT(State->computer[White]||State->computer[Black]);
+//   ASSERT(State->computer[White]||State->computer[Black]);
 
    // init
 
    pos = 0;
 
-   if (FALSE) {
-   } else if (State->computer[White]) {
+   // [HGM] does not account for the hypothetical possibility we played both sides!
+   if (State->playedAllMoves[White]) {
       pos = 0;
-   } else if (State->computer[Black]) {
+   } else if (State->playedAllMoves[Black]) {
       pos = 1;
       result = -result;
    } else {
-      my_fatal("learn(): unknown side\n");
+      return; // [HGM] if we did not play all moves for some side, do not learn, but don't make a fuss!
    }
 
    if (FALSE) {