removed unused variables (-Wunused-variable)
[xboard.git] / backend.c
1 /*
2  * backend.c -- Common back end for X and Windows NT versions of
3  *
4  * Copyright 1991 by Digital Equipment Corporation, Maynard,
5  * Massachusetts.
6  *
7  * Enhancements Copyright 1992-2001, 2002, 2003, 2004, 2005, 2006,
8  * 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
9  *
10  * Enhancements Copyright 2005 Alessandro Scotti
11  *
12  * The following terms apply to Digital Equipment Corporation's copyright
13  * interest in XBoard:
14  * ------------------------------------------------------------------------
15  * All Rights Reserved
16  *
17  * Permission to use, copy, modify, and distribute this software and its
18  * documentation for any purpose and without fee is hereby granted,
19  * provided that the above copyright notice appear in all copies and that
20  * both that copyright notice and this permission notice appear in
21  * supporting documentation, and that the name of Digital not be
22  * used in advertising or publicity pertaining to distribution of the
23  * software without specific, written prior permission.
24  *
25  * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
26  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
27  * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
28  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
29  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
30  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
31  * SOFTWARE.
32  * ------------------------------------------------------------------------
33  *
34  * The following terms apply to the enhanced version of XBoard
35  * distributed by the Free Software Foundation:
36  * ------------------------------------------------------------------------
37  *
38  * GNU XBoard is free software: you can redistribute it and/or modify
39  * it under the terms of the GNU General Public License as published by
40  * the Free Software Foundation, either version 3 of the License, or (at
41  * your option) any later version.
42  *
43  * GNU XBoard is distributed in the hope that it will be useful, but
44  * WITHOUT ANY WARRANTY; without even the implied warranty of
45  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
46  * General Public License for more details.
47  *
48  * You should have received a copy of the GNU General Public License
49  * along with this program. If not, see http://www.gnu.org/licenses/.  *
50  *
51  *------------------------------------------------------------------------
52  ** See the file ChangeLog for a revision history.  */
53
54 /* [AS] Also useful here for debugging */
55 #ifdef WIN32
56 #include <windows.h>
57
58 int flock(int f, int code);
59 #define LOCK_EX 2
60 #define SLASH '\\'
61
62 #else
63
64 #include <sys/file.h>
65 #define SLASH '/'
66
67 #endif
68
69 #include "config.h"
70
71 #include <assert.h>
72 #include <stdio.h>
73 #include <ctype.h>
74 #include <errno.h>
75 #include <sys/types.h>
76 #include <sys/stat.h>
77 #include <math.h>
78 #include <ctype.h>
79
80 #if STDC_HEADERS
81 # include <stdlib.h>
82 # include <string.h>
83 # include <stdarg.h>
84 #else /* not STDC_HEADERS */
85 # if HAVE_STRING_H
86 #  include <string.h>
87 # else /* not HAVE_STRING_H */
88 #  include <strings.h>
89 # endif /* not HAVE_STRING_H */
90 #endif /* not STDC_HEADERS */
91
92 #if HAVE_SYS_FCNTL_H
93 # include <sys/fcntl.h>
94 #else /* not HAVE_SYS_FCNTL_H */
95 # if HAVE_FCNTL_H
96 #  include <fcntl.h>
97 # endif /* HAVE_FCNTL_H */
98 #endif /* not HAVE_SYS_FCNTL_H */
99
100 #if TIME_WITH_SYS_TIME
101 # include <sys/time.h>
102 # include <time.h>
103 #else
104 # if HAVE_SYS_TIME_H
105 #  include <sys/time.h>
106 # else
107 #  include <time.h>
108 # endif
109 #endif
110
111 #if defined(_amigados) && !defined(__GNUC__)
112 struct timezone {
113     int tz_minuteswest;
114     int tz_dsttime;
115 };
116 extern int gettimeofday(struct timeval *, struct timezone *);
117 #endif
118
119 #if HAVE_UNISTD_H
120 # include <unistd.h>
121 #endif
122
123 #include "common.h"
124 #include "frontend.h"
125 #include "backend.h"
126 #include "parser.h"
127 #include "moves.h"
128 #if ZIPPY
129 # include "zippy.h"
130 #endif
131 #include "backendz.h"
132 #include "gettext.h"
133
134 #ifdef ENABLE_NLS
135 # define _(s) gettext (s)
136 # define N_(s) gettext_noop (s)
137 # define T_(s) gettext(s)
138 #else
139 # ifdef WIN32
140 #   define _(s) T_(s)
141 #   define N_(s) s
142 # else
143 #   define _(s) (s)
144 #   define N_(s) s
145 #   define T_(s) s
146 # endif
147 #endif
148
149
150 int establish P((void));
151 void read_from_player P((InputSourceRef isr, VOIDSTAR closure,
152                          char *buf, int count, int error));
153 void read_from_ics P((InputSourceRef isr, VOIDSTAR closure,
154                       char *buf, int count, int error));
155 void ics_printf P((char *format, ...));
156 void SendToICS P((char *s));
157 void SendToICSDelayed P((char *s, long msdelay));
158 void SendMoveToICS P((ChessMove moveType, int fromX, int fromY, int toX, int toY, char promoChar));
159 void HandleMachineMove P((char *message, ChessProgramState *cps));
160 int AutoPlayOneMove P((void));
161 int LoadGameOneMove P((ChessMove readAhead));
162 int LoadGameFromFile P((char *filename, int n, char *title, int useList));
163 int LoadPositionFromFile P((char *filename, int n, char *title));
164 int SavePositionToFile P((char *filename));
165 void MakeMove P((int fromX, int fromY, int toX, int toY, int promoChar));
166 void ShowMove P((int fromX, int fromY, int toX, int toY));
167 int FinishMove P((ChessMove moveType, int fromX, int fromY, int toX, int toY,
168                    /*char*/int promoChar));
169 void BackwardInner P((int target));
170 void ForwardInner P((int target));
171 int Adjudicate P((ChessProgramState *cps));
172 void GameEnds P((ChessMove result, char *resultDetails, int whosays));
173 void EditPositionDone P((Boolean fakeRights));
174 void PrintOpponents P((FILE *fp));
175 void PrintPosition P((FILE *fp, int move));
176 void StartChessProgram P((ChessProgramState *cps));
177 void SendToProgram P((char *message, ChessProgramState *cps));
178 void SendMoveToProgram P((int moveNum, ChessProgramState *cps));
179 void ReceiveFromProgram P((InputSourceRef isr, VOIDSTAR closure,
180                            char *buf, int count, int error));
181 void SendTimeControl P((ChessProgramState *cps,
182                         int mps, long tc, int inc, int sd, int st));
183 char *TimeControlTagValue P((void));
184 void Attention P((ChessProgramState *cps));
185 void FeedMovesToProgram P((ChessProgramState *cps, int upto));
186 int ResurrectChessProgram P((void));
187 void DisplayComment P((int moveNumber, char *text));
188 void DisplayMove P((int moveNumber));
189
190 void ParseGameHistory P((char *game));
191 void ParseBoard12 P((char *string));
192 void KeepAlive P((void));
193 void StartClocks P((void));
194 void SwitchClocks P((int nr));
195 void StopClocks P((void));
196 void ResetClocks P((void));
197 char *PGNDate P((void));
198 void SetGameInfo P((void));
199 int RegisterMove P((void));
200 void MakeRegisteredMove P((void));
201 void TruncateGame P((void));
202 int looking_at P((char *, int *, char *));
203 void CopyPlayerNameIntoFileName P((char **, char *));
204 char *SavePart P((char *));
205 int SaveGameOldStyle P((FILE *));
206 int SaveGamePGN P((FILE *));
207 int CheckFlags P((void));
208 long NextTickLength P((long));
209 void CheckTimeControl P((void));
210 void show_bytes P((FILE *, char *, int));
211 int string_to_rating P((char *str));
212 void ParseFeatures P((char* args, ChessProgramState *cps));
213 void InitBackEnd3 P((void));
214 void FeatureDone P((ChessProgramState* cps, int val));
215 void InitChessProgram P((ChessProgramState *cps, int setup));
216 void OutputKibitz(int window, char *text);
217 int PerpetualChase(int first, int last);
218 int EngineOutputIsUp();
219 void InitDrawingSizes(int x, int y);
220 void NextMatchGame P((void));
221 int NextTourneyGame P((int nr, int *swap));
222 int Pairing P((int nr, int nPlayers, int *w, int *b, int *sync));
223 FILE *WriteTourneyFile P((char *results, FILE *f));
224 void DisplayTwoMachinesTitle P(());
225 static void ExcludeClick P((int index));
226
227 #ifdef WIN32
228        extern void ConsoleCreate();
229 #endif
230
231 ChessProgramState *WhitePlayer();
232 void InsertIntoMemo P((int which, char *text)); // [HGM] kibitz: in engineo.c
233 int VerifyDisplayMode P(());
234
235 char *GetInfoFromComment( int, char * ); // [HGM] PV time: returns stripped comment
236 void InitEngineUCI( const char * iniDir, ChessProgramState * cps ); // [HGM] moved here from winboard.c
237 char *ProbeBook P((int moveNr, char *book)); // [HGM] book: returns a book move
238 char *SendMoveToBookUser P((int nr, ChessProgramState *cps, int initial)); // [HGM] book
239 void ics_update_width P((int new_width));
240 extern char installDir[MSG_SIZ];
241 VariantClass startVariant; /* [HGM] nicks: initial variant */
242 Boolean abortMatch;
243
244 extern int tinyLayout, smallLayout;
245 ChessProgramStats programStats;
246 char lastPV[2][2*MSG_SIZ]; /* [HGM] pv: last PV in thinking output of each engine */
247 int endPV = -1;
248 static int exiting = 0; /* [HGM] moved to top */
249 static int setboardSpoiledMachineBlack = 0 /*, errorExitFlag = 0*/;
250 int startedFromPositionFile = FALSE; Board filePosition;       /* [HGM] loadPos */
251 Board partnerBoard;     /* [HGM] bughouse: for peeking at partner game          */
252 int partnerHighlight[2];
253 Boolean partnerBoardValid = 0;
254 char partnerStatus[MSG_SIZ];
255 Boolean partnerUp;
256 Boolean originalFlip;
257 Boolean twoBoards = 0;
258 char endingGame = 0;    /* [HGM] crash: flag to prevent recursion of GameEnds() */
259 int whiteNPS, blackNPS; /* [HGM] nps: for easily making clocks aware of NPS     */
260 VariantClass currentlyInitializedVariant; /* [HGM] variantswitch */
261 int lastIndex = 0;      /* [HGM] autoinc: last game/position used in match mode */
262 Boolean connectionAlive;/* [HGM] alive: ICS connection status from probing      */
263 int opponentKibitzes;
264 int lastSavedGame; /* [HGM] save: ID of game */
265 char chatPartner[MAX_CHAT][MSG_SIZ]; /* [HGM] chat: list of chatting partners */
266 extern int chatCount;
267 int chattingPartner;
268 char marker[BOARD_RANKS][BOARD_FILES]; /* [HGM] marks for target squares */
269 char lastMsg[MSG_SIZ];
270 ChessSquare pieceSweep = EmptySquare;
271 ChessSquare promoSweep = EmptySquare, defaultPromoChoice;
272 int promoDefaultAltered;
273
274 /* States for ics_getting_history */
275 #define H_FALSE 0
276 #define H_REQUESTED 1
277 #define H_GOT_REQ_HEADER 2
278 #define H_GOT_UNREQ_HEADER 3
279 #define H_GETTING_MOVES 4
280 #define H_GOT_UNWANTED_HEADER 5
281
282 /* whosays values for GameEnds */
283 #define GE_ICS 0
284 #define GE_ENGINE 1
285 #define GE_PLAYER 2
286 #define GE_FILE 3
287 #define GE_XBOARD 4
288 #define GE_ENGINE1 5
289 #define GE_ENGINE2 6
290
291 /* Maximum number of games in a cmail message */
292 #define CMAIL_MAX_GAMES 20
293
294 /* Different types of move when calling RegisterMove */
295 #define CMAIL_MOVE   0
296 #define CMAIL_RESIGN 1
297 #define CMAIL_DRAW   2
298 #define CMAIL_ACCEPT 3
299
300 /* Different types of result to remember for each game */
301 #define CMAIL_NOT_RESULT 0
302 #define CMAIL_OLD_RESULT 1
303 #define CMAIL_NEW_RESULT 2
304
305 /* Telnet protocol constants */
306 #define TN_WILL 0373
307 #define TN_WONT 0374
308 #define TN_DO   0375
309 #define TN_DONT 0376
310 #define TN_IAC  0377
311 #define TN_ECHO 0001
312 #define TN_SGA  0003
313 #define TN_PORT 23
314
315 char*
316 safeStrCpy (char *dst, const char *src, size_t count)
317 { // [HGM] made safe
318   int i;
319   assert( dst != NULL );
320   assert( src != NULL );
321   assert( count > 0 );
322
323   for(i=0; i<count; i++) if((dst[i] = src[i]) == NULLCHAR) break;
324   if(  i == count && dst[count-1] != NULLCHAR)
325     {
326       dst[ count-1 ] = '\0'; // make sure incomplete copy still null-terminated
327       if(appData.debugMode)
328       fprintf(debugFP, "safeStrCpy: copying %s into %s didn't work, not enough space %d\n",src,dst, (int)count);
329     }
330
331   return dst;
332 }
333
334 /* Some compiler can't cast u64 to double
335  * This function do the job for us:
336
337  * We use the highest bit for cast, this only
338  * works if the highest bit is not
339  * in use (This should not happen)
340  *
341  * We used this for all compiler
342  */
343 double
344 u64ToDouble (u64 value)
345 {
346   double r;
347   u64 tmp = value & u64Const(0x7fffffffffffffff);
348   r = (double)(s64)tmp;
349   if (value & u64Const(0x8000000000000000))
350        r +=  9.2233720368547758080e18; /* 2^63 */
351  return r;
352 }
353
354 /* Fake up flags for now, as we aren't keeping track of castling
355    availability yet. [HGM] Change of logic: the flag now only
356    indicates the type of castlings allowed by the rule of the game.
357    The actual rights themselves are maintained in the array
358    castlingRights, as part of the game history, and are not probed
359    by this function.
360  */
361 int
362 PosFlags (index)
363 {
364   int flags = F_ALL_CASTLE_OK;
365   if ((index % 2) == 0) flags |= F_WHITE_ON_MOVE;
366   switch (gameInfo.variant) {
367   case VariantSuicide:
368     flags &= ~F_ALL_CASTLE_OK;
369   case VariantGiveaway:         // [HGM] moved this case label one down: seems Giveaway does have castling on ICC!
370     flags |= F_IGNORE_CHECK;
371   case VariantLosers:
372     flags |= F_MANDATORY_CAPTURE; //[HGM] losers: sets flag so TestLegality rejects non-capts if capts exist
373     break;
374   case VariantAtomic:
375     flags |= F_IGNORE_CHECK | F_ATOMIC_CAPTURE;
376     break;
377   case VariantKriegspiel:
378     flags |= F_KRIEGSPIEL_CAPTURE;
379     break;
380   case VariantCapaRandom:
381   case VariantFischeRandom:
382     flags |= F_FRC_TYPE_CASTLING; /* [HGM] enable this through flag */
383   case VariantNoCastle:
384   case VariantShatranj:
385   case VariantCourier:
386   case VariantMakruk:
387   case VariantGrand:
388     flags &= ~F_ALL_CASTLE_OK;
389     break;
390   default:
391     break;
392   }
393   return flags;
394 }
395
396 FILE *gameFileFP, *debugFP, *serverFP;
397 char *currentDebugFile; // [HGM] debug split: to remember name
398
399 /*
400     [AS] Note: sometimes, the sscanf() function is used to parse the input
401     into a fixed-size buffer. Because of this, we must be prepared to
402     receive strings as long as the size of the input buffer, which is currently
403     set to 4K for Windows and 8K for the rest.
404     So, we must either allocate sufficiently large buffers here, or
405     reduce the size of the input buffer in the input reading part.
406 */
407
408 char cmailMove[CMAIL_MAX_GAMES][MOVE_LEN], cmailMsg[MSG_SIZ];
409 char bookOutput[MSG_SIZ*10], thinkOutput[MSG_SIZ*10], lastHint[MSG_SIZ];
410 char thinkOutput1[MSG_SIZ*10];
411
412 ChessProgramState first, second, pairing;
413
414 /* premove variables */
415 int premoveToX = 0;
416 int premoveToY = 0;
417 int premoveFromX = 0;
418 int premoveFromY = 0;
419 int premovePromoChar = 0;
420 int gotPremove = 0;
421 Boolean alarmSounded;
422 /* end premove variables */
423
424 char *ics_prefix = "$";
425 int ics_type = ICS_GENERIC;
426
427 int currentMove = 0, forwardMostMove = 0, backwardMostMove = 0;
428 int pauseExamForwardMostMove = 0;
429 int nCmailGames = 0, nCmailResults = 0, nCmailMovesRegistered = 0;
430 int cmailMoveRegistered[CMAIL_MAX_GAMES], cmailResult[CMAIL_MAX_GAMES];
431 int cmailMsgLoaded = FALSE, cmailMailedMove = FALSE;
432 int cmailOldMove = -1, firstMove = TRUE, flipView = FALSE;
433 int blackPlaysFirst = FALSE, startedFromSetupPosition = FALSE;
434 int searchTime = 0, pausing = FALSE, pauseExamInvalid = FALSE;
435 int whiteFlag = FALSE, blackFlag = FALSE;
436 int userOfferedDraw = FALSE;
437 int ics_user_moved = 0, ics_gamenum = -1, ics_getting_history = H_FALSE;
438 int matchMode = FALSE, hintRequested = FALSE, bookRequested = FALSE;
439 int cmailMoveType[CMAIL_MAX_GAMES];
440 long ics_clock_paused = 0;
441 ProcRef icsPR = NoProc, cmailPR = NoProc;
442 InputSourceRef telnetISR = NULL, fromUserISR = NULL, cmailISR = NULL;
443 GameMode gameMode = BeginningOfGame;
444 char moveList[MAX_MOVES][MOVE_LEN], parseList[MAX_MOVES][MOVE_LEN * 2];
445 char *commentList[MAX_MOVES], *cmailCommentList[CMAIL_MAX_GAMES];
446 ChessProgramStats_Move pvInfoList[MAX_MOVES]; /* [AS] Info about engine thinking */
447 int hiddenThinkOutputState = 0; /* [AS] */
448 int adjudicateLossThreshold = 0; /* [AS] Automatic adjudication */
449 int adjudicateLossPlies = 6;
450 char white_holding[64], black_holding[64];
451 TimeMark lastNodeCountTime;
452 long lastNodeCount=0;
453 int shiftKey, controlKey; // [HGM] set by mouse handler
454
455 int have_sent_ICS_logon = 0;
456 int movesPerSession;
457 int suddenDeath, whiteStartMove, blackStartMove; /* [HGM] for implementation of 'any per time' sessions, as in first part of byoyomi TC */
458 long whiteTimeRemaining, blackTimeRemaining, timeControl, timeIncrement, lastWhite, lastBlack, activePartnerTime;
459 Boolean adjustedClock;
460 long timeControl_2; /* [AS] Allow separate time controls */
461 char *fullTimeControlString = NULL, *nextSession, *whiteTC, *blackTC, activePartner; /* [HGM] secondary TC: merge of MPS, TC and inc */
462 long timeRemaining[2][MAX_MOVES];
463 int matchGame = 0, nextGame = 0, roundNr = 0;
464 Boolean waitingForGame = FALSE;
465 TimeMark programStartTime, pauseStart;
466 char ics_handle[MSG_SIZ];
467 int have_set_title = 0;
468
469 /* animateTraining preserves the state of appData.animate
470  * when Training mode is activated. This allows the
471  * response to be animated when appData.animate == TRUE and
472  * appData.animateDragging == TRUE.
473  */
474 Boolean animateTraining;
475
476 GameInfo gameInfo;
477
478 AppData appData;
479
480 Board boards[MAX_MOVES];
481 /* [HGM] Following 7 needed for accurate legality tests: */
482 signed char  castlingRank[BOARD_FILES]; // and corresponding ranks
483 signed char  initialRights[BOARD_FILES];
484 int   nrCastlingRights; // For TwoKings, or to implement castling-unknown status
485 int   initialRulePlies, FENrulePlies;
486 FILE  *serverMoves = NULL; // next two for broadcasting (/serverMoves option)
487 int loadFlag = 0;
488 Boolean shuffleOpenings;
489 int mute; // mute all sounds
490
491 // [HGM] vari: next 12 to save and restore variations
492 #define MAX_VARIATIONS 10
493 int framePtr = MAX_MOVES-1; // points to free stack entry
494 int storedGames = 0;
495 int savedFirst[MAX_VARIATIONS];
496 int savedLast[MAX_VARIATIONS];
497 int savedFramePtr[MAX_VARIATIONS];
498 char *savedDetails[MAX_VARIATIONS];
499 ChessMove savedResult[MAX_VARIATIONS];
500
501 void PushTail P((int firstMove, int lastMove));
502 Boolean PopTail P((Boolean annotate));
503 void PushInner P((int firstMove, int lastMove));
504 void PopInner P((Boolean annotate));
505 void CleanupTail P((void));
506
507 ChessSquare  FIDEArray[2][BOARD_FILES] = {
508     { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
509         WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
510     { BlackRook, BlackKnight, BlackBishop, BlackQueen,
511         BlackKing, BlackBishop, BlackKnight, BlackRook }
512 };
513
514 ChessSquare twoKingsArray[2][BOARD_FILES] = {
515     { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
516         WhiteKing, WhiteKing, WhiteKnight, WhiteRook },
517     { BlackRook, BlackKnight, BlackBishop, BlackQueen,
518         BlackKing, BlackKing, BlackKnight, BlackRook }
519 };
520
521 ChessSquare  KnightmateArray[2][BOARD_FILES] = {
522     { WhiteRook, WhiteMan, WhiteBishop, WhiteQueen,
523         WhiteUnicorn, WhiteBishop, WhiteMan, WhiteRook },
524     { BlackRook, BlackMan, BlackBishop, BlackQueen,
525         BlackUnicorn, BlackBishop, BlackMan, BlackRook }
526 };
527
528 ChessSquare SpartanArray[2][BOARD_FILES] = {
529     { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
530         WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
531     { BlackAlfil, BlackMarshall, BlackKing, BlackDragon,
532         BlackDragon, BlackKing, BlackAngel, BlackAlfil }
533 };
534
535 ChessSquare fairyArray[2][BOARD_FILES] = { /* [HGM] Queen side differs from King side */
536     { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
537         WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
538     { BlackCardinal, BlackAlfil, BlackMarshall, BlackAngel,
539         BlackKing, BlackMarshall, BlackAlfil, BlackCardinal }
540 };
541
542 ChessSquare ShatranjArray[2][BOARD_FILES] = { /* [HGM] (movGen knows about Shatranj Q and P) */
543     { WhiteRook, WhiteKnight, WhiteAlfil, WhiteKing,
544         WhiteFerz, WhiteAlfil, WhiteKnight, WhiteRook },
545     { BlackRook, BlackKnight, BlackAlfil, BlackKing,
546         BlackFerz, BlackAlfil, BlackKnight, BlackRook }
547 };
548
549 ChessSquare makrukArray[2][BOARD_FILES] = { /* [HGM] (movGen knows about Shatranj Q and P) */
550     { WhiteRook, WhiteKnight, WhiteMan, WhiteKing,
551         WhiteFerz, WhiteMan, WhiteKnight, WhiteRook },
552     { BlackRook, BlackKnight, BlackMan, BlackFerz,
553         BlackKing, BlackMan, BlackKnight, BlackRook }
554 };
555
556
557 #if (BOARD_FILES>=10)
558 ChessSquare ShogiArray[2][BOARD_FILES] = {
559     { WhiteQueen, WhiteKnight, WhiteFerz, WhiteWazir,
560         WhiteKing, WhiteWazir, WhiteFerz, WhiteKnight, WhiteQueen },
561     { BlackQueen, BlackKnight, BlackFerz, BlackWazir,
562         BlackKing, BlackWazir, BlackFerz, BlackKnight, BlackQueen }
563 };
564
565 ChessSquare XiangqiArray[2][BOARD_FILES] = {
566     { WhiteRook, WhiteKnight, WhiteAlfil, WhiteFerz,
567         WhiteWazir, WhiteFerz, WhiteAlfil, WhiteKnight, WhiteRook },
568     { BlackRook, BlackKnight, BlackAlfil, BlackFerz,
569         BlackWazir, BlackFerz, BlackAlfil, BlackKnight, BlackRook }
570 };
571
572 ChessSquare CapablancaArray[2][BOARD_FILES] = {
573     { WhiteRook, WhiteKnight, WhiteAngel, WhiteBishop, WhiteQueen,
574         WhiteKing, WhiteBishop, WhiteMarshall, WhiteKnight, WhiteRook },
575     { BlackRook, BlackKnight, BlackAngel, BlackBishop, BlackQueen,
576         BlackKing, BlackBishop, BlackMarshall, BlackKnight, BlackRook }
577 };
578
579 ChessSquare GreatArray[2][BOARD_FILES] = {
580     { WhiteDragon, WhiteKnight, WhiteAlfil, WhiteGrasshopper, WhiteKing,
581         WhiteSilver, WhiteCardinal, WhiteAlfil, WhiteKnight, WhiteDragon },
582     { BlackDragon, BlackKnight, BlackAlfil, BlackGrasshopper, BlackKing,
583         BlackSilver, BlackCardinal, BlackAlfil, BlackKnight, BlackDragon },
584 };
585
586 ChessSquare JanusArray[2][BOARD_FILES] = {
587     { WhiteRook, WhiteAngel, WhiteKnight, WhiteBishop, WhiteKing,
588         WhiteQueen, WhiteBishop, WhiteKnight, WhiteAngel, WhiteRook },
589     { BlackRook, BlackAngel, BlackKnight, BlackBishop, BlackKing,
590         BlackQueen, BlackBishop, BlackKnight, BlackAngel, BlackRook }
591 };
592
593 ChessSquare GrandArray[2][BOARD_FILES] = {
594     { EmptySquare, WhiteKnight, WhiteBishop, WhiteQueen, WhiteKing,
595         WhiteMarshall, WhiteAngel, WhiteBishop, WhiteKnight, EmptySquare },
596     { EmptySquare, BlackKnight, BlackBishop, BlackQueen, BlackKing,
597         BlackMarshall, BlackAngel, BlackBishop, BlackKnight, EmptySquare }
598 };
599
600 #ifdef GOTHIC
601 ChessSquare GothicArray[2][BOARD_FILES] = {
602     { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen, WhiteMarshall,
603         WhiteKing, WhiteAngel, WhiteBishop, WhiteKnight, WhiteRook },
604     { BlackRook, BlackKnight, BlackBishop, BlackQueen, BlackMarshall,
605         BlackKing, BlackAngel, BlackBishop, BlackKnight, BlackRook }
606 };
607 #else // !GOTHIC
608 #define GothicArray CapablancaArray
609 #endif // !GOTHIC
610
611 #ifdef FALCON
612 ChessSquare FalconArray[2][BOARD_FILES] = {
613     { WhiteRook, WhiteKnight, WhiteBishop, WhiteFalcon, WhiteQueen,
614         WhiteKing, WhiteFalcon, WhiteBishop, WhiteKnight, WhiteRook },
615     { BlackRook, BlackKnight, BlackBishop, BlackFalcon, BlackQueen,
616         BlackKing, BlackFalcon, BlackBishop, BlackKnight, BlackRook }
617 };
618 #else // !FALCON
619 #define FalconArray CapablancaArray
620 #endif // !FALCON
621
622 #else // !(BOARD_FILES>=10)
623 #define XiangqiPosition FIDEArray
624 #define CapablancaArray FIDEArray
625 #define GothicArray FIDEArray
626 #define GreatArray FIDEArray
627 #endif // !(BOARD_FILES>=10)
628
629 #if (BOARD_FILES>=12)
630 ChessSquare CourierArray[2][BOARD_FILES] = {
631     { WhiteRook, WhiteKnight, WhiteAlfil, WhiteBishop, WhiteMan, WhiteKing,
632         WhiteFerz, WhiteWazir, WhiteBishop, WhiteAlfil, WhiteKnight, WhiteRook },
633     { BlackRook, BlackKnight, BlackAlfil, BlackBishop, BlackMan, BlackKing,
634         BlackFerz, BlackWazir, BlackBishop, BlackAlfil, BlackKnight, BlackRook }
635 };
636 #else // !(BOARD_FILES>=12)
637 #define CourierArray CapablancaArray
638 #endif // !(BOARD_FILES>=12)
639
640
641 Board initialPosition;
642
643
644 /* Convert str to a rating. Checks for special cases of "----",
645
646    "++++", etc. Also strips ()'s */
647 int
648 string_to_rating (char *str)
649 {
650   while(*str && !isdigit(*str)) ++str;
651   if (!*str)
652     return 0;   /* One of the special "no rating" cases */
653   else
654     return atoi(str);
655 }
656
657 void
658 ClearProgramStats ()
659 {
660     /* Init programStats */
661     programStats.movelist[0] = 0;
662     programStats.depth = 0;
663     programStats.nr_moves = 0;
664     programStats.moves_left = 0;
665     programStats.nodes = 0;
666     programStats.time = -1;        // [HGM] PGNtime: make invalid to recognize engine output
667     programStats.score = 0;
668     programStats.got_only_move = 0;
669     programStats.got_fail = 0;
670     programStats.line_is_book = 0;
671 }
672
673 void
674 CommonEngineInit ()
675 {   // [HGM] moved some code here from InitBackend1 that has to be done after both engines have contributed their settings
676     if (appData.firstPlaysBlack) {
677         first.twoMachinesColor = "black\n";
678         second.twoMachinesColor = "white\n";
679     } else {
680         first.twoMachinesColor = "white\n";
681         second.twoMachinesColor = "black\n";
682     }
683
684     first.other = &second;
685     second.other = &first;
686
687     { float norm = 1;
688         if(appData.timeOddsMode) {
689             norm = appData.timeOdds[0];
690             if(norm > appData.timeOdds[1]) norm = appData.timeOdds[1];
691         }
692         first.timeOdds  = appData.timeOdds[0]/norm;
693         second.timeOdds = appData.timeOdds[1]/norm;
694     }
695
696     if(programVersion) free(programVersion);
697     if (appData.noChessProgram) {
698         programVersion = (char*) malloc(5 + strlen(PACKAGE_STRING));
699         sprintf(programVersion, "%s", PACKAGE_STRING);
700     } else {
701       /* [HGM] tidy: use tidy name, in stead of full pathname (which was probably a bug due to / vs \ ) */
702       programVersion = (char*) malloc(8 + strlen(PACKAGE_STRING) + strlen(first.tidy));
703       sprintf(programVersion, "%s + %s", PACKAGE_STRING, first.tidy);
704     }
705 }
706
707 void
708 UnloadEngine (ChessProgramState *cps)
709 {
710         /* Kill off first chess program */
711         if (cps->isr != NULL)
712           RemoveInputSource(cps->isr);
713         cps->isr = NULL;
714
715         if (cps->pr != NoProc) {
716             ExitAnalyzeMode();
717             DoSleep( appData.delayBeforeQuit );
718             SendToProgram("quit\n", cps);
719             DoSleep( appData.delayAfterQuit );
720             DestroyChildProcess(cps->pr, cps->useSigterm);
721         }
722         cps->pr = NoProc;
723         if(appData.debugMode) fprintf(debugFP, "Unload %s\n", cps->which);
724 }
725
726 void
727 ClearOptions (ChessProgramState *cps)
728 {
729     int i;
730     cps->nrOptions = cps->comboCnt = 0;
731     for(i=0; i<MAX_OPTIONS; i++) {
732         cps->option[i].min = cps->option[i].max = cps->option[i].value = 0;
733         cps->option[i].textValue = 0;
734     }
735 }
736
737 char *engineNames[] = {
738   /* TRANSLATORS: "first" is the first of possible two chess engines. It is inserted into strings
739      such as "%s engine" / "%s chess program" / "%s machine" - all meaning the same thing */
740 N_("first"),
741   /* TRANSLATORS: "second" is the second of possible two chess engines. It is inserted into strings
742      such as "%s engine" / "%s chess program" / "%s machine" - all meaning the same thing */
743 N_("second")
744 };
745
746 void
747 InitEngine (ChessProgramState *cps, int n)
748 {   // [HGM] all engine initialiation put in a function that does one engine
749
750     ClearOptions(cps);
751
752     cps->which = engineNames[n];
753     cps->maybeThinking = FALSE;
754     cps->pr = NoProc;
755     cps->isr = NULL;
756     cps->sendTime = 2;
757     cps->sendDrawOffers = 1;
758
759     cps->program = appData.chessProgram[n];
760     cps->host = appData.host[n];
761     cps->dir = appData.directory[n];
762     cps->initString = appData.engInitString[n];
763     cps->computerString = appData.computerString[n];
764     cps->useSigint  = TRUE;
765     cps->useSigterm = TRUE;
766     cps->reuse = appData.reuse[n];
767     cps->nps = appData.NPS[n];   // [HGM] nps: copy nodes per second
768     cps->useSetboard = FALSE;
769     cps->useSAN = FALSE;
770     cps->usePing = FALSE;
771     cps->lastPing = 0;
772     cps->lastPong = 0;
773     cps->usePlayother = FALSE;
774     cps->useColors = TRUE;
775     cps->useUsermove = FALSE;
776     cps->sendICS = FALSE;
777     cps->sendName = appData.icsActive;
778     cps->sdKludge = FALSE;
779     cps->stKludge = FALSE;
780     TidyProgramName(cps->program, cps->host, cps->tidy);
781     cps->matchWins = 0;
782     safeStrCpy(cps->variants, appData.variant, MSG_SIZ);
783     cps->analysisSupport = 2; /* detect */
784     cps->analyzing = FALSE;
785     cps->initDone = FALSE;
786
787     /* New features added by Tord: */
788     cps->useFEN960 = FALSE;
789     cps->useOOCastle = TRUE;
790     /* End of new features added by Tord. */
791     cps->fenOverride  = appData.fenOverride[n];
792
793     /* [HGM] time odds: set factor for each machine */
794     cps->timeOdds  = appData.timeOdds[n];
795
796     /* [HGM] secondary TC: how to handle sessions that do not fit in 'level'*/
797     cps->accumulateTC = appData.accumulateTC[n];
798     cps->maxNrOfSessions = 1;
799
800     /* [HGM] debug */
801     cps->debug = FALSE;
802
803     cps->supportsNPS = UNKNOWN;
804     cps->memSize = FALSE;
805     cps->maxCores = FALSE;
806     cps->egtFormats[0] = NULLCHAR;
807
808     /* [HGM] options */
809     cps->optionSettings  = appData.engOptions[n];
810
811     cps->scoreIsAbsolute = appData.scoreIsAbsolute[n]; /* [AS] */
812     cps->isUCI = appData.isUCI[n]; /* [AS] */
813     cps->hasOwnBookUCI = appData.hasOwnBookUCI[n]; /* [AS] */
814
815     if (appData.protocolVersion[n] > PROTOVER
816         || appData.protocolVersion[n] < 1)
817       {
818         char buf[MSG_SIZ];
819         int len;
820
821         len = snprintf(buf, MSG_SIZ, _("protocol version %d not supported"),
822                        appData.protocolVersion[n]);
823         if( (len >= MSG_SIZ) && appData.debugMode )
824           fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
825
826         DisplayFatalError(buf, 0, 2);
827       }
828     else
829       {
830         cps->protocolVersion = appData.protocolVersion[n];
831       }
832
833     InitEngineUCI( installDir, cps );  // [HGM] moved here from winboard.c, to make available in xboard
834     ParseFeatures(appData.featureDefaults, cps);
835 }
836
837 ChessProgramState *savCps;
838
839 void
840 LoadEngine ()
841 {
842     int i;
843     if(WaitForEngine(savCps, LoadEngine)) return;
844     CommonEngineInit(); // recalculate time odds
845     if(gameInfo.variant != StringToVariant(appData.variant)) {
846         // we changed variant when loading the engine; this forces us to reset
847         Reset(TRUE, savCps != &first);
848         EditGameEvent(); // for consistency with other path, as Reset changes mode
849     }
850     InitChessProgram(savCps, FALSE);
851     SendToProgram("force\n", savCps);
852     DisplayMessage("", "");
853     if (startedFromSetupPosition) SendBoard(savCps, backwardMostMove);
854     for (i = backwardMostMove; i < currentMove; i++) SendMoveToProgram(i, savCps);
855     ThawUI();
856     SetGNUMode();
857 }
858
859 void
860 ReplaceEngine (ChessProgramState *cps, int n)
861 {
862     EditGameEvent();
863     UnloadEngine(cps);
864     appData.noChessProgram = FALSE;
865     appData.clockMode = TRUE;
866     InitEngine(cps, n);
867     UpdateLogos(TRUE);
868     if(n) return; // only startup first engine immediately; second can wait
869     savCps = cps; // parameter to LoadEngine passed as globals, to allow scheduled calling :-(
870     LoadEngine();
871 }
872
873 extern char *engineName, *engineDir, *engineChoice, *engineLine, *nickName, *params;
874 extern Boolean isUCI, hasBook, storeVariant, v1, addToList, useNick;
875
876 static char resetOptions[] = 
877         "-reuse -firstIsUCI false -firstHasOwnBookUCI true -firstTimeOdds 1 "
878         "-firstInitString \"" INIT_STRING "\" -firstComputerString \"" COMPUTER_STRING "\" "
879         "-firstFeatures \"\" -firstLogo \"\" -firstAccumulateTC 1 "
880         "-firstOptions \"\" -firstNPS -1 -fn \"\" -firstScoreAbs false";
881
882 void
883 FloatToFront(char **list, char *engineLine)
884 {
885     char buf[MSG_SIZ], tidy[MSG_SIZ], *p = buf, *q, *r = buf;
886     int i=0;
887     if(appData.recentEngines <= 0) return;
888     TidyProgramName(engineLine, "localhost", tidy+1);
889     tidy[0] = buf[0] = '\n'; strcat(tidy, "\n");
890     strncpy(buf+1, *list, MSG_SIZ-50);
891     if(p = strstr(buf, tidy)) { // tidy name appears in list
892         q = strchr(++p, '\n'); if(q == NULL) return; // malformed, don't touch
893         while(*p++ = *++q); // squeeze out
894     }
895     strcat(tidy, buf+1); // put list behind tidy name
896     p = tidy + 1; while(q = strchr(p, '\n')) i++, r = p, p = q + 1; // count entries in new list
897     if(i > appData.recentEngines) *r = NULLCHAR; // if maximum rached, strip off last
898     ASSIGN(*list, tidy+1);
899 }
900
901 char *insert, *wbOptions; // point in ChessProgramNames were we should insert new engine
902
903 void
904 Load (ChessProgramState *cps, int i)
905 {
906     char *p, *q, buf[MSG_SIZ], command[MSG_SIZ], buf2[MSG_SIZ];
907     if(engineLine && engineLine[0]) { // an engine was selected from the combo box
908         snprintf(buf, MSG_SIZ, "-fcp %s", engineLine);
909         SwapEngines(i); // kludge to parse -f* / -first* like it is -s* / -second*
910         ParseArgsFromString(resetOptions); appData.pvSAN[0] = FALSE;
911         FREE(appData.fenOverride[0]); appData.fenOverride[0] = NULL;
912         appData.firstProtocolVersion = PROTOVER;
913         ParseArgsFromString(buf);
914         SwapEngines(i);
915         ReplaceEngine(cps, i);
916         FloatToFront(&appData.recentEngineList, engineLine);
917         return;
918     }
919     p = engineName;
920     while(q = strchr(p, SLASH)) p = q+1;
921     if(*p== NULLCHAR) { DisplayError(_("You did not specify the engine executable"), 0); return; }
922     if(engineDir[0] != NULLCHAR) {
923         ASSIGN(appData.directory[i], engineDir); p = engineName;
924     } else if(p != engineName) { // derive directory from engine path, when not given
925         p[-1] = 0;
926         ASSIGN(appData.directory[i], engineName);
927         p[-1] = SLASH;
928         if(SLASH == '/' && p - engineName > 1) *(p -= 2) = '.'; // for XBoard use ./exeName as command after split!
929     } else { ASSIGN(appData.directory[i], "."); }
930     if(params[0]) {
931         if(strchr(p, ' ') && !strchr(p, '"')) snprintf(buf2, MSG_SIZ, "\"%s\"", p), p = buf2; // quote if it contains spaces
932         snprintf(command, MSG_SIZ, "%s %s", p, params);
933         p = command;
934     }
935     ASSIGN(appData.chessProgram[i], p);
936     appData.isUCI[i] = isUCI;
937     appData.protocolVersion[i] = v1 ? 1 : PROTOVER;
938     appData.hasOwnBookUCI[i] = hasBook;
939     if(!nickName[0]) useNick = FALSE;
940     if(useNick) ASSIGN(appData.pgnName[i], nickName);
941     if(addToList) {
942         int len;
943         char quote;
944         q = firstChessProgramNames;
945         if(nickName[0]) snprintf(buf, MSG_SIZ, "\"%s\" -fcp ", nickName); else buf[0] = NULLCHAR;
946         quote = strchr(p, '"') ? '\'' : '"'; // use single quotes around engine command if it contains double quotes
947         snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), "%c%s%c -fd \"%s\"%s%s%s%s%s%s%s%s\n",
948                         quote, p, quote, appData.directory[i], 
949                         useNick ? " -fn \"" : "",
950                         useNick ? nickName : "",
951                         useNick ? "\"" : "",
952                         v1 ? " -firstProtocolVersion 1" : "",
953                         hasBook ? "" : " -fNoOwnBookUCI",
954                         isUCI ? (isUCI == TRUE ? " -fUCI" : gameInfo.variant == VariantShogi ? " -fUSI" : " -fUCCI") : "",
955                         storeVariant ? " -variant " : "",
956                         storeVariant ? VariantName(gameInfo.variant) : "");
957         if(wbOptions && wbOptions[0]) snprintf(buf+strlen(buf)-1, MSG_SIZ-strlen(buf), " %s\n", wbOptions);
958         firstChessProgramNames = malloc(len = strlen(q) + strlen(buf) + 1);
959         if(insert != q) insert[-1] = NULLCHAR;
960         snprintf(firstChessProgramNames, len, "%s\n%s%s", q, buf, insert);
961         if(q)   free(q);
962         FloatToFront(&appData.recentEngineList, buf);
963     }
964     ReplaceEngine(cps, i);
965 }
966
967 void
968 InitTimeControls ()
969 {
970     int matched, min, sec;
971     /*
972      * Parse timeControl resource
973      */
974     if (!ParseTimeControl(appData.timeControl, appData.timeIncrement,
975                           appData.movesPerSession)) {
976         char buf[MSG_SIZ];
977         snprintf(buf, sizeof(buf), _("bad timeControl option %s"), appData.timeControl);
978         DisplayFatalError(buf, 0, 2);
979     }
980
981     /*
982      * Parse searchTime resource
983      */
984     if (*appData.searchTime != NULLCHAR) {
985         matched = sscanf(appData.searchTime, "%d:%d", &min, &sec);
986         if (matched == 1) {
987             searchTime = min * 60;
988         } else if (matched == 2) {
989             searchTime = min * 60 + sec;
990         } else {
991             char buf[MSG_SIZ];
992             snprintf(buf, sizeof(buf), _("bad searchTime option %s"), appData.searchTime);
993             DisplayFatalError(buf, 0, 2);
994         }
995     }
996 }
997
998 void
999 InitBackEnd1 ()
1000 {
1001
1002     ShowThinkingEvent(); // [HGM] thinking: make sure post/nopost state is set according to options
1003     startVariant = StringToVariant(appData.variant); // [HGM] nicks: remember original variant
1004
1005     GetTimeMark(&programStartTime);
1006     srandom((programStartTime.ms + 1000*programStartTime.sec)*0x1001001); // [HGM] book: makes sure random is unpredictabe to msec level
1007     appData.seedBase = random() + (random()<<15);
1008     pauseStart = programStartTime; pauseStart.sec -= 100; // [HGM] matchpause: fake a pause that has long since ended
1009
1010     ClearProgramStats();
1011     programStats.ok_to_send = 1;
1012     programStats.seen_stat = 0;
1013
1014     /*
1015      * Initialize game list
1016      */
1017     ListNew(&gameList);
1018
1019
1020     /*
1021      * Internet chess server status
1022      */
1023     if (appData.icsActive) {
1024         appData.matchMode = FALSE;
1025         appData.matchGames = 0;
1026 #if ZIPPY
1027         appData.noChessProgram = !appData.zippyPlay;
1028 #else
1029         appData.zippyPlay = FALSE;
1030         appData.zippyTalk = FALSE;
1031         appData.noChessProgram = TRUE;
1032 #endif
1033         if (*appData.icsHelper != NULLCHAR) {
1034             appData.useTelnet = TRUE;
1035             appData.telnetProgram = appData.icsHelper;
1036         }
1037     } else {
1038         appData.zippyTalk = appData.zippyPlay = FALSE;
1039     }
1040
1041     /* [AS] Initialize pv info list [HGM] and game state */
1042     {
1043         int i, j;
1044
1045         for( i=0; i<=framePtr; i++ ) {
1046             pvInfoList[i].depth = -1;
1047             boards[i][EP_STATUS] = EP_NONE;
1048             for( j=0; j<BOARD_FILES-2; j++ ) boards[i][CASTLING][j] = NoRights;
1049         }
1050     }
1051
1052     InitTimeControls();
1053
1054     /* [AS] Adjudication threshold */
1055     adjudicateLossThreshold = appData.adjudicateLossThreshold;
1056
1057     InitEngine(&first, 0);
1058     InitEngine(&second, 1);
1059     CommonEngineInit();
1060
1061     pairing.which = "pairing"; // pairing engine
1062     pairing.pr = NoProc;
1063     pairing.isr = NULL;
1064     pairing.program = appData.pairingEngine;
1065     pairing.host = "localhost";
1066     pairing.dir = ".";
1067
1068     if (appData.icsActive) {
1069         appData.clockMode = TRUE;  /* changes dynamically in ICS mode */
1070     } else if (appData.noChessProgram) { // [HGM] st: searchTime mode now also is clockMode
1071         appData.clockMode = FALSE;
1072         first.sendTime = second.sendTime = 0;
1073     }
1074
1075 #if ZIPPY
1076     /* Override some settings from environment variables, for backward
1077        compatibility.  Unfortunately it's not feasible to have the env
1078        vars just set defaults, at least in xboard.  Ugh.
1079     */
1080     if (appData.icsActive && (appData.zippyPlay || appData.zippyTalk)) {
1081       ZippyInit();
1082     }
1083 #endif
1084
1085     if (!appData.icsActive) {
1086       char buf[MSG_SIZ];
1087       int len;
1088
1089       /* Check for variants that are supported only in ICS mode,
1090          or not at all.  Some that are accepted here nevertheless
1091          have bugs; see comments below.
1092       */
1093       VariantClass variant = StringToVariant(appData.variant);
1094       switch (variant) {
1095       case VariantBughouse:     /* need four players and two boards */
1096       case VariantKriegspiel:   /* need to hide pieces and move details */
1097         /* case VariantFischeRandom: (Fabien: moved below) */
1098         len = snprintf(buf,MSG_SIZ, _("Variant %s supported only in ICS mode"), appData.variant);
1099         if( (len >= MSG_SIZ) && appData.debugMode )
1100           fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
1101
1102         DisplayFatalError(buf, 0, 2);
1103         return;
1104
1105       case VariantUnknown:
1106       case VariantLoadable:
1107       case Variant29:
1108       case Variant30:
1109       case Variant31:
1110       case Variant32:
1111       case Variant33:
1112       case Variant34:
1113       case Variant35:
1114       case Variant36:
1115       default:
1116         len = snprintf(buf, MSG_SIZ, _("Unknown variant name %s"), appData.variant);
1117         if( (len >= MSG_SIZ) && appData.debugMode )
1118           fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
1119
1120         DisplayFatalError(buf, 0, 2);
1121         return;
1122
1123       case VariantXiangqi:    /* [HGM] repetition rules not implemented */
1124       case VariantFairy:      /* [HGM] TestLegality definitely off! */
1125       case VariantGothic:     /* [HGM] should work */
1126       case VariantCapablanca: /* [HGM] should work */
1127       case VariantCourier:    /* [HGM] initial forced moves not implemented */
1128       case VariantShogi:      /* [HGM] could still mate with pawn drop */
1129       case VariantKnightmate: /* [HGM] should work */
1130       case VariantCylinder:   /* [HGM] untested */
1131       case VariantFalcon:     /* [HGM] untested */
1132       case VariantCrazyhouse: /* holdings not shown, ([HGM] fixed that!)
1133                                  offboard interposition not understood */
1134       case VariantNormal:     /* definitely works! */
1135       case VariantWildCastle: /* pieces not automatically shuffled */
1136       case VariantNoCastle:   /* pieces not automatically shuffled */
1137       case VariantFischeRandom: /* [HGM] works and shuffles pieces */
1138       case VariantLosers:     /* should work except for win condition,
1139                                  and doesn't know captures are mandatory */
1140       case VariantSuicide:    /* should work except for win condition,
1141                                  and doesn't know captures are mandatory */
1142       case VariantGiveaway:   /* should work except for win condition,
1143                                  and doesn't know captures are mandatory */
1144       case VariantTwoKings:   /* should work */
1145       case VariantAtomic:     /* should work except for win condition */
1146       case Variant3Check:     /* should work except for win condition */
1147       case VariantShatranj:   /* should work except for all win conditions */
1148       case VariantMakruk:     /* should work except for draw countdown */
1149       case VariantBerolina:   /* might work if TestLegality is off */
1150       case VariantCapaRandom: /* should work */
1151       case VariantJanus:      /* should work */
1152       case VariantSuper:      /* experimental */
1153       case VariantGreat:      /* experimental, requires legality testing to be off */
1154       case VariantSChess:     /* S-Chess, should work */
1155       case VariantGrand:      /* should work */
1156       case VariantSpartan:    /* should work */
1157         break;
1158       }
1159     }
1160
1161 }
1162
1163 int
1164 NextIntegerFromString (char ** str, long * value)
1165 {
1166     int result = -1;
1167     char * s = *str;
1168
1169     while( *s == ' ' || *s == '\t' ) {
1170         s++;
1171     }
1172
1173     *value = 0;
1174
1175     if( *s >= '0' && *s <= '9' ) {
1176         while( *s >= '0' && *s <= '9' ) {
1177             *value = *value * 10 + (*s - '0');
1178             s++;
1179         }
1180
1181         result = 0;
1182     }
1183
1184     *str = s;
1185
1186     return result;
1187 }
1188
1189 int
1190 NextTimeControlFromString (char ** str, long * value)
1191 {
1192     long temp;
1193     int result = NextIntegerFromString( str, &temp );
1194
1195     if( result == 0 ) {
1196         *value = temp * 60; /* Minutes */
1197         if( **str == ':' ) {
1198             (*str)++;
1199             result = NextIntegerFromString( str, &temp );
1200             *value += temp; /* Seconds */
1201         }
1202     }
1203
1204     return result;
1205 }
1206
1207 int
1208 NextSessionFromString (char ** str, int *moves, long * tc, long *inc, int *incType)
1209 {   /* [HGM] routine added to read '+moves/time' for secondary time control. */
1210     int result = -1, type = 0; long temp, temp2;
1211
1212     if(**str != ':') return -1; // old params remain in force!
1213     (*str)++;
1214     if(**str == '*') type = *(*str)++, temp = 0; // sandclock TC
1215     if( NextIntegerFromString( str, &temp ) ) return -1;
1216     if(type) { *moves = 0; *tc = temp * 500; *inc = temp * 1000; *incType = '*'; return 0; }
1217
1218     if(**str != '/') {
1219         /* time only: incremental or sudden-death time control */
1220         if(**str == '+') { /* increment follows; read it */
1221             (*str)++;
1222             if(**str == '!') type = *(*str)++; // Bronstein TC
1223             if(result = NextIntegerFromString( str, &temp2)) return -1;
1224             *inc = temp2 * 1000;
1225             if(**str == '.') { // read fraction of increment
1226                 char *start = ++(*str);
1227                 if(result = NextIntegerFromString( str, &temp2)) return -1;
1228                 temp2 *= 1000;
1229                 while(start++ < *str) temp2 /= 10;
1230                 *inc += temp2;
1231             }
1232         } else *inc = 0;
1233         *moves = 0; *tc = temp * 1000; *incType = type;
1234         return 0;
1235     }
1236
1237     (*str)++; /* classical time control */
1238     result = NextIntegerFromString( str, &temp2); // NOTE: already converted to seconds by ParseTimeControl()
1239
1240     if(result == 0) {
1241         *moves = temp;
1242         *tc    = temp2 * 1000;
1243         *inc   = 0;
1244         *incType = type;
1245     }
1246     return result;
1247 }
1248
1249 int
1250 GetTimeQuota (int movenr, int lastUsed, char *tcString)
1251 {   /* [HGM] get time to add from the multi-session time-control string */
1252     int incType, moves=1; /* kludge to force reading of first session */
1253     long time, increment;
1254     char *s = tcString;
1255
1256     if(!*s) return 0; // empty TC string means we ran out of the last sudden-death version
1257     do {
1258         if(moves) NextSessionFromString(&s, &moves, &time, &increment, &incType);
1259         nextSession = s; suddenDeath = moves == 0 && increment == 0;
1260         if(movenr == -1) return time;    /* last move before new session     */
1261         if(incType == '*') increment = 0; else // for sandclock, time is added while not thinking
1262         if(incType == '!' && lastUsed < increment) increment = lastUsed;
1263         if(!moves) return increment;     /* current session is incremental   */
1264         if(movenr >= 0) movenr -= moves; /* we already finished this session */
1265     } while(movenr >= -1);               /* try again for next session       */
1266
1267     return 0; // no new time quota on this move
1268 }
1269
1270 int
1271 ParseTimeControl (char *tc, float ti, int mps)
1272 {
1273   long tc1;
1274   long tc2;
1275   char buf[MSG_SIZ], buf2[MSG_SIZ], *mytc = tc;
1276   int min, sec=0;
1277
1278   if(ti >= 0 && !strchr(tc, '+') && !strchr(tc, '/') ) mps = 0;
1279   if(!strchr(tc, '+') && !strchr(tc, '/') && sscanf(tc, "%d:%d", &min, &sec) >= 1)
1280       sprintf(mytc=buf2, "%d", 60*min+sec); // convert 'classical' min:sec tc string to seconds
1281   if(ti > 0) {
1282
1283     if(mps)
1284       snprintf(buf, MSG_SIZ, ":%d/%s+%g", mps, mytc, ti);
1285     else 
1286       snprintf(buf, MSG_SIZ, ":%s+%g", mytc, ti);
1287   } else {
1288     if(mps)
1289       snprintf(buf, MSG_SIZ, ":%d/%s", mps, mytc);
1290     else 
1291       snprintf(buf, MSG_SIZ, ":%s", mytc);
1292   }
1293   fullTimeControlString = StrSave(buf); // this should now be in PGN format
1294   
1295   if( NextTimeControlFromString( &tc, &tc1 ) != 0 ) {
1296     return FALSE;
1297   }
1298
1299   if( *tc == '/' ) {
1300     /* Parse second time control */
1301     tc++;
1302
1303     if( NextTimeControlFromString( &tc, &tc2 ) != 0 ) {
1304       return FALSE;
1305     }
1306
1307     if( tc2 == 0 ) {
1308       return FALSE;
1309     }
1310
1311     timeControl_2 = tc2 * 1000;
1312   }
1313   else {
1314     timeControl_2 = 0;
1315   }
1316
1317   if( tc1 == 0 ) {
1318     return FALSE;
1319   }
1320
1321   timeControl = tc1 * 1000;
1322
1323   if (ti >= 0) {
1324     timeIncrement = ti * 1000;  /* convert to ms */
1325     movesPerSession = 0;
1326   } else {
1327     timeIncrement = 0;
1328     movesPerSession = mps;
1329   }
1330   return TRUE;
1331 }
1332
1333 void
1334 InitBackEnd2 ()
1335 {
1336     if (appData.debugMode) {
1337         fprintf(debugFP, "%s\n", programVersion);
1338     }
1339     ASSIGN(currentDebugFile, appData.nameOfDebugFile); // [HGM] debug split: remember initial name in use
1340
1341     set_cont_sequence(appData.wrapContSeq);
1342     if (appData.matchGames > 0) {
1343         appData.matchMode = TRUE;
1344     } else if (appData.matchMode) {
1345         appData.matchGames = 1;
1346     }
1347     if(appData.matchMode && appData.sameColorGames > 0) /* [HGM] alternate: overrule matchGames */
1348         appData.matchGames = appData.sameColorGames;
1349     if(appData.rewindIndex > 1) { /* [HGM] autoinc: rewind implies auto-increment and overrules given index */
1350         if(appData.loadPositionIndex >= 0) appData.loadPositionIndex = -1;
1351         if(appData.loadGameIndex >= 0) appData.loadGameIndex = -1;
1352     }
1353     Reset(TRUE, FALSE);
1354     if (appData.noChessProgram || first.protocolVersion == 1) {
1355       InitBackEnd3();
1356     } else {
1357       /* kludge: allow timeout for initial "feature" commands */
1358       FreezeUI();
1359       DisplayMessage("", _("Starting chess program"));
1360       ScheduleDelayedEvent(InitBackEnd3, FEATURE_TIMEOUT);
1361     }
1362 }
1363
1364 int
1365 CalculateIndex (int index, int gameNr)
1366 {   // [HGM] autoinc: absolute way to determine load index from game number (taking auto-inc and rewind into account)
1367     int res;
1368     if(index > 0) return index; // fixed nmber
1369     if(index == 0) return 1;
1370     res = (index == -1 ? gameNr : (gameNr-1)/2 + 1); // autoinc
1371     if(appData.rewindIndex > 0) res = (res-1) % appData.rewindIndex + 1; // rewind
1372     return res;
1373 }
1374
1375 int
1376 LoadGameOrPosition (int gameNr)
1377 {   // [HGM] taken out of MatchEvent and NextMatchGame (to combine it)
1378     if (*appData.loadGameFile != NULLCHAR) {
1379         if (!LoadGameFromFile(appData.loadGameFile,
1380                 CalculateIndex(appData.loadGameIndex, gameNr),
1381                               appData.loadGameFile, FALSE)) {
1382             DisplayFatalError(_("Bad game file"), 0, 1);
1383             return 0;
1384         }
1385     } else if (*appData.loadPositionFile != NULLCHAR) {
1386         if (!LoadPositionFromFile(appData.loadPositionFile,
1387                 CalculateIndex(appData.loadPositionIndex, gameNr),
1388                                   appData.loadPositionFile)) {
1389             DisplayFatalError(_("Bad position file"), 0, 1);
1390             return 0;
1391         }
1392     }
1393     return 1;
1394 }
1395
1396 void
1397 ReserveGame (int gameNr, char resChar)
1398 {
1399     FILE *tf = fopen(appData.tourneyFile, "r+");
1400     char *p, *q, c, buf[MSG_SIZ];
1401     if(tf == NULL) { nextGame = appData.matchGames + 1; return; } // kludge to terminate match
1402     safeStrCpy(buf, lastMsg, MSG_SIZ);
1403     DisplayMessage(_("Pick new game"), "");
1404     flock(fileno(tf), LOCK_EX); // lock the tourney file while we are messing with it
1405     ParseArgsFromFile(tf);
1406     p = q = appData.results;
1407     if(appData.debugMode) {
1408       char *r = appData.participants;
1409       fprintf(debugFP, "results = '%s'\n", p);
1410       while(*r) fprintf(debugFP, *r >= ' ' ? "%c" : "\\%03o", *r), r++;
1411       fprintf(debugFP, "\n");
1412     }
1413     while(*q && *q != ' ') q++; // get first un-played game (could be beyond end!)
1414     nextGame = q - p;
1415     q = malloc(strlen(p) + 2); // could be arbitrary long, but allow to extend by one!
1416     safeStrCpy(q, p, strlen(p) + 2);
1417     if(gameNr >= 0) q[gameNr] = resChar; // replace '*' with result
1418     if(appData.debugMode) fprintf(debugFP, "pick next game from '%s': %d\n", q, nextGame);
1419     if(nextGame <= appData.matchGames && resChar != ' ' && !abortMatch) { // reserve next game if tourney not yet done
1420         if(q[nextGame] == NULLCHAR) q[nextGame+1] = NULLCHAR; // append one char
1421         q[nextGame] = '*';
1422     }
1423     fseek(tf, -(strlen(p)+4), SEEK_END);
1424     c = fgetc(tf);
1425     if(c != '"') // depending on DOS or Unix line endings we can be one off
1426          fseek(tf, -(strlen(p)+2), SEEK_END);
1427     else fseek(tf, -(strlen(p)+3), SEEK_END);
1428     fprintf(tf, "%s\"\n", q); fclose(tf); // update, and flush by closing
1429     DisplayMessage(buf, "");
1430     free(p); appData.results = q;
1431     if(nextGame <= appData.matchGames && resChar != ' ' && !abortMatch &&
1432        (gameNr < 0 || nextGame / appData.defaultMatchGames != gameNr / appData.defaultMatchGames)) {
1433       int round = appData.defaultMatchGames * appData.tourneyType;
1434       if(gameNr < 0 || appData.tourneyType < 1 ||  // gauntlet engine can always stay loaded as first engine
1435          appData.tourneyType > 1 && nextGame/round != gameNr/round) // in multi-gauntlet change only after round
1436         UnloadEngine(&first);  // next game belongs to other pairing;
1437         UnloadEngine(&second); // already unload the engines, so TwoMachinesEvent will load new ones.
1438     }
1439     if(appData.debugMode) fprintf(debugFP, "Reserved, next=%d, nr=%d\n", nextGame, gameNr);
1440 }
1441
1442 void
1443 MatchEvent (int mode)
1444 {       // [HGM] moved out of InitBackend3, to make it callable when match starts through menu
1445         int dummy;
1446         if(matchMode) { // already in match mode: switch it off
1447             abortMatch = TRUE;
1448             if(!appData.tourneyFile[0]) appData.matchGames = matchGame; // kludge to let match terminate after next game.
1449             return;
1450         }
1451 //      if(gameMode != BeginningOfGame) {
1452 //          DisplayError(_("You can only start a match from the initial position."), 0);
1453 //          return;
1454 //      }
1455         abortMatch = FALSE;
1456         if(mode == 2) appData.matchGames = appData.defaultMatchGames;
1457         /* Set up machine vs. machine match */
1458         nextGame = 0;
1459         NextTourneyGame(-1, &dummy); // sets appData.matchGames if this is tourney, to make sure ReserveGame knows it
1460         if(appData.tourneyFile[0]) {
1461             ReserveGame(-1, 0);
1462             if(nextGame > appData.matchGames) {
1463                 char buf[MSG_SIZ];
1464                 if(strchr(appData.results, '*') == NULL) {
1465                     FILE *f;
1466                     appData.tourneyCycles++;
1467                     if(f = WriteTourneyFile(appData.results, NULL)) { // make a tourney file with increased number of cycles
1468                         fclose(f);
1469                         NextTourneyGame(-1, &dummy);
1470                         ReserveGame(-1, 0);
1471                         if(nextGame <= appData.matchGames) {
1472                             DisplayNote(_("You restarted an already completed tourney\nOne more cycle will now be added to it\nGames commence in 10 sec"));
1473                             matchMode = mode;
1474                             ScheduleDelayedEvent(NextMatchGame, 10000);
1475                             return;
1476                         }
1477                     }
1478                 }
1479                 snprintf(buf, MSG_SIZ, _("All games in tourney '%s' are already played or playing"), appData.tourneyFile);
1480                 DisplayError(buf, 0);
1481                 appData.tourneyFile[0] = 0;
1482                 return;
1483             }
1484         } else
1485         if (appData.noChessProgram) {  // [HGM] in tourney engines are loaded automatically
1486             DisplayFatalError(_("Can't have a match with no chess programs"),
1487                               0, 2);
1488             return;
1489         }
1490         matchMode = mode;
1491         matchGame = roundNr = 1;
1492         first.matchWins = second.matchWins = 0; // [HGM] match: needed in later matches
1493         NextMatchGame();
1494 }
1495
1496 char *comboLine = NULL; // [HGM] recent: WinBoard's first-engine combobox line
1497
1498 void
1499 InitBackEnd3 P((void))
1500 {
1501     GameMode initialMode;
1502     char buf[MSG_SIZ];
1503     int err, len;
1504
1505     InitChessProgram(&first, startedFromSetupPosition);
1506
1507     if(!appData.noChessProgram) {  /* [HGM] tidy: redo program version to use name from myname feature */
1508         free(programVersion);
1509         programVersion = (char*) malloc(8 + strlen(PACKAGE_STRING) + strlen(first.tidy));
1510         sprintf(programVersion, "%s + %s", PACKAGE_STRING, first.tidy);
1511         FloatToFront(&appData.recentEngineList, comboLine ? comboLine : appData.firstChessProgram);
1512     }
1513
1514     if (appData.icsActive) {
1515 #ifdef WIN32
1516         /* [DM] Make a console window if needed [HGM] merged ifs */
1517         ConsoleCreate();
1518 #endif
1519         err = establish();
1520         if (err != 0)
1521           {
1522             if (*appData.icsCommPort != NULLCHAR)
1523               len = snprintf(buf, MSG_SIZ, _("Could not open comm port %s"),
1524                              appData.icsCommPort);
1525             else
1526               len = snprintf(buf, MSG_SIZ, _("Could not connect to host %s, port %s"),
1527                         appData.icsHost, appData.icsPort);
1528
1529             if( (len >= MSG_SIZ) && appData.debugMode )
1530               fprintf(debugFP, "InitBackEnd3: buffer truncated.\n");
1531
1532             DisplayFatalError(buf, err, 1);
1533             return;
1534         }
1535         SetICSMode();
1536         telnetISR =
1537           AddInputSource(icsPR, FALSE, read_from_ics, &telnetISR);
1538         fromUserISR =
1539           AddInputSource(NoProc, FALSE, read_from_player, &fromUserISR);
1540         if(appData.keepAlive) // [HGM] alive: schedule sending of dummy 'date' command
1541             ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
1542     } else if (appData.noChessProgram) {
1543         SetNCPMode();
1544     } else {
1545         SetGNUMode();
1546     }
1547
1548     if (*appData.cmailGameName != NULLCHAR) {
1549         SetCmailMode();
1550         OpenLoopback(&cmailPR);
1551         cmailISR =
1552           AddInputSource(cmailPR, FALSE, CmailSigHandlerCallBack, &cmailISR);
1553     }
1554
1555     ThawUI();
1556     DisplayMessage("", "");
1557     if (StrCaseCmp(appData.initialMode, "") == 0) {
1558       initialMode = BeginningOfGame;
1559       if(!appData.icsActive && appData.noChessProgram) { // [HGM] could be fall-back
1560         gameMode = MachinePlaysBlack; // "Machine Black" might have been implicitly highlighted
1561         ModeHighlight(); // make sure XBoard knows it is highlighted, so it will un-highlight it
1562         gameMode = BeginningOfGame; // in case BeginningOfGame now means "Edit Position"
1563         ModeHighlight();
1564       }
1565     } else if (StrCaseCmp(appData.initialMode, "TwoMachines") == 0) {
1566       initialMode = TwoMachinesPlay;
1567     } else if (StrCaseCmp(appData.initialMode, "AnalyzeFile") == 0) {
1568       initialMode = AnalyzeFile;
1569     } else if (StrCaseCmp(appData.initialMode, "Analysis") == 0) {
1570       initialMode = AnalyzeMode;
1571     } else if (StrCaseCmp(appData.initialMode, "MachineWhite") == 0) {
1572       initialMode = MachinePlaysWhite;
1573     } else if (StrCaseCmp(appData.initialMode, "MachineBlack") == 0) {
1574       initialMode = MachinePlaysBlack;
1575     } else if (StrCaseCmp(appData.initialMode, "EditGame") == 0) {
1576       initialMode = EditGame;
1577     } else if (StrCaseCmp(appData.initialMode, "EditPosition") == 0) {
1578       initialMode = EditPosition;
1579     } else if (StrCaseCmp(appData.initialMode, "Training") == 0) {
1580       initialMode = Training;
1581     } else {
1582       len = snprintf(buf, MSG_SIZ, _("Unknown initialMode %s"), appData.initialMode);
1583       if( (len >= MSG_SIZ) && appData.debugMode )
1584         fprintf(debugFP, "InitBackEnd3: buffer truncated.\n");
1585
1586       DisplayFatalError(buf, 0, 2);
1587       return;
1588     }
1589
1590     if (appData.matchMode) {
1591         if(appData.tourneyFile[0]) { // start tourney from command line
1592             FILE *f;
1593             if(f = fopen(appData.tourneyFile, "r")) {
1594                 ParseArgsFromFile(f); // make sure tourney parmeters re known
1595                 fclose(f);
1596                 appData.clockMode = TRUE;
1597                 SetGNUMode();
1598             } else appData.tourneyFile[0] = NULLCHAR; // for now ignore bad tourney file
1599         }
1600         MatchEvent(TRUE);
1601     } else if (*appData.cmailGameName != NULLCHAR) {
1602         /* Set up cmail mode */
1603         ReloadCmailMsgEvent(TRUE);
1604     } else {
1605         /* Set up other modes */
1606         if (initialMode == AnalyzeFile) {
1607           if (*appData.loadGameFile == NULLCHAR) {
1608             DisplayFatalError(_("AnalyzeFile mode requires a game file"), 0, 1);
1609             return;
1610           }
1611         }
1612         if (*appData.loadGameFile != NULLCHAR) {
1613             (void) LoadGameFromFile(appData.loadGameFile,
1614                                     appData.loadGameIndex,
1615                                     appData.loadGameFile, TRUE);
1616         } else if (*appData.loadPositionFile != NULLCHAR) {
1617             (void) LoadPositionFromFile(appData.loadPositionFile,
1618                                         appData.loadPositionIndex,
1619                                         appData.loadPositionFile);
1620             /* [HGM] try to make self-starting even after FEN load */
1621             /* to allow automatic setup of fairy variants with wtm */
1622             if(initialMode == BeginningOfGame && !blackPlaysFirst) {
1623                 gameMode = BeginningOfGame;
1624                 setboardSpoiledMachineBlack = 1;
1625             }
1626             /* [HGM] loadPos: make that every new game uses the setup */
1627             /* from file as long as we do not switch variant          */
1628             if(!blackPlaysFirst) {
1629                 startedFromPositionFile = TRUE;
1630                 CopyBoard(filePosition, boards[0]);
1631             }
1632         }
1633         if (initialMode == AnalyzeMode) {
1634           if (appData.noChessProgram) {
1635             DisplayFatalError(_("Analysis mode requires a chess engine"), 0, 2);
1636             return;
1637           }
1638           if (appData.icsActive) {
1639             DisplayFatalError(_("Analysis mode does not work with ICS mode"),0,2);
1640             return;
1641           }
1642           AnalyzeModeEvent();
1643         } else if (initialMode == AnalyzeFile) {
1644           appData.showThinking = TRUE; // [HGM] thinking: moved out of ShowThinkingEvent
1645           ShowThinkingEvent();
1646           AnalyzeFileEvent();
1647           AnalysisPeriodicEvent(1);
1648         } else if (initialMode == MachinePlaysWhite) {
1649           if (appData.noChessProgram) {
1650             DisplayFatalError(_("MachineWhite mode requires a chess engine"),
1651                               0, 2);
1652             return;
1653           }
1654           if (appData.icsActive) {
1655             DisplayFatalError(_("MachineWhite mode does not work with ICS mode"),
1656                               0, 2);
1657             return;
1658           }
1659           MachineWhiteEvent();
1660         } else if (initialMode == MachinePlaysBlack) {
1661           if (appData.noChessProgram) {
1662             DisplayFatalError(_("MachineBlack mode requires a chess engine"),
1663                               0, 2);
1664             return;
1665           }
1666           if (appData.icsActive) {
1667             DisplayFatalError(_("MachineBlack mode does not work with ICS mode"),
1668                               0, 2);
1669             return;
1670           }
1671           MachineBlackEvent();
1672         } else if (initialMode == TwoMachinesPlay) {
1673           if (appData.noChessProgram) {
1674             DisplayFatalError(_("TwoMachines mode requires a chess engine"),
1675                               0, 2);
1676             return;
1677           }
1678           if (appData.icsActive) {
1679             DisplayFatalError(_("TwoMachines mode does not work with ICS mode"),
1680                               0, 2);
1681             return;
1682           }
1683           TwoMachinesEvent();
1684         } else if (initialMode == EditGame) {
1685           EditGameEvent();
1686         } else if (initialMode == EditPosition) {
1687           EditPositionEvent();
1688         } else if (initialMode == Training) {
1689           if (*appData.loadGameFile == NULLCHAR) {
1690             DisplayFatalError(_("Training mode requires a game file"), 0, 2);
1691             return;
1692           }
1693           TrainingEvent();
1694         }
1695     }
1696 }
1697
1698 void
1699 HistorySet (char movelist[][2*MOVE_LEN], int first, int last, int current)
1700 {
1701     DisplayBook(current+1);
1702
1703     MoveHistorySet( movelist, first, last, current, pvInfoList );
1704
1705     EvalGraphSet( first, last, current, pvInfoList );
1706
1707     MakeEngineOutputTitle();
1708 }
1709
1710 /*
1711  * Establish will establish a contact to a remote host.port.
1712  * Sets icsPR to a ProcRef for a process (or pseudo-process)
1713  *  used to talk to the host.
1714  * Returns 0 if okay, error code if not.
1715  */
1716 int
1717 establish ()
1718 {
1719     char buf[MSG_SIZ];
1720
1721     if (*appData.icsCommPort != NULLCHAR) {
1722         /* Talk to the host through a serial comm port */
1723         return OpenCommPort(appData.icsCommPort, &icsPR);
1724
1725     } else if (*appData.gateway != NULLCHAR) {
1726         if (*appData.remoteShell == NULLCHAR) {
1727             /* Use the rcmd protocol to run telnet program on a gateway host */
1728             snprintf(buf, sizeof(buf), "%s %s %s",
1729                     appData.telnetProgram, appData.icsHost, appData.icsPort);
1730             return OpenRcmd(appData.gateway, appData.remoteUser, buf, &icsPR);
1731
1732         } else {
1733             /* Use the rsh program to run telnet program on a gateway host */
1734             if (*appData.remoteUser == NULLCHAR) {
1735                 snprintf(buf, sizeof(buf), "%s %s %s %s %s", appData.remoteShell,
1736                         appData.gateway, appData.telnetProgram,
1737                         appData.icsHost, appData.icsPort);
1738             } else {
1739                 snprintf(buf, sizeof(buf), "%s %s -l %s %s %s %s",
1740                         appData.remoteShell, appData.gateway,
1741                         appData.remoteUser, appData.telnetProgram,
1742                         appData.icsHost, appData.icsPort);
1743             }
1744             return StartChildProcess(buf, "", &icsPR);
1745
1746         }
1747     } else if (appData.useTelnet) {
1748         return OpenTelnet(appData.icsHost, appData.icsPort, &icsPR);
1749
1750     } else {
1751         /* TCP socket interface differs somewhat between
1752            Unix and NT; handle details in the front end.
1753            */
1754         return OpenTCP(appData.icsHost, appData.icsPort, &icsPR);
1755     }
1756 }
1757
1758 void
1759 EscapeExpand (char *p, char *q)
1760 {       // [HGM] initstring: routine to shape up string arguments
1761         while(*p++ = *q++) if(p[-1] == '\\')
1762             switch(*q++) {
1763                 case 'n': p[-1] = '\n'; break;
1764                 case 'r': p[-1] = '\r'; break;
1765                 case 't': p[-1] = '\t'; break;
1766                 case '\\': p[-1] = '\\'; break;
1767                 case 0: *p = 0; return;
1768                 default: p[-1] = q[-1]; break;
1769             }
1770 }
1771
1772 void
1773 show_bytes (FILE *fp, char *buf, int count)
1774 {
1775     while (count--) {
1776         if (*buf < 040 || *(unsigned char *) buf > 0177) {
1777             fprintf(fp, "\\%03o", *buf & 0xff);
1778         } else {
1779             putc(*buf, fp);
1780         }
1781         buf++;
1782     }
1783     fflush(fp);
1784 }
1785
1786 /* Returns an errno value */
1787 int
1788 OutputMaybeTelnet (ProcRef pr, char *message, int count, int *outError)
1789 {
1790     char buf[8192], *p, *q, *buflim;
1791     int left, newcount, outcount;
1792
1793     if (*appData.icsCommPort != NULLCHAR || appData.useTelnet ||
1794         *appData.gateway != NULLCHAR) {
1795         if (appData.debugMode) {
1796             fprintf(debugFP, ">ICS: ");
1797             show_bytes(debugFP, message, count);
1798             fprintf(debugFP, "\n");
1799         }
1800         return OutputToProcess(pr, message, count, outError);
1801     }
1802
1803     buflim = &buf[sizeof(buf)-1]; /* allow 1 byte for expanding last char */
1804     p = message;
1805     q = buf;
1806     left = count;
1807     newcount = 0;
1808     while (left) {
1809         if (q >= buflim) {
1810             if (appData.debugMode) {
1811                 fprintf(debugFP, ">ICS: ");
1812                 show_bytes(debugFP, buf, newcount);
1813                 fprintf(debugFP, "\n");
1814             }
1815             outcount = OutputToProcess(pr, buf, newcount, outError);
1816             if (outcount < newcount) return -1; /* to be sure */
1817             q = buf;
1818             newcount = 0;
1819         }
1820         if (*p == '\n') {
1821             *q++ = '\r';
1822             newcount++;
1823         } else if (((unsigned char) *p) == TN_IAC) {
1824             *q++ = (char) TN_IAC;
1825             newcount ++;
1826         }
1827         *q++ = *p++;
1828         newcount++;
1829         left--;
1830     }
1831     if (appData.debugMode) {
1832         fprintf(debugFP, ">ICS: ");
1833         show_bytes(debugFP, buf, newcount);
1834         fprintf(debugFP, "\n");
1835     }
1836     outcount = OutputToProcess(pr, buf, newcount, outError);
1837     if (outcount < newcount) return -1; /* to be sure */
1838     return count;
1839 }
1840
1841 void
1842 read_from_player (InputSourceRef isr, VOIDSTAR closure, char *message, int count, int error)
1843 {
1844     int outError, outCount;
1845     static int gotEof = 0;
1846
1847     /* Pass data read from player on to ICS */
1848     if (count > 0) {
1849         gotEof = 0;
1850         outCount = OutputMaybeTelnet(icsPR, message, count, &outError);
1851         if (outCount < count) {
1852             DisplayFatalError(_("Error writing to ICS"), outError, 1);
1853         }
1854     } else if (count < 0) {
1855         RemoveInputSource(isr);
1856         DisplayFatalError(_("Error reading from keyboard"), error, 1);
1857     } else if (gotEof++ > 0) {
1858         RemoveInputSource(isr);
1859         DisplayFatalError(_("Got end of file from keyboard"), 0, 0);
1860     }
1861 }
1862
1863 void
1864 KeepAlive ()
1865 {   // [HGM] alive: periodically send dummy (date) command to ICS to prevent time-out
1866     if(!connectionAlive) DisplayFatalError("No response from ICS", 0, 1);
1867     connectionAlive = FALSE; // only sticks if no response to 'date' command.
1868     SendToICS("date\n");
1869     if(appData.keepAlive) ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
1870 }
1871
1872 /* added routine for printf style output to ics */
1873 void
1874 ics_printf (char *format, ...)
1875 {
1876     char buffer[MSG_SIZ];
1877     va_list args;
1878
1879     va_start(args, format);
1880     vsnprintf(buffer, sizeof(buffer), format, args);
1881     buffer[sizeof(buffer)-1] = '\0';
1882     SendToICS(buffer);
1883     va_end(args);
1884 }
1885
1886 void
1887 SendToICS (char *s)
1888 {
1889     int count, outCount, outError;
1890
1891     if (icsPR == NoProc) return;
1892
1893     count = strlen(s);
1894     outCount = OutputMaybeTelnet(icsPR, s, count, &outError);
1895     if (outCount < count) {
1896         DisplayFatalError(_("Error writing to ICS"), outError, 1);
1897     }
1898 }
1899
1900 /* This is used for sending logon scripts to the ICS. Sending
1901    without a delay causes problems when using timestamp on ICC
1902    (at least on my machine). */
1903 void
1904 SendToICSDelayed (char *s, long msdelay)
1905 {
1906     int count, outCount, outError;
1907
1908     if (icsPR == NoProc) return;
1909
1910     count = strlen(s);
1911     if (appData.debugMode) {
1912         fprintf(debugFP, ">ICS: ");
1913         show_bytes(debugFP, s, count);
1914         fprintf(debugFP, "\n");
1915     }
1916     outCount = OutputToProcessDelayed(icsPR, s, count, &outError,
1917                                       msdelay);
1918     if (outCount < count) {
1919         DisplayFatalError(_("Error writing to ICS"), outError, 1);
1920     }
1921 }
1922
1923
1924 /* Remove all highlighting escape sequences in s
1925    Also deletes any suffix starting with '('
1926    */
1927 char *
1928 StripHighlightAndTitle (char *s)
1929 {
1930     static char retbuf[MSG_SIZ];
1931     char *p = retbuf;
1932
1933     while (*s != NULLCHAR) {
1934         while (*s == '\033') {
1935             while (*s != NULLCHAR && !isalpha(*s)) s++;
1936             if (*s != NULLCHAR) s++;
1937         }
1938         while (*s != NULLCHAR && *s != '\033') {
1939             if (*s == '(' || *s == '[') {
1940                 *p = NULLCHAR;
1941                 return retbuf;
1942             }
1943             *p++ = *s++;
1944         }
1945     }
1946     *p = NULLCHAR;
1947     return retbuf;
1948 }
1949
1950 /* Remove all highlighting escape sequences in s */
1951 char *
1952 StripHighlight (char *s)
1953 {
1954     static char retbuf[MSG_SIZ];
1955     char *p = retbuf;
1956
1957     while (*s != NULLCHAR) {
1958         while (*s == '\033') {
1959             while (*s != NULLCHAR && !isalpha(*s)) s++;
1960             if (*s != NULLCHAR) s++;
1961         }
1962         while (*s != NULLCHAR && *s != '\033') {
1963             *p++ = *s++;
1964         }
1965     }
1966     *p = NULLCHAR;
1967     return retbuf;
1968 }
1969
1970 char *variantNames[] = VARIANT_NAMES;
1971 char *
1972 VariantName (VariantClass v)
1973 {
1974     return variantNames[v];
1975 }
1976
1977
1978 /* Identify a variant from the strings the chess servers use or the
1979    PGN Variant tag names we use. */
1980 VariantClass
1981 StringToVariant (char *e)
1982 {
1983     char *p;
1984     int wnum = -1;
1985     VariantClass v = VariantNormal;
1986     int i, found = FALSE;
1987     char buf[MSG_SIZ];
1988     int len;
1989
1990     if (!e) return v;
1991
1992     /* [HGM] skip over optional board-size prefixes */
1993     if( sscanf(e, "%dx%d_", &i, &i) == 2 ||
1994         sscanf(e, "%dx%d+%d_", &i, &i, &i) == 3 ) {
1995         while( *e++ != '_');
1996     }
1997
1998     if(StrCaseStr(e, "misc/")) { // [HGM] on FICS, misc/shogi is not shogi
1999         v = VariantNormal;
2000         found = TRUE;
2001     } else
2002     for (i=0; i<sizeof(variantNames)/sizeof(char*); i++) {
2003       if (StrCaseStr(e, variantNames[i])) {
2004         v = (VariantClass) i;
2005         found = TRUE;
2006         break;
2007       }
2008     }
2009
2010     if (!found) {
2011       if ((StrCaseStr(e, "fischer") && StrCaseStr(e, "random"))
2012           || StrCaseStr(e, "wild/fr")
2013           || StrCaseStr(e, "frc") || StrCaseStr(e, "960")) {
2014         v = VariantFischeRandom;
2015       } else if ((i = 4, p = StrCaseStr(e, "wild")) ||
2016                  (i = 1, p = StrCaseStr(e, "w"))) {
2017         p += i;
2018         while (*p && (isspace(*p) || *p == '(' || *p == '/')) p++;
2019         if (isdigit(*p)) {
2020           wnum = atoi(p);
2021         } else {
2022           wnum = -1;
2023         }
2024         switch (wnum) {
2025         case 0: /* FICS only, actually */
2026         case 1:
2027           /* Castling legal even if K starts on d-file */
2028           v = VariantWildCastle;
2029           break;
2030         case 2:
2031         case 3:
2032         case 4:
2033           /* Castling illegal even if K & R happen to start in
2034              normal positions. */
2035           v = VariantNoCastle;
2036           break;
2037         case 5:
2038         case 7:
2039         case 8:
2040         case 10:
2041         case 11:
2042         case 12:
2043         case 13:
2044         case 14:
2045         case 15:
2046         case 18:
2047         case 19:
2048           /* Castling legal iff K & R start in normal positions */
2049           v = VariantNormal;
2050           break;
2051         case 6:
2052         case 20:
2053         case 21:
2054           /* Special wilds for position setup; unclear what to do here */
2055           v = VariantLoadable;
2056           break;
2057         case 9:
2058           /* Bizarre ICC game */
2059           v = VariantTwoKings;
2060           break;
2061         case 16:
2062           v = VariantKriegspiel;
2063           break;
2064         case 17:
2065           v = VariantLosers;
2066           break;
2067         case 22:
2068           v = VariantFischeRandom;
2069           break;
2070         case 23:
2071           v = VariantCrazyhouse;
2072           break;
2073         case 24:
2074           v = VariantBughouse;
2075           break;
2076         case 25:
2077           v = Variant3Check;
2078           break;
2079         case 26:
2080           /* Not quite the same as FICS suicide! */
2081           v = VariantGiveaway;
2082           break;
2083         case 27:
2084           v = VariantAtomic;
2085           break;
2086         case 28:
2087           v = VariantShatranj;
2088           break;
2089
2090         /* Temporary names for future ICC types.  The name *will* change in
2091            the next xboard/WinBoard release after ICC defines it. */
2092         case 29:
2093           v = Variant29;
2094           break;
2095         case 30:
2096           v = Variant30;
2097           break;
2098         case 31:
2099           v = Variant31;
2100           break;
2101         case 32:
2102           v = Variant32;
2103           break;
2104         case 33:
2105           v = Variant33;
2106           break;
2107         case 34:
2108           v = Variant34;
2109           break;
2110         case 35:
2111           v = Variant35;
2112           break;
2113         case 36:
2114           v = Variant36;
2115           break;
2116         case 37:
2117           v = VariantShogi;
2118           break;
2119         case 38:
2120           v = VariantXiangqi;
2121           break;
2122         case 39:
2123           v = VariantCourier;
2124           break;
2125         case 40:
2126           v = VariantGothic;
2127           break;
2128         case 41:
2129           v = VariantCapablanca;
2130           break;
2131         case 42:
2132           v = VariantKnightmate;
2133           break;
2134         case 43:
2135           v = VariantFairy;
2136           break;
2137         case 44:
2138           v = VariantCylinder;
2139           break;
2140         case 45:
2141           v = VariantFalcon;
2142           break;
2143         case 46:
2144           v = VariantCapaRandom;
2145           break;
2146         case 47:
2147           v = VariantBerolina;
2148           break;
2149         case 48:
2150           v = VariantJanus;
2151           break;
2152         case 49:
2153           v = VariantSuper;
2154           break;
2155         case 50:
2156           v = VariantGreat;
2157           break;
2158         case -1:
2159           /* Found "wild" or "w" in the string but no number;
2160              must assume it's normal chess. */
2161           v = VariantNormal;
2162           break;
2163         default:
2164           len = snprintf(buf, MSG_SIZ, _("Unknown wild type %d"), wnum);
2165           if( (len >= MSG_SIZ) && appData.debugMode )
2166             fprintf(debugFP, "StringToVariant: buffer truncated.\n");
2167
2168           DisplayError(buf, 0);
2169           v = VariantUnknown;
2170           break;
2171         }
2172       }
2173     }
2174     if (appData.debugMode) {
2175       fprintf(debugFP, _("recognized '%s' (%d) as variant %s\n"),
2176               e, wnum, VariantName(v));
2177     }
2178     return v;
2179 }
2180
2181 static int leftover_start = 0, leftover_len = 0;
2182 char star_match[STAR_MATCH_N][MSG_SIZ];
2183
2184 /* Test whether pattern is present at &buf[*index]; if so, return TRUE,
2185    advance *index beyond it, and set leftover_start to the new value of
2186    *index; else return FALSE.  If pattern contains the character '*', it
2187    matches any sequence of characters not containing '\r', '\n', or the
2188    character following the '*' (if any), and the matched sequence(s) are
2189    copied into star_match.
2190    */
2191 int
2192 looking_at ( char *buf, int *index, char *pattern)
2193 {
2194     char *bufp = &buf[*index], *patternp = pattern;
2195     int star_count = 0;
2196     char *matchp = star_match[0];
2197
2198     for (;;) {
2199         if (*patternp == NULLCHAR) {
2200             *index = leftover_start = bufp - buf;
2201             *matchp = NULLCHAR;
2202             return TRUE;
2203         }
2204         if (*bufp == NULLCHAR) return FALSE;
2205         if (*patternp == '*') {
2206             if (*bufp == *(patternp + 1)) {
2207                 *matchp = NULLCHAR;
2208                 matchp = star_match[++star_count];
2209                 patternp += 2;
2210                 bufp++;
2211                 continue;
2212             } else if (*bufp == '\n' || *bufp == '\r') {
2213                 patternp++;
2214                 if (*patternp == NULLCHAR)
2215                   continue;
2216                 else
2217                   return FALSE;
2218             } else {
2219                 *matchp++ = *bufp++;
2220                 continue;
2221             }
2222         }
2223         if (*patternp != *bufp) return FALSE;
2224         patternp++;
2225         bufp++;
2226     }
2227 }
2228
2229 void
2230 SendToPlayer (char *data, int length)
2231 {
2232     int error, outCount;
2233     outCount = OutputToProcess(NoProc, data, length, &error);
2234     if (outCount < length) {
2235         DisplayFatalError(_("Error writing to display"), error, 1);
2236     }
2237 }
2238
2239 void
2240 PackHolding (char packed[], char *holding)
2241 {
2242     char *p = holding;
2243     char *q = packed;
2244     int runlength = 0;
2245     int curr = 9999;
2246     do {
2247         if (*p == curr) {
2248             runlength++;
2249         } else {
2250             switch (runlength) {
2251               case 0:
2252                 break;
2253               case 1:
2254                 *q++ = curr;
2255                 break;
2256               case 2:
2257                 *q++ = curr;
2258                 *q++ = curr;
2259                 break;
2260               default:
2261                 sprintf(q, "%d", runlength);
2262                 while (*q) q++;
2263                 *q++ = curr;
2264                 break;
2265             }
2266             runlength = 1;
2267             curr = *p;
2268         }
2269     } while (*p++);
2270     *q = NULLCHAR;
2271 }
2272
2273 /* Telnet protocol requests from the front end */
2274 void
2275 TelnetRequest (unsigned char ddww, unsigned char option)
2276 {
2277     unsigned char msg[3];
2278     int outCount, outError;
2279
2280     if (*appData.icsCommPort != NULLCHAR || appData.useTelnet) return;
2281
2282     if (appData.debugMode) {
2283         char buf1[8], buf2[8], *ddwwStr, *optionStr;
2284         switch (ddww) {
2285           case TN_DO:
2286             ddwwStr = "DO";
2287             break;
2288           case TN_DONT:
2289             ddwwStr = "DONT";
2290             break;
2291           case TN_WILL:
2292             ddwwStr = "WILL";
2293             break;
2294           case TN_WONT:
2295             ddwwStr = "WONT";
2296             break;
2297           default:
2298             ddwwStr = buf1;
2299             snprintf(buf1,sizeof(buf1)/sizeof(buf1[0]), "%d", ddww);
2300             break;
2301         }
2302         switch (option) {
2303           case TN_ECHO:
2304             optionStr = "ECHO";
2305             break;
2306           default:
2307             optionStr = buf2;
2308             snprintf(buf2,sizeof(buf2)/sizeof(buf2[0]), "%d", option);
2309             break;
2310         }
2311         fprintf(debugFP, ">%s %s ", ddwwStr, optionStr);
2312     }
2313     msg[0] = TN_IAC;
2314     msg[1] = ddww;
2315     msg[2] = option;
2316     outCount = OutputToProcess(icsPR, (char *)msg, 3, &outError);
2317     if (outCount < 3) {
2318         DisplayFatalError(_("Error writing to ICS"), outError, 1);
2319     }
2320 }
2321
2322 void
2323 DoEcho ()
2324 {
2325     if (!appData.icsActive) return;
2326     TelnetRequest(TN_DO, TN_ECHO);
2327 }
2328
2329 void
2330 DontEcho ()
2331 {
2332     if (!appData.icsActive) return;
2333     TelnetRequest(TN_DONT, TN_ECHO);
2334 }
2335
2336 void
2337 CopyHoldings (Board board, char *holdings, ChessSquare lowestPiece)
2338 {
2339     /* put the holdings sent to us by the server on the board holdings area */
2340     int i, j, holdingsColumn, holdingsStartRow, direction, countsColumn;
2341     char p;
2342     ChessSquare piece;
2343
2344     if(gameInfo.holdingsWidth < 2)  return;
2345     if(gameInfo.variant != VariantBughouse && board[HOLDINGS_SET])
2346         return; // prevent overwriting by pre-board holdings
2347
2348     if( (int)lowestPiece >= BlackPawn ) {
2349         holdingsColumn = 0;
2350         countsColumn = 1;
2351         holdingsStartRow = BOARD_HEIGHT-1;
2352         direction = -1;
2353     } else {
2354         holdingsColumn = BOARD_WIDTH-1;
2355         countsColumn = BOARD_WIDTH-2;
2356         holdingsStartRow = 0;
2357         direction = 1;
2358     }
2359
2360     for(i=0; i<BOARD_HEIGHT; i++) { /* clear holdings */
2361         board[i][holdingsColumn] = EmptySquare;
2362         board[i][countsColumn]   = (ChessSquare) 0;
2363     }
2364     while( (p=*holdings++) != NULLCHAR ) {
2365         piece = CharToPiece( ToUpper(p) );
2366         if(piece == EmptySquare) continue;
2367         /*j = (int) piece - (int) WhitePawn;*/
2368         j = PieceToNumber(piece);
2369         if(j >= gameInfo.holdingsSize) continue; /* ignore pieces that do not fit */
2370         if(j < 0) continue;               /* should not happen */
2371         piece = (ChessSquare) ( (int)piece + (int)lowestPiece );
2372         board[holdingsStartRow+j*direction][holdingsColumn] = piece;
2373         board[holdingsStartRow+j*direction][countsColumn]++;
2374     }
2375 }
2376
2377
2378 void
2379 VariantSwitch (Board board, VariantClass newVariant)
2380 {
2381    int newHoldingsWidth, newWidth = 8, newHeight = 8, i, j;
2382    static Board oldBoard;
2383
2384    startedFromPositionFile = FALSE;
2385    if(gameInfo.variant == newVariant) return;
2386
2387    /* [HGM] This routine is called each time an assignment is made to
2388     * gameInfo.variant during a game, to make sure the board sizes
2389     * are set to match the new variant. If that means adding or deleting
2390     * holdings, we shift the playing board accordingly
2391     * This kludge is needed because in ICS observe mode, we get boards
2392     * of an ongoing game without knowing the variant, and learn about the
2393     * latter only later. This can be because of the move list we requested,
2394     * in which case the game history is refilled from the beginning anyway,
2395     * but also when receiving holdings of a crazyhouse game. In the latter
2396     * case we want to add those holdings to the already received position.
2397     */
2398
2399
2400    if (appData.debugMode) {
2401      fprintf(debugFP, "Switch board from %s to %s\n",
2402              VariantName(gameInfo.variant), VariantName(newVariant));
2403      setbuf(debugFP, NULL);
2404    }
2405    shuffleOpenings = 0;       /* [HGM] shuffle */
2406    gameInfo.holdingsSize = 5; /* [HGM] prepare holdings */
2407    switch(newVariant)
2408      {
2409      case VariantShogi:
2410        newWidth = 9;  newHeight = 9;
2411        gameInfo.holdingsSize = 7;
2412      case VariantBughouse:
2413      case VariantCrazyhouse:
2414        newHoldingsWidth = 2; break;
2415      case VariantGreat:
2416        newWidth = 10;
2417      case VariantSuper:
2418        newHoldingsWidth = 2;
2419        gameInfo.holdingsSize = 8;
2420        break;
2421      case VariantGothic:
2422      case VariantCapablanca:
2423      case VariantCapaRandom:
2424        newWidth = 10;
2425      default:
2426        newHoldingsWidth = gameInfo.holdingsSize = 0;
2427      };
2428
2429    if(newWidth  != gameInfo.boardWidth  ||
2430       newHeight != gameInfo.boardHeight ||
2431       newHoldingsWidth != gameInfo.holdingsWidth ) {
2432
2433      /* shift position to new playing area, if needed */
2434      if(newHoldingsWidth > gameInfo.holdingsWidth) {
2435        for(i=0; i<BOARD_HEIGHT; i++)
2436          for(j=BOARD_RGHT-1; j>=BOARD_LEFT; j--)
2437            board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
2438              board[i][j];
2439        for(i=0; i<newHeight; i++) {
2440          board[i][0] = board[i][newWidth+2*newHoldingsWidth-1] = EmptySquare;
2441          board[i][1] = board[i][newWidth+2*newHoldingsWidth-2] = (ChessSquare) 0;
2442        }
2443      } else if(newHoldingsWidth < gameInfo.holdingsWidth) {
2444        for(i=0; i<BOARD_HEIGHT; i++)
2445          for(j=BOARD_LEFT; j<BOARD_RGHT; j++)
2446            board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
2447              board[i][j];
2448      }
2449      board[HOLDINGS_SET] = 0;
2450      gameInfo.boardWidth  = newWidth;
2451      gameInfo.boardHeight = newHeight;
2452      gameInfo.holdingsWidth = newHoldingsWidth;
2453      gameInfo.variant = newVariant;
2454      InitDrawingSizes(-2, 0);
2455    } else gameInfo.variant = newVariant;
2456    CopyBoard(oldBoard, board);   // remember correctly formatted board
2457      InitPosition(FALSE);          /* this sets up board[0], but also other stuff        */
2458    DrawPosition(TRUE, currentMove ? boards[currentMove] : oldBoard);
2459 }
2460
2461 static int loggedOn = FALSE;
2462
2463 /*-- Game start info cache: --*/
2464 int gs_gamenum;
2465 char gs_kind[MSG_SIZ];
2466 static char player1Name[128] = "";
2467 static char player2Name[128] = "";
2468 static char cont_seq[] = "\n\\   ";
2469 static int player1Rating = -1;
2470 static int player2Rating = -1;
2471 /*----------------------------*/
2472
2473 ColorClass curColor = ColorNormal;
2474 int suppressKibitz = 0;
2475
2476 // [HGM] seekgraph
2477 Boolean soughtPending = FALSE;
2478 Boolean seekGraphUp;
2479 #define MAX_SEEK_ADS 200
2480 #define SQUARE 0x80
2481 char *seekAdList[MAX_SEEK_ADS];
2482 int ratingList[MAX_SEEK_ADS], xList[MAX_SEEK_ADS], yList[MAX_SEEK_ADS], seekNrList[MAX_SEEK_ADS], zList[MAX_SEEK_ADS];
2483 float tcList[MAX_SEEK_ADS];
2484 char colorList[MAX_SEEK_ADS];
2485 int nrOfSeekAds = 0;
2486 int minRating = 1010, maxRating = 2800;
2487 int hMargin = 10, vMargin = 20, h, w;
2488 extern int squareSize, lineGap;
2489
2490 void
2491 PlotSeekAd (int i)
2492 {
2493         int x, y, color = 0, r = ratingList[i]; float tc = tcList[i];
2494         xList[i] = yList[i] = -100; // outside graph, so cannot be clicked
2495         if(r < minRating+100 && r >=0 ) r = minRating+100;
2496         if(r > maxRating) r = maxRating;
2497         if(tc < 1.) tc = 1.;
2498         if(tc > 95.) tc = 95.;
2499         x = (w-hMargin-squareSize/8-7)* log(tc)/log(95.) + hMargin;
2500         y = ((double)r - minRating)/(maxRating - minRating)
2501             * (h-vMargin-squareSize/8-1) + vMargin;
2502         if(ratingList[i] < 0) y = vMargin + squareSize/4;
2503         if(strstr(seekAdList[i], " u ")) color = 1;
2504         if(!strstr(seekAdList[i], "lightning") && // for now all wilds same color
2505            !strstr(seekAdList[i], "bullet") &&
2506            !strstr(seekAdList[i], "blitz") &&
2507            !strstr(seekAdList[i], "standard") ) color = 2;
2508         if(strstr(seekAdList[i], "(C) ")) color |= SQUARE; // plot computer seeks as squares
2509         DrawSeekDot(xList[i]=x+3*(color&~SQUARE), yList[i]=h-1-y, colorList[i]=color);
2510 }
2511
2512 void
2513 AddAd (char *handle, char *rating, int base, int inc,  char rated, char *type, int nr, Boolean plot)
2514 {
2515         char buf[MSG_SIZ], *ext = "";
2516         VariantClass v = StringToVariant(type);
2517         if(strstr(type, "wild")) {
2518             ext = type + 4; // append wild number
2519             if(v == VariantFischeRandom) type = "chess960"; else
2520             if(v == VariantLoadable) type = "setup"; else
2521             type = VariantName(v);
2522         }
2523         snprintf(buf, MSG_SIZ, "%s (%s) %d %d %c %s%s", handle, rating, base, inc, rated, type, ext);
2524         if(nrOfSeekAds < MAX_SEEK_ADS-1) {
2525             if(seekAdList[nrOfSeekAds]) free(seekAdList[nrOfSeekAds]);
2526             ratingList[nrOfSeekAds] = -1; // for if seeker has no rating
2527             sscanf(rating, "%d", &ratingList[nrOfSeekAds]);
2528             tcList[nrOfSeekAds] = base + (2./3.)*inc;
2529             seekNrList[nrOfSeekAds] = nr;
2530             zList[nrOfSeekAds] = 0;
2531             seekAdList[nrOfSeekAds++] = StrSave(buf);
2532             if(plot) PlotSeekAd(nrOfSeekAds-1);
2533         }
2534 }
2535
2536 void
2537 EraseSeekDot (int i)
2538 {
2539     int x = xList[i], y = yList[i], d=squareSize/4, k;
2540     DrawSeekBackground(x-squareSize/8, y-squareSize/8, x+squareSize/8+1, y+squareSize/8+1);
2541     if(x < hMargin+d) DrawSeekAxis(hMargin, y-squareSize/8, hMargin, y+squareSize/8+1);
2542     // now replot every dot that overlapped
2543     for(k=0; k<nrOfSeekAds; k++) if(k != i) {
2544         int xx = xList[k], yy = yList[k];
2545         if(xx <= x+d && xx > x-d && yy <= y+d && yy > y-d)
2546             DrawSeekDot(xx, yy, colorList[k]);
2547     }
2548 }
2549
2550 void
2551 RemoveSeekAd (int nr)
2552 {
2553         int i;
2554         for(i=0; i<nrOfSeekAds; i++) if(seekNrList[i] == nr) {
2555             EraseSeekDot(i);
2556             if(seekAdList[i]) free(seekAdList[i]);
2557             seekAdList[i] = seekAdList[--nrOfSeekAds];
2558             seekNrList[i] = seekNrList[nrOfSeekAds];
2559             ratingList[i] = ratingList[nrOfSeekAds];
2560             colorList[i]  = colorList[nrOfSeekAds];
2561             tcList[i] = tcList[nrOfSeekAds];
2562             xList[i]  = xList[nrOfSeekAds];
2563             yList[i]  = yList[nrOfSeekAds];
2564             zList[i]  = zList[nrOfSeekAds];
2565             seekAdList[nrOfSeekAds] = NULL;
2566             break;
2567         }
2568 }
2569
2570 Boolean
2571 MatchSoughtLine (char *line)
2572 {
2573     char handle[MSG_SIZ], rating[MSG_SIZ], type[MSG_SIZ];
2574     int nr, base, inc, u=0; char dummy;
2575
2576     if(sscanf(line, "%d %s %s %d %d rated %s", &nr, rating, handle, &base, &inc, type) == 6 ||
2577        sscanf(line, "%d %s %s %s %d %d rated %c", &nr, rating, handle, type, &base, &inc, &dummy) == 7 ||
2578        (u=1) &&
2579        (sscanf(line, "%d %s %s %d %d unrated %s", &nr, rating, handle, &base, &inc, type) == 6 ||
2580         sscanf(line, "%d %s %s %s %d %d unrated %c", &nr, rating, handle, type, &base, &inc, &dummy) == 7)  ) {
2581         // match: compact and save the line
2582         AddAd(handle, rating, base, inc, u ? 'u' : 'r', type, nr, FALSE);
2583         return TRUE;
2584     }
2585     return FALSE;
2586 }
2587
2588 int
2589 DrawSeekGraph ()
2590 {
2591     int i;
2592     if(!seekGraphUp) return FALSE;
2593     h = BOARD_HEIGHT * (squareSize + lineGap) + lineGap;
2594     w = BOARD_WIDTH  * (squareSize + lineGap) + lineGap;
2595
2596     DrawSeekBackground(0, 0, w, h);
2597     DrawSeekAxis(hMargin, h-1-vMargin, w-5, h-1-vMargin);
2598     DrawSeekAxis(hMargin, h-1-vMargin, hMargin, 5);
2599     for(i=0; i<4000; i+= 100) if(i>=minRating && i<maxRating) {
2600         int yy =((double)i - minRating)/(maxRating - minRating)*(h-vMargin-squareSize/8-1) + vMargin;
2601         yy = h-1-yy;
2602         DrawSeekAxis(hMargin+5*(i%500==0), yy, hMargin-5, yy); // rating ticks
2603         if(i%500 == 0) {
2604             char buf[MSG_SIZ];
2605             snprintf(buf, MSG_SIZ, "%d", i);
2606             DrawSeekText(buf, hMargin+squareSize/8+7, yy);
2607         }
2608     }
2609     DrawSeekText("unrated", hMargin+squareSize/8+7, h-1-vMargin-squareSize/4);
2610     for(i=1; i<100; i+=(i<10?1:5)) {
2611         int xx = (w-hMargin-squareSize/8-7)* log((double)i)/log(95.) + hMargin;
2612         DrawSeekAxis(xx, h-1-vMargin, xx, h-6-vMargin-3*(i%10==0)); // TC ticks
2613         if(i<=5 || (i>40 ? i%20 : i%10) == 0) {
2614             char buf[MSG_SIZ];
2615             snprintf(buf, MSG_SIZ, "%d", i);
2616             DrawSeekText(buf, xx-2-3*(i>9), h-1-vMargin/2);
2617         }
2618     }
2619     for(i=0; i<nrOfSeekAds; i++) PlotSeekAd(i);
2620     return TRUE;
2621 }
2622
2623 int
2624 SeekGraphClick (ClickType click, int x, int y, int moving)
2625 {
2626     static int lastDown = 0, displayed = 0, lastSecond;
2627     if(y < 0) return FALSE;
2628     if(!(appData.seekGraph && appData.icsActive && loggedOn &&
2629         (gameMode == BeginningOfGame || gameMode == IcsIdle))) {
2630         if(!seekGraphUp) return FALSE;
2631         seekGraphUp = FALSE; // seek graph is up when it shouldn't be: take it down
2632         DrawPosition(TRUE, NULL);
2633         return TRUE;
2634     }
2635     if(!seekGraphUp) { // initiate cration of seek graph by requesting seek-ad list
2636         if(click == Release || moving) return FALSE;
2637         nrOfSeekAds = 0;
2638         soughtPending = TRUE;
2639         SendToICS(ics_prefix);
2640         SendToICS("sought\n"); // should this be "sought all"?
2641     } else { // issue challenge based on clicked ad
2642         int dist = 10000; int i, closest = 0, second = 0;
2643         for(i=0; i<nrOfSeekAds; i++) {
2644             int d = (x-xList[i])*(x-xList[i]) +  (y-yList[i])*(y-yList[i]) + zList[i];
2645             if(d < dist) { dist = d; closest = i; }
2646             second += (d - zList[i] < 120); // count in-range ads
2647             if(click == Press && moving != 1 && zList[i]>0) zList[i] *= 0.8; // age priority
2648         }
2649         if(dist < 120) {
2650             char buf[MSG_SIZ];
2651             second = (second > 1);
2652             if(displayed != closest || second != lastSecond) {
2653                 DisplayMessage(second ? "!" : "", seekAdList[closest]);
2654                 lastSecond = second; displayed = closest;
2655             }
2656             if(click == Press) {
2657                 if(moving == 2) zList[closest] = 100; // right-click; push to back on press
2658                 lastDown = closest;
2659                 return TRUE;
2660             } // on press 'hit', only show info
2661             if(moving == 2) return TRUE; // ignore right up-clicks on dot
2662             snprintf(buf, MSG_SIZ, "play %d\n", seekNrList[closest]);
2663             SendToICS(ics_prefix);
2664             SendToICS(buf);
2665             return TRUE; // let incoming board of started game pop down the graph
2666         } else if(click == Release) { // release 'miss' is ignored
2667             zList[lastDown] = 100; // make future selection of the rejected ad more difficult
2668             if(moving == 2) { // right up-click
2669                 nrOfSeekAds = 0; // refresh graph
2670                 soughtPending = TRUE;
2671                 SendToICS(ics_prefix);
2672                 SendToICS("sought\n"); // should this be "sought all"?
2673             }
2674             return TRUE;
2675         } else if(moving) { if(displayed >= 0) DisplayMessage("", ""); displayed = -1; return TRUE; }
2676         // press miss or release hit 'pop down' seek graph
2677         seekGraphUp = FALSE;
2678         DrawPosition(TRUE, NULL);
2679     }
2680     return TRUE;
2681 }
2682
2683 void
2684 read_from_ics (InputSourceRef isr, VOIDSTAR closure, char *data, int count, int error)
2685 {
2686 #define BUF_SIZE (16*1024) /* overflowed at 8K with "inchannel 1" on FICS? */
2687 #define STARTED_NONE 0
2688 #define STARTED_MOVES 1
2689 #define STARTED_BOARD 2
2690 #define STARTED_OBSERVE 3
2691 #define STARTED_HOLDINGS 4
2692 #define STARTED_CHATTER 5
2693 #define STARTED_COMMENT 6
2694 #define STARTED_MOVES_NOHIDE 7
2695
2696     static int started = STARTED_NONE;
2697     static char parse[20000];
2698     static int parse_pos = 0;
2699     static char buf[BUF_SIZE + 1];
2700     static int firstTime = TRUE, intfSet = FALSE;
2701     static ColorClass prevColor = ColorNormal;
2702     static int savingComment = FALSE;
2703     static int cmatch = 0; // continuation sequence match
2704     char *bp;
2705     char str[MSG_SIZ];
2706     int i, oldi;
2707     int buf_len;
2708     int next_out;
2709     int tkind;
2710     int backup;    /* [DM] For zippy color lines */
2711     char *p;
2712     char talker[MSG_SIZ]; // [HGM] chat
2713     int channel;
2714
2715     connectionAlive = TRUE; // [HGM] alive: I think, therefore I am...
2716
2717     if (appData.debugMode) {
2718       if (!error) {
2719         fprintf(debugFP, "<ICS: ");
2720         show_bytes(debugFP, data, count);
2721         fprintf(debugFP, "\n");
2722       }
2723     }
2724
2725     if (appData.debugMode) { int f = forwardMostMove;
2726         fprintf(debugFP, "ics input %d, castling = %d %d %d %d %d %d\n", f,
2727                 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
2728                 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
2729     }
2730     if (count > 0) {
2731         /* If last read ended with a partial line that we couldn't parse,
2732            prepend it to the new read and try again. */
2733         if (leftover_len > 0) {
2734             for (i=0; i<leftover_len; i++)
2735               buf[i] = buf[leftover_start + i];
2736         }
2737
2738     /* copy new characters into the buffer */
2739     bp = buf + leftover_len;
2740     buf_len=leftover_len;
2741     for (i=0; i<count; i++)
2742     {
2743         // ignore these
2744         if (data[i] == '\r')
2745             continue;
2746
2747         // join lines split by ICS?
2748         if (!appData.noJoin)
2749         {
2750             /*
2751                 Joining just consists of finding matches against the
2752                 continuation sequence, and discarding that sequence
2753                 if found instead of copying it.  So, until a match
2754                 fails, there's nothing to do since it might be the
2755                 complete sequence, and thus, something we don't want
2756                 copied.
2757             */
2758             if (data[i] == cont_seq[cmatch])
2759             {
2760                 cmatch++;
2761                 if (cmatch == strlen(cont_seq))
2762                 {
2763                     cmatch = 0; // complete match.  just reset the counter
2764
2765                     /*
2766                         it's possible for the ICS to not include the space
2767                         at the end of the last word, making our [correct]
2768                         join operation fuse two separate words.  the server
2769                         does this when the space occurs at the width setting.
2770                     */
2771                     if (!buf_len || buf[buf_len-1] != ' ')
2772                     {
2773                         *bp++ = ' ';
2774                         buf_len++;
2775                     }
2776                 }
2777                 continue;
2778             }
2779             else if (cmatch)
2780             {
2781                 /*
2782                     match failed, so we have to copy what matched before
2783                     falling through and copying this character.  In reality,
2784                     this will only ever be just the newline character, but
2785                     it doesn't hurt to be precise.
2786                 */
2787                 strncpy(bp, cont_seq, cmatch);
2788                 bp += cmatch;
2789                 buf_len += cmatch;
2790                 cmatch = 0;
2791             }
2792         }
2793
2794         // copy this char
2795         *bp++ = data[i];
2796         buf_len++;
2797     }
2798
2799         buf[buf_len] = NULLCHAR;
2800 //      next_out = leftover_len; // [HGM] should we set this to 0, and not print it in advance?
2801         next_out = 0;
2802         leftover_start = 0;
2803
2804         i = 0;
2805         while (i < buf_len) {
2806             /* Deal with part of the TELNET option negotiation
2807                protocol.  We refuse to do anything beyond the
2808                defaults, except that we allow the WILL ECHO option,
2809                which ICS uses to turn off password echoing when we are
2810                directly connected to it.  We reject this option
2811                if localLineEditing mode is on (always on in xboard)
2812                and we are talking to port 23, which might be a real
2813                telnet server that will try to keep WILL ECHO on permanently.
2814              */
2815             if (buf_len - i >= 3 && (unsigned char) buf[i] == TN_IAC) {
2816                 static int remoteEchoOption = FALSE; /* telnet ECHO option */
2817                 unsigned char option;
2818                 oldi = i;
2819                 switch ((unsigned char) buf[++i]) {
2820                   case TN_WILL:
2821                     if (appData.debugMode)
2822                       fprintf(debugFP, "\n<WILL ");
2823                     switch (option = (unsigned char) buf[++i]) {
2824                       case TN_ECHO:
2825                         if (appData.debugMode)
2826                           fprintf(debugFP, "ECHO ");
2827                         /* Reply only if this is a change, according
2828                            to the protocol rules. */
2829                         if (remoteEchoOption) break;
2830                         if (appData.localLineEditing &&
2831                             atoi(appData.icsPort) == TN_PORT) {
2832                             TelnetRequest(TN_DONT, TN_ECHO);
2833                         } else {
2834                             EchoOff();
2835                             TelnetRequest(TN_DO, TN_ECHO);
2836                             remoteEchoOption = TRUE;
2837                         }
2838                         break;
2839                       default:
2840                         if (appData.debugMode)
2841                           fprintf(debugFP, "%d ", option);
2842                         /* Whatever this is, we don't want it. */
2843                         TelnetRequest(TN_DONT, option);
2844                         break;
2845                     }
2846                     break;
2847                   case TN_WONT:
2848                     if (appData.debugMode)
2849                       fprintf(debugFP, "\n<WONT ");
2850                     switch (option = (unsigned char) buf[++i]) {
2851                       case TN_ECHO:
2852                         if (appData.debugMode)
2853                           fprintf(debugFP, "ECHO ");
2854                         /* Reply only if this is a change, according
2855                            to the protocol rules. */
2856                         if (!remoteEchoOption) break;
2857                         EchoOn();
2858                         TelnetRequest(TN_DONT, TN_ECHO);
2859                         remoteEchoOption = FALSE;
2860                         break;
2861                       default:
2862                         if (appData.debugMode)
2863                           fprintf(debugFP, "%d ", (unsigned char) option);
2864                         /* Whatever this is, it must already be turned
2865                            off, because we never agree to turn on
2866                            anything non-default, so according to the
2867                            protocol rules, we don't reply. */
2868                         break;
2869                     }
2870                     break;
2871                   case TN_DO:
2872                     if (appData.debugMode)
2873                       fprintf(debugFP, "\n<DO ");
2874                     switch (option = (unsigned char) buf[++i]) {
2875                       default:
2876                         /* Whatever this is, we refuse to do it. */
2877                         if (appData.debugMode)
2878                           fprintf(debugFP, "%d ", option);
2879                         TelnetRequest(TN_WONT, option);
2880                         break;
2881                     }
2882                     break;
2883                   case TN_DONT:
2884                     if (appData.debugMode)
2885                       fprintf(debugFP, "\n<DONT ");
2886                     switch (option = (unsigned char) buf[++i]) {
2887                       default:
2888                         if (appData.debugMode)
2889                           fprintf(debugFP, "%d ", option);
2890                         /* Whatever this is, we are already not doing
2891                            it, because we never agree to do anything
2892                            non-default, so according to the protocol
2893                            rules, we don't reply. */
2894                         break;
2895                     }
2896                     break;
2897                   case TN_IAC:
2898                     if (appData.debugMode)
2899                       fprintf(debugFP, "\n<IAC ");
2900                     /* Doubled IAC; pass it through */
2901                     i--;
2902                     break;
2903                   default:
2904                     if (appData.debugMode)
2905                       fprintf(debugFP, "\n<%d ", (unsigned char) buf[i]);
2906                     /* Drop all other telnet commands on the floor */
2907                     break;
2908                 }
2909                 if (oldi > next_out)
2910                   SendToPlayer(&buf[next_out], oldi - next_out);
2911                 if (++i > next_out)
2912                   next_out = i;
2913                 continue;
2914             }
2915
2916             /* OK, this at least will *usually* work */
2917             if (!loggedOn && looking_at(buf, &i, "ics%")) {
2918                 loggedOn = TRUE;
2919             }
2920
2921             if (loggedOn && !intfSet) {
2922                 if (ics_type == ICS_ICC) {
2923                   snprintf(str, MSG_SIZ,
2924                           "/set-quietly interface %s\n/set-quietly style 12\n",
2925                           programVersion);
2926                   if(appData.seekGraph && appData.autoRefresh) // [HGM] seekgraph
2927                       strcat(str, "/set-2 51 1\n/set seek 1\n");
2928                 } else if (ics_type == ICS_CHESSNET) {
2929                   snprintf(str, MSG_SIZ, "/style 12\n");
2930                 } else {
2931                   safeStrCpy(str, "alias $ @\n$set interface ", sizeof(str)/sizeof(str[0]));
2932                   strcat(str, programVersion);
2933                   strcat(str, "\n$iset startpos 1\n$iset ms 1\n");
2934                   if(appData.seekGraph && appData.autoRefresh) // [HGM] seekgraph
2935                       strcat(str, "$iset seekremove 1\n$set seek 1\n");
2936 #ifdef WIN32
2937                   strcat(str, "$iset nohighlight 1\n");
2938 #endif
2939                   strcat(str, "$iset lock 1\n$style 12\n");
2940                 }
2941                 SendToICS(str);
2942                 NotifyFrontendLogin();
2943                 intfSet = TRUE;
2944             }
2945
2946             if (started == STARTED_COMMENT) {
2947                 /* Accumulate characters in comment */
2948                 parse[parse_pos++] = buf[i];
2949                 if (buf[i] == '\n') {
2950                     parse[parse_pos] = NULLCHAR;
2951                     if(chattingPartner>=0) {
2952                         char mess[MSG_SIZ];
2953                         snprintf(mess, MSG_SIZ, "%s%s", talker, parse);
2954                         OutputChatMessage(chattingPartner, mess);
2955                         chattingPartner = -1;
2956                         next_out = i+1; // [HGM] suppress printing in ICS window
2957                     } else
2958                     if(!suppressKibitz) // [HGM] kibitz
2959                         AppendComment(forwardMostMove, StripHighlight(parse), TRUE);
2960                     else { // [HGM kibitz: divert memorized engine kibitz to engine-output window
2961                         int nrDigit = 0, nrAlph = 0, j;
2962                         if(parse_pos > MSG_SIZ - 30) // defuse unreasonably long input
2963                         { parse_pos = MSG_SIZ-30; parse[parse_pos - 1] = '\n'; }
2964                         parse[parse_pos] = NULLCHAR;
2965                         // try to be smart: if it does not look like search info, it should go to
2966                         // ICS interaction window after all, not to engine-output window.
2967                         for(j=0; j<parse_pos; j++) { // count letters and digits
2968                             nrDigit += (parse[j] >= '0' && parse[j] <= '9');
2969                             nrAlph  += (parse[j] >= 'a' && parse[j] <= 'z');
2970                             nrAlph  += (parse[j] >= 'A' && parse[j] <= 'Z');
2971                         }
2972                         if(nrAlph < 9*nrDigit) { // if more than 10% digit we assume search info
2973                             int depth=0; float score;
2974                             if(sscanf(parse, "!!! %f/%d", &score, &depth) == 2 && depth>0) {
2975                                 // [HGM] kibitz: save kibitzed opponent info for PGN and eval graph
2976                                 pvInfoList[forwardMostMove-1].depth = depth;
2977                                 pvInfoList[forwardMostMove-1].score = 100*score;
2978                             }
2979                             OutputKibitz(suppressKibitz, parse);
2980                         } else {
2981                             char tmp[MSG_SIZ];
2982                             if(gameMode == IcsObserving) // restore original ICS messages
2983                               snprintf(tmp, MSG_SIZ, "%s kibitzes: %s", star_match[0], parse);
2984                             else
2985                             snprintf(tmp, MSG_SIZ, _("your opponent kibitzes: %s"), parse);
2986                             SendToPlayer(tmp, strlen(tmp));
2987                         }
2988                         next_out = i+1; // [HGM] suppress printing in ICS window
2989                     }
2990                     started = STARTED_NONE;
2991                 } else {
2992                     /* Don't match patterns against characters in comment */
2993                     i++;
2994                     continue;
2995                 }
2996             }
2997             if (started == STARTED_CHATTER) {
2998                 if (buf[i] != '\n') {
2999                     /* Don't match patterns against characters in chatter */
3000                     i++;
3001                     continue;
3002                 }
3003                 started = STARTED_NONE;
3004                 if(suppressKibitz) next_out = i+1;
3005             }
3006
3007             /* Kludge to deal with rcmd protocol */
3008             if (firstTime && looking_at(buf, &i, "\001*")) {
3009                 DisplayFatalError(&buf[1], 0, 1);
3010                 continue;
3011             } else {
3012                 firstTime = FALSE;
3013             }
3014
3015             if (!loggedOn && looking_at(buf, &i, "chessclub.com")) {
3016                 ics_type = ICS_ICC;
3017                 ics_prefix = "/";
3018                 if (appData.debugMode)
3019                   fprintf(debugFP, "ics_type %d\n", ics_type);
3020                 continue;
3021             }
3022             if (!loggedOn && looking_at(buf, &i, "freechess.org")) {
3023                 ics_type = ICS_FICS;
3024                 ics_prefix = "$";
3025                 if (appData.debugMode)
3026                   fprintf(debugFP, "ics_type %d\n", ics_type);
3027                 continue;
3028             }
3029             if (!loggedOn && looking_at(buf, &i, "chess.net")) {
3030                 ics_type = ICS_CHESSNET;
3031                 ics_prefix = "/";
3032                 if (appData.debugMode)
3033                   fprintf(debugFP, "ics_type %d\n", ics_type);
3034                 continue;
3035             }
3036
3037             if (!loggedOn &&
3038                 (looking_at(buf, &i, "\"*\" is *a registered name") ||
3039                  looking_at(buf, &i, "Logging you in as \"*\"") ||
3040                  looking_at(buf, &i, "will be \"*\""))) {
3041               safeStrCpy(ics_handle, star_match[0], sizeof(ics_handle)/sizeof(ics_handle[0]));
3042               continue;
3043             }
3044
3045             if (loggedOn && !have_set_title && ics_handle[0] != NULLCHAR) {
3046               char buf[MSG_SIZ];
3047               snprintf(buf, sizeof(buf), "%s@%s", ics_handle, appData.icsHost);
3048               DisplayIcsInteractionTitle(buf);
3049               have_set_title = TRUE;
3050             }
3051
3052             /* skip finger notes */
3053             if (started == STARTED_NONE &&
3054                 ((buf[i] == ' ' && isdigit(buf[i+1])) ||
3055                  (buf[i] == '1' && buf[i+1] == '0')) &&
3056                 buf[i+2] == ':' && buf[i+3] == ' ') {
3057               started = STARTED_CHATTER;
3058               i += 3;
3059               continue;
3060             }
3061
3062             oldi = i;
3063             // [HGM] seekgraph: recognize sought lines and end-of-sought message
3064             if(appData.seekGraph) {
3065                 if(soughtPending && MatchSoughtLine(buf+i)) {
3066                     i = strstr(buf+i, "rated") - buf;
3067                     if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3068                     next_out = leftover_start = i;
3069                     started = STARTED_CHATTER;
3070                     suppressKibitz = TRUE;
3071                     continue;
3072                 }
3073                 if((gameMode == IcsIdle || gameMode == BeginningOfGame)
3074                         && looking_at(buf, &i, "* ads displayed")) {
3075                     soughtPending = FALSE;
3076                     seekGraphUp = TRUE;
3077                     DrawSeekGraph();
3078                     continue;
3079                 }
3080                 if(appData.autoRefresh) {
3081                     if(looking_at(buf, &i, "* (*) seeking * * * * *\"play *\" to respond)\n")) {
3082                         int s = (ics_type == ICS_ICC); // ICC format differs
3083                         if(seekGraphUp)
3084                         AddAd(star_match[0], star_match[1], atoi(star_match[2+s]), atoi(star_match[3+s]),
3085                               star_match[4+s][0], star_match[5-3*s], atoi(star_match[7]), TRUE);
3086                         looking_at(buf, &i, "*% "); // eat prompt
3087                         if(oldi > 0 && buf[oldi-1] == '\n') oldi--; // suppress preceding LF, if any
3088                         if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3089                         next_out = i; // suppress
3090                         continue;
3091                     }
3092                     if(looking_at(buf, &i, "\nAds removed: *\n") || looking_at(buf, &i, "\031(51 * *\031)")) {
3093                         char *p = star_match[0];
3094                         while(*p) {
3095                             if(seekGraphUp) RemoveSeekAd(atoi(p));
3096                             while(*p && *p++ != ' '); // next
3097                         }
3098                         looking_at(buf, &i, "*% "); // eat prompt
3099                         if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3100                         next_out = i;
3101                         continue;
3102                     }
3103                 }
3104             }
3105
3106             /* skip formula vars */
3107             if (started == STARTED_NONE &&
3108                 buf[i] == 'f' && isdigit(buf[i+1]) && buf[i+2] == ':') {
3109               started = STARTED_CHATTER;
3110               i += 3;
3111               continue;
3112             }
3113
3114             // [HGM] kibitz: try to recognize opponent engine-score kibitzes, to divert them to engine-output window
3115             if (appData.autoKibitz && started == STARTED_NONE &&
3116                 !appData.icsEngineAnalyze &&                     // [HGM] [DM] ICS analyze
3117                 (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack || gameMode == IcsObserving)) {
3118                 if((looking_at(buf, &i, "\n* kibitzes: ") || looking_at(buf, &i, "\n* whispers: ") ||
3119                     looking_at(buf, &i, "* kibitzes: ") || looking_at(buf, &i, "* whispers: ")) &&
3120                    (StrStr(star_match[0], gameInfo.white) == star_match[0] ||
3121                     StrStr(star_match[0], gameInfo.black) == star_match[0]   )) { // kibitz of self or opponent
3122                         suppressKibitz = TRUE;
3123                         if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3124                         next_out = i;
3125                         if((StrStr(star_match[0], gameInfo.white) == star_match[0]
3126                                 && (gameMode == IcsPlayingWhite)) ||
3127                            (StrStr(star_match[0], gameInfo.black) == star_match[0]
3128                                 && (gameMode == IcsPlayingBlack))   ) // opponent kibitz
3129                             started = STARTED_CHATTER; // own kibitz we simply discard
3130                         else {
3131                             started = STARTED_COMMENT; // make sure it will be collected in parse[]
3132                             parse_pos = 0; parse[0] = NULLCHAR;
3133                             savingComment = TRUE;
3134                             suppressKibitz = gameMode != IcsObserving ? 2 :
3135                                 (StrStr(star_match[0], gameInfo.white) == NULL) + 1;
3136                         }
3137                         continue;
3138                 } else
3139                 if((looking_at(buf, &i, "\nkibitzed to *\n") || looking_at(buf, &i, "kibitzed to *\n") ||
3140                     looking_at(buf, &i, "\n(kibitzed to *\n") || looking_at(buf, &i, "(kibitzed to *\n"))
3141                          && atoi(star_match[0])) {
3142                     // suppress the acknowledgements of our own autoKibitz
3143                     char *p;
3144                     if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3145                     if(p = strchr(star_match[0], ' ')) p[1] = NULLCHAR; // clip off "players)" on FICS
3146                     SendToPlayer(star_match[0], strlen(star_match[0]));
3147                     if(looking_at(buf, &i, "*% ")) // eat prompt
3148                         suppressKibitz = FALSE;
3149                     next_out = i;
3150                     continue;
3151                 }
3152             } // [HGM] kibitz: end of patch
3153
3154             if(looking_at(buf, &i, "* rating adjustment: * --> *\n")) continue;
3155
3156             // [HGM] chat: intercept tells by users for which we have an open chat window
3157             channel = -1;
3158             if(started == STARTED_NONE && (looking_at(buf, &i, "* tells you:") || looking_at(buf, &i, "* says:") ||
3159                                            looking_at(buf, &i, "* whispers:") ||
3160                                            looking_at(buf, &i, "* kibitzes:") ||
3161                                            looking_at(buf, &i, "* shouts:") ||
3162                                            looking_at(buf, &i, "* c-shouts:") ||
3163                                            looking_at(buf, &i, "--> * ") ||
3164                                            looking_at(buf, &i, "*(*):") && (sscanf(star_match[1], "%d", &channel),1) ||
3165                                            looking_at(buf, &i, "*(*)(*):") && (sscanf(star_match[2], "%d", &channel),1) ||
3166                                            looking_at(buf, &i, "*(*)(*)(*):") && (sscanf(star_match[3], "%d", &channel),1) ||
3167                                            looking_at(buf, &i, "*(*)(*)(*)(*):") && sscanf(star_match[4], "%d", &channel) == 1 )) {
3168                 int p;
3169                 sscanf(star_match[0], "%[^(]", talker+1); // strip (C) or (U) off ICS handle
3170                 chattingPartner = -1;
3171
3172                 if(channel >= 0) // channel broadcast; look if there is a chatbox for this channel
3173                 for(p=0; p<MAX_CHAT; p++) {
3174                     if(chatPartner[p][0] >= '0' && chatPartner[p][0] <= '9' && channel == atoi(chatPartner[p])) {
3175                     talker[0] = '['; strcat(talker, "] ");
3176                     Colorize(channel == 1 ? ColorChannel1 : ColorChannel, FALSE);
3177                     chattingPartner = p; break;
3178                     }
3179                 } else
3180                 if(buf[i-3] == 'e') // kibitz; look if there is a KIBITZ chatbox
3181                 for(p=0; p<MAX_CHAT; p++) {
3182                     if(!strcmp("kibitzes", chatPartner[p])) {
3183                         talker[0] = '['; strcat(talker, "] ");
3184                         chattingPartner = p; break;
3185                     }
3186                 } else
3187                 if(buf[i-3] == 'r') // whisper; look if there is a WHISPER chatbox
3188                 for(p=0; p<MAX_CHAT; p++) {
3189                     if(!strcmp("whispers", chatPartner[p])) {
3190                         talker[0] = '['; strcat(talker, "] ");
3191                         chattingPartner = p; break;
3192                     }
3193                 } else
3194                 if(buf[i-3] == 't' || buf[oldi+2] == '>') {// shout, c-shout or it; look if there is a 'shouts' chatbox
3195                   if(buf[i-8] == '-' && buf[i-3] == 't')
3196                   for(p=0; p<MAX_CHAT; p++) { // c-shout; check if dedicatesd c-shout box exists
3197                     if(!strcmp("c-shouts", chatPartner[p])) {
3198                         talker[0] = '('; strcat(talker, ") "); Colorize(ColorSShout, FALSE);
3199                         chattingPartner = p; break;
3200                     }
3201                   }
3202                   if(chattingPartner < 0)
3203                   for(p=0; p<MAX_CHAT; p++) {
3204                     if(!strcmp("shouts", chatPartner[p])) {
3205                         if(buf[oldi+2] == '>') { talker[0] = '<'; strcat(talker, "> "); Colorize(ColorShout, FALSE); }
3206                         else if(buf[i-8] == '-') { talker[0] = '('; strcat(talker, ") "); Colorize(ColorSShout, FALSE); }
3207                         else { talker[0] = '['; strcat(talker, "] "); Colorize(ColorShout, FALSE); }
3208                         chattingPartner = p; break;
3209                     }
3210                   }
3211                 }
3212                 if(chattingPartner<0) // if not, look if there is a chatbox for this indivdual
3213                 for(p=0; p<MAX_CHAT; p++) if(!StrCaseCmp(talker+1, chatPartner[p])) {
3214                     talker[0] = 0; Colorize(ColorTell, FALSE);
3215                     chattingPartner = p; break;
3216                 }
3217                 if(chattingPartner<0) i = oldi; else {
3218                     Colorize(curColor, TRUE); // undo the bogus colorations we just made to trigger the souds
3219                     if(oldi > 0 && buf[oldi-1] == '\n') oldi--;
3220                     if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3221                     started = STARTED_COMMENT;
3222                     parse_pos = 0; parse[0] = NULLCHAR;
3223                     savingComment = 3 + chattingPartner; // counts as TRUE
3224                     suppressKibitz = TRUE;
3225                     continue;
3226                 }
3227             } // [HGM] chat: end of patch
3228
3229           backup = i;
3230             if (appData.zippyTalk || appData.zippyPlay) {
3231                 /* [DM] Backup address for color zippy lines */
3232 #if ZIPPY
3233                if (loggedOn == TRUE)
3234                        if (ZippyControl(buf, &backup) || ZippyConverse(buf, &backup) ||
3235                           (appData.zippyPlay && ZippyMatch(buf, &backup)));
3236 #endif
3237             } // [DM] 'else { ' deleted
3238                 if (
3239                     /* Regular tells and says */
3240                     (tkind = 1, looking_at(buf, &i, "* tells you: ")) ||
3241                     looking_at(buf, &i, "* (your partner) tells you: ") ||
3242                     looking_at(buf, &i, "* says: ") ||
3243                     /* Don't color "message" or "messages" output */
3244                     (tkind = 5, looking_at(buf, &i, "*. * (*:*): ")) ||
3245                     looking_at(buf, &i, "*. * at *:*: ") ||
3246                     looking_at(buf, &i, "--* (*:*): ") ||
3247                     /* Message notifications (same color as tells) */
3248                     looking_at(buf, &i, "* has left a message ") ||
3249                     looking_at(buf, &i, "* just sent you a message:\n") ||
3250                     /* Whispers and kibitzes */
3251                     (tkind = 2, looking_at(buf, &i, "* whispers: ")) ||
3252                     looking_at(buf, &i, "* kibitzes: ") ||
3253                     /* Channel tells */
3254                     (tkind = 3, looking_at(buf, &i, "*(*: "))) {
3255
3256                   if (tkind == 1 && strchr(star_match[0], ':')) {
3257                       /* Avoid "tells you:" spoofs in channels */
3258                      tkind = 3;
3259                   }
3260                   if (star_match[0][0] == NULLCHAR ||
3261                       strchr(star_match[0], ' ') ||
3262                       (tkind == 3 && strchr(star_match[1], ' '))) {
3263                     /* Reject bogus matches */
3264                     i = oldi;
3265                   } else {
3266                     if (appData.colorize) {
3267                       if (oldi > next_out) {
3268                         SendToPlayer(&buf[next_out], oldi - next_out);
3269                         next_out = oldi;
3270                       }
3271                       switch (tkind) {
3272                       case 1:
3273                         Colorize(ColorTell, FALSE);
3274                         curColor = ColorTell;
3275                         break;
3276                       case 2:
3277                         Colorize(ColorKibitz, FALSE);
3278                         curColor = ColorKibitz;
3279                         break;
3280                       case 3:
3281                         p = strrchr(star_match[1], '(');
3282                         if (p == NULL) {
3283                           p = star_match[1];
3284                         } else {
3285                           p++;
3286                         }
3287                         if (atoi(p) == 1) {
3288                           Colorize(ColorChannel1, FALSE);
3289                           curColor = ColorChannel1;
3290                         } else {
3291                           Colorize(ColorChannel, FALSE);
3292                           curColor = ColorChannel;
3293                         }
3294                         break;
3295                       case 5:
3296                         curColor = ColorNormal;
3297                         break;
3298                       }
3299                     }
3300                     if (started == STARTED_NONE && appData.autoComment &&
3301                         (gameMode == IcsObserving ||
3302                          gameMode == IcsPlayingWhite ||
3303                          gameMode == IcsPlayingBlack)) {
3304                       parse_pos = i - oldi;
3305                       memcpy(parse, &buf[oldi], parse_pos);
3306                       parse[parse_pos] = NULLCHAR;
3307                       started = STARTED_COMMENT;
3308                       savingComment = TRUE;
3309                     } else {
3310                       started = STARTED_CHATTER;
3311                       savingComment = FALSE;
3312                     }
3313                     loggedOn = TRUE;
3314                     continue;
3315                   }
3316                 }
3317
3318                 if (looking_at(buf, &i, "* s-shouts: ") ||
3319                     looking_at(buf, &i, "* c-shouts: ")) {
3320                     if (appData.colorize) {
3321                         if (oldi > next_out) {
3322                             SendToPlayer(&buf[next_out], oldi - next_out);
3323                             next_out = oldi;
3324                         }
3325                         Colorize(ColorSShout, FALSE);
3326                         curColor = ColorSShout;
3327                     }
3328                     loggedOn = TRUE;
3329                     started = STARTED_CHATTER;
3330                     continue;
3331                 }
3332
3333                 if (looking_at(buf, &i, "--->")) {
3334                     loggedOn = TRUE;
3335                     continue;
3336                 }
3337
3338                 if (looking_at(buf, &i, "* shouts: ") ||
3339                     looking_at(buf, &i, "--> ")) {
3340                     if (appData.colorize) {
3341                         if (oldi > next_out) {
3342                             SendToPlayer(&buf[next_out], oldi - next_out);
3343                             next_out = oldi;
3344                         }
3345                         Colorize(ColorShout, FALSE);
3346                         curColor = ColorShout;
3347                     }
3348                     loggedOn = TRUE;
3349                     started = STARTED_CHATTER;
3350                     continue;
3351                 }
3352
3353                 if (looking_at( buf, &i, "Challenge:")) {
3354                     if (appData.colorize) {
3355                         if (oldi > next_out) {
3356                             SendToPlayer(&buf[next_out], oldi - next_out);
3357                             next_out = oldi;
3358                         }
3359                         Colorize(ColorChallenge, FALSE);
3360                         curColor = ColorChallenge;
3361                     }
3362                     loggedOn = TRUE;
3363                     continue;
3364                 }
3365
3366                 if (looking_at(buf, &i, "* offers you") ||
3367                     looking_at(buf, &i, "* offers to be") ||
3368                     looking_at(buf, &i, "* would like to") ||
3369                     looking_at(buf, &i, "* requests to") ||
3370                     looking_at(buf, &i, "Your opponent offers") ||
3371                     looking_at(buf, &i, "Your opponent requests")) {
3372
3373                     if (appData.colorize) {
3374                         if (oldi > next_out) {
3375                             SendToPlayer(&buf[next_out], oldi - next_out);
3376                             next_out = oldi;
3377                         }
3378                         Colorize(ColorRequest, FALSE);
3379                         curColor = ColorRequest;
3380                     }
3381                     continue;
3382                 }
3383
3384                 if (looking_at(buf, &i, "* (*) seeking")) {
3385                     if (appData.colorize) {
3386                         if (oldi > next_out) {
3387                             SendToPlayer(&buf[next_out], oldi - next_out);
3388                             next_out = oldi;
3389                         }
3390                         Colorize(ColorSeek, FALSE);
3391                         curColor = ColorSeek;
3392                     }
3393                     continue;
3394             }
3395
3396           if(i < backup) { i = backup; continue; } // [HGM] for if ZippyControl matches, but the colorie code doesn't
3397
3398             if (looking_at(buf, &i, "\\   ")) {
3399                 if (prevColor != ColorNormal) {
3400                     if (oldi > next_out) {
3401                         SendToPlayer(&buf[next_out], oldi - next_out);
3402                         next_out = oldi;
3403                     }
3404                     Colorize(prevColor, TRUE);
3405                     curColor = prevColor;
3406                 }
3407                 if (savingComment) {
3408                     parse_pos = i - oldi;
3409                     memcpy(parse, &buf[oldi], parse_pos);
3410                     parse[parse_pos] = NULLCHAR;
3411                     started = STARTED_COMMENT;
3412                     if(savingComment >= 3) // [HGM] chat: continuation of line for chat box
3413                         chattingPartner = savingComment - 3; // kludge to remember the box
3414                 } else {
3415                     started = STARTED_CHATTER;
3416                 }
3417                 continue;
3418             }
3419
3420             if (looking_at(buf, &i, "Black Strength :") ||
3421                 looking_at(buf, &i, "<<< style 10 board >>>") ||
3422                 looking_at(buf, &i, "<10>") ||
3423                 looking_at(buf, &i, "#@#")) {
3424                 /* Wrong board style */
3425                 loggedOn = TRUE;
3426                 SendToICS(ics_prefix);
3427                 SendToICS("set style 12\n");
3428                 SendToICS(ics_prefix);
3429                 SendToICS("refresh\n");
3430                 continue;
3431             }
3432
3433             if (!have_sent_ICS_logon && looking_at(buf, &i, "login:")) {
3434                 ICSInitScript();
3435                 have_sent_ICS_logon = 1;
3436                 continue;
3437             }
3438
3439             if (ics_getting_history != H_GETTING_MOVES /*smpos kludge*/ &&
3440                 (looking_at(buf, &i, "\n<12> ") ||
3441                  looking_at(buf, &i, "<12> "))) {
3442                 loggedOn = TRUE;
3443                 if (oldi > next_out) {
3444                     SendToPlayer(&buf[next_out], oldi - next_out);
3445                 }
3446                 next_out = i;
3447                 started = STARTED_BOARD;
3448                 parse_pos = 0;
3449                 continue;
3450             }
3451
3452             if ((started == STARTED_NONE && looking_at(buf, &i, "\n<b1> ")) ||
3453                 looking_at(buf, &i, "<b1> ")) {
3454                 if (oldi > next_out) {
3455                     SendToPlayer(&buf[next_out], oldi - next_out);
3456                 }
3457                 next_out = i;
3458                 started = STARTED_HOLDINGS;
3459                 parse_pos = 0;
3460                 continue;
3461             }
3462
3463             if (looking_at(buf, &i, "* *vs. * *--- *")) {
3464                 loggedOn = TRUE;
3465                 /* Header for a move list -- first line */
3466
3467                 switch (ics_getting_history) {
3468                   case H_FALSE:
3469                     switch (gameMode) {
3470                       case IcsIdle:
3471                       case BeginningOfGame:
3472                         /* User typed "moves" or "oldmoves" while we
3473                            were idle.  Pretend we asked for these
3474                            moves and soak them up so user can step
3475                            through them and/or save them.
3476                            */
3477                         Reset(FALSE, TRUE);
3478                         gameMode = IcsObserving;
3479                         ModeHighlight();
3480                         ics_gamenum = -1;
3481                         ics_getting_history = H_GOT_UNREQ_HEADER;
3482                         break;
3483                       case EditGame: /*?*/
3484                       case EditPosition: /*?*/
3485                         /* Should above feature work in these modes too? */
3486                         /* For now it doesn't */
3487                         ics_getting_history = H_GOT_UNWANTED_HEADER;
3488                         break;
3489                       default:
3490                         ics_getting_history = H_GOT_UNWANTED_HEADER;
3491                         break;
3492                     }
3493                     break;
3494                   case H_REQUESTED:
3495                     /* Is this the right one? */
3496                     if (gameInfo.white && gameInfo.black &&
3497                         strcmp(gameInfo.white, star_match[0]) == 0 &&
3498                         strcmp(gameInfo.black, star_match[2]) == 0) {
3499                         /* All is well */
3500                         ics_getting_history = H_GOT_REQ_HEADER;
3501                     }
3502                     break;
3503                   case H_GOT_REQ_HEADER:
3504                   case H_GOT_UNREQ_HEADER:
3505                   case H_GOT_UNWANTED_HEADER:
3506                   case H_GETTING_MOVES:
3507                     /* Should not happen */
3508                     DisplayError(_("Error gathering move list: two headers"), 0);
3509                     ics_getting_history = H_FALSE;
3510                     break;
3511                 }
3512
3513                 /* Save player ratings into gameInfo if needed */
3514                 if ((ics_getting_history == H_GOT_REQ_HEADER ||
3515                      ics_getting_history == H_GOT_UNREQ_HEADER) &&
3516                     (gameInfo.whiteRating == -1 ||
3517                      gameInfo.blackRating == -1)) {
3518
3519                     gameInfo.whiteRating = string_to_rating(star_match[1]);
3520                     gameInfo.blackRating = string_to_rating(star_match[3]);
3521                     if (appData.debugMode)
3522                       fprintf(debugFP, _("Ratings from header: W %d, B %d\n"),
3523                               gameInfo.whiteRating, gameInfo.blackRating);
3524                 }
3525                 continue;
3526             }
3527
3528             if (looking_at(buf, &i,
3529               "* * match, initial time: * minute*, increment: * second")) {
3530                 /* Header for a move list -- second line */
3531                 /* Initial board will follow if this is a wild game */
3532                 if (gameInfo.event != NULL) free(gameInfo.event);
3533                 snprintf(str, MSG_SIZ, "ICS %s %s match", star_match[0], star_match[1]);
3534                 gameInfo.event = StrSave(str);
3535                 /* [HGM] we switched variant. Translate boards if needed. */
3536                 VariantSwitch(boards[currentMove], StringToVariant(gameInfo.event));
3537                 continue;
3538             }
3539
3540             if (looking_at(buf, &i, "Move  ")) {
3541                 /* Beginning of a move list */
3542                 switch (ics_getting_history) {
3543                   case H_FALSE:
3544                     /* Normally should not happen */
3545                     /* Maybe user hit reset while we were parsing */
3546                     break;
3547                   case H_REQUESTED:
3548                     /* Happens if we are ignoring a move list that is not
3549                      * the one we just requested.  Common if the user
3550                      * tries to observe two games without turning off
3551                      * getMoveList */
3552                     break;
3553                   case H_GETTING_MOVES:
3554                     /* Should not happen */
3555                     DisplayError(_("Error gathering move list: nested"), 0);
3556                     ics_getting_history = H_FALSE;
3557                     break;
3558                   case H_GOT_REQ_HEADER:
3559                     ics_getting_history = H_GETTING_MOVES;
3560                     started = STARTED_MOVES;
3561                     parse_pos = 0;
3562                     if (oldi > next_out) {
3563                         SendToPlayer(&buf[next_out], oldi - next_out);
3564                     }
3565                     break;
3566                   case H_GOT_UNREQ_HEADER:
3567                     ics_getting_history = H_GETTING_MOVES;
3568                     started = STARTED_MOVES_NOHIDE;
3569                     parse_pos = 0;
3570                     break;
3571                   case H_GOT_UNWANTED_HEADER:
3572                     ics_getting_history = H_FALSE;
3573                     break;
3574                 }
3575                 continue;
3576             }
3577
3578             if (looking_at(buf, &i, "% ") ||
3579                 ((started == STARTED_MOVES || started == STARTED_MOVES_NOHIDE)
3580                  && looking_at(buf, &i, "}*"))) { char *bookHit = NULL; // [HGM] book
3581                 if(soughtPending && nrOfSeekAds) { // [HGM] seekgraph: on ICC sought-list has no termination line
3582                     soughtPending = FALSE;
3583                     seekGraphUp = TRUE;
3584                     DrawSeekGraph();
3585                 }
3586                 if(suppressKibitz) next_out = i;
3587                 savingComment = FALSE;
3588                 suppressKibitz = 0;
3589                 switch (started) {
3590                   case STARTED_MOVES:
3591                   case STARTED_MOVES_NOHIDE:
3592                     memcpy(&parse[parse_pos], &buf[oldi], i - oldi);
3593                     parse[parse_pos + i - oldi] = NULLCHAR;
3594                     ParseGameHistory(parse);
3595 #if ZIPPY
3596                     if (appData.zippyPlay && first.initDone) {
3597                         FeedMovesToProgram(&first, forwardMostMove);
3598                         if (gameMode == IcsPlayingWhite) {
3599                             if (WhiteOnMove(forwardMostMove)) {
3600                                 if (first.sendTime) {
3601                                   if (first.useColors) {
3602                                     SendToProgram("black\n", &first);
3603                                   }
3604                                   SendTimeRemaining(&first, TRUE);
3605                                 }
3606                                 if (first.useColors) {
3607                                   SendToProgram("white\n", &first); // [HGM] book: made sending of "go\n" book dependent
3608                                 }
3609                                 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: probe book for initial pos
3610                                 first.maybeThinking = TRUE;
3611                             } else {
3612                                 if (first.usePlayother) {
3613                                   if (first.sendTime) {
3614                                     SendTimeRemaining(&first, TRUE);
3615                                   }
3616                                   SendToProgram("playother\n", &first);
3617                                   firstMove = FALSE;
3618                                 } else {
3619                                   firstMove = TRUE;
3620                                 }
3621                             }
3622                         } else if (gameMode == IcsPlayingBlack) {
3623                             if (!WhiteOnMove(forwardMostMove)) {
3624                                 if (first.sendTime) {
3625                                   if (first.useColors) {
3626                                     SendToProgram("white\n", &first);
3627                                   }
3628                                   SendTimeRemaining(&first, FALSE);
3629                                 }
3630                                 if (first.useColors) {
3631                                   SendToProgram("black\n", &first);
3632                                 }
3633                                 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE);
3634                                 first.maybeThinking = TRUE;
3635                             } else {
3636                                 if (first.usePlayother) {
3637                                   if (first.sendTime) {
3638                                     SendTimeRemaining(&first, FALSE);
3639                                   }
3640                                   SendToProgram("playother\n", &first);
3641                                   firstMove = FALSE;
3642                                 } else {
3643                                   firstMove = TRUE;
3644                                 }
3645                             }
3646                         }
3647                     }
3648 #endif
3649                     if (gameMode == IcsObserving && ics_gamenum == -1) {
3650                         /* Moves came from oldmoves or moves command
3651                            while we weren't doing anything else.
3652                            */
3653                         currentMove = forwardMostMove;
3654                         ClearHighlights();/*!!could figure this out*/
3655                         flipView = appData.flipView;
3656                         DrawPosition(TRUE, boards[currentMove]);
3657                         DisplayBothClocks();
3658                         snprintf(str, MSG_SIZ, "%s %s %s",
3659                                 gameInfo.white, _("vs."),  gameInfo.black);
3660                         DisplayTitle(str);
3661                         gameMode = IcsIdle;
3662                     } else {
3663                         /* Moves were history of an active game */
3664                         if (gameInfo.resultDetails != NULL) {
3665                             free(gameInfo.resultDetails);
3666                             gameInfo.resultDetails = NULL;
3667                         }
3668                     }
3669                     HistorySet(parseList, backwardMostMove,
3670                                forwardMostMove, currentMove-1);
3671                     DisplayMove(currentMove - 1);
3672                     if (started == STARTED_MOVES) next_out = i;
3673                     started = STARTED_NONE;
3674                     ics_getting_history = H_FALSE;
3675                     break;
3676
3677                   case STARTED_OBSERVE:
3678                     started = STARTED_NONE;
3679                     SendToICS(ics_prefix);
3680                     SendToICS("refresh\n");
3681                     break;
3682
3683                   default:
3684                     break;
3685                 }
3686                 if(bookHit) { // [HGM] book: simulate book reply
3687                     static char bookMove[MSG_SIZ]; // a bit generous?
3688
3689                     programStats.nodes = programStats.depth = programStats.time =
3690                     programStats.score = programStats.got_only_move = 0;
3691                     sprintf(programStats.movelist, "%s (xbook)", bookHit);
3692
3693                     safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
3694                     strcat(bookMove, bookHit);
3695                     HandleMachineMove(bookMove, &first);
3696                 }
3697                 continue;
3698             }
3699
3700             if ((started == STARTED_MOVES || started == STARTED_BOARD ||
3701                  started == STARTED_HOLDINGS ||
3702                  started == STARTED_MOVES_NOHIDE) && i >= leftover_len) {
3703                 /* Accumulate characters in move list or board */
3704                 parse[parse_pos++] = buf[i];
3705             }
3706
3707             /* Start of game messages.  Mostly we detect start of game
3708                when the first board image arrives.  On some versions
3709                of the ICS, though, we need to do a "refresh" after starting
3710                to observe in order to get the current board right away. */
3711             if (looking_at(buf, &i, "Adding game * to observation list")) {
3712                 started = STARTED_OBSERVE;
3713                 continue;
3714             }
3715
3716             /* Handle auto-observe */
3717             if (appData.autoObserve &&
3718                 (gameMode == IcsIdle || gameMode == BeginningOfGame) &&
3719                 looking_at(buf, &i, "Game notification: * (*) vs. * (*)")) {
3720                 char *player;
3721                 /* Choose the player that was highlighted, if any. */
3722                 if (star_match[0][0] == '\033' ||
3723                     star_match[1][0] != '\033') {
3724                     player = star_match[0];
3725                 } else {
3726                     player = star_match[2];
3727                 }
3728                 snprintf(str, MSG_SIZ, "%sobserve %s\n",
3729                         ics_prefix, StripHighlightAndTitle(player));
3730                 SendToICS(str);
3731
3732                 /* Save ratings from notify string */
3733                 safeStrCpy(player1Name, star_match[0], sizeof(player1Name)/sizeof(player1Name[0]));
3734                 player1Rating = string_to_rating(star_match[1]);
3735                 safeStrCpy(player2Name, star_match[2], sizeof(player2Name)/sizeof(player2Name[0]));
3736                 player2Rating = string_to_rating(star_match[3]);
3737
3738                 if (appData.debugMode)
3739                   fprintf(debugFP,
3740                           "Ratings from 'Game notification:' %s %d, %s %d\n",
3741                           player1Name, player1Rating,
3742                           player2Name, player2Rating);
3743
3744                 continue;
3745             }
3746
3747             /* Deal with automatic examine mode after a game,
3748                and with IcsObserving -> IcsExamining transition */
3749             if (looking_at(buf, &i, "Entering examine mode for game *") ||
3750                 looking_at(buf, &i, "has made you an examiner of game *")) {
3751
3752                 int gamenum = atoi(star_match[0]);
3753                 if ((gameMode == IcsIdle || gameMode == IcsObserving) &&
3754                     gamenum == ics_gamenum) {
3755                     /* We were already playing or observing this game;
3756                        no need to refetch history */
3757                     gameMode = IcsExamining;
3758                     if (pausing) {
3759                         pauseExamForwardMostMove = forwardMostMove;
3760                     } else if (currentMove < forwardMostMove) {
3761                         ForwardInner(forwardMostMove);
3762                     }
3763                 } else {
3764                     /* I don't think this case really can happen */
3765                     SendToICS(ics_prefix);
3766                     SendToICS("refresh\n");
3767                 }
3768                 continue;
3769             }
3770
3771             /* Error messages */
3772 //          if (ics_user_moved) {
3773             if (1) { // [HGM] old way ignored error after move type in; ics_user_moved is not set then!
3774                 if (looking_at(buf, &i, "Illegal move") ||
3775                     looking_at(buf, &i, "Not a legal move") ||
3776                     looking_at(buf, &i, "Your king is in check") ||
3777                     looking_at(buf, &i, "It isn't your turn") ||
3778                     looking_at(buf, &i, "It is not your move")) {
3779                     /* Illegal move */
3780                     if (ics_user_moved && forwardMostMove > backwardMostMove) { // only backup if we already moved
3781                         currentMove = forwardMostMove-1;
3782                         DisplayMove(currentMove - 1); /* before DMError */
3783                         DrawPosition(FALSE, boards[currentMove]);
3784                         SwitchClocks(forwardMostMove-1); // [HGM] race
3785                         DisplayBothClocks();
3786                     }
3787                     DisplayMoveError(_("Illegal move (rejected by ICS)")); // [HGM] but always relay error msg
3788                     ics_user_moved = 0;
3789                     continue;
3790                 }
3791             }
3792
3793             if (looking_at(buf, &i, "still have time") ||
3794                 looking_at(buf, &i, "not out of time") ||
3795                 looking_at(buf, &i, "either player is out of time") ||
3796                 looking_at(buf, &i, "has timeseal; checking")) {
3797                 /* We must have called his flag a little too soon */
3798                 whiteFlag = blackFlag = FALSE;
3799                 continue;
3800             }
3801
3802             if (looking_at(buf, &i, "added * seconds to") ||
3803                 looking_at(buf, &i, "seconds were added to")) {
3804                 /* Update the clocks */
3805                 SendToICS(ics_prefix);
3806                 SendToICS("refresh\n");
3807                 continue;
3808             }
3809
3810             if (!ics_clock_paused && looking_at(buf, &i, "clock paused")) {
3811                 ics_clock_paused = TRUE;
3812                 StopClocks();
3813                 continue;
3814             }
3815
3816             if (ics_clock_paused && looking_at(buf, &i, "clock resumed")) {
3817                 ics_clock_paused = FALSE;
3818                 StartClocks();
3819                 continue;
3820             }
3821
3822             /* Grab player ratings from the Creating: message.
3823                Note we have to check for the special case when
3824                the ICS inserts things like [white] or [black]. */
3825             if (looking_at(buf, &i, "Creating: * (*)* * (*)") ||
3826                 looking_at(buf, &i, "Creating: * (*) [*] * (*)")) {
3827                 /* star_matches:
3828                    0    player 1 name (not necessarily white)
3829                    1    player 1 rating
3830                    2    empty, white, or black (IGNORED)
3831                    3    player 2 name (not necessarily black)
3832                    4    player 2 rating
3833
3834                    The names/ratings are sorted out when the game
3835                    actually starts (below).
3836                 */
3837                 safeStrCpy(player1Name, StripHighlightAndTitle(star_match[0]), sizeof(player1Name)/sizeof(player1Name[0]));
3838                 player1Rating = string_to_rating(star_match[1]);
3839                 safeStrCpy(player2Name, StripHighlightAndTitle(star_match[3]), sizeof(player2Name)/sizeof(player2Name[0]));
3840                 player2Rating = string_to_rating(star_match[4]);
3841
3842                 if (appData.debugMode)
3843                   fprintf(debugFP,
3844                           "Ratings from 'Creating:' %s %d, %s %d\n",
3845                           player1Name, player1Rating,
3846                           player2Name, player2Rating);
3847
3848                 continue;
3849             }
3850
3851             /* Improved generic start/end-of-game messages */
3852             if ((tkind=0, looking_at(buf, &i, "{Game * (* vs. *) *}*")) ||
3853                 (tkind=1, looking_at(buf, &i, "{Game * (*(*) vs. *(*)) *}*"))){
3854                 /* If tkind == 0: */
3855                 /* star_match[0] is the game number */
3856                 /*           [1] is the white player's name */
3857                 /*           [2] is the black player's name */
3858                 /* For end-of-game: */
3859                 /*           [3] is the reason for the game end */
3860                 /*           [4] is a PGN end game-token, preceded by " " */
3861                 /* For start-of-game: */
3862                 /*           [3] begins with "Creating" or "Continuing" */
3863                 /*           [4] is " *" or empty (don't care). */
3864                 int gamenum = atoi(star_match[0]);
3865                 char *whitename, *blackname, *why, *endtoken;
3866                 ChessMove endtype = EndOfFile;
3867
3868                 if (tkind == 0) {
3869                   whitename = star_match[1];
3870                   blackname = star_match[2];
3871                   why = star_match[3];
3872                   endtoken = star_match[4];
3873                 } else {
3874                   whitename = star_match[1];
3875                   blackname = star_match[3];
3876                   why = star_match[5];
3877                   endtoken = star_match[6];
3878                 }
3879
3880                 /* Game start messages */
3881                 if (strncmp(why, "Creating ", 9) == 0 ||
3882                     strncmp(why, "Continuing ", 11) == 0) {
3883                     gs_gamenum = gamenum;
3884                     safeStrCpy(gs_kind, strchr(why, ' ') + 1,sizeof(gs_kind)/sizeof(gs_kind[0]));
3885                     if(ics_gamenum == -1) // [HGM] only if we are not already involved in a game (because gin=1 sends us such messages)
3886                     VariantSwitch(boards[currentMove], StringToVariant(gs_kind)); // [HGM] variantswitch: even before we get first board
3887 #if ZIPPY
3888                     if (appData.zippyPlay) {
3889                         ZippyGameStart(whitename, blackname);
3890                     }
3891 #endif /*ZIPPY*/
3892                     partnerBoardValid = FALSE; // [HGM] bughouse
3893                     continue;
3894                 }
3895
3896                 /* Game end messages */
3897                 if (gameMode == IcsIdle || gameMode == BeginningOfGame ||
3898                     ics_gamenum != gamenum) {
3899                     continue;
3900                 }
3901                 while (endtoken[0] == ' ') endtoken++;
3902                 switch (endtoken[0]) {
3903                   case '*':
3904                   default:
3905                     endtype = GameUnfinished;
3906                     break;
3907                   case '0':
3908                     endtype = BlackWins;
3909                     break;
3910                   case '1':
3911                     if (endtoken[1] == '/')
3912                       endtype = GameIsDrawn;
3913                     else
3914                       endtype = WhiteWins;
3915                     break;
3916                 }
3917                 GameEnds(endtype, why, GE_ICS);
3918 #if ZIPPY
3919                 if (appData.zippyPlay && first.initDone) {
3920                     ZippyGameEnd(endtype, why);
3921                     if (first.pr == NoProc) {
3922                       /* Start the next process early so that we'll
3923                          be ready for the next challenge */
3924                       StartChessProgram(&first);
3925                     }
3926                     /* Send "new" early, in case this command takes
3927                        a long time to finish, so that we'll be ready
3928                        for the next challenge. */
3929                     gameInfo.variant = VariantNormal; // [HGM] variantswitch: suppress sending of 'variant'
3930                     Reset(TRUE, TRUE);
3931                 }
3932 #endif /*ZIPPY*/
3933                 if(appData.bgObserve && partnerBoardValid) DrawPosition(TRUE, partnerBoard);
3934                 continue;
3935             }
3936
3937             if (looking_at(buf, &i, "Removing game * from observation") ||
3938                 looking_at(buf, &i, "no longer observing game *") ||
3939                 looking_at(buf, &i, "Game * (*) has no examiners")) {
3940                 if (gameMode == IcsObserving &&
3941                     atoi(star_match[0]) == ics_gamenum)
3942                   {
3943                       /* icsEngineAnalyze */
3944                       if (appData.icsEngineAnalyze) {
3945                             ExitAnalyzeMode();
3946                             ModeHighlight();
3947                       }
3948                       StopClocks();
3949                       gameMode = IcsIdle;
3950                       ics_gamenum = -1;
3951                       ics_user_moved = FALSE;
3952                   }
3953                 continue;
3954             }
3955
3956             if (looking_at(buf, &i, "no longer examining game *")) {
3957                 if (gameMode == IcsExamining &&
3958                     atoi(star_match[0]) == ics_gamenum)
3959                   {
3960                       gameMode = IcsIdle;
3961                       ics_gamenum = -1;
3962                       ics_user_moved = FALSE;
3963                   }
3964                 continue;
3965             }
3966
3967             /* Advance leftover_start past any newlines we find,
3968                so only partial lines can get reparsed */
3969             if (looking_at(buf, &i, "\n")) {
3970                 prevColor = curColor;
3971                 if (curColor != ColorNormal) {
3972                     if (oldi > next_out) {
3973                         SendToPlayer(&buf[next_out], oldi - next_out);
3974                         next_out = oldi;
3975                     }
3976                     Colorize(ColorNormal, FALSE);
3977                     curColor = ColorNormal;
3978                 }
3979                 if (started == STARTED_BOARD) {
3980                     started = STARTED_NONE;
3981                     parse[parse_pos] = NULLCHAR;
3982                     ParseBoard12(parse);
3983                     ics_user_moved = 0;
3984
3985                     /* Send premove here */
3986                     if (appData.premove) {
3987                       char str[MSG_SIZ];
3988                       if (currentMove == 0 &&
3989                           gameMode == IcsPlayingWhite &&
3990                           appData.premoveWhite) {
3991                         snprintf(str, MSG_SIZ, "%s\n", appData.premoveWhiteText);
3992                         if (appData.debugMode)
3993                           fprintf(debugFP, "Sending premove:\n");
3994                         SendToICS(str);
3995                       } else if (currentMove == 1 &&
3996                                  gameMode == IcsPlayingBlack &&
3997                                  appData.premoveBlack) {
3998                         snprintf(str, MSG_SIZ, "%s\n", appData.premoveBlackText);
3999                         if (appData.debugMode)
4000                           fprintf(debugFP, "Sending premove:\n");
4001                         SendToICS(str);
4002                       } else if (gotPremove) {
4003                         gotPremove = 0;
4004                         ClearPremoveHighlights();
4005                         if (appData.debugMode)
4006                           fprintf(debugFP, "Sending premove:\n");
4007                           UserMoveEvent(premoveFromX, premoveFromY,
4008                                         premoveToX, premoveToY,
4009                                         premovePromoChar);
4010                       }
4011                     }
4012
4013                     /* Usually suppress following prompt */
4014                     if (!(forwardMostMove == 0 && gameMode == IcsExamining)) {
4015                         while(looking_at(buf, &i, "\n")); // [HGM] skip empty lines
4016                         if (looking_at(buf, &i, "*% ")) {
4017                             savingComment = FALSE;
4018                             suppressKibitz = 0;
4019                         }
4020                     }
4021                     next_out = i;
4022                 } else if (started == STARTED_HOLDINGS) {
4023                     int gamenum;
4024                     char new_piece[MSG_SIZ];
4025                     started = STARTED_NONE;
4026                     parse[parse_pos] = NULLCHAR;
4027                     if (appData.debugMode)
4028                       fprintf(debugFP, "Parsing holdings: %s, currentMove = %d\n",
4029                                                         parse, currentMove);
4030                     if (sscanf(parse, " game %d", &gamenum) == 1) {
4031                       if(gamenum == ics_gamenum) { // [HGM] bughouse: old code if part of foreground game
4032                         if (gameInfo.variant == VariantNormal) {
4033                           /* [HGM] We seem to switch variant during a game!
4034                            * Presumably no holdings were displayed, so we have
4035                            * to move the position two files to the right to
4036                            * create room for them!
4037                            */
4038                           VariantClass newVariant;
4039                           switch(gameInfo.boardWidth) { // base guess on board width
4040                                 case 9:  newVariant = VariantShogi; break;
4041                                 case 10: newVariant = VariantGreat; break;
4042                                 default: newVariant = VariantCrazyhouse; break;
4043                           }
4044                           VariantSwitch(boards[currentMove], newVariant); /* temp guess */
4045                           /* Get a move list just to see the header, which
4046                              will tell us whether this is really bug or zh */
4047                           if (ics_getting_history == H_FALSE) {
4048                             ics_getting_history = H_REQUESTED;
4049                             snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4050                             SendToICS(str);
4051                           }
4052                         }
4053                         new_piece[0] = NULLCHAR;
4054                         sscanf(parse, "game %d white [%s black [%s <- %s",
4055                                &gamenum, white_holding, black_holding,
4056                                new_piece);
4057                         white_holding[strlen(white_holding)-1] = NULLCHAR;
4058                         black_holding[strlen(black_holding)-1] = NULLCHAR;
4059                         /* [HGM] copy holdings to board holdings area */
4060                         CopyHoldings(boards[forwardMostMove], white_holding, WhitePawn);
4061                         CopyHoldings(boards[forwardMostMove], black_holding, BlackPawn);
4062                         boards[forwardMostMove][HOLDINGS_SET] = 1; // flag holdings as set
4063 #if ZIPPY
4064                         if (appData.zippyPlay && first.initDone) {
4065                             ZippyHoldings(white_holding, black_holding,
4066                                           new_piece);
4067                         }
4068 #endif /*ZIPPY*/
4069                         if (tinyLayout || smallLayout) {
4070                             char wh[16], bh[16];
4071                             PackHolding(wh, white_holding);
4072                             PackHolding(bh, black_holding);
4073                             snprintf(str, MSG_SIZ, "[%s-%s] %s-%s", wh, bh,
4074                                     gameInfo.white, gameInfo.black);
4075                         } else {
4076                           snprintf(str, MSG_SIZ, "%s [%s] %s %s [%s]",
4077                                     gameInfo.white, white_holding, _("vs."),
4078                                     gameInfo.black, black_holding);
4079                         }
4080                         if(!partnerUp) // [HGM] bughouse: when peeking at partner game we already know what he captured...
4081                         DrawPosition(FALSE, boards[currentMove]);
4082                         DisplayTitle(str);
4083                       } else if(appData.bgObserve) { // [HGM] bughouse: holdings of other game => background
4084                         sscanf(parse, "game %d white [%s black [%s <- %s",
4085                                &gamenum, white_holding, black_holding,
4086                                new_piece);
4087                         white_holding[strlen(white_holding)-1] = NULLCHAR;
4088                         black_holding[strlen(black_holding)-1] = NULLCHAR;
4089                         /* [HGM] copy holdings to partner-board holdings area */
4090                         CopyHoldings(partnerBoard, white_holding, WhitePawn);
4091                         CopyHoldings(partnerBoard, black_holding, BlackPawn);
4092                         if(twoBoards) { partnerUp = 1; flipView = !flipView; } // [HGM] dual: always draw
4093                         if(partnerUp) DrawPosition(FALSE, partnerBoard);
4094                         if(twoBoards) { partnerUp = 0; flipView = !flipView; }
4095                       }
4096                     }
4097                     /* Suppress following prompt */
4098                     if (looking_at(buf, &i, "*% ")) {
4099                         if(strchr(star_match[0], 7)) SendToPlayer("\007", 1); // Bell(); // FICS fuses bell for next board with prompt in zh captures
4100                         savingComment = FALSE;
4101                         suppressKibitz = 0;
4102                     }
4103                     next_out = i;
4104                 }
4105                 continue;
4106             }
4107
4108             i++;                /* skip unparsed character and loop back */
4109         }
4110
4111         if (started != STARTED_MOVES && started != STARTED_BOARD && !suppressKibitz && // [HGM] kibitz
4112 //          started != STARTED_HOLDINGS && i > next_out) { // [HGM] should we compare to leftover_start in stead of i?
4113 //          SendToPlayer(&buf[next_out], i - next_out);
4114             started != STARTED_HOLDINGS && leftover_start > next_out) {
4115             SendToPlayer(&buf[next_out], leftover_start - next_out);
4116             next_out = i;
4117         }
4118
4119         leftover_len = buf_len - leftover_start;
4120         /* if buffer ends with something we couldn't parse,
4121            reparse it after appending the next read */
4122
4123     } else if (count == 0) {
4124         RemoveInputSource(isr);
4125         DisplayFatalError(_("Connection closed by ICS"), 0, 0);
4126     } else {
4127         DisplayFatalError(_("Error reading from ICS"), error, 1);
4128     }
4129 }
4130
4131
4132 /* Board style 12 looks like this:
4133
4134    <12> r-b---k- pp----pp ---bP--- ---p---- q------- ------P- P--Q--BP -----R-K W -1 0 0 0 0 0 0 paf MaxII 0 2 12 21 25 234 174 24 Q/d7-a4 (0:06) Qxa4 0 0
4135
4136  * The "<12> " is stripped before it gets to this routine.  The two
4137  * trailing 0's (flip state and clock ticking) are later addition, and
4138  * some chess servers may not have them, or may have only the first.
4139  * Additional trailing fields may be added in the future.
4140  */
4141
4142 #define PATTERN "%c%d%d%d%d%d%d%d%s%s%d%d%d%d%d%d%d%d%s%s%s%d%d"
4143
4144 #define RELATION_OBSERVING_PLAYED    0
4145 #define RELATION_OBSERVING_STATIC   -2   /* examined, oldmoves, or smoves */
4146 #define RELATION_PLAYING_MYMOVE      1
4147 #define RELATION_PLAYING_NOTMYMOVE  -1
4148 #define RELATION_EXAMINING           2
4149 #define RELATION_ISOLATED_BOARD     -3
4150 #define RELATION_STARTING_POSITION  -4   /* FICS only */
4151
4152 void
4153 ParseBoard12 (char *string)
4154 {
4155     GameMode newGameMode;
4156     int gamenum, newGame, newMove, relation, basetime, increment, ics_flip = 0, i;
4157     int j, k, n, moveNum, white_stren, black_stren, white_time, black_time, takeback;
4158     int double_push, castle_ws, castle_wl, castle_bs, castle_bl, irrev_count;
4159     char to_play, board_chars[200];
4160     char move_str[MSG_SIZ], str[MSG_SIZ], elapsed_time[MSG_SIZ];
4161     char black[32], white[32];
4162     Board board;
4163     int prevMove = currentMove;
4164     int ticking = 2;
4165     ChessMove moveType;
4166     int fromX, fromY, toX, toY;
4167     char promoChar;
4168     int ranks=1, files=0; /* [HGM] ICS80: allow variable board size */
4169     char *bookHit = NULL; // [HGM] book
4170     Boolean weird = FALSE, reqFlag = FALSE;
4171
4172     fromX = fromY = toX = toY = -1;
4173
4174     newGame = FALSE;
4175
4176     if (appData.debugMode)
4177       fprintf(debugFP, _("Parsing board: %s\n"), string);
4178
4179     move_str[0] = NULLCHAR;
4180     elapsed_time[0] = NULLCHAR;
4181     {   /* [HGM] figure out how many ranks and files the board has, for ICS extension used by Capablanca server */
4182         int  i = 0, j;
4183         while(i < 199 && (string[i] != ' ' || string[i+2] != ' ')) {
4184             if(string[i] == ' ') { ranks++; files = 0; }
4185             else files++;
4186             if(!strchr(" -pnbrqkPNBRQK" , string[i])) weird = TRUE; // test for fairies
4187             i++;
4188         }
4189         for(j = 0; j <i; j++) board_chars[j] = string[j];
4190         board_chars[i] = '\0';
4191         string += i + 1;
4192     }
4193     n = sscanf(string, PATTERN, &to_play, &double_push,
4194                &castle_ws, &castle_wl, &castle_bs, &castle_bl, &irrev_count,
4195                &gamenum, white, black, &relation, &basetime, &increment,
4196                &white_stren, &black_stren, &white_time, &black_time,
4197                &moveNum, str, elapsed_time, move_str, &ics_flip,
4198                &ticking);
4199
4200     if (n < 21) {
4201         snprintf(str, MSG_SIZ, _("Failed to parse board string:\n\"%s\""), string);
4202         DisplayError(str, 0);
4203         return;
4204     }
4205
4206     /* Convert the move number to internal form */
4207     moveNum = (moveNum - 1) * 2;
4208     if (to_play == 'B') moveNum++;
4209     if (moveNum > framePtr) { // [HGM] vari: do not run into saved variations
4210       DisplayFatalError(_("Game too long; increase MAX_MOVES and recompile"),
4211                         0, 1);
4212       return;
4213     }
4214
4215     switch (relation) {
4216       case RELATION_OBSERVING_PLAYED:
4217       case RELATION_OBSERVING_STATIC:
4218         if (gamenum == -1) {
4219             /* Old ICC buglet */
4220             relation = RELATION_OBSERVING_STATIC;
4221         }
4222         newGameMode = IcsObserving;
4223         break;
4224       case RELATION_PLAYING_MYMOVE:
4225       case RELATION_PLAYING_NOTMYMOVE:
4226         newGameMode =
4227           ((relation == RELATION_PLAYING_MYMOVE) == (to_play == 'W')) ?
4228             IcsPlayingWhite : IcsPlayingBlack;
4229         soughtPending =FALSE; // [HGM] seekgraph: solve race condition
4230         break;
4231       case RELATION_EXAMINING:
4232         newGameMode = IcsExamining;
4233         break;
4234       case RELATION_ISOLATED_BOARD:
4235       default:
4236         /* Just display this board.  If user was doing something else,
4237            we will forget about it until the next board comes. */
4238         newGameMode = IcsIdle;
4239         break;
4240       case RELATION_STARTING_POSITION:
4241         newGameMode = gameMode;
4242         break;
4243     }
4244
4245     if((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
4246         gameMode == IcsObserving && appData.dualBoard) // also allow use of second board for observing two games
4247          && newGameMode == IcsObserving && gamenum != ics_gamenum && appData.bgObserve) {
4248       // [HGM] bughouse: don't act on alien boards while we play. Just parse the board and save it */
4249       int fac = strchr(elapsed_time, '.') ? 1 : 1000;
4250       static int lastBgGame = -1;
4251       char *toSqr;
4252       for (k = 0; k < ranks; k++) {
4253         for (j = 0; j < files; j++)
4254           board[k][j+gameInfo.holdingsWidth] = CharToPiece(board_chars[(ranks-1-k)*(files+1) + j]);
4255         if(gameInfo.holdingsWidth > 1) {
4256              board[k][0] = board[k][BOARD_WIDTH-1] = EmptySquare;
4257              board[k][1] = board[k][BOARD_WIDTH-2] = (ChessSquare) 0;;
4258         }
4259       }
4260       CopyBoard(partnerBoard, board);
4261       if(toSqr = strchr(str, '/')) { // extract highlights from long move
4262         partnerBoard[EP_STATUS-3] = toSqr[1] - AAA; // kludge: hide highlighting info in board
4263         partnerBoard[EP_STATUS-4] = toSqr[2] - ONE;
4264       } else partnerBoard[EP_STATUS-4] = partnerBoard[EP_STATUS-3] = -1;
4265       if(toSqr = strchr(str, '-')) {
4266         partnerBoard[EP_STATUS-1] = toSqr[1] - AAA;
4267         partnerBoard[EP_STATUS-2] = toSqr[2] - ONE;
4268       } else partnerBoard[EP_STATUS-1] = partnerBoard[EP_STATUS-2] = -1;
4269       if(appData.dualBoard && !twoBoards) { twoBoards = 1; InitDrawingSizes(-2,0); }
4270       if(twoBoards) { partnerUp = 1; flipView = !flipView; } // [HGM] dual
4271       if(partnerUp) DrawPosition(FALSE, partnerBoard);
4272       if(twoBoards) {
4273           DisplayWhiteClock(white_time*fac, to_play == 'W');
4274           DisplayBlackClock(black_time*fac, to_play != 'W');
4275           activePartner = to_play;
4276           if(gamenum != lastBgGame) {
4277               char buf[MSG_SIZ];
4278               snprintf(buf, MSG_SIZ, "%s %s %s", white, _("vs."), black);
4279               DisplayTitle(buf);
4280           }
4281           lastBgGame = gamenum;
4282           activePartnerTime = to_play == 'W' ? white_time*fac : black_time*fac;
4283                       partnerUp = 0; flipView = !flipView; } // [HGM] dual
4284       snprintf(partnerStatus, MSG_SIZ,"W: %d:%02d B: %d:%02d (%d-%d) %c", white_time*fac/60000, (white_time*fac%60000)/1000,
4285                  (black_time*fac/60000), (black_time*fac%60000)/1000, white_stren, black_stren, to_play);
4286       DisplayMessage(partnerStatus, "");
4287         partnerBoardValid = TRUE;
4288       return;
4289     }
4290
4291     if(appData.dualBoard && appData.bgObserve) {
4292         if((newGameMode == IcsPlayingWhite || newGameMode == IcsPlayingBlack) && moveNum == 1)
4293             SendToICS(ics_prefix), SendToICS("pobserve\n");
4294         else if(newGameMode == IcsObserving && (gameMode == BeginningOfGame || gameMode == IcsIdle)) {
4295             char buf[MSG_SIZ];
4296             snprintf(buf, MSG_SIZ, "%spobserve %s\n", ics_prefix, white);
4297             SendToICS(buf);
4298         }
4299     }
4300
4301     /* Modify behavior for initial board display on move listing
4302        of wild games.
4303        */
4304     switch (ics_getting_history) {
4305       case H_FALSE:
4306       case H_REQUESTED:
4307         break;
4308       case H_GOT_REQ_HEADER:
4309       case H_GOT_UNREQ_HEADER:
4310         /* This is the initial position of the current game */
4311         gamenum = ics_gamenum;
4312         moveNum = 0;            /* old ICS bug workaround */
4313         if (to_play == 'B') {
4314           startedFromSetupPosition = TRUE;
4315           blackPlaysFirst = TRUE;
4316           moveNum = 1;
4317           if (forwardMostMove == 0) forwardMostMove = 1;
4318           if (backwardMostMove == 0) backwardMostMove = 1;
4319           if (currentMove == 0) currentMove = 1;
4320         }
4321         newGameMode = gameMode;
4322         relation = RELATION_STARTING_POSITION; /* ICC needs this */
4323         break;
4324       case H_GOT_UNWANTED_HEADER:
4325         /* This is an initial board that we don't want */
4326         return;
4327       case H_GETTING_MOVES:
4328         /* Should not happen */
4329         DisplayError(_("Error gathering move list: extra board"), 0);
4330         ics_getting_history = H_FALSE;
4331         return;
4332     }
4333
4334    if (gameInfo.boardHeight != ranks || gameInfo.boardWidth != files ||
4335                                         move_str[1] == '@' && !gameInfo.holdingsWidth ||
4336                                         weird && (int)gameInfo.variant < (int)VariantShogi) {
4337      /* [HGM] We seem to have switched variant unexpectedly
4338       * Try to guess new variant from board size
4339       */
4340           VariantClass newVariant = VariantFairy; // if 8x8, but fairies present
4341           if(ranks == 8 && files == 10) newVariant = VariantCapablanca; else
4342           if(ranks == 10 && files == 9) newVariant = VariantXiangqi; else
4343           if(ranks == 8 && files == 12) newVariant = VariantCourier; else
4344           if(ranks == 9 && files == 9)  newVariant = VariantShogi; else
4345           if(ranks == 10 && files == 10) newVariant = VariantGrand; else
4346           if(!weird) newVariant = move_str[1] == '@' ? VariantCrazyhouse : VariantNormal;
4347           VariantSwitch(boards[currentMove], newVariant); /* temp guess */
4348           /* Get a move list just to see the header, which
4349              will tell us whether this is really bug or zh */
4350           if (ics_getting_history == H_FALSE) {
4351             ics_getting_history = H_REQUESTED; reqFlag = TRUE;
4352             snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4353             SendToICS(str);
4354           }
4355     }
4356
4357     /* Take action if this is the first board of a new game, or of a
4358        different game than is currently being displayed.  */
4359     if (gamenum != ics_gamenum || newGameMode != gameMode ||
4360         relation == RELATION_ISOLATED_BOARD) {
4361
4362         /* Forget the old game and get the history (if any) of the new one */
4363         if (gameMode != BeginningOfGame) {
4364           Reset(TRUE, TRUE);
4365         }
4366         newGame = TRUE;
4367         if (appData.autoRaiseBoard) BoardToTop();
4368         prevMove = -3;
4369         if (gamenum == -1) {
4370             newGameMode = IcsIdle;
4371         } else if ((moveNum > 0 || newGameMode == IcsObserving) && newGameMode != IcsIdle &&
4372                    appData.getMoveList && !reqFlag) {
4373             /* Need to get game history */
4374             ics_getting_history = H_REQUESTED;
4375             snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4376             SendToICS(str);
4377         }
4378
4379         /* Initially flip the board to have black on the bottom if playing
4380            black or if the ICS flip flag is set, but let the user change
4381            it with the Flip View button. */
4382         flipView = appData.autoFlipView ?
4383           (newGameMode == IcsPlayingBlack) || ics_flip :
4384           appData.flipView;
4385
4386         /* Done with values from previous mode; copy in new ones */
4387         gameMode = newGameMode;
4388         ModeHighlight();
4389         ics_gamenum = gamenum;
4390         if (gamenum == gs_gamenum) {
4391             int klen = strlen(gs_kind);
4392             if (gs_kind[klen - 1] == '.') gs_kind[klen - 1] = NULLCHAR;
4393             snprintf(str, MSG_SIZ, "ICS %s", gs_kind);
4394             gameInfo.event = StrSave(str);
4395         } else {
4396             gameInfo.event = StrSave("ICS game");
4397         }
4398         gameInfo.site = StrSave(appData.icsHost);
4399         gameInfo.date = PGNDate();
4400         gameInfo.round = StrSave("-");
4401         gameInfo.white = StrSave(white);
4402         gameInfo.black = StrSave(black);
4403         timeControl = basetime * 60 * 1000;
4404         timeControl_2 = 0;
4405         timeIncrement = increment * 1000;
4406         movesPerSession = 0;
4407         gameInfo.timeControl = TimeControlTagValue();
4408         VariantSwitch(boards[currentMove], StringToVariant(gameInfo.event) );
4409   if (appData.debugMode) {
4410     fprintf(debugFP, "ParseBoard says variant = '%s'\n", gameInfo.event);
4411     fprintf(debugFP, "recognized as %s\n", VariantName(gameInfo.variant));
4412     setbuf(debugFP, NULL);
4413   }
4414
4415         gameInfo.outOfBook = NULL;
4416
4417         /* Do we have the ratings? */
4418         if (strcmp(player1Name, white) == 0 &&
4419             strcmp(player2Name, black) == 0) {
4420             if (appData.debugMode)
4421               fprintf(debugFP, "Remembered ratings: W %d, B %d\n",
4422                       player1Rating, player2Rating);
4423             gameInfo.whiteRating = player1Rating;
4424             gameInfo.blackRating = player2Rating;
4425         } else if (strcmp(player2Name, white) == 0 &&
4426                    strcmp(player1Name, black) == 0) {
4427             if (appData.debugMode)
4428               fprintf(debugFP, "Remembered ratings: W %d, B %d\n",
4429                       player2Rating, player1Rating);
4430             gameInfo.whiteRating = player2Rating;
4431             gameInfo.blackRating = player1Rating;
4432         }
4433         player1Name[0] = player2Name[0] = NULLCHAR;
4434
4435         /* Silence shouts if requested */
4436         if (appData.quietPlay &&
4437             (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)) {
4438             SendToICS(ics_prefix);
4439             SendToICS("set shout 0\n");
4440         }
4441     }
4442
4443     /* Deal with midgame name changes */
4444     if (!newGame) {
4445         if (!gameInfo.white || strcmp(gameInfo.white, white) != 0) {
4446             if (gameInfo.white) free(gameInfo.white);
4447             gameInfo.white = StrSave(white);
4448         }
4449         if (!gameInfo.black || strcmp(gameInfo.black, black) != 0) {
4450             if (gameInfo.black) free(gameInfo.black);
4451             gameInfo.black = StrSave(black);
4452         }
4453     }
4454
4455     /* Throw away game result if anything actually changes in examine mode */
4456     if (gameMode == IcsExamining && !newGame) {
4457         gameInfo.result = GameUnfinished;
4458         if (gameInfo.resultDetails != NULL) {
4459             free(gameInfo.resultDetails);
4460             gameInfo.resultDetails = NULL;
4461         }
4462     }
4463
4464     /* In pausing && IcsExamining mode, we ignore boards coming
4465        in if they are in a different variation than we are. */
4466     if (pauseExamInvalid) return;
4467     if (pausing && gameMode == IcsExamining) {
4468         if (moveNum <= pauseExamForwardMostMove) {
4469             pauseExamInvalid = TRUE;
4470             forwardMostMove = pauseExamForwardMostMove;
4471             return;
4472         }
4473     }
4474
4475   if (appData.debugMode) {
4476     fprintf(debugFP, "load %dx%d board\n", files, ranks);
4477   }
4478     /* Parse the board */
4479     for (k = 0; k < ranks; k++) {
4480       for (j = 0; j < files; j++)
4481         board[k][j+gameInfo.holdingsWidth] = CharToPiece(board_chars[(ranks-1-k)*(files+1) + j]);
4482       if(gameInfo.holdingsWidth > 1) {
4483            board[k][0] = board[k][BOARD_WIDTH-1] = EmptySquare;
4484            board[k][1] = board[k][BOARD_WIDTH-2] = (ChessSquare) 0;;
4485       }
4486     }
4487     if(moveNum==0 && gameInfo.variant == VariantSChess) {
4488       board[5][BOARD_RGHT+1] = WhiteAngel;
4489       board[6][BOARD_RGHT+1] = WhiteMarshall;
4490       board[1][0] = BlackMarshall;
4491       board[2][0] = BlackAngel;
4492       board[1][1] = board[2][1] = board[5][BOARD_RGHT] = board[6][BOARD_RGHT] = 1;
4493     }
4494     CopyBoard(boards[moveNum], board);
4495     boards[moveNum][HOLDINGS_SET] = 0; // [HGM] indicate holdings not set
4496     if (moveNum == 0) {
4497         startedFromSetupPosition =
4498           !CompareBoards(board, initialPosition);
4499         if(startedFromSetupPosition)
4500             initialRulePlies = irrev_count; /* [HGM] 50-move counter offset */
4501     }
4502
4503     /* [HGM] Set castling rights. Take the outermost Rooks,
4504        to make it also work for FRC opening positions. Note that board12
4505        is really defective for later FRC positions, as it has no way to
4506        indicate which Rook can castle if they are on the same side of King.
4507        For the initial position we grant rights to the outermost Rooks,
4508        and remember thos rights, and we then copy them on positions
4509        later in an FRC game. This means WB might not recognize castlings with
4510        Rooks that have moved back to their original position as illegal,
4511        but in ICS mode that is not its job anyway.
4512     */
4513     if(moveNum == 0 || gameInfo.variant != VariantFischeRandom)
4514     { int i, j; ChessSquare wKing = WhiteKing, bKing = BlackKing;
4515
4516         for(i=BOARD_LEFT, j=NoRights; i<BOARD_RGHT; i++)
4517             if(board[0][i] == WhiteRook) j = i;
4518         initialRights[0] = boards[moveNum][CASTLING][0] = (castle_ws == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4519         for(i=BOARD_RGHT-1, j=NoRights; i>=BOARD_LEFT; i--)
4520             if(board[0][i] == WhiteRook) j = i;
4521         initialRights[1] = boards[moveNum][CASTLING][1] = (castle_wl == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4522         for(i=BOARD_LEFT, j=NoRights; i<BOARD_RGHT; i++)
4523             if(board[BOARD_HEIGHT-1][i] == BlackRook) j = i;
4524         initialRights[3] = boards[moveNum][CASTLING][3] = (castle_bs == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4525         for(i=BOARD_RGHT-1, j=NoRights; i>=BOARD_LEFT; i--)
4526             if(board[BOARD_HEIGHT-1][i] == BlackRook) j = i;
4527         initialRights[4] = boards[moveNum][CASTLING][4] = (castle_bl == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4528
4529         boards[moveNum][CASTLING][2] = boards[moveNum][CASTLING][5] = NoRights;
4530         if(gameInfo.variant == VariantKnightmate) { wKing = WhiteUnicorn; bKing = BlackUnicorn; }
4531         for(k=BOARD_LEFT; k<BOARD_RGHT; k++)
4532             if(board[0][k] == wKing) initialRights[2] = boards[moveNum][CASTLING][2] = k;
4533         for(k=BOARD_LEFT; k<BOARD_RGHT; k++)
4534             if(board[BOARD_HEIGHT-1][k] == bKing)
4535                 initialRights[5] = boards[moveNum][CASTLING][5] = k;
4536         if(gameInfo.variant == VariantTwoKings) {
4537             // In TwoKings looking for a King does not work, so always give castling rights to a King on e1/e8
4538             if(board[0][4] == wKing) initialRights[2] = boards[moveNum][CASTLING][2] = 4;
4539             if(board[BOARD_HEIGHT-1][4] == bKing) initialRights[5] = boards[moveNum][CASTLING][5] = 4;
4540         }
4541     } else { int r;
4542         r = boards[moveNum][CASTLING][0] = initialRights[0];
4543         if(board[0][r] != WhiteRook) boards[moveNum][CASTLING][0] = NoRights;
4544         r = boards[moveNum][CASTLING][1] = initialRights[1];
4545         if(board[0][r] != WhiteRook) boards[moveNum][CASTLING][1] = NoRights;
4546         r = boards[moveNum][CASTLING][3] = initialRights[3];
4547         if(board[BOARD_HEIGHT-1][r] != BlackRook) boards[moveNum][CASTLING][3] = NoRights;
4548         r = boards[moveNum][CASTLING][4] = initialRights[4];
4549         if(board[BOARD_HEIGHT-1][r] != BlackRook) boards[moveNum][CASTLING][4] = NoRights;
4550         /* wildcastle kludge: always assume King has rights */
4551         r = boards[moveNum][CASTLING][2] = initialRights[2];
4552         r = boards[moveNum][CASTLING][5] = initialRights[5];
4553     }
4554     /* [HGM] e.p. rights. Assume that ICS sends file number here? */
4555     boards[moveNum][EP_STATUS] = EP_NONE;
4556     if(str[0] == 'P') boards[moveNum][EP_STATUS] = EP_PAWN_MOVE;
4557     if(strchr(move_str, 'x')) boards[moveNum][EP_STATUS] = EP_CAPTURE;
4558     if(double_push !=  -1) boards[moveNum][EP_STATUS] = double_push + BOARD_LEFT;
4559
4560
4561     if (ics_getting_history == H_GOT_REQ_HEADER ||
4562         ics_getting_history == H_GOT_UNREQ_HEADER) {
4563         /* This was an initial position from a move list, not
4564            the current position */
4565         return;
4566     }
4567
4568     /* Update currentMove and known move number limits */
4569     newMove = newGame || moveNum > forwardMostMove;
4570
4571     if (newGame) {
4572         forwardMostMove = backwardMostMove = currentMove = moveNum;
4573         if (gameMode == IcsExamining && moveNum == 0) {
4574           /* Workaround for ICS limitation: we are not told the wild
4575              type when starting to examine a game.  But if we ask for
4576              the move list, the move list header will tell us */
4577             ics_getting_history = H_REQUESTED;
4578             snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4579             SendToICS(str);
4580         }
4581     } else if (moveNum == forwardMostMove + 1 || moveNum == forwardMostMove
4582                || (moveNum < forwardMostMove && moveNum >= backwardMostMove)) {
4583 #if ZIPPY
4584         /* [DM] If we found takebacks during icsEngineAnalyze try send to engine */
4585         /* [HGM] applied this also to an engine that is silently watching        */
4586         if (appData.zippyPlay && moveNum < forwardMostMove && first.initDone &&
4587             (gameMode == IcsObserving || gameMode == IcsExamining) &&
4588             gameInfo.variant == currentlyInitializedVariant) {
4589           takeback = forwardMostMove - moveNum;
4590           for (i = 0; i < takeback; i++) {
4591             if (appData.debugMode) fprintf(debugFP, "take back move\n");
4592             SendToProgram("undo\n", &first);
4593           }
4594         }
4595 #endif
4596
4597         forwardMostMove = moveNum;
4598         if (!pausing || currentMove > forwardMostMove)
4599           currentMove = forwardMostMove;
4600     } else {
4601         /* New part of history that is not contiguous with old part */
4602         if (pausing && gameMode == IcsExamining) {
4603             pauseExamInvalid = TRUE;
4604             forwardMostMove = pauseExamForwardMostMove;
4605             return;
4606         }
4607         if (gameMode == IcsExamining && moveNum > 0 && appData.getMoveList) {
4608 #if ZIPPY
4609             if(appData.zippyPlay && forwardMostMove > 0 && first.initDone) {
4610                 // [HGM] when we will receive the move list we now request, it will be
4611                 // fed to the engine from the first move on. So if the engine is not
4612                 // in the initial position now, bring it there.
4613                 InitChessProgram(&first, 0);
4614             }
4615 #endif
4616             ics_getting_history = H_REQUESTED;
4617             snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4618             SendToICS(str);
4619         }
4620         forwardMostMove = backwardMostMove = currentMove = moveNum;
4621     }
4622
4623     /* Update the clocks */
4624     if (strchr(elapsed_time, '.')) {
4625       /* Time is in ms */
4626       timeRemaining[0][moveNum] = whiteTimeRemaining = white_time;
4627       timeRemaining[1][moveNum] = blackTimeRemaining = black_time;
4628     } else {
4629       /* Time is in seconds */
4630       timeRemaining[0][moveNum] = whiteTimeRemaining = white_time * 1000;
4631       timeRemaining[1][moveNum] = blackTimeRemaining = black_time * 1000;
4632     }
4633
4634
4635 #if ZIPPY
4636     if (appData.zippyPlay && newGame &&
4637         gameMode != IcsObserving && gameMode != IcsIdle &&
4638         gameMode != IcsExamining)
4639       ZippyFirstBoard(moveNum, basetime, increment);
4640 #endif
4641
4642     /* Put the move on the move list, first converting
4643        to canonical algebraic form. */
4644     if (moveNum > 0) {
4645   if (appData.debugMode) {
4646     if (appData.debugMode) { int f = forwardMostMove;
4647         fprintf(debugFP, "parseboard %d, castling = %d %d %d %d %d %d\n", f,
4648                 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
4649                 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
4650     }
4651     fprintf(debugFP, "accepted move %s from ICS, parse it.\n", move_str);
4652     fprintf(debugFP, "moveNum = %d\n", moveNum);
4653     fprintf(debugFP, "board = %d-%d x %d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT);
4654     setbuf(debugFP, NULL);
4655   }
4656         if (moveNum <= backwardMostMove) {
4657             /* We don't know what the board looked like before
4658                this move.  Punt. */
4659           safeStrCpy(parseList[moveNum - 1], move_str, sizeof(parseList[moveNum - 1])/sizeof(parseList[moveNum - 1][0]));
4660             strcat(parseList[moveNum - 1], " ");
4661             strcat(parseList[moveNum - 1], elapsed_time);
4662             moveList[moveNum - 1][0] = NULLCHAR;
4663         } else if (strcmp(move_str, "none") == 0) {
4664             // [HGM] long SAN: swapped order; test for 'none' before parsing move
4665             /* Again, we don't know what the board looked like;
4666                this is really the start of the game. */
4667             parseList[moveNum - 1][0] = NULLCHAR;
4668             moveList[moveNum - 1][0] = NULLCHAR;
4669             backwardMostMove = moveNum;
4670             startedFromSetupPosition = TRUE;
4671             fromX = fromY = toX = toY = -1;
4672         } else {
4673           // [HGM] long SAN: if legality-testing is off, disambiguation might not work or give wrong move.
4674           //                 So we parse the long-algebraic move string in stead of the SAN move
4675           int valid; char buf[MSG_SIZ], *prom;
4676
4677           if(gameInfo.variant == VariantShogi && !strchr(move_str, '=') && !strchr(move_str, '@'))
4678                 strcat(move_str, "="); // if ICS does not say 'promote' on non-drop, we defer.
4679           // str looks something like "Q/a1-a2"; kill the slash
4680           if(str[1] == '/')
4681             snprintf(buf, MSG_SIZ,"%c%s", str[0], str+2);
4682           else  safeStrCpy(buf, str, sizeof(buf)/sizeof(buf[0])); // might be castling
4683           if((prom = strstr(move_str, "=")) && !strstr(buf, "="))
4684                 strcat(buf, prom); // long move lacks promo specification!
4685           if(!appData.testLegality && move_str[1] != '@') { // drops never ambiguous (parser chokes on long form!)
4686                 if(appData.debugMode)
4687                         fprintf(debugFP, "replaced ICS move '%s' by '%s'\n", move_str, buf);
4688                 safeStrCpy(move_str, buf, MSG_SIZ);
4689           }
4690           valid = ParseOneMove(move_str, moveNum - 1, &moveType,
4691                                 &fromX, &fromY, &toX, &toY, &promoChar)
4692                || ParseOneMove(buf, moveNum - 1, &moveType,
4693                                 &fromX, &fromY, &toX, &toY, &promoChar);
4694           // end of long SAN patch
4695           if (valid) {
4696             (void) CoordsToAlgebraic(boards[moveNum - 1],
4697                                      PosFlags(moveNum - 1),
4698                                      fromY, fromX, toY, toX, promoChar,
4699                                      parseList[moveNum-1]);
4700             switch (MateTest(boards[moveNum], PosFlags(moveNum)) ) {
4701               case MT_NONE:
4702               case MT_STALEMATE:
4703               default:
4704                 break;
4705               case MT_CHECK:
4706                 if(gameInfo.variant != VariantShogi)
4707                     strcat(parseList[moveNum - 1], "+");
4708                 break;
4709               case MT_CHECKMATE:
4710               case MT_STAINMATE: // [HGM] xq: for notation stalemate that wins counts as checkmate
4711                 strcat(parseList[moveNum - 1], "#");
4712                 break;
4713             }
4714             strcat(parseList[moveNum - 1], " ");
4715             strcat(parseList[moveNum - 1], elapsed_time);
4716             /* currentMoveString is set as a side-effect of ParseOneMove */
4717             if(gameInfo.variant == VariantShogi && currentMoveString[4]) currentMoveString[4] = '^';
4718             safeStrCpy(moveList[moveNum - 1], currentMoveString, sizeof(moveList[moveNum - 1])/sizeof(moveList[moveNum - 1][0]));
4719             strcat(moveList[moveNum - 1], "\n");
4720
4721             if(gameInfo.holdingsWidth && !appData.disguise && gameInfo.variant != VariantSuper && gameInfo.variant != VariantGreat
4722                && gameInfo.variant != VariantGrand&& gameInfo.variant != VariantSChess) // inherit info that ICS does not give from previous board
4723               for(k=0; k<ranks; k++) for(j=BOARD_LEFT; j<BOARD_RGHT; j++) {
4724                 ChessSquare old, new = boards[moveNum][k][j];
4725                   if(fromY == DROP_RANK && k==toY && j==toX) continue; // dropped pieces always stand for themselves
4726                   old = (k==toY && j==toX) ? boards[moveNum-1][fromY][fromX] : boards[moveNum-1][k][j]; // trace back mover
4727                   if(old == new) continue;
4728                   if(old == PROMOTED new) boards[moveNum][k][j] = old; // prevent promoted pieces to revert to primordial ones
4729                   else if(new == WhiteWazir || new == BlackWazir) {
4730                       if(old < WhiteCannon || old >= BlackPawn && old < BlackCannon)
4731                            boards[moveNum][k][j] = PROMOTED old; // choose correct type of Gold in promotion
4732                       else boards[moveNum][k][j] = old; // preserve type of Gold
4733                   } else if((old == WhitePawn || old == BlackPawn) && new != EmptySquare) // Pawn promotions (but not e.p.capture!)
4734                       boards[moveNum][k][j] = PROMOTED new; // use non-primordial representation of chosen piece
4735               }
4736           } else {
4737             /* Move from ICS was illegal!?  Punt. */
4738             if (appData.debugMode) {
4739               fprintf(debugFP, "Illegal move from ICS '%s'\n", move_str);
4740               fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
4741             }
4742             safeStrCpy(parseList[moveNum - 1], move_str, sizeof(parseList[moveNum - 1])/sizeof(parseList[moveNum - 1][0]));
4743             strcat(parseList[moveNum - 1], " ");
4744             strcat(parseList[moveNum - 1], elapsed_time);
4745             moveList[moveNum - 1][0] = NULLCHAR;
4746             fromX = fromY = toX = toY = -1;
4747           }
4748         }
4749   if (appData.debugMode) {
4750     fprintf(debugFP, "Move parsed to '%s'\n", parseList[moveNum - 1]);
4751     setbuf(debugFP, NULL);
4752   }
4753
4754 #if ZIPPY
4755         /* Send move to chess program (BEFORE animating it). */
4756         if (appData.zippyPlay && !newGame && newMove &&
4757            (!appData.getMoveList || backwardMostMove == 0) && first.initDone) {
4758
4759             if ((gameMode == IcsPlayingWhite && WhiteOnMove(moveNum)) ||
4760                 (gameMode == IcsPlayingBlack && !WhiteOnMove(moveNum))) {
4761                 if (moveList[moveNum - 1][0] == NULLCHAR) {
4762                   snprintf(str, MSG_SIZ, _("Couldn't parse move \"%s\" from ICS"),
4763                             move_str);
4764                     DisplayError(str, 0);
4765                 } else {
4766                     if (first.sendTime) {
4767                         SendTimeRemaining(&first, gameMode == IcsPlayingWhite);
4768                     }
4769                     bookHit = SendMoveToBookUser(moveNum - 1, &first, FALSE); // [HGM] book
4770                     if (firstMove && !bookHit) {
4771                         firstMove = FALSE;
4772                         if (first.useColors) {
4773                           SendToProgram(gameMode == IcsPlayingWhite ?
4774                                         "white\ngo\n" :
4775                                         "black\ngo\n", &first);
4776                         } else {
4777                           SendToProgram("go\n", &first);
4778                         }
4779                         first.maybeThinking = TRUE;
4780                     }
4781                 }
4782             } else if (gameMode == IcsObserving || gameMode == IcsExamining) {
4783               if (moveList[moveNum - 1][0] == NULLCHAR) {
4784                 snprintf(str, MSG_SIZ, _("Couldn't parse move \"%s\" from ICS"), move_str);
4785                 DisplayError(str, 0);
4786               } else {
4787                 if(gameInfo.variant == currentlyInitializedVariant) // [HGM] refrain sending moves engine can't understand!
4788                 SendMoveToProgram(moveNum - 1, &first);
4789               }
4790             }
4791         }
4792 #endif
4793     }
4794
4795     if (moveNum > 0 && !gotPremove && !appData.noGUI) {
4796         /* If move comes from a remote source, animate it.  If it
4797            isn't remote, it will have already been animated. */
4798         if (!pausing && !ics_user_moved && prevMove == moveNum - 1) {
4799             AnimateMove(boards[moveNum - 1], fromX, fromY, toX, toY);
4800         }
4801         if (!pausing && appData.highlightLastMove) {
4802             SetHighlights(fromX, fromY, toX, toY);
4803         }
4804     }
4805
4806     /* Start the clocks */
4807     whiteFlag = blackFlag = FALSE;
4808     appData.clockMode = !(basetime == 0 && increment == 0);
4809     if (ticking == 0) {
4810       ics_clock_paused = TRUE;
4811       StopClocks();
4812     } else if (ticking == 1) {
4813       ics_clock_paused = FALSE;
4814     }
4815     if (gameMode == IcsIdle ||
4816         relation == RELATION_OBSERVING_STATIC ||
4817         relation == RELATION_EXAMINING ||
4818         ics_clock_paused)
4819       DisplayBothClocks();
4820     else
4821       StartClocks();
4822
4823     /* Display opponents and material strengths */
4824     if (gameInfo.variant != VariantBughouse &&
4825         gameInfo.variant != VariantCrazyhouse && !appData.noGUI) {
4826         if (tinyLayout || smallLayout) {
4827             if(gameInfo.variant == VariantNormal)
4828               snprintf(str, MSG_SIZ, "%s(%d) %s(%d) {%d %d}",
4829                     gameInfo.white, white_stren, gameInfo.black, black_stren,
4830                     basetime, increment);
4831             else
4832               snprintf(str, MSG_SIZ, "%s(%d) %s(%d) {%d %d w%d}",
4833                     gameInfo.white, white_stren, gameInfo.black, black_stren,
4834                     basetime, increment, (int) gameInfo.variant);
4835         } else {
4836             if(gameInfo.variant == VariantNormal)
4837               snprintf(str, MSG_SIZ, "%s (%d) %s %s (%d) {%d %d}",
4838                     gameInfo.white, white_stren, _("vs."), gameInfo.black, black_stren,
4839                     basetime, increment);
4840             else
4841               snprintf(str, MSG_SIZ, "%s (%d) %s %s (%d) {%d %d %s}",
4842                     gameInfo.white, white_stren, _("vs."), gameInfo.black, black_stren,
4843                     basetime, increment, VariantName(gameInfo.variant));
4844         }
4845         DisplayTitle(str);
4846   if (appData.debugMode) {
4847     fprintf(debugFP, "Display title '%s, gameInfo.variant = %d'\n", str, gameInfo.variant);
4848   }
4849     }
4850
4851
4852     /* Display the board */
4853     if (!pausing && !appData.noGUI) {
4854
4855       if (appData.premove)
4856           if (!gotPremove ||
4857              ((gameMode == IcsPlayingWhite) && (WhiteOnMove(currentMove))) ||
4858              ((gameMode == IcsPlayingBlack) && (!WhiteOnMove(currentMove))))
4859               ClearPremoveHighlights();
4860
4861       j = seekGraphUp; seekGraphUp = FALSE; // [HGM] seekgraph: when we draw a board, it overwrites the seek graph
4862         if(partnerUp) { flipView = originalFlip; partnerUp = FALSE; j = TRUE; } // [HGM] bughouse: restore view
4863       DrawPosition(j, boards[currentMove]);
4864
4865       DisplayMove(moveNum - 1);
4866       if (appData.ringBellAfterMoves && /*!ics_user_moved*/ // [HGM] use absolute method to recognize own move
4867             !((gameMode == IcsPlayingWhite) && (!WhiteOnMove(moveNum)) ||
4868               (gameMode == IcsPlayingBlack) &&  (WhiteOnMove(moveNum))   ) ) {
4869         if(newMove) RingBell(); else PlayIcsUnfinishedSound();
4870       }
4871     }
4872
4873     HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
4874 #if ZIPPY
4875     if(bookHit) { // [HGM] book: simulate book reply
4876         static char bookMove[MSG_SIZ]; // a bit generous?
4877
4878         programStats.nodes = programStats.depth = programStats.time =
4879         programStats.score = programStats.got_only_move = 0;
4880         sprintf(programStats.movelist, "%s (xbook)", bookHit);
4881
4882         safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
4883         strcat(bookMove, bookHit);
4884         HandleMachineMove(bookMove, &first);
4885     }
4886 #endif
4887 }
4888
4889 void
4890 GetMoveListEvent ()
4891 {
4892     char buf[MSG_SIZ];
4893     if (appData.icsActive && gameMode != IcsIdle && ics_gamenum > 0) {
4894         ics_getting_history = H_REQUESTED;
4895         snprintf(buf, MSG_SIZ, "%smoves %d\n", ics_prefix, ics_gamenum);
4896         SendToICS(buf);
4897     }
4898 }
4899
4900 void
4901 AnalysisPeriodicEvent (int force)
4902 {
4903     if (((programStats.ok_to_send == 0 || programStats.line_is_book)
4904          && !force) || !appData.periodicUpdates)
4905       return;
4906
4907     /* Send . command to Crafty to collect stats */
4908     SendToProgram(".\n", &first);
4909
4910     /* Don't send another until we get a response (this makes
4911        us stop sending to old Crafty's which don't understand
4912        the "." command (sending illegal cmds resets node count & time,
4913        which looks bad)) */
4914     programStats.ok_to_send = 0;
4915 }
4916
4917 void
4918 ics_update_width (int new_width)
4919 {
4920         ics_printf("set width %d\n", new_width);
4921 }
4922
4923 void
4924 SendMoveToProgram (int moveNum, ChessProgramState *cps)
4925 {
4926     char buf[MSG_SIZ];
4927
4928     if(moveList[moveNum][1] == '@' && moveList[moveNum][0] == '@') {
4929         // null move in variant where engine does not understand it (for analysis purposes)
4930         SendBoard(cps, moveNum + 1); // send position after move in stead.
4931         return;
4932     }
4933     if (cps->useUsermove) {
4934       SendToProgram("usermove ", cps);
4935     }
4936     if (cps->useSAN) {
4937       char *space;
4938       if ((space = strchr(parseList[moveNum], ' ')) != NULL) {
4939         int len = space - parseList[moveNum];
4940         memcpy(buf, parseList[moveNum], len);
4941         buf[len++] = '\n';
4942         buf[len] = NULLCHAR;
4943       } else {
4944         snprintf(buf, MSG_SIZ,"%s\n", parseList[moveNum]);
4945       }
4946       SendToProgram(buf, cps);
4947     } else {
4948       if(cps->alphaRank) { /* [HGM] shogi: temporarily convert to shogi coordinates before sending */
4949         AlphaRank(moveList[moveNum], 4);
4950         SendToProgram(moveList[moveNum], cps);
4951         AlphaRank(moveList[moveNum], 4); // and back
4952       } else
4953       /* Added by Tord: Send castle moves in "O-O" in FRC games if required by
4954        * the engine. It would be nice to have a better way to identify castle
4955        * moves here. */
4956       if((gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom)
4957                                                                          && cps->useOOCastle) {
4958         int fromX = moveList[moveNum][0] - AAA;
4959         int fromY = moveList[moveNum][1] - ONE;
4960         int toX = moveList[moveNum][2] - AAA;
4961         int toY = moveList[moveNum][3] - ONE;
4962         if((boards[moveNum][fromY][fromX] == WhiteKing
4963             && boards[moveNum][toY][toX] == WhiteRook)
4964            || (boards[moveNum][fromY][fromX] == BlackKing
4965                && boards[moveNum][toY][toX] == BlackRook)) {
4966           if(toX > fromX) SendToProgram("O-O\n", cps);
4967           else SendToProgram("O-O-O\n", cps);
4968         }
4969         else SendToProgram(moveList[moveNum], cps);
4970       } else
4971       if(BOARD_HEIGHT > 10) { // [HGM] big: convert ranks to double-digit where needed
4972         if(moveList[moveNum][1] == '@' && (BOARD_HEIGHT < 16 || moveList[moveNum][0] <= 'Z')) { // drop move
4973           if(moveList[moveNum][0]== '@') snprintf(buf, MSG_SIZ, "@@@@\n"); else
4974           snprintf(buf, MSG_SIZ, "%c@%c%d%s", moveList[moveNum][0],
4975                                               moveList[moveNum][2], moveList[moveNum][3] - '0', moveList[moveNum]+4);
4976         } else
4977           snprintf(buf, MSG_SIZ, "%c%d%c%d%s", moveList[moveNum][0], moveList[moveNum][1] - '0',
4978                                                moveList[moveNum][2], moveList[moveNum][3] - '0', moveList[moveNum]+4);
4979         SendToProgram(buf, cps);
4980       }
4981       else SendToProgram(moveList[moveNum], cps);
4982       /* End of additions by Tord */
4983     }
4984
4985     /* [HGM] setting up the opening has brought engine in force mode! */
4986     /*       Send 'go' if we are in a mode where machine should play. */
4987     if( (moveNum == 0 && setboardSpoiledMachineBlack && cps == &first) &&
4988         (gameMode == TwoMachinesPlay   ||
4989 #if ZIPPY
4990          gameMode == IcsPlayingBlack     || gameMode == IcsPlayingWhite ||
4991 #endif
4992          gameMode == MachinePlaysBlack || gameMode == MachinePlaysWhite) ) {
4993         SendToProgram("go\n", cps);
4994   if (appData.debugMode) {
4995     fprintf(debugFP, "(extra)\n");
4996   }
4997     }
4998     setboardSpoiledMachineBlack = 0;
4999 }
5000
5001 void
5002 SendMoveToICS (ChessMove moveType, int fromX, int fromY, int toX, int toY, char promoChar)
5003 {
5004     char user_move[MSG_SIZ];
5005     char suffix[4];
5006
5007     if(gameInfo.variant == VariantSChess && promoChar) {
5008         snprintf(suffix, 4, "=%c", toX == BOARD_WIDTH<<1 ? ToUpper(promoChar) : ToLower(promoChar));
5009         if(moveType == NormalMove) moveType = WhitePromotion; // kludge to do gating
5010     } else suffix[0] = NULLCHAR;
5011
5012     switch (moveType) {
5013       default:
5014         snprintf(user_move, MSG_SIZ, _("say Internal error; bad moveType %d (%d,%d-%d,%d)"),
5015                 (int)moveType, fromX, fromY, toX, toY);
5016         DisplayError(user_move + strlen("say "), 0);
5017         break;
5018       case WhiteKingSideCastle:
5019       case BlackKingSideCastle:
5020       case WhiteQueenSideCastleWild:
5021       case BlackQueenSideCastleWild:
5022       /* PUSH Fabien */
5023       case WhiteHSideCastleFR:
5024       case BlackHSideCastleFR:
5025       /* POP Fabien */
5026         snprintf(user_move, MSG_SIZ, "o-o%s\n", suffix);
5027         break;
5028       case WhiteQueenSideCastle:
5029       case BlackQueenSideCastle:
5030       case WhiteKingSideCastleWild:
5031       case BlackKingSideCastleWild:
5032       /* PUSH Fabien */
5033       case WhiteASideCastleFR:
5034       case BlackASideCastleFR:
5035       /* POP Fabien */
5036         snprintf(user_move, MSG_SIZ, "o-o-o%s\n",suffix);
5037         break;
5038       case WhiteNonPromotion:
5039       case BlackNonPromotion:
5040         sprintf(user_move, "%c%c%c%c==\n", AAA + fromX, ONE + fromY, AAA + toX, ONE + toY);
5041         break;
5042       case WhitePromotion:
5043       case BlackPromotion:
5044         if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
5045           snprintf(user_move, MSG_SIZ, "%c%c%c%c=%c\n",
5046                 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
5047                 PieceToChar(WhiteFerz));
5048         else if(gameInfo.variant == VariantGreat)
5049           snprintf(user_move, MSG_SIZ,"%c%c%c%c=%c\n",
5050                 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
5051                 PieceToChar(WhiteMan));
5052         else
5053           snprintf(user_move, MSG_SIZ, "%c%c%c%c=%c\n",
5054                 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
5055                 promoChar);
5056         break;
5057       case WhiteDrop:
5058       case BlackDrop:
5059       drop:
5060         snprintf(user_move, MSG_SIZ, "%c@%c%c\n",
5061                  ToUpper(PieceToChar((ChessSquare) fromX)),
5062                  AAA + toX, ONE + toY);
5063         break;
5064       case IllegalMove:  /* could be a variant we don't quite understand */
5065         if(fromY == DROP_RANK) goto drop; // We need 'IllegalDrop' move type?
5066       case NormalMove:
5067       case WhiteCapturesEnPassant:
5068       case BlackCapturesEnPassant:
5069         snprintf(user_move, MSG_SIZ,"%c%c%c%c\n",
5070                 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY);
5071         break;
5072     }
5073     SendToICS(user_move);
5074     if(appData.keepAlive) // [HGM] alive: schedule sending of dummy 'date' command
5075         ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
5076 }
5077
5078 void
5079 UploadGameEvent ()
5080 {   // [HGM] upload: send entire stored game to ICS as long-algebraic moves.
5081     int i, last = forwardMostMove; // make sure ICS reply cannot pre-empt us by clearing fmm
5082     static char *castlingStrings[4] = { "none", "kside", "qside", "both" };
5083     if(gameMode == IcsObserving || gameMode == IcsPlayingBlack || gameMode == IcsPlayingWhite) {
5084       DisplayError(_("You cannot do this while you are playing or observing"), 0);
5085       return;
5086     }
5087     if(gameMode != IcsExamining) { // is this ever not the case?
5088         char buf[MSG_SIZ], *p, *fen, command[MSG_SIZ], bsetup = 0;
5089
5090         if(ics_type == ICS_ICC) { // on ICC match ourselves in applicable variant
5091           snprintf(command,MSG_SIZ, "match %s", ics_handle);
5092         } else { // on FICS we must first go to general examine mode
5093           safeStrCpy(command, "examine\nbsetup", sizeof(command)/sizeof(command[0])); // and specify variant within it with bsetups
5094         }
5095         if(gameInfo.variant != VariantNormal) {
5096             // try figure out wild number, as xboard names are not always valid on ICS
5097             for(i=1; i<=36; i++) {
5098               snprintf(buf, MSG_SIZ, "wild/%d", i);
5099                 if(StringToVariant(buf) == gameInfo.variant) break;
5100             }
5101             if(i<=36 && ics_type == ICS_ICC) snprintf(buf, MSG_SIZ,"%s w%d\n", command, i);
5102             else if(i == 22) snprintf(buf,MSG_SIZ, "%s fr\n", command);
5103             else snprintf(buf, MSG_SIZ,"%s %s\n", command, VariantName(gameInfo.variant));
5104         } else snprintf(buf, MSG_SIZ,"%s\n", ics_type == ICS_ICC ? command : "examine\n"); // match yourself or examine
5105         SendToICS(ics_prefix);
5106         SendToICS(buf);
5107         if(startedFromSetupPosition || backwardMostMove != 0) {
5108           fen = PositionToFEN(backwardMostMove, NULL);
5109           if(ics_type == ICS_ICC) { // on ICC we can simply send a complete FEN to set everything
5110             snprintf(buf, MSG_SIZ,"loadfen %s\n", fen);
5111             SendToICS(buf);
5112           } else { // FICS: everything has to set by separate bsetup commands
5113             p = strchr(fen, ' '); p[0] = NULLCHAR; // cut after board
5114             snprintf(buf, MSG_SIZ,"bsetup fen %s\n", fen);
5115             SendToICS(buf);
5116             if(!WhiteOnMove(backwardMostMove)) {
5117                 SendToICS("bsetup tomove black\n");
5118             }
5119             i = (strchr(p+3, 'K') != NULL) + 2*(strchr(p+3, 'Q') != NULL);
5120             snprintf(buf, MSG_SIZ,"bsetup wcastle %s\n", castlingStrings[i]);
5121             SendToICS(buf);
5122             i = (strchr(p+3, 'k') != NULL) + 2*(strchr(p+3, 'q') != NULL);
5123             snprintf(buf, MSG_SIZ, "bsetup bcastle %s\n", castlingStrings[i]);
5124             SendToICS(buf);
5125             i = boards[backwardMostMove][EP_STATUS];
5126             if(i >= 0) { // set e.p.
5127               snprintf(buf, MSG_SIZ,"bsetup eppos %c\n", i+AAA);
5128                 SendToICS(buf);
5129             }
5130             bsetup++;
5131           }
5132         }
5133       if(bsetup || ics_type != ICS_ICC && gameInfo.variant != VariantNormal)
5134             SendToICS("bsetup done\n"); // switch to normal examining.
5135     }
5136     for(i = backwardMostMove; i<last; i++) {
5137         char buf[20];
5138         snprintf(buf, sizeof(buf)/sizeof(buf[0]),"%s\n", parseList[i]);
5139         SendToICS(buf);
5140     }
5141     SendToICS(ics_prefix);
5142     SendToICS(ics_type == ICS_ICC ? "tag result Game in progress\n" : "commit\n");
5143 }
5144
5145 void
5146 CoordsToComputerAlgebraic (int rf, int ff, int rt, int ft, char promoChar, char move[7])
5147 {
5148     if (rf == DROP_RANK) {
5149       if(ff == EmptySquare) sprintf(move, "@@@@\n"); else // [HGM] pass
5150       sprintf(move, "%c@%c%c\n",
5151                 ToUpper(PieceToChar((ChessSquare) ff)), AAA + ft, ONE + rt);
5152     } else {
5153         if (promoChar == 'x' || promoChar == NULLCHAR) {
5154           sprintf(move, "%c%c%c%c\n",
5155                     AAA + ff, ONE + rf, AAA + ft, ONE + rt);
5156         } else {
5157             sprintf(move, "%c%c%c%c%c\n",
5158                     AAA + ff, ONE + rf, AAA + ft, ONE + rt, promoChar);
5159         }
5160     }
5161 }
5162
5163 void
5164 ProcessICSInitScript (FILE *f)
5165 {
5166     char buf[MSG_SIZ];
5167
5168     while (fgets(buf, MSG_SIZ, f)) {
5169         SendToICSDelayed(buf,(long)appData.msLoginDelay);
5170     }
5171
5172     fclose(f);
5173 }
5174
5175
5176 static int lastX, lastY, selectFlag, dragging;
5177
5178 void
5179 Sweep (int step)
5180 {
5181     ChessSquare king = WhiteKing, pawn = WhitePawn, last = promoSweep;
5182     if(gameInfo.variant == VariantKnightmate) king = WhiteUnicorn;
5183     if(gameInfo.variant == VariantSuicide || gameInfo.variant == VariantGiveaway) king = EmptySquare;
5184     if(promoSweep >= BlackPawn) king = WHITE_TO_BLACK king, pawn = WHITE_TO_BLACK pawn;
5185     if(gameInfo.variant == VariantSpartan && pawn == BlackPawn) pawn = BlackLance, king = EmptySquare;
5186     if(fromY != BOARD_HEIGHT-2 && fromY != 1) pawn = EmptySquare;
5187     do {
5188         promoSweep -= step;
5189         if(promoSweep == EmptySquare) promoSweep = BlackPawn; // wrap
5190         else if((int)promoSweep == -1) promoSweep = WhiteKing;
5191         else if(promoSweep == BlackPawn && step < 0) promoSweep = WhitePawn;
5192         else if(promoSweep == WhiteKing && step > 0) promoSweep = BlackKing;
5193         if(!step) step = -1;
5194     } while(PieceToChar(promoSweep) == '.' || PieceToChar(promoSweep) == '~' || promoSweep == pawn ||
5195             appData.testLegality && (promoSweep == king ||
5196             gameInfo.variant == VariantShogi && promoSweep != PROMOTED last && last != PROMOTED promoSweep && last != promoSweep));
5197     if(toX >= 0) {
5198         int victim = boards[currentMove][toY][toX];
5199         boards[currentMove][toY][toX] = promoSweep;
5200         DrawPosition(FALSE, boards[currentMove]);
5201         boards[currentMove][toY][toX] = victim;
5202     } else
5203     ChangeDragPiece(promoSweep);
5204 }
5205
5206 int
5207 PromoScroll (int x, int y)
5208 {
5209   int step = 0;
5210
5211   if(promoSweep == EmptySquare || !appData.sweepSelect) return FALSE;
5212   if(abs(x - lastX) < 25 && abs(y - lastY) < 25) return FALSE;
5213   if( y > lastY + 2 ) step = -1; else if(y < lastY - 2) step = 1;
5214   if(!step) return FALSE;
5215   lastX = x; lastY = y;
5216   if((promoSweep < BlackPawn) == flipView) step = -step;
5217   if(step > 0) selectFlag = 1;
5218   if(!selectFlag) Sweep(step);
5219   return FALSE;
5220 }
5221
5222 void
5223 NextPiece (int step)
5224 {
5225     ChessSquare piece = boards[currentMove][toY][toX];
5226     do {
5227         pieceSweep -= step;
5228         if(pieceSweep == EmptySquare) pieceSweep = WhitePawn; // wrap
5229         if((int)pieceSweep == -1) pieceSweep = BlackKing;
5230         if(!step) step = -1;
5231     } while(PieceToChar(pieceSweep) == '.');
5232     boards[currentMove][toY][toX] = pieceSweep;
5233     DrawPosition(FALSE, boards[currentMove]);
5234     boards[currentMove][toY][toX] = piece;
5235 }
5236 /* [HGM] Shogi move preprocessor: swap digits for letters, vice versa */
5237 void
5238 AlphaRank (char *move, int n)
5239 {
5240 //    char *p = move, c; int x, y;
5241
5242     if (appData.debugMode) {
5243         fprintf(debugFP, "alphaRank(%s,%d)\n", move, n);
5244     }
5245
5246     if(move[1]=='*' &&
5247        move[2]>='0' && move[2]<='9' &&
5248        move[3]>='a' && move[3]<='x'    ) {
5249         move[1] = '@';
5250         move[2] = BOARD_RGHT  -1 - (move[2]-'1') + AAA;
5251         move[3] = BOARD_HEIGHT-1 - (move[3]-'a') + ONE;
5252     } else
5253     if(move[0]>='0' && move[0]<='9' &&
5254        move[1]>='a' && move[1]<='x' &&
5255        move[2]>='0' && move[2]<='9' &&
5256        move[3]>='a' && move[3]<='x'    ) {
5257         /* input move, Shogi -> normal */
5258         move[0] = BOARD_RGHT  -1 - (move[0]-'1') + AAA;
5259         move[1] = BOARD_HEIGHT-1 - (move[1]-'a') + ONE;
5260         move[2] = BOARD_RGHT  -1 - (move[2]-'1') + AAA;
5261         move[3] = BOARD_HEIGHT-1 - (move[3]-'a') + ONE;
5262     } else
5263     if(move[1]=='@' &&
5264        move[3]>='0' && move[3]<='9' &&
5265        move[2]>='a' && move[2]<='x'    ) {
5266         move[1] = '*';
5267         move[2] = BOARD_RGHT - 1 - (move[2]-AAA) + '1';
5268         move[3] = BOARD_HEIGHT-1 - (move[3]-ONE) + 'a';
5269     } else
5270     if(
5271        move[0]>='a' && move[0]<='x' &&
5272        move[3]>='0' && move[3]<='9' &&
5273        move[2]>='a' && move[2]<='x'    ) {
5274          /* output move, normal -> Shogi */
5275         move[0] = BOARD_RGHT - 1 - (move[0]-AAA) + '1';
5276         move[1] = BOARD_HEIGHT-1 - (move[1]-ONE) + 'a';
5277         move[2] = BOARD_RGHT - 1 - (move[2]-AAA) + '1';
5278         move[3] = BOARD_HEIGHT-1 - (move[3]-ONE) + 'a';
5279         if(move[4] == PieceToChar(BlackQueen)) move[4] = '+';
5280     }
5281     if (appData.debugMode) {
5282         fprintf(debugFP, "   out = '%s'\n", move);
5283     }
5284 }
5285
5286 char yy_textstr[8000];
5287
5288 /* Parser for moves from gnuchess, ICS, or user typein box */
5289 Boolean
5290 ParseOneMove (char *move, int moveNum, ChessMove *moveType, int *fromX, int *fromY, int *toX, int *toY, char *promoChar)
5291 {
5292     *moveType = yylexstr(moveNum, move, yy_textstr, sizeof yy_textstr);
5293
5294     switch (*moveType) {
5295       case WhitePromotion:
5296       case BlackPromotion:
5297       case WhiteNonPromotion:
5298       case BlackNonPromotion:
5299       case NormalMove:
5300       case WhiteCapturesEnPassant:
5301       case BlackCapturesEnPassant:
5302       case WhiteKingSideCastle:
5303       case WhiteQueenSideCastle:
5304       case BlackKingSideCastle:
5305       case BlackQueenSideCastle:
5306       case WhiteKingSideCastleWild:
5307       case WhiteQueenSideCastleWild:
5308       case BlackKingSideCastleWild:
5309       case BlackQueenSideCastleWild:
5310       /* Code added by Tord: */
5311       case WhiteHSideCastleFR:
5312       case WhiteASideCastleFR:
5313       case BlackHSideCastleFR:
5314       case BlackASideCastleFR:
5315       /* End of code added by Tord */
5316       case IllegalMove:         /* bug or odd chess variant */
5317         *fromX = currentMoveString[0] - AAA;
5318         *fromY = currentMoveString[1] - ONE;
5319         *toX = currentMoveString[2] - AAA;
5320         *toY = currentMoveString[3] - ONE;
5321         *promoChar = currentMoveString[4];
5322         if (*fromX < BOARD_LEFT || *fromX >= BOARD_RGHT || *fromY < 0 || *fromY >= BOARD_HEIGHT ||
5323             *toX < BOARD_LEFT || *toX >= BOARD_RGHT || *toY < 0 || *toY >= BOARD_HEIGHT) {
5324     if (appData.debugMode) {
5325         fprintf(debugFP, "Off-board move (%d,%d)-(%d,%d)%c, type = %d\n", *fromX, *fromY, *toX, *toY, *promoChar, *moveType);
5326     }
5327             *fromX = *fromY = *toX = *toY = 0;
5328             return FALSE;
5329         }
5330         if (appData.testLegality) {
5331           return (*moveType != IllegalMove);
5332         } else {
5333           return !(*fromX == *toX && *fromY == *toY) && boards[moveNum][*fromY][*fromX] != EmptySquare &&
5334                         WhiteOnMove(moveNum) == (boards[moveNum][*fromY][*fromX] < BlackPawn);
5335         }
5336
5337       case WhiteDrop:
5338       case BlackDrop:
5339         *fromX = *moveType == WhiteDrop ?
5340           (int) CharToPiece(ToUpper(currentMoveString[0])) :
5341           (int) CharToPiece(ToLower(currentMoveString[0]));
5342         *fromY = DROP_RANK;
5343         *toX = currentMoveString[2] - AAA;
5344         *toY = currentMoveString[3] - ONE;
5345         *promoChar = NULLCHAR;
5346         return TRUE;
5347
5348       case AmbiguousMove:
5349       case ImpossibleMove:
5350       case EndOfFile:
5351       case ElapsedTime:
5352       case Comment:
5353       case PGNTag:
5354       case NAG:
5355       case WhiteWins:
5356       case BlackWins:
5357       case GameIsDrawn:
5358       default:
5359     if (appData.debugMode) {
5360         fprintf(debugFP, "Impossible move %s, type = %d\n", currentMoveString, *moveType);
5361     }
5362         /* bug? */
5363         *fromX = *fromY = *toX = *toY = 0;
5364         *promoChar = NULLCHAR;
5365         return FALSE;
5366     }
5367 }
5368
5369 Boolean pushed = FALSE;
5370 char *lastParseAttempt;
5371
5372 void
5373 ParsePV (char *pv, Boolean storeComments, Boolean atEnd)
5374 { // Parse a string of PV moves, and append to current game, behind forwardMostMove
5375   int fromX, fromY, toX, toY; char promoChar;
5376   ChessMove moveType;
5377   Boolean valid;
5378   int nr = 0;
5379
5380   if (gameMode == AnalyzeMode && currentMove < forwardMostMove) {
5381     PushInner(currentMove, forwardMostMove); // [HGM] engine might not be thinking on forwardMost position!
5382     pushed = TRUE;
5383   }
5384   endPV = forwardMostMove;
5385   do {
5386     while(*pv == ' ' || *pv == '\n' || *pv == '\t') pv++; // must still read away whitespace
5387     if(nr == 0 && !storeComments && *pv == '(') pv++; // first (ponder) move can be in parentheses
5388     lastParseAttempt = pv;
5389     valid = ParseOneMove(pv, endPV, &moveType, &fromX, &fromY, &toX, &toY, &promoChar);
5390     if(!valid && nr == 0 &&
5391        ParseOneMove(pv, endPV-1, &moveType, &fromX, &fromY, &toX, &toY, &promoChar)){
5392         nr++; moveType = Comment; // First move has been played; kludge to make sure we continue
5393         // Hande case where played move is different from leading PV move
5394         CopyBoard(boards[endPV+1], boards[endPV-1]); // tentatively unplay last game move
5395         CopyBoard(boards[endPV+2], boards[endPV-1]); // and play first move of PV
5396         ApplyMove(fromX, fromY, toX, toY, promoChar, boards[endPV+2]);
5397         if(!CompareBoards(boards[endPV], boards[endPV+2])) {
5398           endPV += 2; // if position different, keep this
5399           moveList[endPV-1][0] = fromX + AAA;
5400           moveList[endPV-1][1] = fromY + ONE;
5401           moveList[endPV-1][2] = toX + AAA;
5402           moveList[endPV-1][3] = toY + ONE;
5403           parseList[endPV-1][0] = NULLCHAR;
5404           safeStrCpy(moveList[endPV-2], "_0_0", sizeof(moveList[endPV-2])/sizeof(moveList[endPV-2][0])); // suppress premove highlight on takeback move
5405         }
5406       }
5407     pv = strstr(pv, yy_textstr) + strlen(yy_textstr); // skip what we parsed
5408     if(nr == 0 && !storeComments && *pv == ')') pv++; // closing parenthesis of ponder move;
5409     if(moveType == Comment && storeComments) AppendComment(endPV, yy_textstr, FALSE);
5410     if(moveType == Comment || moveType == NAG || moveType == ElapsedTime) {
5411         valid++; // allow comments in PV
5412         continue;
5413     }
5414     nr++;
5415     if(endPV+1 > framePtr) break; // no space, truncate
5416     if(!valid) break;
5417     endPV++;
5418     CopyBoard(boards[endPV], boards[endPV-1]);
5419     ApplyMove(fromX, fromY, toX, toY, promoChar, boards[endPV]);
5420     CoordsToComputerAlgebraic(fromY, fromX, toY, toX, promoChar, moveList[endPV - 1]);
5421     strncat(moveList[endPV-1], "\n", MOVE_LEN);
5422     CoordsToAlgebraic(boards[endPV - 1],
5423                              PosFlags(endPV - 1),
5424                              fromY, fromX, toY, toX, promoChar,
5425                              parseList[endPV - 1]);
5426   } while(valid);
5427   if(atEnd == 2) return; // used hidden, for PV conversion
5428   currentMove = (atEnd || endPV == forwardMostMove) ? endPV : forwardMostMove + 1;
5429   if(currentMove == forwardMostMove) ClearPremoveHighlights(); else
5430   SetPremoveHighlights(moveList[currentMove-1][0]-AAA, moveList[currentMove-1][1]-ONE,
5431                        moveList[currentMove-1][2]-AAA, moveList[currentMove-1][3]-ONE);
5432   DrawPosition(TRUE, boards[currentMove]);
5433 }
5434
5435 int
5436 MultiPV (ChessProgramState *cps)
5437 {       // check if engine supports MultiPV, and if so, return the number of the option that sets it
5438         int i;
5439         for(i=0; i<cps->nrOptions; i++)
5440             if(!strcmp(cps->option[i].name, "MultiPV") && cps->option[i].type == Spin)
5441                 return i;
5442         return -1;
5443 }
5444
5445 Boolean
5446 LoadMultiPV (int x, int y, char *buf, int index, int *start, int *end)
5447 {
5448         int startPV, multi, lineStart, origIndex = index;
5449         char *p, buf2[MSG_SIZ];
5450
5451         if(index < 0 || index >= strlen(buf)) return FALSE; // sanity
5452         lastX = x; lastY = y;
5453         while(index > 0 && buf[index-1] != '\n') index--; // beginning of line
5454         lineStart = startPV = index;
5455         while(buf[index] != '\n') if(buf[index++] == '\t') startPV = index;
5456         if(index == startPV && (p = StrCaseStr(buf+index, "PV="))) startPV = p - buf + 3;
5457         index = startPV;
5458         do{ while(buf[index] && buf[index] != '\n') index++;
5459         } while(buf[index] == '\n' && buf[index+1] == '\\' && buf[index+2] == ' ' && index++); // join kibitzed PV continuation line
5460         buf[index] = 0;
5461         if(lineStart == 0 && gameMode == AnalyzeMode && (multi = MultiPV(&first)) >= 0) {
5462                 int n = first.option[multi].value;
5463                 if(origIndex > 17 && origIndex < 24) { if(n>1) n--; } else if(origIndex > index - 6) n++;
5464                 snprintf(buf2, MSG_SIZ, "option MultiPV=%d\n", n);
5465                 if(first.option[multi].value != n) SendToProgram(buf2, &first);
5466                 first.option[multi].value = n;
5467                 *start = *end = 0;
5468                 return FALSE;
5469         } else if(strstr(buf+lineStart, "exclude:") == buf+lineStart) { // exclude moves clicked
5470                 ExcludeClick(origIndex - lineStart);
5471                 return FALSE;
5472         }
5473         ParsePV(buf+startPV, FALSE, gameMode != AnalyzeMode);
5474         *start = startPV; *end = index-1;
5475         return TRUE;
5476 }
5477
5478 char *
5479 PvToSAN (char *pv)
5480 {
5481         static char buf[10*MSG_SIZ];
5482         int i, k=0, savedEnd=endPV, saveFMM = forwardMostMove;
5483         *buf = NULLCHAR;
5484         if(forwardMostMove < endPV) PushInner(forwardMostMove, endPV);
5485         ParsePV(pv, FALSE, 2); // this appends PV to game, suppressing any display of it
5486         for(i = forwardMostMove; i<endPV; i++){
5487             if(i&1) snprintf(buf+k, 10*MSG_SIZ-k, "%s ", parseList[i]);
5488             else    snprintf(buf+k, 10*MSG_SIZ-k, "%d. %s ", i/2 + 1, parseList[i]);
5489             k += strlen(buf+k);
5490         }
5491         snprintf(buf+k, 10*MSG_SIZ-k, "%s", lastParseAttempt); // if we ran into stuff that could not be parsed, print it verbatim
5492         if(forwardMostMove < savedEnd) { PopInner(0); forwardMostMove = saveFMM; } // PopInner would set fmm to endPV!
5493         endPV = savedEnd;
5494         return buf;
5495 }
5496
5497 Boolean
5498 LoadPV (int x, int y)
5499 { // called on right mouse click to load PV
5500   int which = gameMode == TwoMachinesPlay && (WhiteOnMove(forwardMostMove) == (second.twoMachinesColor[0] == 'w'));
5501   lastX = x; lastY = y;
5502   ParsePV(lastPV[which], FALSE, TRUE); // load the PV of the thinking engine in the boards array.
5503   return TRUE;
5504 }
5505
5506 void
5507 UnLoadPV ()
5508 {
5509   int oldFMM = forwardMostMove; // N.B.: this was currentMove before PV was loaded!
5510   if(endPV < 0) return;
5511   if(appData.autoCopyPV) CopyFENToClipboard();
5512   endPV = -1;
5513   if(gameMode == AnalyzeMode && currentMove > forwardMostMove) {
5514         Boolean saveAnimate = appData.animate;
5515         if(pushed) {
5516             if(shiftKey && storedGames < MAX_VARIATIONS-2) { // wants to start variation, and there is space
5517                 if(storedGames == 1) GreyRevert(FALSE);      // we already pushed the tail, so just make it official
5518             } else storedGames--; // abandon shelved tail of original game
5519         }
5520         pushed = FALSE;
5521         forwardMostMove = currentMove;
5522         currentMove = oldFMM;
5523         appData.animate = FALSE;
5524         ToNrEvent(forwardMostMove);
5525         appData.animate = saveAnimate;
5526   }
5527   currentMove = forwardMostMove;
5528   if(pushed) { PopInner(0); pushed = FALSE; } // restore shelved game continuation
5529   ClearPremoveHighlights();
5530   DrawPosition(TRUE, boards[currentMove]);
5531 }
5532
5533 void
5534 MovePV (int x, int y, int h)
5535 { // step through PV based on mouse coordinates (called on mouse move)
5536   int margin = h>>3, step = 0, threshold = (pieceSweep == EmptySquare ? 10 : 15);
5537
5538   // we must somehow check if right button is still down (might be released off board!)
5539   if(endPV < 0 && pieceSweep == EmptySquare) return; // needed in XBoard because lastX/Y is shared :-(
5540   if(abs(x - lastX) < threshold && abs(y - lastY) < threshold) return;
5541   if( y > lastY + 2 ) step = -1; else if(y < lastY - 2) step = 1;
5542   if(!step) return;
5543   lastX = x; lastY = y;
5544
5545   if(pieceSweep != EmptySquare) { NextPiece(step); return; }
5546   if(endPV < 0) return;
5547   if(y < margin) step = 1; else
5548   if(y > h - margin) step = -1;
5549   if(currentMove + step > endPV || currentMove + step < forwardMostMove) step = 0;
5550   currentMove += step;
5551   if(currentMove == forwardMostMove) ClearPremoveHighlights(); else
5552   SetPremoveHighlights(moveList[currentMove-1][0]-AAA, moveList[currentMove-1][1]-ONE,
5553                        moveList[currentMove-1][2]-AAA, moveList[currentMove-1][3]-ONE);
5554   DrawPosition(FALSE, boards[currentMove]);
5555 }
5556
5557
5558 // [HGM] shuffle: a general way to suffle opening setups, applicable to arbitrary variants.
5559 // All positions will have equal probability, but the current method will not provide a unique
5560 // numbering scheme for arrays that contain 3 or more pieces of the same kind.
5561 #define DARK 1
5562 #define LITE 2
5563 #define ANY 3
5564
5565 int squaresLeft[4];
5566 int piecesLeft[(int)BlackPawn];
5567 int seed, nrOfShuffles;
5568
5569 void
5570 GetPositionNumber ()
5571 {       // sets global variable seed
5572         int i;
5573
5574         seed = appData.defaultFrcPosition;
5575         if(seed < 0) { // randomize based on time for negative FRC position numbers
5576                 for(i=0; i<50; i++) seed += random();
5577                 seed = random() ^ random() >> 8 ^ random() << 8;
5578                 if(seed<0) seed = -seed;
5579         }
5580 }
5581
5582 int
5583 put (Board board, int pieceType, int rank, int n, int shade)
5584 // put the piece on the (n-1)-th empty squares of the given shade
5585 {
5586         int i;
5587
5588         for(i=BOARD_LEFT; i<BOARD_RGHT; i++) {
5589                 if( (((i-BOARD_LEFT)&1)+1) & shade && board[rank][i] == EmptySquare && n-- == 0) {
5590                         board[rank][i] = (ChessSquare) pieceType;
5591                         squaresLeft[((i-BOARD_LEFT)&1) + 1]--;
5592                         squaresLeft[ANY]--;
5593                         piecesLeft[pieceType]--;
5594                         return i;
5595                 }
5596         }
5597         return -1;
5598 }
5599
5600
5601 void
5602 AddOnePiece (Board board, int pieceType, int rank, int shade)
5603 // calculate where the next piece goes, (any empty square), and put it there
5604 {
5605         int i;
5606
5607         i = seed % squaresLeft[shade];
5608         nrOfShuffles *= squaresLeft[shade];
5609         seed /= squaresLeft[shade];
5610         put(board, pieceType, rank, i, shade);
5611 }
5612
5613 void
5614 AddTwoPieces (Board board, int pieceType, int rank)
5615 // calculate where the next 2 identical pieces go, (any empty square), and put it there
5616 {
5617         int i, n=squaresLeft[ANY], j=n-1, k;
5618
5619         k = n*(n-1)/2; // nr of possibilities, not counting permutations
5620         i = seed % k;  // pick one
5621         nrOfShuffles *= k;
5622         seed /= k;
5623         while(i >= j) i -= j--;
5624         j = n - 1 - j; i += j;
5625         put(board, pieceType, rank, j, ANY);
5626         put(board, pieceType, rank, i, ANY);
5627 }
5628
5629 void
5630 SetUpShuffle (Board board, int number)
5631 {
5632         int i, p, first=1;
5633
5634         GetPositionNumber(); nrOfShuffles = 1;
5635
5636         squaresLeft[DARK] = (BOARD_RGHT - BOARD_LEFT + 1)/2;
5637         squaresLeft[ANY]  = BOARD_RGHT - BOARD_LEFT;
5638         squaresLeft[LITE] = squaresLeft[ANY] - squaresLeft[DARK];
5639
5640         for(p = 0; p<=(int)WhiteKing; p++) piecesLeft[p] = 0;
5641
5642         for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // count pieces and clear board
5643             p = (int) board[0][i];
5644             if(p < (int) BlackPawn) piecesLeft[p] ++;
5645             board[0][i] = EmptySquare;
5646         }
5647
5648         if(PosFlags(0) & F_ALL_CASTLE_OK) {
5649             // shuffles restricted to allow normal castling put KRR first
5650             if(piecesLeft[(int)WhiteKing]) // King goes rightish of middle
5651                 put(board, WhiteKing, 0, (gameInfo.boardWidth+1)/2, ANY);
5652             else if(piecesLeft[(int)WhiteUnicorn]) // in Knightmate Unicorn castles
5653                 put(board, WhiteUnicorn, 0, (gameInfo.boardWidth+1)/2, ANY);
5654             if(piecesLeft[(int)WhiteRook]) // First supply a Rook for K-side castling
5655                 put(board, WhiteRook, 0, gameInfo.boardWidth-2, ANY);
5656             if(piecesLeft[(int)WhiteRook]) // Then supply a Rook for Q-side castling
5657                 put(board, WhiteRook, 0, 0, ANY);
5658             // in variants with super-numerary Kings and Rooks, we leave these for the shuffle
5659         }
5660
5661         if(((BOARD_RGHT-BOARD_LEFT) & 1) == 0)
5662             // only for even boards make effort to put pairs of colorbound pieces on opposite colors
5663             for(p = (int) WhiteKing; p > (int) WhitePawn; p--) {
5664                 if(p != (int) WhiteBishop && p != (int) WhiteFerz && p != (int) WhiteAlfil) continue;
5665                 while(piecesLeft[p] >= 2) {
5666                     AddOnePiece(board, p, 0, LITE);
5667                     AddOnePiece(board, p, 0, DARK);
5668                 }
5669                 // Odd color-bound pieces are shuffled with the rest (to not run out of paired squares)
5670             }
5671
5672         for(p = (int) WhiteKing - 2; p > (int) WhitePawn; p--) {
5673             // Remaining pieces (non-colorbound, or odd color bound) can be put anywhere
5674             // but we leave King and Rooks for last, to possibly obey FRC restriction
5675             if(p == (int)WhiteRook) continue;
5676             while(piecesLeft[p] >= 2) AddTwoPieces(board, p, 0); // add in pairs, for not counting permutations
5677             if(piecesLeft[p]) AddOnePiece(board, p, 0, ANY);     // add the odd piece
5678         }
5679
5680         // now everything is placed, except perhaps King (Unicorn) and Rooks
5681
5682         if(PosFlags(0) & F_FRC_TYPE_CASTLING) {
5683             // Last King gets castling rights
5684             while(piecesLeft[(int)WhiteUnicorn]) {
5685                 i = put(board, WhiteUnicorn, 0, piecesLeft[(int)WhiteRook]/2, ANY);
5686                 initialRights[2]  = initialRights[5]  = board[CASTLING][2] = board[CASTLING][5] = i;
5687             }
5688
5689             while(piecesLeft[(int)WhiteKing]) {
5690                 i = put(board, WhiteKing, 0, piecesLeft[(int)WhiteRook]/2, ANY);
5691                 initialRights[2]  = initialRights[5]  = board[CASTLING][2] = board[CASTLING][5] = i;
5692             }
5693
5694
5695         } else {
5696             while(piecesLeft[(int)WhiteKing])    AddOnePiece(board, WhiteKing, 0, ANY);
5697             while(piecesLeft[(int)WhiteUnicorn]) AddOnePiece(board, WhiteUnicorn, 0, ANY);
5698         }
5699
5700         // Only Rooks can be left; simply place them all
5701         while(piecesLeft[(int)WhiteRook]) {
5702                 i = put(board, WhiteRook, 0, 0, ANY);
5703                 if(PosFlags(0) & F_FRC_TYPE_CASTLING) { // first and last Rook get FRC castling rights
5704                         if(first) {
5705                                 first=0;
5706                                 initialRights[1]  = initialRights[4]  = board[CASTLING][1] = board[CASTLING][4] = i;
5707                         }
5708                         initialRights[0]  = initialRights[3]  = board[CASTLING][0] = board[CASTLING][3] = i;
5709                 }
5710         }
5711         for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // copy black from white
5712             board[BOARD_HEIGHT-1][i] =  (int) board[0][i] < BlackPawn ? WHITE_TO_BLACK board[0][i] : EmptySquare;
5713         }
5714
5715         if(number >= 0) appData.defaultFrcPosition %= nrOfShuffles; // normalize
5716 }
5717
5718 int
5719 SetCharTable (char *table, const char * map)
5720 /* [HGM] moved here from winboard.c because of its general usefulness */
5721 /*       Basically a safe strcpy that uses the last character as King */
5722 {
5723     int result = FALSE; int NrPieces;
5724
5725     if( map != NULL && (NrPieces=strlen(map)) <= (int) EmptySquare
5726                     && NrPieces >= 12 && !(NrPieces&1)) {
5727         int i; /* [HGM] Accept even length from 12 to 34 */
5728
5729         for( i=0; i<(int) EmptySquare; i++ ) table[i] = '.';
5730         for( i=0; i<NrPieces/2-1; i++ ) {
5731             table[i] = map[i];
5732             table[i + (int)BlackPawn - (int) WhitePawn] = map[i+NrPieces/2];
5733         }
5734         table[(int) WhiteKing]  = map[NrPieces/2-1];
5735         table[(int) BlackKing]  = map[NrPieces-1];
5736
5737         result = TRUE;
5738     }
5739
5740     return result;
5741 }
5742
5743 void
5744 Prelude (Board board)
5745 {       // [HGM] superchess: random selection of exo-pieces
5746         int i, j, k; ChessSquare p;
5747         static ChessSquare exoPieces[4] = { WhiteAngel, WhiteMarshall, WhiteSilver, WhiteLance };
5748
5749         GetPositionNumber(); // use FRC position number
5750
5751         if(appData.pieceToCharTable != NULL) { // select pieces to participate from given char table
5752             SetCharTable(pieceToChar, appData.pieceToCharTable);
5753             for(i=(int)WhiteQueen+1, j=0; i<(int)WhiteKing && j<4; i++)
5754                 if(PieceToChar((ChessSquare)i) != '.') exoPieces[j++] = (ChessSquare) i;
5755         }
5756
5757         j = seed%4;                 seed /= 4;
5758         p = board[0][BOARD_LEFT+j];   board[0][BOARD_LEFT+j] = EmptySquare; k = PieceToNumber(p);
5759         board[k][BOARD_WIDTH-1] = p;  board[k][BOARD_WIDTH-2]++;
5760         board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p;  board[BOARD_HEIGHT-1-k][1]++;
5761         j = seed%3 + (seed%3 >= j); seed /= 3;
5762         p = board[0][BOARD_LEFT+j];   board[0][BOARD_LEFT+j] = EmptySquare; k = PieceToNumber(p);
5763         board[k][BOARD_WIDTH-1] = p;  board[k][BOARD_WIDTH-2]++;
5764         board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p;  board[BOARD_HEIGHT-1-k][1]++;
5765         j = seed%3;                 seed /= 3;
5766         p = board[0][BOARD_LEFT+j+5]; board[0][BOARD_LEFT+j+5] = EmptySquare; k = PieceToNumber(p);
5767         board[k][BOARD_WIDTH-1] = p;  board[k][BOARD_WIDTH-2]++;
5768         board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p;  board[BOARD_HEIGHT-1-k][1]++;
5769         j = seed%2 + (seed%2 >= j); seed /= 2;
5770         p = board[0][BOARD_LEFT+j+5]; board[0][BOARD_LEFT+j+5] = EmptySquare; k = PieceToNumber(p);
5771         board[k][BOARD_WIDTH-1] = p;  board[k][BOARD_WIDTH-2]++;
5772         board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p;  board[BOARD_HEIGHT-1-k][1]++;
5773         j = seed%4; seed /= 4; put(board, exoPieces[3],    0, j, ANY);
5774         j = seed%3; seed /= 3; put(board, exoPieces[2],   0, j, ANY);
5775         j = seed%2; seed /= 2; put(board, exoPieces[1], 0, j, ANY);
5776         put(board, exoPieces[0],    0, 0, ANY);
5777         for(i=BOARD_LEFT; i<BOARD_RGHT; i++) board[BOARD_HEIGHT-1][i] = WHITE_TO_BLACK board[0][i];
5778 }
5779
5780 void
5781 InitPosition (int redraw)
5782 {
5783     ChessSquare (* pieces)[BOARD_FILES];
5784     int i, j, pawnRow, overrule,
5785     oldx = gameInfo.boardWidth,
5786     oldy = gameInfo.boardHeight,
5787     oldh = gameInfo.holdingsWidth;
5788     static int oldv;
5789
5790     if(appData.icsActive) shuffleOpenings = FALSE; // [HGM] shuffle: in ICS mode, only shuffle on ICS request
5791
5792     /* [AS] Initialize pv info list [HGM] and game status */
5793     {
5794         for( i=0; i<=framePtr; i++ ) { // [HGM] vari: spare saved variations
5795             pvInfoList[i].depth = 0;
5796             boards[i][EP_STATUS] = EP_NONE;
5797             for( j=0; j<BOARD_FILES-2; j++ ) boards[i][CASTLING][j] = NoRights;
5798         }
5799
5800         initialRulePlies = 0; /* 50-move counter start */
5801
5802         castlingRank[0] = castlingRank[1] = castlingRank[2] = 0;
5803         castlingRank[3] = castlingRank[4] = castlingRank[5] = BOARD_HEIGHT-1;
5804     }
5805
5806
5807     /* [HGM] logic here is completely changed. In stead of full positions */
5808     /* the initialized data only consist of the two backranks. The switch */
5809     /* selects which one we will use, which is than copied to the Board   */
5810     /* initialPosition, which for the rest is initialized by Pawns and    */
5811     /* empty squares. This initial position is then copied to boards[0],  */
5812     /* possibly after shuffling, so that it remains available.            */
5813
5814     gameInfo.holdingsWidth = 0; /* default board sizes */
5815     gameInfo.boardWidth    = 8;
5816     gameInfo.boardHeight   = 8;
5817     gameInfo.holdingsSize  = 0;
5818     nrCastlingRights = -1; /* [HGM] Kludge to indicate default should be used */
5819     for(i=0; i<BOARD_FILES-2; i++)
5820       initialPosition[CASTLING][i] = initialRights[i] = NoRights; /* but no rights yet */
5821     initialPosition[EP_STATUS] = EP_NONE;
5822     SetCharTable(pieceToChar, "PNBRQ...........Kpnbrq...........k");
5823     if(startVariant == gameInfo.variant) // [HGM] nicks: enable nicknames in original variant
5824          SetCharTable(pieceNickName, appData.pieceNickNames);
5825     else SetCharTable(pieceNickName, "............");
5826     pieces = FIDEArray;
5827
5828     switch (gameInfo.variant) {
5829     case VariantFischeRandom:
5830       shuffleOpenings = TRUE;
5831     default:
5832       break;
5833     case VariantShatranj:
5834       pieces = ShatranjArray;
5835       nrCastlingRights = 0;
5836       SetCharTable(pieceToChar, "PN.R.QB...Kpn.r.qb...k");
5837       break;
5838     case VariantMakruk:
5839       pieces = makrukArray;
5840       nrCastlingRights = 0;
5841       startedFromSetupPosition = TRUE;
5842       SetCharTable(pieceToChar, "PN.R.M....SKpn.r.m....sk");
5843       break;
5844     case VariantTwoKings:
5845       pieces = twoKingsArray;
5846       break;
5847     case VariantGrand:
5848       pieces = GrandArray;
5849       nrCastlingRights = 0;
5850       SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
5851       gameInfo.boardWidth = 10;
5852       gameInfo.boardHeight = 10;
5853       gameInfo.holdingsSize = 7;
5854       break;
5855     case VariantCapaRandom:
5856       shuffleOpenings = TRUE;
5857     case VariantCapablanca:
5858       pieces = CapablancaArray;
5859       gameInfo.boardWidth = 10;
5860       SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
5861       break;
5862     case VariantGothic:
5863       pieces = GothicArray;
5864       gameInfo.boardWidth = 10;
5865       SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
5866       break;
5867     case VariantSChess:
5868       SetCharTable(pieceToChar, "PNBRQ..HEKpnbrq..hek");
5869       gameInfo.holdingsSize = 7;
5870       break;
5871     case VariantJanus:
5872       pieces = JanusArray;
5873       gameInfo.boardWidth = 10;
5874       SetCharTable(pieceToChar, "PNBRQ..JKpnbrq..jk");
5875       nrCastlingRights = 6;
5876         initialPosition[CASTLING][0] = initialRights[0] = BOARD_RGHT-1;
5877         initialPosition[CASTLING][1] = initialRights[1] = BOARD_LEFT;
5878         initialPosition[CASTLING][2] = initialRights[2] =(BOARD_WIDTH-1)>>1;
5879         initialPosition[CASTLING][3] = initialRights[3] = BOARD_RGHT-1;
5880         initialPosition[CASTLING][4] = initialRights[4] = BOARD_LEFT;
5881         initialPosition[CASTLING][5] = initialRights[5] =(BOARD_WIDTH-1)>>1;
5882       break;
5883     case VariantFalcon:
5884       pieces = FalconArray;
5885       gameInfo.boardWidth = 10;
5886       SetCharTable(pieceToChar, "PNBRQ.............FKpnbrq.............fk");
5887       break;
5888     case VariantXiangqi:
5889       pieces = XiangqiArray;
5890       gameInfo.boardWidth  = 9;
5891       gameInfo.boardHeight = 10;
5892       nrCastlingRights = 0;
5893       SetCharTable(pieceToChar, "PH.R.AE..K.C.ph.r.ae..k.c.");
5894       break;
5895     case VariantShogi:
5896       pieces = ShogiArray;
5897       gameInfo.boardWidth  = 9;
5898       gameInfo.boardHeight = 9;
5899       gameInfo.holdingsSize = 7;
5900       nrCastlingRights = 0;
5901       SetCharTable(pieceToChar, "PNBRLS...G.++++++Kpnbrls...g.++++++k");
5902       break;
5903     case VariantCourier:
5904       pieces = CourierArray;
5905       gameInfo.boardWidth  = 12;
5906       nrCastlingRights = 0;
5907       SetCharTable(pieceToChar, "PNBR.FE..WMKpnbr.fe..wmk");
5908       break;
5909     case VariantKnightmate:
5910       pieces = KnightmateArray;
5911       SetCharTable(pieceToChar, "P.BRQ.....M.........K.p.brq.....m.........k.");
5912       break;
5913     case VariantSpartan:
5914       pieces = SpartanArray;
5915       SetCharTable(pieceToChar, "PNBRQ................K......lwg.....c...h..k");
5916       break;
5917     case VariantFairy:
5918       pieces = fairyArray;
5919       SetCharTable(pieceToChar, "PNBRQFEACWMOHIJGDVLSUKpnbrqfeacwmohijgdvlsuk");
5920       break;
5921     case VariantGreat:
5922       pieces = GreatArray;
5923       gameInfo.boardWidth = 10;
5924       SetCharTable(pieceToChar, "PN....E...S..HWGMKpn....e...s..hwgmk");
5925       gameInfo.holdingsSize = 8;
5926       break;
5927     case VariantSuper:
5928       pieces = FIDEArray;
5929       SetCharTable(pieceToChar, "PNBRQ..SE.......V.AKpnbrq..se.......v.ak");
5930       gameInfo.holdingsSize = 8;
5931       startedFromSetupPosition = TRUE;
5932       break;
5933     case VariantCrazyhouse:
5934     case VariantBughouse:
5935       pieces = FIDEArray;
5936       SetCharTable(pieceToChar, "PNBRQ.......~~~~Kpnbrq.......~~~~k");
5937       gameInfo.holdingsSize = 5;
5938       break;
5939     case VariantWildCastle:
5940       pieces = FIDEArray;
5941       /* !!?shuffle with kings guaranteed to be on d or e file */
5942       shuffleOpenings = 1;
5943       break;
5944     case VariantNoCastle:
5945       pieces = FIDEArray;
5946       nrCastlingRights = 0;
5947       /* !!?unconstrained back-rank shuffle */
5948       shuffleOpenings = 1;
5949       break;
5950     }
5951
5952     overrule = 0;
5953     if(appData.NrFiles >= 0) {
5954         if(gameInfo.boardWidth != appData.NrFiles) overrule++;
5955         gameInfo.boardWidth = appData.NrFiles;
5956     }
5957     if(appData.NrRanks >= 0) {
5958         gameInfo.boardHeight = appData.NrRanks;
5959     }
5960     if(appData.holdingsSize >= 0) {
5961         i = appData.holdingsSize;
5962         if(i > gameInfo.boardHeight) i = gameInfo.boardHeight;
5963         gameInfo.holdingsSize = i;
5964     }
5965     if(gameInfo.holdingsSize) gameInfo.holdingsWidth = 2;
5966     if(BOARD_HEIGHT > BOARD_RANKS || BOARD_WIDTH > BOARD_FILES)
5967         DisplayFatalError(_("Recompile to support this BOARD_RANKS or BOARD_FILES!"), 0, 2);
5968
5969     pawnRow = gameInfo.boardHeight - 7; /* seems to work in all common variants */
5970     if(pawnRow < 1) pawnRow = 1;
5971     if(gameInfo.variant == VariantMakruk || gameInfo.variant == VariantGrand) pawnRow = 2;
5972
5973     /* User pieceToChar list overrules defaults */
5974     if(appData.pieceToCharTable != NULL)
5975         SetCharTable(pieceToChar, appData.pieceToCharTable);
5976
5977     for( j=0; j<BOARD_WIDTH; j++ ) { ChessSquare s = EmptySquare;
5978
5979         if(j==BOARD_LEFT-1 || j==BOARD_RGHT)
5980             s = (ChessSquare) 0; /* account holding counts in guard band */
5981         for( i=0; i<BOARD_HEIGHT; i++ )
5982             initialPosition[i][j] = s;
5983
5984         if(j < BOARD_LEFT || j >= BOARD_RGHT || overrule) continue;
5985         initialPosition[gameInfo.variant == VariantGrand][j] = pieces[0][j-gameInfo.holdingsWidth];
5986         initialPosition[pawnRow][j] = WhitePawn;
5987         initialPosition[BOARD_HEIGHT-pawnRow-1][j] = gameInfo.variant == VariantSpartan ? BlackLance : BlackPawn;
5988         if(gameInfo.variant == VariantXiangqi) {
5989             if(j&1) {
5990                 initialPosition[pawnRow][j] =
5991                 initialPosition[BOARD_HEIGHT-pawnRow-1][j] = EmptySquare;
5992                 if(j==BOARD_LEFT+1 || j>=BOARD_RGHT-2) {
5993                    initialPosition[2][j] = WhiteCannon;
5994                    initialPosition[BOARD_HEIGHT-3][j] = BlackCannon;
5995                 }
5996             }
5997         }
5998         if(gameInfo.variant == VariantGrand) {
5999             if(j==BOARD_LEFT || j>=BOARD_RGHT-1) {
6000                initialPosition[0][j] = WhiteRook;
6001                initialPosition[BOARD_HEIGHT-1][j] = BlackRook;
6002             }
6003         }
6004         initialPosition[BOARD_HEIGHT-1-(gameInfo.variant == VariantGrand)][j] =  pieces[1][j-gameInfo.holdingsWidth];
6005     }
6006     if( (gameInfo.variant == VariantShogi) && !overrule ) {
6007
6008             j=BOARD_LEFT+1;
6009             initialPosition[1][j] = WhiteBishop;
6010             initialPosition[BOARD_HEIGHT-2][j] = BlackRook;
6011             j=BOARD_RGHT-2;
6012             initialPosition[1][j] = WhiteRook;
6013             initialPosition[BOARD_HEIGHT-2][j] = BlackBishop;
6014     }
6015
6016     if( nrCastlingRights == -1) {
6017         /* [HGM] Build normal castling rights (must be done after board sizing!) */
6018         /*       This sets default castling rights from none to normal corners   */
6019         /* Variants with other castling rights must set them themselves above    */
6020         nrCastlingRights = 6;
6021
6022         initialPosition[CASTLING][0] = initialRights[0] = BOARD_RGHT-1;
6023         initialPosition[CASTLING][1] = initialRights[1] = BOARD_LEFT;
6024         initialPosition[CASTLING][2] = initialRights[2] = BOARD_WIDTH>>1;
6025         initialPosition[CASTLING][3] = initialRights[3] = BOARD_RGHT-1;
6026         initialPosition[CASTLING][4] = initialRights[4] = BOARD_LEFT;
6027         initialPosition[CASTLING][5] = initialRights[5] = BOARD_WIDTH>>1;
6028      }
6029
6030      if(gameInfo.variant == VariantSuper) Prelude(initialPosition);
6031      if(gameInfo.variant == VariantGreat) { // promotion commoners
6032         initialPosition[PieceToNumber(WhiteMan)][BOARD_WIDTH-1] = WhiteMan;
6033         initialPosition[PieceToNumber(WhiteMan)][BOARD_WIDTH-2] = 9;
6034         initialPosition[BOARD_HEIGHT-1-PieceToNumber(WhiteMan)][0] = BlackMan;
6035         initialPosition[BOARD_HEIGHT-1-PieceToNumber(WhiteMan)][1] = 9;
6036      }
6037      if( gameInfo.variant == VariantSChess ) {
6038       initialPosition[1][0] = BlackMarshall;
6039       initialPosition[2][0] = BlackAngel;
6040       initialPosition[6][BOARD_WIDTH-1] = WhiteMarshall;
6041       initialPosition[5][BOARD_WIDTH-1] = WhiteAngel;
6042       initialPosition[1][1] = initialPosition[2][1] = 
6043       initialPosition[6][BOARD_WIDTH-2] = initialPosition[5][BOARD_WIDTH-2] = 1;
6044      }
6045   if (appData.debugMode) {
6046     fprintf(debugFP, "shuffleOpenings = %d\n", shuffleOpenings);
6047   }
6048     if(shuffleOpenings) {
6049         SetUpShuffle(initialPosition, appData.defaultFrcPosition);
6050         startedFromSetupPosition = TRUE;
6051     }
6052     if(startedFromPositionFile) {
6053       /* [HGM] loadPos: use PositionFile for every new game */
6054       CopyBoard(initialPosition, filePosition);
6055       for(i=0; i<nrCastlingRights; i++)
6056           initialRights[i] = filePosition[CASTLING][i];
6057       startedFromSetupPosition = TRUE;
6058     }
6059
6060     CopyBoard(boards[0], initialPosition);
6061
6062     if(oldx != gameInfo.boardWidth ||
6063        oldy != gameInfo.boardHeight ||
6064        oldv != gameInfo.variant ||
6065        oldh != gameInfo.holdingsWidth
6066                                          )
6067             InitDrawingSizes(-2 ,0);
6068
6069     oldv = gameInfo.variant;
6070     if (redraw)
6071       DrawPosition(TRUE, boards[currentMove]);
6072 }
6073
6074 void
6075 SendBoard (ChessProgramState *cps, int moveNum)
6076 {
6077     char message[MSG_SIZ];
6078
6079     if (cps->useSetboard) {
6080       char* fen = PositionToFEN(moveNum, cps->fenOverride);
6081       snprintf(message, MSG_SIZ,"setboard %s\n", fen);
6082       SendToProgram(message, cps);
6083       free(fen);
6084
6085     } else {
6086       ChessSquare *bp;
6087       int i, j, left=0, right=BOARD_WIDTH;
6088       /* Kludge to set black to move, avoiding the troublesome and now
6089        * deprecated "black" command.
6090        */
6091       if (!WhiteOnMove(moveNum)) // [HGM] but better a deprecated command than an illegal move...
6092         SendToProgram(boards[0][1][BOARD_LEFT] == WhitePawn ? "a2a3\n" : "black\n", cps);
6093
6094       if(!cps->extendedEdit) left = BOARD_LEFT, right = BOARD_RGHT; // only board proper
6095
6096       SendToProgram("edit\n", cps);
6097       SendToProgram("#\n", cps);
6098       for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
6099         bp = &boards[moveNum][i][left];
6100         for (j = left; j < right; j++, bp++) {
6101           if(j == BOARD_LEFT-1 || j == BOARD_RGHT) continue;
6102           if ((int) *bp < (int) BlackPawn) {
6103             if(j == BOARD_RGHT+1)
6104                  snprintf(message, MSG_SIZ, "%c@%d\n", PieceToChar(*bp), bp[-1]);
6105             else snprintf(message, MSG_SIZ, "%c%c%c\n", PieceToChar(*bp), AAA + j, ONE + i);
6106             if(message[0] == '+' || message[0] == '~') {
6107               snprintf(message, MSG_SIZ,"%c%c%c+\n",
6108                         PieceToChar((ChessSquare)(DEMOTED *bp)),
6109                         AAA + j, ONE + i);
6110             }
6111             if(cps->alphaRank) { /* [HGM] shogi: translate coords */
6112                 message[1] = BOARD_RGHT   - 1 - j + '1';
6113                 message[2] = BOARD_HEIGHT - 1 - i + 'a';
6114             }
6115             SendToProgram(message, cps);
6116           }
6117         }
6118       }
6119
6120       SendToProgram("c\n", cps);
6121       for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
6122         bp = &boards[moveNum][i][left];
6123         for (j = left; j < right; j++, bp++) {
6124           if(j == BOARD_LEFT-1 || j == BOARD_RGHT) continue;
6125           if (((int) *bp != (int) EmptySquare)
6126               && ((int) *bp >= (int) BlackPawn)) {
6127             if(j == BOARD_LEFT-2)
6128                  snprintf(message, MSG_SIZ, "%c@%d\n", ToUpper(PieceToChar(*bp)), bp[1]);
6129             else snprintf(message,MSG_SIZ, "%c%c%c\n", ToUpper(PieceToChar(*bp)),
6130                     AAA + j, ONE + i);
6131             if(message[0] == '+' || message[0] == '~') {
6132               snprintf(message, MSG_SIZ,"%c%c%c+\n",
6133                         PieceToChar((ChessSquare)(DEMOTED *bp)),
6134                         AAA + j, ONE + i);
6135             }
6136             if(cps->alphaRank) { /* [HGM] shogi: translate coords */
6137                 message[1] = BOARD_RGHT   - 1 - j + '1';
6138                 message[2] = BOARD_HEIGHT - 1 - i + 'a';
6139             }
6140             SendToProgram(message, cps);
6141           }
6142         }
6143       }
6144
6145       SendToProgram(".\n", cps);
6146     }
6147     setboardSpoiledMachineBlack = 0; /* [HGM] assume WB 4.2.7 already solves this after sending setboard */
6148 }
6149
6150 char exclusionHeader[MSG_SIZ];
6151 int exCnt, excludePtr;
6152 typedef struct { int ff, fr, tf, tr, pc, mark; } Exclusion;
6153 static Exclusion excluTab[200];
6154 static char excludeMap[(BOARD_RANKS*BOARD_FILES*BOARD_RANKS*BOARD_FILES+7)/8]; // [HGM] exclude: bitmap for excluced moves
6155
6156 static void
6157 WriteMap (int s)
6158 {
6159     int j;
6160     for(j=0; j<(BOARD_RANKS*BOARD_FILES*BOARD_RANKS*BOARD_FILES+7)/8; j++) excludeMap[j] = s;
6161     exclusionHeader[19] = s ? '-' : '+'; // update tail state
6162 }
6163
6164 static void
6165 ClearMap ()
6166 {
6167     safeStrCpy(exclusionHeader, "exclude: none best +tail                                          \n", MSG_SIZ);
6168     excludePtr = 24; exCnt = 0;
6169     WriteMap(0);
6170 }
6171
6172 static void
6173 UpdateExcludeHeader (int fromY, int fromX, int toY, int toX, char promoChar, char state)
6174 {   // search given move in table of header moves, to know where it is listed (and add if not there), and update state
6175     char buf[2*MOVE_LEN], *p;
6176     Exclusion *e = excluTab;
6177     int i;
6178     for(i=0; i<exCnt; i++)
6179         if(e[i].ff == fromX && e[i].fr == fromY &&
6180            e[i].tf == toX   && e[i].tr == toY && e[i].pc == promoChar) break;
6181     if(i == exCnt) { // was not in exclude list; add it
6182         CoordsToAlgebraic(boards[currentMove], PosFlags(currentMove), fromY, fromX, toY, toX, promoChar, buf);
6183         if(strlen(exclusionHeader + excludePtr) < strlen(buf)) { // no space to write move
6184             if(state != exclusionHeader[19]) exclusionHeader[19] = '*'; // tail is now in mixed state
6185             return; // abort
6186         }
6187         e[i].ff = fromX; e[i].fr = fromY; e[i].tf = toX; e[i].tr = toY; e[i].pc = promoChar;
6188         excludePtr++; e[i].mark = excludePtr++;
6189         for(p=buf; *p; p++) exclusionHeader[excludePtr++] = *p; // copy move
6190         exCnt++;
6191     }
6192     exclusionHeader[e[i].mark] = state;
6193 }
6194
6195 static int
6196 ExcludeOneMove (int fromY, int fromX, int toY, int toX, signed char promoChar, char state)
6197 {   // include or exclude the given move, as specified by state ('+' or '-'), or toggle
6198     char buf[MSG_SIZ];
6199     int j, k;
6200     ChessMove moveType;
6201     if(promoChar == -1) { // kludge to indicate best move
6202         if(!ParseOneMove(lastPV[0], currentMove, &moveType, &fromX, &fromY, &toX, &toY, &promoChar)) // get current best move from last PV
6203             return 1; // if unparsable, abort
6204     }
6205     // update exclusion map (resolving toggle by consulting existing state)
6206     k=(BOARD_FILES*fromY+fromX)*BOARD_RANKS*BOARD_FILES + (BOARD_FILES*toY+toX);
6207     j = k%8; k >>= 3;
6208     if(state == '*') state = (excludeMap[k] & 1<<j ? '+' : '-'); // toggle
6209     if(state == '-' && !promoChar) // only non-promotions get marked as excluded, to allow exclusion of under-promotions
6210          excludeMap[k] |=   1<<j;
6211     else excludeMap[k] &= ~(1<<j);
6212     // update header
6213     UpdateExcludeHeader(fromY, fromX, toY, toX, promoChar, state);
6214     // inform engine
6215     snprintf(buf, MSG_SIZ, "%sclude ", state == '+' ? "in" : "ex");
6216     CoordsToComputerAlgebraic(fromY, fromX, toY, toX, promoChar, buf+8);
6217     SendToProgram(buf, &first);
6218     return (state == '+');
6219 }
6220
6221 static void
6222 ExcludeClick (int index)
6223 {
6224     int i, j;
6225     Exclusion *e = excluTab;
6226     if(index < 25) { // none, best or tail clicked
6227         if(index < 13) { // none: include all
6228             WriteMap(0); // clear map
6229             for(i=0; i<exCnt; i++) exclusionHeader[excluTab[i].mark] = '+'; // and moves
6230             SendToProgram("include all\n", &first); // and inform engine
6231         } else if(index > 18) { // tail
6232             if(exclusionHeader[19] == '-') { // tail was excluded
6233                 SendToProgram("include all\n", &first);
6234                 WriteMap(0); // clear map completely
6235                 // now re-exclude selected moves
6236                 for(i=0; i<exCnt; i++) if(exclusionHeader[e[i].mark] == '-')
6237                     ExcludeOneMove(e[i].fr, e[i].ff, e[i].tr, e[i].tf, e[i].pc, '-');
6238             } else { // tail was included or in mixed state
6239                 SendToProgram("exclude all\n", &first);
6240                 WriteMap(0xFF); // fill map completely
6241                 // now re-include selected moves
6242                 j = 0; // count them
6243                 for(i=0; i<exCnt; i++) if(exclusionHeader[e[i].mark] == '+')
6244                     ExcludeOneMove(e[i].fr, e[i].ff, e[i].tr, e[i].tf, e[i].pc, '+'), j++;
6245                 if(!j) ExcludeOneMove(0, 0, 0, 0, -1, '+'); // if no moves were selected, keep best
6246             }
6247         } else { // best
6248             ExcludeOneMove(0, 0, 0, 0, -1, '-'); // exclude it
6249         }
6250     } else {
6251         for(i=0; i<exCnt; i++) if(i == exCnt-1 || excluTab[i+1].mark > index) {
6252             char *p=exclusionHeader + excluTab[i].mark; // do trust header more than map (promotions!)
6253             ExcludeOneMove(e[i].fr, e[i].ff, e[i].tr, e[i].tf, e[i].pc, *p == '+' ? '-' : '+');
6254             break;
6255         }
6256     }
6257 }
6258
6259 ChessSquare
6260 DefaultPromoChoice (int white)
6261 {
6262     ChessSquare result;
6263     if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
6264         result = WhiteFerz; // no choice
6265     else if(gameInfo.variant == VariantSuicide || gameInfo.variant == VariantGiveaway)
6266         result= WhiteKing; // in Suicide Q is the last thing we want
6267     else if(gameInfo.variant == VariantSpartan)
6268         result = white ? WhiteQueen : WhiteAngel;
6269     else result = WhiteQueen;
6270     if(!white) result = WHITE_TO_BLACK result;
6271     return result;
6272 }
6273
6274 static int autoQueen; // [HGM] oneclick
6275
6276 int
6277 HasPromotionChoice (int fromX, int fromY, int toX, int toY, char *promoChoice, int sweepSelect)
6278 {
6279     /* [HGM] rewritten IsPromotion to only flag promotions that offer a choice */
6280     /* [HGM] add Shogi promotions */
6281     int promotionZoneSize=1, highestPromotingPiece = (int)WhitePawn;
6282     ChessSquare piece;
6283     ChessMove moveType;
6284     Boolean premove;
6285
6286     if(fromX < BOARD_LEFT || fromX >= BOARD_RGHT) return FALSE; // drop
6287     if(toX   < BOARD_LEFT || toX   >= BOARD_RGHT) return FALSE; // move into holdings
6288
6289     if(gameMode == EditPosition || gameInfo.variant == VariantXiangqi || // no promotions
6290       !(fromX >=0 && fromY >= 0 && toX >= 0 && toY >= 0) ) // invalid move
6291         return FALSE;
6292
6293     piece = boards[currentMove][fromY][fromX];
6294     if(gameInfo.variant == VariantShogi) {
6295         promotionZoneSize = BOARD_HEIGHT/3;
6296         highestPromotingPiece = (int)WhiteFerz;
6297     } else if(gameInfo.variant == VariantMakruk || gameInfo.variant == VariantGrand) {
6298         promotionZoneSize = 3;
6299     }
6300
6301     // Treat Lance as Pawn when it is not representing Amazon
6302     if(gameInfo.variant != VariantSuper) {
6303         if(piece == WhiteLance) piece = WhitePawn; else
6304         if(piece == BlackLance) piece = BlackPawn;
6305     }
6306
6307     // next weed out all moves that do not touch the promotion zone at all
6308     if((int)piece >= BlackPawn) {
6309         if(toY >= promotionZoneSize && fromY >= promotionZoneSize)
6310              return FALSE;
6311         highestPromotingPiece = WHITE_TO_BLACK highestPromotingPiece;
6312     } else {
6313         if(  toY < BOARD_HEIGHT - promotionZoneSize &&
6314            fromY < BOARD_HEIGHT - promotionZoneSize) return FALSE;
6315     }
6316
6317     if( (int)piece > highestPromotingPiece ) return FALSE; // non-promoting piece
6318
6319     // weed out mandatory Shogi promotions
6320     if(gameInfo.variant == VariantShogi) {
6321         if(piece >= BlackPawn) {
6322             if(toY == 0 && piece == BlackPawn ||
6323                toY == 0 && piece == BlackQueen ||
6324                toY <= 1 && piece == BlackKnight) {
6325                 *promoChoice = '+';
6326                 return FALSE;
6327             }
6328         } else {
6329             if(toY == BOARD_HEIGHT-1 && piece == WhitePawn ||
6330                toY == BOARD_HEIGHT-1 && piece == WhiteQueen ||
6331                toY >= BOARD_HEIGHT-2 && piece == WhiteKnight) {
6332                 *promoChoice = '+';
6333                 return FALSE;
6334             }
6335         }
6336     }
6337
6338     // weed out obviously illegal Pawn moves
6339     if(appData.testLegality  && (piece == WhitePawn || piece == BlackPawn) ) {
6340         if(toX > fromX+1 || toX < fromX-1) return FALSE; // wide
6341         if(piece == WhitePawn && toY != fromY+1) return FALSE; // deep
6342         if(piece == BlackPawn && toY != fromY-1) return FALSE; // deep
6343         if(fromX != toX && gameInfo.variant == VariantShogi) return FALSE;
6344         // note we are not allowed to test for valid (non-)capture, due to premove
6345     }
6346
6347     // we either have a choice what to promote to, or (in Shogi) whether to promote
6348     if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk) {
6349         *promoChoice = PieceToChar(BlackFerz);  // no choice
6350         return FALSE;
6351     }
6352     // no sense asking what we must promote to if it is going to explode...
6353     if(gameInfo.variant == VariantAtomic && boards[currentMove][toY][toX] != EmptySquare) {
6354         *promoChoice = PieceToChar(BlackQueen); // Queen as good as any
6355         return FALSE;
6356     }
6357     // give caller the default choice even if we will not make it
6358     *promoChoice = ToLower(PieceToChar(defaultPromoChoice));
6359     if(gameInfo.variant == VariantShogi) *promoChoice = (defaultPromoChoice == piece ? '=' : '+');
6360     if(        sweepSelect && gameInfo.variant != VariantGreat
6361                            && gameInfo.variant != VariantGrand
6362                            && gameInfo.variant != VariantSuper) return FALSE;
6363     if(autoQueen) return FALSE; // predetermined
6364
6365     // suppress promotion popup on illegal moves that are not premoves
6366     premove = gameMode == IcsPlayingWhite && !WhiteOnMove(currentMove) ||
6367               gameMode == IcsPlayingBlack &&  WhiteOnMove(currentMove);
6368     if(appData.testLegality && !premove) {
6369         moveType = LegalityTest(boards[currentMove], PosFlags(currentMove),
6370                         fromY, fromX, toY, toX, gameInfo.variant == VariantShogi ? '+' : NULLCHAR);
6371         if(moveType != WhitePromotion && moveType  != BlackPromotion)
6372             return FALSE;
6373     }
6374
6375     return TRUE;
6376 }
6377
6378 int
6379 InPalace (int row, int column)
6380 {   /* [HGM] for Xiangqi */
6381     if( (row < 3 || row > BOARD_HEIGHT-4) &&
6382          column < (BOARD_WIDTH + 4)/2 &&
6383          column > (BOARD_WIDTH - 5)/2 ) return TRUE;
6384     return FALSE;
6385 }
6386
6387 int
6388 PieceForSquare (int x, int y)
6389 {
6390   if (x < 0 || x >= BOARD_WIDTH || y < 0 || y >= BOARD_HEIGHT)
6391      return -1;
6392   else
6393      return boards[currentMove][y][x];
6394 }
6395
6396 int
6397 OKToStartUserMove (int x, int y)
6398 {
6399     ChessSquare from_piece;
6400     int white_piece;
6401
6402     if (matchMode) return FALSE;
6403     if (gameMode == EditPosition) return TRUE;
6404
6405     if (x >= 0 && y >= 0)
6406       from_piece = boards[currentMove][y][x];
6407     else
6408       from_piece = EmptySquare;
6409
6410     if (from_piece == EmptySquare) return FALSE;
6411
6412     white_piece = (int)from_piece >= (int)WhitePawn &&
6413       (int)from_piece < (int)BlackPawn; /* [HGM] can be > King! */
6414
6415     switch (gameMode) {
6416       case AnalyzeFile:
6417       case TwoMachinesPlay:
6418       case EndOfGame:
6419         return FALSE;
6420
6421       case IcsObserving:
6422       case IcsIdle:
6423         return FALSE;
6424
6425       case MachinePlaysWhite:
6426       case IcsPlayingBlack:
6427         if (appData.zippyPlay) return FALSE;
6428         if (white_piece) {
6429             DisplayMoveError(_("You are playing Black"));
6430             return FALSE;
6431         }
6432         break;
6433
6434       case MachinePlaysBlack:
6435       case IcsPlayingWhite:
6436         if (appData.zippyPlay) return FALSE;
6437         if (!white_piece) {
6438             DisplayMoveError(_("You are playing White"));
6439             return FALSE;
6440         }
6441         break;
6442
6443       case PlayFromGameFile:
6444             if(!shiftKey || !appData.variations) return FALSE; // [HGM] allow starting variation in this mode
6445       case EditGame:
6446         if (!white_piece && WhiteOnMove(currentMove)) {
6447             DisplayMoveError(_("It is White's turn"));
6448             return FALSE;
6449         }
6450         if (white_piece && !WhiteOnMove(currentMove)) {
6451             DisplayMoveError(_("It is Black's turn"));
6452             return FALSE;
6453         }
6454         if (cmailMsgLoaded && (currentMove < cmailOldMove)) {
6455             /* Editing correspondence game history */
6456             /* Could disallow this or prompt for confirmation */
6457             cmailOldMove = -1;
6458         }
6459         break;
6460
6461       case BeginningOfGame:
6462         if (appData.icsActive) return FALSE;
6463         if (!appData.noChessProgram) {
6464             if (!white_piece) {
6465                 DisplayMoveError(_("You are playing White"));
6466                 return FALSE;
6467             }
6468         }
6469         break;
6470
6471       case Training:
6472         if (!white_piece && WhiteOnMove(currentMove)) {
6473             DisplayMoveError(_("It is White's turn"));
6474             return FALSE;
6475         }
6476         if (white_piece && !WhiteOnMove(currentMove)) {
6477             DisplayMoveError(_("It is Black's turn"));
6478             return FALSE;
6479         }
6480         break;
6481
6482       default:
6483       case IcsExamining:
6484         break;
6485     }
6486     if (currentMove != forwardMostMove && gameMode != AnalyzeMode
6487         && gameMode != EditGame // [HGM] vari: treat as AnalyzeMode
6488         && gameMode != PlayFromGameFile // [HGM] as EditGame, with protected main line
6489         && gameMode != AnalyzeFile && gameMode != Training) {
6490         DisplayMoveError(_("Displayed position is not current"));
6491         return FALSE;
6492     }
6493     return TRUE;
6494 }
6495
6496 Boolean
6497 OnlyMove (int *x, int *y, Boolean captures) 
6498 {
6499     DisambiguateClosure cl;
6500     if (appData.zippyPlay || !appData.testLegality) return FALSE;
6501     switch(gameMode) {
6502       case MachinePlaysBlack:
6503       case IcsPlayingWhite:
6504       case BeginningOfGame:
6505         if(!WhiteOnMove(currentMove)) return FALSE;
6506         break;
6507       case MachinePlaysWhite:
6508       case IcsPlayingBlack:
6509         if(WhiteOnMove(currentMove)) return FALSE;
6510         break;
6511       case EditGame:
6512         break;
6513       default:
6514         return FALSE;
6515     }
6516     cl.pieceIn = EmptySquare;
6517     cl.rfIn = *y;
6518     cl.ffIn = *x;
6519     cl.rtIn = -1;
6520     cl.ftIn = -1;
6521     cl.promoCharIn = NULLCHAR;
6522     Disambiguate(boards[currentMove], PosFlags(currentMove), &cl);
6523     if( cl.kind == NormalMove ||
6524         cl.kind == AmbiguousMove && captures && cl.captures == 1 ||
6525         cl.kind == WhitePromotion || cl.kind == BlackPromotion ||
6526         cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) {
6527       fromX = cl.ff;
6528       fromY = cl.rf;
6529       *x = cl.ft;
6530       *y = cl.rt;
6531       return TRUE;
6532     }
6533     if(cl.kind != ImpossibleMove) return FALSE;
6534     cl.pieceIn = EmptySquare;
6535     cl.rfIn = -1;
6536     cl.ffIn = -1;
6537     cl.rtIn = *y;
6538     cl.ftIn = *x;
6539     cl.promoCharIn = NULLCHAR;
6540     Disambiguate(boards[currentMove], PosFlags(currentMove), &cl);
6541     if( cl.kind == NormalMove ||
6542         cl.kind == AmbiguousMove && captures && cl.captures == 1 ||
6543         cl.kind == WhitePromotion || cl.kind == BlackPromotion ||
6544         cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) {
6545       fromX = cl.ff;
6546       fromY = cl.rf;
6547       *x = cl.ft;
6548       *y = cl.rt;
6549       autoQueen = TRUE; // act as if autoQueen on when we click to-square
6550       return TRUE;
6551     }
6552     return FALSE;
6553 }
6554
6555 FILE *lastLoadGameFP = NULL, *lastLoadPositionFP = NULL;
6556 int lastLoadGameNumber = 0, lastLoadPositionNumber = 0;
6557 int lastLoadGameUseList = FALSE;
6558 char lastLoadGameTitle[MSG_SIZ], lastLoadPositionTitle[MSG_SIZ];
6559 ChessMove lastLoadGameStart = EndOfFile;
6560 int doubleClick;
6561
6562 void
6563 UserMoveEvent(int fromX, int fromY, int toX, int toY, int promoChar)
6564 {
6565     ChessMove moveType;
6566     ChessSquare pdown, pup;
6567     int ff=fromX, rf=fromY, ft=toX, rt=toY;
6568
6569
6570     /* Check if the user is playing in turn.  This is complicated because we
6571        let the user "pick up" a piece before it is his turn.  So the piece he
6572        tried to pick up may have been captured by the time he puts it down!
6573        Therefore we use the color the user is supposed to be playing in this
6574        test, not the color of the piece that is currently on the starting
6575        square---except in EditGame mode, where the user is playing both
6576        sides; fortunately there the capture race can't happen.  (It can
6577        now happen in IcsExamining mode, but that's just too bad.  The user
6578        will get a somewhat confusing message in that case.)
6579        */
6580
6581     switch (gameMode) {
6582       case AnalyzeFile:
6583       case TwoMachinesPlay:
6584       case EndOfGame:
6585       case IcsObserving:
6586       case IcsIdle:
6587         /* We switched into a game mode where moves are not accepted,
6588            perhaps while the mouse button was down. */
6589         return;
6590
6591       case MachinePlaysWhite:
6592         /* User is moving for Black */
6593         if (WhiteOnMove(currentMove)) {
6594             DisplayMoveError(_("It is White's turn"));
6595             return;
6596         }
6597         break;
6598
6599       case MachinePlaysBlack:
6600         /* User is moving for White */
6601         if (!WhiteOnMove(currentMove)) {
6602             DisplayMoveError(_("It is Black's turn"));
6603             return;
6604         }
6605         break;
6606
6607       case PlayFromGameFile:
6608             if(!shiftKey ||!appData.variations) return; // [HGM] only variations
6609       case EditGame:
6610       case IcsExamining:
6611       case BeginningOfGame:
6612       case AnalyzeMode:
6613       case Training:
6614         if(fromY == DROP_RANK) break; // [HGM] drop moves (entered through move type-in) are automatically assigned to side-to-move
6615         if ((int) boards[currentMove][fromY][fromX] >= (int) BlackPawn &&
6616             (int) boards[currentMove][fromY][fromX] < (int) EmptySquare) {
6617             /* User is moving for Black */
6618             if (WhiteOnMove(currentMove)) {
6619                 DisplayMoveError(_("It is White's turn"));
6620                 return;
6621             }
6622         } else {
6623             /* User is moving for White */
6624             if (!WhiteOnMove(currentMove)) {
6625                 DisplayMoveError(_("It is Black's turn"));
6626                 return;
6627             }
6628         }
6629         break;
6630
6631       case IcsPlayingBlack:
6632         /* User is moving for Black */
6633         if (WhiteOnMove(currentMove)) {
6634             if (!appData.premove) {
6635                 DisplayMoveError(_("It is White's turn"));
6636             } else if (toX >= 0 && toY >= 0) {
6637                 premoveToX = toX;
6638                 premoveToY = toY;
6639                 premoveFromX = fromX;
6640                 premoveFromY = fromY;
6641                 premovePromoChar = promoChar;
6642                 gotPremove = 1;
6643                 if (appData.debugMode)
6644                     fprintf(debugFP, "Got premove: fromX %d,"
6645                             "fromY %d, toX %d, toY %d\n",
6646                             fromX, fromY, toX, toY);
6647             }
6648             return;
6649         }
6650         break;
6651
6652       case IcsPlayingWhite:
6653         /* User is moving for White */
6654         if (!WhiteOnMove(currentMove)) {
6655             if (!appData.premove) {
6656                 DisplayMoveError(_("It is Black's turn"));
6657             } else if (toX >= 0 && toY >= 0) {
6658                 premoveToX = toX;
6659                 premoveToY = toY;
6660                 premoveFromX = fromX;
6661                 premoveFromY = fromY;
6662                 premovePromoChar = promoChar;
6663                 gotPremove = 1;
6664                 if (appData.debugMode)
6665                     fprintf(debugFP, "Got premove: fromX %d,"
6666                             "fromY %d, toX %d, toY %d\n",
6667                             fromX, fromY, toX, toY);
6668             }
6669             return;
6670         }
6671         break;
6672
6673       default:
6674         break;
6675
6676       case EditPosition:
6677         /* EditPosition, empty square, or different color piece;
6678            click-click move is possible */
6679         if (toX == -2 || toY == -2) {
6680             boards[0][fromY][fromX] = EmptySquare;
6681             DrawPosition(FALSE, boards[currentMove]);
6682             return;
6683         } else if (toX >= 0 && toY >= 0) {
6684             boards[0][toY][toX] = boards[0][fromY][fromX];
6685             if(fromX == BOARD_LEFT-2) { // handle 'moves' out of holdings
6686                 if(boards[0][fromY][0] != EmptySquare) {
6687                     if(boards[0][fromY][1]) boards[0][fromY][1]--;
6688                     if(boards[0][fromY][1] == 0)  boards[0][fromY][0] = EmptySquare;
6689                 }
6690             } else
6691             if(fromX == BOARD_RGHT+1) {
6692                 if(boards[0][fromY][BOARD_WIDTH-1] != EmptySquare) {
6693                     if(boards[0][fromY][BOARD_WIDTH-2]) boards[0][fromY][BOARD_WIDTH-2]--;
6694                     if(boards[0][fromY][BOARD_WIDTH-2] == 0)  boards[0][fromY][BOARD_WIDTH-1] = EmptySquare;
6695                 }
6696             } else
6697             boards[0][fromY][fromX] = gatingPiece;
6698             DrawPosition(FALSE, boards[currentMove]);
6699             return;
6700         }
6701         return;
6702     }
6703
6704     if(toX < 0 || toY < 0) return;
6705     pdown = boards[currentMove][fromY][fromX];
6706     pup = boards[currentMove][toY][toX];
6707
6708     /* [HGM] If move started in holdings, it means a drop. Convert to standard form */
6709     if( (fromX == BOARD_LEFT-2 || fromX == BOARD_RGHT+1) && fromY != DROP_RANK ) {
6710          if( pup != EmptySquare ) return;
6711          moveType = WhiteOnMove(currentMove) ? WhiteDrop : BlackDrop;
6712            if(appData.debugMode) fprintf(debugFP, "Drop move %d, curr=%d, x=%d,y=%d, p=%d\n", 
6713                 moveType, currentMove, fromX, fromY, boards[currentMove][fromY][fromX]);
6714            // holdings might not be sent yet in ICS play; we have to figure out which piece belongs here
6715            if(fromX == 0) fromY = BOARD_HEIGHT-1 - fromY; // black holdings upside-down
6716            fromX = fromX ? WhitePawn : BlackPawn; // first piece type in selected holdings
6717            while(PieceToChar(fromX) == '.' || PieceToNumber(fromX) != fromY && fromX != (int) EmptySquare) fromX++; 
6718          fromY = DROP_RANK;
6719     }
6720
6721     /* [HGM] always test for legality, to get promotion info */
6722     moveType = LegalityTest(boards[currentMove], PosFlags(currentMove),
6723                                          fromY, fromX, toY, toX, promoChar);
6724
6725     if(fromY == DROP_RANK && fromX == EmptySquare && (gameMode == AnalyzeMode || gameMode == EditGame)) moveType = NormalMove;
6726
6727     /* [HGM] but possibly ignore an IllegalMove result */
6728     if (appData.testLegality) {
6729         if (moveType == IllegalMove || moveType == ImpossibleMove) {
6730             DisplayMoveError(_("Illegal move"));
6731             return;
6732         }
6733     }
6734
6735     if(doubleClick) { // [HGM] exclude: move entered with double-click on from square is for exclusion, not playing
6736         if(ExcludeOneMove(fromY, fromX, toY, toX, promoChar, '*')) // toggle
6737              ClearPremoveHighlights(); // was included
6738         else ClearHighlights(), SetPremoveHighlights(ff, rf, ft, rt); // exclusion indicated  by premove highlights
6739         return;
6740     }
6741
6742     FinishMove(moveType, fromX, fromY, toX, toY, promoChar);
6743 }
6744
6745 /* Common tail of UserMoveEvent and DropMenuEvent */
6746 int
6747 FinishMove (ChessMove moveType, int fromX, int fromY, int toX, int toY, int promoChar)
6748 {
6749     char *bookHit = 0;
6750
6751     if((gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand) && promoChar != NULLCHAR) {
6752         // [HGM] superchess: suppress promotions to non-available piece (but P always allowed)
6753         int k = PieceToNumber(CharToPiece(ToUpper(promoChar)));
6754         if(WhiteOnMove(currentMove)) {
6755             if(!boards[currentMove][k][BOARD_WIDTH-2]) return 0;
6756         } else {
6757             if(!boards[currentMove][BOARD_HEIGHT-1-k][1]) return 0;
6758         }
6759     }
6760
6761     /* [HGM] <popupFix> kludge to avoid having to know the exact promotion
6762        move type in caller when we know the move is a legal promotion */
6763     if(moveType == NormalMove && promoChar)
6764         moveType = WhiteOnMove(currentMove) ? WhitePromotion : BlackPromotion;
6765
6766     /* [HGM] <popupFix> The following if has been moved here from
6767        UserMoveEvent(). Because it seemed to belong here (why not allow
6768        piece drops in training games?), and because it can only be
6769        performed after it is known to what we promote. */
6770     if (gameMode == Training) {
6771       /* compare the move played on the board to the next move in the
6772        * game. If they match, display the move and the opponent's response.
6773        * If they don't match, display an error message.
6774        */
6775       int saveAnimate;
6776       Board testBoard;
6777       CopyBoard(testBoard, boards[currentMove]);
6778       ApplyMove(fromX, fromY, toX, toY, promoChar, testBoard);
6779
6780       if (CompareBoards(testBoard, boards[currentMove+1])) {
6781         ForwardInner(currentMove+1);
6782
6783         /* Autoplay the opponent's response.
6784          * if appData.animate was TRUE when Training mode was entered,
6785          * the response will be animated.
6786          */
6787         saveAnimate = appData.animate;
6788         appData.animate = animateTraining;
6789         ForwardInner(currentMove+1);
6790         appData.animate = saveAnimate;
6791
6792         /* check for the end of the game */
6793         if (currentMove >= forwardMostMove) {
6794           gameMode = PlayFromGameFile;
6795           ModeHighlight();
6796           SetTrainingModeOff();
6797           DisplayInformation(_("End of game"));
6798         }
6799       } else {
6800         DisplayError(_("Incorrect move"), 0);
6801       }
6802       return 1;
6803     }
6804
6805   /* Ok, now we know that the move is good, so we can kill
6806      the previous line in Analysis Mode */
6807   if ((gameMode == AnalyzeMode || gameMode == EditGame || gameMode == PlayFromGameFile && appData.variations && shiftKey)
6808                                 && currentMove < forwardMostMove) {
6809     if(appData.variations && shiftKey) PushTail(currentMove, forwardMostMove); // [HGM] vari: save tail of game
6810     else forwardMostMove = currentMove;
6811   }
6812
6813   ClearMap();
6814
6815   /* If we need the chess program but it's dead, restart it */
6816   ResurrectChessProgram();
6817
6818   /* A user move restarts a paused game*/
6819   if (pausing)
6820     PauseEvent();
6821
6822   thinkOutput[0] = NULLCHAR;
6823
6824   MakeMove(fromX, fromY, toX, toY, promoChar); /*updates forwardMostMove*/
6825
6826   if(Adjudicate(NULL)) { // [HGM] adjudicate: take care of automatic game end
6827     ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6828     return 1;
6829   }
6830
6831   if (gameMode == BeginningOfGame) {
6832     if (appData.noChessProgram) {
6833       gameMode = EditGame;
6834       SetGameInfo();
6835     } else {
6836       char buf[MSG_SIZ];
6837       gameMode = MachinePlaysBlack;
6838       StartClocks();
6839       SetGameInfo();
6840       snprintf(buf, MSG_SIZ, "%s %s %s", gameInfo.white, _("vs."), gameInfo.black);
6841       DisplayTitle(buf);
6842       if (first.sendName) {
6843         snprintf(buf, MSG_SIZ,"name %s\n", gameInfo.white);
6844         SendToProgram(buf, &first);
6845       }
6846       StartClocks();
6847     }
6848     ModeHighlight();
6849   }
6850
6851   /* Relay move to ICS or chess engine */
6852   if (appData.icsActive) {
6853     if (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
6854         gameMode == IcsExamining) {
6855       if(userOfferedDraw && (signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
6856         SendToICS(ics_prefix); // [HGM] drawclaim: send caim and move on one line for FICS
6857         SendToICS("draw ");
6858         SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
6859       }
6860       // also send plain move, in case ICS does not understand atomic claims
6861       SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
6862       ics_user_moved = 1;
6863     }
6864   } else {
6865     if (first.sendTime && (gameMode == BeginningOfGame ||
6866                            gameMode == MachinePlaysWhite ||
6867                            gameMode == MachinePlaysBlack)) {
6868       SendTimeRemaining(&first, gameMode != MachinePlaysBlack);
6869     }
6870     if (gameMode != EditGame && gameMode != PlayFromGameFile) {
6871          // [HGM] book: if program might be playing, let it use book
6872         bookHit = SendMoveToBookUser(forwardMostMove-1, &first, FALSE);
6873         first.maybeThinking = TRUE;
6874     } else if(fromY == DROP_RANK && fromX == EmptySquare) {
6875         if(!first.useSetboard) SendToProgram("undo\n", &first); // kludge to change stm in engines that do not support setboard
6876         SendBoard(&first, currentMove+1);
6877     } else SendMoveToProgram(forwardMostMove-1, &first);
6878     if (currentMove == cmailOldMove + 1) {
6879       cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
6880     }
6881   }
6882
6883   ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6884
6885   switch (gameMode) {
6886   case EditGame:
6887     if(appData.testLegality)
6888     switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
6889     case MT_NONE:
6890     case MT_CHECK:
6891       break;
6892     case MT_CHECKMATE:
6893     case MT_STAINMATE:
6894       if (WhiteOnMove(currentMove)) {
6895         GameEnds(BlackWins, "Black mates", GE_PLAYER);
6896       } else {
6897         GameEnds(WhiteWins, "White mates", GE_PLAYER);
6898       }
6899       break;
6900     case MT_STALEMATE:
6901       GameEnds(GameIsDrawn, "Stalemate", GE_PLAYER);
6902       break;
6903     }
6904     break;
6905
6906   case MachinePlaysBlack:
6907   case MachinePlaysWhite:
6908     /* disable certain menu options while machine is thinking */
6909     SetMachineThinkingEnables();
6910     break;
6911
6912   default:
6913     break;
6914   }
6915
6916   userOfferedDraw = FALSE; // [HGM] drawclaim: after move made, and tested for claimable draw
6917   promoDefaultAltered = FALSE; // [HGM] fall back on default choice
6918
6919   if(bookHit) { // [HGM] book: simulate book reply
6920         static char bookMove[MSG_SIZ]; // a bit generous?
6921
6922         programStats.nodes = programStats.depth = programStats.time =
6923         programStats.score = programStats.got_only_move = 0;
6924         sprintf(programStats.movelist, "%s (xbook)", bookHit);
6925
6926         safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
6927         strcat(bookMove, bookHit);
6928         HandleMachineMove(bookMove, &first);
6929   }
6930   return 1;
6931 }
6932
6933 void
6934 Mark (Board board, int flags, ChessMove kind, int rf, int ff, int rt, int ft, VOIDSTAR closure)
6935 {
6936     typedef char Markers[BOARD_RANKS][BOARD_FILES];
6937     Markers *m = (Markers *) closure;
6938     if(rf == fromY && ff == fromX)
6939         (*m)[rt][ft] = 1 + (board[rt][ft] != EmptySquare
6940                          || kind == WhiteCapturesEnPassant
6941                          || kind == BlackCapturesEnPassant);
6942     else if(flags & F_MANDATORY_CAPTURE && board[rt][ft] != EmptySquare) (*m)[rt][ft] = 3;
6943 }
6944
6945 void
6946 MarkTargetSquares (int clear)
6947 {
6948   int x, y;
6949   if(clear) // no reason to ever suppress clearing
6950     for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) marker[y][x] = 0;
6951   if(!appData.markers || !appData.highlightDragging || appData.icsActive && gameInfo.variant < VariantShogi ||
6952      !appData.testLegality || gameMode == EditPosition) return;
6953   if(!clear) {
6954     int capt = 0;
6955     GenLegal(boards[currentMove], PosFlags(currentMove), Mark, (void*) marker, EmptySquare);
6956     if(PosFlags(0) & F_MANDATORY_CAPTURE) {
6957       for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) if(marker[y][x]>1) capt++;
6958       if(capt)
6959       for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) if(marker[y][x] == 1) marker[y][x] = 0;
6960     }
6961   }
6962   DrawPosition(FALSE, NULL);
6963 }
6964
6965 int
6966 Explode (Board board, int fromX, int fromY, int toX, int toY)
6967 {
6968     if(gameInfo.variant == VariantAtomic &&
6969        (board[toY][toX] != EmptySquare ||                     // capture?
6970         toX != fromX && (board[fromY][fromX] == WhitePawn ||  // e.p. ?
6971                          board[fromY][fromX] == BlackPawn   )
6972       )) {
6973         AnimateAtomicCapture(board, fromX, fromY, toX, toY);
6974         return TRUE;
6975     }
6976     return FALSE;
6977 }
6978
6979 ChessSquare gatingPiece = EmptySquare; // exported to front-end, for dragging
6980
6981 int
6982 CanPromote (ChessSquare piece, int y)
6983 {
6984         if(gameMode == EditPosition) return FALSE; // no promotions when editing position
6985         // some variants have fixed promotion piece, no promotion at all, or another selection mechanism
6986         if(gameInfo.variant == VariantShogi    || gameInfo.variant == VariantXiangqi ||
6987            gameInfo.variant == VariantSuper    || gameInfo.variant == VariantGreat   ||
6988            gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier ||
6989                                                   gameInfo.variant == VariantMakruk) return FALSE;
6990         return (piece == BlackPawn && y == 1 ||
6991                 piece == WhitePawn && y == BOARD_HEIGHT-2 ||
6992                 piece == BlackLance && y == 1 ||
6993                 piece == WhiteLance && y == BOARD_HEIGHT-2 );
6994 }
6995
6996 void
6997 LeftClick (ClickType clickType, int xPix, int yPix)
6998 {
6999     int x, y;
7000     Boolean saveAnimate;
7001     static int second = 0, promotionChoice = 0, clearFlag = 0, sweepSelecting = 0;
7002     char promoChoice = NULLCHAR;
7003     ChessSquare piece;
7004     static TimeMark lastClickTime, prevClickTime;
7005
7006     if(SeekGraphClick(clickType, xPix, yPix, 0)) return;
7007
7008     prevClickTime = lastClickTime; GetTimeMark(&lastClickTime);
7009
7010     if (clickType == Press) ErrorPopDown();
7011
7012     x = EventToSquare(xPix, BOARD_WIDTH);
7013     y = EventToSquare(yPix, BOARD_HEIGHT);
7014     if (!flipView && y >= 0) {
7015         y = BOARD_HEIGHT - 1 - y;
7016     }
7017     if (flipView && x >= 0) {
7018         x = BOARD_WIDTH - 1 - x;
7019     }
7020
7021     if(promoSweep != EmptySquare) { // up-click during sweep-select of promo-piece
7022         defaultPromoChoice = promoSweep;
7023         promoSweep = EmptySquare;   // terminate sweep
7024         promoDefaultAltered = TRUE;
7025         if(!selectFlag && !sweepSelecting && (x != toX || y != toY)) x = fromX, y = fromY; // and fake up-click on same square if we were still selecting
7026     }
7027
7028     if(promotionChoice) { // we are waiting for a click to indicate promotion piece
7029         if(clickType == Release) return; // ignore upclick of click-click destination
7030         promotionChoice = FALSE; // only one chance: if click not OK it is interpreted as cancel
7031         if(appData.debugMode) fprintf(debugFP, "promotion click, x=%d, y=%d\n", x, y);
7032         if(gameInfo.holdingsWidth &&
7033                 (WhiteOnMove(currentMove)
7034                         ? x == BOARD_WIDTH-1 && y < gameInfo.holdingsSize && y >= 0
7035                         : x == 0 && y >= BOARD_HEIGHT - gameInfo.holdingsSize && y < BOARD_HEIGHT) ) {
7036             // click in right holdings, for determining promotion piece
7037             ChessSquare p = boards[currentMove][y][x];
7038             if(appData.debugMode) fprintf(debugFP, "square contains %d\n", (int)p);
7039             if(p == WhitePawn || p == BlackPawn) p = EmptySquare; // [HGM] Pawns could be valid as deferral
7040             if(p != EmptySquare || gameInfo.variant == VariantGrand && toY != 0 && toY != BOARD_HEIGHT-1) { // [HGM] grand: empty square means defer
7041                 FinishMove(NormalMove, fromX, fromY, toX, toY, p==EmptySquare ? NULLCHAR : ToLower(PieceToChar(p)));
7042                 fromX = fromY = -1;
7043                 return;
7044             }
7045         }
7046         DrawPosition(FALSE, boards[currentMove]);
7047         return;
7048     }
7049
7050     /* [HGM] holdings: next 5 lines: ignore all clicks between board and holdings */
7051     if(clickType == Press
7052             && ( x == BOARD_LEFT-1 || x == BOARD_RGHT
7053               || x == BOARD_LEFT-2 && y < BOARD_HEIGHT-gameInfo.holdingsSize
7054               || x == BOARD_RGHT+1 && y >= gameInfo.holdingsSize) )
7055         return;
7056
7057     if(gotPremove && x == premoveFromX && y == premoveFromY && clickType == Release) {
7058         // could be static click on premove from-square: abort premove
7059         gotPremove = 0;
7060         ClearPremoveHighlights();
7061     }
7062
7063     if(clickType == Press && fromX == x && fromY == y && promoDefaultAltered && SubtractTimeMarks(&lastClickTime, &prevClickTime) >= 200)
7064         fromX = fromY = -1; // second click on piece after altering default promo piece treated as first click
7065
7066     if(!promoDefaultAltered) { // determine default promotion piece, based on the side the user is moving for
7067         int side = (gameMode == IcsPlayingWhite || gameMode == MachinePlaysBlack ||
7068                     gameMode != MachinePlaysWhite && gameMode != IcsPlayingBlack && WhiteOnMove(currentMove));
7069         defaultPromoChoice = DefaultPromoChoice(side);
7070     }
7071
7072     autoQueen = appData.alwaysPromoteToQueen;
7073
7074     if (fromX == -1) {
7075       int originalY = y;
7076       gatingPiece = EmptySquare;
7077       if (clickType != Press) {
7078         if(dragging) { // [HGM] from-square must have been reset due to game end since last press
7079             DragPieceEnd(xPix, yPix); dragging = 0;
7080             DrawPosition(FALSE, NULL);
7081         }
7082         return;
7083       }
7084       doubleClick = FALSE;
7085       fromX = x; fromY = y; toX = toY = -1;
7086       if(!appData.oneClick || !OnlyMove(&x, &y, FALSE) ||
7087          // even if only move, we treat as normal when this would trigger a promotion popup, to allow sweep selection
7088          appData.sweepSelect && CanPromote(boards[currentMove][fromY][fromX], fromY) && originalY != y) {
7089             /* First square */
7090             if (OKToStartUserMove(fromX, fromY)) {
7091                 second = 0;
7092                 MarkTargetSquares(0);
7093                 if(gameMode == EditPosition && controlKey) gatingPiece = boards[currentMove][fromY][fromX];
7094                 DragPieceBegin(xPix, yPix, FALSE); dragging = 1;
7095                 if(appData.sweepSelect && CanPromote(piece = boards[currentMove][fromY][fromX], fromY)) {
7096                     promoSweep = defaultPromoChoice;
7097                     selectFlag = 0; lastX = xPix; lastY = yPix;
7098                     Sweep(0); // Pawn that is going to promote: preview promotion piece
7099                     DisplayMessage("", _("Pull pawn backwards to under-promote"));
7100                 }
7101                 if (appData.highlightDragging) {
7102                     SetHighlights(fromX, fromY, -1, -1);
7103                 } else {
7104                     ClearHighlights();
7105                 }
7106             } else fromX = fromY = -1;
7107             return;
7108         }
7109     }
7110
7111     /* fromX != -1 */
7112     if (clickType == Press && gameMode != EditPosition) {
7113         ChessSquare fromP;
7114         ChessSquare toP;
7115         int frc;
7116
7117         // ignore off-board to clicks
7118         if(y < 0 || x < 0) return;
7119
7120         /* Check if clicking again on the same color piece */
7121         fromP = boards[currentMove][fromY][fromX];
7122         toP = boards[currentMove][y][x];
7123         frc = gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom || gameInfo.variant == VariantSChess;
7124         if ((WhitePawn <= fromP && fromP <= WhiteKing &&
7125              WhitePawn <= toP && toP <= WhiteKing &&
7126              !(fromP == WhiteKing && toP == WhiteRook && frc) &&
7127              !(fromP == WhiteRook && toP == WhiteKing && frc)) ||
7128             (BlackPawn <= fromP && fromP <= BlackKing &&
7129              BlackPawn <= toP && toP <= BlackKing &&
7130              !(fromP == BlackRook && toP == BlackKing && frc) && // allow also RxK as FRC castling
7131              !(fromP == BlackKing && toP == BlackRook && frc))) {
7132             /* Clicked again on same color piece -- changed his mind */
7133             second = (x == fromX && y == fromY);
7134             if(second && gameMode == AnalyzeMode && SubtractTimeMarks(&lastClickTime, &prevClickTime) < 200) {
7135                 second = FALSE; // first double-click rather than scond click
7136                 doubleClick = first.excludeMoves; // used by UserMoveEvent to recognize exclude moves
7137             }
7138             promoDefaultAltered = FALSE;
7139             MarkTargetSquares(1);
7140            if(!second || appData.oneClick && !OnlyMove(&x, &y, TRUE)) {
7141             if (appData.highlightDragging) {
7142                 SetHighlights(x, y, -1, -1);
7143             } else {
7144                 ClearHighlights();
7145             }
7146             if (OKToStartUserMove(x, y)) {
7147                 if(gameInfo.variant == VariantSChess && // S-Chess: back-rank piece selected after holdings means gating
7148                   (fromX == BOARD_LEFT-2 || fromX == BOARD_RGHT+1) &&
7149                y == (toP < BlackPawn ? 0 : BOARD_HEIGHT-1))
7150                  gatingPiece = boards[currentMove][fromY][fromX];
7151                 else gatingPiece = doubleClick ? fromP : EmptySquare;
7152                 fromX = x;
7153                 fromY = y; dragging = 1;
7154                 MarkTargetSquares(0);
7155                 DragPieceBegin(xPix, yPix, FALSE);
7156                 if(appData.sweepSelect && CanPromote(piece = boards[currentMove][y][x], y)) {
7157                     promoSweep = defaultPromoChoice;
7158                     selectFlag = 0; lastX = xPix; lastY = yPix;
7159                     Sweep(0); // Pawn that is going to promote: preview promotion piece
7160                 }
7161             }
7162            }
7163            if(x == fromX && y == fromY) return; // if OnlyMove altered (x,y) we go on
7164            second = FALSE; 
7165         }
7166         // ignore clicks on holdings
7167         if(x < BOARD_LEFT || x >= BOARD_RGHT) return;
7168     }
7169
7170     if (clickType == Release && x == fromX && y == fromY) {
7171         DragPieceEnd(xPix, yPix); dragging = 0;
7172         if(clearFlag) {
7173             // a deferred attempt to click-click move an empty square on top of a piece
7174             boards[currentMove][y][x] = EmptySquare;
7175             ClearHighlights();
7176             DrawPosition(FALSE, boards[currentMove]);
7177             fromX = fromY = -1; clearFlag = 0;
7178             return;
7179         }
7180         if (appData.animateDragging) {
7181             /* Undo animation damage if any */
7182             DrawPosition(FALSE, NULL);
7183         }
7184         if (second || sweepSelecting) {
7185             /* Second up/down in same square; just abort move */
7186             if(sweepSelecting) DrawPosition(FALSE, boards[currentMove]);
7187             second = sweepSelecting = 0;
7188             fromX = fromY = -1;
7189             gatingPiece = EmptySquare;
7190             ClearHighlights();
7191             gotPremove = 0;
7192             ClearPremoveHighlights();
7193         } else {
7194             /* First upclick in same square; start click-click mode */
7195             SetHighlights(x, y, -1, -1);
7196         }
7197         return;
7198     }
7199
7200     clearFlag = 0;
7201
7202     /* we now have a different from- and (possibly off-board) to-square */
7203     /* Completed move */
7204     if(!sweepSelecting) {
7205         toX = x;
7206         toY = y;
7207         saveAnimate = appData.animate;
7208     } else sweepSelecting = 0; // this must be the up-click corresponding to the down-click that started the sweep
7209
7210     if (clickType == Press) {
7211         if(gameMode == EditPosition && boards[currentMove][fromY][fromX] == EmptySquare) {
7212             // must be Edit Position mode with empty-square selected
7213             fromX = x; fromY = y; DragPieceBegin(xPix, yPix, FALSE); dragging = 1; // consider this a new attempt to drag
7214             if(x >= BOARD_LEFT && x < BOARD_RGHT) clearFlag = 1; // and defer click-click move of empty-square to up-click
7215             return;
7216         }
7217         if(HasPromotionChoice(fromX, fromY, toX, toY, &promoChoice, FALSE)) {
7218           if(appData.sweepSelect) {
7219             ChessSquare piece = boards[currentMove][fromY][fromX];
7220             promoSweep = defaultPromoChoice;
7221             if(PieceToChar(PROMOTED piece) == '+') promoSweep = PROMOTED piece;
7222             selectFlag = 0; lastX = xPix; lastY = yPix;
7223             Sweep(0); // Pawn that is going to promote: preview promotion piece
7224             sweepSelecting = 1;
7225             DisplayMessage("", _("Pull pawn backwards to under-promote"));
7226             MarkTargetSquares(1);
7227           }
7228           return; // promo popup appears on up-click
7229         }
7230         /* Finish clickclick move */
7231         if (appData.animate || appData.highlightLastMove) {
7232             SetHighlights(fromX, fromY, toX, toY);
7233         } else {
7234             ClearHighlights();
7235         }
7236     } else {
7237         /* Finish drag move */
7238         if (appData.highlightLastMove) {
7239             SetHighlights(fromX, fromY, toX, toY);
7240         } else {
7241             ClearHighlights();
7242         }
7243         DragPieceEnd(xPix, yPix); dragging = 0;
7244         /* Don't animate move and drag both */
7245         appData.animate = FALSE;
7246     }
7247     MarkTargetSquares(1);
7248
7249     // moves into holding are invalid for now (except in EditPosition, adapting to-square)
7250     if(x >= 0 && x < BOARD_LEFT || x >= BOARD_RGHT) {
7251         ChessSquare piece = boards[currentMove][fromY][fromX];
7252         if(gameMode == EditPosition && piece != EmptySquare &&
7253            fromX >= BOARD_LEFT && fromX < BOARD_RGHT) {
7254             int n;
7255
7256             if(x == BOARD_LEFT-2 && piece >= BlackPawn) {
7257                 n = PieceToNumber(piece - (int)BlackPawn);
7258                 if(n >= gameInfo.holdingsSize) { n = 0; piece = BlackPawn; }
7259                 boards[currentMove][BOARD_HEIGHT-1 - n][0] = piece;
7260                 boards[currentMove][BOARD_HEIGHT-1 - n][1]++;
7261             } else
7262             if(x == BOARD_RGHT+1 && piece < BlackPawn) {
7263                 n = PieceToNumber(piece);
7264                 if(n >= gameInfo.holdingsSize) { n = 0; piece = WhitePawn; }
7265                 boards[currentMove][n][BOARD_WIDTH-1] = piece;
7266                 boards[currentMove][n][BOARD_WIDTH-2]++;
7267             }
7268             boards[currentMove][fromY][fromX] = EmptySquare;
7269         }
7270         ClearHighlights();
7271         fromX = fromY = -1;
7272         DrawPosition(TRUE, boards[currentMove]);
7273         return;
7274     }
7275
7276     // off-board moves should not be highlighted
7277     if(x < 0 || y < 0) ClearHighlights();
7278
7279     if(gatingPiece != EmptySquare && gameInfo.variant == VariantSChess) promoChoice = ToLower(PieceToChar(gatingPiece));
7280
7281     if (HasPromotionChoice(fromX, fromY, toX, toY, &promoChoice, appData.sweepSelect)) {
7282         SetHighlights(fromX, fromY, toX, toY);
7283         if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand) {
7284             // [HGM] super: promotion to captured piece selected from holdings
7285             ChessSquare p = boards[currentMove][fromY][fromX], q = boards[currentMove][toY][toX];
7286             promotionChoice = TRUE;
7287             // kludge follows to temporarily execute move on display, without promoting yet
7288             boards[currentMove][fromY][fromX] = EmptySquare; // move Pawn to 8th rank
7289             boards[currentMove][toY][toX] = p;
7290             DrawPosition(FALSE, boards[currentMove]);
7291             boards[currentMove][fromY][fromX] = p; // take back, but display stays
7292             boards[currentMove][toY][toX] = q;
7293             DisplayMessage("Click in holdings to choose piece", "");
7294             return;
7295         }
7296         PromotionPopUp();
7297     } else {
7298         int oldMove = currentMove;
7299         UserMoveEvent(fromX, fromY, toX, toY, promoChoice);
7300         if (!appData.highlightLastMove || gotPremove) ClearHighlights();
7301         if (gotPremove) SetPremoveHighlights(fromX, fromY, toX, toY);
7302         if(saveAnimate && !appData.animate && currentMove != oldMove && // drag-move was performed
7303            Explode(boards[currentMove-1], fromX, fromY, toX, toY))
7304             DrawPosition(TRUE, boards[currentMove]);
7305         fromX = fromY = -1;
7306     }
7307     appData.animate = saveAnimate;
7308     if (appData.animate || appData.animateDragging) {
7309         /* Undo animation damage if needed */
7310         DrawPosition(FALSE, NULL);
7311     }
7312 }
7313
7314 int
7315 RightClick (ClickType action, int x, int y, int *fromX, int *fromY)
7316 {   // front-end-free part taken out of PieceMenuPopup
7317     int whichMenu; int xSqr, ySqr;
7318
7319     if(seekGraphUp) { // [HGM] seekgraph
7320         if(action == Press)   SeekGraphClick(Press, x, y, 2); // 2 indicates right-click: no pop-down on miss
7321         if(action == Release) SeekGraphClick(Release, x, y, 2); // and no challenge on hit
7322         return -2;
7323     }
7324
7325     if((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)
7326          && !appData.zippyPlay && appData.bgObserve) { // [HGM] bughouse: show background game
7327         if(!partnerBoardValid) return -2; // suppress display of uninitialized boards
7328         if( appData.dualBoard) return -2; // [HGM] dual: is already displayed
7329         if(action == Press)   {
7330             originalFlip = flipView;
7331             flipView = !flipView; // temporarily flip board to see game from partners perspective
7332             DrawPosition(TRUE, partnerBoard);
7333             DisplayMessage(partnerStatus, "");
7334             partnerUp = TRUE;
7335         } else if(action == Release) {
7336             flipView = originalFlip;
7337             DrawPosition(TRUE, boards[currentMove]);
7338             partnerUp = FALSE;
7339         }
7340         return -2;
7341     }
7342
7343     xSqr = EventToSquare(x, BOARD_WIDTH);
7344     ySqr = EventToSquare(y, BOARD_HEIGHT);
7345     if (action == Release) {
7346         if(pieceSweep != EmptySquare) {
7347             EditPositionMenuEvent(pieceSweep, toX, toY);
7348             pieceSweep = EmptySquare;
7349         } else UnLoadPV(); // [HGM] pv
7350     }
7351     if (action != Press) return -2; // return code to be ignored
7352     switch (gameMode) {
7353       case IcsExamining:
7354         if(xSqr < BOARD_LEFT || xSqr >= BOARD_RGHT) return -1;
7355       case EditPosition:
7356         if (xSqr == BOARD_LEFT-1 || xSqr == BOARD_RGHT) return -1;
7357         if (xSqr < 0 || ySqr < 0) return -1;
7358         if(appData.pieceMenu) { whichMenu = 0; break; } // edit-position menu
7359         pieceSweep = shiftKey ? BlackPawn : WhitePawn;  // [HGM] sweep: prepare selecting piece by mouse sweep
7360         toX = xSqr; toY = ySqr; lastX = x, lastY = y;
7361         if(flipView) toX = BOARD_WIDTH - 1 - toX; else toY = BOARD_HEIGHT - 1 - toY;
7362         NextPiece(0);
7363         return 2; // grab
7364       case IcsObserving:
7365         if(!appData.icsEngineAnalyze) return -1;
7366       case IcsPlayingWhite:
7367       case IcsPlayingBlack:
7368         if(!appData.zippyPlay) goto noZip;
7369       case AnalyzeMode:
7370       case AnalyzeFile:
7371       case MachinePlaysWhite:
7372       case MachinePlaysBlack:
7373       case TwoMachinesPlay: // [HGM] pv: use for showing PV
7374         if (!appData.dropMenu) {
7375           LoadPV(x, y);
7376           return 2; // flag front-end to grab mouse events
7377         }
7378         if(gameMode == TwoMachinesPlay || gameMode == AnalyzeMode ||
7379            gameMode == AnalyzeFile || gameMode == IcsObserving) return -1;
7380       case EditGame:
7381       noZip:
7382         if (xSqr < 0 || ySqr < 0) return -1;
7383         if (!appData.dropMenu || appData.testLegality &&
7384             gameInfo.variant != VariantBughouse &&
7385             gameInfo.variant != VariantCrazyhouse) return -1;
7386         whichMenu = 1; // drop menu
7387         break;
7388       default:
7389         return -1;
7390     }
7391
7392     if (((*fromX = xSqr) < 0) ||
7393         ((*fromY = ySqr) < 0)) {
7394         *fromX = *fromY = -1;
7395         return -1;
7396     }
7397     if (flipView)
7398       *fromX = BOARD_WIDTH - 1 - *fromX;
7399     else
7400       *fromY = BOARD_HEIGHT - 1 - *fromY;
7401
7402     return whichMenu;
7403 }
7404
7405 void
7406 SendProgramStatsToFrontend (ChessProgramState * cps, ChessProgramStats * cpstats)
7407 {
7408 //    char * hint = lastHint;
7409     FrontEndProgramStats stats;
7410
7411     stats.which = cps == &first ? 0 : 1;
7412     stats.depth = cpstats->depth;
7413     stats.nodes = cpstats->nodes;
7414     stats.score = cpstats->score;
7415     stats.time = cpstats->time;
7416     stats.pv = cpstats->movelist;
7417     stats.hint = lastHint;
7418     stats.an_move_index = 0;
7419     stats.an_move_count = 0;
7420
7421     if( gameMode == AnalyzeMode || gameMode == AnalyzeFile ) {
7422         stats.hint = cpstats->move_name;
7423         stats.an_move_index = cpstats->nr_moves - cpstats->moves_left;
7424         stats.an_move_count = cpstats->nr_moves;
7425     }
7426
7427     if(stats.pv && stats.pv[0]) safeStrCpy(lastPV[stats.which], stats.pv, sizeof(lastPV[stats.which])/sizeof(lastPV[stats.which][0])); // [HGM] pv: remember last PV of each
7428
7429     SetProgramStats( &stats );
7430 }
7431
7432 void
7433 ClearEngineOutputPane (int which)
7434 {
7435     static FrontEndProgramStats dummyStats;
7436     dummyStats.which = which;
7437     dummyStats.pv = "#";
7438     SetProgramStats( &dummyStats );
7439 }
7440
7441 #define MAXPLAYERS 500
7442
7443 char *
7444 TourneyStandings (int display)
7445 {
7446     int i, w, b, color, wScore, bScore, dummy, nr=0, nPlayers=0;
7447     int score[MAXPLAYERS], ranking[MAXPLAYERS], points[MAXPLAYERS], games[MAXPLAYERS];
7448     char result, *p, *names[MAXPLAYERS];
7449
7450     if(appData.tourneyType < 0 && !strchr(appData.results, '*'))
7451         return strdup(_("Swiss tourney finished")); // standings of Swiss yet TODO
7452     names[0] = p = strdup(appData.participants);
7453     while(p = strchr(p, '\n')) *p++ = NULLCHAR, names[++nPlayers] = p; // count participants
7454
7455     for(i=0; i<nPlayers; i++) score[i] = games[i] = 0;
7456
7457     while(result = appData.results[nr]) {
7458         color = Pairing(nr, nPlayers, &w, &b, &dummy);
7459         if(!(color ^ matchGame & 1)) { dummy = w; w = b; b = dummy; }
7460         wScore = bScore = 0;
7461         switch(result) {
7462           case '+': wScore = 2; break;
7463           case '-': bScore = 2; break;
7464           case '=': wScore = bScore = 1; break;
7465           case ' ':
7466           case '*': return strdup("busy"); // tourney not finished
7467         }
7468         score[w] += wScore;
7469         score[b] += bScore;
7470         games[w]++;
7471         games[b]++;
7472         nr++;
7473     }
7474     if(appData.tourneyType > 0) nPlayers = appData.tourneyType; // in gauntlet, list only gauntlet engine(s)
7475     for(w=0; w<nPlayers; w++) {
7476         bScore = -1;
7477         for(i=0; i<nPlayers; i++) if(score[i] > bScore) bScore = score[i], b = i;
7478         ranking[w] = b; points[w] = bScore; score[b] = -2;
7479     }
7480     p = malloc(nPlayers*34+1);
7481     for(w=0; w<nPlayers && w<display; w++)
7482         sprintf(p+34*w, "%2d. %5.1f/%-3d %-19.19s\n", w+1, points[w]/2., games[ranking[w]], names[ranking[w]]);
7483     free(names[0]);
7484     return p;
7485 }
7486
7487 void
7488 Count (Board board, int pCnt[], int *nW, int *nB, int *wStale, int *bStale, int *bishopColor)
7489 {       // count all piece types
7490         int p, f, r;
7491         *nB = *nW = *wStale = *bStale = *bishopColor = 0;
7492         for(p=WhitePawn; p<=EmptySquare; p++) pCnt[p] = 0;
7493         for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
7494                 p = board[r][f];
7495                 pCnt[p]++;
7496                 if(p == WhitePawn && r == BOARD_HEIGHT-1) (*wStale)++; else
7497                 if(p == BlackPawn && r == 0) (*bStale)++; // count last-Rank Pawns (XQ) separately
7498                 if(p <= WhiteKing) (*nW)++; else if(p <= BlackKing) (*nB)++;
7499                 if(p == WhiteBishop || p == WhiteFerz || p == WhiteAlfil ||
7500                    p == BlackBishop || p == BlackFerz || p == BlackAlfil   )
7501                         *bishopColor |= 1 << ((f^r)&1); // track square color of color-bound pieces
7502         }
7503 }
7504
7505 int
7506 SufficientDefence (int pCnt[], int side, int nMine, int nHis)
7507 {
7508         int myPawns = pCnt[WhitePawn+side]; // my total Pawn count;
7509         int majorDefense = pCnt[BlackRook-side] + pCnt[BlackCannon-side] + pCnt[BlackKnight-side];
7510
7511         nMine -= pCnt[WhiteFerz+side] + pCnt[WhiteAlfil+side]; // discount defenders
7512         if(nMine - myPawns > 2) return FALSE; // no trivial draws with more than 1 major
7513         if(myPawns == 2 && nMine == 3) // KPP
7514             return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] >= 3;
7515         if(myPawns == 1 && nMine == 2) // KP
7516             return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side]  + pCnt[BlackPawn-side] >= 1;
7517         if(myPawns == 1 && nMine == 3 && pCnt[WhiteKnight+side]) // KHP
7518             return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side]*2 >= 5;
7519         if(myPawns) return FALSE;
7520         if(pCnt[WhiteRook+side])
7521             return pCnt[BlackRook-side] ||
7522                    pCnt[BlackCannon-side] && (pCnt[BlackFerz-side] >= 2 || pCnt[BlackAlfil-side] >= 2) ||
7523                    pCnt[BlackKnight-side] && pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] > 2 ||
7524                    pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] >= 4;
7525         if(pCnt[WhiteCannon+side]) {
7526             if(pCnt[WhiteFerz+side] + myPawns == 0) return TRUE; // Cannon needs platform
7527             return majorDefense || pCnt[BlackAlfil-side] >= 2;
7528         }
7529         if(pCnt[WhiteKnight+side])
7530             return majorDefense || pCnt[BlackFerz-side] >= 2 || pCnt[BlackAlfil-side] + pCnt[BlackPawn-side] >= 1;
7531         return FALSE;
7532 }
7533
7534 int
7535 MatingPotential (int pCnt[], int side, int nMine, int nHis, int stale, int bisColor)
7536 {
7537         VariantClass v = gameInfo.variant;
7538
7539         if(v == VariantShogi || v == VariantCrazyhouse || v == VariantBughouse) return TRUE; // drop games always winnable
7540         if(v == VariantShatranj) return TRUE; // always winnable through baring
7541         if(v == VariantLosers || v == VariantSuicide || v == VariantGiveaway) return TRUE;
7542         if(v == Variant3Check || v == VariantAtomic) return nMine > 1; // can win through checking / exploding King
7543
7544         if(v == VariantXiangqi) {
7545                 int majors = 5*pCnt[BlackKnight-side] + 7*pCnt[BlackCannon-side] + 7*pCnt[BlackRook-side];
7546
7547                 nMine -= pCnt[WhiteFerz+side] + pCnt[WhiteAlfil+side] + stale; // discount defensive pieces and back-rank Pawns
7548                 if(nMine + stale == 1) return (pCnt[BlackFerz-side] > 1 && pCnt[BlackKnight-side] > 0); // bare K can stalemate KHAA (!)
7549                 if(nMine > 2) return TRUE; // if we don't have P, H or R, we must have CC
7550                 if(nMine == 2 && pCnt[WhiteCannon+side] == 0) return TRUE; // We have at least one P, H or R
7551                 // if we get here, we must have KC... or KP..., possibly with additional A, E or last-rank P
7552                 if(stale) // we have at least one last-rank P plus perhaps C
7553                     return majors // KPKX
7554                         || pCnt[BlackFerz-side] && pCnt[BlackFerz-side] + pCnt[WhiteCannon+side] + stale > 2; // KPKAA, KPPKA and KCPKA
7555                 else // KCA*E*
7556                     return pCnt[WhiteFerz+side] // KCAK
7557                         || pCnt[WhiteAlfil+side] && pCnt[BlackRook-side] + pCnt[BlackCannon-side] + pCnt[BlackFerz-side] // KCEKA, KCEKX (X!=H)
7558                         || majors + (12*pCnt[BlackFerz-side] | 6*pCnt[BlackAlfil-side]) > 16; // KCKAA, KCKAX, KCKEEX, KCKEXX (XX!=HH), KCKXXX
7559                 // TO DO: cases wih an unpromoted f-Pawn acting as platform for an opponent Cannon
7560
7561         } else if(pCnt[WhiteKing] == 1 && pCnt[BlackKing] == 1) { // other variants with orthodox Kings
7562                 int nBishops = pCnt[WhiteBishop+side] + pCnt[WhiteFerz+side];
7563
7564                 if(nMine == 1) return FALSE; // bare King
7565                 if(nBishops && bisColor == 3) return TRUE; // There must be a second B/A/F, which can either block (his) or attack (mine) the escape square
7566                 nMine += (nBishops > 0) - nBishops; // By now all Bishops (and Ferz) on like-colored squares, so count as one
7567                 if(nMine > 2 && nMine != pCnt[WhiteAlfil+side] + 1) return TRUE; // At least two pieces, not all Alfils
7568                 // by now we have King + 1 piece (or multiple Bishops on the same color)
7569                 if(pCnt[WhiteKnight+side])
7570                         return (pCnt[BlackKnight-side] + pCnt[BlackBishop-side] + pCnt[BlackMan-side] +
7571                                 pCnt[BlackWazir-side] + pCnt[BlackSilver-side] + bisColor // KNKN, KNKB, KNKF, KNKE, KNKW, KNKM, KNKS
7572                              || nHis > 3); // be sure to cover suffocation mates in corner (e.g. KNKQCA)
7573                 if(nBishops)
7574                         return (pCnt[BlackKnight-side]); // KBKN, KFKN
7575                 if(pCnt[WhiteAlfil+side])
7576                         return (nHis > 2); // Alfils can in general not reach a corner square, but there might be edge (suffocation) mates
7577                 if(pCnt[WhiteWazir+side])
7578                         return (pCnt[BlackKnight-side] + pCnt[BlackWazir-side] + pCnt[BlackAlfil-side]); // KWKN, KWKW, KWKE
7579         }
7580
7581         return TRUE;
7582 }
7583
7584 int
7585 CompareWithRights (Board b1, Board b2)
7586 {
7587     int rights = 0;
7588     if(!CompareBoards(b1, b2)) return FALSE;
7589     if(b1[EP_STATUS] != b2[EP_STATUS]) return FALSE;
7590     /* compare castling rights */
7591     if( b1[CASTLING][2] != b2[CASTLING][2] && (b2[CASTLING][0] != NoRights || b2[CASTLING][1] != NoRights) )
7592            rights++; /* King lost rights, while rook still had them */
7593     if( b1[CASTLING][2] != NoRights ) { /* king has rights */
7594         if( b1[CASTLING][0] != b2[CASTLING][0] || b1[CASTLING][1] != b2[CASTLING][1] )
7595            rights++; /* but at least one rook lost them */
7596     }
7597     if( b1[CASTLING][5] != b1[CASTLING][5] && (b2[CASTLING][3] != NoRights || b2[CASTLING][4] != NoRights) )
7598            rights++;
7599     if( b1[CASTLING][5] != NoRights ) {
7600         if( b1[CASTLING][3] != b2[CASTLING][3] || b1[CASTLING][4] != b2[CASTLING][4] )
7601            rights++;
7602     }
7603     return rights == 0;
7604 }
7605
7606 int
7607 Adjudicate (ChessProgramState *cps)
7608 {       // [HGM] some adjudications useful with buggy engines
7609         // [HGM] adjudicate: made into separate routine, which now can be called after every move
7610         //       In any case it determnes if the game is a claimable draw (filling in EP_STATUS).
7611         //       Actually ending the game is now based on the additional internal condition canAdjudicate.
7612         //       Only when the game is ended, and the opponent is a computer, this opponent gets the move relayed.
7613         int k, count = 0; static int bare = 1;
7614         ChessProgramState *engineOpponent = (gameMode == TwoMachinesPlay ? cps->other : (cps ? NULL : &first));
7615         Boolean canAdjudicate = !appData.icsActive;
7616
7617         // most tests only when we understand the game, i.e. legality-checking on
7618             if( appData.testLegality )
7619             {   /* [HGM] Some more adjudications for obstinate engines */
7620                 int nrW, nrB, bishopColor, staleW, staleB, nr[EmptySquare+1], i;
7621                 static int moveCount = 6;
7622                 ChessMove result;
7623                 char *reason = NULL;
7624
7625                 /* Count what is on board. */
7626                 Count(boards[forwardMostMove], nr, &nrW, &nrB, &staleW, &staleB, &bishopColor);
7627
7628                 /* Some material-based adjudications that have to be made before stalemate test */
7629                 if(gameInfo.variant == VariantAtomic && nr[WhiteKing] + nr[BlackKing] < 2) {
7630                     // [HGM] atomic: stm must have lost his King on previous move, as destroying own K is illegal
7631                      boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE; // make claimable as if stm is checkmated
7632                      if(canAdjudicate && appData.checkMates) {
7633                          if(engineOpponent)
7634                            SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets move
7635                          GameEnds( WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins,
7636                                                         "Xboard adjudication: King destroyed", GE_XBOARD );
7637                          return 1;
7638                      }
7639                 }
7640
7641                 /* Bare King in Shatranj (loses) or Losers (wins) */
7642                 if( nrW == 1 || nrB == 1) {
7643                   if( gameInfo.variant == VariantLosers) { // [HGM] losers: bare King wins (stm must have it first)
7644                      boards[forwardMostMove][EP_STATUS] = EP_WINS;  // mark as win, so it becomes claimable
7645                      if(canAdjudicate && appData.checkMates) {
7646                          if(engineOpponent)
7647                            SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets to see move
7648                          GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
7649                                                         "Xboard adjudication: Bare king", GE_XBOARD );
7650                          return 1;
7651                      }
7652                   } else
7653                   if( gameInfo.variant == VariantShatranj && --bare < 0)
7654                   {    /* bare King */
7655                         boards[forwardMostMove][EP_STATUS] = EP_WINS; // make claimable as win for stm
7656                         if(canAdjudicate && appData.checkMates) {
7657                             /* but only adjudicate if adjudication enabled */
7658                             if(engineOpponent)
7659                               SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets move
7660                             GameEnds( nrW > 1 ? WhiteWins : nrB > 1 ? BlackWins : GameIsDrawn,
7661                                                         "Xboard adjudication: Bare king", GE_XBOARD );
7662                             return 1;
7663                         }
7664                   }
7665                 } else bare = 1;
7666
7667
7668             // don't wait for engine to announce game end if we can judge ourselves
7669             switch (MateTest(boards[forwardMostMove], PosFlags(forwardMostMove)) ) {
7670               case MT_CHECK:
7671                 if(gameInfo.variant == Variant3Check) { // [HGM] 3check: when in check, test if 3rd time
7672                     int i, checkCnt = 0;    // (should really be done by making nr of checks part of game state)
7673                     for(i=forwardMostMove-2; i>=backwardMostMove; i-=2) {
7674                         if(MateTest(boards[i], PosFlags(i)) == MT_CHECK)
7675                             checkCnt++;
7676                         if(checkCnt >= 2) {
7677                             reason = "Xboard adjudication: 3rd check";
7678                             boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE;
7679                             break;
7680                         }
7681                     }
7682                 }
7683               case MT_NONE:
7684               default:
7685                 break;
7686               case MT_STALEMATE:
7687               case MT_STAINMATE:
7688                 reason = "Xboard adjudication: Stalemate";
7689                 if((signed char)boards[forwardMostMove][EP_STATUS] != EP_CHECKMATE) { // [HGM] don't touch win through baring or K-capt
7690                     boards[forwardMostMove][EP_STATUS] = EP_STALEMATE;   // default result for stalemate is draw
7691                     if(gameInfo.variant == VariantLosers  || gameInfo.variant == VariantGiveaway) // [HGM] losers:
7692                         boards[forwardMostMove][EP_STATUS] = EP_WINS;    // in these variants stalemated is always a win
7693                     else if(gameInfo.variant == VariantSuicide) // in suicide it depends
7694                         boards[forwardMostMove][EP_STATUS] = nrW == nrB ? EP_STALEMATE :
7695                                                    ((nrW < nrB) != WhiteOnMove(forwardMostMove) ?
7696                                                                         EP_CHECKMATE : EP_WINS);
7697                     else if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantXiangqi)
7698                         boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE; // and in these variants being stalemated loses
7699                 }
7700                 break;
7701               case MT_CHECKMATE:
7702                 reason = "Xboard adjudication: Checkmate";
7703                 boards[forwardMostMove][EP_STATUS] = (gameInfo.variant == VariantLosers ? EP_WINS : EP_CHECKMATE);
7704                 break;
7705             }
7706
7707                 switch(i = (signed char)boards[forwardMostMove][EP_STATUS]) {
7708                     case EP_STALEMATE:
7709                         result = GameIsDrawn; break;
7710                     case EP_CHECKMATE:
7711                         result = WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins; break;
7712                     case EP_WINS:
7713                         result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins; break;
7714                     default:
7715                         result = EndOfFile;
7716                 }
7717                 if(canAdjudicate && appData.checkMates && result) { // [HGM] mates: adjudicate finished games if requested
7718                     if(engineOpponent)
7719                       SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7720                     GameEnds( result, reason, GE_XBOARD );
7721                     return 1;
7722                 }
7723
7724                 /* Next absolutely insufficient mating material. */
7725                 if(!MatingPotential(nr, WhitePawn, nrW, nrB, staleW, bishopColor) &&
7726                    !MatingPotential(nr, BlackPawn, nrB, nrW, staleB, bishopColor))
7727                 {    /* includes KBK, KNK, KK of KBKB with like Bishops */
7728
7729                      /* always flag draws, for judging claims */
7730                      boards[forwardMostMove][EP_STATUS] = EP_INSUF_DRAW;
7731
7732                      if(canAdjudicate && appData.materialDraws) {
7733                          /* but only adjudicate them if adjudication enabled */
7734                          if(engineOpponent) {
7735                            SendToProgram("force\n", engineOpponent); // suppress reply
7736                            SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see last move */
7737                          }
7738                          GameEnds( GameIsDrawn, "Xboard adjudication: Insufficient mating material", GE_XBOARD );
7739                          return 1;
7740                      }
7741                 }
7742
7743                 /* Then some trivial draws (only adjudicate, cannot be claimed) */
7744                 if(gameInfo.variant == VariantXiangqi ?
7745                        SufficientDefence(nr, WhitePawn, nrW, nrB) && SufficientDefence(nr, BlackPawn, nrB, nrW)
7746                  : nrW + nrB == 4 &&
7747                    (   nr[WhiteRook] == 1 && nr[BlackRook] == 1 /* KRKR */
7748                    || nr[WhiteQueen] && nr[BlackQueen]==1     /* KQKQ */
7749                    || nr[WhiteKnight]==2 || nr[BlackKnight]==2     /* KNNK */
7750                    || nr[WhiteKnight]+nr[WhiteBishop] == 1 && nr[BlackKnight]+nr[BlackBishop] == 1 /* KBKN, KBKB, KNKN */
7751                    ) ) {
7752                      if(--moveCount < 0 && appData.trivialDraws && canAdjudicate)
7753                      {    /* if the first 3 moves do not show a tactical win, declare draw */
7754                           if(engineOpponent) {
7755                             SendToProgram("force\n", engineOpponent); // suppress reply
7756                             SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7757                           }
7758                           GameEnds( GameIsDrawn, "Xboard adjudication: Trivial draw", GE_XBOARD );
7759                           return 1;
7760                      }
7761                 } else moveCount = 6;
7762             }
7763
7764         // Repetition draws and 50-move rule can be applied independently of legality testing
7765
7766                 /* Check for rep-draws */
7767                 count = 0;
7768                 for(k = forwardMostMove-2;
7769                     k>=backwardMostMove && k>=forwardMostMove-100 &&
7770                         (signed char)boards[k][EP_STATUS] < EP_UNKNOWN &&
7771                         (signed char)boards[k+2][EP_STATUS] <= EP_NONE && (signed char)boards[k+1][EP_STATUS] <= EP_NONE;
7772                     k-=2)
7773                 {   int rights=0;
7774                     if(CompareBoards(boards[k], boards[forwardMostMove])) {
7775                         /* compare castling rights */
7776                         if( boards[forwardMostMove][CASTLING][2] != boards[k][CASTLING][2] &&
7777                              (boards[k][CASTLING][0] != NoRights || boards[k][CASTLING][1] != NoRights) )
7778                                 rights++; /* King lost rights, while rook still had them */
7779                         if( boards[forwardMostMove][CASTLING][2] != NoRights ) { /* king has rights */
7780                             if( boards[forwardMostMove][CASTLING][0] != boards[k][CASTLING][0] ||
7781                                 boards[forwardMostMove][CASTLING][1] != boards[k][CASTLING][1] )
7782                                    rights++; /* but at least one rook lost them */
7783                         }
7784                         if( boards[forwardMostMove][CASTLING][5] != boards[k][CASTLING][5] &&
7785                              (boards[k][CASTLING][3] != NoRights || boards[k][CASTLING][4] != NoRights) )
7786                                 rights++;
7787                         if( boards[forwardMostMove][CASTLING][5] != NoRights ) {
7788                             if( boards[forwardMostMove][CASTLING][3] != boards[k][CASTLING][3] ||
7789                                 boards[forwardMostMove][CASTLING][4] != boards[k][CASTLING][4] )
7790                                    rights++;
7791                         }
7792                         if( rights == 0 && ++count > appData.drawRepeats-2 && canAdjudicate
7793                             && appData.drawRepeats > 1) {
7794                              /* adjudicate after user-specified nr of repeats */
7795                              int result = GameIsDrawn;
7796                              char *details = "XBoard adjudication: repetition draw";
7797                              if(gameInfo.variant == VariantXiangqi && appData.testLegality) {
7798                                 // [HGM] xiangqi: check for forbidden perpetuals
7799                                 int m, ourPerpetual = 1, hisPerpetual = 1;
7800                                 for(m=forwardMostMove; m>k; m-=2) {
7801                                     if(MateTest(boards[m], PosFlags(m)) != MT_CHECK)
7802                                         ourPerpetual = 0; // the current mover did not always check
7803                                     if(MateTest(boards[m-1], PosFlags(m-1)) != MT_CHECK)
7804                                         hisPerpetual = 0; // the opponent did not always check
7805                                 }
7806                                 if(appData.debugMode) fprintf(debugFP, "XQ perpetual test, our=%d, his=%d\n",
7807                                                                         ourPerpetual, hisPerpetual);
7808                                 if(ourPerpetual && !hisPerpetual) { // we are actively checking him: forfeit
7809                                     result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
7810                                     details = "Xboard adjudication: perpetual checking";
7811                                 } else
7812                                 if(hisPerpetual && !ourPerpetual) { // he is checking us, but did not repeat yet
7813                                     break; // (or we would have caught him before). Abort repetition-checking loop.
7814                                 } else
7815                                 // Now check for perpetual chases
7816                                 if(!ourPerpetual && !hisPerpetual) { // no perpetual check, test for chase
7817                                     hisPerpetual = PerpetualChase(k, forwardMostMove);
7818                                     ourPerpetual = PerpetualChase(k+1, forwardMostMove);
7819                                     if(ourPerpetual && !hisPerpetual) { // we are actively chasing him: forfeit
7820                                         static char resdet[MSG_SIZ];
7821                                         result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
7822                                         details = resdet;
7823                                         snprintf(resdet, MSG_SIZ, "Xboard adjudication: perpetual chasing of %c%c", ourPerpetual>>8, ourPerpetual&255);
7824                                     } else
7825                                     if(hisPerpetual && !ourPerpetual)   // he is chasing us, but did not repeat yet
7826                                         break; // Abort repetition-checking loop.
7827                                 }
7828                                 // if neither of us is checking or chasing all the time, or both are, it is draw
7829                              }
7830                              if(engineOpponent) {
7831                                SendToProgram("force\n", engineOpponent); // suppress reply
7832                                SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7833                              }
7834                              GameEnds( result, details, GE_XBOARD );
7835                              return 1;
7836                         }
7837                         if( rights == 0 && count > 1 ) /* occurred 2 or more times before */
7838                              boards[forwardMostMove][EP_STATUS] = EP_REP_DRAW;
7839                     }
7840                 }
7841
7842                 /* Now we test for 50-move draws. Determine ply count */
7843                 count = forwardMostMove;
7844                 /* look for last irreversble move */
7845                 while( (signed char)boards[count][EP_STATUS] <= EP_NONE && count > backwardMostMove )
7846                     count--;
7847                 /* if we hit starting position, add initial plies */
7848                 if( count == backwardMostMove )
7849                     count -= initialRulePlies;
7850                 count = forwardMostMove - count;
7851                 if(gameInfo.variant == VariantXiangqi && ( count >= 100 || count >= 2*appData.ruleMoves ) ) {
7852                         // adjust reversible move counter for checks in Xiangqi
7853                         int i = forwardMostMove - count, inCheck = 0, lastCheck;
7854                         if(i < backwardMostMove) i = backwardMostMove;
7855                         while(i <= forwardMostMove) {
7856                                 lastCheck = inCheck; // check evasion does not count
7857                                 inCheck = (MateTest(boards[i], PosFlags(i)) == MT_CHECK);
7858                                 if(inCheck || lastCheck) count--; // check does not count
7859                                 i++;
7860                         }
7861                 }
7862                 if( count >= 100)
7863                          boards[forwardMostMove][EP_STATUS] = EP_RULE_DRAW;
7864                          /* this is used to judge if draw claims are legal */
7865                 if(canAdjudicate && appData.ruleMoves > 0 && count >= 2*appData.ruleMoves) {
7866                          if(engineOpponent) {
7867                            SendToProgram("force\n", engineOpponent); // suppress reply
7868                            SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7869                          }
7870                          GameEnds( GameIsDrawn, "Xboard adjudication: 50-move rule", GE_XBOARD );
7871                          return 1;
7872                 }
7873
7874                 /* if draw offer is pending, treat it as a draw claim
7875                  * when draw condition present, to allow engines a way to
7876                  * claim draws before making their move to avoid a race
7877                  * condition occurring after their move
7878                  */
7879                 if((gameMode == TwoMachinesPlay ? second.offeredDraw : userOfferedDraw) || first.offeredDraw ) {
7880                          char *p = NULL;
7881                          if((signed char)boards[forwardMostMove][EP_STATUS] == EP_RULE_DRAW)
7882                              p = "Draw claim: 50-move rule";
7883                          if((signed char)boards[forwardMostMove][EP_STATUS] == EP_REP_DRAW)
7884                              p = "Draw claim: 3-fold repetition";
7885                          if((signed char)boards[forwardMostMove][EP_STATUS] == EP_INSUF_DRAW)
7886                              p = "Draw claim: insufficient mating material";
7887                          if( p != NULL && canAdjudicate) {
7888                              if(engineOpponent) {
7889                                SendToProgram("force\n", engineOpponent); // suppress reply
7890                                SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7891                              }
7892                              GameEnds( GameIsDrawn, p, GE_XBOARD );
7893                              return 1;
7894                          }
7895                 }
7896
7897                 if( canAdjudicate && appData.adjudicateDrawMoves > 0 && forwardMostMove > (2*appData.adjudicateDrawMoves) ) {
7898                     if(engineOpponent) {
7899                       SendToProgram("force\n", engineOpponent); // suppress reply
7900                       SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7901                     }
7902                     GameEnds( GameIsDrawn, "Xboard adjudication: long game", GE_XBOARD );
7903                     return 1;
7904                 }
7905         return 0;
7906 }
7907
7908 char *
7909 SendMoveToBookUser (int moveNr, ChessProgramState *cps, int initial)
7910 {   // [HGM] book: this routine intercepts moves to simulate book replies
7911     char *bookHit = NULL;
7912
7913     //first determine if the incoming move brings opponent into his book
7914     if(appData.usePolyglotBook && (cps == &first ? !appData.firstHasOwnBookUCI : !appData.secondHasOwnBookUCI))
7915         bookHit = ProbeBook(moveNr+1, appData.polyglotBook); // returns move
7916     if(appData.debugMode) fprintf(debugFP, "book hit = %s\n", bookHit ? bookHit : "(NULL)");
7917     if(bookHit != NULL && !cps->bookSuspend) {
7918         // make sure opponent is not going to reply after receiving move to book position
7919         SendToProgram("force\n", cps);
7920         cps->bookSuspend = TRUE; // flag indicating it has to be restarted
7921     }
7922     if(!initial) SendMoveToProgram(moveNr, cps); // with hit on initial position there is no move
7923     // now arrange restart after book miss
7924     if(bookHit) {
7925         // after a book hit we never send 'go', and the code after the call to this routine
7926         // has '&& !bookHit' added to suppress potential sending there (based on 'firstMove').
7927         char buf[MSG_SIZ], *move = bookHit;
7928         if(cps->useSAN) {
7929             int fromX, fromY, toX, toY;
7930             char promoChar;
7931             ChessMove moveType;
7932             move = buf + 30;
7933             if (ParseOneMove(bookHit, forwardMostMove, &moveType,
7934                                  &fromX, &fromY, &toX, &toY, &promoChar)) {
7935                 (void) CoordsToAlgebraic(boards[forwardMostMove],
7936                                     PosFlags(forwardMostMove),
7937                                     fromY, fromX, toY, toX, promoChar, move);
7938             } else {
7939                 if(appData.debugMode) fprintf(debugFP, "Book move could not be parsed\n");
7940                 bookHit = NULL;
7941             }
7942         }
7943         snprintf(buf, MSG_SIZ, "%s%s\n", (cps->useUsermove ? "usermove " : ""), move); // force book move into program supposed to play it
7944         SendToProgram(buf, cps);
7945         if(!initial) firstMove = FALSE; // normally we would clear the firstMove condition after return & sending 'go'
7946     } else if(initial) { // 'go' was needed irrespective of firstMove, and it has to be done in this routine
7947         SendToProgram("go\n", cps);
7948         cps->bookSuspend = FALSE; // after a 'go' we are never suspended
7949     } else { // 'go' might be sent based on 'firstMove' after this routine returns
7950         if(cps->bookSuspend && !firstMove) // 'go' needed, and it will not be done after we return
7951             SendToProgram("go\n", cps);
7952         cps->bookSuspend = FALSE; // anyhow, we will not be suspended after a miss
7953     }
7954     return bookHit; // notify caller of hit, so it can take action to send move to opponent
7955 }
7956
7957 int
7958 LoadError (char *errmess, ChessProgramState *cps)
7959 {   // unloads engine and switches back to -ncp mode if it was first
7960     if(cps->initDone) return FALSE;
7961     cps->isr = NULL; // this should suppress further error popups from breaking pipes
7962     DestroyChildProcess(cps->pr, 9 ); // just to be sure
7963     cps->pr = NoProc; 
7964     if(cps == &first) {
7965         appData.noChessProgram = TRUE;
7966         gameMode = MachinePlaysBlack; ModeHighlight(); // kludge to unmark Machine Black menu
7967         gameMode = BeginningOfGame; ModeHighlight();
7968         SetNCPMode();
7969     }
7970     if(GetDelayedEvent()) CancelDelayedEvent(), ThawUI(); // [HGM] cancel remaining loading effort scheduled after feature timeout
7971     DisplayMessage("", ""); // erase waiting message
7972     if(errmess) DisplayError(errmess, 0); // announce reason, if given
7973     return TRUE;
7974 }
7975
7976 char *savedMessage;
7977 ChessProgramState *savedState;
7978 void
7979 DeferredBookMove (void)
7980 {
7981         if(savedState->lastPing != savedState->lastPong)
7982                     ScheduleDelayedEvent(DeferredBookMove, 10);
7983         else
7984         HandleMachineMove(savedMessage, savedState);
7985 }
7986
7987 static int savedWhitePlayer, savedBlackPlayer, pairingReceived;
7988
7989 void
7990 HandleMachineMove (char *message, ChessProgramState *cps)
7991 {
7992     char machineMove[MSG_SIZ], buf1[MSG_SIZ*10], buf2[MSG_SIZ];
7993     char realname[MSG_SIZ];
7994     int fromX, fromY, toX, toY;
7995     ChessMove moveType;
7996     char promoChar;
7997     char *p, *pv=buf1;
7998     int machineWhite, oldError;
7999     char *bookHit;
8000
8001     if(cps == &pairing && sscanf(message, "%d-%d", &savedWhitePlayer, &savedBlackPlayer) == 2) {
8002         // [HGM] pairing: Mega-hack! Pairing engine also uses this routine (so it could give other WB commands).
8003         if(savedWhitePlayer == 0 || savedBlackPlayer == 0) {
8004             DisplayError(_("Invalid pairing from pairing engine"), 0);
8005             return;
8006         }
8007         pairingReceived = 1;
8008         NextMatchGame();
8009         return; // Skim the pairing messages here.
8010     }
8011
8012     oldError = cps->userError; cps->userError = 0;
8013
8014 FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book hit
8015     /*
8016      * Kludge to ignore BEL characters
8017      */
8018     while (*message == '\007') message++;
8019
8020     /*
8021      * [HGM] engine debug message: ignore lines starting with '#' character
8022      */
8023     if(cps->debug && *message == '#') return;
8024
8025     /*
8026      * Look for book output
8027      */
8028     if (cps == &first && bookRequested) {
8029         if (message[0] == '\t' || message[0] == ' ') {
8030             /* Part of the book output is here; append it */
8031             strcat(bookOutput, message);
8032             strcat(bookOutput, "  \n");
8033             return;
8034         } else if (bookOutput[0] != NULLCHAR) {
8035             /* All of book output has arrived; display it */
8036             char *p = bookOutput;
8037             while (*p != NULLCHAR) {
8038                 if (*p == '\t') *p = ' ';
8039                 p++;
8040             }
8041             DisplayInformation(bookOutput);
8042             bookRequested = FALSE;
8043             /* Fall through to parse the current output */
8044         }
8045     }
8046
8047     /*
8048      * Look for machine move.
8049      */
8050     if ((sscanf(message, "%s %s %s", buf1, buf2, machineMove) == 3 && strcmp(buf2, "...") == 0) ||
8051         (sscanf(message, "%s %s", buf1, machineMove) == 2 && strcmp(buf1, "move") == 0))
8052     {
8053         /* This method is only useful on engines that support ping */
8054         if (cps->lastPing != cps->lastPong) {
8055           if (gameMode == BeginningOfGame) {
8056             /* Extra move from before last new; ignore */
8057             if (appData.debugMode) {
8058                 fprintf(debugFP, "Ignoring extra move from %s\n", cps->which);
8059             }
8060           } else {
8061             if (appData.debugMode) {
8062                 fprintf(debugFP, "Undoing extra move from %s, gameMode %d\n",
8063                         cps->which, gameMode);
8064             }
8065
8066             SendToProgram("undo\n", cps);
8067           }
8068           return;
8069         }
8070
8071         switch (gameMode) {
8072           case BeginningOfGame:
8073             /* Extra move from before last reset; ignore */
8074             if (appData.debugMode) {
8075                 fprintf(debugFP, "Ignoring extra move from %s\n", cps->which);
8076             }
8077             return;
8078
8079           case EndOfGame:
8080           case IcsIdle:
8081           default:
8082             /* Extra move after we tried to stop.  The mode test is
8083                not a reliable way of detecting this problem, but it's
8084                the best we can do on engines that don't support ping.
8085             */
8086             if (appData.debugMode) {
8087                 fprintf(debugFP, "Undoing extra move from %s, gameMode %d\n",
8088                         cps->which, gameMode);
8089             }
8090             SendToProgram("undo\n", cps);
8091             return;
8092
8093           case MachinePlaysWhite:
8094           case IcsPlayingWhite:
8095             machineWhite = TRUE;
8096             break;
8097
8098           case MachinePlaysBlack:
8099           case IcsPlayingBlack:
8100             machineWhite = FALSE;
8101             break;
8102
8103           case TwoMachinesPlay:
8104             machineWhite = (cps->twoMachinesColor[0] == 'w');
8105             break;
8106         }
8107         if (WhiteOnMove(forwardMostMove) != machineWhite) {
8108             if (appData.debugMode) {
8109                 fprintf(debugFP,
8110                         "Ignoring move out of turn by %s, gameMode %d"
8111                         ", forwardMost %d\n",
8112                         cps->which, gameMode, forwardMostMove);
8113             }
8114             return;
8115         }
8116
8117         if(cps->alphaRank) AlphaRank(machineMove, 4);
8118         if (!ParseOneMove(machineMove, forwardMostMove, &moveType,
8119                               &fromX, &fromY, &toX, &toY, &promoChar)) {
8120             /* Machine move could not be parsed; ignore it. */
8121           snprintf(buf1, MSG_SIZ*10, _("Illegal move \"%s\" from %s machine"),
8122                     machineMove, _(cps->which));
8123             DisplayError(buf1, 0);
8124             snprintf(buf1, MSG_SIZ*10, "Xboard: Forfeit due to invalid move: %s (%c%c%c%c) res=%d",
8125                     machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, moveType);
8126             if (gameMode == TwoMachinesPlay) {
8127               GameEnds(machineWhite ? BlackWins : WhiteWins,
8128                        buf1, GE_XBOARD);
8129             }
8130             return;
8131         }
8132
8133         /* [HGM] Apparently legal, but so far only tested with EP_UNKOWN */
8134         /* So we have to redo legality test with true e.p. status here,  */
8135         /* to make sure an illegal e.p. capture does not slip through,   */
8136         /* to cause a forfeit on a justified illegal-move complaint      */
8137         /* of the opponent.                                              */
8138         if( gameMode==TwoMachinesPlay && appData.testLegality ) {
8139            ChessMove moveType;
8140            moveType = LegalityTest(boards[forwardMostMove], PosFlags(forwardMostMove),
8141                              fromY, fromX, toY, toX, promoChar);
8142             if(moveType == IllegalMove) {
8143               snprintf(buf1, MSG_SIZ*10, "Xboard: Forfeit due to illegal move: %s (%c%c%c%c)%c",
8144                         machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, 0);
8145                 GameEnds(machineWhite ? BlackWins : WhiteWins,
8146                            buf1, GE_XBOARD);
8147                 return;
8148            } else if(gameInfo.variant != VariantFischeRandom && gameInfo.variant != VariantCapaRandom)
8149            /* [HGM] Kludge to handle engines that send FRC-style castling
8150               when they shouldn't (like TSCP-Gothic) */
8151            switch(moveType) {
8152              case WhiteASideCastleFR:
8153              case BlackASideCastleFR:
8154                toX+=2;
8155                currentMoveString[2]++;
8156                break;
8157              case WhiteHSideCastleFR:
8158              case BlackHSideCastleFR:
8159                toX--;
8160                currentMoveString[2]--;
8161                break;
8162              default: ; // nothing to do, but suppresses warning of pedantic compilers
8163            }
8164         }
8165         hintRequested = FALSE;
8166         lastHint[0] = NULLCHAR;
8167         bookRequested = FALSE;
8168         /* Program may be pondering now */
8169         cps->maybeThinking = TRUE;
8170         if (cps->sendTime == 2) cps->sendTime = 1;
8171         if (cps->offeredDraw) cps->offeredDraw--;
8172
8173         /* [AS] Save move info*/
8174         pvInfoList[ forwardMostMove ].score = programStats.score;
8175         pvInfoList[ forwardMostMove ].depth = programStats.depth;
8176         pvInfoList[ forwardMostMove ].time =  programStats.time; // [HGM] PGNtime: take time from engine stats
8177
8178         MakeMove(fromX, fromY, toX, toY, promoChar);/*updates forwardMostMove*/
8179
8180         /* [AS] Adjudicate game if needed (note: remember that forwardMostMove now points past the last move) */
8181         if( gameMode == TwoMachinesPlay && adjudicateLossThreshold != 0 && forwardMostMove >= adjudicateLossPlies ) {
8182             int count = 0;
8183
8184             while( count < adjudicateLossPlies ) {
8185                 int score = pvInfoList[ forwardMostMove - count - 1 ].score;
8186
8187                 if( count & 1 ) {
8188                     score = -score; /* Flip score for winning side */
8189                 }
8190
8191                 if( score > adjudicateLossThreshold ) {
8192                     break;
8193                 }
8194
8195                 count++;
8196             }
8197
8198             if( count >= adjudicateLossPlies ) {
8199                 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
8200
8201                 GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
8202                     "Xboard adjudication",
8203                     GE_XBOARD );
8204
8205                 return;
8206             }
8207         }
8208
8209         if(Adjudicate(cps)) {
8210             ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
8211             return; // [HGM] adjudicate: for all automatic game ends
8212         }
8213
8214 #if ZIPPY
8215         if ((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack) &&
8216             first.initDone) {
8217           if(cps->offeredDraw && (signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
8218                 SendToICS(ics_prefix); // [HGM] drawclaim: send caim and move on one line for FICS
8219                 SendToICS("draw ");
8220                 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
8221           }
8222           SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
8223           ics_user_moved = 1;
8224           if(appData.autoKibitz && !appData.icsEngineAnalyze ) { /* [HGM] kibitz: send most-recent PV info to ICS */
8225                 char buf[3*MSG_SIZ];
8226
8227                 snprintf(buf, 3*MSG_SIZ, "kibitz !!! %+.2f/%d (%.2f sec, %u nodes, %.0f knps) PV=%s\n",
8228                         programStats.score / 100.,
8229                         programStats.depth,
8230                         programStats.time / 100.,
8231                         (unsigned int)programStats.nodes,
8232                         (unsigned int)programStats.nodes / (10*abs(programStats.time) + 1.),
8233                         programStats.movelist);
8234                 SendToICS(buf);
8235 if(appData.debugMode) fprintf(debugFP, "nodes = %d, %lld\n", (int) programStats.nodes, programStats.nodes);
8236           }
8237         }
8238 #endif
8239
8240         /* [AS] Clear stats for next move */
8241         ClearProgramStats();
8242         thinkOutput[0] = NULLCHAR;
8243         hiddenThinkOutputState = 0;
8244
8245         bookHit = NULL;
8246         if (gameMode == TwoMachinesPlay) {
8247             /* [HGM] relaying draw offers moved to after reception of move */
8248             /* and interpreting offer as claim if it brings draw condition */
8249             if (cps->offeredDraw == 1 && cps->other->sendDrawOffers) {
8250                 SendToProgram("draw\n", cps->other);
8251             }
8252             if (cps->other->sendTime) {
8253                 SendTimeRemaining(cps->other,
8254                                   cps->other->twoMachinesColor[0] == 'w');
8255             }
8256             bookHit = SendMoveToBookUser(forwardMostMove-1, cps->other, FALSE);
8257             if (firstMove && !bookHit) {
8258                 firstMove = FALSE;
8259                 if (cps->other->useColors) {
8260                   SendToProgram(cps->other->twoMachinesColor, cps->other);
8261                 }
8262                 SendToProgram("go\n", cps->other);
8263             }
8264             cps->other->maybeThinking = TRUE;
8265         }
8266
8267         ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
8268
8269         if (!pausing && appData.ringBellAfterMoves) {
8270             RingBell();
8271         }
8272
8273         /*
8274          * Reenable menu items that were disabled while
8275          * machine was thinking
8276          */
8277         if (gameMode != TwoMachinesPlay)
8278             SetUserThinkingEnables();
8279
8280         // [HGM] book: after book hit opponent has received move and is now in force mode
8281         // force the book reply into it, and then fake that it outputted this move by jumping
8282         // back to the beginning of HandleMachineMove, with cps toggled and message set to this move
8283         if(bookHit) {
8284                 static char bookMove[MSG_SIZ]; // a bit generous?
8285
8286                 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
8287                 strcat(bookMove, bookHit);
8288                 message = bookMove;
8289                 cps = cps->other;
8290                 programStats.nodes = programStats.depth = programStats.time =
8291                 programStats.score = programStats.got_only_move = 0;
8292                 sprintf(programStats.movelist, "%s (xbook)", bookHit);
8293
8294                 if(cps->lastPing != cps->lastPong) {
8295                     savedMessage = message; // args for deferred call
8296                     savedState = cps;
8297                     ScheduleDelayedEvent(DeferredBookMove, 10);
8298                     return;
8299                 }
8300                 goto FakeBookMove;
8301         }
8302
8303         return;
8304     }
8305
8306     /* Set special modes for chess engines.  Later something general
8307      *  could be added here; for now there is just one kludge feature,
8308      *  needed because Crafty 15.10 and earlier don't ignore SIGINT
8309      *  when "xboard" is given as an interactive command.
8310      */
8311     if (strncmp(message, "kibitz Hello from Crafty", 24) == 0) {
8312         cps->useSigint = FALSE;
8313         cps->useSigterm = FALSE;
8314     }
8315     if (strncmp(message, "feature ", 8) == 0) { // [HGM] moved forward to pre-empt non-compliant commands
8316       ParseFeatures(message+8, cps);
8317       return; // [HGM] This return was missing, causing option features to be recognized as non-compliant commands!
8318     }
8319
8320     if ((!appData.testLegality || gameInfo.variant == VariantFairy) && 
8321                                         !strncmp(message, "setup ", 6)) { // [HGM] allow first engine to define opening position
8322       int dummy, s=6; char buf[MSG_SIZ];
8323       if(appData.icsActive || forwardMostMove != 0 || cps != &first) return;
8324       if(sscanf(message, "setup (%s", buf) == 1) s = 8 + strlen(buf), buf[s-9] = NULLCHAR, SetCharTable(pieceToChar, buf);
8325       if(startedFromSetupPosition) return;
8326       ParseFEN(boards[0], &dummy, message+s);
8327       DrawPosition(TRUE, boards[0]);
8328       startedFromSetupPosition = TRUE;
8329       return;
8330     }
8331     /* [HGM] Allow engine to set up a position. Don't ask me why one would
8332      * want this, I was asked to put it in, and obliged.
8333      */
8334     if (!strncmp(message, "setboard ", 9)) {
8335         Board initial_position;
8336
8337         GameEnds(GameUnfinished, "Engine aborts game", GE_XBOARD);
8338
8339         if (!ParseFEN(initial_position, &blackPlaysFirst, message + 9)) {
8340             DisplayError(_("Bad FEN received from engine"), 0);
8341             return ;
8342         } else {
8343            Reset(TRUE, FALSE);
8344            CopyBoard(boards[0], initial_position);
8345            initialRulePlies = FENrulePlies;
8346            if(blackPlaysFirst) gameMode = MachinePlaysWhite;
8347            else gameMode = MachinePlaysBlack;
8348            DrawPosition(FALSE, boards[currentMove]);
8349         }
8350         return;
8351     }
8352
8353     /*
8354      * Look for communication commands
8355      */
8356     if (!strncmp(message, "telluser ", 9)) {
8357         if(message[9] == '\\' && message[10] == '\\')
8358             EscapeExpand(message+9, message+11); // [HGM] esc: allow escape sequences in popup box
8359         PlayTellSound();
8360         DisplayNote(message + 9);
8361         return;
8362     }
8363     if (!strncmp(message, "tellusererror ", 14)) {
8364         cps->userError = 1;
8365         if(message[14] == '\\' && message[15] == '\\')
8366             EscapeExpand(message+14, message+16); // [HGM] esc: allow escape sequences in popup box
8367         PlayTellSound();
8368         DisplayError(message + 14, 0);
8369         return;
8370     }
8371     if (!strncmp(message, "tellopponent ", 13)) {
8372       if (appData.icsActive) {
8373         if (loggedOn) {
8374           snprintf(buf1, sizeof(buf1), "%ssay %s\n", ics_prefix, message + 13);
8375           SendToICS(buf1);
8376         }
8377       } else {
8378         DisplayNote(message + 13);
8379       }
8380       return;
8381     }
8382     if (!strncmp(message, "tellothers ", 11)) {
8383       if (appData.icsActive) {
8384         if (loggedOn) {
8385           snprintf(buf1, sizeof(buf1), "%swhisper %s\n", ics_prefix, message + 11);
8386           SendToICS(buf1);
8387         }
8388       }
8389       return;
8390     }
8391     if (!strncmp(message, "tellall ", 8)) {
8392       if (appData.icsActive) {
8393         if (loggedOn) {
8394           snprintf(buf1, sizeof(buf1), "%skibitz %s\n", ics_prefix, message + 8);
8395           SendToICS(buf1);
8396         }
8397       } else {
8398         DisplayNote(message + 8);
8399       }
8400       return;
8401     }
8402     if (strncmp(message, "warning", 7) == 0) {
8403         /* Undocumented feature, use tellusererror in new code */
8404         DisplayError(message, 0);
8405         return;
8406     }
8407     if (sscanf(message, "askuser %s %[^\n]", buf1, buf2) == 2) {
8408         safeStrCpy(realname, cps->tidy, sizeof(realname)/sizeof(realname[0]));
8409         strcat(realname, " query");
8410         AskQuestion(realname, buf2, buf1, cps->pr);
8411         return;
8412     }
8413     /* Commands from the engine directly to ICS.  We don't allow these to be
8414      *  sent until we are logged on. Crafty kibitzes have been known to
8415      *  interfere with the login process.
8416      */
8417     if (loggedOn) {
8418         if (!strncmp(message, "tellics ", 8)) {
8419             SendToICS(message + 8);
8420             SendToICS("\n");
8421             return;
8422         }
8423         if (!strncmp(message, "tellicsnoalias ", 15)) {
8424             SendToICS(ics_prefix);
8425             SendToICS(message + 15);
8426             SendToICS("\n");
8427             return;
8428         }
8429         /* The following are for backward compatibility only */
8430         if (!strncmp(message,"whisper",7) || !strncmp(message,"kibitz",6) ||
8431             !strncmp(message,"draw",4) || !strncmp(message,"tell",3)) {
8432             SendToICS(ics_prefix);
8433             SendToICS(message);
8434             SendToICS("\n");
8435             return;
8436         }
8437     }
8438     if (sscanf(message, "pong %d", &cps->lastPong) == 1) {
8439         return;
8440     }
8441     /*
8442      * If the move is illegal, cancel it and redraw the board.
8443      * Also deal with other error cases.  Matching is rather loose
8444      * here to accommodate engines written before the spec.
8445      */
8446     if (strncmp(message + 1, "llegal move", 11) == 0 ||
8447         strncmp(message, "Error", 5) == 0) {
8448         if (StrStr(message, "name") ||
8449             StrStr(message, "rating") || StrStr(message, "?") ||
8450             StrStr(message, "result") || StrStr(message, "board") ||
8451             StrStr(message, "bk") || StrStr(message, "computer") ||
8452             StrStr(message, "variant") || StrStr(message, "hint") ||
8453             StrStr(message, "random") || StrStr(message, "depth") ||
8454             StrStr(message, "accepted")) {
8455             return;
8456         }
8457         if (StrStr(message, "protover")) {
8458           /* Program is responding to input, so it's apparently done
8459              initializing, and this error message indicates it is
8460              protocol version 1.  So we don't need to wait any longer
8461              for it to initialize and send feature commands. */
8462           FeatureDone(cps, 1);
8463           cps->protocolVersion = 1;
8464           return;
8465         }
8466         cps->maybeThinking = FALSE;
8467
8468         if (StrStr(message, "draw")) {
8469             /* Program doesn't have "draw" command */
8470             cps->sendDrawOffers = 0;
8471             return;
8472         }
8473         if (cps->sendTime != 1 &&
8474             (StrStr(message, "time") || StrStr(message, "otim"))) {
8475           /* Program apparently doesn't have "time" or "otim" command */
8476           cps->sendTime = 0;
8477           return;
8478         }
8479         if (StrStr(message, "analyze")) {
8480             cps->analysisSupport = FALSE;
8481             cps->analyzing = FALSE;
8482 //          Reset(FALSE, TRUE); // [HGM] this caused discrepancy between display and internal state!
8483             EditGameEvent(); // [HGM] try to preserve loaded game
8484             snprintf(buf2,MSG_SIZ, _("%s does not support analysis"), cps->tidy);
8485             DisplayError(buf2, 0);
8486             return;
8487         }
8488         if (StrStr(message, "(no matching move)st")) {
8489           /* Special kludge for GNU Chess 4 only */
8490           cps->stKludge = TRUE;
8491           SendTimeControl(cps, movesPerSession, timeControl,
8492                           timeIncrement, appData.searchDepth,
8493                           searchTime);
8494           return;
8495         }
8496         if (StrStr(message, "(no matching move)sd")) {
8497           /* Special kludge for GNU Chess 4 only */
8498           cps->sdKludge = TRUE;
8499           SendTimeControl(cps, movesPerSession, timeControl,
8500                           timeIncrement, appData.searchDepth,
8501                           searchTime);
8502           return;
8503         }
8504         if (!StrStr(message, "llegal")) {
8505             return;
8506         }
8507         if (gameMode == BeginningOfGame || gameMode == EndOfGame ||
8508             gameMode == IcsIdle) return;
8509         if (forwardMostMove <= backwardMostMove) return;
8510         if (pausing) PauseEvent();
8511       if(appData.forceIllegal) {
8512             // [HGM] illegal: machine refused move; force position after move into it
8513           SendToProgram("force\n", cps);
8514           if(!cps->useSetboard) { // hideous kludge on kludge, because SendBoard sucks.
8515                 // we have a real problem now, as SendBoard will use the a2a3 kludge
8516                 // when black is to move, while there might be nothing on a2 or black
8517                 // might already have the move. So send the board as if white has the move.
8518                 // But first we must change the stm of the engine, as it refused the last move
8519                 SendBoard(cps, 0); // always kludgeless, as white is to move on boards[0]
8520                 if(WhiteOnMove(forwardMostMove)) {
8521                     SendToProgram("a7a6\n", cps); // for the engine black still had the move
8522                     SendBoard(cps, forwardMostMove); // kludgeless board
8523                 } else {
8524                     SendToProgram("a2a3\n", cps); // for the engine white still had the move
8525                     CopyBoard(boards[forwardMostMove+1], boards[forwardMostMove]);
8526                     SendBoard(cps, forwardMostMove+1); // kludgeless board
8527                 }
8528           } else SendBoard(cps, forwardMostMove); // FEN case, also sets stm properly
8529             if(gameMode == MachinePlaysWhite || gameMode == MachinePlaysBlack ||
8530                  gameMode == TwoMachinesPlay)
8531               SendToProgram("go\n", cps);
8532             return;
8533       } else
8534         if (gameMode == PlayFromGameFile) {
8535             /* Stop reading this game file */
8536             gameMode = EditGame;
8537             ModeHighlight();
8538         }
8539         /* [HGM] illegal-move claim should forfeit game when Xboard */
8540         /* only passes fully legal moves                            */
8541         if( appData.testLegality && gameMode == TwoMachinesPlay ) {
8542             GameEnds( cps->twoMachinesColor[0] == 'w' ? BlackWins : WhiteWins,
8543                                 "False illegal-move claim", GE_XBOARD );
8544             return; // do not take back move we tested as valid
8545         }
8546         currentMove = forwardMostMove-1;
8547         DisplayMove(currentMove-1); /* before DisplayMoveError */
8548         SwitchClocks(forwardMostMove-1); // [HGM] race
8549         DisplayBothClocks();
8550         snprintf(buf1, 10*MSG_SIZ, _("Illegal move \"%s\" (rejected by %s chess program)"),
8551                 parseList[currentMove], _(cps->which));
8552         DisplayMoveError(buf1);
8553         DrawPosition(FALSE, boards[currentMove]);
8554
8555         SetUserThinkingEnables();
8556         return;
8557     }
8558     if (strncmp(message, "time", 4) == 0 && StrStr(message, "Illegal")) {
8559         /* Program has a broken "time" command that
8560            outputs a string not ending in newline.
8561            Don't use it. */
8562         cps->sendTime = 0;
8563     }
8564
8565     /*
8566      * If chess program startup fails, exit with an error message.
8567      * Attempts to recover here are futile. [HGM] Well, we try anyway
8568      */
8569     if ((StrStr(message, "unknown host") != NULL)
8570         || (StrStr(message, "No remote directory") != NULL)
8571         || (StrStr(message, "not found") != NULL)
8572         || (StrStr(message, "No such file") != NULL)
8573         || (StrStr(message, "can't alloc") != NULL)
8574         || (StrStr(message, "Permission denied") != NULL)) {
8575
8576         cps->maybeThinking = FALSE;
8577         snprintf(buf1, sizeof(buf1), _("Failed to start %s chess program %s on %s: %s\n"),
8578                 _(cps->which), cps->program, cps->host, message);
8579         RemoveInputSource(cps->isr);
8580         if(appData.icsActive) DisplayFatalError(buf1, 0, 1); else {
8581             if(LoadError(oldError ? NULL : buf1, cps)) return; // error has then been handled by LoadError
8582             if(!oldError) DisplayError(buf1, 0); // if reason neatly announced, suppress general error popup
8583         }
8584         return;
8585     }
8586
8587     /*
8588      * Look for hint output
8589      */
8590     if (sscanf(message, "Hint: %s", buf1) == 1) {
8591         if (cps == &first && hintRequested) {
8592             hintRequested = FALSE;
8593             if (ParseOneMove(buf1, forwardMostMove, &moveType,
8594                                  &fromX, &fromY, &toX, &toY, &promoChar)) {
8595                 (void) CoordsToAlgebraic(boards[forwardMostMove],
8596                                     PosFlags(forwardMostMove),
8597                                     fromY, fromX, toY, toX, promoChar, buf1);
8598                 snprintf(buf2, sizeof(buf2), _("Hint: %s"), buf1);
8599                 DisplayInformation(buf2);
8600             } else {
8601                 /* Hint move could not be parsed!? */
8602               snprintf(buf2, sizeof(buf2),
8603                         _("Illegal hint move \"%s\"\nfrom %s chess program"),
8604                         buf1, _(cps->which));
8605                 DisplayError(buf2, 0);
8606             }
8607         } else {
8608           safeStrCpy(lastHint, buf1, sizeof(lastHint)/sizeof(lastHint[0]));
8609         }
8610         return;
8611     }
8612
8613     /*
8614      * Ignore other messages if game is not in progress
8615      */
8616     if (gameMode == BeginningOfGame || gameMode == EndOfGame ||
8617         gameMode == IcsIdle || cps->lastPing != cps->lastPong) return;
8618
8619     /*
8620      * look for win, lose, draw, or draw offer
8621      */
8622     if (strncmp(message, "1-0", 3) == 0) {
8623         char *p, *q, *r = "";
8624         p = strchr(message, '{');
8625         if (p) {
8626             q = strchr(p, '}');
8627             if (q) {
8628                 *q = NULLCHAR;
8629                 r = p + 1;
8630             }
8631         }
8632         GameEnds(WhiteWins, r, GE_ENGINE1 + (cps != &first)); /* [HGM] pass claimer indication for claim test */
8633         return;
8634     } else if (strncmp(message, "0-1", 3) == 0) {
8635         char *p, *q, *r = "";
8636         p = strchr(message, '{');
8637         if (p) {
8638             q = strchr(p, '}');
8639             if (q) {
8640                 *q = NULLCHAR;
8641                 r = p + 1;
8642             }
8643         }
8644         /* Kludge for Arasan 4.1 bug */
8645         if (strcmp(r, "Black resigns") == 0) {
8646             GameEnds(WhiteWins, r, GE_ENGINE1 + (cps != &first));
8647             return;
8648         }
8649         GameEnds(BlackWins, r, GE_ENGINE1 + (cps != &first));
8650         return;
8651     } else if (strncmp(message, "1/2", 3) == 0) {
8652         char *p, *q, *r = "";
8653         p = strchr(message, '{');
8654         if (p) {
8655             q = strchr(p, '}');
8656             if (q) {
8657                 *q = NULLCHAR;
8658                 r = p + 1;
8659             }
8660         }
8661
8662         GameEnds(GameIsDrawn, r, GE_ENGINE1 + (cps != &first));
8663         return;
8664
8665     } else if (strncmp(message, "White resign", 12) == 0) {
8666         GameEnds(BlackWins, "White resigns", GE_ENGINE1 + (cps != &first));
8667         return;
8668     } else if (strncmp(message, "Black resign", 12) == 0) {
8669         GameEnds(WhiteWins, "Black resigns", GE_ENGINE1 + (cps != &first));
8670         return;
8671     } else if (strncmp(message, "White matches", 13) == 0 ||
8672                strncmp(message, "Black matches", 13) == 0   ) {
8673         /* [HGM] ignore GNUShogi noises */
8674         return;
8675     } else if (strncmp(message, "White", 5) == 0 &&
8676                message[5] != '(' &&
8677                StrStr(message, "Black") == NULL) {
8678         GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
8679         return;
8680     } else if (strncmp(message, "Black", 5) == 0 &&
8681                message[5] != '(') {
8682         GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
8683         return;
8684     } else if (strcmp(message, "resign") == 0 ||
8685                strcmp(message, "computer resigns") == 0) {
8686         switch (gameMode) {
8687           case MachinePlaysBlack:
8688           case IcsPlayingBlack:
8689             GameEnds(WhiteWins, "Black resigns", GE_ENGINE);
8690             break;
8691           case MachinePlaysWhite:
8692           case IcsPlayingWhite:
8693             GameEnds(BlackWins, "White resigns", GE_ENGINE);
8694             break;
8695           case TwoMachinesPlay:
8696             if (cps->twoMachinesColor[0] == 'w')
8697               GameEnds(BlackWins, "White resigns", GE_ENGINE1 + (cps != &first));
8698             else
8699               GameEnds(WhiteWins, "Black resigns", GE_ENGINE1 + (cps != &first));
8700             break;
8701           default:
8702             /* can't happen */
8703             break;
8704         }
8705         return;
8706     } else if (strncmp(message, "opponent mates", 14) == 0) {
8707         switch (gameMode) {
8708           case MachinePlaysBlack:
8709           case IcsPlayingBlack:
8710             GameEnds(WhiteWins, "White mates", GE_ENGINE);
8711             break;
8712           case MachinePlaysWhite:
8713           case IcsPlayingWhite:
8714             GameEnds(BlackWins, "Black mates", GE_ENGINE);
8715             break;
8716           case TwoMachinesPlay:
8717             if (cps->twoMachinesColor[0] == 'w')
8718               GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
8719             else
8720               GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
8721             break;
8722           default:
8723             /* can't happen */
8724             break;
8725         }
8726         return;
8727     } else if (strncmp(message, "computer mates", 14) == 0) {
8728         switch (gameMode) {
8729           case MachinePlaysBlack:
8730           case IcsPlayingBlack:
8731             GameEnds(BlackWins, "Black mates", GE_ENGINE1);
8732             break;
8733           case MachinePlaysWhite:
8734           case IcsPlayingWhite:
8735             GameEnds(WhiteWins, "White mates", GE_ENGINE);
8736             break;
8737           case TwoMachinesPlay:
8738             if (cps->twoMachinesColor[0] == 'w')
8739               GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
8740             else
8741               GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
8742             break;
8743           default:
8744             /* can't happen */
8745             break;
8746         }
8747         return;
8748     } else if (strncmp(message, "checkmate", 9) == 0) {
8749         if (WhiteOnMove(forwardMostMove)) {
8750             GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
8751         } else {
8752             GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
8753         }
8754         return;
8755     } else if (strstr(message, "Draw") != NULL ||
8756                strstr(message, "game is a draw") != NULL) {
8757         GameEnds(GameIsDrawn, "Draw", GE_ENGINE1 + (cps != &first));
8758         return;
8759     } else if (strstr(message, "offer") != NULL &&
8760                strstr(message, "draw") != NULL) {
8761 #if ZIPPY
8762         if (appData.zippyPlay && first.initDone) {
8763             /* Relay offer to ICS */
8764             SendToICS(ics_prefix);
8765             SendToICS("draw\n");
8766         }
8767 #endif
8768         cps->offeredDraw = 2; /* valid until this engine moves twice */
8769         if (gameMode == TwoMachinesPlay) {
8770             if (cps->other->offeredDraw) {
8771                 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
8772             /* [HGM] in two-machine mode we delay relaying draw offer      */
8773             /* until after we also have move, to see if it is really claim */
8774             }
8775         } else if (gameMode == MachinePlaysWhite ||
8776                    gameMode == MachinePlaysBlack) {
8777           if (userOfferedDraw) {
8778             DisplayInformation(_("Machine accepts your draw offer"));
8779             GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
8780           } else {
8781             DisplayInformation(_("Machine offers a draw\nSelect Action / Draw to agree"));
8782           }
8783         }
8784     }
8785
8786
8787     /*
8788      * Look for thinking output
8789      */
8790     if ( appData.showThinking // [HGM] thinking: test all options that cause this output
8791           || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp()
8792                                 ) {
8793         int plylev, mvleft, mvtot, curscore, time;
8794         char mvname[MOVE_LEN];
8795         u64 nodes; // [DM]
8796         char plyext;
8797         int ignore = FALSE;
8798         int prefixHint = FALSE;
8799         mvname[0] = NULLCHAR;
8800
8801         switch (gameMode) {
8802           case MachinePlaysBlack:
8803           case IcsPlayingBlack:
8804             if (WhiteOnMove(forwardMostMove)) prefixHint = TRUE;
8805             break;
8806           case MachinePlaysWhite:
8807           case IcsPlayingWhite:
8808             if (!WhiteOnMove(forwardMostMove)) prefixHint = TRUE;
8809             break;
8810           case AnalyzeMode:
8811           case AnalyzeFile:
8812             break;
8813           case IcsObserving: /* [DM] icsEngineAnalyze */
8814             if (!appData.icsEngineAnalyze) ignore = TRUE;
8815             break;
8816           case TwoMachinesPlay:
8817             if ((cps->twoMachinesColor[0] == 'w') != WhiteOnMove(forwardMostMove)) {
8818                 ignore = TRUE;
8819             }
8820             break;
8821           default:
8822             ignore = TRUE;
8823             break;
8824         }
8825
8826         if (!ignore) {
8827             ChessProgramStats tempStats = programStats; // [HGM] info: filter out info lines
8828             buf1[0] = NULLCHAR;
8829             if (sscanf(message, "%d%c %d %d " u64Display " %[^\n]\n",
8830                        &plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5) {
8831
8832                 if (plyext != ' ' && plyext != '\t') {
8833                     time *= 100;
8834                 }
8835
8836                 /* [AS] Negate score if machine is playing black and reporting absolute scores */
8837                 if( cps->scoreIsAbsolute &&
8838                     ( gameMode == MachinePlaysBlack ||
8839                       gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b' ||
8840                       gameMode == IcsPlayingBlack ||     // [HGM] also add other situations where engine should report black POV
8841                      (gameMode == AnalyzeMode || gameMode == AnalyzeFile || gameMode == IcsObserving && appData.icsEngineAnalyze) &&
8842                      !WhiteOnMove(currentMove)
8843                     ) )
8844                 {
8845                     curscore = -curscore;
8846                 }
8847
8848                 if(appData.pvSAN[cps==&second]) pv = PvToSAN(buf1);
8849
8850                 if(serverMoves && (time > 100 || time == 0 && plylev > 7)) {
8851                         char buf[MSG_SIZ];
8852                         FILE *f;
8853                         snprintf(buf, MSG_SIZ, "%s", appData.serverMovesName);
8854                         buf[strlen(buf)-1] = gameMode == MachinePlaysWhite ? 'w' :
8855                                              gameMode == MachinePlaysBlack ? 'b' : cps->twoMachinesColor[0];
8856                         if(appData.debugMode) fprintf(debugFP, "write PV on file '%s'\n", buf);
8857                         if(f = fopen(buf, "w")) { // export PV to applicable PV file
8858                                 fprintf(f, "%5.2f/%-2d %s", curscore/100., plylev, pv);
8859                                 fclose(f);
8860                         } else DisplayError(_("failed writing PV"), 0);
8861                 }
8862
8863                 tempStats.depth = plylev;
8864                 tempStats.nodes = nodes;
8865                 tempStats.time = time;
8866                 tempStats.score = curscore;
8867                 tempStats.got_only_move = 0;
8868
8869                 if(cps->nps >= 0) { /* [HGM] nps: use engine nodes or time to decrement clock */
8870                         int ticklen;
8871
8872                         if(cps->nps == 0) ticklen = 10*time;                    // use engine reported time
8873                         else ticklen = (1000. * u64ToDouble(nodes)) / cps->nps; // convert node count to time
8874                         if(WhiteOnMove(forwardMostMove) && (gameMode == MachinePlaysWhite ||
8875                                                 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'w'))
8876                              whiteTimeRemaining = timeRemaining[0][forwardMostMove] - ticklen;
8877                         if(!WhiteOnMove(forwardMostMove) && (gameMode == MachinePlaysBlack ||
8878                                                 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b'))
8879                              blackTimeRemaining = timeRemaining[1][forwardMostMove] - ticklen;
8880                 }
8881
8882                 /* Buffer overflow protection */
8883                 if (pv[0] != NULLCHAR) {
8884                     if (strlen(pv) >= sizeof(tempStats.movelist)
8885                         && appData.debugMode) {
8886                         fprintf(debugFP,
8887                                 "PV is too long; using the first %u bytes.\n",
8888                                 (unsigned) sizeof(tempStats.movelist) - 1);
8889                     }
8890
8891                     safeStrCpy( tempStats.movelist, pv, sizeof(tempStats.movelist)/sizeof(tempStats.movelist[0]) );
8892                 } else {
8893                     sprintf(tempStats.movelist, " no PV\n");
8894                 }
8895
8896                 if (tempStats.seen_stat) {
8897                     tempStats.ok_to_send = 1;
8898                 }
8899
8900                 if (strchr(tempStats.movelist, '(') != NULL) {
8901                     tempStats.line_is_book = 1;
8902                     tempStats.nr_moves = 0;
8903                     tempStats.moves_left = 0;
8904                 } else {
8905                     tempStats.line_is_book = 0;
8906                 }
8907
8908                     if(tempStats.score != 0 || tempStats.nodes != 0 || tempStats.time != 0)
8909                         programStats = tempStats; // [HGM] info: only set stats if genuine PV and not an info line
8910
8911                 SendProgramStatsToFrontend( cps, &tempStats );
8912
8913                 /*
8914                     [AS] Protect the thinkOutput buffer from overflow... this
8915                     is only useful if buf1 hasn't overflowed first!
8916                 */
8917                 snprintf(thinkOutput, sizeof(thinkOutput)/sizeof(thinkOutput[0]), "[%d]%c%+.2f %s%s",
8918                          plylev,
8919                          (gameMode == TwoMachinesPlay ?
8920                           ToUpper(cps->twoMachinesColor[0]) : ' '),
8921                          ((double) curscore) / 100.0,
8922                          prefixHint ? lastHint : "",
8923                          prefixHint ? " " : "" );
8924
8925                 if( buf1[0] != NULLCHAR ) {
8926                     unsigned max_len = sizeof(thinkOutput) - strlen(thinkOutput) - 1;
8927
8928                     if( strlen(pv) > max_len ) {
8929                         if( appData.debugMode) {
8930                             fprintf(debugFP,"PV is too long for thinkOutput, truncating.\n");
8931                         }
8932                         pv[max_len+1] = '\0';
8933                     }
8934
8935                     strcat( thinkOutput, pv);
8936                 }
8937
8938                 if (currentMove == forwardMostMove || gameMode == AnalyzeMode
8939                         || gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
8940                     DisplayMove(currentMove - 1);
8941                 }
8942                 return;
8943
8944             } else if ((p=StrStr(message, "(only move)")) != NULL) {
8945                 /* crafty (9.25+) says "(only move) <move>"
8946                  * if there is only 1 legal move
8947                  */
8948                 sscanf(p, "(only move) %s", buf1);
8949                 snprintf(thinkOutput, sizeof(thinkOutput)/sizeof(thinkOutput[0]), "%s (only move)", buf1);
8950                 sprintf(programStats.movelist, "%s (only move)", buf1);
8951                 programStats.depth = 1;
8952                 programStats.nr_moves = 1;
8953                 programStats.moves_left = 1;
8954                 programStats.nodes = 1;
8955                 programStats.time = 1;
8956                 programStats.got_only_move = 1;
8957
8958                 /* Not really, but we also use this member to
8959                    mean "line isn't going to change" (Crafty
8960                    isn't searching, so stats won't change) */
8961                 programStats.line_is_book = 1;
8962
8963                 SendProgramStatsToFrontend( cps, &programStats );
8964
8965                 if (currentMove == forwardMostMove || gameMode==AnalyzeMode ||
8966                            gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
8967                     DisplayMove(currentMove - 1);
8968                 }
8969                 return;
8970             } else if (sscanf(message,"stat01: %d " u64Display " %d %d %d %s",
8971                               &time, &nodes, &plylev, &mvleft,
8972                               &mvtot, mvname) >= 5) {
8973                 /* The stat01: line is from Crafty (9.29+) in response
8974                    to the "." command */
8975                 programStats.seen_stat = 1;
8976                 cps->maybeThinking = TRUE;
8977
8978                 if (programStats.got_only_move || !appData.periodicUpdates)
8979                   return;
8980
8981                 programStats.depth = plylev;
8982                 programStats.time = time;
8983                 programStats.nodes = nodes;
8984                 programStats.moves_left = mvleft;
8985                 programStats.nr_moves = mvtot;
8986                 safeStrCpy(programStats.move_name, mvname, sizeof(programStats.move_name)/sizeof(programStats.move_name[0]));
8987                 programStats.ok_to_send = 1;
8988                 programStats.movelist[0] = '\0';
8989
8990                 SendProgramStatsToFrontend( cps, &programStats );
8991
8992                 return;
8993
8994             } else if (strncmp(message,"++",2) == 0) {
8995                 /* Crafty 9.29+ outputs this */
8996                 programStats.got_fail = 2;
8997                 return;
8998
8999             } else if (strncmp(message,"--",2) == 0) {
9000                 /* Crafty 9.29+ outputs this */
9001                 programStats.got_fail = 1;
9002                 return;
9003
9004             } else if (thinkOutput[0] != NULLCHAR &&
9005                        strncmp(message, "    ", 4) == 0) {
9006                 unsigned message_len;
9007
9008                 p = message;
9009                 while (*p && *p == ' ') p++;
9010
9011                 message_len = strlen( p );
9012
9013                 /* [AS] Avoid buffer overflow */
9014                 if( sizeof(thinkOutput) - strlen(thinkOutput) - 1 > message_len ) {
9015                     strcat(thinkOutput, " ");
9016                     strcat(thinkOutput, p);
9017                 }
9018
9019                 if( sizeof(programStats.movelist) - strlen(programStats.movelist) - 1 > message_len ) {
9020                     strcat(programStats.movelist, " ");
9021                     strcat(programStats.movelist, p);
9022                 }
9023
9024                 if (currentMove == forwardMostMove || gameMode==AnalyzeMode ||
9025                            gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
9026                     DisplayMove(currentMove - 1);
9027                 }
9028                 return;
9029             }
9030         }
9031         else {
9032             buf1[0] = NULLCHAR;
9033
9034             if (sscanf(message, "%d%c %d %d " u64Display " %[^\n]\n",
9035                        &plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5)
9036             {
9037                 ChessProgramStats cpstats;
9038
9039                 if (plyext != ' ' && plyext != '\t') {
9040                     time *= 100;
9041                 }
9042
9043                 /* [AS] Negate score if machine is playing black and reporting absolute scores */
9044                 if( cps->scoreIsAbsolute && ((gameMode == MachinePlaysBlack) || (gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b')) ) {
9045                     curscore = -curscore;
9046                 }
9047
9048                 cpstats.depth = plylev;
9049                 cpstats.nodes = nodes;
9050                 cpstats.time = time;
9051                 cpstats.score = curscore;
9052                 cpstats.got_only_move = 0;
9053                 cpstats.movelist[0] = '\0';
9054
9055                 if (buf1[0] != NULLCHAR) {
9056                     safeStrCpy( cpstats.movelist, buf1, sizeof(cpstats.movelist)/sizeof(cpstats.movelist[0]) );
9057                 }
9058
9059                 cpstats.ok_to_send = 0;
9060                 cpstats.line_is_book = 0;
9061                 cpstats.nr_moves = 0;
9062                 cpstats.moves_left = 0;
9063
9064                 SendProgramStatsToFrontend( cps, &cpstats );
9065             }
9066         }
9067     }
9068 }
9069
9070
9071 /* Parse a game score from the character string "game", and
9072    record it as the history of the current game.  The game
9073    score is NOT assumed to start from the standard position.
9074    The display is not updated in any way.
9075    */
9076 void
9077 ParseGameHistory (char *game)
9078 {
9079     ChessMove moveType;
9080     int fromX, fromY, toX, toY, boardIndex;
9081     char promoChar;
9082     char *p, *q;
9083     char buf[MSG_SIZ];
9084
9085     if (appData.debugMode)
9086       fprintf(debugFP, "Parsing game history: %s\n", game);
9087
9088     if (gameInfo.event == NULL) gameInfo.event = StrSave("ICS game");
9089     gameInfo.site = StrSave(appData.icsHost);
9090     gameInfo.date = PGNDate();
9091     gameInfo.round = StrSave("-");
9092
9093     /* Parse out names of players */
9094     while (*game == ' ') game++;
9095     p = buf;
9096     while (*game != ' ') *p++ = *game++;
9097     *p = NULLCHAR;
9098     gameInfo.white = StrSave(buf);
9099     while (*game == ' ') game++;
9100     p = buf;
9101     while (*game != ' ' && *game != '\n') *p++ = *game++;
9102     *p = NULLCHAR;
9103     gameInfo.black = StrSave(buf);
9104
9105     /* Parse moves */
9106     boardIndex = blackPlaysFirst ? 1 : 0;
9107     yynewstr(game);
9108     for (;;) {
9109         yyboardindex = boardIndex;
9110         moveType = (ChessMove) Myylex();
9111         switch (moveType) {
9112           case IllegalMove:             /* maybe suicide chess, etc. */
9113   if (appData.debugMode) {
9114     fprintf(debugFP, "Illegal move from ICS: '%s'\n", yy_text);
9115     fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
9116     setbuf(debugFP, NULL);
9117   }
9118           case WhitePromotion:
9119           case BlackPromotion:
9120           case WhiteNonPromotion:
9121           case BlackNonPromotion:
9122           case NormalMove:
9123           case WhiteCapturesEnPassant:
9124           case BlackCapturesEnPassant:
9125           case WhiteKingSideCastle:
9126           case WhiteQueenSideCastle:
9127           case BlackKingSideCastle:
9128           case BlackQueenSideCastle:
9129           case WhiteKingSideCastleWild:
9130           case WhiteQueenSideCastleWild:
9131           case BlackKingSideCastleWild:
9132           case BlackQueenSideCastleWild:
9133           /* PUSH Fabien */
9134           case WhiteHSideCastleFR:
9135           case WhiteASideCastleFR:
9136           case BlackHSideCastleFR:
9137           case BlackASideCastleFR:
9138           /* POP Fabien */
9139             fromX = currentMoveString[0] - AAA;
9140             fromY = currentMoveString[1] - ONE;
9141             toX = currentMoveString[2] - AAA;
9142             toY = currentMoveString[3] - ONE;
9143             promoChar = currentMoveString[4];
9144             break;
9145           case WhiteDrop:
9146           case BlackDrop:
9147             if(currentMoveString[0] == '@') continue; // no null moves in ICS mode!
9148             fromX = moveType == WhiteDrop ?
9149               (int) CharToPiece(ToUpper(currentMoveString[0])) :
9150             (int) CharToPiece(ToLower(currentMoveString[0]));
9151             fromY = DROP_RANK;
9152             toX = currentMoveString[2] - AAA;
9153             toY = currentMoveString[3] - ONE;
9154             promoChar = NULLCHAR;
9155             break;
9156           case AmbiguousMove:
9157             /* bug? */
9158             snprintf(buf, MSG_SIZ, _("Ambiguous move in ICS output: \"%s\""), yy_text);
9159   if (appData.debugMode) {
9160     fprintf(debugFP, "Ambiguous move from ICS: '%s'\n", yy_text);
9161     fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
9162     setbuf(debugFP, NULL);
9163   }
9164             DisplayError(buf, 0);
9165             return;
9166           case ImpossibleMove:
9167             /* bug? */
9168             snprintf(buf, MSG_SIZ, _("Illegal move in ICS output: \"%s\""), yy_text);
9169   if (appData.debugMode) {
9170     fprintf(debugFP, "Impossible move from ICS: '%s'\n", yy_text);
9171     fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
9172     setbuf(debugFP, NULL);
9173   }
9174             DisplayError(buf, 0);
9175             return;
9176           case EndOfFile:
9177             if (boardIndex < backwardMostMove) {
9178                 /* Oops, gap.  How did that happen? */
9179                 DisplayError(_("Gap in move list"), 0);
9180                 return;
9181             }
9182             backwardMostMove =  blackPlaysFirst ? 1 : 0;
9183             if (boardIndex > forwardMostMove) {
9184                 forwardMostMove = boardIndex;
9185             }
9186             return;
9187           case ElapsedTime:
9188             if (boardIndex > (blackPlaysFirst ? 1 : 0)) {
9189                 strcat(parseList[boardIndex-1], " ");
9190                 strcat(parseList[boardIndex-1], yy_text);
9191             }
9192             continue;
9193           case Comment:
9194           case PGNTag:
9195           case NAG:
9196           default:
9197             /* ignore */
9198             continue;
9199           case WhiteWins:
9200           case BlackWins:
9201           case GameIsDrawn:
9202           case GameUnfinished:
9203             if (gameMode == IcsExamining) {
9204                 if (boardIndex < backwardMostMove) {
9205                     /* Oops, gap.  How did that happen? */
9206                     return;
9207                 }
9208                 backwardMostMove = blackPlaysFirst ? 1 : 0;
9209                 return;
9210             }
9211             gameInfo.result = moveType;
9212             p = strchr(yy_text, '{');
9213             if (p == NULL) p = strchr(yy_text, '(');
9214             if (p == NULL) {
9215                 p = yy_text;
9216                 if (p[0] == '0' || p[0] == '1' || p[0] == '*') p = "";
9217             } else {
9218                 q = strchr(p, *p == '{' ? '}' : ')');
9219                 if (q != NULL) *q = NULLCHAR;
9220                 p++;
9221             }
9222             while(q = strchr(p, '\n')) *q = ' '; // [HGM] crush linefeeds in result message
9223             gameInfo.resultDetails = StrSave(p);
9224             continue;
9225         }
9226         if (boardIndex >= forwardMostMove &&
9227             !(gameMode == IcsObserving && ics_gamenum == -1)) {
9228             backwardMostMove = blackPlaysFirst ? 1 : 0;
9229             return;
9230         }
9231         (void) CoordsToAlgebraic(boards[boardIndex], PosFlags(boardIndex),
9232                                  fromY, fromX, toY, toX, promoChar,
9233                                  parseList[boardIndex]);
9234         CopyBoard(boards[boardIndex + 1], boards[boardIndex]);
9235         /* currentMoveString is set as a side-effect of yylex */
9236         safeStrCpy(moveList[boardIndex], currentMoveString, sizeof(moveList[boardIndex])/sizeof(moveList[boardIndex][0]));
9237         strcat(moveList[boardIndex], "\n");
9238         boardIndex++;
9239         ApplyMove(fromX, fromY, toX, toY, promoChar, boards[boardIndex]);
9240         switch (MateTest(boards[boardIndex], PosFlags(boardIndex)) ) {
9241           case MT_NONE:
9242           case MT_STALEMATE:
9243           default:
9244             break;
9245           case MT_CHECK:
9246             if(gameInfo.variant != VariantShogi)
9247                 strcat(parseList[boardIndex - 1], "+");
9248             break;
9249           case MT_CHECKMATE:
9250           case MT_STAINMATE:
9251             strcat(parseList[boardIndex - 1], "#");
9252             break;
9253         }
9254     }
9255 }
9256
9257
9258 /* Apply a move to the given board  */
9259 void
9260 ApplyMove (int fromX, int fromY, int toX, int toY, int promoChar, Board board)
9261 {
9262   ChessSquare captured = board[toY][toX], piece, king; int p, oldEP = EP_NONE, berolina = 0;
9263   int promoRank = gameInfo.variant == VariantMakruk || gameInfo.variant == VariantGrand ? 3 : 1;
9264
9265     /* [HGM] compute & store e.p. status and castling rights for new position */
9266     /* we can always do that 'in place', now pointers to these rights are passed to ApplyMove */
9267
9268       if(gameInfo.variant == VariantBerolina) berolina = EP_BEROLIN_A;
9269       oldEP = (signed char)board[EP_STATUS];
9270       board[EP_STATUS] = EP_NONE;
9271
9272   if (fromY == DROP_RANK) {
9273         /* must be first */
9274         if(fromX == EmptySquare) { // [HGM] pass: empty drop encodes null move; nothing to change.
9275             board[EP_STATUS] = EP_CAPTURE; // null move considered irreversible
9276             return;
9277         }
9278         piece = board[toY][toX] = (ChessSquare) fromX;
9279   } else {
9280       int i;
9281
9282       if( board[toY][toX] != EmptySquare )
9283            board[EP_STATUS] = EP_CAPTURE;
9284
9285       if( board[fromY][fromX] == WhiteLance || board[fromY][fromX] == BlackLance ) {
9286            if( gameInfo.variant != VariantSuper && gameInfo.variant != VariantShogi )
9287                board[EP_STATUS] = EP_PAWN_MOVE; // Lance is Pawn-like in most variants
9288       } else
9289       if( board[fromY][fromX] == WhitePawn ) {
9290            if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
9291                board[EP_STATUS] = EP_PAWN_MOVE;
9292            if( toY-fromY==2) {
9293                if(toX>BOARD_LEFT   && board[toY][toX-1] == BlackPawn &&
9294                         gameInfo.variant != VariantBerolina || toX < fromX)
9295                       board[EP_STATUS] = toX | berolina;
9296                if(toX<BOARD_RGHT-1 && board[toY][toX+1] == BlackPawn &&
9297                         gameInfo.variant != VariantBerolina || toX > fromX)
9298                       board[EP_STATUS] = toX;
9299            }
9300       } else
9301       if( board[fromY][fromX] == BlackPawn ) {
9302            if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
9303                board[EP_STATUS] = EP_PAWN_MOVE;
9304            if( toY-fromY== -2) {
9305                if(toX>BOARD_LEFT   && board[toY][toX-1] == WhitePawn &&
9306                         gameInfo.variant != VariantBerolina || toX < fromX)
9307                       board[EP_STATUS] = toX | berolina;
9308                if(toX<BOARD_RGHT-1 && board[toY][toX+1] == WhitePawn &&
9309                         gameInfo.variant != VariantBerolina || toX > fromX)
9310                       board[EP_STATUS] = toX;
9311            }
9312        }
9313
9314        for(i=0; i<nrCastlingRights; i++) {
9315            if(board[CASTLING][i] == fromX && castlingRank[i] == fromY ||
9316               board[CASTLING][i] == toX   && castlingRank[i] == toY
9317              ) board[CASTLING][i] = NoRights; // revoke for moved or captured piece
9318        }
9319
9320      if (fromX == toX && fromY == toY) return;
9321
9322      piece = board[fromY][fromX]; /* [HGM] remember, for Shogi promotion */
9323      king = piece < (int) BlackPawn ? WhiteKing : BlackKing; /* [HGM] Knightmate simplify testing for castling */
9324      if(gameInfo.variant == VariantKnightmate)
9325          king += (int) WhiteUnicorn - (int) WhiteKing;
9326
9327     /* Code added by Tord: */
9328     /* FRC castling assumed when king captures friendly rook. [HGM] or RxK for S-Chess */
9329     if (board[fromY][fromX] == WhiteKing && board[toY][toX] == WhiteRook ||
9330         board[fromY][fromX] == WhiteRook && board[toY][toX] == WhiteKing) {
9331       board[fromY][fromX] = EmptySquare;
9332       board[toY][toX] = EmptySquare;
9333       if((toX > fromX) != (piece == WhiteRook)) {
9334         board[0][BOARD_RGHT-2] = WhiteKing; board[0][BOARD_RGHT-3] = WhiteRook;
9335       } else {
9336         board[0][BOARD_LEFT+2] = WhiteKing; board[0][BOARD_LEFT+3] = WhiteRook;
9337       }
9338     } else if (board[fromY][fromX] == BlackKing && board[toY][toX] == BlackRook ||
9339                board[fromY][fromX] == BlackRook && board[toY][toX] == BlackKing) {
9340       board[fromY][fromX] = EmptySquare;
9341       board[toY][toX] = EmptySquare;
9342       if((toX > fromX) != (piece == BlackRook)) {
9343         board[BOARD_HEIGHT-1][BOARD_RGHT-2] = BlackKing; board[BOARD_HEIGHT-1][BOARD_RGHT-3] = BlackRook;
9344       } else {
9345         board[BOARD_HEIGHT-1][BOARD_LEFT+2] = BlackKing; board[BOARD_HEIGHT-1][BOARD_LEFT+3] = BlackRook;
9346       }
9347     /* End of code added by Tord */
9348
9349     } else if (board[fromY][fromX] == king
9350         && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
9351         && toY == fromY && toX > fromX+1) {
9352         board[fromY][fromX] = EmptySquare;
9353         board[toY][toX] = king;
9354         board[toY][toX-1] = board[fromY][BOARD_RGHT-1];
9355         board[fromY][BOARD_RGHT-1] = EmptySquare;
9356     } else if (board[fromY][fromX] == king
9357         && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
9358                && toY == fromY && toX < fromX-1) {
9359         board[fromY][fromX] = EmptySquare;
9360         board[toY][toX] = king;
9361         board[toY][toX+1] = board[fromY][BOARD_LEFT];
9362         board[fromY][BOARD_LEFT] = EmptySquare;
9363     } else if ((board[fromY][fromX] == WhitePawn && gameInfo.variant != VariantXiangqi ||
9364                 board[fromY][fromX] == WhiteLance && gameInfo.variant != VariantSuper && gameInfo.variant != VariantShogi)
9365                && toY >= BOARD_HEIGHT-promoRank && promoChar // defaulting to Q is done elsewhere
9366                ) {
9367         /* white pawn promotion */
9368         board[toY][toX] = CharToPiece(ToUpper(promoChar));
9369         if(gameInfo.variant==VariantBughouse ||
9370            gameInfo.variant==VariantCrazyhouse) /* [HGM] use shadow piece */
9371             board[toY][toX] = (ChessSquare) (PROMOTED board[toY][toX]);
9372         board[fromY][fromX] = EmptySquare;
9373     } else if ((fromY >= BOARD_HEIGHT>>1)
9374                && (toX != fromX)
9375                && gameInfo.variant != VariantXiangqi
9376                && gameInfo.variant != VariantBerolina
9377                && (board[fromY][fromX] == WhitePawn)
9378                && (board[toY][toX] == EmptySquare)) {
9379         board[fromY][fromX] = EmptySquare;
9380         board[toY][toX] = WhitePawn;
9381         captured = board[toY - 1][toX];
9382         board[toY - 1][toX] = EmptySquare;
9383     } else if ((fromY == BOARD_HEIGHT-4)
9384                && (toX == fromX)
9385                && gameInfo.variant == VariantBerolina
9386                && (board[fromY][fromX] == WhitePawn)
9387                && (board[toY][toX] == EmptySquare)) {
9388         board[fromY][fromX] = EmptySquare;
9389         board[toY][toX] = WhitePawn;
9390         if(oldEP & EP_BEROLIN_A) {
9391                 captured = board[fromY][fromX-1];
9392                 board[fromY][fromX-1] = EmptySquare;
9393         }else{  captured = board[fromY][fromX+1];
9394                 board[fromY][fromX+1] = EmptySquare;
9395         }
9396     } else if (board[fromY][fromX] == king
9397         && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
9398                && toY == fromY && toX > fromX+1) {
9399         board[fromY][fromX] = EmptySquare;
9400         board[toY][toX] = king;
9401         board[toY][toX-1] = board[fromY][BOARD_RGHT-1];
9402         board[fromY][BOARD_RGHT-1] = EmptySquare;
9403     } else if (board[fromY][fromX] == king
9404         && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
9405                && toY == fromY && toX < fromX-1) {
9406         board[fromY][fromX] = EmptySquare;
9407         board[toY][toX] = king;
9408         board[toY][toX+1] = board[fromY][BOARD_LEFT];
9409         board[fromY][BOARD_LEFT] = EmptySquare;
9410     } else if (fromY == 7 && fromX == 3
9411                && board[fromY][fromX] == BlackKing
9412                && toY == 7 && toX == 5) {
9413         board[fromY][fromX] = EmptySquare;
9414         board[toY][toX] = BlackKing;
9415         board[fromY][7] = EmptySquare;
9416         board[toY][4] = BlackRook;
9417     } else if (fromY == 7 && fromX == 3
9418                && board[fromY][fromX] == BlackKing
9419                && toY == 7 && toX == 1) {
9420         board[fromY][fromX] = EmptySquare;
9421         board[toY][toX] = BlackKing;
9422         board[fromY][0] = EmptySquare;
9423         board[toY][2] = BlackRook;
9424     } else if ((board[fromY][fromX] == BlackPawn && gameInfo.variant != VariantXiangqi ||
9425                 board[fromY][fromX] == BlackLance && gameInfo.variant != VariantSuper && gameInfo.variant != VariantShogi)
9426                && toY < promoRank && promoChar
9427                ) {
9428         /* black pawn promotion */
9429         board[toY][toX] = CharToPiece(ToLower(promoChar));
9430         if(gameInfo.variant==VariantBughouse ||
9431            gameInfo.variant==VariantCrazyhouse) /* [HGM] use shadow piece */
9432             board[toY][toX] = (ChessSquare) (PROMOTED board[toY][toX]);
9433         board[fromY][fromX] = EmptySquare;
9434     } else if ((fromY < BOARD_HEIGHT>>1)
9435                && (toX != fromX)
9436                && gameInfo.variant != VariantXiangqi
9437                && gameInfo.variant != VariantBerolina
9438                && (board[fromY][fromX] == BlackPawn)
9439                && (board[toY][toX] == EmptySquare)) {
9440         board[fromY][fromX] = EmptySquare;
9441         board[toY][toX] = BlackPawn;
9442         captured = board[toY + 1][toX];
9443         board[toY + 1][toX] = EmptySquare;
9444     } else if ((fromY == 3)
9445                && (toX == fromX)
9446                && gameInfo.variant == VariantBerolina
9447                && (board[fromY][fromX] == BlackPawn)
9448                && (board[toY][toX] == EmptySquare)) {
9449         board[fromY][fromX] = EmptySquare;
9450         board[toY][toX] = BlackPawn;
9451         if(oldEP & EP_BEROLIN_A) {
9452                 captured = board[fromY][fromX-1];
9453                 board[fromY][fromX-1] = EmptySquare;
9454         }else{  captured = board[fromY][fromX+1];
9455                 board[fromY][fromX+1] = EmptySquare;
9456         }
9457     } else {
9458         board[toY][toX] = board[fromY][fromX];
9459         board[fromY][fromX] = EmptySquare;
9460     }
9461   }
9462
9463     if (gameInfo.holdingsWidth != 0) {
9464
9465       /* !!A lot more code needs to be written to support holdings  */
9466       /* [HGM] OK, so I have written it. Holdings are stored in the */
9467       /* penultimate board files, so they are automaticlly stored   */
9468       /* in the game history.                                       */
9469       if (fromY == DROP_RANK || gameInfo.variant == VariantSChess
9470                                 && promoChar && piece != WhitePawn && piece != BlackPawn) {
9471         /* Delete from holdings, by decreasing count */
9472         /* and erasing image if necessary            */
9473         p = fromY == DROP_RANK ? (int) fromX : CharToPiece(piece > BlackPawn ? ToLower(promoChar) : ToUpper(promoChar));
9474         if(p < (int) BlackPawn) { /* white drop */
9475              p -= (int)WhitePawn;
9476                  p = PieceToNumber((ChessSquare)p);
9477              if(p >= gameInfo.holdingsSize) p = 0;
9478              if(--board[p][BOARD_WIDTH-2] <= 0)
9479                   board[p][BOARD_WIDTH-1] = EmptySquare;
9480              if((int)board[p][BOARD_WIDTH-2] < 0)
9481                         board[p][BOARD_WIDTH-2] = 0;
9482         } else {                  /* black drop */
9483              p -= (int)BlackPawn;
9484                  p = PieceToNumber((ChessSquare)p);
9485              if(p >= gameInfo.holdingsSize) p = 0;
9486              if(--board[BOARD_HEIGHT-1-p][1] <= 0)
9487                   board[BOARD_HEIGHT-1-p][0] = EmptySquare;
9488              if((int)board[BOARD_HEIGHT-1-p][1] < 0)
9489                         board[BOARD_HEIGHT-1-p][1] = 0;
9490         }
9491       }
9492       if (captured != EmptySquare && gameInfo.holdingsSize > 0
9493           && gameInfo.variant != VariantBughouse && gameInfo.variant != VariantSChess        ) {
9494         /* [HGM] holdings: Add to holdings, if holdings exist */
9495         if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand) {
9496                 // [HGM] superchess: suppress flipping color of captured pieces by reverse pre-flip
9497                 captured = (int) captured >= (int) BlackPawn ? BLACK_TO_WHITE captured : WHITE_TO_BLACK captured;
9498         }
9499         p = (int) captured;
9500         if (p >= (int) BlackPawn) {
9501           p -= (int)BlackPawn;
9502           if(gameInfo.variant == VariantShogi && DEMOTED p >= 0) {
9503                   /* in Shogi restore piece to its original  first */
9504                   captured = (ChessSquare) (DEMOTED captured);
9505                   p = DEMOTED p;
9506           }
9507           p = PieceToNumber((ChessSquare)p);
9508           if(p >= gameInfo.holdingsSize) { p = 0; captured = BlackPawn; }
9509           board[p][BOARD_WIDTH-2]++;
9510           board[p][BOARD_WIDTH-1] = BLACK_TO_WHITE captured;
9511         } else {
9512           p -= (int)WhitePawn;
9513           if(gameInfo.variant == VariantShogi && DEMOTED p >= 0) {
9514                   captured = (ChessSquare) (DEMOTED captured);
9515                   p = DEMOTED p;
9516           }
9517           p = PieceToNumber((ChessSquare)p);
9518           if(p >= gameInfo.holdingsSize) { p = 0; captured = WhitePawn; }
9519           board[BOARD_HEIGHT-1-p][1]++;
9520           board[BOARD_HEIGHT-1-p][0] = WHITE_TO_BLACK captured;
9521         }
9522       }
9523     } else if (gameInfo.variant == VariantAtomic) {
9524       if (captured != EmptySquare) {
9525         int y, x;
9526         for (y = toY-1; y <= toY+1; y++) {
9527           for (x = toX-1; x <= toX+1; x++) {
9528             if (y >= 0 && y < BOARD_HEIGHT && x >= BOARD_LEFT && x < BOARD_RGHT &&
9529                 board[y][x] != WhitePawn && board[y][x] != BlackPawn) {
9530               board[y][x] = EmptySquare;
9531             }
9532           }
9533         }
9534         board[toY][toX] = EmptySquare;
9535       }
9536     }
9537     if(gameInfo.variant == VariantSChess && promoChar != NULLCHAR && promoChar != '=' && piece != WhitePawn && piece != BlackPawn) {
9538         board[fromY][fromX] = CharToPiece(piece < BlackPawn ? ToUpper(promoChar) : ToLower(promoChar)); // S-Chess gating
9539     } else
9540     if(promoChar == '+') {
9541         /* [HGM] Shogi-style promotions, to piece implied by original (Might overwrite ordinary Pawn promotion) */
9542         board[toY][toX] = (ChessSquare) (PROMOTED piece);
9543     } else if(!appData.testLegality && promoChar != NULLCHAR && promoChar != '=') { // without legality testing, unconditionally believe promoChar
9544         ChessSquare newPiece = CharToPiece(piece < BlackPawn ? ToUpper(promoChar) : ToLower(promoChar));
9545         if((newPiece <= WhiteMan || newPiece >= BlackPawn && newPiece <= BlackMan) // unpromoted piece specified
9546            && pieceToChar[PROMOTED newPiece] == '~') newPiece = PROMOTED newPiece; // but promoted version available
9547         board[toY][toX] = newPiece;
9548     }
9549     if((gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand)
9550                 && promoChar != NULLCHAR && gameInfo.holdingsSize) {
9551         // [HGM] superchess: take promotion piece out of holdings
9552         int k = PieceToNumber(CharToPiece(ToUpper(promoChar)));
9553         if((int)piece < (int)BlackPawn) { // determine stm from piece color
9554             if(!--board[k][BOARD_WIDTH-2])
9555                 board[k][BOARD_WIDTH-1] = EmptySquare;
9556         } else {
9557             if(!--board[BOARD_HEIGHT-1-k][1])
9558                 board[BOARD_HEIGHT-1-k][0] = EmptySquare;
9559         }
9560     }
9561
9562 }
9563
9564 /* Updates forwardMostMove */
9565 void
9566 MakeMove (int fromX, int fromY, int toX, int toY, int promoChar)
9567 {
9568 //    forwardMostMove++; // [HGM] bare: moved downstream
9569
9570     (void) CoordsToAlgebraic(boards[forwardMostMove],
9571                              PosFlags(forwardMostMove),
9572                              fromY, fromX, toY, toX, promoChar,
9573                              parseList[forwardMostMove]);
9574
9575     if(serverMoves != NULL) { /* [HGM] write moves on file for broadcasting (should be separate routine, really) */
9576         int timeLeft; static int lastLoadFlag=0; int king, piece;
9577         piece = boards[forwardMostMove][fromY][fromX];
9578         king = piece < (int) BlackPawn ? WhiteKing : BlackKing;
9579         if(gameInfo.variant == VariantKnightmate)
9580             king += (int) WhiteUnicorn - (int) WhiteKing;
9581         if(forwardMostMove == 0) {
9582             if(gameMode == MachinePlaysBlack || gameMode == BeginningOfGame)
9583                 fprintf(serverMoves, "%s;", UserName());
9584             else if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b')
9585                 fprintf(serverMoves, "%s;", second.tidy);
9586             fprintf(serverMoves, "%s;", first.tidy);
9587             if(gameMode == MachinePlaysWhite)
9588                 fprintf(serverMoves, "%s;", UserName());
9589             else if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w')
9590                 fprintf(serverMoves, "%s;", second.tidy);
9591         } else fprintf(serverMoves, loadFlag|lastLoadFlag ? ":" : ";");
9592         lastLoadFlag = loadFlag;
9593         // print base move
9594         fprintf(serverMoves, "%c%c:%c%c", AAA+fromX, ONE+fromY, AAA+toX, ONE+toY);
9595         // print castling suffix
9596         if( toY == fromY && piece == king ) {
9597             if(toX-fromX > 1)
9598                 fprintf(serverMoves, ":%c%c:%c%c", AAA+BOARD_RGHT-1, ONE+fromY, AAA+toX-1,ONE+toY);
9599             if(fromX-toX >1)
9600                 fprintf(serverMoves, ":%c%c:%c%c", AAA+BOARD_LEFT, ONE+fromY, AAA+toX+1,ONE+toY);
9601         }
9602         // e.p. suffix
9603         if( (boards[forwardMostMove][fromY][fromX] == WhitePawn ||
9604              boards[forwardMostMove][fromY][fromX] == BlackPawn   ) &&
9605              boards[forwardMostMove][toY][toX] == EmptySquare
9606              && fromX != toX && fromY != toY)
9607                 fprintf(serverMoves, ":%c%c:%c%c", AAA+fromX, ONE+fromY, AAA+toX, ONE+fromY);
9608         // promotion suffix
9609         if(promoChar != NULLCHAR)
9610                 fprintf(serverMoves, ":%c:%c%c", ToLower(promoChar), AAA+toX, ONE+toY);
9611         if(!loadFlag) {
9612                 char buf[MOVE_LEN*2], *p; int len;
9613             fprintf(serverMoves, "/%d/%d",
9614                pvInfoList[forwardMostMove].depth, pvInfoList[forwardMostMove].score);
9615             if(forwardMostMove+1 & 1) timeLeft = whiteTimeRemaining/1000;
9616             else                      timeLeft = blackTimeRemaining/1000;
9617             fprintf(serverMoves, "/%d", timeLeft);
9618                 strncpy(buf, parseList[forwardMostMove], MOVE_LEN*2);
9619                 if(p = strchr(buf, '=')) *p = NULLCHAR;
9620                 len = strlen(buf); if(len > 1 && buf[len-2] != '-') buf[len-2] = NULLCHAR; // strip to-square
9621             fprintf(serverMoves, "/%s", buf);
9622         }
9623         fflush(serverMoves);
9624     }
9625
9626     if (forwardMostMove+1 > framePtr) { // [HGM] vari: do not run into saved variations..
9627         GameEnds(GameUnfinished, _("Game too long; increase MAX_MOVES and recompile"), GE_XBOARD);
9628       return;
9629     }
9630     UnLoadPV(); // [HGM] pv: if we are looking at a PV, abort this
9631     if (commentList[forwardMostMove+1] != NULL) {
9632         free(commentList[forwardMostMove+1]);
9633         commentList[forwardMostMove+1] = NULL;
9634     }
9635     CopyBoard(boards[forwardMostMove+1], boards[forwardMostMove]);
9636     ApplyMove(fromX, fromY, toX, toY, promoChar, boards[forwardMostMove+1]);
9637     // forwardMostMove++; // [HGM] bare: moved to after ApplyMove, to make sure clock interrupt finds complete board
9638     SwitchClocks(forwardMostMove+1); // [HGM] race: incrementing move nr inside
9639     timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
9640     timeRemaining[1][forwardMostMove] = blackTimeRemaining;
9641     adjustedClock = FALSE;
9642     gameInfo.result = GameUnfinished;
9643     if (gameInfo.resultDetails != NULL) {
9644         free(gameInfo.resultDetails);
9645         gameInfo.resultDetails = NULL;
9646     }
9647     CoordsToComputerAlgebraic(fromY, fromX, toY, toX, promoChar,
9648                               moveList[forwardMostMove - 1]);
9649     switch (MateTest(boards[forwardMostMove], PosFlags(forwardMostMove)) ) {
9650       case MT_NONE:
9651       case MT_STALEMATE:
9652       default:
9653         break;
9654       case MT_CHECK:
9655         if(gameInfo.variant != VariantShogi)
9656             strcat(parseList[forwardMostMove - 1], "+");
9657         break;
9658       case MT_CHECKMATE:
9659       case MT_STAINMATE:
9660         strcat(parseList[forwardMostMove - 1], "#");
9661         break;
9662     }
9663
9664 }
9665
9666 /* Updates currentMove if not pausing */
9667 void
9668 ShowMove (int fromX, int fromY, int toX, int toY)
9669 {
9670     int instant = (gameMode == PlayFromGameFile) ?
9671         (matchMode || (appData.timeDelay == 0 && !pausing)) : pausing;
9672     if(appData.noGUI) return;
9673     if (!pausing || gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
9674         if (!instant) {
9675             if (forwardMostMove == currentMove + 1) {
9676                 AnimateMove(boards[forwardMostMove - 1],
9677                             fromX, fromY, toX, toY);
9678             }
9679             if (appData.highlightLastMove) {
9680                 SetHighlights(fromX, fromY, toX, toY);
9681             }
9682         }
9683         currentMove = forwardMostMove;
9684     }
9685
9686     if (instant) return;
9687
9688     DisplayMove(currentMove - 1);
9689     DrawPosition(FALSE, boards[currentMove]);
9690     DisplayBothClocks();
9691     HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
9692 }
9693
9694 void
9695 SendEgtPath (ChessProgramState *cps)
9696 {       /* [HGM] EGT: match formats given in feature with those given by user, and send info for each match */
9697         char buf[MSG_SIZ], name[MSG_SIZ], *p;
9698
9699         if((p = cps->egtFormats) == NULL || appData.egtFormats == NULL) return;
9700
9701         while(*p) {
9702             char c, *q = name+1, *r, *s;
9703
9704             name[0] = ','; // extract next format name from feature and copy with prefixed ','
9705             while(*p && *p != ',') *q++ = *p++;
9706             *q++ = ':'; *q = 0;
9707             if( appData.defaultPathEGTB && appData.defaultPathEGTB[0] &&
9708                 strcmp(name, ",nalimov:") == 0 ) {
9709                 // take nalimov path from the menu-changeable option first, if it is defined
9710               snprintf(buf, MSG_SIZ, "egtpath nalimov %s\n", appData.defaultPathEGTB);
9711                 SendToProgram(buf,cps);     // send egtbpath command for nalimov
9712             } else
9713             if( (s = StrStr(appData.egtFormats, name+1)) == appData.egtFormats ||
9714                 (s = StrStr(appData.egtFormats, name)) != NULL) {
9715                 // format name occurs amongst user-supplied formats, at beginning or immediately after comma
9716                 s = r = StrStr(s, ":") + 1; // beginning of path info
9717                 while(*r && *r != ',') r++; // path info is everything upto next ';' or end of string
9718                 c = *r; *r = 0;             // temporarily null-terminate path info
9719                     *--q = 0;               // strip of trailig ':' from name
9720                     snprintf(buf, MSG_SIZ, "egtpath %s %s\n", name+1, s);
9721                 *r = c;
9722                 SendToProgram(buf,cps);     // send egtbpath command for this format
9723             }
9724             if(*p == ',') p++; // read away comma to position for next format name
9725         }
9726 }
9727
9728 void
9729 InitChessProgram (ChessProgramState *cps, int setup)
9730 /* setup needed to setup FRC opening position */
9731 {
9732     char buf[MSG_SIZ], b[MSG_SIZ]; int overruled;
9733     if (appData.noChessProgram) return;
9734     hintRequested = FALSE;
9735     bookRequested = FALSE;
9736
9737     ParseFeatures(appData.features[cps == &second], cps); // [HGM] allow user to overrule features
9738     /* [HGM] some new WB protocol commands to configure engine are sent now, if engine supports them */
9739     /*       moved to before sending initstring in 4.3.15, so Polyglot can delay UCI 'isready' to recepton of 'new' */
9740     if(cps->memSize) { /* [HGM] memory */
9741       snprintf(buf, MSG_SIZ, "memory %d\n", appData.defaultHashSize + appData.defaultCacheSizeEGTB);
9742         SendToProgram(buf, cps);
9743     }
9744     SendEgtPath(cps); /* [HGM] EGT */
9745     if(cps->maxCores) { /* [HGM] SMP: (protocol specified must be last settings command before new!) */
9746       snprintf(buf, MSG_SIZ, "cores %d\n", appData.smpCores);
9747         SendToProgram(buf, cps);
9748     }
9749
9750     SendToProgram(cps->initString, cps);
9751     if (gameInfo.variant != VariantNormal &&
9752         gameInfo.variant != VariantLoadable
9753         /* [HGM] also send variant if board size non-standard */
9754         || gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0
9755                                             ) {
9756       char *v = VariantName(gameInfo.variant);
9757       if (cps->protocolVersion != 1 && StrStr(cps->variants, v) == NULL) {
9758         /* [HGM] in protocol 1 we have to assume all variants valid */
9759         snprintf(buf, MSG_SIZ, _("Variant %s not supported by %s"), v, cps->tidy);
9760         DisplayFatalError(buf, 0, 1);
9761         return;
9762       }
9763
9764       /* [HGM] make prefix for non-standard board size. Awkward testing... */
9765       overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
9766       if( gameInfo.variant == VariantXiangqi )
9767            overruled = gameInfo.boardWidth != 9 || gameInfo.boardHeight != 10 || gameInfo.holdingsSize != 0;
9768       if( gameInfo.variant == VariantShogi )
9769            overruled = gameInfo.boardWidth != 9 || gameInfo.boardHeight != 9 || gameInfo.holdingsSize != 7;
9770       if( gameInfo.variant == VariantBughouse || gameInfo.variant == VariantCrazyhouse )
9771            overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 5;
9772       if( gameInfo.variant == VariantCapablanca || gameInfo.variant == VariantCapaRandom ||
9773           gameInfo.variant == VariantGothic || gameInfo.variant == VariantFalcon || gameInfo.variant == VariantJanus )
9774            overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
9775       if( gameInfo.variant == VariantCourier )
9776            overruled = gameInfo.boardWidth != 12 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
9777       if( gameInfo.variant == VariantSuper )
9778            overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 8;
9779       if( gameInfo.variant == VariantGreat )
9780            overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 8;
9781       if( gameInfo.variant == VariantSChess )
9782            overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 7;
9783       if( gameInfo.variant == VariantGrand )
9784            overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 10 || gameInfo.holdingsSize != 7;
9785
9786       if(overruled) {
9787         snprintf(b, MSG_SIZ, "%dx%d+%d_%s", gameInfo.boardWidth, gameInfo.boardHeight,
9788                  gameInfo.holdingsSize, VariantName(gameInfo.variant)); // cook up sized variant name
9789            /* [HGM] varsize: try first if this defiant size variant is specifically known */
9790            if(StrStr(cps->variants, b) == NULL) {
9791                // specific sized variant not known, check if general sizing allowed
9792                if (cps->protocolVersion != 1) { // for protocol 1 we cannot check and hope for the best
9793                    if(StrStr(cps->variants, "boardsize") == NULL) {
9794                      snprintf(buf, MSG_SIZ, "Board size %dx%d+%d not supported by %s",
9795                             gameInfo.boardWidth, gameInfo.boardHeight, gameInfo.holdingsSize, cps->tidy);
9796                        DisplayFatalError(buf, 0, 1);
9797                        return;
9798                    }
9799                    /* [HGM] here we really should compare with the maximum supported board size */
9800                }
9801            }
9802       } else snprintf(b, MSG_SIZ,"%s", VariantName(gameInfo.variant));
9803       snprintf(buf, MSG_SIZ, "variant %s\n", b);
9804       SendToProgram(buf, cps);
9805     }
9806     currentlyInitializedVariant = gameInfo.variant;
9807
9808     /* [HGM] send opening position in FRC to first engine */
9809     if(setup) {
9810           SendToProgram("force\n", cps);
9811           SendBoard(cps, 0);
9812           /* engine is now in force mode! Set flag to wake it up after first move. */
9813           setboardSpoiledMachineBlack = 1;
9814     }
9815
9816     if (cps->sendICS) {
9817       snprintf(buf, sizeof(buf), "ics %s\n", appData.icsActive ? appData.icsHost : "-");
9818       SendToProgram(buf, cps);
9819     }
9820     cps->maybeThinking = FALSE;
9821     cps->offeredDraw = 0;
9822     if (!appData.icsActive) {
9823         SendTimeControl(cps, movesPerSession, timeControl,
9824                         timeIncrement, appData.searchDepth,
9825                         searchTime);
9826     }
9827     if (appData.showThinking
9828         // [HGM] thinking: four options require thinking output to be sent
9829         || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp()
9830                                 ) {
9831         SendToProgram("post\n", cps);
9832     }
9833     SendToProgram("hard\n", cps);
9834     if (!appData.ponderNextMove) {
9835         /* Warning: "easy" is a toggle in GNU Chess, so don't send
9836            it without being sure what state we are in first.  "hard"
9837            is not a toggle, so that one is OK.
9838          */
9839         SendToProgram("easy\n", cps);
9840     }
9841     if (cps->usePing) {
9842       snprintf(buf, MSG_SIZ, "ping %d\n", ++cps->lastPing);
9843       SendToProgram(buf, cps);
9844     }
9845     cps->initDone = TRUE;
9846     ClearEngineOutputPane(cps == &second);
9847 }
9848
9849
9850 void
9851 StartChessProgram (ChessProgramState *cps)
9852 {
9853     char buf[MSG_SIZ];
9854     int err;
9855
9856     if (appData.noChessProgram) return;
9857     cps->initDone = FALSE;
9858
9859     if (strcmp(cps->host, "localhost") == 0) {
9860         err = StartChildProcess(cps->program, cps->dir, &cps->pr);
9861     } else if (*appData.remoteShell == NULLCHAR) {
9862         err = OpenRcmd(cps->host, appData.remoteUser, cps->program, &cps->pr);
9863     } else {
9864         if (*appData.remoteUser == NULLCHAR) {
9865           snprintf(buf, sizeof(buf), "%s %s %s", appData.remoteShell, cps->host,
9866                     cps->program);
9867         } else {
9868           snprintf(buf, sizeof(buf), "%s %s -l %s %s", appData.remoteShell,
9869                     cps->host, appData.remoteUser, cps->program);
9870         }
9871         err = StartChildProcess(buf, "", &cps->pr);
9872     }
9873
9874     if (err != 0) {
9875       snprintf(buf, MSG_SIZ, _("Startup failure on '%s'"), cps->program);
9876         DisplayError(buf, err); // [HGM] bit of a rough kludge: ignore failure, (which XBoard would do anyway), and let I/O discover it
9877         if(cps != &first) return;
9878         appData.noChessProgram = TRUE;
9879         ThawUI();
9880         SetNCPMode();
9881 //      DisplayFatalError(buf, err, 1);
9882 //      cps->pr = NoProc;
9883 //      cps->isr = NULL;
9884         return;
9885     }
9886
9887     cps->isr = AddInputSource(cps->pr, TRUE, ReceiveFromProgram, cps);
9888     if (cps->protocolVersion > 1) {
9889       snprintf(buf, MSG_SIZ, "xboard\nprotover %d\n", cps->protocolVersion);
9890       cps->nrOptions = 0; // [HGM] options: clear all engine-specific options
9891       cps->comboCnt = 0;  //                and values of combo boxes
9892       SendToProgram(buf, cps);
9893     } else {
9894       SendToProgram("xboard\n", cps);
9895     }
9896 }
9897
9898 void
9899 TwoMachinesEventIfReady P((void))
9900 {
9901   static int curMess = 0;
9902   if (first.lastPing != first.lastPong) {
9903     if(curMess != 1) DisplayMessage("", _("Waiting for first chess program")); curMess = 1;
9904     ScheduleDelayedEvent(TwoMachinesEventIfReady, 10); // [HGM] fast: lowered from 1000
9905     return;
9906   }
9907   if (second.lastPing != second.lastPong) {
9908     if(curMess != 2) DisplayMessage("", _("Waiting for second chess program")); curMess = 2;
9909     ScheduleDelayedEvent(TwoMachinesEventIfReady, 10); // [HGM] fast: lowered from 1000
9910     return;
9911   }
9912   DisplayMessage("", ""); curMess = 0;
9913   ThawUI();
9914   TwoMachinesEvent();
9915 }
9916
9917 char *
9918 MakeName (char *template)
9919 {
9920     time_t clock;
9921     struct tm *tm;
9922     static char buf[MSG_SIZ];
9923     char *p = buf;
9924     int i;
9925
9926     clock = time((time_t *)NULL);
9927     tm = localtime(&clock);
9928
9929     while(*p++ = *template++) if(p[-1] == '%') {
9930         switch(*template++) {
9931           case 0:   *p = 0; return buf;
9932           case 'Y': i = tm->tm_year+1900; break;
9933           case 'y': i = tm->tm_year-100; break;
9934           case 'M': i = tm->tm_mon+1; break;
9935           case 'd': i = tm->tm_mday; break;
9936           case 'h': i = tm->tm_hour; break;
9937           case 'm': i = tm->tm_min; break;
9938           case 's': i = tm->tm_sec; break;
9939           default:  i = 0;
9940         }
9941         snprintf(p-1, MSG_SIZ-10 - (p - buf), "%02d", i); p += strlen(p);
9942     }
9943     return buf;
9944 }
9945
9946 int
9947 CountPlayers (char *p)
9948 {
9949     int n = 0;
9950     while(p = strchr(p, '\n')) p++, n++; // count participants
9951     return n;
9952 }
9953
9954 FILE *
9955 WriteTourneyFile (char *results, FILE *f)
9956 {   // write tournament parameters on tourneyFile; on success return the stream pointer for closing
9957     if(f == NULL) f = fopen(appData.tourneyFile, "w");
9958     if(f == NULL) DisplayError(_("Could not write on tourney file"), 0); else {
9959         // create a file with tournament description
9960         fprintf(f, "-participants {%s}\n", appData.participants);
9961         fprintf(f, "-seedBase %d\n", appData.seedBase);
9962         fprintf(f, "-tourneyType %d\n", appData.tourneyType);
9963         fprintf(f, "-tourneyCycles %d\n", appData.tourneyCycles);
9964         fprintf(f, "-defaultMatchGames %d\n", appData.defaultMatchGames);
9965         fprintf(f, "-syncAfterRound %s\n", appData.roundSync ? "true" : "false");
9966         fprintf(f, "-syncAfterCycle %s\n", appData.cycleSync ? "true" : "false");
9967         fprintf(f, "-saveGameFile \"%s\"\n", appData.saveGameFile);
9968         fprintf(f, "-loadGameFile \"%s\"\n", appData.loadGameFile);
9969         fprintf(f, "-loadGameIndex %d\n", appData.loadGameIndex);
9970         fprintf(f, "-loadPositionFile \"%s\"\n", appData.loadPositionFile);
9971         fprintf(f, "-loadPositionIndex %d\n", appData.loadPositionIndex);
9972         fprintf(f, "-rewindIndex %d\n", appData.rewindIndex);
9973         fprintf(f, "-discourageOwnBooks %s\n", appData.defNoBook ? "true" : "false");
9974         if(searchTime > 0)
9975                 fprintf(f, "-searchTime \"%d:%02d\"\n", searchTime/60, searchTime%60);
9976         else {
9977                 fprintf(f, "-mps %d\n", appData.movesPerSession);
9978                 fprintf(f, "-tc %s\n", appData.timeControl);
9979                 fprintf(f, "-inc %.2f\n", appData.timeIncrement);
9980         }
9981         fprintf(f, "-results \"%s\"\n", results);
9982     }
9983     return f;
9984 }
9985
9986 char *command[MAXENGINES], *mnemonic[MAXENGINES];
9987
9988 void
9989 Substitute (char *participants, int expunge)
9990 {
9991     int i, changed, changes=0, nPlayers=0;
9992     char *p, *q, *r, buf[MSG_SIZ];
9993     if(participants == NULL) return;
9994     if(appData.tourneyFile[0] == NULLCHAR) { free(participants); return; }
9995     r = p = participants; q = appData.participants;
9996     while(*p && *p == *q) {
9997         if(*p == '\n') r = p+1, nPlayers++;
9998         p++; q++;
9999     }
10000     if(*p) { // difference
10001         while(*p && *p++ != '\n');
10002         while(*q && *q++ != '\n');
10003       changed = nPlayers;
10004         changes = 1 + (strcmp(p, q) != 0);
10005     }
10006     if(changes == 1) { // a single engine mnemonic was changed
10007         q = r; while(*q) nPlayers += (*q++ == '\n');
10008         p = buf; while(*r && (*p = *r++) != '\n') p++;
10009         *p = NULLCHAR;
10010         NamesToList(firstChessProgramNames, command, mnemonic, "all");
10011         for(i=1; mnemonic[i]; i++) if(!strcmp(buf, mnemonic[i])) break;
10012         if(mnemonic[i]) { // The substitute is valid
10013             FILE *f;
10014             if(appData.tourneyFile[0] && (f = fopen(appData.tourneyFile, "r+")) ) {
10015                 flock(fileno(f), LOCK_EX);
10016                 ParseArgsFromFile(f);
10017                 fseek(f, 0, SEEK_SET);
10018                 FREE(appData.participants); appData.participants = participants;
10019                 if(expunge) { // erase results of replaced engine
10020                     int len = strlen(appData.results), w, b, dummy;
10021                     for(i=0; i<len; i++) {
10022                         Pairing(i, nPlayers, &w, &b, &dummy);
10023                         if((w == changed || b == changed) && appData.results[i] == '*') {
10024                             DisplayError(_("You cannot replace an engine while it is engaged!\nTerminate its game first."), 0);
10025                             fclose(f);
10026                             return;
10027                         }
10028                     }
10029                     for(i=0; i<len; i++) {
10030                         Pairing(i, nPlayers, &w, &b, &dummy);
10031                         if(w == changed || b == changed) appData.results[i] = ' '; // mark as not played
10032                     }
10033                 }
10034                 WriteTourneyFile(appData.results, f);
10035                 fclose(f); // release lock
10036                 return;
10037             }
10038         } else DisplayError(_("No engine with the name you gave is installed"), 0);
10039     }
10040     if(changes == 0) DisplayError(_("First change an engine by editing the participants list\nof the Tournament Options dialog"), 0);
10041     if(changes > 1)  DisplayError(_("You can only change one engine at the time"), 0);
10042     free(participants);
10043     return;
10044 }
10045
10046 int
10047 CreateTourney (char *name)
10048 {
10049         FILE *f;
10050         if(matchMode && strcmp(name, appData.tourneyFile)) {
10051              ASSIGN(name, appData.tourneyFile); //do not allow change of tourneyfile while playing
10052         }
10053         if(name[0] == NULLCHAR) {
10054             if(appData.participants[0])
10055                 DisplayError(_("You must supply a tournament file,\nfor storing the tourney progress"), 0);
10056             return 0;
10057         }
10058         f = fopen(name, "r");
10059         if(f) { // file exists
10060             ASSIGN(appData.tourneyFile, name);
10061             ParseArgsFromFile(f); // parse it
10062         } else {
10063             if(!appData.participants[0]) return 0; // ignore tourney file if non-existing & no participants
10064             if(CountPlayers(appData.participants) < (appData.tourneyType>0 ? appData.tourneyType+1 : 2)) {
10065                 DisplayError(_("Not enough participants"), 0);
10066                 return 0;
10067             }
10068             ASSIGN(appData.tourneyFile, name);
10069             if(appData.tourneyType < 0) appData.defaultMatchGames = 1; // Swiss forces games/pairing = 1
10070             if((f = WriteTourneyFile("", NULL)) == NULL) return 0;
10071         }
10072         fclose(f);
10073         appData.noChessProgram = FALSE;
10074         appData.clockMode = TRUE;
10075         SetGNUMode();
10076         return 1;
10077 }
10078
10079 int
10080 NamesToList (char *names, char **engineList, char **engineMnemonic, char *group)
10081 {
10082     char buf[MSG_SIZ], *p, *q;
10083     int i=1, header, skip, all = !strcmp(group, "all"), depth = 0;
10084     insert = names; // afterwards, this global will point just after last retrieved engine line or group end in the 'names'
10085     skip = !all && group[0]; // if group requested, we start in skip mode
10086     for(;*names && depth >= 0 && i < MAXENGINES-1; names = p) {
10087         p = names; q = buf; header = 0;
10088         while(*p && *p != '\n') *q++ = *p++;
10089         *q = 0;
10090         if(*p == '\n') p++;
10091         if(buf[0] == '#') {
10092             if(strstr(buf, "# end") == buf) { if(!--depth) insert = p; continue; } // leave group, and suppress printing label
10093             depth++; // we must be entering a new group
10094             if(all) continue; // suppress printing group headers when complete list requested
10095             header = 1;
10096             if(skip && !strcmp(group, buf)) { depth = 0; skip = FALSE; } // start when we reach requested group
10097         }
10098         if(depth != header && !all || skip) continue; // skip contents of group (but print first-level header)
10099         if(engineList[i]) free(engineList[i]);
10100         engineList[i] = strdup(buf);
10101         if(buf[0] != '#') insert = p, TidyProgramName(engineList[i], "localhost", buf); // group headers not tidied
10102         if(engineMnemonic[i]) free(engineMnemonic[i]);
10103         if((q = strstr(engineList[i]+2, "variant")) && q[-2]== ' ' && (q[-1]=='/' || q[-1]=='-') && (q[7]==' ' || q[7]=='=')) {
10104             strcat(buf, " (");
10105             sscanf(q + 8, "%s", buf + strlen(buf));
10106             strcat(buf, ")");
10107         }
10108         engineMnemonic[i] = strdup(buf);
10109         i++;
10110     }
10111     engineList[i] = engineMnemonic[i] = NULL;
10112     return i;
10113 }
10114
10115 // following implemented as macro to avoid type limitations
10116 #define SWAP(item, temp) temp = appData.item[0]; appData.item[0] = appData.item[n]; appData.item[n] = temp;
10117
10118 void
10119 SwapEngines (int n)
10120 {   // swap settings for first engine and other engine (so far only some selected options)
10121     int h;
10122     char *p;
10123     if(n == 0) return;
10124     SWAP(directory, p)
10125     SWAP(chessProgram, p)
10126     SWAP(isUCI, h)
10127     SWAP(hasOwnBookUCI, h)
10128     SWAP(protocolVersion, h)
10129     SWAP(reuse, h)
10130     SWAP(scoreIsAbsolute, h)
10131     SWAP(timeOdds, h)
10132     SWAP(logo, p)
10133     SWAP(pgnName, p)
10134     SWAP(pvSAN, h)
10135     SWAP(engOptions, p)
10136     SWAP(engInitString, p)
10137     SWAP(computerString, p)
10138     SWAP(features, p)
10139     SWAP(fenOverride, p)
10140     SWAP(NPS, h)
10141     SWAP(accumulateTC, h)
10142     SWAP(host, p)
10143 }
10144
10145 int
10146 SetPlayer (int player, char *p)
10147 {   // [HGM] find the engine line of the partcipant given by number, and parse its options.
10148     int i;
10149     char buf[MSG_SIZ], *engineName;
10150     for(i=0; i<player; i++) p = strchr(p, '\n') + 1;
10151     engineName = strdup(p); if(p = strchr(engineName, '\n')) *p = NULLCHAR;
10152     for(i=1; command[i]; i++) if(!strcmp(mnemonic[i], engineName)) break;
10153     if(mnemonic[i]) {
10154         snprintf(buf, MSG_SIZ, "-fcp %s", command[i]);
10155         ParseArgsFromString(resetOptions); appData.fenOverride[0] = NULL; appData.pvSAN[0] = FALSE;
10156         appData.firstHasOwnBookUCI = !appData.defNoBook; appData.protocolVersion[0] = PROTOVER;
10157         ParseArgsFromString(buf);
10158     }
10159     free(engineName);
10160     return i;
10161 }
10162
10163 char *recentEngines;
10164
10165 void
10166 RecentEngineEvent (int nr)
10167 {
10168     int n;
10169 //    SwapEngines(1); // bump first to second
10170 //    ReplaceEngine(&second, 1); // and load it there
10171     NamesToList(firstChessProgramNames, command, mnemonic, "all"); // get mnemonics of installed engines
10172     n = SetPlayer(nr, recentEngines); // select new (using original menu order!)
10173     if(mnemonic[n]) { // if somehow the engine with the selected nickname is no longer found in the list, we skip
10174         ReplaceEngine(&first, 0);
10175         FloatToFront(&appData.recentEngineList, command[n]);
10176     }
10177 }
10178
10179 int
10180 Pairing (int nr, int nPlayers, int *whitePlayer, int *blackPlayer, int *syncInterval)
10181 {   // determine players from game number
10182     int curCycle, curRound, curPairing, gamesPerCycle, gamesPerRound, roundsPerCycle=1, pairingsPerRound=1;
10183
10184     if(appData.tourneyType == 0) {
10185         roundsPerCycle = (nPlayers - 1) | 1;
10186         pairingsPerRound = nPlayers / 2;
10187     } else if(appData.tourneyType > 0) {
10188         roundsPerCycle = nPlayers - appData.tourneyType;
10189         pairingsPerRound = appData.tourneyType;
10190     }
10191     gamesPerRound = pairingsPerRound * appData.defaultMatchGames;
10192     gamesPerCycle = gamesPerRound * roundsPerCycle;
10193     appData.matchGames = gamesPerCycle * appData.tourneyCycles - 1; // fake like all games are one big match
10194     curCycle = nr / gamesPerCycle; nr %= gamesPerCycle;
10195     curRound = nr / gamesPerRound; nr %= gamesPerRound;
10196     curPairing = nr / appData.defaultMatchGames; nr %= appData.defaultMatchGames;
10197     matchGame = nr + curCycle * appData.defaultMatchGames + 1; // fake game nr that loads correct game or position from file
10198     roundNr = (curCycle * roundsPerCycle + curRound) * appData.defaultMatchGames + nr + 1;
10199
10200     if(appData.cycleSync) *syncInterval = gamesPerCycle;
10201     if(appData.roundSync) *syncInterval = gamesPerRound;
10202
10203     if(appData.debugMode) fprintf(debugFP, "cycle=%d, round=%d, pairing=%d curGame=%d\n", curCycle, curRound, curPairing, matchGame);
10204
10205     if(appData.tourneyType == 0) {
10206         if(curPairing == (nPlayers-1)/2 ) {
10207             *whitePlayer = curRound;
10208             *blackPlayer = nPlayers - 1; // this is the 'bye' when nPlayer is odd
10209         } else {
10210             *whitePlayer = curRound - (nPlayers-1)/2 + curPairing;
10211             if(*whitePlayer < 0) *whitePlayer += nPlayers-1+(nPlayers&1);
10212             *blackPlayer = curRound + (nPlayers-1)/2 - curPairing;
10213             if(*blackPlayer >= nPlayers-1+(nPlayers&1)) *blackPlayer -= nPlayers-1+(nPlayers&1);
10214         }
10215     } else if(appData.tourneyType > 1) {
10216         *blackPlayer = curPairing; // in multi-gauntlet, assign gauntlet engines to second, so first an be kept loaded during round
10217         *whitePlayer = curRound + appData.tourneyType;
10218     } else if(appData.tourneyType > 0) {
10219         *whitePlayer = curPairing;
10220         *blackPlayer = curRound + appData.tourneyType;
10221     }
10222
10223     // take care of white/black alternation per round. 
10224     // For cycles and games this is already taken care of by default, derived from matchGame!
10225     return curRound & 1;
10226 }
10227
10228 int
10229 NextTourneyGame (int nr, int *swapColors)
10230 {   // !!!major kludge!!! fiddle appData settings to get everything in order for next tourney game
10231     char *p, *q;
10232     int whitePlayer, blackPlayer, firstBusy=1000000000, syncInterval = 0, nPlayers;
10233     FILE *tf;
10234     if(appData.tourneyFile[0] == NULLCHAR) return 1; // no tourney, always allow next game
10235     tf = fopen(appData.tourneyFile, "r");
10236     if(tf == NULL) { DisplayFatalError(_("Bad tournament file"), 0, 1); return 0; }
10237     ParseArgsFromFile(tf); fclose(tf);
10238     InitTimeControls(); // TC might be altered from tourney file
10239
10240     nPlayers = CountPlayers(appData.participants); // count participants
10241     if(appData.tourneyType < 0) syncInterval = nPlayers/2; else
10242     *swapColors = Pairing(nr<0 ? 0 : nr, nPlayers, &whitePlayer, &blackPlayer, &syncInterval);
10243
10244     if(syncInterval) {
10245         p = q = appData.results;
10246         while(*q) if(*q++ == '*' || q[-1] == ' ') { firstBusy = q - p - 1; break; }
10247         if(firstBusy/syncInterval < (nextGame/syncInterval)) {
10248             DisplayMessage(_("Waiting for other game(s)"),"");
10249             waitingForGame = TRUE;
10250             ScheduleDelayedEvent(NextMatchGame, 1000); // wait for all games of previous round to finish
10251             return 0;
10252         }
10253         waitingForGame = FALSE;
10254     }
10255
10256     if(appData.tourneyType < 0) {
10257         if(nr>=0 && !pairingReceived) {
10258             char buf[1<<16];
10259             if(pairing.pr == NoProc) {
10260                 if(!appData.pairingEngine[0]) {
10261                     DisplayFatalError(_("No pairing engine specified"), 0, 1);
10262                     return 0;
10263                 }
10264                 StartChessProgram(&pairing); // starts the pairing engine
10265             }
10266             snprintf(buf, 1<<16, "results %d %s\n", nPlayers, appData.results);
10267             SendToProgram(buf, &pairing);
10268             snprintf(buf, 1<<16, "pairing %d\n", nr+1);
10269             SendToProgram(buf, &pairing);
10270             return 0; // wait for pairing engine to answer (which causes NextTourneyGame to be called again...
10271         }
10272         pairingReceived = 0;                              // ... so we continue here 
10273         *swapColors = 0;
10274         appData.matchGames = appData.tourneyCycles * syncInterval - 1;
10275         whitePlayer = savedWhitePlayer-1; blackPlayer = savedBlackPlayer-1;
10276         matchGame = 1; roundNr = nr / syncInterval + 1;
10277     }
10278
10279     if(first.pr != NoProc && second.pr != NoProc || nr<0) return 1; // engines already loaded
10280
10281     // redefine engines, engine dir, etc.
10282     NamesToList(firstChessProgramNames, command, mnemonic, "all"); // get mnemonics of installed engines
10283     if(first.pr == NoProc) {
10284       SetPlayer(whitePlayer, appData.participants); // find white player amongst it, and parse its engine line
10285       InitEngine(&first, 0);  // initialize ChessProgramStates based on new settings.
10286     }
10287     if(second.pr == NoProc) {
10288       SwapEngines(1);
10289       SetPlayer(blackPlayer, appData.participants); // find black player amongst it, and parse its engine line
10290       SwapEngines(1);         // and make that valid for second engine by swapping
10291       InitEngine(&second, 1);
10292     }
10293     CommonEngineInit();     // after this TwoMachinesEvent will create correct engine processes
10294     UpdateLogos(FALSE);     // leave display to ModeHiglight()
10295     return 1;
10296 }
10297
10298 void
10299 NextMatchGame ()
10300 {   // performs game initialization that does not invoke engines, and then tries to start the game
10301     int res, firstWhite, swapColors = 0;
10302     if(!NextTourneyGame(nextGame, &swapColors)) return; // this sets matchGame, -fcp / -scp and other options for next game, if needed
10303     if(matchMode && appData.debugMode) { // [HGM] debug split: game is part of a match; we might have to create a debug file just for this game
10304         char buf[MSG_SIZ];
10305         snprintf(buf, MSG_SIZ, appData.nameOfDebugFile, nextGame+1); // expand name of debug file with %d in it
10306         if(strcmp(buf, currentDebugFile)) { // name has changed
10307             FILE *f = fopen(buf, "w");
10308             if(f) { // if opening the new file failed, just keep using the old one
10309                 ASSIGN(currentDebugFile, buf);
10310                 fclose(debugFP);
10311                 debugFP = f;
10312             }
10313             if(appData.serverFileName) {
10314                 if(serverFP) fclose(serverFP);
10315                 serverFP = fopen(appData.serverFileName, "w");
10316                 if(serverFP && first.pr != NoProc) fprintf(serverFP, "StartChildProcess (dir=\".\") .\\%s\n", first.tidy);
10317                 if(serverFP && second.pr != NoProc) fprintf(serverFP, "StartChildProcess (dir=\".\") .\\%s\n", second.tidy);
10318             }
10319         }
10320     }
10321     firstWhite = appData.firstPlaysBlack ^ (matchGame & 1 | appData.sameColorGames > 1); // non-incremental default
10322     firstWhite ^= swapColors; // reverses if NextTourneyGame says we are in an odd round
10323     first.twoMachinesColor =  firstWhite ? "white\n" : "black\n";   // perform actual color assignement
10324     second.twoMachinesColor = firstWhite ? "black\n" : "white\n";
10325     appData.noChessProgram = (first.pr == NoProc); // kludge to prevent Reset from starting up chess program
10326     if(appData.loadGameIndex == -2) srandom(appData.seedBase + 68163*(nextGame & ~1)); // deterministic seed to force same opening
10327     Reset(FALSE, first.pr != NoProc);
10328     res = LoadGameOrPosition(matchGame); // setup game
10329     appData.noChessProgram = FALSE; // LoadGameOrPosition might call Reset too!
10330     if(!res) return; // abort when bad game/pos file
10331     TwoMachinesEvent();
10332 }
10333
10334 void
10335 UserAdjudicationEvent (int result)
10336 {
10337     ChessMove gameResult = GameIsDrawn;
10338
10339     if( result > 0 ) {
10340         gameResult = WhiteWins;
10341     }
10342     else if( result < 0 ) {
10343         gameResult = BlackWins;
10344     }
10345
10346     if( gameMode == TwoMachinesPlay ) {
10347         GameEnds( gameResult, "User adjudication", GE_XBOARD );
10348     }
10349 }
10350
10351
10352 // [HGM] save: calculate checksum of game to make games easily identifiable
10353 int
10354 StringCheckSum (char *s)
10355 {
10356         int i = 0;
10357         if(s==NULL) return 0;
10358         while(*s) i = i*259 + *s++;
10359         return i;
10360 }
10361
10362 int
10363 GameCheckSum ()
10364 {
10365         int i, sum=0;
10366         for(i=backwardMostMove; i<forwardMostMove; i++) {
10367                 sum += pvInfoList[i].depth;
10368                 sum += StringCheckSum(parseList[i]);
10369                 sum += StringCheckSum(commentList[i]);
10370                 sum *= 261;
10371         }
10372         if(i>1 && sum==0) sum++; // make sure never zero for non-empty game
10373         return sum + StringCheckSum(commentList[i]);
10374 } // end of save patch
10375
10376 void
10377 GameEnds (ChessMove result, char *resultDetails, int whosays)
10378 {
10379     GameMode nextGameMode;
10380     int isIcsGame;
10381     char buf[MSG_SIZ], popupRequested = 0, *ranking = NULL;
10382
10383     if(endingGame) return; /* [HGM] crash: forbid recursion */
10384     endingGame = 1;
10385     if(twoBoards) { // [HGM] dual: switch back to one board
10386         twoBoards = partnerUp = 0; InitDrawingSizes(-2, 0);
10387         DrawPosition(TRUE, partnerBoard); // observed game becomes foreground
10388     }
10389     if (appData.debugMode) {
10390       fprintf(debugFP, "GameEnds(%d, %s, %d)\n",
10391               result, resultDetails ? resultDetails : "(null)", whosays);
10392     }
10393
10394     fromX = fromY = -1; // [HGM] abort any move the user is entering.
10395
10396     if (appData.icsActive && (whosays == GE_ENGINE || whosays >= GE_ENGINE1)) {
10397         /* If we are playing on ICS, the server decides when the
10398            game is over, but the engine can offer to draw, claim
10399            a draw, or resign.
10400          */
10401 #if ZIPPY
10402         if (appData.zippyPlay && first.initDone) {
10403             if (result == GameIsDrawn) {
10404                 /* In case draw still needs to be claimed */
10405                 SendToICS(ics_prefix);
10406                 SendToICS("draw\n");
10407             } else if (StrCaseStr(resultDetails, "resign")) {
10408                 SendToICS(ics_prefix);
10409                 SendToICS("resign\n");
10410             }
10411         }
10412 #endif
10413         endingGame = 0; /* [HGM] crash */
10414         return;
10415     }
10416
10417     /* If we're loading the game from a file, stop */
10418     if (whosays == GE_FILE) {
10419       (void) StopLoadGameTimer();
10420       gameFileFP = NULL;
10421     }
10422
10423     /* Cancel draw offers */
10424     first.offeredDraw = second.offeredDraw = 0;
10425
10426     /* If this is an ICS game, only ICS can really say it's done;
10427        if not, anyone can. */
10428     isIcsGame = (gameMode == IcsPlayingWhite ||
10429                  gameMode == IcsPlayingBlack ||
10430                  gameMode == IcsObserving    ||
10431                  gameMode == IcsExamining);
10432
10433     if (!isIcsGame || whosays == GE_ICS) {
10434         /* OK -- not an ICS game, or ICS said it was done */
10435         StopClocks();
10436         if (!isIcsGame && !appData.noChessProgram)
10437           SetUserThinkingEnables();
10438
10439         /* [HGM] if a machine claims the game end we verify this claim */
10440         if(gameMode == TwoMachinesPlay && appData.testClaims) {
10441             if(appData.testLegality && whosays >= GE_ENGINE1 ) {
10442                 char claimer;
10443                 ChessMove trueResult = (ChessMove) -1;
10444
10445                 claimer = whosays == GE_ENGINE1 ?      /* color of claimer */
10446                                             first.twoMachinesColor[0] :
10447                                             second.twoMachinesColor[0] ;
10448
10449                 // [HGM] losers: because the logic is becoming a bit hairy, determine true result first
10450                 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_CHECKMATE) {
10451                     /* [HGM] verify: engine mate claims accepted if they were flagged */
10452                     trueResult = WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins;
10453                 } else
10454                 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_WINS) { // added code for games where being mated is a win
10455                     /* [HGM] verify: engine mate claims accepted if they were flagged */
10456                     trueResult = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
10457                 } else
10458                 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_STALEMATE) { // only used to indicate draws now
10459                     trueResult = GameIsDrawn; // default; in variants where stalemate loses, Status is CHECKMATE
10460                 }
10461
10462                 // now verify win claims, but not in drop games, as we don't understand those yet
10463                 if( (gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper
10464                                                  || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand) &&
10465                     (result == WhiteWins && claimer == 'w' ||
10466                      result == BlackWins && claimer == 'b'   ) ) { // case to verify: engine claims own win
10467                       if (appData.debugMode) {
10468                         fprintf(debugFP, "result=%d sp=%d move=%d\n",
10469                                 result, (signed char)boards[forwardMostMove][EP_STATUS], forwardMostMove);
10470                       }
10471                       if(result != trueResult) {
10472                         snprintf(buf, MSG_SIZ, "False win claim: '%s'", resultDetails);
10473                               result = claimer == 'w' ? BlackWins : WhiteWins;
10474                               resultDetails = buf;
10475                       }
10476                 } else
10477                 if( result == GameIsDrawn && (signed char)boards[forwardMostMove][EP_STATUS] > EP_DRAWS
10478                     && (forwardMostMove <= backwardMostMove ||
10479                         (signed char)boards[forwardMostMove-1][EP_STATUS] > EP_DRAWS ||
10480                         (claimer=='b')==(forwardMostMove&1))
10481                                                                                   ) {
10482                       /* [HGM] verify: draws that were not flagged are false claims */
10483                   snprintf(buf, MSG_SIZ, "False draw claim: '%s'", resultDetails);
10484                       result = claimer == 'w' ? BlackWins : WhiteWins;
10485                       resultDetails = buf;
10486                 }
10487                 /* (Claiming a loss is accepted no questions asked!) */
10488             }
10489             /* [HGM] bare: don't allow bare King to win */
10490             if((gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper
10491                                             || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand)
10492                && gameInfo.variant != VariantLosers && gameInfo.variant != VariantGiveaway
10493                && gameInfo.variant != VariantSuicide // [HGM] losers: except in losers, of course...
10494                && result != GameIsDrawn)
10495             {   int i, j, k=0, color = (result==WhiteWins ? (int)WhitePawn : (int)BlackPawn);
10496                 for(j=BOARD_LEFT; j<BOARD_RGHT; j++) for(i=0; i<BOARD_HEIGHT; i++) {
10497                         int p = (signed char)boards[forwardMostMove][i][j] - color;
10498                         if(p >= 0 && p <= (int)WhiteKing) k++;
10499                 }
10500                 if (appData.debugMode) {
10501                      fprintf(debugFP, "GE(%d, %s, %d) bare king k=%d color=%d\n",
10502                         result, resultDetails ? resultDetails : "(null)", whosays, k, color);
10503                 }
10504                 if(k <= 1) {
10505                         result = GameIsDrawn;
10506                         snprintf(buf, MSG_SIZ, "%s but bare king", resultDetails);
10507                         resultDetails = buf;
10508                 }
10509             }
10510         }
10511
10512
10513         if(serverMoves != NULL && !loadFlag) { char c = '=';
10514             if(result==WhiteWins) c = '+';
10515             if(result==BlackWins) c = '-';
10516             if(resultDetails != NULL)
10517                 fprintf(serverMoves, ";%c;%s\n", c, resultDetails), fflush(serverMoves);
10518         }
10519         if (resultDetails != NULL) {
10520             gameInfo.result = result;
10521             gameInfo.resultDetails = StrSave(resultDetails);
10522
10523             /* display last move only if game was not loaded from file */
10524             if ((whosays != GE_FILE) && (currentMove == forwardMostMove))
10525                 DisplayMove(currentMove - 1);
10526
10527             if (forwardMostMove != 0) {
10528                 if (gameMode != PlayFromGameFile && gameMode != EditGame
10529                     && lastSavedGame != GameCheckSum() // [HGM] save: suppress duplicates
10530                                                                 ) {
10531                     if (*appData.saveGameFile != NULLCHAR) {
10532                         SaveGameToFile(appData.saveGameFile, TRUE);
10533                     } else if (appData.autoSaveGames) {
10534                         AutoSaveGame();
10535                     }
10536                     if (*appData.savePositionFile != NULLCHAR) {
10537                         SavePositionToFile(appData.savePositionFile);
10538                     }
10539                 }
10540             }
10541
10542             /* Tell program how game ended in case it is learning */
10543             /* [HGM] Moved this to after saving the PGN, just in case */
10544             /* engine died and we got here through time loss. In that */
10545             /* case we will get a fatal error writing the pipe, which */
10546             /* would otherwise lose us the PGN.                       */
10547             /* [HGM] crash: not needed anymore, but doesn't hurt;     */
10548             /* output during GameEnds should never be fatal anymore   */
10549             if (gameMode == MachinePlaysWhite ||
10550                 gameMode == MachinePlaysBlack ||
10551                 gameMode == TwoMachinesPlay ||
10552                 gameMode == IcsPlayingWhite ||
10553                 gameMode == IcsPlayingBlack ||
10554                 gameMode == BeginningOfGame) {
10555                 char buf[MSG_SIZ];
10556                 snprintf(buf, MSG_SIZ, "result %s {%s}\n", PGNResult(result),
10557                         resultDetails);
10558                 if (first.pr != NoProc) {
10559                     SendToProgram(buf, &first);
10560                 }
10561                 if (second.pr != NoProc &&
10562                     gameMode == TwoMachinesPlay) {
10563                     SendToProgram(buf, &second);
10564                 }
10565             }
10566         }
10567
10568         if (appData.icsActive) {
10569             if (appData.quietPlay &&
10570                 (gameMode == IcsPlayingWhite ||
10571                  gameMode == IcsPlayingBlack)) {
10572                 SendToICS(ics_prefix);
10573                 SendToICS("set shout 1\n");
10574             }
10575             nextGameMode = IcsIdle;
10576             ics_user_moved = FALSE;
10577             /* clean up premove.  It's ugly when the game has ended and the
10578              * premove highlights are still on the board.
10579              */
10580             if (gotPremove) {
10581               gotPremove = FALSE;
10582               ClearPremoveHighlights();
10583               DrawPosition(FALSE, boards[currentMove]);
10584             }
10585             if (whosays == GE_ICS) {
10586                 switch (result) {
10587                 case WhiteWins:
10588                     if (gameMode == IcsPlayingWhite)
10589                         PlayIcsWinSound();
10590                     else if(gameMode == IcsPlayingBlack)
10591                         PlayIcsLossSound();
10592                     break;
10593                 case BlackWins:
10594                     if (gameMode == IcsPlayingBlack)
10595                         PlayIcsWinSound();
10596                     else if(gameMode == IcsPlayingWhite)
10597                         PlayIcsLossSound();
10598                     break;
10599                 case GameIsDrawn:
10600                     PlayIcsDrawSound();
10601                     break;
10602                 default:
10603                     PlayIcsUnfinishedSound();
10604                 }
10605             }
10606         } else if (gameMode == EditGame ||
10607                    gameMode == PlayFromGameFile ||
10608                    gameMode == AnalyzeMode ||
10609                    gameMode == AnalyzeFile) {
10610             nextGameMode = gameMode;
10611         } else {
10612             nextGameMode = EndOfGame;
10613         }
10614         pausing = FALSE;
10615         ModeHighlight();
10616     } else {
10617         nextGameMode = gameMode;
10618     }
10619
10620     if (appData.noChessProgram) {
10621         gameMode = nextGameMode;
10622         ModeHighlight();
10623         endingGame = 0; /* [HGM] crash */
10624         return;
10625     }
10626
10627     if (first.reuse) {
10628         /* Put first chess program into idle state */
10629         if (first.pr != NoProc &&
10630             (gameMode == MachinePlaysWhite ||
10631              gameMode == MachinePlaysBlack ||
10632              gameMode == TwoMachinesPlay ||
10633              gameMode == IcsPlayingWhite ||
10634              gameMode == IcsPlayingBlack ||
10635              gameMode == BeginningOfGame)) {
10636             SendToProgram("force\n", &first);
10637             if (first.usePing) {
10638               char buf[MSG_SIZ];
10639               snprintf(buf, MSG_SIZ, "ping %d\n", ++first.lastPing);
10640               SendToProgram(buf, &first);
10641             }
10642         }
10643     } else if (result != GameUnfinished || nextGameMode == IcsIdle) {
10644         /* Kill off first chess program */
10645         if (first.isr != NULL)
10646           RemoveInputSource(first.isr);
10647         first.isr = NULL;
10648
10649         if (first.pr != NoProc) {
10650             ExitAnalyzeMode();
10651             DoSleep( appData.delayBeforeQuit );
10652             SendToProgram("quit\n", &first);
10653             DoSleep( appData.delayAfterQuit );
10654             DestroyChildProcess(first.pr, first.useSigterm);
10655         }
10656         first.pr = NoProc;
10657     }
10658     if (second.reuse) {
10659         /* Put second chess program into idle state */
10660         if (second.pr != NoProc &&
10661             gameMode == TwoMachinesPlay) {
10662             SendToProgram("force\n", &second);
10663             if (second.usePing) {
10664               char buf[MSG_SIZ];
10665               snprintf(buf, MSG_SIZ, "ping %d\n", ++second.lastPing);
10666               SendToProgram(buf, &second);
10667             }
10668         }
10669     } else if (result != GameUnfinished || nextGameMode == IcsIdle) {
10670         /* Kill off second chess program */
10671         if (second.isr != NULL)
10672           RemoveInputSource(second.isr);
10673         second.isr = NULL;
10674
10675         if (second.pr != NoProc) {
10676             DoSleep( appData.delayBeforeQuit );
10677             SendToProgram("quit\n", &second);
10678             DoSleep( appData.delayAfterQuit );
10679             DestroyChildProcess(second.pr, second.useSigterm);
10680         }
10681         second.pr = NoProc;
10682     }
10683
10684     if (matchMode && (gameMode == TwoMachinesPlay || waitingForGame && exiting)) {
10685         char resChar = '=';
10686         switch (result) {
10687         case WhiteWins:
10688           resChar = '+';
10689           if (first.twoMachinesColor[0] == 'w') {
10690             first.matchWins++;
10691           } else {
10692             second.matchWins++;
10693           }
10694           break;
10695         case BlackWins:
10696           resChar = '-';
10697           if (first.twoMachinesColor[0] == 'b') {
10698             first.matchWins++;
10699           } else {
10700             second.matchWins++;
10701           }
10702           break;
10703         case GameUnfinished:
10704           resChar = ' ';
10705         default:
10706           break;
10707         }
10708
10709         if(waitingForGame) resChar = ' '; // quit while waiting for round sync: unreserve already reserved game
10710         if(appData.tourneyFile[0]){ // [HGM] we are in a tourney; update tourney file with game result
10711             if(appData.afterGame && appData.afterGame[0]) RunCommand(appData.afterGame);
10712             ReserveGame(nextGame, resChar); // sets nextGame
10713             if(nextGame > appData.matchGames) appData.tourneyFile[0] = 0, ranking = TourneyStandings(3); // tourney is done
10714             else ranking = strdup("busy"); //suppress popup when aborted but not finished
10715         } else roundNr = nextGame = matchGame + 1; // normal match, just increment; round equals matchGame
10716
10717         if (nextGame <= appData.matchGames && !abortMatch) {
10718             gameMode = nextGameMode;
10719             matchGame = nextGame; // this will be overruled in tourney mode!
10720             GetTimeMark(&pauseStart); // [HGM] matchpause: stipulate a pause
10721             ScheduleDelayedEvent(NextMatchGame, 10); // but start game immediately (as it will wait out the pause itself)
10722             endingGame = 0; /* [HGM] crash */
10723             return;
10724         } else {
10725             gameMode = nextGameMode;
10726             snprintf(buf, MSG_SIZ, _("Match %s vs. %s: final score %d-%d-%d"),
10727                      first.tidy, second.tidy,
10728                      first.matchWins, second.matchWins,
10729                      appData.matchGames - (first.matchWins + second.matchWins));
10730             if(!appData.tourneyFile[0]) matchGame++, DisplayTwoMachinesTitle(); // [HGM] update result in window title
10731             if(ranking && strcmp(ranking, "busy") && appData.afterTourney && appData.afterTourney[0]) RunCommand(appData.afterTourney);
10732             popupRequested++; // [HGM] crash: postpone to after resetting endingGame
10733             if (appData.firstPlaysBlack) { // [HGM] match: back to original for next match
10734                 first.twoMachinesColor = "black\n";
10735                 second.twoMachinesColor = "white\n";
10736             } else {
10737                 first.twoMachinesColor = "white\n";
10738                 second.twoMachinesColor = "black\n";
10739             }
10740         }
10741     }
10742     if ((gameMode == AnalyzeMode || gameMode == AnalyzeFile) &&
10743         !(nextGameMode == AnalyzeMode || nextGameMode == AnalyzeFile))
10744       ExitAnalyzeMode();
10745     gameMode = nextGameMode;
10746     ModeHighlight();
10747     endingGame = 0;  /* [HGM] crash */
10748     if(popupRequested) { // [HGM] crash: this calls GameEnds recursively through ExitEvent! Make it a harmless tail recursion.
10749         if(matchMode == TRUE) { // match through command line: exit with or without popup
10750             if(ranking) {
10751                 ToNrEvent(forwardMostMove);
10752                 if(strcmp(ranking, "busy")) DisplayFatalError(ranking, 0, 0);
10753                 else ExitEvent(0);
10754             } else DisplayFatalError(buf, 0, 0);
10755         } else { // match through menu; just stop, with or without popup
10756             matchMode = FALSE; appData.matchGames = matchGame = roundNr = 0;
10757             ModeHighlight();
10758             if(ranking){
10759                 if(strcmp(ranking, "busy")) DisplayNote(ranking);
10760             } else DisplayNote(buf);
10761       }
10762       if(ranking) free(ranking);
10763     }
10764 }
10765
10766 /* Assumes program was just initialized (initString sent).
10767    Leaves program in force mode. */
10768 void
10769 FeedMovesToProgram (ChessProgramState *cps, int upto)
10770 {
10771     int i;
10772
10773     if (appData.debugMode)
10774       fprintf(debugFP, "Feeding %smoves %d through %d to %s chess program\n",
10775               startedFromSetupPosition ? "position and " : "",
10776               backwardMostMove, upto, cps->which);
10777     if(currentlyInitializedVariant != gameInfo.variant) {
10778       char buf[MSG_SIZ];
10779         // [HGM] variantswitch: make engine aware of new variant
10780         if(cps->protocolVersion > 1 && StrStr(cps->variants, VariantName(gameInfo.variant)) == NULL)
10781                 return; // [HGM] refrain from feeding moves altogether if variant is unsupported!
10782         snprintf(buf, MSG_SIZ, "variant %s\n", VariantName(gameInfo.variant));
10783         SendToProgram(buf, cps);
10784         currentlyInitializedVariant = gameInfo.variant;
10785     }
10786     SendToProgram("force\n", cps);
10787     if (startedFromSetupPosition) {
10788         SendBoard(cps, backwardMostMove);
10789     if (appData.debugMode) {
10790         fprintf(debugFP, "feedMoves\n");
10791     }
10792     }
10793     for (i = backwardMostMove; i < upto; i++) {
10794         SendMoveToProgram(i, cps);
10795     }
10796 }
10797
10798
10799 int
10800 ResurrectChessProgram ()
10801 {
10802      /* The chess program may have exited.
10803         If so, restart it and feed it all the moves made so far. */
10804     static int doInit = 0;
10805
10806     if (appData.noChessProgram) return 1;
10807
10808     if(matchMode && appData.tourneyFile[0]) { // [HGM] tourney: make sure we get features after engine replacement. (Should we always do this?)
10809         if(WaitForEngine(&first, TwoMachinesEventIfReady)) { doInit = 1; return 0; } // request to do init on next visit
10810         if(!doInit) return 1; // this replaces testing first.pr != NoProc, which is true when we get here, but first time no reason to abort
10811         doInit = 0; // we fell through (first time after starting the engine); make sure it doesn't happen again
10812     } else {
10813         if (first.pr != NoProc) return 1;
10814         StartChessProgram(&first);
10815     }
10816     InitChessProgram(&first, FALSE);
10817     FeedMovesToProgram(&first, currentMove);
10818
10819     if (!first.sendTime) {
10820         /* can't tell gnuchess what its clock should read,
10821            so we bow to its notion. */
10822         ResetClocks();
10823         timeRemaining[0][currentMove] = whiteTimeRemaining;
10824         timeRemaining[1][currentMove] = blackTimeRemaining;
10825     }
10826
10827     if ((gameMode == AnalyzeMode || gameMode == AnalyzeFile ||
10828                 appData.icsEngineAnalyze) && first.analysisSupport) {
10829       SendToProgram("analyze\n", &first);
10830       first.analyzing = TRUE;
10831     }
10832     return 1;
10833 }
10834
10835 /*
10836  * Button procedures
10837  */
10838 void
10839 Reset (int redraw, int init)
10840 {
10841     int i;
10842
10843     if (appData.debugMode) {
10844         fprintf(debugFP, "Reset(%d, %d) from gameMode %d\n",
10845                 redraw, init, gameMode);
10846     }
10847     CleanupTail(); // [HGM] vari: delete any stored variations
10848     CommentPopDown(); // [HGM] make sure no comments to the previous game keep hanging on
10849     pausing = pauseExamInvalid = FALSE;
10850     startedFromSetupPosition = blackPlaysFirst = FALSE;
10851     firstMove = TRUE;
10852     whiteFlag = blackFlag = FALSE;
10853     userOfferedDraw = FALSE;
10854     hintRequested = bookRequested = FALSE;
10855     first.maybeThinking = FALSE;
10856     second.maybeThinking = FALSE;
10857     first.bookSuspend = FALSE; // [HGM] book
10858     second.bookSuspend = FALSE;
10859     thinkOutput[0] = NULLCHAR;
10860     lastHint[0] = NULLCHAR;
10861     ClearGameInfo(&gameInfo);
10862     gameInfo.variant = StringToVariant(appData.variant);
10863     ics_user_moved = ics_clock_paused = FALSE;
10864     ics_getting_history = H_FALSE;
10865     ics_gamenum = -1;
10866     white_holding[0] = black_holding[0] = NULLCHAR;
10867     ClearProgramStats();
10868     opponentKibitzes = FALSE; // [HGM] kibitz: do not reserve space in engine-output window in zippy mode
10869
10870     ResetFrontEnd();
10871     ClearHighlights();
10872     flipView = appData.flipView;
10873     ClearPremoveHighlights();
10874     gotPremove = FALSE;
10875     alarmSounded = FALSE;
10876
10877     GameEnds(EndOfFile, NULL, GE_PLAYER);
10878     if(appData.serverMovesName != NULL) {
10879         /* [HGM] prepare to make moves file for broadcasting */
10880         clock_t t = clock();
10881         if(serverMoves != NULL) fclose(serverMoves);
10882         serverMoves = fopen(appData.serverMovesName, "r");
10883         if(serverMoves != NULL) {
10884             fclose(serverMoves);
10885             /* delay 15 sec before overwriting, so all clients can see end */
10886             while(clock()-t < appData.serverPause*CLOCKS_PER_SEC);
10887         }
10888         serverMoves = fopen(appData.serverMovesName, "w");
10889     }
10890
10891     ExitAnalyzeMode();
10892     gameMode = BeginningOfGame;
10893     ModeHighlight();
10894     if(appData.icsActive) gameInfo.variant = VariantNormal;
10895     currentMove = forwardMostMove = backwardMostMove = 0;
10896     MarkTargetSquares(1);
10897     InitPosition(redraw);
10898     for (i = 0; i < MAX_MOVES; i++) {
10899         if (commentList[i] != NULL) {
10900             free(commentList[i]);
10901             commentList[i] = NULL;
10902         }
10903     }
10904     ResetClocks();
10905     timeRemaining[0][0] = whiteTimeRemaining;
10906     timeRemaining[1][0] = blackTimeRemaining;
10907
10908     if (first.pr == NoProc) {
10909         StartChessProgram(&first);
10910     }
10911     if (init) {
10912             InitChessProgram(&first, startedFromSetupPosition);
10913     }
10914     DisplayTitle("");
10915     DisplayMessage("", "");
10916     HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
10917     lastSavedGame = 0; // [HGM] save: make sure next game counts as unsaved
10918     ClearMap();        // [HGM] exclude: invalidate map
10919 }
10920
10921 void
10922 AutoPlayGameLoop ()
10923 {
10924     for (;;) {
10925         if (!AutoPlayOneMove())
10926           return;
10927         if (matchMode || appData.timeDelay == 0)
10928           continue;
10929         if (appData.timeDelay < 0)
10930           return;
10931         StartLoadGameTimer((long)(1000.0 * appData.timeDelay));
10932         break;
10933     }
10934 }
10935
10936
10937 int
10938 AutoPlayOneMove ()
10939 {
10940     int fromX, fromY, toX, toY;
10941
10942     if (appData.debugMode) {
10943       fprintf(debugFP, "AutoPlayOneMove(): current %d\n", currentMove);
10944     }
10945
10946     if (gameMode != PlayFromGameFile && gameMode != AnalyzeFile)
10947       return FALSE;
10948
10949     if (gameMode == AnalyzeFile && currentMove > backwardMostMove) {
10950       pvInfoList[currentMove].depth = programStats.depth;
10951       pvInfoList[currentMove].score = programStats.score;
10952       pvInfoList[currentMove].time  = 0;
10953       if(currentMove < forwardMostMove) AppendComment(currentMove+1, lastPV[0], 2);
10954     }
10955
10956     if (currentMove >= forwardMostMove) {
10957       if(gameMode == AnalyzeFile) { ExitAnalyzeMode(); SendToProgram("force\n", &first); }
10958 //      gameMode = EndOfGame;
10959 //      ModeHighlight();
10960
10961       /* [AS] Clear current move marker at the end of a game */
10962       /* HistorySet(parseList, backwardMostMove, forwardMostMove, -1); */
10963
10964       return FALSE;
10965     }
10966
10967     toX = moveList[currentMove][2] - AAA;
10968     toY = moveList[currentMove][3] - ONE;
10969
10970     if (moveList[currentMove][1] == '@') {
10971         if (appData.highlightLastMove) {
10972             SetHighlights(-1, -1, toX, toY);
10973         }
10974     } else {
10975         fromX = moveList[currentMove][0] - AAA;
10976         fromY = moveList[currentMove][1] - ONE;
10977
10978         HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove); /* [AS] */
10979
10980         AnimateMove(boards[currentMove], fromX, fromY, toX, toY);
10981
10982         if (appData.highlightLastMove) {
10983             SetHighlights(fromX, fromY, toX, toY);
10984         }
10985     }
10986     DisplayMove(currentMove);
10987     SendMoveToProgram(currentMove++, &first);
10988     DisplayBothClocks();
10989     DrawPosition(FALSE, boards[currentMove]);
10990     // [HGM] PV info: always display, routine tests if empty
10991     DisplayComment(currentMove - 1, commentList[currentMove]);
10992     return TRUE;
10993 }
10994
10995
10996 int
10997 LoadGameOneMove (ChessMove readAhead)
10998 {
10999     int fromX = 0, fromY = 0, toX = 0, toY = 0, done;
11000     char promoChar = NULLCHAR;
11001     ChessMove moveType;
11002     char move[MSG_SIZ];
11003     char *p, *q;
11004
11005     if (gameMode != PlayFromGameFile && gameMode != AnalyzeFile &&
11006         gameMode != AnalyzeMode && gameMode != Training) {
11007         gameFileFP = NULL;
11008         return FALSE;
11009     }
11010
11011     yyboardindex = forwardMostMove;
11012     if (readAhead != EndOfFile) {
11013       moveType = readAhead;
11014     } else {
11015       if (gameFileFP == NULL)
11016           return FALSE;
11017       moveType = (ChessMove) Myylex();
11018     }
11019
11020     done = FALSE;
11021     switch (moveType) {
11022       case Comment:
11023         if (appData.debugMode)
11024           fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
11025         p = yy_text;
11026
11027         /* append the comment but don't display it */
11028         AppendComment(currentMove, p, FALSE);
11029         return TRUE;
11030
11031       case WhiteCapturesEnPassant:
11032       case BlackCapturesEnPassant:
11033       case WhitePromotion:
11034       case BlackPromotion:
11035       case WhiteNonPromotion:
11036       case BlackNonPromotion:
11037       case NormalMove:
11038       case WhiteKingSideCastle:
11039       case WhiteQueenSideCastle:
11040       case BlackKingSideCastle:
11041       case BlackQueenSideCastle:
11042       case WhiteKingSideCastleWild:
11043       case WhiteQueenSideCastleWild:
11044       case BlackKingSideCastleWild:
11045       case BlackQueenSideCastleWild:
11046       /* PUSH Fabien */
11047       case WhiteHSideCastleFR:
11048       case WhiteASideCastleFR:
11049       case BlackHSideCastleFR:
11050       case BlackASideCastleFR:
11051       /* POP Fabien */
11052         if (appData.debugMode)
11053           fprintf(debugFP, "Parsed %s into %s\n", yy_text, currentMoveString);
11054         fromX = currentMoveString[0] - AAA;
11055         fromY = currentMoveString[1] - ONE;
11056         toX = currentMoveString[2] - AAA;
11057         toY = currentMoveString[3] - ONE;
11058         promoChar = currentMoveString[4];
11059         break;
11060
11061       case WhiteDrop:
11062       case BlackDrop:
11063         if (appData.debugMode)
11064           fprintf(debugFP, "Parsed %s into %s\n", yy_text, currentMoveString);
11065         fromX = moveType == WhiteDrop ?
11066           (int) CharToPiece(ToUpper(currentMoveString[0])) :
11067         (int) CharToPiece(ToLower(currentMoveString[0]));
11068         fromY = DROP_RANK;
11069         toX = currentMoveString[2] - AAA;
11070         toY = currentMoveString[3] - ONE;
11071         break;
11072
11073       case WhiteWins:
11074       case BlackWins:
11075       case GameIsDrawn:
11076       case GameUnfinished:
11077         if (appData.debugMode)
11078           fprintf(debugFP, "Parsed game end: %s\n", yy_text);
11079         p = strchr(yy_text, '{');
11080         if (p == NULL) p = strchr(yy_text, '(');
11081         if (p == NULL) {
11082             p = yy_text;
11083             if (p[0] == '0' || p[0] == '1' || p[0] == '*') p = "";
11084         } else {
11085             q = strchr(p, *p == '{' ? '}' : ')');
11086             if (q != NULL) *q = NULLCHAR;
11087             p++;
11088         }
11089         while(q = strchr(p, '\n')) *q = ' '; // [HGM] crush linefeeds in result message
11090         GameEnds(moveType, p, GE_FILE);
11091         done = TRUE;
11092         if (cmailMsgLoaded) {
11093             ClearHighlights();
11094             flipView = WhiteOnMove(currentMove);
11095             if (moveType == GameUnfinished) flipView = !flipView;
11096             if (appData.debugMode)
11097               fprintf(debugFP, "Setting flipView to %d\n", flipView) ;
11098         }
11099         break;
11100
11101       case EndOfFile:
11102         if (appData.debugMode)
11103           fprintf(debugFP, "Parser hit end of file\n");
11104         switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
11105           case MT_NONE:
11106           case MT_CHECK:
11107             break;
11108           case MT_CHECKMATE:
11109           case MT_STAINMATE:
11110             if (WhiteOnMove(currentMove)) {
11111                 GameEnds(BlackWins, "Black mates", GE_FILE);
11112             } else {
11113                 GameEnds(WhiteWins, "White mates", GE_FILE);
11114             }
11115             break;
11116           case MT_STALEMATE:
11117             GameEnds(GameIsDrawn, "Stalemate", GE_FILE);
11118             break;
11119         }
11120         done = TRUE;
11121         break;
11122
11123       case MoveNumberOne:
11124         if (lastLoadGameStart == GNUChessGame) {
11125             /* GNUChessGames have numbers, but they aren't move numbers */
11126             if (appData.debugMode)
11127               fprintf(debugFP, "Parser ignoring: '%s' (%d)\n",
11128                       yy_text, (int) moveType);
11129             return LoadGameOneMove(EndOfFile); /* tail recursion */
11130         }
11131         /* else fall thru */
11132
11133       case XBoardGame:
11134       case GNUChessGame:
11135       case PGNTag:
11136         /* Reached start of next game in file */
11137         if (appData.debugMode)
11138           fprintf(debugFP, "Parsed start of next game: %s\n", yy_text);
11139         switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
11140           case MT_NONE:
11141           case MT_CHECK:
11142             break;
11143           case MT_CHECKMATE:
11144           case MT_STAINMATE:
11145             if (WhiteOnMove(currentMove)) {
11146                 GameEnds(BlackWins, "Black mates", GE_FILE);
11147             } else {
11148                 GameEnds(WhiteWins, "White mates", GE_FILE);
11149             }
11150             break;
11151           case MT_STALEMATE:
11152             GameEnds(GameIsDrawn, "Stalemate", GE_FILE);
11153             break;
11154         }
11155         done = TRUE;
11156         break;
11157
11158       case PositionDiagram:     /* should not happen; ignore */
11159       case ElapsedTime:         /* ignore */
11160       case NAG:                 /* ignore */
11161         if (appData.debugMode)
11162           fprintf(debugFP, "Parser ignoring: '%s' (%d)\n",
11163                   yy_text, (int) moveType);
11164         return LoadGameOneMove(EndOfFile); /* tail recursion */
11165
11166       case IllegalMove:
11167         if (appData.testLegality) {
11168             if (appData.debugMode)
11169               fprintf(debugFP, "Parsed IllegalMove: %s\n", yy_text);
11170             snprintf(move, MSG_SIZ, _("Illegal move: %d.%s%s"),
11171                     (forwardMostMove / 2) + 1,
11172                     WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
11173             DisplayError(move, 0);
11174             done = TRUE;
11175         } else {
11176             if (appData.debugMode)
11177               fprintf(debugFP, "Parsed %s into IllegalMove %s\n",
11178                       yy_text, currentMoveString);
11179             fromX = currentMoveString[0] - AAA;
11180             fromY = currentMoveString[1] - ONE;
11181             toX = currentMoveString[2] - AAA;
11182             toY = currentMoveString[3] - ONE;
11183             promoChar = currentMoveString[4];
11184         }
11185         break;
11186
11187       case AmbiguousMove:
11188         if (appData.debugMode)
11189           fprintf(debugFP, "Parsed AmbiguousMove: %s\n", yy_text);
11190         snprintf(move, MSG_SIZ, _("Ambiguous move: %d.%s%s"),
11191                 (forwardMostMove / 2) + 1,
11192                 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
11193         DisplayError(move, 0);
11194         done = TRUE;
11195         break;
11196
11197       default:
11198       case ImpossibleMove:
11199         if (appData.debugMode)
11200           fprintf(debugFP, "Parsed ImpossibleMove (type = %d): %s\n", moveType, yy_text);
11201         snprintf(move, MSG_SIZ, _("Illegal move: %d.%s%s"),
11202                 (forwardMostMove / 2) + 1,
11203                 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
11204         DisplayError(move, 0);
11205         done = TRUE;
11206         break;
11207     }
11208
11209     if (done) {
11210         if (appData.matchMode || (appData.timeDelay == 0 && !pausing)) {
11211             DrawPosition(FALSE, boards[currentMove]);
11212             DisplayBothClocks();
11213             if (!appData.matchMode) // [HGM] PV info: routine tests if empty
11214               DisplayComment(currentMove - 1, commentList[currentMove]);
11215         }
11216         (void) StopLoadGameTimer();
11217         gameFileFP = NULL;
11218         cmailOldMove = forwardMostMove;
11219         return FALSE;
11220     } else {
11221         /* currentMoveString is set as a side-effect of yylex */
11222
11223         thinkOutput[0] = NULLCHAR;
11224         MakeMove(fromX, fromY, toX, toY, promoChar);
11225         currentMove = forwardMostMove;
11226         return TRUE;
11227     }
11228 }
11229
11230 /* Load the nth game from the given file */
11231 int
11232 LoadGameFromFile (char *filename, int n, char *title, int useList)
11233 {
11234     FILE *f;
11235     char buf[MSG_SIZ];
11236
11237     if (strcmp(filename, "-") == 0) {
11238         f = stdin;
11239         title = "stdin";
11240     } else {
11241         f = fopen(filename, "rb");
11242         if (f == NULL) {
11243           snprintf(buf, sizeof(buf),  _("Can't open \"%s\""), filename);
11244             DisplayError(buf, errno);
11245             return FALSE;
11246         }
11247     }
11248     if (fseek(f, 0, 0) == -1) {
11249         /* f is not seekable; probably a pipe */
11250         useList = FALSE;
11251     }
11252     if (useList && n == 0) {
11253         int error = GameListBuild(f);
11254         if (error) {
11255             DisplayError(_("Cannot build game list"), error);
11256         } else if (!ListEmpty(&gameList) &&
11257                    ((ListGame *) gameList.tailPred)->number > 1) {
11258             GameListPopUp(f, title);
11259             return TRUE;
11260         }
11261         GameListDestroy();
11262         n = 1;
11263     }
11264     if (n == 0) n = 1;
11265     return LoadGame(f, n, title, FALSE);
11266 }
11267
11268
11269 void
11270 MakeRegisteredMove ()
11271 {
11272     int fromX, fromY, toX, toY;
11273     char promoChar;
11274     if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
11275         switch (cmailMoveType[lastLoadGameNumber - 1]) {
11276           case CMAIL_MOVE:
11277           case CMAIL_DRAW:
11278             if (appData.debugMode)
11279               fprintf(debugFP, "Restoring %s for game %d\n",
11280                       cmailMove[lastLoadGameNumber - 1], lastLoadGameNumber);
11281
11282             thinkOutput[0] = NULLCHAR;
11283             safeStrCpy(moveList[currentMove], cmailMove[lastLoadGameNumber - 1], sizeof(moveList[currentMove])/sizeof(moveList[currentMove][0]));
11284             fromX = cmailMove[lastLoadGameNumber - 1][0] - AAA;
11285             fromY = cmailMove[lastLoadGameNumber - 1][1] - ONE;
11286             toX = cmailMove[lastLoadGameNumber - 1][2] - AAA;
11287             toY = cmailMove[lastLoadGameNumber - 1][3] - ONE;
11288             promoChar = cmailMove[lastLoadGameNumber - 1][4];
11289             MakeMove(fromX, fromY, toX, toY, promoChar);
11290             ShowMove(fromX, fromY, toX, toY);
11291
11292             switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
11293               case MT_NONE:
11294               case MT_CHECK:
11295                 break;
11296
11297               case MT_CHECKMATE:
11298               case MT_STAINMATE:
11299                 if (WhiteOnMove(currentMove)) {
11300                     GameEnds(BlackWins, "Black mates", GE_PLAYER);
11301                 } else {
11302                     GameEnds(WhiteWins, "White mates", GE_PLAYER);
11303                 }
11304                 break;
11305
11306               case MT_STALEMATE:
11307                 GameEnds(GameIsDrawn, "Stalemate", GE_PLAYER);
11308                 break;
11309             }
11310
11311             break;
11312
11313           case CMAIL_RESIGN:
11314             if (WhiteOnMove(currentMove)) {
11315                 GameEnds(BlackWins, "White resigns", GE_PLAYER);
11316             } else {
11317                 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
11318             }
11319             break;
11320
11321           case CMAIL_ACCEPT:
11322             GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
11323             break;
11324
11325           default:
11326             break;
11327         }
11328     }
11329
11330     return;
11331 }
11332
11333 /* Wrapper around LoadGame for use when a Cmail message is loaded */
11334 int
11335 CmailLoadGame (FILE *f, int gameNumber, char *title, int useList)
11336 {
11337     int retVal;
11338
11339     if (gameNumber > nCmailGames) {
11340         DisplayError(_("No more games in this message"), 0);
11341         return FALSE;
11342     }
11343     if (f == lastLoadGameFP) {
11344         int offset = gameNumber - lastLoadGameNumber;
11345         if (offset == 0) {
11346             cmailMsg[0] = NULLCHAR;
11347             if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
11348                 cmailMoveRegistered[lastLoadGameNumber - 1] = FALSE;
11349                 nCmailMovesRegistered--;
11350             }
11351             cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
11352             if (cmailResult[lastLoadGameNumber - 1] == CMAIL_NEW_RESULT) {
11353                 cmailResult[lastLoadGameNumber - 1] = CMAIL_NOT_RESULT;
11354             }
11355         } else {
11356             if (! RegisterMove()) return FALSE;
11357         }
11358     }
11359
11360     retVal = LoadGame(f, gameNumber, title, useList);
11361
11362     /* Make move registered during previous look at this game, if any */
11363     MakeRegisteredMove();
11364
11365     if (cmailCommentList[lastLoadGameNumber - 1] != NULL) {
11366         commentList[currentMove]
11367           = StrSave(cmailCommentList[lastLoadGameNumber - 1]);
11368         DisplayComment(currentMove - 1, commentList[currentMove]);
11369     }
11370
11371     return retVal;
11372 }
11373
11374 /* Support for LoadNextGame, LoadPreviousGame, ReloadSameGame */
11375 int
11376 ReloadGame (int offset)
11377 {
11378     int gameNumber = lastLoadGameNumber + offset;
11379     if (lastLoadGameFP == NULL) {
11380         DisplayError(_("No game has been loaded yet"), 0);
11381         return FALSE;
11382     }
11383     if (gameNumber <= 0) {
11384         DisplayError(_("Can't back up any further"), 0);
11385         return FALSE;
11386     }
11387     if (cmailMsgLoaded) {
11388         return CmailLoadGame(lastLoadGameFP, gameNumber,
11389                              lastLoadGameTitle, lastLoadGameUseList);
11390     } else {
11391         return LoadGame(lastLoadGameFP, gameNumber,
11392                         lastLoadGameTitle, lastLoadGameUseList);
11393     }
11394 }
11395
11396 int keys[EmptySquare+1];
11397
11398 int
11399 PositionMatches (Board b1, Board b2)
11400 {
11401     int r, f, sum=0;
11402     switch(appData.searchMode) {
11403         case 1: return CompareWithRights(b1, b2);
11404         case 2:
11405             for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11406                 if(b2[r][f] != EmptySquare && b1[r][f] != b2[r][f]) return FALSE;
11407             }
11408             return TRUE;
11409         case 3:
11410             for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11411               if((b2[r][f] == WhitePawn || b2[r][f] == BlackPawn) && b1[r][f] != b2[r][f]) return FALSE;
11412                 sum += keys[b1[r][f]] - keys[b2[r][f]];
11413             }
11414             return sum==0;
11415         case 4:
11416             for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11417                 sum += keys[b1[r][f]] - keys[b2[r][f]];
11418             }
11419             return sum==0;
11420     }
11421     return TRUE;
11422 }
11423
11424 #define Q_PROMO  4
11425 #define Q_EP     3
11426 #define Q_BCASTL 2
11427 #define Q_WCASTL 1
11428
11429 int pieceList[256], quickBoard[256];
11430 ChessSquare pieceType[256] = { EmptySquare };
11431 Board soughtBoard, reverseBoard, flipBoard, rotateBoard;
11432 int counts[EmptySquare], minSought[EmptySquare], minReverse[EmptySquare], maxSought[EmptySquare], maxReverse[EmptySquare];
11433 int soughtTotal, turn;
11434 Boolean epOK, flipSearch;
11435
11436 typedef struct {
11437     unsigned char piece, to;
11438 } Move;
11439
11440 #define DSIZE (250000)
11441
11442 Move initialSpace[DSIZE+1000]; // gamble on that game will not be more than 500 moves
11443 Move *moveDatabase = initialSpace;
11444 unsigned int movePtr, dataSize = DSIZE;
11445
11446 int
11447 MakePieceList (Board board, int *counts)
11448 {
11449     int r, f, n=Q_PROMO, total=0;
11450     for(r=0;r<EmptySquare;r++) counts[r] = 0; // piece-type counts
11451     for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11452         int sq = f + (r<<4);
11453         if(board[r][f] == EmptySquare) quickBoard[sq] = 0; else {
11454             quickBoard[sq] = ++n;
11455             pieceList[n] = sq;
11456             pieceType[n] = board[r][f];
11457             counts[board[r][f]]++;
11458             if(board[r][f] == WhiteKing) pieceList[1] = n; else
11459             if(board[r][f] == BlackKing) pieceList[2] = n; // remember which are Kings, for castling
11460             total++;
11461         }
11462     }
11463     epOK = gameInfo.variant != VariantXiangqi && gameInfo.variant != VariantBerolina;
11464     return total;
11465 }
11466
11467 void
11468 PackMove (int fromX, int fromY, int toX, int toY, ChessSquare promoPiece)
11469 {
11470     int sq = fromX + (fromY<<4);
11471     int piece = quickBoard[sq];
11472     quickBoard[sq] = 0;
11473     moveDatabase[movePtr].to = pieceList[piece] = sq = toX + (toY<<4);
11474     if(piece == pieceList[1] && fromY == toY && (toX > fromX+1 || toX < fromX-1) && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1) {
11475         int from = toX>fromX ? BOARD_RGHT-1 : BOARD_LEFT;
11476         moveDatabase[movePtr++].piece = Q_WCASTL;
11477         quickBoard[sq] = piece;
11478         piece = quickBoard[from]; quickBoard[from] = 0;
11479         moveDatabase[movePtr].to = pieceList[piece] = sq = toX>fromX ? sq-1 : sq+1;
11480     } else
11481     if(piece == pieceList[2] && fromY == toY && (toX > fromX+1 || toX < fromX-1) && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1) {
11482         int from = (toX>fromX ? BOARD_RGHT-1 : BOARD_LEFT) + (BOARD_HEIGHT-1 <<4);
11483         moveDatabase[movePtr++].piece = Q_BCASTL;
11484         quickBoard[sq] = piece;
11485         piece = quickBoard[from]; quickBoard[from] = 0;
11486         moveDatabase[movePtr].to = pieceList[piece] = sq = toX>fromX ? sq-1 : sq+1;
11487     } else
11488     if(epOK && (pieceType[piece] == WhitePawn || pieceType[piece] == BlackPawn) && fromX != toX && quickBoard[sq] == 0) {
11489         quickBoard[(fromY<<4)+toX] = 0;
11490         moveDatabase[movePtr].piece = Q_EP;
11491         moveDatabase[movePtr++].to = (fromY<<4)+toX;
11492         moveDatabase[movePtr].to = sq;
11493     } else
11494     if(promoPiece != pieceType[piece]) {
11495         moveDatabase[movePtr++].piece = Q_PROMO;
11496         moveDatabase[movePtr].to = pieceType[piece] = (int) promoPiece;
11497     }
11498     moveDatabase[movePtr].piece = piece;
11499     quickBoard[sq] = piece;
11500     movePtr++;
11501 }
11502
11503 int
11504 PackGame (Board board)
11505 {
11506     Move *newSpace = NULL;
11507     moveDatabase[movePtr].piece = 0; // terminate previous game
11508     if(movePtr > dataSize) {
11509         if(appData.debugMode) fprintf(debugFP, "move-cache overflow, enlarge to %d MB\n", dataSize/128);
11510         dataSize *= 8; // increase size by factor 8 (512KB -> 4MB -> 32MB -> 256MB -> 2GB)
11511         if(dataSize) newSpace = (Move*) calloc(dataSize + 1000, sizeof(Move));
11512         if(newSpace) {
11513             int i;
11514             Move *p = moveDatabase, *q = newSpace;
11515             for(i=0; i<movePtr; i++) *q++ = *p++;    // copy to newly allocated space
11516             if(dataSize > 8*DSIZE) free(moveDatabase); // and free old space (if it was allocated)
11517             moveDatabase = newSpace;
11518         } else { // calloc failed, we must be out of memory. Too bad...
11519             dataSize = 0; // prevent calloc events for all subsequent games
11520             return 0;     // and signal this one isn't cached
11521         }
11522     }
11523     movePtr++;
11524     MakePieceList(board, counts);
11525     return movePtr;
11526 }
11527
11528 int
11529 QuickCompare (Board board, int *minCounts, int *maxCounts)
11530 {   // compare according to search mode
11531     int r, f;
11532     switch(appData.searchMode)
11533     {
11534       case 1: // exact position match
11535         if(!(turn & board[EP_STATUS-1])) return FALSE; // wrong side to move
11536         for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11537             if(board[r][f] != pieceType[quickBoard[(r<<4)+f]]) return FALSE;
11538         }
11539         break;
11540       case 2: // can have extra material on empty squares
11541         for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11542             if(board[r][f] == EmptySquare) continue;
11543             if(board[r][f] != pieceType[quickBoard[(r<<4)+f]]) return FALSE;
11544         }
11545         break;
11546       case 3: // material with exact Pawn structure
11547         for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11548             if(board[r][f] != WhitePawn && board[r][f] != BlackPawn) continue;
11549             if(board[r][f] != pieceType[quickBoard[(r<<4)+f]]) return FALSE;
11550         } // fall through to material comparison
11551       case 4: // exact material
11552         for(r=0; r<EmptySquare; r++) if(counts[r] != maxCounts[r]) return FALSE;
11553         break;
11554       case 6: // material range with given imbalance
11555         for(r=0; r<BlackPawn; r++) if(counts[r] - minCounts[r] != counts[r+BlackPawn] - minCounts[r+BlackPawn]) return FALSE;
11556         // fall through to range comparison
11557       case 5: // material range
11558         for(r=0; r<EmptySquare; r++) if(counts[r] < minCounts[r] || counts[r] > maxCounts[r]) return FALSE;
11559     }
11560     return TRUE;
11561 }
11562
11563 int
11564 QuickScan (Board board, Move *move)
11565 {   // reconstruct game,and compare all positions in it
11566     int cnt=0, stretch=0, total = MakePieceList(board, counts), delayedKing = -1;
11567     do {
11568         int piece = move->piece;
11569         int to = move->to, from = pieceList[piece];
11570         if(piece <= Q_PROMO) { // special moves encoded by otherwise invalid piece numbers 1-4
11571           if(!piece) return -1;
11572           if(piece == Q_PROMO) { // promotion, encoded as (Q_PROMO, to) + (piece, promoType)
11573             piece = (++move)->piece;
11574             from = pieceList[piece];
11575             counts[pieceType[piece]]--;
11576             pieceType[piece] = (ChessSquare) move->to;
11577             counts[move->to]++;
11578           } else if(piece == Q_EP) { // e.p. capture, encoded as (Q_EP, ep-sqr) + (piece, to)
11579             counts[pieceType[quickBoard[to]]]--;
11580             quickBoard[to] = 0; total--;
11581             move++;
11582             continue;
11583           } else if(piece <= Q_BCASTL) { // castling, encoded as (Q_XCASTL, king-to) + (rook, rook-to)
11584             piece = pieceList[piece]; // first two elements of pieceList contain King numbers
11585             from  = pieceList[piece]; // so this must be King
11586             quickBoard[from] = 0;
11587             pieceList[piece] = to;
11588             from = pieceList[(++move)->piece]; // for FRC this has to be done here
11589             quickBoard[from] = 0; // rook
11590             quickBoard[to] = piece;
11591             to = move->to; piece = move->piece;
11592             goto aftercastle;
11593           }
11594         }
11595         if(appData.searchMode > 2) counts[pieceType[quickBoard[to]]]--; // account capture
11596         if((total -= (quickBoard[to] != 0)) < soughtTotal) return -1; // piece count dropped below what we search for
11597         quickBoard[from] = 0;
11598       aftercastle:
11599         quickBoard[to] = piece;
11600         pieceList[piece] = to;
11601         cnt++; turn ^= 3;
11602         if(QuickCompare(soughtBoard, minSought, maxSought) ||
11603            appData.ignoreColors && QuickCompare(reverseBoard, minReverse, maxReverse) ||
11604            flipSearch && (QuickCompare(flipBoard, minSought, maxSought) ||
11605                                 appData.ignoreColors && QuickCompare(rotateBoard, minReverse, maxReverse))
11606           ) {
11607             static int lastCounts[EmptySquare+1];
11608             int i;
11609             if(stretch) for(i=0; i<EmptySquare; i++) if(lastCounts[i] != counts[i]) { stretch = 0; break; } // reset if material changes
11610             if(stretch++ == 0) for(i=0; i<EmptySquare; i++) lastCounts[i] = counts[i]; // remember actual material
11611         } else stretch = 0;
11612         if(stretch && (appData.searchMode == 1 || stretch >= appData.stretch)) return cnt + 1 - stretch;
11613         move++; delayedKing = -1;
11614     } while(1);
11615 }
11616
11617 void
11618 InitSearch ()
11619 {
11620     int r, f;
11621     flipSearch = FALSE;
11622     CopyBoard(soughtBoard, boards[currentMove]);
11623     soughtTotal = MakePieceList(soughtBoard, maxSought);
11624     soughtBoard[EP_STATUS-1] = (currentMove & 1) + 1;
11625     if(currentMove == 0 && gameMode == EditPosition) soughtBoard[EP_STATUS-1] = blackPlaysFirst + 1; // (!)
11626     CopyBoard(reverseBoard, boards[currentMove]);
11627     for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11628         int piece = boards[currentMove][BOARD_HEIGHT-1-r][f];
11629         if(piece < BlackPawn) piece += BlackPawn; else if(piece < EmptySquare) piece -= BlackPawn; // color-flip
11630         reverseBoard[r][f] = piece;
11631     }
11632     reverseBoard[EP_STATUS-1] = soughtBoard[EP_STATUS-1] ^ 3; 
11633     for(r=0; r<6; r++) reverseBoard[CASTLING][r] = boards[currentMove][CASTLING][(r+3)%6];
11634     if(appData.findMirror && appData.searchMode <= 3 && (!nrCastlingRights
11635                  || (boards[currentMove][CASTLING][2] == NoRights || 
11636                      boards[currentMove][CASTLING][0] == NoRights && boards[currentMove][CASTLING][1] == NoRights )
11637                  && (boards[currentMove][CASTLING][5] == NoRights || 
11638                      boards[currentMove][CASTLING][3] == NoRights && boards[currentMove][CASTLING][4] == NoRights ) )
11639       ) {
11640         flipSearch = TRUE;
11641         CopyBoard(flipBoard, soughtBoard);
11642         CopyBoard(rotateBoard, reverseBoard);
11643         for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11644             flipBoard[r][f]    = soughtBoard[r][BOARD_WIDTH-1-f];
11645             rotateBoard[r][f] = reverseBoard[r][BOARD_WIDTH-1-f];
11646         }
11647     }
11648     for(r=0; r<BlackPawn; r++) maxReverse[r] = maxSought[r+BlackPawn], maxReverse[r+BlackPawn] = maxSought[r];
11649     if(appData.searchMode >= 5) {
11650         for(r=BOARD_HEIGHT/2; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) soughtBoard[r][f] = EmptySquare;
11651         MakePieceList(soughtBoard, minSought);
11652         for(r=0; r<BlackPawn; r++) minReverse[r] = minSought[r+BlackPawn], minReverse[r+BlackPawn] = minSought[r];
11653     }
11654     if(gameInfo.variant == VariantCrazyhouse || gameInfo.variant == VariantShogi || gameInfo.variant == VariantBughouse)
11655         soughtTotal = 0; // in drop games nr of pieces does not fall monotonously
11656 }
11657
11658 GameInfo dummyInfo;
11659
11660 int
11661 GameContainsPosition (FILE *f, ListGame *lg)
11662 {
11663     int next, btm=0, plyNr=0, scratch=forwardMostMove+2&~1;
11664     int fromX, fromY, toX, toY;
11665     char promoChar;
11666     static int initDone=FALSE;
11667
11668     // weed out games based on numerical tag comparison
11669     if(lg->gameInfo.variant != gameInfo.variant) return -1; // wrong variant
11670     if(appData.eloThreshold1 && (lg->gameInfo.whiteRating < appData.eloThreshold1 && lg->gameInfo.blackRating < appData.eloThreshold1)) return -1;
11671     if(appData.eloThreshold2 && (lg->gameInfo.whiteRating < appData.eloThreshold2 || lg->gameInfo.blackRating < appData.eloThreshold2)) return -1;
11672     if(appData.dateThreshold && (!lg->gameInfo.date || atoi(lg->gameInfo.date) < appData.dateThreshold)) return -1;
11673     if(!initDone) {
11674         for(next = WhitePawn; next<EmptySquare; next++) keys[next] = random()>>8 ^ random()<<6 ^random()<<20;
11675         initDone = TRUE;
11676     }
11677     if(lg->gameInfo.fen) ParseFEN(boards[scratch], &btm, lg->gameInfo.fen);
11678     else CopyBoard(boards[scratch], initialPosition); // default start position
11679     if(lg->moves) {
11680         turn = btm + 1;
11681         if((next = QuickScan( boards[scratch], &moveDatabase[lg->moves] )) < 0) return -1; // quick scan rules out it is there
11682         if(appData.searchMode >= 4) return next; // for material searches, trust QuickScan.
11683     }
11684     if(btm) plyNr++;
11685     if(PositionMatches(boards[scratch], boards[currentMove])) return plyNr;
11686     fseek(f, lg->offset, 0);
11687     yynewfile(f);
11688     while(1) {
11689         yyboardindex = scratch;
11690         quickFlag = plyNr+1;
11691         next = Myylex();
11692         quickFlag = 0;
11693         switch(next) {
11694             case PGNTag:
11695                 if(plyNr) return -1; // after we have seen moves, any tags will be start of next game
11696             default:
11697                 continue;
11698
11699             case XBoardGame:
11700             case GNUChessGame:
11701                 if(plyNr) return -1; // after we have seen moves, this is for new game
11702               continue;
11703
11704             case AmbiguousMove: // we cannot reconstruct the game beyond these two
11705             case ImpossibleMove:
11706             case WhiteWins: // game ends here with these four
11707             case BlackWins:
11708             case GameIsDrawn:
11709             case GameUnfinished:
11710                 return -1;
11711
11712             case IllegalMove:
11713                 if(appData.testLegality) return -1;
11714             case WhiteCapturesEnPassant:
11715             case BlackCapturesEnPassant:
11716             case WhitePromotion:
11717             case BlackPromotion:
11718             case WhiteNonPromotion:
11719             case BlackNonPromotion:
11720             case NormalMove:
11721             case WhiteKingSideCastle:
11722             case WhiteQueenSideCastle:
11723             case BlackKingSideCastle:
11724             case BlackQueenSideCastle:
11725             case WhiteKingSideCastleWild:
11726             case WhiteQueenSideCastleWild:
11727             case BlackKingSideCastleWild:
11728             case BlackQueenSideCastleWild:
11729             case WhiteHSideCastleFR:
11730             case WhiteASideCastleFR:
11731             case BlackHSideCastleFR:
11732             case BlackASideCastleFR:
11733                 fromX = currentMoveString[0] - AAA;
11734                 fromY = currentMoveString[1] - ONE;
11735                 toX = currentMoveString[2] - AAA;
11736                 toY = currentMoveString[3] - ONE;
11737                 promoChar = currentMoveString[4];
11738                 break;
11739             case WhiteDrop:
11740             case BlackDrop:
11741                 fromX = next == WhiteDrop ?
11742                   (int) CharToPiece(ToUpper(currentMoveString[0])) :
11743                   (int) CharToPiece(ToLower(currentMoveString[0]));
11744                 fromY = DROP_RANK;
11745                 toX = currentMoveString[2] - AAA;
11746                 toY = currentMoveString[3] - ONE;
11747                 promoChar = 0;
11748                 break;
11749         }
11750         // Move encountered; peform it. We need to shuttle between two boards, as even/odd index determines side to move
11751         plyNr++;
11752         ApplyMove(fromX, fromY, toX, toY, promoChar, boards[scratch]);
11753         if(PositionMatches(boards[scratch], boards[currentMove])) return plyNr;
11754         if(appData.ignoreColors && PositionMatches(boards[scratch], reverseBoard)) return plyNr;
11755         if(appData.findMirror) {
11756             if(PositionMatches(boards[scratch], flipBoard)) return plyNr;
11757             if(appData.ignoreColors && PositionMatches(boards[scratch], rotateBoard)) return plyNr;
11758         }
11759     }
11760 }
11761
11762 /* Load the nth game from open file f */
11763 int
11764 LoadGame (FILE *f, int gameNumber, char *title, int useList)
11765 {
11766     ChessMove cm;
11767     char buf[MSG_SIZ];
11768     int gn = gameNumber;
11769     ListGame *lg = NULL;
11770     int numPGNTags = 0;
11771     int err, pos = -1;
11772     GameMode oldGameMode;
11773     VariantClass oldVariant = gameInfo.variant; /* [HGM] PGNvariant */
11774
11775     if (appData.debugMode)
11776         fprintf(debugFP, "LoadGame(): on entry, gameMode %d\n", gameMode);
11777
11778     if (gameMode == Training )
11779         SetTrainingModeOff();
11780
11781     oldGameMode = gameMode;
11782     if (gameMode != BeginningOfGame) {
11783       Reset(FALSE, TRUE);
11784     }
11785
11786     gameFileFP = f;
11787     if (lastLoadGameFP != NULL && lastLoadGameFP != f) {
11788         fclose(lastLoadGameFP);
11789     }
11790
11791     if (useList) {
11792         lg = (ListGame *) ListElem(&gameList, gameNumber-1);
11793
11794         if (lg) {
11795             fseek(f, lg->offset, 0);
11796             GameListHighlight(gameNumber);
11797             pos = lg->position;
11798             gn = 1;
11799         }
11800         else {
11801             DisplayError(_("Game number out of range"), 0);
11802             return FALSE;
11803         }
11804     } else {
11805         GameListDestroy();
11806         if (fseek(f, 0, 0) == -1) {
11807             if (f == lastLoadGameFP ?
11808                 gameNumber == lastLoadGameNumber + 1 :
11809                 gameNumber == 1) {
11810                 gn = 1;
11811             } else {
11812                 DisplayError(_("Can't seek on game file"), 0);
11813                 return FALSE;
11814             }
11815         }
11816     }
11817     lastLoadGameFP = f;
11818     lastLoadGameNumber = gameNumber;
11819     safeStrCpy(lastLoadGameTitle, title, sizeof(lastLoadGameTitle)/sizeof(lastLoadGameTitle[0]));
11820     lastLoadGameUseList = useList;
11821
11822     yynewfile(f);
11823
11824     if (lg && lg->gameInfo.white && lg->gameInfo.black) {
11825       snprintf(buf, sizeof(buf), "%s %s %s", lg->gameInfo.white, _("vs."),
11826                 lg->gameInfo.black);
11827             DisplayTitle(buf);
11828     } else if (*title != NULLCHAR) {
11829         if (gameNumber > 1) {
11830           snprintf(buf, MSG_SIZ, "%s %d", title, gameNumber);
11831             DisplayTitle(buf);
11832         } else {
11833             DisplayTitle(title);
11834         }
11835     }
11836
11837     if (gameMode != AnalyzeFile && gameMode != AnalyzeMode) {
11838         gameMode = PlayFromGameFile;
11839         ModeHighlight();
11840     }
11841
11842     currentMove = forwardMostMove = backwardMostMove = 0;
11843     CopyBoard(boards[0], initialPosition);
11844     StopClocks();
11845
11846     /*
11847      * Skip the first gn-1 games in the file.
11848      * Also skip over anything that precedes an identifiable
11849      * start of game marker, to avoid being confused by
11850      * garbage at the start of the file.  Currently
11851      * recognized start of game markers are the move number "1",
11852      * the pattern "gnuchess .* game", the pattern
11853      * "^[#;%] [^ ]* game file", and a PGN tag block.
11854      * A game that starts with one of the latter two patterns
11855      * will also have a move number 1, possibly
11856      * following a position diagram.
11857      * 5-4-02: Let's try being more lenient and allowing a game to
11858      * start with an unnumbered move.  Does that break anything?
11859      */
11860     cm = lastLoadGameStart = EndOfFile;
11861     while (gn > 0) {
11862         yyboardindex = forwardMostMove;
11863         cm = (ChessMove) Myylex();
11864         switch (cm) {
11865           case EndOfFile:
11866             if (cmailMsgLoaded) {
11867                 nCmailGames = CMAIL_MAX_GAMES - gn;
11868             } else {
11869                 Reset(TRUE, TRUE);
11870                 DisplayError(_("Game not found in file"), 0);
11871             }
11872             return FALSE;
11873
11874           case GNUChessGame:
11875           case XBoardGame:
11876             gn--;
11877             lastLoadGameStart = cm;
11878             break;
11879
11880           case MoveNumberOne:
11881             switch (lastLoadGameStart) {
11882               case GNUChessGame:
11883               case XBoardGame:
11884               case PGNTag:
11885                 break;
11886               case MoveNumberOne:
11887               case EndOfFile:
11888                 gn--;           /* count this game */
11889                 lastLoadGameStart = cm;
11890                 break;
11891               default:
11892                 /* impossible */
11893                 break;
11894             }
11895             break;
11896
11897           case PGNTag:
11898             switch (lastLoadGameStart) {
11899               case GNUChessGame:
11900               case PGNTag:
11901               case MoveNumberOne:
11902               case EndOfFile:
11903                 gn--;           /* count this game */
11904                 lastLoadGameStart = cm;
11905                 break;
11906               case XBoardGame:
11907                 lastLoadGameStart = cm; /* game counted already */
11908                 break;
11909               default:
11910                 /* impossible */
11911                 break;
11912             }
11913             if (gn > 0) {
11914                 do {
11915                     yyboardindex = forwardMostMove;
11916                     cm = (ChessMove) Myylex();
11917                 } while (cm == PGNTag || cm == Comment);
11918             }
11919             break;
11920
11921           case WhiteWins:
11922           case BlackWins:
11923           case GameIsDrawn:
11924             if (cmailMsgLoaded && (CMAIL_MAX_GAMES == lastLoadGameNumber)) {
11925                 if (   cmailResult[CMAIL_MAX_GAMES - gn - 1]
11926                     != CMAIL_OLD_RESULT) {
11927                     nCmailResults ++ ;
11928                     cmailResult[  CMAIL_MAX_GAMES
11929                                 - gn - 1] = CMAIL_OLD_RESULT;
11930                 }
11931             }
11932             break;
11933
11934           case NormalMove:
11935             /* Only a NormalMove can be at the start of a game
11936              * without a position diagram. */
11937             if (lastLoadGameStart == EndOfFile ) {
11938               gn--;
11939               lastLoadGameStart = MoveNumberOne;
11940             }
11941             break;
11942
11943           default:
11944             break;
11945         }
11946     }
11947
11948     if (appData.debugMode)
11949       fprintf(debugFP, "Parsed game start '%s' (%d)\n", yy_text, (int) cm);
11950
11951     if (cm == XBoardGame) {
11952         /* Skip any header junk before position diagram and/or move 1 */
11953         for (;;) {
11954             yyboardindex = forwardMostMove;
11955             cm = (ChessMove) Myylex();
11956
11957             if (cm == EndOfFile ||
11958                 cm == GNUChessGame || cm == XBoardGame) {
11959                 /* Empty game; pretend end-of-file and handle later */
11960                 cm = EndOfFile;
11961                 break;
11962             }
11963
11964             if (cm == MoveNumberOne || cm == PositionDiagram ||
11965                 cm == PGNTag || cm == Comment)
11966               break;
11967         }
11968     } else if (cm == GNUChessGame) {
11969         if (gameInfo.event != NULL) {
11970             free(gameInfo.event);
11971         }
11972         gameInfo.event = StrSave(yy_text);
11973     }
11974
11975     startedFromSetupPosition = FALSE;
11976     while (cm == PGNTag) {
11977         if (appData.debugMode)
11978           fprintf(debugFP, "Parsed PGNTag: %s\n", yy_text);
11979         err = ParsePGNTag(yy_text, &gameInfo);
11980         if (!err) numPGNTags++;
11981
11982         /* [HGM] PGNvariant: automatically switch to variant given in PGN tag */
11983         if(gameInfo.variant != oldVariant) {
11984             startedFromPositionFile = FALSE; /* [HGM] loadPos: variant switch likely makes position invalid */
11985             ResetFrontEnd(); // [HGM] might need other bitmaps. Cannot use Reset() because it clears gameInfo :-(
11986             InitPosition(TRUE);
11987             oldVariant = gameInfo.variant;
11988             if (appData.debugMode)
11989               fprintf(debugFP, "New variant %d\n", (int) oldVariant);
11990         }
11991
11992
11993         if (gameInfo.fen != NULL) {
11994           Board initial_position;
11995           startedFromSetupPosition = TRUE;
11996           if (!ParseFEN(initial_position, &blackPlaysFirst, gameInfo.fen)) {
11997             Reset(TRUE, TRUE);
11998             DisplayError(_("Bad FEN position in file"), 0);
11999             return FALSE;
12000           }
12001           CopyBoard(boards[0], initial_position);
12002           if (blackPlaysFirst) {
12003             currentMove = forwardMostMove = backwardMostMove = 1;
12004             CopyBoard(boards[1], initial_position);
12005             safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
12006             safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
12007             timeRemaining[0][1] = whiteTimeRemaining;
12008             timeRemaining[1][1] = blackTimeRemaining;
12009             if (commentList[0] != NULL) {
12010               commentList[1] = commentList[0];
12011               commentList[0] = NULL;
12012             }
12013           } else {
12014             currentMove = forwardMostMove = backwardMostMove = 0;
12015           }
12016           /* [HGM] copy FEN attributes as well. Bugfix 4.3.14m and 4.3.15e: moved to after 'blackPlaysFirst' */
12017           {   int i;
12018               initialRulePlies = FENrulePlies;
12019               for( i=0; i< nrCastlingRights; i++ )
12020                   initialRights[i] = initial_position[CASTLING][i];
12021           }
12022           yyboardindex = forwardMostMove;
12023           free(gameInfo.fen);
12024           gameInfo.fen = NULL;
12025         }
12026
12027         yyboardindex = forwardMostMove;
12028         cm = (ChessMove) Myylex();
12029
12030         /* Handle comments interspersed among the tags */
12031         while (cm == Comment) {
12032             char *p;
12033             if (appData.debugMode)
12034               fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
12035             p = yy_text;
12036             AppendComment(currentMove, p, FALSE);
12037             yyboardindex = forwardMostMove;
12038             cm = (ChessMove) Myylex();
12039         }
12040     }
12041
12042     /* don't rely on existence of Event tag since if game was
12043      * pasted from clipboard the Event tag may not exist
12044      */
12045     if (numPGNTags > 0){
12046         char *tags;
12047         if (gameInfo.variant == VariantNormal) {
12048           VariantClass v = StringToVariant(gameInfo.event);
12049           // [HGM] do not recognize variants from event tag that were introduced after supporting variant tag
12050           if(v < VariantShogi) gameInfo.variant = v;
12051         }
12052         if (!matchMode) {
12053           if( appData.autoDisplayTags ) {
12054             tags = PGNTags(&gameInfo);
12055             TagsPopUp(tags, CmailMsg());
12056             free(tags);
12057           }
12058         }
12059     } else {
12060         /* Make something up, but don't display it now */
12061         SetGameInfo();
12062         TagsPopDown();
12063     }
12064
12065     if (cm == PositionDiagram) {
12066         int i, j;
12067         char *p;
12068         Board initial_position;
12069
12070         if (appData.debugMode)
12071           fprintf(debugFP, "Parsed PositionDiagram: %s\n", yy_text);
12072
12073         if (!startedFromSetupPosition) {
12074             p = yy_text;
12075             for (i = BOARD_HEIGHT - 1; i >= 0; i--)
12076               for (j = BOARD_LEFT; j < BOARD_RGHT; p++)
12077                 switch (*p) {
12078                   case '{':
12079                   case '[':
12080                   case '-':
12081                   case ' ':
12082                   case '\t':
12083                   case '\n':
12084                   case '\r':
12085                     break;
12086                   default:
12087                     initial_position[i][j++] = CharToPiece(*p);
12088                     break;
12089                 }
12090             while (*p == ' ' || *p == '\t' ||
12091                    *p == '\n' || *p == '\r') p++;
12092
12093             if (strncmp(p, "black", strlen("black"))==0)
12094               blackPlaysFirst = TRUE;
12095             else
12096               blackPlaysFirst = FALSE;
12097             startedFromSetupPosition = TRUE;
12098
12099             CopyBoard(boards[0], initial_position);
12100             if (blackPlaysFirst) {
12101                 currentMove = forwardMostMove = backwardMostMove = 1;
12102                 CopyBoard(boards[1], initial_position);
12103                 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
12104                 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
12105                 timeRemaining[0][1] = whiteTimeRemaining;
12106                 timeRemaining[1][1] = blackTimeRemaining;
12107                 if (commentList[0] != NULL) {
12108                     commentList[1] = commentList[0];
12109                     commentList[0] = NULL;
12110                 }
12111             } else {
12112                 currentMove = forwardMostMove = backwardMostMove = 0;
12113             }
12114         }
12115         yyboardindex = forwardMostMove;
12116         cm = (ChessMove) Myylex();
12117     }
12118
12119     if (first.pr == NoProc) {
12120         StartChessProgram(&first);
12121     }
12122     InitChessProgram(&first, FALSE);
12123     SendToProgram("force\n", &first);
12124     if (startedFromSetupPosition) {
12125         SendBoard(&first, forwardMostMove);
12126     if (appData.debugMode) {
12127         fprintf(debugFP, "Load Game\n");
12128     }
12129         DisplayBothClocks();
12130     }
12131
12132     /* [HGM] server: flag to write setup moves in broadcast file as one */
12133     loadFlag = appData.suppressLoadMoves;
12134
12135     while (cm == Comment) {
12136         char *p;
12137         if (appData.debugMode)
12138           fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
12139         p = yy_text;
12140         AppendComment(currentMove, p, FALSE);
12141         yyboardindex = forwardMostMove;
12142         cm = (ChessMove) Myylex();
12143     }
12144
12145     if ((cm == EndOfFile && lastLoadGameStart != EndOfFile ) ||
12146         cm == WhiteWins || cm == BlackWins ||
12147         cm == GameIsDrawn || cm == GameUnfinished) {
12148         DisplayMessage("", _("No moves in game"));
12149         if (cmailMsgLoaded) {
12150             if (appData.debugMode)
12151               fprintf(debugFP, "Setting flipView to %d.\n", FALSE);
12152             ClearHighlights();
12153             flipView = FALSE;
12154         }
12155         DrawPosition(FALSE, boards[currentMove]);
12156         DisplayBothClocks();
12157         gameMode = EditGame;
12158         ModeHighlight();
12159         gameFileFP = NULL;
12160         cmailOldMove = 0;
12161         return TRUE;
12162     }
12163
12164     // [HGM] PV info: routine tests if comment empty
12165     if (!matchMode && (pausing || appData.timeDelay != 0)) {
12166         DisplayComment(currentMove - 1, commentList[currentMove]);
12167     }
12168     if (!matchMode && appData.timeDelay != 0)
12169       DrawPosition(FALSE, boards[currentMove]);
12170
12171     if (gameMode == AnalyzeFile || gameMode == AnalyzeMode) {
12172       programStats.ok_to_send = 1;
12173     }
12174
12175     /* if the first token after the PGN tags is a move
12176      * and not move number 1, retrieve it from the parser
12177      */
12178     if (cm != MoveNumberOne)
12179         LoadGameOneMove(cm);
12180
12181     /* load the remaining moves from the file */
12182     while (LoadGameOneMove(EndOfFile)) {
12183       timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
12184       timeRemaining[1][forwardMostMove] = blackTimeRemaining;
12185     }
12186
12187     /* rewind to the start of the game */
12188     currentMove = backwardMostMove;
12189
12190     HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
12191
12192     if (oldGameMode == AnalyzeFile ||
12193         oldGameMode == AnalyzeMode) {
12194       AnalyzeFileEvent();
12195     }
12196
12197     if (!matchMode && pos > 0) {
12198         ToNrEvent(pos); // [HGM] no autoplay if selected on position
12199     } else
12200     if (matchMode || appData.timeDelay == 0) {
12201       ToEndEvent();
12202     } else if (appData.timeDelay > 0) {
12203       AutoPlayGameLoop();
12204     }
12205
12206     if (appData.debugMode)
12207         fprintf(debugFP, "LoadGame(): on exit, gameMode %d\n", gameMode);
12208
12209     loadFlag = 0; /* [HGM] true game starts */
12210     return TRUE;
12211 }
12212
12213 /* Support for LoadNextPosition, LoadPreviousPosition, ReloadSamePosition */
12214 int
12215 ReloadPosition (int offset)
12216 {
12217     int positionNumber = lastLoadPositionNumber + offset;
12218     if (lastLoadPositionFP == NULL) {
12219         DisplayError(_("No position has been loaded yet"), 0);
12220         return FALSE;
12221     }
12222     if (positionNumber <= 0) {
12223         DisplayError(_("Can't back up any further"), 0);
12224         return FALSE;
12225     }
12226     return LoadPosition(lastLoadPositionFP, positionNumber,
12227                         lastLoadPositionTitle);
12228 }
12229
12230 /* Load the nth position from the given file */
12231 int
12232 LoadPositionFromFile (char *filename, int n, char *title)
12233 {
12234     FILE *f;
12235     char buf[MSG_SIZ];
12236
12237     if (strcmp(filename, "-") == 0) {
12238         return LoadPosition(stdin, n, "stdin");
12239     } else {
12240         f = fopen(filename, "rb");
12241         if (f == NULL) {
12242             snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
12243             DisplayError(buf, errno);
12244             return FALSE;
12245         } else {
12246             return LoadPosition(f, n, title);
12247         }
12248     }
12249 }
12250
12251 /* Load the nth position from the given open file, and close it */
12252 int
12253 LoadPosition (FILE *f, int positionNumber, char *title)
12254 {
12255     char *p, line[MSG_SIZ];
12256     Board initial_position;
12257     int i, j, fenMode, pn;
12258
12259     if (gameMode == Training )
12260         SetTrainingModeOff();
12261
12262     if (gameMode != BeginningOfGame) {
12263         Reset(FALSE, TRUE);
12264     }
12265     if (lastLoadPositionFP != NULL && lastLoadPositionFP != f) {
12266         fclose(lastLoadPositionFP);
12267     }
12268     if (positionNumber == 0) positionNumber = 1;
12269     lastLoadPositionFP = f;
12270     lastLoadPositionNumber = positionNumber;
12271     safeStrCpy(lastLoadPositionTitle, title, sizeof(lastLoadPositionTitle)/sizeof(lastLoadPositionTitle[0]));
12272     if (first.pr == NoProc && !appData.noChessProgram) {
12273       StartChessProgram(&first);
12274       InitChessProgram(&first, FALSE);
12275     }
12276     pn = positionNumber;
12277     if (positionNumber < 0) {
12278         /* Negative position number means to seek to that byte offset */
12279         if (fseek(f, -positionNumber, 0) == -1) {
12280             DisplayError(_("Can't seek on position file"), 0);
12281             return FALSE;
12282         };
12283         pn = 1;
12284     } else {
12285         if (fseek(f, 0, 0) == -1) {
12286             if (f == lastLoadPositionFP ?
12287                 positionNumber == lastLoadPositionNumber + 1 :
12288                 positionNumber == 1) {
12289                 pn = 1;
12290             } else {
12291                 DisplayError(_("Can't seek on position file"), 0);
12292                 return FALSE;
12293             }
12294         }
12295     }
12296     /* See if this file is FEN or old-style xboard */
12297     if (fgets(line, MSG_SIZ, f) == NULL) {
12298         DisplayError(_("Position not found in file"), 0);
12299         return FALSE;
12300     }
12301     // [HGM] FEN can begin with digit, any piece letter valid in this variant, or a + for Shogi promoted pieces
12302     fenMode = line[0] >= '0' && line[0] <= '9' || line[0] == '+' || CharToPiece(line[0]) != EmptySquare;
12303
12304     if (pn >= 2) {
12305         if (fenMode || line[0] == '#') pn--;
12306         while (pn > 0) {
12307             /* skip positions before number pn */
12308             if (fgets(line, MSG_SIZ, f) == NULL) {
12309                 Reset(TRUE, TRUE);
12310                 DisplayError(_("Position not found in file"), 0);
12311                 return FALSE;
12312             }
12313             if (fenMode || line[0] == '#') pn--;
12314         }
12315     }
12316
12317     if (fenMode) {
12318         if (!ParseFEN(initial_position, &blackPlaysFirst, line)) {
12319             DisplayError(_("Bad FEN position in file"), 0);
12320             return FALSE;
12321         }
12322     } else {
12323         (void) fgets(line, MSG_SIZ, f);
12324         (void) fgets(line, MSG_SIZ, f);
12325
12326         for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
12327             (void) fgets(line, MSG_SIZ, f);
12328             for (p = line, j = BOARD_LEFT; j < BOARD_RGHT; p++) {
12329                 if (*p == ' ')
12330                   continue;
12331                 initial_position[i][j++] = CharToPiece(*p);
12332             }
12333         }
12334
12335         blackPlaysFirst = FALSE;
12336         if (!feof(f)) {
12337             (void) fgets(line, MSG_SIZ, f);
12338             if (strncmp(line, "black", strlen("black"))==0)
12339               blackPlaysFirst = TRUE;
12340         }
12341     }
12342     startedFromSetupPosition = TRUE;
12343
12344     CopyBoard(boards[0], initial_position);
12345     if (blackPlaysFirst) {
12346         currentMove = forwardMostMove = backwardMostMove = 1;
12347         safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
12348         safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
12349         CopyBoard(boards[1], initial_position);
12350         DisplayMessage("", _("Black to play"));
12351     } else {
12352         currentMove = forwardMostMove = backwardMostMove = 0;
12353         DisplayMessage("", _("White to play"));
12354     }
12355     initialRulePlies = FENrulePlies; /* [HGM] copy FEN attributes as well */
12356     if(first.pr != NoProc) { // [HGM] in tourney-mode a position can be loaded before the chess engine is installed
12357         SendToProgram("force\n", &first);
12358         SendBoard(&first, forwardMostMove);
12359     }
12360     if (appData.debugMode) {
12361 int i, j;
12362   for(i=0;i<2;i++){for(j=0;j<6;j++)fprintf(debugFP, " %d", boards[i][CASTLING][j]);fprintf(debugFP,"\n");}
12363   for(j=0;j<6;j++)fprintf(debugFP, " %d", initialRights[j]);fprintf(debugFP,"\n");
12364         fprintf(debugFP, "Load Position\n");
12365     }
12366
12367     if (positionNumber > 1) {
12368       snprintf(line, MSG_SIZ, "%s %d", title, positionNumber);
12369         DisplayTitle(line);
12370     } else {
12371         DisplayTitle(title);
12372     }
12373     gameMode = EditGame;
12374     ModeHighlight();
12375     ResetClocks();
12376     timeRemaining[0][1] = whiteTimeRemaining;
12377     timeRemaining[1][1] = blackTimeRemaining;
12378     DrawPosition(FALSE, boards[currentMove]);
12379
12380     return TRUE;
12381 }
12382
12383
12384 void
12385 CopyPlayerNameIntoFileName (char **dest, char *src)
12386 {
12387     while (*src != NULLCHAR && *src != ',') {
12388         if (*src == ' ') {
12389             *(*dest)++ = '_';
12390             src++;
12391         } else {
12392             *(*dest)++ = *src++;
12393         }
12394     }
12395 }
12396
12397 char *
12398 DefaultFileName (char *ext)
12399 {
12400     static char def[MSG_SIZ];
12401     char *p;
12402
12403     if (gameInfo.white != NULL && gameInfo.white[0] != '-') {
12404         p = def;
12405         CopyPlayerNameIntoFileName(&p, gameInfo.white);
12406         *p++ = '-';
12407         CopyPlayerNameIntoFileName(&p, gameInfo.black);
12408         *p++ = '.';
12409         safeStrCpy(p, ext, MSG_SIZ-2-strlen(gameInfo.white)-strlen(gameInfo.black));
12410     } else {
12411         def[0] = NULLCHAR;
12412     }
12413     return def;
12414 }
12415
12416 /* Save the current game to the given file */
12417 int
12418 SaveGameToFile (char *filename, int append)
12419 {
12420     FILE *f;
12421     char buf[MSG_SIZ];
12422     int result, i, t,tot=0;
12423
12424     if (strcmp(filename, "-") == 0) {
12425         return SaveGame(stdout, 0, NULL);
12426     } else {
12427         for(i=0; i<10; i++) { // upto 10 tries
12428              f = fopen(filename, append ? "a" : "w");
12429              if(f && i) fprintf(f, "[Delay \"%d retries, %d msec\"]\n",i,tot);
12430              if(f || errno != 13) break;
12431              DoSleep(t = 5 + random()%11); // wait 5-15 msec
12432              tot += t;
12433         }
12434         if (f == NULL) {
12435             snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
12436             DisplayError(buf, errno);
12437             return FALSE;
12438         } else {
12439             safeStrCpy(buf, lastMsg, MSG_SIZ);
12440             DisplayMessage(_("Waiting for access to save file"), "");
12441             flock(fileno(f), LOCK_EX); // [HGM] lock: lock file while we are writing
12442             DisplayMessage(_("Saving game"), "");
12443             if(lseek(fileno(f), 0, SEEK_END) == -1) DisplayError(_("Bad Seek"), errno);     // better safe than sorry...
12444             result = SaveGame(f, 0, NULL);
12445             DisplayMessage(buf, "");
12446             return result;
12447         }
12448     }
12449 }
12450
12451 char *
12452 SavePart (char *str)
12453 {
12454     static char buf[MSG_SIZ];
12455     char *p;
12456
12457     p = strchr(str, ' ');
12458     if (p == NULL) return str;
12459     strncpy(buf, str, p - str);
12460     buf[p - str] = NULLCHAR;
12461     return buf;
12462 }
12463
12464 #define PGN_MAX_LINE 75
12465
12466 #define PGN_SIDE_WHITE  0
12467 #define PGN_SIDE_BLACK  1
12468
12469 static int
12470 FindFirstMoveOutOfBook (int side)
12471 {
12472     int result = -1;
12473
12474     if( backwardMostMove == 0 && ! startedFromSetupPosition) {
12475         int index = backwardMostMove;
12476         int has_book_hit = 0;
12477
12478         if( (index % 2) != side ) {
12479             index++;
12480         }
12481
12482         while( index < forwardMostMove ) {
12483             /* Check to see if engine is in book */
12484             int depth = pvInfoList[index].depth;
12485             int score = pvInfoList[index].score;
12486             int in_book = 0;
12487
12488             if( depth <= 2 ) {
12489                 in_book = 1;
12490             }
12491             else if( score == 0 && depth == 63 ) {
12492                 in_book = 1; /* Zappa */
12493             }
12494             else if( score == 2 && depth == 99 ) {
12495                 in_book = 1; /* Abrok */
12496             }
12497
12498             has_book_hit += in_book;
12499
12500             if( ! in_book ) {
12501                 result = index;
12502
12503                 break;
12504             }
12505
12506             index += 2;
12507         }
12508     }
12509
12510     return result;
12511 }
12512
12513 void
12514 GetOutOfBookInfo (char * buf)
12515 {
12516     int oob[2];
12517     int i;
12518     int offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
12519
12520     oob[0] = FindFirstMoveOutOfBook( PGN_SIDE_WHITE );
12521     oob[1] = FindFirstMoveOutOfBook( PGN_SIDE_BLACK );
12522
12523     *buf = '\0';
12524
12525     if( oob[0] >= 0 || oob[1] >= 0 ) {
12526         for( i=0; i<2; i++ ) {
12527             int idx = oob[i];
12528
12529             if( idx >= 0 ) {
12530                 if( i > 0 && oob[0] >= 0 ) {
12531                     strcat( buf, "   " );
12532                 }
12533
12534                 sprintf( buf+strlen(buf), "%d%s. ", (idx - offset)/2 + 1, idx & 1 ? ".." : "" );
12535                 sprintf( buf+strlen(buf), "%s%.2f",
12536                     pvInfoList[idx].score >= 0 ? "+" : "",
12537                     pvInfoList[idx].score / 100.0 );
12538             }
12539         }
12540     }
12541 }
12542
12543 /* Save game in PGN style and close the file */
12544 int
12545 SaveGamePGN (FILE *f)
12546 {
12547     int i, offset, linelen, newblock;
12548     time_t tm;
12549 //    char *movetext;
12550     char numtext[32];
12551     int movelen, numlen, blank;
12552     char move_buffer[100]; /* [AS] Buffer for move+PV info */
12553
12554     offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
12555
12556     tm = time((time_t *) NULL);
12557
12558     PrintPGNTags(f, &gameInfo);
12559
12560     if(appData.numberTag && matchMode) fprintf(f, "[Number \"%d\"]\n", nextGame+1); // [HGM] number tag
12561
12562     if (backwardMostMove > 0 || startedFromSetupPosition) {
12563         char *fen = PositionToFEN(backwardMostMove, NULL);
12564         fprintf(f, "[FEN \"%s\"]\n[SetUp \"1\"]\n", fen);
12565         fprintf(f, "\n{--------------\n");
12566         PrintPosition(f, backwardMostMove);
12567         fprintf(f, "--------------}\n");
12568         free(fen);
12569     }
12570     else {
12571         /* [AS] Out of book annotation */
12572         if( appData.saveOutOfBookInfo ) {
12573             char buf[64];
12574
12575             GetOutOfBookInfo( buf );
12576
12577             if( buf[0] != '\0' ) {
12578                 fprintf( f, "[%s \"%s\"]\n", PGN_OUT_OF_BOOK, buf );
12579             }
12580         }
12581
12582         fprintf(f, "\n");
12583     }
12584
12585     i = backwardMostMove;
12586     linelen = 0;
12587     newblock = TRUE;
12588
12589     while (i < forwardMostMove) {
12590         /* Print comments preceding this move */
12591         if (commentList[i] != NULL) {
12592             if (linelen > 0) fprintf(f, "\n");
12593             fprintf(f, "%s", commentList[i]);
12594             linelen = 0;
12595             newblock = TRUE;
12596         }
12597
12598         /* Format move number */
12599         if ((i % 2) == 0)
12600           snprintf(numtext, sizeof(numtext)/sizeof(numtext[0]),"%d.", (i - offset)/2 + 1);
12601         else
12602           if (newblock)
12603             snprintf(numtext, sizeof(numtext)/sizeof(numtext[0]), "%d...", (i - offset)/2 + 1);
12604           else
12605             numtext[0] = NULLCHAR;
12606
12607         numlen = strlen(numtext);
12608         newblock = FALSE;
12609
12610         /* Print move number */
12611         blank = linelen > 0 && numlen > 0;
12612         if (linelen + (blank ? 1 : 0) + numlen > PGN_MAX_LINE) {
12613             fprintf(f, "\n");
12614             linelen = 0;
12615             blank = 0;
12616         }
12617         if (blank) {
12618             fprintf(f, " ");
12619             linelen++;
12620         }
12621         fprintf(f, "%s", numtext);
12622         linelen += numlen;
12623
12624         /* Get move */
12625         safeStrCpy(move_buffer, SavePart(parseList[i]), sizeof(move_buffer)/sizeof(move_buffer[0])); // [HGM] pgn: print move via buffer, so it can be edited
12626         movelen = strlen(move_buffer); /* [HGM] pgn: line-break point before move */
12627
12628         /* Print move */
12629         blank = linelen > 0 && movelen > 0;
12630         if (linelen + (blank ? 1 : 0) + movelen > PGN_MAX_LINE) {
12631             fprintf(f, "\n");
12632             linelen = 0;
12633             blank = 0;
12634         }
12635         if (blank) {
12636             fprintf(f, " ");
12637             linelen++;
12638         }
12639         fprintf(f, "%s", move_buffer);
12640         linelen += movelen;
12641
12642         /* [AS] Add PV info if present */
12643         if( i >= 0 && appData.saveExtendedInfoInPGN && pvInfoList[i].depth > 0 ) {
12644             /* [HGM] add time */
12645             char buf[MSG_SIZ]; int seconds;
12646
12647             seconds = (pvInfoList[i].time+5)/10; // deci-seconds, rounded to nearest
12648
12649             if( seconds <= 0)
12650               buf[0] = 0;
12651             else
12652               if( seconds < 30 )
12653                 snprintf(buf, MSG_SIZ, " %3.1f%c", seconds/10., 0);
12654               else
12655                 {
12656                   seconds = (seconds + 4)/10; // round to full seconds
12657                   if( seconds < 60 )
12658                     snprintf(buf, MSG_SIZ, " %d%c", seconds, 0);
12659                   else
12660                     snprintf(buf, MSG_SIZ, " %d:%02d%c", seconds/60, seconds%60, 0);
12661                 }
12662
12663             snprintf( move_buffer, sizeof(move_buffer)/sizeof(move_buffer[0]),"{%s%.2f/%d%s}",
12664                       pvInfoList[i].score >= 0 ? "+" : "",
12665                       pvInfoList[i].score / 100.0,
12666                       pvInfoList[i].depth,
12667                       buf );
12668
12669             movelen = strlen(move_buffer); /* [HGM] pgn: line-break point after move */
12670
12671             /* Print score/depth */
12672             blank = linelen > 0 && movelen > 0;
12673             if (linelen + (blank ? 1 : 0) + movelen > PGN_MAX_LINE) {
12674                 fprintf(f, "\n");
12675                 linelen = 0;
12676                 blank = 0;
12677             }
12678             if (blank) {
12679                 fprintf(f, " ");
12680                 linelen++;
12681             }
12682             fprintf(f, "%s", move_buffer);
12683             linelen += movelen;
12684         }
12685
12686         i++;
12687     }
12688
12689     /* Start a new line */
12690     if (linelen > 0) fprintf(f, "\n");
12691
12692     /* Print comments after last move */
12693     if (commentList[i] != NULL) {
12694         fprintf(f, "%s\n", commentList[i]);
12695     }
12696
12697     /* Print result */
12698     if (gameInfo.resultDetails != NULL &&
12699         gameInfo.resultDetails[0] != NULLCHAR) {
12700         fprintf(f, "{%s} %s\n\n", gameInfo.resultDetails,
12701                 PGNResult(gameInfo.result));
12702     } else {
12703         fprintf(f, "%s\n\n", PGNResult(gameInfo.result));
12704     }
12705
12706     fclose(f);
12707     lastSavedGame = GameCheckSum(); // [HGM] save: remember ID of last saved game to prevent double saving
12708     return TRUE;
12709 }
12710
12711 /* Save game in old style and close the file */
12712 int
12713 SaveGameOldStyle (FILE *f)
12714 {
12715     int i, offset;
12716     time_t tm;
12717
12718     tm = time((time_t *) NULL);
12719
12720     fprintf(f, "# %s game file -- %s", programName, ctime(&tm));
12721     PrintOpponents(f);
12722
12723     if (backwardMostMove > 0 || startedFromSetupPosition) {
12724         fprintf(f, "\n[--------------\n");
12725         PrintPosition(f, backwardMostMove);
12726         fprintf(f, "--------------]\n");
12727     } else {
12728         fprintf(f, "\n");
12729     }
12730
12731     i = backwardMostMove;
12732     offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
12733
12734     while (i < forwardMostMove) {
12735         if (commentList[i] != NULL) {
12736             fprintf(f, "[%s]\n", commentList[i]);
12737         }
12738
12739         if ((i % 2) == 1) {
12740             fprintf(f, "%d. ...  %s\n", (i - offset)/2 + 1, parseList[i]);
12741             i++;
12742         } else {
12743             fprintf(f, "%d. %s  ", (i - offset)/2 + 1, parseList[i]);
12744             i++;
12745             if (commentList[i] != NULL) {
12746                 fprintf(f, "\n");
12747                 continue;
12748             }
12749             if (i >= forwardMostMove) {
12750                 fprintf(f, "\n");
12751                 break;
12752             }
12753             fprintf(f, "%s\n", parseList[i]);
12754             i++;
12755         }
12756     }
12757
12758     if (commentList[i] != NULL) {
12759         fprintf(f, "[%s]\n", commentList[i]);
12760     }
12761
12762     /* This isn't really the old style, but it's close enough */
12763     if (gameInfo.resultDetails != NULL &&
12764         gameInfo.resultDetails[0] != NULLCHAR) {
12765         fprintf(f, "%s (%s)\n\n", PGNResult(gameInfo.result),
12766                 gameInfo.resultDetails);
12767     } else {
12768         fprintf(f, "%s\n\n", PGNResult(gameInfo.result));
12769     }
12770
12771     fclose(f);
12772     return TRUE;
12773 }
12774
12775 /* Save the current game to open file f and close the file */
12776 int
12777 SaveGame (FILE *f, int dummy, char *dummy2)
12778 {
12779     if (gameMode == EditPosition) EditPositionDone(TRUE);
12780     lastSavedGame = GameCheckSum(); // [HGM] save: remember ID of last saved game to prevent double saving
12781     if (appData.oldSaveStyle)
12782       return SaveGameOldStyle(f);
12783     else
12784       return SaveGamePGN(f);
12785 }
12786
12787 /* Save the current position to the given file */
12788 int
12789 SavePositionToFile (char *filename)
12790 {
12791     FILE *f;
12792     char buf[MSG_SIZ];
12793
12794     if (strcmp(filename, "-") == 0) {
12795         return SavePosition(stdout, 0, NULL);
12796     } else {
12797         f = fopen(filename, "a");
12798         if (f == NULL) {
12799             snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
12800             DisplayError(buf, errno);
12801             return FALSE;
12802         } else {
12803             safeStrCpy(buf, lastMsg, MSG_SIZ);
12804             DisplayMessage(_("Waiting for access to save file"), "");
12805             flock(fileno(f), LOCK_EX); // [HGM] lock
12806             DisplayMessage(_("Saving position"), "");
12807             lseek(fileno(f), 0, SEEK_END);     // better safe than sorry...
12808             SavePosition(f, 0, NULL);
12809             DisplayMessage(buf, "");
12810             return TRUE;
12811         }
12812     }
12813 }
12814
12815 /* Save the current position to the given open file and close the file */
12816 int
12817 SavePosition (FILE *f, int dummy, char *dummy2)
12818 {
12819     time_t tm;
12820     char *fen;
12821
12822     if (gameMode == EditPosition) EditPositionDone(TRUE);
12823     if (appData.oldSaveStyle) {
12824         tm = time((time_t *) NULL);
12825
12826         fprintf(f, "# %s position file -- %s", programName, ctime(&tm));
12827         PrintOpponents(f);
12828         fprintf(f, "[--------------\n");
12829         PrintPosition(f, currentMove);
12830         fprintf(f, "--------------]\n");
12831     } else {
12832         fen = PositionToFEN(currentMove, NULL);
12833         fprintf(f, "%s\n", fen);
12834         free(fen);
12835     }
12836     fclose(f);
12837     return TRUE;
12838 }
12839
12840 void
12841 ReloadCmailMsgEvent (int unregister)
12842 {
12843 #if !WIN32
12844     static char *inFilename = NULL;
12845     static char *outFilename;
12846     int i;
12847     struct stat inbuf, outbuf;
12848     int status;
12849
12850     /* Any registered moves are unregistered if unregister is set, */
12851     /* i.e. invoked by the signal handler */
12852     if (unregister) {
12853         for (i = 0; i < CMAIL_MAX_GAMES; i ++) {
12854             cmailMoveRegistered[i] = FALSE;
12855             if (cmailCommentList[i] != NULL) {
12856                 free(cmailCommentList[i]);
12857                 cmailCommentList[i] = NULL;
12858             }
12859         }
12860         nCmailMovesRegistered = 0;
12861     }
12862
12863     for (i = 0; i < CMAIL_MAX_GAMES; i ++) {
12864         cmailResult[i] = CMAIL_NOT_RESULT;
12865     }
12866     nCmailResults = 0;
12867
12868     if (inFilename == NULL) {
12869         /* Because the filenames are static they only get malloced once  */
12870         /* and they never get freed                                      */
12871         inFilename = (char *) malloc(strlen(appData.cmailGameName) + 9);
12872         sprintf(inFilename, "%s.game.in", appData.cmailGameName);
12873
12874         outFilename = (char *) malloc(strlen(appData.cmailGameName) + 5);
12875         sprintf(outFilename, "%s.out", appData.cmailGameName);
12876     }
12877
12878     status = stat(outFilename, &outbuf);
12879     if (status < 0) {
12880         cmailMailedMove = FALSE;
12881     } else {
12882         status = stat(inFilename, &inbuf);
12883         cmailMailedMove = (inbuf.st_mtime < outbuf.st_mtime);
12884     }
12885
12886     /* LoadGameFromFile(CMAIL_MAX_GAMES) with cmailMsgLoaded == TRUE
12887        counts the games, notes how each one terminated, etc.
12888
12889        It would be nice to remove this kludge and instead gather all
12890        the information while building the game list.  (And to keep it
12891        in the game list nodes instead of having a bunch of fixed-size
12892        parallel arrays.)  Note this will require getting each game's
12893        termination from the PGN tags, as the game list builder does
12894        not process the game moves.  --mann
12895        */
12896     cmailMsgLoaded = TRUE;
12897     LoadGameFromFile(inFilename, CMAIL_MAX_GAMES, "", FALSE);
12898
12899     /* Load first game in the file or popup game menu */
12900     LoadGameFromFile(inFilename, 0, appData.cmailGameName, TRUE);
12901
12902 #endif /* !WIN32 */
12903     return;
12904 }
12905
12906 int
12907 RegisterMove ()
12908 {
12909     FILE *f;
12910     char string[MSG_SIZ];
12911
12912     if (   cmailMailedMove
12913         || (cmailResult[lastLoadGameNumber - 1] == CMAIL_OLD_RESULT)) {
12914         return TRUE;            /* Allow free viewing  */
12915     }
12916
12917     /* Unregister move to ensure that we don't leave RegisterMove        */
12918     /* with the move registered when the conditions for registering no   */
12919     /* longer hold                                                       */
12920     if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
12921         cmailMoveRegistered[lastLoadGameNumber - 1] = FALSE;
12922         nCmailMovesRegistered --;
12923
12924         if (cmailCommentList[lastLoadGameNumber - 1] != NULL)
12925           {
12926               free(cmailCommentList[lastLoadGameNumber - 1]);
12927               cmailCommentList[lastLoadGameNumber - 1] = NULL;
12928           }
12929     }
12930
12931     if (cmailOldMove == -1) {
12932         DisplayError(_("You have edited the game history.\nUse Reload Same Game and make your move again."), 0);
12933         return FALSE;
12934     }
12935
12936     if (currentMove > cmailOldMove + 1) {
12937         DisplayError(_("You have entered too many moves.\nBack up to the correct position and try again."), 0);
12938         return FALSE;
12939     }
12940
12941     if (currentMove < cmailOldMove) {
12942         DisplayError(_("Displayed position is not current.\nStep forward to the correct position and try again."), 0);
12943         return FALSE;
12944     }
12945
12946     if (forwardMostMove > currentMove) {
12947         /* Silently truncate extra moves */
12948         TruncateGame();
12949     }
12950
12951     if (   (currentMove == cmailOldMove + 1)
12952         || (   (currentMove == cmailOldMove)
12953             && (   (cmailMoveType[lastLoadGameNumber - 1] == CMAIL_ACCEPT)
12954                 || (cmailMoveType[lastLoadGameNumber - 1] == CMAIL_RESIGN)))) {
12955         if (gameInfo.result != GameUnfinished) {
12956             cmailResult[lastLoadGameNumber - 1] = CMAIL_NEW_RESULT;
12957         }
12958
12959         if (commentList[currentMove] != NULL) {
12960             cmailCommentList[lastLoadGameNumber - 1]
12961               = StrSave(commentList[currentMove]);
12962         }
12963         safeStrCpy(cmailMove[lastLoadGameNumber - 1], moveList[currentMove - 1], sizeof(cmailMove[lastLoadGameNumber - 1])/sizeof(cmailMove[lastLoadGameNumber - 1][0]));
12964
12965         if (appData.debugMode)
12966           fprintf(debugFP, "Saving %s for game %d\n",
12967                   cmailMove[lastLoadGameNumber - 1], lastLoadGameNumber);
12968
12969         snprintf(string, MSG_SIZ, "%s.game.out.%d", appData.cmailGameName, lastLoadGameNumber);
12970
12971         f = fopen(string, "w");
12972         if (appData.oldSaveStyle) {
12973             SaveGameOldStyle(f); /* also closes the file */
12974
12975             snprintf(string, MSG_SIZ, "%s.pos.out", appData.cmailGameName);
12976             f = fopen(string, "w");
12977             SavePosition(f, 0, NULL); /* also closes the file */
12978         } else {
12979             fprintf(f, "{--------------\n");
12980             PrintPosition(f, currentMove);
12981             fprintf(f, "--------------}\n\n");
12982
12983             SaveGame(f, 0, NULL); /* also closes the file*/
12984         }
12985
12986         cmailMoveRegistered[lastLoadGameNumber - 1] = TRUE;
12987         nCmailMovesRegistered ++;
12988     } else if (nCmailGames == 1) {
12989         DisplayError(_("You have not made a move yet"), 0);
12990         return FALSE;
12991     }
12992
12993     return TRUE;
12994 }
12995
12996 void
12997 MailMoveEvent ()
12998 {
12999 #if !WIN32
13000     static char *partCommandString = "cmail -xv%s -remail -game %s 2>&1";
13001     FILE *commandOutput;
13002     char buffer[MSG_SIZ], msg[MSG_SIZ], string[MSG_SIZ];
13003     int nBytes = 0;             /*  Suppress warnings on uninitialized variables    */
13004     int nBuffers;
13005     int i;
13006     int archived;
13007     char *arcDir;
13008
13009     if (! cmailMsgLoaded) {
13010         DisplayError(_("The cmail message is not loaded.\nUse Reload CMail Message and make your move again."), 0);
13011         return;
13012     }
13013
13014     if (nCmailGames == nCmailResults) {
13015         DisplayError(_("No unfinished games"), 0);
13016         return;
13017     }
13018
13019 #if CMAIL_PROHIBIT_REMAIL
13020     if (cmailMailedMove) {
13021       snprintf(msg, MSG_SIZ, _("You have already mailed a move.\nWait until a move arrives from your opponent.\nTo resend the same move, type\n\"cmail -remail -game %s\"\non the command line."), appData.cmailGameName);
13022         DisplayError(msg, 0);
13023         return;
13024     }
13025 #endif
13026
13027     if (! (cmailMailedMove || RegisterMove())) return;
13028
13029     if (   cmailMailedMove
13030         || (nCmailMovesRegistered + nCmailResults == nCmailGames)) {
13031       snprintf(string, MSG_SIZ, partCommandString,
13032                appData.debugMode ? " -v" : "", appData.cmailGameName);
13033         commandOutput = popen(string, "r");
13034
13035         if (commandOutput == NULL) {
13036             DisplayError(_("Failed to invoke cmail"), 0);
13037         } else {
13038             for (nBuffers = 0; (! feof(commandOutput)); nBuffers ++) {
13039                 nBytes = fread(buffer, 1, MSG_SIZ - 1, commandOutput);
13040             }
13041             if (nBuffers > 1) {
13042                 (void) memcpy(msg, buffer + nBytes, MSG_SIZ - nBytes - 1);
13043                 (void) memcpy(msg + MSG_SIZ - nBytes - 1, buffer, nBytes);
13044                 nBytes = MSG_SIZ - 1;
13045             } else {
13046                 (void) memcpy(msg, buffer, nBytes);
13047             }
13048             *(msg + nBytes) = '\0'; /* \0 for end-of-string*/
13049
13050             if(StrStr(msg, "Mailed cmail message to ") != NULL) {
13051                 cmailMailedMove = TRUE; /* Prevent >1 moves    */
13052
13053                 archived = TRUE;
13054                 for (i = 0; i < nCmailGames; i ++) {
13055                     if (cmailResult[i] == CMAIL_NOT_RESULT) {
13056                         archived = FALSE;
13057                     }
13058                 }
13059                 if (   archived
13060                     && (   (arcDir = (char *) getenv("CMAIL_ARCDIR"))
13061                         != NULL)) {
13062                   snprintf(buffer, MSG_SIZ, "%s/%s.%s.archive",
13063                            arcDir,
13064                            appData.cmailGameName,
13065                            gameInfo.date);
13066                     LoadGameFromFile(buffer, 1, buffer, FALSE);
13067                     cmailMsgLoaded = FALSE;
13068                 }
13069             }
13070
13071             DisplayInformation(msg);
13072             pclose(commandOutput);
13073         }
13074     } else {
13075         if ((*cmailMsg) != '\0') {
13076             DisplayInformation(cmailMsg);
13077         }
13078     }
13079
13080     return;
13081 #endif /* !WIN32 */
13082 }
13083
13084 char *
13085 CmailMsg ()
13086 {
13087 #if WIN32
13088     return NULL;
13089 #else
13090     int  prependComma = 0;
13091     char number[5];
13092     char string[MSG_SIZ];       /* Space for game-list */
13093     int  i;
13094
13095     if (!cmailMsgLoaded) return "";
13096
13097     if (cmailMailedMove) {
13098       snprintf(cmailMsg, MSG_SIZ, _("Waiting for reply from opponent\n"));
13099     } else {
13100         /* Create a list of games left */
13101       snprintf(string, MSG_SIZ, "[");
13102         for (i = 0; i < nCmailGames; i ++) {
13103             if (! (   cmailMoveRegistered[i]
13104                    || (cmailResult[i] == CMAIL_OLD_RESULT))) {
13105                 if (prependComma) {
13106                     snprintf(number, sizeof(number)/sizeof(number[0]), ",%d", i + 1);
13107                 } else {
13108                     snprintf(number, sizeof(number)/sizeof(number[0]), "%d", i + 1);
13109                     prependComma = 1;
13110                 }
13111
13112                 strcat(string, number);
13113             }
13114         }
13115         strcat(string, "]");
13116
13117         if (nCmailMovesRegistered + nCmailResults == 0) {
13118             switch (nCmailGames) {
13119               case 1:
13120                 snprintf(cmailMsg, MSG_SIZ, _("Still need to make move for game\n"));
13121                 break;
13122
13123               case 2:
13124                 snprintf(cmailMsg, MSG_SIZ, _("Still need to make moves for both games\n"));
13125                 break;
13126
13127               default:
13128                 snprintf(cmailMsg, MSG_SIZ, _("Still need to make moves for all %d games\n"),
13129                          nCmailGames);
13130                 break;
13131             }
13132         } else {
13133             switch (nCmailGames - nCmailMovesRegistered - nCmailResults) {
13134               case 1:
13135                 snprintf(cmailMsg, MSG_SIZ, _("Still need to make a move for game %s\n"),
13136                          string);
13137                 break;
13138
13139               case 0:
13140                 if (nCmailResults == nCmailGames) {
13141                   snprintf(cmailMsg, MSG_SIZ, _("No unfinished games\n"));
13142                 } else {
13143                   snprintf(cmailMsg, MSG_SIZ, _("Ready to send mail\n"));
13144                 }
13145                 break;
13146
13147               default:
13148                 snprintf(cmailMsg, MSG_SIZ, _("Still need to make moves for games %s\n"),
13149                          string);
13150             }
13151         }
13152     }
13153     return cmailMsg;
13154 #endif /* WIN32 */
13155 }
13156
13157 void
13158 ResetGameEvent ()
13159 {
13160     if (gameMode == Training)
13161       SetTrainingModeOff();
13162
13163     Reset(TRUE, TRUE);
13164     cmailMsgLoaded = FALSE;
13165     if (appData.icsActive) {
13166       SendToICS(ics_prefix);
13167       SendToICS("refresh\n");
13168     }
13169 }
13170
13171 void
13172 ExitEvent (int status)
13173 {
13174     exiting++;
13175     if (exiting > 2) {
13176       /* Give up on clean exit */
13177       exit(status);
13178     }
13179     if (exiting > 1) {
13180       /* Keep trying for clean exit */
13181       return;
13182     }
13183
13184     if (appData.icsActive && appData.colorize) Colorize(ColorNone, FALSE);
13185
13186     if (telnetISR != NULL) {
13187       RemoveInputSource(telnetISR);
13188     }
13189     if (icsPR != NoProc) {
13190       DestroyChildProcess(icsPR, TRUE);
13191     }
13192
13193     /* [HGM] crash: leave writing PGN and position entirely to GameEnds() */
13194     GameEnds(gameInfo.result, gameInfo.resultDetails==NULL ? "xboard exit" : gameInfo.resultDetails, GE_PLAYER);
13195
13196     /* [HGM] crash: the above GameEnds() is a dud if another one was running */
13197     /* make sure this other one finishes before killing it!                  */
13198     if(endingGame) { int count = 0;
13199         if(appData.debugMode) fprintf(debugFP, "ExitEvent() during GameEnds(), wait\n");
13200         while(endingGame && count++ < 10) DoSleep(1);
13201         if(appData.debugMode && endingGame) fprintf(debugFP, "GameEnds() seems stuck, proceed exiting\n");
13202     }
13203
13204     /* Kill off chess programs */
13205     if (first.pr != NoProc) {
13206         ExitAnalyzeMode();
13207
13208         DoSleep( appData.delayBeforeQuit );
13209         SendToProgram("quit\n", &first);
13210         DoSleep( appData.delayAfterQuit );
13211         DestroyChildProcess(first.pr, 10 /* [AS] first.useSigterm */ );
13212     }
13213     if (second.pr != NoProc) {
13214         DoSleep( appData.delayBeforeQuit );
13215         SendToProgram("quit\n", &second);
13216         DoSleep( appData.delayAfterQuit );
13217         DestroyChildProcess(second.pr, 10 /* [AS] second.useSigterm */ );
13218     }
13219     if (first.isr != NULL) {
13220         RemoveInputSource(first.isr);
13221     }
13222     if (second.isr != NULL) {
13223         RemoveInputSource(second.isr);
13224     }
13225
13226     if (pairing.pr != NoProc) SendToProgram("quit\n", &pairing);
13227     if (pairing.isr != NULL) RemoveInputSource(pairing.isr);
13228
13229     ShutDownFrontEnd();
13230     exit(status);
13231 }
13232
13233 void
13234 PauseEvent ()
13235 {
13236     if (appData.debugMode)
13237         fprintf(debugFP, "PauseEvent(): pausing %d\n", pausing);
13238     if (pausing) {
13239         pausing = FALSE;
13240         ModeHighlight();
13241         if (gameMode == MachinePlaysWhite ||
13242             gameMode == MachinePlaysBlack) {
13243             StartClocks();
13244         } else {
13245             DisplayBothClocks();
13246         }
13247         if (gameMode == PlayFromGameFile) {
13248             if (appData.timeDelay >= 0)
13249                 AutoPlayGameLoop();
13250         } else if (gameMode == IcsExamining && pauseExamInvalid) {
13251             Reset(FALSE, TRUE);
13252             SendToICS(ics_prefix);
13253             SendToICS("refresh\n");
13254         } else if (currentMove < forwardMostMove) {
13255             ForwardInner(forwardMostMove);
13256         }
13257         pauseExamInvalid = FALSE;
13258     } else {
13259         switch (gameMode) {
13260           default:
13261             return;
13262           case IcsExamining:
13263             pauseExamForwardMostMove = forwardMostMove;
13264             pauseExamInvalid = FALSE;
13265             /* fall through */
13266           case IcsObserving:
13267           case IcsPlayingWhite:
13268           case IcsPlayingBlack:
13269             pausing = TRUE;
13270             ModeHighlight();
13271             return;
13272           case PlayFromGameFile:
13273             (void) StopLoadGameTimer();
13274             pausing = TRUE;
13275             ModeHighlight();
13276             break;
13277           case BeginningOfGame:
13278             if (appData.icsActive) return;
13279             /* else fall through */
13280           case MachinePlaysWhite:
13281           case MachinePlaysBlack:
13282           case TwoMachinesPlay:
13283             if (forwardMostMove == 0)
13284               return;           /* don't pause if no one has moved */
13285             if ((gameMode == MachinePlaysWhite &&
13286                  !WhiteOnMove(forwardMostMove)) ||
13287                 (gameMode == MachinePlaysBlack &&
13288                  WhiteOnMove(forwardMostMove))) {
13289                 StopClocks();
13290             }
13291             pausing = TRUE;
13292             ModeHighlight();
13293             break;
13294         }
13295     }
13296 }
13297
13298 void
13299 EditCommentEvent ()
13300 {
13301     char title[MSG_SIZ];
13302
13303     if (currentMove < 1 || parseList[currentMove - 1][0] == NULLCHAR) {
13304       safeStrCpy(title, _("Edit comment"), sizeof(title)/sizeof(title[0]));
13305     } else {
13306       snprintf(title, MSG_SIZ, _("Edit comment on %d.%s%s"), (currentMove - 1) / 2 + 1,
13307                WhiteOnMove(currentMove - 1) ? " " : ".. ",
13308                parseList[currentMove - 1]);
13309     }
13310
13311     EditCommentPopUp(currentMove, title, commentList[currentMove]);
13312 }
13313
13314
13315 void
13316 EditTagsEvent ()
13317 {
13318     char *tags = PGNTags(&gameInfo);
13319     bookUp = FALSE;
13320     EditTagsPopUp(tags, NULL);
13321     free(tags);
13322 }
13323
13324 void
13325 AnalyzeModeEvent ()
13326 {
13327     if (appData.noChessProgram || gameMode == AnalyzeMode)
13328       return;
13329
13330     if (gameMode != AnalyzeFile) {
13331         if (!appData.icsEngineAnalyze) {
13332                EditGameEvent();
13333                if (gameMode != EditGame) return;
13334         }
13335         ResurrectChessProgram();
13336         SendToProgram("analyze\n", &first);
13337         first.analyzing = TRUE;
13338         /*first.maybeThinking = TRUE;*/
13339         first.maybeThinking = FALSE; /* avoid killing GNU Chess */
13340         EngineOutputPopUp();
13341     }
13342     if (!appData.icsEngineAnalyze) gameMode = AnalyzeMode;
13343     pausing = FALSE;
13344     ModeHighlight();
13345     SetGameInfo();
13346
13347     StartAnalysisClock();
13348     GetTimeMark(&lastNodeCountTime);
13349     lastNodeCount = 0;
13350 }
13351
13352 void
13353 AnalyzeFileEvent ()
13354 {
13355     if (appData.noChessProgram || gameMode == AnalyzeFile)
13356       return;
13357
13358     if (gameMode != AnalyzeMode) {
13359         EditGameEvent();
13360         if (gameMode != EditGame) return;
13361         ResurrectChessProgram();
13362         SendToProgram("analyze\n", &first);
13363         first.analyzing = TRUE;
13364         /*first.maybeThinking = TRUE;*/
13365         first.maybeThinking = FALSE; /* avoid killing GNU Chess */
13366         EngineOutputPopUp();
13367     }
13368     gameMode = AnalyzeFile;
13369     pausing = FALSE;
13370     ModeHighlight();
13371     SetGameInfo();
13372
13373     StartAnalysisClock();
13374     GetTimeMark(&lastNodeCountTime);
13375     lastNodeCount = 0;
13376     if(appData.timeDelay > 0) StartLoadGameTimer((long)(1000.0 * appData.timeDelay));
13377 }
13378
13379 void
13380 MachineWhiteEvent ()
13381 {
13382     char buf[MSG_SIZ];
13383     char *bookHit = NULL;
13384
13385     if (appData.noChessProgram || (gameMode == MachinePlaysWhite))
13386       return;
13387
13388
13389     if (gameMode == PlayFromGameFile ||
13390         gameMode == TwoMachinesPlay  ||
13391         gameMode == Training         ||
13392         gameMode == AnalyzeMode      ||
13393         gameMode == EndOfGame)
13394         EditGameEvent();
13395
13396     if (gameMode == EditPosition)
13397         EditPositionDone(TRUE);
13398
13399     if (!WhiteOnMove(currentMove)) {
13400         DisplayError(_("It is not White's turn"), 0);
13401         return;
13402     }
13403
13404     if (gameMode == AnalyzeMode || gameMode == AnalyzeFile)
13405       ExitAnalyzeMode();
13406
13407     if (gameMode == EditGame || gameMode == AnalyzeMode ||
13408         gameMode == AnalyzeFile)
13409         TruncateGame();
13410
13411     ResurrectChessProgram();    /* in case it isn't running */
13412     if(gameMode == BeginningOfGame) { /* [HGM] time odds: to get right odds in human mode */
13413         gameMode = MachinePlaysWhite;
13414         ResetClocks();
13415     } else
13416     gameMode = MachinePlaysWhite;
13417     pausing = FALSE;
13418     ModeHighlight();
13419     SetGameInfo();
13420     snprintf(buf, MSG_SIZ, "%s %s %s", gameInfo.white, _("vs."), gameInfo.black);
13421     DisplayTitle(buf);
13422     if (first.sendName) {
13423       snprintf(buf, MSG_SIZ, "name %s\n", gameInfo.black);
13424       SendToProgram(buf, &first);
13425     }
13426     if (first.sendTime) {
13427       if (first.useColors) {
13428         SendToProgram("black\n", &first); /*gnu kludge*/
13429       }
13430       SendTimeRemaining(&first, TRUE);
13431     }
13432     if (first.useColors) {
13433       SendToProgram("white\n", &first); // [HGM] book: send 'go' separately
13434     }
13435     bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: send go or retrieve book move
13436     SetMachineThinkingEnables();
13437     first.maybeThinking = TRUE;
13438     StartClocks();
13439     firstMove = FALSE;
13440
13441     if (appData.autoFlipView && !flipView) {
13442       flipView = !flipView;
13443       DrawPosition(FALSE, NULL);
13444       DisplayBothClocks();       // [HGM] logo: clocks might have to be exchanged;
13445     }
13446
13447     if(bookHit) { // [HGM] book: simulate book reply
13448         static char bookMove[MSG_SIZ]; // a bit generous?
13449
13450         programStats.nodes = programStats.depth = programStats.time =
13451         programStats.score = programStats.got_only_move = 0;
13452         sprintf(programStats.movelist, "%s (xbook)", bookHit);
13453
13454         safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
13455         strcat(bookMove, bookHit);
13456         HandleMachineMove(bookMove, &first);
13457     }
13458 }
13459
13460 void
13461 MachineBlackEvent ()
13462 {
13463   char buf[MSG_SIZ];
13464   char *bookHit = NULL;
13465
13466     if (appData.noChessProgram || (gameMode == MachinePlaysBlack))
13467         return;
13468
13469
13470     if (gameMode == PlayFromGameFile ||
13471         gameMode == TwoMachinesPlay  ||
13472         gameMode == Training         ||
13473         gameMode == AnalyzeMode      ||
13474         gameMode == EndOfGame)
13475         EditGameEvent();
13476
13477     if (gameMode == EditPosition)
13478         EditPositionDone(TRUE);
13479
13480     if (WhiteOnMove(currentMove)) {
13481         DisplayError(_("It is not Black's turn"), 0);
13482         return;
13483     }
13484
13485     if (gameMode == AnalyzeMode || gameMode == AnalyzeFile)
13486       ExitAnalyzeMode();
13487
13488     if (gameMode == EditGame || gameMode == AnalyzeMode ||
13489         gameMode == AnalyzeFile)
13490         TruncateGame();
13491
13492     ResurrectChessProgram();    /* in case it isn't running */
13493     gameMode = MachinePlaysBlack;
13494     pausing = FALSE;
13495     ModeHighlight();
13496     SetGameInfo();
13497     snprintf(buf, MSG_SIZ, "%s %s %s", gameInfo.white, _("vs."), gameInfo.black);
13498     DisplayTitle(buf);
13499     if (first.sendName) {
13500       snprintf(buf, MSG_SIZ, "name %s\n", gameInfo.white);
13501       SendToProgram(buf, &first);
13502     }
13503     if (first.sendTime) {
13504       if (first.useColors) {
13505         SendToProgram("white\n", &first); /*gnu kludge*/
13506       }
13507       SendTimeRemaining(&first, FALSE);
13508     }
13509     if (first.useColors) {
13510       SendToProgram("black\n", &first); // [HGM] book: 'go' sent separately
13511     }
13512     bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: send go or retrieve book move
13513     SetMachineThinkingEnables();
13514     first.maybeThinking = TRUE;
13515     StartClocks();
13516
13517     if (appData.autoFlipView && flipView) {
13518       flipView = !flipView;
13519       DrawPosition(FALSE, NULL);
13520       DisplayBothClocks();       // [HGM] logo: clocks might have to be exchanged;
13521     }
13522     if(bookHit) { // [HGM] book: simulate book reply
13523         static char bookMove[MSG_SIZ]; // a bit generous?
13524
13525         programStats.nodes = programStats.depth = programStats.time =
13526         programStats.score = programStats.got_only_move = 0;
13527         sprintf(programStats.movelist, "%s (xbook)", bookHit);
13528
13529         safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
13530         strcat(bookMove, bookHit);
13531         HandleMachineMove(bookMove, &first);
13532     }
13533 }
13534
13535
13536 void
13537 DisplayTwoMachinesTitle ()
13538 {
13539     char buf[MSG_SIZ];
13540     if (appData.matchGames > 0) {
13541         if(appData.tourneyFile[0]) {
13542           snprintf(buf, MSG_SIZ, "%s %s %s (%d/%d%s)",
13543                    gameInfo.white, _("vs."), gameInfo.black,
13544                    nextGame+1, appData.matchGames+1,
13545                    appData.tourneyType>0 ? "gt" : appData.tourneyType<0 ? "sw" : "rr");
13546         } else 
13547         if (first.twoMachinesColor[0] == 'w') {
13548           snprintf(buf, MSG_SIZ, "%s %s %s (%d-%d-%d)",
13549                    gameInfo.white, _("vs."),  gameInfo.black,
13550                    first.matchWins, second.matchWins,
13551                    matchGame - 1 - (first.matchWins + second.matchWins));
13552         } else {
13553           snprintf(buf, MSG_SIZ, "%s %s %s (%d-%d-%d)",
13554                    gameInfo.white, _("vs."), gameInfo.black,
13555                    second.matchWins, first.matchWins,
13556                    matchGame - 1 - (first.matchWins + second.matchWins));
13557         }
13558     } else {
13559       snprintf(buf, MSG_SIZ, "%s %s %s", gameInfo.white, _("vs."), gameInfo.black);
13560     }
13561     DisplayTitle(buf);
13562 }
13563
13564 void
13565 SettingsMenuIfReady ()
13566 {
13567   if (second.lastPing != second.lastPong) {
13568     DisplayMessage("", _("Waiting for second chess program"));
13569     ScheduleDelayedEvent(SettingsMenuIfReady, 10); // [HGM] fast: lowered from 1000
13570     return;
13571   }
13572   ThawUI();
13573   DisplayMessage("", "");
13574   SettingsPopUp(&second);
13575 }
13576
13577 int
13578 WaitForEngine (ChessProgramState *cps, DelayedEventCallback retry)
13579 {
13580     char buf[MSG_SIZ];
13581     if (cps->pr == NoProc) {
13582         StartChessProgram(cps);
13583         if (cps->protocolVersion == 1) {
13584           retry();
13585         } else {
13586           /* kludge: allow timeout for initial "feature" command */
13587           FreezeUI();
13588           snprintf(buf, MSG_SIZ, _("Starting %s chess program"), _(cps->which));
13589           DisplayMessage("", buf);
13590           ScheduleDelayedEvent(retry, FEATURE_TIMEOUT);
13591         }
13592         return 1;
13593     }
13594     return 0;
13595 }
13596
13597 void
13598 TwoMachinesEvent P((void))
13599 {
13600     int i;
13601     char buf[MSG_SIZ];
13602     ChessProgramState *onmove;
13603     char *bookHit = NULL;
13604     static int stalling = 0;
13605     TimeMark now;
13606     long wait;
13607
13608     if (appData.noChessProgram) return;
13609
13610     if(second.protocolVersion >= 2 && !strstr(second.variants, VariantName(gameInfo.variant))) {
13611         DisplayError("second engine does not play this", 0);
13612         return;
13613     }
13614
13615     switch (gameMode) {
13616       case TwoMachinesPlay:
13617         return;
13618       case MachinePlaysWhite:
13619       case MachinePlaysBlack:
13620         if (WhiteOnMove(forwardMostMove) == (gameMode == MachinePlaysWhite)) {
13621             DisplayError(_("Wait until your turn,\nor select Move Now"), 0);
13622             return;
13623         }
13624         /* fall through */
13625       case BeginningOfGame:
13626       case PlayFromGameFile:
13627       case EndOfGame:
13628         EditGameEvent();
13629         if (gameMode != EditGame) return;
13630         break;
13631       case EditPosition:
13632         EditPositionDone(TRUE);
13633         break;
13634       case AnalyzeMode:
13635       case AnalyzeFile:
13636         ExitAnalyzeMode();
13637         break;
13638       case EditGame:
13639       default:
13640         break;
13641     }
13642
13643 //    forwardMostMove = currentMove;
13644     TruncateGame(); // [HGM] vari: MachineWhite and MachineBlack do this...
13645
13646     if(!ResurrectChessProgram()) return;   /* in case first program isn't running (unbalances its ping due to InitChessProgram!) */
13647
13648     if(WaitForEngine(&second, TwoMachinesEventIfReady)) return; // (if needed:) started up second engine, so wait for features
13649     if(first.lastPing != first.lastPong) { // [HGM] wait till we are sure first engine has set up position
13650       ScheduleDelayedEvent(TwoMachinesEventIfReady, 10);
13651       return;
13652     }
13653     if(!stalling) {
13654       InitChessProgram(&second, FALSE); // unbalances ping of second engine
13655       SendToProgram("force\n", &second);
13656       stalling = 1;
13657       ScheduleDelayedEvent(TwoMachinesEventIfReady, 10);
13658       return;
13659     }
13660     GetTimeMark(&now); // [HGM] matchpause: implement match pause after engine load
13661     if(appData.matchPause>10000 || appData.matchPause<10)
13662                 appData.matchPause = 10000; /* [HGM] make pause adjustable */
13663     wait = SubtractTimeMarks(&now, &pauseStart);
13664     if(wait < appData.matchPause) {
13665         ScheduleDelayedEvent(TwoMachinesEventIfReady, appData.matchPause - wait);
13666         return;
13667     }
13668     // we are now committed to starting the game
13669     stalling = 0;
13670     DisplayMessage("", "");
13671     if (startedFromSetupPosition) {
13672         SendBoard(&second, backwardMostMove);
13673     if (appData.debugMode) {
13674         fprintf(debugFP, "Two Machines\n");
13675     }
13676     }
13677     for (i = backwardMostMove; i < forwardMostMove; i++) {
13678         SendMoveToProgram(i, &second);
13679     }
13680
13681     gameMode = TwoMachinesPlay;
13682     pausing = FALSE;
13683     ModeHighlight(); // [HGM] logo: this triggers display update of logos
13684     SetGameInfo();
13685     DisplayTwoMachinesTitle();
13686     firstMove = TRUE;
13687     if ((first.twoMachinesColor[0] == 'w') == WhiteOnMove(forwardMostMove)) {
13688         onmove = &first;
13689     } else {
13690         onmove = &second;
13691     }
13692     if(appData.debugMode) fprintf(debugFP, "New game (%d): %s-%s (%c)\n", matchGame, first.tidy, second.tidy, first.twoMachinesColor[0]);
13693     SendToProgram(first.computerString, &first);
13694     if (first.sendName) {
13695       snprintf(buf, MSG_SIZ, "name %s\n", second.tidy);
13696       SendToProgram(buf, &first);
13697     }
13698     SendToProgram(second.computerString, &second);
13699     if (second.sendName) {
13700       snprintf(buf, MSG_SIZ, "name %s\n", first.tidy);
13701       SendToProgram(buf, &second);
13702     }
13703
13704     ResetClocks();
13705     if (!first.sendTime || !second.sendTime) {
13706         timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
13707         timeRemaining[1][forwardMostMove] = blackTimeRemaining;
13708     }
13709     if (onmove->sendTime) {
13710       if (onmove->useColors) {
13711         SendToProgram(onmove->other->twoMachinesColor, onmove); /*gnu kludge*/
13712       }
13713       SendTimeRemaining(onmove, WhiteOnMove(forwardMostMove));
13714     }
13715     if (onmove->useColors) {
13716       SendToProgram(onmove->twoMachinesColor, onmove);
13717     }
13718     bookHit = SendMoveToBookUser(forwardMostMove-1, onmove, TRUE); // [HGM] book: send go or retrieve book move
13719 //    SendToProgram("go\n", onmove);
13720     onmove->maybeThinking = TRUE;
13721     SetMachineThinkingEnables();
13722
13723     StartClocks();
13724
13725     if(bookHit) { // [HGM] book: simulate book reply
13726         static char bookMove[MSG_SIZ]; // a bit generous?
13727
13728         programStats.nodes = programStats.depth = programStats.time =
13729         programStats.score = programStats.got_only_move = 0;
13730         sprintf(programStats.movelist, "%s (xbook)", bookHit);
13731
13732         safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
13733         strcat(bookMove, bookHit);
13734         savedMessage = bookMove; // args for deferred call
13735         savedState = onmove;
13736         ScheduleDelayedEvent(DeferredBookMove, 1);
13737     }
13738 }
13739
13740 void
13741 TrainingEvent ()
13742 {
13743     if (gameMode == Training) {
13744       SetTrainingModeOff();
13745       gameMode = PlayFromGameFile;
13746       DisplayMessage("", _("Training mode off"));
13747     } else {
13748       gameMode = Training;
13749       animateTraining = appData.animate;
13750
13751       /* make sure we are not already at the end of the game */
13752       if (currentMove < forwardMostMove) {
13753         SetTrainingModeOn();
13754         DisplayMessage("", _("Training mode on"));
13755       } else {
13756         gameMode = PlayFromGameFile;
13757         DisplayError(_("Already at end of game"), 0);
13758       }
13759     }
13760     ModeHighlight();
13761 }
13762
13763 void
13764 IcsClientEvent ()
13765 {
13766     if (!appData.icsActive) return;
13767     switch (gameMode) {
13768       case IcsPlayingWhite:
13769       case IcsPlayingBlack:
13770       case IcsObserving:
13771       case IcsIdle:
13772       case BeginningOfGame:
13773       case IcsExamining:
13774         return;
13775
13776       case EditGame:
13777         break;
13778
13779       case EditPosition:
13780         EditPositionDone(TRUE);
13781         break;
13782
13783       case AnalyzeMode:
13784       case AnalyzeFile:
13785         ExitAnalyzeMode();
13786         break;
13787
13788       default:
13789         EditGameEvent();
13790         break;
13791     }
13792
13793     gameMode = IcsIdle;
13794     ModeHighlight();
13795     return;
13796 }
13797
13798 void
13799 EditGameEvent ()
13800 {
13801     int i;
13802
13803     switch (gameMode) {
13804       case Training:
13805         SetTrainingModeOff();
13806         break;
13807       case MachinePlaysWhite:
13808       case MachinePlaysBlack:
13809       case BeginningOfGame:
13810         SendToProgram("force\n", &first);
13811         SetUserThinkingEnables();
13812         break;
13813       case PlayFromGameFile:
13814         (void) StopLoadGameTimer();
13815         if (gameFileFP != NULL) {
13816             gameFileFP = NULL;
13817         }
13818         break;
13819       case EditPosition:
13820         EditPositionDone(TRUE);
13821         break;
13822       case AnalyzeMode:
13823       case AnalyzeFile:
13824         ExitAnalyzeMode();
13825         SendToProgram("force\n", &first);
13826         break;
13827       case TwoMachinesPlay:
13828         GameEnds(EndOfFile, NULL, GE_PLAYER);
13829         ResurrectChessProgram();
13830         SetUserThinkingEnables();
13831         break;
13832       case EndOfGame:
13833         ResurrectChessProgram();
13834         break;
13835       case IcsPlayingBlack:
13836       case IcsPlayingWhite:
13837         DisplayError(_("Warning: You are still playing a game"), 0);
13838         break;
13839       case IcsObserving:
13840         DisplayError(_("Warning: You are still observing a game"), 0);
13841         break;
13842       case IcsExamining:
13843         DisplayError(_("Warning: You are still examining a game"), 0);
13844         break;
13845       case IcsIdle:
13846         break;
13847       case EditGame:
13848       default:
13849         return;
13850     }
13851
13852     pausing = FALSE;
13853     StopClocks();
13854     first.offeredDraw = second.offeredDraw = 0;
13855
13856     if (gameMode == PlayFromGameFile) {
13857         whiteTimeRemaining = timeRemaining[0][currentMove];
13858         blackTimeRemaining = timeRemaining[1][currentMove];
13859         DisplayTitle("");
13860     }
13861
13862     if (gameMode == MachinePlaysWhite ||
13863         gameMode == MachinePlaysBlack ||
13864         gameMode == TwoMachinesPlay ||
13865         gameMode == EndOfGame) {
13866         i = forwardMostMove;
13867         while (i > currentMove) {
13868             SendToProgram("undo\n", &first);
13869             i--;
13870         }
13871         if(!adjustedClock) {
13872         whiteTimeRemaining = timeRemaining[0][currentMove];
13873         blackTimeRemaining = timeRemaining[1][currentMove];
13874         DisplayBothClocks();
13875         }
13876         if (whiteFlag || blackFlag) {
13877             whiteFlag = blackFlag = 0;
13878         }
13879         DisplayTitle("");
13880     }
13881
13882     gameMode = EditGame;
13883     ModeHighlight();
13884     SetGameInfo();
13885 }
13886
13887
13888 void
13889 EditPositionEvent ()
13890 {
13891     if (gameMode == EditPosition) {
13892         EditGameEvent();
13893         return;
13894     }
13895
13896     EditGameEvent();
13897     if (gameMode != EditGame) return;
13898
13899     gameMode = EditPosition;
13900     ModeHighlight();
13901     SetGameInfo();
13902     if (currentMove > 0)
13903       CopyBoard(boards[0], boards[currentMove]);
13904
13905     blackPlaysFirst = !WhiteOnMove(currentMove);
13906     ResetClocks();
13907     currentMove = forwardMostMove = backwardMostMove = 0;
13908     HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
13909     DisplayMove(-1);
13910     if(!appData.pieceMenu) DisplayMessage(_("Click clock to clear board"), "");
13911 }
13912
13913 void
13914 ExitAnalyzeMode ()
13915 {
13916     /* [DM] icsEngineAnalyze - possible call from other functions */
13917     if (appData.icsEngineAnalyze) {
13918         appData.icsEngineAnalyze = FALSE;
13919
13920         DisplayMessage("",_("Close ICS engine analyze..."));
13921     }
13922     if (first.analysisSupport && first.analyzing) {
13923       SendToProgram("exit\n", &first);
13924       first.analyzing = FALSE;
13925     }
13926     thinkOutput[0] = NULLCHAR;
13927 }
13928
13929 void
13930 EditPositionDone (Boolean fakeRights)
13931 {
13932     int king = gameInfo.variant == VariantKnightmate ? WhiteUnicorn : WhiteKing;
13933
13934     startedFromSetupPosition = TRUE;
13935     InitChessProgram(&first, FALSE);
13936     if(fakeRights) { // [HGM] suppress this if we just pasted a FEN.
13937       boards[0][EP_STATUS] = EP_NONE;
13938       boards[0][CASTLING][2] = boards[0][CASTLING][5] = BOARD_WIDTH>>1;
13939     if(boards[0][0][BOARD_WIDTH>>1] == king) {
13940         boards[0][CASTLING][1] = boards[0][0][BOARD_LEFT] == WhiteRook ? 0 : NoRights;
13941         boards[0][CASTLING][0] = boards[0][0][BOARD_RGHT-1] == WhiteRook ? BOARD_RGHT-1 : NoRights;
13942       } else boards[0][CASTLING][2] = NoRights;
13943     if(boards[0][BOARD_HEIGHT-1][BOARD_WIDTH>>1] == WHITE_TO_BLACK king) {
13944         boards[0][CASTLING][4] = boards[0][BOARD_HEIGHT-1][BOARD_LEFT] == BlackRook ? 0 : NoRights;
13945         boards[0][CASTLING][3] = boards[0][BOARD_HEIGHT-1][BOARD_RGHT-1] == BlackRook ? BOARD_RGHT-1 : NoRights;
13946       } else boards[0][CASTLING][5] = NoRights;
13947     }
13948     SendToProgram("force\n", &first);
13949     if (blackPlaysFirst) {
13950         safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
13951         safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
13952         currentMove = forwardMostMove = backwardMostMove = 1;
13953         CopyBoard(boards[1], boards[0]);
13954     } else {
13955         currentMove = forwardMostMove = backwardMostMove = 0;
13956     }
13957     SendBoard(&first, forwardMostMove);
13958     if (appData.debugMode) {
13959         fprintf(debugFP, "EditPosDone\n");
13960     }
13961     DisplayTitle("");
13962     timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
13963     timeRemaining[1][forwardMostMove] = blackTimeRemaining;
13964     gameMode = EditGame;
13965     ModeHighlight();
13966     HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
13967     ClearHighlights(); /* [AS] */
13968 }
13969
13970 /* Pause for `ms' milliseconds */
13971 /* !! Ugh, this is a kludge. Fix it sometime. --tpm */
13972 void
13973 TimeDelay (long ms)
13974 {
13975     TimeMark m1, m2;
13976
13977     GetTimeMark(&m1);
13978     do {
13979         GetTimeMark(&m2);
13980     } while (SubtractTimeMarks(&m2, &m1) < ms);
13981 }
13982
13983 /* !! Ugh, this is a kludge. Fix it sometime. --tpm */
13984 void
13985 SendMultiLineToICS (char *buf)
13986 {
13987     char temp[MSG_SIZ+1], *p;
13988     int len;
13989
13990     len = strlen(buf);
13991     if (len > MSG_SIZ)
13992       len = MSG_SIZ;
13993
13994     strncpy(temp, buf, len);
13995     temp[len] = 0;
13996
13997     p = temp;
13998     while (*p) {
13999         if (*p == '\n' || *p == '\r')
14000           *p = ' ';
14001         ++p;
14002     }
14003
14004     strcat(temp, "\n");
14005     SendToICS(temp);
14006     SendToPlayer(temp, strlen(temp));
14007 }
14008
14009 void
14010 SetWhiteToPlayEvent ()
14011 {
14012     if (gameMode == EditPosition) {
14013         blackPlaysFirst = FALSE;
14014         DisplayBothClocks();    /* works because currentMove is 0 */
14015     } else if (gameMode == IcsExamining) {
14016         SendToICS(ics_prefix);
14017         SendToICS("tomove white\n");
14018     }
14019 }
14020
14021 void
14022 SetBlackToPlayEvent ()
14023 {
14024     if (gameMode == EditPosition) {
14025         blackPlaysFirst = TRUE;
14026         currentMove = 1;        /* kludge */
14027         DisplayBothClocks();
14028         currentMove = 0;
14029     } else if (gameMode == IcsExamining) {
14030         SendToICS(ics_prefix);
14031         SendToICS("tomove black\n");
14032     }
14033 }
14034
14035 void
14036 EditPositionMenuEvent (ChessSquare selection, int x, int y)
14037 {
14038     char buf[MSG_SIZ];
14039     ChessSquare piece = boards[0][y][x];
14040
14041     if (gameMode != EditPosition && gameMode != IcsExamining) return;
14042
14043     switch (selection) {
14044       case ClearBoard:
14045         if (gameMode == IcsExamining && ics_type == ICS_FICS) {
14046             SendToICS(ics_prefix);
14047             SendToICS("bsetup clear\n");
14048         } else if (gameMode == IcsExamining && ics_type == ICS_ICC) {
14049             SendToICS(ics_prefix);
14050             SendToICS("clearboard\n");
14051         } else {
14052             for (x = 0; x < BOARD_WIDTH; x++) { ChessSquare p = EmptySquare;
14053                 if(x == BOARD_LEFT-1 || x == BOARD_RGHT) p = (ChessSquare) 0; /* [HGM] holdings */
14054                 for (y = 0; y < BOARD_HEIGHT; y++) {
14055                     if (gameMode == IcsExamining) {
14056                         if (boards[currentMove][y][x] != EmptySquare) {
14057                           snprintf(buf, MSG_SIZ, "%sx@%c%c\n", ics_prefix,
14058                                     AAA + x, ONE + y);
14059                             SendToICS(buf);
14060                         }
14061                     } else {
14062                         boards[0][y][x] = p;
14063                     }
14064                 }
14065             }
14066         }
14067         if (gameMode == EditPosition) {
14068             DrawPosition(FALSE, boards[0]);
14069         }
14070         break;
14071
14072       case WhitePlay:
14073         SetWhiteToPlayEvent();
14074         break;
14075
14076       case BlackPlay:
14077         SetBlackToPlayEvent();
14078         break;
14079
14080       case EmptySquare:
14081         if (gameMode == IcsExamining) {
14082             if (x < BOARD_LEFT || x >= BOARD_RGHT) break; // [HGM] holdings
14083             snprintf(buf, MSG_SIZ, "%sx@%c%c\n", ics_prefix, AAA + x, ONE + y);
14084             SendToICS(buf);
14085         } else {
14086             if(x < BOARD_LEFT || x >= BOARD_RGHT) {
14087                 if(x == BOARD_LEFT-2) {
14088                     if(y < BOARD_HEIGHT-1-gameInfo.holdingsSize) break;
14089                     boards[0][y][1] = 0;
14090                 } else
14091                 if(x == BOARD_RGHT+1) {
14092                     if(y >= gameInfo.holdingsSize) break;
14093                     boards[0][y][BOARD_WIDTH-2] = 0;
14094                 } else break;
14095             }
14096             boards[0][y][x] = EmptySquare;
14097             DrawPosition(FALSE, boards[0]);
14098         }
14099         break;
14100
14101       case PromotePiece:
14102         if(piece >= (int)WhitePawn && piece < (int)WhiteMan ||
14103            piece >= (int)BlackPawn && piece < (int)BlackMan   ) {
14104             selection = (ChessSquare) (PROMOTED piece);
14105         } else if(piece == EmptySquare) selection = WhiteSilver;
14106         else selection = (ChessSquare)((int)piece - 1);
14107         goto defaultlabel;
14108
14109       case DemotePiece:
14110         if(piece > (int)WhiteMan && piece <= (int)WhiteKing ||
14111            piece > (int)BlackMan && piece <= (int)BlackKing   ) {
14112             selection = (ChessSquare) (DEMOTED piece);
14113         } else if(piece == EmptySquare) selection = BlackSilver;
14114         else selection = (ChessSquare)((int)piece + 1);
14115         goto defaultlabel;
14116
14117       case WhiteQueen:
14118       case BlackQueen:
14119         if(gameInfo.variant == VariantShatranj ||
14120            gameInfo.variant == VariantXiangqi  ||
14121            gameInfo.variant == VariantCourier  ||
14122            gameInfo.variant == VariantMakruk     )
14123             selection = (ChessSquare)((int)selection - (int)WhiteQueen + (int)WhiteFerz);
14124         goto defaultlabel;
14125
14126       case WhiteKing:
14127       case BlackKing:
14128         if(gameInfo.variant == VariantXiangqi)
14129             selection = (ChessSquare)((int)selection - (int)WhiteKing + (int)WhiteWazir);
14130         if(gameInfo.variant == VariantKnightmate)
14131             selection = (ChessSquare)((int)selection - (int)WhiteKing + (int)WhiteUnicorn);
14132       default:
14133         defaultlabel:
14134         if (gameMode == IcsExamining) {
14135             if (x < BOARD_LEFT || x >= BOARD_RGHT) break; // [HGM] holdings
14136             snprintf(buf, MSG_SIZ, "%s%c@%c%c\n", ics_prefix,
14137                      PieceToChar(selection), AAA + x, ONE + y);
14138             SendToICS(buf);
14139         } else {
14140             if(x < BOARD_LEFT || x >= BOARD_RGHT) {
14141                 int n;
14142                 if(x == BOARD_LEFT-2 && selection >= BlackPawn) {
14143                     n = PieceToNumber(selection - BlackPawn);
14144                     if(n >= gameInfo.holdingsSize) { n = 0; selection = BlackPawn; }
14145                     boards[0][BOARD_HEIGHT-1-n][0] = selection;
14146                     boards[0][BOARD_HEIGHT-1-n][1]++;
14147                 } else
14148                 if(x == BOARD_RGHT+1 && selection < BlackPawn) {
14149                     n = PieceToNumber(selection);
14150                     if(n >= gameInfo.holdingsSize) { n = 0; selection = WhitePawn; }
14151                     boards[0][n][BOARD_WIDTH-1] = selection;
14152                     boards[0][n][BOARD_WIDTH-2]++;
14153                 }
14154             } else
14155             boards[0][y][x] = selection;
14156             DrawPosition(TRUE, boards[0]);
14157             ClearHighlights();
14158             fromX = fromY = -1;
14159         }
14160         break;
14161     }
14162 }
14163
14164
14165 void
14166 DropMenuEvent (ChessSquare selection, int x, int y)
14167 {
14168     ChessMove moveType;
14169
14170     switch (gameMode) {
14171       case IcsPlayingWhite:
14172       case MachinePlaysBlack:
14173         if (!WhiteOnMove(currentMove)) {
14174             DisplayMoveError(_("It is Black's turn"));
14175             return;
14176         }
14177         moveType = WhiteDrop;
14178         break;
14179       case IcsPlayingBlack:
14180       case MachinePlaysWhite:
14181         if (WhiteOnMove(currentMove)) {
14182             DisplayMoveError(_("It is White's turn"));
14183             return;
14184         }
14185         moveType = BlackDrop;
14186         break;
14187       case EditGame:
14188         moveType = WhiteOnMove(currentMove) ? WhiteDrop : BlackDrop;
14189         break;
14190       default:
14191         return;
14192     }
14193
14194     if (moveType == BlackDrop && selection < BlackPawn) {
14195       selection = (ChessSquare) ((int) selection
14196                                  + (int) BlackPawn - (int) WhitePawn);
14197     }
14198     if (boards[currentMove][y][x] != EmptySquare) {
14199         DisplayMoveError(_("That square is occupied"));
14200         return;
14201     }
14202
14203     FinishMove(moveType, (int) selection, DROP_RANK, x, y, NULLCHAR);
14204 }
14205
14206 void
14207 AcceptEvent ()
14208 {
14209     /* Accept a pending offer of any kind from opponent */
14210
14211     if (appData.icsActive) {
14212         SendToICS(ics_prefix);
14213         SendToICS("accept\n");
14214     } else if (cmailMsgLoaded) {
14215         if (currentMove == cmailOldMove &&
14216             commentList[cmailOldMove] != NULL &&
14217             StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
14218                    "Black offers a draw" : "White offers a draw")) {
14219             TruncateGame();
14220             GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
14221             cmailMoveType[lastLoadGameNumber - 1] = CMAIL_ACCEPT;
14222         } else {
14223             DisplayError(_("There is no pending offer on this move"), 0);
14224             cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
14225         }
14226     } else {
14227         /* Not used for offers from chess program */
14228     }
14229 }
14230
14231 void
14232 DeclineEvent ()
14233 {
14234     /* Decline a pending offer of any kind from opponent */
14235
14236     if (appData.icsActive) {
14237         SendToICS(ics_prefix);
14238         SendToICS("decline\n");
14239     } else if (cmailMsgLoaded) {
14240         if (currentMove == cmailOldMove &&
14241             commentList[cmailOldMove] != NULL &&
14242             StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
14243                    "Black offers a draw" : "White offers a draw")) {
14244 #ifdef NOTDEF
14245             AppendComment(cmailOldMove, "Draw declined", TRUE);
14246             DisplayComment(cmailOldMove - 1, "Draw declined");
14247 #endif /*NOTDEF*/
14248         } else {
14249             DisplayError(_("There is no pending offer on this move"), 0);
14250         }
14251     } else {
14252         /* Not used for offers from chess program */
14253     }
14254 }
14255
14256 void
14257 RematchEvent ()
14258 {
14259     /* Issue ICS rematch command */
14260     if (appData.icsActive) {
14261         SendToICS(ics_prefix);
14262         SendToICS("rematch\n");
14263     }
14264 }
14265
14266 void
14267 CallFlagEvent ()
14268 {
14269     /* Call your opponent's flag (claim a win on time) */
14270     if (appData.icsActive) {
14271         SendToICS(ics_prefix);
14272         SendToICS("flag\n");
14273     } else {
14274         switch (gameMode) {
14275           default:
14276             return;
14277           case MachinePlaysWhite:
14278             if (whiteFlag) {
14279                 if (blackFlag)
14280                   GameEnds(GameIsDrawn, "Both players ran out of time",
14281                            GE_PLAYER);
14282                 else
14283                   GameEnds(BlackWins, "Black wins on time", GE_PLAYER);
14284             } else {
14285                 DisplayError(_("Your opponent is not out of time"), 0);
14286             }
14287             break;
14288           case MachinePlaysBlack:
14289             if (blackFlag) {
14290                 if (whiteFlag)
14291                   GameEnds(GameIsDrawn, "Both players ran out of time",
14292                            GE_PLAYER);
14293                 else
14294                   GameEnds(WhiteWins, "White wins on time", GE_PLAYER);
14295             } else {
14296                 DisplayError(_("Your opponent is not out of time"), 0);
14297             }
14298             break;
14299         }
14300     }
14301 }
14302
14303 void
14304 ClockClick (int which)
14305 {       // [HGM] code moved to back-end from winboard.c
14306         if(which) { // black clock
14307           if (gameMode == EditPosition || gameMode == IcsExamining) {
14308             if(!appData.pieceMenu && blackPlaysFirst) EditPositionMenuEvent(ClearBoard, 0, 0);
14309             SetBlackToPlayEvent();
14310           } else if ((gameMode == AnalyzeMode || gameMode == EditGame) && !blackFlag && WhiteOnMove(currentMove)) {
14311           UserMoveEvent((int)EmptySquare, DROP_RANK, 0, 0, 0); // [HGM] multi-move: if not out of time, enters null move
14312           } else if (shiftKey) {
14313             AdjustClock(which, -1);
14314           } else if (gameMode == IcsPlayingWhite ||
14315                      gameMode == MachinePlaysBlack) {
14316             CallFlagEvent();
14317           }
14318         } else { // white clock
14319           if (gameMode == EditPosition || gameMode == IcsExamining) {
14320             if(!appData.pieceMenu && !blackPlaysFirst) EditPositionMenuEvent(ClearBoard, 0, 0);
14321             SetWhiteToPlayEvent();
14322           } else if ((gameMode == AnalyzeMode || gameMode == EditGame) && !whiteFlag && !WhiteOnMove(currentMove)) {
14323           UserMoveEvent((int)EmptySquare, DROP_RANK, 0, 0, 0); // [HGM] multi-move
14324           } else if (shiftKey) {
14325             AdjustClock(which, -1);
14326           } else if (gameMode == IcsPlayingBlack ||
14327                    gameMode == MachinePlaysWhite) {
14328             CallFlagEvent();
14329           }
14330         }
14331 }
14332
14333 void
14334 DrawEvent ()
14335 {
14336     /* Offer draw or accept pending draw offer from opponent */
14337
14338     if (appData.icsActive) {
14339         /* Note: tournament rules require draw offers to be
14340            made after you make your move but before you punch
14341            your clock.  Currently ICS doesn't let you do that;
14342            instead, you immediately punch your clock after making
14343            a move, but you can offer a draw at any time. */
14344
14345         SendToICS(ics_prefix);
14346         SendToICS("draw\n");
14347         userOfferedDraw = TRUE; // [HGM] drawclaim: also set flag in ICS play
14348     } else if (cmailMsgLoaded) {
14349         if (currentMove == cmailOldMove &&
14350             commentList[cmailOldMove] != NULL &&
14351             StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
14352                    "Black offers a draw" : "White offers a draw")) {
14353             GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
14354             cmailMoveType[lastLoadGameNumber - 1] = CMAIL_ACCEPT;
14355         } else if (currentMove == cmailOldMove + 1) {
14356             char *offer = WhiteOnMove(cmailOldMove) ?
14357               "White offers a draw" : "Black offers a draw";
14358             AppendComment(currentMove, offer, TRUE);
14359             DisplayComment(currentMove - 1, offer);
14360             cmailMoveType[lastLoadGameNumber - 1] = CMAIL_DRAW;
14361         } else {
14362             DisplayError(_("You must make your move before offering a draw"), 0);
14363             cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
14364         }
14365     } else if (first.offeredDraw) {
14366         GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
14367     } else {
14368         if (first.sendDrawOffers) {
14369             SendToProgram("draw\n", &first);
14370             userOfferedDraw = TRUE;
14371         }
14372     }
14373 }
14374
14375 void
14376 AdjournEvent ()
14377 {
14378     /* Offer Adjourn or accept pending Adjourn offer from opponent */
14379
14380     if (appData.icsActive) {
14381         SendToICS(ics_prefix);
14382         SendToICS("adjourn\n");
14383     } else {
14384         /* Currently GNU Chess doesn't offer or accept Adjourns */
14385     }
14386 }
14387
14388
14389 void
14390 AbortEvent ()
14391 {
14392     /* Offer Abort or accept pending Abort offer from opponent */
14393
14394     if (appData.icsActive) {
14395         SendToICS(ics_prefix);
14396         SendToICS("abort\n");
14397     } else {
14398         GameEnds(GameUnfinished, "Game aborted", GE_PLAYER);
14399     }
14400 }
14401
14402 void
14403 ResignEvent ()
14404 {
14405     /* Resign.  You can do this even if it's not your turn. */
14406
14407     if (appData.icsActive) {
14408         SendToICS(ics_prefix);
14409         SendToICS("resign\n");
14410     } else {
14411         switch (gameMode) {
14412           case MachinePlaysWhite:
14413             GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
14414             break;
14415           case MachinePlaysBlack:
14416             GameEnds(BlackWins, "White resigns", GE_PLAYER);
14417             break;
14418           case EditGame:
14419             if (cmailMsgLoaded) {
14420                 TruncateGame();
14421                 if (WhiteOnMove(cmailOldMove)) {
14422                     GameEnds(BlackWins, "White resigns", GE_PLAYER);
14423                 } else {
14424                     GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
14425                 }
14426                 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_RESIGN;
14427             }
14428             break;
14429           default:
14430             break;
14431         }
14432     }
14433 }
14434
14435
14436 void
14437 StopObservingEvent ()
14438 {
14439     /* Stop observing current games */
14440     SendToICS(ics_prefix);
14441     SendToICS("unobserve\n");
14442 }
14443
14444 void
14445 StopExaminingEvent ()
14446 {
14447     /* Stop observing current game */
14448     SendToICS(ics_prefix);
14449     SendToICS("unexamine\n");
14450 }
14451
14452 void
14453 ForwardInner (int target)
14454 {
14455     int limit; int oldSeekGraphUp = seekGraphUp;
14456
14457     if (appData.debugMode)
14458         fprintf(debugFP, "ForwardInner(%d), current %d, forward %d\n",
14459                 target, currentMove, forwardMostMove);
14460
14461     if (gameMode == EditPosition)
14462       return;
14463
14464     seekGraphUp = FALSE;
14465     MarkTargetSquares(1);
14466
14467     if (gameMode == PlayFromGameFile && !pausing)
14468       PauseEvent();
14469
14470     if (gameMode == IcsExamining && pausing)
14471       limit = pauseExamForwardMostMove;
14472     else
14473       limit = forwardMostMove;
14474
14475     if (target > limit) target = limit;
14476
14477     if (target > 0 && moveList[target - 1][0]) {
14478         int fromX, fromY, toX, toY;
14479         toX = moveList[target - 1][2] - AAA;
14480         toY = moveList[target - 1][3] - ONE;
14481         if (moveList[target - 1][1] == '@') {
14482             if (appData.highlightLastMove) {
14483                 SetHighlights(-1, -1, toX, toY);
14484             }
14485         } else {
14486             fromX = moveList[target - 1][0] - AAA;
14487             fromY = moveList[target - 1][1] - ONE;
14488             if (target == currentMove + 1) {
14489                 AnimateMove(boards[currentMove], fromX, fromY, toX, toY);
14490             }
14491             if (appData.highlightLastMove) {
14492                 SetHighlights(fromX, fromY, toX, toY);
14493             }
14494         }
14495     }
14496     if (gameMode == EditGame || gameMode == AnalyzeMode ||
14497         gameMode == Training || gameMode == PlayFromGameFile ||
14498         gameMode == AnalyzeFile) {
14499         while (currentMove < target) {
14500             SendMoveToProgram(currentMove++, &first);
14501         }
14502     } else {
14503         currentMove = target;
14504     }
14505
14506     if (gameMode == EditGame || gameMode == EndOfGame) {
14507         whiteTimeRemaining = timeRemaining[0][currentMove];
14508         blackTimeRemaining = timeRemaining[1][currentMove];
14509     }
14510     DisplayBothClocks();
14511     DisplayMove(currentMove - 1);
14512     DrawPosition(oldSeekGraphUp, boards[currentMove]);
14513     HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
14514     if ( !matchMode && gameMode != Training) { // [HGM] PV info: routine tests if empty
14515         DisplayComment(currentMove - 1, commentList[currentMove]);
14516     }
14517     ClearMap(); // [HGM] exclude: invalidate map
14518 }
14519
14520
14521 void
14522 ForwardEvent ()
14523 {
14524     if (gameMode == IcsExamining && !pausing) {
14525         SendToICS(ics_prefix);
14526         SendToICS("forward\n");
14527     } else {
14528         ForwardInner(currentMove + 1);
14529     }
14530 }
14531
14532 void
14533 ToEndEvent ()
14534 {
14535     if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
14536         /* to optimze, we temporarily turn off analysis mode while we feed
14537          * the remaining moves to the engine. Otherwise we get analysis output
14538          * after each move.
14539          */
14540         if (first.analysisSupport) {
14541           SendToProgram("exit\nforce\n", &first);
14542           first.analyzing = FALSE;
14543         }
14544     }
14545
14546     if (gameMode == IcsExamining && !pausing) {
14547         SendToICS(ics_prefix);
14548         SendToICS("forward 999999\n");
14549     } else {
14550         ForwardInner(forwardMostMove);
14551     }
14552
14553     if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
14554         /* we have fed all the moves, so reactivate analysis mode */
14555         SendToProgram("analyze\n", &first);
14556         first.analyzing = TRUE;
14557         /*first.maybeThinking = TRUE;*/
14558         first.maybeThinking = FALSE; /* avoid killing GNU Chess */
14559     }
14560 }
14561
14562 void
14563 BackwardInner (int target)
14564 {
14565     int full_redraw = TRUE; /* [AS] Was FALSE, had to change it! */
14566
14567     if (appData.debugMode)
14568         fprintf(debugFP, "BackwardInner(%d), current %d, forward %d\n",
14569                 target, currentMove, forwardMostMove);
14570
14571     if (gameMode == EditPosition) return;
14572     seekGraphUp = FALSE;
14573     MarkTargetSquares(1);
14574     if (currentMove <= backwardMostMove) {
14575         ClearHighlights();
14576         DrawPosition(full_redraw, boards[currentMove]);
14577         return;
14578     }
14579     if (gameMode == PlayFromGameFile && !pausing)
14580       PauseEvent();
14581
14582     if (moveList[target][0]) {
14583         int fromX, fromY, toX, toY;
14584         toX = moveList[target][2] - AAA;
14585         toY = moveList[target][3] - ONE;
14586         if (moveList[target][1] == '@') {
14587             if (appData.highlightLastMove) {
14588                 SetHighlights(-1, -1, toX, toY);
14589             }
14590         } else {
14591             fromX = moveList[target][0] - AAA;
14592             fromY = moveList[target][1] - ONE;
14593             if (target == currentMove - 1) {
14594                 AnimateMove(boards[currentMove], toX, toY, fromX, fromY);
14595             }
14596             if (appData.highlightLastMove) {
14597                 SetHighlights(fromX, fromY, toX, toY);
14598             }
14599         }
14600     }
14601     if (gameMode == EditGame || gameMode==AnalyzeMode ||
14602         gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
14603         while (currentMove > target) {
14604             if(moveList[currentMove-1][1] == '@' && moveList[currentMove-1][0] == '@') {
14605                 // null move cannot be undone. Reload program with move history before it.
14606                 int i;
14607                 for(i=target; i>backwardMostMove; i--) { // seek back to start or previous null move
14608                     if(moveList[i-1][1] == '@' && moveList[i-1][0] == '@') break;
14609                 }
14610                 SendBoard(&first, i); 
14611                 for(currentMove=i; currentMove<target; currentMove++) SendMoveToProgram(currentMove, &first);
14612                 break;
14613             }
14614             SendToProgram("undo\n", &first);
14615             currentMove--;
14616         }
14617     } else {
14618         currentMove = target;
14619     }
14620
14621     if (gameMode == EditGame || gameMode == EndOfGame) {
14622         whiteTimeRemaining = timeRemaining[0][currentMove];
14623         blackTimeRemaining = timeRemaining[1][currentMove];
14624     }
14625     DisplayBothClocks();
14626     DisplayMove(currentMove - 1);
14627     DrawPosition(full_redraw, boards[currentMove]);
14628     HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
14629     // [HGM] PV info: routine tests if comment empty
14630     DisplayComment(currentMove - 1, commentList[currentMove]);
14631     ClearMap(); // [HGM] exclude: invalidate map
14632 }
14633
14634 void
14635 BackwardEvent ()
14636 {
14637     if (gameMode == IcsExamining && !pausing) {
14638         SendToICS(ics_prefix);
14639         SendToICS("backward\n");
14640     } else {
14641         BackwardInner(currentMove - 1);
14642     }
14643 }
14644
14645 void
14646 ToStartEvent ()
14647 {
14648     if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
14649         /* to optimize, we temporarily turn off analysis mode while we undo
14650          * all the moves. Otherwise we get analysis output after each undo.
14651          */
14652         if (first.analysisSupport) {
14653           SendToProgram("exit\nforce\n", &first);
14654           first.analyzing = FALSE;
14655         }
14656     }
14657
14658     if (gameMode == IcsExamining && !pausing) {
14659         SendToICS(ics_prefix);
14660         SendToICS("backward 999999\n");
14661     } else {
14662         BackwardInner(backwardMostMove);
14663     }
14664
14665     if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
14666         /* we have fed all the moves, so reactivate analysis mode */
14667         SendToProgram("analyze\n", &first);
14668         first.analyzing = TRUE;
14669         /*first.maybeThinking = TRUE;*/
14670         first.maybeThinking = FALSE; /* avoid killing GNU Chess */
14671     }
14672 }
14673
14674 void
14675 ToNrEvent (int to)
14676 {
14677   if (gameMode == PlayFromGameFile && !pausing) PauseEvent();
14678   if (to >= forwardMostMove) to = forwardMostMove;
14679   if (to <= backwardMostMove) to = backwardMostMove;
14680   if (to < currentMove) {
14681     BackwardInner(to);
14682   } else {
14683     ForwardInner(to);
14684   }
14685 }
14686
14687 void
14688 RevertEvent (Boolean annotate)
14689 {
14690     if(PopTail(annotate)) { // [HGM] vari: restore old game tail
14691         return;
14692     }
14693     if (gameMode != IcsExamining) {
14694         DisplayError(_("You are not examining a game"), 0);
14695         return;
14696     }
14697     if (pausing) {
14698         DisplayError(_("You can't revert while pausing"), 0);
14699         return;
14700     }
14701     SendToICS(ics_prefix);
14702     SendToICS("revert\n");
14703 }
14704
14705 void
14706 RetractMoveEvent ()
14707 {
14708     switch (gameMode) {
14709       case MachinePlaysWhite:
14710       case MachinePlaysBlack:
14711         if (WhiteOnMove(forwardMostMove) == (gameMode == MachinePlaysWhite)) {
14712             DisplayError(_("Wait until your turn,\nor select Move Now"), 0);
14713             return;
14714         }
14715         if (forwardMostMove < 2) return;
14716         currentMove = forwardMostMove = forwardMostMove - 2;
14717         whiteTimeRemaining = timeRemaining[0][currentMove];
14718         blackTimeRemaining = timeRemaining[1][currentMove];
14719         DisplayBothClocks();
14720         DisplayMove(currentMove - 1);
14721         ClearHighlights();/*!! could figure this out*/
14722         DrawPosition(TRUE, boards[currentMove]); /* [AS] Changed to full redraw! */
14723         SendToProgram("remove\n", &first);
14724         /*first.maybeThinking = TRUE;*/ /* GNU Chess does not ponder here */
14725         break;
14726
14727       case BeginningOfGame:
14728       default:
14729         break;
14730
14731       case IcsPlayingWhite:
14732       case IcsPlayingBlack:
14733         if (WhiteOnMove(forwardMostMove) == (gameMode == IcsPlayingWhite)) {
14734             SendToICS(ics_prefix);
14735             SendToICS("takeback 2\n");
14736         } else {
14737             SendToICS(ics_prefix);
14738             SendToICS("takeback 1\n");
14739         }
14740         break;
14741     }
14742 }
14743
14744 void
14745 MoveNowEvent ()
14746 {
14747     ChessProgramState *cps;
14748
14749     switch (gameMode) {
14750       case MachinePlaysWhite:
14751         if (!WhiteOnMove(forwardMostMove)) {
14752             DisplayError(_("It is your turn"), 0);
14753             return;
14754         }
14755         cps = &first;
14756         break;
14757       case MachinePlaysBlack:
14758         if (WhiteOnMove(forwardMostMove)) {
14759             DisplayError(_("It is your turn"), 0);
14760             return;
14761         }
14762         cps = &first;
14763         break;
14764       case TwoMachinesPlay:
14765         if (WhiteOnMove(forwardMostMove) ==
14766             (first.twoMachinesColor[0] == 'w')) {
14767             cps = &first;
14768         } else {
14769             cps = &second;
14770         }
14771         break;
14772       case BeginningOfGame:
14773       default:
14774         return;
14775     }
14776     SendToProgram("?\n", cps);
14777 }
14778
14779 void
14780 TruncateGameEvent ()
14781 {
14782     EditGameEvent();
14783     if (gameMode != EditGame) return;
14784     TruncateGame();
14785 }
14786
14787 void
14788 TruncateGame ()
14789 {
14790     CleanupTail(); // [HGM] vari: only keep current variation if we explicitly truncate
14791     if (forwardMostMove > currentMove) {
14792         if (gameInfo.resultDetails != NULL) {
14793             free(gameInfo.resultDetails);
14794             gameInfo.resultDetails = NULL;
14795             gameInfo.result = GameUnfinished;
14796         }
14797         forwardMostMove = currentMove;
14798         HistorySet(parseList, backwardMostMove, forwardMostMove,
14799                    currentMove-1);
14800     }
14801 }
14802
14803 void
14804 HintEvent ()
14805 {
14806     if (appData.noChessProgram) return;
14807     switch (gameMode) {
14808       case MachinePlaysWhite:
14809         if (WhiteOnMove(forwardMostMove)) {
14810             DisplayError(_("Wait until your turn"), 0);
14811             return;
14812         }
14813         break;
14814       case BeginningOfGame:
14815       case MachinePlaysBlack:
14816         if (!WhiteOnMove(forwardMostMove)) {
14817             DisplayError(_("Wait until your turn"), 0);
14818             return;
14819         }
14820         break;
14821       default:
14822         DisplayError(_("No hint available"), 0);
14823         return;
14824     }
14825     SendToProgram("hint\n", &first);
14826     hintRequested = TRUE;
14827 }
14828
14829 void
14830 BookEvent ()
14831 {
14832     if (appData.noChessProgram) return;
14833     switch (gameMode) {
14834       case MachinePlaysWhite:
14835         if (WhiteOnMove(forwardMostMove)) {
14836             DisplayError(_("Wait until your turn"), 0);
14837             return;
14838         }
14839         break;
14840       case BeginningOfGame:
14841       case MachinePlaysBlack:
14842         if (!WhiteOnMove(forwardMostMove)) {
14843             DisplayError(_("Wait until your turn"), 0);
14844             return;
14845         }
14846         break;
14847       case EditPosition:
14848         EditPositionDone(TRUE);
14849         break;
14850       case TwoMachinesPlay:
14851         return;
14852       default:
14853         break;
14854     }
14855     SendToProgram("bk\n", &first);
14856     bookOutput[0] = NULLCHAR;
14857     bookRequested = TRUE;
14858 }
14859
14860 void
14861 AboutGameEvent ()
14862 {
14863     char *tags = PGNTags(&gameInfo);
14864     TagsPopUp(tags, CmailMsg());
14865     free(tags);
14866 }
14867
14868 /* end button procedures */
14869
14870 void
14871 PrintPosition (FILE *fp, int move)
14872 {
14873     int i, j;
14874
14875     for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
14876         for (j = BOARD_LEFT; j < BOARD_RGHT; j++) {
14877             char c = PieceToChar(boards[move][i][j]);
14878             fputc(c == 'x' ? '.' : c, fp);
14879             fputc(j == BOARD_RGHT - 1 ? '\n' : ' ', fp);
14880         }
14881     }
14882     if ((gameMode == EditPosition) ? !blackPlaysFirst : (move % 2 == 0))
14883       fprintf(fp, "white to play\n");
14884     else
14885       fprintf(fp, "black to play\n");
14886 }
14887
14888 void
14889 PrintOpponents (FILE *fp)
14890 {
14891     if (gameInfo.white != NULL) {
14892         fprintf(fp, "\t%s vs. %s\n", gameInfo.white, gameInfo.black);
14893     } else {
14894         fprintf(fp, "\n");
14895     }
14896 }
14897
14898 /* Find last component of program's own name, using some heuristics */
14899 void
14900 TidyProgramName (char *prog, char *host, char buf[MSG_SIZ])
14901 {
14902     char *p, *q, c;
14903     int local = (strcmp(host, "localhost") == 0);
14904     while (!local && (p = strchr(prog, ';')) != NULL) {
14905         p++;
14906         while (*p == ' ') p++;
14907         prog = p;
14908     }
14909     if (*prog == '"' || *prog == '\'') {
14910         q = strchr(prog + 1, *prog);
14911     } else {
14912         q = strchr(prog, ' ');
14913     }
14914     if (q == NULL) q = prog + strlen(prog);
14915     p = q;
14916     while (p >= prog && *p != '/' && *p != '\\') p--;
14917     p++;
14918     if(p == prog && *p == '"') p++;
14919     c = *q; *q = 0;
14920     if (q - p >= 4 && StrCaseCmp(q - 4, ".exe") == 0) *q = c, q -= 4; else *q = c;
14921     memcpy(buf, p, q - p);
14922     buf[q - p] = NULLCHAR;
14923     if (!local) {
14924         strcat(buf, "@");
14925         strcat(buf, host);
14926     }
14927 }
14928
14929 char *
14930 TimeControlTagValue ()
14931 {
14932     char buf[MSG_SIZ];
14933     if (!appData.clockMode) {
14934       safeStrCpy(buf, "-", sizeof(buf)/sizeof(buf[0]));
14935     } else if (movesPerSession > 0) {
14936       snprintf(buf, MSG_SIZ, "%d/%ld", movesPerSession, timeControl/1000);
14937     } else if (timeIncrement == 0) {
14938       snprintf(buf, MSG_SIZ, "%ld", timeControl/1000);
14939     } else {
14940       snprintf(buf, MSG_SIZ, "%ld+%ld", timeControl/1000, timeIncrement/1000);
14941     }
14942     return StrSave(buf);
14943 }
14944
14945 void
14946 SetGameInfo ()
14947 {
14948     /* This routine is used only for certain modes */
14949     VariantClass v = gameInfo.variant;
14950     ChessMove r = GameUnfinished;
14951     char *p = NULL;
14952
14953     if(gameMode == EditGame) { // [HGM] vari: do not erase result on EditGame
14954         r = gameInfo.result;
14955         p = gameInfo.resultDetails;
14956         gameInfo.resultDetails = NULL;
14957     }
14958     ClearGameInfo(&gameInfo);
14959     gameInfo.variant = v;
14960
14961     switch (gameMode) {
14962       case MachinePlaysWhite:
14963         gameInfo.event = StrSave( appData.pgnEventHeader );
14964         gameInfo.site = StrSave(HostName());
14965         gameInfo.date = PGNDate();
14966         gameInfo.round = StrSave("-");
14967         gameInfo.white = StrSave(first.tidy);
14968         gameInfo.black = StrSave(UserName());
14969         gameInfo.timeControl = TimeControlTagValue();
14970         break;
14971
14972       case MachinePlaysBlack:
14973         gameInfo.event = StrSave( appData.pgnEventHeader );
14974         gameInfo.site = StrSave(HostName());
14975         gameInfo.date = PGNDate();
14976         gameInfo.round = StrSave("-");
14977         gameInfo.white = StrSave(UserName());
14978         gameInfo.black = StrSave(first.tidy);
14979         gameInfo.timeControl = TimeControlTagValue();
14980         break;
14981
14982       case TwoMachinesPlay:
14983         gameInfo.event = StrSave( appData.pgnEventHeader );
14984         gameInfo.site = StrSave(HostName());
14985         gameInfo.date = PGNDate();
14986         if (roundNr > 0) {
14987             char buf[MSG_SIZ];
14988             snprintf(buf, MSG_SIZ, "%d", roundNr);
14989             gameInfo.round = StrSave(buf);
14990         } else {
14991             gameInfo.round = StrSave("-");
14992         }
14993         if (first.twoMachinesColor[0] == 'w') {
14994             gameInfo.white = StrSave(appData.pgnName[0][0] ? appData.pgnName[0] : first.tidy);
14995             gameInfo.black = StrSave(appData.pgnName[1][0] ? appData.pgnName[1] : second.tidy);
14996         } else {
14997             gameInfo.white = StrSave(appData.pgnName[1][0] ? appData.pgnName[1] : second.tidy);
14998             gameInfo.black = StrSave(appData.pgnName[0][0] ? appData.pgnName[0] : first.tidy);
14999         }
15000         gameInfo.timeControl = TimeControlTagValue();
15001         break;
15002
15003       case EditGame:
15004         gameInfo.event = StrSave("Edited game");
15005         gameInfo.site = StrSave(HostName());
15006         gameInfo.date = PGNDate();
15007         gameInfo.round = StrSave("-");
15008         gameInfo.white = StrSave("-");
15009         gameInfo.black = StrSave("-");
15010         gameInfo.result = r;
15011         gameInfo.resultDetails = p;
15012         break;
15013
15014       case EditPosition:
15015         gameInfo.event = StrSave("Edited position");
15016         gameInfo.site = StrSave(HostName());
15017         gameInfo.date = PGNDate();
15018         gameInfo.round = StrSave("-");
15019         gameInfo.white = StrSave("-");
15020         gameInfo.black = StrSave("-");
15021         break;
15022
15023       case IcsPlayingWhite:
15024       case IcsPlayingBlack:
15025       case IcsObserving:
15026       case IcsExamining:
15027         break;
15028
15029       case PlayFromGameFile:
15030         gameInfo.event = StrSave("Game from non-PGN file");
15031         gameInfo.site = StrSave(HostName());
15032         gameInfo.date = PGNDate();
15033         gameInfo.round = StrSave("-");
15034         gameInfo.white = StrSave("?");
15035         gameInfo.black = StrSave("?");
15036         break;
15037
15038       default:
15039         break;
15040     }
15041 }
15042
15043 void
15044 ReplaceComment (int index, char *text)
15045 {
15046     int len;
15047     char *p;
15048     float score;
15049
15050     if(index && sscanf(text, "%f/%d", &score, &len) == 2 && 
15051        pvInfoList[index-1].depth == len &&
15052        fabs(pvInfoList[index-1].score - score*100.) < 0.5 &&
15053        (p = strchr(text, '\n'))) text = p; // [HGM] strip off first line with PV info, if any
15054     while (*text == '\n') text++;
15055     len = strlen(text);
15056     while (len > 0 && text[len - 1] == '\n') len--;
15057
15058     if (commentList[index] != NULL)
15059       free(commentList[index]);
15060
15061     if (len == 0) {
15062         commentList[index] = NULL;
15063         return;
15064     }
15065   if( *text == '{' && strchr(text, '}') || // [HGM] braces: if certainy malformed, put braces
15066       *text == '[' && strchr(text, ']') || // otherwise hope the user knows what he is doing
15067       *text == '(' && strchr(text, ')')) { // (perhaps check if this parses as comment-only?)
15068     commentList[index] = (char *) malloc(len + 2);
15069     strncpy(commentList[index], text, len);
15070     commentList[index][len] = '\n';
15071     commentList[index][len + 1] = NULLCHAR;
15072   } else {
15073     // [HGM] braces: if text does not start with known OK delimiter, put braces around it.
15074     char *p;
15075     commentList[index] = (char *) malloc(len + 7);
15076     safeStrCpy(commentList[index], "{\n", 3);
15077     safeStrCpy(commentList[index]+2, text, len+1);
15078     commentList[index][len+2] = NULLCHAR;
15079     while(p = strchr(commentList[index], '}')) *p = ')'; // kill all } to make it one comment
15080     strcat(commentList[index], "\n}\n");
15081   }
15082 }
15083
15084 void
15085 CrushCRs (char *text)
15086 {
15087   char *p = text;
15088   char *q = text;
15089   char ch;
15090
15091   do {
15092     ch = *p++;
15093     if (ch == '\r') continue;
15094     *q++ = ch;
15095   } while (ch != '\0');
15096 }
15097
15098 void
15099 AppendComment (int index, char *text, Boolean addBraces)
15100 /* addBraces  tells if we should add {} */
15101 {
15102     int oldlen, len;
15103     char *old;
15104
15105 if(appData.debugMode) fprintf(debugFP, "Append: in='%s' %d\n", text, addBraces); fflush(debugFP);
15106     text = GetInfoFromComment( index, text ); /* [HGM] PV time: strip PV info from comment */
15107
15108     CrushCRs(text);
15109     while (*text == '\n') text++;
15110     len = strlen(text);
15111     while (len > 0 && text[len - 1] == '\n') len--;
15112     text[len] = NULLCHAR;
15113
15114     if (len == 0) return;
15115
15116     if (commentList[index] != NULL) {
15117       Boolean addClosingBrace = addBraces;
15118         old = commentList[index];
15119         oldlen = strlen(old);
15120         while(commentList[index][oldlen-1] ==  '\n')
15121           commentList[index][--oldlen] = NULLCHAR;
15122         commentList[index] = (char *) malloc(oldlen + len + 6); // might waste 4
15123         safeStrCpy(commentList[index], old, oldlen + len + 6);
15124         free(old);
15125         // [HGM] braces: join "{A\n}\n" + "{\nB}" as "{A\nB\n}"
15126         if(commentList[index][oldlen-1] == '}' && (text[0] == '{' || addBraces == TRUE)) {
15127           if(addBraces == TRUE) addBraces = FALSE; else { text++; len--; }
15128           while (*text == '\n') { text++; len--; }
15129           commentList[index][--oldlen] = NULLCHAR;
15130       }
15131         if(addBraces) strcat(commentList[index], addBraces == 2 ? "\n(" : "\n{\n");
15132         else          strcat(commentList[index], "\n");
15133         strcat(commentList[index], text);
15134         if(addClosingBrace) strcat(commentList[index], addClosingBrace == 2 ? ")\n" : "\n}\n");
15135         else          strcat(commentList[index], "\n");
15136     } else {
15137         commentList[index] = (char *) malloc(len + 6); // perhaps wastes 4...
15138         if(addBraces)
15139           safeStrCpy(commentList[index], addBraces == 2 ? "(" : "{\n", 3);
15140         else commentList[index][0] = NULLCHAR;
15141         strcat(commentList[index], text);
15142         strcat(commentList[index], addBraces == 2 ? ")\n" : "\n");
15143         if(addBraces == TRUE) strcat(commentList[index], "}\n");
15144     }
15145 }
15146
15147 static char *
15148 FindStr (char * text, char * sub_text)
15149 {
15150     char * result = strstr( text, sub_text );
15151
15152     if( result != NULL ) {
15153         result += strlen( sub_text );
15154     }
15155
15156     return result;
15157 }
15158
15159 /* [AS] Try to extract PV info from PGN comment */
15160 /* [HGM] PV time: and then remove it, to prevent it appearing twice */
15161 char *
15162 GetInfoFromComment (int index, char * text)
15163 {
15164     char * sep = text, *p;
15165
15166     if( text != NULL && index > 0 ) {
15167         int score = 0;
15168         int depth = 0;
15169         int time = -1, sec = 0, deci;
15170         char * s_eval = FindStr( text, "[%eval " );
15171         char * s_emt = FindStr( text, "[%emt " );
15172
15173         if( s_eval != NULL || s_emt != NULL ) {
15174             /* New style */
15175             char delim;
15176
15177             if( s_eval != NULL ) {
15178                 if( sscanf( s_eval, "%d,%d%c", &score, &depth, &delim ) != 3 ) {
15179                     return text;
15180                 }
15181
15182                 if( delim != ']' ) {
15183                     return text;
15184                 }
15185             }
15186
15187             if( s_emt != NULL ) {
15188             }
15189                 return text;
15190         }
15191         else {
15192             /* We expect something like: [+|-]nnn.nn/dd */
15193             int score_lo = 0;
15194
15195             if(*text != '{') return text; // [HGM] braces: must be normal comment
15196
15197             sep = strchr( text, '/' );
15198             if( sep == NULL || sep < (text+4) ) {
15199                 return text;
15200             }
15201
15202             p = text;
15203             if(p[1] == '(') { // comment starts with PV
15204                p = strchr(p, ')'); // locate end of PV
15205                if(p == NULL || sep < p+5) return text;
15206                // at this point we have something like "{(.*) +0.23/6 ..."
15207                p = text; while(*++p != ')') p[-1] = *p; p[-1] = ')';
15208                *p = '\n'; while(*p == ' ' || *p == '\n') p++; *--p = '{';
15209                // we now moved the brace to behind the PV: "(.*) {+0.23/6 ..."
15210             }
15211             time = -1; sec = -1; deci = -1;
15212             if( sscanf( p+1, "%d.%d/%d %d:%d", &score, &score_lo, &depth, &time, &sec ) != 5 &&
15213                 sscanf( p+1, "%d.%d/%d %d.%d", &score, &score_lo, &depth, &time, &deci ) != 5 &&
15214                 sscanf( p+1, "%d.%d/%d %d", &score, &score_lo, &depth, &time ) != 4 &&
15215                 sscanf( p+1, "%d.%d/%d", &score, &score_lo, &depth ) != 3   ) {
15216                 return text;
15217             }
15218
15219             if( score_lo < 0 || score_lo >= 100 ) {
15220                 return text;
15221             }
15222
15223             if(sec >= 0) time = 600*time + 10*sec; else
15224             if(deci >= 0) time = 10*time + deci; else time *= 10; // deci-sec
15225
15226             score = score >= 0 ? score*100 + score_lo : score*100 - score_lo;
15227
15228             /* [HGM] PV time: now locate end of PV info */
15229             while( *++sep >= '0' && *sep <= '9'); // strip depth
15230             if(time >= 0)
15231             while( *++sep >= '0' && *sep <= '9' || *sep == '\n'); // strip time
15232             if(sec >= 0)
15233             while( *++sep >= '0' && *sep <= '9'); // strip seconds
15234             if(deci >= 0)
15235             while( *++sep >= '0' && *sep <= '9'); // strip fractional seconds
15236             while(*sep == ' ' || *sep == '\n' || *sep == '\r') sep++;
15237         }
15238
15239         if( depth <= 0 ) {
15240             return text;
15241         }
15242
15243         if( time < 0 ) {
15244             time = -1;
15245         }
15246
15247         pvInfoList[index-1].depth = depth;
15248         pvInfoList[index-1].score = score;
15249         pvInfoList[index-1].time  = 10*time; // centi-sec
15250         if(*sep == '}') *sep = 0; else *--sep = '{';
15251         if(p != text) { while(*p++ = *sep++); sep = text; } // squeeze out space between PV and comment, and return both
15252     }
15253     return sep;
15254 }
15255
15256 void
15257 SendToProgram (char *message, ChessProgramState *cps)
15258 {
15259     int count, outCount, error;
15260     char buf[MSG_SIZ];
15261
15262     if (cps->pr == NoProc) return;
15263     Attention(cps);
15264
15265     if (appData.debugMode) {
15266         TimeMark now;
15267         GetTimeMark(&now);
15268         fprintf(debugFP, "%ld >%-6s: %s",
15269                 SubtractTimeMarks(&now, &programStartTime),
15270                 cps->which, message);
15271         if(serverFP)
15272             fprintf(serverFP, "%ld >%-6s: %s",
15273                 SubtractTimeMarks(&now, &programStartTime),
15274                 cps->which, message), fflush(serverFP);
15275     }
15276
15277     count = strlen(message);
15278     outCount = OutputToProcess(cps->pr, message, count, &error);
15279     if (outCount < count && !exiting
15280                          && !endingGame) { /* [HGM] crash: to not hang GameEnds() writing to deceased engines */
15281       if(!cps->initDone) return; // [HGM] should not generate fatal error during engine load
15282       snprintf(buf, MSG_SIZ, _("Error writing to %s chess program"), _(cps->which));
15283         if(gameInfo.resultDetails==NULL) { /* [HGM] crash: if game in progress, give reason for abort */
15284             if((signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
15285                 snprintf(buf, MSG_SIZ, _("%s program exits in draw position (%s)"), _(cps->which), cps->program);
15286                 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(GameIsDrawn, buf, GE_XBOARD); return; }
15287                 gameInfo.result = GameIsDrawn; /* [HGM] accept exit as draw claim */
15288             } else {
15289                 ChessMove res = cps->twoMachinesColor[0]=='w' ? BlackWins : WhiteWins;
15290                 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(res, buf, GE_XBOARD); return; }
15291                 gameInfo.result = res;
15292             }
15293             gameInfo.resultDetails = StrSave(buf);
15294         }
15295         if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; return; }
15296         if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, error, 1); else errorExitStatus = 1;
15297     }
15298 }
15299
15300 void
15301 ReceiveFromProgram (InputSourceRef isr, VOIDSTAR closure, char *message, int count, int error)
15302 {
15303     char *end_str;
15304     char buf[MSG_SIZ];
15305     ChessProgramState *cps = (ChessProgramState *)closure;
15306
15307     if (isr != cps->isr) return; /* Killed intentionally */
15308     if (count <= 0) {
15309         if (count == 0) {
15310             RemoveInputSource(cps->isr);
15311             snprintf(buf, MSG_SIZ, _("Error: %s chess program (%s) exited unexpectedly"),
15312                     _(cps->which), cps->program);
15313             if(LoadError(cps->userError ? NULL : buf, cps)) return; // [HGM] should not generate fatal error during engine load
15314             if(gameInfo.resultDetails==NULL) { /* [HGM] crash: if game in progress, give reason for abort */
15315                 if((signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
15316                     snprintf(buf, MSG_SIZ, _("%s program exits in draw position (%s)"), _(cps->which), cps->program);
15317                     if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(GameIsDrawn, buf, GE_XBOARD); return; }
15318                     gameInfo.result = GameIsDrawn; /* [HGM] accept exit as draw claim */
15319                 } else {
15320                     ChessMove res = cps->twoMachinesColor[0]=='w' ? BlackWins : WhiteWins;
15321                     if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(res, buf, GE_XBOARD); return; }
15322                     gameInfo.result = res;
15323                 }
15324                 gameInfo.resultDetails = StrSave(buf);
15325             }
15326             if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; return; }
15327             if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, 0, 1); else errorExitStatus = 1;
15328         } else {
15329             snprintf(buf, MSG_SIZ, _("Error reading from %s chess program (%s)"),
15330                     _(cps->which), cps->program);
15331             RemoveInputSource(cps->isr);
15332
15333             /* [AS] Program is misbehaving badly... kill it */
15334             if( count == -2 ) {
15335                 DestroyChildProcess( cps->pr, 9 );
15336                 cps->pr = NoProc;
15337             }
15338
15339             if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, error, 1); else errorExitStatus = 1;
15340         }
15341         return;
15342     }
15343
15344     if ((end_str = strchr(message, '\r')) != NULL)
15345       *end_str = NULLCHAR;
15346     if ((end_str = strchr(message, '\n')) != NULL)
15347       *end_str = NULLCHAR;
15348
15349     if (appData.debugMode) {
15350         TimeMark now; int print = 1;
15351         char *quote = ""; char c; int i;
15352
15353         if(appData.engineComments != 1) { /* [HGM] debug: decide if protocol-violating output is written */
15354                 char start = message[0];
15355                 if(start >='A' && start <= 'Z') start += 'a' - 'A'; // be tolerant to capitalizing
15356                 if(sscanf(message, "%d%c%d%d%d", &i, &c, &i, &i, &i) != 5 &&
15357                    sscanf(message, "move %c", &c)!=1  && sscanf(message, "offer%c", &c)!=1 &&
15358                    sscanf(message, "resign%c", &c)!=1 && sscanf(message, "feature %c", &c)!=1 &&
15359                    sscanf(message, "error %c", &c)!=1 && sscanf(message, "illegal %c", &c)!=1 &&
15360                    sscanf(message, "tell%c", &c)!=1   && sscanf(message, "0-1 %c", &c)!=1 &&
15361                    sscanf(message, "1-0 %c", &c)!=1   && sscanf(message, "1/2-1/2 %c", &c)!=1 &&
15362                    sscanf(message, "setboard %c", &c)!=1   && sscanf(message, "setup %c", &c)!=1 &&
15363                    sscanf(message, "hint: %c", &c)!=1 && 
15364                    sscanf(message, "pong %c", &c)!=1   && start != '#') {
15365                     quote = appData.engineComments == 2 ? "# " : "### NON-COMPLIANT! ### ";
15366                     print = (appData.engineComments >= 2);
15367                 }
15368                 message[0] = start; // restore original message
15369         }
15370         if(print) {
15371                 GetTimeMark(&now);
15372                 fprintf(debugFP, "%ld <%-6s: %s%s\n",
15373                         SubtractTimeMarks(&now, &programStartTime), cps->which,
15374                         quote,
15375                         message);
15376                 if(serverFP)
15377                     fprintf(serverFP, "%ld <%-6s: %s%s\n",
15378                         SubtractTimeMarks(&now, &programStartTime), cps->which,
15379                         quote,
15380                         message), fflush(serverFP);
15381         }
15382     }
15383
15384     /* [DM] if icsEngineAnalyze is active we block all whisper and kibitz output, because nobody want to see this */
15385     if (appData.icsEngineAnalyze) {
15386         if (strstr(message, "whisper") != NULL ||
15387              strstr(message, "kibitz") != NULL ||
15388             strstr(message, "tellics") != NULL) return;
15389     }
15390
15391     HandleMachineMove(message, cps);
15392 }
15393
15394
15395 void
15396 SendTimeControl (ChessProgramState *cps, int mps, long tc, int inc, int sd, int st)
15397 {
15398     char buf[MSG_SIZ];
15399     int seconds;
15400
15401     if( timeControl_2 > 0 ) {
15402         if( (gameMode == MachinePlaysBlack) || (gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b') ) {
15403             tc = timeControl_2;
15404         }
15405     }
15406     tc  /= cps->timeOdds; /* [HGM] time odds: apply before telling engine */
15407     inc /= cps->timeOdds;
15408     st  /= cps->timeOdds;
15409
15410     seconds = (tc / 1000) % 60; /* [HGM] displaced to after applying odds */
15411
15412     if (st > 0) {
15413       /* Set exact time per move, normally using st command */
15414       if (cps->stKludge) {
15415         /* GNU Chess 4 has no st command; uses level in a nonstandard way */
15416         seconds = st % 60;
15417         if (seconds == 0) {
15418           snprintf(buf, MSG_SIZ, "level 1 %d\n", st/60);
15419         } else {
15420           snprintf(buf, MSG_SIZ, "level 1 %d:%02d\n", st/60, seconds);
15421         }
15422       } else {
15423         snprintf(buf, MSG_SIZ, "st %d\n", st);
15424       }
15425     } else {
15426       /* Set conventional or incremental time control, using level command */
15427       if (seconds == 0) {
15428         /* Note old gnuchess bug -- minutes:seconds used to not work.
15429            Fixed in later versions, but still avoid :seconds
15430            when seconds is 0. */
15431         snprintf(buf, MSG_SIZ, "level %d %ld %g\n", mps, tc/60000, inc/1000.);
15432       } else {
15433         snprintf(buf, MSG_SIZ, "level %d %ld:%02d %g\n", mps, tc/60000,
15434                  seconds, inc/1000.);
15435       }
15436     }
15437     SendToProgram(buf, cps);
15438
15439     /* Orthoganally (except for GNU Chess 4), limit time to st seconds */
15440     /* Orthogonally, limit search to given depth */
15441     if (sd > 0) {
15442       if (cps->sdKludge) {
15443         snprintf(buf, MSG_SIZ, "depth\n%d\n", sd);
15444       } else {
15445         snprintf(buf, MSG_SIZ, "sd %d\n", sd);
15446       }
15447       SendToProgram(buf, cps);
15448     }
15449
15450     if(cps->nps >= 0) { /* [HGM] nps */
15451         if(cps->supportsNPS == FALSE)
15452           cps->nps = -1; // don't use if engine explicitly says not supported!
15453         else {
15454           snprintf(buf, MSG_SIZ, "nps %d\n", cps->nps);
15455           SendToProgram(buf, cps);
15456         }
15457     }
15458 }
15459
15460 ChessProgramState *
15461 WhitePlayer ()
15462 /* [HGM] return pointer to 'first' or 'second', depending on who plays white */
15463 {
15464     if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b' ||
15465        gameMode == BeginningOfGame || gameMode == MachinePlaysBlack)
15466         return &second;
15467     return &first;
15468 }
15469
15470 void
15471 SendTimeRemaining (ChessProgramState *cps, int machineWhite)
15472 {
15473     char message[MSG_SIZ];
15474     long time, otime;
15475
15476     /* Note: this routine must be called when the clocks are stopped
15477        or when they have *just* been set or switched; otherwise
15478        it will be off by the time since the current tick started.
15479     */
15480     if (machineWhite) {
15481         time = whiteTimeRemaining / 10;
15482         otime = blackTimeRemaining / 10;
15483     } else {
15484         time = blackTimeRemaining / 10;
15485         otime = whiteTimeRemaining / 10;
15486     }
15487     /* [HGM] translate opponent's time by time-odds factor */
15488     otime = (otime * cps->other->timeOdds) / cps->timeOdds;
15489
15490     if (time <= 0) time = 1;
15491     if (otime <= 0) otime = 1;
15492
15493     snprintf(message, MSG_SIZ, "time %ld\n", time);
15494     SendToProgram(message, cps);
15495
15496     snprintf(message, MSG_SIZ, "otim %ld\n", otime);
15497     SendToProgram(message, cps);
15498 }
15499
15500 int
15501 BoolFeature (char **p, char *name, int *loc, ChessProgramState *cps)
15502 {
15503   char buf[MSG_SIZ];
15504   int len = strlen(name);
15505   int val;
15506
15507   if (strncmp((*p), name, len) == 0 && (*p)[len] == '=') {
15508     (*p) += len + 1;
15509     sscanf(*p, "%d", &val);
15510     *loc = (val != 0);
15511     while (**p && **p != ' ')
15512       (*p)++;
15513     snprintf(buf, MSG_SIZ, "accepted %s\n", name);
15514     SendToProgram(buf, cps);
15515     return TRUE;
15516   }
15517   return FALSE;
15518 }
15519
15520 int
15521 IntFeature (char **p, char *name, int *loc, ChessProgramState *cps)
15522 {
15523   char buf[MSG_SIZ];
15524   int len = strlen(name);
15525   if (strncmp((*p), name, len) == 0 && (*p)[len] == '=') {
15526     (*p) += len + 1;
15527     sscanf(*p, "%d", loc);
15528     while (**p && **p != ' ') (*p)++;
15529     snprintf(buf, MSG_SIZ, "accepted %s\n", name);
15530     SendToProgram(buf, cps);
15531     return TRUE;
15532   }
15533   return FALSE;
15534 }
15535
15536 int
15537 StringFeature (char **p, char *name, char loc[], ChessProgramState *cps)
15538 {
15539   char buf[MSG_SIZ];
15540   int len = strlen(name);
15541   if (strncmp((*p), name, len) == 0
15542       && (*p)[len] == '=' && (*p)[len+1] == '\"') {
15543     (*p) += len + 2;
15544     sscanf(*p, "%[^\"]", loc);
15545     while (**p && **p != '\"') (*p)++;
15546     if (**p == '\"') (*p)++;
15547     snprintf(buf, MSG_SIZ, "accepted %s\n", name);
15548     SendToProgram(buf, cps);
15549     return TRUE;
15550   }
15551   return FALSE;
15552 }
15553
15554 int
15555 ParseOption (Option *opt, ChessProgramState *cps)
15556 // [HGM] options: process the string that defines an engine option, and determine
15557 // name, type, default value, and allowed value range
15558 {
15559         char *p, *q, buf[MSG_SIZ];
15560         int n, min = (-1)<<31, max = 1<<31, def;
15561
15562         if(p = strstr(opt->name, " -spin ")) {
15563             if((n = sscanf(p, " -spin %d %d %d", &def, &min, &max)) < 3 ) return FALSE;
15564             if(max < min) max = min; // enforce consistency
15565             if(def < min) def = min;
15566             if(def > max) def = max;
15567             opt->value = def;
15568             opt->min = min;
15569             opt->max = max;
15570             opt->type = Spin;
15571         } else if((p = strstr(opt->name, " -slider "))) {
15572             // for now -slider is a synonym for -spin, to already provide compatibility with future polyglots
15573             if((n = sscanf(p, " -slider %d %d %d", &def, &min, &max)) < 3 ) return FALSE;
15574             if(max < min) max = min; // enforce consistency
15575             if(def < min) def = min;
15576             if(def > max) def = max;
15577             opt->value = def;
15578             opt->min = min;
15579             opt->max = max;
15580             opt->type = Spin; // Slider;
15581         } else if((p = strstr(opt->name, " -string "))) {
15582             opt->textValue = p+9;
15583             opt->type = TextBox;
15584         } else if((p = strstr(opt->name, " -file "))) {
15585             // for now -file is a synonym for -string, to already provide compatibility with future polyglots
15586             opt->textValue = p+7;
15587             opt->type = FileName; // FileName;
15588         } else if((p = strstr(opt->name, " -path "))) {
15589             // for now -file is a synonym for -string, to already provide compatibility with future polyglots
15590             opt->textValue = p+7;
15591             opt->type = PathName; // PathName;
15592         } else if(p = strstr(opt->name, " -check ")) {
15593             if(sscanf(p, " -check %d", &def) < 1) return FALSE;
15594             opt->value = (def != 0);
15595             opt->type = CheckBox;
15596         } else if(p = strstr(opt->name, " -combo ")) {
15597             opt->textValue = (char*) (opt->choice = &cps->comboList[cps->comboCnt]); // cheat with pointer type
15598             cps->comboList[cps->comboCnt++] = q = p+8; // holds possible choices
15599             if(*q == '*') cps->comboList[cps->comboCnt-1]++;
15600             opt->value = n = 0;
15601             while(q = StrStr(q, " /// ")) {
15602                 n++; *q = 0;    // count choices, and null-terminate each of them
15603                 q += 5;
15604                 if(*q == '*') { // remember default, which is marked with * prefix
15605                     q++;
15606                     opt->value = n;
15607                 }
15608                 cps->comboList[cps->comboCnt++] = q;
15609             }
15610             cps->comboList[cps->comboCnt++] = NULL;
15611             opt->max = n + 1;
15612             opt->type = ComboBox;
15613         } else if(p = strstr(opt->name, " -button")) {
15614             opt->type = Button;
15615         } else if(p = strstr(opt->name, " -save")) {
15616             opt->type = SaveButton;
15617         } else return FALSE;
15618         *p = 0; // terminate option name
15619         // now look if the command-line options define a setting for this engine option.
15620         if(cps->optionSettings && cps->optionSettings[0])
15621             p = strstr(cps->optionSettings, opt->name); else p = NULL;
15622         if(p && (p == cps->optionSettings || p[-1] == ',')) {
15623           snprintf(buf, MSG_SIZ, "option %s", p);
15624                 if(p = strstr(buf, ",")) *p = 0;
15625                 if(q = strchr(buf, '=')) switch(opt->type) {
15626                     case ComboBox:
15627                         for(n=0; n<opt->max; n++)
15628                             if(!strcmp(((char**)opt->textValue)[n], q+1)) opt->value = n;
15629                         break;
15630                     case TextBox:
15631                         safeStrCpy(opt->textValue, q+1, MSG_SIZ - (opt->textValue - opt->name));
15632                         break;
15633                     case Spin:
15634                     case CheckBox:
15635                         opt->value = atoi(q+1);
15636                     default:
15637                         break;
15638                 }
15639                 strcat(buf, "\n");
15640                 SendToProgram(buf, cps);
15641         }
15642         return TRUE;
15643 }
15644
15645 void
15646 FeatureDone (ChessProgramState *cps, int val)
15647 {
15648   DelayedEventCallback cb = GetDelayedEvent();
15649   if ((cb == InitBackEnd3 && cps == &first) ||
15650       (cb == SettingsMenuIfReady && cps == &second) ||
15651       (cb == LoadEngine) ||
15652       (cb == TwoMachinesEventIfReady)) {
15653     CancelDelayedEvent();
15654     ScheduleDelayedEvent(cb, val ? 1 : 3600000);
15655   }
15656   cps->initDone = val;
15657 }
15658
15659 /* Parse feature command from engine */
15660 void
15661 ParseFeatures (char *args, ChessProgramState *cps)
15662 {
15663   char *p = args;
15664   char *q;
15665   int val;
15666   char buf[MSG_SIZ];
15667
15668   for (;;) {
15669     while (*p == ' ') p++;
15670     if (*p == NULLCHAR) return;
15671
15672     if (BoolFeature(&p, "setboard", &cps->useSetboard, cps)) continue;
15673     if (BoolFeature(&p, "xedit", &cps->extendedEdit, cps)) continue;
15674     if (BoolFeature(&p, "time", &cps->sendTime, cps)) continue;
15675     if (BoolFeature(&p, "draw", &cps->sendDrawOffers, cps)) continue;
15676     if (BoolFeature(&p, "sigint", &cps->useSigint, cps)) continue;
15677     if (BoolFeature(&p, "sigterm", &cps->useSigterm, cps)) continue;
15678     if (BoolFeature(&p, "reuse", &val, cps)) {
15679       /* Engine can disable reuse, but can't enable it if user said no */
15680       if (!val) cps->reuse = FALSE;
15681       continue;
15682     }
15683     if (BoolFeature(&p, "analyze", &cps->analysisSupport, cps)) continue;
15684     if (StringFeature(&p, "myname", cps->tidy, cps)) {
15685       if (gameMode == TwoMachinesPlay) {
15686         DisplayTwoMachinesTitle();
15687       } else {
15688         DisplayTitle("");
15689       }
15690       continue;
15691     }
15692     if (StringFeature(&p, "variants", cps->variants, cps)) continue;
15693     if (BoolFeature(&p, "san", &cps->useSAN, cps)) continue;
15694     if (BoolFeature(&p, "ping", &cps->usePing, cps)) continue;
15695     if (BoolFeature(&p, "playother", &cps->usePlayother, cps)) continue;
15696     if (BoolFeature(&p, "colors", &cps->useColors, cps)) continue;
15697     if (BoolFeature(&p, "usermove", &cps->useUsermove, cps)) continue;
15698     if (BoolFeature(&p, "exclude", &cps->excludeMoves, cps)) continue;
15699     if (BoolFeature(&p, "ics", &cps->sendICS, cps)) continue;
15700     if (BoolFeature(&p, "name", &cps->sendName, cps)) continue;
15701     if (BoolFeature(&p, "pause", &val, cps)) continue; /* unused at present */
15702     if (IntFeature(&p, "done", &val, cps)) {
15703       FeatureDone(cps, val);
15704       continue;
15705     }
15706     /* Added by Tord: */
15707     if (BoolFeature(&p, "fen960", &cps->useFEN960, cps)) continue;
15708     if (BoolFeature(&p, "oocastle", &cps->useOOCastle, cps)) continue;
15709     /* End of additions by Tord */
15710
15711     /* [HGM] added features: */
15712     if (BoolFeature(&p, "debug", &cps->debug, cps)) continue;
15713     if (BoolFeature(&p, "nps", &cps->supportsNPS, cps)) continue;
15714     if (IntFeature(&p, "level", &cps->maxNrOfSessions, cps)) continue;
15715     if (BoolFeature(&p, "memory", &cps->memSize, cps)) continue;
15716     if (BoolFeature(&p, "smp", &cps->maxCores, cps)) continue;
15717     if (StringFeature(&p, "egt", cps->egtFormats, cps)) continue;
15718     if (StringFeature(&p, "option", buf, cps)) {
15719         FREE(cps->option[cps->nrOptions].name);
15720         cps->option[cps->nrOptions].name = malloc(MSG_SIZ);
15721         safeStrCpy(cps->option[cps->nrOptions].name, buf, MSG_SIZ);
15722         if(!ParseOption(&(cps->option[cps->nrOptions++]), cps)) { // [HGM] options: add option feature
15723           snprintf(buf, MSG_SIZ, "rejected option %s\n", cps->option[--cps->nrOptions].name);
15724             SendToProgram(buf, cps);
15725             continue;
15726         }
15727         if(cps->nrOptions >= MAX_OPTIONS) {
15728             cps->nrOptions--;
15729             snprintf(buf, MSG_SIZ, _("%s engine has too many options\n"), _(cps->which));
15730             DisplayError(buf, 0);
15731         }
15732         continue;
15733     }
15734     /* End of additions by HGM */
15735
15736     /* unknown feature: complain and skip */
15737     q = p;
15738     while (*q && *q != '=') q++;
15739     snprintf(buf, MSG_SIZ,"rejected %.*s\n", (int)(q-p), p);
15740     SendToProgram(buf, cps);
15741     p = q;
15742     if (*p == '=') {
15743       p++;
15744       if (*p == '\"') {
15745         p++;
15746         while (*p && *p != '\"') p++;
15747         if (*p == '\"') p++;
15748       } else {
15749         while (*p && *p != ' ') p++;
15750       }
15751     }
15752   }
15753
15754 }
15755
15756 void
15757 PeriodicUpdatesEvent (int newState)
15758 {
15759     if (newState == appData.periodicUpdates)
15760       return;
15761
15762     appData.periodicUpdates=newState;
15763
15764     /* Display type changes, so update it now */
15765 //    DisplayAnalysis();
15766
15767     /* Get the ball rolling again... */
15768     if (newState) {
15769         AnalysisPeriodicEvent(1);
15770         StartAnalysisClock();
15771     }
15772 }
15773
15774 void
15775 PonderNextMoveEvent (int newState)
15776 {
15777     if (newState == appData.ponderNextMove) return;
15778     if (gameMode == EditPosition) EditPositionDone(TRUE);
15779     if (newState) {
15780         SendToProgram("hard\n", &first);
15781         if (gameMode == TwoMachinesPlay) {
15782             SendToProgram("hard\n", &second);
15783         }
15784     } else {
15785         SendToProgram("easy\n", &first);
15786         thinkOutput[0] = NULLCHAR;
15787         if (gameMode == TwoMachinesPlay) {
15788             SendToProgram("easy\n", &second);
15789         }
15790     }
15791     appData.ponderNextMove = newState;
15792 }
15793
15794 void
15795 NewSettingEvent (int option, int *feature, char *command, int value)
15796 {
15797     char buf[MSG_SIZ];
15798
15799     if (gameMode == EditPosition) EditPositionDone(TRUE);
15800     snprintf(buf, MSG_SIZ,"%s%s %d\n", (option ? "option ": ""), command, value);
15801     if(feature == NULL || *feature) SendToProgram(buf, &first);
15802     if (gameMode == TwoMachinesPlay) {
15803         if(feature == NULL || feature[(int*)&second - (int*)&first]) SendToProgram(buf, &second);
15804     }
15805 }
15806
15807 void
15808 ShowThinkingEvent ()
15809 // [HGM] thinking: this routine is now also called from "Options -> Engine..." popup
15810 {
15811     static int oldState = 2; // kludge alert! Neither true nor fals, so first time oldState is always updated
15812     int newState = appData.showThinking
15813         // [HGM] thinking: other features now need thinking output as well
15814         || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp();
15815
15816     if (oldState == newState) return;
15817     oldState = newState;
15818     if (gameMode == EditPosition) EditPositionDone(TRUE);
15819     if (oldState) {
15820         SendToProgram("post\n", &first);
15821         if (gameMode == TwoMachinesPlay) {
15822             SendToProgram("post\n", &second);
15823         }
15824     } else {
15825         SendToProgram("nopost\n", &first);
15826         thinkOutput[0] = NULLCHAR;
15827         if (gameMode == TwoMachinesPlay) {
15828             SendToProgram("nopost\n", &second);
15829         }
15830     }
15831 //    appData.showThinking = newState; // [HGM] thinking: responsible option should already have be changed when calling this routine!
15832 }
15833
15834 void
15835 AskQuestionEvent (char *title, char *question, char *replyPrefix, char *which)
15836 {
15837   ProcRef pr = (which[0] == '1') ? first.pr : second.pr;
15838   if (pr == NoProc) return;
15839   AskQuestion(title, question, replyPrefix, pr);
15840 }
15841
15842 void
15843 TypeInEvent (char firstChar)
15844 {
15845     if ((gameMode == BeginningOfGame && !appData.icsActive) || 
15846         gameMode == MachinePlaysWhite || gameMode == MachinePlaysBlack ||
15847         gameMode == AnalyzeMode || gameMode == EditGame || 
15848         gameMode == EditPosition || gameMode == IcsExamining ||
15849         gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
15850         isdigit(firstChar) && // [HGM] movenum: allow typing in of move nr in 'passive' modes
15851                 ( gameMode == AnalyzeFile || gameMode == PlayFromGameFile ||
15852                   gameMode == IcsObserving || gameMode == TwoMachinesPlay    ) ||
15853         gameMode == Training) PopUpMoveDialog(firstChar);
15854 }
15855
15856 void
15857 TypeInDoneEvent (char *move)
15858 {
15859         Board board;
15860         int n, fromX, fromY, toX, toY;
15861         char promoChar;
15862         ChessMove moveType;
15863
15864         // [HGM] FENedit
15865         if(gameMode == EditPosition && ParseFEN(board, &n, move) ) {
15866                 EditPositionPasteFEN(move);
15867                 return;
15868         }
15869         // [HGM] movenum: allow move number to be typed in any mode
15870         if(sscanf(move, "%d", &n) == 1 && n != 0 ) {
15871           ToNrEvent(2*n-1);
15872           return;
15873         }
15874         // undocumented kludge: allow command-line option to be typed in!
15875         // (potentially fatal, and does not implement the effect of the option.)
15876         // should only be used for options that are values on which future decisions will be made,
15877         // and definitely not on options that would be used during initialization.
15878         if(strstr(move, "!!! -") == move) {
15879             ParseArgsFromString(move+4);
15880             return;
15881         }
15882
15883       if (gameMode != EditGame && currentMove != forwardMostMove && 
15884         gameMode != Training) {
15885         DisplayMoveError(_("Displayed move is not current"));
15886       } else {
15887         int ok = ParseOneMove(move, gameMode == EditPosition ? blackPlaysFirst : currentMove, 
15888           &moveType, &fromX, &fromY, &toX, &toY, &promoChar);
15889         if(!ok && move[0] >= 'a') { move[0] += 'A' - 'a'; ok = 2; } // [HGM] try also capitalized
15890         if (ok==1 || ok && ParseOneMove(move, gameMode == EditPosition ? blackPlaysFirst : currentMove, 
15891           &moveType, &fromX, &fromY, &toX, &toY, &promoChar)) {
15892           UserMoveEvent(fromX, fromY, toX, toY, promoChar);     
15893         } else {
15894           DisplayMoveError(_("Could not parse move"));
15895         }
15896       }
15897 }
15898
15899 void
15900 DisplayMove (int moveNumber)
15901 {
15902     char message[MSG_SIZ];
15903     char res[MSG_SIZ];
15904     char cpThinkOutput[MSG_SIZ];
15905
15906     if(appData.noGUI) return; // [HGM] fast: suppress display of moves
15907
15908     if (moveNumber == forwardMostMove - 1 ||
15909         gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
15910
15911         safeStrCpy(cpThinkOutput, thinkOutput, sizeof(cpThinkOutput)/sizeof(cpThinkOutput[0]));
15912
15913         if (strchr(cpThinkOutput, '\n')) {
15914             *strchr(cpThinkOutput, '\n') = NULLCHAR;
15915         }
15916     } else {
15917         *cpThinkOutput = NULLCHAR;
15918     }
15919
15920     /* [AS] Hide thinking from human user */
15921     if( appData.hideThinkingFromHuman && gameMode != TwoMachinesPlay ) {
15922         *cpThinkOutput = NULLCHAR;
15923         if( thinkOutput[0] != NULLCHAR ) {
15924             int i;
15925
15926             for( i=0; i<=hiddenThinkOutputState; i++ ) {
15927                 cpThinkOutput[i] = '.';
15928             }
15929             cpThinkOutput[i] = NULLCHAR;
15930             hiddenThinkOutputState = (hiddenThinkOutputState + 1) % 3;
15931         }
15932     }
15933
15934     if (moveNumber == forwardMostMove - 1 &&
15935         gameInfo.resultDetails != NULL) {
15936         if (gameInfo.resultDetails[0] == NULLCHAR) {
15937           snprintf(res, MSG_SIZ, " %s", PGNResult(gameInfo.result));
15938         } else {
15939           snprintf(res, MSG_SIZ, " {%s} %s",
15940                     T_(gameInfo.resultDetails), PGNResult(gameInfo.result));
15941         }
15942     } else {
15943         res[0] = NULLCHAR;
15944     }
15945
15946     if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
15947         DisplayMessage(res, cpThinkOutput);
15948     } else {
15949       snprintf(message, MSG_SIZ, "%d.%s%s%s", moveNumber / 2 + 1,
15950                 WhiteOnMove(moveNumber) ? " " : ".. ",
15951                 parseList[moveNumber], res);
15952         DisplayMessage(message, cpThinkOutput);
15953     }
15954 }
15955
15956 void
15957 DisplayComment (int moveNumber, char *text)
15958 {
15959     char title[MSG_SIZ];
15960
15961     if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
15962       safeStrCpy(title, "Comment", sizeof(title)/sizeof(title[0]));
15963     } else {
15964       snprintf(title,MSG_SIZ, "Comment on %d.%s%s", moveNumber / 2 + 1,
15965               WhiteOnMove(moveNumber) ? " " : ".. ",
15966               parseList[moveNumber]);
15967     }
15968     if (text != NULL && (appData.autoDisplayComment || commentUp))
15969         CommentPopUp(title, text);
15970 }
15971
15972 /* This routine sends a ^C interrupt to gnuchess, to awaken it if it
15973  * might be busy thinking or pondering.  It can be omitted if your
15974  * gnuchess is configured to stop thinking immediately on any user
15975  * input.  However, that gnuchess feature depends on the FIONREAD
15976  * ioctl, which does not work properly on some flavors of Unix.
15977  */
15978 void
15979 Attention (ChessProgramState *cps)
15980 {
15981 #if ATTENTION
15982     if (!cps->useSigint) return;
15983     if (appData.noChessProgram || (cps->pr == NoProc)) return;
15984     switch (gameMode) {
15985       case MachinePlaysWhite:
15986       case MachinePlaysBlack:
15987       case TwoMachinesPlay:
15988       case IcsPlayingWhite:
15989       case IcsPlayingBlack:
15990       case AnalyzeMode:
15991       case AnalyzeFile:
15992         /* Skip if we know it isn't thinking */
15993         if (!cps->maybeThinking) return;
15994         if (appData.debugMode)
15995           fprintf(debugFP, "Interrupting %s\n", cps->which);
15996         InterruptChildProcess(cps->pr);
15997         cps->maybeThinking = FALSE;
15998         break;
15999       default:
16000         break;
16001     }
16002 #endif /*ATTENTION*/
16003 }
16004
16005 int
16006 CheckFlags ()
16007 {
16008     if (whiteTimeRemaining <= 0) {
16009         if (!whiteFlag) {
16010             whiteFlag = TRUE;
16011             if (appData.icsActive) {
16012                 if (appData.autoCallFlag &&
16013                     gameMode == IcsPlayingBlack && !blackFlag) {
16014                   SendToICS(ics_prefix);
16015                   SendToICS("flag\n");
16016                 }
16017             } else {
16018                 if (blackFlag) {
16019                     if(gameMode != TwoMachinesPlay) DisplayTitle(_("Both flags fell"));
16020                 } else {
16021                     if(gameMode != TwoMachinesPlay) DisplayTitle(_("White's flag fell"));
16022                     if (appData.autoCallFlag) {
16023                         GameEnds(BlackWins, "Black wins on time", GE_XBOARD);
16024                         return TRUE;
16025                     }
16026                 }
16027             }
16028         }
16029     }
16030     if (blackTimeRemaining <= 0) {
16031         if (!blackFlag) {
16032             blackFlag = TRUE;
16033             if (appData.icsActive) {
16034                 if (appData.autoCallFlag &&
16035                     gameMode == IcsPlayingWhite && !whiteFlag) {
16036                   SendToICS(ics_prefix);
16037                   SendToICS("flag\n");
16038                 }
16039             } else {
16040                 if (whiteFlag) {
16041                     if(gameMode != TwoMachinesPlay) DisplayTitle(_("Both flags fell"));
16042                 } else {
16043                     if(gameMode != TwoMachinesPlay) DisplayTitle(_("Black's flag fell"));
16044                     if (appData.autoCallFlag) {
16045                         GameEnds(WhiteWins, "White wins on time", GE_XBOARD);
16046                         return TRUE;
16047                     }
16048                 }
16049             }
16050         }
16051     }
16052     return FALSE;
16053 }
16054
16055 void
16056 CheckTimeControl ()
16057 {
16058     if (!appData.clockMode || appData.icsActive || searchTime || // [HGM] st: no inc in st mode
16059         gameMode == PlayFromGameFile || forwardMostMove == 0) return;
16060
16061     /*
16062      * add time to clocks when time control is achieved ([HGM] now also used for increment)
16063      */
16064     if ( !WhiteOnMove(forwardMostMove) ) {
16065         /* White made time control */
16066         lastWhite -= whiteTimeRemaining; // [HGM] contains start time, socalculate thinking time
16067         whiteTimeRemaining += GetTimeQuota((forwardMostMove-whiteStartMove-1)/2, lastWhite, whiteTC)
16068         /* [HGM] time odds: correct new time quota for time odds! */
16069                                             / WhitePlayer()->timeOdds;
16070         lastBlack = blackTimeRemaining; // [HGM] leave absolute time (after quota), so next switch we can us it to calculate thinking time
16071     } else {
16072         lastBlack -= blackTimeRemaining;
16073         /* Black made time control */
16074         blackTimeRemaining += GetTimeQuota((forwardMostMove-blackStartMove-1)/2, lastBlack, blackTC)
16075                                             / WhitePlayer()->other->timeOdds;
16076         lastWhite = whiteTimeRemaining;
16077     }
16078 }
16079
16080 void
16081 DisplayBothClocks ()
16082 {
16083     int wom = gameMode == EditPosition ?
16084       !blackPlaysFirst : WhiteOnMove(currentMove);
16085     DisplayWhiteClock(whiteTimeRemaining, wom);
16086     DisplayBlackClock(blackTimeRemaining, !wom);
16087 }
16088
16089
16090 /* Timekeeping seems to be a portability nightmare.  I think everyone
16091    has ftime(), but I'm really not sure, so I'm including some ifdefs
16092    to use other calls if you don't.  Clocks will be less accurate if
16093    you have neither ftime nor gettimeofday.
16094 */
16095
16096 /* VS 2008 requires the #include outside of the function */
16097 #if !HAVE_GETTIMEOFDAY && HAVE_FTIME
16098 #include <sys/timeb.h>
16099 #endif
16100
16101 /* Get the current time as a TimeMark */
16102 void
16103 GetTimeMark (TimeMark *tm)
16104 {
16105 #if HAVE_GETTIMEOFDAY
16106
16107     struct timeval timeVal;
16108     struct timezone timeZone;
16109
16110     gettimeofday(&timeVal, &timeZone);
16111     tm->sec = (long) timeVal.tv_sec;
16112     tm->ms = (int) (timeVal.tv_usec / 1000L);
16113
16114 #else /*!HAVE_GETTIMEOFDAY*/
16115 #if HAVE_FTIME
16116
16117 // include <sys/timeb.h> / moved to just above start of function
16118     struct timeb timeB;
16119
16120     ftime(&timeB);
16121     tm->sec = (long) timeB.time;
16122     tm->ms = (int) timeB.millitm;
16123
16124 #else /*!HAVE_FTIME && !HAVE_GETTIMEOFDAY*/
16125     tm->sec = (long) time(NULL);
16126     tm->ms = 0;
16127 #endif
16128 #endif
16129 }
16130
16131 /* Return the difference in milliseconds between two
16132    time marks.  We assume the difference will fit in a long!
16133 */
16134 long
16135 SubtractTimeMarks (TimeMark *tm2, TimeMark *tm1)
16136 {
16137     return 1000L*(tm2->sec - tm1->sec) +
16138            (long) (tm2->ms - tm1->ms);
16139 }
16140
16141
16142 /*
16143  * Code to manage the game clocks.
16144  *
16145  * In tournament play, black starts the clock and then white makes a move.
16146  * We give the human user a slight advantage if he is playing white---the
16147  * clocks don't run until he makes his first move, so it takes zero time.
16148  * Also, we don't account for network lag, so we could get out of sync
16149  * with GNU Chess's clock -- but then, referees are always right.
16150  */
16151
16152 static TimeMark tickStartTM;
16153 static long intendedTickLength;
16154
16155 long
16156 NextTickLength (long timeRemaining)
16157 {
16158     long nominalTickLength, nextTickLength;
16159
16160     if (timeRemaining > 0L && timeRemaining <= 10000L)
16161       nominalTickLength = 100L;
16162     else
16163       nominalTickLength = 1000L;
16164     nextTickLength = timeRemaining % nominalTickLength;
16165     if (nextTickLength <= 0) nextTickLength += nominalTickLength;
16166
16167     return nextTickLength;
16168 }
16169
16170 /* Adjust clock one minute up or down */
16171 void
16172 AdjustClock (Boolean which, int dir)
16173 {
16174     if(appData.autoCallFlag) { DisplayError(_("Clock adjustment not allowed in auto-flag mode"), 0); return; }
16175     if(which) blackTimeRemaining += 60000*dir;
16176     else      whiteTimeRemaining += 60000*dir;
16177     DisplayBothClocks();
16178     adjustedClock = TRUE;
16179 }
16180
16181 /* Stop clocks and reset to a fresh time control */
16182 void
16183 ResetClocks ()
16184 {
16185     (void) StopClockTimer();
16186     if (appData.icsActive) {
16187         whiteTimeRemaining = blackTimeRemaining = 0;
16188     } else if (searchTime) {
16189         whiteTimeRemaining = 1000*searchTime / WhitePlayer()->timeOdds;
16190         blackTimeRemaining = 1000*searchTime / WhitePlayer()->other->timeOdds;
16191     } else { /* [HGM] correct new time quote for time odds */
16192         whiteTC = blackTC = fullTimeControlString;
16193         whiteTimeRemaining = GetTimeQuota(-1, 0, whiteTC) / WhitePlayer()->timeOdds;
16194         blackTimeRemaining = GetTimeQuota(-1, 0, blackTC) / WhitePlayer()->other->timeOdds;
16195     }
16196     if (whiteFlag || blackFlag) {
16197         DisplayTitle("");
16198         whiteFlag = blackFlag = FALSE;
16199     }
16200     lastWhite = lastBlack = whiteStartMove = blackStartMove = 0;
16201     DisplayBothClocks();
16202     adjustedClock = FALSE;
16203 }
16204
16205 #define FUDGE 25 /* 25ms = 1/40 sec; should be plenty even for 50 Hz clocks */
16206
16207 /* Decrement running clock by amount of time that has passed */
16208 void
16209 DecrementClocks ()
16210 {
16211     long timeRemaining;
16212     long lastTickLength, fudge;
16213     TimeMark now;
16214
16215     if (!appData.clockMode) return;
16216     if (gameMode==AnalyzeMode || gameMode == AnalyzeFile) return;
16217
16218     GetTimeMark(&now);
16219
16220     lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
16221
16222     /* Fudge if we woke up a little too soon */
16223     fudge = intendedTickLength - lastTickLength;
16224     if (fudge < 0 || fudge > FUDGE) fudge = 0;
16225
16226     if (WhiteOnMove(forwardMostMove)) {
16227         if(whiteNPS >= 0) lastTickLength = 0;
16228         timeRemaining = whiteTimeRemaining -= lastTickLength;
16229         if(timeRemaining < 0 && !appData.icsActive) {
16230             GetTimeQuota((forwardMostMove-whiteStartMove-1)/2, 0, whiteTC); // sets suddenDeath & nextSession;
16231             if(suddenDeath) { // [HGM] if we run out of a non-last incremental session, go to the next
16232                 whiteStartMove = forwardMostMove; whiteTC = nextSession;
16233                 lastWhite= timeRemaining = whiteTimeRemaining += GetTimeQuota(-1, 0, whiteTC);
16234             }
16235         }
16236         DisplayWhiteClock(whiteTimeRemaining - fudge,
16237                           WhiteOnMove(currentMove < forwardMostMove ? currentMove : forwardMostMove));
16238     } else {
16239         if(blackNPS >= 0) lastTickLength = 0;
16240         timeRemaining = blackTimeRemaining -= lastTickLength;
16241         if(timeRemaining < 0 && !appData.icsActive) { // [HGM] if we run out of a non-last incremental session, go to the next
16242             GetTimeQuota((forwardMostMove-blackStartMove-1)/2, 0, blackTC);
16243             if(suddenDeath) {
16244                 blackStartMove = forwardMostMove;
16245                 lastBlack = timeRemaining = blackTimeRemaining += GetTimeQuota(-1, 0, blackTC=nextSession);
16246             }
16247         }
16248         DisplayBlackClock(blackTimeRemaining - fudge,
16249                           !WhiteOnMove(currentMove < forwardMostMove ? currentMove : forwardMostMove));
16250     }
16251     if (CheckFlags()) return;
16252
16253     if(twoBoards) { // count down secondary board's clocks as well
16254         activePartnerTime -= lastTickLength;
16255         partnerUp = 1;
16256         if(activePartner == 'W')
16257             DisplayWhiteClock(activePartnerTime, TRUE); // the counting clock is always the highlighted one!
16258         else
16259             DisplayBlackClock(activePartnerTime, TRUE);
16260         partnerUp = 0;
16261     }
16262
16263     tickStartTM = now;
16264     intendedTickLength = NextTickLength(timeRemaining - fudge) + fudge;
16265     StartClockTimer(intendedTickLength);
16266
16267     /* if the time remaining has fallen below the alarm threshold, sound the
16268      * alarm. if the alarm has sounded and (due to a takeback or time control
16269      * with increment) the time remaining has increased to a level above the
16270      * threshold, reset the alarm so it can sound again.
16271      */
16272
16273     if (appData.icsActive && appData.icsAlarm) {
16274
16275         /* make sure we are dealing with the user's clock */
16276         if (!( ((gameMode == IcsPlayingWhite) && WhiteOnMove(currentMove)) ||
16277                ((gameMode == IcsPlayingBlack) && !WhiteOnMove(currentMove))
16278            )) return;
16279
16280         if (alarmSounded && (timeRemaining > appData.icsAlarmTime)) {
16281             alarmSounded = FALSE;
16282         } else if (!alarmSounded && (timeRemaining <= appData.icsAlarmTime)) {
16283             PlayAlarmSound();
16284             alarmSounded = TRUE;
16285         }
16286     }
16287 }
16288
16289
16290 /* A player has just moved, so stop the previously running
16291    clock and (if in clock mode) start the other one.
16292    We redisplay both clocks in case we're in ICS mode, because
16293    ICS gives us an update to both clocks after every move.
16294    Note that this routine is called *after* forwardMostMove
16295    is updated, so the last fractional tick must be subtracted
16296    from the color that is *not* on move now.
16297 */
16298 void
16299 SwitchClocks (int newMoveNr)
16300 {
16301     long lastTickLength;
16302     TimeMark now;
16303     int flagged = FALSE;
16304
16305     GetTimeMark(&now);
16306
16307     if (StopClockTimer() && appData.clockMode) {
16308         lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
16309         if (!WhiteOnMove(forwardMostMove)) {
16310             if(blackNPS >= 0) lastTickLength = 0;
16311             blackTimeRemaining -= lastTickLength;
16312            /* [HGM] PGNtime: save time for PGN file if engine did not give it */
16313 //         if(pvInfoList[forwardMostMove].time == -1)
16314                  pvInfoList[forwardMostMove].time =               // use GUI time
16315                       (timeRemaining[1][forwardMostMove-1] - blackTimeRemaining)/10;
16316         } else {
16317            if(whiteNPS >= 0) lastTickLength = 0;
16318            whiteTimeRemaining -= lastTickLength;
16319            /* [HGM] PGNtime: save time for PGN file if engine did not give it */
16320 //         if(pvInfoList[forwardMostMove].time == -1)
16321                  pvInfoList[forwardMostMove].time =
16322                       (timeRemaining[0][forwardMostMove-1] - whiteTimeRemaining)/10;
16323         }
16324         flagged = CheckFlags();
16325     }
16326     forwardMostMove = newMoveNr; // [HGM] race: change stm when no timer interrupt scheduled
16327     CheckTimeControl();
16328
16329     if (flagged || !appData.clockMode) return;
16330
16331     switch (gameMode) {
16332       case MachinePlaysBlack:
16333       case MachinePlaysWhite:
16334       case BeginningOfGame:
16335         if (pausing) return;
16336         break;
16337
16338       case EditGame:
16339       case PlayFromGameFile:
16340       case IcsExamining:
16341         return;
16342
16343       default:
16344         break;
16345     }
16346
16347     if (searchTime) { // [HGM] st: set clock of player that has to move to max time
16348         if(WhiteOnMove(forwardMostMove))
16349              whiteTimeRemaining = 1000*searchTime / WhitePlayer()->timeOdds;
16350         else blackTimeRemaining = 1000*searchTime / WhitePlayer()->other->timeOdds;
16351     }
16352
16353     tickStartTM = now;
16354     intendedTickLength = NextTickLength(WhiteOnMove(forwardMostMove) ?
16355       whiteTimeRemaining : blackTimeRemaining);
16356     StartClockTimer(intendedTickLength);
16357 }
16358
16359
16360 /* Stop both clocks */
16361 void
16362 StopClocks ()
16363 {
16364     long lastTickLength;
16365     TimeMark now;
16366
16367     if (!StopClockTimer()) return;
16368     if (!appData.clockMode) return;
16369
16370     GetTimeMark(&now);
16371
16372     lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
16373     if (WhiteOnMove(forwardMostMove)) {
16374         if(whiteNPS >= 0) lastTickLength = 0;
16375         whiteTimeRemaining -= lastTickLength;
16376         DisplayWhiteClock(whiteTimeRemaining, WhiteOnMove(currentMove));
16377     } else {
16378         if(blackNPS >= 0) lastTickLength = 0;
16379         blackTimeRemaining -= lastTickLength;
16380         DisplayBlackClock(blackTimeRemaining, !WhiteOnMove(currentMove));
16381     }
16382     CheckFlags();
16383 }
16384
16385 /* Start clock of player on move.  Time may have been reset, so
16386    if clock is already running, stop and restart it. */
16387 void
16388 StartClocks ()
16389 {
16390     (void) StopClockTimer(); /* in case it was running already */
16391     DisplayBothClocks();
16392     if (CheckFlags()) return;
16393
16394     if (!appData.clockMode) return;
16395     if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) return;
16396
16397     GetTimeMark(&tickStartTM);
16398     intendedTickLength = NextTickLength(WhiteOnMove(forwardMostMove) ?
16399       whiteTimeRemaining : blackTimeRemaining);
16400
16401    /* [HGM] nps: figure out nps factors, by determining which engine plays white and/or black once and for all */
16402     whiteNPS = blackNPS = -1;
16403     if(gameMode == MachinePlaysWhite || gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w'
16404        || appData.zippyPlay && gameMode == IcsPlayingBlack) // first (perhaps only) engine has white
16405         whiteNPS = first.nps;
16406     if(gameMode == MachinePlaysBlack || gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b'
16407        || appData.zippyPlay && gameMode == IcsPlayingWhite) // first (perhaps only) engine has black
16408         blackNPS = first.nps;
16409     if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b') // second only used in Two-Machines mode
16410         whiteNPS = second.nps;
16411     if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w')
16412         blackNPS = second.nps;
16413     if(appData.debugMode) fprintf(debugFP, "nps: w=%d, b=%d\n", whiteNPS, blackNPS);
16414
16415     StartClockTimer(intendedTickLength);
16416 }
16417
16418 char *
16419 TimeString (long ms)
16420 {
16421     long second, minute, hour, day;
16422     char *sign = "";
16423     static char buf[32];
16424
16425     if (ms > 0 && ms <= 9900) {
16426       /* convert milliseconds to tenths, rounding up */
16427       double tenths = floor( ((double)(ms + 99L)) / 100.00 );
16428
16429       snprintf(buf,sizeof(buf)/sizeof(buf[0]), " %03.1f ", tenths/10.0);
16430       return buf;
16431     }
16432
16433     /* convert milliseconds to seconds, rounding up */
16434     /* use floating point to avoid strangeness of integer division
16435        with negative dividends on many machines */
16436     second = (long) floor(((double) (ms + 999L)) / 1000.0);
16437
16438     if (second < 0) {
16439         sign = "-";
16440         second = -second;
16441     }
16442
16443     day = second / (60 * 60 * 24);
16444     second = second % (60 * 60 * 24);
16445     hour = second / (60 * 60);
16446     second = second % (60 * 60);
16447     minute = second / 60;
16448     second = second % 60;
16449
16450     if (day > 0)
16451       snprintf(buf, sizeof(buf)/sizeof(buf[0]), " %s%ld:%02ld:%02ld:%02ld ",
16452               sign, day, hour, minute, second);
16453     else if (hour > 0)
16454       snprintf(buf, sizeof(buf)/sizeof(buf[0]), " %s%ld:%02ld:%02ld ", sign, hour, minute, second);
16455     else
16456       snprintf(buf, sizeof(buf)/sizeof(buf[0]), " %s%2ld:%02ld ", sign, minute, second);
16457
16458     return buf;
16459 }
16460
16461
16462 /*
16463  * This is necessary because some C libraries aren't ANSI C compliant yet.
16464  */
16465 char *
16466 StrStr (char *string, char *match)
16467 {
16468     int i, length;
16469
16470     length = strlen(match);
16471
16472     for (i = strlen(string) - length; i >= 0; i--, string++)
16473       if (!strncmp(match, string, length))
16474         return string;
16475
16476     return NULL;
16477 }
16478
16479 char *
16480 StrCaseStr (char *string, char *match)
16481 {
16482     int i, j, length;
16483
16484     length = strlen(match);
16485
16486     for (i = strlen(string) - length; i >= 0; i--, string++) {
16487         for (j = 0; j < length; j++) {
16488             if (ToLower(match[j]) != ToLower(string[j]))
16489               break;
16490         }
16491         if (j == length) return string;
16492     }
16493
16494     return NULL;
16495 }
16496
16497 #ifndef _amigados
16498 int
16499 StrCaseCmp (char *s1, char *s2)
16500 {
16501     char c1, c2;
16502
16503     for (;;) {
16504         c1 = ToLower(*s1++);
16505         c2 = ToLower(*s2++);
16506         if (c1 > c2) return 1;
16507         if (c1 < c2) return -1;
16508         if (c1 == NULLCHAR) return 0;
16509     }
16510 }
16511
16512
16513 int
16514 ToLower (int c)
16515 {
16516     return isupper(c) ? tolower(c) : c;
16517 }
16518
16519
16520 int
16521 ToUpper (int c)
16522 {
16523     return islower(c) ? toupper(c) : c;
16524 }
16525 #endif /* !_amigados    */
16526
16527 char *
16528 StrSave (char *s)
16529 {
16530   char *ret;
16531
16532   if ((ret = (char *) malloc(strlen(s) + 1)))
16533     {
16534       safeStrCpy(ret, s, strlen(s)+1);
16535     }
16536   return ret;
16537 }
16538
16539 char *
16540 StrSavePtr (char *s, char **savePtr)
16541 {
16542     if (*savePtr) {
16543         free(*savePtr);
16544     }
16545     if ((*savePtr = (char *) malloc(strlen(s) + 1))) {
16546       safeStrCpy(*savePtr, s, strlen(s)+1);
16547     }
16548     return(*savePtr);
16549 }
16550
16551 char *
16552 PGNDate ()
16553 {
16554     time_t clock;
16555     struct tm *tm;
16556     char buf[MSG_SIZ];
16557
16558     clock = time((time_t *)NULL);
16559     tm = localtime(&clock);
16560     snprintf(buf, MSG_SIZ, "%04d.%02d.%02d",
16561             tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
16562     return StrSave(buf);
16563 }
16564
16565
16566 char *
16567 PositionToFEN (int move, char *overrideCastling)
16568 {
16569     int i, j, fromX, fromY, toX, toY;
16570     int whiteToPlay;
16571     char buf[MSG_SIZ];
16572     char *p, *q;
16573     int emptycount;
16574     ChessSquare piece;
16575
16576     whiteToPlay = (gameMode == EditPosition) ?
16577       !blackPlaysFirst : (move % 2 == 0);
16578     p = buf;
16579
16580     /* Piece placement data */
16581     for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
16582         if(MSG_SIZ - (p - buf) < BOARD_RGHT - BOARD_LEFT + 20) { *p = 0; return StrSave(buf); }
16583         emptycount = 0;
16584         for (j = BOARD_LEFT; j < BOARD_RGHT; j++) {
16585             if (boards[move][i][j] == EmptySquare) {
16586                 emptycount++;
16587             } else { ChessSquare piece = boards[move][i][j];
16588                 if (emptycount > 0) {
16589                     if(emptycount<10) /* [HGM] can be >= 10 */
16590                         *p++ = '0' + emptycount;
16591                     else { *p++ = '0' + emptycount/10; *p++ = '0' + emptycount%10; }
16592                     emptycount = 0;
16593                 }
16594                 if(PieceToChar(piece) == '+') {
16595                     /* [HGM] write promoted pieces as '+<unpromoted>' (Shogi) */
16596                     *p++ = '+';
16597                     piece = (ChessSquare)(DEMOTED piece);
16598                 }
16599                 *p++ = PieceToChar(piece);
16600                 if(p[-1] == '~') {
16601                     /* [HGM] flag promoted pieces as '<promoted>~' (Crazyhouse) */
16602                     p[-1] = PieceToChar((ChessSquare)(DEMOTED piece));
16603                     *p++ = '~';
16604                 }
16605             }
16606         }
16607         if (emptycount > 0) {
16608             if(emptycount<10) /* [HGM] can be >= 10 */
16609                 *p++ = '0' + emptycount;
16610             else { *p++ = '0' + emptycount/10; *p++ = '0' + emptycount%10; }
16611             emptycount = 0;
16612         }
16613         *p++ = '/';
16614     }
16615     *(p - 1) = ' ';
16616
16617     /* [HGM] print Crazyhouse or Shogi holdings */
16618     if( gameInfo.holdingsWidth ) {
16619         *(p-1) = '['; /* if we wanted to support BFEN, this could be '/' */
16620         q = p;
16621         for(i=0; i<gameInfo.holdingsSize; i++) { /* white holdings */
16622             piece = boards[move][i][BOARD_WIDTH-1];
16623             if( piece != EmptySquare )
16624               for(j=0; j<(int) boards[move][i][BOARD_WIDTH-2]; j++)
16625                   *p++ = PieceToChar(piece);
16626         }
16627         for(i=0; i<gameInfo.holdingsSize; i++) { /* black holdings */
16628             piece = boards[move][BOARD_HEIGHT-i-1][0];
16629             if( piece != EmptySquare )
16630               for(j=0; j<(int) boards[move][BOARD_HEIGHT-i-1][1]; j++)
16631                   *p++ = PieceToChar(piece);
16632         }
16633
16634         if( q == p ) *p++ = '-';
16635         *p++ = ']';
16636         *p++ = ' ';
16637     }
16638
16639     /* Active color */
16640     *p++ = whiteToPlay ? 'w' : 'b';
16641     *p++ = ' ';
16642
16643   if(q = overrideCastling) { // [HGM] FRC: override castling & e.p fields for non-compliant engines
16644     while(*p++ = *q++); if(q != overrideCastling+1) p[-1] = ' '; else --p;
16645   } else {
16646   if(nrCastlingRights) {
16647      q = p;
16648      if(gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom) {
16649        /* [HGM] write directly from rights */
16650            if(boards[move][CASTLING][2] != NoRights &&
16651               boards[move][CASTLING][0] != NoRights   )
16652                 *p++ = boards[move][CASTLING][0] + AAA + 'A' - 'a';
16653            if(boards[move][CASTLING][2] != NoRights &&
16654               boards[move][CASTLING][1] != NoRights   )
16655                 *p++ = boards[move][CASTLING][1] + AAA + 'A' - 'a';
16656            if(boards[move][CASTLING][5] != NoRights &&
16657               boards[move][CASTLING][3] != NoRights   )
16658                 *p++ = boards[move][CASTLING][3] + AAA;
16659            if(boards[move][CASTLING][5] != NoRights &&
16660               boards[move][CASTLING][4] != NoRights   )
16661                 *p++ = boards[move][CASTLING][4] + AAA;
16662      } else {
16663
16664         /* [HGM] write true castling rights */
16665         if( nrCastlingRights == 6 ) {
16666             if(boards[move][CASTLING][0] == BOARD_RGHT-1 &&
16667                boards[move][CASTLING][2] != NoRights  ) *p++ = 'K';
16668             if(boards[move][CASTLING][1] == BOARD_LEFT &&
16669                boards[move][CASTLING][2] != NoRights  ) *p++ = 'Q';
16670             if(boards[move][CASTLING][3] == BOARD_RGHT-1 &&
16671                boards[move][CASTLING][5] != NoRights  ) *p++ = 'k';
16672             if(boards[move][CASTLING][4] == BOARD_LEFT &&
16673                boards[move][CASTLING][5] != NoRights  ) *p++ = 'q';
16674         }
16675      }
16676      if (q == p) *p++ = '-'; /* No castling rights */
16677      *p++ = ' ';
16678   }
16679
16680   if(gameInfo.variant != VariantShogi    && gameInfo.variant != VariantXiangqi &&
16681      gameInfo.variant != VariantShatranj && gameInfo.variant != VariantCourier && gameInfo.variant != VariantMakruk ) {
16682     /* En passant target square */
16683     if (move > backwardMostMove) {
16684         fromX = moveList[move - 1][0] - AAA;
16685         fromY = moveList[move - 1][1] - ONE;
16686         toX = moveList[move - 1][2] - AAA;
16687         toY = moveList[move - 1][3] - ONE;
16688         if (fromY == (whiteToPlay ? BOARD_HEIGHT-2 : 1) &&
16689             toY == (whiteToPlay ? BOARD_HEIGHT-4 : 3) &&
16690             boards[move][toY][toX] == (whiteToPlay ? BlackPawn : WhitePawn) &&
16691             fromX == toX) {
16692             /* 2-square pawn move just happened */
16693             *p++ = toX + AAA;
16694             *p++ = whiteToPlay ? '6'+BOARD_HEIGHT-8 : '3';
16695         } else {
16696             *p++ = '-';
16697         }
16698     } else if(move == backwardMostMove) {
16699         // [HGM] perhaps we should always do it like this, and forget the above?
16700         if((signed char)boards[move][EP_STATUS] >= 0) {
16701             *p++ = boards[move][EP_STATUS] + AAA;
16702             *p++ = whiteToPlay ? '6'+BOARD_HEIGHT-8 : '3';
16703         } else {
16704             *p++ = '-';
16705         }
16706     } else {
16707         *p++ = '-';
16708     }
16709     *p++ = ' ';
16710   }
16711   }
16712
16713     /* [HGM] find reversible plies */
16714     {   int i = 0, j=move;
16715
16716         if (appData.debugMode) { int k;
16717             fprintf(debugFP, "write FEN 50-move: %d %d %d\n", initialRulePlies, forwardMostMove, backwardMostMove);
16718             for(k=backwardMostMove; k<=forwardMostMove; k++)
16719                 fprintf(debugFP, "e%d. p=%d\n", k, (signed char)boards[k][EP_STATUS]);
16720
16721         }
16722
16723         while(j > backwardMostMove && (signed char)boards[j][EP_STATUS] <= EP_NONE) j--,i++;
16724         if( j == backwardMostMove ) i += initialRulePlies;
16725         sprintf(p, "%d ", i);
16726         p += i>=100 ? 4 : i >= 10 ? 3 : 2;
16727     }
16728     /* Fullmove number */
16729     sprintf(p, "%d", (move / 2) + 1);
16730
16731     return StrSave(buf);
16732 }
16733
16734 Boolean
16735 ParseFEN (Board board, int *blackPlaysFirst, char *fen)
16736 {
16737     int i, j;
16738     char *p, c;
16739     int emptycount;
16740     ChessSquare piece;
16741
16742     p = fen;
16743
16744     /* [HGM] by default clear Crazyhouse holdings, if present */
16745     if(gameInfo.holdingsWidth) {
16746        for(i=0; i<BOARD_HEIGHT; i++) {
16747            board[i][0]             = EmptySquare; /* black holdings */
16748            board[i][BOARD_WIDTH-1] = EmptySquare; /* white holdings */
16749            board[i][1]             = (ChessSquare) 0; /* black counts */
16750            board[i][BOARD_WIDTH-2] = (ChessSquare) 0; /* white counts */
16751        }
16752     }
16753
16754     /* Piece placement data */
16755     for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
16756         j = 0;
16757         for (;;) {
16758             if (*p == '/' || *p == ' ' || (*p == '[' && i == 0) ) {
16759                 if (*p == '/') p++;
16760                 emptycount = gameInfo.boardWidth - j;
16761                 while (emptycount--)
16762                         board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
16763                 break;
16764 #if(BOARD_FILES >= 10)
16765             } else if(*p=='x' || *p=='X') { /* [HGM] X means 10 */
16766                 p++; emptycount=10;
16767                 if (j + emptycount > gameInfo.boardWidth) return FALSE;
16768                 while (emptycount--)
16769                         board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
16770 #endif
16771             } else if (isdigit(*p)) {
16772                 emptycount = *p++ - '0';
16773                 while(isdigit(*p)) emptycount = 10*emptycount + *p++ - '0'; /* [HGM] allow > 9 */
16774                 if (j + emptycount > gameInfo.boardWidth) return FALSE;
16775                 while (emptycount--)
16776                         board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
16777             } else if (*p == '+' || isalpha(*p)) {
16778                 if (j >= gameInfo.boardWidth) return FALSE;
16779                 if(*p=='+') {
16780                     piece = CharToPiece(*++p);
16781                     if(piece == EmptySquare) return FALSE; /* unknown piece */
16782                     piece = (ChessSquare) (PROMOTED piece ); p++;
16783                     if(PieceToChar(piece) != '+') return FALSE; /* unpromotable piece */
16784                 } else piece = CharToPiece(*p++);
16785
16786                 if(piece==EmptySquare) return FALSE; /* unknown piece */
16787                 if(*p == '~') { /* [HGM] make it a promoted piece for Crazyhouse */
16788                     piece = (ChessSquare) (PROMOTED piece);
16789                     if(PieceToChar(piece) != '~') return FALSE; /* cannot be a promoted piece */
16790                     p++;
16791                 }
16792                 board[i][(j++)+gameInfo.holdingsWidth] = piece;
16793             } else {
16794                 return FALSE;
16795             }
16796         }
16797     }
16798     while (*p == '/' || *p == ' ') p++;
16799
16800     /* [HGM] look for Crazyhouse holdings here */
16801     while(*p==' ') p++;
16802     if( gameInfo.holdingsWidth && p[-1] == '/' || *p == '[') {
16803         if(*p == '[') p++;
16804         if(*p == '-' ) p++; /* empty holdings */ else {
16805             if( !gameInfo.holdingsWidth ) return FALSE; /* no room to put holdings! */
16806             /* if we would allow FEN reading to set board size, we would   */
16807             /* have to add holdings and shift the board read so far here   */
16808             while( (piece = CharToPiece(*p) ) != EmptySquare ) {
16809                 p++;
16810                 if((int) piece >= (int) BlackPawn ) {
16811                     i = (int)piece - (int)BlackPawn;
16812                     i = PieceToNumber((ChessSquare)i);
16813                     if( i >= gameInfo.holdingsSize ) return FALSE;
16814                     board[BOARD_HEIGHT-1-i][0] = piece; /* black holdings */
16815                     board[BOARD_HEIGHT-1-i][1]++;       /* black counts   */
16816                 } else {
16817                     i = (int)piece - (int)WhitePawn;
16818                     i = PieceToNumber((ChessSquare)i);
16819                     if( i >= gameInfo.holdingsSize ) return FALSE;
16820                     board[i][BOARD_WIDTH-1] = piece;    /* white holdings */
16821                     board[i][BOARD_WIDTH-2]++;          /* black holdings */
16822                 }
16823             }
16824         }
16825         if(*p == ']') p++;
16826     }
16827
16828     while(*p == ' ') p++;
16829
16830     /* Active color */
16831     c = *p++;
16832     if(appData.colorNickNames) {
16833       if( c == appData.colorNickNames[0] ) c = 'w'; else
16834       if( c == appData.colorNickNames[1] ) c = 'b';
16835     }
16836     switch (c) {
16837       case 'w':
16838         *blackPlaysFirst = FALSE;
16839         break;
16840       case 'b':
16841         *blackPlaysFirst = TRUE;
16842         break;
16843       default:
16844         return FALSE;
16845     }
16846
16847     /* [HGM] We NO LONGER ignore the rest of the FEN notation */
16848     /* return the extra info in global variiables             */
16849
16850     /* set defaults in case FEN is incomplete */
16851     board[EP_STATUS] = EP_UNKNOWN;
16852     for(i=0; i<nrCastlingRights; i++ ) {
16853         board[CASTLING][i] =
16854             gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom ? NoRights : initialRights[i];
16855     }   /* assume possible unless obviously impossible */
16856     if(initialRights[0]!=NoRights && board[castlingRank[0]][initialRights[0]] != WhiteRook) board[CASTLING][0] = NoRights;
16857     if(initialRights[1]!=NoRights && board[castlingRank[1]][initialRights[1]] != WhiteRook) board[CASTLING][1] = NoRights;
16858     if(initialRights[2]!=NoRights && board[castlingRank[2]][initialRights[2]] != WhiteUnicorn
16859                                   && board[castlingRank[2]][initialRights[2]] != WhiteKing) board[CASTLING][2] = NoRights;
16860     if(initialRights[3]!=NoRights && board[castlingRank[3]][initialRights[3]] != BlackRook) board[CASTLING][3] = NoRights;
16861     if(initialRights[4]!=NoRights && board[castlingRank[4]][initialRights[4]] != BlackRook) board[CASTLING][4] = NoRights;
16862     if(initialRights[5]!=NoRights && board[castlingRank[5]][initialRights[5]] != BlackUnicorn
16863                                   && board[castlingRank[5]][initialRights[5]] != BlackKing) board[CASTLING][5] = NoRights;
16864     FENrulePlies = 0;
16865
16866     while(*p==' ') p++;
16867     if(nrCastlingRights) {
16868       if(*p=='K' || *p=='Q' || *p=='k' || *p=='q' || *p=='-') {
16869           /* castling indicator present, so default becomes no castlings */
16870           for(i=0; i<nrCastlingRights; i++ ) {
16871                  board[CASTLING][i] = NoRights;
16872           }
16873       }
16874       while(*p=='K' || *p=='Q' || *p=='k' || *p=='q' || *p=='-' ||
16875              (gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom) &&
16876              ( *p >= 'a' && *p < 'a' + gameInfo.boardWidth) ||
16877              ( *p >= 'A' && *p < 'A' + gameInfo.boardWidth)   ) {
16878         char c = *p++; int whiteKingFile=NoRights, blackKingFile=NoRights;
16879
16880         for(i=BOARD_LEFT; i<BOARD_RGHT; i++) {
16881             if(board[BOARD_HEIGHT-1][i] == BlackKing) blackKingFile = i;
16882             if(board[0             ][i] == WhiteKing) whiteKingFile = i;
16883         }
16884         if(gameInfo.variant == VariantTwoKings || gameInfo.variant == VariantKnightmate)
16885             whiteKingFile = blackKingFile = BOARD_WIDTH >> 1; // for these variant scanning fails
16886         if(whiteKingFile == NoRights || board[0][whiteKingFile] != WhiteUnicorn
16887                                      && board[0][whiteKingFile] != WhiteKing) whiteKingFile = NoRights;
16888         if(blackKingFile == NoRights || board[BOARD_HEIGHT-1][blackKingFile] != BlackUnicorn
16889                                      && board[BOARD_HEIGHT-1][blackKingFile] != BlackKing) blackKingFile = NoRights;
16890         switch(c) {
16891           case'K':
16892               for(i=BOARD_RGHT-1; board[0][i]!=WhiteRook && i>whiteKingFile; i--);
16893               board[CASTLING][0] = i != whiteKingFile ? i : NoRights;
16894               board[CASTLING][2] = whiteKingFile;
16895               break;
16896           case'Q':
16897               for(i=BOARD_LEFT;  i<BOARD_RGHT && board[0][i]!=WhiteRook && i<whiteKingFile; i++);
16898               board[CASTLING][1] = i != whiteKingFile ? i : NoRights;
16899               board[CASTLING][2] = whiteKingFile;
16900               break;
16901           case'k':
16902               for(i=BOARD_RGHT-1; board[BOARD_HEIGHT-1][i]!=BlackRook && i>blackKingFile; i--);
16903               board[CASTLING][3] = i != blackKingFile ? i : NoRights;
16904               board[CASTLING][5] = blackKingFile;
16905               break;
16906           case'q':
16907               for(i=BOARD_LEFT; i<BOARD_RGHT && board[BOARD_HEIGHT-1][i]!=BlackRook && i<blackKingFile; i++);
16908               board[CASTLING][4] = i != blackKingFile ? i : NoRights;
16909               board[CASTLING][5] = blackKingFile;
16910           case '-':
16911               break;
16912           default: /* FRC castlings */
16913               if(c >= 'a') { /* black rights */
16914                   for(i=BOARD_LEFT; i<BOARD_RGHT; i++)
16915                     if(board[BOARD_HEIGHT-1][i] == BlackKing) break;
16916                   if(i == BOARD_RGHT) break;
16917                   board[CASTLING][5] = i;
16918                   c -= AAA;
16919                   if(board[BOARD_HEIGHT-1][c] <  BlackPawn ||
16920                      board[BOARD_HEIGHT-1][c] >= BlackKing   ) break;
16921                   if(c > i)
16922                       board[CASTLING][3] = c;
16923                   else
16924                       board[CASTLING][4] = c;
16925               } else { /* white rights */
16926                   for(i=BOARD_LEFT; i<BOARD_RGHT; i++)
16927                     if(board[0][i] == WhiteKing) break;
16928                   if(i == BOARD_RGHT) break;
16929                   board[CASTLING][2] = i;
16930                   c -= AAA - 'a' + 'A';
16931                   if(board[0][c] >= WhiteKing) break;
16932                   if(c > i)
16933                       board[CASTLING][0] = c;
16934                   else
16935                       board[CASTLING][1] = c;
16936               }
16937         }
16938       }
16939       for(i=0; i<nrCastlingRights; i++)
16940         if(board[CASTLING][i] != NoRights) initialRights[i] = board[CASTLING][i];
16941     if (appData.debugMode) {
16942         fprintf(debugFP, "FEN castling rights:");
16943         for(i=0; i<nrCastlingRights; i++)
16944         fprintf(debugFP, " %d", board[CASTLING][i]);
16945         fprintf(debugFP, "\n");
16946     }
16947
16948       while(*p==' ') p++;
16949     }
16950
16951     /* read e.p. field in games that know e.p. capture */
16952     if(gameInfo.variant != VariantShogi    && gameInfo.variant != VariantXiangqi &&
16953        gameInfo.variant != VariantShatranj && gameInfo.variant != VariantCourier && gameInfo.variant != VariantMakruk ) {
16954       if(*p=='-') {
16955         p++; board[EP_STATUS] = EP_NONE;
16956       } else {
16957          char c = *p++ - AAA;
16958
16959          if(c < BOARD_LEFT || c >= BOARD_RGHT) return TRUE;
16960          if(*p >= '0' && *p <='9') p++;
16961          board[EP_STATUS] = c;
16962       }
16963     }
16964
16965
16966     if(sscanf(p, "%d", &i) == 1) {
16967         FENrulePlies = i; /* 50-move ply counter */
16968         /* (The move number is still ignored)    */
16969     }
16970
16971     return TRUE;
16972 }
16973
16974 void
16975 EditPositionPasteFEN (char *fen)
16976 {
16977   if (fen != NULL) {
16978     Board initial_position;
16979
16980     if (!ParseFEN(initial_position, &blackPlaysFirst, fen)) {
16981       DisplayError(_("Bad FEN position in clipboard"), 0);
16982       return ;
16983     } else {
16984       int savedBlackPlaysFirst = blackPlaysFirst;
16985       EditPositionEvent();
16986       blackPlaysFirst = savedBlackPlaysFirst;
16987       CopyBoard(boards[0], initial_position);
16988       initialRulePlies = FENrulePlies; /* [HGM] copy FEN attributes as well */
16989       EditPositionDone(FALSE); // [HGM] fake: do not fake rights if we had FEN
16990       DisplayBothClocks();
16991       DrawPosition(FALSE, boards[currentMove]);
16992     }
16993   }
16994 }
16995
16996 static char cseq[12] = "\\   ";
16997
16998 Boolean
16999 set_cont_sequence (char *new_seq)
17000 {
17001     int len;
17002     Boolean ret;
17003
17004     // handle bad attempts to set the sequence
17005         if (!new_seq)
17006                 return 0; // acceptable error - no debug
17007
17008     len = strlen(new_seq);
17009     ret = (len > 0) && (len < sizeof(cseq));
17010     if (ret)
17011       safeStrCpy(cseq, new_seq, sizeof(cseq)/sizeof(cseq[0]));
17012     else if (appData.debugMode)
17013       fprintf(debugFP, "Invalid continuation sequence \"%s\"  (maximum length is: %u)\n", new_seq, (unsigned) sizeof(cseq)-1);
17014     return ret;
17015 }
17016
17017 /*
17018     reformat a source message so words don't cross the width boundary.  internal
17019     newlines are not removed.  returns the wrapped size (no null character unless
17020     included in source message).  If dest is NULL, only calculate the size required
17021     for the dest buffer.  lp argument indicats line position upon entry, and it's
17022     passed back upon exit.
17023 */
17024 int
17025 wrap (char *dest, char *src, int count, int width, int *lp)
17026 {
17027     int len, i, ansi, cseq_len, line, old_line, old_i, old_len, clen;
17028
17029     cseq_len = strlen(cseq);
17030     old_line = line = *lp;
17031     ansi = len = clen = 0;
17032
17033     for (i=0; i < count; i++)
17034     {
17035         if (src[i] == '\033')
17036             ansi = 1;
17037
17038         // if we hit the width, back up
17039         if (!ansi && (line >= width) && src[i] != '\n' && src[i] != ' ')
17040         {
17041             // store i & len in case the word is too long
17042             old_i = i, old_len = len;
17043
17044             // find the end of the last word
17045             while (i && src[i] != ' ' && src[i] != '\n')
17046             {
17047                 i--;
17048                 len--;
17049             }
17050
17051             // word too long?  restore i & len before splitting it
17052             if ((old_i-i+clen) >= width)
17053             {
17054                 i = old_i;
17055                 len = old_len;
17056             }
17057
17058             // extra space?
17059             if (i && src[i-1] == ' ')
17060                 len--;
17061
17062             if (src[i] != ' ' && src[i] != '\n')
17063             {
17064                 i--;
17065                 if (len)
17066                     len--;
17067             }
17068
17069             // now append the newline and continuation sequence
17070             if (dest)
17071                 dest[len] = '\n';
17072             len++;
17073             if (dest)
17074                 strncpy(dest+len, cseq, cseq_len);
17075             len += cseq_len;
17076             line = cseq_len;
17077             clen = cseq_len;
17078             continue;
17079         }
17080
17081         if (dest)
17082             dest[len] = src[i];
17083         len++;
17084         if (!ansi)
17085             line++;
17086         if (src[i] == '\n')
17087             line = 0;
17088         if (src[i] == 'm')
17089             ansi = 0;
17090     }
17091     if (dest && appData.debugMode)
17092     {
17093         fprintf(debugFP, "wrap(count:%d,width:%d,line:%d,len:%d,*lp:%d,src: ",
17094             count, width, line, len, *lp);
17095         show_bytes(debugFP, src, count);
17096         fprintf(debugFP, "\ndest: ");
17097         show_bytes(debugFP, dest, len);
17098         fprintf(debugFP, "\n");
17099     }
17100     *lp = dest ? line : old_line;
17101
17102     return len;
17103 }
17104
17105 // [HGM] vari: routines for shelving variations
17106 Boolean modeRestore = FALSE;
17107
17108 void
17109 PushInner (int firstMove, int lastMove)
17110 {
17111         int i, j, nrMoves = lastMove - firstMove;
17112
17113         // push current tail of game on stack
17114         savedResult[storedGames] = gameInfo.result;
17115         savedDetails[storedGames] = gameInfo.resultDetails;
17116         gameInfo.resultDetails = NULL;
17117         savedFirst[storedGames] = firstMove;
17118         savedLast [storedGames] = lastMove;
17119         savedFramePtr[storedGames] = framePtr;
17120         framePtr -= nrMoves; // reserve space for the boards
17121         for(i=nrMoves; i>=1; i--) { // copy boards to stack, working downwards, in case of overlap
17122             CopyBoard(boards[framePtr+i], boards[firstMove+i]);
17123             for(j=0; j<MOVE_LEN; j++)
17124                 moveList[framePtr+i][j] = moveList[firstMove+i-1][j];
17125             for(j=0; j<2*MOVE_LEN; j++)
17126                 parseList[framePtr+i][j] = parseList[firstMove+i-1][j];
17127             timeRemaining[0][framePtr+i] = timeRemaining[0][firstMove+i];
17128             timeRemaining[1][framePtr+i] = timeRemaining[1][firstMove+i];
17129             pvInfoList[framePtr+i] = pvInfoList[firstMove+i-1];
17130             pvInfoList[firstMove+i-1].depth = 0;
17131             commentList[framePtr+i] = commentList[firstMove+i];
17132             commentList[firstMove+i] = NULL;
17133         }
17134
17135         storedGames++;
17136         forwardMostMove = firstMove; // truncate game so we can start variation
17137 }
17138
17139 void
17140 PushTail (int firstMove, int lastMove)
17141 {
17142         if(appData.icsActive) { // only in local mode
17143                 forwardMostMove = currentMove; // mimic old ICS behavior
17144                 return;
17145         }
17146         if(storedGames >= MAX_VARIATIONS-2) return; // leave one for PV-walk
17147
17148         PushInner(firstMove, lastMove);
17149         if(storedGames == 1) GreyRevert(FALSE);
17150         if(gameMode == PlayFromGameFile) gameMode = EditGame, modeRestore = TRUE;
17151 }
17152
17153 void
17154 PopInner (Boolean annotate)
17155 {
17156         int i, j, nrMoves;
17157         char buf[8000], moveBuf[20];
17158
17159         ToNrEvent(savedFirst[storedGames-1]); // sets currentMove
17160         storedGames--; // do this after ToNrEvent, to make sure HistorySet will refresh entire game after PopInner returns
17161         nrMoves = savedLast[storedGames] - currentMove;
17162         if(annotate) {
17163                 int cnt = 10;
17164                 if(!WhiteOnMove(currentMove))
17165                   snprintf(buf, sizeof(buf)/sizeof(buf[0]),"(%d...", (currentMove+2)>>1);
17166                 else safeStrCpy(buf, "(", sizeof(buf)/sizeof(buf[0]));
17167                 for(i=currentMove; i<forwardMostMove; i++) {
17168                         if(WhiteOnMove(i))
17169                           snprintf(moveBuf, sizeof(moveBuf)/sizeof(moveBuf[0]), " %d. %s", (i+2)>>1, SavePart(parseList[i]));
17170                         else snprintf(moveBuf, sizeof(moveBuf)/sizeof(moveBuf[0])," %s", SavePart(parseList[i]));
17171                         strcat(buf, moveBuf);
17172                         if(commentList[i]) { strcat(buf, " "); strcat(buf, commentList[i]); }
17173                         if(!--cnt) { strcat(buf, "\n"); cnt = 10; }
17174                 }
17175                 strcat(buf, ")");
17176         }
17177         for(i=1; i<=nrMoves; i++) { // copy last variation back
17178             CopyBoard(boards[currentMove+i], boards[framePtr+i]);
17179             for(j=0; j<MOVE_LEN; j++)
17180                 moveList[currentMove+i-1][j] = moveList[framePtr+i][j];
17181             for(j=0; j<2*MOVE_LEN; j++)
17182                 parseList[currentMove+i-1][j] = parseList[framePtr+i][j];
17183             timeRemaining[0][currentMove+i] = timeRemaining[0][framePtr+i];
17184             timeRemaining[1][currentMove+i] = timeRemaining[1][framePtr+i];
17185             pvInfoList[currentMove+i-1] = pvInfoList[framePtr+i];
17186             if(commentList[currentMove+i]) free(commentList[currentMove+i]);
17187             commentList[currentMove+i] = commentList[framePtr+i];
17188             commentList[framePtr+i] = NULL;
17189         }
17190         if(annotate) AppendComment(currentMove+1, buf, FALSE);
17191         framePtr = savedFramePtr[storedGames];
17192         gameInfo.result = savedResult[storedGames];
17193         if(gameInfo.resultDetails != NULL) {
17194             free(gameInfo.resultDetails);
17195       }
17196         gameInfo.resultDetails = savedDetails[storedGames];
17197         forwardMostMove = currentMove + nrMoves;
17198 }
17199
17200 Boolean
17201 PopTail (Boolean annotate)
17202 {
17203         if(appData.icsActive) return FALSE; // only in local mode
17204         if(!storedGames) return FALSE; // sanity
17205         CommentPopDown(); // make sure no stale variation comments to the destroyed line can remain open
17206
17207         PopInner(annotate);
17208         if(currentMove < forwardMostMove) ForwardEvent(); else
17209         HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
17210
17211         if(storedGames == 0) { GreyRevert(TRUE); if(modeRestore) modeRestore = FALSE, gameMode = PlayFromGameFile; }
17212         return TRUE;
17213 }
17214
17215 void
17216 CleanupTail ()
17217 {       // remove all shelved variations
17218         int i;
17219         for(i=0; i<storedGames; i++) {
17220             if(savedDetails[i])
17221                 free(savedDetails[i]);
17222             savedDetails[i] = NULL;
17223         }
17224         for(i=framePtr; i<MAX_MOVES; i++) {
17225                 if(commentList[i]) free(commentList[i]);
17226                 commentList[i] = NULL;
17227         }
17228         framePtr = MAX_MOVES-1;
17229         storedGames = 0;
17230 }
17231
17232 void
17233 LoadVariation (int index, char *text)
17234 {       // [HGM] vari: shelve previous line and load new variation, parsed from text around text[index]
17235         char *p = text, *start = NULL, *end = NULL, wait = NULLCHAR;
17236         int level = 0, move;
17237
17238         if(gameMode != EditGame && gameMode != AnalyzeMode && gameMode != PlayFromGameFile) return;
17239         // first find outermost bracketing variation
17240         while(*p) { // hope I got this right... Non-nesting {} and [] can screen each other and nesting ()
17241             if(!wait) { // while inside [] pr {}, ignore everyting except matching closing ]}
17242                 if(*p == '{') wait = '}'; else
17243                 if(*p == '[') wait = ']'; else
17244                 if(*p == '(' && level++ == 0 && p-text < index) start = p+1;
17245                 if(*p == ')' && level > 0 && --level == 0 && p-text > index && end == NULL) end = p-1;
17246             }
17247             if(*p == wait) wait = NULLCHAR; // closing ]} found
17248             p++;
17249         }
17250         if(!start || !end) return; // no variation found, or syntax error in PGN: ignore click
17251         if(appData.debugMode) fprintf(debugFP, "at move %d load variation '%s'\n", currentMove, start);
17252         end[1] = NULLCHAR; // clip off comment beyond variation
17253         ToNrEvent(currentMove-1);
17254         PushTail(currentMove, forwardMostMove); // shelve main variation. This truncates game
17255         // kludge: use ParsePV() to append variation to game
17256         move = currentMove;
17257         ParsePV(start, TRUE, TRUE);
17258         forwardMostMove = endPV; endPV = -1; currentMove = move; // cleanup what ParsePV did
17259         ClearPremoveHighlights();
17260         CommentPopDown();
17261         ToNrEvent(currentMove+1);
17262 }
17263