worked on premove bug
[xboard.git] / backend.c
index 94d0e81..304acc2 100644 (file)
--- a/backend.c
+++ b/backend.c
@@ -469,6 +469,7 @@ char  FENepStatus;
 FILE  *serverMoves = NULL; // next two for broadcasting (/serverMoves option)
 int loadFlag = 0; 
 int shuffleOpenings;
+int mute; // mute all sounds
 
 ChessSquare  FIDEArray[2][BOARD_SIZE] = {
     { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
@@ -2093,6 +2094,7 @@ read_from_ics(isr, closure, data, count, error)
     int backup;    /* [DM] For zippy color lines */
     char *p;
     char talker[MSG_SIZ]; // [HGM] chat
+    int channel;
 
     if (appData.debugMode) {
       if (!error) {
@@ -2418,18 +2420,31 @@ read_from_ics(isr, closure, data, count, error)
 //if(appData.debugMode) fprintf(debugFP, "hunt for tell, buf = %s\n", buf+i);
 
            // [HGM] chat: intercept tells by users for which we have an open chat window
+           channel = -1;
            if(started == STARTED_NONE && (looking_at(buf, &i, "* tells you:") || looking_at(buf, &i, "* says:") || 
-                                          looking_at(buf, &i, "* whispers:"))) {
+                                          looking_at(buf, &i, "* whispers:") ||
+                                          looking_at(buf, &i, "*(*):") && (sscanf(star_match[1], "%d", &channel),1) ||
+                                          looking_at(buf, &i, "*(*)(*):") && sscanf(star_match[2], "%d", &channel) == 1 )) {
                int p;
                sscanf(star_match[0], "%[^(]", talker+1); // strip (C) or (U) off ICS handle
                chattingPartner = -1;
-               if(buf[i-3] == 'r') // whisper; look if there is a WHISPER chatbox
-               for(p=0; p<MAX_CHAT; p++) if(!strcmp("WHISPER", chatPartner[p])) {
+
+               if(channel >= 0) // channel broadcast; look if there is a chatbox for this channel
+               for(p=0; p<MAX_CHAT; p++) {
+                   if(channel == atoi(chatPartner[p])) {
                    talker[0] = '['; strcat(talker, "]");
                    chattingPartner = p; break;
+                   }
+               } else
+               if(buf[i-3] == 'r') // whisper; look if there is a WHISPER chatbox
+               for(p=0; p<MAX_CHAT; p++) {
+                   if(!strcmp("WHISPER", chatPartner[p])) {
+                       talker[0] = '['; strcat(talker, "]");
+                       chattingPartner = p; break;
+                   }
                }
                if(chattingPartner<0) // if not, look if there is a chatbox for this indivdual
-               for(p=0; p<MAX_CHAT; p++) if(!strcasecmp(talker+1, chatPartner[p])) {
+               for(p=0; p<MAX_CHAT; p++) if(!StrCaseCmp(talker+1, chatPartner[p])) {
                    talker[0] = 0;
                    chattingPartner = p; break;
                }
@@ -4542,7 +4557,6 @@ InitPosition(redraw)
     oldh = gameInfo.holdingsWidth,
     oldv = gameInfo.variant;
 
-    currentMove = forwardMostMove = backwardMostMove = 0;
     if(appData.icsActive) shuffleOpenings = FALSE; // [HGM] shuffle: in ICS mode, only shuffle on ICS request
 
     /* [AS] Initialize pv info list [HGM] and game status */
@@ -5144,6 +5158,12 @@ UserMoveTest(fromX, fromY, toX, toY, promoChar)
                    fprintf(debugFP, "Got premove: fromX %d,"
                            "fromY %d, toX %d, toY %d\n",
                            fromX, fromY, toX, toY);
+               if(!WhiteOnMove(currentMove) && gotPremove == 1) {
+                   // [HGM] race: we must have been hit by an opponent move from the ICS while preparing the premove
+                   if (appData.debugMode) 
+                       fprintf(debugFP, "Execute as normal move\n");
+                   gotPremove = 0; break;
+               }
            }
             return ImpossibleMove;
        }
@@ -5165,6 +5185,12 @@ UserMoveTest(fromX, fromY, toX, toY, promoChar)
                    fprintf(debugFP, "Got premove: fromX %d,"
                            "fromY %d, toX %d, toY %d\n",
                            fromX, fromY, toX, toY);
+               if(WhiteOnMove(currentMove) && gotPremove == 1) {
+                   // [HGM] race: we must have been hit by an opponent move from the ICS while preparing the premove
+                   if (appData.debugMode) 
+                       fprintf(debugFP, "Execute as normal move\n");
+                   gotPremove = 0; break;
+               }
            }
             return ImpossibleMove;
        }
@@ -5426,7 +5452,9 @@ UserMoveEvent(fromX, fromY, toX, toY, promoChar)
     */
     ChessMove moveType = UserMoveTest(fromX, fromY, toX, toY, promoChar);
 if(appData.debugMode) fprintf(debugFP, "moveType 4 = %d, promochar = %x\n", moveType, promoChar);
-    if(moveType != ImpossibleMove)
+    if(moveType == AmbiguousMove)
+       DrawPosition(FALSE, boards[currentMove]);
+    else if(moveType != ImpossibleMove)
         FinishMove(moveType, fromX, fromY, toX, toY, promoChar);
 }
 
@@ -7535,7 +7563,7 @@ void SendEgtPath(ChessProgramState *cps)
                while(*r && *r != ',') r++; // path info is everything upto next ';' or end of string
                c = *r; *r = 0;             // temporarily null-terminate path info
                    *--q = 0;               // strip of trailig ':' from name
-                   sprintf(buf, "egtbpath %s %s\n", name+1, s);
+                   sprintf(buf, "egtpath %s %s\n", name+1, s);
                *r = c;
                SendToProgram(buf,cps);     // send egtbpath command for this format
            }
@@ -8264,6 +8292,7 @@ Reset(redraw, init)
     gameMode = BeginningOfGame;
     ModeHighlight();
     if(appData.icsActive) gameInfo.variant = VariantNormal;
+    currentMove = forwardMostMove = backwardMostMove = 0;
     InitPosition(redraw);
     for (i = 0; i < MAX_MOVES; i++) {
        if (commentList[i] != NULL) {
@@ -10526,6 +10555,7 @@ MachineWhiteEvent()
     SetMachineThinkingEnables();
     first.maybeThinking = TRUE;
     StartClocks();
+    firstMove = FALSE;
 
     if (appData.autoFlipView && !flipView) {
       flipView = !flipView;
@@ -12256,7 +12286,8 @@ ReceiveFromProgram(isr, closure, message, count, error)
                   sscanf(message, "resign%c", &c)!=1 && sscanf(message, "feature %c", &c)!=1 &&
                   sscanf(message, "error %c", &c)!=1 && sscanf(message, "illegal %c", &c)!=1 &&
                   sscanf(message, "tell%c", &c)!=1   && sscanf(message, "0-1 %c", &c)!=1 &&
-                  sscanf(message, "1-0 %c", &c)!=1   && sscanf(message, "1/2-1/2 %c", &c)!=1 && start != '#')
+                  sscanf(message, "1-0 %c", &c)!=1   && sscanf(message, "1/2-1/2 %c", &c)!=1 &&
+                  sscanf(message, "pong %c", &c)!=1   && start != '#')
                        { quote = "# "; print = (appData.engineComments == 2); }
                message[0] = start; // restore original message
        }