2 * backend.c -- Common back end for X and Windows NT versions of
4 * Copyright 1991 by Digital Equipment Corporation, Maynard,
7 * Enhancements Copyright 1992-2001, 2002, 2003, 2004, 2005, 2006,
8 * 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
10 * Enhancements Copyright 2005 Alessandro Scotti
12 * The following terms apply to Digital Equipment Corporation's copyright
14 * ------------------------------------------------------------------------
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.
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
32 * ------------------------------------------------------------------------
34 * The following terms apply to the enhanced version of XBoard
35 * distributed by the Free Software Foundation:
36 * ------------------------------------------------------------------------
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.
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.
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/. *
51 *------------------------------------------------------------------------
52 ** See the file ChangeLog for a revision history. */
54 /* [AS] Also useful here for debugging */
58 #define DoSleep( n ) if( (n) != 0 ) Sleep( (n) );
62 #define DoSleep( n ) if( (n) >= 0) sleep(n)
72 #include <sys/types.h>
81 #else /* not STDC_HEADERS */
84 # else /* not HAVE_STRING_H */
86 # endif /* not HAVE_STRING_H */
87 #endif /* not STDC_HEADERS */
90 # include <sys/fcntl.h>
91 #else /* not HAVE_SYS_FCNTL_H */
94 # endif /* HAVE_FCNTL_H */
95 #endif /* not HAVE_SYS_FCNTL_H */
97 #if TIME_WITH_SYS_TIME
98 # include <sys/time.h>
102 # include <sys/time.h>
108 #if defined(_amigados) && !defined(__GNUC__)
113 extern int gettimeofday(struct timeval *, struct timezone *);
121 #include "frontend.h"
128 #include "backendz.h"
132 # define _(s) gettext (s)
133 # define N_(s) gettext_noop (s)
134 # define T_(s) gettext(s)
147 /* A point in time */
149 long sec; /* Assuming this is >= 32 bits */
150 int ms; /* Assuming this is >= 16 bits */
153 int establish P((void));
154 void read_from_player P((InputSourceRef isr, VOIDSTAR closure,
155 char *buf, int count, int error));
156 void read_from_ics P((InputSourceRef isr, VOIDSTAR closure,
157 char *buf, int count, int error));
158 void ics_printf P((char *format, ...));
159 void SendToICS P((char *s));
160 void SendToICSDelayed P((char *s, long msdelay));
161 void SendMoveToICS P((ChessMove moveType, int fromX, int fromY,
163 void HandleMachineMove P((char *message, ChessProgramState *cps));
164 int AutoPlayOneMove P((void));
165 int LoadGameOneMove P((ChessMove readAhead));
166 int LoadGameFromFile P((char *filename, int n, char *title, int useList));
167 int LoadPositionFromFile P((char *filename, int n, char *title));
168 int SavePositionToFile P((char *filename));
169 void ApplyMove P((int fromX, int fromY, int toX, int toY, int promoChar,
171 void MakeMove P((int fromX, int fromY, int toX, int toY, int promoChar));
172 void ShowMove P((int fromX, int fromY, int toX, int toY));
173 int FinishMove P((ChessMove moveType, int fromX, int fromY, int toX, int toY,
174 /*char*/int promoChar));
175 void BackwardInner P((int target));
176 void ForwardInner P((int target));
177 int Adjudicate P((ChessProgramState *cps));
178 void GameEnds P((ChessMove result, char *resultDetails, int whosays));
179 void EditPositionDone P((Boolean fakeRights));
180 void PrintOpponents P((FILE *fp));
181 void PrintPosition P((FILE *fp, int move));
182 void StartChessProgram P((ChessProgramState *cps));
183 void SendToProgram P((char *message, ChessProgramState *cps));
184 void SendMoveToProgram P((int moveNum, ChessProgramState *cps));
185 void ReceiveFromProgram P((InputSourceRef isr, VOIDSTAR closure,
186 char *buf, int count, int error));
187 void SendTimeControl P((ChessProgramState *cps,
188 int mps, long tc, int inc, int sd, int st));
189 char *TimeControlTagValue P((void));
190 void Attention P((ChessProgramState *cps));
191 void FeedMovesToProgram P((ChessProgramState *cps, int upto));
192 void ResurrectChessProgram P((void));
193 void DisplayComment P((int moveNumber, char *text));
194 void DisplayMove P((int moveNumber));
196 void ParseGameHistory P((char *game));
197 void ParseBoard12 P((char *string));
198 void KeepAlive P((void));
199 void StartClocks P((void));
200 void SwitchClocks P((int nr));
201 void StopClocks P((void));
202 void ResetClocks P((void));
203 char *PGNDate P((void));
204 void SetGameInfo P((void));
205 Boolean ParseFEN P((Board board, int *blackPlaysFirst, char *fen));
206 int RegisterMove P((void));
207 void MakeRegisteredMove P((void));
208 void TruncateGame P((void));
209 int looking_at P((char *, int *, char *));
210 void CopyPlayerNameIntoFileName P((char **, char *));
211 char *SavePart P((char *));
212 int SaveGameOldStyle P((FILE *));
213 int SaveGamePGN P((FILE *));
214 void GetTimeMark P((TimeMark *));
215 long SubtractTimeMarks P((TimeMark *, TimeMark *));
216 int CheckFlags P((void));
217 long NextTickLength P((long));
218 void CheckTimeControl P((void));
219 void show_bytes P((FILE *, char *, int));
220 int string_to_rating P((char *str));
221 void ParseFeatures P((char* args, ChessProgramState *cps));
222 void InitBackEnd3 P((void));
223 void FeatureDone P((ChessProgramState* cps, int val));
224 void InitChessProgram P((ChessProgramState *cps, int setup));
225 void OutputKibitz(int window, char *text);
226 int PerpetualChase(int first, int last);
227 int EngineOutputIsUp();
228 void InitDrawingSizes(int x, int y);
231 extern void ConsoleCreate();
234 ChessProgramState *WhitePlayer();
235 void InsertIntoMemo P((int which, char *text)); // [HGM] kibitz: in engineo.c
236 int VerifyDisplayMode P(());
238 char *GetInfoFromComment( int, char * ); // [HGM] PV time: returns stripped comment
239 void InitEngineUCI( const char * iniDir, ChessProgramState * cps ); // [HGM] moved here from winboard.c
240 char *ProbeBook P((int moveNr, char *book)); // [HGM] book: returns a book move
241 char *SendMoveToBookUser P((int nr, ChessProgramState *cps, int initial)); // [HGM] book
242 void ics_update_width P((int new_width));
243 extern char installDir[MSG_SIZ];
244 VariantClass startVariant; /* [HGM] nicks: initial variant */
246 extern int tinyLayout, smallLayout;
247 ChessProgramStats programStats;
248 char lastPV[2][2*MSG_SIZ]; /* [HGM] pv: last PV in thinking output of each engine */
250 static int exiting = 0; /* [HGM] moved to top */
251 static int setboardSpoiledMachineBlack = 0 /*, errorExitFlag = 0*/;
252 int startedFromPositionFile = FALSE; Board filePosition; /* [HGM] loadPos */
253 Board partnerBoard; /* [HGM] bughouse: for peeking at partner game */
254 int partnerHighlight[2];
255 Boolean partnerBoardValid = 0;
256 char partnerStatus[MSG_SIZ];
258 Boolean originalFlip;
259 Boolean twoBoards = 0;
260 char endingGame = 0; /* [HGM] crash: flag to prevent recursion of GameEnds() */
261 int whiteNPS, blackNPS; /* [HGM] nps: for easily making clocks aware of NPS */
262 VariantClass currentlyInitializedVariant; /* [HGM] variantswitch */
263 int lastIndex = 0; /* [HGM] autoinc: last game/position used in match mode */
264 Boolean connectionAlive;/* [HGM] alive: ICS connection status from probing */
265 int opponentKibitzes;
266 int lastSavedGame; /* [HGM] save: ID of game */
267 char chatPartner[MAX_CHAT][MSG_SIZ]; /* [HGM] chat: list of chatting partners */
268 extern int chatCount;
270 char marker[BOARD_RANKS][BOARD_FILES]; /* [HGM] marks for target squares */
272 /* States for ics_getting_history */
274 #define H_REQUESTED 1
275 #define H_GOT_REQ_HEADER 2
276 #define H_GOT_UNREQ_HEADER 3
277 #define H_GETTING_MOVES 4
278 #define H_GOT_UNWANTED_HEADER 5
280 /* whosays values for GameEnds */
289 /* Maximum number of games in a cmail message */
290 #define CMAIL_MAX_GAMES 20
292 /* Different types of move when calling RegisterMove */
294 #define CMAIL_RESIGN 1
296 #define CMAIL_ACCEPT 3
298 /* Different types of result to remember for each game */
299 #define CMAIL_NOT_RESULT 0
300 #define CMAIL_OLD_RESULT 1
301 #define CMAIL_NEW_RESULT 2
303 /* Telnet protocol constants */
314 static char * safeStrCpy( char * dst, const char * src, size_t count )
316 assert( dst != NULL );
317 assert( src != NULL );
320 strncpy( dst, src, count );
321 dst[ count-1 ] = '\0';
325 /* Some compiler can't cast u64 to double
326 * This function do the job for us:
328 * We use the highest bit for cast, this only
329 * works if the highest bit is not
330 * in use (This should not happen)
332 * We used this for all compiler
335 u64ToDouble(u64 value)
338 u64 tmp = value & u64Const(0x7fffffffffffffff);
339 r = (double)(s64)tmp;
340 if (value & u64Const(0x8000000000000000))
341 r += 9.2233720368547758080e18; /* 2^63 */
345 /* Fake up flags for now, as we aren't keeping track of castling
346 availability yet. [HGM] Change of logic: the flag now only
347 indicates the type of castlings allowed by the rule of the game.
348 The actual rights themselves are maintained in the array
349 castlingRights, as part of the game history, and are not probed
355 int flags = F_ALL_CASTLE_OK;
356 if ((index % 2) == 0) flags |= F_WHITE_ON_MOVE;
357 switch (gameInfo.variant) {
359 flags &= ~F_ALL_CASTLE_OK;
360 case VariantGiveaway: // [HGM] moved this case label one down: seems Giveaway does have castling on ICC!
361 flags |= F_IGNORE_CHECK;
363 flags |= F_MANDATORY_CAPTURE; //[HGM] losers: sets flag so TestLegality rejects non-capts if capts exist
366 flags |= F_IGNORE_CHECK | F_ATOMIC_CAPTURE;
368 case VariantKriegspiel:
369 flags |= F_KRIEGSPIEL_CAPTURE;
371 case VariantCapaRandom:
372 case VariantFischeRandom:
373 flags |= F_FRC_TYPE_CASTLING; /* [HGM] enable this through flag */
374 case VariantNoCastle:
375 case VariantShatranj:
378 flags &= ~F_ALL_CASTLE_OK;
386 FILE *gameFileFP, *debugFP;
389 [AS] Note: sometimes, the sscanf() function is used to parse the input
390 into a fixed-size buffer. Because of this, we must be prepared to
391 receive strings as long as the size of the input buffer, which is currently
392 set to 4K for Windows and 8K for the rest.
393 So, we must either allocate sufficiently large buffers here, or
394 reduce the size of the input buffer in the input reading part.
397 char cmailMove[CMAIL_MAX_GAMES][MOVE_LEN], cmailMsg[MSG_SIZ];
398 char bookOutput[MSG_SIZ*10], thinkOutput[MSG_SIZ*10], lastHint[MSG_SIZ];
399 char thinkOutput1[MSG_SIZ*10];
401 ChessProgramState first, second;
403 /* premove variables */
406 int premoveFromX = 0;
407 int premoveFromY = 0;
408 int premovePromoChar = 0;
410 Boolean alarmSounded;
411 /* end premove variables */
413 char *ics_prefix = "$";
414 int ics_type = ICS_GENERIC;
416 int currentMove = 0, forwardMostMove = 0, backwardMostMove = 0;
417 int pauseExamForwardMostMove = 0;
418 int nCmailGames = 0, nCmailResults = 0, nCmailMovesRegistered = 0;
419 int cmailMoveRegistered[CMAIL_MAX_GAMES], cmailResult[CMAIL_MAX_GAMES];
420 int cmailMsgLoaded = FALSE, cmailMailedMove = FALSE;
421 int cmailOldMove = -1, firstMove = TRUE, flipView = FALSE;
422 int blackPlaysFirst = FALSE, startedFromSetupPosition = FALSE;
423 int searchTime = 0, pausing = FALSE, pauseExamInvalid = FALSE;
424 int whiteFlag = FALSE, blackFlag = FALSE;
425 int userOfferedDraw = FALSE;
426 int ics_user_moved = 0, ics_gamenum = -1, ics_getting_history = H_FALSE;
427 int matchMode = FALSE, hintRequested = FALSE, bookRequested = FALSE;
428 int cmailMoveType[CMAIL_MAX_GAMES];
429 long ics_clock_paused = 0;
430 ProcRef icsPR = NoProc, cmailPR = NoProc;
431 InputSourceRef telnetISR = NULL, fromUserISR = NULL, cmailISR = NULL;
432 GameMode gameMode = BeginningOfGame;
433 char moveList[MAX_MOVES][MOVE_LEN], parseList[MAX_MOVES][MOVE_LEN * 2];
434 char *commentList[MAX_MOVES], *cmailCommentList[CMAIL_MAX_GAMES];
435 ChessProgramStats_Move pvInfoList[MAX_MOVES]; /* [AS] Info about engine thinking */
436 int hiddenThinkOutputState = 0; /* [AS] */
437 int adjudicateLossThreshold = 0; /* [AS] Automatic adjudication */
438 int adjudicateLossPlies = 6;
439 char white_holding[64], black_holding[64];
440 TimeMark lastNodeCountTime;
441 long lastNodeCount=0;
442 int have_sent_ICS_logon = 0;
444 long whiteTimeRemaining, blackTimeRemaining, timeControl, timeIncrement;
445 long timeControl_2; /* [AS] Allow separate time controls */
446 char *fullTimeControlString = NULL; /* [HGM] secondary TC: merge of MPS, TC and inc */
447 long timeRemaining[2][MAX_MOVES];
449 TimeMark programStartTime;
450 char ics_handle[MSG_SIZ];
451 int have_set_title = 0;
453 /* animateTraining preserves the state of appData.animate
454 * when Training mode is activated. This allows the
455 * response to be animated when appData.animate == TRUE and
456 * appData.animateDragging == TRUE.
458 Boolean animateTraining;
464 Board boards[MAX_MOVES];
465 /* [HGM] Following 7 needed for accurate legality tests: */
466 signed char castlingRank[BOARD_FILES]; // and corresponding ranks
467 signed char initialRights[BOARD_FILES];
468 int nrCastlingRights; // For TwoKings, or to implement castling-unknown status
469 int initialRulePlies, FENrulePlies;
470 FILE *serverMoves = NULL; // next two for broadcasting (/serverMoves option)
473 int mute; // mute all sounds
475 // [HGM] vari: next 12 to save and restore variations
476 #define MAX_VARIATIONS 10
477 int framePtr = MAX_MOVES-1; // points to free stack entry
479 int savedFirst[MAX_VARIATIONS];
480 int savedLast[MAX_VARIATIONS];
481 int savedFramePtr[MAX_VARIATIONS];
482 char *savedDetails[MAX_VARIATIONS];
483 ChessMove savedResult[MAX_VARIATIONS];
485 void PushTail P((int firstMove, int lastMove));
486 Boolean PopTail P((Boolean annotate));
487 void CleanupTail P((void));
489 ChessSquare FIDEArray[2][BOARD_FILES] = {
490 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
491 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
492 { BlackRook, BlackKnight, BlackBishop, BlackQueen,
493 BlackKing, BlackBishop, BlackKnight, BlackRook }
496 ChessSquare twoKingsArray[2][BOARD_FILES] = {
497 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
498 WhiteKing, WhiteKing, WhiteKnight, WhiteRook },
499 { BlackRook, BlackKnight, BlackBishop, BlackQueen,
500 BlackKing, BlackKing, BlackKnight, BlackRook }
503 ChessSquare KnightmateArray[2][BOARD_FILES] = {
504 { WhiteRook, WhiteMan, WhiteBishop, WhiteQueen,
505 WhiteUnicorn, WhiteBishop, WhiteMan, WhiteRook },
506 { BlackRook, BlackMan, BlackBishop, BlackQueen,
507 BlackUnicorn, BlackBishop, BlackMan, BlackRook }
510 ChessSquare fairyArray[2][BOARD_FILES] = { /* [HGM] Queen side differs from King side */
511 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
512 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
513 { BlackLance, BlackAlfil, BlackMarshall, BlackAngel,
514 BlackKing, BlackMarshall, BlackAlfil, BlackLance }
517 ChessSquare ShatranjArray[2][BOARD_FILES] = { /* [HGM] (movGen knows about Shatranj Q and P) */
518 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteKing,
519 WhiteFerz, WhiteAlfil, WhiteKnight, WhiteRook },
520 { BlackRook, BlackKnight, BlackAlfil, BlackKing,
521 BlackFerz, BlackAlfil, BlackKnight, BlackRook }
524 ChessSquare makrukArray[2][BOARD_FILES] = { /* [HGM] (movGen knows about Shatranj Q and P) */
525 { WhiteRook, WhiteKnight, WhiteMan, WhiteKing,
526 WhiteFerz, WhiteMan, WhiteKnight, WhiteRook },
527 { BlackRook, BlackKnight, BlackMan, BlackFerz,
528 BlackKing, BlackMan, BlackKnight, BlackRook }
532 #if (BOARD_FILES>=10)
533 ChessSquare ShogiArray[2][BOARD_FILES] = {
534 { WhiteQueen, WhiteKnight, WhiteFerz, WhiteWazir,
535 WhiteKing, WhiteWazir, WhiteFerz, WhiteKnight, WhiteQueen },
536 { BlackQueen, BlackKnight, BlackFerz, BlackWazir,
537 BlackKing, BlackWazir, BlackFerz, BlackKnight, BlackQueen }
540 ChessSquare XiangqiArray[2][BOARD_FILES] = {
541 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteFerz,
542 WhiteWazir, WhiteFerz, WhiteAlfil, WhiteKnight, WhiteRook },
543 { BlackRook, BlackKnight, BlackAlfil, BlackFerz,
544 BlackWazir, BlackFerz, BlackAlfil, BlackKnight, BlackRook }
547 ChessSquare CapablancaArray[2][BOARD_FILES] = {
548 { WhiteRook, WhiteKnight, WhiteAngel, WhiteBishop, WhiteQueen,
549 WhiteKing, WhiteBishop, WhiteMarshall, WhiteKnight, WhiteRook },
550 { BlackRook, BlackKnight, BlackAngel, BlackBishop, BlackQueen,
551 BlackKing, BlackBishop, BlackMarshall, BlackKnight, BlackRook }
554 ChessSquare GreatArray[2][BOARD_FILES] = {
555 { WhiteDragon, WhiteKnight, WhiteAlfil, WhiteGrasshopper, WhiteKing,
556 WhiteSilver, WhiteCardinal, WhiteAlfil, WhiteKnight, WhiteDragon },
557 { BlackDragon, BlackKnight, BlackAlfil, BlackGrasshopper, BlackKing,
558 BlackSilver, BlackCardinal, BlackAlfil, BlackKnight, BlackDragon },
561 ChessSquare JanusArray[2][BOARD_FILES] = {
562 { WhiteRook, WhiteAngel, WhiteKnight, WhiteBishop, WhiteKing,
563 WhiteQueen, WhiteBishop, WhiteKnight, WhiteAngel, WhiteRook },
564 { BlackRook, BlackAngel, BlackKnight, BlackBishop, BlackKing,
565 BlackQueen, BlackBishop, BlackKnight, BlackAngel, BlackRook }
569 ChessSquare GothicArray[2][BOARD_FILES] = {
570 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen, WhiteMarshall,
571 WhiteKing, WhiteAngel, WhiteBishop, WhiteKnight, WhiteRook },
572 { BlackRook, BlackKnight, BlackBishop, BlackQueen, BlackMarshall,
573 BlackKing, BlackAngel, BlackBishop, BlackKnight, BlackRook }
576 #define GothicArray CapablancaArray
580 ChessSquare FalconArray[2][BOARD_FILES] = {
581 { WhiteRook, WhiteKnight, WhiteBishop, WhiteLance, WhiteQueen,
582 WhiteKing, WhiteLance, WhiteBishop, WhiteKnight, WhiteRook },
583 { BlackRook, BlackKnight, BlackBishop, BlackLance, BlackQueen,
584 BlackKing, BlackLance, BlackBishop, BlackKnight, BlackRook }
587 #define FalconArray CapablancaArray
590 #else // !(BOARD_FILES>=10)
591 #define XiangqiPosition FIDEArray
592 #define CapablancaArray FIDEArray
593 #define GothicArray FIDEArray
594 #define GreatArray FIDEArray
595 #endif // !(BOARD_FILES>=10)
597 #if (BOARD_FILES>=12)
598 ChessSquare CourierArray[2][BOARD_FILES] = {
599 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteBishop, WhiteMan, WhiteKing,
600 WhiteFerz, WhiteWazir, WhiteBishop, WhiteAlfil, WhiteKnight, WhiteRook },
601 { BlackRook, BlackKnight, BlackAlfil, BlackBishop, BlackMan, BlackKing,
602 BlackFerz, BlackWazir, BlackBishop, BlackAlfil, BlackKnight, BlackRook }
604 #else // !(BOARD_FILES>=12)
605 #define CourierArray CapablancaArray
606 #endif // !(BOARD_FILES>=12)
609 Board initialPosition;
612 /* Convert str to a rating. Checks for special cases of "----",
614 "++++", etc. Also strips ()'s */
616 string_to_rating(str)
619 while(*str && !isdigit(*str)) ++str;
621 return 0; /* One of the special "no rating" cases */
629 /* Init programStats */
630 programStats.movelist[0] = 0;
631 programStats.depth = 0;
632 programStats.nr_moves = 0;
633 programStats.moves_left = 0;
634 programStats.nodes = 0;
635 programStats.time = -1; // [HGM] PGNtime: make invalid to recognize engine output
636 programStats.score = 0;
637 programStats.got_only_move = 0;
638 programStats.got_fail = 0;
639 programStats.line_is_book = 0;
645 int matched, min, sec;
647 ShowThinkingEvent(); // [HGM] thinking: make sure post/nopost state is set according to options
648 startVariant = StringToVariant(appData.variant); // [HGM] nicks: remember original variant
650 GetTimeMark(&programStartTime);
651 srandom((programStartTime.ms + 1000*programStartTime.sec)*0x1001001); // [HGM] book: makes sure random is unpredictabe to msec level
654 programStats.ok_to_send = 1;
655 programStats.seen_stat = 0;
658 * Initialize game list
664 * Internet chess server status
666 if (appData.icsActive) {
667 appData.matchMode = FALSE;
668 appData.matchGames = 0;
670 appData.noChessProgram = !appData.zippyPlay;
672 appData.zippyPlay = FALSE;
673 appData.zippyTalk = FALSE;
674 appData.noChessProgram = TRUE;
676 if (*appData.icsHelper != NULLCHAR) {
677 appData.useTelnet = TRUE;
678 appData.telnetProgram = appData.icsHelper;
681 appData.zippyTalk = appData.zippyPlay = FALSE;
684 /* [AS] Initialize pv info list [HGM] and game state */
688 for( i=0; i<=framePtr; i++ ) {
689 pvInfoList[i].depth = -1;
690 boards[i][EP_STATUS] = EP_NONE;
691 for( j=0; j<BOARD_FILES-2; j++ ) boards[i][CASTLING][j] = NoRights;
696 * Parse timeControl resource
698 if (!ParseTimeControl(appData.timeControl, appData.timeIncrement,
699 appData.movesPerSession)) {
701 snprintf(buf, sizeof(buf), _("bad timeControl option %s"), appData.timeControl);
702 DisplayFatalError(buf, 0, 2);
706 * Parse searchTime resource
708 if (*appData.searchTime != NULLCHAR) {
709 matched = sscanf(appData.searchTime, "%d:%d", &min, &sec);
711 searchTime = min * 60;
712 } else if (matched == 2) {
713 searchTime = min * 60 + sec;
716 snprintf(buf, sizeof(buf), _("bad searchTime option %s"), appData.searchTime);
717 DisplayFatalError(buf, 0, 2);
721 /* [AS] Adjudication threshold */
722 adjudicateLossThreshold = appData.adjudicateLossThreshold;
724 first.which = _("first");
725 second.which = _("second");
726 first.maybeThinking = second.maybeThinking = FALSE;
727 first.pr = second.pr = NoProc;
728 first.isr = second.isr = NULL;
729 first.sendTime = second.sendTime = 2;
730 first.sendDrawOffers = 1;
731 if (appData.firstPlaysBlack) {
732 first.twoMachinesColor = "black\n";
733 second.twoMachinesColor = "white\n";
735 first.twoMachinesColor = "white\n";
736 second.twoMachinesColor = "black\n";
738 first.program = appData.firstChessProgram;
739 second.program = appData.secondChessProgram;
740 first.host = appData.firstHost;
741 second.host = appData.secondHost;
742 first.dir = appData.firstDirectory;
743 second.dir = appData.secondDirectory;
744 first.other = &second;
745 second.other = &first;
746 first.initString = appData.initString;
747 second.initString = appData.secondInitString;
748 first.computerString = appData.firstComputerString;
749 second.computerString = appData.secondComputerString;
750 first.useSigint = second.useSigint = TRUE;
751 first.useSigterm = second.useSigterm = TRUE;
752 first.reuse = appData.reuseFirst;
753 second.reuse = appData.reuseSecond;
754 first.nps = appData.firstNPS; // [HGM] nps: copy nodes per second
755 second.nps = appData.secondNPS;
756 first.useSetboard = second.useSetboard = FALSE;
757 first.useSAN = second.useSAN = FALSE;
758 first.usePing = second.usePing = FALSE;
759 first.lastPing = second.lastPing = 0;
760 first.lastPong = second.lastPong = 0;
761 first.usePlayother = second.usePlayother = FALSE;
762 first.useColors = second.useColors = TRUE;
763 first.useUsermove = second.useUsermove = FALSE;
764 first.sendICS = second.sendICS = FALSE;
765 first.sendName = second.sendName = appData.icsActive;
766 first.sdKludge = second.sdKludge = FALSE;
767 first.stKludge = second.stKludge = FALSE;
768 TidyProgramName(first.program, first.host, first.tidy);
769 TidyProgramName(second.program, second.host, second.tidy);
770 first.matchWins = second.matchWins = 0;
771 strcpy(first.variants, appData.variant);
772 strcpy(second.variants, appData.variant);
773 first.analysisSupport = second.analysisSupport = 2; /* detect */
774 first.analyzing = second.analyzing = FALSE;
775 first.initDone = second.initDone = FALSE;
777 /* New features added by Tord: */
778 first.useFEN960 = FALSE; second.useFEN960 = FALSE;
779 first.useOOCastle = TRUE; second.useOOCastle = TRUE;
780 /* End of new features added by Tord. */
781 first.fenOverride = appData.fenOverride1;
782 second.fenOverride = appData.fenOverride2;
784 /* [HGM] time odds: set factor for each machine */
785 first.timeOdds = appData.firstTimeOdds;
786 second.timeOdds = appData.secondTimeOdds;
788 if(appData.timeOddsMode) {
789 norm = first.timeOdds;
790 if(norm > second.timeOdds) norm = second.timeOdds;
792 first.timeOdds /= norm;
793 second.timeOdds /= norm;
796 /* [HGM] secondary TC: how to handle sessions that do not fit in 'level'*/
797 first.accumulateTC = appData.firstAccumulateTC;
798 second.accumulateTC = appData.secondAccumulateTC;
799 first.maxNrOfSessions = second.maxNrOfSessions = 1;
802 first.debug = second.debug = FALSE;
803 first.supportsNPS = second.supportsNPS = UNKNOWN;
806 first.optionSettings = appData.firstOptions;
807 second.optionSettings = appData.secondOptions;
809 first.scoreIsAbsolute = appData.firstScoreIsAbsolute; /* [AS] */
810 second.scoreIsAbsolute = appData.secondScoreIsAbsolute; /* [AS] */
811 first.isUCI = appData.firstIsUCI; /* [AS] */
812 second.isUCI = appData.secondIsUCI; /* [AS] */
813 first.hasOwnBookUCI = appData.firstHasOwnBookUCI; /* [AS] */
814 second.hasOwnBookUCI = appData.secondHasOwnBookUCI; /* [AS] */
816 if (appData.firstProtocolVersion > PROTOVER ||
817 appData.firstProtocolVersion < 1) {
819 sprintf(buf, _("protocol version %d not supported"),
820 appData.firstProtocolVersion);
821 DisplayFatalError(buf, 0, 2);
823 first.protocolVersion = appData.firstProtocolVersion;
826 if (appData.secondProtocolVersion > PROTOVER ||
827 appData.secondProtocolVersion < 1) {
829 sprintf(buf, _("protocol version %d not supported"),
830 appData.secondProtocolVersion);
831 DisplayFatalError(buf, 0, 2);
833 second.protocolVersion = appData.secondProtocolVersion;
836 if (appData.icsActive) {
837 appData.clockMode = TRUE; /* changes dynamically in ICS mode */
838 // } else if (*appData.searchTime != NULLCHAR || appData.noChessProgram) {
839 } else if (appData.noChessProgram) { // [HGM] st: searchTime mode now also is clockMode
840 appData.clockMode = FALSE;
841 first.sendTime = second.sendTime = 0;
845 /* Override some settings from environment variables, for backward
846 compatibility. Unfortunately it's not feasible to have the env
847 vars just set defaults, at least in xboard. Ugh.
849 if (appData.icsActive && (appData.zippyPlay || appData.zippyTalk)) {
854 if (appData.noChessProgram) {
855 programVersion = (char*) malloc(5 + strlen(PACKAGE_STRING));
856 sprintf(programVersion, "%s", PACKAGE_STRING);
858 /* [HGM] tidy: use tidy name, in stead of full pathname (which was probably a bug due to / vs \ ) */
859 programVersion = (char*) malloc(8 + strlen(PACKAGE_STRING) + strlen(first.tidy));
860 sprintf(programVersion, "%s + %s", PACKAGE_STRING, first.tidy);
863 if (!appData.icsActive) {
865 /* Check for variants that are supported only in ICS mode,
866 or not at all. Some that are accepted here nevertheless
867 have bugs; see comments below.
869 VariantClass variant = StringToVariant(appData.variant);
871 case VariantBughouse: /* need four players and two boards */
872 case VariantKriegspiel: /* need to hide pieces and move details */
873 /* case VariantFischeRandom: (Fabien: moved below) */
874 sprintf(buf, _("Variant %s supported only in ICS mode"), appData.variant);
875 DisplayFatalError(buf, 0, 2);
879 case VariantLoadable:
889 sprintf(buf, _("Unknown variant name %s"), appData.variant);
890 DisplayFatalError(buf, 0, 2);
893 case VariantXiangqi: /* [HGM] repetition rules not implemented */
894 case VariantFairy: /* [HGM] TestLegality definitely off! */
895 case VariantGothic: /* [HGM] should work */
896 case VariantCapablanca: /* [HGM] should work */
897 case VariantCourier: /* [HGM] initial forced moves not implemented */
898 case VariantShogi: /* [HGM] could still mate with pawn drop */
899 case VariantKnightmate: /* [HGM] should work */
900 case VariantCylinder: /* [HGM] untested */
901 case VariantFalcon: /* [HGM] untested */
902 case VariantCrazyhouse: /* holdings not shown, ([HGM] fixed that!)
903 offboard interposition not understood */
904 case VariantNormal: /* definitely works! */
905 case VariantWildCastle: /* pieces not automatically shuffled */
906 case VariantNoCastle: /* pieces not automatically shuffled */
907 case VariantFischeRandom: /* [HGM] works and shuffles pieces */
908 case VariantLosers: /* should work except for win condition,
909 and doesn't know captures are mandatory */
910 case VariantSuicide: /* should work except for win condition,
911 and doesn't know captures are mandatory */
912 case VariantGiveaway: /* should work except for win condition,
913 and doesn't know captures are mandatory */
914 case VariantTwoKings: /* should work */
915 case VariantAtomic: /* should work except for win condition */
916 case Variant3Check: /* should work except for win condition */
917 case VariantShatranj: /* should work except for all win conditions */
918 case VariantMakruk: /* should work except for daw countdown */
919 case VariantBerolina: /* might work if TestLegality is off */
920 case VariantCapaRandom: /* should work */
921 case VariantJanus: /* should work */
922 case VariantSuper: /* experimental */
923 case VariantGreat: /* experimental, requires legality testing to be off */
928 InitEngineUCI( installDir, &first ); // [HGM] moved here from winboard.c, to make available in xboard
929 InitEngineUCI( installDir, &second );
932 int NextIntegerFromString( char ** str, long * value )
937 while( *s == ' ' || *s == '\t' ) {
943 if( *s >= '0' && *s <= '9' ) {
944 while( *s >= '0' && *s <= '9' ) {
945 *value = *value * 10 + (*s - '0');
957 int NextTimeControlFromString( char ** str, long * value )
960 int result = NextIntegerFromString( str, &temp );
963 *value = temp * 60; /* Minutes */
966 result = NextIntegerFromString( str, &temp );
967 *value += temp; /* Seconds */
974 int NextSessionFromString( char ** str, int *moves, long * tc, long *inc)
975 { /* [HGM] routine added to read '+moves/time' for secondary time control */
976 int result = -1; long temp, temp2;
978 if(**str != '+') return -1; // old params remain in force!
980 if( NextTimeControlFromString( str, &temp ) ) return -1;
983 /* time only: incremental or sudden-death time control */
984 if(**str == '+') { /* increment follows; read it */
986 if(result = NextIntegerFromString( str, &temp2)) return -1;
989 *moves = 0; *tc = temp * 1000;
991 } else if(temp % 60 != 0) return -1; /* moves was given as min:sec */
993 (*str)++; /* classical time control */
994 result = NextTimeControlFromString( str, &temp2);
1003 int GetTimeQuota(int movenr)
1004 { /* [HGM] get time to add from the multi-session time-control string */
1005 int moves=1; /* kludge to force reading of first session */
1006 long time, increment;
1007 char *s = fullTimeControlString;
1009 if(appData.debugMode) fprintf(debugFP, "TC string = '%s'\n", fullTimeControlString);
1011 if(moves) NextSessionFromString(&s, &moves, &time, &increment);
1012 if(appData.debugMode) fprintf(debugFP, "mps=%d tc=%d inc=%d\n", moves, (int) time, (int) increment);
1013 if(movenr == -1) return time; /* last move before new session */
1014 if(!moves) return increment; /* current session is incremental */
1015 if(movenr >= 0) movenr -= moves; /* we already finished this session */
1016 } while(movenr >= -1); /* try again for next session */
1018 return 0; // no new time quota on this move
1022 ParseTimeControl(tc, ti, mps)
1031 if(ti >= 0 && !strchr(tc, '+') && !strchr(tc, '/') ) mps = 0;
1034 sprintf(buf, "+%d/%s+%d", mps, tc, ti);
1035 else sprintf(buf, "+%s+%d", tc, ti);
1038 sprintf(buf, "+%d/%s", mps, tc);
1039 else sprintf(buf, "+%s", tc);
1041 fullTimeControlString = StrSave(buf);
1043 if( NextTimeControlFromString( &tc, &tc1 ) != 0 ) {
1048 /* Parse second time control */
1051 if( NextTimeControlFromString( &tc, &tc2 ) != 0 ) {
1059 timeControl_2 = tc2 * 1000;
1069 timeControl = tc1 * 1000;
1072 timeIncrement = ti * 1000; /* convert to ms */
1073 movesPerSession = 0;
1076 movesPerSession = mps;
1084 if (appData.debugMode) {
1085 fprintf(debugFP, "%s\n", programVersion);
1088 set_cont_sequence(appData.wrapContSeq);
1089 if (appData.matchGames > 0) {
1090 appData.matchMode = TRUE;
1091 } else if (appData.matchMode) {
1092 appData.matchGames = 1;
1094 if(appData.matchMode && appData.sameColorGames > 0) /* [HGM] alternate: overrule matchGames */
1095 appData.matchGames = appData.sameColorGames;
1096 if(appData.rewindIndex > 1) { /* [HGM] autoinc: rewind implies auto-increment and overrules given index */
1097 if(appData.loadPositionIndex >= 0) appData.loadPositionIndex = -1;
1098 if(appData.loadGameIndex >= 0) appData.loadGameIndex = -1;
1101 if (appData.noChessProgram || first.protocolVersion == 1) {
1104 /* kludge: allow timeout for initial "feature" commands */
1106 DisplayMessage("", _("Starting chess program"));
1107 ScheduleDelayedEvent(InitBackEnd3, FEATURE_TIMEOUT);
1112 InitBackEnd3 P((void))
1114 GameMode initialMode;
1118 InitChessProgram(&first, startedFromSetupPosition);
1120 if(!appData.noChessProgram) { /* [HGM] tidy: redo program version to use name from myname feature */
1121 free(programVersion);
1122 programVersion = (char*) malloc(8 + strlen(PACKAGE_STRING) + strlen(first.tidy));
1123 sprintf(programVersion, "%s + %s", PACKAGE_STRING, first.tidy);
1126 if (appData.icsActive) {
1128 /* [DM] Make a console window if needed [HGM] merged ifs */
1133 if (*appData.icsCommPort != NULLCHAR) {
1134 sprintf(buf, _("Could not open comm port %s"),
1135 appData.icsCommPort);
1137 snprintf(buf, sizeof(buf), _("Could not connect to host %s, port %s"),
1138 appData.icsHost, appData.icsPort);
1140 DisplayFatalError(buf, err, 1);
1145 AddInputSource(icsPR, FALSE, read_from_ics, &telnetISR);
1147 AddInputSource(NoProc, FALSE, read_from_player, &fromUserISR);
1148 if(appData.keepAlive) // [HGM] alive: schedule sending of dummy 'date' command
1149 ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
1150 } else if (appData.noChessProgram) {
1156 if (*appData.cmailGameName != NULLCHAR) {
1158 OpenLoopback(&cmailPR);
1160 AddInputSource(cmailPR, FALSE, CmailSigHandlerCallBack, &cmailISR);
1164 DisplayMessage("", "");
1165 if (StrCaseCmp(appData.initialMode, "") == 0) {
1166 initialMode = BeginningOfGame;
1167 } else if (StrCaseCmp(appData.initialMode, "TwoMachines") == 0) {
1168 initialMode = TwoMachinesPlay;
1169 } else if (StrCaseCmp(appData.initialMode, "AnalyzeFile") == 0) {
1170 initialMode = AnalyzeFile;
1171 } else if (StrCaseCmp(appData.initialMode, "Analysis") == 0) {
1172 initialMode = AnalyzeMode;
1173 } else if (StrCaseCmp(appData.initialMode, "MachineWhite") == 0) {
1174 initialMode = MachinePlaysWhite;
1175 } else if (StrCaseCmp(appData.initialMode, "MachineBlack") == 0) {
1176 initialMode = MachinePlaysBlack;
1177 } else if (StrCaseCmp(appData.initialMode, "EditGame") == 0) {
1178 initialMode = EditGame;
1179 } else if (StrCaseCmp(appData.initialMode, "EditPosition") == 0) {
1180 initialMode = EditPosition;
1181 } else if (StrCaseCmp(appData.initialMode, "Training") == 0) {
1182 initialMode = Training;
1184 sprintf(buf, _("Unknown initialMode %s"), appData.initialMode);
1185 DisplayFatalError(buf, 0, 2);
1189 if (appData.matchMode) {
1190 /* Set up machine vs. machine match */
1191 if (appData.noChessProgram) {
1192 DisplayFatalError(_("Can't have a match with no chess programs"),
1198 if (*appData.loadGameFile != NULLCHAR) {
1199 int index = appData.loadGameIndex; // [HGM] autoinc
1200 if(index<0) lastIndex = index = 1;
1201 if (!LoadGameFromFile(appData.loadGameFile,
1203 appData.loadGameFile, FALSE)) {
1204 DisplayFatalError(_("Bad game file"), 0, 1);
1207 } else if (*appData.loadPositionFile != NULLCHAR) {
1208 int index = appData.loadPositionIndex; // [HGM] autoinc
1209 if(index<0) lastIndex = index = 1;
1210 if (!LoadPositionFromFile(appData.loadPositionFile,
1212 appData.loadPositionFile)) {
1213 DisplayFatalError(_("Bad position file"), 0, 1);
1218 } else if (*appData.cmailGameName != NULLCHAR) {
1219 /* Set up cmail mode */
1220 ReloadCmailMsgEvent(TRUE);
1222 /* Set up other modes */
1223 if (initialMode == AnalyzeFile) {
1224 if (*appData.loadGameFile == NULLCHAR) {
1225 DisplayFatalError(_("AnalyzeFile mode requires a game file"), 0, 1);
1229 if (*appData.loadGameFile != NULLCHAR) {
1230 (void) LoadGameFromFile(appData.loadGameFile,
1231 appData.loadGameIndex,
1232 appData.loadGameFile, TRUE);
1233 } else if (*appData.loadPositionFile != NULLCHAR) {
1234 (void) LoadPositionFromFile(appData.loadPositionFile,
1235 appData.loadPositionIndex,
1236 appData.loadPositionFile);
1237 /* [HGM] try to make self-starting even after FEN load */
1238 /* to allow automatic setup of fairy variants with wtm */
1239 if(initialMode == BeginningOfGame && !blackPlaysFirst) {
1240 gameMode = BeginningOfGame;
1241 setboardSpoiledMachineBlack = 1;
1243 /* [HGM] loadPos: make that every new game uses the setup */
1244 /* from file as long as we do not switch variant */
1245 if(!blackPlaysFirst) {
1246 startedFromPositionFile = TRUE;
1247 CopyBoard(filePosition, boards[0]);
1250 if (initialMode == AnalyzeMode) {
1251 if (appData.noChessProgram) {
1252 DisplayFatalError(_("Analysis mode requires a chess engine"), 0, 2);
1255 if (appData.icsActive) {
1256 DisplayFatalError(_("Analysis mode does not work with ICS mode"),0,2);
1260 } else if (initialMode == AnalyzeFile) {
1261 appData.showThinking = TRUE; // [HGM] thinking: moved out of ShowThinkingEvent
1262 ShowThinkingEvent();
1264 AnalysisPeriodicEvent(1);
1265 } else if (initialMode == MachinePlaysWhite) {
1266 if (appData.noChessProgram) {
1267 DisplayFatalError(_("MachineWhite mode requires a chess engine"),
1271 if (appData.icsActive) {
1272 DisplayFatalError(_("MachineWhite mode does not work with ICS mode"),
1276 MachineWhiteEvent();
1277 } else if (initialMode == MachinePlaysBlack) {
1278 if (appData.noChessProgram) {
1279 DisplayFatalError(_("MachineBlack mode requires a chess engine"),
1283 if (appData.icsActive) {
1284 DisplayFatalError(_("MachineBlack mode does not work with ICS mode"),
1288 MachineBlackEvent();
1289 } else if (initialMode == TwoMachinesPlay) {
1290 if (appData.noChessProgram) {
1291 DisplayFatalError(_("TwoMachines mode requires a chess engine"),
1295 if (appData.icsActive) {
1296 DisplayFatalError(_("TwoMachines mode does not work with ICS mode"),
1301 } else if (initialMode == EditGame) {
1303 } else if (initialMode == EditPosition) {
1304 EditPositionEvent();
1305 } else if (initialMode == Training) {
1306 if (*appData.loadGameFile == NULLCHAR) {
1307 DisplayFatalError(_("Training mode requires a game file"), 0, 2);
1316 * Establish will establish a contact to a remote host.port.
1317 * Sets icsPR to a ProcRef for a process (or pseudo-process)
1318 * used to talk to the host.
1319 * Returns 0 if okay, error code if not.
1326 if (*appData.icsCommPort != NULLCHAR) {
1327 /* Talk to the host through a serial comm port */
1328 return OpenCommPort(appData.icsCommPort, &icsPR);
1330 } else if (*appData.gateway != NULLCHAR) {
1331 if (*appData.remoteShell == NULLCHAR) {
1332 /* Use the rcmd protocol to run telnet program on a gateway host */
1333 snprintf(buf, sizeof(buf), "%s %s %s",
1334 appData.telnetProgram, appData.icsHost, appData.icsPort);
1335 return OpenRcmd(appData.gateway, appData.remoteUser, buf, &icsPR);
1338 /* Use the rsh program to run telnet program on a gateway host */
1339 if (*appData.remoteUser == NULLCHAR) {
1340 snprintf(buf, sizeof(buf), "%s %s %s %s %s", appData.remoteShell,
1341 appData.gateway, appData.telnetProgram,
1342 appData.icsHost, appData.icsPort);
1344 snprintf(buf, sizeof(buf), "%s %s -l %s %s %s %s",
1345 appData.remoteShell, appData.gateway,
1346 appData.remoteUser, appData.telnetProgram,
1347 appData.icsHost, appData.icsPort);
1349 return StartChildProcess(buf, "", &icsPR);
1352 } else if (appData.useTelnet) {
1353 return OpenTelnet(appData.icsHost, appData.icsPort, &icsPR);
1356 /* TCP socket interface differs somewhat between
1357 Unix and NT; handle details in the front end.
1359 return OpenTCP(appData.icsHost, appData.icsPort, &icsPR);
1363 void EscapeExpand(char *p, char *q)
1364 { // [HGM] initstring: routine to shape up string arguments
1365 while(*p++ = *q++) if(p[-1] == '\\')
1367 case 'n': p[-1] = '\n'; break;
1368 case 'r': p[-1] = '\r'; break;
1369 case 't': p[-1] = '\t'; break;
1370 case '\\': p[-1] = '\\'; break;
1371 case 0: *p = 0; return;
1372 default: p[-1] = q[-1]; break;
1377 show_bytes(fp, buf, count)
1383 if (*buf < 040 || *(unsigned char *) buf > 0177) {
1384 fprintf(fp, "\\%03o", *buf & 0xff);
1393 /* Returns an errno value */
1395 OutputMaybeTelnet(pr, message, count, outError)
1401 char buf[8192], *p, *q, *buflim;
1402 int left, newcount, outcount;
1404 if (*appData.icsCommPort != NULLCHAR || appData.useTelnet ||
1405 *appData.gateway != NULLCHAR) {
1406 if (appData.debugMode) {
1407 fprintf(debugFP, ">ICS: ");
1408 show_bytes(debugFP, message, count);
1409 fprintf(debugFP, "\n");
1411 return OutputToProcess(pr, message, count, outError);
1414 buflim = &buf[sizeof(buf)-1]; /* allow 1 byte for expanding last char */
1421 if (appData.debugMode) {
1422 fprintf(debugFP, ">ICS: ");
1423 show_bytes(debugFP, buf, newcount);
1424 fprintf(debugFP, "\n");
1426 outcount = OutputToProcess(pr, buf, newcount, outError);
1427 if (outcount < newcount) return -1; /* to be sure */
1434 } else if (((unsigned char) *p) == TN_IAC) {
1435 *q++ = (char) TN_IAC;
1442 if (appData.debugMode) {
1443 fprintf(debugFP, ">ICS: ");
1444 show_bytes(debugFP, buf, newcount);
1445 fprintf(debugFP, "\n");
1447 outcount = OutputToProcess(pr, buf, newcount, outError);
1448 if (outcount < newcount) return -1; /* to be sure */
1453 read_from_player(isr, closure, message, count, error)
1460 int outError, outCount;
1461 static int gotEof = 0;
1463 /* Pass data read from player on to ICS */
1466 outCount = OutputMaybeTelnet(icsPR, message, count, &outError);
1467 if (outCount < count) {
1468 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1470 } else if (count < 0) {
1471 RemoveInputSource(isr);
1472 DisplayFatalError(_("Error reading from keyboard"), error, 1);
1473 } else if (gotEof++ > 0) {
1474 RemoveInputSource(isr);
1475 DisplayFatalError(_("Got end of file from keyboard"), 0, 0);
1481 { // [HGM] alive: periodically send dummy (date) command to ICS to prevent time-out
1482 if(!connectionAlive) DisplayFatalError("No response from ICS", 0, 1);
1483 connectionAlive = FALSE; // only sticks if no response to 'date' command.
1484 SendToICS("date\n");
1485 if(appData.keepAlive) ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
1488 /* added routine for printf style output to ics */
1489 void ics_printf(char *format, ...)
1491 char buffer[MSG_SIZ];
1494 va_start(args, format);
1495 vsnprintf(buffer, sizeof(buffer), format, args);
1496 buffer[sizeof(buffer)-1] = '\0';
1505 int count, outCount, outError;
1507 if (icsPR == NULL) return;
1510 outCount = OutputMaybeTelnet(icsPR, s, count, &outError);
1511 if (outCount < count) {
1512 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1516 /* This is used for sending logon scripts to the ICS. Sending
1517 without a delay causes problems when using timestamp on ICC
1518 (at least on my machine). */
1520 SendToICSDelayed(s,msdelay)
1524 int count, outCount, outError;
1526 if (icsPR == NULL) return;
1529 if (appData.debugMode) {
1530 fprintf(debugFP, ">ICS: ");
1531 show_bytes(debugFP, s, count);
1532 fprintf(debugFP, "\n");
1534 outCount = OutputToProcessDelayed(icsPR, s, count, &outError,
1536 if (outCount < count) {
1537 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1542 /* Remove all highlighting escape sequences in s
1543 Also deletes any suffix starting with '('
1546 StripHighlightAndTitle(s)
1549 static char retbuf[MSG_SIZ];
1552 while (*s != NULLCHAR) {
1553 while (*s == '\033') {
1554 while (*s != NULLCHAR && !isalpha(*s)) s++;
1555 if (*s != NULLCHAR) s++;
1557 while (*s != NULLCHAR && *s != '\033') {
1558 if (*s == '(' || *s == '[') {
1569 /* Remove all highlighting escape sequences in s */
1574 static char retbuf[MSG_SIZ];
1577 while (*s != NULLCHAR) {
1578 while (*s == '\033') {
1579 while (*s != NULLCHAR && !isalpha(*s)) s++;
1580 if (*s != NULLCHAR) s++;
1582 while (*s != NULLCHAR && *s != '\033') {
1590 char *variantNames[] = VARIANT_NAMES;
1595 return variantNames[v];
1599 /* Identify a variant from the strings the chess servers use or the
1600 PGN Variant tag names we use. */
1607 VariantClass v = VariantNormal;
1608 int i, found = FALSE;
1613 /* [HGM] skip over optional board-size prefixes */
1614 if( sscanf(e, "%dx%d_", &i, &i) == 2 ||
1615 sscanf(e, "%dx%d+%d_", &i, &i, &i) == 3 ) {
1616 while( *e++ != '_');
1619 if(StrCaseStr(e, "misc/")) { // [HGM] on FICS, misc/shogi is not shogi
1623 for (i=0; i<sizeof(variantNames)/sizeof(char*); i++) {
1624 if (StrCaseStr(e, variantNames[i])) {
1625 v = (VariantClass) i;
1632 if ((StrCaseStr(e, "fischer") && StrCaseStr(e, "random"))
1633 || StrCaseStr(e, "wild/fr")
1634 || StrCaseStr(e, "frc") || StrCaseStr(e, "960")) {
1635 v = VariantFischeRandom;
1636 } else if ((i = 4, p = StrCaseStr(e, "wild")) ||
1637 (i = 1, p = StrCaseStr(e, "w"))) {
1639 while (*p && (isspace(*p) || *p == '(' || *p == '/')) p++;
1646 case 0: /* FICS only, actually */
1648 /* Castling legal even if K starts on d-file */
1649 v = VariantWildCastle;
1654 /* Castling illegal even if K & R happen to start in
1655 normal positions. */
1656 v = VariantNoCastle;
1669 /* Castling legal iff K & R start in normal positions */
1675 /* Special wilds for position setup; unclear what to do here */
1676 v = VariantLoadable;
1679 /* Bizarre ICC game */
1680 v = VariantTwoKings;
1683 v = VariantKriegspiel;
1689 v = VariantFischeRandom;
1692 v = VariantCrazyhouse;
1695 v = VariantBughouse;
1701 /* Not quite the same as FICS suicide! */
1702 v = VariantGiveaway;
1708 v = VariantShatranj;
1711 /* Temporary names for future ICC types. The name *will* change in
1712 the next xboard/WinBoard release after ICC defines it. */
1750 v = VariantCapablanca;
1753 v = VariantKnightmate;
1759 v = VariantCylinder;
1765 v = VariantCapaRandom;
1768 v = VariantBerolina;
1780 /* Found "wild" or "w" in the string but no number;
1781 must assume it's normal chess. */
1785 sprintf(buf, _("Unknown wild type %d"), wnum);
1786 DisplayError(buf, 0);
1792 if (appData.debugMode) {
1793 fprintf(debugFP, _("recognized '%s' (%d) as variant %s\n"),
1794 e, wnum, VariantName(v));
1799 static int leftover_start = 0, leftover_len = 0;
1800 char star_match[STAR_MATCH_N][MSG_SIZ];
1802 /* Test whether pattern is present at &buf[*index]; if so, return TRUE,
1803 advance *index beyond it, and set leftover_start to the new value of
1804 *index; else return FALSE. If pattern contains the character '*', it
1805 matches any sequence of characters not containing '\r', '\n', or the
1806 character following the '*' (if any), and the matched sequence(s) are
1807 copied into star_match.
1810 looking_at(buf, index, pattern)
1815 char *bufp = &buf[*index], *patternp = pattern;
1817 char *matchp = star_match[0];
1820 if (*patternp == NULLCHAR) {
1821 *index = leftover_start = bufp - buf;
1825 if (*bufp == NULLCHAR) return FALSE;
1826 if (*patternp == '*') {
1827 if (*bufp == *(patternp + 1)) {
1829 matchp = star_match[++star_count];
1833 } else if (*bufp == '\n' || *bufp == '\r') {
1835 if (*patternp == NULLCHAR)
1840 *matchp++ = *bufp++;
1844 if (*patternp != *bufp) return FALSE;
1851 SendToPlayer(data, length)
1855 int error, outCount;
1856 outCount = OutputToProcess(NoProc, data, length, &error);
1857 if (outCount < length) {
1858 DisplayFatalError(_("Error writing to display"), error, 1);
1863 PackHolding(packed, holding)
1875 switch (runlength) {
1886 sprintf(q, "%d", runlength);
1898 /* Telnet protocol requests from the front end */
1900 TelnetRequest(ddww, option)
1901 unsigned char ddww, option;
1903 unsigned char msg[3];
1904 int outCount, outError;
1906 if (*appData.icsCommPort != NULLCHAR || appData.useTelnet) return;
1908 if (appData.debugMode) {
1909 char buf1[8], buf2[8], *ddwwStr, *optionStr;
1925 sprintf(buf1, "%d", ddww);
1934 sprintf(buf2, "%d", option);
1937 fprintf(debugFP, ">%s %s ", ddwwStr, optionStr);
1942 outCount = OutputToProcess(icsPR, (char *)msg, 3, &outError);
1944 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1951 if (!appData.icsActive) return;
1952 TelnetRequest(TN_DO, TN_ECHO);
1958 if (!appData.icsActive) return;
1959 TelnetRequest(TN_DONT, TN_ECHO);
1963 CopyHoldings(Board board, char *holdings, ChessSquare lowestPiece)
1965 /* put the holdings sent to us by the server on the board holdings area */
1966 int i, j, holdingsColumn, holdingsStartRow, direction, countsColumn;
1970 if(gameInfo.holdingsWidth < 2) return;
1971 if(gameInfo.variant != VariantBughouse && board[HOLDINGS_SET])
1972 return; // prevent overwriting by pre-board holdings
1974 if( (int)lowestPiece >= BlackPawn ) {
1977 holdingsStartRow = BOARD_HEIGHT-1;
1980 holdingsColumn = BOARD_WIDTH-1;
1981 countsColumn = BOARD_WIDTH-2;
1982 holdingsStartRow = 0;
1986 for(i=0; i<BOARD_HEIGHT; i++) { /* clear holdings */
1987 board[i][holdingsColumn] = EmptySquare;
1988 board[i][countsColumn] = (ChessSquare) 0;
1990 while( (p=*holdings++) != NULLCHAR ) {
1991 piece = CharToPiece( ToUpper(p) );
1992 if(piece == EmptySquare) continue;
1993 /*j = (int) piece - (int) WhitePawn;*/
1994 j = PieceToNumber(piece);
1995 if(j >= gameInfo.holdingsSize) continue; /* ignore pieces that do not fit */
1996 if(j < 0) continue; /* should not happen */
1997 piece = (ChessSquare) ( (int)piece + (int)lowestPiece );
1998 board[holdingsStartRow+j*direction][holdingsColumn] = piece;
1999 board[holdingsStartRow+j*direction][countsColumn]++;
2005 VariantSwitch(Board board, VariantClass newVariant)
2007 int newHoldingsWidth, newWidth = 8, newHeight = 8, i, j;
2008 static Board oldBoard;
2010 startedFromPositionFile = FALSE;
2011 if(gameInfo.variant == newVariant) return;
2013 /* [HGM] This routine is called each time an assignment is made to
2014 * gameInfo.variant during a game, to make sure the board sizes
2015 * are set to match the new variant. If that means adding or deleting
2016 * holdings, we shift the playing board accordingly
2017 * This kludge is needed because in ICS observe mode, we get boards
2018 * of an ongoing game without knowing the variant, and learn about the
2019 * latter only later. This can be because of the move list we requested,
2020 * in which case the game history is refilled from the beginning anyway,
2021 * but also when receiving holdings of a crazyhouse game. In the latter
2022 * case we want to add those holdings to the already received position.
2026 if (appData.debugMode) {
2027 fprintf(debugFP, "Switch board from %s to %s\n",
2028 VariantName(gameInfo.variant), VariantName(newVariant));
2029 setbuf(debugFP, NULL);
2031 shuffleOpenings = 0; /* [HGM] shuffle */
2032 gameInfo.holdingsSize = 5; /* [HGM] prepare holdings */
2036 newWidth = 9; newHeight = 9;
2037 gameInfo.holdingsSize = 7;
2038 case VariantBughouse:
2039 case VariantCrazyhouse:
2040 newHoldingsWidth = 2; break;
2044 newHoldingsWidth = 2;
2045 gameInfo.holdingsSize = 8;
2048 case VariantCapablanca:
2049 case VariantCapaRandom:
2052 newHoldingsWidth = gameInfo.holdingsSize = 0;
2055 if(newWidth != gameInfo.boardWidth ||
2056 newHeight != gameInfo.boardHeight ||
2057 newHoldingsWidth != gameInfo.holdingsWidth ) {
2059 /* shift position to new playing area, if needed */
2060 if(newHoldingsWidth > gameInfo.holdingsWidth) {
2061 for(i=0; i<BOARD_HEIGHT; i++)
2062 for(j=BOARD_RGHT-1; j>=BOARD_LEFT; j--)
2063 board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
2065 for(i=0; i<newHeight; i++) {
2066 board[i][0] = board[i][newWidth+2*newHoldingsWidth-1] = EmptySquare;
2067 board[i][1] = board[i][newWidth+2*newHoldingsWidth-2] = (ChessSquare) 0;
2069 } else if(newHoldingsWidth < gameInfo.holdingsWidth) {
2070 for(i=0; i<BOARD_HEIGHT; i++)
2071 for(j=BOARD_LEFT; j<BOARD_RGHT; j++)
2072 board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
2075 gameInfo.boardWidth = newWidth;
2076 gameInfo.boardHeight = newHeight;
2077 gameInfo.holdingsWidth = newHoldingsWidth;
2078 gameInfo.variant = newVariant;
2079 InitDrawingSizes(-2, 0);
2080 } else gameInfo.variant = newVariant;
2081 CopyBoard(oldBoard, board); // remember correctly formatted board
2082 InitPosition(FALSE); /* this sets up board[0], but also other stuff */
2083 DrawPosition(TRUE, currentMove ? boards[currentMove] : oldBoard);
2086 static int loggedOn = FALSE;
2088 /*-- Game start info cache: --*/
2090 char gs_kind[MSG_SIZ];
2091 static char player1Name[128] = "";
2092 static char player2Name[128] = "";
2093 static char cont_seq[] = "\n\\ ";
2094 static int player1Rating = -1;
2095 static int player2Rating = -1;
2096 /*----------------------------*/
2098 ColorClass curColor = ColorNormal;
2099 int suppressKibitz = 0;
2102 Boolean soughtPending = FALSE;
2103 Boolean seekGraphUp;
2104 #define MAX_SEEK_ADS 200
2106 char *seekAdList[MAX_SEEK_ADS];
2107 int ratingList[MAX_SEEK_ADS], xList[MAX_SEEK_ADS], yList[MAX_SEEK_ADS], seekNrList[MAX_SEEK_ADS], zList[MAX_SEEK_ADS];
2108 float tcList[MAX_SEEK_ADS];
2109 char colorList[MAX_SEEK_ADS];
2110 int nrOfSeekAds = 0;
2111 int minRating = 1010, maxRating = 2800;
2112 int hMargin = 10, vMargin = 20, h, w;
2113 extern int squareSize, lineGap;
2118 int x, y, color = 0, r = ratingList[i]; float tc = tcList[i];
2119 xList[i] = yList[i] = -100; // outside graph, so cannot be clicked
2120 if(r < minRating+100 && r >=0 ) r = minRating+100;
2121 if(r > maxRating) r = maxRating;
2122 if(tc < 1.) tc = 1.;
2123 if(tc > 95.) tc = 95.;
2124 x = (w-hMargin-squareSize/8-7)* log(tc)/log(95.) + hMargin;
2125 y = ((double)r - minRating)/(maxRating - minRating)
2126 * (h-vMargin-squareSize/8-1) + vMargin;
2127 if(ratingList[i] < 0) y = vMargin + squareSize/4;
2128 if(strstr(seekAdList[i], " u ")) color = 1;
2129 if(!strstr(seekAdList[i], "lightning") && // for now all wilds same color
2130 !strstr(seekAdList[i], "bullet") &&
2131 !strstr(seekAdList[i], "blitz") &&
2132 !strstr(seekAdList[i], "standard") ) color = 2;
2133 if(strstr(seekAdList[i], "(C) ")) color |= SQUARE; // plot computer seeks as squares
2134 DrawSeekDot(xList[i]=x+3*(color&~SQUARE), yList[i]=h-1-y, colorList[i]=color);
2138 AddAd(char *handle, char *rating, int base, int inc, char rated, char *type, int nr, Boolean plot)
2140 char buf[MSG_SIZ], *ext = "";
2141 VariantClass v = StringToVariant(type);
2142 if(strstr(type, "wild")) {
2143 ext = type + 4; // append wild number
2144 if(v == VariantFischeRandom) type = "chess960"; else
2145 if(v == VariantLoadable) type = "setup"; else
2146 type = VariantName(v);
2148 sprintf(buf, "%s (%s) %d %d %c %s%s", handle, rating, base, inc, rated, type, ext);
2149 if(nrOfSeekAds < MAX_SEEK_ADS-1) {
2150 if(seekAdList[nrOfSeekAds]) free(seekAdList[nrOfSeekAds]);
2151 ratingList[nrOfSeekAds] = -1; // for if seeker has no rating
2152 sscanf(rating, "%d", &ratingList[nrOfSeekAds]);
2153 tcList[nrOfSeekAds] = base + (2./3.)*inc;
2154 seekNrList[nrOfSeekAds] = nr;
2155 zList[nrOfSeekAds] = 0;
2156 seekAdList[nrOfSeekAds++] = StrSave(buf);
2157 if(plot) PlotSeekAd(nrOfSeekAds-1);
2164 int x = xList[i], y = yList[i], d=squareSize/4, k;
2165 DrawSeekBackground(x-squareSize/8, y-squareSize/8, x+squareSize/8+1, y+squareSize/8+1);
2166 if(x < hMargin+d) DrawSeekAxis(hMargin, y-squareSize/8, hMargin, y+squareSize/8+1);
2167 // now replot every dot that overlapped
2168 for(k=0; k<nrOfSeekAds; k++) if(k != i) {
2169 int xx = xList[k], yy = yList[k];
2170 if(xx <= x+d && xx > x-d && yy <= y+d && yy > y-d)
2171 DrawSeekDot(xx, yy, colorList[k]);
2176 RemoveSeekAd(int nr)
2179 for(i=0; i<nrOfSeekAds; i++) if(seekNrList[i] == nr) {
2181 if(seekAdList[i]) free(seekAdList[i]);
2182 seekAdList[i] = seekAdList[--nrOfSeekAds];
2183 seekNrList[i] = seekNrList[nrOfSeekAds];
2184 ratingList[i] = ratingList[nrOfSeekAds];
2185 colorList[i] = colorList[nrOfSeekAds];
2186 tcList[i] = tcList[nrOfSeekAds];
2187 xList[i] = xList[nrOfSeekAds];
2188 yList[i] = yList[nrOfSeekAds];
2189 zList[i] = zList[nrOfSeekAds];
2190 seekAdList[nrOfSeekAds] = NULL;
2196 MatchSoughtLine(char *line)
2198 char handle[MSG_SIZ], rating[MSG_SIZ], type[MSG_SIZ];
2199 int nr, base, inc, u=0; char dummy;
2201 if(sscanf(line, "%d %s %s %d %d rated %s", &nr, rating, handle, &base, &inc, type) == 6 ||
2202 sscanf(line, "%d %s %s %s %d %d rated %c", &nr, rating, handle, type, &base, &inc, &dummy) == 7 ||
2204 (sscanf(line, "%d %s %s %d %d unrated %s", &nr, rating, handle, &base, &inc, type) == 6 ||
2205 sscanf(line, "%d %s %s %s %d %d unrated %c", &nr, rating, handle, type, &base, &inc, &dummy) == 7) ) {
2206 // match: compact and save the line
2207 AddAd(handle, rating, base, inc, u ? 'u' : 'r', type, nr, FALSE);
2217 if(!seekGraphUp) return FALSE;
2218 h = BOARD_HEIGHT * (squareSize + lineGap) + lineGap;
2219 w = BOARD_WIDTH * (squareSize + lineGap) + lineGap;
2221 DrawSeekBackground(0, 0, w, h);
2222 DrawSeekAxis(hMargin, h-1-vMargin, w-5, h-1-vMargin);
2223 DrawSeekAxis(hMargin, h-1-vMargin, hMargin, 5);
2224 for(i=0; i<4000; i+= 100) if(i>=minRating && i<maxRating) {
2225 int yy =((double)i - minRating)/(maxRating - minRating)*(h-vMargin-squareSize/8-1) + vMargin;
2227 DrawSeekAxis(hMargin+5*(i%500==0), yy, hMargin-5, yy); // rating ticks
2230 sprintf(buf, "%d", i);
2231 DrawSeekText(buf, hMargin+squareSize/8+7, yy);
2234 DrawSeekText("unrated", hMargin+squareSize/8+7, h-1-vMargin-squareSize/4);
2235 for(i=1; i<100; i+=(i<10?1:5)) {
2236 int xx = (w-hMargin-squareSize/8-7)* log((double)i)/log(95.) + hMargin;
2237 DrawSeekAxis(xx, h-1-vMargin, xx, h-6-vMargin-3*(i%10==0)); // TC ticks
2238 if(i<=5 || (i>40 ? i%20 : i%10) == 0) {
2240 sprintf(buf, "%d", i);
2241 DrawSeekText(buf, xx-2-3*(i>9), h-1-vMargin/2);
2244 for(i=0; i<nrOfSeekAds; i++) PlotSeekAd(i);
2248 int SeekGraphClick(ClickType click, int x, int y, int moving)
2250 static int lastDown = 0, displayed = 0, lastSecond;
2251 if(!seekGraphUp) { // initiate cration of seek graph by requesting seek-ad list
2252 if(click == Release || moving) return FALSE;
2254 soughtPending = TRUE;
2255 SendToICS(ics_prefix);
2256 SendToICS("sought\n"); // should this be "sought all"?
2257 } else { // issue challenge based on clicked ad
2258 int dist = 10000; int i, closest = 0, second = 0;
2259 for(i=0; i<nrOfSeekAds; i++) {
2260 int d = (x-xList[i])*(x-xList[i]) + (y-yList[i])*(y-yList[i]) + zList[i];
2261 if(d < dist) { dist = d; closest = i; }
2262 second += (d - zList[i] < 120); // count in-range ads
2263 if(click == Press && moving != 1 && zList[i]>0) zList[i] *= 0.8; // age priority
2267 second = (second > 1);
2268 if(displayed != closest || second != lastSecond) {
2269 DisplayMessage(second ? "!" : "", seekAdList[closest]);
2270 lastSecond = second; displayed = closest;
2272 if(click == Press) {
2273 if(moving == 2) zList[closest] = 100; // right-click; push to back on press
2276 } // on press 'hit', only show info
2277 if(moving == 2) return TRUE; // ignore right up-clicks on dot
2278 sprintf(buf, "play %d\n", seekNrList[closest]);
2279 SendToICS(ics_prefix);
2281 return TRUE; // let incoming board of started game pop down the graph
2282 } else if(click == Release) { // release 'miss' is ignored
2283 zList[lastDown] = 100; // make future selection of the rejected ad more difficult
2284 if(moving == 2) { // right up-click
2285 nrOfSeekAds = 0; // refresh graph
2286 soughtPending = TRUE;
2287 SendToICS(ics_prefix);
2288 SendToICS("sought\n"); // should this be "sought all"?
2291 } else if(moving) { if(displayed >= 0) DisplayMessage("", ""); displayed = -1; return TRUE; }
2292 // press miss or release hit 'pop down' seek graph
2293 seekGraphUp = FALSE;
2294 DrawPosition(TRUE, NULL);
2300 read_from_ics(isr, closure, data, count, error)
2307 #define BUF_SIZE (16*1024) /* overflowed at 8K with "inchannel 1" on FICS? */
2308 #define STARTED_NONE 0
2309 #define STARTED_MOVES 1
2310 #define STARTED_BOARD 2
2311 #define STARTED_OBSERVE 3
2312 #define STARTED_HOLDINGS 4
2313 #define STARTED_CHATTER 5
2314 #define STARTED_COMMENT 6
2315 #define STARTED_MOVES_NOHIDE 7
2317 static int started = STARTED_NONE;
2318 static char parse[20000];
2319 static int parse_pos = 0;
2320 static char buf[BUF_SIZE + 1];
2321 static int firstTime = TRUE, intfSet = FALSE;
2322 static ColorClass prevColor = ColorNormal;
2323 static int savingComment = FALSE;
2324 static int cmatch = 0; // continuation sequence match
2331 int backup; /* [DM] For zippy color lines */
2333 char talker[MSG_SIZ]; // [HGM] chat
2336 connectionAlive = TRUE; // [HGM] alive: I think, therefore I am...
2338 if (appData.debugMode) {
2340 fprintf(debugFP, "<ICS: ");
2341 show_bytes(debugFP, data, count);
2342 fprintf(debugFP, "\n");
2346 if (appData.debugMode) { int f = forwardMostMove;
2347 fprintf(debugFP, "ics input %d, castling = %d %d %d %d %d %d\n", f,
2348 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
2349 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
2352 /* If last read ended with a partial line that we couldn't parse,
2353 prepend it to the new read and try again. */
2354 if (leftover_len > 0) {
2355 for (i=0; i<leftover_len; i++)
2356 buf[i] = buf[leftover_start + i];
2359 /* copy new characters into the buffer */
2360 bp = buf + leftover_len;
2361 buf_len=leftover_len;
2362 for (i=0; i<count; i++)
2365 if (data[i] == '\r')
2368 // join lines split by ICS?
2369 if (!appData.noJoin)
2372 Joining just consists of finding matches against the
2373 continuation sequence, and discarding that sequence
2374 if found instead of copying it. So, until a match
2375 fails, there's nothing to do since it might be the
2376 complete sequence, and thus, something we don't want
2379 if (data[i] == cont_seq[cmatch])
2382 if (cmatch == strlen(cont_seq))
2384 cmatch = 0; // complete match. just reset the counter
2387 it's possible for the ICS to not include the space
2388 at the end of the last word, making our [correct]
2389 join operation fuse two separate words. the server
2390 does this when the space occurs at the width setting.
2392 if (!buf_len || buf[buf_len-1] != ' ')
2403 match failed, so we have to copy what matched before
2404 falling through and copying this character. In reality,
2405 this will only ever be just the newline character, but
2406 it doesn't hurt to be precise.
2408 strncpy(bp, cont_seq, cmatch);
2420 buf[buf_len] = NULLCHAR;
2421 // next_out = leftover_len; // [HGM] should we set this to 0, and not print it in advance?
2426 while (i < buf_len) {
2427 /* Deal with part of the TELNET option negotiation
2428 protocol. We refuse to do anything beyond the
2429 defaults, except that we allow the WILL ECHO option,
2430 which ICS uses to turn off password echoing when we are
2431 directly connected to it. We reject this option
2432 if localLineEditing mode is on (always on in xboard)
2433 and we are talking to port 23, which might be a real
2434 telnet server that will try to keep WILL ECHO on permanently.
2436 if (buf_len - i >= 3 && (unsigned char) buf[i] == TN_IAC) {
2437 static int remoteEchoOption = FALSE; /* telnet ECHO option */
2438 unsigned char option;
2440 switch ((unsigned char) buf[++i]) {
2442 if (appData.debugMode)
2443 fprintf(debugFP, "\n<WILL ");
2444 switch (option = (unsigned char) buf[++i]) {
2446 if (appData.debugMode)
2447 fprintf(debugFP, "ECHO ");
2448 /* Reply only if this is a change, according
2449 to the protocol rules. */
2450 if (remoteEchoOption) break;
2451 if (appData.localLineEditing &&
2452 atoi(appData.icsPort) == TN_PORT) {
2453 TelnetRequest(TN_DONT, TN_ECHO);
2456 TelnetRequest(TN_DO, TN_ECHO);
2457 remoteEchoOption = TRUE;
2461 if (appData.debugMode)
2462 fprintf(debugFP, "%d ", option);
2463 /* Whatever this is, we don't want it. */
2464 TelnetRequest(TN_DONT, option);
2469 if (appData.debugMode)
2470 fprintf(debugFP, "\n<WONT ");
2471 switch (option = (unsigned char) buf[++i]) {
2473 if (appData.debugMode)
2474 fprintf(debugFP, "ECHO ");
2475 /* Reply only if this is a change, according
2476 to the protocol rules. */
2477 if (!remoteEchoOption) break;
2479 TelnetRequest(TN_DONT, TN_ECHO);
2480 remoteEchoOption = FALSE;
2483 if (appData.debugMode)
2484 fprintf(debugFP, "%d ", (unsigned char) option);
2485 /* Whatever this is, it must already be turned
2486 off, because we never agree to turn on
2487 anything non-default, so according to the
2488 protocol rules, we don't reply. */
2493 if (appData.debugMode)
2494 fprintf(debugFP, "\n<DO ");
2495 switch (option = (unsigned char) buf[++i]) {
2497 /* Whatever this is, we refuse to do it. */
2498 if (appData.debugMode)
2499 fprintf(debugFP, "%d ", option);
2500 TelnetRequest(TN_WONT, option);
2505 if (appData.debugMode)
2506 fprintf(debugFP, "\n<DONT ");
2507 switch (option = (unsigned char) buf[++i]) {
2509 if (appData.debugMode)
2510 fprintf(debugFP, "%d ", option);
2511 /* Whatever this is, we are already not doing
2512 it, because we never agree to do anything
2513 non-default, so according to the protocol
2514 rules, we don't reply. */
2519 if (appData.debugMode)
2520 fprintf(debugFP, "\n<IAC ");
2521 /* Doubled IAC; pass it through */
2525 if (appData.debugMode)
2526 fprintf(debugFP, "\n<%d ", (unsigned char) buf[i]);
2527 /* Drop all other telnet commands on the floor */
2530 if (oldi > next_out)
2531 SendToPlayer(&buf[next_out], oldi - next_out);
2537 /* OK, this at least will *usually* work */
2538 if (!loggedOn && looking_at(buf, &i, "ics%")) {
2542 if (loggedOn && !intfSet) {
2543 if (ics_type == ICS_ICC) {
2545 "/set-quietly interface %s\n/set-quietly style 12\n",
2547 if(appData.seekGraph && appData.autoRefresh) // [HGM] seekgraph
2548 strcat(str, "/set-2 51 1\n/set seek 1\n");
2549 } else if (ics_type == ICS_CHESSNET) {
2550 sprintf(str, "/style 12\n");
2552 strcpy(str, "alias $ @\n$set interface ");
2553 strcat(str, programVersion);
2554 strcat(str, "\n$iset startpos 1\n$iset ms 1\n");
2555 if(appData.seekGraph && appData.autoRefresh) // [HGM] seekgraph
2556 strcat(str, "$iset seekremove 1\n$set seek 1\n");
2558 strcat(str, "$iset nohighlight 1\n");
2560 strcat(str, "$iset lock 1\n$style 12\n");
2563 NotifyFrontendLogin();
2567 if (started == STARTED_COMMENT) {
2568 /* Accumulate characters in comment */
2569 parse[parse_pos++] = buf[i];
2570 if (buf[i] == '\n') {
2571 parse[parse_pos] = NULLCHAR;
2572 if(chattingPartner>=0) {
2574 sprintf(mess, "%s%s", talker, parse);
2575 OutputChatMessage(chattingPartner, mess);
2576 chattingPartner = -1;
2577 next_out = i+1; // [HGM] suppress printing in ICS window
2579 if(!suppressKibitz) // [HGM] kibitz
2580 AppendComment(forwardMostMove, StripHighlight(parse), TRUE);
2581 else { // [HGM kibitz: divert memorized engine kibitz to engine-output window
2582 int nrDigit = 0, nrAlph = 0, j;
2583 if(parse_pos > MSG_SIZ - 30) // defuse unreasonably long input
2584 { parse_pos = MSG_SIZ-30; parse[parse_pos - 1] = '\n'; }
2585 parse[parse_pos] = NULLCHAR;
2586 // try to be smart: if it does not look like search info, it should go to
2587 // ICS interaction window after all, not to engine-output window.
2588 for(j=0; j<parse_pos; j++) { // count letters and digits
2589 nrDigit += (parse[j] >= '0' && parse[j] <= '9');
2590 nrAlph += (parse[j] >= 'a' && parse[j] <= 'z');
2591 nrAlph += (parse[j] >= 'A' && parse[j] <= 'Z');
2593 if(nrAlph < 9*nrDigit) { // if more than 10% digit we assume search info
2594 int depth=0; float score;
2595 if(sscanf(parse, "!!! %f/%d", &score, &depth) == 2 && depth>0) {
2596 // [HGM] kibitz: save kibitzed opponent info for PGN and eval graph
2597 pvInfoList[forwardMostMove-1].depth = depth;
2598 pvInfoList[forwardMostMove-1].score = 100*score;
2600 OutputKibitz(suppressKibitz, parse);
2603 sprintf(tmp, _("your opponent kibitzes: %s"), parse);
2604 SendToPlayer(tmp, strlen(tmp));
2606 next_out = i+1; // [HGM] suppress printing in ICS window
2608 started = STARTED_NONE;
2610 /* Don't match patterns against characters in comment */
2615 if (started == STARTED_CHATTER) {
2616 if (buf[i] != '\n') {
2617 /* Don't match patterns against characters in chatter */
2621 started = STARTED_NONE;
2622 if(suppressKibitz) next_out = i+1;
2625 /* Kludge to deal with rcmd protocol */
2626 if (firstTime && looking_at(buf, &i, "\001*")) {
2627 DisplayFatalError(&buf[1], 0, 1);
2633 if (!loggedOn && looking_at(buf, &i, "chessclub.com")) {
2636 if (appData.debugMode)
2637 fprintf(debugFP, "ics_type %d\n", ics_type);
2640 if (!loggedOn && looking_at(buf, &i, "freechess.org")) {
2641 ics_type = ICS_FICS;
2643 if (appData.debugMode)
2644 fprintf(debugFP, "ics_type %d\n", ics_type);
2647 if (!loggedOn && looking_at(buf, &i, "chess.net")) {
2648 ics_type = ICS_CHESSNET;
2650 if (appData.debugMode)
2651 fprintf(debugFP, "ics_type %d\n", ics_type);
2656 (looking_at(buf, &i, "\"*\" is *a registered name") ||
2657 looking_at(buf, &i, "Logging you in as \"*\"") ||
2658 looking_at(buf, &i, "will be \"*\""))) {
2659 strcpy(ics_handle, star_match[0]);
2663 if (loggedOn && !have_set_title && ics_handle[0] != NULLCHAR) {
2665 snprintf(buf, sizeof(buf), "%s@%s", ics_handle, appData.icsHost);
2666 DisplayIcsInteractionTitle(buf);
2667 have_set_title = TRUE;
2670 /* skip finger notes */
2671 if (started == STARTED_NONE &&
2672 ((buf[i] == ' ' && isdigit(buf[i+1])) ||
2673 (buf[i] == '1' && buf[i+1] == '0')) &&
2674 buf[i+2] == ':' && buf[i+3] == ' ') {
2675 started = STARTED_CHATTER;
2681 // [HGM] seekgraph: recognize sought lines and end-of-sought message
2682 if(appData.seekGraph) {
2683 if(soughtPending && MatchSoughtLine(buf+i)) {
2684 i = strstr(buf+i, "rated") - buf;
2685 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2686 next_out = leftover_start = i;
2687 started = STARTED_CHATTER;
2688 suppressKibitz = TRUE;
2691 if((gameMode == IcsIdle || gameMode == BeginningOfGame)
2692 && looking_at(buf, &i, "* ads displayed")) {
2693 soughtPending = FALSE;
2698 if(appData.autoRefresh) {
2699 if(looking_at(buf, &i, "* (*) seeking * * * * *\"play *\" to respond)\n")) {
2700 int s = (ics_type == ICS_ICC); // ICC format differs
2702 AddAd(star_match[0], star_match[1], atoi(star_match[2+s]), atoi(star_match[3+s]),
2703 star_match[4+s][0], star_match[5-3*s], atoi(star_match[7]), TRUE);
2704 looking_at(buf, &i, "*% "); // eat prompt
2705 if(oldi > 0 && buf[oldi-1] == '\n') oldi--; // suppress preceding LF, if any
2706 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2707 next_out = i; // suppress
2710 if(looking_at(buf, &i, "\nAds removed: *\n") || looking_at(buf, &i, "\031(51 * *\031)")) {
2711 char *p = star_match[0];
2713 if(seekGraphUp) RemoveSeekAd(atoi(p));
2714 while(*p && *p++ != ' '); // next
2716 looking_at(buf, &i, "*% "); // eat prompt
2717 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2724 /* skip formula vars */
2725 if (started == STARTED_NONE &&
2726 buf[i] == 'f' && isdigit(buf[i+1]) && buf[i+2] == ':') {
2727 started = STARTED_CHATTER;
2732 // [HGM] kibitz: try to recognize opponent engine-score kibitzes, to divert them to engine-output window
2733 if (appData.autoKibitz && started == STARTED_NONE &&
2734 !appData.icsEngineAnalyze && // [HGM] [DM] ICS analyze
2735 (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack || gameMode == IcsObserving)) {
2736 if((looking_at(buf, &i, "* kibitzes: ") || looking_at(buf, &i, "* whispers: ")) &&
2737 (StrStr(star_match[0], gameInfo.white) == star_match[0] ||
2738 StrStr(star_match[0], gameInfo.black) == star_match[0] )) { // kibitz of self or opponent
2739 suppressKibitz = TRUE;
2740 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2742 if((StrStr(star_match[0], gameInfo.white) == star_match[0]
2743 && (gameMode == IcsPlayingWhite)) ||
2744 (StrStr(star_match[0], gameInfo.black) == star_match[0]
2745 && (gameMode == IcsPlayingBlack)) ) // opponent kibitz
2746 started = STARTED_CHATTER; // own kibitz we simply discard
2748 started = STARTED_COMMENT; // make sure it will be collected in parse[]
2749 parse_pos = 0; parse[0] = NULLCHAR;
2750 savingComment = TRUE;
2751 suppressKibitz = gameMode != IcsObserving ? 2 :
2752 (StrStr(star_match[0], gameInfo.white) == NULL) + 1;
2756 if((looking_at(buf, &i, "\nkibitzed to *\n") || looking_at(buf, &i, "kibitzed to *\n") ||
2757 looking_at(buf, &i, "\n(kibitzed to *\n") || looking_at(buf, &i, "(kibitzed to *\n"))
2758 && atoi(star_match[0])) {
2759 // suppress the acknowledgements of our own autoKibitz
2761 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2762 if(p = strchr(star_match[0], ' ')) p[1] = NULLCHAR; // clip off "players)" on FICS
2763 SendToPlayer(star_match[0], strlen(star_match[0]));
2764 if(looking_at(buf, &i, "*% ")) // eat prompt
2765 suppressKibitz = FALSE;
2769 } // [HGM] kibitz: end of patch
2771 // [HGM] chat: intercept tells by users for which we have an open chat window
2773 if(started == STARTED_NONE && (looking_at(buf, &i, "* tells you:") || looking_at(buf, &i, "* says:") ||
2774 looking_at(buf, &i, "* whispers:") ||
2775 looking_at(buf, &i, "* kibitzes:") ||
2776 looking_at(buf, &i, "* shouts:") ||
2777 looking_at(buf, &i, "* c-shouts:") ||
2778 looking_at(buf, &i, "--> * ") ||
2779 looking_at(buf, &i, "*(*):") && (sscanf(star_match[1], "%d", &channel),1) ||
2780 looking_at(buf, &i, "*(*)(*):") && (sscanf(star_match[2], "%d", &channel),1) ||
2781 looking_at(buf, &i, "*(*)(*)(*):") && (sscanf(star_match[3], "%d", &channel),1) ||
2782 looking_at(buf, &i, "*(*)(*)(*)(*):") && sscanf(star_match[4], "%d", &channel) == 1 )) {
2784 sscanf(star_match[0], "%[^(]", talker+1); // strip (C) or (U) off ICS handle
2785 chattingPartner = -1;
2787 if(channel >= 0) // channel broadcast; look if there is a chatbox for this channel
2788 for(p=0; p<MAX_CHAT; p++) {
2789 if(channel == atoi(chatPartner[p])) {
2790 talker[0] = '['; strcat(talker, "] ");
2791 Colorize(channel == 1 ? ColorChannel1 : ColorChannel, FALSE);
2792 chattingPartner = p; break;
2795 if(buf[i-3] == 'e') // kibitz; look if there is a KIBITZ chatbox
2796 for(p=0; p<MAX_CHAT; p++) {
2797 if(!strcmp("kibitzes", chatPartner[p])) {
2798 talker[0] = '['; strcat(talker, "] ");
2799 chattingPartner = p; break;
2802 if(buf[i-3] == 'r') // whisper; look if there is a WHISPER chatbox
2803 for(p=0; p<MAX_CHAT; p++) {
2804 if(!strcmp("whispers", chatPartner[p])) {
2805 talker[0] = '['; strcat(talker, "] ");
2806 chattingPartner = p; break;
2809 if(buf[i-3] == 't' || buf[oldi+2] == '>') {// shout, c-shout or it; look if there is a 'shouts' chatbox
2810 if(buf[i-8] == '-' && buf[i-3] == 't')
2811 for(p=0; p<MAX_CHAT; p++) { // c-shout; check if dedicatesd c-shout box exists
2812 if(!strcmp("c-shouts", chatPartner[p])) {
2813 talker[0] = '('; strcat(talker, ") "); Colorize(ColorSShout, FALSE);
2814 chattingPartner = p; break;
2817 if(chattingPartner < 0)
2818 for(p=0; p<MAX_CHAT; p++) {
2819 if(!strcmp("shouts", chatPartner[p])) {
2820 if(buf[oldi+2] == '>') { talker[0] = '<'; strcat(talker, "> "); Colorize(ColorShout, FALSE); }
2821 else if(buf[i-8] == '-') { talker[0] = '('; strcat(talker, ") "); Colorize(ColorSShout, FALSE); }
2822 else { talker[0] = '['; strcat(talker, "] "); Colorize(ColorShout, FALSE); }
2823 chattingPartner = p; break;
2827 if(chattingPartner<0) // if not, look if there is a chatbox for this indivdual
2828 for(p=0; p<MAX_CHAT; p++) if(!StrCaseCmp(talker+1, chatPartner[p])) {
2829 talker[0] = 0; Colorize(ColorTell, FALSE);
2830 chattingPartner = p; break;
2832 if(chattingPartner<0) i = oldi; else {
2833 Colorize(curColor, TRUE); // undo the bogus colorations we just made to trigger the souds
2834 if(oldi > 0 && buf[oldi-1] == '\n') oldi--;
2835 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2836 started = STARTED_COMMENT;
2837 parse_pos = 0; parse[0] = NULLCHAR;
2838 savingComment = 3 + chattingPartner; // counts as TRUE
2839 suppressKibitz = TRUE;
2842 } // [HGM] chat: end of patch
2844 if (appData.zippyTalk || appData.zippyPlay) {
2845 /* [DM] Backup address for color zippy lines */
2848 if (loggedOn == TRUE)
2849 if (ZippyControl(buf, &backup) || ZippyConverse(buf, &backup) ||
2850 (appData.zippyPlay && ZippyMatch(buf, &backup)));
2852 } // [DM] 'else { ' deleted
2854 /* Regular tells and says */
2855 (tkind = 1, looking_at(buf, &i, "* tells you: ")) ||
2856 looking_at(buf, &i, "* (your partner) tells you: ") ||
2857 looking_at(buf, &i, "* says: ") ||
2858 /* Don't color "message" or "messages" output */
2859 (tkind = 5, looking_at(buf, &i, "*. * (*:*): ")) ||
2860 looking_at(buf, &i, "*. * at *:*: ") ||
2861 looking_at(buf, &i, "--* (*:*): ") ||
2862 /* Message notifications (same color as tells) */
2863 looking_at(buf, &i, "* has left a message ") ||
2864 looking_at(buf, &i, "* just sent you a message:\n") ||
2865 /* Whispers and kibitzes */
2866 (tkind = 2, looking_at(buf, &i, "* whispers: ")) ||
2867 looking_at(buf, &i, "* kibitzes: ") ||
2869 (tkind = 3, looking_at(buf, &i, "*(*: "))) {
2871 if (tkind == 1 && strchr(star_match[0], ':')) {
2872 /* Avoid "tells you:" spoofs in channels */
2875 if (star_match[0][0] == NULLCHAR ||
2876 strchr(star_match[0], ' ') ||
2877 (tkind == 3 && strchr(star_match[1], ' '))) {
2878 /* Reject bogus matches */
2881 if (appData.colorize) {
2882 if (oldi > next_out) {
2883 SendToPlayer(&buf[next_out], oldi - next_out);
2888 Colorize(ColorTell, FALSE);
2889 curColor = ColorTell;
2892 Colorize(ColorKibitz, FALSE);
2893 curColor = ColorKibitz;
2896 p = strrchr(star_match[1], '(');
2903 Colorize(ColorChannel1, FALSE);
2904 curColor = ColorChannel1;
2906 Colorize(ColorChannel, FALSE);
2907 curColor = ColorChannel;
2911 curColor = ColorNormal;
2915 if (started == STARTED_NONE && appData.autoComment &&
2916 (gameMode == IcsObserving ||
2917 gameMode == IcsPlayingWhite ||
2918 gameMode == IcsPlayingBlack)) {
2919 parse_pos = i - oldi;
2920 memcpy(parse, &buf[oldi], parse_pos);
2921 parse[parse_pos] = NULLCHAR;
2922 started = STARTED_COMMENT;
2923 savingComment = TRUE;
2925 started = STARTED_CHATTER;
2926 savingComment = FALSE;
2933 if (looking_at(buf, &i, "* s-shouts: ") ||
2934 looking_at(buf, &i, "* c-shouts: ")) {
2935 if (appData.colorize) {
2936 if (oldi > next_out) {
2937 SendToPlayer(&buf[next_out], oldi - next_out);
2940 Colorize(ColorSShout, FALSE);
2941 curColor = ColorSShout;
2944 started = STARTED_CHATTER;
2948 if (looking_at(buf, &i, "--->")) {
2953 if (looking_at(buf, &i, "* shouts: ") ||
2954 looking_at(buf, &i, "--> ")) {
2955 if (appData.colorize) {
2956 if (oldi > next_out) {
2957 SendToPlayer(&buf[next_out], oldi - next_out);
2960 Colorize(ColorShout, FALSE);
2961 curColor = ColorShout;
2964 started = STARTED_CHATTER;
2968 if (looking_at( buf, &i, "Challenge:")) {
2969 if (appData.colorize) {
2970 if (oldi > next_out) {
2971 SendToPlayer(&buf[next_out], oldi - next_out);
2974 Colorize(ColorChallenge, FALSE);
2975 curColor = ColorChallenge;
2981 if (looking_at(buf, &i, "* offers you") ||
2982 looking_at(buf, &i, "* offers to be") ||
2983 looking_at(buf, &i, "* would like to") ||
2984 looking_at(buf, &i, "* requests to") ||
2985 looking_at(buf, &i, "Your opponent offers") ||
2986 looking_at(buf, &i, "Your opponent requests")) {
2988 if (appData.colorize) {
2989 if (oldi > next_out) {
2990 SendToPlayer(&buf[next_out], oldi - next_out);
2993 Colorize(ColorRequest, FALSE);
2994 curColor = ColorRequest;
2999 if (looking_at(buf, &i, "* (*) seeking")) {
3000 if (appData.colorize) {
3001 if (oldi > next_out) {
3002 SendToPlayer(&buf[next_out], oldi - next_out);
3005 Colorize(ColorSeek, FALSE);
3006 curColor = ColorSeek;
3011 if (looking_at(buf, &i, "\\ ")) {
3012 if (prevColor != ColorNormal) {
3013 if (oldi > next_out) {
3014 SendToPlayer(&buf[next_out], oldi - next_out);
3017 Colorize(prevColor, TRUE);
3018 curColor = prevColor;
3020 if (savingComment) {
3021 parse_pos = i - oldi;
3022 memcpy(parse, &buf[oldi], parse_pos);
3023 parse[parse_pos] = NULLCHAR;
3024 started = STARTED_COMMENT;
3025 if(savingComment >= 3) // [HGM] chat: continuation of line for chat box
3026 chattingPartner = savingComment - 3; // kludge to remember the box
3028 started = STARTED_CHATTER;
3033 if (looking_at(buf, &i, "Black Strength :") ||
3034 looking_at(buf, &i, "<<< style 10 board >>>") ||
3035 looking_at(buf, &i, "<10>") ||
3036 looking_at(buf, &i, "#@#")) {
3037 /* Wrong board style */
3039 SendToICS(ics_prefix);
3040 SendToICS("set style 12\n");
3041 SendToICS(ics_prefix);
3042 SendToICS("refresh\n");
3046 if (!have_sent_ICS_logon && looking_at(buf, &i, "login:")) {
3048 have_sent_ICS_logon = 1;
3052 if (ics_getting_history != H_GETTING_MOVES /*smpos kludge*/ &&
3053 (looking_at(buf, &i, "\n<12> ") ||
3054 looking_at(buf, &i, "<12> "))) {
3056 if (oldi > next_out) {
3057 SendToPlayer(&buf[next_out], oldi - next_out);
3060 started = STARTED_BOARD;
3065 if ((started == STARTED_NONE && looking_at(buf, &i, "\n<b1> ")) ||
3066 looking_at(buf, &i, "<b1> ")) {
3067 if (oldi > next_out) {
3068 SendToPlayer(&buf[next_out], oldi - next_out);
3071 started = STARTED_HOLDINGS;
3076 if (looking_at(buf, &i, "* *vs. * *--- *")) {
3078 /* Header for a move list -- first line */
3080 switch (ics_getting_history) {
3084 case BeginningOfGame:
3085 /* User typed "moves" or "oldmoves" while we
3086 were idle. Pretend we asked for these
3087 moves and soak them up so user can step
3088 through them and/or save them.
3091 gameMode = IcsObserving;
3094 ics_getting_history = H_GOT_UNREQ_HEADER;
3096 case EditGame: /*?*/
3097 case EditPosition: /*?*/
3098 /* Should above feature work in these modes too? */
3099 /* For now it doesn't */
3100 ics_getting_history = H_GOT_UNWANTED_HEADER;
3103 ics_getting_history = H_GOT_UNWANTED_HEADER;
3108 /* Is this the right one? */
3109 if (gameInfo.white && gameInfo.black &&
3110 strcmp(gameInfo.white, star_match[0]) == 0 &&
3111 strcmp(gameInfo.black, star_match[2]) == 0) {
3113 ics_getting_history = H_GOT_REQ_HEADER;
3116 case H_GOT_REQ_HEADER:
3117 case H_GOT_UNREQ_HEADER:
3118 case H_GOT_UNWANTED_HEADER:
3119 case H_GETTING_MOVES:
3120 /* Should not happen */
3121 DisplayError(_("Error gathering move list: two headers"), 0);
3122 ics_getting_history = H_FALSE;
3126 /* Save player ratings into gameInfo if needed */
3127 if ((ics_getting_history == H_GOT_REQ_HEADER ||
3128 ics_getting_history == H_GOT_UNREQ_HEADER) &&
3129 (gameInfo.whiteRating == -1 ||
3130 gameInfo.blackRating == -1)) {
3132 gameInfo.whiteRating = string_to_rating(star_match[1]);
3133 gameInfo.blackRating = string_to_rating(star_match[3]);
3134 if (appData.debugMode)
3135 fprintf(debugFP, _("Ratings from header: W %d, B %d\n"),
3136 gameInfo.whiteRating, gameInfo.blackRating);
3141 if (looking_at(buf, &i,
3142 "* * match, initial time: * minute*, increment: * second")) {
3143 /* Header for a move list -- second line */
3144 /* Initial board will follow if this is a wild game */
3145 if (gameInfo.event != NULL) free(gameInfo.event);
3146 sprintf(str, "ICS %s %s match", star_match[0], star_match[1]);
3147 gameInfo.event = StrSave(str);
3148 /* [HGM] we switched variant. Translate boards if needed. */
3149 VariantSwitch(boards[currentMove], StringToVariant(gameInfo.event));
3153 if (looking_at(buf, &i, "Move ")) {
3154 /* Beginning of a move list */
3155 switch (ics_getting_history) {
3157 /* Normally should not happen */
3158 /* Maybe user hit reset while we were parsing */
3161 /* Happens if we are ignoring a move list that is not
3162 * the one we just requested. Common if the user
3163 * tries to observe two games without turning off
3166 case H_GETTING_MOVES:
3167 /* Should not happen */
3168 DisplayError(_("Error gathering move list: nested"), 0);
3169 ics_getting_history = H_FALSE;
3171 case H_GOT_REQ_HEADER:
3172 ics_getting_history = H_GETTING_MOVES;
3173 started = STARTED_MOVES;
3175 if (oldi > next_out) {
3176 SendToPlayer(&buf[next_out], oldi - next_out);
3179 case H_GOT_UNREQ_HEADER:
3180 ics_getting_history = H_GETTING_MOVES;
3181 started = STARTED_MOVES_NOHIDE;
3184 case H_GOT_UNWANTED_HEADER:
3185 ics_getting_history = H_FALSE;
3191 if (looking_at(buf, &i, "% ") ||
3192 ((started == STARTED_MOVES || started == STARTED_MOVES_NOHIDE)
3193 && looking_at(buf, &i, "}*"))) { char *bookHit = NULL; // [HGM] book
3194 if(ics_type == ICS_ICC && soughtPending) { // [HGM] seekgraph: on ICC sought-list has no termination line
3195 soughtPending = FALSE;
3199 if(suppressKibitz) next_out = i;
3200 savingComment = FALSE;
3204 case STARTED_MOVES_NOHIDE:
3205 memcpy(&parse[parse_pos], &buf[oldi], i - oldi);
3206 parse[parse_pos + i - oldi] = NULLCHAR;
3207 ParseGameHistory(parse);
3209 if (appData.zippyPlay && first.initDone) {
3210 FeedMovesToProgram(&first, forwardMostMove);
3211 if (gameMode == IcsPlayingWhite) {
3212 if (WhiteOnMove(forwardMostMove)) {
3213 if (first.sendTime) {
3214 if (first.useColors) {
3215 SendToProgram("black\n", &first);
3217 SendTimeRemaining(&first, TRUE);
3219 if (first.useColors) {
3220 SendToProgram("white\n", &first); // [HGM] book: made sending of "go\n" book dependent
3222 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: probe book for initial pos
3223 first.maybeThinking = TRUE;
3225 if (first.usePlayother) {
3226 if (first.sendTime) {
3227 SendTimeRemaining(&first, TRUE);
3229 SendToProgram("playother\n", &first);
3235 } else if (gameMode == IcsPlayingBlack) {
3236 if (!WhiteOnMove(forwardMostMove)) {
3237 if (first.sendTime) {
3238 if (first.useColors) {
3239 SendToProgram("white\n", &first);
3241 SendTimeRemaining(&first, FALSE);
3243 if (first.useColors) {
3244 SendToProgram("black\n", &first);
3246 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE);
3247 first.maybeThinking = TRUE;
3249 if (first.usePlayother) {
3250 if (first.sendTime) {
3251 SendTimeRemaining(&first, FALSE);
3253 SendToProgram("playother\n", &first);
3262 if (gameMode == IcsObserving && ics_gamenum == -1) {
3263 /* Moves came from oldmoves or moves command
3264 while we weren't doing anything else.
3266 currentMove = forwardMostMove;
3267 ClearHighlights();/*!!could figure this out*/
3268 flipView = appData.flipView;
3269 DrawPosition(TRUE, boards[currentMove]);
3270 DisplayBothClocks();
3271 sprintf(str, "%s vs. %s",
3272 gameInfo.white, gameInfo.black);
3276 /* Moves were history of an active game */
3277 if (gameInfo.resultDetails != NULL) {
3278 free(gameInfo.resultDetails);
3279 gameInfo.resultDetails = NULL;
3282 HistorySet(parseList, backwardMostMove,
3283 forwardMostMove, currentMove-1);
3284 DisplayMove(currentMove - 1);
3285 if (started == STARTED_MOVES) next_out = i;
3286 started = STARTED_NONE;
3287 ics_getting_history = H_FALSE;
3290 case STARTED_OBSERVE:
3291 started = STARTED_NONE;
3292 SendToICS(ics_prefix);
3293 SendToICS("refresh\n");
3299 if(bookHit) { // [HGM] book: simulate book reply
3300 static char bookMove[MSG_SIZ]; // a bit generous?
3302 programStats.nodes = programStats.depth = programStats.time =
3303 programStats.score = programStats.got_only_move = 0;
3304 sprintf(programStats.movelist, "%s (xbook)", bookHit);
3306 strcpy(bookMove, "move ");
3307 strcat(bookMove, bookHit);
3308 HandleMachineMove(bookMove, &first);
3313 if ((started == STARTED_MOVES || started == STARTED_BOARD ||
3314 started == STARTED_HOLDINGS ||
3315 started == STARTED_MOVES_NOHIDE) && i >= leftover_len) {
3316 /* Accumulate characters in move list or board */
3317 parse[parse_pos++] = buf[i];
3320 /* Start of game messages. Mostly we detect start of game
3321 when the first board image arrives. On some versions
3322 of the ICS, though, we need to do a "refresh" after starting
3323 to observe in order to get the current board right away. */
3324 if (looking_at(buf, &i, "Adding game * to observation list")) {
3325 started = STARTED_OBSERVE;
3329 /* Handle auto-observe */
3330 if (appData.autoObserve &&
3331 (gameMode == IcsIdle || gameMode == BeginningOfGame) &&
3332 looking_at(buf, &i, "Game notification: * (*) vs. * (*)")) {
3334 /* Choose the player that was highlighted, if any. */
3335 if (star_match[0][0] == '\033' ||
3336 star_match[1][0] != '\033') {
3337 player = star_match[0];
3339 player = star_match[2];
3341 sprintf(str, "%sobserve %s\n",
3342 ics_prefix, StripHighlightAndTitle(player));
3345 /* Save ratings from notify string */
3346 strcpy(player1Name, star_match[0]);
3347 player1Rating = string_to_rating(star_match[1]);
3348 strcpy(player2Name, star_match[2]);
3349 player2Rating = string_to_rating(star_match[3]);
3351 if (appData.debugMode)
3353 "Ratings from 'Game notification:' %s %d, %s %d\n",
3354 player1Name, player1Rating,
3355 player2Name, player2Rating);
3360 /* Deal with automatic examine mode after a game,
3361 and with IcsObserving -> IcsExamining transition */
3362 if (looking_at(buf, &i, "Entering examine mode for game *") ||
3363 looking_at(buf, &i, "has made you an examiner of game *")) {
3365 int gamenum = atoi(star_match[0]);
3366 if ((gameMode == IcsIdle || gameMode == IcsObserving) &&
3367 gamenum == ics_gamenum) {
3368 /* We were already playing or observing this game;
3369 no need to refetch history */
3370 gameMode = IcsExamining;
3372 pauseExamForwardMostMove = forwardMostMove;
3373 } else if (currentMove < forwardMostMove) {
3374 ForwardInner(forwardMostMove);
3377 /* I don't think this case really can happen */
3378 SendToICS(ics_prefix);
3379 SendToICS("refresh\n");
3384 /* Error messages */
3385 // if (ics_user_moved) {
3386 if (1) { // [HGM] old way ignored error after move type in; ics_user_moved is not set then!
3387 if (looking_at(buf, &i, "Illegal move") ||
3388 looking_at(buf, &i, "Not a legal move") ||
3389 looking_at(buf, &i, "Your king is in check") ||
3390 looking_at(buf, &i, "It isn't your turn") ||
3391 looking_at(buf, &i, "It is not your move")) {
3393 if (ics_user_moved && forwardMostMove > backwardMostMove) { // only backup if we already moved
3394 currentMove = forwardMostMove-1;
3395 DisplayMove(currentMove - 1); /* before DMError */
3396 DrawPosition(FALSE, boards[currentMove]);
3397 SwitchClocks(forwardMostMove-1); // [HGM] race
3398 DisplayBothClocks();
3400 DisplayMoveError(_("Illegal move (rejected by ICS)")); // [HGM] but always relay error msg
3406 if (looking_at(buf, &i, "still have time") ||
3407 looking_at(buf, &i, "not out of time") ||
3408 looking_at(buf, &i, "either player is out of time") ||
3409 looking_at(buf, &i, "has timeseal; checking")) {
3410 /* We must have called his flag a little too soon */
3411 whiteFlag = blackFlag = FALSE;
3415 if (looking_at(buf, &i, "added * seconds to") ||
3416 looking_at(buf, &i, "seconds were added to")) {
3417 /* Update the clocks */
3418 SendToICS(ics_prefix);
3419 SendToICS("refresh\n");
3423 if (!ics_clock_paused && looking_at(buf, &i, "clock paused")) {
3424 ics_clock_paused = TRUE;
3429 if (ics_clock_paused && looking_at(buf, &i, "clock resumed")) {
3430 ics_clock_paused = FALSE;
3435 /* Grab player ratings from the Creating: message.
3436 Note we have to check for the special case when
3437 the ICS inserts things like [white] or [black]. */
3438 if (looking_at(buf, &i, "Creating: * (*)* * (*)") ||
3439 looking_at(buf, &i, "Creating: * (*) [*] * (*)")) {
3441 0 player 1 name (not necessarily white)
3443 2 empty, white, or black (IGNORED)
3444 3 player 2 name (not necessarily black)
3447 The names/ratings are sorted out when the game
3448 actually starts (below).
3450 strcpy(player1Name, StripHighlightAndTitle(star_match[0]));
3451 player1Rating = string_to_rating(star_match[1]);
3452 strcpy(player2Name, StripHighlightAndTitle(star_match[3]));
3453 player2Rating = string_to_rating(star_match[4]);
3455 if (appData.debugMode)
3457 "Ratings from 'Creating:' %s %d, %s %d\n",
3458 player1Name, player1Rating,
3459 player2Name, player2Rating);
3464 /* Improved generic start/end-of-game messages */
3465 if ((tkind=0, looking_at(buf, &i, "{Game * (* vs. *) *}*")) ||
3466 (tkind=1, looking_at(buf, &i, "{Game * (*(*) vs. *(*)) *}*"))){
3467 /* If tkind == 0: */
3468 /* star_match[0] is the game number */
3469 /* [1] is the white player's name */
3470 /* [2] is the black player's name */
3471 /* For end-of-game: */
3472 /* [3] is the reason for the game end */
3473 /* [4] is a PGN end game-token, preceded by " " */
3474 /* For start-of-game: */
3475 /* [3] begins with "Creating" or "Continuing" */
3476 /* [4] is " *" or empty (don't care). */
3477 int gamenum = atoi(star_match[0]);
3478 char *whitename, *blackname, *why, *endtoken;
3479 ChessMove endtype = (ChessMove) 0;
3482 whitename = star_match[1];
3483 blackname = star_match[2];
3484 why = star_match[3];
3485 endtoken = star_match[4];
3487 whitename = star_match[1];
3488 blackname = star_match[3];
3489 why = star_match[5];
3490 endtoken = star_match[6];
3493 /* Game start messages */
3494 if (strncmp(why, "Creating ", 9) == 0 ||
3495 strncmp(why, "Continuing ", 11) == 0) {
3496 gs_gamenum = gamenum;
3497 strcpy(gs_kind, strchr(why, ' ') + 1);
3498 VariantSwitch(boards[currentMove], StringToVariant(gs_kind)); // [HGM] variantswitch: even before we get first board
3500 if (appData.zippyPlay) {
3501 ZippyGameStart(whitename, blackname);
3504 partnerBoardValid = FALSE; // [HGM] bughouse
3508 /* Game end messages */
3509 if (gameMode == IcsIdle || gameMode == BeginningOfGame ||
3510 ics_gamenum != gamenum) {
3513 while (endtoken[0] == ' ') endtoken++;
3514 switch (endtoken[0]) {
3517 endtype = GameUnfinished;
3520 endtype = BlackWins;
3523 if (endtoken[1] == '/')
3524 endtype = GameIsDrawn;
3526 endtype = WhiteWins;
3529 GameEnds(endtype, why, GE_ICS);
3531 if (appData.zippyPlay && first.initDone) {
3532 ZippyGameEnd(endtype, why);
3533 if (first.pr == NULL) {
3534 /* Start the next process early so that we'll
3535 be ready for the next challenge */
3536 StartChessProgram(&first);
3538 /* Send "new" early, in case this command takes
3539 a long time to finish, so that we'll be ready
3540 for the next challenge. */
3541 gameInfo.variant = VariantNormal; // [HGM] variantswitch: suppress sending of 'variant'
3545 if(appData.bgObserve && partnerBoardValid) DrawPosition(TRUE, partnerBoard);
3549 if (looking_at(buf, &i, "Removing game * from observation") ||
3550 looking_at(buf, &i, "no longer observing game *") ||
3551 looking_at(buf, &i, "Game * (*) has no examiners")) {
3552 if (gameMode == IcsObserving &&
3553 atoi(star_match[0]) == ics_gamenum)
3555 /* icsEngineAnalyze */
3556 if (appData.icsEngineAnalyze) {
3563 ics_user_moved = FALSE;
3568 if (looking_at(buf, &i, "no longer examining game *")) {
3569 if (gameMode == IcsExamining &&
3570 atoi(star_match[0]) == ics_gamenum)
3574 ics_user_moved = FALSE;
3579 /* Advance leftover_start past any newlines we find,
3580 so only partial lines can get reparsed */
3581 if (looking_at(buf, &i, "\n")) {
3582 prevColor = curColor;
3583 if (curColor != ColorNormal) {
3584 if (oldi > next_out) {
3585 SendToPlayer(&buf[next_out], oldi - next_out);
3588 Colorize(ColorNormal, FALSE);
3589 curColor = ColorNormal;
3591 if (started == STARTED_BOARD) {
3592 started = STARTED_NONE;
3593 parse[parse_pos] = NULLCHAR;
3594 ParseBoard12(parse);
3597 /* Send premove here */
3598 if (appData.premove) {
3600 if (currentMove == 0 &&
3601 gameMode == IcsPlayingWhite &&
3602 appData.premoveWhite) {
3603 sprintf(str, "%s\n", appData.premoveWhiteText);
3604 if (appData.debugMode)
3605 fprintf(debugFP, "Sending premove:\n");
3607 } else if (currentMove == 1 &&
3608 gameMode == IcsPlayingBlack &&
3609 appData.premoveBlack) {
3610 sprintf(str, "%s\n", appData.premoveBlackText);
3611 if (appData.debugMode)
3612 fprintf(debugFP, "Sending premove:\n");
3614 } else if (gotPremove) {
3616 ClearPremoveHighlights();
3617 if (appData.debugMode)
3618 fprintf(debugFP, "Sending premove:\n");
3619 UserMoveEvent(premoveFromX, premoveFromY,
3620 premoveToX, premoveToY,
3625 /* Usually suppress following prompt */
3626 if (!(forwardMostMove == 0 && gameMode == IcsExamining)) {
3627 while(looking_at(buf, &i, "\n")); // [HGM] skip empty lines
3628 if (looking_at(buf, &i, "*% ")) {
3629 savingComment = FALSE;
3634 } else if (started == STARTED_HOLDINGS) {
3636 char new_piece[MSG_SIZ];
3637 started = STARTED_NONE;
3638 parse[parse_pos] = NULLCHAR;
3639 if (appData.debugMode)
3640 fprintf(debugFP, "Parsing holdings: %s, currentMove = %d\n",
3641 parse, currentMove);
3642 if (sscanf(parse, " game %d", &gamenum) == 1) {
3643 if(gamenum == ics_gamenum) { // [HGM] bughouse: old code if part of foreground game
3644 if (gameInfo.variant == VariantNormal) {
3645 /* [HGM] We seem to switch variant during a game!
3646 * Presumably no holdings were displayed, so we have
3647 * to move the position two files to the right to
3648 * create room for them!
3650 VariantClass newVariant;
3651 switch(gameInfo.boardWidth) { // base guess on board width
3652 case 9: newVariant = VariantShogi; break;
3653 case 10: newVariant = VariantGreat; break;
3654 default: newVariant = VariantCrazyhouse; break;
3656 VariantSwitch(boards[currentMove], newVariant); /* temp guess */
3657 /* Get a move list just to see the header, which
3658 will tell us whether this is really bug or zh */
3659 if (ics_getting_history == H_FALSE) {
3660 ics_getting_history = H_REQUESTED;
3661 sprintf(str, "%smoves %d\n", ics_prefix, gamenum);
3665 new_piece[0] = NULLCHAR;
3666 sscanf(parse, "game %d white [%s black [%s <- %s",
3667 &gamenum, white_holding, black_holding,
3669 white_holding[strlen(white_holding)-1] = NULLCHAR;
3670 black_holding[strlen(black_holding)-1] = NULLCHAR;
3671 /* [HGM] copy holdings to board holdings area */
3672 CopyHoldings(boards[forwardMostMove], white_holding, WhitePawn);
3673 CopyHoldings(boards[forwardMostMove], black_holding, BlackPawn);
3674 boards[forwardMostMove][HOLDINGS_SET] = 1; // flag holdings as set
3676 if (appData.zippyPlay && first.initDone) {
3677 ZippyHoldings(white_holding, black_holding,
3681 if (tinyLayout || smallLayout) {
3682 char wh[16], bh[16];
3683 PackHolding(wh, white_holding);
3684 PackHolding(bh, black_holding);
3685 sprintf(str, "[%s-%s] %s-%s", wh, bh,
3686 gameInfo.white, gameInfo.black);
3688 sprintf(str, "%s [%s] vs. %s [%s]",
3689 gameInfo.white, white_holding,
3690 gameInfo.black, black_holding);
3692 if(!partnerUp) // [HGM] bughouse: when peeking at partner game we already know what he captured...
3693 DrawPosition(FALSE, boards[currentMove]);
3695 } else if(appData.bgObserve) { // [HGM] bughouse: holdings of other game => background
3696 sscanf(parse, "game %d white [%s black [%s <- %s",
3697 &gamenum, white_holding, black_holding,
3699 white_holding[strlen(white_holding)-1] = NULLCHAR;
3700 black_holding[strlen(black_holding)-1] = NULLCHAR;
3701 /* [HGM] copy holdings to partner-board holdings area */
3702 CopyHoldings(partnerBoard, white_holding, WhitePawn);
3703 CopyHoldings(partnerBoard, black_holding, BlackPawn);
3704 if(twoBoards) { partnerUp = 1; flipView = !flipView; } // [HGM] dual: always draw
3705 if(partnerUp) DrawPosition(FALSE, partnerBoard);
3706 if(twoBoards) { partnerUp = 0; flipView = !flipView; }
3709 /* Suppress following prompt */
3710 if (looking_at(buf, &i, "*% ")) {
3711 if(strchr(star_match[0], 7)) SendToPlayer("\007", 1); // Bell(); // FICS fuses bell for next board with prompt in zh captures
3712 savingComment = FALSE;
3720 i++; /* skip unparsed character and loop back */
3723 if (started != STARTED_MOVES && started != STARTED_BOARD && !suppressKibitz && // [HGM] kibitz
3724 // started != STARTED_HOLDINGS && i > next_out) { // [HGM] should we compare to leftover_start in stead of i?
3725 // SendToPlayer(&buf[next_out], i - next_out);
3726 started != STARTED_HOLDINGS && leftover_start > next_out) {
3727 SendToPlayer(&buf[next_out], leftover_start - next_out);
3731 leftover_len = buf_len - leftover_start;
3732 /* if buffer ends with something we couldn't parse,
3733 reparse it after appending the next read */
3735 } else if (count == 0) {
3736 RemoveInputSource(isr);
3737 DisplayFatalError(_("Connection closed by ICS"), 0, 0);
3739 DisplayFatalError(_("Error reading from ICS"), error, 1);
3744 /* Board style 12 looks like this:
3746 <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
3748 * The "<12> " is stripped before it gets to this routine. The two
3749 * trailing 0's (flip state and clock ticking) are later addition, and
3750 * some chess servers may not have them, or may have only the first.
3751 * Additional trailing fields may be added in the future.
3754 #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"
3756 #define RELATION_OBSERVING_PLAYED 0
3757 #define RELATION_OBSERVING_STATIC -2 /* examined, oldmoves, or smoves */
3758 #define RELATION_PLAYING_MYMOVE 1
3759 #define RELATION_PLAYING_NOTMYMOVE -1
3760 #define RELATION_EXAMINING 2
3761 #define RELATION_ISOLATED_BOARD -3
3762 #define RELATION_STARTING_POSITION -4 /* FICS only */
3765 ParseBoard12(string)
3768 GameMode newGameMode;
3769 int gamenum, newGame, newMove, relation, basetime, increment, ics_flip = 0, i;
3770 int j, k, n, moveNum, white_stren, black_stren, white_time, black_time, takeback;
3771 int double_push, castle_ws, castle_wl, castle_bs, castle_bl, irrev_count;
3772 char to_play, board_chars[200];
3773 char move_str[500], str[500], elapsed_time[500];
3774 char black[32], white[32];
3776 int prevMove = currentMove;
3779 int fromX, fromY, toX, toY;
3781 int ranks=1, files=0; /* [HGM] ICS80: allow variable board size */
3782 char *bookHit = NULL; // [HGM] book
3783 Boolean weird = FALSE, reqFlag = FALSE;
3785 fromX = fromY = toX = toY = -1;
3789 if (appData.debugMode)
3790 fprintf(debugFP, _("Parsing board: %s\n"), string);
3792 move_str[0] = NULLCHAR;
3793 elapsed_time[0] = NULLCHAR;
3794 { /* [HGM] figure out how many ranks and files the board has, for ICS extension used by Capablanca server */
3796 while(i < 199 && (string[i] != ' ' || string[i+2] != ' ')) {
3797 if(string[i] == ' ') { ranks++; files = 0; }
3799 if(!strchr(" -pnbrqkPNBRQK" , string[i])) weird = TRUE; // test for fairies
3802 for(j = 0; j <i; j++) board_chars[j] = string[j];
3803 board_chars[i] = '\0';
3806 n = sscanf(string, PATTERN, &to_play, &double_push,
3807 &castle_ws, &castle_wl, &castle_bs, &castle_bl, &irrev_count,
3808 &gamenum, white, black, &relation, &basetime, &increment,
3809 &white_stren, &black_stren, &white_time, &black_time,
3810 &moveNum, str, elapsed_time, move_str, &ics_flip,
3814 snprintf(str, sizeof(str), _("Failed to parse board string:\n\"%s\""), string);
3815 DisplayError(str, 0);
3819 /* Convert the move number to internal form */
3820 moveNum = (moveNum - 1) * 2;
3821 if (to_play == 'B') moveNum++;
3822 if (moveNum > framePtr) { // [HGM] vari: do not run into saved variations
3823 DisplayFatalError(_("Game too long; increase MAX_MOVES and recompile"),
3829 case RELATION_OBSERVING_PLAYED:
3830 case RELATION_OBSERVING_STATIC:
3831 if (gamenum == -1) {
3832 /* Old ICC buglet */
3833 relation = RELATION_OBSERVING_STATIC;
3835 newGameMode = IcsObserving;
3837 case RELATION_PLAYING_MYMOVE:
3838 case RELATION_PLAYING_NOTMYMOVE:
3840 ((relation == RELATION_PLAYING_MYMOVE) == (to_play == 'W')) ?
3841 IcsPlayingWhite : IcsPlayingBlack;
3843 case RELATION_EXAMINING:
3844 newGameMode = IcsExamining;
3846 case RELATION_ISOLATED_BOARD:
3848 /* Just display this board. If user was doing something else,
3849 we will forget about it until the next board comes. */
3850 newGameMode = IcsIdle;
3852 case RELATION_STARTING_POSITION:
3853 newGameMode = gameMode;
3857 if((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)
3858 && newGameMode == IcsObserving && gamenum != ics_gamenum && appData.bgObserve) {
3859 // [HGM] bughouse: don't act on alien boards while we play. Just parse the board and save it */
3861 for (k = 0; k < ranks; k++) {
3862 for (j = 0; j < files; j++)
3863 board[k][j+gameInfo.holdingsWidth] = CharToPiece(board_chars[(ranks-1-k)*(files+1) + j]);
3864 if(gameInfo.holdingsWidth > 1) {
3865 board[k][0] = board[k][BOARD_WIDTH-1] = EmptySquare;
3866 board[k][1] = board[k][BOARD_WIDTH-2] = (ChessSquare) 0;;
3869 CopyBoard(partnerBoard, board);
3870 if(toSqr = strchr(str, '/')) { // extract highlights from long move
3871 partnerBoard[EP_STATUS-3] = toSqr[1] - AAA; // kludge: hide highlighting info in board
3872 partnerBoard[EP_STATUS-4] = toSqr[2] - ONE;
3873 } else partnerBoard[EP_STATUS-4] = partnerBoard[EP_STATUS-3] = -1;
3874 if(toSqr = strchr(str, '-')) {
3875 partnerBoard[EP_STATUS-1] = toSqr[1] - AAA;
3876 partnerBoard[EP_STATUS-2] = toSqr[2] - ONE;
3877 } else partnerBoard[EP_STATUS-1] = partnerBoard[EP_STATUS-2] = -1;
3878 if(appData.dualBoard && !twoBoards) { twoBoards = 1; InitDrawingSizes(-2,0); }
3879 if(twoBoards) { partnerUp = 1; flipView = !flipView; } // [HGM] dual
3880 if(partnerUp) DrawPosition(FALSE, partnerBoard);
3881 if(twoBoards) { partnerUp = 0; flipView = !flipView; } // [HGM] dual
3882 sprintf(partnerStatus, "W: %d:%02d B: %d:%02d (%d-%d) %c", white_time/60000, (white_time%60000)/1000,
3883 (black_time/60000), (black_time%60000)/1000, white_stren, black_stren, to_play);
3884 DisplayMessage(partnerStatus, "");
3885 partnerBoardValid = TRUE;
3889 /* Modify behavior for initial board display on move listing
3892 switch (ics_getting_history) {
3896 case H_GOT_REQ_HEADER:
3897 case H_GOT_UNREQ_HEADER:
3898 /* This is the initial position of the current game */
3899 gamenum = ics_gamenum;
3900 moveNum = 0; /* old ICS bug workaround */
3901 if (to_play == 'B') {
3902 startedFromSetupPosition = TRUE;
3903 blackPlaysFirst = TRUE;
3905 if (forwardMostMove == 0) forwardMostMove = 1;
3906 if (backwardMostMove == 0) backwardMostMove = 1;
3907 if (currentMove == 0) currentMove = 1;
3909 newGameMode = gameMode;
3910 relation = RELATION_STARTING_POSITION; /* ICC needs this */
3912 case H_GOT_UNWANTED_HEADER:
3913 /* This is an initial board that we don't want */
3915 case H_GETTING_MOVES:
3916 /* Should not happen */
3917 DisplayError(_("Error gathering move list: extra board"), 0);
3918 ics_getting_history = H_FALSE;
3922 if (gameInfo.boardHeight != ranks || gameInfo.boardWidth != files ||
3923 weird && (int)gameInfo.variant <= (int)VariantShogi) {
3924 /* [HGM] We seem to have switched variant unexpectedly
3925 * Try to guess new variant from board size
3927 VariantClass newVariant = VariantFairy; // if 8x8, but fairies present
3928 if(ranks == 8 && files == 10) newVariant = VariantCapablanca; else
3929 if(ranks == 10 && files == 9) newVariant = VariantXiangqi; else
3930 if(ranks == 8 && files == 12) newVariant = VariantCourier; else
3931 if(ranks == 9 && files == 9) newVariant = VariantShogi; else
3932 if(!weird) newVariant = VariantNormal;
3933 VariantSwitch(boards[currentMove], newVariant); /* temp guess */
3934 /* Get a move list just to see the header, which
3935 will tell us whether this is really bug or zh */
3936 if (ics_getting_history == H_FALSE) {
3937 ics_getting_history = H_REQUESTED; reqFlag = TRUE;
3938 sprintf(str, "%smoves %d\n", ics_prefix, gamenum);
3943 /* Take action if this is the first board of a new game, or of a
3944 different game than is currently being displayed. */
3945 if (gamenum != ics_gamenum || newGameMode != gameMode ||
3946 relation == RELATION_ISOLATED_BOARD) {
3948 /* Forget the old game and get the history (if any) of the new one */
3949 if (gameMode != BeginningOfGame) {
3953 if (appData.autoRaiseBoard) BoardToTop();
3955 if (gamenum == -1) {
3956 newGameMode = IcsIdle;
3957 } else if ((moveNum > 0 || newGameMode == IcsObserving) && newGameMode != IcsIdle &&
3958 appData.getMoveList && !reqFlag) {
3959 /* Need to get game history */
3960 ics_getting_history = H_REQUESTED;
3961 sprintf(str, "%smoves %d\n", ics_prefix, gamenum);
3965 /* Initially flip the board to have black on the bottom if playing
3966 black or if the ICS flip flag is set, but let the user change
3967 it with the Flip View button. */
3968 flipView = appData.autoFlipView ?
3969 (newGameMode == IcsPlayingBlack) || ics_flip :
3972 /* Done with values from previous mode; copy in new ones */
3973 gameMode = newGameMode;
3975 ics_gamenum = gamenum;
3976 if (gamenum == gs_gamenum) {
3977 int klen = strlen(gs_kind);
3978 if (gs_kind[klen - 1] == '.') gs_kind[klen - 1] = NULLCHAR;
3979 sprintf(str, "ICS %s", gs_kind);
3980 gameInfo.event = StrSave(str);
3982 gameInfo.event = StrSave("ICS game");
3984 gameInfo.site = StrSave(appData.icsHost);
3985 gameInfo.date = PGNDate();
3986 gameInfo.round = StrSave("-");
3987 gameInfo.white = StrSave(white);
3988 gameInfo.black = StrSave(black);
3989 timeControl = basetime * 60 * 1000;
3991 timeIncrement = increment * 1000;
3992 movesPerSession = 0;
3993 gameInfo.timeControl = TimeControlTagValue();
3994 VariantSwitch(boards[currentMove], StringToVariant(gameInfo.event) );
3995 if (appData.debugMode) {
3996 fprintf(debugFP, "ParseBoard says variant = '%s'\n", gameInfo.event);
3997 fprintf(debugFP, "recognized as %s\n", VariantName(gameInfo.variant));
3998 setbuf(debugFP, NULL);
4001 gameInfo.outOfBook = NULL;
4003 /* Do we have the ratings? */
4004 if (strcmp(player1Name, white) == 0 &&
4005 strcmp(player2Name, black) == 0) {
4006 if (appData.debugMode)
4007 fprintf(debugFP, "Remembered ratings: W %d, B %d\n",
4008 player1Rating, player2Rating);
4009 gameInfo.whiteRating = player1Rating;
4010 gameInfo.blackRating = player2Rating;
4011 } else if (strcmp(player2Name, white) == 0 &&
4012 strcmp(player1Name, black) == 0) {
4013 if (appData.debugMode)
4014 fprintf(debugFP, "Remembered ratings: W %d, B %d\n",
4015 player2Rating, player1Rating);
4016 gameInfo.whiteRating = player2Rating;
4017 gameInfo.blackRating = player1Rating;
4019 player1Name[0] = player2Name[0] = NULLCHAR;
4021 /* Silence shouts if requested */
4022 if (appData.quietPlay &&
4023 (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)) {
4024 SendToICS(ics_prefix);
4025 SendToICS("set shout 0\n");
4029 /* Deal with midgame name changes */
4031 if (!gameInfo.white || strcmp(gameInfo.white, white) != 0) {
4032 if (gameInfo.white) free(gameInfo.white);
4033 gameInfo.white = StrSave(white);
4035 if (!gameInfo.black || strcmp(gameInfo.black, black) != 0) {
4036 if (gameInfo.black) free(gameInfo.black);
4037 gameInfo.black = StrSave(black);
4041 /* Throw away game result if anything actually changes in examine mode */
4042 if (gameMode == IcsExamining && !newGame) {
4043 gameInfo.result = GameUnfinished;
4044 if (gameInfo.resultDetails != NULL) {
4045 free(gameInfo.resultDetails);
4046 gameInfo.resultDetails = NULL;
4050 /* In pausing && IcsExamining mode, we ignore boards coming
4051 in if they are in a different variation than we are. */
4052 if (pauseExamInvalid) return;
4053 if (pausing && gameMode == IcsExamining) {
4054 if (moveNum <= pauseExamForwardMostMove) {
4055 pauseExamInvalid = TRUE;
4056 forwardMostMove = pauseExamForwardMostMove;
4061 if (appData.debugMode) {
4062 fprintf(debugFP, "load %dx%d board\n", files, ranks);
4064 /* Parse the board */
4065 for (k = 0; k < ranks; k++) {
4066 for (j = 0; j < files; j++)
4067 board[k][j+gameInfo.holdingsWidth] = CharToPiece(board_chars[(ranks-1-k)*(files+1) + j]);
4068 if(gameInfo.holdingsWidth > 1) {
4069 board[k][0] = board[k][BOARD_WIDTH-1] = EmptySquare;
4070 board[k][1] = board[k][BOARD_WIDTH-2] = (ChessSquare) 0;;
4073 CopyBoard(boards[moveNum], board);
4074 boards[moveNum][HOLDINGS_SET] = 0; // [HGM] indicate holdings not set
4076 startedFromSetupPosition =
4077 !CompareBoards(board, initialPosition);
4078 if(startedFromSetupPosition)
4079 initialRulePlies = irrev_count; /* [HGM] 50-move counter offset */
4082 /* [HGM] Set castling rights. Take the outermost Rooks,
4083 to make it also work for FRC opening positions. Note that board12
4084 is really defective for later FRC positions, as it has no way to
4085 indicate which Rook can castle if they are on the same side of King.
4086 For the initial position we grant rights to the outermost Rooks,
4087 and remember thos rights, and we then copy them on positions
4088 later in an FRC game. This means WB might not recognize castlings with
4089 Rooks that have moved back to their original position as illegal,
4090 but in ICS mode that is not its job anyway.
4092 if(moveNum == 0 || gameInfo.variant != VariantFischeRandom)
4093 { int i, j; ChessSquare wKing = WhiteKing, bKing = BlackKing;
4095 for(i=BOARD_LEFT, j=NoRights; i<BOARD_RGHT; i++)
4096 if(board[0][i] == WhiteRook) j = i;
4097 initialRights[0] = boards[moveNum][CASTLING][0] = (castle_ws == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4098 for(i=BOARD_RGHT-1, j=NoRights; i>=BOARD_LEFT; i--)
4099 if(board[0][i] == WhiteRook) j = i;
4100 initialRights[1] = boards[moveNum][CASTLING][1] = (castle_wl == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4101 for(i=BOARD_LEFT, j=NoRights; i<BOARD_RGHT; i++)
4102 if(board[BOARD_HEIGHT-1][i] == BlackRook) j = i;
4103 initialRights[3] = boards[moveNum][CASTLING][3] = (castle_bs == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4104 for(i=BOARD_RGHT-1, j=NoRights; i>=BOARD_LEFT; i--)
4105 if(board[BOARD_HEIGHT-1][i] == BlackRook) j = i;
4106 initialRights[4] = boards[moveNum][CASTLING][4] = (castle_bl == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4108 if(gameInfo.variant == VariantKnightmate) { wKing = WhiteUnicorn; bKing = BlackUnicorn; }
4109 for(k=BOARD_LEFT; k<BOARD_RGHT; k++)
4110 if(board[0][k] == wKing) initialRights[2] = boards[moveNum][CASTLING][2] = k;
4111 for(k=BOARD_LEFT; k<BOARD_RGHT; k++)
4112 if(board[BOARD_HEIGHT-1][k] == bKing)
4113 initialRights[5] = boards[moveNum][CASTLING][5] = k;
4114 if(gameInfo.variant == VariantTwoKings) {
4115 // In TwoKings looking for a King does not work, so always give castling rights to a King on e1/e8
4116 if(board[0][4] == wKing) initialRights[2] = boards[moveNum][CASTLING][2] = 4;
4117 if(board[BOARD_HEIGHT-1][4] == bKing) initialRights[5] = boards[moveNum][CASTLING][5] = 4;
4120 r = boards[moveNum][CASTLING][0] = initialRights[0];
4121 if(board[0][r] != WhiteRook) boards[moveNum][CASTLING][0] = NoRights;
4122 r = boards[moveNum][CASTLING][1] = initialRights[1];
4123 if(board[0][r] != WhiteRook) boards[moveNum][CASTLING][1] = NoRights;
4124 r = boards[moveNum][CASTLING][3] = initialRights[3];
4125 if(board[BOARD_HEIGHT-1][r] != BlackRook) boards[moveNum][CASTLING][3] = NoRights;
4126 r = boards[moveNum][CASTLING][4] = initialRights[4];
4127 if(board[BOARD_HEIGHT-1][r] != BlackRook) boards[moveNum][CASTLING][4] = NoRights;
4128 /* wildcastle kludge: always assume King has rights */
4129 r = boards[moveNum][CASTLING][2] = initialRights[2];
4130 r = boards[moveNum][CASTLING][5] = initialRights[5];
4132 /* [HGM] e.p. rights. Assume that ICS sends file number here? */
4133 boards[moveNum][EP_STATUS] = double_push == -1 ? EP_NONE : double_push + BOARD_LEFT;
4136 if (ics_getting_history == H_GOT_REQ_HEADER ||
4137 ics_getting_history == H_GOT_UNREQ_HEADER) {
4138 /* This was an initial position from a move list, not
4139 the current position */
4143 /* Update currentMove and known move number limits */
4144 newMove = newGame || moveNum > forwardMostMove;
4147 forwardMostMove = backwardMostMove = currentMove = moveNum;
4148 if (gameMode == IcsExamining && moveNum == 0) {
4149 /* Workaround for ICS limitation: we are not told the wild
4150 type when starting to examine a game. But if we ask for
4151 the move list, the move list header will tell us */
4152 ics_getting_history = H_REQUESTED;
4153 sprintf(str, "%smoves %d\n", ics_prefix, gamenum);
4156 } else if (moveNum == forwardMostMove + 1 || moveNum == forwardMostMove
4157 || (moveNum < forwardMostMove && moveNum >= backwardMostMove)) {
4159 /* [DM] If we found takebacks during icsEngineAnalyze try send to engine */
4160 /* [HGM] applied this also to an engine that is silently watching */
4161 if (appData.zippyPlay && moveNum < forwardMostMove && first.initDone &&
4162 (gameMode == IcsObserving || gameMode == IcsExamining) &&
4163 gameInfo.variant == currentlyInitializedVariant) {
4164 takeback = forwardMostMove - moveNum;
4165 for (i = 0; i < takeback; i++) {
4166 if (appData.debugMode) fprintf(debugFP, "take back move\n");
4167 SendToProgram("undo\n", &first);
4172 forwardMostMove = moveNum;
4173 if (!pausing || currentMove > forwardMostMove)
4174 currentMove = forwardMostMove;
4176 /* New part of history that is not contiguous with old part */
4177 if (pausing && gameMode == IcsExamining) {
4178 pauseExamInvalid = TRUE;
4179 forwardMostMove = pauseExamForwardMostMove;
4182 if (gameMode == IcsExamining && moveNum > 0 && appData.getMoveList) {
4184 if(appData.zippyPlay && forwardMostMove > 0 && first.initDone) {
4185 // [HGM] when we will receive the move list we now request, it will be
4186 // fed to the engine from the first move on. So if the engine is not
4187 // in the initial position now, bring it there.
4188 InitChessProgram(&first, 0);
4191 ics_getting_history = H_REQUESTED;
4192 sprintf(str, "%smoves %d\n", ics_prefix, gamenum);
4195 forwardMostMove = backwardMostMove = currentMove = moveNum;
4198 /* Update the clocks */
4199 if (strchr(elapsed_time, '.')) {
4201 timeRemaining[0][moveNum] = whiteTimeRemaining = white_time;
4202 timeRemaining[1][moveNum] = blackTimeRemaining = black_time;
4204 /* Time is in seconds */
4205 timeRemaining[0][moveNum] = whiteTimeRemaining = white_time * 1000;
4206 timeRemaining[1][moveNum] = blackTimeRemaining = black_time * 1000;
4211 if (appData.zippyPlay && newGame &&
4212 gameMode != IcsObserving && gameMode != IcsIdle &&
4213 gameMode != IcsExamining)
4214 ZippyFirstBoard(moveNum, basetime, increment);
4217 /* Put the move on the move list, first converting
4218 to canonical algebraic form. */
4220 if (appData.debugMode) {
4221 if (appData.debugMode) { int f = forwardMostMove;
4222 fprintf(debugFP, "parseboard %d, castling = %d %d %d %d %d %d\n", f,
4223 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
4224 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
4226 fprintf(debugFP, "accepted move %s from ICS, parse it.\n", move_str);
4227 fprintf(debugFP, "moveNum = %d\n", moveNum);
4228 fprintf(debugFP, "board = %d-%d x %d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT);
4229 setbuf(debugFP, NULL);
4231 if (moveNum <= backwardMostMove) {
4232 /* We don't know what the board looked like before
4234 strcpy(parseList[moveNum - 1], move_str);
4235 strcat(parseList[moveNum - 1], " ");
4236 strcat(parseList[moveNum - 1], elapsed_time);
4237 moveList[moveNum - 1][0] = NULLCHAR;
4238 } else if (strcmp(move_str, "none") == 0) {
4239 // [HGM] long SAN: swapped order; test for 'none' before parsing move
4240 /* Again, we don't know what the board looked like;
4241 this is really the start of the game. */
4242 parseList[moveNum - 1][0] = NULLCHAR;
4243 moveList[moveNum - 1][0] = NULLCHAR;
4244 backwardMostMove = moveNum;
4245 startedFromSetupPosition = TRUE;
4246 fromX = fromY = toX = toY = -1;
4248 // [HGM] long SAN: if legality-testing is off, disambiguation might not work or give wrong move.
4249 // So we parse the long-algebraic move string in stead of the SAN move
4250 int valid; char buf[MSG_SIZ], *prom;
4252 // str looks something like "Q/a1-a2"; kill the slash
4254 sprintf(buf, "%c%s", str[0], str+2);
4255 else strcpy(buf, str); // might be castling
4256 if((prom = strstr(move_str, "=")) && !strstr(buf, "="))
4257 strcat(buf, prom); // long move lacks promo specification!
4258 if(!appData.testLegality && move_str[1] != '@') { // drops never ambiguous (parser chokes on long form!)
4259 if(appData.debugMode)
4260 fprintf(debugFP, "replaced ICS move '%s' by '%s'\n", move_str, buf);
4261 strcpy(move_str, buf);
4263 valid = ParseOneMove(move_str, moveNum - 1, &moveType,
4264 &fromX, &fromY, &toX, &toY, &promoChar)
4265 || ParseOneMove(buf, moveNum - 1, &moveType,
4266 &fromX, &fromY, &toX, &toY, &promoChar);
4267 // end of long SAN patch
4269 (void) CoordsToAlgebraic(boards[moveNum - 1],
4270 PosFlags(moveNum - 1),
4271 fromY, fromX, toY, toX, promoChar,
4272 parseList[moveNum-1]);
4273 switch (MateTest(boards[moveNum], PosFlags(moveNum)) ) {
4279 if(gameInfo.variant != VariantShogi)
4280 strcat(parseList[moveNum - 1], "+");
4283 case MT_STAINMATE: // [HGM] xq: for notation stalemate that wins counts as checkmate
4284 strcat(parseList[moveNum - 1], "#");
4287 strcat(parseList[moveNum - 1], " ");
4288 strcat(parseList[moveNum - 1], elapsed_time);
4289 /* currentMoveString is set as a side-effect of ParseOneMove */
4290 strcpy(moveList[moveNum - 1], currentMoveString);
4291 strcat(moveList[moveNum - 1], "\n");
4293 /* Move from ICS was illegal!? Punt. */
4294 if (appData.debugMode) {
4295 fprintf(debugFP, "Illegal move from ICS '%s'\n", move_str);
4296 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
4298 strcpy(parseList[moveNum - 1], move_str);
4299 strcat(parseList[moveNum - 1], " ");
4300 strcat(parseList[moveNum - 1], elapsed_time);
4301 moveList[moveNum - 1][0] = NULLCHAR;
4302 fromX = fromY = toX = toY = -1;
4305 if (appData.debugMode) {
4306 fprintf(debugFP, "Move parsed to '%s'\n", parseList[moveNum - 1]);
4307 setbuf(debugFP, NULL);
4311 /* Send move to chess program (BEFORE animating it). */
4312 if (appData.zippyPlay && !newGame && newMove &&
4313 (!appData.getMoveList || backwardMostMove == 0) && first.initDone) {
4315 if ((gameMode == IcsPlayingWhite && WhiteOnMove(moveNum)) ||
4316 (gameMode == IcsPlayingBlack && !WhiteOnMove(moveNum))) {
4317 if (moveList[moveNum - 1][0] == NULLCHAR) {
4318 sprintf(str, _("Couldn't parse move \"%s\" from ICS"),
4320 DisplayError(str, 0);
4322 if (first.sendTime) {
4323 SendTimeRemaining(&first, gameMode == IcsPlayingWhite);
4325 bookHit = SendMoveToBookUser(moveNum - 1, &first, FALSE); // [HGM] book
4326 if (firstMove && !bookHit) {
4328 if (first.useColors) {
4329 SendToProgram(gameMode == IcsPlayingWhite ?
4331 "black\ngo\n", &first);
4333 SendToProgram("go\n", &first);
4335 first.maybeThinking = TRUE;
4338 } else if (gameMode == IcsObserving || gameMode == IcsExamining) {
4339 if (moveList[moveNum - 1][0] == NULLCHAR) {
4340 sprintf(str, _("Couldn't parse move \"%s\" from ICS"), move_str);
4341 DisplayError(str, 0);
4343 if(gameInfo.variant == currentlyInitializedVariant) // [HGM] refrain sending moves engine can't understand!
4344 SendMoveToProgram(moveNum - 1, &first);
4351 if (moveNum > 0 && !gotPremove && !appData.noGUI) {
4352 /* If move comes from a remote source, animate it. If it
4353 isn't remote, it will have already been animated. */
4354 if (!pausing && !ics_user_moved && prevMove == moveNum - 1) {
4355 AnimateMove(boards[moveNum - 1], fromX, fromY, toX, toY);
4357 if (!pausing && appData.highlightLastMove) {
4358 SetHighlights(fromX, fromY, toX, toY);
4362 /* Start the clocks */
4363 whiteFlag = blackFlag = FALSE;
4364 appData.clockMode = !(basetime == 0 && increment == 0);
4366 ics_clock_paused = TRUE;
4368 } else if (ticking == 1) {
4369 ics_clock_paused = FALSE;
4371 if (gameMode == IcsIdle ||
4372 relation == RELATION_OBSERVING_STATIC ||
4373 relation == RELATION_EXAMINING ||
4375 DisplayBothClocks();
4379 /* Display opponents and material strengths */
4380 if (gameInfo.variant != VariantBughouse &&
4381 gameInfo.variant != VariantCrazyhouse && !appData.noGUI) {
4382 if (tinyLayout || smallLayout) {
4383 if(gameInfo.variant == VariantNormal)
4384 sprintf(str, "%s(%d) %s(%d) {%d %d}",
4385 gameInfo.white, white_stren, gameInfo.black, black_stren,
4386 basetime, increment);
4388 sprintf(str, "%s(%d) %s(%d) {%d %d w%d}",
4389 gameInfo.white, white_stren, gameInfo.black, black_stren,
4390 basetime, increment, (int) gameInfo.variant);
4392 if(gameInfo.variant == VariantNormal)
4393 sprintf(str, "%s (%d) vs. %s (%d) {%d %d}",
4394 gameInfo.white, white_stren, gameInfo.black, black_stren,
4395 basetime, increment);
4397 sprintf(str, "%s (%d) vs. %s (%d) {%d %d %s}",
4398 gameInfo.white, white_stren, gameInfo.black, black_stren,
4399 basetime, increment, VariantName(gameInfo.variant));
4402 if (appData.debugMode) {
4403 fprintf(debugFP, "Display title '%s, gameInfo.variant = %d'\n", str, gameInfo.variant);
4408 /* Display the board */
4409 if (!pausing && !appData.noGUI) {
4411 if (appData.premove)
4413 ((gameMode == IcsPlayingWhite) && (WhiteOnMove(currentMove))) ||
4414 ((gameMode == IcsPlayingBlack) && (!WhiteOnMove(currentMove))))
4415 ClearPremoveHighlights();
4417 j = seekGraphUp; seekGraphUp = FALSE; // [HGM] seekgraph: when we draw a board, it overwrites the seek graph
4418 if(partnerUp) { flipView = originalFlip; partnerUp = FALSE; j = TRUE; } // [HGM] bughouse: restore view
4419 DrawPosition(j, boards[currentMove]);
4421 DisplayMove(moveNum - 1);
4422 if (appData.ringBellAfterMoves && /*!ics_user_moved*/ // [HGM] use absolute method to recognize own move
4423 !((gameMode == IcsPlayingWhite) && (!WhiteOnMove(moveNum)) ||
4424 (gameMode == IcsPlayingBlack) && (WhiteOnMove(moveNum)) ) ) {
4425 if(newMove) RingBell(); else PlayIcsUnfinishedSound();
4429 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
4431 if(bookHit) { // [HGM] book: simulate book reply
4432 static char bookMove[MSG_SIZ]; // a bit generous?
4434 programStats.nodes = programStats.depth = programStats.time =
4435 programStats.score = programStats.got_only_move = 0;
4436 sprintf(programStats.movelist, "%s (xbook)", bookHit);
4438 strcpy(bookMove, "move ");
4439 strcat(bookMove, bookHit);
4440 HandleMachineMove(bookMove, &first);
4449 if (appData.icsActive && gameMode != IcsIdle && ics_gamenum > 0) {
4450 ics_getting_history = H_REQUESTED;
4451 sprintf(buf, "%smoves %d\n", ics_prefix, ics_gamenum);
4457 AnalysisPeriodicEvent(force)
4460 if (((programStats.ok_to_send == 0 || programStats.line_is_book)
4461 && !force) || !appData.periodicUpdates)
4464 /* Send . command to Crafty to collect stats */
4465 SendToProgram(".\n", &first);
4467 /* Don't send another until we get a response (this makes
4468 us stop sending to old Crafty's which don't understand
4469 the "." command (sending illegal cmds resets node count & time,
4470 which looks bad)) */
4471 programStats.ok_to_send = 0;
4474 void ics_update_width(new_width)
4477 ics_printf("set width %d\n", new_width);
4481 SendMoveToProgram(moveNum, cps)
4483 ChessProgramState *cps;
4487 if (cps->useUsermove) {
4488 SendToProgram("usermove ", cps);
4492 if ((space = strchr(parseList[moveNum], ' ')) != NULL) {
4493 int len = space - parseList[moveNum];
4494 memcpy(buf, parseList[moveNum], len);
4496 buf[len] = NULLCHAR;
4498 sprintf(buf, "%s\n", parseList[moveNum]);
4500 SendToProgram(buf, cps);
4502 if(cps->alphaRank) { /* [HGM] shogi: temporarily convert to shogi coordinates before sending */
4503 AlphaRank(moveList[moveNum], 4);
4504 SendToProgram(moveList[moveNum], cps);
4505 AlphaRank(moveList[moveNum], 4); // and back
4507 /* Added by Tord: Send castle moves in "O-O" in FRC games if required by
4508 * the engine. It would be nice to have a better way to identify castle
4510 if((gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom)
4511 && cps->useOOCastle) {
4512 int fromX = moveList[moveNum][0] - AAA;
4513 int fromY = moveList[moveNum][1] - ONE;
4514 int toX = moveList[moveNum][2] - AAA;
4515 int toY = moveList[moveNum][3] - ONE;
4516 if((boards[moveNum][fromY][fromX] == WhiteKing
4517 && boards[moveNum][toY][toX] == WhiteRook)
4518 || (boards[moveNum][fromY][fromX] == BlackKing
4519 && boards[moveNum][toY][toX] == BlackRook)) {
4520 if(toX > fromX) SendToProgram("O-O\n", cps);
4521 else SendToProgram("O-O-O\n", cps);
4523 else SendToProgram(moveList[moveNum], cps);
4525 else SendToProgram(moveList[moveNum], cps);
4526 /* End of additions by Tord */
4529 /* [HGM] setting up the opening has brought engine in force mode! */
4530 /* Send 'go' if we are in a mode where machine should play. */
4531 if( (moveNum == 0 && setboardSpoiledMachineBlack && cps == &first) &&
4532 (gameMode == TwoMachinesPlay ||
4534 gameMode == IcsPlayingBlack || gameMode == IcsPlayingWhite ||
4536 gameMode == MachinePlaysBlack || gameMode == MachinePlaysWhite) ) {
4537 SendToProgram("go\n", cps);
4538 if (appData.debugMode) {
4539 fprintf(debugFP, "(extra)\n");
4542 setboardSpoiledMachineBlack = 0;
4546 SendMoveToICS(moveType, fromX, fromY, toX, toY)
4548 int fromX, fromY, toX, toY;
4550 char user_move[MSG_SIZ];
4554 sprintf(user_move, _("say Internal error; bad moveType %d (%d,%d-%d,%d)"),
4555 (int)moveType, fromX, fromY, toX, toY);
4556 DisplayError(user_move + strlen("say "), 0);
4558 case WhiteKingSideCastle:
4559 case BlackKingSideCastle:
4560 case WhiteQueenSideCastleWild:
4561 case BlackQueenSideCastleWild:
4563 case WhiteHSideCastleFR:
4564 case BlackHSideCastleFR:
4566 sprintf(user_move, "o-o\n");
4568 case WhiteQueenSideCastle:
4569 case BlackQueenSideCastle:
4570 case WhiteKingSideCastleWild:
4571 case BlackKingSideCastleWild:
4573 case WhiteASideCastleFR:
4574 case BlackASideCastleFR:
4576 sprintf(user_move, "o-o-o\n");
4578 case WhiteNonPromotion:
4579 case BlackNonPromotion:
4580 sprintf(user_move, "%c%c%c%c=\n", AAA + fromX, ONE + fromY, AAA + toX, ONE + toY);
4582 case WhitePromotionQueen:
4583 case BlackPromotionQueen:
4584 case WhitePromotionRook:
4585 case BlackPromotionRook:
4586 case WhitePromotionBishop:
4587 case BlackPromotionBishop:
4588 case WhitePromotionKnight:
4589 case BlackPromotionKnight:
4590 case WhitePromotionKing:
4591 case BlackPromotionKing:
4592 case WhitePromotionChancellor:
4593 case BlackPromotionChancellor:
4594 case WhitePromotionArchbishop:
4595 case BlackPromotionArchbishop:
4596 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
4597 sprintf(user_move, "%c%c%c%c=%c\n",
4598 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
4599 PieceToChar(WhiteFerz));
4600 else if(gameInfo.variant == VariantGreat)
4601 sprintf(user_move, "%c%c%c%c=%c\n",
4602 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
4603 PieceToChar(WhiteMan));
4605 sprintf(user_move, "%c%c%c%c=%c\n",
4606 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
4607 PieceToChar(PromoPiece(moveType)));
4611 sprintf(user_move, "%c@%c%c\n",
4612 ToUpper(PieceToChar((ChessSquare) fromX)),
4613 AAA + toX, ONE + toY);
4616 case WhiteCapturesEnPassant:
4617 case BlackCapturesEnPassant:
4618 case IllegalMove: /* could be a variant we don't quite understand */
4619 sprintf(user_move, "%c%c%c%c\n",
4620 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY);
4623 SendToICS(user_move);
4624 if(appData.keepAlive) // [HGM] alive: schedule sending of dummy 'date' command
4625 ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
4630 { // [HGM] upload: send entire stored game to ICS as long-algebraic moves.
4631 int i, last = forwardMostMove; // make sure ICS reply cannot pre-empt us by clearing fmm
4632 static char *castlingStrings[4] = { "none", "kside", "qside", "both" };
4633 if(gameMode == IcsObserving || gameMode == IcsPlayingBlack || gameMode == IcsPlayingWhite) {
4634 DisplayError("You cannot do this while you are playing or observing", 0);
4637 if(gameMode != IcsExamining) { // is this ever not the case?
4638 char buf[MSG_SIZ], *p, *fen, command[MSG_SIZ], bsetup = 0;
4640 if(ics_type == ICS_ICC) { // on ICC match ourselves in applicable variant
4641 sprintf(command, "match %s", ics_handle);
4642 } else { // on FICS we must first go to general examine mode
4643 strcpy(command, "examine\nbsetup"); // and specify variant within it with bsetups
4645 if(gameInfo.variant != VariantNormal) {
4646 // try figure out wild number, as xboard names are not always valid on ICS
4647 for(i=1; i<=36; i++) {
4648 sprintf(buf, "wild/%d", i);
4649 if(StringToVariant(buf) == gameInfo.variant) break;
4651 if(i<=36 && ics_type == ICS_ICC) sprintf(buf, "%s w%d\n", command, i);
4652 else if(i == 22) sprintf(buf, "%s fr\n", command);
4653 else sprintf(buf, "%s %s\n", command, VariantName(gameInfo.variant));
4654 } else sprintf(buf, "%s\n", ics_type == ICS_ICC ? command : "examine\n"); // match yourself or examine
4655 SendToICS(ics_prefix);
4657 if(startedFromSetupPosition || backwardMostMove != 0) {
4658 fen = PositionToFEN(backwardMostMove, NULL);
4659 if(ics_type == ICS_ICC) { // on ICC we can simply send a complete FEN to set everything
4660 sprintf(buf, "loadfen %s\n", fen);
4662 } else { // FICS: everything has to set by separate bsetup commands
4663 p = strchr(fen, ' '); p[0] = NULLCHAR; // cut after board
4664 sprintf(buf, "bsetup fen %s\n", fen);
4666 if(!WhiteOnMove(backwardMostMove)) {
4667 SendToICS("bsetup tomove black\n");
4669 i = (strchr(p+3, 'K') != NULL) + 2*(strchr(p+3, 'Q') != NULL);
4670 sprintf(buf, "bsetup wcastle %s\n", castlingStrings[i]);
4672 i = (strchr(p+3, 'k') != NULL) + 2*(strchr(p+3, 'q') != NULL);
4673 sprintf(buf, "bsetup bcastle %s\n", castlingStrings[i]);
4675 i = boards[backwardMostMove][EP_STATUS];
4676 if(i >= 0) { // set e.p.
4677 sprintf(buf, "bsetup eppos %c\n", i+AAA);
4683 if(bsetup || ics_type != ICS_ICC && gameInfo.variant != VariantNormal)
4684 SendToICS("bsetup done\n"); // switch to normal examining.
4686 for(i = backwardMostMove; i<last; i++) {
4688 sprintf(buf, "%s\n", parseList[i]);
4691 SendToICS(ics_prefix);
4692 SendToICS(ics_type == ICS_ICC ? "tag result Game in progress\n" : "commit\n");
4696 CoordsToComputerAlgebraic(rf, ff, rt, ft, promoChar, move)
4701 if (rf == DROP_RANK) {
4702 sprintf(move, "%c@%c%c\n",
4703 ToUpper(PieceToChar((ChessSquare) ff)), AAA + ft, ONE + rt);
4705 if (promoChar == 'x' || promoChar == NULLCHAR) {
4706 sprintf(move, "%c%c%c%c\n",
4707 AAA + ff, ONE + rf, AAA + ft, ONE + rt);
4709 sprintf(move, "%c%c%c%c%c\n",
4710 AAA + ff, ONE + rf, AAA + ft, ONE + rt, promoChar);
4716 ProcessICSInitScript(f)
4721 while (fgets(buf, MSG_SIZ, f)) {
4722 SendToICSDelayed(buf,(long)appData.msLoginDelay);
4729 /* [HGM] Shogi move preprocessor: swap digits for letters, vice versa */
4731 AlphaRank(char *move, int n)
4733 // char *p = move, c; int x, y;
4735 if (appData.debugMode) {
4736 fprintf(debugFP, "alphaRank(%s,%d)\n", move, n);
4740 move[2]>='0' && move[2]<='9' &&
4741 move[3]>='a' && move[3]<='x' ) {
4743 move[2] = BOARD_RGHT -1 - (move[2]-'1') + AAA;
4744 move[3] = BOARD_HEIGHT-1 - (move[3]-'a') + ONE;
4746 if(move[0]>='0' && move[0]<='9' &&
4747 move[1]>='a' && move[1]<='x' &&
4748 move[2]>='0' && move[2]<='9' &&
4749 move[3]>='a' && move[3]<='x' ) {
4750 /* input move, Shogi -> normal */
4751 move[0] = BOARD_RGHT -1 - (move[0]-'1') + AAA;
4752 move[1] = BOARD_HEIGHT-1 - (move[1]-'a') + ONE;
4753 move[2] = BOARD_RGHT -1 - (move[2]-'1') + AAA;
4754 move[3] = BOARD_HEIGHT-1 - (move[3]-'a') + ONE;
4757 move[3]>='0' && move[3]<='9' &&
4758 move[2]>='a' && move[2]<='x' ) {
4760 move[2] = BOARD_RGHT - 1 - (move[2]-AAA) + '1';
4761 move[3] = BOARD_HEIGHT-1 - (move[3]-ONE) + 'a';
4764 move[0]>='a' && move[0]<='x' &&
4765 move[3]>='0' && move[3]<='9' &&
4766 move[2]>='a' && move[2]<='x' ) {
4767 /* output move, normal -> Shogi */
4768 move[0] = BOARD_RGHT - 1 - (move[0]-AAA) + '1';
4769 move[1] = BOARD_HEIGHT-1 - (move[1]-ONE) + 'a';
4770 move[2] = BOARD_RGHT - 1 - (move[2]-AAA) + '1';
4771 move[3] = BOARD_HEIGHT-1 - (move[3]-ONE) + 'a';
4772 if(move[4] == PieceToChar(BlackQueen)) move[4] = '+';
4774 if (appData.debugMode) {
4775 fprintf(debugFP, " out = '%s'\n", move);
4779 char yy_textstr[8000];
4781 /* Parser for moves from gnuchess, ICS, or user typein box */
4783 ParseOneMove(move, moveNum, moveType, fromX, fromY, toX, toY, promoChar)
4786 ChessMove *moveType;
4787 int *fromX, *fromY, *toX, *toY;
4790 if (appData.debugMode) {
4791 fprintf(debugFP, "move to parse: %s\n", move);
4793 *moveType = yylexstr(moveNum, move, yy_textstr, sizeof yy_textstr);
4795 switch (*moveType) {
4796 case WhitePromotionChancellor:
4797 case BlackPromotionChancellor:
4798 case WhitePromotionArchbishop:
4799 case BlackPromotionArchbishop:
4800 case WhitePromotionQueen:
4801 case BlackPromotionQueen:
4802 case WhitePromotionRook:
4803 case BlackPromotionRook:
4804 case WhitePromotionBishop:
4805 case BlackPromotionBishop:
4806 case WhitePromotionKnight:
4807 case BlackPromotionKnight:
4808 case WhitePromotionKing:
4809 case BlackPromotionKing:
4810 case WhiteNonPromotion:
4811 case BlackNonPromotion:
4813 case WhiteCapturesEnPassant:
4814 case BlackCapturesEnPassant:
4815 case WhiteKingSideCastle:
4816 case WhiteQueenSideCastle:
4817 case BlackKingSideCastle:
4818 case BlackQueenSideCastle:
4819 case WhiteKingSideCastleWild:
4820 case WhiteQueenSideCastleWild:
4821 case BlackKingSideCastleWild:
4822 case BlackQueenSideCastleWild:
4823 /* Code added by Tord: */
4824 case WhiteHSideCastleFR:
4825 case WhiteASideCastleFR:
4826 case BlackHSideCastleFR:
4827 case BlackASideCastleFR:
4828 /* End of code added by Tord */
4829 case IllegalMove: /* bug or odd chess variant */
4830 *fromX = currentMoveString[0] - AAA;
4831 *fromY = currentMoveString[1] - ONE;
4832 *toX = currentMoveString[2] - AAA;
4833 *toY = currentMoveString[3] - ONE;
4834 *promoChar = currentMoveString[4];
4835 if (*fromX < BOARD_LEFT || *fromX >= BOARD_RGHT || *fromY < 0 || *fromY >= BOARD_HEIGHT ||
4836 *toX < BOARD_LEFT || *toX >= BOARD_RGHT || *toY < 0 || *toY >= BOARD_HEIGHT) {
4837 if (appData.debugMode) {
4838 fprintf(debugFP, "Off-board move (%d,%d)-(%d,%d)%c, type = %d\n", *fromX, *fromY, *toX, *toY, *promoChar, *moveType);
4840 *fromX = *fromY = *toX = *toY = 0;
4843 if (appData.testLegality) {
4844 return (*moveType != IllegalMove);
4846 return !(*fromX == *toX && *fromY == *toY) && boards[moveNum][*fromY][*fromX] != EmptySquare &&
4847 WhiteOnMove(moveNum) == (boards[moveNum][*fromY][*fromX] < BlackPawn);
4852 *fromX = *moveType == WhiteDrop ?
4853 (int) CharToPiece(ToUpper(currentMoveString[0])) :
4854 (int) CharToPiece(ToLower(currentMoveString[0]));
4856 *toX = currentMoveString[2] - AAA;
4857 *toY = currentMoveString[3] - ONE;
4858 *promoChar = NULLCHAR;
4862 case ImpossibleMove:
4863 case (ChessMove) 0: /* end of file */
4872 if (appData.debugMode) {
4873 fprintf(debugFP, "Impossible move %s, type = %d\n", currentMoveString, *moveType);
4876 *fromX = *fromY = *toX = *toY = 0;
4877 *promoChar = NULLCHAR;
4884 ParsePV(char *pv, Boolean storeComments)
4885 { // Parse a string of PV moves, and append to current game, behind forwardMostMove
4886 int fromX, fromY, toX, toY; char promoChar;
4891 endPV = forwardMostMove;
4893 while(*pv == ' ' || *pv == '\n' || *pv == '\t') pv++; // must still read away whitespace
4894 if(nr == 0 && !storeComments && *pv == '(') pv++; // first (ponder) move can be in parentheses
4895 valid = ParseOneMove(pv, endPV, &moveType, &fromX, &fromY, &toX, &toY, &promoChar);
4896 if(appData.debugMode){
4897 fprintf(debugFP,"parsePV: %d %c%c%c%c yy='%s'\nPV = '%s'\n", valid, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, yy_textstr, pv);
4899 if(!valid && nr == 0 &&
4900 ParseOneMove(pv, endPV-1, &moveType, &fromX, &fromY, &toX, &toY, &promoChar)){
4901 nr++; moveType = Comment; // First move has been played; kludge to make sure we continue
4902 // Hande case where played move is different from leading PV move
4903 CopyBoard(boards[endPV+1], boards[endPV-1]); // tentatively unplay last game move
4904 CopyBoard(boards[endPV+2], boards[endPV-1]); // and play first move of PV
4905 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[endPV+2]);
4906 if(!CompareBoards(boards[endPV], boards[endPV+2])) {
4907 endPV += 2; // if position different, keep this
4908 moveList[endPV-1][0] = fromX + AAA;
4909 moveList[endPV-1][1] = fromY + ONE;
4910 moveList[endPV-1][2] = toX + AAA;
4911 moveList[endPV-1][3] = toY + ONE;
4912 parseList[endPV-1][0] = NULLCHAR;
4913 strcpy(moveList[endPV-2], "_0_0"); // suppress premove highlight on takeback move
4916 pv = strstr(pv, yy_textstr) + strlen(yy_textstr); // skip what we parsed
4917 if(nr == 0 && !storeComments && *pv == ')') pv++; // closing parenthesis of ponder move;
4918 if(moveType == Comment && storeComments) AppendComment(endPV, yy_textstr, FALSE);
4919 if(moveType == Comment || moveType == NAG || moveType == ElapsedTime) {
4920 valid++; // allow comments in PV
4924 if(endPV+1 > framePtr) break; // no space, truncate
4927 CopyBoard(boards[endPV], boards[endPV-1]);
4928 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[endPV]);
4929 moveList[endPV-1][0] = fromX + AAA;
4930 moveList[endPV-1][1] = fromY + ONE;
4931 moveList[endPV-1][2] = toX + AAA;
4932 moveList[endPV-1][3] = toY + ONE;
4934 CoordsToAlgebraic(boards[endPV - 1],
4935 PosFlags(endPV - 1),
4936 fromY, fromX, toY, toX, promoChar,
4937 parseList[endPV - 1]);
4939 parseList[endPV-1][0] = NULLCHAR;
4941 currentMove = endPV;
4942 if(currentMove == forwardMostMove) ClearPremoveHighlights(); else
4943 SetPremoveHighlights(moveList[currentMove-1][0]-AAA, moveList[currentMove-1][1]-ONE,
4944 moveList[currentMove-1][2]-AAA, moveList[currentMove-1][3]-ONE);
4945 DrawPosition(TRUE, boards[currentMove]);
4948 static int lastX, lastY;
4951 LoadMultiPV(int x, int y, char *buf, int index, int *start, int *end)
4956 if(index < 0 || index >= strlen(buf)) return FALSE; // sanity
4957 lastX = x; lastY = y;
4958 while(index > 0 && buf[index-1] != '\n') index--; // beginning of line
4960 while(buf[index] != '\n') if(buf[index++] == '\t') startPV = index;
4961 if(index == startPV && (p = StrCaseStr(buf+index, "PV="))) startPV = p - buf + 3;
4963 do{ while(buf[index] && buf[index] != '\n') index++;
4964 } while(buf[index] == '\n' && buf[index+1] == '\\' && buf[index+2] == ' ' && index++); // join kibitzed PV continuation line
4966 ParsePV(buf+startPV, FALSE);
4967 *start = startPV; *end = index-1;
4972 LoadPV(int x, int y)
4973 { // called on right mouse click to load PV
4974 int which = gameMode == TwoMachinesPlay && (WhiteOnMove(forwardMostMove) == (second.twoMachinesColor[0] == 'w'));
4975 lastX = x; lastY = y;
4976 ParsePV(lastPV[which], FALSE); // load the PV of the thinking engine in the boards array.
4983 if(endPV < 0) return;
4985 currentMove = forwardMostMove;
4986 ClearPremoveHighlights();
4987 DrawPosition(TRUE, boards[currentMove]);
4991 MovePV(int x, int y, int h)
4992 { // step through PV based on mouse coordinates (called on mouse move)
4993 int margin = h>>3, step = 0;
4995 if(endPV < 0) return;
4996 // we must somehow check if right button is still down (might be released off board!)
4997 if(y < margin && (abs(x - lastX) > 6 || abs(y - lastY) > 6)) step = 1; else
4998 if(y > h - margin && (abs(x - lastX) > 6 || abs(y - lastY) > 6)) step = -1; else
4999 if( y > lastY + 6 ) step = -1; else if(y < lastY - 6) step = 1;
5001 lastX = x; lastY = y;
5002 if(currentMove + step > endPV || currentMove + step < forwardMostMove) step = 0;
5003 currentMove += step;
5004 if(currentMove == forwardMostMove) ClearPremoveHighlights(); else
5005 SetPremoveHighlights(moveList[currentMove-1][0]-AAA, moveList[currentMove-1][1]-ONE,
5006 moveList[currentMove-1][2]-AAA, moveList[currentMove-1][3]-ONE);
5007 DrawPosition(FALSE, boards[currentMove]);
5011 // [HGM] shuffle: a general way to suffle opening setups, applicable to arbitrary variants.
5012 // All positions will have equal probability, but the current method will not provide a unique
5013 // numbering scheme for arrays that contain 3 or more pieces of the same kind.
5019 int piecesLeft[(int)BlackPawn];
5020 int seed, nrOfShuffles;
5022 void GetPositionNumber()
5023 { // sets global variable seed
5026 seed = appData.defaultFrcPosition;
5027 if(seed < 0) { // randomize based on time for negative FRC position numbers
5028 for(i=0; i<50; i++) seed += random();
5029 seed = random() ^ random() >> 8 ^ random() << 8;
5030 if(seed<0) seed = -seed;
5034 int put(Board board, int pieceType, int rank, int n, int shade)
5035 // put the piece on the (n-1)-th empty squares of the given shade
5039 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) {
5040 if( (((i-BOARD_LEFT)&1)+1) & shade && board[rank][i] == EmptySquare && n-- == 0) {
5041 board[rank][i] = (ChessSquare) pieceType;
5042 squaresLeft[((i-BOARD_LEFT)&1) + 1]--;
5044 piecesLeft[pieceType]--;
5052 void AddOnePiece(Board board, int pieceType, int rank, int shade)
5053 // calculate where the next piece goes, (any empty square), and put it there
5057 i = seed % squaresLeft[shade];
5058 nrOfShuffles *= squaresLeft[shade];
5059 seed /= squaresLeft[shade];
5060 put(board, pieceType, rank, i, shade);
5063 void AddTwoPieces(Board board, int pieceType, int rank)
5064 // calculate where the next 2 identical pieces go, (any empty square), and put it there
5066 int i, n=squaresLeft[ANY], j=n-1, k;
5068 k = n*(n-1)/2; // nr of possibilities, not counting permutations
5069 i = seed % k; // pick one
5072 while(i >= j) i -= j--;
5073 j = n - 1 - j; i += j;
5074 put(board, pieceType, rank, j, ANY);
5075 put(board, pieceType, rank, i, ANY);
5078 void SetUpShuffle(Board board, int number)
5082 GetPositionNumber(); nrOfShuffles = 1;
5084 squaresLeft[DARK] = (BOARD_RGHT - BOARD_LEFT + 1)/2;
5085 squaresLeft[ANY] = BOARD_RGHT - BOARD_LEFT;
5086 squaresLeft[LITE] = squaresLeft[ANY] - squaresLeft[DARK];
5088 for(p = 0; p<=(int)WhiteKing; p++) piecesLeft[p] = 0;
5090 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // count pieces and clear board
5091 p = (int) board[0][i];
5092 if(p < (int) BlackPawn) piecesLeft[p] ++;
5093 board[0][i] = EmptySquare;
5096 if(PosFlags(0) & F_ALL_CASTLE_OK) {
5097 // shuffles restricted to allow normal castling put KRR first
5098 if(piecesLeft[(int)WhiteKing]) // King goes rightish of middle
5099 put(board, WhiteKing, 0, (gameInfo.boardWidth+1)/2, ANY);
5100 else if(piecesLeft[(int)WhiteUnicorn]) // in Knightmate Unicorn castles
5101 put(board, WhiteUnicorn, 0, (gameInfo.boardWidth+1)/2, ANY);
5102 if(piecesLeft[(int)WhiteRook]) // First supply a Rook for K-side castling
5103 put(board, WhiteRook, 0, gameInfo.boardWidth-2, ANY);
5104 if(piecesLeft[(int)WhiteRook]) // Then supply a Rook for Q-side castling
5105 put(board, WhiteRook, 0, 0, ANY);
5106 // in variants with super-numerary Kings and Rooks, we leave these for the shuffle
5109 if(((BOARD_RGHT-BOARD_LEFT) & 1) == 0)
5110 // only for even boards make effort to put pairs of colorbound pieces on opposite colors
5111 for(p = (int) WhiteKing; p > (int) WhitePawn; p--) {
5112 if(p != (int) WhiteBishop && p != (int) WhiteFerz && p != (int) WhiteAlfil) continue;
5113 while(piecesLeft[p] >= 2) {
5114 AddOnePiece(board, p, 0, LITE);
5115 AddOnePiece(board, p, 0, DARK);
5117 // Odd color-bound pieces are shuffled with the rest (to not run out of paired squares)
5120 for(p = (int) WhiteKing - 2; p > (int) WhitePawn; p--) {
5121 // Remaining pieces (non-colorbound, or odd color bound) can be put anywhere
5122 // but we leave King and Rooks for last, to possibly obey FRC restriction
5123 if(p == (int)WhiteRook) continue;
5124 while(piecesLeft[p] >= 2) AddTwoPieces(board, p, 0); // add in pairs, for not counting permutations
5125 if(piecesLeft[p]) AddOnePiece(board, p, 0, ANY); // add the odd piece
5128 // now everything is placed, except perhaps King (Unicorn) and Rooks
5130 if(PosFlags(0) & F_FRC_TYPE_CASTLING) {
5131 // Last King gets castling rights
5132 while(piecesLeft[(int)WhiteUnicorn]) {
5133 i = put(board, WhiteUnicorn, 0, piecesLeft[(int)WhiteRook]/2, ANY);
5134 initialRights[2] = initialRights[5] = board[CASTLING][2] = board[CASTLING][5] = i;
5137 while(piecesLeft[(int)WhiteKing]) {
5138 i = put(board, WhiteKing, 0, piecesLeft[(int)WhiteRook]/2, ANY);
5139 initialRights[2] = initialRights[5] = board[CASTLING][2] = board[CASTLING][5] = i;
5144 while(piecesLeft[(int)WhiteKing]) AddOnePiece(board, WhiteKing, 0, ANY);
5145 while(piecesLeft[(int)WhiteUnicorn]) AddOnePiece(board, WhiteUnicorn, 0, ANY);
5148 // Only Rooks can be left; simply place them all
5149 while(piecesLeft[(int)WhiteRook]) {
5150 i = put(board, WhiteRook, 0, 0, ANY);
5151 if(PosFlags(0) & F_FRC_TYPE_CASTLING) { // first and last Rook get FRC castling rights
5154 initialRights[1] = initialRights[4] = board[CASTLING][1] = board[CASTLING][4] = i;
5156 initialRights[0] = initialRights[3] = board[CASTLING][0] = board[CASTLING][3] = i;
5159 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // copy black from white
5160 board[BOARD_HEIGHT-1][i] = (int) board[0][i] < BlackPawn ? WHITE_TO_BLACK board[0][i] : EmptySquare;
5163 if(number >= 0) appData.defaultFrcPosition %= nrOfShuffles; // normalize
5166 int SetCharTable( char *table, const char * map )
5167 /* [HGM] moved here from winboard.c because of its general usefulness */
5168 /* Basically a safe strcpy that uses the last character as King */
5170 int result = FALSE; int NrPieces;
5172 if( map != NULL && (NrPieces=strlen(map)) <= (int) EmptySquare
5173 && NrPieces >= 12 && !(NrPieces&1)) {
5174 int i; /* [HGM] Accept even length from 12 to 34 */
5176 for( i=0; i<(int) EmptySquare; i++ ) table[i] = '.';
5177 for( i=0; i<NrPieces/2-1; i++ ) {
5179 table[i + (int)BlackPawn - (int) WhitePawn] = map[i+NrPieces/2];
5181 table[(int) WhiteKing] = map[NrPieces/2-1];
5182 table[(int) BlackKing] = map[NrPieces-1];
5190 void Prelude(Board board)
5191 { // [HGM] superchess: random selection of exo-pieces
5192 int i, j, k; ChessSquare p;
5193 static ChessSquare exoPieces[4] = { WhiteAngel, WhiteMarshall, WhiteSilver, WhiteLance };
5195 GetPositionNumber(); // use FRC position number
5197 if(appData.pieceToCharTable != NULL) { // select pieces to participate from given char table
5198 SetCharTable(pieceToChar, appData.pieceToCharTable);
5199 for(i=(int)WhiteQueen+1, j=0; i<(int)WhiteKing && j<4; i++)
5200 if(PieceToChar((ChessSquare)i) != '.') exoPieces[j++] = (ChessSquare) i;
5203 j = seed%4; seed /= 4;
5204 p = board[0][BOARD_LEFT+j]; board[0][BOARD_LEFT+j] = EmptySquare; k = PieceToNumber(p);
5205 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5206 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5207 j = seed%3 + (seed%3 >= j); seed /= 3;
5208 p = board[0][BOARD_LEFT+j]; board[0][BOARD_LEFT+j] = EmptySquare; k = PieceToNumber(p);
5209 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5210 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5211 j = seed%3; seed /= 3;
5212 p = board[0][BOARD_LEFT+j+5]; board[0][BOARD_LEFT+j+5] = EmptySquare; k = PieceToNumber(p);
5213 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5214 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5215 j = seed%2 + (seed%2 >= j); seed /= 2;
5216 p = board[0][BOARD_LEFT+j+5]; board[0][BOARD_LEFT+j+5] = EmptySquare; k = PieceToNumber(p);
5217 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5218 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5219 j = seed%4; seed /= 4; put(board, exoPieces[3], 0, j, ANY);
5220 j = seed%3; seed /= 3; put(board, exoPieces[2], 0, j, ANY);
5221 j = seed%2; seed /= 2; put(board, exoPieces[1], 0, j, ANY);
5222 put(board, exoPieces[0], 0, 0, ANY);
5223 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) board[BOARD_HEIGHT-1][i] = WHITE_TO_BLACK board[0][i];
5227 InitPosition(redraw)
5230 ChessSquare (* pieces)[BOARD_FILES];
5231 int i, j, pawnRow, overrule,
5232 oldx = gameInfo.boardWidth,
5233 oldy = gameInfo.boardHeight,
5234 oldh = gameInfo.holdingsWidth,
5235 oldv = gameInfo.variant;
5237 if(appData.icsActive) shuffleOpenings = FALSE; // [HGM] shuffle: in ICS mode, only shuffle on ICS request
5239 /* [AS] Initialize pv info list [HGM] and game status */
5241 for( i=0; i<=framePtr; i++ ) { // [HGM] vari: spare saved variations
5242 pvInfoList[i].depth = 0;
5243 boards[i][EP_STATUS] = EP_NONE;
5244 for( j=0; j<BOARD_FILES-2; j++ ) boards[i][CASTLING][j] = NoRights;
5247 initialRulePlies = 0; /* 50-move counter start */
5249 castlingRank[0] = castlingRank[1] = castlingRank[2] = 0;
5250 castlingRank[3] = castlingRank[4] = castlingRank[5] = BOARD_HEIGHT-1;
5254 /* [HGM] logic here is completely changed. In stead of full positions */
5255 /* the initialized data only consist of the two backranks. The switch */
5256 /* selects which one we will use, which is than copied to the Board */
5257 /* initialPosition, which for the rest is initialized by Pawns and */
5258 /* empty squares. This initial position is then copied to boards[0], */
5259 /* possibly after shuffling, so that it remains available. */
5261 gameInfo.holdingsWidth = 0; /* default board sizes */
5262 gameInfo.boardWidth = 8;
5263 gameInfo.boardHeight = 8;
5264 gameInfo.holdingsSize = 0;
5265 nrCastlingRights = -1; /* [HGM] Kludge to indicate default should be used */
5266 for(i=0; i<BOARD_FILES-2; i++)
5267 initialPosition[CASTLING][i] = initialRights[i] = NoRights; /* but no rights yet */
5268 initialPosition[EP_STATUS] = EP_NONE;
5269 SetCharTable(pieceToChar, "PNBRQ...........Kpnbrq...........k");
5270 if(startVariant == gameInfo.variant) // [HGM] nicks: enable nicknames in original variant
5271 SetCharTable(pieceNickName, appData.pieceNickNames);
5272 else SetCharTable(pieceNickName, "............");
5274 switch (gameInfo.variant) {
5275 case VariantFischeRandom:
5276 shuffleOpenings = TRUE;
5280 case VariantShatranj:
5281 pieces = ShatranjArray;
5282 nrCastlingRights = 0;
5283 SetCharTable(pieceToChar, "PN.R.QB...Kpn.r.qb...k");
5286 pieces = makrukArray;
5287 nrCastlingRights = 0;
5288 startedFromSetupPosition = TRUE;
5289 SetCharTable(pieceToChar, "PN.R.M....SKpn.r.m....sk");
5291 case VariantTwoKings:
5292 pieces = twoKingsArray;
5294 case VariantCapaRandom:
5295 shuffleOpenings = TRUE;
5296 case VariantCapablanca:
5297 pieces = CapablancaArray;
5298 gameInfo.boardWidth = 10;
5299 SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
5302 pieces = GothicArray;
5303 gameInfo.boardWidth = 10;
5304 SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
5307 pieces = JanusArray;
5308 gameInfo.boardWidth = 10;
5309 SetCharTable(pieceToChar, "PNBRQ..JKpnbrq..jk");
5310 nrCastlingRights = 6;
5311 initialPosition[CASTLING][0] = initialRights[0] = BOARD_RGHT-1;
5312 initialPosition[CASTLING][1] = initialRights[1] = BOARD_LEFT;
5313 initialPosition[CASTLING][2] = initialRights[2] =(BOARD_WIDTH-1)>>1;
5314 initialPosition[CASTLING][3] = initialRights[3] = BOARD_RGHT-1;
5315 initialPosition[CASTLING][4] = initialRights[4] = BOARD_LEFT;
5316 initialPosition[CASTLING][5] = initialRights[5] =(BOARD_WIDTH-1)>>1;
5319 pieces = FalconArray;
5320 gameInfo.boardWidth = 10;
5321 SetCharTable(pieceToChar, "PNBRQ.............FKpnbrq.............fk");
5323 case VariantXiangqi:
5324 pieces = XiangqiArray;
5325 gameInfo.boardWidth = 9;
5326 gameInfo.boardHeight = 10;
5327 nrCastlingRights = 0;
5328 SetCharTable(pieceToChar, "PH.R.AE..K.C.ph.r.ae..k.c.");
5331 pieces = ShogiArray;
5332 gameInfo.boardWidth = 9;
5333 gameInfo.boardHeight = 9;
5334 gameInfo.holdingsSize = 7;
5335 nrCastlingRights = 0;
5336 SetCharTable(pieceToChar, "PNBRLS...G.++++++Kpnbrls...g.++++++k");
5338 case VariantCourier:
5339 pieces = CourierArray;
5340 gameInfo.boardWidth = 12;
5341 nrCastlingRights = 0;
5342 SetCharTable(pieceToChar, "PNBR.FE..WMKpnbr.fe..wmk");
5344 case VariantKnightmate:
5345 pieces = KnightmateArray;
5346 SetCharTable(pieceToChar, "P.BRQ.....M.........K.p.brq.....m.........k.");
5349 pieces = fairyArray;
5350 SetCharTable(pieceToChar, "PNBRQFEACWMOHIJGDVLSUKpnbrqfeacwmohijgdvlsuk");
5353 pieces = GreatArray;
5354 gameInfo.boardWidth = 10;
5355 SetCharTable(pieceToChar, "PN....E...S..HWGMKpn....e...s..hwgmk");
5356 gameInfo.holdingsSize = 8;
5360 SetCharTable(pieceToChar, "PNBRQ..SE.......V.AKpnbrq..se.......v.ak");
5361 gameInfo.holdingsSize = 8;
5362 startedFromSetupPosition = TRUE;
5364 case VariantCrazyhouse:
5365 case VariantBughouse:
5367 SetCharTable(pieceToChar, "PNBRQ.......~~~~Kpnbrq.......~~~~k");
5368 gameInfo.holdingsSize = 5;
5370 case VariantWildCastle:
5372 /* !!?shuffle with kings guaranteed to be on d or e file */
5373 shuffleOpenings = 1;
5375 case VariantNoCastle:
5377 nrCastlingRights = 0;
5378 /* !!?unconstrained back-rank shuffle */
5379 shuffleOpenings = 1;
5384 if(appData.NrFiles >= 0) {
5385 if(gameInfo.boardWidth != appData.NrFiles) overrule++;
5386 gameInfo.boardWidth = appData.NrFiles;
5388 if(appData.NrRanks >= 0) {
5389 gameInfo.boardHeight = appData.NrRanks;
5391 if(appData.holdingsSize >= 0) {
5392 i = appData.holdingsSize;
5393 if(i > gameInfo.boardHeight) i = gameInfo.boardHeight;
5394 gameInfo.holdingsSize = i;
5396 if(gameInfo.holdingsSize) gameInfo.holdingsWidth = 2;
5397 if(BOARD_HEIGHT > BOARD_RANKS || BOARD_WIDTH > BOARD_FILES)
5398 DisplayFatalError(_("Recompile to support this BOARD_RANKS or BOARD_FILES!"), 0, 2);
5400 pawnRow = gameInfo.boardHeight - 7; /* seems to work in all common variants */
5401 if(pawnRow < 1) pawnRow = 1;
5402 if(gameInfo.variant == VariantMakruk) pawnRow = 2;
5404 /* User pieceToChar list overrules defaults */
5405 if(appData.pieceToCharTable != NULL)
5406 SetCharTable(pieceToChar, appData.pieceToCharTable);
5408 for( j=0; j<BOARD_WIDTH; j++ ) { ChessSquare s = EmptySquare;
5410 if(j==BOARD_LEFT-1 || j==BOARD_RGHT)
5411 s = (ChessSquare) 0; /* account holding counts in guard band */
5412 for( i=0; i<BOARD_HEIGHT; i++ )
5413 initialPosition[i][j] = s;
5415 if(j < BOARD_LEFT || j >= BOARD_RGHT || overrule) continue;
5416 initialPosition[0][j] = pieces[0][j-gameInfo.holdingsWidth];
5417 initialPosition[pawnRow][j] = WhitePawn;
5418 initialPosition[BOARD_HEIGHT-pawnRow-1][j] = BlackPawn;
5419 if(gameInfo.variant == VariantXiangqi) {
5421 initialPosition[pawnRow][j] =
5422 initialPosition[BOARD_HEIGHT-pawnRow-1][j] = EmptySquare;
5423 if(j==BOARD_LEFT+1 || j>=BOARD_RGHT-2) {
5424 initialPosition[2][j] = WhiteCannon;
5425 initialPosition[BOARD_HEIGHT-3][j] = BlackCannon;
5429 initialPosition[BOARD_HEIGHT-1][j] = pieces[1][j-gameInfo.holdingsWidth];
5431 if( (gameInfo.variant == VariantShogi) && !overrule ) {
5434 initialPosition[1][j] = WhiteBishop;
5435 initialPosition[BOARD_HEIGHT-2][j] = BlackRook;
5437 initialPosition[1][j] = WhiteRook;
5438 initialPosition[BOARD_HEIGHT-2][j] = BlackBishop;
5441 if( nrCastlingRights == -1) {
5442 /* [HGM] Build normal castling rights (must be done after board sizing!) */
5443 /* This sets default castling rights from none to normal corners */
5444 /* Variants with other castling rights must set them themselves above */
5445 nrCastlingRights = 6;
5447 initialPosition[CASTLING][0] = initialRights[0] = BOARD_RGHT-1;
5448 initialPosition[CASTLING][1] = initialRights[1] = BOARD_LEFT;
5449 initialPosition[CASTLING][2] = initialRights[2] = BOARD_WIDTH>>1;
5450 initialPosition[CASTLING][3] = initialRights[3] = BOARD_RGHT-1;
5451 initialPosition[CASTLING][4] = initialRights[4] = BOARD_LEFT;
5452 initialPosition[CASTLING][5] = initialRights[5] = BOARD_WIDTH>>1;
5455 if(gameInfo.variant == VariantSuper) Prelude(initialPosition);
5456 if(gameInfo.variant == VariantGreat) { // promotion commoners
5457 initialPosition[PieceToNumber(WhiteMan)][BOARD_WIDTH-1] = WhiteMan;
5458 initialPosition[PieceToNumber(WhiteMan)][BOARD_WIDTH-2] = 9;
5459 initialPosition[BOARD_HEIGHT-1-PieceToNumber(WhiteMan)][0] = BlackMan;
5460 initialPosition[BOARD_HEIGHT-1-PieceToNumber(WhiteMan)][1] = 9;
5462 if (appData.debugMode) {
5463 fprintf(debugFP, "shuffleOpenings = %d\n", shuffleOpenings);
5465 if(shuffleOpenings) {
5466 SetUpShuffle(initialPosition, appData.defaultFrcPosition);
5467 startedFromSetupPosition = TRUE;
5469 if(startedFromPositionFile) {
5470 /* [HGM] loadPos: use PositionFile for every new game */
5471 CopyBoard(initialPosition, filePosition);
5472 for(i=0; i<nrCastlingRights; i++)
5473 initialRights[i] = filePosition[CASTLING][i];
5474 startedFromSetupPosition = TRUE;
5477 CopyBoard(boards[0], initialPosition);
5479 if(oldx != gameInfo.boardWidth ||
5480 oldy != gameInfo.boardHeight ||
5481 oldh != gameInfo.holdingsWidth
5483 || oldv == VariantGothic || // For licensing popups
5484 gameInfo.variant == VariantGothic
5487 || oldv == VariantFalcon ||
5488 gameInfo.variant == VariantFalcon
5491 InitDrawingSizes(-2 ,0);
5494 DrawPosition(TRUE, boards[currentMove]);
5498 SendBoard(cps, moveNum)
5499 ChessProgramState *cps;
5502 char message[MSG_SIZ];
5504 if (cps->useSetboard) {
5505 char* fen = PositionToFEN(moveNum, cps->fenOverride);
5506 sprintf(message, "setboard %s\n", fen);
5507 SendToProgram(message, cps);
5513 /* Kludge to set black to move, avoiding the troublesome and now
5514 * deprecated "black" command.
5516 if (!WhiteOnMove(moveNum)) SendToProgram("a2a3\n", cps);
5518 SendToProgram("edit\n", cps);
5519 SendToProgram("#\n", cps);
5520 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
5521 bp = &boards[moveNum][i][BOARD_LEFT];
5522 for (j = BOARD_LEFT; j < BOARD_RGHT; j++, bp++) {
5523 if ((int) *bp < (int) BlackPawn) {
5524 sprintf(message, "%c%c%c\n", PieceToChar(*bp),
5526 if(message[0] == '+' || message[0] == '~') {
5527 sprintf(message, "%c%c%c+\n",
5528 PieceToChar((ChessSquare)(DEMOTED *bp)),
5531 if(cps->alphaRank) { /* [HGM] shogi: translate coords */
5532 message[1] = BOARD_RGHT - 1 - j + '1';
5533 message[2] = BOARD_HEIGHT - 1 - i + 'a';
5535 SendToProgram(message, cps);
5540 SendToProgram("c\n", cps);
5541 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
5542 bp = &boards[moveNum][i][BOARD_LEFT];
5543 for (j = BOARD_LEFT; j < BOARD_RGHT; j++, bp++) {
5544 if (((int) *bp != (int) EmptySquare)
5545 && ((int) *bp >= (int) BlackPawn)) {
5546 sprintf(message, "%c%c%c\n", ToUpper(PieceToChar(*bp)),
5548 if(message[0] == '+' || message[0] == '~') {
5549 sprintf(message, "%c%c%c+\n",
5550 PieceToChar((ChessSquare)(DEMOTED *bp)),
5553 if(cps->alphaRank) { /* [HGM] shogi: translate coords */
5554 message[1] = BOARD_RGHT - 1 - j + '1';
5555 message[2] = BOARD_HEIGHT - 1 - i + 'a';
5557 SendToProgram(message, cps);
5562 SendToProgram(".\n", cps);
5564 setboardSpoiledMachineBlack = 0; /* [HGM] assume WB 4.2.7 already solves this after sending setboard */
5567 static int autoQueen; // [HGM] oneclick
5570 HasPromotionChoice(int fromX, int fromY, int toX, int toY, char *promoChoice)
5572 /* [HGM] rewritten IsPromotion to only flag promotions that offer a choice */
5573 /* [HGM] add Shogi promotions */
5574 int promotionZoneSize=1, highestPromotingPiece = (int)WhitePawn;
5579 if(fromX < BOARD_LEFT || fromX >= BOARD_RGHT) return FALSE; // drop
5580 if(toX < BOARD_LEFT || toX >= BOARD_RGHT) return FALSE; // move into holdings
5582 if(gameMode == EditPosition || gameInfo.variant == VariantXiangqi || // no promotions
5583 !(fromX >=0 && fromY >= 0 && toX >= 0 && toY >= 0) ) // invalid move
5586 piece = boards[currentMove][fromY][fromX];
5587 if(gameInfo.variant == VariantShogi) {
5588 promotionZoneSize = 3;
5589 highestPromotingPiece = (int)WhiteFerz;
5590 } else if(gameInfo.variant == VariantMakruk) {
5591 promotionZoneSize = 3;
5594 // next weed out all moves that do not touch the promotion zone at all
5595 if((int)piece >= BlackPawn) {
5596 if(toY >= promotionZoneSize && fromY >= promotionZoneSize)
5598 highestPromotingPiece = WHITE_TO_BLACK highestPromotingPiece;
5600 if( toY < BOARD_HEIGHT - promotionZoneSize &&
5601 fromY < BOARD_HEIGHT - promotionZoneSize) return FALSE;
5604 if( (int)piece > highestPromotingPiece ) return FALSE; // non-promoting piece
5606 // weed out mandatory Shogi promotions
5607 if(gameInfo.variant == VariantShogi) {
5608 if(piece >= BlackPawn) {
5609 if(toY == 0 && piece == BlackPawn ||
5610 toY == 0 && piece == BlackQueen ||
5611 toY <= 1 && piece == BlackKnight) {
5616 if(toY == BOARD_HEIGHT-1 && piece == WhitePawn ||
5617 toY == BOARD_HEIGHT-1 && piece == WhiteQueen ||
5618 toY >= BOARD_HEIGHT-2 && piece == WhiteKnight) {
5625 // weed out obviously illegal Pawn moves
5626 if(appData.testLegality && (piece == WhitePawn || piece == BlackPawn) ) {
5627 if(toX > fromX+1 || toX < fromX-1) return FALSE; // wide
5628 if(piece == WhitePawn && toY != fromY+1) return FALSE; // deep
5629 if(piece == BlackPawn && toY != fromY-1) return FALSE; // deep
5630 if(fromX != toX && gameInfo.variant == VariantShogi) return FALSE;
5631 // note we are not allowed to test for valid (non-)capture, due to premove
5634 // we either have a choice what to promote to, or (in Shogi) whether to promote
5635 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk) {
5636 *promoChoice = PieceToChar(BlackFerz); // no choice
5639 if(autoQueen) { // predetermined
5640 if(gameInfo.variant == VariantSuicide || gameInfo.variant == VariantLosers)
5641 *promoChoice = PieceToChar(BlackKing); // in Suicide Q is the last thing we want
5642 else *promoChoice = PieceToChar(BlackQueen);
5646 // suppress promotion popup on illegal moves that are not premoves
5647 premove = gameMode == IcsPlayingWhite && !WhiteOnMove(currentMove) ||
5648 gameMode == IcsPlayingBlack && WhiteOnMove(currentMove);
5649 if(appData.testLegality && !premove) {
5650 moveType = LegalityTest(boards[currentMove], PosFlags(currentMove),
5651 fromY, fromX, toY, toX, NULLCHAR);
5652 if(moveType != WhitePromotionQueen && moveType != BlackPromotionQueen &&
5653 moveType != WhitePromotionKnight && moveType != BlackPromotionKnight)
5661 InPalace(row, column)
5663 { /* [HGM] for Xiangqi */
5664 if( (row < 3 || row > BOARD_HEIGHT-4) &&
5665 column < (BOARD_WIDTH + 4)/2 &&
5666 column > (BOARD_WIDTH - 5)/2 ) return TRUE;
5671 PieceForSquare (x, y)
5675 if (x < 0 || x >= BOARD_WIDTH || y < 0 || y >= BOARD_HEIGHT)
5678 return boards[currentMove][y][x];
5682 OKToStartUserMove(x, y)
5685 ChessSquare from_piece;
5688 if (matchMode) return FALSE;
5689 if (gameMode == EditPosition) return TRUE;
5691 if (x >= 0 && y >= 0)
5692 from_piece = boards[currentMove][y][x];
5694 from_piece = EmptySquare;
5696 if (from_piece == EmptySquare) return FALSE;
5698 white_piece = (int)from_piece >= (int)WhitePawn &&
5699 (int)from_piece < (int)BlackPawn; /* [HGM] can be > King! */
5702 case PlayFromGameFile:
5704 case TwoMachinesPlay:
5712 case MachinePlaysWhite:
5713 case IcsPlayingBlack:
5714 if (appData.zippyPlay) return FALSE;
5716 DisplayMoveError(_("You are playing Black"));
5721 case MachinePlaysBlack:
5722 case IcsPlayingWhite:
5723 if (appData.zippyPlay) return FALSE;
5725 DisplayMoveError(_("You are playing White"));
5731 if (!white_piece && WhiteOnMove(currentMove)) {
5732 DisplayMoveError(_("It is White's turn"));
5735 if (white_piece && !WhiteOnMove(currentMove)) {
5736 DisplayMoveError(_("It is Black's turn"));
5739 if (cmailMsgLoaded && (currentMove < cmailOldMove)) {
5740 /* Editing correspondence game history */
5741 /* Could disallow this or prompt for confirmation */
5746 case BeginningOfGame:
5747 if (appData.icsActive) return FALSE;
5748 if (!appData.noChessProgram) {
5750 DisplayMoveError(_("You are playing White"));
5757 if (!white_piece && WhiteOnMove(currentMove)) {
5758 DisplayMoveError(_("It is White's turn"));
5761 if (white_piece && !WhiteOnMove(currentMove)) {
5762 DisplayMoveError(_("It is Black's turn"));
5771 if (currentMove != forwardMostMove && gameMode != AnalyzeMode
5772 && gameMode != EditGame // [HGM] vari: treat as AnalyzeMode
5773 && gameMode != AnalyzeFile && gameMode != Training) {
5774 DisplayMoveError(_("Displayed position is not current"));
5781 OnlyMove(int *x, int *y, Boolean captures) {
5782 DisambiguateClosure cl;
5783 if (appData.zippyPlay) return FALSE;
5785 case MachinePlaysBlack:
5786 case IcsPlayingWhite:
5787 case BeginningOfGame:
5788 if(!WhiteOnMove(currentMove)) return FALSE;
5790 case MachinePlaysWhite:
5791 case IcsPlayingBlack:
5792 if(WhiteOnMove(currentMove)) return FALSE;
5797 cl.pieceIn = EmptySquare;
5802 cl.promoCharIn = NULLCHAR;
5803 Disambiguate(boards[currentMove], PosFlags(currentMove), &cl);
5804 if( cl.kind == NormalMove ||
5805 cl.kind == AmbiguousMove && captures && cl.captures == 1 ||
5806 cl.kind == WhitePromotionQueen || cl.kind == BlackPromotionQueen ||
5807 cl.kind == WhitePromotionKnight || cl.kind == BlackPromotionKnight ||
5808 cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) {
5815 if(cl.kind != ImpossibleMove) return FALSE;
5816 cl.pieceIn = EmptySquare;
5821 cl.promoCharIn = NULLCHAR;
5822 Disambiguate(boards[currentMove], PosFlags(currentMove), &cl);
5823 if( cl.kind == NormalMove ||
5824 cl.kind == AmbiguousMove && captures && cl.captures == 1 ||
5825 cl.kind == WhitePromotionQueen || cl.kind == BlackPromotionQueen ||
5826 cl.kind == WhitePromotionKnight || cl.kind == BlackPromotionKnight ||
5827 cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) {
5832 autoQueen = TRUE; // act as if autoQueen on when we click to-square
5838 FILE *lastLoadGameFP = NULL, *lastLoadPositionFP = NULL;
5839 int lastLoadGameNumber = 0, lastLoadPositionNumber = 0;
5840 int lastLoadGameUseList = FALSE;
5841 char lastLoadGameTitle[MSG_SIZ], lastLoadPositionTitle[MSG_SIZ];
5842 ChessMove lastLoadGameStart = (ChessMove) 0;
5845 UserMoveEvent(fromX, fromY, toX, toY, promoChar)
5846 int fromX, fromY, toX, toY;
5850 ChessSquare pdown, pup;
5852 /* Check if the user is playing in turn. This is complicated because we
5853 let the user "pick up" a piece before it is his turn. So the piece he
5854 tried to pick up may have been captured by the time he puts it down!
5855 Therefore we use the color the user is supposed to be playing in this
5856 test, not the color of the piece that is currently on the starting
5857 square---except in EditGame mode, where the user is playing both
5858 sides; fortunately there the capture race can't happen. (It can
5859 now happen in IcsExamining mode, but that's just too bad. The user
5860 will get a somewhat confusing message in that case.)
5864 case PlayFromGameFile:
5866 case TwoMachinesPlay:
5870 /* We switched into a game mode where moves are not accepted,
5871 perhaps while the mouse button was down. */
5874 case MachinePlaysWhite:
5875 /* User is moving for Black */
5876 if (WhiteOnMove(currentMove)) {
5877 DisplayMoveError(_("It is White's turn"));
5882 case MachinePlaysBlack:
5883 /* User is moving for White */
5884 if (!WhiteOnMove(currentMove)) {
5885 DisplayMoveError(_("It is Black's turn"));
5892 case BeginningOfGame:
5895 if ((int) boards[currentMove][fromY][fromX] >= (int) BlackPawn &&
5896 (int) boards[currentMove][fromY][fromX] < (int) EmptySquare) {
5897 /* User is moving for Black */
5898 if (WhiteOnMove(currentMove)) {
5899 DisplayMoveError(_("It is White's turn"));
5903 /* User is moving for White */
5904 if (!WhiteOnMove(currentMove)) {
5905 DisplayMoveError(_("It is Black's turn"));
5911 case IcsPlayingBlack:
5912 /* User is moving for Black */
5913 if (WhiteOnMove(currentMove)) {
5914 if (!appData.premove) {
5915 DisplayMoveError(_("It is White's turn"));
5916 } else if (toX >= 0 && toY >= 0) {
5919 premoveFromX = fromX;
5920 premoveFromY = fromY;
5921 premovePromoChar = promoChar;
5923 if (appData.debugMode)
5924 fprintf(debugFP, "Got premove: fromX %d,"
5925 "fromY %d, toX %d, toY %d\n",
5926 fromX, fromY, toX, toY);
5932 case IcsPlayingWhite:
5933 /* User is moving for White */
5934 if (!WhiteOnMove(currentMove)) {
5935 if (!appData.premove) {
5936 DisplayMoveError(_("It is Black's turn"));
5937 } else if (toX >= 0 && toY >= 0) {
5940 premoveFromX = fromX;
5941 premoveFromY = fromY;
5942 premovePromoChar = promoChar;
5944 if (appData.debugMode)
5945 fprintf(debugFP, "Got premove: fromX %d,"
5946 "fromY %d, toX %d, toY %d\n",
5947 fromX, fromY, toX, toY);
5957 /* EditPosition, empty square, or different color piece;
5958 click-click move is possible */
5959 if (toX == -2 || toY == -2) {
5960 boards[0][fromY][fromX] = EmptySquare;
5961 DrawPosition(FALSE, boards[currentMove]);
5963 } else if (toX >= 0 && toY >= 0) {
5964 boards[0][toY][toX] = boards[0][fromY][fromX];
5965 if(fromX == BOARD_LEFT-2) { // handle 'moves' out of holdings
5966 if(boards[0][fromY][0] != EmptySquare) {
5967 if(boards[0][fromY][1]) boards[0][fromY][1]--;
5968 if(boards[0][fromY][1] == 0) boards[0][fromY][0] = EmptySquare;
5971 if(fromX == BOARD_RGHT+1) {
5972 if(boards[0][fromY][BOARD_WIDTH-1] != EmptySquare) {
5973 if(boards[0][fromY][BOARD_WIDTH-2]) boards[0][fromY][BOARD_WIDTH-2]--;
5974 if(boards[0][fromY][BOARD_WIDTH-2] == 0) boards[0][fromY][BOARD_WIDTH-1] = EmptySquare;
5977 boards[0][fromY][fromX] = EmptySquare;
5978 DrawPosition(FALSE, boards[currentMove]);
5984 if(toX < 0 || toY < 0) return;
5985 pdown = boards[currentMove][fromY][fromX];
5986 pup = boards[currentMove][toY][toX];
5988 /* [HGM] If move started in holdings, it means a drop. Convert to standard form */
5989 if( fromX == BOARD_LEFT-2 || fromX == BOARD_RGHT+1) {
5990 if( pup != EmptySquare ) return;
5991 moveType = WhiteOnMove(currentMove) ? WhiteDrop : BlackDrop;
5992 if(appData.debugMode) fprintf(debugFP, "Drop move %d, curr=%d, x=%d,y=%d, p=%d\n",
5993 moveType, currentMove, fromX, fromY, boards[currentMove][fromY][fromX]);
5994 // holdings might not be sent yet in ICS play; we have to figure out which piece belongs here
5995 if(fromX == 0) fromY = BOARD_HEIGHT-1 - fromY; // black holdings upside-down
5996 fromX = fromX ? WhitePawn : BlackPawn; // first piece type in selected holdings
5997 while(PieceToChar(fromX) == '.' || PieceToNumber(fromX) != fromY && fromX != (int) EmptySquare) fromX++;
6001 /* [HGM] always test for legality, to get promotion info */
6002 moveType = LegalityTest(boards[currentMove], PosFlags(currentMove),
6003 fromY, fromX, toY, toX, promoChar);
6004 /* [HGM] but possibly ignore an IllegalMove result */
6005 if (appData.testLegality) {
6006 if (moveType == IllegalMove || moveType == ImpossibleMove) {
6007 DisplayMoveError(_("Illegal move"));
6012 FinishMove(moveType, fromX, fromY, toX, toY, promoChar);
6015 /* Common tail of UserMoveEvent and DropMenuEvent */
6017 FinishMove(moveType, fromX, fromY, toX, toY, promoChar)
6019 int fromX, fromY, toX, toY;
6020 /*char*/int promoChar;
6024 if((gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat) && promoChar != NULLCHAR) {
6025 // [HGM] superchess: suppress promotions to non-available piece
6026 int k = PieceToNumber(CharToPiece(ToUpper(promoChar)));
6027 if(WhiteOnMove(currentMove)) {
6028 if(!boards[currentMove][k][BOARD_WIDTH-2]) return 0;
6030 if(!boards[currentMove][BOARD_HEIGHT-1-k][1]) return 0;
6034 /* [HGM] <popupFix> kludge to avoid having to know the exact promotion
6035 move type in caller when we know the move is a legal promotion */
6036 if(moveType == NormalMove && promoChar)
6037 moveType = PromoCharToMoveType(WhiteOnMove(currentMove), promoChar);
6039 /* [HGM] <popupFix> The following if has been moved here from
6040 UserMoveEvent(). Because it seemed to belong here (why not allow
6041 piece drops in training games?), and because it can only be
6042 performed after it is known to what we promote. */
6043 if (gameMode == Training) {
6044 /* compare the move played on the board to the next move in the
6045 * game. If they match, display the move and the opponent's response.
6046 * If they don't match, display an error message.
6050 CopyBoard(testBoard, boards[currentMove]);
6051 ApplyMove(fromX, fromY, toX, toY, promoChar, testBoard);
6053 if (CompareBoards(testBoard, boards[currentMove+1])) {
6054 ForwardInner(currentMove+1);
6056 /* Autoplay the opponent's response.
6057 * if appData.animate was TRUE when Training mode was entered,
6058 * the response will be animated.
6060 saveAnimate = appData.animate;
6061 appData.animate = animateTraining;
6062 ForwardInner(currentMove+1);
6063 appData.animate = saveAnimate;
6065 /* check for the end of the game */
6066 if (currentMove >= forwardMostMove) {
6067 gameMode = PlayFromGameFile;
6069 SetTrainingModeOff();
6070 DisplayInformation(_("End of game"));
6073 DisplayError(_("Incorrect move"), 0);
6078 /* Ok, now we know that the move is good, so we can kill
6079 the previous line in Analysis Mode */
6080 if ((gameMode == AnalyzeMode || gameMode == EditGame)
6081 && currentMove < forwardMostMove) {
6082 PushTail(currentMove, forwardMostMove); // [HGM] vari: save tail of game
6085 /* If we need the chess program but it's dead, restart it */
6086 ResurrectChessProgram();
6088 /* A user move restarts a paused game*/
6092 thinkOutput[0] = NULLCHAR;
6094 MakeMove(fromX, fromY, toX, toY, promoChar); /*updates forwardMostMove*/
6096 if(Adjudicate(NULL)) return 1; // [HGM] adjudicate: take care of automtic game end
6098 if (gameMode == BeginningOfGame) {
6099 if (appData.noChessProgram) {
6100 gameMode = EditGame;
6104 gameMode = MachinePlaysBlack;
6107 sprintf(buf, "%s vs. %s", gameInfo.white, gameInfo.black);
6109 if (first.sendName) {
6110 sprintf(buf, "name %s\n", gameInfo.white);
6111 SendToProgram(buf, &first);
6118 /* Relay move to ICS or chess engine */
6119 if (appData.icsActive) {
6120 if (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
6121 gameMode == IcsExamining) {
6122 if(userOfferedDraw && (signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
6123 SendToICS(ics_prefix); // [HGM] drawclaim: send caim and move on one line for FICS
6125 SendMoveToICS(moveType, fromX, fromY, toX, toY);
6127 // also send plain move, in case ICS does not understand atomic claims
6128 SendMoveToICS(moveType, fromX, fromY, toX, toY);
6132 if (first.sendTime && (gameMode == BeginningOfGame ||
6133 gameMode == MachinePlaysWhite ||
6134 gameMode == MachinePlaysBlack)) {
6135 SendTimeRemaining(&first, gameMode != MachinePlaysBlack);
6137 if (gameMode != EditGame && gameMode != PlayFromGameFile) {
6138 // [HGM] book: if program might be playing, let it use book
6139 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, FALSE);
6140 first.maybeThinking = TRUE;
6141 } else SendMoveToProgram(forwardMostMove-1, &first);
6142 if (currentMove == cmailOldMove + 1) {
6143 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
6147 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6151 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
6157 if (WhiteOnMove(currentMove)) {
6158 GameEnds(BlackWins, "Black mates", GE_PLAYER);
6160 GameEnds(WhiteWins, "White mates", GE_PLAYER);
6164 GameEnds(GameIsDrawn, "Stalemate", GE_PLAYER);
6169 case MachinePlaysBlack:
6170 case MachinePlaysWhite:
6171 /* disable certain menu options while machine is thinking */
6172 SetMachineThinkingEnables();
6179 userOfferedDraw = FALSE; // [HGM] drawclaim: after move made, and tested for claimable draw
6181 if(bookHit) { // [HGM] book: simulate book reply
6182 static char bookMove[MSG_SIZ]; // a bit generous?
6184 programStats.nodes = programStats.depth = programStats.time =
6185 programStats.score = programStats.got_only_move = 0;
6186 sprintf(programStats.movelist, "%s (xbook)", bookHit);
6188 strcpy(bookMove, "move ");
6189 strcat(bookMove, bookHit);
6190 HandleMachineMove(bookMove, &first);
6196 Mark(board, flags, kind, rf, ff, rt, ft, closure)
6203 typedef char Markers[BOARD_RANKS][BOARD_FILES];
6204 Markers *m = (Markers *) closure;
6205 if(rf == fromY && ff == fromX)
6206 (*m)[rt][ft] = 1 + (board[rt][ft] != EmptySquare
6207 || kind == WhiteCapturesEnPassant
6208 || kind == BlackCapturesEnPassant);
6209 else if(flags & F_MANDATORY_CAPTURE && board[rt][ft] != EmptySquare) (*m)[rt][ft] = 3;
6213 MarkTargetSquares(int clear)
6216 if(!appData.markers || !appData.highlightDragging ||
6217 !appData.testLegality || gameMode == EditPosition) return;
6219 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) marker[y][x] = 0;
6222 GenLegal(boards[currentMove], PosFlags(currentMove), Mark, (void*) marker);
6223 if(PosFlags(0) & F_MANDATORY_CAPTURE) {
6224 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) if(marker[y][x]>1) capt++;
6226 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) if(marker[y][x] == 1) marker[y][x] = 0;
6229 DrawPosition(TRUE, NULL);
6232 void LeftClick(ClickType clickType, int xPix, int yPix)
6235 Boolean saveAnimate;
6236 static int second = 0, promotionChoice = 0, dragging = 0;
6237 char promoChoice = NULLCHAR;
6239 if(appData.seekGraph && appData.icsActive && loggedOn &&
6240 (gameMode == BeginningOfGame || gameMode == IcsIdle)) {
6241 SeekGraphClick(clickType, xPix, yPix, 0);
6245 if (clickType == Press) ErrorPopDown();
6246 MarkTargetSquares(1);
6248 x = EventToSquare(xPix, BOARD_WIDTH);
6249 y = EventToSquare(yPix, BOARD_HEIGHT);
6250 if (!flipView && y >= 0) {
6251 y = BOARD_HEIGHT - 1 - y;
6253 if (flipView && x >= 0) {
6254 x = BOARD_WIDTH - 1 - x;
6257 if(promotionChoice) { // we are waiting for a click to indicate promotion piece
6258 if(clickType == Release) return; // ignore upclick of click-click destination
6259 promotionChoice = FALSE; // only one chance: if click not OK it is interpreted as cancel
6260 if(appData.debugMode) fprintf(debugFP, "promotion click, x=%d, y=%d\n", x, y);
6261 if(gameInfo.holdingsWidth &&
6262 (WhiteOnMove(currentMove)
6263 ? x == BOARD_WIDTH-1 && y < gameInfo.holdingsSize && y > 0
6264 : x == 0 && y >= BOARD_HEIGHT - gameInfo.holdingsSize && y < BOARD_HEIGHT-1) ) {
6265 // click in right holdings, for determining promotion piece
6266 ChessSquare p = boards[currentMove][y][x];
6267 if(appData.debugMode) fprintf(debugFP, "square contains %d\n", (int)p);
6268 if(p != EmptySquare) {
6269 FinishMove(NormalMove, fromX, fromY, toX, toY, ToLower(PieceToChar(p)));
6274 DrawPosition(FALSE, boards[currentMove]);
6278 /* [HGM] holdings: next 5 lines: ignore all clicks between board and holdings */
6279 if(clickType == Press
6280 && ( x == BOARD_LEFT-1 || x == BOARD_RGHT
6281 || x == BOARD_LEFT-2 && y < BOARD_HEIGHT-gameInfo.holdingsSize
6282 || x == BOARD_RGHT+1 && y >= gameInfo.holdingsSize) )
6285 autoQueen = appData.alwaysPromoteToQueen;
6288 if(!appData.oneClick || !OnlyMove(&x, &y, FALSE)) {
6289 if (clickType == Press) {
6291 if (OKToStartUserMove(x, y)) {
6295 MarkTargetSquares(0);
6296 DragPieceBegin(xPix, yPix); dragging = 1;
6297 if (appData.highlightDragging) {
6298 SetHighlights(x, y, -1, -1);
6301 } else if(dragging) { // [HGM] from-square must have been reset due to game end since last press
6302 DragPieceEnd(xPix, yPix); dragging = 0;
6303 DrawPosition(FALSE, NULL);
6310 if (clickType == Press && gameMode != EditPosition) {
6315 // ignore off-board to clicks
6316 if(y < 0 || x < 0) return;
6318 /* Check if clicking again on the same color piece */
6319 fromP = boards[currentMove][fromY][fromX];
6320 toP = boards[currentMove][y][x];
6321 frc = gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom;
6322 if ((WhitePawn <= fromP && fromP <= WhiteKing &&
6323 WhitePawn <= toP && toP <= WhiteKing &&
6324 !(fromP == WhiteKing && toP == WhiteRook && frc) &&
6325 !(fromP == WhiteRook && toP == WhiteKing && frc)) ||
6326 (BlackPawn <= fromP && fromP <= BlackKing &&
6327 BlackPawn <= toP && toP <= BlackKing &&
6328 !(fromP == BlackRook && toP == BlackKing && frc) && // allow also RxK as FRC castling
6329 !(fromP == BlackKing && toP == BlackRook && frc))) {
6330 /* Clicked again on same color piece -- changed his mind */
6331 second = (x == fromX && y == fromY);
6332 if(!second || appData.oneClick && !OnlyMove(&x, &y, TRUE)) {
6333 if (appData.highlightDragging) {
6334 SetHighlights(x, y, -1, -1);
6338 if (OKToStartUserMove(x, y)) {
6340 fromY = y; dragging = 1;
6341 MarkTargetSquares(0);
6342 DragPieceBegin(xPix, yPix);
6347 // ignore clicks on holdings
6348 if(x < BOARD_LEFT || x >= BOARD_RGHT) return;
6351 if (clickType == Release && x == fromX && y == fromY) {
6352 DragPieceEnd(xPix, yPix); dragging = 0;
6353 if (appData.animateDragging) {
6354 /* Undo animation damage if any */
6355 DrawPosition(FALSE, NULL);
6358 /* Second up/down in same square; just abort move */
6363 ClearPremoveHighlights();
6365 /* First upclick in same square; start click-click mode */
6366 SetHighlights(x, y, -1, -1);
6371 /* we now have a different from- and (possibly off-board) to-square */
6372 /* Completed move */
6375 saveAnimate = appData.animate;
6376 if (clickType == Press) {
6377 /* Finish clickclick move */
6378 if (appData.animate || appData.highlightLastMove) {
6379 SetHighlights(fromX, fromY, toX, toY);
6384 /* Finish drag move */
6385 if (appData.highlightLastMove) {
6386 SetHighlights(fromX, fromY, toX, toY);
6390 DragPieceEnd(xPix, yPix); dragging = 0;
6391 /* Don't animate move and drag both */
6392 appData.animate = FALSE;
6395 // moves into holding are invalid for now (except in EditPosition, adapting to-square)
6396 if(x >= 0 && x < BOARD_LEFT || x >= BOARD_RGHT) {
6397 ChessSquare piece = boards[currentMove][fromY][fromX];
6398 if(gameMode == EditPosition && piece != EmptySquare &&
6399 fromX >= BOARD_LEFT && fromX < BOARD_RGHT) {
6402 if(x == BOARD_LEFT-2 && piece >= BlackPawn) {
6403 n = PieceToNumber(piece - (int)BlackPawn);
6404 if(n >= gameInfo.holdingsSize) { n = 0; piece = BlackPawn; }
6405 boards[currentMove][BOARD_HEIGHT-1 - n][0] = piece;
6406 boards[currentMove][BOARD_HEIGHT-1 - n][1]++;
6408 if(x == BOARD_RGHT+1 && piece < BlackPawn) {
6409 n = PieceToNumber(piece);
6410 if(n >= gameInfo.holdingsSize) { n = 0; piece = WhitePawn; }
6411 boards[currentMove][n][BOARD_WIDTH-1] = piece;
6412 boards[currentMove][n][BOARD_WIDTH-2]++;
6414 boards[currentMove][fromY][fromX] = EmptySquare;
6418 DrawPosition(TRUE, boards[currentMove]);
6422 // off-board moves should not be highlighted
6423 if(x < 0 || x < 0) ClearHighlights();
6425 if (HasPromotionChoice(fromX, fromY, toX, toY, &promoChoice)) {
6426 SetHighlights(fromX, fromY, toX, toY);
6427 if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat) {
6428 // [HGM] super: promotion to captured piece selected from holdings
6429 ChessSquare p = boards[currentMove][fromY][fromX], q = boards[currentMove][toY][toX];
6430 promotionChoice = TRUE;
6431 // kludge follows to temporarily execute move on display, without promoting yet
6432 boards[currentMove][fromY][fromX] = EmptySquare; // move Pawn to 8th rank
6433 boards[currentMove][toY][toX] = p;
6434 DrawPosition(FALSE, boards[currentMove]);
6435 boards[currentMove][fromY][fromX] = p; // take back, but display stays
6436 boards[currentMove][toY][toX] = q;
6437 DisplayMessage("Click in holdings to choose piece", "");
6442 UserMoveEvent(fromX, fromY, toX, toY, promoChoice);
6443 if (!appData.highlightLastMove || gotPremove) ClearHighlights();
6444 if (gotPremove) SetPremoveHighlights(fromX, fromY, toX, toY);
6447 appData.animate = saveAnimate;
6448 if (appData.animate || appData.animateDragging) {
6449 /* Undo animation damage if needed */
6450 DrawPosition(FALSE, NULL);
6454 int RightClick(ClickType action, int x, int y, int *fromX, int *fromY)
6455 { // front-end-free part taken out of PieceMenuPopup
6456 int whichMenu; int xSqr, ySqr;
6458 if(seekGraphUp) { // [HGM] seekgraph
6459 if(action == Press) SeekGraphClick(Press, x, y, 2); // 2 indicates right-click: no pop-down on miss
6460 if(action == Release) SeekGraphClick(Release, x, y, 2); // and no challenge on hit
6464 if((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)
6465 && !appData.zippyPlay && appData.bgObserve) { // [HGM] bughouse: show background game
6466 if(!partnerBoardValid) return -2; // suppress display of uninitialized boards
6467 if( appData.dualBoard) return -2; // [HGM] dual: is already displayed
6468 if(action == Press) {
6469 originalFlip = flipView;
6470 flipView = !flipView; // temporarily flip board to see game from partners perspective
6471 DrawPosition(TRUE, partnerBoard);
6472 DisplayMessage(partnerStatus, "");
6474 } else if(action == Release) {
6475 flipView = originalFlip;
6476 DrawPosition(TRUE, boards[currentMove]);
6482 xSqr = EventToSquare(x, BOARD_WIDTH);
6483 ySqr = EventToSquare(y, BOARD_HEIGHT);
6484 if (action == Release) UnLoadPV(); // [HGM] pv
6485 if (action != Press) return -2; // return code to be ignored
6488 if(xSqr < BOARD_LEFT || xSqr >= BOARD_RGHT) return -1;
\r
6490 if (xSqr == BOARD_LEFT-1 || xSqr == BOARD_RGHT) return -1;
\r
6491 if (xSqr < 0 || ySqr < 0) return -1;
\r
6492 whichMenu = 0; // edit-position menu
6495 if(!appData.icsEngineAnalyze) return -1;
6496 case IcsPlayingWhite:
6497 case IcsPlayingBlack:
6498 if(!appData.zippyPlay) goto noZip;
6501 case MachinePlaysWhite:
6502 case MachinePlaysBlack:
6503 case TwoMachinesPlay: // [HGM] pv: use for showing PV
6504 if (!appData.dropMenu) {
6506 return 2; // flag front-end to grab mouse events
6508 if(gameMode == TwoMachinesPlay || gameMode == AnalyzeMode ||
6509 gameMode == AnalyzeFile || gameMode == IcsObserving) return -1;
6512 if (xSqr < 0 || ySqr < 0) return -1;
6513 if (!appData.dropMenu || appData.testLegality &&
6514 gameInfo.variant != VariantBughouse &&
6515 gameInfo.variant != VariantCrazyhouse) return -1;
6516 whichMenu = 1; // drop menu
6522 if (((*fromX = xSqr) < 0) ||
6523 ((*fromY = ySqr) < 0)) {
6524 *fromX = *fromY = -1;
6528 *fromX = BOARD_WIDTH - 1 - *fromX;
6530 *fromY = BOARD_HEIGHT - 1 - *fromY;
6535 void SendProgramStatsToFrontend( ChessProgramState * cps, ChessProgramStats * cpstats )
6537 // char * hint = lastHint;
6538 FrontEndProgramStats stats;
6540 stats.which = cps == &first ? 0 : 1;
6541 stats.depth = cpstats->depth;
6542 stats.nodes = cpstats->nodes;
6543 stats.score = cpstats->score;
6544 stats.time = cpstats->time;
6545 stats.pv = cpstats->movelist;
6546 stats.hint = lastHint;
6547 stats.an_move_index = 0;
6548 stats.an_move_count = 0;
6550 if( gameMode == AnalyzeMode || gameMode == AnalyzeFile ) {
6551 stats.hint = cpstats->move_name;
6552 stats.an_move_index = cpstats->nr_moves - cpstats->moves_left;
6553 stats.an_move_count = cpstats->nr_moves;
6556 if(stats.pv && stats.pv[0]) strcpy(lastPV[stats.which], stats.pv); // [HGM] pv: remember last PV of each
6558 SetProgramStats( &stats );
6562 Count(Board board, int pCnt[], int *nW, int *nB, int *wStale, int *bStale, int *bishopColor)
6563 { // count all piece types
6565 *nB = *nW = *wStale = *bStale = *bishopColor = 0;
6566 for(p=WhitePawn; p<=EmptySquare; p++) pCnt[p] = 0;
6567 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
6570 if(p == WhitePawn && r == BOARD_HEIGHT-1) (*wStale)++; else
6571 if(p == BlackPawn && r == 0) (*bStale)++; // count last-Rank Pawns (XQ) separately
6572 if(p <= WhiteKing) (*nW)++; else if(p <= BlackKing) (*nB)++;
6573 if(p == WhiteBishop || p == WhiteFerz || p == WhiteAlfil ||
6574 p == BlackBishop || p == BlackFerz || p == BlackAlfil )
6575 *bishopColor |= 1 << ((f^r)&1); // track square color of color-bound pieces
6580 SufficientDefence(int pCnt[], int side, int nMine, int nHis)
6582 int myPawns = pCnt[WhitePawn+side]; // my total Pawn count;
6583 int majorDefense = pCnt[BlackRook-side] + pCnt[BlackCannon-side] + pCnt[BlackKnight-side];
6585 nMine -= pCnt[WhiteFerz+side] + pCnt[WhiteAlfil+side]; // discount defenders
6586 if(nMine - myPawns > 2) return FALSE; // no trivial draws with more than 1 major
6587 if(myPawns == 2 && nMine == 3) // KPP
6588 return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] >= 3;
6589 if(myPawns == 1 && nMine == 2) // KP
6590 return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] + pCnt[BlackPawn-side] >= 1;
6591 if(myPawns == 1 && nMine == 3 && pCnt[WhiteKnight+side]) // KHP
6592 return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side]*2 >= 5;
6593 if(myPawns) return FALSE;
6594 if(pCnt[WhiteRook+side])
6595 return pCnt[BlackRook-side] ||
6596 pCnt[BlackCannon-side] && (pCnt[BlackFerz-side] >= 2 || pCnt[BlackAlfil-side] >= 2) ||
6597 pCnt[BlackKnight-side] && pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] > 2 ||
6598 pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] >= 4;
6599 if(pCnt[WhiteCannon+side]) {
6600 if(pCnt[WhiteFerz+side] + myPawns == 0) return TRUE; // Cannon needs platform
6601 return majorDefense || pCnt[BlackAlfil-side] >= 2;
6603 if(pCnt[WhiteKnight+side])
6604 return majorDefense || pCnt[BlackFerz-side] >= 2 || pCnt[BlackAlfil-side] + pCnt[BlackPawn-side] >= 1;
6609 MatingPotential(int pCnt[], int side, int nMine, int nHis, int stale, int bisColor)
6611 VariantClass v = gameInfo.variant;
6613 if(v == VariantShogi || v == VariantCrazyhouse || v == VariantBughouse) return TRUE; // drop games always winnable
6614 if(v == VariantShatranj) return TRUE; // always winnable through baring
6615 if(v == VariantLosers || v == VariantSuicide || v == VariantGiveaway) return TRUE;
6616 if(v == Variant3Check || v == VariantAtomic) return nMine > 1; // can win through checking / exploding King
6618 if(v == VariantXiangqi) {
6619 int majors = 5*pCnt[BlackKnight-side] + 7*pCnt[BlackCannon-side] + 7*pCnt[BlackRook-side];
6621 nMine -= pCnt[WhiteFerz+side] + pCnt[WhiteAlfil+side] + stale; // discount defensive pieces and back-rank Pawns
6622 if(nMine + stale == 1) return (pCnt[BlackFerz-side] > 1 && pCnt[BlackKnight-side] > 0); // bare K can stalemate KHAA (!)
6623 if(nMine > 2) return TRUE; // if we don't have P, H or R, we must have CC
6624 if(nMine == 2 && pCnt[WhiteCannon+side] == 0) return TRUE; // We have at least one P, H or R
6625 // if we get here, we must have KC... or KP..., possibly with additional A, E or last-rank P
6626 if(stale) // we have at least one last-rank P plus perhaps C
6627 return majors // KPKX
6628 || pCnt[BlackFerz-side] && pCnt[BlackFerz-side] + pCnt[WhiteCannon+side] + stale > 2; // KPKAA, KPPKA and KCPKA
6630 return pCnt[WhiteFerz+side] // KCAK
6631 || pCnt[WhiteAlfil+side] && pCnt[BlackRook-side] + pCnt[BlackCannon-side] + pCnt[BlackFerz-side] // KCEKA, KCEKX (X!=H)
6632 || majors + (12*pCnt[BlackFerz-side] | 6*pCnt[BlackAlfil-side]) > 16; // KCKAA, KCKAX, KCKEEX, KCKEXX (XX!=HH), KCKXXX
6633 // TO DO: cases wih an unpromoted f-Pawn acting as platform for an opponent Cannon
6635 } else if(pCnt[WhiteKing] == 1 && pCnt[BlackKing] == 1) { // other variants with orthodox Kings
6636 int nBishops = pCnt[WhiteBishop+side] + pCnt[WhiteFerz+side];
6638 if(nMine == 1) return FALSE; // bare King
6639 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
6640 nMine += (nBishops > 0) - nBishops; // By now all Bishops (and Ferz) on like-colored squares, so count as one
6641 if(nMine > 2 && nMine != pCnt[WhiteAlfil+side] + 1) return TRUE; // At least two pieces, not all Alfils
6642 // by now we have King + 1 piece (or multiple Bishops on the same color)
6643 if(pCnt[WhiteKnight+side])
6644 return (pCnt[BlackKnight-side] + pCnt[BlackBishop-side] + pCnt[BlackMan-side] +
6645 pCnt[BlackWazir-side] + pCnt[BlackSilver-side] + bisColor // KNKN, KNKB, KNKF, KNKE, KNKW, KNKM, KNKS
6646 || nHis > 3); // be sure to cover suffocation mates in corner (e.g. KNKQCA)
6648 return (pCnt[BlackKnight-side]); // KBKN, KFKN
6649 if(pCnt[WhiteAlfil+side])
6650 return (nHis > 2); // Alfils can in general not reach a corner square, but there might be edge (suffocation) mates
6651 if(pCnt[WhiteWazir+side])
6652 return (pCnt[BlackKnight-side] + pCnt[BlackWazir-side] + pCnt[BlackAlfil-side]); // KWKN, KWKW, KWKE
6659 Adjudicate(ChessProgramState *cps)
6660 { // [HGM] some adjudications useful with buggy engines
6661 // [HGM] adjudicate: made into separate routine, which now can be called after every move
6662 // In any case it determnes if the game is a claimable draw (filling in EP_STATUS).
6663 // Actually ending the game is now based on the additional internal condition canAdjudicate.
6664 // Only when the game is ended, and the opponent is a computer, this opponent gets the move relayed.
6665 int k, count = 0; static int bare = 1;
6666 ChessProgramState *engineOpponent = (gameMode == TwoMachinesPlay ? cps->other : (cps ? NULL : &first));
6667 Boolean canAdjudicate = !appData.icsActive;
6669 // most tests only when we understand the game, i.e. legality-checking on, and (for the time being) no piece drops
6670 if(gameInfo.holdingsSize == 0 || gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat) {
6671 if( appData.testLegality )
6672 { /* [HGM] Some more adjudications for obstinate engines */
6673 int nrW, nrB, bishopColor, staleW, staleB, nr[EmptySquare+1], i;
6674 static int moveCount = 6;
6676 char *reason = NULL;
6678 /* Count what is on board. */
6679 Count(boards[forwardMostMove], nr, &nrW, &nrB, &staleW, &staleB, &bishopColor);
6681 /* Some material-based adjudications that have to be made before stalemate test */
6682 if(gameInfo.variant == VariantAtomic && nr[WhiteKing] + nr[BlackKing] < 2) {
6683 // [HGM] atomic: stm must have lost his King on previous move, as destroying own K is illegal
6684 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE; // make claimable as if stm is checkmated
6685 if(canAdjudicate && appData.checkMates) {
6687 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets move
6688 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6689 GameEnds( WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins,
6690 "Xboard adjudication: King destroyed", GE_XBOARD );
6695 /* Bare King in Shatranj (loses) or Losers (wins) */
6696 if( nrW == 1 || nrB == 1) {
6697 if( gameInfo.variant == VariantLosers) { // [HGM] losers: bare King wins (stm must have it first)
6698 boards[forwardMostMove][EP_STATUS] = EP_WINS; // mark as win, so it becomes claimable
6699 if(canAdjudicate && appData.checkMates) {
6701 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets to see move
6702 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6703 GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
6704 "Xboard adjudication: Bare king", GE_XBOARD );
6708 if( gameInfo.variant == VariantShatranj && --bare < 0)
6710 boards[forwardMostMove][EP_STATUS] = EP_WINS; // make claimable as win for stm
6711 if(canAdjudicate && appData.checkMates) {
6712 /* but only adjudicate if adjudication enabled */
6714 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets move
6715 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6716 GameEnds( nrW > 1 ? WhiteWins : nrB > 1 ? BlackWins : GameIsDrawn,
6717 "Xboard adjudication: Bare king", GE_XBOARD );
6724 // don't wait for engine to announce game end if we can judge ourselves
6725 switch (MateTest(boards[forwardMostMove], PosFlags(forwardMostMove)) ) {
6727 if(gameInfo.variant == Variant3Check) { // [HGM] 3check: when in check, test if 3rd time
6728 int i, checkCnt = 0; // (should really be done by making nr of checks part of game state)
6729 for(i=forwardMostMove-2; i>=backwardMostMove; i-=2) {
6730 if(MateTest(boards[i], PosFlags(i)) == MT_CHECK)
6733 reason = "Xboard adjudication: 3rd check";
6734 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE;
6744 reason = "Xboard adjudication: Stalemate";
6745 if((signed char)boards[forwardMostMove][EP_STATUS] != EP_CHECKMATE) { // [HGM] don't touch win through baring or K-capt
6746 boards[forwardMostMove][EP_STATUS] = EP_STALEMATE; // default result for stalemate is draw
6747 if(gameInfo.variant == VariantLosers || gameInfo.variant == VariantGiveaway) // [HGM] losers:
6748 boards[forwardMostMove][EP_STATUS] = EP_WINS; // in these variants stalemated is always a win
6749 else if(gameInfo.variant == VariantSuicide) // in suicide it depends
6750 boards[forwardMostMove][EP_STATUS] = nrW == nrB ? EP_STALEMATE :
6751 ((nrW < nrB) != WhiteOnMove(forwardMostMove) ?
6752 EP_CHECKMATE : EP_WINS);
6753 else if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantXiangqi)
6754 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE; // and in these variants being stalemated loses
6758 reason = "Xboard adjudication: Checkmate";
6759 boards[forwardMostMove][EP_STATUS] = (gameInfo.variant == VariantLosers ? EP_WINS : EP_CHECKMATE);
6763 switch(i = (signed char)boards[forwardMostMove][EP_STATUS]) {
6765 result = GameIsDrawn; break;
6767 result = WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins; break;
6769 result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins; break;
6771 result = (ChessMove) 0;
6773 if(canAdjudicate && appData.checkMates && result) { // [HGM] mates: adjudicate finished games if requested
6775 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
6776 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6777 GameEnds( result, reason, GE_XBOARD );
6781 /* Next absolutely insufficient mating material. */
6782 if(!MatingPotential(nr, WhitePawn, nrW, nrB, staleW, bishopColor) &&
6783 !MatingPotential(nr, BlackPawn, nrB, nrW, staleB, bishopColor))
6784 { /* includes KBK, KNK, KK of KBKB with like Bishops */
6786 /* always flag draws, for judging claims */
6787 boards[forwardMostMove][EP_STATUS] = EP_INSUF_DRAW;
6789 if(canAdjudicate && appData.materialDraws) {
6790 /* but only adjudicate them if adjudication enabled */
6791 if(engineOpponent) {
6792 SendToProgram("force\n", engineOpponent); // suppress reply
6793 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see last move */
6795 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6796 GameEnds( GameIsDrawn, "Xboard adjudication: Insufficient mating material", GE_XBOARD );
6801 /* Then some trivial draws (only adjudicate, cannot be claimed) */
6802 if(gameInfo.variant == VariantXiangqi ?
6803 SufficientDefence(nr, WhitePawn, nrW, nrB) && SufficientDefence(nr, BlackPawn, nrB, nrW)
6805 ( nr[WhiteRook] == 1 && nr[BlackRook] == 1 /* KRKR */
6806 || nr[WhiteQueen] && nr[BlackQueen]==1 /* KQKQ */
6807 || nr[WhiteKnight]==2 || nr[BlackKnight]==2 /* KNNK */
6808 || nr[WhiteKnight]+nr[WhiteBishop] == 1 && nr[BlackKnight]+nr[BlackBishop] == 1 /* KBKN, KBKB, KNKN */
6810 if(--moveCount < 0 && appData.trivialDraws && canAdjudicate)
6811 { /* if the first 3 moves do not show a tactical win, declare draw */
6812 if(engineOpponent) {
6813 SendToProgram("force\n", engineOpponent); // suppress reply
6814 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
6816 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6817 GameEnds( GameIsDrawn, "Xboard adjudication: Trivial draw", GE_XBOARD );
6820 } else moveCount = 6;
6824 if (appData.debugMode) { int i;
6825 fprintf(debugFP, "repeat test fmm=%d bmm=%d ep=%d, reps=%d\n",
6826 forwardMostMove, backwardMostMove, boards[backwardMostMove][EP_STATUS],
6827 appData.drawRepeats);
6828 for( i=forwardMostMove; i>=backwardMostMove; i-- )
6829 fprintf(debugFP, "%d ep=%d\n", i, (signed char)boards[i][EP_STATUS]);
6833 // Repetition draws and 50-move rule can be applied independently of legality testing
6835 /* Check for rep-draws */
6837 for(k = forwardMostMove-2;
6838 k>=backwardMostMove && k>=forwardMostMove-100 &&
6839 (signed char)boards[k][EP_STATUS] < EP_UNKNOWN &&
6840 (signed char)boards[k+2][EP_STATUS] <= EP_NONE && (signed char)boards[k+1][EP_STATUS] <= EP_NONE;
6843 if(CompareBoards(boards[k], boards[forwardMostMove])) {
6844 /* compare castling rights */
6845 if( boards[forwardMostMove][CASTLING][2] != boards[k][CASTLING][2] &&
6846 (boards[k][CASTLING][0] != NoRights || boards[k][CASTLING][1] != NoRights) )
6847 rights++; /* King lost rights, while rook still had them */
6848 if( boards[forwardMostMove][CASTLING][2] != NoRights ) { /* king has rights */
6849 if( boards[forwardMostMove][CASTLING][0] != boards[k][CASTLING][0] ||
6850 boards[forwardMostMove][CASTLING][1] != boards[k][CASTLING][1] )
6851 rights++; /* but at least one rook lost them */
6853 if( boards[forwardMostMove][CASTLING][5] != boards[k][CASTLING][5] &&
6854 (boards[k][CASTLING][3] != NoRights || boards[k][CASTLING][4] != NoRights) )
6856 if( boards[forwardMostMove][CASTLING][5] != NoRights ) {
6857 if( boards[forwardMostMove][CASTLING][3] != boards[k][CASTLING][3] ||
6858 boards[forwardMostMove][CASTLING][4] != boards[k][CASTLING][4] )
6861 if( rights == 0 && ++count > appData.drawRepeats-2 && canAdjudicate
6862 && appData.drawRepeats > 1) {
6863 /* adjudicate after user-specified nr of repeats */
6864 int result = GameIsDrawn;
6865 char *details = "XBoard adjudication: repetition draw";
6866 if(gameInfo.variant == VariantXiangqi && appData.testLegality) {
6867 // [HGM] xiangqi: check for forbidden perpetuals
6868 int m, ourPerpetual = 1, hisPerpetual = 1;
6869 for(m=forwardMostMove; m>k; m-=2) {
6870 if(MateTest(boards[m], PosFlags(m)) != MT_CHECK)
6871 ourPerpetual = 0; // the current mover did not always check
6872 if(MateTest(boards[m-1], PosFlags(m-1)) != MT_CHECK)
6873 hisPerpetual = 0; // the opponent did not always check
6875 if(appData.debugMode) fprintf(debugFP, "XQ perpetual test, our=%d, his=%d\n",
6876 ourPerpetual, hisPerpetual);
6877 if(ourPerpetual && !hisPerpetual) { // we are actively checking him: forfeit
6878 result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
6879 details = "Xboard adjudication: perpetual checking";
6881 if(hisPerpetual && !ourPerpetual) { // he is checking us, but did not repeat yet
6882 break; // (or we would have caught him before). Abort repetition-checking loop.
6884 // Now check for perpetual chases
6885 if(!ourPerpetual && !hisPerpetual) { // no perpetual check, test for chase
6886 hisPerpetual = PerpetualChase(k, forwardMostMove);
6887 ourPerpetual = PerpetualChase(k+1, forwardMostMove);
6888 if(ourPerpetual && !hisPerpetual) { // we are actively chasing him: forfeit
6889 result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
6890 details = "Xboard adjudication: perpetual chasing";
6892 if(hisPerpetual && !ourPerpetual) // he is chasing us, but did not repeat yet
6893 break; // Abort repetition-checking loop.
6895 // if neither of us is checking or chasing all the time, or both are, it is draw
6897 if(engineOpponent) {
6898 SendToProgram("force\n", engineOpponent); // suppress reply
6899 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
6901 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6902 GameEnds( result, details, GE_XBOARD );
6905 if( rights == 0 && count > 1 ) /* occurred 2 or more times before */
6906 boards[forwardMostMove][EP_STATUS] = EP_REP_DRAW;
6910 /* Now we test for 50-move draws. Determine ply count */
6911 count = forwardMostMove;
6912 /* look for last irreversble move */
6913 while( (signed char)boards[count][EP_STATUS] <= EP_NONE && count > backwardMostMove )
6915 /* if we hit starting position, add initial plies */
6916 if( count == backwardMostMove )
6917 count -= initialRulePlies;
6918 count = forwardMostMove - count;
6919 if(gameInfo.variant == VariantXiangqi && ( count >= 100 || count >= 2*appData.ruleMoves ) ) {
6920 // adjust reversible move counter for checks in Xiangqi
6921 int i = forwardMostMove - count, inCheck = 0, lastCheck;
6922 if(i < backwardMostMove) i = backwardMostMove;
6923 while(i <= forwardMostMove) {
6924 lastCheck = inCheck; // check evasion does not count
6925 inCheck = (MateTest(boards[i], PosFlags(i)) == MT_CHECK);
6926 if(inCheck || lastCheck) count--; // check does not count
6931 boards[forwardMostMove][EP_STATUS] = EP_RULE_DRAW;
6932 /* this is used to judge if draw claims are legal */
6933 if(canAdjudicate && appData.ruleMoves > 0 && count >= 2*appData.ruleMoves) {
6934 if(engineOpponent) {
6935 SendToProgram("force\n", engineOpponent); // suppress reply
6936 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
6938 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6939 GameEnds( GameIsDrawn, "Xboard adjudication: 50-move rule", GE_XBOARD );
6943 /* if draw offer is pending, treat it as a draw claim
6944 * when draw condition present, to allow engines a way to
6945 * claim draws before making their move to avoid a race
6946 * condition occurring after their move
6948 if((gameMode == TwoMachinesPlay ? second.offeredDraw : userOfferedDraw) || first.offeredDraw ) {
6950 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_RULE_DRAW)
6951 p = "Draw claim: 50-move rule";
6952 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_REP_DRAW)
6953 p = "Draw claim: 3-fold repetition";
6954 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_INSUF_DRAW)
6955 p = "Draw claim: insufficient mating material";
6956 if( p != NULL && canAdjudicate) {
6957 if(engineOpponent) {
6958 SendToProgram("force\n", engineOpponent); // suppress reply
6959 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
6961 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6962 GameEnds( GameIsDrawn, p, GE_XBOARD );
6967 if( canAdjudicate && appData.adjudicateDrawMoves > 0 && forwardMostMove > (2*appData.adjudicateDrawMoves) ) {
6968 if(engineOpponent) {
6969 SendToProgram("force\n", engineOpponent); // suppress reply
6970 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
6972 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6973 GameEnds( GameIsDrawn, "Xboard adjudication: long game", GE_XBOARD );
6979 char *SendMoveToBookUser(int moveNr, ChessProgramState *cps, int initial)
6980 { // [HGM] book: this routine intercepts moves to simulate book replies
6981 char *bookHit = NULL;
6983 //first determine if the incoming move brings opponent into his book
6984 if(appData.usePolyglotBook && (cps == &first ? !appData.firstHasOwnBookUCI : !appData.secondHasOwnBookUCI))
6985 bookHit = ProbeBook(moveNr+1, appData.polyglotBook); // returns move
6986 if(appData.debugMode) fprintf(debugFP, "book hit = %s\n", bookHit ? bookHit : "(NULL)");
6987 if(bookHit != NULL && !cps->bookSuspend) {
6988 // make sure opponent is not going to reply after receiving move to book position
6989 SendToProgram("force\n", cps);
6990 cps->bookSuspend = TRUE; // flag indicating it has to be restarted
6992 if(!initial) SendMoveToProgram(moveNr, cps); // with hit on initial position there is no move
6993 // now arrange restart after book miss
6995 // after a book hit we never send 'go', and the code after the call to this routine
6996 // has '&& !bookHit' added to suppress potential sending there (based on 'firstMove').
6998 sprintf(buf, "%s%s\n", (cps->useUsermove ? "usermove " : ""), bookHit); // force book move into program supposed to play it
6999 SendToProgram(buf, cps);
7000 if(!initial) firstMove = FALSE; // normally we would clear the firstMove condition after return & sending 'go'
7001 } else if(initial) { // 'go' was needed irrespective of firstMove, and it has to be done in this routine
7002 SendToProgram("go\n", cps);
7003 cps->bookSuspend = FALSE; // after a 'go' we are never suspended
7004 } else { // 'go' might be sent based on 'firstMove' after this routine returns
7005 if(cps->bookSuspend && !firstMove) // 'go' needed, and it will not be done after we return
7006 SendToProgram("go\n", cps);
7007 cps->bookSuspend = FALSE; // anyhow, we will not be suspended after a miss
7009 return bookHit; // notify caller of hit, so it can take action to send move to opponent
7013 ChessProgramState *savedState;
7014 void DeferredBookMove(void)
7016 if(savedState->lastPing != savedState->lastPong)
7017 ScheduleDelayedEvent(DeferredBookMove, 10);
7019 HandleMachineMove(savedMessage, savedState);
7023 HandleMachineMove(message, cps)
7025 ChessProgramState *cps;
7027 char machineMove[MSG_SIZ], buf1[MSG_SIZ*10], buf2[MSG_SIZ];
7028 char realname[MSG_SIZ];
7029 int fromX, fromY, toX, toY;
7038 FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book hit
7040 * Kludge to ignore BEL characters
7042 while (*message == '\007') message++;
7045 * [HGM] engine debug message: ignore lines starting with '#' character
7047 if(cps->debug && *message == '#') return;
7050 * Look for book output
7052 if (cps == &first && bookRequested) {
7053 if (message[0] == '\t' || message[0] == ' ') {
7054 /* Part of the book output is here; append it */
7055 strcat(bookOutput, message);
7056 strcat(bookOutput, " \n");
7058 } else if (bookOutput[0] != NULLCHAR) {
7059 /* All of book output has arrived; display it */
7060 char *p = bookOutput;
7061 while (*p != NULLCHAR) {
7062 if (*p == '\t') *p = ' ';
7065 DisplayInformation(bookOutput);
7066 bookRequested = FALSE;
7067 /* Fall through to parse the current output */
7072 * Look for machine move.
7074 if ((sscanf(message, "%s %s %s", buf1, buf2, machineMove) == 3 && strcmp(buf2, "...") == 0) ||
7075 (sscanf(message, "%s %s", buf1, machineMove) == 2 && strcmp(buf1, "move") == 0))
7077 /* This method is only useful on engines that support ping */
7078 if (cps->lastPing != cps->lastPong) {
7079 if (gameMode == BeginningOfGame) {
7080 /* Extra move from before last new; ignore */
7081 if (appData.debugMode) {
7082 fprintf(debugFP, "Ignoring extra move from %s\n", cps->which);
7085 if (appData.debugMode) {
7086 fprintf(debugFP, "Undoing extra move from %s, gameMode %d\n",
7087 cps->which, gameMode);
7090 SendToProgram("undo\n", cps);
7096 case BeginningOfGame:
7097 /* Extra move from before last reset; ignore */
7098 if (appData.debugMode) {
7099 fprintf(debugFP, "Ignoring extra move from %s\n", cps->which);
7106 /* Extra move after we tried to stop. The mode test is
7107 not a reliable way of detecting this problem, but it's
7108 the best we can do on engines that don't support ping.
7110 if (appData.debugMode) {
7111 fprintf(debugFP, "Undoing extra move from %s, gameMode %d\n",
7112 cps->which, gameMode);
7114 SendToProgram("undo\n", cps);
7117 case MachinePlaysWhite:
7118 case IcsPlayingWhite:
7119 machineWhite = TRUE;
7122 case MachinePlaysBlack:
7123 case IcsPlayingBlack:
7124 machineWhite = FALSE;
7127 case TwoMachinesPlay:
7128 machineWhite = (cps->twoMachinesColor[0] == 'w');
7131 if (WhiteOnMove(forwardMostMove) != machineWhite) {
7132 if (appData.debugMode) {
7134 "Ignoring move out of turn by %s, gameMode %d"
7135 ", forwardMost %d\n",
7136 cps->which, gameMode, forwardMostMove);
7141 if (appData.debugMode) { int f = forwardMostMove;
7142 fprintf(debugFP, "machine move %d, castling = %d %d %d %d %d %d\n", f,
7143 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
7144 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
7146 if(cps->alphaRank) AlphaRank(machineMove, 4);
7147 if (!ParseOneMove(machineMove, forwardMostMove, &moveType,
7148 &fromX, &fromY, &toX, &toY, &promoChar)) {
7149 /* Machine move could not be parsed; ignore it. */
7150 sprintf(buf1, _("Illegal move \"%s\" from %s machine"),
7151 machineMove, cps->which);
7152 DisplayError(buf1, 0);
7153 sprintf(buf1, "Xboard: Forfeit due to invalid move: %s (%c%c%c%c) res=%d",
7154 machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, moveType);
7155 if (gameMode == TwoMachinesPlay) {
7156 GameEnds(machineWhite ? BlackWins : WhiteWins,
7162 /* [HGM] Apparently legal, but so far only tested with EP_UNKOWN */
7163 /* So we have to redo legality test with true e.p. status here, */
7164 /* to make sure an illegal e.p. capture does not slip through, */
7165 /* to cause a forfeit on a justified illegal-move complaint */
7166 /* of the opponent. */
7167 if( gameMode==TwoMachinesPlay && appData.testLegality ) {
7169 moveType = LegalityTest(boards[forwardMostMove], PosFlags(forwardMostMove),
7170 fromY, fromX, toY, toX, promoChar);
7171 if (appData.debugMode) {
7173 for(i=0; i< nrCastlingRights; i++) fprintf(debugFP, "(%d,%d) ",
7174 boards[forwardMostMove][CASTLING][i], castlingRank[i]);
7175 fprintf(debugFP, "castling rights\n");
7177 if(moveType == IllegalMove) {
7178 sprintf(buf1, "Xboard: Forfeit due to illegal move: %s (%c%c%c%c)%c",
7179 machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, 0);
7180 GameEnds(machineWhite ? BlackWins : WhiteWins,
7183 } else if(gameInfo.variant != VariantFischeRandom && gameInfo.variant != VariantCapaRandom)
7184 /* [HGM] Kludge to handle engines that send FRC-style castling
7185 when they shouldn't (like TSCP-Gothic) */
7187 case WhiteASideCastleFR:
7188 case BlackASideCastleFR:
7190 currentMoveString[2]++;
7192 case WhiteHSideCastleFR:
7193 case BlackHSideCastleFR:
7195 currentMoveString[2]--;
7197 default: ; // nothing to do, but suppresses warning of pedantic compilers
7200 hintRequested = FALSE;
7201 lastHint[0] = NULLCHAR;
7202 bookRequested = FALSE;
7203 /* Program may be pondering now */
7204 cps->maybeThinking = TRUE;
7205 if (cps->sendTime == 2) cps->sendTime = 1;
7206 if (cps->offeredDraw) cps->offeredDraw--;
7208 /* currentMoveString is set as a side-effect of ParseOneMove */
7209 strcpy(machineMove, currentMoveString);
7210 strcat(machineMove, "\n");
7211 strcpy(moveList[forwardMostMove], machineMove);
7213 /* [AS] Save move info*/
7214 pvInfoList[ forwardMostMove ].score = programStats.score;
7215 pvInfoList[ forwardMostMove ].depth = programStats.depth;
7216 pvInfoList[ forwardMostMove ].time = programStats.time; // [HGM] PGNtime: take time from engine stats
7218 MakeMove(fromX, fromY, toX, toY, promoChar);/*updates forwardMostMove*/
7220 /* [AS] Adjudicate game if needed (note: remember that forwardMostMove now points past the last move) */
7221 if( gameMode == TwoMachinesPlay && adjudicateLossThreshold != 0 && forwardMostMove >= adjudicateLossPlies ) {
7224 while( count < adjudicateLossPlies ) {
7225 int score = pvInfoList[ forwardMostMove - count - 1 ].score;
7228 score = -score; /* Flip score for winning side */
7231 if( score > adjudicateLossThreshold ) {
7238 if( count >= adjudicateLossPlies ) {
7239 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
7241 GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
7242 "Xboard adjudication",
7249 if(Adjudicate(cps)) return; // [HGM] adjudicate: for all automatic game ends
7252 if ((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack) &&
7254 if(cps->offeredDraw && (signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
7255 SendToICS(ics_prefix); // [HGM] drawclaim: send caim and move on one line for FICS
7257 SendMoveToICS(moveType, fromX, fromY, toX, toY);
7259 SendMoveToICS(moveType, fromX, fromY, toX, toY);
7261 if(appData.autoKibitz && !appData.icsEngineAnalyze ) { /* [HGM] kibitz: send most-recent PV info to ICS */
7262 char buf[3*MSG_SIZ];
7264 sprintf(buf, "kibitz !!! %+.2f/%d (%.2f sec, %u nodes, %.0f knps) PV=%s\n",
7265 programStats.score / 100.,
7267 programStats.time / 100.,
7268 (unsigned int)programStats.nodes,
7269 (unsigned int)programStats.nodes / (10*abs(programStats.time) + 1.),
7270 programStats.movelist);
7272 if(appData.debugMode) fprintf(debugFP, "nodes = %d, %lld\n", (int) programStats.nodes, programStats.nodes);
7277 /* [AS] Clear stats for next move */
7278 ClearProgramStats();
7279 thinkOutput[0] = NULLCHAR;
7280 hiddenThinkOutputState = 0;
7283 if (gameMode == TwoMachinesPlay) {
7284 /* [HGM] relaying draw offers moved to after reception of move */
7285 /* and interpreting offer as claim if it brings draw condition */
7286 if (cps->offeredDraw == 1 && cps->other->sendDrawOffers) {
7287 SendToProgram("draw\n", cps->other);
7289 if (cps->other->sendTime) {
7290 SendTimeRemaining(cps->other,
7291 cps->other->twoMachinesColor[0] == 'w');
7293 bookHit = SendMoveToBookUser(forwardMostMove-1, cps->other, FALSE);
7294 if (firstMove && !bookHit) {
7296 if (cps->other->useColors) {
7297 SendToProgram(cps->other->twoMachinesColor, cps->other);
7299 SendToProgram("go\n", cps->other);
7301 cps->other->maybeThinking = TRUE;
7304 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
7306 if (!pausing && appData.ringBellAfterMoves) {
7311 * Reenable menu items that were disabled while
7312 * machine was thinking
7314 if (gameMode != TwoMachinesPlay)
7315 SetUserThinkingEnables();
7317 // [HGM] book: after book hit opponent has received move and is now in force mode
7318 // force the book reply into it, and then fake that it outputted this move by jumping
7319 // back to the beginning of HandleMachineMove, with cps toggled and message set to this move
7321 static char bookMove[MSG_SIZ]; // a bit generous?
7323 strcpy(bookMove, "move ");
7324 strcat(bookMove, bookHit);
7327 programStats.nodes = programStats.depth = programStats.time =
7328 programStats.score = programStats.got_only_move = 0;
7329 sprintf(programStats.movelist, "%s (xbook)", bookHit);
7331 if(cps->lastPing != cps->lastPong) {
7332 savedMessage = message; // args for deferred call
7334 ScheduleDelayedEvent(DeferredBookMove, 10);
7343 /* Set special modes for chess engines. Later something general
7344 * could be added here; for now there is just one kludge feature,
7345 * needed because Crafty 15.10 and earlier don't ignore SIGINT
7346 * when "xboard" is given as an interactive command.
7348 if (strncmp(message, "kibitz Hello from Crafty", 24) == 0) {
7349 cps->useSigint = FALSE;
7350 cps->useSigterm = FALSE;
7352 if (strncmp(message, "feature ", 8) == 0) { // [HGM] moved forward to pre-empt non-compliant commands
7353 ParseFeatures(message+8, cps);
7354 return; // [HGM] This return was missing, causing option features to be recognized as non-compliant commands!
7357 /* [HGM] Allow engine to set up a position. Don't ask me why one would
7358 * want this, I was asked to put it in, and obliged.
7360 if (!strncmp(message, "setboard ", 9)) {
7361 Board initial_position;
7363 GameEnds(GameUnfinished, "Engine aborts game", GE_XBOARD);
7365 if (!ParseFEN(initial_position, &blackPlaysFirst, message + 9)) {
7366 DisplayError(_("Bad FEN received from engine"), 0);
7370 CopyBoard(boards[0], initial_position);
7371 initialRulePlies = FENrulePlies;
7372 if(blackPlaysFirst) gameMode = MachinePlaysWhite;
7373 else gameMode = MachinePlaysBlack;
7374 DrawPosition(FALSE, boards[currentMove]);
7380 * Look for communication commands
7382 if (!strncmp(message, "telluser ", 9)) {
7383 EscapeExpand(message+9, message+9); // [HGM] esc: allow escape sequences in popup box
7384 DisplayNote(message + 9);
7387 if (!strncmp(message, "tellusererror ", 14)) {
7389 EscapeExpand(message+14, message+14); // [HGM] esc: allow escape sequences in popup box
7390 DisplayError(message + 14, 0);
7393 if (!strncmp(message, "tellopponent ", 13)) {
7394 if (appData.icsActive) {
7396 snprintf(buf1, sizeof(buf1), "%ssay %s\n", ics_prefix, message + 13);
7400 DisplayNote(message + 13);
7404 if (!strncmp(message, "tellothers ", 11)) {
7405 if (appData.icsActive) {
7407 snprintf(buf1, sizeof(buf1), "%swhisper %s\n", ics_prefix, message + 11);
7413 if (!strncmp(message, "tellall ", 8)) {
7414 if (appData.icsActive) {
7416 snprintf(buf1, sizeof(buf1), "%skibitz %s\n", ics_prefix, message + 8);
7420 DisplayNote(message + 8);
7424 if (strncmp(message, "warning", 7) == 0) {
7425 /* Undocumented feature, use tellusererror in new code */
7426 DisplayError(message, 0);
7429 if (sscanf(message, "askuser %s %[^\n]", buf1, buf2) == 2) {
7430 strcpy(realname, cps->tidy);
7431 strcat(realname, " query");
7432 AskQuestion(realname, buf2, buf1, cps->pr);
7435 /* Commands from the engine directly to ICS. We don't allow these to be
7436 * sent until we are logged on. Crafty kibitzes have been known to
7437 * interfere with the login process.
7440 if (!strncmp(message, "tellics ", 8)) {
7441 SendToICS(message + 8);
7445 if (!strncmp(message, "tellicsnoalias ", 15)) {
7446 SendToICS(ics_prefix);
7447 SendToICS(message + 15);
7451 /* The following are for backward compatibility only */
7452 if (!strncmp(message,"whisper",7) || !strncmp(message,"kibitz",6) ||
7453 !strncmp(message,"draw",4) || !strncmp(message,"tell",3)) {
7454 SendToICS(ics_prefix);
7460 if (sscanf(message, "pong %d", &cps->lastPong) == 1) {
7464 * If the move is illegal, cancel it and redraw the board.
7465 * Also deal with other error cases. Matching is rather loose
7466 * here to accommodate engines written before the spec.
7468 if (strncmp(message + 1, "llegal move", 11) == 0 ||
7469 strncmp(message, "Error", 5) == 0) {
7470 if (StrStr(message, "name") ||
7471 StrStr(message, "rating") || StrStr(message, "?") ||
7472 StrStr(message, "result") || StrStr(message, "board") ||
7473 StrStr(message, "bk") || StrStr(message, "computer") ||
7474 StrStr(message, "variant") || StrStr(message, "hint") ||
7475 StrStr(message, "random") || StrStr(message, "depth") ||
7476 StrStr(message, "accepted")) {
7479 if (StrStr(message, "protover")) {
7480 /* Program is responding to input, so it's apparently done
7481 initializing, and this error message indicates it is
7482 protocol version 1. So we don't need to wait any longer
7483 for it to initialize and send feature commands. */
7484 FeatureDone(cps, 1);
7485 cps->protocolVersion = 1;
7488 cps->maybeThinking = FALSE;
7490 if (StrStr(message, "draw")) {
7491 /* Program doesn't have "draw" command */
7492 cps->sendDrawOffers = 0;
7495 if (cps->sendTime != 1 &&
7496 (StrStr(message, "time") || StrStr(message, "otim"))) {
7497 /* Program apparently doesn't have "time" or "otim" command */
7501 if (StrStr(message, "analyze")) {
7502 cps->analysisSupport = FALSE;
7503 cps->analyzing = FALSE;
7505 sprintf(buf2, _("%s does not support analysis"), cps->tidy);
7506 DisplayError(buf2, 0);
7509 if (StrStr(message, "(no matching move)st")) {
7510 /* Special kludge for GNU Chess 4 only */
7511 cps->stKludge = TRUE;
7512 SendTimeControl(cps, movesPerSession, timeControl,
7513 timeIncrement, appData.searchDepth,
7517 if (StrStr(message, "(no matching move)sd")) {
7518 /* Special kludge for GNU Chess 4 only */
7519 cps->sdKludge = TRUE;
7520 SendTimeControl(cps, movesPerSession, timeControl,
7521 timeIncrement, appData.searchDepth,
7525 if (!StrStr(message, "llegal")) {
7528 if (gameMode == BeginningOfGame || gameMode == EndOfGame ||
7529 gameMode == IcsIdle) return;
7530 if (forwardMostMove <= backwardMostMove) return;
7531 if (pausing) PauseEvent();
7532 if(appData.forceIllegal) {
7533 // [HGM] illegal: machine refused move; force position after move into it
7534 SendToProgram("force\n", cps);
7535 if(!cps->useSetboard) { // hideous kludge on kludge, because SendBoard sucks.
7536 // we have a real problem now, as SendBoard will use the a2a3 kludge
7537 // when black is to move, while there might be nothing on a2 or black
7538 // might already have the move. So send the board as if white has the move.
7539 // But first we must change the stm of the engine, as it refused the last move
7540 SendBoard(cps, 0); // always kludgeless, as white is to move on boards[0]
7541 if(WhiteOnMove(forwardMostMove)) {
7542 SendToProgram("a7a6\n", cps); // for the engine black still had the move
7543 SendBoard(cps, forwardMostMove); // kludgeless board
7545 SendToProgram("a2a3\n", cps); // for the engine white still had the move
7546 CopyBoard(boards[forwardMostMove+1], boards[forwardMostMove]);
7547 SendBoard(cps, forwardMostMove+1); // kludgeless board
7549 } else SendBoard(cps, forwardMostMove); // FEN case, also sets stm properly
7550 if(gameMode == MachinePlaysWhite || gameMode == MachinePlaysBlack ||
7551 gameMode == TwoMachinesPlay)
7552 SendToProgram("go\n", cps);
7555 if (gameMode == PlayFromGameFile) {
7556 /* Stop reading this game file */
7557 gameMode = EditGame;
7560 currentMove = forwardMostMove-1;
7561 DisplayMove(currentMove-1); /* before DisplayMoveError */
7562 SwitchClocks(forwardMostMove-1); // [HGM] race
7563 DisplayBothClocks();
7564 sprintf(buf1, _("Illegal move \"%s\" (rejected by %s chess program)"),
7565 parseList[currentMove], cps->which);
7566 DisplayMoveError(buf1);
7567 DrawPosition(FALSE, boards[currentMove]);
7569 /* [HGM] illegal-move claim should forfeit game when Xboard */
7570 /* only passes fully legal moves */
7571 if( appData.testLegality && gameMode == TwoMachinesPlay ) {
7572 GameEnds( cps->twoMachinesColor[0] == 'w' ? BlackWins : WhiteWins,
7573 "False illegal-move claim", GE_XBOARD );
7577 if (strncmp(message, "time", 4) == 0 && StrStr(message, "Illegal")) {
7578 /* Program has a broken "time" command that
7579 outputs a string not ending in newline.
7585 * If chess program startup fails, exit with an error message.
7586 * Attempts to recover here are futile.
7588 if ((StrStr(message, "unknown host") != NULL)
7589 || (StrStr(message, "No remote directory") != NULL)
7590 || (StrStr(message, "not found") != NULL)
7591 || (StrStr(message, "No such file") != NULL)
7592 || (StrStr(message, "can't alloc") != NULL)
7593 || (StrStr(message, "Permission denied") != NULL)) {
7595 cps->maybeThinking = FALSE;
7596 snprintf(buf1, sizeof(buf1), _("Failed to start %s chess program %s on %s: %s\n"),
7597 cps->which, cps->program, cps->host, message);
7598 RemoveInputSource(cps->isr);
7599 DisplayFatalError(buf1, 0, 1);
7604 * Look for hint output
7606 if (sscanf(message, "Hint: %s", buf1) == 1) {
7607 if (cps == &first && hintRequested) {
7608 hintRequested = FALSE;
7609 if (ParseOneMove(buf1, forwardMostMove, &moveType,
7610 &fromX, &fromY, &toX, &toY, &promoChar)) {
7611 (void) CoordsToAlgebraic(boards[forwardMostMove],
7612 PosFlags(forwardMostMove),
7613 fromY, fromX, toY, toX, promoChar, buf1);
7614 snprintf(buf2, sizeof(buf2), _("Hint: %s"), buf1);
7615 DisplayInformation(buf2);
7617 /* Hint move could not be parsed!? */
7618 snprintf(buf2, sizeof(buf2),
7619 _("Illegal hint move \"%s\"\nfrom %s chess program"),
7621 DisplayError(buf2, 0);
7624 strcpy(lastHint, buf1);
7630 * Ignore other messages if game is not in progress
7632 if (gameMode == BeginningOfGame || gameMode == EndOfGame ||
7633 gameMode == IcsIdle || cps->lastPing != cps->lastPong) return;
7636 * look for win, lose, draw, or draw offer
7638 if (strncmp(message, "1-0", 3) == 0) {
7639 char *p, *q, *r = "";
7640 p = strchr(message, '{');
7648 GameEnds(WhiteWins, r, GE_ENGINE1 + (cps != &first)); /* [HGM] pass claimer indication for claim test */
7650 } else if (strncmp(message, "0-1", 3) == 0) {
7651 char *p, *q, *r = "";
7652 p = strchr(message, '{');
7660 /* Kludge for Arasan 4.1 bug */
7661 if (strcmp(r, "Black resigns") == 0) {
7662 GameEnds(WhiteWins, r, GE_ENGINE1 + (cps != &first));
7665 GameEnds(BlackWins, r, GE_ENGINE1 + (cps != &first));
7667 } else if (strncmp(message, "1/2", 3) == 0) {
7668 char *p, *q, *r = "";
7669 p = strchr(message, '{');
7678 GameEnds(GameIsDrawn, r, GE_ENGINE1 + (cps != &first));
7681 } else if (strncmp(message, "White resign", 12) == 0) {
7682 GameEnds(BlackWins, "White resigns", GE_ENGINE1 + (cps != &first));
7684 } else if (strncmp(message, "Black resign", 12) == 0) {
7685 GameEnds(WhiteWins, "Black resigns", GE_ENGINE1 + (cps != &first));
7687 } else if (strncmp(message, "White matches", 13) == 0 ||
7688 strncmp(message, "Black matches", 13) == 0 ) {
7689 /* [HGM] ignore GNUShogi noises */
7691 } else if (strncmp(message, "White", 5) == 0 &&
7692 message[5] != '(' &&
7693 StrStr(message, "Black") == NULL) {
7694 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
7696 } else if (strncmp(message, "Black", 5) == 0 &&
7697 message[5] != '(') {
7698 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
7700 } else if (strcmp(message, "resign") == 0 ||
7701 strcmp(message, "computer resigns") == 0) {
7703 case MachinePlaysBlack:
7704 case IcsPlayingBlack:
7705 GameEnds(WhiteWins, "Black resigns", GE_ENGINE);
7707 case MachinePlaysWhite:
7708 case IcsPlayingWhite:
7709 GameEnds(BlackWins, "White resigns", GE_ENGINE);
7711 case TwoMachinesPlay:
7712 if (cps->twoMachinesColor[0] == 'w')
7713 GameEnds(BlackWins, "White resigns", GE_ENGINE1 + (cps != &first));
7715 GameEnds(WhiteWins, "Black resigns", GE_ENGINE1 + (cps != &first));
7722 } else if (strncmp(message, "opponent mates", 14) == 0) {
7724 case MachinePlaysBlack:
7725 case IcsPlayingBlack:
7726 GameEnds(WhiteWins, "White mates", GE_ENGINE);
7728 case MachinePlaysWhite:
7729 case IcsPlayingWhite:
7730 GameEnds(BlackWins, "Black mates", GE_ENGINE);
7732 case TwoMachinesPlay:
7733 if (cps->twoMachinesColor[0] == 'w')
7734 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
7736 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
7743 } else if (strncmp(message, "computer mates", 14) == 0) {
7745 case MachinePlaysBlack:
7746 case IcsPlayingBlack:
7747 GameEnds(BlackWins, "Black mates", GE_ENGINE1);
7749 case MachinePlaysWhite:
7750 case IcsPlayingWhite:
7751 GameEnds(WhiteWins, "White mates", GE_ENGINE);
7753 case TwoMachinesPlay:
7754 if (cps->twoMachinesColor[0] == 'w')
7755 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
7757 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
7764 } else if (strncmp(message, "checkmate", 9) == 0) {
7765 if (WhiteOnMove(forwardMostMove)) {
7766 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
7768 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
7771 } else if (strstr(message, "Draw") != NULL ||
7772 strstr(message, "game is a draw") != NULL) {
7773 GameEnds(GameIsDrawn, "Draw", GE_ENGINE1 + (cps != &first));
7775 } else if (strstr(message, "offer") != NULL &&
7776 strstr(message, "draw") != NULL) {
7778 if (appData.zippyPlay && first.initDone) {
7779 /* Relay offer to ICS */
7780 SendToICS(ics_prefix);
7781 SendToICS("draw\n");
7784 cps->offeredDraw = 2; /* valid until this engine moves twice */
7785 if (gameMode == TwoMachinesPlay) {
7786 if (cps->other->offeredDraw) {
7787 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
7788 /* [HGM] in two-machine mode we delay relaying draw offer */
7789 /* until after we also have move, to see if it is really claim */
7791 } else if (gameMode == MachinePlaysWhite ||
7792 gameMode == MachinePlaysBlack) {
7793 if (userOfferedDraw) {
7794 DisplayInformation(_("Machine accepts your draw offer"));
7795 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
7797 DisplayInformation(_("Machine offers a draw\nSelect Action / Draw to agree"));
7804 * Look for thinking output
7806 if ( appData.showThinking // [HGM] thinking: test all options that cause this output
7807 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp()
7809 int plylev, mvleft, mvtot, curscore, time;
7810 char mvname[MOVE_LEN];
7814 int prefixHint = FALSE;
7815 mvname[0] = NULLCHAR;
7818 case MachinePlaysBlack:
7819 case IcsPlayingBlack:
7820 if (WhiteOnMove(forwardMostMove)) prefixHint = TRUE;
7822 case MachinePlaysWhite:
7823 case IcsPlayingWhite:
7824 if (!WhiteOnMove(forwardMostMove)) prefixHint = TRUE;
7829 case IcsObserving: /* [DM] icsEngineAnalyze */
7830 if (!appData.icsEngineAnalyze) ignore = TRUE;
7832 case TwoMachinesPlay:
7833 if ((cps->twoMachinesColor[0] == 'w') != WhiteOnMove(forwardMostMove)) {
7843 ChessProgramStats tempStats = programStats; // [HGM] info: filter out info lines
7845 if (sscanf(message, "%d%c %d %d " u64Display " %[^\n]\n",
7846 &plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5) {
7848 if (plyext != ' ' && plyext != '\t') {
7852 /* [AS] Negate score if machine is playing black and reporting absolute scores */
7853 if( cps->scoreIsAbsolute &&
7854 ( gameMode == MachinePlaysBlack ||
7855 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b' ||
7856 gameMode == IcsPlayingBlack || // [HGM] also add other situations where engine should report black POV
7857 (gameMode == AnalyzeMode || gameMode == AnalyzeFile || gameMode == IcsObserving && appData.icsEngineAnalyze) &&
7858 !WhiteOnMove(currentMove)
7861 curscore = -curscore;
7865 tempStats.depth = plylev;
7866 tempStats.nodes = nodes;
7867 tempStats.time = time;
7868 tempStats.score = curscore;
7869 tempStats.got_only_move = 0;
7871 if(cps->nps >= 0) { /* [HGM] nps: use engine nodes or time to decrement clock */
7874 if(cps->nps == 0) ticklen = 10*time; // use engine reported time
7875 else ticklen = (1000. * u64ToDouble(nodes)) / cps->nps; // convert node count to time
7876 if(WhiteOnMove(forwardMostMove) && (gameMode == MachinePlaysWhite ||
7877 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'w'))
7878 whiteTimeRemaining = timeRemaining[0][forwardMostMove] - ticklen;
7879 if(!WhiteOnMove(forwardMostMove) && (gameMode == MachinePlaysBlack ||
7880 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b'))
7881 blackTimeRemaining = timeRemaining[1][forwardMostMove] - ticklen;
7884 /* Buffer overflow protection */
7885 if (buf1[0] != NULLCHAR) {
7886 if (strlen(buf1) >= sizeof(tempStats.movelist)
7887 && appData.debugMode) {
7889 "PV is too long; using the first %u bytes.\n",
7890 (unsigned) sizeof(tempStats.movelist) - 1);
7893 safeStrCpy( tempStats.movelist, buf1, sizeof(tempStats.movelist) );
7895 sprintf(tempStats.movelist, " no PV\n");
7898 if (tempStats.seen_stat) {
7899 tempStats.ok_to_send = 1;
7902 if (strchr(tempStats.movelist, '(') != NULL) {
7903 tempStats.line_is_book = 1;
7904 tempStats.nr_moves = 0;
7905 tempStats.moves_left = 0;
7907 tempStats.line_is_book = 0;
7910 if(tempStats.score != 0 || tempStats.nodes != 0 || tempStats.time != 0)
7911 programStats = tempStats; // [HGM] info: only set stats if genuine PV and not an info line
7913 SendProgramStatsToFrontend( cps, &tempStats );
7916 [AS] Protect the thinkOutput buffer from overflow... this
7917 is only useful if buf1 hasn't overflowed first!
7919 sprintf(thinkOutput, "[%d]%c%+.2f %s%s",
7921 (gameMode == TwoMachinesPlay ?
7922 ToUpper(cps->twoMachinesColor[0]) : ' '),
7923 ((double) curscore) / 100.0,
7924 prefixHint ? lastHint : "",
7925 prefixHint ? " " : "" );
7927 if( buf1[0] != NULLCHAR ) {
7928 unsigned max_len = sizeof(thinkOutput) - strlen(thinkOutput) - 1;
7930 if( strlen(buf1) > max_len ) {
7931 if( appData.debugMode) {
7932 fprintf(debugFP,"PV is too long for thinkOutput, truncating.\n");
7934 buf1[max_len+1] = '\0';
7937 strcat( thinkOutput, buf1 );
7940 if (currentMove == forwardMostMove || gameMode == AnalyzeMode
7941 || gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
7942 DisplayMove(currentMove - 1);
7946 } else if ((p=StrStr(message, "(only move)")) != NULL) {
7947 /* crafty (9.25+) says "(only move) <move>"
7948 * if there is only 1 legal move
7950 sscanf(p, "(only move) %s", buf1);
7951 sprintf(thinkOutput, "%s (only move)", buf1);
7952 sprintf(programStats.movelist, "%s (only move)", buf1);
7953 programStats.depth = 1;
7954 programStats.nr_moves = 1;
7955 programStats.moves_left = 1;
7956 programStats.nodes = 1;
7957 programStats.time = 1;
7958 programStats.got_only_move = 1;
7960 /* Not really, but we also use this member to
7961 mean "line isn't going to change" (Crafty
7962 isn't searching, so stats won't change) */
7963 programStats.line_is_book = 1;
7965 SendProgramStatsToFrontend( cps, &programStats );
7967 if (currentMove == forwardMostMove || gameMode==AnalyzeMode ||
7968 gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
7969 DisplayMove(currentMove - 1);
7972 } else if (sscanf(message,"stat01: %d " u64Display " %d %d %d %s",
7973 &time, &nodes, &plylev, &mvleft,
7974 &mvtot, mvname) >= 5) {
7975 /* The stat01: line is from Crafty (9.29+) in response
7976 to the "." command */
7977 programStats.seen_stat = 1;
7978 cps->maybeThinking = TRUE;
7980 if (programStats.got_only_move || !appData.periodicUpdates)
7983 programStats.depth = plylev;
7984 programStats.time = time;
7985 programStats.nodes = nodes;
7986 programStats.moves_left = mvleft;
7987 programStats.nr_moves = mvtot;
7988 strcpy(programStats.move_name, mvname);
7989 programStats.ok_to_send = 1;
7990 programStats.movelist[0] = '\0';
7992 SendProgramStatsToFrontend( cps, &programStats );
7996 } else if (strncmp(message,"++",2) == 0) {
7997 /* Crafty 9.29+ outputs this */
7998 programStats.got_fail = 2;
8001 } else if (strncmp(message,"--",2) == 0) {
8002 /* Crafty 9.29+ outputs this */
8003 programStats.got_fail = 1;
8006 } else if (thinkOutput[0] != NULLCHAR &&
8007 strncmp(message, " ", 4) == 0) {
8008 unsigned message_len;
8011 while (*p && *p == ' ') p++;
8013 message_len = strlen( p );
8015 /* [AS] Avoid buffer overflow */
8016 if( sizeof(thinkOutput) - strlen(thinkOutput) - 1 > message_len ) {
8017 strcat(thinkOutput, " ");
8018 strcat(thinkOutput, p);
8021 if( sizeof(programStats.movelist) - strlen(programStats.movelist) - 1 > message_len ) {
8022 strcat(programStats.movelist, " ");
8023 strcat(programStats.movelist, p);
8026 if (currentMove == forwardMostMove || gameMode==AnalyzeMode ||
8027 gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
8028 DisplayMove(currentMove - 1);
8036 if (sscanf(message, "%d%c %d %d " u64Display " %[^\n]\n",
8037 &plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5)
8039 ChessProgramStats cpstats;
8041 if (plyext != ' ' && plyext != '\t') {
8045 /* [AS] Negate score if machine is playing black and reporting absolute scores */
8046 if( cps->scoreIsAbsolute && ((gameMode == MachinePlaysBlack) || (gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b')) ) {
8047 curscore = -curscore;
8050 cpstats.depth = plylev;
8051 cpstats.nodes = nodes;
8052 cpstats.time = time;
8053 cpstats.score = curscore;
8054 cpstats.got_only_move = 0;
8055 cpstats.movelist[0] = '\0';
8057 if (buf1[0] != NULLCHAR) {
8058 safeStrCpy( cpstats.movelist, buf1, sizeof(cpstats.movelist) );
8061 cpstats.ok_to_send = 0;
8062 cpstats.line_is_book = 0;
8063 cpstats.nr_moves = 0;
8064 cpstats.moves_left = 0;
8066 SendProgramStatsToFrontend( cps, &cpstats );
8073 /* Parse a game score from the character string "game", and
8074 record it as the history of the current game. The game
8075 score is NOT assumed to start from the standard position.
8076 The display is not updated in any way.
8079 ParseGameHistory(game)
8083 int fromX, fromY, toX, toY, boardIndex;
8088 if (appData.debugMode)
8089 fprintf(debugFP, "Parsing game history: %s\n", game);
8091 if (gameInfo.event == NULL) gameInfo.event = StrSave("ICS game");
8092 gameInfo.site = StrSave(appData.icsHost);
8093 gameInfo.date = PGNDate();
8094 gameInfo.round = StrSave("-");
8096 /* Parse out names of players */
8097 while (*game == ' ') game++;
8099 while (*game != ' ') *p++ = *game++;
8101 gameInfo.white = StrSave(buf);
8102 while (*game == ' ') game++;
8104 while (*game != ' ' && *game != '\n') *p++ = *game++;
8106 gameInfo.black = StrSave(buf);
8109 boardIndex = blackPlaysFirst ? 1 : 0;
8112 yyboardindex = boardIndex;
8113 moveType = (ChessMove) yylex();
8115 case IllegalMove: /* maybe suicide chess, etc. */
8116 if (appData.debugMode) {
8117 fprintf(debugFP, "Illegal move from ICS: '%s'\n", yy_text);
8118 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
8119 setbuf(debugFP, NULL);
8121 case WhitePromotionChancellor:
8122 case BlackPromotionChancellor:
8123 case WhitePromotionArchbishop:
8124 case BlackPromotionArchbishop:
8125 case WhitePromotionQueen:
8126 case BlackPromotionQueen:
8127 case WhitePromotionRook:
8128 case BlackPromotionRook:
8129 case WhitePromotionBishop:
8130 case BlackPromotionBishop:
8131 case WhitePromotionKnight:
8132 case BlackPromotionKnight:
8133 case WhitePromotionKing:
8134 case BlackPromotionKing:
8135 case WhiteNonPromotion:
8136 case BlackNonPromotion:
8138 case WhiteCapturesEnPassant:
8139 case BlackCapturesEnPassant:
8140 case WhiteKingSideCastle:
8141 case WhiteQueenSideCastle:
8142 case BlackKingSideCastle:
8143 case BlackQueenSideCastle:
8144 case WhiteKingSideCastleWild:
8145 case WhiteQueenSideCastleWild:
8146 case BlackKingSideCastleWild:
8147 case BlackQueenSideCastleWild:
8149 case WhiteHSideCastleFR:
8150 case WhiteASideCastleFR:
8151 case BlackHSideCastleFR:
8152 case BlackASideCastleFR:
8154 fromX = currentMoveString[0] - AAA;
8155 fromY = currentMoveString[1] - ONE;
8156 toX = currentMoveString[2] - AAA;
8157 toY = currentMoveString[3] - ONE;
8158 promoChar = currentMoveString[4];
8162 fromX = moveType == WhiteDrop ?
8163 (int) CharToPiece(ToUpper(currentMoveString[0])) :
8164 (int) CharToPiece(ToLower(currentMoveString[0]));
8166 toX = currentMoveString[2] - AAA;
8167 toY = currentMoveString[3] - ONE;
8168 promoChar = NULLCHAR;
8172 sprintf(buf, _("Ambiguous move in ICS output: \"%s\""), yy_text);
8173 if (appData.debugMode) {
8174 fprintf(debugFP, "Ambiguous move from ICS: '%s'\n", yy_text);
8175 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
8176 setbuf(debugFP, NULL);
8178 DisplayError(buf, 0);
8180 case ImpossibleMove:
8182 sprintf(buf, _("Illegal move in ICS output: \"%s\""), yy_text);
8183 if (appData.debugMode) {
8184 fprintf(debugFP, "Impossible move from ICS: '%s'\n", yy_text);
8185 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
8186 setbuf(debugFP, NULL);
8188 DisplayError(buf, 0);
8190 case (ChessMove) 0: /* end of file */
8191 if (boardIndex < backwardMostMove) {
8192 /* Oops, gap. How did that happen? */
8193 DisplayError(_("Gap in move list"), 0);
8196 backwardMostMove = blackPlaysFirst ? 1 : 0;
8197 if (boardIndex > forwardMostMove) {
8198 forwardMostMove = boardIndex;
8202 if (boardIndex > (blackPlaysFirst ? 1 : 0)) {
8203 strcat(parseList[boardIndex-1], " ");
8204 strcat(parseList[boardIndex-1], yy_text);
8216 case GameUnfinished:
8217 if (gameMode == IcsExamining) {
8218 if (boardIndex < backwardMostMove) {
8219 /* Oops, gap. How did that happen? */
8222 backwardMostMove = blackPlaysFirst ? 1 : 0;
8225 gameInfo.result = moveType;
8226 p = strchr(yy_text, '{');
8227 if (p == NULL) p = strchr(yy_text, '(');
8230 if (p[0] == '0' || p[0] == '1' || p[0] == '*') p = "";
8232 q = strchr(p, *p == '{' ? '}' : ')');
8233 if (q != NULL) *q = NULLCHAR;
8236 gameInfo.resultDetails = StrSave(p);
8239 if (boardIndex >= forwardMostMove &&
8240 !(gameMode == IcsObserving && ics_gamenum == -1)) {
8241 backwardMostMove = blackPlaysFirst ? 1 : 0;
8244 (void) CoordsToAlgebraic(boards[boardIndex], PosFlags(boardIndex),
8245 fromY, fromX, toY, toX, promoChar,
8246 parseList[boardIndex]);
8247 CopyBoard(boards[boardIndex + 1], boards[boardIndex]);
8248 /* currentMoveString is set as a side-effect of yylex */
8249 strcpy(moveList[boardIndex], currentMoveString);
8250 strcat(moveList[boardIndex], "\n");
8252 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[boardIndex]);
8253 switch (MateTest(boards[boardIndex], PosFlags(boardIndex)) ) {
8259 if(gameInfo.variant != VariantShogi)
8260 strcat(parseList[boardIndex - 1], "+");
8264 strcat(parseList[boardIndex - 1], "#");
8271 /* Apply a move to the given board */
8273 ApplyMove(fromX, fromY, toX, toY, promoChar, board)
8274 int fromX, fromY, toX, toY;
8278 ChessSquare captured = board[toY][toX], piece, king; int p, oldEP = EP_NONE, berolina = 0;
8279 int promoRank = gameInfo.variant == VariantMakruk ? 3 : 1;
8281 /* [HGM] compute & store e.p. status and castling rights for new position */
8282 /* we can always do that 'in place', now pointers to these rights are passed to ApplyMove */
8284 if(gameInfo.variant == VariantBerolina) berolina = EP_BEROLIN_A;
8285 oldEP = (signed char)board[EP_STATUS];
8286 board[EP_STATUS] = EP_NONE;
8288 if( board[toY][toX] != EmptySquare )
8289 board[EP_STATUS] = EP_CAPTURE;
8291 /* [HGM] In Shatranj and Courier all promotions are to Ferz */
8292 if((gameInfo.variant==VariantShatranj || gameInfo.variant==VariantCourier || gameInfo.variant == VariantMakruk)
8293 && promoChar != 0) promoChar = PieceToChar(WhiteFerz);
8295 if (fromY == DROP_RANK) {
8297 piece = board[toY][toX] = (ChessSquare) fromX;
8301 if( board[fromY][fromX] == WhitePawn ) {
8302 if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
8303 board[EP_STATUS] = EP_PAWN_MOVE;
8305 if(toX>BOARD_LEFT && board[toY][toX-1] == BlackPawn &&
8306 gameInfo.variant != VariantBerolina || toX < fromX)
8307 board[EP_STATUS] = toX | berolina;
8308 if(toX<BOARD_RGHT-1 && board[toY][toX+1] == BlackPawn &&
8309 gameInfo.variant != VariantBerolina || toX > fromX)
8310 board[EP_STATUS] = toX;
8313 if( board[fromY][fromX] == BlackPawn ) {
8314 if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
8315 board[EP_STATUS] = EP_PAWN_MOVE;
8316 if( toY-fromY== -2) {
8317 if(toX>BOARD_LEFT && board[toY][toX-1] == WhitePawn &&
8318 gameInfo.variant != VariantBerolina || toX < fromX)
8319 board[EP_STATUS] = toX | berolina;
8320 if(toX<BOARD_RGHT-1 && board[toY][toX+1] == WhitePawn &&
8321 gameInfo.variant != VariantBerolina || toX > fromX)
8322 board[EP_STATUS] = toX;
8326 for(i=0; i<nrCastlingRights; i++) {
8327 if(board[CASTLING][i] == fromX && castlingRank[i] == fromY ||
8328 board[CASTLING][i] == toX && castlingRank[i] == toY
8329 ) board[CASTLING][i] = NoRights; // revoke for moved or captured piece
8332 if (fromX == toX && fromY == toY) return;
8334 piece = board[fromY][fromX]; /* [HGM] remember, for Shogi promotion */
8335 king = piece < (int) BlackPawn ? WhiteKing : BlackKing; /* [HGM] Knightmate simplify testing for castling */
8336 if(gameInfo.variant == VariantKnightmate)
8337 king += (int) WhiteUnicorn - (int) WhiteKing;
8339 /* Code added by Tord: */
8340 /* FRC castling assumed when king captures friendly rook. */
8341 if (board[fromY][fromX] == WhiteKing &&
8342 board[toY][toX] == WhiteRook) {
8343 board[fromY][fromX] = EmptySquare;
8344 board[toY][toX] = EmptySquare;
8346 board[0][BOARD_RGHT-2] = WhiteKing; board[0][BOARD_RGHT-3] = WhiteRook;
8348 board[0][BOARD_LEFT+2] = WhiteKing; board[0][BOARD_LEFT+3] = WhiteRook;
8350 } else if (board[fromY][fromX] == BlackKing &&
8351 board[toY][toX] == BlackRook) {
8352 board[fromY][fromX] = EmptySquare;
8353 board[toY][toX] = EmptySquare;
8355 board[BOARD_HEIGHT-1][BOARD_RGHT-2] = BlackKing; board[BOARD_HEIGHT-1][BOARD_RGHT-3] = BlackRook;
8357 board[BOARD_HEIGHT-1][BOARD_LEFT+2] = BlackKing; board[BOARD_HEIGHT-1][BOARD_LEFT+3] = BlackRook;
8359 /* End of code added by Tord */
8361 } else if (board[fromY][fromX] == king
8362 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
8363 && toY == fromY && toX > fromX+1) {
8364 board[fromY][fromX] = EmptySquare;
8365 board[toY][toX] = king;
8366 board[toY][toX-1] = board[fromY][BOARD_RGHT-1];
8367 board[fromY][BOARD_RGHT-1] = EmptySquare;
8368 } else if (board[fromY][fromX] == king
8369 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
8370 && toY == fromY && toX < fromX-1) {
8371 board[fromY][fromX] = EmptySquare;
8372 board[toY][toX] = king;
8373 board[toY][toX+1] = board[fromY][BOARD_LEFT];
8374 board[fromY][BOARD_LEFT] = EmptySquare;
8375 } else if (board[fromY][fromX] == WhitePawn
8376 && toY >= BOARD_HEIGHT-promoRank
8377 && gameInfo.variant != VariantXiangqi
8379 /* white pawn promotion */
8380 board[toY][toX] = CharToPiece(ToUpper(promoChar));
8381 if (board[toY][toX] == EmptySquare) {
8382 board[toY][toX] = WhiteQueen;
8384 if(gameInfo.variant==VariantBughouse ||
8385 gameInfo.variant==VariantCrazyhouse) /* [HGM] use shadow piece */
8386 board[toY][toX] = (ChessSquare) (PROMOTED board[toY][toX]);
8387 board[fromY][fromX] = EmptySquare;
8388 } else if ((fromY == BOARD_HEIGHT-4)
8390 && gameInfo.variant != VariantXiangqi
8391 && gameInfo.variant != VariantBerolina
8392 && (board[fromY][fromX] == WhitePawn)
8393 && (board[toY][toX] == EmptySquare)) {
8394 board[fromY][fromX] = EmptySquare;
8395 board[toY][toX] = WhitePawn;
8396 captured = board[toY - 1][toX];
8397 board[toY - 1][toX] = EmptySquare;
8398 } else if ((fromY == BOARD_HEIGHT-4)
8400 && gameInfo.variant == VariantBerolina
8401 && (board[fromY][fromX] == WhitePawn)
8402 && (board[toY][toX] == EmptySquare)) {
8403 board[fromY][fromX] = EmptySquare;
8404 board[toY][toX] = WhitePawn;
8405 if(oldEP & EP_BEROLIN_A) {
8406 captured = board[fromY][fromX-1];
8407 board[fromY][fromX-1] = EmptySquare;
8408 }else{ captured = board[fromY][fromX+1];
8409 board[fromY][fromX+1] = EmptySquare;
8411 } else if (board[fromY][fromX] == king
8412 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
8413 && toY == fromY && toX > fromX+1) {
8414 board[fromY][fromX] = EmptySquare;
8415 board[toY][toX] = king;
8416 board[toY][toX-1] = board[fromY][BOARD_RGHT-1];
8417 board[fromY][BOARD_RGHT-1] = EmptySquare;
8418 } else if (board[fromY][fromX] == king
8419 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
8420 && toY == fromY && toX < fromX-1) {
8421 board[fromY][fromX] = EmptySquare;
8422 board[toY][toX] = king;
8423 board[toY][toX+1] = board[fromY][BOARD_LEFT];
8424 board[fromY][BOARD_LEFT] = EmptySquare;
8425 } else if (fromY == 7 && fromX == 3
8426 && board[fromY][fromX] == BlackKing
8427 && toY == 7 && toX == 5) {
8428 board[fromY][fromX] = EmptySquare;
8429 board[toY][toX] = BlackKing;
8430 board[fromY][7] = EmptySquare;
8431 board[toY][4] = BlackRook;
8432 } else if (fromY == 7 && fromX == 3
8433 && board[fromY][fromX] == BlackKing
8434 && toY == 7 && toX == 1) {
8435 board[fromY][fromX] = EmptySquare;
8436 board[toY][toX] = BlackKing;
8437 board[fromY][0] = EmptySquare;
8438 board[toY][2] = BlackRook;
8439 } else if (board[fromY][fromX] == BlackPawn
8441 && gameInfo.variant != VariantXiangqi
8443 /* black pawn promotion */
8444 board[toY][toX] = CharToPiece(ToLower(promoChar));
8445 if (board[toY][toX] == EmptySquare) {
8446 board[toY][toX] = BlackQueen;
8448 if(gameInfo.variant==VariantBughouse ||
8449 gameInfo.variant==VariantCrazyhouse) /* [HGM] use shadow piece */
8450 board[toY][toX] = (ChessSquare) (PROMOTED board[toY][toX]);
8451 board[fromY][fromX] = EmptySquare;
8452 } else if ((fromY == 3)
8454 && gameInfo.variant != VariantXiangqi
8455 && gameInfo.variant != VariantBerolina
8456 && (board[fromY][fromX] == BlackPawn)
8457 && (board[toY][toX] == EmptySquare)) {
8458 board[fromY][fromX] = EmptySquare;
8459 board[toY][toX] = BlackPawn;
8460 captured = board[toY + 1][toX];
8461 board[toY + 1][toX] = EmptySquare;
8462 } else if ((fromY == 3)
8464 && gameInfo.variant == VariantBerolina
8465 && (board[fromY][fromX] == BlackPawn)
8466 && (board[toY][toX] == EmptySquare)) {
8467 board[fromY][fromX] = EmptySquare;
8468 board[toY][toX] = BlackPawn;
8469 if(oldEP & EP_BEROLIN_A) {
8470 captured = board[fromY][fromX-1];
8471 board[fromY][fromX-1] = EmptySquare;
8472 }else{ captured = board[fromY][fromX+1];
8473 board[fromY][fromX+1] = EmptySquare;
8476 board[toY][toX] = board[fromY][fromX];
8477 board[fromY][fromX] = EmptySquare;
8480 /* [HGM] now we promote for Shogi, if needed */
8481 if(gameInfo.variant == VariantShogi && promoChar == 'q')
8482 board[toY][toX] = (ChessSquare) (PROMOTED piece);
8485 if (gameInfo.holdingsWidth != 0) {
8487 /* !!A lot more code needs to be written to support holdings */
8488 /* [HGM] OK, so I have written it. Holdings are stored in the */
8489 /* penultimate board files, so they are automaticlly stored */
8490 /* in the game history. */
8491 if (fromY == DROP_RANK) {
8492 /* Delete from holdings, by decreasing count */
8493 /* and erasing image if necessary */
8495 if(p < (int) BlackPawn) { /* white drop */
8496 p -= (int)WhitePawn;
8497 p = PieceToNumber((ChessSquare)p);
8498 if(p >= gameInfo.holdingsSize) p = 0;
8499 if(--board[p][BOARD_WIDTH-2] <= 0)
8500 board[p][BOARD_WIDTH-1] = EmptySquare;
8501 if((int)board[p][BOARD_WIDTH-2] < 0)
8502 board[p][BOARD_WIDTH-2] = 0;
8503 } else { /* black drop */
8504 p -= (int)BlackPawn;
8505 p = PieceToNumber((ChessSquare)p);
8506 if(p >= gameInfo.holdingsSize) p = 0;
8507 if(--board[BOARD_HEIGHT-1-p][1] <= 0)
8508 board[BOARD_HEIGHT-1-p][0] = EmptySquare;
8509 if((int)board[BOARD_HEIGHT-1-p][1] < 0)
8510 board[BOARD_HEIGHT-1-p][1] = 0;
8513 if (captured != EmptySquare && gameInfo.holdingsSize > 0
8514 && gameInfo.variant != VariantBughouse ) {
8515 /* [HGM] holdings: Add to holdings, if holdings exist */
8516 if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat) {
8517 // [HGM] superchess: suppress flipping color of captured pieces by reverse pre-flip
8518 captured = (int) captured >= (int) BlackPawn ? BLACK_TO_WHITE captured : WHITE_TO_BLACK captured;
8521 if (p >= (int) BlackPawn) {
8522 p -= (int)BlackPawn;
8523 if(gameInfo.variant == VariantShogi && DEMOTED p >= 0) {
8524 /* in Shogi restore piece to its original first */
8525 captured = (ChessSquare) (DEMOTED captured);
8528 p = PieceToNumber((ChessSquare)p);
8529 if(p >= gameInfo.holdingsSize) { p = 0; captured = BlackPawn; }
8530 board[p][BOARD_WIDTH-2]++;
8531 board[p][BOARD_WIDTH-1] = BLACK_TO_WHITE captured;
8533 p -= (int)WhitePawn;
8534 if(gameInfo.variant == VariantShogi && DEMOTED p >= 0) {
8535 captured = (ChessSquare) (DEMOTED captured);
8538 p = PieceToNumber((ChessSquare)p);
8539 if(p >= gameInfo.holdingsSize) { p = 0; captured = WhitePawn; }
8540 board[BOARD_HEIGHT-1-p][1]++;
8541 board[BOARD_HEIGHT-1-p][0] = WHITE_TO_BLACK captured;
8544 } else if (gameInfo.variant == VariantAtomic) {
8545 if (captured != EmptySquare) {
8547 for (y = toY-1; y <= toY+1; y++) {
8548 for (x = toX-1; x <= toX+1; x++) {
8549 if (y >= 0 && y < BOARD_HEIGHT && x >= BOARD_LEFT && x < BOARD_RGHT &&
8550 board[y][x] != WhitePawn && board[y][x] != BlackPawn) {
8551 board[y][x] = EmptySquare;
8555 board[toY][toX] = EmptySquare;
8558 if(gameInfo.variant == VariantShogi && promoChar != NULLCHAR && promoChar != '=') {
8559 /* [HGM] Shogi promotions */
8560 board[toY][toX] = (ChessSquare) (PROMOTED piece);
8563 if((gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat)
8564 && promoChar != NULLCHAR && gameInfo.holdingsSize) {
8565 // [HGM] superchess: take promotion piece out of holdings
8566 int k = PieceToNumber(CharToPiece(ToUpper(promoChar)));
8567 if((int)piece < (int)BlackPawn) { // determine stm from piece color
8568 if(!--board[k][BOARD_WIDTH-2])
8569 board[k][BOARD_WIDTH-1] = EmptySquare;
8571 if(!--board[BOARD_HEIGHT-1-k][1])
8572 board[BOARD_HEIGHT-1-k][0] = EmptySquare;
8578 /* Updates forwardMostMove */
8580 MakeMove(fromX, fromY, toX, toY, promoChar)
8581 int fromX, fromY, toX, toY;
8584 // forwardMostMove++; // [HGM] bare: moved downstream
8586 if(serverMoves != NULL) { /* [HGM] write moves on file for broadcasting (should be separate routine, really) */
8587 int timeLeft; static int lastLoadFlag=0; int king, piece;
8588 piece = boards[forwardMostMove][fromY][fromX];
8589 king = piece < (int) BlackPawn ? WhiteKing : BlackKing;
8590 if(gameInfo.variant == VariantKnightmate)
8591 king += (int) WhiteUnicorn - (int) WhiteKing;
8592 if(forwardMostMove == 0) {
8594 fprintf(serverMoves, "%s;", second.tidy);
8595 fprintf(serverMoves, "%s;", first.tidy);
8596 if(!blackPlaysFirst)
8597 fprintf(serverMoves, "%s;", second.tidy);
8598 } else fprintf(serverMoves, loadFlag|lastLoadFlag ? ":" : ";");
8599 lastLoadFlag = loadFlag;
8601 fprintf(serverMoves, "%c%c:%c%c", AAA+fromX, ONE+fromY, AAA+toX, ONE+toY);
8602 // print castling suffix
8603 if( toY == fromY && piece == king ) {
8605 fprintf(serverMoves, ":%c%c:%c%c", AAA+BOARD_RGHT-1, ONE+fromY, AAA+toX-1,ONE+toY);
8607 fprintf(serverMoves, ":%c%c:%c%c", AAA+BOARD_LEFT, ONE+fromY, AAA+toX+1,ONE+toY);
8610 if( (boards[forwardMostMove][fromY][fromX] == WhitePawn ||
8611 boards[forwardMostMove][fromY][fromX] == BlackPawn ) &&
8612 boards[forwardMostMove][toY][toX] == EmptySquare
8613 && fromX != toX && fromY != toY)
8614 fprintf(serverMoves, ":%c%c:%c%c", AAA+fromX, ONE+fromY, AAA+toX, ONE+fromY);
8616 if(promoChar != NULLCHAR)
8617 fprintf(serverMoves, ":%c:%c%c", promoChar, AAA+toX, ONE+toY);
8619 fprintf(serverMoves, "/%d/%d",
8620 pvInfoList[forwardMostMove].depth, pvInfoList[forwardMostMove].score);
8621 if(forwardMostMove+1 & 1) timeLeft = whiteTimeRemaining/1000;
8622 else timeLeft = blackTimeRemaining/1000;
8623 fprintf(serverMoves, "/%d", timeLeft);
8625 fflush(serverMoves);
8628 if (forwardMostMove+1 > framePtr) { // [HGM] vari: do not run into saved variations
8629 DisplayFatalError(_("Game too long; increase MAX_MOVES and recompile"),
8633 UnLoadPV(); // [HGM] pv: if we are looking at a PV, abort this
8634 if (commentList[forwardMostMove+1] != NULL) {
8635 free(commentList[forwardMostMove+1]);
8636 commentList[forwardMostMove+1] = NULL;
8638 CopyBoard(boards[forwardMostMove+1], boards[forwardMostMove]);
8639 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[forwardMostMove+1]);
8640 // forwardMostMove++; // [HGM] bare: moved to after ApplyMove, to make sure clock interrupt finds complete board
8641 SwitchClocks(forwardMostMove+1); // [HGM] race: incrementing move nr inside
8642 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
8643 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
8644 gameInfo.result = GameUnfinished;
8645 if (gameInfo.resultDetails != NULL) {
8646 free(gameInfo.resultDetails);
8647 gameInfo.resultDetails = NULL;
8649 CoordsToComputerAlgebraic(fromY, fromX, toY, toX, promoChar,
8650 moveList[forwardMostMove - 1]);
8651 (void) CoordsToAlgebraic(boards[forwardMostMove - 1],
8652 PosFlags(forwardMostMove - 1),
8653 fromY, fromX, toY, toX, promoChar,
8654 parseList[forwardMostMove - 1]);
8655 switch (MateTest(boards[forwardMostMove], PosFlags(forwardMostMove)) ) {
8661 if(gameInfo.variant != VariantShogi)
8662 strcat(parseList[forwardMostMove - 1], "+");
8666 strcat(parseList[forwardMostMove - 1], "#");
8669 if (appData.debugMode) {
8670 fprintf(debugFP, "move: %s, parse: %s (%c)\n", moveList[forwardMostMove-1], parseList[forwardMostMove-1], moveList[forwardMostMove-1][4]);
8675 /* Updates currentMove if not pausing */
8677 ShowMove(fromX, fromY, toX, toY)
8679 int instant = (gameMode == PlayFromGameFile) ?
8680 (matchMode || (appData.timeDelay == 0 && !pausing)) : pausing;
8681 if(appData.noGUI) return;
8682 if (!pausing || gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
8684 if (forwardMostMove == currentMove + 1) {
8685 AnimateMove(boards[forwardMostMove - 1],
8686 fromX, fromY, toX, toY);
8688 if (appData.highlightLastMove) {
8689 SetHighlights(fromX, fromY, toX, toY);
8692 currentMove = forwardMostMove;
8695 if (instant) return;
8697 DisplayMove(currentMove - 1);
8698 DrawPosition(FALSE, boards[currentMove]);
8699 DisplayBothClocks();
8700 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
8703 void SendEgtPath(ChessProgramState *cps)
8704 { /* [HGM] EGT: match formats given in feature with those given by user, and send info for each match */
8705 char buf[MSG_SIZ], name[MSG_SIZ], *p;
8707 if((p = cps->egtFormats) == NULL || appData.egtFormats == NULL) return;
8710 char c, *q = name+1, *r, *s;
8712 name[0] = ','; // extract next format name from feature and copy with prefixed ','
8713 while(*p && *p != ',') *q++ = *p++;
8715 if( appData.defaultPathEGTB && appData.defaultPathEGTB[0] &&
8716 strcmp(name, ",nalimov:") == 0 ) {
8717 // take nalimov path from the menu-changeable option first, if it is defined
8718 sprintf(buf, "egtpath nalimov %s\n", appData.defaultPathEGTB);
8719 SendToProgram(buf,cps); // send egtbpath command for nalimov
8721 if( (s = StrStr(appData.egtFormats, name+1)) == appData.egtFormats ||
8722 (s = StrStr(appData.egtFormats, name)) != NULL) {
8723 // format name occurs amongst user-supplied formats, at beginning or immediately after comma
8724 s = r = StrStr(s, ":") + 1; // beginning of path info
8725 while(*r && *r != ',') r++; // path info is everything upto next ';' or end of string
8726 c = *r; *r = 0; // temporarily null-terminate path info
8727 *--q = 0; // strip of trailig ':' from name
8728 sprintf(buf, "egtpath %s %s\n", name+1, s);
8730 SendToProgram(buf,cps); // send egtbpath command for this format
8732 if(*p == ',') p++; // read away comma to position for next format name
8737 InitChessProgram(cps, setup)
8738 ChessProgramState *cps;
8739 int setup; /* [HGM] needed to setup FRC opening position */
8741 char buf[MSG_SIZ], b[MSG_SIZ]; int overruled;
8742 if (appData.noChessProgram) return;
8743 hintRequested = FALSE;
8744 bookRequested = FALSE;
8746 /* [HGM] some new WB protocol commands to configure engine are sent now, if engine supports them */
8747 /* moved to before sending initstring in 4.3.15, so Polyglot can delay UCI 'isready' to recepton of 'new' */
8748 if(cps->memSize) { /* [HGM] memory */
8749 sprintf(buf, "memory %d\n", appData.defaultHashSize + appData.defaultCacheSizeEGTB);
8750 SendToProgram(buf, cps);
8752 SendEgtPath(cps); /* [HGM] EGT */
8753 if(cps->maxCores) { /* [HGM] SMP: (protocol specified must be last settings command before new!) */
8754 sprintf(buf, "cores %d\n", appData.smpCores);
8755 SendToProgram(buf, cps);
8758 SendToProgram(cps->initString, cps);
8759 if (gameInfo.variant != VariantNormal &&
8760 gameInfo.variant != VariantLoadable
8761 /* [HGM] also send variant if board size non-standard */
8762 || gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0
8764 char *v = VariantName(gameInfo.variant);
8765 if (cps->protocolVersion != 1 && StrStr(cps->variants, v) == NULL) {
8766 /* [HGM] in protocol 1 we have to assume all variants valid */
8767 sprintf(buf, _("Variant %s not supported by %s"), v, cps->tidy);
8768 DisplayFatalError(buf, 0, 1);
8772 /* [HGM] make prefix for non-standard board size. Awkward testing... */
8773 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
8774 if( gameInfo.variant == VariantXiangqi )
8775 overruled = gameInfo.boardWidth != 9 || gameInfo.boardHeight != 10 || gameInfo.holdingsSize != 0;
8776 if( gameInfo.variant == VariantShogi )
8777 overruled = gameInfo.boardWidth != 9 || gameInfo.boardHeight != 9 || gameInfo.holdingsSize != 7;
8778 if( gameInfo.variant == VariantBughouse || gameInfo.variant == VariantCrazyhouse )
8779 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 5;
8780 if( gameInfo.variant == VariantCapablanca || gameInfo.variant == VariantCapaRandom ||
8781 gameInfo.variant == VariantGothic || gameInfo.variant == VariantFalcon )
8782 overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
8783 if( gameInfo.variant == VariantCourier )
8784 overruled = gameInfo.boardWidth != 12 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
8785 if( gameInfo.variant == VariantSuper )
8786 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 8;
8787 if( gameInfo.variant == VariantGreat )
8788 overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 8;
8791 sprintf(b, "%dx%d+%d_%s", gameInfo.boardWidth, gameInfo.boardHeight,
8792 gameInfo.holdingsSize, VariantName(gameInfo.variant)); // cook up sized variant name
8793 /* [HGM] varsize: try first if this defiant size variant is specifically known */
8794 if(StrStr(cps->variants, b) == NULL) {
8795 // specific sized variant not known, check if general sizing allowed
8796 if (cps->protocolVersion != 1) { // for protocol 1 we cannot check and hope for the best
8797 if(StrStr(cps->variants, "boardsize") == NULL) {
8798 sprintf(buf, "Board size %dx%d+%d not supported by %s",
8799 gameInfo.boardWidth, gameInfo.boardHeight, gameInfo.holdingsSize, cps->tidy);
8800 DisplayFatalError(buf, 0, 1);
8803 /* [HGM] here we really should compare with the maximum supported board size */
8806 } else sprintf(b, "%s", VariantName(gameInfo.variant));
8807 sprintf(buf, "variant %s\n", b);
8808 SendToProgram(buf, cps);
8810 currentlyInitializedVariant = gameInfo.variant;
8812 /* [HGM] send opening position in FRC to first engine */
8814 SendToProgram("force\n", cps);
8816 /* engine is now in force mode! Set flag to wake it up after first move. */
8817 setboardSpoiledMachineBlack = 1;
8821 snprintf(buf, sizeof(buf), "ics %s\n", appData.icsActive ? appData.icsHost : "-");
8822 SendToProgram(buf, cps);
8824 cps->maybeThinking = FALSE;
8825 cps->offeredDraw = 0;
8826 if (!appData.icsActive) {
8827 SendTimeControl(cps, movesPerSession, timeControl,
8828 timeIncrement, appData.searchDepth,
8831 if (appData.showThinking
8832 // [HGM] thinking: four options require thinking output to be sent
8833 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp()
8835 SendToProgram("post\n", cps);
8837 SendToProgram("hard\n", cps);
8838 if (!appData.ponderNextMove) {
8839 /* Warning: "easy" is a toggle in GNU Chess, so don't send
8840 it without being sure what state we are in first. "hard"
8841 is not a toggle, so that one is OK.
8843 SendToProgram("easy\n", cps);
8846 sprintf(buf, "ping %d\n", ++cps->lastPing);
8847 SendToProgram(buf, cps);
8849 cps->initDone = TRUE;
8854 StartChessProgram(cps)
8855 ChessProgramState *cps;
8860 if (appData.noChessProgram) return;
8861 cps->initDone = FALSE;
8863 if (strcmp(cps->host, "localhost") == 0) {
8864 err = StartChildProcess(cps->program, cps->dir, &cps->pr);
8865 } else if (*appData.remoteShell == NULLCHAR) {
8866 err = OpenRcmd(cps->host, appData.remoteUser, cps->program, &cps->pr);
8868 if (*appData.remoteUser == NULLCHAR) {
8869 snprintf(buf, sizeof(buf), "%s %s %s", appData.remoteShell, cps->host,
8872 snprintf(buf, sizeof(buf), "%s %s -l %s %s", appData.remoteShell,
8873 cps->host, appData.remoteUser, cps->program);
8875 err = StartChildProcess(buf, "", &cps->pr);
8879 sprintf(buf, _("Startup failure on '%s'"), cps->program);
8880 DisplayFatalError(buf, err, 1);
8886 cps->isr = AddInputSource(cps->pr, TRUE, ReceiveFromProgram, cps);
8887 if (cps->protocolVersion > 1) {
8888 sprintf(buf, "xboard\nprotover %d\n", cps->protocolVersion);
8889 cps->nrOptions = 0; // [HGM] options: clear all engine-specific options
8890 cps->comboCnt = 0; // and values of combo boxes
8891 SendToProgram(buf, cps);
8893 SendToProgram("xboard\n", cps);
8899 TwoMachinesEventIfReady P((void))
8901 if (first.lastPing != first.lastPong) {
8902 DisplayMessage("", _("Waiting for first chess program"));
8903 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10); // [HGM] fast: lowered from 1000
8906 if (second.lastPing != second.lastPong) {
8907 DisplayMessage("", _("Waiting for second chess program"));
8908 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10); // [HGM] fast: lowered from 1000
8916 NextMatchGame P((void))
8918 int index; /* [HGM] autoinc: step load index during match */
8920 if (*appData.loadGameFile != NULLCHAR) {
8921 index = appData.loadGameIndex;
8922 if(index < 0) { // [HGM] autoinc
8923 lastIndex = index = (index == -2 && first.twoMachinesColor[0] == 'b') ? lastIndex : lastIndex+1;
8924 if(appData.rewindIndex > 0 && index > appData.rewindIndex) lastIndex = index = 1;
8926 LoadGameFromFile(appData.loadGameFile,
8928 appData.loadGameFile, FALSE);
8929 } else if (*appData.loadPositionFile != NULLCHAR) {
8930 index = appData.loadPositionIndex;
8931 if(index < 0) { // [HGM] autoinc
8932 lastIndex = index = (index == -2 && first.twoMachinesColor[0] == 'b') ? lastIndex : lastIndex+1;
8933 if(appData.rewindIndex > 0 && index > appData.rewindIndex) lastIndex = index = 1;
8935 LoadPositionFromFile(appData.loadPositionFile,
8937 appData.loadPositionFile);
8939 TwoMachinesEventIfReady();
8942 void UserAdjudicationEvent( int result )
8944 ChessMove gameResult = GameIsDrawn;
8947 gameResult = WhiteWins;
8949 else if( result < 0 ) {
8950 gameResult = BlackWins;
8953 if( gameMode == TwoMachinesPlay ) {
8954 GameEnds( gameResult, "User adjudication", GE_XBOARD );
8959 // [HGM] save: calculate checksum of game to make games easily identifiable
8960 int StringCheckSum(char *s)
8963 if(s==NULL) return 0;
8964 while(*s) i = i*259 + *s++;
8971 for(i=backwardMostMove; i<forwardMostMove; i++) {
8972 sum += pvInfoList[i].depth;
8973 sum += StringCheckSum(parseList[i]);
8974 sum += StringCheckSum(commentList[i]);
8977 if(i>1 && sum==0) sum++; // make sure never zero for non-empty game
8978 return sum + StringCheckSum(commentList[i]);
8979 } // end of save patch
8982 GameEnds(result, resultDetails, whosays)
8984 char *resultDetails;
8987 GameMode nextGameMode;
8989 char buf[MSG_SIZ], popupRequested = 0;
8991 if(endingGame) return; /* [HGM] crash: forbid recursion */
8993 if(twoBoards) { // [HGM] dual: switch back to one board
8994 twoBoards = partnerUp = 0; InitDrawingSizes(-2, 0);
8995 DrawPosition(TRUE, partnerBoard); // observed game becomes foreground
8997 if (appData.debugMode) {
8998 fprintf(debugFP, "GameEnds(%d, %s, %d)\n",
8999 result, resultDetails ? resultDetails : "(null)", whosays);
9002 fromX = fromY = -1; // [HGM] abort any move the user is entering.
9004 if (appData.icsActive && (whosays == GE_ENGINE || whosays >= GE_ENGINE1)) {
9005 /* If we are playing on ICS, the server decides when the
9006 game is over, but the engine can offer to draw, claim
9010 if (appData.zippyPlay && first.initDone) {
9011 if (result == GameIsDrawn) {
9012 /* In case draw still needs to be claimed */
9013 SendToICS(ics_prefix);
9014 SendToICS("draw\n");
9015 } else if (StrCaseStr(resultDetails, "resign")) {
9016 SendToICS(ics_prefix);
9017 SendToICS("resign\n");
9021 endingGame = 0; /* [HGM] crash */
9025 /* If we're loading the game from a file, stop */
9026 if (whosays == GE_FILE) {
9027 (void) StopLoadGameTimer();
9031 /* Cancel draw offers */
9032 first.offeredDraw = second.offeredDraw = 0;
9034 /* If this is an ICS game, only ICS can really say it's done;
9035 if not, anyone can. */
9036 isIcsGame = (gameMode == IcsPlayingWhite ||
9037 gameMode == IcsPlayingBlack ||
9038 gameMode == IcsObserving ||
9039 gameMode == IcsExamining);
9041 if (!isIcsGame || whosays == GE_ICS) {
9042 /* OK -- not an ICS game, or ICS said it was done */
9044 if (!isIcsGame && !appData.noChessProgram)
9045 SetUserThinkingEnables();
9047 /* [HGM] if a machine claims the game end we verify this claim */
9048 if(gameMode == TwoMachinesPlay && appData.testClaims) {
9049 if(appData.testLegality && whosays >= GE_ENGINE1 ) {
9051 ChessMove trueResult = (ChessMove) -1;
9053 claimer = whosays == GE_ENGINE1 ? /* color of claimer */
9054 first.twoMachinesColor[0] :
9055 second.twoMachinesColor[0] ;
9057 // [HGM] losers: because the logic is becoming a bit hairy, determine true result first
9058 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_CHECKMATE) {
9059 /* [HGM] verify: engine mate claims accepted if they were flagged */
9060 trueResult = WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins;
9062 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_WINS) { // added code for games where being mated is a win
9063 /* [HGM] verify: engine mate claims accepted if they were flagged */
9064 trueResult = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
9066 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_STALEMATE) { // only used to indicate draws now
9067 trueResult = GameIsDrawn; // default; in variants where stalemate loses, Status is CHECKMATE
9070 // now verify win claims, but not in drop games, as we don't understand those yet
9071 if( (gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper
9072 || gameInfo.variant == VariantGreat) &&
9073 (result == WhiteWins && claimer == 'w' ||
9074 result == BlackWins && claimer == 'b' ) ) { // case to verify: engine claims own win
9075 if (appData.debugMode) {
9076 fprintf(debugFP, "result=%d sp=%d move=%d\n",
9077 result, (signed char)boards[forwardMostMove][EP_STATUS], forwardMostMove);
9079 if(result != trueResult) {
9080 sprintf(buf, "False win claim: '%s'", resultDetails);
9081 result = claimer == 'w' ? BlackWins : WhiteWins;
9082 resultDetails = buf;
9085 if( result == GameIsDrawn && (signed char)boards[forwardMostMove][EP_STATUS] > EP_DRAWS
9086 && (forwardMostMove <= backwardMostMove ||
9087 (signed char)boards[forwardMostMove-1][EP_STATUS] > EP_DRAWS ||
9088 (claimer=='b')==(forwardMostMove&1))
9090 /* [HGM] verify: draws that were not flagged are false claims */
9091 sprintf(buf, "False draw claim: '%s'", resultDetails);
9092 result = claimer == 'w' ? BlackWins : WhiteWins;
9093 resultDetails = buf;
9095 /* (Claiming a loss is accepted no questions asked!) */
9097 /* [HGM] bare: don't allow bare King to win */
9098 if((gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat)
9099 && gameInfo.variant != VariantLosers && gameInfo.variant != VariantGiveaway
9100 && gameInfo.variant != VariantSuicide // [HGM] losers: except in losers, of course...
9101 && result != GameIsDrawn)
9102 { int i, j, k=0, color = (result==WhiteWins ? (int)WhitePawn : (int)BlackPawn);
9103 for(j=BOARD_LEFT; j<BOARD_RGHT; j++) for(i=0; i<BOARD_HEIGHT; i++) {
9104 int p = (signed char)boards[forwardMostMove][i][j] - color;
9105 if(p >= 0 && p <= (int)WhiteKing) k++;
9107 if (appData.debugMode) {
9108 fprintf(debugFP, "GE(%d, %s, %d) bare king k=%d color=%d\n",
9109 result, resultDetails ? resultDetails : "(null)", whosays, k, color);
9112 result = GameIsDrawn;
9113 sprintf(buf, "%s but bare king", resultDetails);
9114 resultDetails = buf;
9120 if(serverMoves != NULL && !loadFlag) { char c = '=';
9121 if(result==WhiteWins) c = '+';
9122 if(result==BlackWins) c = '-';
9123 if(resultDetails != NULL)
9124 fprintf(serverMoves, ";%c;%s\n", c, resultDetails);
9126 if (resultDetails != NULL) {
9127 gameInfo.result = result;
9128 gameInfo.resultDetails = StrSave(resultDetails);
9130 /* display last move only if game was not loaded from file */
9131 if ((whosays != GE_FILE) && (currentMove == forwardMostMove))
9132 DisplayMove(currentMove - 1);
9134 if (forwardMostMove != 0) {
9135 if (gameMode != PlayFromGameFile && gameMode != EditGame
9136 && lastSavedGame != GameCheckSum() // [HGM] save: suppress duplicates
9138 if (*appData.saveGameFile != NULLCHAR) {
9139 SaveGameToFile(appData.saveGameFile, TRUE);
9140 } else if (appData.autoSaveGames) {
9143 if (*appData.savePositionFile != NULLCHAR) {
9144 SavePositionToFile(appData.savePositionFile);
9149 /* Tell program how game ended in case it is learning */
9150 /* [HGM] Moved this to after saving the PGN, just in case */
9151 /* engine died and we got here through time loss. In that */
9152 /* case we will get a fatal error writing the pipe, which */
9153 /* would otherwise lose us the PGN. */
9154 /* [HGM] crash: not needed anymore, but doesn't hurt; */
9155 /* output during GameEnds should never be fatal anymore */
9156 if (gameMode == MachinePlaysWhite ||
9157 gameMode == MachinePlaysBlack ||
9158 gameMode == TwoMachinesPlay ||
9159 gameMode == IcsPlayingWhite ||
9160 gameMode == IcsPlayingBlack ||
9161 gameMode == BeginningOfGame) {
9163 sprintf(buf, "result %s {%s}\n", PGNResult(result),
9165 if (first.pr != NoProc) {
9166 SendToProgram(buf, &first);
9168 if (second.pr != NoProc &&
9169 gameMode == TwoMachinesPlay) {
9170 SendToProgram(buf, &second);
9175 if (appData.icsActive) {
9176 if (appData.quietPlay &&
9177 (gameMode == IcsPlayingWhite ||
9178 gameMode == IcsPlayingBlack)) {
9179 SendToICS(ics_prefix);
9180 SendToICS("set shout 1\n");
9182 nextGameMode = IcsIdle;
9183 ics_user_moved = FALSE;
9184 /* clean up premove. It's ugly when the game has ended and the
9185 * premove highlights are still on the board.
9189 ClearPremoveHighlights();
9190 DrawPosition(FALSE, boards[currentMove]);
9192 if (whosays == GE_ICS) {
9195 if (gameMode == IcsPlayingWhite)
9197 else if(gameMode == IcsPlayingBlack)
9201 if (gameMode == IcsPlayingBlack)
9203 else if(gameMode == IcsPlayingWhite)
9210 PlayIcsUnfinishedSound();
9213 } else if (gameMode == EditGame ||
9214 gameMode == PlayFromGameFile ||
9215 gameMode == AnalyzeMode ||
9216 gameMode == AnalyzeFile) {
9217 nextGameMode = gameMode;
9219 nextGameMode = EndOfGame;
9224 nextGameMode = gameMode;
9227 if (appData.noChessProgram) {
9228 gameMode = nextGameMode;
9230 endingGame = 0; /* [HGM] crash */
9235 /* Put first chess program into idle state */
9236 if (first.pr != NoProc &&
9237 (gameMode == MachinePlaysWhite ||
9238 gameMode == MachinePlaysBlack ||
9239 gameMode == TwoMachinesPlay ||
9240 gameMode == IcsPlayingWhite ||
9241 gameMode == IcsPlayingBlack ||
9242 gameMode == BeginningOfGame)) {
9243 SendToProgram("force\n", &first);
9244 if (first.usePing) {
9246 sprintf(buf, "ping %d\n", ++first.lastPing);
9247 SendToProgram(buf, &first);
9250 } else if (result != GameUnfinished || nextGameMode == IcsIdle) {
9251 /* Kill off first chess program */
9252 if (first.isr != NULL)
9253 RemoveInputSource(first.isr);
9256 if (first.pr != NoProc) {
9258 DoSleep( appData.delayBeforeQuit );
9259 SendToProgram("quit\n", &first);
9260 DoSleep( appData.delayAfterQuit );
9261 DestroyChildProcess(first.pr, first.useSigterm);
9266 /* Put second chess program into idle state */
9267 if (second.pr != NoProc &&
9268 gameMode == TwoMachinesPlay) {
9269 SendToProgram("force\n", &second);
9270 if (second.usePing) {
9272 sprintf(buf, "ping %d\n", ++second.lastPing);
9273 SendToProgram(buf, &second);
9276 } else if (result != GameUnfinished || nextGameMode == IcsIdle) {
9277 /* Kill off second chess program */
9278 if (second.isr != NULL)
9279 RemoveInputSource(second.isr);
9282 if (second.pr != NoProc) {
9283 DoSleep( appData.delayBeforeQuit );
9284 SendToProgram("quit\n", &second);
9285 DoSleep( appData.delayAfterQuit );
9286 DestroyChildProcess(second.pr, second.useSigterm);
9291 if (matchMode && gameMode == TwoMachinesPlay) {
9294 if (first.twoMachinesColor[0] == 'w') {
9301 if (first.twoMachinesColor[0] == 'b') {
9310 if (matchGame < appData.matchGames) {
9312 if(appData.sameColorGames <= 1) { /* [HGM] alternate: suppress color swap */
9313 tmp = first.twoMachinesColor;
9314 first.twoMachinesColor = second.twoMachinesColor;
9315 second.twoMachinesColor = tmp;
9317 gameMode = nextGameMode;
9319 if(appData.matchPause>10000 || appData.matchPause<10)
9320 appData.matchPause = 10000; /* [HGM] make pause adjustable */
9321 ScheduleDelayedEvent(NextMatchGame, appData.matchPause);
9322 endingGame = 0; /* [HGM] crash */
9325 gameMode = nextGameMode;
9326 sprintf(buf, _("Match %s vs. %s: final score %d-%d-%d"),
9327 first.tidy, second.tidy,
9328 first.matchWins, second.matchWins,
9329 appData.matchGames - (first.matchWins + second.matchWins));
9330 popupRequested++; // [HGM] crash: postpone to after resetting endingGame
9333 if ((gameMode == AnalyzeMode || gameMode == AnalyzeFile) &&
9334 !(nextGameMode == AnalyzeMode || nextGameMode == AnalyzeFile))
9336 gameMode = nextGameMode;
9338 endingGame = 0; /* [HGM] crash */
9339 if(popupRequested) { // [HGM] crash: this calls GameEnds recursively through ExitEvent! Make it a harmless tail recursion.
9340 if(matchMode == TRUE) DisplayFatalError(buf, 0, 0); else {
9341 matchMode = FALSE; appData.matchGames = matchGame = 0;
9347 /* Assumes program was just initialized (initString sent).
9348 Leaves program in force mode. */
9350 FeedMovesToProgram(cps, upto)
9351 ChessProgramState *cps;
9356 if (appData.debugMode)
9357 fprintf(debugFP, "Feeding %smoves %d through %d to %s chess program\n",
9358 startedFromSetupPosition ? "position and " : "",
9359 backwardMostMove, upto, cps->which);
9360 if(currentlyInitializedVariant != gameInfo.variant) { char buf[MSG_SIZ];
9361 // [HGM] variantswitch: make engine aware of new variant
9362 if(cps->protocolVersion > 1 && StrStr(cps->variants, VariantName(gameInfo.variant)) == NULL)
9363 return; // [HGM] refrain from feeding moves altogether if variant is unsupported!
9364 sprintf(buf, "variant %s\n", VariantName(gameInfo.variant));
9365 SendToProgram(buf, cps);
9366 currentlyInitializedVariant = gameInfo.variant;
9368 SendToProgram("force\n", cps);
9369 if (startedFromSetupPosition) {
9370 SendBoard(cps, backwardMostMove);
9371 if (appData.debugMode) {
9372 fprintf(debugFP, "feedMoves\n");
9375 for (i = backwardMostMove; i < upto; i++) {
9376 SendMoveToProgram(i, cps);
9382 ResurrectChessProgram()
9384 /* The chess program may have exited.
9385 If so, restart it and feed it all the moves made so far. */
9387 if (appData.noChessProgram || first.pr != NoProc) return;
9389 StartChessProgram(&first);
9390 InitChessProgram(&first, FALSE);
9391 FeedMovesToProgram(&first, currentMove);
9393 if (!first.sendTime) {
9394 /* can't tell gnuchess what its clock should read,
9395 so we bow to its notion. */
9397 timeRemaining[0][currentMove] = whiteTimeRemaining;
9398 timeRemaining[1][currentMove] = blackTimeRemaining;
9401 if ((gameMode == AnalyzeMode || gameMode == AnalyzeFile ||
9402 appData.icsEngineAnalyze) && first.analysisSupport) {
9403 SendToProgram("analyze\n", &first);
9404 first.analyzing = TRUE;
9417 if (appData.debugMode) {
9418 fprintf(debugFP, "Reset(%d, %d) from gameMode %d\n",
9419 redraw, init, gameMode);
9421 CleanupTail(); // [HGM] vari: delete any stored variations
9422 pausing = pauseExamInvalid = FALSE;
9423 startedFromSetupPosition = blackPlaysFirst = FALSE;
9425 whiteFlag = blackFlag = FALSE;
9426 userOfferedDraw = FALSE;
9427 hintRequested = bookRequested = FALSE;
9428 first.maybeThinking = FALSE;
9429 second.maybeThinking = FALSE;
9430 first.bookSuspend = FALSE; // [HGM] book
9431 second.bookSuspend = FALSE;
9432 thinkOutput[0] = NULLCHAR;
9433 lastHint[0] = NULLCHAR;
9434 ClearGameInfo(&gameInfo);
9435 gameInfo.variant = StringToVariant(appData.variant);
9436 ics_user_moved = ics_clock_paused = FALSE;
9437 ics_getting_history = H_FALSE;
9439 white_holding[0] = black_holding[0] = NULLCHAR;
9440 ClearProgramStats();
9441 opponentKibitzes = FALSE; // [HGM] kibitz: do not reserve space in engine-output window in zippy mode
9445 flipView = appData.flipView;
9446 ClearPremoveHighlights();
9448 alarmSounded = FALSE;
9450 GameEnds((ChessMove) 0, NULL, GE_PLAYER);
9451 if(appData.serverMovesName != NULL) {
9452 /* [HGM] prepare to make moves file for broadcasting */
9453 clock_t t = clock();
9454 if(serverMoves != NULL) fclose(serverMoves);
9455 serverMoves = fopen(appData.serverMovesName, "r");
9456 if(serverMoves != NULL) {
9457 fclose(serverMoves);
9458 /* delay 15 sec before overwriting, so all clients can see end */
9459 while(clock()-t < appData.serverPause*CLOCKS_PER_SEC);
9461 serverMoves = fopen(appData.serverMovesName, "w");
9465 gameMode = BeginningOfGame;
9467 if(appData.icsActive) gameInfo.variant = VariantNormal;
9468 currentMove = forwardMostMove = backwardMostMove = 0;
9469 InitPosition(redraw);
9470 for (i = 0; i < MAX_MOVES; i++) {
9471 if (commentList[i] != NULL) {
9472 free(commentList[i]);
9473 commentList[i] = NULL;
9477 timeRemaining[0][0] = whiteTimeRemaining;
9478 timeRemaining[1][0] = blackTimeRemaining;
9479 if (first.pr == NULL) {
9480 StartChessProgram(&first);
9483 InitChessProgram(&first, startedFromSetupPosition);
9486 DisplayMessage("", "");
9487 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
9488 lastSavedGame = 0; // [HGM] save: make sure next game counts as unsaved
9495 if (!AutoPlayOneMove())
9497 if (matchMode || appData.timeDelay == 0)
9499 if (appData.timeDelay < 0 || gameMode == AnalyzeFile)
9501 StartLoadGameTimer((long)(1000.0 * appData.timeDelay));
9510 int fromX, fromY, toX, toY;
9512 if (appData.debugMode) {
9513 fprintf(debugFP, "AutoPlayOneMove(): current %d\n", currentMove);
9516 if (gameMode != PlayFromGameFile)
9519 if (currentMove >= forwardMostMove) {
9520 gameMode = EditGame;
9523 /* [AS] Clear current move marker at the end of a game */
9524 /* HistorySet(parseList, backwardMostMove, forwardMostMove, -1); */
9529 toX = moveList[currentMove][2] - AAA;
9530 toY = moveList[currentMove][3] - ONE;
9532 if (moveList[currentMove][1] == '@') {
9533 if (appData.highlightLastMove) {
9534 SetHighlights(-1, -1, toX, toY);
9537 fromX = moveList[currentMove][0] - AAA;
9538 fromY = moveList[currentMove][1] - ONE;
9540 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove); /* [AS] */
9542 AnimateMove(boards[currentMove], fromX, fromY, toX, toY);
9544 if (appData.highlightLastMove) {
9545 SetHighlights(fromX, fromY, toX, toY);
9548 DisplayMove(currentMove);
9549 SendMoveToProgram(currentMove++, &first);
9550 DisplayBothClocks();
9551 DrawPosition(FALSE, boards[currentMove]);
9552 // [HGM] PV info: always display, routine tests if empty
9553 DisplayComment(currentMove - 1, commentList[currentMove]);
9559 LoadGameOneMove(readAhead)
9560 ChessMove readAhead;
9562 int fromX = 0, fromY = 0, toX = 0, toY = 0, done;
9563 char promoChar = NULLCHAR;
9568 if (gameMode != PlayFromGameFile && gameMode != AnalyzeFile &&
9569 gameMode != AnalyzeMode && gameMode != Training) {
9574 yyboardindex = forwardMostMove;
9575 if (readAhead != (ChessMove)0) {
9576 moveType = readAhead;
9578 if (gameFileFP == NULL)
9580 moveType = (ChessMove) yylex();
9586 if (appData.debugMode)
9587 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
9590 /* append the comment but don't display it */
9591 AppendComment(currentMove, p, FALSE);
9594 case WhiteCapturesEnPassant:
9595 case BlackCapturesEnPassant:
9596 case WhitePromotionChancellor:
9597 case BlackPromotionChancellor:
9598 case WhitePromotionArchbishop:
9599 case BlackPromotionArchbishop:
9600 case WhitePromotionCentaur:
9601 case BlackPromotionCentaur:
9602 case WhitePromotionQueen:
9603 case BlackPromotionQueen:
9604 case WhitePromotionRook:
9605 case BlackPromotionRook:
9606 case WhitePromotionBishop:
9607 case BlackPromotionBishop:
9608 case WhitePromotionKnight:
9609 case BlackPromotionKnight:
9610 case WhitePromotionKing:
9611 case BlackPromotionKing:
9612 case WhiteNonPromotion:
9613 case BlackNonPromotion:
9615 case WhiteKingSideCastle:
9616 case WhiteQueenSideCastle:
9617 case BlackKingSideCastle:
9618 case BlackQueenSideCastle:
9619 case WhiteKingSideCastleWild:
9620 case WhiteQueenSideCastleWild:
9621 case BlackKingSideCastleWild:
9622 case BlackQueenSideCastleWild:
9624 case WhiteHSideCastleFR:
9625 case WhiteASideCastleFR:
9626 case BlackHSideCastleFR:
9627 case BlackASideCastleFR:
9629 if (appData.debugMode)
9630 fprintf(debugFP, "Parsed %s into %s\n", yy_text, currentMoveString);
9631 fromX = currentMoveString[0] - AAA;
9632 fromY = currentMoveString[1] - ONE;
9633 toX = currentMoveString[2] - AAA;
9634 toY = currentMoveString[3] - ONE;
9635 promoChar = currentMoveString[4];
9640 if (appData.debugMode)
9641 fprintf(debugFP, "Parsed %s into %s\n", yy_text, currentMoveString);
9642 fromX = moveType == WhiteDrop ?
9643 (int) CharToPiece(ToUpper(currentMoveString[0])) :
9644 (int) CharToPiece(ToLower(currentMoveString[0]));
9646 toX = currentMoveString[2] - AAA;
9647 toY = currentMoveString[3] - ONE;
9653 case GameUnfinished:
9654 if (appData.debugMode)
9655 fprintf(debugFP, "Parsed game end: %s\n", yy_text);
9656 p = strchr(yy_text, '{');
9657 if (p == NULL) p = strchr(yy_text, '(');
9660 if (p[0] == '0' || p[0] == '1' || p[0] == '*') p = "";
9662 q = strchr(p, *p == '{' ? '}' : ')');
9663 if (q != NULL) *q = NULLCHAR;
9666 GameEnds(moveType, p, GE_FILE);
9668 if (cmailMsgLoaded) {
9670 flipView = WhiteOnMove(currentMove);
9671 if (moveType == GameUnfinished) flipView = !flipView;
9672 if (appData.debugMode)
9673 fprintf(debugFP, "Setting flipView to %d\n", flipView) ;
9677 case (ChessMove) 0: /* end of file */
9678 if (appData.debugMode)
9679 fprintf(debugFP, "Parser hit end of file\n");
9680 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
9686 if (WhiteOnMove(currentMove)) {
9687 GameEnds(BlackWins, "Black mates", GE_FILE);
9689 GameEnds(WhiteWins, "White mates", GE_FILE);
9693 GameEnds(GameIsDrawn, "Stalemate", GE_FILE);
9700 if (lastLoadGameStart == GNUChessGame) {
9701 /* GNUChessGames have numbers, but they aren't move numbers */
9702 if (appData.debugMode)
9703 fprintf(debugFP, "Parser ignoring: '%s' (%d)\n",
9704 yy_text, (int) moveType);
9705 return LoadGameOneMove((ChessMove)0); /* tail recursion */
9707 /* else fall thru */
9712 /* Reached start of next game in file */
9713 if (appData.debugMode)
9714 fprintf(debugFP, "Parsed start of next game: %s\n", yy_text);
9715 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
9721 if (WhiteOnMove(currentMove)) {
9722 GameEnds(BlackWins, "Black mates", GE_FILE);
9724 GameEnds(WhiteWins, "White mates", GE_FILE);
9728 GameEnds(GameIsDrawn, "Stalemate", GE_FILE);
9734 case PositionDiagram: /* should not happen; ignore */
9735 case ElapsedTime: /* ignore */
9736 case NAG: /* ignore */
9737 if (appData.debugMode)
9738 fprintf(debugFP, "Parser ignoring: '%s' (%d)\n",
9739 yy_text, (int) moveType);
9740 return LoadGameOneMove((ChessMove)0); /* tail recursion */
9743 if (appData.testLegality) {
9744 if (appData.debugMode)
9745 fprintf(debugFP, "Parsed IllegalMove: %s\n", yy_text);
9746 sprintf(move, _("Illegal move: %d.%s%s"),
9747 (forwardMostMove / 2) + 1,
9748 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
9749 DisplayError(move, 0);
9752 if (appData.debugMode)
9753 fprintf(debugFP, "Parsed %s into IllegalMove %s\n",
9754 yy_text, currentMoveString);
9755 fromX = currentMoveString[0] - AAA;
9756 fromY = currentMoveString[1] - ONE;
9757 toX = currentMoveString[2] - AAA;
9758 toY = currentMoveString[3] - ONE;
9759 promoChar = currentMoveString[4];
9764 if (appData.debugMode)
9765 fprintf(debugFP, "Parsed AmbiguousMove: %s\n", yy_text);
9766 sprintf(move, _("Ambiguous move: %d.%s%s"),
9767 (forwardMostMove / 2) + 1,
9768 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
9769 DisplayError(move, 0);
9774 case ImpossibleMove:
9775 if (appData.debugMode)
9776 fprintf(debugFP, "Parsed ImpossibleMove (type = %d): %s\n", moveType, yy_text);
9777 sprintf(move, _("Illegal move: %d.%s%s"),
9778 (forwardMostMove / 2) + 1,
9779 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
9780 DisplayError(move, 0);
9786 if (appData.matchMode || (appData.timeDelay == 0 && !pausing)) {
9787 DrawPosition(FALSE, boards[currentMove]);
9788 DisplayBothClocks();
9789 if (!appData.matchMode) // [HGM] PV info: routine tests if empty
9790 DisplayComment(currentMove - 1, commentList[currentMove]);
9792 (void) StopLoadGameTimer();
9794 cmailOldMove = forwardMostMove;
9797 /* currentMoveString is set as a side-effect of yylex */
9798 strcat(currentMoveString, "\n");
9799 strcpy(moveList[forwardMostMove], currentMoveString);
9801 thinkOutput[0] = NULLCHAR;
9802 MakeMove(fromX, fromY, toX, toY, promoChar);
9803 currentMove = forwardMostMove;
9808 /* Load the nth game from the given file */
9810 LoadGameFromFile(filename, n, title, useList)
9814 /*Boolean*/ int useList;
9819 if (strcmp(filename, "-") == 0) {
9823 f = fopen(filename, "rb");
9825 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
9826 DisplayError(buf, errno);
9830 if (fseek(f, 0, 0) == -1) {
9831 /* f is not seekable; probably a pipe */
9834 if (useList && n == 0) {
9835 int error = GameListBuild(f);
9837 DisplayError(_("Cannot build game list"), error);
9838 } else if (!ListEmpty(&gameList) &&
9839 ((ListGame *) gameList.tailPred)->number > 1) {
9840 GameListPopUp(f, title);
9847 return LoadGame(f, n, title, FALSE);
9852 MakeRegisteredMove()
9854 int fromX, fromY, toX, toY;
9856 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
9857 switch (cmailMoveType[lastLoadGameNumber - 1]) {
9860 if (appData.debugMode)
9861 fprintf(debugFP, "Restoring %s for game %d\n",
9862 cmailMove[lastLoadGameNumber - 1], lastLoadGameNumber);
9864 thinkOutput[0] = NULLCHAR;
9865 strcpy(moveList[currentMove], cmailMove[lastLoadGameNumber - 1]);
9866 fromX = cmailMove[lastLoadGameNumber - 1][0] - AAA;
9867 fromY = cmailMove[lastLoadGameNumber - 1][1] - ONE;
9868 toX = cmailMove[lastLoadGameNumber - 1][2] - AAA;
9869 toY = cmailMove[lastLoadGameNumber - 1][3] - ONE;
9870 promoChar = cmailMove[lastLoadGameNumber - 1][4];
9871 MakeMove(fromX, fromY, toX, toY, promoChar);
9872 ShowMove(fromX, fromY, toX, toY);
9874 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
9881 if (WhiteOnMove(currentMove)) {
9882 GameEnds(BlackWins, "Black mates", GE_PLAYER);
9884 GameEnds(WhiteWins, "White mates", GE_PLAYER);
9889 GameEnds(GameIsDrawn, "Stalemate", GE_PLAYER);
9896 if (WhiteOnMove(currentMove)) {
9897 GameEnds(BlackWins, "White resigns", GE_PLAYER);
9899 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
9904 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
9915 /* Wrapper around LoadGame for use when a Cmail message is loaded */
9917 CmailLoadGame(f, gameNumber, title, useList)
9925 if (gameNumber > nCmailGames) {
9926 DisplayError(_("No more games in this message"), 0);
9929 if (f == lastLoadGameFP) {
9930 int offset = gameNumber - lastLoadGameNumber;
9932 cmailMsg[0] = NULLCHAR;
9933 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
9934 cmailMoveRegistered[lastLoadGameNumber - 1] = FALSE;
9935 nCmailMovesRegistered--;
9937 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
9938 if (cmailResult[lastLoadGameNumber - 1] == CMAIL_NEW_RESULT) {
9939 cmailResult[lastLoadGameNumber - 1] = CMAIL_NOT_RESULT;
9942 if (! RegisterMove()) return FALSE;
9946 retVal = LoadGame(f, gameNumber, title, useList);
9948 /* Make move registered during previous look at this game, if any */
9949 MakeRegisteredMove();
9951 if (cmailCommentList[lastLoadGameNumber - 1] != NULL) {
9952 commentList[currentMove]
9953 = StrSave(cmailCommentList[lastLoadGameNumber - 1]);
9954 DisplayComment(currentMove - 1, commentList[currentMove]);
9960 /* Support for LoadNextGame, LoadPreviousGame, ReloadSameGame */
9965 int gameNumber = lastLoadGameNumber + offset;
9966 if (lastLoadGameFP == NULL) {
9967 DisplayError(_("No game has been loaded yet"), 0);
9970 if (gameNumber <= 0) {
9971 DisplayError(_("Can't back up any further"), 0);
9974 if (cmailMsgLoaded) {
9975 return CmailLoadGame(lastLoadGameFP, gameNumber,
9976 lastLoadGameTitle, lastLoadGameUseList);
9978 return LoadGame(lastLoadGameFP, gameNumber,
9979 lastLoadGameTitle, lastLoadGameUseList);
9985 /* Load the nth game from open file f */
9987 LoadGame(f, gameNumber, title, useList)
9995 int gn = gameNumber;
9996 ListGame *lg = NULL;
9999 GameMode oldGameMode;
10000 VariantClass oldVariant = gameInfo.variant; /* [HGM] PGNvariant */
10002 if (appData.debugMode)
10003 fprintf(debugFP, "LoadGame(): on entry, gameMode %d\n", gameMode);
10005 if (gameMode == Training )
10006 SetTrainingModeOff();
10008 oldGameMode = gameMode;
10009 if (gameMode != BeginningOfGame) {
10010 Reset(FALSE, TRUE);
10014 if (lastLoadGameFP != NULL && lastLoadGameFP != f) {
10015 fclose(lastLoadGameFP);
10019 lg = (ListGame *) ListElem(&gameList, gameNumber-1);
10022 fseek(f, lg->offset, 0);
10023 GameListHighlight(gameNumber);
10027 DisplayError(_("Game number out of range"), 0);
10032 if (fseek(f, 0, 0) == -1) {
10033 if (f == lastLoadGameFP ?
10034 gameNumber == lastLoadGameNumber + 1 :
10038 DisplayError(_("Can't seek on game file"), 0);
10043 lastLoadGameFP = f;
10044 lastLoadGameNumber = gameNumber;
10045 strcpy(lastLoadGameTitle, title);
10046 lastLoadGameUseList = useList;
10050 if (lg && lg->gameInfo.white && lg->gameInfo.black) {
10051 snprintf(buf, sizeof(buf), "%s vs. %s", lg->gameInfo.white,
10052 lg->gameInfo.black);
10054 } else if (*title != NULLCHAR) {
10055 if (gameNumber > 1) {
10056 sprintf(buf, "%s %d", title, gameNumber);
10059 DisplayTitle(title);
10063 if (gameMode != AnalyzeFile && gameMode != AnalyzeMode) {
10064 gameMode = PlayFromGameFile;
10068 currentMove = forwardMostMove = backwardMostMove = 0;
10069 CopyBoard(boards[0], initialPosition);
10073 * Skip the first gn-1 games in the file.
10074 * Also skip over anything that precedes an identifiable
10075 * start of game marker, to avoid being confused by
10076 * garbage at the start of the file. Currently
10077 * recognized start of game markers are the move number "1",
10078 * the pattern "gnuchess .* game", the pattern
10079 * "^[#;%] [^ ]* game file", and a PGN tag block.
10080 * A game that starts with one of the latter two patterns
10081 * will also have a move number 1, possibly
10082 * following a position diagram.
10083 * 5-4-02: Let's try being more lenient and allowing a game to
10084 * start with an unnumbered move. Does that break anything?
10086 cm = lastLoadGameStart = (ChessMove) 0;
10088 yyboardindex = forwardMostMove;
10089 cm = (ChessMove) yylex();
10091 case (ChessMove) 0:
10092 if (cmailMsgLoaded) {
10093 nCmailGames = CMAIL_MAX_GAMES - gn;
10096 DisplayError(_("Game not found in file"), 0);
10103 lastLoadGameStart = cm;
10106 case MoveNumberOne:
10107 switch (lastLoadGameStart) {
10112 case MoveNumberOne:
10113 case (ChessMove) 0:
10114 gn--; /* count this game */
10115 lastLoadGameStart = cm;
10124 switch (lastLoadGameStart) {
10127 case MoveNumberOne:
10128 case (ChessMove) 0:
10129 gn--; /* count this game */
10130 lastLoadGameStart = cm;
10133 lastLoadGameStart = cm; /* game counted already */
10141 yyboardindex = forwardMostMove;
10142 cm = (ChessMove) yylex();
10143 } while (cm == PGNTag || cm == Comment);
10150 if (cmailMsgLoaded && (CMAIL_MAX_GAMES == lastLoadGameNumber)) {
10151 if ( cmailResult[CMAIL_MAX_GAMES - gn - 1]
10152 != CMAIL_OLD_RESULT) {
10154 cmailResult[ CMAIL_MAX_GAMES
10155 - gn - 1] = CMAIL_OLD_RESULT;
10161 /* Only a NormalMove can be at the start of a game
10162 * without a position diagram. */
10163 if (lastLoadGameStart == (ChessMove) 0) {
10165 lastLoadGameStart = MoveNumberOne;
10174 if (appData.debugMode)
10175 fprintf(debugFP, "Parsed game start '%s' (%d)\n", yy_text, (int) cm);
10177 if (cm == XBoardGame) {
10178 /* Skip any header junk before position diagram and/or move 1 */
10180 yyboardindex = forwardMostMove;
10181 cm = (ChessMove) yylex();
10183 if (cm == (ChessMove) 0 ||
10184 cm == GNUChessGame || cm == XBoardGame) {
10185 /* Empty game; pretend end-of-file and handle later */
10186 cm = (ChessMove) 0;
10190 if (cm == MoveNumberOne || cm == PositionDiagram ||
10191 cm == PGNTag || cm == Comment)
10194 } else if (cm == GNUChessGame) {
10195 if (gameInfo.event != NULL) {
10196 free(gameInfo.event);
10198 gameInfo.event = StrSave(yy_text);
10201 startedFromSetupPosition = FALSE;
10202 while (cm == PGNTag) {
10203 if (appData.debugMode)
10204 fprintf(debugFP, "Parsed PGNTag: %s\n", yy_text);
10205 err = ParsePGNTag(yy_text, &gameInfo);
10206 if (!err) numPGNTags++;
10208 /* [HGM] PGNvariant: automatically switch to variant given in PGN tag */
10209 if(gameInfo.variant != oldVariant) {
10210 startedFromPositionFile = FALSE; /* [HGM] loadPos: variant switch likely makes position invalid */
10211 ResetFrontEnd(); // [HGM] might need other bitmaps. Cannot use Reset() because it clears gameInfo :-(
10212 InitPosition(TRUE);
10213 oldVariant = gameInfo.variant;
10214 if (appData.debugMode)
10215 fprintf(debugFP, "New variant %d\n", (int) oldVariant);
10219 if (gameInfo.fen != NULL) {
10220 Board initial_position;
10221 startedFromSetupPosition = TRUE;
10222 if (!ParseFEN(initial_position, &blackPlaysFirst, gameInfo.fen)) {
10224 DisplayError(_("Bad FEN position in file"), 0);
10227 CopyBoard(boards[0], initial_position);
10228 if (blackPlaysFirst) {
10229 currentMove = forwardMostMove = backwardMostMove = 1;
10230 CopyBoard(boards[1], initial_position);
10231 strcpy(moveList[0], "");
10232 strcpy(parseList[0], "");
10233 timeRemaining[0][1] = whiteTimeRemaining;
10234 timeRemaining[1][1] = blackTimeRemaining;
10235 if (commentList[0] != NULL) {
10236 commentList[1] = commentList[0];
10237 commentList[0] = NULL;
10240 currentMove = forwardMostMove = backwardMostMove = 0;
10242 /* [HGM] copy FEN attributes as well. Bugfix 4.3.14m and 4.3.15e: moved to after 'blackPlaysFirst' */
10244 initialRulePlies = FENrulePlies;
10245 for( i=0; i< nrCastlingRights; i++ )
10246 initialRights[i] = initial_position[CASTLING][i];
10248 yyboardindex = forwardMostMove;
10249 free(gameInfo.fen);
10250 gameInfo.fen = NULL;
10253 yyboardindex = forwardMostMove;
10254 cm = (ChessMove) yylex();
10256 /* Handle comments interspersed among the tags */
10257 while (cm == Comment) {
10259 if (appData.debugMode)
10260 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
10262 AppendComment(currentMove, p, FALSE);
10263 yyboardindex = forwardMostMove;
10264 cm = (ChessMove) yylex();
10268 /* don't rely on existence of Event tag since if game was
10269 * pasted from clipboard the Event tag may not exist
10271 if (numPGNTags > 0){
10273 if (gameInfo.variant == VariantNormal) {
10274 VariantClass v = StringToVariant(gameInfo.event);
10275 // [HGM] do not recognize variants from event tag that were introduced after supporting variant tag
10276 if(v < VariantShogi) gameInfo.variant = v;
10279 if( appData.autoDisplayTags ) {
10280 tags = PGNTags(&gameInfo);
10281 TagsPopUp(tags, CmailMsg());
10286 /* Make something up, but don't display it now */
10291 if (cm == PositionDiagram) {
10294 Board initial_position;
10296 if (appData.debugMode)
10297 fprintf(debugFP, "Parsed PositionDiagram: %s\n", yy_text);
10299 if (!startedFromSetupPosition) {
10301 for (i = BOARD_HEIGHT - 1; i >= 0; i--)
10302 for (j = BOARD_LEFT; j < BOARD_RGHT; p++)
10312 initial_position[i][j++] = CharToPiece(*p);
10315 while (*p == ' ' || *p == '\t' ||
10316 *p == '\n' || *p == '\r') p++;
10318 if (strncmp(p, "black", strlen("black"))==0)
10319 blackPlaysFirst = TRUE;
10321 blackPlaysFirst = FALSE;
10322 startedFromSetupPosition = TRUE;
10324 CopyBoard(boards[0], initial_position);
10325 if (blackPlaysFirst) {
10326 currentMove = forwardMostMove = backwardMostMove = 1;
10327 CopyBoard(boards[1], initial_position);
10328 strcpy(moveList[0], "");
10329 strcpy(parseList[0], "");
10330 timeRemaining[0][1] = whiteTimeRemaining;
10331 timeRemaining[1][1] = blackTimeRemaining;
10332 if (commentList[0] != NULL) {
10333 commentList[1] = commentList[0];
10334 commentList[0] = NULL;
10337 currentMove = forwardMostMove = backwardMostMove = 0;
10340 yyboardindex = forwardMostMove;
10341 cm = (ChessMove) yylex();
10344 if (first.pr == NoProc) {
10345 StartChessProgram(&first);
10347 InitChessProgram(&first, FALSE);
10348 SendToProgram("force\n", &first);
10349 if (startedFromSetupPosition) {
10350 SendBoard(&first, forwardMostMove);
10351 if (appData.debugMode) {
10352 fprintf(debugFP, "Load Game\n");
10354 DisplayBothClocks();
10357 /* [HGM] server: flag to write setup moves in broadcast file as one */
10358 loadFlag = appData.suppressLoadMoves;
10360 while (cm == Comment) {
10362 if (appData.debugMode)
10363 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
10365 AppendComment(currentMove, p, FALSE);
10366 yyboardindex = forwardMostMove;
10367 cm = (ChessMove) yylex();
10370 if ((cm == (ChessMove) 0 && lastLoadGameStart != (ChessMove) 0) ||
10371 cm == WhiteWins || cm == BlackWins ||
10372 cm == GameIsDrawn || cm == GameUnfinished) {
10373 DisplayMessage("", _("No moves in game"));
10374 if (cmailMsgLoaded) {
10375 if (appData.debugMode)
10376 fprintf(debugFP, "Setting flipView to %d.\n", FALSE);
10380 DrawPosition(FALSE, boards[currentMove]);
10381 DisplayBothClocks();
10382 gameMode = EditGame;
10389 // [HGM] PV info: routine tests if comment empty
10390 if (!matchMode && (pausing || appData.timeDelay != 0)) {
10391 DisplayComment(currentMove - 1, commentList[currentMove]);
10393 if (!matchMode && appData.timeDelay != 0)
10394 DrawPosition(FALSE, boards[currentMove]);
10396 if (gameMode == AnalyzeFile || gameMode == AnalyzeMode) {
10397 programStats.ok_to_send = 1;
10400 /* if the first token after the PGN tags is a move
10401 * and not move number 1, retrieve it from the parser
10403 if (cm != MoveNumberOne)
10404 LoadGameOneMove(cm);
10406 /* load the remaining moves from the file */
10407 while (LoadGameOneMove((ChessMove)0)) {
10408 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
10409 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
10412 /* rewind to the start of the game */
10413 currentMove = backwardMostMove;
10415 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
10417 if (oldGameMode == AnalyzeFile ||
10418 oldGameMode == AnalyzeMode) {
10419 AnalyzeFileEvent();
10422 if (matchMode || appData.timeDelay == 0) {
10424 gameMode = EditGame;
10426 } else if (appData.timeDelay > 0) {
10427 AutoPlayGameLoop();
10430 if (appData.debugMode)
10431 fprintf(debugFP, "LoadGame(): on exit, gameMode %d\n", gameMode);
10433 loadFlag = 0; /* [HGM] true game starts */
10437 /* Support for LoadNextPosition, LoadPreviousPosition, ReloadSamePosition */
10439 ReloadPosition(offset)
10442 int positionNumber = lastLoadPositionNumber + offset;
10443 if (lastLoadPositionFP == NULL) {
10444 DisplayError(_("No position has been loaded yet"), 0);
10447 if (positionNumber <= 0) {
10448 DisplayError(_("Can't back up any further"), 0);
10451 return LoadPosition(lastLoadPositionFP, positionNumber,
10452 lastLoadPositionTitle);
10455 /* Load the nth position from the given file */
10457 LoadPositionFromFile(filename, n, title)
10465 if (strcmp(filename, "-") == 0) {
10466 return LoadPosition(stdin, n, "stdin");
10468 f = fopen(filename, "rb");
10470 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
10471 DisplayError(buf, errno);
10474 return LoadPosition(f, n, title);
10479 /* Load the nth position from the given open file, and close it */
10481 LoadPosition(f, positionNumber, title)
10483 int positionNumber;
10486 char *p, line[MSG_SIZ];
10487 Board initial_position;
10488 int i, j, fenMode, pn;
10490 if (gameMode == Training )
10491 SetTrainingModeOff();
10493 if (gameMode != BeginningOfGame) {
10494 Reset(FALSE, TRUE);
10496 if (lastLoadPositionFP != NULL && lastLoadPositionFP != f) {
10497 fclose(lastLoadPositionFP);
10499 if (positionNumber == 0) positionNumber = 1;
10500 lastLoadPositionFP = f;
10501 lastLoadPositionNumber = positionNumber;
10502 strcpy(lastLoadPositionTitle, title);
10503 if (first.pr == NoProc) {
10504 StartChessProgram(&first);
10505 InitChessProgram(&first, FALSE);
10507 pn = positionNumber;
10508 if (positionNumber < 0) {
10509 /* Negative position number means to seek to that byte offset */
10510 if (fseek(f, -positionNumber, 0) == -1) {
10511 DisplayError(_("Can't seek on position file"), 0);
10516 if (fseek(f, 0, 0) == -1) {
10517 if (f == lastLoadPositionFP ?
10518 positionNumber == lastLoadPositionNumber + 1 :
10519 positionNumber == 1) {
10522 DisplayError(_("Can't seek on position file"), 0);
10527 /* See if this file is FEN or old-style xboard */
10528 if (fgets(line, MSG_SIZ, f) == NULL) {
10529 DisplayError(_("Position not found in file"), 0);
10532 // [HGM] FEN can begin with digit, any piece letter valid in this variant, or a + for Shogi promoted pieces
10533 fenMode = line[0] >= '0' && line[0] <= '9' || line[0] == '+' || CharToPiece(line[0]) != EmptySquare;
10536 if (fenMode || line[0] == '#') pn--;
10538 /* skip positions before number pn */
10539 if (fgets(line, MSG_SIZ, f) == NULL) {
10541 DisplayError(_("Position not found in file"), 0);
10544 if (fenMode || line[0] == '#') pn--;
10549 if (!ParseFEN(initial_position, &blackPlaysFirst, line)) {
10550 DisplayError(_("Bad FEN position in file"), 0);
10554 (void) fgets(line, MSG_SIZ, f);
10555 (void) fgets(line, MSG_SIZ, f);
10557 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
10558 (void) fgets(line, MSG_SIZ, f);
10559 for (p = line, j = BOARD_LEFT; j < BOARD_RGHT; p++) {
10562 initial_position[i][j++] = CharToPiece(*p);
10566 blackPlaysFirst = FALSE;
10568 (void) fgets(line, MSG_SIZ, f);
10569 if (strncmp(line, "black", strlen("black"))==0)
10570 blackPlaysFirst = TRUE;
10573 startedFromSetupPosition = TRUE;
10575 SendToProgram("force\n", &first);
10576 CopyBoard(boards[0], initial_position);
10577 if (blackPlaysFirst) {
10578 currentMove = forwardMostMove = backwardMostMove = 1;
10579 strcpy(moveList[0], "");
10580 strcpy(parseList[0], "");
10581 CopyBoard(boards[1], initial_position);
10582 DisplayMessage("", _("Black to play"));
10584 currentMove = forwardMostMove = backwardMostMove = 0;
10585 DisplayMessage("", _("White to play"));
10587 initialRulePlies = FENrulePlies; /* [HGM] copy FEN attributes as well */
10588 SendBoard(&first, forwardMostMove);
10589 if (appData.debugMode) {
10591 for(i=0;i<2;i++){for(j=0;j<6;j++)fprintf(debugFP, " %d", boards[i][CASTLING][j]);fprintf(debugFP,"\n");}
10592 for(j=0;j<6;j++)fprintf(debugFP, " %d", initialRights[j]);fprintf(debugFP,"\n");
10593 fprintf(debugFP, "Load Position\n");
10596 if (positionNumber > 1) {
10597 sprintf(line, "%s %d", title, positionNumber);
10598 DisplayTitle(line);
10600 DisplayTitle(title);
10602 gameMode = EditGame;
10605 timeRemaining[0][1] = whiteTimeRemaining;
10606 timeRemaining[1][1] = blackTimeRemaining;
10607 DrawPosition(FALSE, boards[currentMove]);
10614 CopyPlayerNameIntoFileName(dest, src)
10617 while (*src != NULLCHAR && *src != ',') {
10622 *(*dest)++ = *src++;
10627 char *DefaultFileName(ext)
10630 static char def[MSG_SIZ];
10633 if (gameInfo.white != NULL && gameInfo.white[0] != '-') {
10635 CopyPlayerNameIntoFileName(&p, gameInfo.white);
10637 CopyPlayerNameIntoFileName(&p, gameInfo.black);
10646 /* Save the current game to the given file */
10648 SaveGameToFile(filename, append)
10655 if (strcmp(filename, "-") == 0) {
10656 return SaveGame(stdout, 0, NULL);
10658 f = fopen(filename, append ? "a" : "w");
10660 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
10661 DisplayError(buf, errno);
10664 return SaveGame(f, 0, NULL);
10673 static char buf[MSG_SIZ];
10676 p = strchr(str, ' ');
10677 if (p == NULL) return str;
10678 strncpy(buf, str, p - str);
10679 buf[p - str] = NULLCHAR;
10683 #define PGN_MAX_LINE 75
10685 #define PGN_SIDE_WHITE 0
10686 #define PGN_SIDE_BLACK 1
10689 static int FindFirstMoveOutOfBook( int side )
10693 if( backwardMostMove == 0 && ! startedFromSetupPosition) {
10694 int index = backwardMostMove;
10695 int has_book_hit = 0;
10697 if( (index % 2) != side ) {
10701 while( index < forwardMostMove ) {
10702 /* Check to see if engine is in book */
10703 int depth = pvInfoList[index].depth;
10704 int score = pvInfoList[index].score;
10710 else if( score == 0 && depth == 63 ) {
10711 in_book = 1; /* Zappa */
10713 else if( score == 2 && depth == 99 ) {
10714 in_book = 1; /* Abrok */
10717 has_book_hit += in_book;
10733 void GetOutOfBookInfo( char * buf )
10737 int offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
10739 oob[0] = FindFirstMoveOutOfBook( PGN_SIDE_WHITE );
10740 oob[1] = FindFirstMoveOutOfBook( PGN_SIDE_BLACK );
10744 if( oob[0] >= 0 || oob[1] >= 0 ) {
10745 for( i=0; i<2; i++ ) {
10749 if( i > 0 && oob[0] >= 0 ) {
10750 strcat( buf, " " );
10753 sprintf( buf+strlen(buf), "%d%s. ", (idx - offset)/2 + 1, idx & 1 ? ".." : "" );
10754 sprintf( buf+strlen(buf), "%s%.2f",
10755 pvInfoList[idx].score >= 0 ? "+" : "",
10756 pvInfoList[idx].score / 100.0 );
10762 /* Save game in PGN style and close the file */
10767 int i, offset, linelen, newblock;
10771 int movelen, numlen, blank;
10772 char move_buffer[100]; /* [AS] Buffer for move+PV info */
10774 offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
10776 tm = time((time_t *) NULL);
10778 PrintPGNTags(f, &gameInfo);
10780 if (backwardMostMove > 0 || startedFromSetupPosition) {
10781 char *fen = PositionToFEN(backwardMostMove, NULL);
10782 fprintf(f, "[FEN \"%s\"]\n[SetUp \"1\"]\n", fen);
10783 fprintf(f, "\n{--------------\n");
10784 PrintPosition(f, backwardMostMove);
10785 fprintf(f, "--------------}\n");
10789 /* [AS] Out of book annotation */
10790 if( appData.saveOutOfBookInfo ) {
10793 GetOutOfBookInfo( buf );
10795 if( buf[0] != '\0' ) {
10796 fprintf( f, "[%s \"%s\"]\n", PGN_OUT_OF_BOOK, buf );
10803 i = backwardMostMove;
10807 while (i < forwardMostMove) {
10808 /* Print comments preceding this move */
10809 if (commentList[i] != NULL) {
10810 if (linelen > 0) fprintf(f, "\n");
10811 fprintf(f, "%s", commentList[i]);
10816 /* Format move number */
10817 if ((i % 2) == 0) {
10818 sprintf(numtext, "%d.", (i - offset)/2 + 1);
10821 sprintf(numtext, "%d...", (i - offset)/2 + 1);
10823 numtext[0] = NULLCHAR;
10826 numlen = strlen(numtext);
10829 /* Print move number */
10830 blank = linelen > 0 && numlen > 0;
10831 if (linelen + (blank ? 1 : 0) + numlen > PGN_MAX_LINE) {
10840 fprintf(f, "%s", numtext);
10844 strcpy(move_buffer, SavePart(parseList[i])); // [HGM] pgn: print move via buffer, so it can be edited
10845 movelen = strlen(move_buffer); /* [HGM] pgn: line-break point before move */
10848 blank = linelen > 0 && movelen > 0;
10849 if (linelen + (blank ? 1 : 0) + movelen > PGN_MAX_LINE) {
10858 fprintf(f, "%s", move_buffer);
10859 linelen += movelen;
10861 /* [AS] Add PV info if present */
10862 if( i >= 0 && appData.saveExtendedInfoInPGN && pvInfoList[i].depth > 0 ) {
10863 /* [HGM] add time */
10864 char buf[MSG_SIZ]; int seconds;
10866 seconds = (pvInfoList[i].time+5)/10; // deci-seconds, rounded to nearest
10868 if( seconds <= 0) buf[0] = 0; else
10869 if( seconds < 30 ) sprintf(buf, " %3.1f%c", seconds/10., 0); else {
10870 seconds = (seconds + 4)/10; // round to full seconds
10871 if( seconds < 60 ) sprintf(buf, " %d%c", seconds, 0); else
10872 sprintf(buf, " %d:%02d%c", seconds/60, seconds%60, 0);
10875 sprintf( move_buffer, "{%s%.2f/%d%s}",
10876 pvInfoList[i].score >= 0 ? "+" : "",
10877 pvInfoList[i].score / 100.0,
10878 pvInfoList[i].depth,
10881 movelen = strlen(move_buffer); /* [HGM] pgn: line-break point after move */
10883 /* Print score/depth */
10884 blank = linelen > 0 && movelen > 0;
10885 if (linelen + (blank ? 1 : 0) + movelen > PGN_MAX_LINE) {
10894 fprintf(f, "%s", move_buffer);
10895 linelen += movelen;
10901 /* Start a new line */
10902 if (linelen > 0) fprintf(f, "\n");
10904 /* Print comments after last move */
10905 if (commentList[i] != NULL) {
10906 fprintf(f, "%s\n", commentList[i]);
10910 if (gameInfo.resultDetails != NULL &&
10911 gameInfo.resultDetails[0] != NULLCHAR) {
10912 fprintf(f, "{%s} %s\n\n", gameInfo.resultDetails,
10913 PGNResult(gameInfo.result));
10915 fprintf(f, "%s\n\n", PGNResult(gameInfo.result));
10919 lastSavedGame = GameCheckSum(); // [HGM] save: remember ID of last saved game to prevent double saving
10923 /* Save game in old style and close the file */
10925 SaveGameOldStyle(f)
10931 tm = time((time_t *) NULL);
10933 fprintf(f, "# %s game file -- %s", programName, ctime(&tm));
10936 if (backwardMostMove > 0 || startedFromSetupPosition) {
10937 fprintf(f, "\n[--------------\n");
10938 PrintPosition(f, backwardMostMove);
10939 fprintf(f, "--------------]\n");
10944 i = backwardMostMove;
10945 offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
10947 while (i < forwardMostMove) {
10948 if (commentList[i] != NULL) {
10949 fprintf(f, "[%s]\n", commentList[i]);
10952 if ((i % 2) == 1) {
10953 fprintf(f, "%d. ... %s\n", (i - offset)/2 + 1, parseList[i]);
10956 fprintf(f, "%d. %s ", (i - offset)/2 + 1, parseList[i]);
10958 if (commentList[i] != NULL) {
10962 if (i >= forwardMostMove) {
10966 fprintf(f, "%s\n", parseList[i]);
10971 if (commentList[i] != NULL) {
10972 fprintf(f, "[%s]\n", commentList[i]);
10975 /* This isn't really the old style, but it's close enough */
10976 if (gameInfo.resultDetails != NULL &&
10977 gameInfo.resultDetails[0] != NULLCHAR) {
10978 fprintf(f, "%s (%s)\n\n", PGNResult(gameInfo.result),
10979 gameInfo.resultDetails);
10981 fprintf(f, "%s\n\n", PGNResult(gameInfo.result));
10988 /* Save the current game to open file f and close the file */
10990 SaveGame(f, dummy, dummy2)
10995 if (gameMode == EditPosition) EditPositionDone(TRUE);
10996 lastSavedGame = GameCheckSum(); // [HGM] save: remember ID of last saved game to prevent double saving
10997 if (appData.oldSaveStyle)
10998 return SaveGameOldStyle(f);
11000 return SaveGamePGN(f);
11003 /* Save the current position to the given file */
11005 SavePositionToFile(filename)
11011 if (strcmp(filename, "-") == 0) {
11012 return SavePosition(stdout, 0, NULL);
11014 f = fopen(filename, "a");
11016 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
11017 DisplayError(buf, errno);
11020 SavePosition(f, 0, NULL);
11026 /* Save the current position to the given open file and close the file */
11028 SavePosition(f, dummy, dummy2)
11036 if (gameMode == EditPosition) EditPositionDone(TRUE);
11037 if (appData.oldSaveStyle) {
11038 tm = time((time_t *) NULL);
11040 fprintf(f, "# %s position file -- %s", programName, ctime(&tm));
11042 fprintf(f, "[--------------\n");
11043 PrintPosition(f, currentMove);
11044 fprintf(f, "--------------]\n");
11046 fen = PositionToFEN(currentMove, NULL);
11047 fprintf(f, "%s\n", fen);
11055 ReloadCmailMsgEvent(unregister)
11059 static char *inFilename = NULL;
11060 static char *outFilename;
11062 struct stat inbuf, outbuf;
11065 /* Any registered moves are unregistered if unregister is set, */
11066 /* i.e. invoked by the signal handler */
11068 for (i = 0; i < CMAIL_MAX_GAMES; i ++) {
11069 cmailMoveRegistered[i] = FALSE;
11070 if (cmailCommentList[i] != NULL) {
11071 free(cmailCommentList[i]);
11072 cmailCommentList[i] = NULL;
11075 nCmailMovesRegistered = 0;
11078 for (i = 0; i < CMAIL_MAX_GAMES; i ++) {
11079 cmailResult[i] = CMAIL_NOT_RESULT;
11083 if (inFilename == NULL) {
11084 /* Because the filenames are static they only get malloced once */
11085 /* and they never get freed */
11086 inFilename = (char *) malloc(strlen(appData.cmailGameName) + 9);
11087 sprintf(inFilename, "%s.game.in", appData.cmailGameName);
11089 outFilename = (char *) malloc(strlen(appData.cmailGameName) + 5);
11090 sprintf(outFilename, "%s.out", appData.cmailGameName);
11093 status = stat(outFilename, &outbuf);
11095 cmailMailedMove = FALSE;
11097 status = stat(inFilename, &inbuf);
11098 cmailMailedMove = (inbuf.st_mtime < outbuf.st_mtime);
11101 /* LoadGameFromFile(CMAIL_MAX_GAMES) with cmailMsgLoaded == TRUE
11102 counts the games, notes how each one terminated, etc.
11104 It would be nice to remove this kludge and instead gather all
11105 the information while building the game list. (And to keep it
11106 in the game list nodes instead of having a bunch of fixed-size
11107 parallel arrays.) Note this will require getting each game's
11108 termination from the PGN tags, as the game list builder does
11109 not process the game moves. --mann
11111 cmailMsgLoaded = TRUE;
11112 LoadGameFromFile(inFilename, CMAIL_MAX_GAMES, "", FALSE);
11114 /* Load first game in the file or popup game menu */
11115 LoadGameFromFile(inFilename, 0, appData.cmailGameName, TRUE);
11117 #endif /* !WIN32 */
11125 char string[MSG_SIZ];
11127 if ( cmailMailedMove
11128 || (cmailResult[lastLoadGameNumber - 1] == CMAIL_OLD_RESULT)) {
11129 return TRUE; /* Allow free viewing */
11132 /* Unregister move to ensure that we don't leave RegisterMove */
11133 /* with the move registered when the conditions for registering no */
11135 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
11136 cmailMoveRegistered[lastLoadGameNumber - 1] = FALSE;
11137 nCmailMovesRegistered --;
11139 if (cmailCommentList[lastLoadGameNumber - 1] != NULL)
11141 free(cmailCommentList[lastLoadGameNumber - 1]);
11142 cmailCommentList[lastLoadGameNumber - 1] = NULL;
11146 if (cmailOldMove == -1) {
11147 DisplayError(_("You have edited the game history.\nUse Reload Same Game and make your move again."), 0);
11151 if (currentMove > cmailOldMove + 1) {
11152 DisplayError(_("You have entered too many moves.\nBack up to the correct position and try again."), 0);
11156 if (currentMove < cmailOldMove) {
11157 DisplayError(_("Displayed position is not current.\nStep forward to the correct position and try again."), 0);
11161 if (forwardMostMove > currentMove) {
11162 /* Silently truncate extra moves */
11166 if ( (currentMove == cmailOldMove + 1)
11167 || ( (currentMove == cmailOldMove)
11168 && ( (cmailMoveType[lastLoadGameNumber - 1] == CMAIL_ACCEPT)
11169 || (cmailMoveType[lastLoadGameNumber - 1] == CMAIL_RESIGN)))) {
11170 if (gameInfo.result != GameUnfinished) {
11171 cmailResult[lastLoadGameNumber - 1] = CMAIL_NEW_RESULT;
11174 if (commentList[currentMove] != NULL) {
11175 cmailCommentList[lastLoadGameNumber - 1]
11176 = StrSave(commentList[currentMove]);
11178 strcpy(cmailMove[lastLoadGameNumber - 1], moveList[currentMove - 1]);
11180 if (appData.debugMode)
11181 fprintf(debugFP, "Saving %s for game %d\n",
11182 cmailMove[lastLoadGameNumber - 1], lastLoadGameNumber);
11185 "%s.game.out.%d", appData.cmailGameName, lastLoadGameNumber);
11187 f = fopen(string, "w");
11188 if (appData.oldSaveStyle) {
11189 SaveGameOldStyle(f); /* also closes the file */
11191 sprintf(string, "%s.pos.out", appData.cmailGameName);
11192 f = fopen(string, "w");
11193 SavePosition(f, 0, NULL); /* also closes the file */
11195 fprintf(f, "{--------------\n");
11196 PrintPosition(f, currentMove);
11197 fprintf(f, "--------------}\n\n");
11199 SaveGame(f, 0, NULL); /* also closes the file*/
11202 cmailMoveRegistered[lastLoadGameNumber - 1] = TRUE;
11203 nCmailMovesRegistered ++;
11204 } else if (nCmailGames == 1) {
11205 DisplayError(_("You have not made a move yet"), 0);
11216 static char *partCommandString = "cmail -xv%s -remail -game %s 2>&1";
11217 FILE *commandOutput;
11218 char buffer[MSG_SIZ], msg[MSG_SIZ], string[MSG_SIZ];
11219 int nBytes = 0; /* Suppress warnings on uninitialized variables */
11225 if (! cmailMsgLoaded) {
11226 DisplayError(_("The cmail message is not loaded.\nUse Reload CMail Message and make your move again."), 0);
11230 if (nCmailGames == nCmailResults) {
11231 DisplayError(_("No unfinished games"), 0);
11235 #if CMAIL_PROHIBIT_REMAIL
11236 if (cmailMailedMove) {
11237 sprintf(msg, _("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);
11238 DisplayError(msg, 0);
11243 if (! (cmailMailedMove || RegisterMove())) return;
11245 if ( cmailMailedMove
11246 || (nCmailMovesRegistered + nCmailResults == nCmailGames)) {
11247 sprintf(string, partCommandString,
11248 appData.debugMode ? " -v" : "", appData.cmailGameName);
11249 commandOutput = popen(string, "r");
11251 if (commandOutput == NULL) {
11252 DisplayError(_("Failed to invoke cmail"), 0);
11254 for (nBuffers = 0; (! feof(commandOutput)); nBuffers ++) {
11255 nBytes = fread(buffer, 1, MSG_SIZ - 1, commandOutput);
11257 if (nBuffers > 1) {
11258 (void) memcpy(msg, buffer + nBytes, MSG_SIZ - nBytes - 1);
11259 (void) memcpy(msg + MSG_SIZ - nBytes - 1, buffer, nBytes);
11260 nBytes = MSG_SIZ - 1;
11262 (void) memcpy(msg, buffer, nBytes);
11264 *(msg + nBytes) = '\0'; /* \0 for end-of-string*/
11266 if(StrStr(msg, "Mailed cmail message to ") != NULL) {
11267 cmailMailedMove = TRUE; /* Prevent >1 moves */
11270 for (i = 0; i < nCmailGames; i ++) {
11271 if (cmailResult[i] == CMAIL_NOT_RESULT) {
11276 && ( (arcDir = (char *) getenv("CMAIL_ARCDIR"))
11278 sprintf(buffer, "%s/%s.%s.archive",
11280 appData.cmailGameName,
11282 LoadGameFromFile(buffer, 1, buffer, FALSE);
11283 cmailMsgLoaded = FALSE;
11287 DisplayInformation(msg);
11288 pclose(commandOutput);
11291 if ((*cmailMsg) != '\0') {
11292 DisplayInformation(cmailMsg);
11297 #endif /* !WIN32 */
11306 int prependComma = 0;
11308 char string[MSG_SIZ]; /* Space for game-list */
11311 if (!cmailMsgLoaded) return "";
11313 if (cmailMailedMove) {
11314 sprintf(cmailMsg, _("Waiting for reply from opponent\n"));
11316 /* Create a list of games left */
11317 sprintf(string, "[");
11318 for (i = 0; i < nCmailGames; i ++) {
11319 if (! ( cmailMoveRegistered[i]
11320 || (cmailResult[i] == CMAIL_OLD_RESULT))) {
11321 if (prependComma) {
11322 sprintf(number, ",%d", i + 1);
11324 sprintf(number, "%d", i + 1);
11328 strcat(string, number);
11331 strcat(string, "]");
11333 if (nCmailMovesRegistered + nCmailResults == 0) {
11334 switch (nCmailGames) {
11337 _("Still need to make move for game\n"));
11342 _("Still need to make moves for both games\n"));
11347 _("Still need to make moves for all %d games\n"),
11352 switch (nCmailGames - nCmailMovesRegistered - nCmailResults) {
11355 _("Still need to make a move for game %s\n"),
11360 if (nCmailResults == nCmailGames) {
11361 sprintf(cmailMsg, _("No unfinished games\n"));
11363 sprintf(cmailMsg, _("Ready to send mail\n"));
11369 _("Still need to make moves for games %s\n"),
11381 if (gameMode == Training)
11382 SetTrainingModeOff();
11385 cmailMsgLoaded = FALSE;
11386 if (appData.icsActive) {
11387 SendToICS(ics_prefix);
11388 SendToICS("refresh\n");
11398 /* Give up on clean exit */
11402 /* Keep trying for clean exit */
11406 if (appData.icsActive && appData.colorize) Colorize(ColorNone, FALSE);
11408 if (telnetISR != NULL) {
11409 RemoveInputSource(telnetISR);
11411 if (icsPR != NoProc) {
11412 DestroyChildProcess(icsPR, TRUE);
11415 /* [HGM] crash: leave writing PGN and position entirely to GameEnds() */
11416 GameEnds(gameInfo.result, gameInfo.resultDetails==NULL ? "xboard exit" : gameInfo.resultDetails, GE_PLAYER);
11418 /* [HGM] crash: the above GameEnds() is a dud if another one was running */
11419 /* make sure this other one finishes before killing it! */
11420 if(endingGame) { int count = 0;
11421 if(appData.debugMode) fprintf(debugFP, "ExitEvent() during GameEnds(), wait\n");
11422 while(endingGame && count++ < 10) DoSleep(1);
11423 if(appData.debugMode && endingGame) fprintf(debugFP, "GameEnds() seems stuck, proceed exiting\n");
11426 /* Kill off chess programs */
11427 if (first.pr != NoProc) {
11430 DoSleep( appData.delayBeforeQuit );
11431 SendToProgram("quit\n", &first);
11432 DoSleep( appData.delayAfterQuit );
11433 DestroyChildProcess(first.pr, 10 /* [AS] first.useSigterm */ );
11435 if (second.pr != NoProc) {
11436 DoSleep( appData.delayBeforeQuit );
11437 SendToProgram("quit\n", &second);
11438 DoSleep( appData.delayAfterQuit );
11439 DestroyChildProcess(second.pr, 10 /* [AS] second.useSigterm */ );
11441 if (first.isr != NULL) {
11442 RemoveInputSource(first.isr);
11444 if (second.isr != NULL) {
11445 RemoveInputSource(second.isr);
11448 ShutDownFrontEnd();
11455 if (appData.debugMode)
11456 fprintf(debugFP, "PauseEvent(): pausing %d\n", pausing);
11460 if (gameMode == MachinePlaysWhite ||
11461 gameMode == MachinePlaysBlack) {
11464 DisplayBothClocks();
11466 if (gameMode == PlayFromGameFile) {
11467 if (appData.timeDelay >= 0)
11468 AutoPlayGameLoop();
11469 } else if (gameMode == IcsExamining && pauseExamInvalid) {
11470 Reset(FALSE, TRUE);
11471 SendToICS(ics_prefix);
11472 SendToICS("refresh\n");
11473 } else if (currentMove < forwardMostMove) {
11474 ForwardInner(forwardMostMove);
11476 pauseExamInvalid = FALSE;
11478 switch (gameMode) {
11482 pauseExamForwardMostMove = forwardMostMove;
11483 pauseExamInvalid = FALSE;
11486 case IcsPlayingWhite:
11487 case IcsPlayingBlack:
11491 case PlayFromGameFile:
11492 (void) StopLoadGameTimer();
11496 case BeginningOfGame:
11497 if (appData.icsActive) return;
11498 /* else fall through */
11499 case MachinePlaysWhite:
11500 case MachinePlaysBlack:
11501 case TwoMachinesPlay:
11502 if (forwardMostMove == 0)
11503 return; /* don't pause if no one has moved */
11504 if ((gameMode == MachinePlaysWhite &&
11505 !WhiteOnMove(forwardMostMove)) ||
11506 (gameMode == MachinePlaysBlack &&
11507 WhiteOnMove(forwardMostMove))) {
11520 char title[MSG_SIZ];
11522 if (currentMove < 1 || parseList[currentMove - 1][0] == NULLCHAR) {
11523 strcpy(title, _("Edit comment"));
11525 sprintf(title, _("Edit comment on %d.%s%s"), (currentMove - 1) / 2 + 1,
11526 WhiteOnMove(currentMove - 1) ? " " : ".. ",
11527 parseList[currentMove - 1]);
11530 EditCommentPopUp(currentMove, title, commentList[currentMove]);
11537 char *tags = PGNTags(&gameInfo);
11538 EditTagsPopUp(tags);
11545 if (appData.noChessProgram || gameMode == AnalyzeMode)
11548 if (gameMode != AnalyzeFile) {
11549 if (!appData.icsEngineAnalyze) {
11551 if (gameMode != EditGame) return;
11553 ResurrectChessProgram();
11554 SendToProgram("analyze\n", &first);
11555 first.analyzing = TRUE;
11556 /*first.maybeThinking = TRUE;*/
11557 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
11558 EngineOutputPopUp();
11560 if (!appData.icsEngineAnalyze) gameMode = AnalyzeMode;
11565 StartAnalysisClock();
11566 GetTimeMark(&lastNodeCountTime);
11573 if (appData.noChessProgram || gameMode == AnalyzeFile)
11576 if (gameMode != AnalyzeMode) {
11578 if (gameMode != EditGame) return;
11579 ResurrectChessProgram();
11580 SendToProgram("analyze\n", &first);
11581 first.analyzing = TRUE;
11582 /*first.maybeThinking = TRUE;*/
11583 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
11584 EngineOutputPopUp();
11586 gameMode = AnalyzeFile;
11591 StartAnalysisClock();
11592 GetTimeMark(&lastNodeCountTime);
11597 MachineWhiteEvent()
11600 char *bookHit = NULL;
11602 if (appData.noChessProgram || (gameMode == MachinePlaysWhite))
11606 if (gameMode == PlayFromGameFile ||
11607 gameMode == TwoMachinesPlay ||
11608 gameMode == Training ||
11609 gameMode == AnalyzeMode ||
11610 gameMode == EndOfGame)
11613 if (gameMode == EditPosition)
11614 EditPositionDone(TRUE);
11616 if (!WhiteOnMove(currentMove)) {
11617 DisplayError(_("It is not White's turn"), 0);
11621 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile)
11624 if (gameMode == EditGame || gameMode == AnalyzeMode ||
11625 gameMode == AnalyzeFile)
11628 ResurrectChessProgram(); /* in case it isn't running */
11629 if(gameMode == BeginningOfGame) { /* [HGM] time odds: to get right odds in human mode */
11630 gameMode = MachinePlaysWhite;
11633 gameMode = MachinePlaysWhite;
11637 sprintf(buf, "%s vs. %s", gameInfo.white, gameInfo.black);
11639 if (first.sendName) {
11640 sprintf(buf, "name %s\n", gameInfo.black);
11641 SendToProgram(buf, &first);
11643 if (first.sendTime) {
11644 if (first.useColors) {
11645 SendToProgram("black\n", &first); /*gnu kludge*/
11647 SendTimeRemaining(&first, TRUE);
11649 if (first.useColors) {
11650 SendToProgram("white\n", &first); // [HGM] book: send 'go' separately
11652 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: send go or retrieve book move
11653 SetMachineThinkingEnables();
11654 first.maybeThinking = TRUE;
11658 if (appData.autoFlipView && !flipView) {
11659 flipView = !flipView;
11660 DrawPosition(FALSE, NULL);
11661 DisplayBothClocks(); // [HGM] logo: clocks might have to be exchanged;
11664 if(bookHit) { // [HGM] book: simulate book reply
11665 static char bookMove[MSG_SIZ]; // a bit generous?
11667 programStats.nodes = programStats.depth = programStats.time =
11668 programStats.score = programStats.got_only_move = 0;
11669 sprintf(programStats.movelist, "%s (xbook)", bookHit);
11671 strcpy(bookMove, "move ");
11672 strcat(bookMove, bookHit);
11673 HandleMachineMove(bookMove, &first);
11678 MachineBlackEvent()
11681 char *bookHit = NULL;
11683 if (appData.noChessProgram || (gameMode == MachinePlaysBlack))
11687 if (gameMode == PlayFromGameFile ||
11688 gameMode == TwoMachinesPlay ||
11689 gameMode == Training ||
11690 gameMode == AnalyzeMode ||
11691 gameMode == EndOfGame)
11694 if (gameMode == EditPosition)
11695 EditPositionDone(TRUE);
11697 if (WhiteOnMove(currentMove)) {
11698 DisplayError(_("It is not Black's turn"), 0);
11702 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile)
11705 if (gameMode == EditGame || gameMode == AnalyzeMode ||
11706 gameMode == AnalyzeFile)
11709 ResurrectChessProgram(); /* in case it isn't running */
11710 gameMode = MachinePlaysBlack;
11714 sprintf(buf, "%s vs. %s", gameInfo.white, gameInfo.black);
11716 if (first.sendName) {
11717 sprintf(buf, "name %s\n", gameInfo.white);
11718 SendToProgram(buf, &first);
11720 if (first.sendTime) {
11721 if (first.useColors) {
11722 SendToProgram("white\n", &first); /*gnu kludge*/
11724 SendTimeRemaining(&first, FALSE);
11726 if (first.useColors) {
11727 SendToProgram("black\n", &first); // [HGM] book: 'go' sent separately
11729 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: send go or retrieve book move
11730 SetMachineThinkingEnables();
11731 first.maybeThinking = TRUE;
11734 if (appData.autoFlipView && flipView) {
11735 flipView = !flipView;
11736 DrawPosition(FALSE, NULL);
11737 DisplayBothClocks(); // [HGM] logo: clocks might have to be exchanged;
11739 if(bookHit) { // [HGM] book: simulate book reply
11740 static char bookMove[MSG_SIZ]; // a bit generous?
11742 programStats.nodes = programStats.depth = programStats.time =
11743 programStats.score = programStats.got_only_move = 0;
11744 sprintf(programStats.movelist, "%s (xbook)", bookHit);
11746 strcpy(bookMove, "move ");
11747 strcat(bookMove, bookHit);
11748 HandleMachineMove(bookMove, &first);
11754 DisplayTwoMachinesTitle()
11757 if (appData.matchGames > 0) {
11758 if (first.twoMachinesColor[0] == 'w') {
11759 sprintf(buf, "%s vs. %s (%d-%d-%d)",
11760 gameInfo.white, gameInfo.black,
11761 first.matchWins, second.matchWins,
11762 matchGame - 1 - (first.matchWins + second.matchWins));
11764 sprintf(buf, "%s vs. %s (%d-%d-%d)",
11765 gameInfo.white, gameInfo.black,
11766 second.matchWins, first.matchWins,
11767 matchGame - 1 - (first.matchWins + second.matchWins));
11770 sprintf(buf, "%s vs. %s", gameInfo.white, gameInfo.black);
11776 TwoMachinesEvent P((void))
11780 ChessProgramState *onmove;
11781 char *bookHit = NULL;
11783 if (appData.noChessProgram) return;
11785 switch (gameMode) {
11786 case TwoMachinesPlay:
11788 case MachinePlaysWhite:
11789 case MachinePlaysBlack:
11790 if (WhiteOnMove(forwardMostMove) == (gameMode == MachinePlaysWhite)) {
11791 DisplayError(_("Wait until your turn,\nor select Move Now"), 0);
11795 case BeginningOfGame:
11796 case PlayFromGameFile:
11799 if (gameMode != EditGame) return;
11802 EditPositionDone(TRUE);
11813 // forwardMostMove = currentMove;
11814 TruncateGame(); // [HGM] vari: MachineWhite and MachineBlack do this...
11815 ResurrectChessProgram(); /* in case first program isn't running */
11817 if (second.pr == NULL) {
11818 StartChessProgram(&second);
11819 if (second.protocolVersion == 1) {
11820 TwoMachinesEventIfReady();
11822 /* kludge: allow timeout for initial "feature" command */
11824 DisplayMessage("", _("Starting second chess program"));
11825 ScheduleDelayedEvent(TwoMachinesEventIfReady, FEATURE_TIMEOUT);
11829 DisplayMessage("", "");
11830 InitChessProgram(&second, FALSE);
11831 SendToProgram("force\n", &second);
11832 if (startedFromSetupPosition) {
11833 SendBoard(&second, backwardMostMove);
11834 if (appData.debugMode) {
11835 fprintf(debugFP, "Two Machines\n");
11838 for (i = backwardMostMove; i < forwardMostMove; i++) {
11839 SendMoveToProgram(i, &second);
11842 gameMode = TwoMachinesPlay;
11846 DisplayTwoMachinesTitle();
11848 if ((first.twoMachinesColor[0] == 'w') == WhiteOnMove(forwardMostMove)) {
11854 SendToProgram(first.computerString, &first);
11855 if (first.sendName) {
11856 sprintf(buf, "name %s\n", second.tidy);
11857 SendToProgram(buf, &first);
11859 SendToProgram(second.computerString, &second);
11860 if (second.sendName) {
11861 sprintf(buf, "name %s\n", first.tidy);
11862 SendToProgram(buf, &second);
11866 if (!first.sendTime || !second.sendTime) {
11867 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
11868 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
11870 if (onmove->sendTime) {
11871 if (onmove->useColors) {
11872 SendToProgram(onmove->other->twoMachinesColor, onmove); /*gnu kludge*/
11874 SendTimeRemaining(onmove, WhiteOnMove(forwardMostMove));
11876 if (onmove->useColors) {
11877 SendToProgram(onmove->twoMachinesColor, onmove);
11879 bookHit = SendMoveToBookUser(forwardMostMove-1, onmove, TRUE); // [HGM] book: send go or retrieve book move
11880 // SendToProgram("go\n", onmove);
11881 onmove->maybeThinking = TRUE;
11882 SetMachineThinkingEnables();
11886 if(bookHit) { // [HGM] book: simulate book reply
11887 static char bookMove[MSG_SIZ]; // a bit generous?
11889 programStats.nodes = programStats.depth = programStats.time =
11890 programStats.score = programStats.got_only_move = 0;
11891 sprintf(programStats.movelist, "%s (xbook)", bookHit);
11893 strcpy(bookMove, "move ");
11894 strcat(bookMove, bookHit);
11895 savedMessage = bookMove; // args for deferred call
11896 savedState = onmove;
11897 ScheduleDelayedEvent(DeferredBookMove, 1);
11904 if (gameMode == Training) {
11905 SetTrainingModeOff();
11906 gameMode = PlayFromGameFile;
11907 DisplayMessage("", _("Training mode off"));
11909 gameMode = Training;
11910 animateTraining = appData.animate;
11912 /* make sure we are not already at the end of the game */
11913 if (currentMove < forwardMostMove) {
11914 SetTrainingModeOn();
11915 DisplayMessage("", _("Training mode on"));
11917 gameMode = PlayFromGameFile;
11918 DisplayError(_("Already at end of game"), 0);
11927 if (!appData.icsActive) return;
11928 switch (gameMode) {
11929 case IcsPlayingWhite:
11930 case IcsPlayingBlack:
11933 case BeginningOfGame:
11941 EditPositionDone(TRUE);
11954 gameMode = IcsIdle;
11965 switch (gameMode) {
11967 SetTrainingModeOff();
11969 case MachinePlaysWhite:
11970 case MachinePlaysBlack:
11971 case BeginningOfGame:
11972 SendToProgram("force\n", &first);
11973 SetUserThinkingEnables();
11975 case PlayFromGameFile:
11976 (void) StopLoadGameTimer();
11977 if (gameFileFP != NULL) {
11982 EditPositionDone(TRUE);
11987 SendToProgram("force\n", &first);
11989 case TwoMachinesPlay:
11990 GameEnds((ChessMove) 0, NULL, GE_PLAYER);
11991 ResurrectChessProgram();
11992 SetUserThinkingEnables();
11995 ResurrectChessProgram();
11997 case IcsPlayingBlack:
11998 case IcsPlayingWhite:
11999 DisplayError(_("Warning: You are still playing a game"), 0);
12002 DisplayError(_("Warning: You are still observing a game"), 0);
12005 DisplayError(_("Warning: You are still examining a game"), 0);
12016 first.offeredDraw = second.offeredDraw = 0;
12018 if (gameMode == PlayFromGameFile) {
12019 whiteTimeRemaining = timeRemaining[0][currentMove];
12020 blackTimeRemaining = timeRemaining[1][currentMove];
12024 if (gameMode == MachinePlaysWhite ||
12025 gameMode == MachinePlaysBlack ||
12026 gameMode == TwoMachinesPlay ||
12027 gameMode == EndOfGame) {
12028 i = forwardMostMove;
12029 while (i > currentMove) {
12030 SendToProgram("undo\n", &first);
12033 whiteTimeRemaining = timeRemaining[0][currentMove];
12034 blackTimeRemaining = timeRemaining[1][currentMove];
12035 DisplayBothClocks();
12036 if (whiteFlag || blackFlag) {
12037 whiteFlag = blackFlag = 0;
12042 gameMode = EditGame;
12049 EditPositionEvent()
12051 if (gameMode == EditPosition) {
12057 if (gameMode != EditGame) return;
12059 gameMode = EditPosition;
12062 if (currentMove > 0)
12063 CopyBoard(boards[0], boards[currentMove]);
12065 blackPlaysFirst = !WhiteOnMove(currentMove);
12067 currentMove = forwardMostMove = backwardMostMove = 0;
12068 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
12075 /* [DM] icsEngineAnalyze - possible call from other functions */
12076 if (appData.icsEngineAnalyze) {
12077 appData.icsEngineAnalyze = FALSE;
12079 DisplayMessage("",_("Close ICS engine analyze..."));
12081 if (first.analysisSupport && first.analyzing) {
12082 SendToProgram("exit\n", &first);
12083 first.analyzing = FALSE;
12085 thinkOutput[0] = NULLCHAR;
12089 EditPositionDone(Boolean fakeRights)
12091 int king = gameInfo.variant == VariantKnightmate ? WhiteUnicorn : WhiteKing;
12093 startedFromSetupPosition = TRUE;
12094 InitChessProgram(&first, FALSE);
12095 if(fakeRights) { // [HGM] suppress this if we just pasted a FEN.
12096 boards[0][EP_STATUS] = EP_NONE;
12097 boards[0][CASTLING][2] = boards[0][CASTLING][5] = BOARD_WIDTH>>1;
12098 if(boards[0][0][BOARD_WIDTH>>1] == king) {
12099 boards[0][CASTLING][1] = boards[0][0][BOARD_LEFT] == WhiteRook ? 0 : NoRights;
12100 boards[0][CASTLING][0] = boards[0][0][BOARD_RGHT-1] == WhiteRook ? BOARD_RGHT-1 : NoRights;
12101 } else boards[0][CASTLING][2] = NoRights;
12102 if(boards[0][BOARD_HEIGHT-1][BOARD_WIDTH>>1] == WHITE_TO_BLACK king) {
12103 boards[0][CASTLING][4] = boards[0][BOARD_HEIGHT-1][BOARD_LEFT] == BlackRook ? 0 : NoRights;
12104 boards[0][CASTLING][3] = boards[0][BOARD_HEIGHT-1][BOARD_RGHT-1] == BlackRook ? BOARD_RGHT-1 : NoRights;
12105 } else boards[0][CASTLING][5] = NoRights;
12107 SendToProgram("force\n", &first);
12108 if (blackPlaysFirst) {
12109 strcpy(moveList[0], "");
12110 strcpy(parseList[0], "");
12111 currentMove = forwardMostMove = backwardMostMove = 1;
12112 CopyBoard(boards[1], boards[0]);
12114 currentMove = forwardMostMove = backwardMostMove = 0;
12116 SendBoard(&first, forwardMostMove);
12117 if (appData.debugMode) {
12118 fprintf(debugFP, "EditPosDone\n");
12121 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
12122 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
12123 gameMode = EditGame;
12125 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
12126 ClearHighlights(); /* [AS] */
12129 /* Pause for `ms' milliseconds */
12130 /* !! Ugh, this is a kludge. Fix it sometime. --tpm */
12140 } while (SubtractTimeMarks(&m2, &m1) < ms);
12143 /* !! Ugh, this is a kludge. Fix it sometime. --tpm */
12145 SendMultiLineToICS(buf)
12148 char temp[MSG_SIZ+1], *p;
12155 strncpy(temp, buf, len);
12160 if (*p == '\n' || *p == '\r')
12165 strcat(temp, "\n");
12167 SendToPlayer(temp, strlen(temp));
12171 SetWhiteToPlayEvent()
12173 if (gameMode == EditPosition) {
12174 blackPlaysFirst = FALSE;
12175 DisplayBothClocks(); /* works because currentMove is 0 */
12176 } else if (gameMode == IcsExamining) {
12177 SendToICS(ics_prefix);
12178 SendToICS("tomove white\n");
12183 SetBlackToPlayEvent()
12185 if (gameMode == EditPosition) {
12186 blackPlaysFirst = TRUE;
12187 currentMove = 1; /* kludge */
12188 DisplayBothClocks();
12190 } else if (gameMode == IcsExamining) {
12191 SendToICS(ics_prefix);
12192 SendToICS("tomove black\n");
12197 EditPositionMenuEvent(selection, x, y)
12198 ChessSquare selection;
12202 ChessSquare piece = boards[0][y][x];
12204 if (gameMode != EditPosition && gameMode != IcsExamining) return;
12206 switch (selection) {
12208 if (gameMode == IcsExamining && ics_type == ICS_FICS) {
12209 SendToICS(ics_prefix);
12210 SendToICS("bsetup clear\n");
12211 } else if (gameMode == IcsExamining && ics_type == ICS_ICC) {
12212 SendToICS(ics_prefix);
12213 SendToICS("clearboard\n");
12215 for (x = 0; x < BOARD_WIDTH; x++) { ChessSquare p = EmptySquare;
12216 if(x == BOARD_LEFT-1 || x == BOARD_RGHT) p = (ChessSquare) 0; /* [HGM] holdings */
12217 for (y = 0; y < BOARD_HEIGHT; y++) {
12218 if (gameMode == IcsExamining) {
12219 if (boards[currentMove][y][x] != EmptySquare) {
12220 sprintf(buf, "%sx@%c%c\n", ics_prefix,
12225 boards[0][y][x] = p;
12230 if (gameMode == EditPosition) {
12231 DrawPosition(FALSE, boards[0]);
12236 SetWhiteToPlayEvent();
12240 SetBlackToPlayEvent();
12244 if (gameMode == IcsExamining) {
12245 if (x < BOARD_LEFT || x >= BOARD_RGHT) break; // [HGM] holdings
12246 sprintf(buf, "%sx@%c%c\n", ics_prefix, AAA + x, ONE + y);
12249 if(x < BOARD_LEFT || x >= BOARD_RGHT) {
12250 if(x == BOARD_LEFT-2) {
12251 if(y < BOARD_HEIGHT-1-gameInfo.holdingsSize) break;
12252 boards[0][y][1] = 0;
12254 if(x == BOARD_RGHT+1) {
12255 if(y >= gameInfo.holdingsSize) break;
12256 boards[0][y][BOARD_WIDTH-2] = 0;
12259 boards[0][y][x] = EmptySquare;
12260 DrawPosition(FALSE, boards[0]);
12265 if(piece >= (int)WhitePawn && piece < (int)WhiteMan ||
12266 piece >= (int)BlackPawn && piece < (int)BlackMan ) {
12267 selection = (ChessSquare) (PROMOTED piece);
12268 } else if(piece == EmptySquare) selection = WhiteSilver;
12269 else selection = (ChessSquare)((int)piece - 1);
12273 if(piece > (int)WhiteMan && piece <= (int)WhiteKing ||
12274 piece > (int)BlackMan && piece <= (int)BlackKing ) {
12275 selection = (ChessSquare) (DEMOTED piece);
12276 } else if(piece == EmptySquare) selection = BlackSilver;
12277 else selection = (ChessSquare)((int)piece + 1);
12282 if(gameInfo.variant == VariantShatranj ||
12283 gameInfo.variant == VariantXiangqi ||
12284 gameInfo.variant == VariantCourier ||
12285 gameInfo.variant == VariantMakruk )
12286 selection = (ChessSquare)((int)selection - (int)WhiteQueen + (int)WhiteFerz);
12291 if(gameInfo.variant == VariantXiangqi)
12292 selection = (ChessSquare)((int)selection - (int)WhiteKing + (int)WhiteWazir);
12293 if(gameInfo.variant == VariantKnightmate)
12294 selection = (ChessSquare)((int)selection - (int)WhiteKing + (int)WhiteUnicorn);
12297 if (gameMode == IcsExamining) {
12298 if (x < BOARD_LEFT || x >= BOARD_RGHT) break; // [HGM] holdings
12299 sprintf(buf, "%s%c@%c%c\n", ics_prefix,
12300 PieceToChar(selection), AAA + x, ONE + y);
12303 if(x < BOARD_LEFT || x >= BOARD_RGHT) {
12305 if(x == BOARD_LEFT-2 && selection >= BlackPawn) {
12306 n = PieceToNumber(selection - BlackPawn);
12307 if(n >= gameInfo.holdingsSize) { n = 0; selection = BlackPawn; }
12308 boards[0][BOARD_HEIGHT-1-n][0] = selection;
12309 boards[0][BOARD_HEIGHT-1-n][1]++;
12311 if(x == BOARD_RGHT+1 && selection < BlackPawn) {
12312 n = PieceToNumber(selection);
12313 if(n >= gameInfo.holdingsSize) { n = 0; selection = WhitePawn; }
12314 boards[0][n][BOARD_WIDTH-1] = selection;
12315 boards[0][n][BOARD_WIDTH-2]++;
12318 boards[0][y][x] = selection;
12319 DrawPosition(TRUE, boards[0]);
12327 DropMenuEvent(selection, x, y)
12328 ChessSquare selection;
12331 ChessMove moveType;
12333 switch (gameMode) {
12334 case IcsPlayingWhite:
12335 case MachinePlaysBlack:
12336 if (!WhiteOnMove(currentMove)) {
12337 DisplayMoveError(_("It is Black's turn"));
12340 moveType = WhiteDrop;
12342 case IcsPlayingBlack:
12343 case MachinePlaysWhite:
12344 if (WhiteOnMove(currentMove)) {
12345 DisplayMoveError(_("It is White's turn"));
12348 moveType = BlackDrop;
12351 moveType = WhiteOnMove(currentMove) ? WhiteDrop : BlackDrop;
12357 if (moveType == BlackDrop && selection < BlackPawn) {
12358 selection = (ChessSquare) ((int) selection
12359 + (int) BlackPawn - (int) WhitePawn);
12361 if (boards[currentMove][y][x] != EmptySquare) {
12362 DisplayMoveError(_("That square is occupied"));
12366 FinishMove(moveType, (int) selection, DROP_RANK, x, y, NULLCHAR);
12372 /* Accept a pending offer of any kind from opponent */
12374 if (appData.icsActive) {
12375 SendToICS(ics_prefix);
12376 SendToICS("accept\n");
12377 } else if (cmailMsgLoaded) {
12378 if (currentMove == cmailOldMove &&
12379 commentList[cmailOldMove] != NULL &&
12380 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
12381 "Black offers a draw" : "White offers a draw")) {
12383 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
12384 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_ACCEPT;
12386 DisplayError(_("There is no pending offer on this move"), 0);
12387 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
12390 /* Not used for offers from chess program */
12397 /* Decline a pending offer of any kind from opponent */
12399 if (appData.icsActive) {
12400 SendToICS(ics_prefix);
12401 SendToICS("decline\n");
12402 } else if (cmailMsgLoaded) {
12403 if (currentMove == cmailOldMove &&
12404 commentList[cmailOldMove] != NULL &&
12405 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
12406 "Black offers a draw" : "White offers a draw")) {
12408 AppendComment(cmailOldMove, "Draw declined", TRUE);
12409 DisplayComment(cmailOldMove - 1, "Draw declined");
12412 DisplayError(_("There is no pending offer on this move"), 0);
12415 /* Not used for offers from chess program */
12422 /* Issue ICS rematch command */
12423 if (appData.icsActive) {
12424 SendToICS(ics_prefix);
12425 SendToICS("rematch\n");
12432 /* Call your opponent's flag (claim a win on time) */
12433 if (appData.icsActive) {
12434 SendToICS(ics_prefix);
12435 SendToICS("flag\n");
12437 switch (gameMode) {
12440 case MachinePlaysWhite:
12443 GameEnds(GameIsDrawn, "Both players ran out of time",
12446 GameEnds(BlackWins, "Black wins on time", GE_PLAYER);
12448 DisplayError(_("Your opponent is not out of time"), 0);
12451 case MachinePlaysBlack:
12454 GameEnds(GameIsDrawn, "Both players ran out of time",
12457 GameEnds(WhiteWins, "White wins on time", GE_PLAYER);
12459 DisplayError(_("Your opponent is not out of time"), 0);
12469 /* Offer draw or accept pending draw offer from opponent */
12471 if (appData.icsActive) {
12472 /* Note: tournament rules require draw offers to be
12473 made after you make your move but before you punch
12474 your clock. Currently ICS doesn't let you do that;
12475 instead, you immediately punch your clock after making
12476 a move, but you can offer a draw at any time. */
12478 SendToICS(ics_prefix);
12479 SendToICS("draw\n");
12480 userOfferedDraw = TRUE; // [HGM] drawclaim: also set flag in ICS play
12481 } else if (cmailMsgLoaded) {
12482 if (currentMove == cmailOldMove &&
12483 commentList[cmailOldMove] != NULL &&
12484 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
12485 "Black offers a draw" : "White offers a draw")) {
12486 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
12487 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_ACCEPT;
12488 } else if (currentMove == cmailOldMove + 1) {
12489 char *offer = WhiteOnMove(cmailOldMove) ?
12490 "White offers a draw" : "Black offers a draw";
12491 AppendComment(currentMove, offer, TRUE);
12492 DisplayComment(currentMove - 1, offer);
12493 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_DRAW;
12495 DisplayError(_("You must make your move before offering a draw"), 0);
12496 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
12498 } else if (first.offeredDraw) {
12499 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
12501 if (first.sendDrawOffers) {
12502 SendToProgram("draw\n", &first);
12503 userOfferedDraw = TRUE;
12511 /* Offer Adjourn or accept pending Adjourn offer from opponent */
12513 if (appData.icsActive) {
12514 SendToICS(ics_prefix);
12515 SendToICS("adjourn\n");
12517 /* Currently GNU Chess doesn't offer or accept Adjourns */
12525 /* Offer Abort or accept pending Abort offer from opponent */
12527 if (appData.icsActive) {
12528 SendToICS(ics_prefix);
12529 SendToICS("abort\n");
12531 GameEnds(GameUnfinished, "Game aborted", GE_PLAYER);
12538 /* Resign. You can do this even if it's not your turn. */
12540 if (appData.icsActive) {
12541 SendToICS(ics_prefix);
12542 SendToICS("resign\n");
12544 switch (gameMode) {
12545 case MachinePlaysWhite:
12546 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
12548 case MachinePlaysBlack:
12549 GameEnds(BlackWins, "White resigns", GE_PLAYER);
12552 if (cmailMsgLoaded) {
12554 if (WhiteOnMove(cmailOldMove)) {
12555 GameEnds(BlackWins, "White resigns", GE_PLAYER);
12557 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
12559 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_RESIGN;
12570 StopObservingEvent()
12572 /* Stop observing current games */
12573 SendToICS(ics_prefix);
12574 SendToICS("unobserve\n");
12578 StopExaminingEvent()
12580 /* Stop observing current game */
12581 SendToICS(ics_prefix);
12582 SendToICS("unexamine\n");
12586 ForwardInner(target)
12591 if (appData.debugMode)
12592 fprintf(debugFP, "ForwardInner(%d), current %d, forward %d\n",
12593 target, currentMove, forwardMostMove);
12595 if (gameMode == EditPosition)
12598 if (gameMode == PlayFromGameFile && !pausing)
12601 if (gameMode == IcsExamining && pausing)
12602 limit = pauseExamForwardMostMove;
12604 limit = forwardMostMove;
12606 if (target > limit) target = limit;
12608 if (target > 0 && moveList[target - 1][0]) {
12609 int fromX, fromY, toX, toY;
12610 toX = moveList[target - 1][2] - AAA;
12611 toY = moveList[target - 1][3] - ONE;
12612 if (moveList[target - 1][1] == '@') {
12613 if (appData.highlightLastMove) {
12614 SetHighlights(-1, -1, toX, toY);
12617 fromX = moveList[target - 1][0] - AAA;
12618 fromY = moveList[target - 1][1] - ONE;
12619 if (target == currentMove + 1) {
12620 AnimateMove(boards[currentMove], fromX, fromY, toX, toY);
12622 if (appData.highlightLastMove) {
12623 SetHighlights(fromX, fromY, toX, toY);
12627 if (gameMode == EditGame || gameMode == AnalyzeMode ||
12628 gameMode == Training || gameMode == PlayFromGameFile ||
12629 gameMode == AnalyzeFile) {
12630 while (currentMove < target) {
12631 SendMoveToProgram(currentMove++, &first);
12634 currentMove = target;
12637 if (gameMode == EditGame || gameMode == EndOfGame) {
12638 whiteTimeRemaining = timeRemaining[0][currentMove];
12639 blackTimeRemaining = timeRemaining[1][currentMove];
12641 DisplayBothClocks();
12642 DisplayMove(currentMove - 1);
12643 DrawPosition(FALSE, boards[currentMove]);
12644 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
12645 if ( !matchMode && gameMode != Training) { // [HGM] PV info: routine tests if empty
12646 DisplayComment(currentMove - 1, commentList[currentMove]);
12654 if (gameMode == IcsExamining && !pausing) {
12655 SendToICS(ics_prefix);
12656 SendToICS("forward\n");
12658 ForwardInner(currentMove + 1);
12665 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
12666 /* to optimze, we temporarily turn off analysis mode while we feed
12667 * the remaining moves to the engine. Otherwise we get analysis output
12670 if (first.analysisSupport) {
12671 SendToProgram("exit\nforce\n", &first);
12672 first.analyzing = FALSE;
12676 if (gameMode == IcsExamining && !pausing) {
12677 SendToICS(ics_prefix);
12678 SendToICS("forward 999999\n");
12680 ForwardInner(forwardMostMove);
12683 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
12684 /* we have fed all the moves, so reactivate analysis mode */
12685 SendToProgram("analyze\n", &first);
12686 first.analyzing = TRUE;
12687 /*first.maybeThinking = TRUE;*/
12688 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
12693 BackwardInner(target)
12696 int full_redraw = TRUE; /* [AS] Was FALSE, had to change it! */
12698 if (appData.debugMode)
12699 fprintf(debugFP, "BackwardInner(%d), current %d, forward %d\n",
12700 target, currentMove, forwardMostMove);
12702 if (gameMode == EditPosition) return;
12703 if (currentMove <= backwardMostMove) {
12705 DrawPosition(full_redraw, boards[currentMove]);
12708 if (gameMode == PlayFromGameFile && !pausing)
12711 if (moveList[target][0]) {
12712 int fromX, fromY, toX, toY;
12713 toX = moveList[target][2] - AAA;
12714 toY = moveList[target][3] - ONE;
12715 if (moveList[target][1] == '@') {
12716 if (appData.highlightLastMove) {
12717 SetHighlights(-1, -1, toX, toY);
12720 fromX = moveList[target][0] - AAA;
12721 fromY = moveList[target][1] - ONE;
12722 if (target == currentMove - 1) {
12723 AnimateMove(boards[currentMove], toX, toY, fromX, fromY);
12725 if (appData.highlightLastMove) {
12726 SetHighlights(fromX, fromY, toX, toY);
12730 if (gameMode == EditGame || gameMode==AnalyzeMode ||
12731 gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
12732 while (currentMove > target) {
12733 SendToProgram("undo\n", &first);
12737 currentMove = target;
12740 if (gameMode == EditGame || gameMode == EndOfGame) {
12741 whiteTimeRemaining = timeRemaining[0][currentMove];
12742 blackTimeRemaining = timeRemaining[1][currentMove];
12744 DisplayBothClocks();
12745 DisplayMove(currentMove - 1);
12746 DrawPosition(full_redraw, boards[currentMove]);
12747 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
12748 // [HGM] PV info: routine tests if comment empty
12749 DisplayComment(currentMove - 1, commentList[currentMove]);
12755 if (gameMode == IcsExamining && !pausing) {
12756 SendToICS(ics_prefix);
12757 SendToICS("backward\n");
12759 BackwardInner(currentMove - 1);
12766 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
12767 /* to optimize, we temporarily turn off analysis mode while we undo
12768 * all the moves. Otherwise we get analysis output after each undo.
12770 if (first.analysisSupport) {
12771 SendToProgram("exit\nforce\n", &first);
12772 first.analyzing = FALSE;
12776 if (gameMode == IcsExamining && !pausing) {
12777 SendToICS(ics_prefix);
12778 SendToICS("backward 999999\n");
12780 BackwardInner(backwardMostMove);
12783 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
12784 /* we have fed all the moves, so reactivate analysis mode */
12785 SendToProgram("analyze\n", &first);
12786 first.analyzing = TRUE;
12787 /*first.maybeThinking = TRUE;*/
12788 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
12795 if (gameMode == PlayFromGameFile && !pausing) PauseEvent();
12796 if (to >= forwardMostMove) to = forwardMostMove;
12797 if (to <= backwardMostMove) to = backwardMostMove;
12798 if (to < currentMove) {
12806 RevertEvent(Boolean annotate)
12808 if(PopTail(annotate)) { // [HGM] vari: restore old game tail
12811 if (gameMode != IcsExamining) {
12812 DisplayError(_("You are not examining a game"), 0);
12816 DisplayError(_("You can't revert while pausing"), 0);
12819 SendToICS(ics_prefix);
12820 SendToICS("revert\n");
12826 switch (gameMode) {
12827 case MachinePlaysWhite:
12828 case MachinePlaysBlack:
12829 if (WhiteOnMove(forwardMostMove) == (gameMode == MachinePlaysWhite)) {
12830 DisplayError(_("Wait until your turn,\nor select Move Now"), 0);
12833 if (forwardMostMove < 2) return;
12834 currentMove = forwardMostMove = forwardMostMove - 2;
12835 whiteTimeRemaining = timeRemaining[0][currentMove];
12836 blackTimeRemaining = timeRemaining[1][currentMove];
12837 DisplayBothClocks();
12838 DisplayMove(currentMove - 1);
12839 ClearHighlights();/*!! could figure this out*/
12840 DrawPosition(TRUE, boards[currentMove]); /* [AS] Changed to full redraw! */
12841 SendToProgram("remove\n", &first);
12842 /*first.maybeThinking = TRUE;*/ /* GNU Chess does not ponder here */
12845 case BeginningOfGame:
12849 case IcsPlayingWhite:
12850 case IcsPlayingBlack:
12851 if (WhiteOnMove(forwardMostMove) == (gameMode == IcsPlayingWhite)) {
12852 SendToICS(ics_prefix);
12853 SendToICS("takeback 2\n");
12855 SendToICS(ics_prefix);
12856 SendToICS("takeback 1\n");
12865 ChessProgramState *cps;
12867 switch (gameMode) {
12868 case MachinePlaysWhite:
12869 if (!WhiteOnMove(forwardMostMove)) {
12870 DisplayError(_("It is your turn"), 0);
12875 case MachinePlaysBlack:
12876 if (WhiteOnMove(forwardMostMove)) {
12877 DisplayError(_("It is your turn"), 0);
12882 case TwoMachinesPlay:
12883 if (WhiteOnMove(forwardMostMove) ==
12884 (first.twoMachinesColor[0] == 'w')) {
12890 case BeginningOfGame:
12894 SendToProgram("?\n", cps);
12898 TruncateGameEvent()
12901 if (gameMode != EditGame) return;
12908 CleanupTail(); // [HGM] vari: only keep current variation if we explicitly truncate
12909 if (forwardMostMove > currentMove) {
12910 if (gameInfo.resultDetails != NULL) {
12911 free(gameInfo.resultDetails);
12912 gameInfo.resultDetails = NULL;
12913 gameInfo.result = GameUnfinished;
12915 forwardMostMove = currentMove;
12916 HistorySet(parseList, backwardMostMove, forwardMostMove,
12924 if (appData.noChessProgram) return;
12925 switch (gameMode) {
12926 case MachinePlaysWhite:
12927 if (WhiteOnMove(forwardMostMove)) {
12928 DisplayError(_("Wait until your turn"), 0);
12932 case BeginningOfGame:
12933 case MachinePlaysBlack:
12934 if (!WhiteOnMove(forwardMostMove)) {
12935 DisplayError(_("Wait until your turn"), 0);
12940 DisplayError(_("No hint available"), 0);
12943 SendToProgram("hint\n", &first);
12944 hintRequested = TRUE;
12950 if (appData.noChessProgram) return;
12951 switch (gameMode) {
12952 case MachinePlaysWhite:
12953 if (WhiteOnMove(forwardMostMove)) {
12954 DisplayError(_("Wait until your turn"), 0);
12958 case BeginningOfGame:
12959 case MachinePlaysBlack:
12960 if (!WhiteOnMove(forwardMostMove)) {
12961 DisplayError(_("Wait until your turn"), 0);
12966 EditPositionDone(TRUE);
12968 case TwoMachinesPlay:
12973 SendToProgram("bk\n", &first);
12974 bookOutput[0] = NULLCHAR;
12975 bookRequested = TRUE;
12981 char *tags = PGNTags(&gameInfo);
12982 TagsPopUp(tags, CmailMsg());
12986 /* end button procedures */
12989 PrintPosition(fp, move)
12995 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
12996 for (j = BOARD_LEFT; j < BOARD_RGHT; j++) {
12997 char c = PieceToChar(boards[move][i][j]);
12998 fputc(c == 'x' ? '.' : c, fp);
12999 fputc(j == BOARD_RGHT - 1 ? '\n' : ' ', fp);
13002 if ((gameMode == EditPosition) ? !blackPlaysFirst : (move % 2 == 0))
13003 fprintf(fp, "white to play\n");
13005 fprintf(fp, "black to play\n");
13012 if (gameInfo.white != NULL) {
13013 fprintf(fp, "\t%s vs. %s\n", gameInfo.white, gameInfo.black);
13019 /* Find last component of program's own name, using some heuristics */
13021 TidyProgramName(prog, host, buf)
13022 char *prog, *host, buf[MSG_SIZ];
13025 int local = (strcmp(host, "localhost") == 0);
13026 while (!local && (p = strchr(prog, ';')) != NULL) {
13028 while (*p == ' ') p++;
13031 if (*prog == '"' || *prog == '\'') {
13032 q = strchr(prog + 1, *prog);
13034 q = strchr(prog, ' ');
13036 if (q == NULL) q = prog + strlen(prog);
13038 while (p >= prog && *p != '/' && *p != '\\') p--;
13040 if(p == prog && *p == '"') p++;
13041 if (q - p >= 4 && StrCaseCmp(q - 4, ".exe") == 0) q -= 4;
13042 memcpy(buf, p, q - p);
13043 buf[q - p] = NULLCHAR;
13051 TimeControlTagValue()
13054 if (!appData.clockMode) {
13056 } else if (movesPerSession > 0) {
13057 sprintf(buf, "%d/%ld", movesPerSession, timeControl/1000);
13058 } else if (timeIncrement == 0) {
13059 sprintf(buf, "%ld", timeControl/1000);
13061 sprintf(buf, "%ld+%ld", timeControl/1000, timeIncrement/1000);
13063 return StrSave(buf);
13069 /* This routine is used only for certain modes */
13070 VariantClass v = gameInfo.variant;
13071 ChessMove r = GameUnfinished;
13074 if(gameMode == EditGame) { // [HGM] vari: do not erase result on EditGame
13075 r = gameInfo.result;
13076 p = gameInfo.resultDetails;
13077 gameInfo.resultDetails = NULL;
13079 ClearGameInfo(&gameInfo);
13080 gameInfo.variant = v;
13082 switch (gameMode) {
13083 case MachinePlaysWhite:
13084 gameInfo.event = StrSave( appData.pgnEventHeader );
13085 gameInfo.site = StrSave(HostName());
13086 gameInfo.date = PGNDate();
13087 gameInfo.round = StrSave("-");
13088 gameInfo.white = StrSave(first.tidy);
13089 gameInfo.black = StrSave(UserName());
13090 gameInfo.timeControl = TimeControlTagValue();
13093 case MachinePlaysBlack:
13094 gameInfo.event = StrSave( appData.pgnEventHeader );
13095 gameInfo.site = StrSave(HostName());
13096 gameInfo.date = PGNDate();
13097 gameInfo.round = StrSave("-");
13098 gameInfo.white = StrSave(UserName());
13099 gameInfo.black = StrSave(first.tidy);
13100 gameInfo.timeControl = TimeControlTagValue();
13103 case TwoMachinesPlay:
13104 gameInfo.event = StrSave( appData.pgnEventHeader );
13105 gameInfo.site = StrSave(HostName());
13106 gameInfo.date = PGNDate();
13107 if (matchGame > 0) {
13109 sprintf(buf, "%d", matchGame);
13110 gameInfo.round = StrSave(buf);
13112 gameInfo.round = StrSave("-");
13114 if (first.twoMachinesColor[0] == 'w') {
13115 gameInfo.white = StrSave(first.tidy);
13116 gameInfo.black = StrSave(second.tidy);
13118 gameInfo.white = StrSave(second.tidy);
13119 gameInfo.black = StrSave(first.tidy);
13121 gameInfo.timeControl = TimeControlTagValue();
13125 gameInfo.event = StrSave("Edited game");
13126 gameInfo.site = StrSave(HostName());
13127 gameInfo.date = PGNDate();
13128 gameInfo.round = StrSave("-");
13129 gameInfo.white = StrSave("-");
13130 gameInfo.black = StrSave("-");
13131 gameInfo.result = r;
13132 gameInfo.resultDetails = p;
13136 gameInfo.event = StrSave("Edited position");
13137 gameInfo.site = StrSave(HostName());
13138 gameInfo.date = PGNDate();
13139 gameInfo.round = StrSave("-");
13140 gameInfo.white = StrSave("-");
13141 gameInfo.black = StrSave("-");
13144 case IcsPlayingWhite:
13145 case IcsPlayingBlack:
13150 case PlayFromGameFile:
13151 gameInfo.event = StrSave("Game from non-PGN file");
13152 gameInfo.site = StrSave(HostName());
13153 gameInfo.date = PGNDate();
13154 gameInfo.round = StrSave("-");
13155 gameInfo.white = StrSave("?");
13156 gameInfo.black = StrSave("?");
13165 ReplaceComment(index, text)
13171 while (*text == '\n') text++;
13172 len = strlen(text);
13173 while (len > 0 && text[len - 1] == '\n') len--;
13175 if (commentList[index] != NULL)
13176 free(commentList[index]);
13179 commentList[index] = NULL;
13182 if( *text == '{' && strchr(text, '}') || // [HGM] braces: if certainy malformed, put braces
13183 *text == '[' && strchr(text, ']') || // otherwise hope the user knows what he is doing
13184 *text == '(' && strchr(text, ')')) { // (perhaps check if this parses as comment-only?)
13185 commentList[index] = (char *) malloc(len + 2);
13186 strncpy(commentList[index], text, len);
13187 commentList[index][len] = '\n';
13188 commentList[index][len + 1] = NULLCHAR;
13190 // [HGM] braces: if text does not start with known OK delimiter, put braces around it.
13192 commentList[index] = (char *) malloc(len + 6);
13193 strcpy(commentList[index], "{\n");
13194 strncpy(commentList[index]+2, text, len);
13195 commentList[index][len+2] = NULLCHAR;
13196 while(p = strchr(commentList[index], '}')) *p = ')'; // kill all } to make it one comment
13197 strcat(commentList[index], "\n}\n");
13211 if (ch == '\r') continue;
13213 } while (ch != '\0');
13217 AppendComment(index, text, addBraces)
13220 Boolean addBraces; // [HGM] braces: tells if we should add {}
13225 if(appData.debugMode) fprintf(debugFP, "Append: in='%s' %d\n", text, addBraces); fflush(debugFP);
13226 text = GetInfoFromComment( index, text ); /* [HGM] PV time: strip PV info from comment */
13229 while (*text == '\n') text++;
13230 len = strlen(text);
13231 while (len > 0 && text[len - 1] == '\n') len--;
13233 if (len == 0) return;
13235 if (commentList[index] != NULL) {
13236 old = commentList[index];
13237 oldlen = strlen(old);
13238 while(commentList[index][oldlen-1] == '\n')
13239 commentList[index][--oldlen] = NULLCHAR;
13240 commentList[index] = (char *) malloc(oldlen + len + 6); // might waste 4
13241 strcpy(commentList[index], old);
13243 // [HGM] braces: join "{A\n}\n" + "{\nB}" as "{A\nB\n}"
13244 if(commentList[index][oldlen-1] == '}' && (text[0] == '{' || addBraces)) {
13245 if(addBraces) addBraces = FALSE; else { text++; len--; }
13246 while (*text == '\n') { text++; len--; }
13247 commentList[index][--oldlen] = NULLCHAR;
13249 if(addBraces) strcat(commentList[index], "\n{\n");
13250 else strcat(commentList[index], "\n");
13251 strcat(commentList[index], text);
13252 if(addBraces) strcat(commentList[index], "\n}\n");
13253 else strcat(commentList[index], "\n");
13255 commentList[index] = (char *) malloc(len + 6); // perhaps wastes 4...
13257 strcpy(commentList[index], "{\n");
13258 else commentList[index][0] = NULLCHAR;
13259 strcat(commentList[index], text);
13260 strcat(commentList[index], "\n");
13261 if(addBraces) strcat(commentList[index], "}\n");
13265 static char * FindStr( char * text, char * sub_text )
13267 char * result = strstr( text, sub_text );
13269 if( result != NULL ) {
13270 result += strlen( sub_text );
13276 /* [AS] Try to extract PV info from PGN comment */
13277 /* [HGM] PV time: and then remove it, to prevent it appearing twice */
13278 char *GetInfoFromComment( int index, char * text )
13282 if( text != NULL && index > 0 ) {
13285 int time = -1, sec = 0, deci;
13286 char * s_eval = FindStr( text, "[%eval " );
13287 char * s_emt = FindStr( text, "[%emt " );
13289 if( s_eval != NULL || s_emt != NULL ) {
13293 if( s_eval != NULL ) {
13294 if( sscanf( s_eval, "%d,%d%c", &score, &depth, &delim ) != 3 ) {
13298 if( delim != ']' ) {
13303 if( s_emt != NULL ) {
13308 /* We expect something like: [+|-]nnn.nn/dd */
13311 if(*text != '{') return text; // [HGM] braces: must be normal comment
13313 sep = strchr( text, '/' );
13314 if( sep == NULL || sep < (text+4) ) {
13318 time = -1; sec = -1; deci = -1;
13319 if( sscanf( text+1, "%d.%d/%d %d:%d", &score, &score_lo, &depth, &time, &sec ) != 5 &&
13320 sscanf( text+1, "%d.%d/%d %d.%d", &score, &score_lo, &depth, &time, &deci ) != 5 &&
13321 sscanf( text+1, "%d.%d/%d %d", &score, &score_lo, &depth, &time ) != 4 &&
13322 sscanf( text+1, "%d.%d/%d", &score, &score_lo, &depth ) != 3 ) {
13326 if( score_lo < 0 || score_lo >= 100 ) {
13330 if(sec >= 0) time = 600*time + 10*sec; else
13331 if(deci >= 0) time = 10*time + deci; else time *= 10; // deci-sec
13333 score = score >= 0 ? score*100 + score_lo : score*100 - score_lo;
13335 /* [HGM] PV time: now locate end of PV info */
13336 while( *++sep >= '0' && *sep <= '9'); // strip depth
13338 while( *++sep >= '0' && *sep <= '9'); // strip time
13340 while( *++sep >= '0' && *sep <= '9'); // strip seconds
13342 while( *++sep >= '0' && *sep <= '9'); // strip fractional seconds
13343 while(*sep == ' ') sep++;
13354 pvInfoList[index-1].depth = depth;
13355 pvInfoList[index-1].score = score;
13356 pvInfoList[index-1].time = 10*time; // centi-sec
13357 if(*sep == '}') *sep = 0; else *--sep = '{';
13363 SendToProgram(message, cps)
13365 ChessProgramState *cps;
13367 int count, outCount, error;
13370 if (cps->pr == NULL) return;
13373 if (appData.debugMode) {
13376 fprintf(debugFP, "%ld >%-6s: %s",
13377 SubtractTimeMarks(&now, &programStartTime),
13378 cps->which, message);
13381 count = strlen(message);
13382 outCount = OutputToProcess(cps->pr, message, count, &error);
13383 if (outCount < count && !exiting
13384 && !endingGame) { /* [HGM] crash: to not hang GameEnds() writing to deceased engines */
13385 sprintf(buf, _("Error writing to %s chess program"), cps->which);
13386 if(gameInfo.resultDetails==NULL) { /* [HGM] crash: if game in progress, give reason for abort */
13387 if((signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
13388 gameInfo.result = GameIsDrawn; /* [HGM] accept exit as draw claim */
13389 sprintf(buf, "%s program exits in draw position (%s)", cps->which, cps->program);
13391 gameInfo.result = cps->twoMachinesColor[0]=='w' ? BlackWins : WhiteWins;
13393 gameInfo.resultDetails = StrSave(buf);
13395 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, error, 1); else errorExitStatus = 1;
13400 ReceiveFromProgram(isr, closure, message, count, error)
13401 InputSourceRef isr;
13409 ChessProgramState *cps = (ChessProgramState *)closure;
13411 if (isr != cps->isr) return; /* Killed intentionally */
13415 _("Error: %s chess program (%s) exited unexpectedly"),
13416 cps->which, cps->program);
13417 if(gameInfo.resultDetails==NULL) { /* [HGM] crash: if game in progress, give reason for abort */
13418 if((signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
13419 gameInfo.result = GameIsDrawn; /* [HGM] accept exit as draw claim */
13420 sprintf(buf, _("%s program exits in draw position (%s)"), cps->which, cps->program);
13422 gameInfo.result = cps->twoMachinesColor[0]=='w' ? BlackWins : WhiteWins;
13424 gameInfo.resultDetails = StrSave(buf);
13426 RemoveInputSource(cps->isr);
13427 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, 0, 1); else errorExitStatus = 1;
13430 _("Error reading from %s chess program (%s)"),
13431 cps->which, cps->program);
13432 RemoveInputSource(cps->isr);
13434 /* [AS] Program is misbehaving badly... kill it */
13435 if( count == -2 ) {
13436 DestroyChildProcess( cps->pr, 9 );
13440 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, error, 1); else errorExitStatus = 1;
13445 if ((end_str = strchr(message, '\r')) != NULL)
13446 *end_str = NULLCHAR;
13447 if ((end_str = strchr(message, '\n')) != NULL)
13448 *end_str = NULLCHAR;
13450 if (appData.debugMode) {
13451 TimeMark now; int print = 1;
13452 char *quote = ""; char c; int i;
13454 if(appData.engineComments != 1) { /* [HGM] debug: decide if protocol-violating output is written */
13455 char start = message[0];
13456 if(start >='A' && start <= 'Z') start += 'a' - 'A'; // be tolerant to capitalizing
13457 if(sscanf(message, "%d%c%d%d%d", &i, &c, &i, &i, &i) != 5 &&
13458 sscanf(message, "move %c", &c)!=1 && sscanf(message, "offer%c", &c)!=1 &&
13459 sscanf(message, "resign%c", &c)!=1 && sscanf(message, "feature %c", &c)!=1 &&
13460 sscanf(message, "error %c", &c)!=1 && sscanf(message, "illegal %c", &c)!=1 &&
13461 sscanf(message, "tell%c", &c)!=1 && sscanf(message, "0-1 %c", &c)!=1 &&
13462 sscanf(message, "1-0 %c", &c)!=1 && sscanf(message, "1/2-1/2 %c", &c)!=1 &&
13463 sscanf(message, "pong %c", &c)!=1 && start != '#') {
13464 quote = appData.engineComments == 2 ? "# " : "### NON-COMPLIANT! ### ";
13465 print = (appData.engineComments >= 2);
13467 message[0] = start; // restore original message
13471 fprintf(debugFP, "%ld <%-6s: %s%s\n",
13472 SubtractTimeMarks(&now, &programStartTime), cps->which,
13478 /* [DM] if icsEngineAnalyze is active we block all whisper and kibitz output, because nobody want to see this */
13479 if (appData.icsEngineAnalyze) {
13480 if (strstr(message, "whisper") != NULL ||
13481 strstr(message, "kibitz") != NULL ||
13482 strstr(message, "tellics") != NULL) return;
13485 HandleMachineMove(message, cps);
13490 SendTimeControl(cps, mps, tc, inc, sd, st)
13491 ChessProgramState *cps;
13492 int mps, inc, sd, st;
13498 if( timeControl_2 > 0 ) {
13499 if( (gameMode == MachinePlaysBlack) || (gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b') ) {
13500 tc = timeControl_2;
13503 tc /= cps->timeOdds; /* [HGM] time odds: apply before telling engine */
13504 inc /= cps->timeOdds;
13505 st /= cps->timeOdds;
13507 seconds = (tc / 1000) % 60; /* [HGM] displaced to after applying odds */
13510 /* Set exact time per move, normally using st command */
13511 if (cps->stKludge) {
13512 /* GNU Chess 4 has no st command; uses level in a nonstandard way */
13514 if (seconds == 0) {
13515 sprintf(buf, "level 1 %d\n", st/60);
13517 sprintf(buf, "level 1 %d:%02d\n", st/60, seconds);
13520 sprintf(buf, "st %d\n", st);
13523 /* Set conventional or incremental time control, using level command */
13524 if (seconds == 0) {
13525 /* Note old gnuchess bug -- minutes:seconds used to not work.
13526 Fixed in later versions, but still avoid :seconds
13527 when seconds is 0. */
13528 sprintf(buf, "level %d %ld %d\n", mps, tc/60000, inc/1000);
13530 sprintf(buf, "level %d %ld:%02d %d\n", mps, tc/60000,
13531 seconds, inc/1000);
13534 SendToProgram(buf, cps);
13536 /* Orthoganally (except for GNU Chess 4), limit time to st seconds */
13537 /* Orthogonally, limit search to given depth */
13539 if (cps->sdKludge) {
13540 sprintf(buf, "depth\n%d\n", sd);
13542 sprintf(buf, "sd %d\n", sd);
13544 SendToProgram(buf, cps);
13547 if(cps->nps > 0) { /* [HGM] nps */
13548 if(cps->supportsNPS == FALSE) cps->nps = -1; // don't use if engine explicitly says not supported!
13550 sprintf(buf, "nps %d\n", cps->nps);
13551 SendToProgram(buf, cps);
13556 ChessProgramState *WhitePlayer()
13557 /* [HGM] return pointer to 'first' or 'second', depending on who plays white */
13559 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b' ||
13560 gameMode == BeginningOfGame || gameMode == MachinePlaysBlack)
13566 SendTimeRemaining(cps, machineWhite)
13567 ChessProgramState *cps;
13568 int /*boolean*/ machineWhite;
13570 char message[MSG_SIZ];
13573 /* Note: this routine must be called when the clocks are stopped
13574 or when they have *just* been set or switched; otherwise
13575 it will be off by the time since the current tick started.
13577 if (machineWhite) {
13578 time = whiteTimeRemaining / 10;
13579 otime = blackTimeRemaining / 10;
13581 time = blackTimeRemaining / 10;
13582 otime = whiteTimeRemaining / 10;
13584 /* [HGM] translate opponent's time by time-odds factor */
13585 otime = (otime * cps->other->timeOdds) / cps->timeOdds;
13586 if (appData.debugMode) {
13587 fprintf(debugFP, "time odds: %f %f \n", cps->timeOdds, cps->other->timeOdds);
13590 if (time <= 0) time = 1;
13591 if (otime <= 0) otime = 1;
13593 sprintf(message, "time %ld\n", time);
13594 SendToProgram(message, cps);
13596 sprintf(message, "otim %ld\n", otime);
13597 SendToProgram(message, cps);
13601 BoolFeature(p, name, loc, cps)
13605 ChessProgramState *cps;
13608 int len = strlen(name);
13610 if (strncmp((*p), name, len) == 0 && (*p)[len] == '=') {
13612 sscanf(*p, "%d", &val);
13614 while (**p && **p != ' ') (*p)++;
13615 sprintf(buf, "accepted %s\n", name);
13616 SendToProgram(buf, cps);
13623 IntFeature(p, name, loc, cps)
13627 ChessProgramState *cps;
13630 int len = strlen(name);
13631 if (strncmp((*p), name, len) == 0 && (*p)[len] == '=') {
13633 sscanf(*p, "%d", loc);
13634 while (**p && **p != ' ') (*p)++;
13635 sprintf(buf, "accepted %s\n", name);
13636 SendToProgram(buf, cps);
13643 StringFeature(p, name, loc, cps)
13647 ChessProgramState *cps;
13650 int len = strlen(name);
13651 if (strncmp((*p), name, len) == 0
13652 && (*p)[len] == '=' && (*p)[len+1] == '\"') {
13654 sscanf(*p, "%[^\"]", loc);
13655 while (**p && **p != '\"') (*p)++;
13656 if (**p == '\"') (*p)++;
13657 sprintf(buf, "accepted %s\n", name);
13658 SendToProgram(buf, cps);
13665 ParseOption(Option *opt, ChessProgramState *cps)
13666 // [HGM] options: process the string that defines an engine option, and determine
13667 // name, type, default value, and allowed value range
13669 char *p, *q, buf[MSG_SIZ];
13670 int n, min = (-1)<<31, max = 1<<31, def;
13672 if(p = strstr(opt->name, " -spin ")) {
13673 if((n = sscanf(p, " -spin %d %d %d", &def, &min, &max)) < 3 ) return FALSE;
13674 if(max < min) max = min; // enforce consistency
13675 if(def < min) def = min;
13676 if(def > max) def = max;
13681 } else if((p = strstr(opt->name, " -slider "))) {
13682 // for now -slider is a synonym for -spin, to already provide compatibility with future polyglots
13683 if((n = sscanf(p, " -slider %d %d %d", &def, &min, &max)) < 3 ) return FALSE;
13684 if(max < min) max = min; // enforce consistency
13685 if(def < min) def = min;
13686 if(def > max) def = max;
13690 opt->type = Spin; // Slider;
13691 } else if((p = strstr(opt->name, " -string "))) {
13692 opt->textValue = p+9;
13693 opt->type = TextBox;
13694 } else if((p = strstr(opt->name, " -file "))) {
13695 // for now -file is a synonym for -string, to already provide compatibility with future polyglots
13696 opt->textValue = p+7;
13697 opt->type = TextBox; // FileName;
13698 } else if((p = strstr(opt->name, " -path "))) {
13699 // for now -file is a synonym for -string, to already provide compatibility with future polyglots
13700 opt->textValue = p+7;
13701 opt->type = TextBox; // PathName;
13702 } else if(p = strstr(opt->name, " -check ")) {
13703 if(sscanf(p, " -check %d", &def) < 1) return FALSE;
13704 opt->value = (def != 0);
13705 opt->type = CheckBox;
13706 } else if(p = strstr(opt->name, " -combo ")) {
13707 opt->textValue = (char*) (&cps->comboList[cps->comboCnt]); // cheat with pointer type
13708 cps->comboList[cps->comboCnt++] = q = p+8; // holds possible choices
13709 if(*q == '*') cps->comboList[cps->comboCnt-1]++;
13710 opt->value = n = 0;
13711 while(q = StrStr(q, " /// ")) {
13712 n++; *q = 0; // count choices, and null-terminate each of them
13714 if(*q == '*') { // remember default, which is marked with * prefix
13718 cps->comboList[cps->comboCnt++] = q;
13720 cps->comboList[cps->comboCnt++] = NULL;
13722 opt->type = ComboBox;
13723 } else if(p = strstr(opt->name, " -button")) {
13724 opt->type = Button;
13725 } else if(p = strstr(opt->name, " -save")) {
13726 opt->type = SaveButton;
13727 } else return FALSE;
13728 *p = 0; // terminate option name
13729 // now look if the command-line options define a setting for this engine option.
13730 if(cps->optionSettings && cps->optionSettings[0])
13731 p = strstr(cps->optionSettings, opt->name); else p = NULL;
13732 if(p && (p == cps->optionSettings || p[-1] == ',')) {
13733 sprintf(buf, "option %s", p);
13734 if(p = strstr(buf, ",")) *p = 0;
13736 SendToProgram(buf, cps);
13742 FeatureDone(cps, val)
13743 ChessProgramState* cps;
13746 DelayedEventCallback cb = GetDelayedEvent();
13747 if ((cb == InitBackEnd3 && cps == &first) ||
13748 (cb == TwoMachinesEventIfReady && cps == &second)) {
13749 CancelDelayedEvent();
13750 ScheduleDelayedEvent(cb, val ? 1 : 3600000);
13752 cps->initDone = val;
13755 /* Parse feature command from engine */
13757 ParseFeatures(args, cps)
13759 ChessProgramState *cps;
13767 while (*p == ' ') p++;
13768 if (*p == NULLCHAR) return;
13770 if (BoolFeature(&p, "setboard", &cps->useSetboard, cps)) continue;
13771 if (BoolFeature(&p, "time", &cps->sendTime, cps)) continue;
13772 if (BoolFeature(&p, "draw", &cps->sendDrawOffers, cps)) continue;
13773 if (BoolFeature(&p, "sigint", &cps->useSigint, cps)) continue;
13774 if (BoolFeature(&p, "sigterm", &cps->useSigterm, cps)) continue;
13775 if (BoolFeature(&p, "reuse", &val, cps)) {
13776 /* Engine can disable reuse, but can't enable it if user said no */
13777 if (!val) cps->reuse = FALSE;
13780 if (BoolFeature(&p, "analyze", &cps->analysisSupport, cps)) continue;
13781 if (StringFeature(&p, "myname", &cps->tidy, cps)) {
13782 if (gameMode == TwoMachinesPlay) {
13783 DisplayTwoMachinesTitle();
13789 if (StringFeature(&p, "variants", &cps->variants, cps)) continue;
13790 if (BoolFeature(&p, "san", &cps->useSAN, cps)) continue;
13791 if (BoolFeature(&p, "ping", &cps->usePing, cps)) continue;
13792 if (BoolFeature(&p, "playother", &cps->usePlayother, cps)) continue;
13793 if (BoolFeature(&p, "colors", &cps->useColors, cps)) continue;
13794 if (BoolFeature(&p, "usermove", &cps->useUsermove, cps)) continue;
13795 if (BoolFeature(&p, "ics", &cps->sendICS, cps)) continue;
13796 if (BoolFeature(&p, "name", &cps->sendName, cps)) continue;
13797 if (BoolFeature(&p, "pause", &val, cps)) continue; /* unused at present */
13798 if (IntFeature(&p, "done", &val, cps)) {
13799 FeatureDone(cps, val);
13802 /* Added by Tord: */
13803 if (BoolFeature(&p, "fen960", &cps->useFEN960, cps)) continue;
13804 if (BoolFeature(&p, "oocastle", &cps->useOOCastle, cps)) continue;
13805 /* End of additions by Tord */
13807 /* [HGM] added features: */
13808 if (BoolFeature(&p, "debug", &cps->debug, cps)) continue;
13809 if (BoolFeature(&p, "nps", &cps->supportsNPS, cps)) continue;
13810 if (IntFeature(&p, "level", &cps->maxNrOfSessions, cps)) continue;
13811 if (BoolFeature(&p, "memory", &cps->memSize, cps)) continue;
13812 if (BoolFeature(&p, "smp", &cps->maxCores, cps)) continue;
13813 if (StringFeature(&p, "egt", &cps->egtFormats, cps)) continue;
13814 if (StringFeature(&p, "option", &(cps->option[cps->nrOptions].name), cps)) {
13815 if(!ParseOption(&(cps->option[cps->nrOptions++]), cps)) { // [HGM] options: add option feature
13816 sprintf(buf, "rejected option %s\n", cps->option[--cps->nrOptions].name);
13817 SendToProgram(buf, cps);
13820 if(cps->nrOptions >= MAX_OPTIONS) {
13822 sprintf(buf, "%s engine has too many options\n", cps->which);
13823 DisplayError(buf, 0);
13827 /* End of additions by HGM */
13829 /* unknown feature: complain and skip */
13831 while (*q && *q != '=') q++;
13832 sprintf(buf, "rejected %.*s\n", (int)(q-p), p);
13833 SendToProgram(buf, cps);
13839 while (*p && *p != '\"') p++;
13840 if (*p == '\"') p++;
13842 while (*p && *p != ' ') p++;
13850 PeriodicUpdatesEvent(newState)
13853 if (newState == appData.periodicUpdates)
13856 appData.periodicUpdates=newState;
13858 /* Display type changes, so update it now */
13859 // DisplayAnalysis();
13861 /* Get the ball rolling again... */
13863 AnalysisPeriodicEvent(1);
13864 StartAnalysisClock();
13869 PonderNextMoveEvent(newState)
13872 if (newState == appData.ponderNextMove) return;
13873 if (gameMode == EditPosition) EditPositionDone(TRUE);
13875 SendToProgram("hard\n", &first);
13876 if (gameMode == TwoMachinesPlay) {
13877 SendToProgram("hard\n", &second);
13880 SendToProgram("easy\n", &first);
13881 thinkOutput[0] = NULLCHAR;
13882 if (gameMode == TwoMachinesPlay) {
13883 SendToProgram("easy\n", &second);
13886 appData.ponderNextMove = newState;
13890 NewSettingEvent(option, feature, command, value)
13892 int option, value, *feature;
13896 if (gameMode == EditPosition) EditPositionDone(TRUE);
13897 sprintf(buf, "%s%s %d\n", (option ? "option ": ""), command, value);
13898 if(feature == NULL || *feature) SendToProgram(buf, &first);
13899 if (gameMode == TwoMachinesPlay) {
13900 if(feature == NULL || feature[(int*)&second - (int*)&first]) SendToProgram(buf, &second);
13905 ShowThinkingEvent()
13906 // [HGM] thinking: this routine is now also called from "Options -> Engine..." popup
13908 static int oldState = 2; // kludge alert! Neither true nor fals, so first time oldState is always updated
13909 int newState = appData.showThinking
13910 // [HGM] thinking: other features now need thinking output as well
13911 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp();
13913 if (oldState == newState) return;
13914 oldState = newState;
13915 if (gameMode == EditPosition) EditPositionDone(TRUE);
13917 SendToProgram("post\n", &first);
13918 if (gameMode == TwoMachinesPlay) {
13919 SendToProgram("post\n", &second);
13922 SendToProgram("nopost\n", &first);
13923 thinkOutput[0] = NULLCHAR;
13924 if (gameMode == TwoMachinesPlay) {
13925 SendToProgram("nopost\n", &second);
13928 // appData.showThinking = newState; // [HGM] thinking: responsible option should already have be changed when calling this routine!
13932 AskQuestionEvent(title, question, replyPrefix, which)
13933 char *title; char *question; char *replyPrefix; char *which;
13935 ProcRef pr = (which[0] == '1') ? first.pr : second.pr;
13936 if (pr == NoProc) return;
13937 AskQuestion(title, question, replyPrefix, pr);
13941 DisplayMove(moveNumber)
13944 char message[MSG_SIZ];
13946 char cpThinkOutput[MSG_SIZ];
13948 if(appData.noGUI) return; // [HGM] fast: suppress display of moves
13950 if (moveNumber == forwardMostMove - 1 ||
13951 gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
13953 safeStrCpy(cpThinkOutput, thinkOutput, sizeof(cpThinkOutput));
13955 if (strchr(cpThinkOutput, '\n')) {
13956 *strchr(cpThinkOutput, '\n') = NULLCHAR;
13959 *cpThinkOutput = NULLCHAR;
13962 /* [AS] Hide thinking from human user */
13963 if( appData.hideThinkingFromHuman && gameMode != TwoMachinesPlay ) {
13964 *cpThinkOutput = NULLCHAR;
13965 if( thinkOutput[0] != NULLCHAR ) {
13968 for( i=0; i<=hiddenThinkOutputState; i++ ) {
13969 cpThinkOutput[i] = '.';
13971 cpThinkOutput[i] = NULLCHAR;
13972 hiddenThinkOutputState = (hiddenThinkOutputState + 1) % 3;
13976 if (moveNumber == forwardMostMove - 1 &&
13977 gameInfo.resultDetails != NULL) {
13978 if (gameInfo.resultDetails[0] == NULLCHAR) {
13979 sprintf(res, " %s", PGNResult(gameInfo.result));
13981 sprintf(res, " {%s} %s",
13982 T_(gameInfo.resultDetails), PGNResult(gameInfo.result));
13988 if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
13989 DisplayMessage(res, cpThinkOutput);
13991 sprintf(message, "%d.%s%s%s", moveNumber / 2 + 1,
13992 WhiteOnMove(moveNumber) ? " " : ".. ",
13993 parseList[moveNumber], res);
13994 DisplayMessage(message, cpThinkOutput);
13999 DisplayComment(moveNumber, text)
14003 char title[MSG_SIZ];
14004 char buf[8000]; // comment can be long!
14007 if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
14008 strcpy(title, "Comment");
14010 sprintf(title, "Comment on %d.%s%s", moveNumber / 2 + 1,
14011 WhiteOnMove(moveNumber) ? " " : ".. ",
14012 parseList[moveNumber]);
14014 // [HGM] PV info: display PV info together with (or as) comment
14015 if(moveNumber >= 0 && (depth = pvInfoList[moveNumber].depth) > 0) {
14016 if(text == NULL) text = "";
14017 score = pvInfoList[moveNumber].score;
14018 sprintf(buf, "%s%.2f/%d %d\n%s", score>0 ? "+" : "", score/100.,
14019 depth, (pvInfoList[moveNumber].time+50)/100, text);
14022 if (text != NULL && (appData.autoDisplayComment || commentUp))
14023 CommentPopUp(title, text);
14026 /* This routine sends a ^C interrupt to gnuchess, to awaken it if it
14027 * might be busy thinking or pondering. It can be omitted if your
14028 * gnuchess is configured to stop thinking immediately on any user
14029 * input. However, that gnuchess feature depends on the FIONREAD
14030 * ioctl, which does not work properly on some flavors of Unix.
14034 ChessProgramState *cps;
14037 if (!cps->useSigint) return;
14038 if (appData.noChessProgram || (cps->pr == NoProc)) return;
14039 switch (gameMode) {
14040 case MachinePlaysWhite:
14041 case MachinePlaysBlack:
14042 case TwoMachinesPlay:
14043 case IcsPlayingWhite:
14044 case IcsPlayingBlack:
14047 /* Skip if we know it isn't thinking */
14048 if (!cps->maybeThinking) return;
14049 if (appData.debugMode)
14050 fprintf(debugFP, "Interrupting %s\n", cps->which);
14051 InterruptChildProcess(cps->pr);
14052 cps->maybeThinking = FALSE;
14057 #endif /*ATTENTION*/
14063 if (whiteTimeRemaining <= 0) {
14066 if (appData.icsActive) {
14067 if (appData.autoCallFlag &&
14068 gameMode == IcsPlayingBlack && !blackFlag) {
14069 SendToICS(ics_prefix);
14070 SendToICS("flag\n");
14074 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Both flags fell"));
14076 if(gameMode != TwoMachinesPlay) DisplayTitle(_("White's flag fell"));
14077 if (appData.autoCallFlag) {
14078 GameEnds(BlackWins, "Black wins on time", GE_XBOARD);
14085 if (blackTimeRemaining <= 0) {
14088 if (appData.icsActive) {
14089 if (appData.autoCallFlag &&
14090 gameMode == IcsPlayingWhite && !whiteFlag) {
14091 SendToICS(ics_prefix);
14092 SendToICS("flag\n");
14096 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Both flags fell"));
14098 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Black's flag fell"));
14099 if (appData.autoCallFlag) {
14100 GameEnds(WhiteWins, "White wins on time", GE_XBOARD);
14113 if (!appData.clockMode || appData.icsActive || searchTime || // [HGM] st: no inc in st mode
14114 gameMode == PlayFromGameFile || forwardMostMove == 0) return;
14117 * add time to clocks when time control is achieved ([HGM] now also used for increment)
14119 if ( !WhiteOnMove(forwardMostMove) )
14120 /* White made time control */
14121 whiteTimeRemaining += GetTimeQuota((forwardMostMove-1)/2)
14122 /* [HGM] time odds: correct new time quota for time odds! */
14123 / WhitePlayer()->timeOdds;
14125 /* Black made time control */
14126 blackTimeRemaining += GetTimeQuota((forwardMostMove-1)/2)
14127 / WhitePlayer()->other->timeOdds;
14131 DisplayBothClocks()
14133 int wom = gameMode == EditPosition ?
14134 !blackPlaysFirst : WhiteOnMove(currentMove);
14135 DisplayWhiteClock(whiteTimeRemaining, wom);
14136 DisplayBlackClock(blackTimeRemaining, !wom);
14140 /* Timekeeping seems to be a portability nightmare. I think everyone
14141 has ftime(), but I'm really not sure, so I'm including some ifdefs
14142 to use other calls if you don't. Clocks will be less accurate if
14143 you have neither ftime nor gettimeofday.
14146 /* VS 2008 requires the #include outside of the function */
14147 #if !HAVE_GETTIMEOFDAY && HAVE_FTIME
14148 #include <sys/timeb.h>
14151 /* Get the current time as a TimeMark */
14156 #if HAVE_GETTIMEOFDAY
14158 struct timeval timeVal;
14159 struct timezone timeZone;
14161 gettimeofday(&timeVal, &timeZone);
14162 tm->sec = (long) timeVal.tv_sec;
14163 tm->ms = (int) (timeVal.tv_usec / 1000L);
14165 #else /*!HAVE_GETTIMEOFDAY*/
14168 // include <sys/timeb.h> / moved to just above start of function
14169 struct timeb timeB;
14172 tm->sec = (long) timeB.time;
14173 tm->ms = (int) timeB.millitm;
14175 #else /*!HAVE_FTIME && !HAVE_GETTIMEOFDAY*/
14176 tm->sec = (long) time(NULL);
14182 /* Return the difference in milliseconds between two
14183 time marks. We assume the difference will fit in a long!
14186 SubtractTimeMarks(tm2, tm1)
14187 TimeMark *tm2, *tm1;
14189 return 1000L*(tm2->sec - tm1->sec) +
14190 (long) (tm2->ms - tm1->ms);
14195 * Code to manage the game clocks.
14197 * In tournament play, black starts the clock and then white makes a move.
14198 * We give the human user a slight advantage if he is playing white---the
14199 * clocks don't run until he makes his first move, so it takes zero time.
14200 * Also, we don't account for network lag, so we could get out of sync
14201 * with GNU Chess's clock -- but then, referees are always right.
14204 static TimeMark tickStartTM;
14205 static long intendedTickLength;
14208 NextTickLength(timeRemaining)
14209 long timeRemaining;
14211 long nominalTickLength, nextTickLength;
14213 if (timeRemaining > 0L && timeRemaining <= 10000L)
14214 nominalTickLength = 100L;
14216 nominalTickLength = 1000L;
14217 nextTickLength = timeRemaining % nominalTickLength;
14218 if (nextTickLength <= 0) nextTickLength += nominalTickLength;
14220 return nextTickLength;
14223 /* Adjust clock one minute up or down */
14225 AdjustClock(Boolean which, int dir)
14227 if(which) blackTimeRemaining += 60000*dir;
14228 else whiteTimeRemaining += 60000*dir;
14229 DisplayBothClocks();
14232 /* Stop clocks and reset to a fresh time control */
14236 (void) StopClockTimer();
14237 if (appData.icsActive) {
14238 whiteTimeRemaining = blackTimeRemaining = 0;
14239 } else if (searchTime) {
14240 whiteTimeRemaining = 1000*searchTime / WhitePlayer()->timeOdds;
14241 blackTimeRemaining = 1000*searchTime / WhitePlayer()->other->timeOdds;
14242 } else { /* [HGM] correct new time quote for time odds */
14243 whiteTimeRemaining = GetTimeQuota(-1) / WhitePlayer()->timeOdds;
14244 blackTimeRemaining = GetTimeQuota(-1) / WhitePlayer()->other->timeOdds;
14246 if (whiteFlag || blackFlag) {
14248 whiteFlag = blackFlag = FALSE;
14250 DisplayBothClocks();
14253 #define FUDGE 25 /* 25ms = 1/40 sec; should be plenty even for 50 Hz clocks */
14255 /* Decrement running clock by amount of time that has passed */
14259 long timeRemaining;
14260 long lastTickLength, fudge;
14263 if (!appData.clockMode) return;
14264 if (gameMode==AnalyzeMode || gameMode == AnalyzeFile) return;
14268 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
14270 /* Fudge if we woke up a little too soon */
14271 fudge = intendedTickLength - lastTickLength;
14272 if (fudge < 0 || fudge > FUDGE) fudge = 0;
14274 if (WhiteOnMove(forwardMostMove)) {
14275 if(whiteNPS >= 0) lastTickLength = 0;
14276 timeRemaining = whiteTimeRemaining -= lastTickLength;
14277 DisplayWhiteClock(whiteTimeRemaining - fudge,
14278 WhiteOnMove(currentMove < forwardMostMove ? currentMove : forwardMostMove));
14280 if(blackNPS >= 0) lastTickLength = 0;
14281 timeRemaining = blackTimeRemaining -= lastTickLength;
14282 DisplayBlackClock(blackTimeRemaining - fudge,
14283 !WhiteOnMove(currentMove < forwardMostMove ? currentMove : forwardMostMove));
14286 if (CheckFlags()) return;
14289 intendedTickLength = NextTickLength(timeRemaining - fudge) + fudge;
14290 StartClockTimer(intendedTickLength);
14292 /* if the time remaining has fallen below the alarm threshold, sound the
14293 * alarm. if the alarm has sounded and (due to a takeback or time control
14294 * with increment) the time remaining has increased to a level above the
14295 * threshold, reset the alarm so it can sound again.
14298 if (appData.icsActive && appData.icsAlarm) {
14300 /* make sure we are dealing with the user's clock */
14301 if (!( ((gameMode == IcsPlayingWhite) && WhiteOnMove(currentMove)) ||
14302 ((gameMode == IcsPlayingBlack) && !WhiteOnMove(currentMove))
14305 if (alarmSounded && (timeRemaining > appData.icsAlarmTime)) {
14306 alarmSounded = FALSE;
14307 } else if (!alarmSounded && (timeRemaining <= appData.icsAlarmTime)) {
14309 alarmSounded = TRUE;
14315 /* A player has just moved, so stop the previously running
14316 clock and (if in clock mode) start the other one.
14317 We redisplay both clocks in case we're in ICS mode, because
14318 ICS gives us an update to both clocks after every move.
14319 Note that this routine is called *after* forwardMostMove
14320 is updated, so the last fractional tick must be subtracted
14321 from the color that is *not* on move now.
14324 SwitchClocks(int newMoveNr)
14326 long lastTickLength;
14328 int flagged = FALSE;
14332 if (StopClockTimer() && appData.clockMode) {
14333 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
14334 if (!WhiteOnMove(forwardMostMove)) {
14335 if(blackNPS >= 0) lastTickLength = 0;
14336 blackTimeRemaining -= lastTickLength;
14337 /* [HGM] PGNtime: save time for PGN file if engine did not give it */
14338 // if(pvInfoList[forwardMostMove-1].time == -1)
14339 pvInfoList[forwardMostMove-1].time = // use GUI time
14340 (timeRemaining[1][forwardMostMove-1] - blackTimeRemaining)/10;
14342 if(whiteNPS >= 0) lastTickLength = 0;
14343 whiteTimeRemaining -= lastTickLength;
14344 /* [HGM] PGNtime: save time for PGN file if engine did not give it */
14345 // if(pvInfoList[forwardMostMove-1].time == -1)
14346 pvInfoList[forwardMostMove-1].time =
14347 (timeRemaining[0][forwardMostMove-1] - whiteTimeRemaining)/10;
14349 flagged = CheckFlags();
14351 forwardMostMove = newMoveNr; // [HGM] race: change stm when no timer interrupt scheduled
14352 CheckTimeControl();
14354 if (flagged || !appData.clockMode) return;
14356 switch (gameMode) {
14357 case MachinePlaysBlack:
14358 case MachinePlaysWhite:
14359 case BeginningOfGame:
14360 if (pausing) return;
14364 case PlayFromGameFile:
14372 if (searchTime) { // [HGM] st: set clock of player that has to move to max time
14373 if(WhiteOnMove(forwardMostMove))
14374 whiteTimeRemaining = 1000*searchTime / WhitePlayer()->timeOdds;
14375 else blackTimeRemaining = 1000*searchTime / WhitePlayer()->other->timeOdds;
14379 intendedTickLength = NextTickLength(WhiteOnMove(forwardMostMove) ?
14380 whiteTimeRemaining : blackTimeRemaining);
14381 StartClockTimer(intendedTickLength);
14385 /* Stop both clocks */
14389 long lastTickLength;
14392 if (!StopClockTimer()) return;
14393 if (!appData.clockMode) return;
14397 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
14398 if (WhiteOnMove(forwardMostMove)) {
14399 if(whiteNPS >= 0) lastTickLength = 0;
14400 whiteTimeRemaining -= lastTickLength;
14401 DisplayWhiteClock(whiteTimeRemaining, WhiteOnMove(currentMove));
14403 if(blackNPS >= 0) lastTickLength = 0;
14404 blackTimeRemaining -= lastTickLength;
14405 DisplayBlackClock(blackTimeRemaining, !WhiteOnMove(currentMove));
14410 /* Start clock of player on move. Time may have been reset, so
14411 if clock is already running, stop and restart it. */
14415 (void) StopClockTimer(); /* in case it was running already */
14416 DisplayBothClocks();
14417 if (CheckFlags()) return;
14419 if (!appData.clockMode) return;
14420 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) return;
14422 GetTimeMark(&tickStartTM);
14423 intendedTickLength = NextTickLength(WhiteOnMove(forwardMostMove) ?
14424 whiteTimeRemaining : blackTimeRemaining);
14426 /* [HGM] nps: figure out nps factors, by determining which engine plays white and/or black once and for all */
14427 whiteNPS = blackNPS = -1;
14428 if(gameMode == MachinePlaysWhite || gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w'
14429 || appData.zippyPlay && gameMode == IcsPlayingBlack) // first (perhaps only) engine has white
14430 whiteNPS = first.nps;
14431 if(gameMode == MachinePlaysBlack || gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b'
14432 || appData.zippyPlay && gameMode == IcsPlayingWhite) // first (perhaps only) engine has black
14433 blackNPS = first.nps;
14434 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b') // second only used in Two-Machines mode
14435 whiteNPS = second.nps;
14436 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w')
14437 blackNPS = second.nps;
14438 if(appData.debugMode) fprintf(debugFP, "nps: w=%d, b=%d\n", whiteNPS, blackNPS);
14440 StartClockTimer(intendedTickLength);
14447 long second, minute, hour, day;
14449 static char buf[32];
14451 if (ms > 0 && ms <= 9900) {
14452 /* convert milliseconds to tenths, rounding up */
14453 double tenths = floor( ((double)(ms + 99L)) / 100.00 );
14455 sprintf(buf, " %03.1f ", tenths/10.0);
14459 /* convert milliseconds to seconds, rounding up */
14460 /* use floating point to avoid strangeness of integer division
14461 with negative dividends on many machines */
14462 second = (long) floor(((double) (ms + 999L)) / 1000.0);
14469 day = second / (60 * 60 * 24);
14470 second = second % (60 * 60 * 24);
14471 hour = second / (60 * 60);
14472 second = second % (60 * 60);
14473 minute = second / 60;
14474 second = second % 60;
14477 sprintf(buf, " %s%ld:%02ld:%02ld:%02ld ",
14478 sign, day, hour, minute, second);
14480 sprintf(buf, " %s%ld:%02ld:%02ld ", sign, hour, minute, second);
14482 sprintf(buf, " %s%2ld:%02ld ", sign, minute, second);
14489 * This is necessary because some C libraries aren't ANSI C compliant yet.
14492 StrStr(string, match)
14493 char *string, *match;
14497 length = strlen(match);
14499 for (i = strlen(string) - length; i >= 0; i--, string++)
14500 if (!strncmp(match, string, length))
14507 StrCaseStr(string, match)
14508 char *string, *match;
14512 length = strlen(match);
14514 for (i = strlen(string) - length; i >= 0; i--, string++) {
14515 for (j = 0; j < length; j++) {
14516 if (ToLower(match[j]) != ToLower(string[j]))
14519 if (j == length) return string;
14533 c1 = ToLower(*s1++);
14534 c2 = ToLower(*s2++);
14535 if (c1 > c2) return 1;
14536 if (c1 < c2) return -1;
14537 if (c1 == NULLCHAR) return 0;
14546 return isupper(c) ? tolower(c) : c;
14554 return islower(c) ? toupper(c) : c;
14556 #endif /* !_amigados */
14564 if ((ret = (char *) malloc(strlen(s) + 1))) {
14571 StrSavePtr(s, savePtr)
14572 char *s, **savePtr;
14577 if ((*savePtr = (char *) malloc(strlen(s) + 1))) {
14578 strcpy(*savePtr, s);
14590 clock = time((time_t *)NULL);
14591 tm = localtime(&clock);
14592 sprintf(buf, "%04d.%02d.%02d",
14593 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
14594 return StrSave(buf);
14599 PositionToFEN(move, overrideCastling)
14601 char *overrideCastling;
14603 int i, j, fromX, fromY, toX, toY;
14610 whiteToPlay = (gameMode == EditPosition) ?
14611 !blackPlaysFirst : (move % 2 == 0);
14614 /* Piece placement data */
14615 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
14617 for (j = BOARD_LEFT; j < BOARD_RGHT; j++) {
14618 if (boards[move][i][j] == EmptySquare) {
14620 } else { ChessSquare piece = boards[move][i][j];
14621 if (emptycount > 0) {
14622 if(emptycount<10) /* [HGM] can be >= 10 */
14623 *p++ = '0' + emptycount;
14624 else { *p++ = '0' + emptycount/10; *p++ = '0' + emptycount%10; }
14627 if(PieceToChar(piece) == '+') {
14628 /* [HGM] write promoted pieces as '+<unpromoted>' (Shogi) */
14630 piece = (ChessSquare)(DEMOTED piece);
14632 *p++ = PieceToChar(piece);
14634 /* [HGM] flag promoted pieces as '<promoted>~' (Crazyhouse) */
14635 p[-1] = PieceToChar((ChessSquare)(DEMOTED piece));
14640 if (emptycount > 0) {
14641 if(emptycount<10) /* [HGM] can be >= 10 */
14642 *p++ = '0' + emptycount;
14643 else { *p++ = '0' + emptycount/10; *p++ = '0' + emptycount%10; }
14650 /* [HGM] print Crazyhouse or Shogi holdings */
14651 if( gameInfo.holdingsWidth ) {
14652 *(p-1) = '['; /* if we wanted to support BFEN, this could be '/' */
14654 for(i=0; i<gameInfo.holdingsSize; i++) { /* white holdings */
14655 piece = boards[move][i][BOARD_WIDTH-1];
14656 if( piece != EmptySquare )
14657 for(j=0; j<(int) boards[move][i][BOARD_WIDTH-2]; j++)
14658 *p++ = PieceToChar(piece);
14660 for(i=0; i<gameInfo.holdingsSize; i++) { /* black holdings */
14661 piece = boards[move][BOARD_HEIGHT-i-1][0];
14662 if( piece != EmptySquare )
14663 for(j=0; j<(int) boards[move][BOARD_HEIGHT-i-1][1]; j++)
14664 *p++ = PieceToChar(piece);
14667 if( q == p ) *p++ = '-';
14673 *p++ = whiteToPlay ? 'w' : 'b';
14676 if(q = overrideCastling) { // [HGM] FRC: override castling & e.p fields for non-compliant engines
14677 while(*p++ = *q++); if(q != overrideCastling+1) p[-1] = ' '; else --p;
14679 if(nrCastlingRights) {
14681 if(gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom) {
14682 /* [HGM] write directly from rights */
14683 if(boards[move][CASTLING][2] != NoRights &&
14684 boards[move][CASTLING][0] != NoRights )
14685 *p++ = boards[move][CASTLING][0] + AAA + 'A' - 'a';
14686 if(boards[move][CASTLING][2] != NoRights &&
14687 boards[move][CASTLING][1] != NoRights )
14688 *p++ = boards[move][CASTLING][1] + AAA + 'A' - 'a';
14689 if(boards[move][CASTLING][5] != NoRights &&
14690 boards[move][CASTLING][3] != NoRights )
14691 *p++ = boards[move][CASTLING][3] + AAA;
14692 if(boards[move][CASTLING][5] != NoRights &&
14693 boards[move][CASTLING][4] != NoRights )
14694 *p++ = boards[move][CASTLING][4] + AAA;
14697 /* [HGM] write true castling rights */
14698 if( nrCastlingRights == 6 ) {
14699 if(boards[move][CASTLING][0] == BOARD_RGHT-1 &&
14700 boards[move][CASTLING][2] != NoRights ) *p++ = 'K';
14701 if(boards[move][CASTLING][1] == BOARD_LEFT &&
14702 boards[move][CASTLING][2] != NoRights ) *p++ = 'Q';
14703 if(boards[move][CASTLING][3] == BOARD_RGHT-1 &&
14704 boards[move][CASTLING][5] != NoRights ) *p++ = 'k';
14705 if(boards[move][CASTLING][4] == BOARD_LEFT &&
14706 boards[move][CASTLING][5] != NoRights ) *p++ = 'q';
14709 if (q == p) *p++ = '-'; /* No castling rights */
14713 if(gameInfo.variant != VariantShogi && gameInfo.variant != VariantXiangqi &&
14714 gameInfo.variant != VariantShatranj && gameInfo.variant != VariantCourier && gameInfo.variant != VariantMakruk ) {
14715 /* En passant target square */
14716 if (move > backwardMostMove) {
14717 fromX = moveList[move - 1][0] - AAA;
14718 fromY = moveList[move - 1][1] - ONE;
14719 toX = moveList[move - 1][2] - AAA;
14720 toY = moveList[move - 1][3] - ONE;
14721 if (fromY == (whiteToPlay ? BOARD_HEIGHT-2 : 1) &&
14722 toY == (whiteToPlay ? BOARD_HEIGHT-4 : 3) &&
14723 boards[move][toY][toX] == (whiteToPlay ? BlackPawn : WhitePawn) &&
14725 /* 2-square pawn move just happened */
14727 *p++ = whiteToPlay ? '6'+BOARD_HEIGHT-8 : '3';
14731 } else if(move == backwardMostMove) {
14732 // [HGM] perhaps we should always do it like this, and forget the above?
14733 if((signed char)boards[move][EP_STATUS] >= 0) {
14734 *p++ = boards[move][EP_STATUS] + AAA;
14735 *p++ = whiteToPlay ? '6'+BOARD_HEIGHT-8 : '3';
14746 /* [HGM] find reversible plies */
14747 { int i = 0, j=move;
14749 if (appData.debugMode) { int k;
14750 fprintf(debugFP, "write FEN 50-move: %d %d %d\n", initialRulePlies, forwardMostMove, backwardMostMove);
14751 for(k=backwardMostMove; k<=forwardMostMove; k++)
14752 fprintf(debugFP, "e%d. p=%d\n", k, (signed char)boards[k][EP_STATUS]);
14756 while(j > backwardMostMove && (signed char)boards[j][EP_STATUS] <= EP_NONE) j--,i++;
14757 if( j == backwardMostMove ) i += initialRulePlies;
14758 sprintf(p, "%d ", i);
14759 p += i>=100 ? 4 : i >= 10 ? 3 : 2;
14761 /* Fullmove number */
14762 sprintf(p, "%d", (move / 2) + 1);
14764 return StrSave(buf);
14768 ParseFEN(board, blackPlaysFirst, fen)
14770 int *blackPlaysFirst;
14780 /* [HGM] by default clear Crazyhouse holdings, if present */
14781 if(gameInfo.holdingsWidth) {
14782 for(i=0; i<BOARD_HEIGHT; i++) {
14783 board[i][0] = EmptySquare; /* black holdings */
14784 board[i][BOARD_WIDTH-1] = EmptySquare; /* white holdings */
14785 board[i][1] = (ChessSquare) 0; /* black counts */
14786 board[i][BOARD_WIDTH-2] = (ChessSquare) 0; /* white counts */
14790 /* Piece placement data */
14791 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
14794 if (*p == '/' || *p == ' ' || (*p == '[' && i == 0) ) {
14795 if (*p == '/') p++;
14796 emptycount = gameInfo.boardWidth - j;
14797 while (emptycount--)
14798 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
14800 #if(BOARD_FILES >= 10)
14801 } else if(*p=='x' || *p=='X') { /* [HGM] X means 10 */
14802 p++; emptycount=10;
14803 if (j + emptycount > gameInfo.boardWidth) return FALSE;
14804 while (emptycount--)
14805 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
14807 } else if (isdigit(*p)) {
14808 emptycount = *p++ - '0';
14809 while(isdigit(*p)) emptycount = 10*emptycount + *p++ - '0'; /* [HGM] allow > 9 */
14810 if (j + emptycount > gameInfo.boardWidth) return FALSE;
14811 while (emptycount--)
14812 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
14813 } else if (*p == '+' || isalpha(*p)) {
14814 if (j >= gameInfo.boardWidth) return FALSE;
14816 piece = CharToPiece(*++p);
14817 if(piece == EmptySquare) return FALSE; /* unknown piece */
14818 piece = (ChessSquare) (PROMOTED piece ); p++;
14819 if(PieceToChar(piece) != '+') return FALSE; /* unpromotable piece */
14820 } else piece = CharToPiece(*p++);
14822 if(piece==EmptySquare) return FALSE; /* unknown piece */
14823 if(*p == '~') { /* [HGM] make it a promoted piece for Crazyhouse */
14824 piece = (ChessSquare) (PROMOTED piece);
14825 if(PieceToChar(piece) != '~') return FALSE; /* cannot be a promoted piece */
14828 board[i][(j++)+gameInfo.holdingsWidth] = piece;
14834 while (*p == '/' || *p == ' ') p++;
14836 /* [HGM] look for Crazyhouse holdings here */
14837 while(*p==' ') p++;
14838 if( gameInfo.holdingsWidth && p[-1] == '/' || *p == '[') {
14840 if(*p == '-' ) *p++; /* empty holdings */ else {
14841 if( !gameInfo.holdingsWidth ) return FALSE; /* no room to put holdings! */
14842 /* if we would allow FEN reading to set board size, we would */
14843 /* have to add holdings and shift the board read so far here */
14844 while( (piece = CharToPiece(*p) ) != EmptySquare ) {
14846 if((int) piece >= (int) BlackPawn ) {
14847 i = (int)piece - (int)BlackPawn;
14848 i = PieceToNumber((ChessSquare)i);
14849 if( i >= gameInfo.holdingsSize ) return FALSE;
14850 board[BOARD_HEIGHT-1-i][0] = piece; /* black holdings */
14851 board[BOARD_HEIGHT-1-i][1]++; /* black counts */
14853 i = (int)piece - (int)WhitePawn;
14854 i = PieceToNumber((ChessSquare)i);
14855 if( i >= gameInfo.holdingsSize ) return FALSE;
14856 board[i][BOARD_WIDTH-1] = piece; /* white holdings */
14857 board[i][BOARD_WIDTH-2]++; /* black holdings */
14861 if(*p == ']') *p++;
14864 while(*p == ' ') p++;
14868 if(appData.colorNickNames) {
14869 if( c == appData.colorNickNames[0] ) c = 'w'; else
14870 if( c == appData.colorNickNames[1] ) c = 'b';
14874 *blackPlaysFirst = FALSE;
14877 *blackPlaysFirst = TRUE;
14883 /* [HGM] We NO LONGER ignore the rest of the FEN notation */
14884 /* return the extra info in global variiables */
14886 /* set defaults in case FEN is incomplete */
14887 board[EP_STATUS] = EP_UNKNOWN;
14888 for(i=0; i<nrCastlingRights; i++ ) {
14889 board[CASTLING][i] =
14890 gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom ? NoRights : initialRights[i];
14891 } /* assume possible unless obviously impossible */
14892 if(initialRights[0]!=NoRights && board[castlingRank[0]][initialRights[0]] != WhiteRook) board[CASTLING][0] = NoRights;
14893 if(initialRights[1]!=NoRights && board[castlingRank[1]][initialRights[1]] != WhiteRook) board[CASTLING][1] = NoRights;
14894 if(initialRights[2]!=NoRights && board[castlingRank[2]][initialRights[2]] != WhiteUnicorn
14895 && board[castlingRank[2]][initialRights[2]] != WhiteKing) board[CASTLING][2] = NoRights;
14896 if(initialRights[3]!=NoRights && board[castlingRank[3]][initialRights[3]] != BlackRook) board[CASTLING][3] = NoRights;
14897 if(initialRights[4]!=NoRights && board[castlingRank[4]][initialRights[4]] != BlackRook) board[CASTLING][4] = NoRights;
14898 if(initialRights[5]!=NoRights && board[castlingRank[5]][initialRights[5]] != BlackUnicorn
14899 && board[castlingRank[5]][initialRights[5]] != BlackKing) board[CASTLING][5] = NoRights;
14902 while(*p==' ') p++;
14903 if(nrCastlingRights) {
14904 if(*p=='K' || *p=='Q' || *p=='k' || *p=='q' || *p=='-') {
14905 /* castling indicator present, so default becomes no castlings */
14906 for(i=0; i<nrCastlingRights; i++ ) {
14907 board[CASTLING][i] = NoRights;
14910 while(*p=='K' || *p=='Q' || *p=='k' || *p=='q' || *p=='-' ||
14911 (gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom) &&
14912 ( *p >= 'a' && *p < 'a' + gameInfo.boardWidth) ||
14913 ( *p >= 'A' && *p < 'A' + gameInfo.boardWidth) ) {
14914 char c = *p++; int whiteKingFile=NoRights, blackKingFile=NoRights;
14916 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) {
14917 if(board[BOARD_HEIGHT-1][i] == BlackKing) blackKingFile = i;
14918 if(board[0 ][i] == WhiteKing) whiteKingFile = i;
14920 if(gameInfo.variant == VariantTwoKings || gameInfo.variant == VariantKnightmate)
14921 whiteKingFile = blackKingFile = BOARD_WIDTH >> 1; // for these variant scanning fails
14922 if(whiteKingFile == NoRights || board[0][whiteKingFile] != WhiteUnicorn
14923 && board[0][whiteKingFile] != WhiteKing) whiteKingFile = NoRights;
14924 if(blackKingFile == NoRights || board[BOARD_HEIGHT-1][blackKingFile] != BlackUnicorn
14925 && board[BOARD_HEIGHT-1][blackKingFile] != BlackKing) blackKingFile = NoRights;
14928 for(i=BOARD_RGHT-1; board[0][i]!=WhiteRook && i>whiteKingFile; i--);
14929 board[CASTLING][0] = i != whiteKingFile ? i : NoRights;
14930 board[CASTLING][2] = whiteKingFile;
14933 for(i=BOARD_LEFT; i<BOARD_RGHT && board[0][i]!=WhiteRook && i<whiteKingFile; i++);
14934 board[CASTLING][1] = i != whiteKingFile ? i : NoRights;
14935 board[CASTLING][2] = whiteKingFile;
14938 for(i=BOARD_RGHT-1; board[BOARD_HEIGHT-1][i]!=BlackRook && i>blackKingFile; i--);
14939 board[CASTLING][3] = i != blackKingFile ? i : NoRights;
14940 board[CASTLING][5] = blackKingFile;
14943 for(i=BOARD_LEFT; i<BOARD_RGHT && board[BOARD_HEIGHT-1][i]!=BlackRook && i<blackKingFile; i++);
14944 board[CASTLING][4] = i != blackKingFile ? i : NoRights;
14945 board[CASTLING][5] = blackKingFile;
14948 default: /* FRC castlings */
14949 if(c >= 'a') { /* black rights */
14950 for(i=BOARD_LEFT; i<BOARD_RGHT; i++)
14951 if(board[BOARD_HEIGHT-1][i] == BlackKing) break;
14952 if(i == BOARD_RGHT) break;
14953 board[CASTLING][5] = i;
14955 if(board[BOARD_HEIGHT-1][c] < BlackPawn ||
14956 board[BOARD_HEIGHT-1][c] >= BlackKing ) break;
14958 board[CASTLING][3] = c;
14960 board[CASTLING][4] = c;
14961 } else { /* white rights */
14962 for(i=BOARD_LEFT; i<BOARD_RGHT; i++)
14963 if(board[0][i] == WhiteKing) break;
14964 if(i == BOARD_RGHT) break;
14965 board[CASTLING][2] = i;
14966 c -= AAA - 'a' + 'A';
14967 if(board[0][c] >= WhiteKing) break;
14969 board[CASTLING][0] = c;
14971 board[CASTLING][1] = c;
14975 for(i=0; i<nrCastlingRights; i++)
14976 if(board[CASTLING][i] != NoRights) initialRights[i] = board[CASTLING][i];
14977 if (appData.debugMode) {
14978 fprintf(debugFP, "FEN castling rights:");
14979 for(i=0; i<nrCastlingRights; i++)
14980 fprintf(debugFP, " %d", board[CASTLING][i]);
14981 fprintf(debugFP, "\n");
14984 while(*p==' ') p++;
14987 /* read e.p. field in games that know e.p. capture */
14988 if(gameInfo.variant != VariantShogi && gameInfo.variant != VariantXiangqi &&
14989 gameInfo.variant != VariantShatranj && gameInfo.variant != VariantCourier && gameInfo.variant != VariantMakruk ) {
14991 p++; board[EP_STATUS] = EP_NONE;
14993 char c = *p++ - AAA;
14995 if(c < BOARD_LEFT || c >= BOARD_RGHT) return TRUE;
14996 if(*p >= '0' && *p <='9') *p++;
14997 board[EP_STATUS] = c;
15002 if(sscanf(p, "%d", &i) == 1) {
15003 FENrulePlies = i; /* 50-move ply counter */
15004 /* (The move number is still ignored) */
15011 EditPositionPasteFEN(char *fen)
15014 Board initial_position;
15016 if (!ParseFEN(initial_position, &blackPlaysFirst, fen)) {
15017 DisplayError(_("Bad FEN position in clipboard"), 0);
15020 int savedBlackPlaysFirst = blackPlaysFirst;
15021 EditPositionEvent();
15022 blackPlaysFirst = savedBlackPlaysFirst;
15023 CopyBoard(boards[0], initial_position);
15024 initialRulePlies = FENrulePlies; /* [HGM] copy FEN attributes as well */
15025 EditPositionDone(FALSE); // [HGM] fake: do not fake rights if we had FEN
15026 DisplayBothClocks();
15027 DrawPosition(FALSE, boards[currentMove]);
15032 static char cseq[12] = "\\ ";
15034 Boolean set_cont_sequence(char *new_seq)
15039 // handle bad attempts to set the sequence
15041 return 0; // acceptable error - no debug
15043 len = strlen(new_seq);
15044 ret = (len > 0) && (len < sizeof(cseq));
15046 strcpy(cseq, new_seq);
15047 else if (appData.debugMode)
15048 fprintf(debugFP, "Invalid continuation sequence \"%s\" (maximum length is: %u)\n", new_seq, (unsigned) sizeof(cseq)-1);
15053 reformat a source message so words don't cross the width boundary. internal
15054 newlines are not removed. returns the wrapped size (no null character unless
15055 included in source message). If dest is NULL, only calculate the size required
15056 for the dest buffer. lp argument indicats line position upon entry, and it's
15057 passed back upon exit.
15059 int wrap(char *dest, char *src, int count, int width, int *lp)
15061 int len, i, ansi, cseq_len, line, old_line, old_i, old_len, clen;
15063 cseq_len = strlen(cseq);
15064 old_line = line = *lp;
15065 ansi = len = clen = 0;
15067 for (i=0; i < count; i++)
15069 if (src[i] == '\033')
15072 // if we hit the width, back up
15073 if (!ansi && (line >= width) && src[i] != '\n' && src[i] != ' ')
15075 // store i & len in case the word is too long
15076 old_i = i, old_len = len;
15078 // find the end of the last word
15079 while (i && src[i] != ' ' && src[i] != '\n')
15085 // word too long? restore i & len before splitting it
15086 if ((old_i-i+clen) >= width)
15093 if (i && src[i-1] == ' ')
15096 if (src[i] != ' ' && src[i] != '\n')
15103 // now append the newline and continuation sequence
15108 strncpy(dest+len, cseq, cseq_len);
15116 dest[len] = src[i];
15120 if (src[i] == '\n')
15125 if (dest && appData.debugMode)
15127 fprintf(debugFP, "wrap(count:%d,width:%d,line:%d,len:%d,*lp:%d,src: ",
15128 count, width, line, len, *lp);
15129 show_bytes(debugFP, src, count);
15130 fprintf(debugFP, "\ndest: ");
15131 show_bytes(debugFP, dest, len);
15132 fprintf(debugFP, "\n");
15134 *lp = dest ? line : old_line;
15139 // [HGM] vari: routines for shelving variations
15142 PushTail(int firstMove, int lastMove)
15144 int i, j, nrMoves = lastMove - firstMove;
15146 if(appData.icsActive) { // only in local mode
15147 forwardMostMove = currentMove; // mimic old ICS behavior
15150 if(storedGames >= MAX_VARIATIONS-1) return;
15152 // push current tail of game on stack
15153 savedResult[storedGames] = gameInfo.result;
15154 savedDetails[storedGames] = gameInfo.resultDetails;
15155 gameInfo.resultDetails = NULL;
15156 savedFirst[storedGames] = firstMove;
15157 savedLast [storedGames] = lastMove;
15158 savedFramePtr[storedGames] = framePtr;
15159 framePtr -= nrMoves; // reserve space for the boards
15160 for(i=nrMoves; i>=1; i--) { // copy boards to stack, working downwards, in case of overlap
15161 CopyBoard(boards[framePtr+i], boards[firstMove+i]);
15162 for(j=0; j<MOVE_LEN; j++)
15163 moveList[framePtr+i][j] = moveList[firstMove+i-1][j];
15164 for(j=0; j<2*MOVE_LEN; j++)
15165 parseList[framePtr+i][j] = parseList[firstMove+i-1][j];
15166 timeRemaining[0][framePtr+i] = timeRemaining[0][firstMove+i];
15167 timeRemaining[1][framePtr+i] = timeRemaining[1][firstMove+i];
15168 pvInfoList[framePtr+i] = pvInfoList[firstMove+i-1];
15169 pvInfoList[firstMove+i-1].depth = 0;
15170 commentList[framePtr+i] = commentList[firstMove+i];
15171 commentList[firstMove+i] = NULL;
15175 forwardMostMove = firstMove; // truncate game so we can start variation
15176 if(storedGames == 1) GreyRevert(FALSE);
15180 PopTail(Boolean annotate)
15183 char buf[8000], moveBuf[20];
15185 if(appData.icsActive) return FALSE; // only in local mode
15186 if(!storedGames) return FALSE; // sanity
15187 CommentPopDown(); // make sure no stale variation comments to the destroyed line can remain open
15190 ToNrEvent(savedFirst[storedGames]); // sets currentMove
15191 nrMoves = savedLast[storedGames] - currentMove;
15194 if(!WhiteOnMove(currentMove)) sprintf(buf, "(%d...", currentMove+2>>1);
15195 else strcpy(buf, "(");
15196 for(i=currentMove; i<forwardMostMove; i++) {
15198 sprintf(moveBuf, " %d. %s", i+2>>1, SavePart(parseList[i]));
15199 else sprintf(moveBuf, " %s", SavePart(parseList[i]));
15200 strcat(buf, moveBuf);
15201 if(commentList[i]) { strcat(buf, " "); strcat(buf, commentList[i]); }
15202 if(!--cnt) { strcat(buf, "\n"); cnt = 10; }
15206 for(i=1; i<=nrMoves; i++) { // copy last variation back
15207 CopyBoard(boards[currentMove+i], boards[framePtr+i]);
15208 for(j=0; j<MOVE_LEN; j++)
15209 moveList[currentMove+i-1][j] = moveList[framePtr+i][j];
15210 for(j=0; j<2*MOVE_LEN; j++)
15211 parseList[currentMove+i-1][j] = parseList[framePtr+i][j];
15212 timeRemaining[0][currentMove+i] = timeRemaining[0][framePtr+i];
15213 timeRemaining[1][currentMove+i] = timeRemaining[1][framePtr+i];
15214 pvInfoList[currentMove+i-1] = pvInfoList[framePtr+i];
15215 if(commentList[currentMove+i]) free(commentList[currentMove+i]);
15216 commentList[currentMove+i] = commentList[framePtr+i];
15217 commentList[framePtr+i] = NULL;
15219 if(annotate) AppendComment(currentMove+1, buf, FALSE);
15220 framePtr = savedFramePtr[storedGames];
15221 gameInfo.result = savedResult[storedGames];
15222 if(gameInfo.resultDetails != NULL) {
15223 free(gameInfo.resultDetails);
15225 gameInfo.resultDetails = savedDetails[storedGames];
15226 forwardMostMove = currentMove + nrMoves;
15227 if(storedGames == 0) GreyRevert(TRUE);
15233 { // remove all shelved variations
15235 for(i=0; i<storedGames; i++) {
15236 if(savedDetails[i])
15237 free(savedDetails[i]);
15238 savedDetails[i] = NULL;
15240 for(i=framePtr; i<MAX_MOVES; i++) {
15241 if(commentList[i]) free(commentList[i]);
15242 commentList[i] = NULL;
15244 framePtr = MAX_MOVES-1;
15249 LoadVariation(int index, char *text)
15250 { // [HGM] vari: shelve previous line and load new variation, parsed from text around text[index]
15251 char *p = text, *start = NULL, *end = NULL, wait = NULLCHAR;
15252 int level = 0, move;
15254 if(gameMode != EditGame && gameMode != AnalyzeMode) return;
15255 // first find outermost bracketing variation
15256 while(*p) { // hope I got this right... Non-nesting {} and [] can screen each other and nesting ()
15257 if(!wait) { // while inside [] pr {}, ignore everyting except matching closing ]}
15258 if(*p == '{') wait = '}'; else
15259 if(*p == '[') wait = ']'; else
15260 if(*p == '(' && level++ == 0 && p-text < index) start = p+1;
15261 if(*p == ')' && level > 0 && --level == 0 && p-text > index && end == NULL) end = p-1;
15263 if(*p == wait) wait = NULLCHAR; // closing ]} found
15266 if(!start || !end) return; // no variation found, or syntax error in PGN: ignore click
15267 if(appData.debugMode) fprintf(debugFP, "at move %d load variation '%s'\n", currentMove, start);
15268 end[1] = NULLCHAR; // clip off comment beyond variation
15269 ToNrEvent(currentMove-1);
15270 PushTail(currentMove, forwardMostMove); // shelve main variation. This truncates game
15271 // kludge: use ParsePV() to append variation to game
15272 move = currentMove;
15273 ParsePV(start, TRUE);
15274 forwardMostMove = endPV; endPV = -1; currentMove = move; // cleanup what ParsePV did
15275 ClearPremoveHighlights();
15277 ToNrEvent(currentMove+1);