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, int toX, int toY, char promoChar));
162 void HandleMachineMove P((char *message, ChessProgramState *cps));
163 int AutoPlayOneMove P((void));
164 int LoadGameOneMove P((ChessMove readAhead));
165 int LoadGameFromFile P((char *filename, int n, char *title, int useList));
166 int LoadPositionFromFile P((char *filename, int n, char *title));
167 int SavePositionToFile P((char *filename));
168 void ApplyMove P((int fromX, int fromY, int toX, int toY, int promoChar,
170 void MakeMove P((int fromX, int fromY, int toX, int toY, int promoChar));
171 void ShowMove P((int fromX, int fromY, int toX, int toY));
172 int FinishMove P((ChessMove moveType, int fromX, int fromY, int toX, int toY,
173 /*char*/int promoChar));
174 void BackwardInner P((int target));
175 void ForwardInner P((int target));
176 int Adjudicate P((ChessProgramState *cps));
177 void GameEnds P((ChessMove result, char *resultDetails, int whosays));
178 void EditPositionDone P((Boolean fakeRights));
179 void PrintOpponents P((FILE *fp));
180 void PrintPosition P((FILE *fp, int move));
181 void StartChessProgram P((ChessProgramState *cps));
182 void SendToProgram P((char *message, ChessProgramState *cps));
183 void SendMoveToProgram P((int moveNum, ChessProgramState *cps));
184 void ReceiveFromProgram P((InputSourceRef isr, VOIDSTAR closure,
185 char *buf, int count, int error));
186 void SendTimeControl P((ChessProgramState *cps,
187 int mps, long tc, int inc, int sd, int st));
188 char *TimeControlTagValue P((void));
189 void Attention P((ChessProgramState *cps));
190 void FeedMovesToProgram P((ChessProgramState *cps, int upto));
191 void ResurrectChessProgram P((void));
192 void DisplayComment P((int moveNumber, char *text));
193 void DisplayMove P((int moveNumber));
195 void ParseGameHistory P((char *game));
196 void ParseBoard12 P((char *string));
197 void KeepAlive P((void));
198 void StartClocks P((void));
199 void SwitchClocks P((int nr));
200 void StopClocks P((void));
201 void ResetClocks P((void));
202 char *PGNDate P((void));
203 void SetGameInfo P((void));
204 Boolean ParseFEN P((Board board, int *blackPlaysFirst, char *fen));
205 int RegisterMove P((void));
206 void MakeRegisteredMove P((void));
207 void TruncateGame P((void));
208 int looking_at P((char *, int *, char *));
209 void CopyPlayerNameIntoFileName P((char **, char *));
210 char *SavePart P((char *));
211 int SaveGameOldStyle P((FILE *));
212 int SaveGamePGN P((FILE *));
213 void GetTimeMark P((TimeMark *));
214 long SubtractTimeMarks P((TimeMark *, TimeMark *));
215 int CheckFlags P((void));
216 long NextTickLength P((long));
217 void CheckTimeControl P((void));
218 void show_bytes P((FILE *, char *, int));
219 int string_to_rating P((char *str));
220 void ParseFeatures P((char* args, ChessProgramState *cps));
221 void InitBackEnd3 P((void));
222 void FeatureDone P((ChessProgramState* cps, int val));
223 void InitChessProgram P((ChessProgramState *cps, int setup));
224 void OutputKibitz(int window, char *text);
225 int PerpetualChase(int first, int last);
226 int EngineOutputIsUp();
227 void InitDrawingSizes(int x, int y);
230 extern void ConsoleCreate();
233 ChessProgramState *WhitePlayer();
234 void InsertIntoMemo P((int which, char *text)); // [HGM] kibitz: in engineo.c
235 int VerifyDisplayMode P(());
237 char *GetInfoFromComment( int, char * ); // [HGM] PV time: returns stripped comment
238 void InitEngineUCI( const char * iniDir, ChessProgramState * cps ); // [HGM] moved here from winboard.c
239 char *ProbeBook P((int moveNr, char *book)); // [HGM] book: returns a book move
240 char *SendMoveToBookUser P((int nr, ChessProgramState *cps, int initial)); // [HGM] book
241 void ics_update_width P((int new_width));
242 extern char installDir[MSG_SIZ];
243 VariantClass startVariant; /* [HGM] nicks: initial variant */
245 extern int tinyLayout, smallLayout;
246 ChessProgramStats programStats;
247 char lastPV[2][2*MSG_SIZ]; /* [HGM] pv: last PV in thinking output of each engine */
249 static int exiting = 0; /* [HGM] moved to top */
250 static int setboardSpoiledMachineBlack = 0 /*, errorExitFlag = 0*/;
251 int startedFromPositionFile = FALSE; Board filePosition; /* [HGM] loadPos */
252 Board partnerBoard; /* [HGM] bughouse: for peeking at partner game */
253 int partnerHighlight[2];
254 Boolean partnerBoardValid = 0;
255 char partnerStatus[MSG_SIZ];
257 Boolean originalFlip;
258 Boolean twoBoards = 0;
259 char endingGame = 0; /* [HGM] crash: flag to prevent recursion of GameEnds() */
260 int whiteNPS, blackNPS; /* [HGM] nps: for easily making clocks aware of NPS */
261 VariantClass currentlyInitializedVariant; /* [HGM] variantswitch */
262 int lastIndex = 0; /* [HGM] autoinc: last game/position used in match mode */
263 Boolean connectionAlive;/* [HGM] alive: ICS connection status from probing */
264 int opponentKibitzes;
265 int lastSavedGame; /* [HGM] save: ID of game */
266 char chatPartner[MAX_CHAT][MSG_SIZ]; /* [HGM] chat: list of chatting partners */
267 extern int chatCount;
269 char marker[BOARD_RANKS][BOARD_FILES]; /* [HGM] marks for target squares */
271 /* States for ics_getting_history */
273 #define H_REQUESTED 1
274 #define H_GOT_REQ_HEADER 2
275 #define H_GOT_UNREQ_HEADER 3
276 #define H_GETTING_MOVES 4
277 #define H_GOT_UNWANTED_HEADER 5
279 /* whosays values for GameEnds */
288 /* Maximum number of games in a cmail message */
289 #define CMAIL_MAX_GAMES 20
291 /* Different types of move when calling RegisterMove */
293 #define CMAIL_RESIGN 1
295 #define CMAIL_ACCEPT 3
297 /* Different types of result to remember for each game */
298 #define CMAIL_NOT_RESULT 0
299 #define CMAIL_OLD_RESULT 1
300 #define CMAIL_NEW_RESULT 2
302 /* Telnet protocol constants */
313 static char * safeStrCpy( char * dst, const char * src, size_t count )
315 assert( dst != NULL );
316 assert( src != NULL );
319 strncpy( dst, src, count );
320 dst[ count-1 ] = '\0';
324 /* Some compiler can't cast u64 to double
325 * This function do the job for us:
327 * We use the highest bit for cast, this only
328 * works if the highest bit is not
329 * in use (This should not happen)
331 * We used this for all compiler
334 u64ToDouble(u64 value)
337 u64 tmp = value & u64Const(0x7fffffffffffffff);
338 r = (double)(s64)tmp;
339 if (value & u64Const(0x8000000000000000))
340 r += 9.2233720368547758080e18; /* 2^63 */
344 /* Fake up flags for now, as we aren't keeping track of castling
345 availability yet. [HGM] Change of logic: the flag now only
346 indicates the type of castlings allowed by the rule of the game.
347 The actual rights themselves are maintained in the array
348 castlingRights, as part of the game history, and are not probed
354 int flags = F_ALL_CASTLE_OK;
355 if ((index % 2) == 0) flags |= F_WHITE_ON_MOVE;
356 switch (gameInfo.variant) {
358 flags &= ~F_ALL_CASTLE_OK;
359 case VariantGiveaway: // [HGM] moved this case label one down: seems Giveaway does have castling on ICC!
360 flags |= F_IGNORE_CHECK;
362 flags |= F_MANDATORY_CAPTURE; //[HGM] losers: sets flag so TestLegality rejects non-capts if capts exist
365 flags |= F_IGNORE_CHECK | F_ATOMIC_CAPTURE;
367 case VariantKriegspiel:
368 flags |= F_KRIEGSPIEL_CAPTURE;
370 case VariantCapaRandom:
371 case VariantFischeRandom:
372 flags |= F_FRC_TYPE_CASTLING; /* [HGM] enable this through flag */
373 case VariantNoCastle:
374 case VariantShatranj:
377 flags &= ~F_ALL_CASTLE_OK;
385 FILE *gameFileFP, *debugFP;
388 [AS] Note: sometimes, the sscanf() function is used to parse the input
389 into a fixed-size buffer. Because of this, we must be prepared to
390 receive strings as long as the size of the input buffer, which is currently
391 set to 4K for Windows and 8K for the rest.
392 So, we must either allocate sufficiently large buffers here, or
393 reduce the size of the input buffer in the input reading part.
396 char cmailMove[CMAIL_MAX_GAMES][MOVE_LEN], cmailMsg[MSG_SIZ];
397 char bookOutput[MSG_SIZ*10], thinkOutput[MSG_SIZ*10], lastHint[MSG_SIZ];
398 char thinkOutput1[MSG_SIZ*10];
400 ChessProgramState first, second;
402 /* premove variables */
405 int premoveFromX = 0;
406 int premoveFromY = 0;
407 int premovePromoChar = 0;
409 Boolean alarmSounded;
410 /* end premove variables */
412 char *ics_prefix = "$";
413 int ics_type = ICS_GENERIC;
415 int currentMove = 0, forwardMostMove = 0, backwardMostMove = 0;
416 int pauseExamForwardMostMove = 0;
417 int nCmailGames = 0, nCmailResults = 0, nCmailMovesRegistered = 0;
418 int cmailMoveRegistered[CMAIL_MAX_GAMES], cmailResult[CMAIL_MAX_GAMES];
419 int cmailMsgLoaded = FALSE, cmailMailedMove = FALSE;
420 int cmailOldMove = -1, firstMove = TRUE, flipView = FALSE;
421 int blackPlaysFirst = FALSE, startedFromSetupPosition = FALSE;
422 int searchTime = 0, pausing = FALSE, pauseExamInvalid = FALSE;
423 int whiteFlag = FALSE, blackFlag = FALSE;
424 int userOfferedDraw = FALSE;
425 int ics_user_moved = 0, ics_gamenum = -1, ics_getting_history = H_FALSE;
426 int matchMode = FALSE, hintRequested = FALSE, bookRequested = FALSE;
427 int cmailMoveType[CMAIL_MAX_GAMES];
428 long ics_clock_paused = 0;
429 ProcRef icsPR = NoProc, cmailPR = NoProc;
430 InputSourceRef telnetISR = NULL, fromUserISR = NULL, cmailISR = NULL;
431 GameMode gameMode = BeginningOfGame;
432 char moveList[MAX_MOVES][MOVE_LEN], parseList[MAX_MOVES][MOVE_LEN * 2];
433 char *commentList[MAX_MOVES], *cmailCommentList[CMAIL_MAX_GAMES];
434 ChessProgramStats_Move pvInfoList[MAX_MOVES]; /* [AS] Info about engine thinking */
435 int hiddenThinkOutputState = 0; /* [AS] */
436 int adjudicateLossThreshold = 0; /* [AS] Automatic adjudication */
437 int adjudicateLossPlies = 6;
438 char white_holding[64], black_holding[64];
439 TimeMark lastNodeCountTime;
440 long lastNodeCount=0;
441 int have_sent_ICS_logon = 0;
443 int suddenDeath, whiteStartMove, blackStartMove; /* [HGM] for implementation of 'any per time' sessions, as in first part of byoyomi TC */
444 long whiteTimeRemaining, blackTimeRemaining, timeControl, timeIncrement, lastWhite, lastBlack;
445 long timeControl_2; /* [AS] Allow separate time controls */
446 char *fullTimeControlString = NULL, *nextSession, *whiteTC, *blackTC; /* [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, int *incType)
975 { /* [HGM] routine added to read '+moves/time' for secondary time control. */
976 int result = -1, type = 0; long temp, temp2;
978 if(**str != ':') return -1; // old params remain in force!
980 if(**str == '*') type = *(*str)++, temp = 0; // sandclock TC
981 if( NextIntegerFromString( str, &temp ) ) return -1;
982 if(type) { *moves = 0; *tc = temp * 500; *inc = temp * 1000; *incType = '*'; return 0; }
985 /* time only: incremental or sudden-death time control */
986 if(**str == '+') { /* increment follows; read it */
988 if(**str == '!') type = *(*str)++; // Bronstein TC
989 if(result = NextIntegerFromString( str, &temp2)) return -1;
992 *moves = 0; *tc = temp * 1000; *incType = type;
996 (*str)++; /* classical time control */
997 result = NextIntegerFromString( str, &temp2); // NOTE: already converted to seconds by ParseTimeControl()
1008 int GetTimeQuota(int movenr, int lastUsed, char *tcString)
1009 { /* [HGM] get time to add from the multi-session time-control string */
1010 int incType, moves=1; /* kludge to force reading of first session */
1011 long time, increment;
1014 if(!*s) return 0; // empty TC string means we ran out of the last sudden-death version
1015 if(appData.debugMode) fprintf(debugFP, "TC string = '%s'\n", tcString);
1017 if(moves) NextSessionFromString(&s, &moves, &time, &increment, &incType);
1018 nextSession = s; suddenDeath = moves == 0 && increment == 0;
1019 if(appData.debugMode) fprintf(debugFP, "mps=%d tc=%d inc=%d\n", moves, (int) time, (int) increment);
1020 if(movenr == -1) return time; /* last move before new session */
1021 if(incType == '*') increment = 0; else // for sandclock, time is added while not thinking
1022 if(incType == '!' && lastUsed < increment) increment = lastUsed;
1023 if(!moves) return increment; /* current session is incremental */
1024 if(movenr >= 0) movenr -= moves; /* we already finished this session */
1025 } while(movenr >= -1); /* try again for next session */
1027 return 0; // no new time quota on this move
1031 ParseTimeControl(tc, ti, mps)
1038 char buf[MSG_SIZ], buf2[MSG_SIZ], *mytc = tc;
1041 if(ti >= 0 && !strchr(tc, '+') && !strchr(tc, '/') ) mps = 0;
1042 if(!strchr(tc, '+') && !strchr(tc, '/') && sscanf(tc, "%d:%d", &min, &sec) >= 1)
1043 sprintf(mytc=buf2, "%d", 60*min+sec); // convert 'classical' min:sec tc string to seconds
1046 sprintf(buf, ":%d/%s+%d", mps, mytc, ti);
1047 else sprintf(buf, ":%s+%d", mytc, ti);
1050 sprintf(buf, ":%d/%s", mps, mytc);
1051 else sprintf(buf, ":%s", mytc);
1053 fullTimeControlString = StrSave(buf); // this should now be in PGN format
1055 if( NextTimeControlFromString( &tc, &tc1 ) != 0 ) {
1060 /* Parse second time control */
1063 if( NextTimeControlFromString( &tc, &tc2 ) != 0 ) {
1071 timeControl_2 = tc2 * 1000;
1081 timeControl = tc1 * 1000;
1084 timeIncrement = ti * 1000; /* convert to ms */
1085 movesPerSession = 0;
1088 movesPerSession = mps;
1096 if (appData.debugMode) {
1097 fprintf(debugFP, "%s\n", programVersion);
1100 set_cont_sequence(appData.wrapContSeq);
1101 if (appData.matchGames > 0) {
1102 appData.matchMode = TRUE;
1103 } else if (appData.matchMode) {
1104 appData.matchGames = 1;
1106 if(appData.matchMode && appData.sameColorGames > 0) /* [HGM] alternate: overrule matchGames */
1107 appData.matchGames = appData.sameColorGames;
1108 if(appData.rewindIndex > 1) { /* [HGM] autoinc: rewind implies auto-increment and overrules given index */
1109 if(appData.loadPositionIndex >= 0) appData.loadPositionIndex = -1;
1110 if(appData.loadGameIndex >= 0) appData.loadGameIndex = -1;
1113 if (appData.noChessProgram || first.protocolVersion == 1) {
1116 /* kludge: allow timeout for initial "feature" commands */
1118 DisplayMessage("", _("Starting chess program"));
1119 ScheduleDelayedEvent(InitBackEnd3, FEATURE_TIMEOUT);
1124 InitBackEnd3 P((void))
1126 GameMode initialMode;
1130 InitChessProgram(&first, startedFromSetupPosition);
1132 if(!appData.noChessProgram) { /* [HGM] tidy: redo program version to use name from myname feature */
1133 free(programVersion);
1134 programVersion = (char*) malloc(8 + strlen(PACKAGE_STRING) + strlen(first.tidy));
1135 sprintf(programVersion, "%s + %s", PACKAGE_STRING, first.tidy);
1138 if (appData.icsActive) {
1140 /* [DM] Make a console window if needed [HGM] merged ifs */
1145 if (*appData.icsCommPort != NULLCHAR) {
1146 sprintf(buf, _("Could not open comm port %s"),
1147 appData.icsCommPort);
1149 snprintf(buf, sizeof(buf), _("Could not connect to host %s, port %s"),
1150 appData.icsHost, appData.icsPort);
1152 DisplayFatalError(buf, err, 1);
1157 AddInputSource(icsPR, FALSE, read_from_ics, &telnetISR);
1159 AddInputSource(NoProc, FALSE, read_from_player, &fromUserISR);
1160 if(appData.keepAlive) // [HGM] alive: schedule sending of dummy 'date' command
1161 ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
1162 } else if (appData.noChessProgram) {
1168 if (*appData.cmailGameName != NULLCHAR) {
1170 OpenLoopback(&cmailPR);
1172 AddInputSource(cmailPR, FALSE, CmailSigHandlerCallBack, &cmailISR);
1176 DisplayMessage("", "");
1177 if (StrCaseCmp(appData.initialMode, "") == 0) {
1178 initialMode = BeginningOfGame;
1179 } else if (StrCaseCmp(appData.initialMode, "TwoMachines") == 0) {
1180 initialMode = TwoMachinesPlay;
1181 } else if (StrCaseCmp(appData.initialMode, "AnalyzeFile") == 0) {
1182 initialMode = AnalyzeFile;
1183 } else if (StrCaseCmp(appData.initialMode, "Analysis") == 0) {
1184 initialMode = AnalyzeMode;
1185 } else if (StrCaseCmp(appData.initialMode, "MachineWhite") == 0) {
1186 initialMode = MachinePlaysWhite;
1187 } else if (StrCaseCmp(appData.initialMode, "MachineBlack") == 0) {
1188 initialMode = MachinePlaysBlack;
1189 } else if (StrCaseCmp(appData.initialMode, "EditGame") == 0) {
1190 initialMode = EditGame;
1191 } else if (StrCaseCmp(appData.initialMode, "EditPosition") == 0) {
1192 initialMode = EditPosition;
1193 } else if (StrCaseCmp(appData.initialMode, "Training") == 0) {
1194 initialMode = Training;
1196 sprintf(buf, _("Unknown initialMode %s"), appData.initialMode);
1197 DisplayFatalError(buf, 0, 2);
1201 if (appData.matchMode) {
1202 /* Set up machine vs. machine match */
1203 if (appData.noChessProgram) {
1204 DisplayFatalError(_("Can't have a match with no chess programs"),
1210 if (*appData.loadGameFile != NULLCHAR) {
1211 int index = appData.loadGameIndex; // [HGM] autoinc
1212 if(index<0) lastIndex = index = 1;
1213 if (!LoadGameFromFile(appData.loadGameFile,
1215 appData.loadGameFile, FALSE)) {
1216 DisplayFatalError(_("Bad game file"), 0, 1);
1219 } else if (*appData.loadPositionFile != NULLCHAR) {
1220 int index = appData.loadPositionIndex; // [HGM] autoinc
1221 if(index<0) lastIndex = index = 1;
1222 if (!LoadPositionFromFile(appData.loadPositionFile,
1224 appData.loadPositionFile)) {
1225 DisplayFatalError(_("Bad position file"), 0, 1);
1230 } else if (*appData.cmailGameName != NULLCHAR) {
1231 /* Set up cmail mode */
1232 ReloadCmailMsgEvent(TRUE);
1234 /* Set up other modes */
1235 if (initialMode == AnalyzeFile) {
1236 if (*appData.loadGameFile == NULLCHAR) {
1237 DisplayFatalError(_("AnalyzeFile mode requires a game file"), 0, 1);
1241 if (*appData.loadGameFile != NULLCHAR) {
1242 (void) LoadGameFromFile(appData.loadGameFile,
1243 appData.loadGameIndex,
1244 appData.loadGameFile, TRUE);
1245 } else if (*appData.loadPositionFile != NULLCHAR) {
1246 (void) LoadPositionFromFile(appData.loadPositionFile,
1247 appData.loadPositionIndex,
1248 appData.loadPositionFile);
1249 /* [HGM] try to make self-starting even after FEN load */
1250 /* to allow automatic setup of fairy variants with wtm */
1251 if(initialMode == BeginningOfGame && !blackPlaysFirst) {
1252 gameMode = BeginningOfGame;
1253 setboardSpoiledMachineBlack = 1;
1255 /* [HGM] loadPos: make that every new game uses the setup */
1256 /* from file as long as we do not switch variant */
1257 if(!blackPlaysFirst) {
1258 startedFromPositionFile = TRUE;
1259 CopyBoard(filePosition, boards[0]);
1262 if (initialMode == AnalyzeMode) {
1263 if (appData.noChessProgram) {
1264 DisplayFatalError(_("Analysis mode requires a chess engine"), 0, 2);
1267 if (appData.icsActive) {
1268 DisplayFatalError(_("Analysis mode does not work with ICS mode"),0,2);
1272 } else if (initialMode == AnalyzeFile) {
1273 appData.showThinking = TRUE; // [HGM] thinking: moved out of ShowThinkingEvent
1274 ShowThinkingEvent();
1276 AnalysisPeriodicEvent(1);
1277 } else if (initialMode == MachinePlaysWhite) {
1278 if (appData.noChessProgram) {
1279 DisplayFatalError(_("MachineWhite mode requires a chess engine"),
1283 if (appData.icsActive) {
1284 DisplayFatalError(_("MachineWhite mode does not work with ICS mode"),
1288 MachineWhiteEvent();
1289 } else if (initialMode == MachinePlaysBlack) {
1290 if (appData.noChessProgram) {
1291 DisplayFatalError(_("MachineBlack mode requires a chess engine"),
1295 if (appData.icsActive) {
1296 DisplayFatalError(_("MachineBlack mode does not work with ICS mode"),
1300 MachineBlackEvent();
1301 } else if (initialMode == TwoMachinesPlay) {
1302 if (appData.noChessProgram) {
1303 DisplayFatalError(_("TwoMachines mode requires a chess engine"),
1307 if (appData.icsActive) {
1308 DisplayFatalError(_("TwoMachines mode does not work with ICS mode"),
1313 } else if (initialMode == EditGame) {
1315 } else if (initialMode == EditPosition) {
1316 EditPositionEvent();
1317 } else if (initialMode == Training) {
1318 if (*appData.loadGameFile == NULLCHAR) {
1319 DisplayFatalError(_("Training mode requires a game file"), 0, 2);
1328 * Establish will establish a contact to a remote host.port.
1329 * Sets icsPR to a ProcRef for a process (or pseudo-process)
1330 * used to talk to the host.
1331 * Returns 0 if okay, error code if not.
1338 if (*appData.icsCommPort != NULLCHAR) {
1339 /* Talk to the host through a serial comm port */
1340 return OpenCommPort(appData.icsCommPort, &icsPR);
1342 } else if (*appData.gateway != NULLCHAR) {
1343 if (*appData.remoteShell == NULLCHAR) {
1344 /* Use the rcmd protocol to run telnet program on a gateway host */
1345 snprintf(buf, sizeof(buf), "%s %s %s",
1346 appData.telnetProgram, appData.icsHost, appData.icsPort);
1347 return OpenRcmd(appData.gateway, appData.remoteUser, buf, &icsPR);
1350 /* Use the rsh program to run telnet program on a gateway host */
1351 if (*appData.remoteUser == NULLCHAR) {
1352 snprintf(buf, sizeof(buf), "%s %s %s %s %s", appData.remoteShell,
1353 appData.gateway, appData.telnetProgram,
1354 appData.icsHost, appData.icsPort);
1356 snprintf(buf, sizeof(buf), "%s %s -l %s %s %s %s",
1357 appData.remoteShell, appData.gateway,
1358 appData.remoteUser, appData.telnetProgram,
1359 appData.icsHost, appData.icsPort);
1361 return StartChildProcess(buf, "", &icsPR);
1364 } else if (appData.useTelnet) {
1365 return OpenTelnet(appData.icsHost, appData.icsPort, &icsPR);
1368 /* TCP socket interface differs somewhat between
1369 Unix and NT; handle details in the front end.
1371 return OpenTCP(appData.icsHost, appData.icsPort, &icsPR);
1375 void EscapeExpand(char *p, char *q)
1376 { // [HGM] initstring: routine to shape up string arguments
1377 while(*p++ = *q++) if(p[-1] == '\\')
1379 case 'n': p[-1] = '\n'; break;
1380 case 'r': p[-1] = '\r'; break;
1381 case 't': p[-1] = '\t'; break;
1382 case '\\': p[-1] = '\\'; break;
1383 case 0: *p = 0; return;
1384 default: p[-1] = q[-1]; break;
1389 show_bytes(fp, buf, count)
1395 if (*buf < 040 || *(unsigned char *) buf > 0177) {
1396 fprintf(fp, "\\%03o", *buf & 0xff);
1405 /* Returns an errno value */
1407 OutputMaybeTelnet(pr, message, count, outError)
1413 char buf[8192], *p, *q, *buflim;
1414 int left, newcount, outcount;
1416 if (*appData.icsCommPort != NULLCHAR || appData.useTelnet ||
1417 *appData.gateway != NULLCHAR) {
1418 if (appData.debugMode) {
1419 fprintf(debugFP, ">ICS: ");
1420 show_bytes(debugFP, message, count);
1421 fprintf(debugFP, "\n");
1423 return OutputToProcess(pr, message, count, outError);
1426 buflim = &buf[sizeof(buf)-1]; /* allow 1 byte for expanding last char */
1433 if (appData.debugMode) {
1434 fprintf(debugFP, ">ICS: ");
1435 show_bytes(debugFP, buf, newcount);
1436 fprintf(debugFP, "\n");
1438 outcount = OutputToProcess(pr, buf, newcount, outError);
1439 if (outcount < newcount) return -1; /* to be sure */
1446 } else if (((unsigned char) *p) == TN_IAC) {
1447 *q++ = (char) TN_IAC;
1454 if (appData.debugMode) {
1455 fprintf(debugFP, ">ICS: ");
1456 show_bytes(debugFP, buf, newcount);
1457 fprintf(debugFP, "\n");
1459 outcount = OutputToProcess(pr, buf, newcount, outError);
1460 if (outcount < newcount) return -1; /* to be sure */
1465 read_from_player(isr, closure, message, count, error)
1472 int outError, outCount;
1473 static int gotEof = 0;
1475 /* Pass data read from player on to ICS */
1478 outCount = OutputMaybeTelnet(icsPR, message, count, &outError);
1479 if (outCount < count) {
1480 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1482 } else if (count < 0) {
1483 RemoveInputSource(isr);
1484 DisplayFatalError(_("Error reading from keyboard"), error, 1);
1485 } else if (gotEof++ > 0) {
1486 RemoveInputSource(isr);
1487 DisplayFatalError(_("Got end of file from keyboard"), 0, 0);
1493 { // [HGM] alive: periodically send dummy (date) command to ICS to prevent time-out
1494 if(!connectionAlive) DisplayFatalError("No response from ICS", 0, 1);
1495 connectionAlive = FALSE; // only sticks if no response to 'date' command.
1496 SendToICS("date\n");
1497 if(appData.keepAlive) ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
1500 /* added routine for printf style output to ics */
1501 void ics_printf(char *format, ...)
1503 char buffer[MSG_SIZ];
1506 va_start(args, format);
1507 vsnprintf(buffer, sizeof(buffer), format, args);
1508 buffer[sizeof(buffer)-1] = '\0';
1517 int count, outCount, outError;
1519 if (icsPR == NULL) return;
1522 outCount = OutputMaybeTelnet(icsPR, s, count, &outError);
1523 if (outCount < count) {
1524 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1528 /* This is used for sending logon scripts to the ICS. Sending
1529 without a delay causes problems when using timestamp on ICC
1530 (at least on my machine). */
1532 SendToICSDelayed(s,msdelay)
1536 int count, outCount, outError;
1538 if (icsPR == NULL) return;
1541 if (appData.debugMode) {
1542 fprintf(debugFP, ">ICS: ");
1543 show_bytes(debugFP, s, count);
1544 fprintf(debugFP, "\n");
1546 outCount = OutputToProcessDelayed(icsPR, s, count, &outError,
1548 if (outCount < count) {
1549 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1554 /* Remove all highlighting escape sequences in s
1555 Also deletes any suffix starting with '('
1558 StripHighlightAndTitle(s)
1561 static char retbuf[MSG_SIZ];
1564 while (*s != NULLCHAR) {
1565 while (*s == '\033') {
1566 while (*s != NULLCHAR && !isalpha(*s)) s++;
1567 if (*s != NULLCHAR) s++;
1569 while (*s != NULLCHAR && *s != '\033') {
1570 if (*s == '(' || *s == '[') {
1581 /* Remove all highlighting escape sequences in s */
1586 static char retbuf[MSG_SIZ];
1589 while (*s != NULLCHAR) {
1590 while (*s == '\033') {
1591 while (*s != NULLCHAR && !isalpha(*s)) s++;
1592 if (*s != NULLCHAR) s++;
1594 while (*s != NULLCHAR && *s != '\033') {
1602 char *variantNames[] = VARIANT_NAMES;
1607 return variantNames[v];
1611 /* Identify a variant from the strings the chess servers use or the
1612 PGN Variant tag names we use. */
1619 VariantClass v = VariantNormal;
1620 int i, found = FALSE;
1625 /* [HGM] skip over optional board-size prefixes */
1626 if( sscanf(e, "%dx%d_", &i, &i) == 2 ||
1627 sscanf(e, "%dx%d+%d_", &i, &i, &i) == 3 ) {
1628 while( *e++ != '_');
1631 if(StrCaseStr(e, "misc/")) { // [HGM] on FICS, misc/shogi is not shogi
1635 for (i=0; i<sizeof(variantNames)/sizeof(char*); i++) {
1636 if (StrCaseStr(e, variantNames[i])) {
1637 v = (VariantClass) i;
1644 if ((StrCaseStr(e, "fischer") && StrCaseStr(e, "random"))
1645 || StrCaseStr(e, "wild/fr")
1646 || StrCaseStr(e, "frc") || StrCaseStr(e, "960")) {
1647 v = VariantFischeRandom;
1648 } else if ((i = 4, p = StrCaseStr(e, "wild")) ||
1649 (i = 1, p = StrCaseStr(e, "w"))) {
1651 while (*p && (isspace(*p) || *p == '(' || *p == '/')) p++;
1658 case 0: /* FICS only, actually */
1660 /* Castling legal even if K starts on d-file */
1661 v = VariantWildCastle;
1666 /* Castling illegal even if K & R happen to start in
1667 normal positions. */
1668 v = VariantNoCastle;
1681 /* Castling legal iff K & R start in normal positions */
1687 /* Special wilds for position setup; unclear what to do here */
1688 v = VariantLoadable;
1691 /* Bizarre ICC game */
1692 v = VariantTwoKings;
1695 v = VariantKriegspiel;
1701 v = VariantFischeRandom;
1704 v = VariantCrazyhouse;
1707 v = VariantBughouse;
1713 /* Not quite the same as FICS suicide! */
1714 v = VariantGiveaway;
1720 v = VariantShatranj;
1723 /* Temporary names for future ICC types. The name *will* change in
1724 the next xboard/WinBoard release after ICC defines it. */
1762 v = VariantCapablanca;
1765 v = VariantKnightmate;
1771 v = VariantCylinder;
1777 v = VariantCapaRandom;
1780 v = VariantBerolina;
1792 /* Found "wild" or "w" in the string but no number;
1793 must assume it's normal chess. */
1797 sprintf(buf, _("Unknown wild type %d"), wnum);
1798 DisplayError(buf, 0);
1804 if (appData.debugMode) {
1805 fprintf(debugFP, _("recognized '%s' (%d) as variant %s\n"),
1806 e, wnum, VariantName(v));
1811 static int leftover_start = 0, leftover_len = 0;
1812 char star_match[STAR_MATCH_N][MSG_SIZ];
1814 /* Test whether pattern is present at &buf[*index]; if so, return TRUE,
1815 advance *index beyond it, and set leftover_start to the new value of
1816 *index; else return FALSE. If pattern contains the character '*', it
1817 matches any sequence of characters not containing '\r', '\n', or the
1818 character following the '*' (if any), and the matched sequence(s) are
1819 copied into star_match.
1822 looking_at(buf, index, pattern)
1827 char *bufp = &buf[*index], *patternp = pattern;
1829 char *matchp = star_match[0];
1832 if (*patternp == NULLCHAR) {
1833 *index = leftover_start = bufp - buf;
1837 if (*bufp == NULLCHAR) return FALSE;
1838 if (*patternp == '*') {
1839 if (*bufp == *(patternp + 1)) {
1841 matchp = star_match[++star_count];
1845 } else if (*bufp == '\n' || *bufp == '\r') {
1847 if (*patternp == NULLCHAR)
1852 *matchp++ = *bufp++;
1856 if (*patternp != *bufp) return FALSE;
1863 SendToPlayer(data, length)
1867 int error, outCount;
1868 outCount = OutputToProcess(NoProc, data, length, &error);
1869 if (outCount < length) {
1870 DisplayFatalError(_("Error writing to display"), error, 1);
1875 PackHolding(packed, holding)
1887 switch (runlength) {
1898 sprintf(q, "%d", runlength);
1910 /* Telnet protocol requests from the front end */
1912 TelnetRequest(ddww, option)
1913 unsigned char ddww, option;
1915 unsigned char msg[3];
1916 int outCount, outError;
1918 if (*appData.icsCommPort != NULLCHAR || appData.useTelnet) return;
1920 if (appData.debugMode) {
1921 char buf1[8], buf2[8], *ddwwStr, *optionStr;
1937 sprintf(buf1, "%d", ddww);
1946 sprintf(buf2, "%d", option);
1949 fprintf(debugFP, ">%s %s ", ddwwStr, optionStr);
1954 outCount = OutputToProcess(icsPR, (char *)msg, 3, &outError);
1956 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1963 if (!appData.icsActive) return;
1964 TelnetRequest(TN_DO, TN_ECHO);
1970 if (!appData.icsActive) return;
1971 TelnetRequest(TN_DONT, TN_ECHO);
1975 CopyHoldings(Board board, char *holdings, ChessSquare lowestPiece)
1977 /* put the holdings sent to us by the server on the board holdings area */
1978 int i, j, holdingsColumn, holdingsStartRow, direction, countsColumn;
1982 if(gameInfo.holdingsWidth < 2) return;
1983 if(gameInfo.variant != VariantBughouse && board[HOLDINGS_SET])
1984 return; // prevent overwriting by pre-board holdings
1986 if( (int)lowestPiece >= BlackPawn ) {
1989 holdingsStartRow = BOARD_HEIGHT-1;
1992 holdingsColumn = BOARD_WIDTH-1;
1993 countsColumn = BOARD_WIDTH-2;
1994 holdingsStartRow = 0;
1998 for(i=0; i<BOARD_HEIGHT; i++) { /* clear holdings */
1999 board[i][holdingsColumn] = EmptySquare;
2000 board[i][countsColumn] = (ChessSquare) 0;
2002 while( (p=*holdings++) != NULLCHAR ) {
2003 piece = CharToPiece( ToUpper(p) );
2004 if(piece == EmptySquare) continue;
2005 /*j = (int) piece - (int) WhitePawn;*/
2006 j = PieceToNumber(piece);
2007 if(j >= gameInfo.holdingsSize) continue; /* ignore pieces that do not fit */
2008 if(j < 0) continue; /* should not happen */
2009 piece = (ChessSquare) ( (int)piece + (int)lowestPiece );
2010 board[holdingsStartRow+j*direction][holdingsColumn] = piece;
2011 board[holdingsStartRow+j*direction][countsColumn]++;
2017 VariantSwitch(Board board, VariantClass newVariant)
2019 int newHoldingsWidth, newWidth = 8, newHeight = 8, i, j;
2020 static Board oldBoard;
2022 startedFromPositionFile = FALSE;
2023 if(gameInfo.variant == newVariant) return;
2025 /* [HGM] This routine is called each time an assignment is made to
2026 * gameInfo.variant during a game, to make sure the board sizes
2027 * are set to match the new variant. If that means adding or deleting
2028 * holdings, we shift the playing board accordingly
2029 * This kludge is needed because in ICS observe mode, we get boards
2030 * of an ongoing game without knowing the variant, and learn about the
2031 * latter only later. This can be because of the move list we requested,
2032 * in which case the game history is refilled from the beginning anyway,
2033 * but also when receiving holdings of a crazyhouse game. In the latter
2034 * case we want to add those holdings to the already received position.
2038 if (appData.debugMode) {
2039 fprintf(debugFP, "Switch board from %s to %s\n",
2040 VariantName(gameInfo.variant), VariantName(newVariant));
2041 setbuf(debugFP, NULL);
2043 shuffleOpenings = 0; /* [HGM] shuffle */
2044 gameInfo.holdingsSize = 5; /* [HGM] prepare holdings */
2048 newWidth = 9; newHeight = 9;
2049 gameInfo.holdingsSize = 7;
2050 case VariantBughouse:
2051 case VariantCrazyhouse:
2052 newHoldingsWidth = 2; break;
2056 newHoldingsWidth = 2;
2057 gameInfo.holdingsSize = 8;
2060 case VariantCapablanca:
2061 case VariantCapaRandom:
2064 newHoldingsWidth = gameInfo.holdingsSize = 0;
2067 if(newWidth != gameInfo.boardWidth ||
2068 newHeight != gameInfo.boardHeight ||
2069 newHoldingsWidth != gameInfo.holdingsWidth ) {
2071 /* shift position to new playing area, if needed */
2072 if(newHoldingsWidth > gameInfo.holdingsWidth) {
2073 for(i=0; i<BOARD_HEIGHT; i++)
2074 for(j=BOARD_RGHT-1; j>=BOARD_LEFT; j--)
2075 board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
2077 for(i=0; i<newHeight; i++) {
2078 board[i][0] = board[i][newWidth+2*newHoldingsWidth-1] = EmptySquare;
2079 board[i][1] = board[i][newWidth+2*newHoldingsWidth-2] = (ChessSquare) 0;
2081 } else if(newHoldingsWidth < gameInfo.holdingsWidth) {
2082 for(i=0; i<BOARD_HEIGHT; i++)
2083 for(j=BOARD_LEFT; j<BOARD_RGHT; j++)
2084 board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
2087 gameInfo.boardWidth = newWidth;
2088 gameInfo.boardHeight = newHeight;
2089 gameInfo.holdingsWidth = newHoldingsWidth;
2090 gameInfo.variant = newVariant;
2091 InitDrawingSizes(-2, 0);
2092 } else gameInfo.variant = newVariant;
2093 CopyBoard(oldBoard, board); // remember correctly formatted board
2094 InitPosition(FALSE); /* this sets up board[0], but also other stuff */
2095 DrawPosition(TRUE, currentMove ? boards[currentMove] : oldBoard);
2098 static int loggedOn = FALSE;
2100 /*-- Game start info cache: --*/
2102 char gs_kind[MSG_SIZ];
2103 static char player1Name[128] = "";
2104 static char player2Name[128] = "";
2105 static char cont_seq[] = "\n\\ ";
2106 static int player1Rating = -1;
2107 static int player2Rating = -1;
2108 /*----------------------------*/
2110 ColorClass curColor = ColorNormal;
2111 int suppressKibitz = 0;
2114 Boolean soughtPending = FALSE;
2115 Boolean seekGraphUp;
2116 #define MAX_SEEK_ADS 200
2118 char *seekAdList[MAX_SEEK_ADS];
2119 int ratingList[MAX_SEEK_ADS], xList[MAX_SEEK_ADS], yList[MAX_SEEK_ADS], seekNrList[MAX_SEEK_ADS], zList[MAX_SEEK_ADS];
2120 float tcList[MAX_SEEK_ADS];
2121 char colorList[MAX_SEEK_ADS];
2122 int nrOfSeekAds = 0;
2123 int minRating = 1010, maxRating = 2800;
2124 int hMargin = 10, vMargin = 20, h, w;
2125 extern int squareSize, lineGap;
2130 int x, y, color = 0, r = ratingList[i]; float tc = tcList[i];
2131 xList[i] = yList[i] = -100; // outside graph, so cannot be clicked
2132 if(r < minRating+100 && r >=0 ) r = minRating+100;
2133 if(r > maxRating) r = maxRating;
2134 if(tc < 1.) tc = 1.;
2135 if(tc > 95.) tc = 95.;
2136 x = (w-hMargin-squareSize/8-7)* log(tc)/log(95.) + hMargin;
2137 y = ((double)r - minRating)/(maxRating - minRating)
2138 * (h-vMargin-squareSize/8-1) + vMargin;
2139 if(ratingList[i] < 0) y = vMargin + squareSize/4;
2140 if(strstr(seekAdList[i], " u ")) color = 1;
2141 if(!strstr(seekAdList[i], "lightning") && // for now all wilds same color
2142 !strstr(seekAdList[i], "bullet") &&
2143 !strstr(seekAdList[i], "blitz") &&
2144 !strstr(seekAdList[i], "standard") ) color = 2;
2145 if(strstr(seekAdList[i], "(C) ")) color |= SQUARE; // plot computer seeks as squares
2146 DrawSeekDot(xList[i]=x+3*(color&~SQUARE), yList[i]=h-1-y, colorList[i]=color);
2150 AddAd(char *handle, char *rating, int base, int inc, char rated, char *type, int nr, Boolean plot)
2152 char buf[MSG_SIZ], *ext = "";
2153 VariantClass v = StringToVariant(type);
2154 if(strstr(type, "wild")) {
2155 ext = type + 4; // append wild number
2156 if(v == VariantFischeRandom) type = "chess960"; else
2157 if(v == VariantLoadable) type = "setup"; else
2158 type = VariantName(v);
2160 sprintf(buf, "%s (%s) %d %d %c %s%s", handle, rating, base, inc, rated, type, ext);
2161 if(nrOfSeekAds < MAX_SEEK_ADS-1) {
2162 if(seekAdList[nrOfSeekAds]) free(seekAdList[nrOfSeekAds]);
2163 ratingList[nrOfSeekAds] = -1; // for if seeker has no rating
2164 sscanf(rating, "%d", &ratingList[nrOfSeekAds]);
2165 tcList[nrOfSeekAds] = base + (2./3.)*inc;
2166 seekNrList[nrOfSeekAds] = nr;
2167 zList[nrOfSeekAds] = 0;
2168 seekAdList[nrOfSeekAds++] = StrSave(buf);
2169 if(plot) PlotSeekAd(nrOfSeekAds-1);
2176 int x = xList[i], y = yList[i], d=squareSize/4, k;
2177 DrawSeekBackground(x-squareSize/8, y-squareSize/8, x+squareSize/8+1, y+squareSize/8+1);
2178 if(x < hMargin+d) DrawSeekAxis(hMargin, y-squareSize/8, hMargin, y+squareSize/8+1);
2179 // now replot every dot that overlapped
2180 for(k=0; k<nrOfSeekAds; k++) if(k != i) {
2181 int xx = xList[k], yy = yList[k];
2182 if(xx <= x+d && xx > x-d && yy <= y+d && yy > y-d)
2183 DrawSeekDot(xx, yy, colorList[k]);
2188 RemoveSeekAd(int nr)
2191 for(i=0; i<nrOfSeekAds; i++) if(seekNrList[i] == nr) {
2193 if(seekAdList[i]) free(seekAdList[i]);
2194 seekAdList[i] = seekAdList[--nrOfSeekAds];
2195 seekNrList[i] = seekNrList[nrOfSeekAds];
2196 ratingList[i] = ratingList[nrOfSeekAds];
2197 colorList[i] = colorList[nrOfSeekAds];
2198 tcList[i] = tcList[nrOfSeekAds];
2199 xList[i] = xList[nrOfSeekAds];
2200 yList[i] = yList[nrOfSeekAds];
2201 zList[i] = zList[nrOfSeekAds];
2202 seekAdList[nrOfSeekAds] = NULL;
2208 MatchSoughtLine(char *line)
2210 char handle[MSG_SIZ], rating[MSG_SIZ], type[MSG_SIZ];
2211 int nr, base, inc, u=0; char dummy;
2213 if(sscanf(line, "%d %s %s %d %d rated %s", &nr, rating, handle, &base, &inc, type) == 6 ||
2214 sscanf(line, "%d %s %s %s %d %d rated %c", &nr, rating, handle, type, &base, &inc, &dummy) == 7 ||
2216 (sscanf(line, "%d %s %s %d %d unrated %s", &nr, rating, handle, &base, &inc, type) == 6 ||
2217 sscanf(line, "%d %s %s %s %d %d unrated %c", &nr, rating, handle, type, &base, &inc, &dummy) == 7) ) {
2218 // match: compact and save the line
2219 AddAd(handle, rating, base, inc, u ? 'u' : 'r', type, nr, FALSE);
2229 if(!seekGraphUp) return FALSE;
2230 h = BOARD_HEIGHT * (squareSize + lineGap) + lineGap;
2231 w = BOARD_WIDTH * (squareSize + lineGap) + lineGap;
2233 DrawSeekBackground(0, 0, w, h);
2234 DrawSeekAxis(hMargin, h-1-vMargin, w-5, h-1-vMargin);
2235 DrawSeekAxis(hMargin, h-1-vMargin, hMargin, 5);
2236 for(i=0; i<4000; i+= 100) if(i>=minRating && i<maxRating) {
2237 int yy =((double)i - minRating)/(maxRating - minRating)*(h-vMargin-squareSize/8-1) + vMargin;
2239 DrawSeekAxis(hMargin+5*(i%500==0), yy, hMargin-5, yy); // rating ticks
2242 sprintf(buf, "%d", i);
2243 DrawSeekText(buf, hMargin+squareSize/8+7, yy);
2246 DrawSeekText("unrated", hMargin+squareSize/8+7, h-1-vMargin-squareSize/4);
2247 for(i=1; i<100; i+=(i<10?1:5)) {
2248 int xx = (w-hMargin-squareSize/8-7)* log((double)i)/log(95.) + hMargin;
2249 DrawSeekAxis(xx, h-1-vMargin, xx, h-6-vMargin-3*(i%10==0)); // TC ticks
2250 if(i<=5 || (i>40 ? i%20 : i%10) == 0) {
2252 sprintf(buf, "%d", i);
2253 DrawSeekText(buf, xx-2-3*(i>9), h-1-vMargin/2);
2256 for(i=0; i<nrOfSeekAds; i++) PlotSeekAd(i);
2260 int SeekGraphClick(ClickType click, int x, int y, int moving)
2262 static int lastDown = 0, displayed = 0, lastSecond;
2263 if(!seekGraphUp) { // initiate cration of seek graph by requesting seek-ad list
2264 if(click == Release || moving) return FALSE;
2266 soughtPending = TRUE;
2267 SendToICS(ics_prefix);
2268 SendToICS("sought\n"); // should this be "sought all"?
2269 } else { // issue challenge based on clicked ad
2270 int dist = 10000; int i, closest = 0, second = 0;
2271 for(i=0; i<nrOfSeekAds; i++) {
2272 int d = (x-xList[i])*(x-xList[i]) + (y-yList[i])*(y-yList[i]) + zList[i];
2273 if(d < dist) { dist = d; closest = i; }
2274 second += (d - zList[i] < 120); // count in-range ads
2275 if(click == Press && moving != 1 && zList[i]>0) zList[i] *= 0.8; // age priority
2279 second = (second > 1);
2280 if(displayed != closest || second != lastSecond) {
2281 DisplayMessage(second ? "!" : "", seekAdList[closest]);
2282 lastSecond = second; displayed = closest;
2284 if(click == Press) {
2285 if(moving == 2) zList[closest] = 100; // right-click; push to back on press
2288 } // on press 'hit', only show info
2289 if(moving == 2) return TRUE; // ignore right up-clicks on dot
2290 sprintf(buf, "play %d\n", seekNrList[closest]);
2291 SendToICS(ics_prefix);
2293 return TRUE; // let incoming board of started game pop down the graph
2294 } else if(click == Release) { // release 'miss' is ignored
2295 zList[lastDown] = 100; // make future selection of the rejected ad more difficult
2296 if(moving == 2) { // right up-click
2297 nrOfSeekAds = 0; // refresh graph
2298 soughtPending = TRUE;
2299 SendToICS(ics_prefix);
2300 SendToICS("sought\n"); // should this be "sought all"?
2303 } else if(moving) { if(displayed >= 0) DisplayMessage("", ""); displayed = -1; return TRUE; }
2304 // press miss or release hit 'pop down' seek graph
2305 seekGraphUp = FALSE;
2306 DrawPosition(TRUE, NULL);
2312 read_from_ics(isr, closure, data, count, error)
2319 #define BUF_SIZE (16*1024) /* overflowed at 8K with "inchannel 1" on FICS? */
2320 #define STARTED_NONE 0
2321 #define STARTED_MOVES 1
2322 #define STARTED_BOARD 2
2323 #define STARTED_OBSERVE 3
2324 #define STARTED_HOLDINGS 4
2325 #define STARTED_CHATTER 5
2326 #define STARTED_COMMENT 6
2327 #define STARTED_MOVES_NOHIDE 7
2329 static int started = STARTED_NONE;
2330 static char parse[20000];
2331 static int parse_pos = 0;
2332 static char buf[BUF_SIZE + 1];
2333 static int firstTime = TRUE, intfSet = FALSE;
2334 static ColorClass prevColor = ColorNormal;
2335 static int savingComment = FALSE;
2336 static int cmatch = 0; // continuation sequence match
2343 int backup; /* [DM] For zippy color lines */
2345 char talker[MSG_SIZ]; // [HGM] chat
2348 connectionAlive = TRUE; // [HGM] alive: I think, therefore I am...
2350 if (appData.debugMode) {
2352 fprintf(debugFP, "<ICS: ");
2353 show_bytes(debugFP, data, count);
2354 fprintf(debugFP, "\n");
2358 if (appData.debugMode) { int f = forwardMostMove;
2359 fprintf(debugFP, "ics input %d, castling = %d %d %d %d %d %d\n", f,
2360 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
2361 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
2364 /* If last read ended with a partial line that we couldn't parse,
2365 prepend it to the new read and try again. */
2366 if (leftover_len > 0) {
2367 for (i=0; i<leftover_len; i++)
2368 buf[i] = buf[leftover_start + i];
2371 /* copy new characters into the buffer */
2372 bp = buf + leftover_len;
2373 buf_len=leftover_len;
2374 for (i=0; i<count; i++)
2377 if (data[i] == '\r')
2380 // join lines split by ICS?
2381 if (!appData.noJoin)
2384 Joining just consists of finding matches against the
2385 continuation sequence, and discarding that sequence
2386 if found instead of copying it. So, until a match
2387 fails, there's nothing to do since it might be the
2388 complete sequence, and thus, something we don't want
2391 if (data[i] == cont_seq[cmatch])
2394 if (cmatch == strlen(cont_seq))
2396 cmatch = 0; // complete match. just reset the counter
2399 it's possible for the ICS to not include the space
2400 at the end of the last word, making our [correct]
2401 join operation fuse two separate words. the server
2402 does this when the space occurs at the width setting.
2404 if (!buf_len || buf[buf_len-1] != ' ')
2415 match failed, so we have to copy what matched before
2416 falling through and copying this character. In reality,
2417 this will only ever be just the newline character, but
2418 it doesn't hurt to be precise.
2420 strncpy(bp, cont_seq, cmatch);
2432 buf[buf_len] = NULLCHAR;
2433 // next_out = leftover_len; // [HGM] should we set this to 0, and not print it in advance?
2438 while (i < buf_len) {
2439 /* Deal with part of the TELNET option negotiation
2440 protocol. We refuse to do anything beyond the
2441 defaults, except that we allow the WILL ECHO option,
2442 which ICS uses to turn off password echoing when we are
2443 directly connected to it. We reject this option
2444 if localLineEditing mode is on (always on in xboard)
2445 and we are talking to port 23, which might be a real
2446 telnet server that will try to keep WILL ECHO on permanently.
2448 if (buf_len - i >= 3 && (unsigned char) buf[i] == TN_IAC) {
2449 static int remoteEchoOption = FALSE; /* telnet ECHO option */
2450 unsigned char option;
2452 switch ((unsigned char) buf[++i]) {
2454 if (appData.debugMode)
2455 fprintf(debugFP, "\n<WILL ");
2456 switch (option = (unsigned char) buf[++i]) {
2458 if (appData.debugMode)
2459 fprintf(debugFP, "ECHO ");
2460 /* Reply only if this is a change, according
2461 to the protocol rules. */
2462 if (remoteEchoOption) break;
2463 if (appData.localLineEditing &&
2464 atoi(appData.icsPort) == TN_PORT) {
2465 TelnetRequest(TN_DONT, TN_ECHO);
2468 TelnetRequest(TN_DO, TN_ECHO);
2469 remoteEchoOption = TRUE;
2473 if (appData.debugMode)
2474 fprintf(debugFP, "%d ", option);
2475 /* Whatever this is, we don't want it. */
2476 TelnetRequest(TN_DONT, option);
2481 if (appData.debugMode)
2482 fprintf(debugFP, "\n<WONT ");
2483 switch (option = (unsigned char) buf[++i]) {
2485 if (appData.debugMode)
2486 fprintf(debugFP, "ECHO ");
2487 /* Reply only if this is a change, according
2488 to the protocol rules. */
2489 if (!remoteEchoOption) break;
2491 TelnetRequest(TN_DONT, TN_ECHO);
2492 remoteEchoOption = FALSE;
2495 if (appData.debugMode)
2496 fprintf(debugFP, "%d ", (unsigned char) option);
2497 /* Whatever this is, it must already be turned
2498 off, because we never agree to turn on
2499 anything non-default, so according to the
2500 protocol rules, we don't reply. */
2505 if (appData.debugMode)
2506 fprintf(debugFP, "\n<DO ");
2507 switch (option = (unsigned char) buf[++i]) {
2509 /* Whatever this is, we refuse to do it. */
2510 if (appData.debugMode)
2511 fprintf(debugFP, "%d ", option);
2512 TelnetRequest(TN_WONT, option);
2517 if (appData.debugMode)
2518 fprintf(debugFP, "\n<DONT ");
2519 switch (option = (unsigned char) buf[++i]) {
2521 if (appData.debugMode)
2522 fprintf(debugFP, "%d ", option);
2523 /* Whatever this is, we are already not doing
2524 it, because we never agree to do anything
2525 non-default, so according to the protocol
2526 rules, we don't reply. */
2531 if (appData.debugMode)
2532 fprintf(debugFP, "\n<IAC ");
2533 /* Doubled IAC; pass it through */
2537 if (appData.debugMode)
2538 fprintf(debugFP, "\n<%d ", (unsigned char) buf[i]);
2539 /* Drop all other telnet commands on the floor */
2542 if (oldi > next_out)
2543 SendToPlayer(&buf[next_out], oldi - next_out);
2549 /* OK, this at least will *usually* work */
2550 if (!loggedOn && looking_at(buf, &i, "ics%")) {
2554 if (loggedOn && !intfSet) {
2555 if (ics_type == ICS_ICC) {
2557 "/set-quietly interface %s\n/set-quietly style 12\n",
2559 if(appData.seekGraph && appData.autoRefresh) // [HGM] seekgraph
2560 strcat(str, "/set-2 51 1\n/set seek 1\n");
2561 } else if (ics_type == ICS_CHESSNET) {
2562 sprintf(str, "/style 12\n");
2564 strcpy(str, "alias $ @\n$set interface ");
2565 strcat(str, programVersion);
2566 strcat(str, "\n$iset startpos 1\n$iset ms 1\n");
2567 if(appData.seekGraph && appData.autoRefresh) // [HGM] seekgraph
2568 strcat(str, "$iset seekremove 1\n$set seek 1\n");
2570 strcat(str, "$iset nohighlight 1\n");
2572 strcat(str, "$iset lock 1\n$style 12\n");
2575 NotifyFrontendLogin();
2579 if (started == STARTED_COMMENT) {
2580 /* Accumulate characters in comment */
2581 parse[parse_pos++] = buf[i];
2582 if (buf[i] == '\n') {
2583 parse[parse_pos] = NULLCHAR;
2584 if(chattingPartner>=0) {
2586 sprintf(mess, "%s%s", talker, parse);
2587 OutputChatMessage(chattingPartner, mess);
2588 chattingPartner = -1;
2589 next_out = i+1; // [HGM] suppress printing in ICS window
2591 if(!suppressKibitz) // [HGM] kibitz
2592 AppendComment(forwardMostMove, StripHighlight(parse), TRUE);
2593 else { // [HGM kibitz: divert memorized engine kibitz to engine-output window
2594 int nrDigit = 0, nrAlph = 0, j;
2595 if(parse_pos > MSG_SIZ - 30) // defuse unreasonably long input
2596 { parse_pos = MSG_SIZ-30; parse[parse_pos - 1] = '\n'; }
2597 parse[parse_pos] = NULLCHAR;
2598 // try to be smart: if it does not look like search info, it should go to
2599 // ICS interaction window after all, not to engine-output window.
2600 for(j=0; j<parse_pos; j++) { // count letters and digits
2601 nrDigit += (parse[j] >= '0' && parse[j] <= '9');
2602 nrAlph += (parse[j] >= 'a' && parse[j] <= 'z');
2603 nrAlph += (parse[j] >= 'A' && parse[j] <= 'Z');
2605 if(nrAlph < 9*nrDigit) { // if more than 10% digit we assume search info
2606 int depth=0; float score;
2607 if(sscanf(parse, "!!! %f/%d", &score, &depth) == 2 && depth>0) {
2608 // [HGM] kibitz: save kibitzed opponent info for PGN and eval graph
2609 pvInfoList[forwardMostMove-1].depth = depth;
2610 pvInfoList[forwardMostMove-1].score = 100*score;
2612 OutputKibitz(suppressKibitz, parse);
2615 sprintf(tmp, _("your opponent kibitzes: %s"), parse);
2616 SendToPlayer(tmp, strlen(tmp));
2618 next_out = i+1; // [HGM] suppress printing in ICS window
2620 started = STARTED_NONE;
2622 /* Don't match patterns against characters in comment */
2627 if (started == STARTED_CHATTER) {
2628 if (buf[i] != '\n') {
2629 /* Don't match patterns against characters in chatter */
2633 started = STARTED_NONE;
2634 if(suppressKibitz) next_out = i+1;
2637 /* Kludge to deal with rcmd protocol */
2638 if (firstTime && looking_at(buf, &i, "\001*")) {
2639 DisplayFatalError(&buf[1], 0, 1);
2645 if (!loggedOn && looking_at(buf, &i, "chessclub.com")) {
2648 if (appData.debugMode)
2649 fprintf(debugFP, "ics_type %d\n", ics_type);
2652 if (!loggedOn && looking_at(buf, &i, "freechess.org")) {
2653 ics_type = ICS_FICS;
2655 if (appData.debugMode)
2656 fprintf(debugFP, "ics_type %d\n", ics_type);
2659 if (!loggedOn && looking_at(buf, &i, "chess.net")) {
2660 ics_type = ICS_CHESSNET;
2662 if (appData.debugMode)
2663 fprintf(debugFP, "ics_type %d\n", ics_type);
2668 (looking_at(buf, &i, "\"*\" is *a registered name") ||
2669 looking_at(buf, &i, "Logging you in as \"*\"") ||
2670 looking_at(buf, &i, "will be \"*\""))) {
2671 strcpy(ics_handle, star_match[0]);
2675 if (loggedOn && !have_set_title && ics_handle[0] != NULLCHAR) {
2677 snprintf(buf, sizeof(buf), "%s@%s", ics_handle, appData.icsHost);
2678 DisplayIcsInteractionTitle(buf);
2679 have_set_title = TRUE;
2682 /* skip finger notes */
2683 if (started == STARTED_NONE &&
2684 ((buf[i] == ' ' && isdigit(buf[i+1])) ||
2685 (buf[i] == '1' && buf[i+1] == '0')) &&
2686 buf[i+2] == ':' && buf[i+3] == ' ') {
2687 started = STARTED_CHATTER;
2693 // [HGM] seekgraph: recognize sought lines and end-of-sought message
2694 if(appData.seekGraph) {
2695 if(soughtPending && MatchSoughtLine(buf+i)) {
2696 i = strstr(buf+i, "rated") - buf;
2697 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2698 next_out = leftover_start = i;
2699 started = STARTED_CHATTER;
2700 suppressKibitz = TRUE;
2703 if((gameMode == IcsIdle || gameMode == BeginningOfGame)
2704 && looking_at(buf, &i, "* ads displayed")) {
2705 soughtPending = FALSE;
2710 if(appData.autoRefresh) {
2711 if(looking_at(buf, &i, "* (*) seeking * * * * *\"play *\" to respond)\n")) {
2712 int s = (ics_type == ICS_ICC); // ICC format differs
2714 AddAd(star_match[0], star_match[1], atoi(star_match[2+s]), atoi(star_match[3+s]),
2715 star_match[4+s][0], star_match[5-3*s], atoi(star_match[7]), TRUE);
2716 looking_at(buf, &i, "*% "); // eat prompt
2717 if(oldi > 0 && buf[oldi-1] == '\n') oldi--; // suppress preceding LF, if any
2718 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2719 next_out = i; // suppress
2722 if(looking_at(buf, &i, "\nAds removed: *\n") || looking_at(buf, &i, "\031(51 * *\031)")) {
2723 char *p = star_match[0];
2725 if(seekGraphUp) RemoveSeekAd(atoi(p));
2726 while(*p && *p++ != ' '); // next
2728 looking_at(buf, &i, "*% "); // eat prompt
2729 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2736 /* skip formula vars */
2737 if (started == STARTED_NONE &&
2738 buf[i] == 'f' && isdigit(buf[i+1]) && buf[i+2] == ':') {
2739 started = STARTED_CHATTER;
2744 // [HGM] kibitz: try to recognize opponent engine-score kibitzes, to divert them to engine-output window
2745 if (appData.autoKibitz && started == STARTED_NONE &&
2746 !appData.icsEngineAnalyze && // [HGM] [DM] ICS analyze
2747 (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack || gameMode == IcsObserving)) {
2748 if((looking_at(buf, &i, "* kibitzes: ") || looking_at(buf, &i, "* whispers: ")) &&
2749 (StrStr(star_match[0], gameInfo.white) == star_match[0] ||
2750 StrStr(star_match[0], gameInfo.black) == star_match[0] )) { // kibitz of self or opponent
2751 suppressKibitz = TRUE;
2752 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2754 if((StrStr(star_match[0], gameInfo.white) == star_match[0]
2755 && (gameMode == IcsPlayingWhite)) ||
2756 (StrStr(star_match[0], gameInfo.black) == star_match[0]
2757 && (gameMode == IcsPlayingBlack)) ) // opponent kibitz
2758 started = STARTED_CHATTER; // own kibitz we simply discard
2760 started = STARTED_COMMENT; // make sure it will be collected in parse[]
2761 parse_pos = 0; parse[0] = NULLCHAR;
2762 savingComment = TRUE;
2763 suppressKibitz = gameMode != IcsObserving ? 2 :
2764 (StrStr(star_match[0], gameInfo.white) == NULL) + 1;
2768 if((looking_at(buf, &i, "\nkibitzed to *\n") || looking_at(buf, &i, "kibitzed to *\n") ||
2769 looking_at(buf, &i, "\n(kibitzed to *\n") || looking_at(buf, &i, "(kibitzed to *\n"))
2770 && atoi(star_match[0])) {
2771 // suppress the acknowledgements of our own autoKibitz
2773 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2774 if(p = strchr(star_match[0], ' ')) p[1] = NULLCHAR; // clip off "players)" on FICS
2775 SendToPlayer(star_match[0], strlen(star_match[0]));
2776 if(looking_at(buf, &i, "*% ")) // eat prompt
2777 suppressKibitz = FALSE;
2781 } // [HGM] kibitz: end of patch
2783 // [HGM] chat: intercept tells by users for which we have an open chat window
2785 if(started == STARTED_NONE && (looking_at(buf, &i, "* tells you:") || looking_at(buf, &i, "* says:") ||
2786 looking_at(buf, &i, "* whispers:") ||
2787 looking_at(buf, &i, "* kibitzes:") ||
2788 looking_at(buf, &i, "* shouts:") ||
2789 looking_at(buf, &i, "* c-shouts:") ||
2790 looking_at(buf, &i, "--> * ") ||
2791 looking_at(buf, &i, "*(*):") && (sscanf(star_match[1], "%d", &channel),1) ||
2792 looking_at(buf, &i, "*(*)(*):") && (sscanf(star_match[2], "%d", &channel),1) ||
2793 looking_at(buf, &i, "*(*)(*)(*):") && (sscanf(star_match[3], "%d", &channel),1) ||
2794 looking_at(buf, &i, "*(*)(*)(*)(*):") && sscanf(star_match[4], "%d", &channel) == 1 )) {
2796 sscanf(star_match[0], "%[^(]", talker+1); // strip (C) or (U) off ICS handle
2797 chattingPartner = -1;
2799 if(channel >= 0) // channel broadcast; look if there is a chatbox for this channel
2800 for(p=0; p<MAX_CHAT; p++) {
2801 if(channel == atoi(chatPartner[p])) {
2802 talker[0] = '['; strcat(talker, "] ");
2803 Colorize(channel == 1 ? ColorChannel1 : ColorChannel, FALSE);
2804 chattingPartner = p; break;
2807 if(buf[i-3] == 'e') // kibitz; look if there is a KIBITZ chatbox
2808 for(p=0; p<MAX_CHAT; p++) {
2809 if(!strcmp("kibitzes", chatPartner[p])) {
2810 talker[0] = '['; strcat(talker, "] ");
2811 chattingPartner = p; break;
2814 if(buf[i-3] == 'r') // whisper; look if there is a WHISPER chatbox
2815 for(p=0; p<MAX_CHAT; p++) {
2816 if(!strcmp("whispers", chatPartner[p])) {
2817 talker[0] = '['; strcat(talker, "] ");
2818 chattingPartner = p; break;
2821 if(buf[i-3] == 't' || buf[oldi+2] == '>') {// shout, c-shout or it; look if there is a 'shouts' chatbox
2822 if(buf[i-8] == '-' && buf[i-3] == 't')
2823 for(p=0; p<MAX_CHAT; p++) { // c-shout; check if dedicatesd c-shout box exists
2824 if(!strcmp("c-shouts", chatPartner[p])) {
2825 talker[0] = '('; strcat(talker, ") "); Colorize(ColorSShout, FALSE);
2826 chattingPartner = p; break;
2829 if(chattingPartner < 0)
2830 for(p=0; p<MAX_CHAT; p++) {
2831 if(!strcmp("shouts", chatPartner[p])) {
2832 if(buf[oldi+2] == '>') { talker[0] = '<'; strcat(talker, "> "); Colorize(ColorShout, FALSE); }
2833 else if(buf[i-8] == '-') { talker[0] = '('; strcat(talker, ") "); Colorize(ColorSShout, FALSE); }
2834 else { talker[0] = '['; strcat(talker, "] "); Colorize(ColorShout, FALSE); }
2835 chattingPartner = p; break;
2839 if(chattingPartner<0) // if not, look if there is a chatbox for this indivdual
2840 for(p=0; p<MAX_CHAT; p++) if(!StrCaseCmp(talker+1, chatPartner[p])) {
2841 talker[0] = 0; Colorize(ColorTell, FALSE);
2842 chattingPartner = p; break;
2844 if(chattingPartner<0) i = oldi; else {
2845 Colorize(curColor, TRUE); // undo the bogus colorations we just made to trigger the souds
2846 if(oldi > 0 && buf[oldi-1] == '\n') oldi--;
2847 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2848 started = STARTED_COMMENT;
2849 parse_pos = 0; parse[0] = NULLCHAR;
2850 savingComment = 3 + chattingPartner; // counts as TRUE
2851 suppressKibitz = TRUE;
2854 } // [HGM] chat: end of patch
2856 if (appData.zippyTalk || appData.zippyPlay) {
2857 /* [DM] Backup address for color zippy lines */
2860 if (loggedOn == TRUE)
2861 if (ZippyControl(buf, &backup) || ZippyConverse(buf, &backup) ||
2862 (appData.zippyPlay && ZippyMatch(buf, &backup)));
2864 } // [DM] 'else { ' deleted
2866 /* Regular tells and says */
2867 (tkind = 1, looking_at(buf, &i, "* tells you: ")) ||
2868 looking_at(buf, &i, "* (your partner) tells you: ") ||
2869 looking_at(buf, &i, "* says: ") ||
2870 /* Don't color "message" or "messages" output */
2871 (tkind = 5, looking_at(buf, &i, "*. * (*:*): ")) ||
2872 looking_at(buf, &i, "*. * at *:*: ") ||
2873 looking_at(buf, &i, "--* (*:*): ") ||
2874 /* Message notifications (same color as tells) */
2875 looking_at(buf, &i, "* has left a message ") ||
2876 looking_at(buf, &i, "* just sent you a message:\n") ||
2877 /* Whispers and kibitzes */
2878 (tkind = 2, looking_at(buf, &i, "* whispers: ")) ||
2879 looking_at(buf, &i, "* kibitzes: ") ||
2881 (tkind = 3, looking_at(buf, &i, "*(*: "))) {
2883 if (tkind == 1 && strchr(star_match[0], ':')) {
2884 /* Avoid "tells you:" spoofs in channels */
2887 if (star_match[0][0] == NULLCHAR ||
2888 strchr(star_match[0], ' ') ||
2889 (tkind == 3 && strchr(star_match[1], ' '))) {
2890 /* Reject bogus matches */
2893 if (appData.colorize) {
2894 if (oldi > next_out) {
2895 SendToPlayer(&buf[next_out], oldi - next_out);
2900 Colorize(ColorTell, FALSE);
2901 curColor = ColorTell;
2904 Colorize(ColorKibitz, FALSE);
2905 curColor = ColorKibitz;
2908 p = strrchr(star_match[1], '(');
2915 Colorize(ColorChannel1, FALSE);
2916 curColor = ColorChannel1;
2918 Colorize(ColorChannel, FALSE);
2919 curColor = ColorChannel;
2923 curColor = ColorNormal;
2927 if (started == STARTED_NONE && appData.autoComment &&
2928 (gameMode == IcsObserving ||
2929 gameMode == IcsPlayingWhite ||
2930 gameMode == IcsPlayingBlack)) {
2931 parse_pos = i - oldi;
2932 memcpy(parse, &buf[oldi], parse_pos);
2933 parse[parse_pos] = NULLCHAR;
2934 started = STARTED_COMMENT;
2935 savingComment = TRUE;
2937 started = STARTED_CHATTER;
2938 savingComment = FALSE;
2945 if (looking_at(buf, &i, "* s-shouts: ") ||
2946 looking_at(buf, &i, "* c-shouts: ")) {
2947 if (appData.colorize) {
2948 if (oldi > next_out) {
2949 SendToPlayer(&buf[next_out], oldi - next_out);
2952 Colorize(ColorSShout, FALSE);
2953 curColor = ColorSShout;
2956 started = STARTED_CHATTER;
2960 if (looking_at(buf, &i, "--->")) {
2965 if (looking_at(buf, &i, "* shouts: ") ||
2966 looking_at(buf, &i, "--> ")) {
2967 if (appData.colorize) {
2968 if (oldi > next_out) {
2969 SendToPlayer(&buf[next_out], oldi - next_out);
2972 Colorize(ColorShout, FALSE);
2973 curColor = ColorShout;
2976 started = STARTED_CHATTER;
2980 if (looking_at( buf, &i, "Challenge:")) {
2981 if (appData.colorize) {
2982 if (oldi > next_out) {
2983 SendToPlayer(&buf[next_out], oldi - next_out);
2986 Colorize(ColorChallenge, FALSE);
2987 curColor = ColorChallenge;
2993 if (looking_at(buf, &i, "* offers you") ||
2994 looking_at(buf, &i, "* offers to be") ||
2995 looking_at(buf, &i, "* would like to") ||
2996 looking_at(buf, &i, "* requests to") ||
2997 looking_at(buf, &i, "Your opponent offers") ||
2998 looking_at(buf, &i, "Your opponent requests")) {
3000 if (appData.colorize) {
3001 if (oldi > next_out) {
3002 SendToPlayer(&buf[next_out], oldi - next_out);
3005 Colorize(ColorRequest, FALSE);
3006 curColor = ColorRequest;
3011 if (looking_at(buf, &i, "* (*) seeking")) {
3012 if (appData.colorize) {
3013 if (oldi > next_out) {
3014 SendToPlayer(&buf[next_out], oldi - next_out);
3017 Colorize(ColorSeek, FALSE);
3018 curColor = ColorSeek;
3023 if (looking_at(buf, &i, "\\ ")) {
3024 if (prevColor != ColorNormal) {
3025 if (oldi > next_out) {
3026 SendToPlayer(&buf[next_out], oldi - next_out);
3029 Colorize(prevColor, TRUE);
3030 curColor = prevColor;
3032 if (savingComment) {
3033 parse_pos = i - oldi;
3034 memcpy(parse, &buf[oldi], parse_pos);
3035 parse[parse_pos] = NULLCHAR;
3036 started = STARTED_COMMENT;
3037 if(savingComment >= 3) // [HGM] chat: continuation of line for chat box
3038 chattingPartner = savingComment - 3; // kludge to remember the box
3040 started = STARTED_CHATTER;
3045 if (looking_at(buf, &i, "Black Strength :") ||
3046 looking_at(buf, &i, "<<< style 10 board >>>") ||
3047 looking_at(buf, &i, "<10>") ||
3048 looking_at(buf, &i, "#@#")) {
3049 /* Wrong board style */
3051 SendToICS(ics_prefix);
3052 SendToICS("set style 12\n");
3053 SendToICS(ics_prefix);
3054 SendToICS("refresh\n");
3058 if (!have_sent_ICS_logon && looking_at(buf, &i, "login:")) {
3060 have_sent_ICS_logon = 1;
3064 if (ics_getting_history != H_GETTING_MOVES /*smpos kludge*/ &&
3065 (looking_at(buf, &i, "\n<12> ") ||
3066 looking_at(buf, &i, "<12> "))) {
3068 if (oldi > next_out) {
3069 SendToPlayer(&buf[next_out], oldi - next_out);
3072 started = STARTED_BOARD;
3077 if ((started == STARTED_NONE && looking_at(buf, &i, "\n<b1> ")) ||
3078 looking_at(buf, &i, "<b1> ")) {
3079 if (oldi > next_out) {
3080 SendToPlayer(&buf[next_out], oldi - next_out);
3083 started = STARTED_HOLDINGS;
3088 if (looking_at(buf, &i, "* *vs. * *--- *")) {
3090 /* Header for a move list -- first line */
3092 switch (ics_getting_history) {
3096 case BeginningOfGame:
3097 /* User typed "moves" or "oldmoves" while we
3098 were idle. Pretend we asked for these
3099 moves and soak them up so user can step
3100 through them and/or save them.
3103 gameMode = IcsObserving;
3106 ics_getting_history = H_GOT_UNREQ_HEADER;
3108 case EditGame: /*?*/
3109 case EditPosition: /*?*/
3110 /* Should above feature work in these modes too? */
3111 /* For now it doesn't */
3112 ics_getting_history = H_GOT_UNWANTED_HEADER;
3115 ics_getting_history = H_GOT_UNWANTED_HEADER;
3120 /* Is this the right one? */
3121 if (gameInfo.white && gameInfo.black &&
3122 strcmp(gameInfo.white, star_match[0]) == 0 &&
3123 strcmp(gameInfo.black, star_match[2]) == 0) {
3125 ics_getting_history = H_GOT_REQ_HEADER;
3128 case H_GOT_REQ_HEADER:
3129 case H_GOT_UNREQ_HEADER:
3130 case H_GOT_UNWANTED_HEADER:
3131 case H_GETTING_MOVES:
3132 /* Should not happen */
3133 DisplayError(_("Error gathering move list: two headers"), 0);
3134 ics_getting_history = H_FALSE;
3138 /* Save player ratings into gameInfo if needed */
3139 if ((ics_getting_history == H_GOT_REQ_HEADER ||
3140 ics_getting_history == H_GOT_UNREQ_HEADER) &&
3141 (gameInfo.whiteRating == -1 ||
3142 gameInfo.blackRating == -1)) {
3144 gameInfo.whiteRating = string_to_rating(star_match[1]);
3145 gameInfo.blackRating = string_to_rating(star_match[3]);
3146 if (appData.debugMode)
3147 fprintf(debugFP, _("Ratings from header: W %d, B %d\n"),
3148 gameInfo.whiteRating, gameInfo.blackRating);
3153 if (looking_at(buf, &i,
3154 "* * match, initial time: * minute*, increment: * second")) {
3155 /* Header for a move list -- second line */
3156 /* Initial board will follow if this is a wild game */
3157 if (gameInfo.event != NULL) free(gameInfo.event);
3158 sprintf(str, "ICS %s %s match", star_match[0], star_match[1]);
3159 gameInfo.event = StrSave(str);
3160 /* [HGM] we switched variant. Translate boards if needed. */
3161 VariantSwitch(boards[currentMove], StringToVariant(gameInfo.event));
3165 if (looking_at(buf, &i, "Move ")) {
3166 /* Beginning of a move list */
3167 switch (ics_getting_history) {
3169 /* Normally should not happen */
3170 /* Maybe user hit reset while we were parsing */
3173 /* Happens if we are ignoring a move list that is not
3174 * the one we just requested. Common if the user
3175 * tries to observe two games without turning off
3178 case H_GETTING_MOVES:
3179 /* Should not happen */
3180 DisplayError(_("Error gathering move list: nested"), 0);
3181 ics_getting_history = H_FALSE;
3183 case H_GOT_REQ_HEADER:
3184 ics_getting_history = H_GETTING_MOVES;
3185 started = STARTED_MOVES;
3187 if (oldi > next_out) {
3188 SendToPlayer(&buf[next_out], oldi - next_out);
3191 case H_GOT_UNREQ_HEADER:
3192 ics_getting_history = H_GETTING_MOVES;
3193 started = STARTED_MOVES_NOHIDE;
3196 case H_GOT_UNWANTED_HEADER:
3197 ics_getting_history = H_FALSE;
3203 if (looking_at(buf, &i, "% ") ||
3204 ((started == STARTED_MOVES || started == STARTED_MOVES_NOHIDE)
3205 && looking_at(buf, &i, "}*"))) { char *bookHit = NULL; // [HGM] book
3206 if(ics_type == ICS_ICC && soughtPending) { // [HGM] seekgraph: on ICC sought-list has no termination line
3207 soughtPending = FALSE;
3211 if(suppressKibitz) next_out = i;
3212 savingComment = FALSE;
3216 case STARTED_MOVES_NOHIDE:
3217 memcpy(&parse[parse_pos], &buf[oldi], i - oldi);
3218 parse[parse_pos + i - oldi] = NULLCHAR;
3219 ParseGameHistory(parse);
3221 if (appData.zippyPlay && first.initDone) {
3222 FeedMovesToProgram(&first, forwardMostMove);
3223 if (gameMode == IcsPlayingWhite) {
3224 if (WhiteOnMove(forwardMostMove)) {
3225 if (first.sendTime) {
3226 if (first.useColors) {
3227 SendToProgram("black\n", &first);
3229 SendTimeRemaining(&first, TRUE);
3231 if (first.useColors) {
3232 SendToProgram("white\n", &first); // [HGM] book: made sending of "go\n" book dependent
3234 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: probe book for initial pos
3235 first.maybeThinking = TRUE;
3237 if (first.usePlayother) {
3238 if (first.sendTime) {
3239 SendTimeRemaining(&first, TRUE);
3241 SendToProgram("playother\n", &first);
3247 } else if (gameMode == IcsPlayingBlack) {
3248 if (!WhiteOnMove(forwardMostMove)) {
3249 if (first.sendTime) {
3250 if (first.useColors) {
3251 SendToProgram("white\n", &first);
3253 SendTimeRemaining(&first, FALSE);
3255 if (first.useColors) {
3256 SendToProgram("black\n", &first);
3258 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE);
3259 first.maybeThinking = TRUE;
3261 if (first.usePlayother) {
3262 if (first.sendTime) {
3263 SendTimeRemaining(&first, FALSE);
3265 SendToProgram("playother\n", &first);
3274 if (gameMode == IcsObserving && ics_gamenum == -1) {
3275 /* Moves came from oldmoves or moves command
3276 while we weren't doing anything else.
3278 currentMove = forwardMostMove;
3279 ClearHighlights();/*!!could figure this out*/
3280 flipView = appData.flipView;
3281 DrawPosition(TRUE, boards[currentMove]);
3282 DisplayBothClocks();
3283 sprintf(str, "%s vs. %s",
3284 gameInfo.white, gameInfo.black);
3288 /* Moves were history of an active game */
3289 if (gameInfo.resultDetails != NULL) {
3290 free(gameInfo.resultDetails);
3291 gameInfo.resultDetails = NULL;
3294 HistorySet(parseList, backwardMostMove,
3295 forwardMostMove, currentMove-1);
3296 DisplayMove(currentMove - 1);
3297 if (started == STARTED_MOVES) next_out = i;
3298 started = STARTED_NONE;
3299 ics_getting_history = H_FALSE;
3302 case STARTED_OBSERVE:
3303 started = STARTED_NONE;
3304 SendToICS(ics_prefix);
3305 SendToICS("refresh\n");
3311 if(bookHit) { // [HGM] book: simulate book reply
3312 static char bookMove[MSG_SIZ]; // a bit generous?
3314 programStats.nodes = programStats.depth = programStats.time =
3315 programStats.score = programStats.got_only_move = 0;
3316 sprintf(programStats.movelist, "%s (xbook)", bookHit);
3318 strcpy(bookMove, "move ");
3319 strcat(bookMove, bookHit);
3320 HandleMachineMove(bookMove, &first);
3325 if ((started == STARTED_MOVES || started == STARTED_BOARD ||
3326 started == STARTED_HOLDINGS ||
3327 started == STARTED_MOVES_NOHIDE) && i >= leftover_len) {
3328 /* Accumulate characters in move list or board */
3329 parse[parse_pos++] = buf[i];
3332 /* Start of game messages. Mostly we detect start of game
3333 when the first board image arrives. On some versions
3334 of the ICS, though, we need to do a "refresh" after starting
3335 to observe in order to get the current board right away. */
3336 if (looking_at(buf, &i, "Adding game * to observation list")) {
3337 started = STARTED_OBSERVE;
3341 /* Handle auto-observe */
3342 if (appData.autoObserve &&
3343 (gameMode == IcsIdle || gameMode == BeginningOfGame) &&
3344 looking_at(buf, &i, "Game notification: * (*) vs. * (*)")) {
3346 /* Choose the player that was highlighted, if any. */
3347 if (star_match[0][0] == '\033' ||
3348 star_match[1][0] != '\033') {
3349 player = star_match[0];
3351 player = star_match[2];
3353 sprintf(str, "%sobserve %s\n",
3354 ics_prefix, StripHighlightAndTitle(player));
3357 /* Save ratings from notify string */
3358 strcpy(player1Name, star_match[0]);
3359 player1Rating = string_to_rating(star_match[1]);
3360 strcpy(player2Name, star_match[2]);
3361 player2Rating = string_to_rating(star_match[3]);
3363 if (appData.debugMode)
3365 "Ratings from 'Game notification:' %s %d, %s %d\n",
3366 player1Name, player1Rating,
3367 player2Name, player2Rating);
3372 /* Deal with automatic examine mode after a game,
3373 and with IcsObserving -> IcsExamining transition */
3374 if (looking_at(buf, &i, "Entering examine mode for game *") ||
3375 looking_at(buf, &i, "has made you an examiner of game *")) {
3377 int gamenum = atoi(star_match[0]);
3378 if ((gameMode == IcsIdle || gameMode == IcsObserving) &&
3379 gamenum == ics_gamenum) {
3380 /* We were already playing or observing this game;
3381 no need to refetch history */
3382 gameMode = IcsExamining;
3384 pauseExamForwardMostMove = forwardMostMove;
3385 } else if (currentMove < forwardMostMove) {
3386 ForwardInner(forwardMostMove);
3389 /* I don't think this case really can happen */
3390 SendToICS(ics_prefix);
3391 SendToICS("refresh\n");
3396 /* Error messages */
3397 // if (ics_user_moved) {
3398 if (1) { // [HGM] old way ignored error after move type in; ics_user_moved is not set then!
3399 if (looking_at(buf, &i, "Illegal move") ||
3400 looking_at(buf, &i, "Not a legal move") ||
3401 looking_at(buf, &i, "Your king is in check") ||
3402 looking_at(buf, &i, "It isn't your turn") ||
3403 looking_at(buf, &i, "It is not your move")) {
3405 if (ics_user_moved && forwardMostMove > backwardMostMove) { // only backup if we already moved
3406 currentMove = forwardMostMove-1;
3407 DisplayMove(currentMove - 1); /* before DMError */
3408 DrawPosition(FALSE, boards[currentMove]);
3409 SwitchClocks(forwardMostMove-1); // [HGM] race
3410 DisplayBothClocks();
3412 DisplayMoveError(_("Illegal move (rejected by ICS)")); // [HGM] but always relay error msg
3418 if (looking_at(buf, &i, "still have time") ||
3419 looking_at(buf, &i, "not out of time") ||
3420 looking_at(buf, &i, "either player is out of time") ||
3421 looking_at(buf, &i, "has timeseal; checking")) {
3422 /* We must have called his flag a little too soon */
3423 whiteFlag = blackFlag = FALSE;
3427 if (looking_at(buf, &i, "added * seconds to") ||
3428 looking_at(buf, &i, "seconds were added to")) {
3429 /* Update the clocks */
3430 SendToICS(ics_prefix);
3431 SendToICS("refresh\n");
3435 if (!ics_clock_paused && looking_at(buf, &i, "clock paused")) {
3436 ics_clock_paused = TRUE;
3441 if (ics_clock_paused && looking_at(buf, &i, "clock resumed")) {
3442 ics_clock_paused = FALSE;
3447 /* Grab player ratings from the Creating: message.
3448 Note we have to check for the special case when
3449 the ICS inserts things like [white] or [black]. */
3450 if (looking_at(buf, &i, "Creating: * (*)* * (*)") ||
3451 looking_at(buf, &i, "Creating: * (*) [*] * (*)")) {
3453 0 player 1 name (not necessarily white)
3455 2 empty, white, or black (IGNORED)
3456 3 player 2 name (not necessarily black)
3459 The names/ratings are sorted out when the game
3460 actually starts (below).
3462 strcpy(player1Name, StripHighlightAndTitle(star_match[0]));
3463 player1Rating = string_to_rating(star_match[1]);
3464 strcpy(player2Name, StripHighlightAndTitle(star_match[3]));
3465 player2Rating = string_to_rating(star_match[4]);
3467 if (appData.debugMode)
3469 "Ratings from 'Creating:' %s %d, %s %d\n",
3470 player1Name, player1Rating,
3471 player2Name, player2Rating);
3476 /* Improved generic start/end-of-game messages */
3477 if ((tkind=0, looking_at(buf, &i, "{Game * (* vs. *) *}*")) ||
3478 (tkind=1, looking_at(buf, &i, "{Game * (*(*) vs. *(*)) *}*"))){
3479 /* If tkind == 0: */
3480 /* star_match[0] is the game number */
3481 /* [1] is the white player's name */
3482 /* [2] is the black player's name */
3483 /* For end-of-game: */
3484 /* [3] is the reason for the game end */
3485 /* [4] is a PGN end game-token, preceded by " " */
3486 /* For start-of-game: */
3487 /* [3] begins with "Creating" or "Continuing" */
3488 /* [4] is " *" or empty (don't care). */
3489 int gamenum = atoi(star_match[0]);
3490 char *whitename, *blackname, *why, *endtoken;
3491 ChessMove endtype = (ChessMove) 0;
3494 whitename = star_match[1];
3495 blackname = star_match[2];
3496 why = star_match[3];
3497 endtoken = star_match[4];
3499 whitename = star_match[1];
3500 blackname = star_match[3];
3501 why = star_match[5];
3502 endtoken = star_match[6];
3505 /* Game start messages */
3506 if (strncmp(why, "Creating ", 9) == 0 ||
3507 strncmp(why, "Continuing ", 11) == 0) {
3508 gs_gamenum = gamenum;
3509 strcpy(gs_kind, strchr(why, ' ') + 1);
3510 VariantSwitch(boards[currentMove], StringToVariant(gs_kind)); // [HGM] variantswitch: even before we get first board
3512 if (appData.zippyPlay) {
3513 ZippyGameStart(whitename, blackname);
3516 partnerBoardValid = FALSE; // [HGM] bughouse
3520 /* Game end messages */
3521 if (gameMode == IcsIdle || gameMode == BeginningOfGame ||
3522 ics_gamenum != gamenum) {
3525 while (endtoken[0] == ' ') endtoken++;
3526 switch (endtoken[0]) {
3529 endtype = GameUnfinished;
3532 endtype = BlackWins;
3535 if (endtoken[1] == '/')
3536 endtype = GameIsDrawn;
3538 endtype = WhiteWins;
3541 GameEnds(endtype, why, GE_ICS);
3543 if (appData.zippyPlay && first.initDone) {
3544 ZippyGameEnd(endtype, why);
3545 if (first.pr == NULL) {
3546 /* Start the next process early so that we'll
3547 be ready for the next challenge */
3548 StartChessProgram(&first);
3550 /* Send "new" early, in case this command takes
3551 a long time to finish, so that we'll be ready
3552 for the next challenge. */
3553 gameInfo.variant = VariantNormal; // [HGM] variantswitch: suppress sending of 'variant'
3557 if(appData.bgObserve && partnerBoardValid) DrawPosition(TRUE, partnerBoard);
3561 if (looking_at(buf, &i, "Removing game * from observation") ||
3562 looking_at(buf, &i, "no longer observing game *") ||
3563 looking_at(buf, &i, "Game * (*) has no examiners")) {
3564 if (gameMode == IcsObserving &&
3565 atoi(star_match[0]) == ics_gamenum)
3567 /* icsEngineAnalyze */
3568 if (appData.icsEngineAnalyze) {
3575 ics_user_moved = FALSE;
3580 if (looking_at(buf, &i, "no longer examining game *")) {
3581 if (gameMode == IcsExamining &&
3582 atoi(star_match[0]) == ics_gamenum)
3586 ics_user_moved = FALSE;
3591 /* Advance leftover_start past any newlines we find,
3592 so only partial lines can get reparsed */
3593 if (looking_at(buf, &i, "\n")) {
3594 prevColor = curColor;
3595 if (curColor != ColorNormal) {
3596 if (oldi > next_out) {
3597 SendToPlayer(&buf[next_out], oldi - next_out);
3600 Colorize(ColorNormal, FALSE);
3601 curColor = ColorNormal;
3603 if (started == STARTED_BOARD) {
3604 started = STARTED_NONE;
3605 parse[parse_pos] = NULLCHAR;
3606 ParseBoard12(parse);
3609 /* Send premove here */
3610 if (appData.premove) {
3612 if (currentMove == 0 &&
3613 gameMode == IcsPlayingWhite &&
3614 appData.premoveWhite) {
3615 sprintf(str, "%s\n", appData.premoveWhiteText);
3616 if (appData.debugMode)
3617 fprintf(debugFP, "Sending premove:\n");
3619 } else if (currentMove == 1 &&
3620 gameMode == IcsPlayingBlack &&
3621 appData.premoveBlack) {
3622 sprintf(str, "%s\n", appData.premoveBlackText);
3623 if (appData.debugMode)
3624 fprintf(debugFP, "Sending premove:\n");
3626 } else if (gotPremove) {
3628 ClearPremoveHighlights();
3629 if (appData.debugMode)
3630 fprintf(debugFP, "Sending premove:\n");
3631 UserMoveEvent(premoveFromX, premoveFromY,
3632 premoveToX, premoveToY,
3637 /* Usually suppress following prompt */
3638 if (!(forwardMostMove == 0 && gameMode == IcsExamining)) {
3639 while(looking_at(buf, &i, "\n")); // [HGM] skip empty lines
3640 if (looking_at(buf, &i, "*% ")) {
3641 savingComment = FALSE;
3646 } else if (started == STARTED_HOLDINGS) {
3648 char new_piece[MSG_SIZ];
3649 started = STARTED_NONE;
3650 parse[parse_pos] = NULLCHAR;
3651 if (appData.debugMode)
3652 fprintf(debugFP, "Parsing holdings: %s, currentMove = %d\n",
3653 parse, currentMove);
3654 if (sscanf(parse, " game %d", &gamenum) == 1) {
3655 if(gamenum == ics_gamenum) { // [HGM] bughouse: old code if part of foreground game
3656 if (gameInfo.variant == VariantNormal) {
3657 /* [HGM] We seem to switch variant during a game!
3658 * Presumably no holdings were displayed, so we have
3659 * to move the position two files to the right to
3660 * create room for them!
3662 VariantClass newVariant;
3663 switch(gameInfo.boardWidth) { // base guess on board width
3664 case 9: newVariant = VariantShogi; break;
3665 case 10: newVariant = VariantGreat; break;
3666 default: newVariant = VariantCrazyhouse; break;
3668 VariantSwitch(boards[currentMove], newVariant); /* temp guess */
3669 /* Get a move list just to see the header, which
3670 will tell us whether this is really bug or zh */
3671 if (ics_getting_history == H_FALSE) {
3672 ics_getting_history = H_REQUESTED;
3673 sprintf(str, "%smoves %d\n", ics_prefix, gamenum);
3677 new_piece[0] = NULLCHAR;
3678 sscanf(parse, "game %d white [%s black [%s <- %s",
3679 &gamenum, white_holding, black_holding,
3681 white_holding[strlen(white_holding)-1] = NULLCHAR;
3682 black_holding[strlen(black_holding)-1] = NULLCHAR;
3683 /* [HGM] copy holdings to board holdings area */
3684 CopyHoldings(boards[forwardMostMove], white_holding, WhitePawn);
3685 CopyHoldings(boards[forwardMostMove], black_holding, BlackPawn);
3686 boards[forwardMostMove][HOLDINGS_SET] = 1; // flag holdings as set
3688 if (appData.zippyPlay && first.initDone) {
3689 ZippyHoldings(white_holding, black_holding,
3693 if (tinyLayout || smallLayout) {
3694 char wh[16], bh[16];
3695 PackHolding(wh, white_holding);
3696 PackHolding(bh, black_holding);
3697 sprintf(str, "[%s-%s] %s-%s", wh, bh,
3698 gameInfo.white, gameInfo.black);
3700 sprintf(str, "%s [%s] vs. %s [%s]",
3701 gameInfo.white, white_holding,
3702 gameInfo.black, black_holding);
3704 if(!partnerUp) // [HGM] bughouse: when peeking at partner game we already know what he captured...
3705 DrawPosition(FALSE, boards[currentMove]);
3707 } else if(appData.bgObserve) { // [HGM] bughouse: holdings of other game => background
3708 sscanf(parse, "game %d white [%s black [%s <- %s",
3709 &gamenum, white_holding, black_holding,
3711 white_holding[strlen(white_holding)-1] = NULLCHAR;
3712 black_holding[strlen(black_holding)-1] = NULLCHAR;
3713 /* [HGM] copy holdings to partner-board holdings area */
3714 CopyHoldings(partnerBoard, white_holding, WhitePawn);
3715 CopyHoldings(partnerBoard, black_holding, BlackPawn);
3716 if(twoBoards) { partnerUp = 1; flipView = !flipView; } // [HGM] dual: always draw
3717 if(partnerUp) DrawPosition(FALSE, partnerBoard);
3718 if(twoBoards) { partnerUp = 0; flipView = !flipView; }
3721 /* Suppress following prompt */
3722 if (looking_at(buf, &i, "*% ")) {
3723 if(strchr(star_match[0], 7)) SendToPlayer("\007", 1); // Bell(); // FICS fuses bell for next board with prompt in zh captures
3724 savingComment = FALSE;
3732 i++; /* skip unparsed character and loop back */
3735 if (started != STARTED_MOVES && started != STARTED_BOARD && !suppressKibitz && // [HGM] kibitz
3736 // started != STARTED_HOLDINGS && i > next_out) { // [HGM] should we compare to leftover_start in stead of i?
3737 // SendToPlayer(&buf[next_out], i - next_out);
3738 started != STARTED_HOLDINGS && leftover_start > next_out) {
3739 SendToPlayer(&buf[next_out], leftover_start - next_out);
3743 leftover_len = buf_len - leftover_start;
3744 /* if buffer ends with something we couldn't parse,
3745 reparse it after appending the next read */
3747 } else if (count == 0) {
3748 RemoveInputSource(isr);
3749 DisplayFatalError(_("Connection closed by ICS"), 0, 0);
3751 DisplayFatalError(_("Error reading from ICS"), error, 1);
3756 /* Board style 12 looks like this:
3758 <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
3760 * The "<12> " is stripped before it gets to this routine. The two
3761 * trailing 0's (flip state and clock ticking) are later addition, and
3762 * some chess servers may not have them, or may have only the first.
3763 * Additional trailing fields may be added in the future.
3766 #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"
3768 #define RELATION_OBSERVING_PLAYED 0
3769 #define RELATION_OBSERVING_STATIC -2 /* examined, oldmoves, or smoves */
3770 #define RELATION_PLAYING_MYMOVE 1
3771 #define RELATION_PLAYING_NOTMYMOVE -1
3772 #define RELATION_EXAMINING 2
3773 #define RELATION_ISOLATED_BOARD -3
3774 #define RELATION_STARTING_POSITION -4 /* FICS only */
3777 ParseBoard12(string)
3780 GameMode newGameMode;
3781 int gamenum, newGame, newMove, relation, basetime, increment, ics_flip = 0, i;
3782 int j, k, n, moveNum, white_stren, black_stren, white_time, black_time, takeback;
3783 int double_push, castle_ws, castle_wl, castle_bs, castle_bl, irrev_count;
3784 char to_play, board_chars[200];
3785 char move_str[500], str[500], elapsed_time[500];
3786 char black[32], white[32];
3788 int prevMove = currentMove;
3791 int fromX, fromY, toX, toY;
3793 int ranks=1, files=0; /* [HGM] ICS80: allow variable board size */
3794 char *bookHit = NULL; // [HGM] book
3795 Boolean weird = FALSE, reqFlag = FALSE;
3797 fromX = fromY = toX = toY = -1;
3801 if (appData.debugMode)
3802 fprintf(debugFP, _("Parsing board: %s\n"), string);
3804 move_str[0] = NULLCHAR;
3805 elapsed_time[0] = NULLCHAR;
3806 { /* [HGM] figure out how many ranks and files the board has, for ICS extension used by Capablanca server */
3808 while(i < 199 && (string[i] != ' ' || string[i+2] != ' ')) {
3809 if(string[i] == ' ') { ranks++; files = 0; }
3811 if(!strchr(" -pnbrqkPNBRQK" , string[i])) weird = TRUE; // test for fairies
3814 for(j = 0; j <i; j++) board_chars[j] = string[j];
3815 board_chars[i] = '\0';
3818 n = sscanf(string, PATTERN, &to_play, &double_push,
3819 &castle_ws, &castle_wl, &castle_bs, &castle_bl, &irrev_count,
3820 &gamenum, white, black, &relation, &basetime, &increment,
3821 &white_stren, &black_stren, &white_time, &black_time,
3822 &moveNum, str, elapsed_time, move_str, &ics_flip,
3826 snprintf(str, sizeof(str), _("Failed to parse board string:\n\"%s\""), string);
3827 DisplayError(str, 0);
3831 /* Convert the move number to internal form */
3832 moveNum = (moveNum - 1) * 2;
3833 if (to_play == 'B') moveNum++;
3834 if (moveNum > framePtr) { // [HGM] vari: do not run into saved variations
3835 DisplayFatalError(_("Game too long; increase MAX_MOVES and recompile"),
3841 case RELATION_OBSERVING_PLAYED:
3842 case RELATION_OBSERVING_STATIC:
3843 if (gamenum == -1) {
3844 /* Old ICC buglet */
3845 relation = RELATION_OBSERVING_STATIC;
3847 newGameMode = IcsObserving;
3849 case RELATION_PLAYING_MYMOVE:
3850 case RELATION_PLAYING_NOTMYMOVE:
3852 ((relation == RELATION_PLAYING_MYMOVE) == (to_play == 'W')) ?
3853 IcsPlayingWhite : IcsPlayingBlack;
3855 case RELATION_EXAMINING:
3856 newGameMode = IcsExamining;
3858 case RELATION_ISOLATED_BOARD:
3860 /* Just display this board. If user was doing something else,
3861 we will forget about it until the next board comes. */
3862 newGameMode = IcsIdle;
3864 case RELATION_STARTING_POSITION:
3865 newGameMode = gameMode;
3869 if((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)
3870 && newGameMode == IcsObserving && gamenum != ics_gamenum && appData.bgObserve) {
3871 // [HGM] bughouse: don't act on alien boards while we play. Just parse the board and save it */
3873 for (k = 0; k < ranks; k++) {
3874 for (j = 0; j < files; j++)
3875 board[k][j+gameInfo.holdingsWidth] = CharToPiece(board_chars[(ranks-1-k)*(files+1) + j]);
3876 if(gameInfo.holdingsWidth > 1) {
3877 board[k][0] = board[k][BOARD_WIDTH-1] = EmptySquare;
3878 board[k][1] = board[k][BOARD_WIDTH-2] = (ChessSquare) 0;;
3881 CopyBoard(partnerBoard, board);
3882 if(toSqr = strchr(str, '/')) { // extract highlights from long move
3883 partnerBoard[EP_STATUS-3] = toSqr[1] - AAA; // kludge: hide highlighting info in board
3884 partnerBoard[EP_STATUS-4] = toSqr[2] - ONE;
3885 } else partnerBoard[EP_STATUS-4] = partnerBoard[EP_STATUS-3] = -1;
3886 if(toSqr = strchr(str, '-')) {
3887 partnerBoard[EP_STATUS-1] = toSqr[1] - AAA;
3888 partnerBoard[EP_STATUS-2] = toSqr[2] - ONE;
3889 } else partnerBoard[EP_STATUS-1] = partnerBoard[EP_STATUS-2] = -1;
3890 if(appData.dualBoard && !twoBoards) { twoBoards = 1; InitDrawingSizes(-2,0); }
3891 if(twoBoards) { partnerUp = 1; flipView = !flipView; } // [HGM] dual
3892 if(partnerUp) DrawPosition(FALSE, partnerBoard);
3893 if(twoBoards) { partnerUp = 0; flipView = !flipView; } // [HGM] dual
3894 sprintf(partnerStatus, "W: %d:%02d B: %d:%02d (%d-%d) %c", white_time/60000, (white_time%60000)/1000,
3895 (black_time/60000), (black_time%60000)/1000, white_stren, black_stren, to_play);
3896 DisplayMessage(partnerStatus, "");
3897 partnerBoardValid = TRUE;
3901 /* Modify behavior for initial board display on move listing
3904 switch (ics_getting_history) {
3908 case H_GOT_REQ_HEADER:
3909 case H_GOT_UNREQ_HEADER:
3910 /* This is the initial position of the current game */
3911 gamenum = ics_gamenum;
3912 moveNum = 0; /* old ICS bug workaround */
3913 if (to_play == 'B') {
3914 startedFromSetupPosition = TRUE;
3915 blackPlaysFirst = TRUE;
3917 if (forwardMostMove == 0) forwardMostMove = 1;
3918 if (backwardMostMove == 0) backwardMostMove = 1;
3919 if (currentMove == 0) currentMove = 1;
3921 newGameMode = gameMode;
3922 relation = RELATION_STARTING_POSITION; /* ICC needs this */
3924 case H_GOT_UNWANTED_HEADER:
3925 /* This is an initial board that we don't want */
3927 case H_GETTING_MOVES:
3928 /* Should not happen */
3929 DisplayError(_("Error gathering move list: extra board"), 0);
3930 ics_getting_history = H_FALSE;
3934 if (gameInfo.boardHeight != ranks || gameInfo.boardWidth != files ||
3935 weird && (int)gameInfo.variant <= (int)VariantShogi) {
3936 /* [HGM] We seem to have switched variant unexpectedly
3937 * Try to guess new variant from board size
3939 VariantClass newVariant = VariantFairy; // if 8x8, but fairies present
3940 if(ranks == 8 && files == 10) newVariant = VariantCapablanca; else
3941 if(ranks == 10 && files == 9) newVariant = VariantXiangqi; else
3942 if(ranks == 8 && files == 12) newVariant = VariantCourier; else
3943 if(ranks == 9 && files == 9) newVariant = VariantShogi; else
3944 if(!weird) newVariant = VariantNormal;
3945 VariantSwitch(boards[currentMove], newVariant); /* temp guess */
3946 /* Get a move list just to see the header, which
3947 will tell us whether this is really bug or zh */
3948 if (ics_getting_history == H_FALSE) {
3949 ics_getting_history = H_REQUESTED; reqFlag = TRUE;
3950 sprintf(str, "%smoves %d\n", ics_prefix, gamenum);
3955 /* Take action if this is the first board of a new game, or of a
3956 different game than is currently being displayed. */
3957 if (gamenum != ics_gamenum || newGameMode != gameMode ||
3958 relation == RELATION_ISOLATED_BOARD) {
3960 /* Forget the old game and get the history (if any) of the new one */
3961 if (gameMode != BeginningOfGame) {
3965 if (appData.autoRaiseBoard) BoardToTop();
3967 if (gamenum == -1) {
3968 newGameMode = IcsIdle;
3969 } else if ((moveNum > 0 || newGameMode == IcsObserving) && newGameMode != IcsIdle &&
3970 appData.getMoveList && !reqFlag) {
3971 /* Need to get game history */
3972 ics_getting_history = H_REQUESTED;
3973 sprintf(str, "%smoves %d\n", ics_prefix, gamenum);
3977 /* Initially flip the board to have black on the bottom if playing
3978 black or if the ICS flip flag is set, but let the user change
3979 it with the Flip View button. */
3980 flipView = appData.autoFlipView ?
3981 (newGameMode == IcsPlayingBlack) || ics_flip :
3984 /* Done with values from previous mode; copy in new ones */
3985 gameMode = newGameMode;
3987 ics_gamenum = gamenum;
3988 if (gamenum == gs_gamenum) {
3989 int klen = strlen(gs_kind);
3990 if (gs_kind[klen - 1] == '.') gs_kind[klen - 1] = NULLCHAR;
3991 sprintf(str, "ICS %s", gs_kind);
3992 gameInfo.event = StrSave(str);
3994 gameInfo.event = StrSave("ICS game");
3996 gameInfo.site = StrSave(appData.icsHost);
3997 gameInfo.date = PGNDate();
3998 gameInfo.round = StrSave("-");
3999 gameInfo.white = StrSave(white);
4000 gameInfo.black = StrSave(black);
4001 timeControl = basetime * 60 * 1000;
4003 timeIncrement = increment * 1000;
4004 movesPerSession = 0;
4005 gameInfo.timeControl = TimeControlTagValue();
4006 VariantSwitch(boards[currentMove], StringToVariant(gameInfo.event) );
4007 if (appData.debugMode) {
4008 fprintf(debugFP, "ParseBoard says variant = '%s'\n", gameInfo.event);
4009 fprintf(debugFP, "recognized as %s\n", VariantName(gameInfo.variant));
4010 setbuf(debugFP, NULL);
4013 gameInfo.outOfBook = NULL;
4015 /* Do we have the ratings? */
4016 if (strcmp(player1Name, white) == 0 &&
4017 strcmp(player2Name, black) == 0) {
4018 if (appData.debugMode)
4019 fprintf(debugFP, "Remembered ratings: W %d, B %d\n",
4020 player1Rating, player2Rating);
4021 gameInfo.whiteRating = player1Rating;
4022 gameInfo.blackRating = player2Rating;
4023 } else if (strcmp(player2Name, white) == 0 &&
4024 strcmp(player1Name, black) == 0) {
4025 if (appData.debugMode)
4026 fprintf(debugFP, "Remembered ratings: W %d, B %d\n",
4027 player2Rating, player1Rating);
4028 gameInfo.whiteRating = player2Rating;
4029 gameInfo.blackRating = player1Rating;
4031 player1Name[0] = player2Name[0] = NULLCHAR;
4033 /* Silence shouts if requested */
4034 if (appData.quietPlay &&
4035 (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)) {
4036 SendToICS(ics_prefix);
4037 SendToICS("set shout 0\n");
4041 /* Deal with midgame name changes */
4043 if (!gameInfo.white || strcmp(gameInfo.white, white) != 0) {
4044 if (gameInfo.white) free(gameInfo.white);
4045 gameInfo.white = StrSave(white);
4047 if (!gameInfo.black || strcmp(gameInfo.black, black) != 0) {
4048 if (gameInfo.black) free(gameInfo.black);
4049 gameInfo.black = StrSave(black);
4053 /* Throw away game result if anything actually changes in examine mode */
4054 if (gameMode == IcsExamining && !newGame) {
4055 gameInfo.result = GameUnfinished;
4056 if (gameInfo.resultDetails != NULL) {
4057 free(gameInfo.resultDetails);
4058 gameInfo.resultDetails = NULL;
4062 /* In pausing && IcsExamining mode, we ignore boards coming
4063 in if they are in a different variation than we are. */
4064 if (pauseExamInvalid) return;
4065 if (pausing && gameMode == IcsExamining) {
4066 if (moveNum <= pauseExamForwardMostMove) {
4067 pauseExamInvalid = TRUE;
4068 forwardMostMove = pauseExamForwardMostMove;
4073 if (appData.debugMode) {
4074 fprintf(debugFP, "load %dx%d board\n", files, ranks);
4076 /* Parse the board */
4077 for (k = 0; k < ranks; k++) {
4078 for (j = 0; j < files; j++)
4079 board[k][j+gameInfo.holdingsWidth] = CharToPiece(board_chars[(ranks-1-k)*(files+1) + j]);
4080 if(gameInfo.holdingsWidth > 1) {
4081 board[k][0] = board[k][BOARD_WIDTH-1] = EmptySquare;
4082 board[k][1] = board[k][BOARD_WIDTH-2] = (ChessSquare) 0;;
4085 CopyBoard(boards[moveNum], board);
4086 boards[moveNum][HOLDINGS_SET] = 0; // [HGM] indicate holdings not set
4088 startedFromSetupPosition =
4089 !CompareBoards(board, initialPosition);
4090 if(startedFromSetupPosition)
4091 initialRulePlies = irrev_count; /* [HGM] 50-move counter offset */
4094 /* [HGM] Set castling rights. Take the outermost Rooks,
4095 to make it also work for FRC opening positions. Note that board12
4096 is really defective for later FRC positions, as it has no way to
4097 indicate which Rook can castle if they are on the same side of King.
4098 For the initial position we grant rights to the outermost Rooks,
4099 and remember thos rights, and we then copy them on positions
4100 later in an FRC game. This means WB might not recognize castlings with
4101 Rooks that have moved back to their original position as illegal,
4102 but in ICS mode that is not its job anyway.
4104 if(moveNum == 0 || gameInfo.variant != VariantFischeRandom)
4105 { int i, j; ChessSquare wKing = WhiteKing, bKing = BlackKing;
4107 for(i=BOARD_LEFT, j=NoRights; i<BOARD_RGHT; i++)
4108 if(board[0][i] == WhiteRook) j = i;
4109 initialRights[0] = boards[moveNum][CASTLING][0] = (castle_ws == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4110 for(i=BOARD_RGHT-1, j=NoRights; i>=BOARD_LEFT; i--)
4111 if(board[0][i] == WhiteRook) j = i;
4112 initialRights[1] = boards[moveNum][CASTLING][1] = (castle_wl == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4113 for(i=BOARD_LEFT, j=NoRights; i<BOARD_RGHT; i++)
4114 if(board[BOARD_HEIGHT-1][i] == BlackRook) j = i;
4115 initialRights[3] = boards[moveNum][CASTLING][3] = (castle_bs == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4116 for(i=BOARD_RGHT-1, j=NoRights; i>=BOARD_LEFT; i--)
4117 if(board[BOARD_HEIGHT-1][i] == BlackRook) j = i;
4118 initialRights[4] = boards[moveNum][CASTLING][4] = (castle_bl == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4120 if(gameInfo.variant == VariantKnightmate) { wKing = WhiteUnicorn; bKing = BlackUnicorn; }
4121 for(k=BOARD_LEFT; k<BOARD_RGHT; k++)
4122 if(board[0][k] == wKing) initialRights[2] = boards[moveNum][CASTLING][2] = k;
4123 for(k=BOARD_LEFT; k<BOARD_RGHT; k++)
4124 if(board[BOARD_HEIGHT-1][k] == bKing)
4125 initialRights[5] = boards[moveNum][CASTLING][5] = k;
4126 if(gameInfo.variant == VariantTwoKings) {
4127 // In TwoKings looking for a King does not work, so always give castling rights to a King on e1/e8
4128 if(board[0][4] == wKing) initialRights[2] = boards[moveNum][CASTLING][2] = 4;
4129 if(board[BOARD_HEIGHT-1][4] == bKing) initialRights[5] = boards[moveNum][CASTLING][5] = 4;
4132 r = boards[moveNum][CASTLING][0] = initialRights[0];
4133 if(board[0][r] != WhiteRook) boards[moveNum][CASTLING][0] = NoRights;
4134 r = boards[moveNum][CASTLING][1] = initialRights[1];
4135 if(board[0][r] != WhiteRook) boards[moveNum][CASTLING][1] = NoRights;
4136 r = boards[moveNum][CASTLING][3] = initialRights[3];
4137 if(board[BOARD_HEIGHT-1][r] != BlackRook) boards[moveNum][CASTLING][3] = NoRights;
4138 r = boards[moveNum][CASTLING][4] = initialRights[4];
4139 if(board[BOARD_HEIGHT-1][r] != BlackRook) boards[moveNum][CASTLING][4] = NoRights;
4140 /* wildcastle kludge: always assume King has rights */
4141 r = boards[moveNum][CASTLING][2] = initialRights[2];
4142 r = boards[moveNum][CASTLING][5] = initialRights[5];
4144 /* [HGM] e.p. rights. Assume that ICS sends file number here? */
4145 boards[moveNum][EP_STATUS] = double_push == -1 ? EP_NONE : double_push + BOARD_LEFT;
4148 if (ics_getting_history == H_GOT_REQ_HEADER ||
4149 ics_getting_history == H_GOT_UNREQ_HEADER) {
4150 /* This was an initial position from a move list, not
4151 the current position */
4155 /* Update currentMove and known move number limits */
4156 newMove = newGame || moveNum > forwardMostMove;
4159 forwardMostMove = backwardMostMove = currentMove = moveNum;
4160 if (gameMode == IcsExamining && moveNum == 0) {
4161 /* Workaround for ICS limitation: we are not told the wild
4162 type when starting to examine a game. But if we ask for
4163 the move list, the move list header will tell us */
4164 ics_getting_history = H_REQUESTED;
4165 sprintf(str, "%smoves %d\n", ics_prefix, gamenum);
4168 } else if (moveNum == forwardMostMove + 1 || moveNum == forwardMostMove
4169 || (moveNum < forwardMostMove && moveNum >= backwardMostMove)) {
4171 /* [DM] If we found takebacks during icsEngineAnalyze try send to engine */
4172 /* [HGM] applied this also to an engine that is silently watching */
4173 if (appData.zippyPlay && moveNum < forwardMostMove && first.initDone &&
4174 (gameMode == IcsObserving || gameMode == IcsExamining) &&
4175 gameInfo.variant == currentlyInitializedVariant) {
4176 takeback = forwardMostMove - moveNum;
4177 for (i = 0; i < takeback; i++) {
4178 if (appData.debugMode) fprintf(debugFP, "take back move\n");
4179 SendToProgram("undo\n", &first);
4184 forwardMostMove = moveNum;
4185 if (!pausing || currentMove > forwardMostMove)
4186 currentMove = forwardMostMove;
4188 /* New part of history that is not contiguous with old part */
4189 if (pausing && gameMode == IcsExamining) {
4190 pauseExamInvalid = TRUE;
4191 forwardMostMove = pauseExamForwardMostMove;
4194 if (gameMode == IcsExamining && moveNum > 0 && appData.getMoveList) {
4196 if(appData.zippyPlay && forwardMostMove > 0 && first.initDone) {
4197 // [HGM] when we will receive the move list we now request, it will be
4198 // fed to the engine from the first move on. So if the engine is not
4199 // in the initial position now, bring it there.
4200 InitChessProgram(&first, 0);
4203 ics_getting_history = H_REQUESTED;
4204 sprintf(str, "%smoves %d\n", ics_prefix, gamenum);
4207 forwardMostMove = backwardMostMove = currentMove = moveNum;
4210 /* Update the clocks */
4211 if (strchr(elapsed_time, '.')) {
4213 timeRemaining[0][moveNum] = whiteTimeRemaining = white_time;
4214 timeRemaining[1][moveNum] = blackTimeRemaining = black_time;
4216 /* Time is in seconds */
4217 timeRemaining[0][moveNum] = whiteTimeRemaining = white_time * 1000;
4218 timeRemaining[1][moveNum] = blackTimeRemaining = black_time * 1000;
4223 if (appData.zippyPlay && newGame &&
4224 gameMode != IcsObserving && gameMode != IcsIdle &&
4225 gameMode != IcsExamining)
4226 ZippyFirstBoard(moveNum, basetime, increment);
4229 /* Put the move on the move list, first converting
4230 to canonical algebraic form. */
4232 if (appData.debugMode) {
4233 if (appData.debugMode) { int f = forwardMostMove;
4234 fprintf(debugFP, "parseboard %d, castling = %d %d %d %d %d %d\n", f,
4235 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
4236 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
4238 fprintf(debugFP, "accepted move %s from ICS, parse it.\n", move_str);
4239 fprintf(debugFP, "moveNum = %d\n", moveNum);
4240 fprintf(debugFP, "board = %d-%d x %d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT);
4241 setbuf(debugFP, NULL);
4243 if (moveNum <= backwardMostMove) {
4244 /* We don't know what the board looked like before
4246 strcpy(parseList[moveNum - 1], move_str);
4247 strcat(parseList[moveNum - 1], " ");
4248 strcat(parseList[moveNum - 1], elapsed_time);
4249 moveList[moveNum - 1][0] = NULLCHAR;
4250 } else if (strcmp(move_str, "none") == 0) {
4251 // [HGM] long SAN: swapped order; test for 'none' before parsing move
4252 /* Again, we don't know what the board looked like;
4253 this is really the start of the game. */
4254 parseList[moveNum - 1][0] = NULLCHAR;
4255 moveList[moveNum - 1][0] = NULLCHAR;
4256 backwardMostMove = moveNum;
4257 startedFromSetupPosition = TRUE;
4258 fromX = fromY = toX = toY = -1;
4260 // [HGM] long SAN: if legality-testing is off, disambiguation might not work or give wrong move.
4261 // So we parse the long-algebraic move string in stead of the SAN move
4262 int valid; char buf[MSG_SIZ], *prom;
4264 // str looks something like "Q/a1-a2"; kill the slash
4266 sprintf(buf, "%c%s", str[0], str+2);
4267 else strcpy(buf, str); // might be castling
4268 if((prom = strstr(move_str, "=")) && !strstr(buf, "="))
4269 strcat(buf, prom); // long move lacks promo specification!
4270 if(!appData.testLegality && move_str[1] != '@') { // drops never ambiguous (parser chokes on long form!)
4271 if(appData.debugMode)
4272 fprintf(debugFP, "replaced ICS move '%s' by '%s'\n", move_str, buf);
4273 strcpy(move_str, buf);
4275 valid = ParseOneMove(move_str, moveNum - 1, &moveType,
4276 &fromX, &fromY, &toX, &toY, &promoChar)
4277 || ParseOneMove(buf, moveNum - 1, &moveType,
4278 &fromX, &fromY, &toX, &toY, &promoChar);
4279 // end of long SAN patch
4281 (void) CoordsToAlgebraic(boards[moveNum - 1],
4282 PosFlags(moveNum - 1),
4283 fromY, fromX, toY, toX, promoChar,
4284 parseList[moveNum-1]);
4285 switch (MateTest(boards[moveNum], PosFlags(moveNum)) ) {
4291 if(gameInfo.variant != VariantShogi)
4292 strcat(parseList[moveNum - 1], "+");
4295 case MT_STAINMATE: // [HGM] xq: for notation stalemate that wins counts as checkmate
4296 strcat(parseList[moveNum - 1], "#");
4299 strcat(parseList[moveNum - 1], " ");
4300 strcat(parseList[moveNum - 1], elapsed_time);
4301 /* currentMoveString is set as a side-effect of ParseOneMove */
4302 strcpy(moveList[moveNum - 1], currentMoveString);
4303 strcat(moveList[moveNum - 1], "\n");
4305 /* Move from ICS was illegal!? Punt. */
4306 if (appData.debugMode) {
4307 fprintf(debugFP, "Illegal move from ICS '%s'\n", move_str);
4308 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
4310 strcpy(parseList[moveNum - 1], move_str);
4311 strcat(parseList[moveNum - 1], " ");
4312 strcat(parseList[moveNum - 1], elapsed_time);
4313 moveList[moveNum - 1][0] = NULLCHAR;
4314 fromX = fromY = toX = toY = -1;
4317 if (appData.debugMode) {
4318 fprintf(debugFP, "Move parsed to '%s'\n", parseList[moveNum - 1]);
4319 setbuf(debugFP, NULL);
4323 /* Send move to chess program (BEFORE animating it). */
4324 if (appData.zippyPlay && !newGame && newMove &&
4325 (!appData.getMoveList || backwardMostMove == 0) && first.initDone) {
4327 if ((gameMode == IcsPlayingWhite && WhiteOnMove(moveNum)) ||
4328 (gameMode == IcsPlayingBlack && !WhiteOnMove(moveNum))) {
4329 if (moveList[moveNum - 1][0] == NULLCHAR) {
4330 sprintf(str, _("Couldn't parse move \"%s\" from ICS"),
4332 DisplayError(str, 0);
4334 if (first.sendTime) {
4335 SendTimeRemaining(&first, gameMode == IcsPlayingWhite);
4337 bookHit = SendMoveToBookUser(moveNum - 1, &first, FALSE); // [HGM] book
4338 if (firstMove && !bookHit) {
4340 if (first.useColors) {
4341 SendToProgram(gameMode == IcsPlayingWhite ?
4343 "black\ngo\n", &first);
4345 SendToProgram("go\n", &first);
4347 first.maybeThinking = TRUE;
4350 } else if (gameMode == IcsObserving || gameMode == IcsExamining) {
4351 if (moveList[moveNum - 1][0] == NULLCHAR) {
4352 sprintf(str, _("Couldn't parse move \"%s\" from ICS"), move_str);
4353 DisplayError(str, 0);
4355 if(gameInfo.variant == currentlyInitializedVariant) // [HGM] refrain sending moves engine can't understand!
4356 SendMoveToProgram(moveNum - 1, &first);
4363 if (moveNum > 0 && !gotPremove && !appData.noGUI) {
4364 /* If move comes from a remote source, animate it. If it
4365 isn't remote, it will have already been animated. */
4366 if (!pausing && !ics_user_moved && prevMove == moveNum - 1) {
4367 AnimateMove(boards[moveNum - 1], fromX, fromY, toX, toY);
4369 if (!pausing && appData.highlightLastMove) {
4370 SetHighlights(fromX, fromY, toX, toY);
4374 /* Start the clocks */
4375 whiteFlag = blackFlag = FALSE;
4376 appData.clockMode = !(basetime == 0 && increment == 0);
4378 ics_clock_paused = TRUE;
4380 } else if (ticking == 1) {
4381 ics_clock_paused = FALSE;
4383 if (gameMode == IcsIdle ||
4384 relation == RELATION_OBSERVING_STATIC ||
4385 relation == RELATION_EXAMINING ||
4387 DisplayBothClocks();
4391 /* Display opponents and material strengths */
4392 if (gameInfo.variant != VariantBughouse &&
4393 gameInfo.variant != VariantCrazyhouse && !appData.noGUI) {
4394 if (tinyLayout || smallLayout) {
4395 if(gameInfo.variant == VariantNormal)
4396 sprintf(str, "%s(%d) %s(%d) {%d %d}",
4397 gameInfo.white, white_stren, gameInfo.black, black_stren,
4398 basetime, increment);
4400 sprintf(str, "%s(%d) %s(%d) {%d %d w%d}",
4401 gameInfo.white, white_stren, gameInfo.black, black_stren,
4402 basetime, increment, (int) gameInfo.variant);
4404 if(gameInfo.variant == VariantNormal)
4405 sprintf(str, "%s (%d) vs. %s (%d) {%d %d}",
4406 gameInfo.white, white_stren, gameInfo.black, black_stren,
4407 basetime, increment);
4409 sprintf(str, "%s (%d) vs. %s (%d) {%d %d %s}",
4410 gameInfo.white, white_stren, gameInfo.black, black_stren,
4411 basetime, increment, VariantName(gameInfo.variant));
4414 if (appData.debugMode) {
4415 fprintf(debugFP, "Display title '%s, gameInfo.variant = %d'\n", str, gameInfo.variant);
4420 /* Display the board */
4421 if (!pausing && !appData.noGUI) {
4423 if (appData.premove)
4425 ((gameMode == IcsPlayingWhite) && (WhiteOnMove(currentMove))) ||
4426 ((gameMode == IcsPlayingBlack) && (!WhiteOnMove(currentMove))))
4427 ClearPremoveHighlights();
4429 j = seekGraphUp; seekGraphUp = FALSE; // [HGM] seekgraph: when we draw a board, it overwrites the seek graph
4430 if(partnerUp) { flipView = originalFlip; partnerUp = FALSE; j = TRUE; } // [HGM] bughouse: restore view
4431 DrawPosition(j, boards[currentMove]);
4433 DisplayMove(moveNum - 1);
4434 if (appData.ringBellAfterMoves && /*!ics_user_moved*/ // [HGM] use absolute method to recognize own move
4435 !((gameMode == IcsPlayingWhite) && (!WhiteOnMove(moveNum)) ||
4436 (gameMode == IcsPlayingBlack) && (WhiteOnMove(moveNum)) ) ) {
4437 if(newMove) RingBell(); else PlayIcsUnfinishedSound();
4441 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
4443 if(bookHit) { // [HGM] book: simulate book reply
4444 static char bookMove[MSG_SIZ]; // a bit generous?
4446 programStats.nodes = programStats.depth = programStats.time =
4447 programStats.score = programStats.got_only_move = 0;
4448 sprintf(programStats.movelist, "%s (xbook)", bookHit);
4450 strcpy(bookMove, "move ");
4451 strcat(bookMove, bookHit);
4452 HandleMachineMove(bookMove, &first);
4461 if (appData.icsActive && gameMode != IcsIdle && ics_gamenum > 0) {
4462 ics_getting_history = H_REQUESTED;
4463 sprintf(buf, "%smoves %d\n", ics_prefix, ics_gamenum);
4469 AnalysisPeriodicEvent(force)
4472 if (((programStats.ok_to_send == 0 || programStats.line_is_book)
4473 && !force) || !appData.periodicUpdates)
4476 /* Send . command to Crafty to collect stats */
4477 SendToProgram(".\n", &first);
4479 /* Don't send another until we get a response (this makes
4480 us stop sending to old Crafty's which don't understand
4481 the "." command (sending illegal cmds resets node count & time,
4482 which looks bad)) */
4483 programStats.ok_to_send = 0;
4486 void ics_update_width(new_width)
4489 ics_printf("set width %d\n", new_width);
4493 SendMoveToProgram(moveNum, cps)
4495 ChessProgramState *cps;
4499 if (cps->useUsermove) {
4500 SendToProgram("usermove ", cps);
4504 if ((space = strchr(parseList[moveNum], ' ')) != NULL) {
4505 int len = space - parseList[moveNum];
4506 memcpy(buf, parseList[moveNum], len);
4508 buf[len] = NULLCHAR;
4510 sprintf(buf, "%s\n", parseList[moveNum]);
4512 SendToProgram(buf, cps);
4514 if(cps->alphaRank) { /* [HGM] shogi: temporarily convert to shogi coordinates before sending */
4515 AlphaRank(moveList[moveNum], 4);
4516 SendToProgram(moveList[moveNum], cps);
4517 AlphaRank(moveList[moveNum], 4); // and back
4519 /* Added by Tord: Send castle moves in "O-O" in FRC games if required by
4520 * the engine. It would be nice to have a better way to identify castle
4522 if((gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom)
4523 && cps->useOOCastle) {
4524 int fromX = moveList[moveNum][0] - AAA;
4525 int fromY = moveList[moveNum][1] - ONE;
4526 int toX = moveList[moveNum][2] - AAA;
4527 int toY = moveList[moveNum][3] - ONE;
4528 if((boards[moveNum][fromY][fromX] == WhiteKing
4529 && boards[moveNum][toY][toX] == WhiteRook)
4530 || (boards[moveNum][fromY][fromX] == BlackKing
4531 && boards[moveNum][toY][toX] == BlackRook)) {
4532 if(toX > fromX) SendToProgram("O-O\n", cps);
4533 else SendToProgram("O-O-O\n", cps);
4535 else SendToProgram(moveList[moveNum], cps);
4537 else SendToProgram(moveList[moveNum], cps);
4538 /* End of additions by Tord */
4541 /* [HGM] setting up the opening has brought engine in force mode! */
4542 /* Send 'go' if we are in a mode where machine should play. */
4543 if( (moveNum == 0 && setboardSpoiledMachineBlack && cps == &first) &&
4544 (gameMode == TwoMachinesPlay ||
4546 gameMode == IcsPlayingBlack || gameMode == IcsPlayingWhite ||
4548 gameMode == MachinePlaysBlack || gameMode == MachinePlaysWhite) ) {
4549 SendToProgram("go\n", cps);
4550 if (appData.debugMode) {
4551 fprintf(debugFP, "(extra)\n");
4554 setboardSpoiledMachineBlack = 0;
4558 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar)
4560 int fromX, fromY, toX, toY;
4563 char user_move[MSG_SIZ];
4567 sprintf(user_move, _("say Internal error; bad moveType %d (%d,%d-%d,%d)"),
4568 (int)moveType, fromX, fromY, toX, toY);
4569 DisplayError(user_move + strlen("say "), 0);
4571 case WhiteKingSideCastle:
4572 case BlackKingSideCastle:
4573 case WhiteQueenSideCastleWild:
4574 case BlackQueenSideCastleWild:
4576 case WhiteHSideCastleFR:
4577 case BlackHSideCastleFR:
4579 sprintf(user_move, "o-o\n");
4581 case WhiteQueenSideCastle:
4582 case BlackQueenSideCastle:
4583 case WhiteKingSideCastleWild:
4584 case BlackKingSideCastleWild:
4586 case WhiteASideCastleFR:
4587 case BlackASideCastleFR:
4589 sprintf(user_move, "o-o-o\n");
4591 case WhiteNonPromotion:
4592 case BlackNonPromotion:
4593 sprintf(user_move, "%c%c%c%c=\n", AAA + fromX, ONE + fromY, AAA + toX, ONE + toY);
4595 case WhitePromotion:
4596 case BlackPromotion:
4597 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
4598 sprintf(user_move, "%c%c%c%c=%c\n",
4599 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
4600 PieceToChar(WhiteFerz));
4601 else if(gameInfo.variant == VariantGreat)
4602 sprintf(user_move, "%c%c%c%c=%c\n",
4603 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
4604 PieceToChar(WhiteMan));
4606 sprintf(user_move, "%c%c%c%c=%c\n",
4607 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
4612 sprintf(user_move, "%c@%c%c\n",
4613 ToUpper(PieceToChar((ChessSquare) fromX)),
4614 AAA + toX, ONE + toY);
4617 case WhiteCapturesEnPassant:
4618 case BlackCapturesEnPassant:
4619 case IllegalMove: /* could be a variant we don't quite understand */
4620 sprintf(user_move, "%c%c%c%c\n",
4621 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY);
4624 SendToICS(user_move);
4625 if(appData.keepAlive) // [HGM] alive: schedule sending of dummy 'date' command
4626 ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
4631 { // [HGM] upload: send entire stored game to ICS as long-algebraic moves.
4632 int i, last = forwardMostMove; // make sure ICS reply cannot pre-empt us by clearing fmm
4633 static char *castlingStrings[4] = { "none", "kside", "qside", "both" };
4634 if(gameMode == IcsObserving || gameMode == IcsPlayingBlack || gameMode == IcsPlayingWhite) {
4635 DisplayError("You cannot do this while you are playing or observing", 0);
4638 if(gameMode != IcsExamining) { // is this ever not the case?
4639 char buf[MSG_SIZ], *p, *fen, command[MSG_SIZ], bsetup = 0;
4641 if(ics_type == ICS_ICC) { // on ICC match ourselves in applicable variant
4642 sprintf(command, "match %s", ics_handle);
4643 } else { // on FICS we must first go to general examine mode
4644 strcpy(command, "examine\nbsetup"); // and specify variant within it with bsetups
4646 if(gameInfo.variant != VariantNormal) {
4647 // try figure out wild number, as xboard names are not always valid on ICS
4648 for(i=1; i<=36; i++) {
4649 sprintf(buf, "wild/%d", i);
4650 if(StringToVariant(buf) == gameInfo.variant) break;
4652 if(i<=36 && ics_type == ICS_ICC) sprintf(buf, "%s w%d\n", command, i);
4653 else if(i == 22) sprintf(buf, "%s fr\n", command);
4654 else sprintf(buf, "%s %s\n", command, VariantName(gameInfo.variant));
4655 } else sprintf(buf, "%s\n", ics_type == ICS_ICC ? command : "examine\n"); // match yourself or examine
4656 SendToICS(ics_prefix);
4658 if(startedFromSetupPosition || backwardMostMove != 0) {
4659 fen = PositionToFEN(backwardMostMove, NULL);
4660 if(ics_type == ICS_ICC) { // on ICC we can simply send a complete FEN to set everything
4661 sprintf(buf, "loadfen %s\n", fen);
4663 } else { // FICS: everything has to set by separate bsetup commands
4664 p = strchr(fen, ' '); p[0] = NULLCHAR; // cut after board
4665 sprintf(buf, "bsetup fen %s\n", fen);
4667 if(!WhiteOnMove(backwardMostMove)) {
4668 SendToICS("bsetup tomove black\n");
4670 i = (strchr(p+3, 'K') != NULL) + 2*(strchr(p+3, 'Q') != NULL);
4671 sprintf(buf, "bsetup wcastle %s\n", castlingStrings[i]);
4673 i = (strchr(p+3, 'k') != NULL) + 2*(strchr(p+3, 'q') != NULL);
4674 sprintf(buf, "bsetup bcastle %s\n", castlingStrings[i]);
4676 i = boards[backwardMostMove][EP_STATUS];
4677 if(i >= 0) { // set e.p.
4678 sprintf(buf, "bsetup eppos %c\n", i+AAA);
4684 if(bsetup || ics_type != ICS_ICC && gameInfo.variant != VariantNormal)
4685 SendToICS("bsetup done\n"); // switch to normal examining.
4687 for(i = backwardMostMove; i<last; i++) {
4689 sprintf(buf, "%s\n", parseList[i]);
4692 SendToICS(ics_prefix);
4693 SendToICS(ics_type == ICS_ICC ? "tag result Game in progress\n" : "commit\n");
4697 CoordsToComputerAlgebraic(rf, ff, rt, ft, promoChar, move)
4702 if (rf == DROP_RANK) {
4703 sprintf(move, "%c@%c%c\n",
4704 ToUpper(PieceToChar((ChessSquare) ff)), AAA + ft, ONE + rt);
4706 if (promoChar == 'x' || promoChar == NULLCHAR) {
4707 sprintf(move, "%c%c%c%c\n",
4708 AAA + ff, ONE + rf, AAA + ft, ONE + rt);
4710 sprintf(move, "%c%c%c%c%c\n",
4711 AAA + ff, ONE + rf, AAA + ft, ONE + rt, promoChar);
4717 ProcessICSInitScript(f)
4722 while (fgets(buf, MSG_SIZ, f)) {
4723 SendToICSDelayed(buf,(long)appData.msLoginDelay);
4730 /* [HGM] Shogi move preprocessor: swap digits for letters, vice versa */
4732 AlphaRank(char *move, int n)
4734 // char *p = move, c; int x, y;
4736 if (appData.debugMode) {
4737 fprintf(debugFP, "alphaRank(%s,%d)\n", move, n);
4741 move[2]>='0' && move[2]<='9' &&
4742 move[3]>='a' && move[3]<='x' ) {
4744 move[2] = BOARD_RGHT -1 - (move[2]-'1') + AAA;
4745 move[3] = BOARD_HEIGHT-1 - (move[3]-'a') + ONE;
4747 if(move[0]>='0' && move[0]<='9' &&
4748 move[1]>='a' && move[1]<='x' &&
4749 move[2]>='0' && move[2]<='9' &&
4750 move[3]>='a' && move[3]<='x' ) {
4751 /* input move, Shogi -> normal */
4752 move[0] = BOARD_RGHT -1 - (move[0]-'1') + AAA;
4753 move[1] = BOARD_HEIGHT-1 - (move[1]-'a') + ONE;
4754 move[2] = BOARD_RGHT -1 - (move[2]-'1') + AAA;
4755 move[3] = BOARD_HEIGHT-1 - (move[3]-'a') + ONE;
4758 move[3]>='0' && move[3]<='9' &&
4759 move[2]>='a' && move[2]<='x' ) {
4761 move[2] = BOARD_RGHT - 1 - (move[2]-AAA) + '1';
4762 move[3] = BOARD_HEIGHT-1 - (move[3]-ONE) + 'a';
4765 move[0]>='a' && move[0]<='x' &&
4766 move[3]>='0' && move[3]<='9' &&
4767 move[2]>='a' && move[2]<='x' ) {
4768 /* output move, normal -> Shogi */
4769 move[0] = BOARD_RGHT - 1 - (move[0]-AAA) + '1';
4770 move[1] = BOARD_HEIGHT-1 - (move[1]-ONE) + 'a';
4771 move[2] = BOARD_RGHT - 1 - (move[2]-AAA) + '1';
4772 move[3] = BOARD_HEIGHT-1 - (move[3]-ONE) + 'a';
4773 if(move[4] == PieceToChar(BlackQueen)) move[4] = '+';
4775 if (appData.debugMode) {
4776 fprintf(debugFP, " out = '%s'\n", move);
4780 char yy_textstr[8000];
4782 /* Parser for moves from gnuchess, ICS, or user typein box */
4784 ParseOneMove(move, moveNum, moveType, fromX, fromY, toX, toY, promoChar)
4787 ChessMove *moveType;
4788 int *fromX, *fromY, *toX, *toY;
4791 if (appData.debugMode) {
4792 fprintf(debugFP, "move to parse: %s\n", move);
4794 *moveType = yylexstr(moveNum, move, yy_textstr, sizeof yy_textstr);
4796 switch (*moveType) {
4797 case WhitePromotion:
4798 case BlackPromotion:
4799 case WhiteNonPromotion:
4800 case BlackNonPromotion:
4802 case WhiteCapturesEnPassant:
4803 case BlackCapturesEnPassant:
4804 case WhiteKingSideCastle:
4805 case WhiteQueenSideCastle:
4806 case BlackKingSideCastle:
4807 case BlackQueenSideCastle:
4808 case WhiteKingSideCastleWild:
4809 case WhiteQueenSideCastleWild:
4810 case BlackKingSideCastleWild:
4811 case BlackQueenSideCastleWild:
4812 /* Code added by Tord: */
4813 case WhiteHSideCastleFR:
4814 case WhiteASideCastleFR:
4815 case BlackHSideCastleFR:
4816 case BlackASideCastleFR:
4817 /* End of code added by Tord */
4818 case IllegalMove: /* bug or odd chess variant */
4819 *fromX = currentMoveString[0] - AAA;
4820 *fromY = currentMoveString[1] - ONE;
4821 *toX = currentMoveString[2] - AAA;
4822 *toY = currentMoveString[3] - ONE;
4823 *promoChar = currentMoveString[4];
4824 if (*fromX < BOARD_LEFT || *fromX >= BOARD_RGHT || *fromY < 0 || *fromY >= BOARD_HEIGHT ||
4825 *toX < BOARD_LEFT || *toX >= BOARD_RGHT || *toY < 0 || *toY >= BOARD_HEIGHT) {
4826 if (appData.debugMode) {
4827 fprintf(debugFP, "Off-board move (%d,%d)-(%d,%d)%c, type = %d\n", *fromX, *fromY, *toX, *toY, *promoChar, *moveType);
4829 *fromX = *fromY = *toX = *toY = 0;
4832 if (appData.testLegality) {
4833 return (*moveType != IllegalMove);
4835 return !(*fromX == *toX && *fromY == *toY) && boards[moveNum][*fromY][*fromX] != EmptySquare &&
4836 WhiteOnMove(moveNum) == (boards[moveNum][*fromY][*fromX] < BlackPawn);
4841 *fromX = *moveType == WhiteDrop ?
4842 (int) CharToPiece(ToUpper(currentMoveString[0])) :
4843 (int) CharToPiece(ToLower(currentMoveString[0]));
4845 *toX = currentMoveString[2] - AAA;
4846 *toY = currentMoveString[3] - ONE;
4847 *promoChar = NULLCHAR;
4851 case ImpossibleMove:
4852 case (ChessMove) 0: /* end of file */
4861 if (appData.debugMode) {
4862 fprintf(debugFP, "Impossible move %s, type = %d\n", currentMoveString, *moveType);
4865 *fromX = *fromY = *toX = *toY = 0;
4866 *promoChar = NULLCHAR;
4873 ParsePV(char *pv, Boolean storeComments)
4874 { // Parse a string of PV moves, and append to current game, behind forwardMostMove
4875 int fromX, fromY, toX, toY; char promoChar;
4880 endPV = forwardMostMove;
4882 while(*pv == ' ' || *pv == '\n' || *pv == '\t') pv++; // must still read away whitespace
4883 if(nr == 0 && !storeComments && *pv == '(') pv++; // first (ponder) move can be in parentheses
4884 valid = ParseOneMove(pv, endPV, &moveType, &fromX, &fromY, &toX, &toY, &promoChar);
4885 if(appData.debugMode){
4886 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);
4888 if(!valid && nr == 0 &&
4889 ParseOneMove(pv, endPV-1, &moveType, &fromX, &fromY, &toX, &toY, &promoChar)){
4890 nr++; moveType = Comment; // First move has been played; kludge to make sure we continue
4891 // Hande case where played move is different from leading PV move
4892 CopyBoard(boards[endPV+1], boards[endPV-1]); // tentatively unplay last game move
4893 CopyBoard(boards[endPV+2], boards[endPV-1]); // and play first move of PV
4894 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[endPV+2]);
4895 if(!CompareBoards(boards[endPV], boards[endPV+2])) {
4896 endPV += 2; // if position different, keep this
4897 moveList[endPV-1][0] = fromX + AAA;
4898 moveList[endPV-1][1] = fromY + ONE;
4899 moveList[endPV-1][2] = toX + AAA;
4900 moveList[endPV-1][3] = toY + ONE;
4901 parseList[endPV-1][0] = NULLCHAR;
4902 strcpy(moveList[endPV-2], "_0_0"); // suppress premove highlight on takeback move
4905 pv = strstr(pv, yy_textstr) + strlen(yy_textstr); // skip what we parsed
4906 if(nr == 0 && !storeComments && *pv == ')') pv++; // closing parenthesis of ponder move;
4907 if(moveType == Comment && storeComments) AppendComment(endPV, yy_textstr, FALSE);
4908 if(moveType == Comment || moveType == NAG || moveType == ElapsedTime) {
4909 valid++; // allow comments in PV
4913 if(endPV+1 > framePtr) break; // no space, truncate
4916 CopyBoard(boards[endPV], boards[endPV-1]);
4917 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[endPV]);
4918 moveList[endPV-1][0] = fromX + AAA;
4919 moveList[endPV-1][1] = fromY + ONE;
4920 moveList[endPV-1][2] = toX + AAA;
4921 moveList[endPV-1][3] = toY + ONE;
4923 CoordsToAlgebraic(boards[endPV - 1],
4924 PosFlags(endPV - 1),
4925 fromY, fromX, toY, toX, promoChar,
4926 parseList[endPV - 1]);
4928 parseList[endPV-1][0] = NULLCHAR;
4930 currentMove = endPV;
4931 if(currentMove == forwardMostMove) ClearPremoveHighlights(); else
4932 SetPremoveHighlights(moveList[currentMove-1][0]-AAA, moveList[currentMove-1][1]-ONE,
4933 moveList[currentMove-1][2]-AAA, moveList[currentMove-1][3]-ONE);
4934 DrawPosition(TRUE, boards[currentMove]);
4937 static int lastX, lastY;
4940 LoadMultiPV(int x, int y, char *buf, int index, int *start, int *end)
4945 if(index < 0 || index >= strlen(buf)) return FALSE; // sanity
4946 lastX = x; lastY = y;
4947 while(index > 0 && buf[index-1] != '\n') index--; // beginning of line
4949 while(buf[index] != '\n') if(buf[index++] == '\t') startPV = index;
4950 if(index == startPV && (p = StrCaseStr(buf+index, "PV="))) startPV = p - buf + 3;
4952 do{ while(buf[index] && buf[index] != '\n') index++;
4953 } while(buf[index] == '\n' && buf[index+1] == '\\' && buf[index+2] == ' ' && index++); // join kibitzed PV continuation line
4955 ParsePV(buf+startPV, FALSE);
4956 *start = startPV; *end = index-1;
4961 LoadPV(int x, int y)
4962 { // called on right mouse click to load PV
4963 int which = gameMode == TwoMachinesPlay && (WhiteOnMove(forwardMostMove) == (second.twoMachinesColor[0] == 'w'));
4964 lastX = x; lastY = y;
4965 ParsePV(lastPV[which], FALSE); // load the PV of the thinking engine in the boards array.
4972 if(endPV < 0) return;
4974 currentMove = forwardMostMove;
4975 ClearPremoveHighlights();
4976 DrawPosition(TRUE, boards[currentMove]);
4980 MovePV(int x, int y, int h)
4981 { // step through PV based on mouse coordinates (called on mouse move)
4982 int margin = h>>3, step = 0;
4984 if(endPV < 0) return;
4985 // we must somehow check if right button is still down (might be released off board!)
4986 if(y < margin && (abs(x - lastX) > 6 || abs(y - lastY) > 6)) step = 1; else
4987 if(y > h - margin && (abs(x - lastX) > 6 || abs(y - lastY) > 6)) step = -1; else
4988 if( y > lastY + 6 ) step = -1; else if(y < lastY - 6) step = 1;
4990 lastX = x; lastY = y;
4991 if(currentMove + step > endPV || currentMove + step < forwardMostMove) step = 0;
4992 currentMove += step;
4993 if(currentMove == forwardMostMove) ClearPremoveHighlights(); else
4994 SetPremoveHighlights(moveList[currentMove-1][0]-AAA, moveList[currentMove-1][1]-ONE,
4995 moveList[currentMove-1][2]-AAA, moveList[currentMove-1][3]-ONE);
4996 DrawPosition(FALSE, boards[currentMove]);
5000 // [HGM] shuffle: a general way to suffle opening setups, applicable to arbitrary variants.
5001 // All positions will have equal probability, but the current method will not provide a unique
5002 // numbering scheme for arrays that contain 3 or more pieces of the same kind.
5008 int piecesLeft[(int)BlackPawn];
5009 int seed, nrOfShuffles;
5011 void GetPositionNumber()
5012 { // sets global variable seed
5015 seed = appData.defaultFrcPosition;
5016 if(seed < 0) { // randomize based on time for negative FRC position numbers
5017 for(i=0; i<50; i++) seed += random();
5018 seed = random() ^ random() >> 8 ^ random() << 8;
5019 if(seed<0) seed = -seed;
5023 int put(Board board, int pieceType, int rank, int n, int shade)
5024 // put the piece on the (n-1)-th empty squares of the given shade
5028 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) {
5029 if( (((i-BOARD_LEFT)&1)+1) & shade && board[rank][i] == EmptySquare && n-- == 0) {
5030 board[rank][i] = (ChessSquare) pieceType;
5031 squaresLeft[((i-BOARD_LEFT)&1) + 1]--;
5033 piecesLeft[pieceType]--;
5041 void AddOnePiece(Board board, int pieceType, int rank, int shade)
5042 // calculate where the next piece goes, (any empty square), and put it there
5046 i = seed % squaresLeft[shade];
5047 nrOfShuffles *= squaresLeft[shade];
5048 seed /= squaresLeft[shade];
5049 put(board, pieceType, rank, i, shade);
5052 void AddTwoPieces(Board board, int pieceType, int rank)
5053 // calculate where the next 2 identical pieces go, (any empty square), and put it there
5055 int i, n=squaresLeft[ANY], j=n-1, k;
5057 k = n*(n-1)/2; // nr of possibilities, not counting permutations
5058 i = seed % k; // pick one
5061 while(i >= j) i -= j--;
5062 j = n - 1 - j; i += j;
5063 put(board, pieceType, rank, j, ANY);
5064 put(board, pieceType, rank, i, ANY);
5067 void SetUpShuffle(Board board, int number)
5071 GetPositionNumber(); nrOfShuffles = 1;
5073 squaresLeft[DARK] = (BOARD_RGHT - BOARD_LEFT + 1)/2;
5074 squaresLeft[ANY] = BOARD_RGHT - BOARD_LEFT;
5075 squaresLeft[LITE] = squaresLeft[ANY] - squaresLeft[DARK];
5077 for(p = 0; p<=(int)WhiteKing; p++) piecesLeft[p] = 0;
5079 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // count pieces and clear board
5080 p = (int) board[0][i];
5081 if(p < (int) BlackPawn) piecesLeft[p] ++;
5082 board[0][i] = EmptySquare;
5085 if(PosFlags(0) & F_ALL_CASTLE_OK) {
5086 // shuffles restricted to allow normal castling put KRR first
5087 if(piecesLeft[(int)WhiteKing]) // King goes rightish of middle
5088 put(board, WhiteKing, 0, (gameInfo.boardWidth+1)/2, ANY);
5089 else if(piecesLeft[(int)WhiteUnicorn]) // in Knightmate Unicorn castles
5090 put(board, WhiteUnicorn, 0, (gameInfo.boardWidth+1)/2, ANY);
5091 if(piecesLeft[(int)WhiteRook]) // First supply a Rook for K-side castling
5092 put(board, WhiteRook, 0, gameInfo.boardWidth-2, ANY);
5093 if(piecesLeft[(int)WhiteRook]) // Then supply a Rook for Q-side castling
5094 put(board, WhiteRook, 0, 0, ANY);
5095 // in variants with super-numerary Kings and Rooks, we leave these for the shuffle
5098 if(((BOARD_RGHT-BOARD_LEFT) & 1) == 0)
5099 // only for even boards make effort to put pairs of colorbound pieces on opposite colors
5100 for(p = (int) WhiteKing; p > (int) WhitePawn; p--) {
5101 if(p != (int) WhiteBishop && p != (int) WhiteFerz && p != (int) WhiteAlfil) continue;
5102 while(piecesLeft[p] >= 2) {
5103 AddOnePiece(board, p, 0, LITE);
5104 AddOnePiece(board, p, 0, DARK);
5106 // Odd color-bound pieces are shuffled with the rest (to not run out of paired squares)
5109 for(p = (int) WhiteKing - 2; p > (int) WhitePawn; p--) {
5110 // Remaining pieces (non-colorbound, or odd color bound) can be put anywhere
5111 // but we leave King and Rooks for last, to possibly obey FRC restriction
5112 if(p == (int)WhiteRook) continue;
5113 while(piecesLeft[p] >= 2) AddTwoPieces(board, p, 0); // add in pairs, for not counting permutations
5114 if(piecesLeft[p]) AddOnePiece(board, p, 0, ANY); // add the odd piece
5117 // now everything is placed, except perhaps King (Unicorn) and Rooks
5119 if(PosFlags(0) & F_FRC_TYPE_CASTLING) {
5120 // Last King gets castling rights
5121 while(piecesLeft[(int)WhiteUnicorn]) {
5122 i = put(board, WhiteUnicorn, 0, piecesLeft[(int)WhiteRook]/2, ANY);
5123 initialRights[2] = initialRights[5] = board[CASTLING][2] = board[CASTLING][5] = i;
5126 while(piecesLeft[(int)WhiteKing]) {
5127 i = put(board, WhiteKing, 0, piecesLeft[(int)WhiteRook]/2, ANY);
5128 initialRights[2] = initialRights[5] = board[CASTLING][2] = board[CASTLING][5] = i;
5133 while(piecesLeft[(int)WhiteKing]) AddOnePiece(board, WhiteKing, 0, ANY);
5134 while(piecesLeft[(int)WhiteUnicorn]) AddOnePiece(board, WhiteUnicorn, 0, ANY);
5137 // Only Rooks can be left; simply place them all
5138 while(piecesLeft[(int)WhiteRook]) {
5139 i = put(board, WhiteRook, 0, 0, ANY);
5140 if(PosFlags(0) & F_FRC_TYPE_CASTLING) { // first and last Rook get FRC castling rights
5143 initialRights[1] = initialRights[4] = board[CASTLING][1] = board[CASTLING][4] = i;
5145 initialRights[0] = initialRights[3] = board[CASTLING][0] = board[CASTLING][3] = i;
5148 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // copy black from white
5149 board[BOARD_HEIGHT-1][i] = (int) board[0][i] < BlackPawn ? WHITE_TO_BLACK board[0][i] : EmptySquare;
5152 if(number >= 0) appData.defaultFrcPosition %= nrOfShuffles; // normalize
5155 int SetCharTable( char *table, const char * map )
5156 /* [HGM] moved here from winboard.c because of its general usefulness */
5157 /* Basically a safe strcpy that uses the last character as King */
5159 int result = FALSE; int NrPieces;
5161 if( map != NULL && (NrPieces=strlen(map)) <= (int) EmptySquare
5162 && NrPieces >= 12 && !(NrPieces&1)) {
5163 int i; /* [HGM] Accept even length from 12 to 34 */
5165 for( i=0; i<(int) EmptySquare; i++ ) table[i] = '.';
5166 for( i=0; i<NrPieces/2-1; i++ ) {
5168 table[i + (int)BlackPawn - (int) WhitePawn] = map[i+NrPieces/2];
5170 table[(int) WhiteKing] = map[NrPieces/2-1];
5171 table[(int) BlackKing] = map[NrPieces-1];
5179 void Prelude(Board board)
5180 { // [HGM] superchess: random selection of exo-pieces
5181 int i, j, k; ChessSquare p;
5182 static ChessSquare exoPieces[4] = { WhiteAngel, WhiteMarshall, WhiteSilver, WhiteLance };
5184 GetPositionNumber(); // use FRC position number
5186 if(appData.pieceToCharTable != NULL) { // select pieces to participate from given char table
5187 SetCharTable(pieceToChar, appData.pieceToCharTable);
5188 for(i=(int)WhiteQueen+1, j=0; i<(int)WhiteKing && j<4; i++)
5189 if(PieceToChar((ChessSquare)i) != '.') exoPieces[j++] = (ChessSquare) i;
5192 j = seed%4; seed /= 4;
5193 p = board[0][BOARD_LEFT+j]; board[0][BOARD_LEFT+j] = EmptySquare; k = PieceToNumber(p);
5194 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5195 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5196 j = seed%3 + (seed%3 >= j); seed /= 3;
5197 p = board[0][BOARD_LEFT+j]; board[0][BOARD_LEFT+j] = EmptySquare; k = PieceToNumber(p);
5198 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5199 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5200 j = seed%3; seed /= 3;
5201 p = board[0][BOARD_LEFT+j+5]; board[0][BOARD_LEFT+j+5] = EmptySquare; k = PieceToNumber(p);
5202 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5203 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5204 j = seed%2 + (seed%2 >= j); seed /= 2;
5205 p = board[0][BOARD_LEFT+j+5]; board[0][BOARD_LEFT+j+5] = EmptySquare; k = PieceToNumber(p);
5206 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5207 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5208 j = seed%4; seed /= 4; put(board, exoPieces[3], 0, j, ANY);
5209 j = seed%3; seed /= 3; put(board, exoPieces[2], 0, j, ANY);
5210 j = seed%2; seed /= 2; put(board, exoPieces[1], 0, j, ANY);
5211 put(board, exoPieces[0], 0, 0, ANY);
5212 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) board[BOARD_HEIGHT-1][i] = WHITE_TO_BLACK board[0][i];
5216 InitPosition(redraw)
5219 ChessSquare (* pieces)[BOARD_FILES];
5220 int i, j, pawnRow, overrule,
5221 oldx = gameInfo.boardWidth,
5222 oldy = gameInfo.boardHeight,
5223 oldh = gameInfo.holdingsWidth,
5224 oldv = gameInfo.variant;
5226 if(appData.icsActive) shuffleOpenings = FALSE; // [HGM] shuffle: in ICS mode, only shuffle on ICS request
5228 /* [AS] Initialize pv info list [HGM] and game status */
5230 for( i=0; i<=framePtr; i++ ) { // [HGM] vari: spare saved variations
5231 pvInfoList[i].depth = 0;
5232 boards[i][EP_STATUS] = EP_NONE;
5233 for( j=0; j<BOARD_FILES-2; j++ ) boards[i][CASTLING][j] = NoRights;
5236 initialRulePlies = 0; /* 50-move counter start */
5238 castlingRank[0] = castlingRank[1] = castlingRank[2] = 0;
5239 castlingRank[3] = castlingRank[4] = castlingRank[5] = BOARD_HEIGHT-1;
5243 /* [HGM] logic here is completely changed. In stead of full positions */
5244 /* the initialized data only consist of the two backranks. The switch */
5245 /* selects which one we will use, which is than copied to the Board */
5246 /* initialPosition, which for the rest is initialized by Pawns and */
5247 /* empty squares. This initial position is then copied to boards[0], */
5248 /* possibly after shuffling, so that it remains available. */
5250 gameInfo.holdingsWidth = 0; /* default board sizes */
5251 gameInfo.boardWidth = 8;
5252 gameInfo.boardHeight = 8;
5253 gameInfo.holdingsSize = 0;
5254 nrCastlingRights = -1; /* [HGM] Kludge to indicate default should be used */
5255 for(i=0; i<BOARD_FILES-2; i++)
5256 initialPosition[CASTLING][i] = initialRights[i] = NoRights; /* but no rights yet */
5257 initialPosition[EP_STATUS] = EP_NONE;
5258 SetCharTable(pieceToChar, "PNBRQ...........Kpnbrq...........k");
5259 if(startVariant == gameInfo.variant) // [HGM] nicks: enable nicknames in original variant
5260 SetCharTable(pieceNickName, appData.pieceNickNames);
5261 else SetCharTable(pieceNickName, "............");
5263 switch (gameInfo.variant) {
5264 case VariantFischeRandom:
5265 shuffleOpenings = TRUE;
5269 case VariantShatranj:
5270 pieces = ShatranjArray;
5271 nrCastlingRights = 0;
5272 SetCharTable(pieceToChar, "PN.R.QB...Kpn.r.qb...k");
5275 pieces = makrukArray;
5276 nrCastlingRights = 0;
5277 startedFromSetupPosition = TRUE;
5278 SetCharTable(pieceToChar, "PN.R.M....SKpn.r.m....sk");
5280 case VariantTwoKings:
5281 pieces = twoKingsArray;
5283 case VariantCapaRandom:
5284 shuffleOpenings = TRUE;
5285 case VariantCapablanca:
5286 pieces = CapablancaArray;
5287 gameInfo.boardWidth = 10;
5288 SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
5291 pieces = GothicArray;
5292 gameInfo.boardWidth = 10;
5293 SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
5296 pieces = JanusArray;
5297 gameInfo.boardWidth = 10;
5298 SetCharTable(pieceToChar, "PNBRQ..JKpnbrq..jk");
5299 nrCastlingRights = 6;
5300 initialPosition[CASTLING][0] = initialRights[0] = BOARD_RGHT-1;
5301 initialPosition[CASTLING][1] = initialRights[1] = BOARD_LEFT;
5302 initialPosition[CASTLING][2] = initialRights[2] =(BOARD_WIDTH-1)>>1;
5303 initialPosition[CASTLING][3] = initialRights[3] = BOARD_RGHT-1;
5304 initialPosition[CASTLING][4] = initialRights[4] = BOARD_LEFT;
5305 initialPosition[CASTLING][5] = initialRights[5] =(BOARD_WIDTH-1)>>1;
5308 pieces = FalconArray;
5309 gameInfo.boardWidth = 10;
5310 SetCharTable(pieceToChar, "PNBRQ.............FKpnbrq.............fk");
5312 case VariantXiangqi:
5313 pieces = XiangqiArray;
5314 gameInfo.boardWidth = 9;
5315 gameInfo.boardHeight = 10;
5316 nrCastlingRights = 0;
5317 SetCharTable(pieceToChar, "PH.R.AE..K.C.ph.r.ae..k.c.");
5320 pieces = ShogiArray;
5321 gameInfo.boardWidth = 9;
5322 gameInfo.boardHeight = 9;
5323 gameInfo.holdingsSize = 7;
5324 nrCastlingRights = 0;
5325 SetCharTable(pieceToChar, "PNBRLS...G.++++++Kpnbrls...g.++++++k");
5327 case VariantCourier:
5328 pieces = CourierArray;
5329 gameInfo.boardWidth = 12;
5330 nrCastlingRights = 0;
5331 SetCharTable(pieceToChar, "PNBR.FE..WMKpnbr.fe..wmk");
5333 case VariantKnightmate:
5334 pieces = KnightmateArray;
5335 SetCharTable(pieceToChar, "P.BRQ.....M.........K.p.brq.....m.........k.");
5338 pieces = fairyArray;
5339 SetCharTable(pieceToChar, "PNBRQFEACWMOHIJGDVLSUKpnbrqfeacwmohijgdvlsuk");
5342 pieces = GreatArray;
5343 gameInfo.boardWidth = 10;
5344 SetCharTable(pieceToChar, "PN....E...S..HWGMKpn....e...s..hwgmk");
5345 gameInfo.holdingsSize = 8;
5349 SetCharTable(pieceToChar, "PNBRQ..SE.......V.AKpnbrq..se.......v.ak");
5350 gameInfo.holdingsSize = 8;
5351 startedFromSetupPosition = TRUE;
5353 case VariantCrazyhouse:
5354 case VariantBughouse:
5356 SetCharTable(pieceToChar, "PNBRQ.......~~~~Kpnbrq.......~~~~k");
5357 gameInfo.holdingsSize = 5;
5359 case VariantWildCastle:
5361 /* !!?shuffle with kings guaranteed to be on d or e file */
5362 shuffleOpenings = 1;
5364 case VariantNoCastle:
5366 nrCastlingRights = 0;
5367 /* !!?unconstrained back-rank shuffle */
5368 shuffleOpenings = 1;
5373 if(appData.NrFiles >= 0) {
5374 if(gameInfo.boardWidth != appData.NrFiles) overrule++;
5375 gameInfo.boardWidth = appData.NrFiles;
5377 if(appData.NrRanks >= 0) {
5378 gameInfo.boardHeight = appData.NrRanks;
5380 if(appData.holdingsSize >= 0) {
5381 i = appData.holdingsSize;
5382 if(i > gameInfo.boardHeight) i = gameInfo.boardHeight;
5383 gameInfo.holdingsSize = i;
5385 if(gameInfo.holdingsSize) gameInfo.holdingsWidth = 2;
5386 if(BOARD_HEIGHT > BOARD_RANKS || BOARD_WIDTH > BOARD_FILES)
5387 DisplayFatalError(_("Recompile to support this BOARD_RANKS or BOARD_FILES!"), 0, 2);
5389 pawnRow = gameInfo.boardHeight - 7; /* seems to work in all common variants */
5390 if(pawnRow < 1) pawnRow = 1;
5391 if(gameInfo.variant == VariantMakruk) pawnRow = 2;
5393 /* User pieceToChar list overrules defaults */
5394 if(appData.pieceToCharTable != NULL)
5395 SetCharTable(pieceToChar, appData.pieceToCharTable);
5397 for( j=0; j<BOARD_WIDTH; j++ ) { ChessSquare s = EmptySquare;
5399 if(j==BOARD_LEFT-1 || j==BOARD_RGHT)
5400 s = (ChessSquare) 0; /* account holding counts in guard band */
5401 for( i=0; i<BOARD_HEIGHT; i++ )
5402 initialPosition[i][j] = s;
5404 if(j < BOARD_LEFT || j >= BOARD_RGHT || overrule) continue;
5405 initialPosition[0][j] = pieces[0][j-gameInfo.holdingsWidth];
5406 initialPosition[pawnRow][j] = WhitePawn;
5407 initialPosition[BOARD_HEIGHT-pawnRow-1][j] = BlackPawn;
5408 if(gameInfo.variant == VariantXiangqi) {
5410 initialPosition[pawnRow][j] =
5411 initialPosition[BOARD_HEIGHT-pawnRow-1][j] = EmptySquare;
5412 if(j==BOARD_LEFT+1 || j>=BOARD_RGHT-2) {
5413 initialPosition[2][j] = WhiteCannon;
5414 initialPosition[BOARD_HEIGHT-3][j] = BlackCannon;
5418 initialPosition[BOARD_HEIGHT-1][j] = pieces[1][j-gameInfo.holdingsWidth];
5420 if( (gameInfo.variant == VariantShogi) && !overrule ) {
5423 initialPosition[1][j] = WhiteBishop;
5424 initialPosition[BOARD_HEIGHT-2][j] = BlackRook;
5426 initialPosition[1][j] = WhiteRook;
5427 initialPosition[BOARD_HEIGHT-2][j] = BlackBishop;
5430 if( nrCastlingRights == -1) {
5431 /* [HGM] Build normal castling rights (must be done after board sizing!) */
5432 /* This sets default castling rights from none to normal corners */
5433 /* Variants with other castling rights must set them themselves above */
5434 nrCastlingRights = 6;
5436 initialPosition[CASTLING][0] = initialRights[0] = BOARD_RGHT-1;
5437 initialPosition[CASTLING][1] = initialRights[1] = BOARD_LEFT;
5438 initialPosition[CASTLING][2] = initialRights[2] = BOARD_WIDTH>>1;
5439 initialPosition[CASTLING][3] = initialRights[3] = BOARD_RGHT-1;
5440 initialPosition[CASTLING][4] = initialRights[4] = BOARD_LEFT;
5441 initialPosition[CASTLING][5] = initialRights[5] = BOARD_WIDTH>>1;
5444 if(gameInfo.variant == VariantSuper) Prelude(initialPosition);
5445 if(gameInfo.variant == VariantGreat) { // promotion commoners
5446 initialPosition[PieceToNumber(WhiteMan)][BOARD_WIDTH-1] = WhiteMan;
5447 initialPosition[PieceToNumber(WhiteMan)][BOARD_WIDTH-2] = 9;
5448 initialPosition[BOARD_HEIGHT-1-PieceToNumber(WhiteMan)][0] = BlackMan;
5449 initialPosition[BOARD_HEIGHT-1-PieceToNumber(WhiteMan)][1] = 9;
5451 if (appData.debugMode) {
5452 fprintf(debugFP, "shuffleOpenings = %d\n", shuffleOpenings);
5454 if(shuffleOpenings) {
5455 SetUpShuffle(initialPosition, appData.defaultFrcPosition);
5456 startedFromSetupPosition = TRUE;
5458 if(startedFromPositionFile) {
5459 /* [HGM] loadPos: use PositionFile for every new game */
5460 CopyBoard(initialPosition, filePosition);
5461 for(i=0; i<nrCastlingRights; i++)
5462 initialRights[i] = filePosition[CASTLING][i];
5463 startedFromSetupPosition = TRUE;
5466 CopyBoard(boards[0], initialPosition);
5468 if(oldx != gameInfo.boardWidth ||
5469 oldy != gameInfo.boardHeight ||
5470 oldh != gameInfo.holdingsWidth
5472 || oldv == VariantGothic || // For licensing popups
5473 gameInfo.variant == VariantGothic
5476 || oldv == VariantFalcon ||
5477 gameInfo.variant == VariantFalcon
5480 InitDrawingSizes(-2 ,0);
5483 DrawPosition(TRUE, boards[currentMove]);
5487 SendBoard(cps, moveNum)
5488 ChessProgramState *cps;
5491 char message[MSG_SIZ];
5493 if (cps->useSetboard) {
5494 char* fen = PositionToFEN(moveNum, cps->fenOverride);
5495 sprintf(message, "setboard %s\n", fen);
5496 SendToProgram(message, cps);
5502 /* Kludge to set black to move, avoiding the troublesome and now
5503 * deprecated "black" command.
5505 if (!WhiteOnMove(moveNum)) SendToProgram("a2a3\n", cps);
5507 SendToProgram("edit\n", cps);
5508 SendToProgram("#\n", cps);
5509 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
5510 bp = &boards[moveNum][i][BOARD_LEFT];
5511 for (j = BOARD_LEFT; j < BOARD_RGHT; j++, bp++) {
5512 if ((int) *bp < (int) BlackPawn) {
5513 sprintf(message, "%c%c%c\n", PieceToChar(*bp),
5515 if(message[0] == '+' || message[0] == '~') {
5516 sprintf(message, "%c%c%c+\n",
5517 PieceToChar((ChessSquare)(DEMOTED *bp)),
5520 if(cps->alphaRank) { /* [HGM] shogi: translate coords */
5521 message[1] = BOARD_RGHT - 1 - j + '1';
5522 message[2] = BOARD_HEIGHT - 1 - i + 'a';
5524 SendToProgram(message, cps);
5529 SendToProgram("c\n", cps);
5530 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
5531 bp = &boards[moveNum][i][BOARD_LEFT];
5532 for (j = BOARD_LEFT; j < BOARD_RGHT; j++, bp++) {
5533 if (((int) *bp != (int) EmptySquare)
5534 && ((int) *bp >= (int) BlackPawn)) {
5535 sprintf(message, "%c%c%c\n", ToUpper(PieceToChar(*bp)),
5537 if(message[0] == '+' || message[0] == '~') {
5538 sprintf(message, "%c%c%c+\n",
5539 PieceToChar((ChessSquare)(DEMOTED *bp)),
5542 if(cps->alphaRank) { /* [HGM] shogi: translate coords */
5543 message[1] = BOARD_RGHT - 1 - j + '1';
5544 message[2] = BOARD_HEIGHT - 1 - i + 'a';
5546 SendToProgram(message, cps);
5551 SendToProgram(".\n", cps);
5553 setboardSpoiledMachineBlack = 0; /* [HGM] assume WB 4.2.7 already solves this after sending setboard */
5556 static int autoQueen; // [HGM] oneclick
5559 HasPromotionChoice(int fromX, int fromY, int toX, int toY, char *promoChoice)
5561 /* [HGM] rewritten IsPromotion to only flag promotions that offer a choice */
5562 /* [HGM] add Shogi promotions */
5563 int promotionZoneSize=1, highestPromotingPiece = (int)WhitePawn;
5568 if(fromX < BOARD_LEFT || fromX >= BOARD_RGHT) return FALSE; // drop
5569 if(toX < BOARD_LEFT || toX >= BOARD_RGHT) return FALSE; // move into holdings
5571 if(gameMode == EditPosition || gameInfo.variant == VariantXiangqi || // no promotions
5572 !(fromX >=0 && fromY >= 0 && toX >= 0 && toY >= 0) ) // invalid move
5575 piece = boards[currentMove][fromY][fromX];
5576 if(gameInfo.variant == VariantShogi) {
5577 promotionZoneSize = BOARD_HEIGHT/3;
5578 highestPromotingPiece = (int)WhiteFerz;
5579 } else if(gameInfo.variant == VariantMakruk) {
5580 promotionZoneSize = 3;
5583 // next weed out all moves that do not touch the promotion zone at all
5584 if((int)piece >= BlackPawn) {
5585 if(toY >= promotionZoneSize && fromY >= promotionZoneSize)
5587 highestPromotingPiece = WHITE_TO_BLACK highestPromotingPiece;
5589 if( toY < BOARD_HEIGHT - promotionZoneSize &&
5590 fromY < BOARD_HEIGHT - promotionZoneSize) return FALSE;
5593 if( (int)piece > highestPromotingPiece ) return FALSE; // non-promoting piece
5595 // weed out mandatory Shogi promotions
5596 if(gameInfo.variant == VariantShogi) {
5597 if(piece >= BlackPawn) {
5598 if(toY == 0 && piece == BlackPawn ||
5599 toY == 0 && piece == BlackQueen ||
5600 toY <= 1 && piece == BlackKnight) {
5605 if(toY == BOARD_HEIGHT-1 && piece == WhitePawn ||
5606 toY == BOARD_HEIGHT-1 && piece == WhiteQueen ||
5607 toY >= BOARD_HEIGHT-2 && piece == WhiteKnight) {
5614 // weed out obviously illegal Pawn moves
5615 if(appData.testLegality && (piece == WhitePawn || piece == BlackPawn) ) {
5616 if(toX > fromX+1 || toX < fromX-1) return FALSE; // wide
5617 if(piece == WhitePawn && toY != fromY+1) return FALSE; // deep
5618 if(piece == BlackPawn && toY != fromY-1) return FALSE; // deep
5619 if(fromX != toX && gameInfo.variant == VariantShogi) return FALSE;
5620 // note we are not allowed to test for valid (non-)capture, due to premove
5623 // we either have a choice what to promote to, or (in Shogi) whether to promote
5624 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk) {
5625 *promoChoice = PieceToChar(BlackFerz); // no choice
5628 if(autoQueen) { // predetermined
5629 if(gameInfo.variant == VariantSuicide || gameInfo.variant == VariantLosers)
5630 *promoChoice = PieceToChar(BlackKing); // in Suicide Q is the last thing we want
5631 else *promoChoice = PieceToChar(BlackQueen);
5635 // suppress promotion popup on illegal moves that are not premoves
5636 premove = gameMode == IcsPlayingWhite && !WhiteOnMove(currentMove) ||
5637 gameMode == IcsPlayingBlack && WhiteOnMove(currentMove);
5638 if(appData.testLegality && !premove) {
5639 moveType = LegalityTest(boards[currentMove], PosFlags(currentMove),
5640 fromY, fromX, toY, toX, NULLCHAR);
5641 if(moveType != WhitePromotion && moveType != BlackPromotion)
5649 InPalace(row, column)
5651 { /* [HGM] for Xiangqi */
5652 if( (row < 3 || row > BOARD_HEIGHT-4) &&
5653 column < (BOARD_WIDTH + 4)/2 &&
5654 column > (BOARD_WIDTH - 5)/2 ) return TRUE;
5659 PieceForSquare (x, y)
5663 if (x < 0 || x >= BOARD_WIDTH || y < 0 || y >= BOARD_HEIGHT)
5666 return boards[currentMove][y][x];
5670 OKToStartUserMove(x, y)
5673 ChessSquare from_piece;
5676 if (matchMode) return FALSE;
5677 if (gameMode == EditPosition) return TRUE;
5679 if (x >= 0 && y >= 0)
5680 from_piece = boards[currentMove][y][x];
5682 from_piece = EmptySquare;
5684 if (from_piece == EmptySquare) return FALSE;
5686 white_piece = (int)from_piece >= (int)WhitePawn &&
5687 (int)from_piece < (int)BlackPawn; /* [HGM] can be > King! */
5690 case PlayFromGameFile:
5692 case TwoMachinesPlay:
5700 case MachinePlaysWhite:
5701 case IcsPlayingBlack:
5702 if (appData.zippyPlay) return FALSE;
5704 DisplayMoveError(_("You are playing Black"));
5709 case MachinePlaysBlack:
5710 case IcsPlayingWhite:
5711 if (appData.zippyPlay) return FALSE;
5713 DisplayMoveError(_("You are playing White"));
5719 if (!white_piece && WhiteOnMove(currentMove)) {
5720 DisplayMoveError(_("It is White's turn"));
5723 if (white_piece && !WhiteOnMove(currentMove)) {
5724 DisplayMoveError(_("It is Black's turn"));
5727 if (cmailMsgLoaded && (currentMove < cmailOldMove)) {
5728 /* Editing correspondence game history */
5729 /* Could disallow this or prompt for confirmation */
5734 case BeginningOfGame:
5735 if (appData.icsActive) return FALSE;
5736 if (!appData.noChessProgram) {
5738 DisplayMoveError(_("You are playing White"));
5745 if (!white_piece && WhiteOnMove(currentMove)) {
5746 DisplayMoveError(_("It is White's turn"));
5749 if (white_piece && !WhiteOnMove(currentMove)) {
5750 DisplayMoveError(_("It is Black's turn"));
5759 if (currentMove != forwardMostMove && gameMode != AnalyzeMode
5760 && gameMode != EditGame // [HGM] vari: treat as AnalyzeMode
5761 && gameMode != AnalyzeFile && gameMode != Training) {
5762 DisplayMoveError(_("Displayed position is not current"));
5769 OnlyMove(int *x, int *y, Boolean captures) {
5770 DisambiguateClosure cl;
5771 if (appData.zippyPlay) return FALSE;
5773 case MachinePlaysBlack:
5774 case IcsPlayingWhite:
5775 case BeginningOfGame:
5776 if(!WhiteOnMove(currentMove)) return FALSE;
5778 case MachinePlaysWhite:
5779 case IcsPlayingBlack:
5780 if(WhiteOnMove(currentMove)) return FALSE;
5785 cl.pieceIn = EmptySquare;
5790 cl.promoCharIn = NULLCHAR;
5791 Disambiguate(boards[currentMove], PosFlags(currentMove), &cl);
5792 if( cl.kind == NormalMove ||
5793 cl.kind == AmbiguousMove && captures && cl.captures == 1 ||
5794 cl.kind == WhitePromotion || cl.kind == BlackPromotion ||
5795 cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) {
5802 if(cl.kind != ImpossibleMove) return FALSE;
5803 cl.pieceIn = EmptySquare;
5808 cl.promoCharIn = NULLCHAR;
5809 Disambiguate(boards[currentMove], PosFlags(currentMove), &cl);
5810 if( cl.kind == NormalMove ||
5811 cl.kind == AmbiguousMove && captures && cl.captures == 1 ||
5812 cl.kind == WhitePromotion || cl.kind == BlackPromotion ||
5813 cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) {
5818 autoQueen = TRUE; // act as if autoQueen on when we click to-square
5824 FILE *lastLoadGameFP = NULL, *lastLoadPositionFP = NULL;
5825 int lastLoadGameNumber = 0, lastLoadPositionNumber = 0;
5826 int lastLoadGameUseList = FALSE;
5827 char lastLoadGameTitle[MSG_SIZ], lastLoadPositionTitle[MSG_SIZ];
5828 ChessMove lastLoadGameStart = (ChessMove) 0;
5831 UserMoveEvent(fromX, fromY, toX, toY, promoChar)
5832 int fromX, fromY, toX, toY;
5836 ChessSquare pdown, pup;
5838 /* Check if the user is playing in turn. This is complicated because we
5839 let the user "pick up" a piece before it is his turn. So the piece he
5840 tried to pick up may have been captured by the time he puts it down!
5841 Therefore we use the color the user is supposed to be playing in this
5842 test, not the color of the piece that is currently on the starting
5843 square---except in EditGame mode, where the user is playing both
5844 sides; fortunately there the capture race can't happen. (It can
5845 now happen in IcsExamining mode, but that's just too bad. The user
5846 will get a somewhat confusing message in that case.)
5850 case PlayFromGameFile:
5852 case TwoMachinesPlay:
5856 /* We switched into a game mode where moves are not accepted,
5857 perhaps while the mouse button was down. */
5860 case MachinePlaysWhite:
5861 /* User is moving for Black */
5862 if (WhiteOnMove(currentMove)) {
5863 DisplayMoveError(_("It is White's turn"));
5868 case MachinePlaysBlack:
5869 /* User is moving for White */
5870 if (!WhiteOnMove(currentMove)) {
5871 DisplayMoveError(_("It is Black's turn"));
5878 case BeginningOfGame:
5881 if ((int) boards[currentMove][fromY][fromX] >= (int) BlackPawn &&
5882 (int) boards[currentMove][fromY][fromX] < (int) EmptySquare) {
5883 /* User is moving for Black */
5884 if (WhiteOnMove(currentMove)) {
5885 DisplayMoveError(_("It is White's turn"));
5889 /* User is moving for White */
5890 if (!WhiteOnMove(currentMove)) {
5891 DisplayMoveError(_("It is Black's turn"));
5897 case IcsPlayingBlack:
5898 /* User is moving for Black */
5899 if (WhiteOnMove(currentMove)) {
5900 if (!appData.premove) {
5901 DisplayMoveError(_("It is White's turn"));
5902 } else if (toX >= 0 && toY >= 0) {
5905 premoveFromX = fromX;
5906 premoveFromY = fromY;
5907 premovePromoChar = promoChar;
5909 if (appData.debugMode)
5910 fprintf(debugFP, "Got premove: fromX %d,"
5911 "fromY %d, toX %d, toY %d\n",
5912 fromX, fromY, toX, toY);
5918 case IcsPlayingWhite:
5919 /* User is moving for White */
5920 if (!WhiteOnMove(currentMove)) {
5921 if (!appData.premove) {
5922 DisplayMoveError(_("It is Black's turn"));
5923 } else if (toX >= 0 && toY >= 0) {
5926 premoveFromX = fromX;
5927 premoveFromY = fromY;
5928 premovePromoChar = promoChar;
5930 if (appData.debugMode)
5931 fprintf(debugFP, "Got premove: fromX %d,"
5932 "fromY %d, toX %d, toY %d\n",
5933 fromX, fromY, toX, toY);
5943 /* EditPosition, empty square, or different color piece;
5944 click-click move is possible */
5945 if (toX == -2 || toY == -2) {
5946 boards[0][fromY][fromX] = EmptySquare;
5947 DrawPosition(FALSE, boards[currentMove]);
5949 } else if (toX >= 0 && toY >= 0) {
5950 boards[0][toY][toX] = boards[0][fromY][fromX];
5951 if(fromX == BOARD_LEFT-2) { // handle 'moves' out of holdings
5952 if(boards[0][fromY][0] != EmptySquare) {
5953 if(boards[0][fromY][1]) boards[0][fromY][1]--;
5954 if(boards[0][fromY][1] == 0) boards[0][fromY][0] = EmptySquare;
5957 if(fromX == BOARD_RGHT+1) {
5958 if(boards[0][fromY][BOARD_WIDTH-1] != EmptySquare) {
5959 if(boards[0][fromY][BOARD_WIDTH-2]) boards[0][fromY][BOARD_WIDTH-2]--;
5960 if(boards[0][fromY][BOARD_WIDTH-2] == 0) boards[0][fromY][BOARD_WIDTH-1] = EmptySquare;
5963 boards[0][fromY][fromX] = EmptySquare;
5964 DrawPosition(FALSE, boards[currentMove]);
5970 if(toX < 0 || toY < 0) return;
5971 pdown = boards[currentMove][fromY][fromX];
5972 pup = boards[currentMove][toY][toX];
5974 /* [HGM] If move started in holdings, it means a drop. Convert to standard form */
5975 if( fromX == BOARD_LEFT-2 || fromX == BOARD_RGHT+1) {
5976 if( pup != EmptySquare ) return;
5977 moveType = WhiteOnMove(currentMove) ? WhiteDrop : BlackDrop;
5978 if(appData.debugMode) fprintf(debugFP, "Drop move %d, curr=%d, x=%d,y=%d, p=%d\n",
5979 moveType, currentMove, fromX, fromY, boards[currentMove][fromY][fromX]);
5980 // holdings might not be sent yet in ICS play; we have to figure out which piece belongs here
5981 if(fromX == 0) fromY = BOARD_HEIGHT-1 - fromY; // black holdings upside-down
5982 fromX = fromX ? WhitePawn : BlackPawn; // first piece type in selected holdings
5983 while(PieceToChar(fromX) == '.' || PieceToNumber(fromX) != fromY && fromX != (int) EmptySquare) fromX++;
5987 /* [HGM] always test for legality, to get promotion info */
5988 moveType = LegalityTest(boards[currentMove], PosFlags(currentMove),
5989 fromY, fromX, toY, toX, promoChar);
5990 /* [HGM] but possibly ignore an IllegalMove result */
5991 if (appData.testLegality) {
5992 if (moveType == IllegalMove || moveType == ImpossibleMove) {
5993 DisplayMoveError(_("Illegal move"));
5998 FinishMove(moveType, fromX, fromY, toX, toY, promoChar);
6001 /* Common tail of UserMoveEvent and DropMenuEvent */
6003 FinishMove(moveType, fromX, fromY, toX, toY, promoChar)
6005 int fromX, fromY, toX, toY;
6006 /*char*/int promoChar;
6010 if((gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat) && promoChar != NULLCHAR) {
6011 // [HGM] superchess: suppress promotions to non-available piece
6012 int k = PieceToNumber(CharToPiece(ToUpper(promoChar)));
6013 if(WhiteOnMove(currentMove)) {
6014 if(!boards[currentMove][k][BOARD_WIDTH-2]) return 0;
6016 if(!boards[currentMove][BOARD_HEIGHT-1-k][1]) return 0;
6020 /* [HGM] <popupFix> kludge to avoid having to know the exact promotion
6021 move type in caller when we know the move is a legal promotion */
6022 if(moveType == NormalMove && promoChar)
6023 moveType = WhiteOnMove(currentMove) ? WhitePromotion : BlackPromotion;
6025 /* [HGM] <popupFix> The following if has been moved here from
6026 UserMoveEvent(). Because it seemed to belong here (why not allow
6027 piece drops in training games?), and because it can only be
6028 performed after it is known to what we promote. */
6029 if (gameMode == Training) {
6030 /* compare the move played on the board to the next move in the
6031 * game. If they match, display the move and the opponent's response.
6032 * If they don't match, display an error message.
6036 CopyBoard(testBoard, boards[currentMove]);
6037 ApplyMove(fromX, fromY, toX, toY, promoChar, testBoard);
6039 if (CompareBoards(testBoard, boards[currentMove+1])) {
6040 ForwardInner(currentMove+1);
6042 /* Autoplay the opponent's response.
6043 * if appData.animate was TRUE when Training mode was entered,
6044 * the response will be animated.
6046 saveAnimate = appData.animate;
6047 appData.animate = animateTraining;
6048 ForwardInner(currentMove+1);
6049 appData.animate = saveAnimate;
6051 /* check for the end of the game */
6052 if (currentMove >= forwardMostMove) {
6053 gameMode = PlayFromGameFile;
6055 SetTrainingModeOff();
6056 DisplayInformation(_("End of game"));
6059 DisplayError(_("Incorrect move"), 0);
6064 /* Ok, now we know that the move is good, so we can kill
6065 the previous line in Analysis Mode */
6066 if ((gameMode == AnalyzeMode || gameMode == EditGame)
6067 && currentMove < forwardMostMove) {
6068 PushTail(currentMove, forwardMostMove); // [HGM] vari: save tail of game
6071 /* If we need the chess program but it's dead, restart it */
6072 ResurrectChessProgram();
6074 /* A user move restarts a paused game*/
6078 thinkOutput[0] = NULLCHAR;
6080 MakeMove(fromX, fromY, toX, toY, promoChar); /*updates forwardMostMove*/
6082 if(Adjudicate(NULL)) return 1; // [HGM] adjudicate: take care of automtic game end
6084 if (gameMode == BeginningOfGame) {
6085 if (appData.noChessProgram) {
6086 gameMode = EditGame;
6090 gameMode = MachinePlaysBlack;
6093 sprintf(buf, "%s vs. %s", gameInfo.white, gameInfo.black);
6095 if (first.sendName) {
6096 sprintf(buf, "name %s\n", gameInfo.white);
6097 SendToProgram(buf, &first);
6104 /* Relay move to ICS or chess engine */
6105 if (appData.icsActive) {
6106 if (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
6107 gameMode == IcsExamining) {
6108 if(userOfferedDraw && (signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
6109 SendToICS(ics_prefix); // [HGM] drawclaim: send caim and move on one line for FICS
6111 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
6113 // also send plain move, in case ICS does not understand atomic claims
6114 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
6118 if (first.sendTime && (gameMode == BeginningOfGame ||
6119 gameMode == MachinePlaysWhite ||
6120 gameMode == MachinePlaysBlack)) {
6121 SendTimeRemaining(&first, gameMode != MachinePlaysBlack);
6123 if (gameMode != EditGame && gameMode != PlayFromGameFile) {
6124 // [HGM] book: if program might be playing, let it use book
6125 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, FALSE);
6126 first.maybeThinking = TRUE;
6127 } else SendMoveToProgram(forwardMostMove-1, &first);
6128 if (currentMove == cmailOldMove + 1) {
6129 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
6133 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6137 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
6143 if (WhiteOnMove(currentMove)) {
6144 GameEnds(BlackWins, "Black mates", GE_PLAYER);
6146 GameEnds(WhiteWins, "White mates", GE_PLAYER);
6150 GameEnds(GameIsDrawn, "Stalemate", GE_PLAYER);
6155 case MachinePlaysBlack:
6156 case MachinePlaysWhite:
6157 /* disable certain menu options while machine is thinking */
6158 SetMachineThinkingEnables();
6165 userOfferedDraw = FALSE; // [HGM] drawclaim: after move made, and tested for claimable draw
6167 if(bookHit) { // [HGM] book: simulate book reply
6168 static char bookMove[MSG_SIZ]; // a bit generous?
6170 programStats.nodes = programStats.depth = programStats.time =
6171 programStats.score = programStats.got_only_move = 0;
6172 sprintf(programStats.movelist, "%s (xbook)", bookHit);
6174 strcpy(bookMove, "move ");
6175 strcat(bookMove, bookHit);
6176 HandleMachineMove(bookMove, &first);
6182 Mark(board, flags, kind, rf, ff, rt, ft, closure)
6189 typedef char Markers[BOARD_RANKS][BOARD_FILES];
6190 Markers *m = (Markers *) closure;
6191 if(rf == fromY && ff == fromX)
6192 (*m)[rt][ft] = 1 + (board[rt][ft] != EmptySquare
6193 || kind == WhiteCapturesEnPassant
6194 || kind == BlackCapturesEnPassant);
6195 else if(flags & F_MANDATORY_CAPTURE && board[rt][ft] != EmptySquare) (*m)[rt][ft] = 3;
6199 MarkTargetSquares(int clear)
6202 if(!appData.markers || !appData.highlightDragging ||
6203 !appData.testLegality || gameMode == EditPosition) return;
6205 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) marker[y][x] = 0;
6208 GenLegal(boards[currentMove], PosFlags(currentMove), Mark, (void*) marker);
6209 if(PosFlags(0) & F_MANDATORY_CAPTURE) {
6210 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) if(marker[y][x]>1) capt++;
6212 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) if(marker[y][x] == 1) marker[y][x] = 0;
6215 DrawPosition(TRUE, NULL);
6218 void LeftClick(ClickType clickType, int xPix, int yPix)
6221 Boolean saveAnimate;
6222 static int second = 0, promotionChoice = 0, dragging = 0;
6223 char promoChoice = NULLCHAR;
6225 if(appData.seekGraph && appData.icsActive && loggedOn &&
6226 (gameMode == BeginningOfGame || gameMode == IcsIdle)) {
6227 SeekGraphClick(clickType, xPix, yPix, 0);
6231 if (clickType == Press) ErrorPopDown();
6232 MarkTargetSquares(1);
6234 x = EventToSquare(xPix, BOARD_WIDTH);
6235 y = EventToSquare(yPix, BOARD_HEIGHT);
6236 if (!flipView && y >= 0) {
6237 y = BOARD_HEIGHT - 1 - y;
6239 if (flipView && x >= 0) {
6240 x = BOARD_WIDTH - 1 - x;
6243 if(promotionChoice) { // we are waiting for a click to indicate promotion piece
6244 if(clickType == Release) return; // ignore upclick of click-click destination
6245 promotionChoice = FALSE; // only one chance: if click not OK it is interpreted as cancel
6246 if(appData.debugMode) fprintf(debugFP, "promotion click, x=%d, y=%d\n", x, y);
6247 if(gameInfo.holdingsWidth &&
6248 (WhiteOnMove(currentMove)
6249 ? x == BOARD_WIDTH-1 && y < gameInfo.holdingsSize && y > 0
6250 : x == 0 && y >= BOARD_HEIGHT - gameInfo.holdingsSize && y < BOARD_HEIGHT-1) ) {
6251 // click in right holdings, for determining promotion piece
6252 ChessSquare p = boards[currentMove][y][x];
6253 if(appData.debugMode) fprintf(debugFP, "square contains %d\n", (int)p);
6254 if(p != EmptySquare) {
6255 FinishMove(NormalMove, fromX, fromY, toX, toY, ToLower(PieceToChar(p)));
6260 DrawPosition(FALSE, boards[currentMove]);
6264 /* [HGM] holdings: next 5 lines: ignore all clicks between board and holdings */
6265 if(clickType == Press
6266 && ( x == BOARD_LEFT-1 || x == BOARD_RGHT
6267 || x == BOARD_LEFT-2 && y < BOARD_HEIGHT-gameInfo.holdingsSize
6268 || x == BOARD_RGHT+1 && y >= gameInfo.holdingsSize) )
6271 autoQueen = appData.alwaysPromoteToQueen;
6274 if(!appData.oneClick || !OnlyMove(&x, &y, FALSE)) {
6275 if (clickType == Press) {
6277 if (OKToStartUserMove(x, y)) {
6281 MarkTargetSquares(0);
6282 DragPieceBegin(xPix, yPix); dragging = 1;
6283 if (appData.highlightDragging) {
6284 SetHighlights(x, y, -1, -1);
6287 } else if(dragging) { // [HGM] from-square must have been reset due to game end since last press
6288 DragPieceEnd(xPix, yPix); dragging = 0;
6289 DrawPosition(FALSE, NULL);
6296 if (clickType == Press && gameMode != EditPosition) {
6301 // ignore off-board to clicks
6302 if(y < 0 || x < 0) return;
6304 /* Check if clicking again on the same color piece */
6305 fromP = boards[currentMove][fromY][fromX];
6306 toP = boards[currentMove][y][x];
6307 frc = gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom;
6308 if ((WhitePawn <= fromP && fromP <= WhiteKing &&
6309 WhitePawn <= toP && toP <= WhiteKing &&
6310 !(fromP == WhiteKing && toP == WhiteRook && frc) &&
6311 !(fromP == WhiteRook && toP == WhiteKing && frc)) ||
6312 (BlackPawn <= fromP && fromP <= BlackKing &&
6313 BlackPawn <= toP && toP <= BlackKing &&
6314 !(fromP == BlackRook && toP == BlackKing && frc) && // allow also RxK as FRC castling
6315 !(fromP == BlackKing && toP == BlackRook && frc))) {
6316 /* Clicked again on same color piece -- changed his mind */
6317 second = (x == fromX && y == fromY);
6318 if(!second || appData.oneClick && !OnlyMove(&x, &y, TRUE)) {
6319 if (appData.highlightDragging) {
6320 SetHighlights(x, y, -1, -1);
6324 if (OKToStartUserMove(x, y)) {
6326 fromY = y; dragging = 1;
6327 MarkTargetSquares(0);
6328 DragPieceBegin(xPix, yPix);
6333 // ignore clicks on holdings
6334 if(x < BOARD_LEFT || x >= BOARD_RGHT) return;
6337 if (clickType == Release && x == fromX && y == fromY) {
6338 DragPieceEnd(xPix, yPix); dragging = 0;
6339 if (appData.animateDragging) {
6340 /* Undo animation damage if any */
6341 DrawPosition(FALSE, NULL);
6344 /* Second up/down in same square; just abort move */
6349 ClearPremoveHighlights();
6351 /* First upclick in same square; start click-click mode */
6352 SetHighlights(x, y, -1, -1);
6357 /* we now have a different from- and (possibly off-board) to-square */
6358 /* Completed move */
6361 saveAnimate = appData.animate;
6362 if (clickType == Press) {
6363 /* Finish clickclick move */
6364 if (appData.animate || appData.highlightLastMove) {
6365 SetHighlights(fromX, fromY, toX, toY);
6370 /* Finish drag move */
6371 if (appData.highlightLastMove) {
6372 SetHighlights(fromX, fromY, toX, toY);
6376 DragPieceEnd(xPix, yPix); dragging = 0;
6377 /* Don't animate move and drag both */
6378 appData.animate = FALSE;
6381 // moves into holding are invalid for now (except in EditPosition, adapting to-square)
6382 if(x >= 0 && x < BOARD_LEFT || x >= BOARD_RGHT) {
6383 ChessSquare piece = boards[currentMove][fromY][fromX];
6384 if(gameMode == EditPosition && piece != EmptySquare &&
6385 fromX >= BOARD_LEFT && fromX < BOARD_RGHT) {
6388 if(x == BOARD_LEFT-2 && piece >= BlackPawn) {
6389 n = PieceToNumber(piece - (int)BlackPawn);
6390 if(n >= gameInfo.holdingsSize) { n = 0; piece = BlackPawn; }
6391 boards[currentMove][BOARD_HEIGHT-1 - n][0] = piece;
6392 boards[currentMove][BOARD_HEIGHT-1 - n][1]++;
6394 if(x == BOARD_RGHT+1 && piece < BlackPawn) {
6395 n = PieceToNumber(piece);
6396 if(n >= gameInfo.holdingsSize) { n = 0; piece = WhitePawn; }
6397 boards[currentMove][n][BOARD_WIDTH-1] = piece;
6398 boards[currentMove][n][BOARD_WIDTH-2]++;
6400 boards[currentMove][fromY][fromX] = EmptySquare;
6404 DrawPosition(TRUE, boards[currentMove]);
6408 // off-board moves should not be highlighted
6409 if(x < 0 || x < 0) ClearHighlights();
6411 if (HasPromotionChoice(fromX, fromY, toX, toY, &promoChoice)) {
6412 SetHighlights(fromX, fromY, toX, toY);
6413 if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat) {
6414 // [HGM] super: promotion to captured piece selected from holdings
6415 ChessSquare p = boards[currentMove][fromY][fromX], q = boards[currentMove][toY][toX];
6416 promotionChoice = TRUE;
6417 // kludge follows to temporarily execute move on display, without promoting yet
6418 boards[currentMove][fromY][fromX] = EmptySquare; // move Pawn to 8th rank
6419 boards[currentMove][toY][toX] = p;
6420 DrawPosition(FALSE, boards[currentMove]);
6421 boards[currentMove][fromY][fromX] = p; // take back, but display stays
6422 boards[currentMove][toY][toX] = q;
6423 DisplayMessage("Click in holdings to choose piece", "");
6428 UserMoveEvent(fromX, fromY, toX, toY, promoChoice);
6429 if (!appData.highlightLastMove || gotPremove) ClearHighlights();
6430 if (gotPremove) SetPremoveHighlights(fromX, fromY, toX, toY);
6433 appData.animate = saveAnimate;
6434 if (appData.animate || appData.animateDragging) {
6435 /* Undo animation damage if needed */
6436 DrawPosition(FALSE, NULL);
6440 int RightClick(ClickType action, int x, int y, int *fromX, int *fromY)
6441 { // front-end-free part taken out of PieceMenuPopup
6442 int whichMenu; int xSqr, ySqr;
6444 if(seekGraphUp) { // [HGM] seekgraph
6445 if(action == Press) SeekGraphClick(Press, x, y, 2); // 2 indicates right-click: no pop-down on miss
6446 if(action == Release) SeekGraphClick(Release, x, y, 2); // and no challenge on hit
6450 if((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)
6451 && !appData.zippyPlay && appData.bgObserve) { // [HGM] bughouse: show background game
6452 if(!partnerBoardValid) return -2; // suppress display of uninitialized boards
6453 if( appData.dualBoard) return -2; // [HGM] dual: is already displayed
6454 if(action == Press) {
6455 originalFlip = flipView;
6456 flipView = !flipView; // temporarily flip board to see game from partners perspective
6457 DrawPosition(TRUE, partnerBoard);
6458 DisplayMessage(partnerStatus, "");
6460 } else if(action == Release) {
6461 flipView = originalFlip;
6462 DrawPosition(TRUE, boards[currentMove]);
6468 xSqr = EventToSquare(x, BOARD_WIDTH);
6469 ySqr = EventToSquare(y, BOARD_HEIGHT);
6470 if (action == Release) UnLoadPV(); // [HGM] pv
6471 if (action != Press) return -2; // return code to be ignored
6474 if(xSqr < BOARD_LEFT || xSqr >= BOARD_RGHT) return -1;
\r
6476 if (xSqr == BOARD_LEFT-1 || xSqr == BOARD_RGHT) return -1;
\r
6477 if (xSqr < 0 || ySqr < 0) return -1;
\r
6478 whichMenu = 0; // edit-position menu
6481 if(!appData.icsEngineAnalyze) return -1;
6482 case IcsPlayingWhite:
6483 case IcsPlayingBlack:
6484 if(!appData.zippyPlay) goto noZip;
6487 case MachinePlaysWhite:
6488 case MachinePlaysBlack:
6489 case TwoMachinesPlay: // [HGM] pv: use for showing PV
6490 if (!appData.dropMenu) {
6492 return 2; // flag front-end to grab mouse events
6494 if(gameMode == TwoMachinesPlay || gameMode == AnalyzeMode ||
6495 gameMode == AnalyzeFile || gameMode == IcsObserving) return -1;
6498 if (xSqr < 0 || ySqr < 0) return -1;
6499 if (!appData.dropMenu || appData.testLegality &&
6500 gameInfo.variant != VariantBughouse &&
6501 gameInfo.variant != VariantCrazyhouse) return -1;
6502 whichMenu = 1; // drop menu
6508 if (((*fromX = xSqr) < 0) ||
6509 ((*fromY = ySqr) < 0)) {
6510 *fromX = *fromY = -1;
6514 *fromX = BOARD_WIDTH - 1 - *fromX;
6516 *fromY = BOARD_HEIGHT - 1 - *fromY;
6521 void SendProgramStatsToFrontend( ChessProgramState * cps, ChessProgramStats * cpstats )
6523 // char * hint = lastHint;
6524 FrontEndProgramStats stats;
6526 stats.which = cps == &first ? 0 : 1;
6527 stats.depth = cpstats->depth;
6528 stats.nodes = cpstats->nodes;
6529 stats.score = cpstats->score;
6530 stats.time = cpstats->time;
6531 stats.pv = cpstats->movelist;
6532 stats.hint = lastHint;
6533 stats.an_move_index = 0;
6534 stats.an_move_count = 0;
6536 if( gameMode == AnalyzeMode || gameMode == AnalyzeFile ) {
6537 stats.hint = cpstats->move_name;
6538 stats.an_move_index = cpstats->nr_moves - cpstats->moves_left;
6539 stats.an_move_count = cpstats->nr_moves;
6542 if(stats.pv && stats.pv[0]) strcpy(lastPV[stats.which], stats.pv); // [HGM] pv: remember last PV of each
6544 SetProgramStats( &stats );
6548 Count(Board board, int pCnt[], int *nW, int *nB, int *wStale, int *bStale, int *bishopColor)
6549 { // count all piece types
6551 *nB = *nW = *wStale = *bStale = *bishopColor = 0;
6552 for(p=WhitePawn; p<=EmptySquare; p++) pCnt[p] = 0;
6553 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
6556 if(p == WhitePawn && r == BOARD_HEIGHT-1) (*wStale)++; else
6557 if(p == BlackPawn && r == 0) (*bStale)++; // count last-Rank Pawns (XQ) separately
6558 if(p <= WhiteKing) (*nW)++; else if(p <= BlackKing) (*nB)++;
6559 if(p == WhiteBishop || p == WhiteFerz || p == WhiteAlfil ||
6560 p == BlackBishop || p == BlackFerz || p == BlackAlfil )
6561 *bishopColor |= 1 << ((f^r)&1); // track square color of color-bound pieces
6566 SufficientDefence(int pCnt[], int side, int nMine, int nHis)
6568 int myPawns = pCnt[WhitePawn+side]; // my total Pawn count;
6569 int majorDefense = pCnt[BlackRook-side] + pCnt[BlackCannon-side] + pCnt[BlackKnight-side];
6571 nMine -= pCnt[WhiteFerz+side] + pCnt[WhiteAlfil+side]; // discount defenders
6572 if(nMine - myPawns > 2) return FALSE; // no trivial draws with more than 1 major
6573 if(myPawns == 2 && nMine == 3) // KPP
6574 return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] >= 3;
6575 if(myPawns == 1 && nMine == 2) // KP
6576 return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] + pCnt[BlackPawn-side] >= 1;
6577 if(myPawns == 1 && nMine == 3 && pCnt[WhiteKnight+side]) // KHP
6578 return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side]*2 >= 5;
6579 if(myPawns) return FALSE;
6580 if(pCnt[WhiteRook+side])
6581 return pCnt[BlackRook-side] ||
6582 pCnt[BlackCannon-side] && (pCnt[BlackFerz-side] >= 2 || pCnt[BlackAlfil-side] >= 2) ||
6583 pCnt[BlackKnight-side] && pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] > 2 ||
6584 pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] >= 4;
6585 if(pCnt[WhiteCannon+side]) {
6586 if(pCnt[WhiteFerz+side] + myPawns == 0) return TRUE; // Cannon needs platform
6587 return majorDefense || pCnt[BlackAlfil-side] >= 2;
6589 if(pCnt[WhiteKnight+side])
6590 return majorDefense || pCnt[BlackFerz-side] >= 2 || pCnt[BlackAlfil-side] + pCnt[BlackPawn-side] >= 1;
6595 MatingPotential(int pCnt[], int side, int nMine, int nHis, int stale, int bisColor)
6597 VariantClass v = gameInfo.variant;
6599 if(v == VariantShogi || v == VariantCrazyhouse || v == VariantBughouse) return TRUE; // drop games always winnable
6600 if(v == VariantShatranj) return TRUE; // always winnable through baring
6601 if(v == VariantLosers || v == VariantSuicide || v == VariantGiveaway) return TRUE;
6602 if(v == Variant3Check || v == VariantAtomic) return nMine > 1; // can win through checking / exploding King
6604 if(v == VariantXiangqi) {
6605 int majors = 5*pCnt[BlackKnight-side] + 7*pCnt[BlackCannon-side] + 7*pCnt[BlackRook-side];
6607 nMine -= pCnt[WhiteFerz+side] + pCnt[WhiteAlfil+side] + stale; // discount defensive pieces and back-rank Pawns
6608 if(nMine + stale == 1) return (pCnt[BlackFerz-side] > 1 && pCnt[BlackKnight-side] > 0); // bare K can stalemate KHAA (!)
6609 if(nMine > 2) return TRUE; // if we don't have P, H or R, we must have CC
6610 if(nMine == 2 && pCnt[WhiteCannon+side] == 0) return TRUE; // We have at least one P, H or R
6611 // if we get here, we must have KC... or KP..., possibly with additional A, E or last-rank P
6612 if(stale) // we have at least one last-rank P plus perhaps C
6613 return majors // KPKX
6614 || pCnt[BlackFerz-side] && pCnt[BlackFerz-side] + pCnt[WhiteCannon+side] + stale > 2; // KPKAA, KPPKA and KCPKA
6616 return pCnt[WhiteFerz+side] // KCAK
6617 || pCnt[WhiteAlfil+side] && pCnt[BlackRook-side] + pCnt[BlackCannon-side] + pCnt[BlackFerz-side] // KCEKA, KCEKX (X!=H)
6618 || majors + (12*pCnt[BlackFerz-side] | 6*pCnt[BlackAlfil-side]) > 16; // KCKAA, KCKAX, KCKEEX, KCKEXX (XX!=HH), KCKXXX
6619 // TO DO: cases wih an unpromoted f-Pawn acting as platform for an opponent Cannon
6621 } else if(pCnt[WhiteKing] == 1 && pCnt[BlackKing] == 1) { // other variants with orthodox Kings
6622 int nBishops = pCnt[WhiteBishop+side] + pCnt[WhiteFerz+side];
6624 if(nMine == 1) return FALSE; // bare King
6625 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
6626 nMine += (nBishops > 0) - nBishops; // By now all Bishops (and Ferz) on like-colored squares, so count as one
6627 if(nMine > 2 && nMine != pCnt[WhiteAlfil+side] + 1) return TRUE; // At least two pieces, not all Alfils
6628 // by now we have King + 1 piece (or multiple Bishops on the same color)
6629 if(pCnt[WhiteKnight+side])
6630 return (pCnt[BlackKnight-side] + pCnt[BlackBishop-side] + pCnt[BlackMan-side] +
6631 pCnt[BlackWazir-side] + pCnt[BlackSilver-side] + bisColor // KNKN, KNKB, KNKF, KNKE, KNKW, KNKM, KNKS
6632 || nHis > 3); // be sure to cover suffocation mates in corner (e.g. KNKQCA)
6634 return (pCnt[BlackKnight-side]); // KBKN, KFKN
6635 if(pCnt[WhiteAlfil+side])
6636 return (nHis > 2); // Alfils can in general not reach a corner square, but there might be edge (suffocation) mates
6637 if(pCnt[WhiteWazir+side])
6638 return (pCnt[BlackKnight-side] + pCnt[BlackWazir-side] + pCnt[BlackAlfil-side]); // KWKN, KWKW, KWKE
6645 Adjudicate(ChessProgramState *cps)
6646 { // [HGM] some adjudications useful with buggy engines
6647 // [HGM] adjudicate: made into separate routine, which now can be called after every move
6648 // In any case it determnes if the game is a claimable draw (filling in EP_STATUS).
6649 // Actually ending the game is now based on the additional internal condition canAdjudicate.
6650 // Only when the game is ended, and the opponent is a computer, this opponent gets the move relayed.
6651 int k, count = 0; static int bare = 1;
6652 ChessProgramState *engineOpponent = (gameMode == TwoMachinesPlay ? cps->other : (cps ? NULL : &first));
6653 Boolean canAdjudicate = !appData.icsActive;
6655 // most tests only when we understand the game, i.e. legality-checking on
6656 if( appData.testLegality )
6657 { /* [HGM] Some more adjudications for obstinate engines */
6658 int nrW, nrB, bishopColor, staleW, staleB, nr[EmptySquare+1], i;
6659 static int moveCount = 6;
6661 char *reason = NULL;
6663 /* Count what is on board. */
6664 Count(boards[forwardMostMove], nr, &nrW, &nrB, &staleW, &staleB, &bishopColor);
6666 /* Some material-based adjudications that have to be made before stalemate test */
6667 if(gameInfo.variant == VariantAtomic && nr[WhiteKing] + nr[BlackKing] < 2) {
6668 // [HGM] atomic: stm must have lost his King on previous move, as destroying own K is illegal
6669 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE; // make claimable as if stm is checkmated
6670 if(canAdjudicate && appData.checkMates) {
6672 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets move
6673 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6674 GameEnds( WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins,
6675 "Xboard adjudication: King destroyed", GE_XBOARD );
6680 /* Bare King in Shatranj (loses) or Losers (wins) */
6681 if( nrW == 1 || nrB == 1) {
6682 if( gameInfo.variant == VariantLosers) { // [HGM] losers: bare King wins (stm must have it first)
6683 boards[forwardMostMove][EP_STATUS] = EP_WINS; // mark as win, so it becomes claimable
6684 if(canAdjudicate && appData.checkMates) {
6686 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets to see move
6687 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6688 GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
6689 "Xboard adjudication: Bare king", GE_XBOARD );
6693 if( gameInfo.variant == VariantShatranj && --bare < 0)
6695 boards[forwardMostMove][EP_STATUS] = EP_WINS; // make claimable as win for stm
6696 if(canAdjudicate && appData.checkMates) {
6697 /* but only adjudicate if adjudication enabled */
6699 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets move
6700 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6701 GameEnds( nrW > 1 ? WhiteWins : nrB > 1 ? BlackWins : GameIsDrawn,
6702 "Xboard adjudication: Bare king", GE_XBOARD );
6709 // don't wait for engine to announce game end if we can judge ourselves
6710 switch (MateTest(boards[forwardMostMove], PosFlags(forwardMostMove)) ) {
6712 if(gameInfo.variant == Variant3Check) { // [HGM] 3check: when in check, test if 3rd time
6713 int i, checkCnt = 0; // (should really be done by making nr of checks part of game state)
6714 for(i=forwardMostMove-2; i>=backwardMostMove; i-=2) {
6715 if(MateTest(boards[i], PosFlags(i)) == MT_CHECK)
6718 reason = "Xboard adjudication: 3rd check";
6719 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE;
6729 reason = "Xboard adjudication: Stalemate";
6730 if((signed char)boards[forwardMostMove][EP_STATUS] != EP_CHECKMATE) { // [HGM] don't touch win through baring or K-capt
6731 boards[forwardMostMove][EP_STATUS] = EP_STALEMATE; // default result for stalemate is draw
6732 if(gameInfo.variant == VariantLosers || gameInfo.variant == VariantGiveaway) // [HGM] losers:
6733 boards[forwardMostMove][EP_STATUS] = EP_WINS; // in these variants stalemated is always a win
6734 else if(gameInfo.variant == VariantSuicide) // in suicide it depends
6735 boards[forwardMostMove][EP_STATUS] = nrW == nrB ? EP_STALEMATE :
6736 ((nrW < nrB) != WhiteOnMove(forwardMostMove) ?
6737 EP_CHECKMATE : EP_WINS);
6738 else if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantXiangqi)
6739 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE; // and in these variants being stalemated loses
6743 reason = "Xboard adjudication: Checkmate";
6744 boards[forwardMostMove][EP_STATUS] = (gameInfo.variant == VariantLosers ? EP_WINS : EP_CHECKMATE);
6748 switch(i = (signed char)boards[forwardMostMove][EP_STATUS]) {
6750 result = GameIsDrawn; break;
6752 result = WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins; break;
6754 result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins; break;
6756 result = (ChessMove) 0;
6758 if(canAdjudicate && appData.checkMates && result) { // [HGM] mates: adjudicate finished games if requested
6760 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
6761 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6762 GameEnds( result, reason, GE_XBOARD );
6766 /* Next absolutely insufficient mating material. */
6767 if(!MatingPotential(nr, WhitePawn, nrW, nrB, staleW, bishopColor) &&
6768 !MatingPotential(nr, BlackPawn, nrB, nrW, staleB, bishopColor))
6769 { /* includes KBK, KNK, KK of KBKB with like Bishops */
6771 /* always flag draws, for judging claims */
6772 boards[forwardMostMove][EP_STATUS] = EP_INSUF_DRAW;
6774 if(canAdjudicate && appData.materialDraws) {
6775 /* but only adjudicate them if adjudication enabled */
6776 if(engineOpponent) {
6777 SendToProgram("force\n", engineOpponent); // suppress reply
6778 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see last move */
6780 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6781 GameEnds( GameIsDrawn, "Xboard adjudication: Insufficient mating material", GE_XBOARD );
6786 /* Then some trivial draws (only adjudicate, cannot be claimed) */
6787 if(gameInfo.variant == VariantXiangqi ?
6788 SufficientDefence(nr, WhitePawn, nrW, nrB) && SufficientDefence(nr, BlackPawn, nrB, nrW)
6790 ( nr[WhiteRook] == 1 && nr[BlackRook] == 1 /* KRKR */
6791 || nr[WhiteQueen] && nr[BlackQueen]==1 /* KQKQ */
6792 || nr[WhiteKnight]==2 || nr[BlackKnight]==2 /* KNNK */
6793 || nr[WhiteKnight]+nr[WhiteBishop] == 1 && nr[BlackKnight]+nr[BlackBishop] == 1 /* KBKN, KBKB, KNKN */
6795 if(--moveCount < 0 && appData.trivialDraws && canAdjudicate)
6796 { /* if the first 3 moves do not show a tactical win, declare draw */
6797 if(engineOpponent) {
6798 SendToProgram("force\n", engineOpponent); // suppress reply
6799 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
6801 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6802 GameEnds( GameIsDrawn, "Xboard adjudication: Trivial draw", GE_XBOARD );
6805 } else moveCount = 6;
6808 if (appData.debugMode) { int i;
6809 fprintf(debugFP, "repeat test fmm=%d bmm=%d ep=%d, reps=%d\n",
6810 forwardMostMove, backwardMostMove, boards[backwardMostMove][EP_STATUS],
6811 appData.drawRepeats);
6812 for( i=forwardMostMove; i>=backwardMostMove; i-- )
6813 fprintf(debugFP, "%d ep=%d\n", i, (signed char)boards[i][EP_STATUS]);
6817 // Repetition draws and 50-move rule can be applied independently of legality testing
6819 /* Check for rep-draws */
6821 for(k = forwardMostMove-2;
6822 k>=backwardMostMove && k>=forwardMostMove-100 &&
6823 (signed char)boards[k][EP_STATUS] < EP_UNKNOWN &&
6824 (signed char)boards[k+2][EP_STATUS] <= EP_NONE && (signed char)boards[k+1][EP_STATUS] <= EP_NONE;
6827 if(CompareBoards(boards[k], boards[forwardMostMove])) {
6828 /* compare castling rights */
6829 if( boards[forwardMostMove][CASTLING][2] != boards[k][CASTLING][2] &&
6830 (boards[k][CASTLING][0] != NoRights || boards[k][CASTLING][1] != NoRights) )
6831 rights++; /* King lost rights, while rook still had them */
6832 if( boards[forwardMostMove][CASTLING][2] != NoRights ) { /* king has rights */
6833 if( boards[forwardMostMove][CASTLING][0] != boards[k][CASTLING][0] ||
6834 boards[forwardMostMove][CASTLING][1] != boards[k][CASTLING][1] )
6835 rights++; /* but at least one rook lost them */
6837 if( boards[forwardMostMove][CASTLING][5] != boards[k][CASTLING][5] &&
6838 (boards[k][CASTLING][3] != NoRights || boards[k][CASTLING][4] != NoRights) )
6840 if( boards[forwardMostMove][CASTLING][5] != NoRights ) {
6841 if( boards[forwardMostMove][CASTLING][3] != boards[k][CASTLING][3] ||
6842 boards[forwardMostMove][CASTLING][4] != boards[k][CASTLING][4] )
6845 if( rights == 0 && ++count > appData.drawRepeats-2 && canAdjudicate
6846 && appData.drawRepeats > 1) {
6847 /* adjudicate after user-specified nr of repeats */
6848 int result = GameIsDrawn;
6849 char *details = "XBoard adjudication: repetition draw";
6850 if(gameInfo.variant == VariantXiangqi && appData.testLegality) {
6851 // [HGM] xiangqi: check for forbidden perpetuals
6852 int m, ourPerpetual = 1, hisPerpetual = 1;
6853 for(m=forwardMostMove; m>k; m-=2) {
6854 if(MateTest(boards[m], PosFlags(m)) != MT_CHECK)
6855 ourPerpetual = 0; // the current mover did not always check
6856 if(MateTest(boards[m-1], PosFlags(m-1)) != MT_CHECK)
6857 hisPerpetual = 0; // the opponent did not always check
6859 if(appData.debugMode) fprintf(debugFP, "XQ perpetual test, our=%d, his=%d\n",
6860 ourPerpetual, hisPerpetual);
6861 if(ourPerpetual && !hisPerpetual) { // we are actively checking him: forfeit
6862 result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
6863 details = "Xboard adjudication: perpetual checking";
6865 if(hisPerpetual && !ourPerpetual) { // he is checking us, but did not repeat yet
6866 break; // (or we would have caught him before). Abort repetition-checking loop.
6868 // Now check for perpetual chases
6869 if(!ourPerpetual && !hisPerpetual) { // no perpetual check, test for chase
6870 hisPerpetual = PerpetualChase(k, forwardMostMove);
6871 ourPerpetual = PerpetualChase(k+1, forwardMostMove);
6872 if(ourPerpetual && !hisPerpetual) { // we are actively chasing him: forfeit
6873 result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
6874 details = "Xboard adjudication: perpetual chasing";
6876 if(hisPerpetual && !ourPerpetual) // he is chasing us, but did not repeat yet
6877 break; // Abort repetition-checking loop.
6879 // if neither of us is checking or chasing all the time, or both are, it is draw
6881 if(engineOpponent) {
6882 SendToProgram("force\n", engineOpponent); // suppress reply
6883 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
6885 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6886 GameEnds( result, details, GE_XBOARD );
6889 if( rights == 0 && count > 1 ) /* occurred 2 or more times before */
6890 boards[forwardMostMove][EP_STATUS] = EP_REP_DRAW;
6894 /* Now we test for 50-move draws. Determine ply count */
6895 count = forwardMostMove;
6896 /* look for last irreversble move */
6897 while( (signed char)boards[count][EP_STATUS] <= EP_NONE && count > backwardMostMove )
6899 /* if we hit starting position, add initial plies */
6900 if( count == backwardMostMove )
6901 count -= initialRulePlies;
6902 count = forwardMostMove - count;
6903 if(gameInfo.variant == VariantXiangqi && ( count >= 100 || count >= 2*appData.ruleMoves ) ) {
6904 // adjust reversible move counter for checks in Xiangqi
6905 int i = forwardMostMove - count, inCheck = 0, lastCheck;
6906 if(i < backwardMostMove) i = backwardMostMove;
6907 while(i <= forwardMostMove) {
6908 lastCheck = inCheck; // check evasion does not count
6909 inCheck = (MateTest(boards[i], PosFlags(i)) == MT_CHECK);
6910 if(inCheck || lastCheck) count--; // check does not count
6915 boards[forwardMostMove][EP_STATUS] = EP_RULE_DRAW;
6916 /* this is used to judge if draw claims are legal */
6917 if(canAdjudicate && appData.ruleMoves > 0 && count >= 2*appData.ruleMoves) {
6918 if(engineOpponent) {
6919 SendToProgram("force\n", engineOpponent); // suppress reply
6920 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
6922 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6923 GameEnds( GameIsDrawn, "Xboard adjudication: 50-move rule", GE_XBOARD );
6927 /* if draw offer is pending, treat it as a draw claim
6928 * when draw condition present, to allow engines a way to
6929 * claim draws before making their move to avoid a race
6930 * condition occurring after their move
6932 if((gameMode == TwoMachinesPlay ? second.offeredDraw : userOfferedDraw) || first.offeredDraw ) {
6934 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_RULE_DRAW)
6935 p = "Draw claim: 50-move rule";
6936 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_REP_DRAW)
6937 p = "Draw claim: 3-fold repetition";
6938 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_INSUF_DRAW)
6939 p = "Draw claim: insufficient mating material";
6940 if( p != NULL && canAdjudicate) {
6941 if(engineOpponent) {
6942 SendToProgram("force\n", engineOpponent); // suppress reply
6943 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
6945 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6946 GameEnds( GameIsDrawn, p, GE_XBOARD );
6951 if( canAdjudicate && appData.adjudicateDrawMoves > 0 && forwardMostMove > (2*appData.adjudicateDrawMoves) ) {
6952 if(engineOpponent) {
6953 SendToProgram("force\n", engineOpponent); // suppress reply
6954 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
6956 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6957 GameEnds( GameIsDrawn, "Xboard adjudication: long game", GE_XBOARD );
6963 char *SendMoveToBookUser(int moveNr, ChessProgramState *cps, int initial)
6964 { // [HGM] book: this routine intercepts moves to simulate book replies
6965 char *bookHit = NULL;
6967 //first determine if the incoming move brings opponent into his book
6968 if(appData.usePolyglotBook && (cps == &first ? !appData.firstHasOwnBookUCI : !appData.secondHasOwnBookUCI))
6969 bookHit = ProbeBook(moveNr+1, appData.polyglotBook); // returns move
6970 if(appData.debugMode) fprintf(debugFP, "book hit = %s\n", bookHit ? bookHit : "(NULL)");
6971 if(bookHit != NULL && !cps->bookSuspend) {
6972 // make sure opponent is not going to reply after receiving move to book position
6973 SendToProgram("force\n", cps);
6974 cps->bookSuspend = TRUE; // flag indicating it has to be restarted
6976 if(!initial) SendMoveToProgram(moveNr, cps); // with hit on initial position there is no move
6977 // now arrange restart after book miss
6979 // after a book hit we never send 'go', and the code after the call to this routine
6980 // has '&& !bookHit' added to suppress potential sending there (based on 'firstMove').
6982 sprintf(buf, "%s%s\n", (cps->useUsermove ? "usermove " : ""), bookHit); // force book move into program supposed to play it
6983 SendToProgram(buf, cps);
6984 if(!initial) firstMove = FALSE; // normally we would clear the firstMove condition after return & sending 'go'
6985 } else if(initial) { // 'go' was needed irrespective of firstMove, and it has to be done in this routine
6986 SendToProgram("go\n", cps);
6987 cps->bookSuspend = FALSE; // after a 'go' we are never suspended
6988 } else { // 'go' might be sent based on 'firstMove' after this routine returns
6989 if(cps->bookSuspend && !firstMove) // 'go' needed, and it will not be done after we return
6990 SendToProgram("go\n", cps);
6991 cps->bookSuspend = FALSE; // anyhow, we will not be suspended after a miss
6993 return bookHit; // notify caller of hit, so it can take action to send move to opponent
6997 ChessProgramState *savedState;
6998 void DeferredBookMove(void)
7000 if(savedState->lastPing != savedState->lastPong)
7001 ScheduleDelayedEvent(DeferredBookMove, 10);
7003 HandleMachineMove(savedMessage, savedState);
7007 HandleMachineMove(message, cps)
7009 ChessProgramState *cps;
7011 char machineMove[MSG_SIZ], buf1[MSG_SIZ*10], buf2[MSG_SIZ];
7012 char realname[MSG_SIZ];
7013 int fromX, fromY, toX, toY;
7022 FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book hit
7024 * Kludge to ignore BEL characters
7026 while (*message == '\007') message++;
7029 * [HGM] engine debug message: ignore lines starting with '#' character
7031 if(cps->debug && *message == '#') return;
7034 * Look for book output
7036 if (cps == &first && bookRequested) {
7037 if (message[0] == '\t' || message[0] == ' ') {
7038 /* Part of the book output is here; append it */
7039 strcat(bookOutput, message);
7040 strcat(bookOutput, " \n");
7042 } else if (bookOutput[0] != NULLCHAR) {
7043 /* All of book output has arrived; display it */
7044 char *p = bookOutput;
7045 while (*p != NULLCHAR) {
7046 if (*p == '\t') *p = ' ';
7049 DisplayInformation(bookOutput);
7050 bookRequested = FALSE;
7051 /* Fall through to parse the current output */
7056 * Look for machine move.
7058 if ((sscanf(message, "%s %s %s", buf1, buf2, machineMove) == 3 && strcmp(buf2, "...") == 0) ||
7059 (sscanf(message, "%s %s", buf1, machineMove) == 2 && strcmp(buf1, "move") == 0))
7061 /* This method is only useful on engines that support ping */
7062 if (cps->lastPing != cps->lastPong) {
7063 if (gameMode == BeginningOfGame) {
7064 /* Extra move from before last new; ignore */
7065 if (appData.debugMode) {
7066 fprintf(debugFP, "Ignoring extra move from %s\n", cps->which);
7069 if (appData.debugMode) {
7070 fprintf(debugFP, "Undoing extra move from %s, gameMode %d\n",
7071 cps->which, gameMode);
7074 SendToProgram("undo\n", cps);
7080 case BeginningOfGame:
7081 /* Extra move from before last reset; ignore */
7082 if (appData.debugMode) {
7083 fprintf(debugFP, "Ignoring extra move from %s\n", cps->which);
7090 /* Extra move after we tried to stop. The mode test is
7091 not a reliable way of detecting this problem, but it's
7092 the best we can do on engines that don't support ping.
7094 if (appData.debugMode) {
7095 fprintf(debugFP, "Undoing extra move from %s, gameMode %d\n",
7096 cps->which, gameMode);
7098 SendToProgram("undo\n", cps);
7101 case MachinePlaysWhite:
7102 case IcsPlayingWhite:
7103 machineWhite = TRUE;
7106 case MachinePlaysBlack:
7107 case IcsPlayingBlack:
7108 machineWhite = FALSE;
7111 case TwoMachinesPlay:
7112 machineWhite = (cps->twoMachinesColor[0] == 'w');
7115 if (WhiteOnMove(forwardMostMove) != machineWhite) {
7116 if (appData.debugMode) {
7118 "Ignoring move out of turn by %s, gameMode %d"
7119 ", forwardMost %d\n",
7120 cps->which, gameMode, forwardMostMove);
7125 if (appData.debugMode) { int f = forwardMostMove;
7126 fprintf(debugFP, "machine move %d, castling = %d %d %d %d %d %d\n", f,
7127 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
7128 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
7130 if(cps->alphaRank) AlphaRank(machineMove, 4);
7131 if (!ParseOneMove(machineMove, forwardMostMove, &moveType,
7132 &fromX, &fromY, &toX, &toY, &promoChar)) {
7133 /* Machine move could not be parsed; ignore it. */
7134 sprintf(buf1, _("Illegal move \"%s\" from %s machine"),
7135 machineMove, cps->which);
7136 DisplayError(buf1, 0);
7137 sprintf(buf1, "Xboard: Forfeit due to invalid move: %s (%c%c%c%c) res=%d",
7138 machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, moveType);
7139 if (gameMode == TwoMachinesPlay) {
7140 GameEnds(machineWhite ? BlackWins : WhiteWins,
7146 /* [HGM] Apparently legal, but so far only tested with EP_UNKOWN */
7147 /* So we have to redo legality test with true e.p. status here, */
7148 /* to make sure an illegal e.p. capture does not slip through, */
7149 /* to cause a forfeit on a justified illegal-move complaint */
7150 /* of the opponent. */
7151 if( gameMode==TwoMachinesPlay && appData.testLegality ) {
7153 moveType = LegalityTest(boards[forwardMostMove], PosFlags(forwardMostMove),
7154 fromY, fromX, toY, toX, promoChar);
7155 if (appData.debugMode) {
7157 for(i=0; i< nrCastlingRights; i++) fprintf(debugFP, "(%d,%d) ",
7158 boards[forwardMostMove][CASTLING][i], castlingRank[i]);
7159 fprintf(debugFP, "castling rights\n");
7161 if(moveType == IllegalMove) {
7162 sprintf(buf1, "Xboard: Forfeit due to illegal move: %s (%c%c%c%c)%c",
7163 machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, 0);
7164 GameEnds(machineWhite ? BlackWins : WhiteWins,
7167 } else if(gameInfo.variant != VariantFischeRandom && gameInfo.variant != VariantCapaRandom)
7168 /* [HGM] Kludge to handle engines that send FRC-style castling
7169 when they shouldn't (like TSCP-Gothic) */
7171 case WhiteASideCastleFR:
7172 case BlackASideCastleFR:
7174 currentMoveString[2]++;
7176 case WhiteHSideCastleFR:
7177 case BlackHSideCastleFR:
7179 currentMoveString[2]--;
7181 default: ; // nothing to do, but suppresses warning of pedantic compilers
7184 hintRequested = FALSE;
7185 lastHint[0] = NULLCHAR;
7186 bookRequested = FALSE;
7187 /* Program may be pondering now */
7188 cps->maybeThinking = TRUE;
7189 if (cps->sendTime == 2) cps->sendTime = 1;
7190 if (cps->offeredDraw) cps->offeredDraw--;
7192 /* currentMoveString is set as a side-effect of ParseOneMove */
7193 strcpy(machineMove, currentMoveString);
7194 strcat(machineMove, "\n");
7195 strcpy(moveList[forwardMostMove], machineMove);
7197 /* [AS] Save move info*/
7198 pvInfoList[ forwardMostMove ].score = programStats.score;
7199 pvInfoList[ forwardMostMove ].depth = programStats.depth;
7200 pvInfoList[ forwardMostMove ].time = programStats.time; // [HGM] PGNtime: take time from engine stats
7202 MakeMove(fromX, fromY, toX, toY, promoChar);/*updates forwardMostMove*/
7204 /* [AS] Adjudicate game if needed (note: remember that forwardMostMove now points past the last move) */
7205 if( gameMode == TwoMachinesPlay && adjudicateLossThreshold != 0 && forwardMostMove >= adjudicateLossPlies ) {
7208 while( count < adjudicateLossPlies ) {
7209 int score = pvInfoList[ forwardMostMove - count - 1 ].score;
7212 score = -score; /* Flip score for winning side */
7215 if( score > adjudicateLossThreshold ) {
7222 if( count >= adjudicateLossPlies ) {
7223 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
7225 GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
7226 "Xboard adjudication",
7233 if(Adjudicate(cps)) return; // [HGM] adjudicate: for all automatic game ends
7236 if ((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack) &&
7238 if(cps->offeredDraw && (signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
7239 SendToICS(ics_prefix); // [HGM] drawclaim: send caim and move on one line for FICS
7241 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
7243 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
7245 if(appData.autoKibitz && !appData.icsEngineAnalyze ) { /* [HGM] kibitz: send most-recent PV info to ICS */
7246 char buf[3*MSG_SIZ];
7248 sprintf(buf, "kibitz !!! %+.2f/%d (%.2f sec, %u nodes, %.0f knps) PV=%s\n",
7249 programStats.score / 100.,
7251 programStats.time / 100.,
7252 (unsigned int)programStats.nodes,
7253 (unsigned int)programStats.nodes / (10*abs(programStats.time) + 1.),
7254 programStats.movelist);
7256 if(appData.debugMode) fprintf(debugFP, "nodes = %d, %lld\n", (int) programStats.nodes, programStats.nodes);
7261 /* [AS] Clear stats for next move */
7262 ClearProgramStats();
7263 thinkOutput[0] = NULLCHAR;
7264 hiddenThinkOutputState = 0;
7267 if (gameMode == TwoMachinesPlay) {
7268 /* [HGM] relaying draw offers moved to after reception of move */
7269 /* and interpreting offer as claim if it brings draw condition */
7270 if (cps->offeredDraw == 1 && cps->other->sendDrawOffers) {
7271 SendToProgram("draw\n", cps->other);
7273 if (cps->other->sendTime) {
7274 SendTimeRemaining(cps->other,
7275 cps->other->twoMachinesColor[0] == 'w');
7277 bookHit = SendMoveToBookUser(forwardMostMove-1, cps->other, FALSE);
7278 if (firstMove && !bookHit) {
7280 if (cps->other->useColors) {
7281 SendToProgram(cps->other->twoMachinesColor, cps->other);
7283 SendToProgram("go\n", cps->other);
7285 cps->other->maybeThinking = TRUE;
7288 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
7290 if (!pausing && appData.ringBellAfterMoves) {
7295 * Reenable menu items that were disabled while
7296 * machine was thinking
7298 if (gameMode != TwoMachinesPlay)
7299 SetUserThinkingEnables();
7301 // [HGM] book: after book hit opponent has received move and is now in force mode
7302 // force the book reply into it, and then fake that it outputted this move by jumping
7303 // back to the beginning of HandleMachineMove, with cps toggled and message set to this move
7305 static char bookMove[MSG_SIZ]; // a bit generous?
7307 strcpy(bookMove, "move ");
7308 strcat(bookMove, bookHit);
7311 programStats.nodes = programStats.depth = programStats.time =
7312 programStats.score = programStats.got_only_move = 0;
7313 sprintf(programStats.movelist, "%s (xbook)", bookHit);
7315 if(cps->lastPing != cps->lastPong) {
7316 savedMessage = message; // args for deferred call
7318 ScheduleDelayedEvent(DeferredBookMove, 10);
7327 /* Set special modes for chess engines. Later something general
7328 * could be added here; for now there is just one kludge feature,
7329 * needed because Crafty 15.10 and earlier don't ignore SIGINT
7330 * when "xboard" is given as an interactive command.
7332 if (strncmp(message, "kibitz Hello from Crafty", 24) == 0) {
7333 cps->useSigint = FALSE;
7334 cps->useSigterm = FALSE;
7336 if (strncmp(message, "feature ", 8) == 0) { // [HGM] moved forward to pre-empt non-compliant commands
7337 ParseFeatures(message+8, cps);
7338 return; // [HGM] This return was missing, causing option features to be recognized as non-compliant commands!
7341 /* [HGM] Allow engine to set up a position. Don't ask me why one would
7342 * want this, I was asked to put it in, and obliged.
7344 if (!strncmp(message, "setboard ", 9)) {
7345 Board initial_position;
7347 GameEnds(GameUnfinished, "Engine aborts game", GE_XBOARD);
7349 if (!ParseFEN(initial_position, &blackPlaysFirst, message + 9)) {
7350 DisplayError(_("Bad FEN received from engine"), 0);
7354 CopyBoard(boards[0], initial_position);
7355 initialRulePlies = FENrulePlies;
7356 if(blackPlaysFirst) gameMode = MachinePlaysWhite;
7357 else gameMode = MachinePlaysBlack;
7358 DrawPosition(FALSE, boards[currentMove]);
7364 * Look for communication commands
7366 if (!strncmp(message, "telluser ", 9)) {
7367 EscapeExpand(message+9, message+9); // [HGM] esc: allow escape sequences in popup box
7368 DisplayNote(message + 9);
7371 if (!strncmp(message, "tellusererror ", 14)) {
7373 EscapeExpand(message+14, message+14); // [HGM] esc: allow escape sequences in popup box
7374 DisplayError(message + 14, 0);
7377 if (!strncmp(message, "tellopponent ", 13)) {
7378 if (appData.icsActive) {
7380 snprintf(buf1, sizeof(buf1), "%ssay %s\n", ics_prefix, message + 13);
7384 DisplayNote(message + 13);
7388 if (!strncmp(message, "tellothers ", 11)) {
7389 if (appData.icsActive) {
7391 snprintf(buf1, sizeof(buf1), "%swhisper %s\n", ics_prefix, message + 11);
7397 if (!strncmp(message, "tellall ", 8)) {
7398 if (appData.icsActive) {
7400 snprintf(buf1, sizeof(buf1), "%skibitz %s\n", ics_prefix, message + 8);
7404 DisplayNote(message + 8);
7408 if (strncmp(message, "warning", 7) == 0) {
7409 /* Undocumented feature, use tellusererror in new code */
7410 DisplayError(message, 0);
7413 if (sscanf(message, "askuser %s %[^\n]", buf1, buf2) == 2) {
7414 strcpy(realname, cps->tidy);
7415 strcat(realname, " query");
7416 AskQuestion(realname, buf2, buf1, cps->pr);
7419 /* Commands from the engine directly to ICS. We don't allow these to be
7420 * sent until we are logged on. Crafty kibitzes have been known to
7421 * interfere with the login process.
7424 if (!strncmp(message, "tellics ", 8)) {
7425 SendToICS(message + 8);
7429 if (!strncmp(message, "tellicsnoalias ", 15)) {
7430 SendToICS(ics_prefix);
7431 SendToICS(message + 15);
7435 /* The following are for backward compatibility only */
7436 if (!strncmp(message,"whisper",7) || !strncmp(message,"kibitz",6) ||
7437 !strncmp(message,"draw",4) || !strncmp(message,"tell",3)) {
7438 SendToICS(ics_prefix);
7444 if (sscanf(message, "pong %d", &cps->lastPong) == 1) {
7448 * If the move is illegal, cancel it and redraw the board.
7449 * Also deal with other error cases. Matching is rather loose
7450 * here to accommodate engines written before the spec.
7452 if (strncmp(message + 1, "llegal move", 11) == 0 ||
7453 strncmp(message, "Error", 5) == 0) {
7454 if (StrStr(message, "name") ||
7455 StrStr(message, "rating") || StrStr(message, "?") ||
7456 StrStr(message, "result") || StrStr(message, "board") ||
7457 StrStr(message, "bk") || StrStr(message, "computer") ||
7458 StrStr(message, "variant") || StrStr(message, "hint") ||
7459 StrStr(message, "random") || StrStr(message, "depth") ||
7460 StrStr(message, "accepted")) {
7463 if (StrStr(message, "protover")) {
7464 /* Program is responding to input, so it's apparently done
7465 initializing, and this error message indicates it is
7466 protocol version 1. So we don't need to wait any longer
7467 for it to initialize and send feature commands. */
7468 FeatureDone(cps, 1);
7469 cps->protocolVersion = 1;
7472 cps->maybeThinking = FALSE;
7474 if (StrStr(message, "draw")) {
7475 /* Program doesn't have "draw" command */
7476 cps->sendDrawOffers = 0;
7479 if (cps->sendTime != 1 &&
7480 (StrStr(message, "time") || StrStr(message, "otim"))) {
7481 /* Program apparently doesn't have "time" or "otim" command */
7485 if (StrStr(message, "analyze")) {
7486 cps->analysisSupport = FALSE;
7487 cps->analyzing = FALSE;
7489 sprintf(buf2, _("%s does not support analysis"), cps->tidy);
7490 DisplayError(buf2, 0);
7493 if (StrStr(message, "(no matching move)st")) {
7494 /* Special kludge for GNU Chess 4 only */
7495 cps->stKludge = TRUE;
7496 SendTimeControl(cps, movesPerSession, timeControl,
7497 timeIncrement, appData.searchDepth,
7501 if (StrStr(message, "(no matching move)sd")) {
7502 /* Special kludge for GNU Chess 4 only */
7503 cps->sdKludge = TRUE;
7504 SendTimeControl(cps, movesPerSession, timeControl,
7505 timeIncrement, appData.searchDepth,
7509 if (!StrStr(message, "llegal")) {
7512 if (gameMode == BeginningOfGame || gameMode == EndOfGame ||
7513 gameMode == IcsIdle) return;
7514 if (forwardMostMove <= backwardMostMove) return;
7515 if (pausing) PauseEvent();
7516 if(appData.forceIllegal) {
7517 // [HGM] illegal: machine refused move; force position after move into it
7518 SendToProgram("force\n", cps);
7519 if(!cps->useSetboard) { // hideous kludge on kludge, because SendBoard sucks.
7520 // we have a real problem now, as SendBoard will use the a2a3 kludge
7521 // when black is to move, while there might be nothing on a2 or black
7522 // might already have the move. So send the board as if white has the move.
7523 // But first we must change the stm of the engine, as it refused the last move
7524 SendBoard(cps, 0); // always kludgeless, as white is to move on boards[0]
7525 if(WhiteOnMove(forwardMostMove)) {
7526 SendToProgram("a7a6\n", cps); // for the engine black still had the move
7527 SendBoard(cps, forwardMostMove); // kludgeless board
7529 SendToProgram("a2a3\n", cps); // for the engine white still had the move
7530 CopyBoard(boards[forwardMostMove+1], boards[forwardMostMove]);
7531 SendBoard(cps, forwardMostMove+1); // kludgeless board
7533 } else SendBoard(cps, forwardMostMove); // FEN case, also sets stm properly
7534 if(gameMode == MachinePlaysWhite || gameMode == MachinePlaysBlack ||
7535 gameMode == TwoMachinesPlay)
7536 SendToProgram("go\n", cps);
7539 if (gameMode == PlayFromGameFile) {
7540 /* Stop reading this game file */
7541 gameMode = EditGame;
7544 currentMove = forwardMostMove-1;
7545 DisplayMove(currentMove-1); /* before DisplayMoveError */
7546 SwitchClocks(forwardMostMove-1); // [HGM] race
7547 DisplayBothClocks();
7548 sprintf(buf1, _("Illegal move \"%s\" (rejected by %s chess program)"),
7549 parseList[currentMove], cps->which);
7550 DisplayMoveError(buf1);
7551 DrawPosition(FALSE, boards[currentMove]);
7553 /* [HGM] illegal-move claim should forfeit game when Xboard */
7554 /* only passes fully legal moves */
7555 if( appData.testLegality && gameMode == TwoMachinesPlay ) {
7556 GameEnds( cps->twoMachinesColor[0] == 'w' ? BlackWins : WhiteWins,
7557 "False illegal-move claim", GE_XBOARD );
7561 if (strncmp(message, "time", 4) == 0 && StrStr(message, "Illegal")) {
7562 /* Program has a broken "time" command that
7563 outputs a string not ending in newline.
7569 * If chess program startup fails, exit with an error message.
7570 * Attempts to recover here are futile.
7572 if ((StrStr(message, "unknown host") != NULL)
7573 || (StrStr(message, "No remote directory") != NULL)
7574 || (StrStr(message, "not found") != NULL)
7575 || (StrStr(message, "No such file") != NULL)
7576 || (StrStr(message, "can't alloc") != NULL)
7577 || (StrStr(message, "Permission denied") != NULL)) {
7579 cps->maybeThinking = FALSE;
7580 snprintf(buf1, sizeof(buf1), _("Failed to start %s chess program %s on %s: %s\n"),
7581 cps->which, cps->program, cps->host, message);
7582 RemoveInputSource(cps->isr);
7583 DisplayFatalError(buf1, 0, 1);
7588 * Look for hint output
7590 if (sscanf(message, "Hint: %s", buf1) == 1) {
7591 if (cps == &first && hintRequested) {
7592 hintRequested = FALSE;
7593 if (ParseOneMove(buf1, forwardMostMove, &moveType,
7594 &fromX, &fromY, &toX, &toY, &promoChar)) {
7595 (void) CoordsToAlgebraic(boards[forwardMostMove],
7596 PosFlags(forwardMostMove),
7597 fromY, fromX, toY, toX, promoChar, buf1);
7598 snprintf(buf2, sizeof(buf2), _("Hint: %s"), buf1);
7599 DisplayInformation(buf2);
7601 /* Hint move could not be parsed!? */
7602 snprintf(buf2, sizeof(buf2),
7603 _("Illegal hint move \"%s\"\nfrom %s chess program"),
7605 DisplayError(buf2, 0);
7608 strcpy(lastHint, buf1);
7614 * Ignore other messages if game is not in progress
7616 if (gameMode == BeginningOfGame || gameMode == EndOfGame ||
7617 gameMode == IcsIdle || cps->lastPing != cps->lastPong) return;
7620 * look for win, lose, draw, or draw offer
7622 if (strncmp(message, "1-0", 3) == 0) {
7623 char *p, *q, *r = "";
7624 p = strchr(message, '{');
7632 GameEnds(WhiteWins, r, GE_ENGINE1 + (cps != &first)); /* [HGM] pass claimer indication for claim test */
7634 } else if (strncmp(message, "0-1", 3) == 0) {
7635 char *p, *q, *r = "";
7636 p = strchr(message, '{');
7644 /* Kludge for Arasan 4.1 bug */
7645 if (strcmp(r, "Black resigns") == 0) {
7646 GameEnds(WhiteWins, r, GE_ENGINE1 + (cps != &first));
7649 GameEnds(BlackWins, r, GE_ENGINE1 + (cps != &first));
7651 } else if (strncmp(message, "1/2", 3) == 0) {
7652 char *p, *q, *r = "";
7653 p = strchr(message, '{');
7662 GameEnds(GameIsDrawn, r, GE_ENGINE1 + (cps != &first));
7665 } else if (strncmp(message, "White resign", 12) == 0) {
7666 GameEnds(BlackWins, "White resigns", GE_ENGINE1 + (cps != &first));
7668 } else if (strncmp(message, "Black resign", 12) == 0) {
7669 GameEnds(WhiteWins, "Black resigns", GE_ENGINE1 + (cps != &first));
7671 } else if (strncmp(message, "White matches", 13) == 0 ||
7672 strncmp(message, "Black matches", 13) == 0 ) {
7673 /* [HGM] ignore GNUShogi noises */
7675 } else if (strncmp(message, "White", 5) == 0 &&
7676 message[5] != '(' &&
7677 StrStr(message, "Black") == NULL) {
7678 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
7680 } else if (strncmp(message, "Black", 5) == 0 &&
7681 message[5] != '(') {
7682 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
7684 } else if (strcmp(message, "resign") == 0 ||
7685 strcmp(message, "computer resigns") == 0) {
7687 case MachinePlaysBlack:
7688 case IcsPlayingBlack:
7689 GameEnds(WhiteWins, "Black resigns", GE_ENGINE);
7691 case MachinePlaysWhite:
7692 case IcsPlayingWhite:
7693 GameEnds(BlackWins, "White resigns", GE_ENGINE);
7695 case TwoMachinesPlay:
7696 if (cps->twoMachinesColor[0] == 'w')
7697 GameEnds(BlackWins, "White resigns", GE_ENGINE1 + (cps != &first));
7699 GameEnds(WhiteWins, "Black resigns", GE_ENGINE1 + (cps != &first));
7706 } else if (strncmp(message, "opponent mates", 14) == 0) {
7708 case MachinePlaysBlack:
7709 case IcsPlayingBlack:
7710 GameEnds(WhiteWins, "White mates", GE_ENGINE);
7712 case MachinePlaysWhite:
7713 case IcsPlayingWhite:
7714 GameEnds(BlackWins, "Black mates", GE_ENGINE);
7716 case TwoMachinesPlay:
7717 if (cps->twoMachinesColor[0] == 'w')
7718 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
7720 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
7727 } else if (strncmp(message, "computer mates", 14) == 0) {
7729 case MachinePlaysBlack:
7730 case IcsPlayingBlack:
7731 GameEnds(BlackWins, "Black mates", GE_ENGINE1);
7733 case MachinePlaysWhite:
7734 case IcsPlayingWhite:
7735 GameEnds(WhiteWins, "White mates", GE_ENGINE);
7737 case TwoMachinesPlay:
7738 if (cps->twoMachinesColor[0] == 'w')
7739 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
7741 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
7748 } else if (strncmp(message, "checkmate", 9) == 0) {
7749 if (WhiteOnMove(forwardMostMove)) {
7750 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
7752 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
7755 } else if (strstr(message, "Draw") != NULL ||
7756 strstr(message, "game is a draw") != NULL) {
7757 GameEnds(GameIsDrawn, "Draw", GE_ENGINE1 + (cps != &first));
7759 } else if (strstr(message, "offer") != NULL &&
7760 strstr(message, "draw") != NULL) {
7762 if (appData.zippyPlay && first.initDone) {
7763 /* Relay offer to ICS */
7764 SendToICS(ics_prefix);
7765 SendToICS("draw\n");
7768 cps->offeredDraw = 2; /* valid until this engine moves twice */
7769 if (gameMode == TwoMachinesPlay) {
7770 if (cps->other->offeredDraw) {
7771 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
7772 /* [HGM] in two-machine mode we delay relaying draw offer */
7773 /* until after we also have move, to see if it is really claim */
7775 } else if (gameMode == MachinePlaysWhite ||
7776 gameMode == MachinePlaysBlack) {
7777 if (userOfferedDraw) {
7778 DisplayInformation(_("Machine accepts your draw offer"));
7779 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
7781 DisplayInformation(_("Machine offers a draw\nSelect Action / Draw to agree"));
7788 * Look for thinking output
7790 if ( appData.showThinking // [HGM] thinking: test all options that cause this output
7791 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp()
7793 int plylev, mvleft, mvtot, curscore, time;
7794 char mvname[MOVE_LEN];
7798 int prefixHint = FALSE;
7799 mvname[0] = NULLCHAR;
7802 case MachinePlaysBlack:
7803 case IcsPlayingBlack:
7804 if (WhiteOnMove(forwardMostMove)) prefixHint = TRUE;
7806 case MachinePlaysWhite:
7807 case IcsPlayingWhite:
7808 if (!WhiteOnMove(forwardMostMove)) prefixHint = TRUE;
7813 case IcsObserving: /* [DM] icsEngineAnalyze */
7814 if (!appData.icsEngineAnalyze) ignore = TRUE;
7816 case TwoMachinesPlay:
7817 if ((cps->twoMachinesColor[0] == 'w') != WhiteOnMove(forwardMostMove)) {
7827 ChessProgramStats tempStats = programStats; // [HGM] info: filter out info lines
7829 if (sscanf(message, "%d%c %d %d " u64Display " %[^\n]\n",
7830 &plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5) {
7832 if (plyext != ' ' && plyext != '\t') {
7836 /* [AS] Negate score if machine is playing black and reporting absolute scores */
7837 if( cps->scoreIsAbsolute &&
7838 ( gameMode == MachinePlaysBlack ||
7839 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b' ||
7840 gameMode == IcsPlayingBlack || // [HGM] also add other situations where engine should report black POV
7841 (gameMode == AnalyzeMode || gameMode == AnalyzeFile || gameMode == IcsObserving && appData.icsEngineAnalyze) &&
7842 !WhiteOnMove(currentMove)
7845 curscore = -curscore;
7849 tempStats.depth = plylev;
7850 tempStats.nodes = nodes;
7851 tempStats.time = time;
7852 tempStats.score = curscore;
7853 tempStats.got_only_move = 0;
7855 if(cps->nps >= 0) { /* [HGM] nps: use engine nodes or time to decrement clock */
7858 if(cps->nps == 0) ticklen = 10*time; // use engine reported time
7859 else ticklen = (1000. * u64ToDouble(nodes)) / cps->nps; // convert node count to time
7860 if(WhiteOnMove(forwardMostMove) && (gameMode == MachinePlaysWhite ||
7861 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'w'))
7862 whiteTimeRemaining = timeRemaining[0][forwardMostMove] - ticklen;
7863 if(!WhiteOnMove(forwardMostMove) && (gameMode == MachinePlaysBlack ||
7864 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b'))
7865 blackTimeRemaining = timeRemaining[1][forwardMostMove] - ticklen;
7868 /* Buffer overflow protection */
7869 if (buf1[0] != NULLCHAR) {
7870 if (strlen(buf1) >= sizeof(tempStats.movelist)
7871 && appData.debugMode) {
7873 "PV is too long; using the first %u bytes.\n",
7874 (unsigned) sizeof(tempStats.movelist) - 1);
7877 safeStrCpy( tempStats.movelist, buf1, sizeof(tempStats.movelist) );
7879 sprintf(tempStats.movelist, " no PV\n");
7882 if (tempStats.seen_stat) {
7883 tempStats.ok_to_send = 1;
7886 if (strchr(tempStats.movelist, '(') != NULL) {
7887 tempStats.line_is_book = 1;
7888 tempStats.nr_moves = 0;
7889 tempStats.moves_left = 0;
7891 tempStats.line_is_book = 0;
7894 if(tempStats.score != 0 || tempStats.nodes != 0 || tempStats.time != 0)
7895 programStats = tempStats; // [HGM] info: only set stats if genuine PV and not an info line
7897 SendProgramStatsToFrontend( cps, &tempStats );
7900 [AS] Protect the thinkOutput buffer from overflow... this
7901 is only useful if buf1 hasn't overflowed first!
7903 sprintf(thinkOutput, "[%d]%c%+.2f %s%s",
7905 (gameMode == TwoMachinesPlay ?
7906 ToUpper(cps->twoMachinesColor[0]) : ' '),
7907 ((double) curscore) / 100.0,
7908 prefixHint ? lastHint : "",
7909 prefixHint ? " " : "" );
7911 if( buf1[0] != NULLCHAR ) {
7912 unsigned max_len = sizeof(thinkOutput) - strlen(thinkOutput) - 1;
7914 if( strlen(buf1) > max_len ) {
7915 if( appData.debugMode) {
7916 fprintf(debugFP,"PV is too long for thinkOutput, truncating.\n");
7918 buf1[max_len+1] = '\0';
7921 strcat( thinkOutput, buf1 );
7924 if (currentMove == forwardMostMove || gameMode == AnalyzeMode
7925 || gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
7926 DisplayMove(currentMove - 1);
7930 } else if ((p=StrStr(message, "(only move)")) != NULL) {
7931 /* crafty (9.25+) says "(only move) <move>"
7932 * if there is only 1 legal move
7934 sscanf(p, "(only move) %s", buf1);
7935 sprintf(thinkOutput, "%s (only move)", buf1);
7936 sprintf(programStats.movelist, "%s (only move)", buf1);
7937 programStats.depth = 1;
7938 programStats.nr_moves = 1;
7939 programStats.moves_left = 1;
7940 programStats.nodes = 1;
7941 programStats.time = 1;
7942 programStats.got_only_move = 1;
7944 /* Not really, but we also use this member to
7945 mean "line isn't going to change" (Crafty
7946 isn't searching, so stats won't change) */
7947 programStats.line_is_book = 1;
7949 SendProgramStatsToFrontend( cps, &programStats );
7951 if (currentMove == forwardMostMove || gameMode==AnalyzeMode ||
7952 gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
7953 DisplayMove(currentMove - 1);
7956 } else if (sscanf(message,"stat01: %d " u64Display " %d %d %d %s",
7957 &time, &nodes, &plylev, &mvleft,
7958 &mvtot, mvname) >= 5) {
7959 /* The stat01: line is from Crafty (9.29+) in response
7960 to the "." command */
7961 programStats.seen_stat = 1;
7962 cps->maybeThinking = TRUE;
7964 if (programStats.got_only_move || !appData.periodicUpdates)
7967 programStats.depth = plylev;
7968 programStats.time = time;
7969 programStats.nodes = nodes;
7970 programStats.moves_left = mvleft;
7971 programStats.nr_moves = mvtot;
7972 strcpy(programStats.move_name, mvname);
7973 programStats.ok_to_send = 1;
7974 programStats.movelist[0] = '\0';
7976 SendProgramStatsToFrontend( cps, &programStats );
7980 } else if (strncmp(message,"++",2) == 0) {
7981 /* Crafty 9.29+ outputs this */
7982 programStats.got_fail = 2;
7985 } else if (strncmp(message,"--",2) == 0) {
7986 /* Crafty 9.29+ outputs this */
7987 programStats.got_fail = 1;
7990 } else if (thinkOutput[0] != NULLCHAR &&
7991 strncmp(message, " ", 4) == 0) {
7992 unsigned message_len;
7995 while (*p && *p == ' ') p++;
7997 message_len = strlen( p );
7999 /* [AS] Avoid buffer overflow */
8000 if( sizeof(thinkOutput) - strlen(thinkOutput) - 1 > message_len ) {
8001 strcat(thinkOutput, " ");
8002 strcat(thinkOutput, p);
8005 if( sizeof(programStats.movelist) - strlen(programStats.movelist) - 1 > message_len ) {
8006 strcat(programStats.movelist, " ");
8007 strcat(programStats.movelist, p);
8010 if (currentMove == forwardMostMove || gameMode==AnalyzeMode ||
8011 gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
8012 DisplayMove(currentMove - 1);
8020 if (sscanf(message, "%d%c %d %d " u64Display " %[^\n]\n",
8021 &plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5)
8023 ChessProgramStats cpstats;
8025 if (plyext != ' ' && plyext != '\t') {
8029 /* [AS] Negate score if machine is playing black and reporting absolute scores */
8030 if( cps->scoreIsAbsolute && ((gameMode == MachinePlaysBlack) || (gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b')) ) {
8031 curscore = -curscore;
8034 cpstats.depth = plylev;
8035 cpstats.nodes = nodes;
8036 cpstats.time = time;
8037 cpstats.score = curscore;
8038 cpstats.got_only_move = 0;
8039 cpstats.movelist[0] = '\0';
8041 if (buf1[0] != NULLCHAR) {
8042 safeStrCpy( cpstats.movelist, buf1, sizeof(cpstats.movelist) );
8045 cpstats.ok_to_send = 0;
8046 cpstats.line_is_book = 0;
8047 cpstats.nr_moves = 0;
8048 cpstats.moves_left = 0;
8050 SendProgramStatsToFrontend( cps, &cpstats );
8057 /* Parse a game score from the character string "game", and
8058 record it as the history of the current game. The game
8059 score is NOT assumed to start from the standard position.
8060 The display is not updated in any way.
8063 ParseGameHistory(game)
8067 int fromX, fromY, toX, toY, boardIndex;
8072 if (appData.debugMode)
8073 fprintf(debugFP, "Parsing game history: %s\n", game);
8075 if (gameInfo.event == NULL) gameInfo.event = StrSave("ICS game");
8076 gameInfo.site = StrSave(appData.icsHost);
8077 gameInfo.date = PGNDate();
8078 gameInfo.round = StrSave("-");
8080 /* Parse out names of players */
8081 while (*game == ' ') game++;
8083 while (*game != ' ') *p++ = *game++;
8085 gameInfo.white = StrSave(buf);
8086 while (*game == ' ') game++;
8088 while (*game != ' ' && *game != '\n') *p++ = *game++;
8090 gameInfo.black = StrSave(buf);
8093 boardIndex = blackPlaysFirst ? 1 : 0;
8096 yyboardindex = boardIndex;
8097 moveType = (ChessMove) yylex();
8099 case IllegalMove: /* maybe suicide chess, etc. */
8100 if (appData.debugMode) {
8101 fprintf(debugFP, "Illegal move from ICS: '%s'\n", yy_text);
8102 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
8103 setbuf(debugFP, NULL);
8105 case WhitePromotion:
8106 case BlackPromotion:
8107 case WhiteNonPromotion:
8108 case BlackNonPromotion:
8110 case WhiteCapturesEnPassant:
8111 case BlackCapturesEnPassant:
8112 case WhiteKingSideCastle:
8113 case WhiteQueenSideCastle:
8114 case BlackKingSideCastle:
8115 case BlackQueenSideCastle:
8116 case WhiteKingSideCastleWild:
8117 case WhiteQueenSideCastleWild:
8118 case BlackKingSideCastleWild:
8119 case BlackQueenSideCastleWild:
8121 case WhiteHSideCastleFR:
8122 case WhiteASideCastleFR:
8123 case BlackHSideCastleFR:
8124 case BlackASideCastleFR:
8126 fromX = currentMoveString[0] - AAA;
8127 fromY = currentMoveString[1] - ONE;
8128 toX = currentMoveString[2] - AAA;
8129 toY = currentMoveString[3] - ONE;
8130 promoChar = currentMoveString[4];
8134 fromX = moveType == WhiteDrop ?
8135 (int) CharToPiece(ToUpper(currentMoveString[0])) :
8136 (int) CharToPiece(ToLower(currentMoveString[0]));
8138 toX = currentMoveString[2] - AAA;
8139 toY = currentMoveString[3] - ONE;
8140 promoChar = NULLCHAR;
8144 sprintf(buf, _("Ambiguous move in ICS output: \"%s\""), yy_text);
8145 if (appData.debugMode) {
8146 fprintf(debugFP, "Ambiguous move from ICS: '%s'\n", yy_text);
8147 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
8148 setbuf(debugFP, NULL);
8150 DisplayError(buf, 0);
8152 case ImpossibleMove:
8154 sprintf(buf, _("Illegal move in ICS output: \"%s\""), yy_text);
8155 if (appData.debugMode) {
8156 fprintf(debugFP, "Impossible move from ICS: '%s'\n", yy_text);
8157 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
8158 setbuf(debugFP, NULL);
8160 DisplayError(buf, 0);
8162 case (ChessMove) 0: /* end of file */
8163 if (boardIndex < backwardMostMove) {
8164 /* Oops, gap. How did that happen? */
8165 DisplayError(_("Gap in move list"), 0);
8168 backwardMostMove = blackPlaysFirst ? 1 : 0;
8169 if (boardIndex > forwardMostMove) {
8170 forwardMostMove = boardIndex;
8174 if (boardIndex > (blackPlaysFirst ? 1 : 0)) {
8175 strcat(parseList[boardIndex-1], " ");
8176 strcat(parseList[boardIndex-1], yy_text);
8188 case GameUnfinished:
8189 if (gameMode == IcsExamining) {
8190 if (boardIndex < backwardMostMove) {
8191 /* Oops, gap. How did that happen? */
8194 backwardMostMove = blackPlaysFirst ? 1 : 0;
8197 gameInfo.result = moveType;
8198 p = strchr(yy_text, '{');
8199 if (p == NULL) p = strchr(yy_text, '(');
8202 if (p[0] == '0' || p[0] == '1' || p[0] == '*') p = "";
8204 q = strchr(p, *p == '{' ? '}' : ')');
8205 if (q != NULL) *q = NULLCHAR;
8208 gameInfo.resultDetails = StrSave(p);
8211 if (boardIndex >= forwardMostMove &&
8212 !(gameMode == IcsObserving && ics_gamenum == -1)) {
8213 backwardMostMove = blackPlaysFirst ? 1 : 0;
8216 (void) CoordsToAlgebraic(boards[boardIndex], PosFlags(boardIndex),
8217 fromY, fromX, toY, toX, promoChar,
8218 parseList[boardIndex]);
8219 CopyBoard(boards[boardIndex + 1], boards[boardIndex]);
8220 /* currentMoveString is set as a side-effect of yylex */
8221 strcpy(moveList[boardIndex], currentMoveString);
8222 strcat(moveList[boardIndex], "\n");
8224 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[boardIndex]);
8225 switch (MateTest(boards[boardIndex], PosFlags(boardIndex)) ) {
8231 if(gameInfo.variant != VariantShogi)
8232 strcat(parseList[boardIndex - 1], "+");
8236 strcat(parseList[boardIndex - 1], "#");
8243 /* Apply a move to the given board */
8245 ApplyMove(fromX, fromY, toX, toY, promoChar, board)
8246 int fromX, fromY, toX, toY;
8250 ChessSquare captured = board[toY][toX], piece, king; int p, oldEP = EP_NONE, berolina = 0;
8251 int promoRank = gameInfo.variant == VariantMakruk ? 3 : 1;
8253 /* [HGM] compute & store e.p. status and castling rights for new position */
8254 /* we can always do that 'in place', now pointers to these rights are passed to ApplyMove */
8256 if(gameInfo.variant == VariantBerolina) berolina = EP_BEROLIN_A;
8257 oldEP = (signed char)board[EP_STATUS];
8258 board[EP_STATUS] = EP_NONE;
8260 if( board[toY][toX] != EmptySquare )
8261 board[EP_STATUS] = EP_CAPTURE;
8263 /* [HGM] In Shatranj and Courier all promotions are to Ferz */
8264 if((gameInfo.variant==VariantShatranj || gameInfo.variant==VariantCourier || gameInfo.variant == VariantMakruk)
8265 && promoChar != 0) promoChar = PieceToChar(WhiteFerz);
8267 if (fromY == DROP_RANK) {
8269 piece = board[toY][toX] = (ChessSquare) fromX;
8273 if( board[fromY][fromX] == WhitePawn ) {
8274 if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
8275 board[EP_STATUS] = EP_PAWN_MOVE;
8277 if(toX>BOARD_LEFT && board[toY][toX-1] == BlackPawn &&
8278 gameInfo.variant != VariantBerolina || toX < fromX)
8279 board[EP_STATUS] = toX | berolina;
8280 if(toX<BOARD_RGHT-1 && board[toY][toX+1] == BlackPawn &&
8281 gameInfo.variant != VariantBerolina || toX > fromX)
8282 board[EP_STATUS] = toX;
8285 if( board[fromY][fromX] == BlackPawn ) {
8286 if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
8287 board[EP_STATUS] = EP_PAWN_MOVE;
8288 if( toY-fromY== -2) {
8289 if(toX>BOARD_LEFT && board[toY][toX-1] == WhitePawn &&
8290 gameInfo.variant != VariantBerolina || toX < fromX)
8291 board[EP_STATUS] = toX | berolina;
8292 if(toX<BOARD_RGHT-1 && board[toY][toX+1] == WhitePawn &&
8293 gameInfo.variant != VariantBerolina || toX > fromX)
8294 board[EP_STATUS] = toX;
8298 for(i=0; i<nrCastlingRights; i++) {
8299 if(board[CASTLING][i] == fromX && castlingRank[i] == fromY ||
8300 board[CASTLING][i] == toX && castlingRank[i] == toY
8301 ) board[CASTLING][i] = NoRights; // revoke for moved or captured piece
8304 if (fromX == toX && fromY == toY) return;
8306 piece = board[fromY][fromX]; /* [HGM] remember, for Shogi promotion */
8307 king = piece < (int) BlackPawn ? WhiteKing : BlackKing; /* [HGM] Knightmate simplify testing for castling */
8308 if(gameInfo.variant == VariantKnightmate)
8309 king += (int) WhiteUnicorn - (int) WhiteKing;
8311 /* Code added by Tord: */
8312 /* FRC castling assumed when king captures friendly rook. */
8313 if (board[fromY][fromX] == WhiteKing &&
8314 board[toY][toX] == WhiteRook) {
8315 board[fromY][fromX] = EmptySquare;
8316 board[toY][toX] = EmptySquare;
8318 board[0][BOARD_RGHT-2] = WhiteKing; board[0][BOARD_RGHT-3] = WhiteRook;
8320 board[0][BOARD_LEFT+2] = WhiteKing; board[0][BOARD_LEFT+3] = WhiteRook;
8322 } else if (board[fromY][fromX] == BlackKing &&
8323 board[toY][toX] == BlackRook) {
8324 board[fromY][fromX] = EmptySquare;
8325 board[toY][toX] = EmptySquare;
8327 board[BOARD_HEIGHT-1][BOARD_RGHT-2] = BlackKing; board[BOARD_HEIGHT-1][BOARD_RGHT-3] = BlackRook;
8329 board[BOARD_HEIGHT-1][BOARD_LEFT+2] = BlackKing; board[BOARD_HEIGHT-1][BOARD_LEFT+3] = BlackRook;
8331 /* End of code added by Tord */
8333 } else if (board[fromY][fromX] == king
8334 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
8335 && toY == fromY && toX > fromX+1) {
8336 board[fromY][fromX] = EmptySquare;
8337 board[toY][toX] = king;
8338 board[toY][toX-1] = board[fromY][BOARD_RGHT-1];
8339 board[fromY][BOARD_RGHT-1] = EmptySquare;
8340 } else if (board[fromY][fromX] == king
8341 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
8342 && toY == fromY && toX < fromX-1) {
8343 board[fromY][fromX] = EmptySquare;
8344 board[toY][toX] = king;
8345 board[toY][toX+1] = board[fromY][BOARD_LEFT];
8346 board[fromY][BOARD_LEFT] = EmptySquare;
8347 } else if (board[fromY][fromX] == WhitePawn
8348 && toY >= BOARD_HEIGHT-promoRank
8349 && gameInfo.variant != VariantXiangqi
8351 /* white pawn promotion */
8352 board[toY][toX] = CharToPiece(ToUpper(promoChar));
8353 if (board[toY][toX] == EmptySquare) {
8354 board[toY][toX] = WhiteQueen;
8356 if(gameInfo.variant==VariantBughouse ||
8357 gameInfo.variant==VariantCrazyhouse) /* [HGM] use shadow piece */
8358 board[toY][toX] = (ChessSquare) (PROMOTED board[toY][toX]);
8359 board[fromY][fromX] = EmptySquare;
8360 } else if ((fromY == BOARD_HEIGHT-4)
8362 && gameInfo.variant != VariantXiangqi
8363 && gameInfo.variant != VariantBerolina
8364 && (board[fromY][fromX] == WhitePawn)
8365 && (board[toY][toX] == EmptySquare)) {
8366 board[fromY][fromX] = EmptySquare;
8367 board[toY][toX] = WhitePawn;
8368 captured = board[toY - 1][toX];
8369 board[toY - 1][toX] = EmptySquare;
8370 } else if ((fromY == BOARD_HEIGHT-4)
8372 && gameInfo.variant == VariantBerolina
8373 && (board[fromY][fromX] == WhitePawn)
8374 && (board[toY][toX] == EmptySquare)) {
8375 board[fromY][fromX] = EmptySquare;
8376 board[toY][toX] = WhitePawn;
8377 if(oldEP & EP_BEROLIN_A) {
8378 captured = board[fromY][fromX-1];
8379 board[fromY][fromX-1] = EmptySquare;
8380 }else{ captured = board[fromY][fromX+1];
8381 board[fromY][fromX+1] = EmptySquare;
8383 } else if (board[fromY][fromX] == king
8384 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
8385 && toY == fromY && toX > fromX+1) {
8386 board[fromY][fromX] = EmptySquare;
8387 board[toY][toX] = king;
8388 board[toY][toX-1] = board[fromY][BOARD_RGHT-1];
8389 board[fromY][BOARD_RGHT-1] = EmptySquare;
8390 } else if (board[fromY][fromX] == king
8391 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
8392 && toY == fromY && toX < fromX-1) {
8393 board[fromY][fromX] = EmptySquare;
8394 board[toY][toX] = king;
8395 board[toY][toX+1] = board[fromY][BOARD_LEFT];
8396 board[fromY][BOARD_LEFT] = EmptySquare;
8397 } else if (fromY == 7 && fromX == 3
8398 && board[fromY][fromX] == BlackKing
8399 && toY == 7 && toX == 5) {
8400 board[fromY][fromX] = EmptySquare;
8401 board[toY][toX] = BlackKing;
8402 board[fromY][7] = EmptySquare;
8403 board[toY][4] = BlackRook;
8404 } else if (fromY == 7 && fromX == 3
8405 && board[fromY][fromX] == BlackKing
8406 && toY == 7 && toX == 1) {
8407 board[fromY][fromX] = EmptySquare;
8408 board[toY][toX] = BlackKing;
8409 board[fromY][0] = EmptySquare;
8410 board[toY][2] = BlackRook;
8411 } else if (board[fromY][fromX] == BlackPawn
8413 && gameInfo.variant != VariantXiangqi
8415 /* black pawn promotion */
8416 board[toY][toX] = CharToPiece(ToLower(promoChar));
8417 if (board[toY][toX] == EmptySquare) {
8418 board[toY][toX] = BlackQueen;
8420 if(gameInfo.variant==VariantBughouse ||
8421 gameInfo.variant==VariantCrazyhouse) /* [HGM] use shadow piece */
8422 board[toY][toX] = (ChessSquare) (PROMOTED board[toY][toX]);
8423 board[fromY][fromX] = EmptySquare;
8424 } else if ((fromY == 3)
8426 && gameInfo.variant != VariantXiangqi
8427 && gameInfo.variant != VariantBerolina
8428 && (board[fromY][fromX] == BlackPawn)
8429 && (board[toY][toX] == EmptySquare)) {
8430 board[fromY][fromX] = EmptySquare;
8431 board[toY][toX] = BlackPawn;
8432 captured = board[toY + 1][toX];
8433 board[toY + 1][toX] = EmptySquare;
8434 } else if ((fromY == 3)
8436 && gameInfo.variant == VariantBerolina
8437 && (board[fromY][fromX] == BlackPawn)
8438 && (board[toY][toX] == EmptySquare)) {
8439 board[fromY][fromX] = EmptySquare;
8440 board[toY][toX] = BlackPawn;
8441 if(oldEP & EP_BEROLIN_A) {
8442 captured = board[fromY][fromX-1];
8443 board[fromY][fromX-1] = EmptySquare;
8444 }else{ captured = board[fromY][fromX+1];
8445 board[fromY][fromX+1] = EmptySquare;
8448 board[toY][toX] = board[fromY][fromX];
8449 board[fromY][fromX] = EmptySquare;
8453 if (gameInfo.holdingsWidth != 0) {
8455 /* !!A lot more code needs to be written to support holdings */
8456 /* [HGM] OK, so I have written it. Holdings are stored in the */
8457 /* penultimate board files, so they are automaticlly stored */
8458 /* in the game history. */
8459 if (fromY == DROP_RANK) {
8460 /* Delete from holdings, by decreasing count */
8461 /* and erasing image if necessary */
8463 if(p < (int) BlackPawn) { /* white drop */
8464 p -= (int)WhitePawn;
8465 p = PieceToNumber((ChessSquare)p);
8466 if(p >= gameInfo.holdingsSize) p = 0;
8467 if(--board[p][BOARD_WIDTH-2] <= 0)
8468 board[p][BOARD_WIDTH-1] = EmptySquare;
8469 if((int)board[p][BOARD_WIDTH-2] < 0)
8470 board[p][BOARD_WIDTH-2] = 0;
8471 } else { /* black drop */
8472 p -= (int)BlackPawn;
8473 p = PieceToNumber((ChessSquare)p);
8474 if(p >= gameInfo.holdingsSize) p = 0;
8475 if(--board[BOARD_HEIGHT-1-p][1] <= 0)
8476 board[BOARD_HEIGHT-1-p][0] = EmptySquare;
8477 if((int)board[BOARD_HEIGHT-1-p][1] < 0)
8478 board[BOARD_HEIGHT-1-p][1] = 0;
8481 if (captured != EmptySquare && gameInfo.holdingsSize > 0
8482 && gameInfo.variant != VariantBughouse ) {
8483 /* [HGM] holdings: Add to holdings, if holdings exist */
8484 if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat) {
8485 // [HGM] superchess: suppress flipping color of captured pieces by reverse pre-flip
8486 captured = (int) captured >= (int) BlackPawn ? BLACK_TO_WHITE captured : WHITE_TO_BLACK captured;
8489 if (p >= (int) BlackPawn) {
8490 p -= (int)BlackPawn;
8491 if(gameInfo.variant == VariantShogi && DEMOTED p >= 0) {
8492 /* in Shogi restore piece to its original first */
8493 captured = (ChessSquare) (DEMOTED captured);
8496 p = PieceToNumber((ChessSquare)p);
8497 if(p >= gameInfo.holdingsSize) { p = 0; captured = BlackPawn; }
8498 board[p][BOARD_WIDTH-2]++;
8499 board[p][BOARD_WIDTH-1] = BLACK_TO_WHITE captured;
8501 p -= (int)WhitePawn;
8502 if(gameInfo.variant == VariantShogi && DEMOTED p >= 0) {
8503 captured = (ChessSquare) (DEMOTED captured);
8506 p = PieceToNumber((ChessSquare)p);
8507 if(p >= gameInfo.holdingsSize) { p = 0; captured = WhitePawn; }
8508 board[BOARD_HEIGHT-1-p][1]++;
8509 board[BOARD_HEIGHT-1-p][0] = WHITE_TO_BLACK captured;
8512 } else if (gameInfo.variant == VariantAtomic) {
8513 if (captured != EmptySquare) {
8515 for (y = toY-1; y <= toY+1; y++) {
8516 for (x = toX-1; x <= toX+1; x++) {
8517 if (y >= 0 && y < BOARD_HEIGHT && x >= BOARD_LEFT && x < BOARD_RGHT &&
8518 board[y][x] != WhitePawn && board[y][x] != BlackPawn) {
8519 board[y][x] = EmptySquare;
8523 board[toY][toX] = EmptySquare;
8526 if(promoChar == '+') {
8527 /* [HGM] Shogi-style promotions, to piece implied by original (Might overwrite orinary Pawn promotion) */
8528 board[toY][toX] = (ChessSquare) (PROMOTED piece);
8529 } else if(!appData.testLegality) { // without legality testing, unconditionally believe promoChar
8530 board[toY][toX] = CharToPiece(promoChar);
8532 if((gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat)
8533 && promoChar != NULLCHAR && gameInfo.holdingsSize) {
8534 // [HGM] superchess: take promotion piece out of holdings
8535 int k = PieceToNumber(CharToPiece(ToUpper(promoChar)));
8536 if((int)piece < (int)BlackPawn) { // determine stm from piece color
8537 if(!--board[k][BOARD_WIDTH-2])
8538 board[k][BOARD_WIDTH-1] = EmptySquare;
8540 if(!--board[BOARD_HEIGHT-1-k][1])
8541 board[BOARD_HEIGHT-1-k][0] = EmptySquare;
8547 /* Updates forwardMostMove */
8549 MakeMove(fromX, fromY, toX, toY, promoChar)
8550 int fromX, fromY, toX, toY;
8553 // forwardMostMove++; // [HGM] bare: moved downstream
8555 if(serverMoves != NULL) { /* [HGM] write moves on file for broadcasting (should be separate routine, really) */
8556 int timeLeft; static int lastLoadFlag=0; int king, piece;
8557 piece = boards[forwardMostMove][fromY][fromX];
8558 king = piece < (int) BlackPawn ? WhiteKing : BlackKing;
8559 if(gameInfo.variant == VariantKnightmate)
8560 king += (int) WhiteUnicorn - (int) WhiteKing;
8561 if(forwardMostMove == 0) {
8563 fprintf(serverMoves, "%s;", second.tidy);
8564 fprintf(serverMoves, "%s;", first.tidy);
8565 if(!blackPlaysFirst)
8566 fprintf(serverMoves, "%s;", second.tidy);
8567 } else fprintf(serverMoves, loadFlag|lastLoadFlag ? ":" : ";");
8568 lastLoadFlag = loadFlag;
8570 fprintf(serverMoves, "%c%c:%c%c", AAA+fromX, ONE+fromY, AAA+toX, ONE+toY);
8571 // print castling suffix
8572 if( toY == fromY && piece == king ) {
8574 fprintf(serverMoves, ":%c%c:%c%c", AAA+BOARD_RGHT-1, ONE+fromY, AAA+toX-1,ONE+toY);
8576 fprintf(serverMoves, ":%c%c:%c%c", AAA+BOARD_LEFT, ONE+fromY, AAA+toX+1,ONE+toY);
8579 if( (boards[forwardMostMove][fromY][fromX] == WhitePawn ||
8580 boards[forwardMostMove][fromY][fromX] == BlackPawn ) &&
8581 boards[forwardMostMove][toY][toX] == EmptySquare
8582 && fromX != toX && fromY != toY)
8583 fprintf(serverMoves, ":%c%c:%c%c", AAA+fromX, ONE+fromY, AAA+toX, ONE+fromY);
8585 if(promoChar != NULLCHAR)
8586 fprintf(serverMoves, ":%c:%c%c", promoChar, AAA+toX, ONE+toY);
8588 fprintf(serverMoves, "/%d/%d",
8589 pvInfoList[forwardMostMove].depth, pvInfoList[forwardMostMove].score);
8590 if(forwardMostMove+1 & 1) timeLeft = whiteTimeRemaining/1000;
8591 else timeLeft = blackTimeRemaining/1000;
8592 fprintf(serverMoves, "/%d", timeLeft);
8594 fflush(serverMoves);
8597 if (forwardMostMove+1 > framePtr) { // [HGM] vari: do not run into saved variations
8598 DisplayFatalError(_("Game too long; increase MAX_MOVES and recompile"),
8602 UnLoadPV(); // [HGM] pv: if we are looking at a PV, abort this
8603 if (commentList[forwardMostMove+1] != NULL) {
8604 free(commentList[forwardMostMove+1]);
8605 commentList[forwardMostMove+1] = NULL;
8607 CopyBoard(boards[forwardMostMove+1], boards[forwardMostMove]);
8608 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[forwardMostMove+1]);
8609 // forwardMostMove++; // [HGM] bare: moved to after ApplyMove, to make sure clock interrupt finds complete board
8610 SwitchClocks(forwardMostMove+1); // [HGM] race: incrementing move nr inside
8611 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
8612 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
8613 gameInfo.result = GameUnfinished;
8614 if (gameInfo.resultDetails != NULL) {
8615 free(gameInfo.resultDetails);
8616 gameInfo.resultDetails = NULL;
8618 CoordsToComputerAlgebraic(fromY, fromX, toY, toX, promoChar,
8619 moveList[forwardMostMove - 1]);
8620 (void) CoordsToAlgebraic(boards[forwardMostMove - 1],
8621 PosFlags(forwardMostMove - 1),
8622 fromY, fromX, toY, toX, promoChar,
8623 parseList[forwardMostMove - 1]);
8624 switch (MateTest(boards[forwardMostMove], PosFlags(forwardMostMove)) ) {
8630 if(gameInfo.variant != VariantShogi)
8631 strcat(parseList[forwardMostMove - 1], "+");
8635 strcat(parseList[forwardMostMove - 1], "#");
8638 if (appData.debugMode) {
8639 fprintf(debugFP, "move: %s, parse: %s (%c)\n", moveList[forwardMostMove-1], parseList[forwardMostMove-1], moveList[forwardMostMove-1][4]);
8644 /* Updates currentMove if not pausing */
8646 ShowMove(fromX, fromY, toX, toY)
8648 int instant = (gameMode == PlayFromGameFile) ?
8649 (matchMode || (appData.timeDelay == 0 && !pausing)) : pausing;
8650 if(appData.noGUI) return;
8651 if (!pausing || gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
8653 if (forwardMostMove == currentMove + 1) {
8654 AnimateMove(boards[forwardMostMove - 1],
8655 fromX, fromY, toX, toY);
8657 if (appData.highlightLastMove) {
8658 SetHighlights(fromX, fromY, toX, toY);
8661 currentMove = forwardMostMove;
8664 if (instant) return;
8666 DisplayMove(currentMove - 1);
8667 DrawPosition(FALSE, boards[currentMove]);
8668 DisplayBothClocks();
8669 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
8672 void SendEgtPath(ChessProgramState *cps)
8673 { /* [HGM] EGT: match formats given in feature with those given by user, and send info for each match */
8674 char buf[MSG_SIZ], name[MSG_SIZ], *p;
8676 if((p = cps->egtFormats) == NULL || appData.egtFormats == NULL) return;
8679 char c, *q = name+1, *r, *s;
8681 name[0] = ','; // extract next format name from feature and copy with prefixed ','
8682 while(*p && *p != ',') *q++ = *p++;
8684 if( appData.defaultPathEGTB && appData.defaultPathEGTB[0] &&
8685 strcmp(name, ",nalimov:") == 0 ) {
8686 // take nalimov path from the menu-changeable option first, if it is defined
8687 sprintf(buf, "egtpath nalimov %s\n", appData.defaultPathEGTB);
8688 SendToProgram(buf,cps); // send egtbpath command for nalimov
8690 if( (s = StrStr(appData.egtFormats, name+1)) == appData.egtFormats ||
8691 (s = StrStr(appData.egtFormats, name)) != NULL) {
8692 // format name occurs amongst user-supplied formats, at beginning or immediately after comma
8693 s = r = StrStr(s, ":") + 1; // beginning of path info
8694 while(*r && *r != ',') r++; // path info is everything upto next ';' or end of string
8695 c = *r; *r = 0; // temporarily null-terminate path info
8696 *--q = 0; // strip of trailig ':' from name
8697 sprintf(buf, "egtpath %s %s\n", name+1, s);
8699 SendToProgram(buf,cps); // send egtbpath command for this format
8701 if(*p == ',') p++; // read away comma to position for next format name
8706 InitChessProgram(cps, setup)
8707 ChessProgramState *cps;
8708 int setup; /* [HGM] needed to setup FRC opening position */
8710 char buf[MSG_SIZ], b[MSG_SIZ]; int overruled;
8711 if (appData.noChessProgram) return;
8712 hintRequested = FALSE;
8713 bookRequested = FALSE;
8715 /* [HGM] some new WB protocol commands to configure engine are sent now, if engine supports them */
8716 /* moved to before sending initstring in 4.3.15, so Polyglot can delay UCI 'isready' to recepton of 'new' */
8717 if(cps->memSize) { /* [HGM] memory */
8718 sprintf(buf, "memory %d\n", appData.defaultHashSize + appData.defaultCacheSizeEGTB);
8719 SendToProgram(buf, cps);
8721 SendEgtPath(cps); /* [HGM] EGT */
8722 if(cps->maxCores) { /* [HGM] SMP: (protocol specified must be last settings command before new!) */
8723 sprintf(buf, "cores %d\n", appData.smpCores);
8724 SendToProgram(buf, cps);
8727 SendToProgram(cps->initString, cps);
8728 if (gameInfo.variant != VariantNormal &&
8729 gameInfo.variant != VariantLoadable
8730 /* [HGM] also send variant if board size non-standard */
8731 || gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0
8733 char *v = VariantName(gameInfo.variant);
8734 if (cps->protocolVersion != 1 && StrStr(cps->variants, v) == NULL) {
8735 /* [HGM] in protocol 1 we have to assume all variants valid */
8736 sprintf(buf, _("Variant %s not supported by %s"), v, cps->tidy);
8737 DisplayFatalError(buf, 0, 1);
8741 /* [HGM] make prefix for non-standard board size. Awkward testing... */
8742 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
8743 if( gameInfo.variant == VariantXiangqi )
8744 overruled = gameInfo.boardWidth != 9 || gameInfo.boardHeight != 10 || gameInfo.holdingsSize != 0;
8745 if( gameInfo.variant == VariantShogi )
8746 overruled = gameInfo.boardWidth != 9 || gameInfo.boardHeight != 9 || gameInfo.holdingsSize != 7;
8747 if( gameInfo.variant == VariantBughouse || gameInfo.variant == VariantCrazyhouse )
8748 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 5;
8749 if( gameInfo.variant == VariantCapablanca || gameInfo.variant == VariantCapaRandom ||
8750 gameInfo.variant == VariantGothic || gameInfo.variant == VariantFalcon )
8751 overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
8752 if( gameInfo.variant == VariantCourier )
8753 overruled = gameInfo.boardWidth != 12 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
8754 if( gameInfo.variant == VariantSuper )
8755 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 8;
8756 if( gameInfo.variant == VariantGreat )
8757 overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 8;
8760 sprintf(b, "%dx%d+%d_%s", gameInfo.boardWidth, gameInfo.boardHeight,
8761 gameInfo.holdingsSize, VariantName(gameInfo.variant)); // cook up sized variant name
8762 /* [HGM] varsize: try first if this defiant size variant is specifically known */
8763 if(StrStr(cps->variants, b) == NULL) {
8764 // specific sized variant not known, check if general sizing allowed
8765 if (cps->protocolVersion != 1) { // for protocol 1 we cannot check and hope for the best
8766 if(StrStr(cps->variants, "boardsize") == NULL) {
8767 sprintf(buf, "Board size %dx%d+%d not supported by %s",
8768 gameInfo.boardWidth, gameInfo.boardHeight, gameInfo.holdingsSize, cps->tidy);
8769 DisplayFatalError(buf, 0, 1);
8772 /* [HGM] here we really should compare with the maximum supported board size */
8775 } else sprintf(b, "%s", VariantName(gameInfo.variant));
8776 sprintf(buf, "variant %s\n", b);
8777 SendToProgram(buf, cps);
8779 currentlyInitializedVariant = gameInfo.variant;
8781 /* [HGM] send opening position in FRC to first engine */
8783 SendToProgram("force\n", cps);
8785 /* engine is now in force mode! Set flag to wake it up after first move. */
8786 setboardSpoiledMachineBlack = 1;
8790 snprintf(buf, sizeof(buf), "ics %s\n", appData.icsActive ? appData.icsHost : "-");
8791 SendToProgram(buf, cps);
8793 cps->maybeThinking = FALSE;
8794 cps->offeredDraw = 0;
8795 if (!appData.icsActive) {
8796 SendTimeControl(cps, movesPerSession, timeControl,
8797 timeIncrement, appData.searchDepth,
8800 if (appData.showThinking
8801 // [HGM] thinking: four options require thinking output to be sent
8802 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp()
8804 SendToProgram("post\n", cps);
8806 SendToProgram("hard\n", cps);
8807 if (!appData.ponderNextMove) {
8808 /* Warning: "easy" is a toggle in GNU Chess, so don't send
8809 it without being sure what state we are in first. "hard"
8810 is not a toggle, so that one is OK.
8812 SendToProgram("easy\n", cps);
8815 sprintf(buf, "ping %d\n", ++cps->lastPing);
8816 SendToProgram(buf, cps);
8818 cps->initDone = TRUE;
8823 StartChessProgram(cps)
8824 ChessProgramState *cps;
8829 if (appData.noChessProgram) return;
8830 cps->initDone = FALSE;
8832 if (strcmp(cps->host, "localhost") == 0) {
8833 err = StartChildProcess(cps->program, cps->dir, &cps->pr);
8834 } else if (*appData.remoteShell == NULLCHAR) {
8835 err = OpenRcmd(cps->host, appData.remoteUser, cps->program, &cps->pr);
8837 if (*appData.remoteUser == NULLCHAR) {
8838 snprintf(buf, sizeof(buf), "%s %s %s", appData.remoteShell, cps->host,
8841 snprintf(buf, sizeof(buf), "%s %s -l %s %s", appData.remoteShell,
8842 cps->host, appData.remoteUser, cps->program);
8844 err = StartChildProcess(buf, "", &cps->pr);
8848 sprintf(buf, _("Startup failure on '%s'"), cps->program);
8849 DisplayFatalError(buf, err, 1);
8855 cps->isr = AddInputSource(cps->pr, TRUE, ReceiveFromProgram, cps);
8856 if (cps->protocolVersion > 1) {
8857 sprintf(buf, "xboard\nprotover %d\n", cps->protocolVersion);
8858 cps->nrOptions = 0; // [HGM] options: clear all engine-specific options
8859 cps->comboCnt = 0; // and values of combo boxes
8860 SendToProgram(buf, cps);
8862 SendToProgram("xboard\n", cps);
8868 TwoMachinesEventIfReady P((void))
8870 if (first.lastPing != first.lastPong) {
8871 DisplayMessage("", _("Waiting for first chess program"));
8872 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10); // [HGM] fast: lowered from 1000
8875 if (second.lastPing != second.lastPong) {
8876 DisplayMessage("", _("Waiting for second chess program"));
8877 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10); // [HGM] fast: lowered from 1000
8885 NextMatchGame P((void))
8887 int index; /* [HGM] autoinc: step load index during match */
8889 if (*appData.loadGameFile != NULLCHAR) {
8890 index = appData.loadGameIndex;
8891 if(index < 0) { // [HGM] autoinc
8892 lastIndex = index = (index == -2 && first.twoMachinesColor[0] == 'b') ? lastIndex : lastIndex+1;
8893 if(appData.rewindIndex > 0 && index > appData.rewindIndex) lastIndex = index = 1;
8895 LoadGameFromFile(appData.loadGameFile,
8897 appData.loadGameFile, FALSE);
8898 } else if (*appData.loadPositionFile != NULLCHAR) {
8899 index = appData.loadPositionIndex;
8900 if(index < 0) { // [HGM] autoinc
8901 lastIndex = index = (index == -2 && first.twoMachinesColor[0] == 'b') ? lastIndex : lastIndex+1;
8902 if(appData.rewindIndex > 0 && index > appData.rewindIndex) lastIndex = index = 1;
8904 LoadPositionFromFile(appData.loadPositionFile,
8906 appData.loadPositionFile);
8908 TwoMachinesEventIfReady();
8911 void UserAdjudicationEvent( int result )
8913 ChessMove gameResult = GameIsDrawn;
8916 gameResult = WhiteWins;
8918 else if( result < 0 ) {
8919 gameResult = BlackWins;
8922 if( gameMode == TwoMachinesPlay ) {
8923 GameEnds( gameResult, "User adjudication", GE_XBOARD );
8928 // [HGM] save: calculate checksum of game to make games easily identifiable
8929 int StringCheckSum(char *s)
8932 if(s==NULL) return 0;
8933 while(*s) i = i*259 + *s++;
8940 for(i=backwardMostMove; i<forwardMostMove; i++) {
8941 sum += pvInfoList[i].depth;
8942 sum += StringCheckSum(parseList[i]);
8943 sum += StringCheckSum(commentList[i]);
8946 if(i>1 && sum==0) sum++; // make sure never zero for non-empty game
8947 return sum + StringCheckSum(commentList[i]);
8948 } // end of save patch
8951 GameEnds(result, resultDetails, whosays)
8953 char *resultDetails;
8956 GameMode nextGameMode;
8958 char buf[MSG_SIZ], popupRequested = 0;
8960 if(endingGame) return; /* [HGM] crash: forbid recursion */
8962 if(twoBoards) { // [HGM] dual: switch back to one board
8963 twoBoards = partnerUp = 0; InitDrawingSizes(-2, 0);
8964 DrawPosition(TRUE, partnerBoard); // observed game becomes foreground
8966 if (appData.debugMode) {
8967 fprintf(debugFP, "GameEnds(%d, %s, %d)\n",
8968 result, resultDetails ? resultDetails : "(null)", whosays);
8971 fromX = fromY = -1; // [HGM] abort any move the user is entering.
8973 if (appData.icsActive && (whosays == GE_ENGINE || whosays >= GE_ENGINE1)) {
8974 /* If we are playing on ICS, the server decides when the
8975 game is over, but the engine can offer to draw, claim
8979 if (appData.zippyPlay && first.initDone) {
8980 if (result == GameIsDrawn) {
8981 /* In case draw still needs to be claimed */
8982 SendToICS(ics_prefix);
8983 SendToICS("draw\n");
8984 } else if (StrCaseStr(resultDetails, "resign")) {
8985 SendToICS(ics_prefix);
8986 SendToICS("resign\n");
8990 endingGame = 0; /* [HGM] crash */
8994 /* If we're loading the game from a file, stop */
8995 if (whosays == GE_FILE) {
8996 (void) StopLoadGameTimer();
9000 /* Cancel draw offers */
9001 first.offeredDraw = second.offeredDraw = 0;
9003 /* If this is an ICS game, only ICS can really say it's done;
9004 if not, anyone can. */
9005 isIcsGame = (gameMode == IcsPlayingWhite ||
9006 gameMode == IcsPlayingBlack ||
9007 gameMode == IcsObserving ||
9008 gameMode == IcsExamining);
9010 if (!isIcsGame || whosays == GE_ICS) {
9011 /* OK -- not an ICS game, or ICS said it was done */
9013 if (!isIcsGame && !appData.noChessProgram)
9014 SetUserThinkingEnables();
9016 /* [HGM] if a machine claims the game end we verify this claim */
9017 if(gameMode == TwoMachinesPlay && appData.testClaims) {
9018 if(appData.testLegality && whosays >= GE_ENGINE1 ) {
9020 ChessMove trueResult = (ChessMove) -1;
9022 claimer = whosays == GE_ENGINE1 ? /* color of claimer */
9023 first.twoMachinesColor[0] :
9024 second.twoMachinesColor[0] ;
9026 // [HGM] losers: because the logic is becoming a bit hairy, determine true result first
9027 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_CHECKMATE) {
9028 /* [HGM] verify: engine mate claims accepted if they were flagged */
9029 trueResult = WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins;
9031 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_WINS) { // added code for games where being mated is a win
9032 /* [HGM] verify: engine mate claims accepted if they were flagged */
9033 trueResult = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
9035 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_STALEMATE) { // only used to indicate draws now
9036 trueResult = GameIsDrawn; // default; in variants where stalemate loses, Status is CHECKMATE
9039 // now verify win claims, but not in drop games, as we don't understand those yet
9040 if( (gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper
9041 || gameInfo.variant == VariantGreat) &&
9042 (result == WhiteWins && claimer == 'w' ||
9043 result == BlackWins && claimer == 'b' ) ) { // case to verify: engine claims own win
9044 if (appData.debugMode) {
9045 fprintf(debugFP, "result=%d sp=%d move=%d\n",
9046 result, (signed char)boards[forwardMostMove][EP_STATUS], forwardMostMove);
9048 if(result != trueResult) {
9049 sprintf(buf, "False win claim: '%s'", resultDetails);
9050 result = claimer == 'w' ? BlackWins : WhiteWins;
9051 resultDetails = buf;
9054 if( result == GameIsDrawn && (signed char)boards[forwardMostMove][EP_STATUS] > EP_DRAWS
9055 && (forwardMostMove <= backwardMostMove ||
9056 (signed char)boards[forwardMostMove-1][EP_STATUS] > EP_DRAWS ||
9057 (claimer=='b')==(forwardMostMove&1))
9059 /* [HGM] verify: draws that were not flagged are false claims */
9060 sprintf(buf, "False draw claim: '%s'", resultDetails);
9061 result = claimer == 'w' ? BlackWins : WhiteWins;
9062 resultDetails = buf;
9064 /* (Claiming a loss is accepted no questions asked!) */
9066 /* [HGM] bare: don't allow bare King to win */
9067 if((gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat)
9068 && gameInfo.variant != VariantLosers && gameInfo.variant != VariantGiveaway
9069 && gameInfo.variant != VariantSuicide // [HGM] losers: except in losers, of course...
9070 && result != GameIsDrawn)
9071 { int i, j, k=0, color = (result==WhiteWins ? (int)WhitePawn : (int)BlackPawn);
9072 for(j=BOARD_LEFT; j<BOARD_RGHT; j++) for(i=0; i<BOARD_HEIGHT; i++) {
9073 int p = (signed char)boards[forwardMostMove][i][j] - color;
9074 if(p >= 0 && p <= (int)WhiteKing) k++;
9076 if (appData.debugMode) {
9077 fprintf(debugFP, "GE(%d, %s, %d) bare king k=%d color=%d\n",
9078 result, resultDetails ? resultDetails : "(null)", whosays, k, color);
9081 result = GameIsDrawn;
9082 sprintf(buf, "%s but bare king", resultDetails);
9083 resultDetails = buf;
9089 if(serverMoves != NULL && !loadFlag) { char c = '=';
9090 if(result==WhiteWins) c = '+';
9091 if(result==BlackWins) c = '-';
9092 if(resultDetails != NULL)
9093 fprintf(serverMoves, ";%c;%s\n", c, resultDetails);
9095 if (resultDetails != NULL) {
9096 gameInfo.result = result;
9097 gameInfo.resultDetails = StrSave(resultDetails);
9099 /* display last move only if game was not loaded from file */
9100 if ((whosays != GE_FILE) && (currentMove == forwardMostMove))
9101 DisplayMove(currentMove - 1);
9103 if (forwardMostMove != 0) {
9104 if (gameMode != PlayFromGameFile && gameMode != EditGame
9105 && lastSavedGame != GameCheckSum() // [HGM] save: suppress duplicates
9107 if (*appData.saveGameFile != NULLCHAR) {
9108 SaveGameToFile(appData.saveGameFile, TRUE);
9109 } else if (appData.autoSaveGames) {
9112 if (*appData.savePositionFile != NULLCHAR) {
9113 SavePositionToFile(appData.savePositionFile);
9118 /* Tell program how game ended in case it is learning */
9119 /* [HGM] Moved this to after saving the PGN, just in case */
9120 /* engine died and we got here through time loss. In that */
9121 /* case we will get a fatal error writing the pipe, which */
9122 /* would otherwise lose us the PGN. */
9123 /* [HGM] crash: not needed anymore, but doesn't hurt; */
9124 /* output during GameEnds should never be fatal anymore */
9125 if (gameMode == MachinePlaysWhite ||
9126 gameMode == MachinePlaysBlack ||
9127 gameMode == TwoMachinesPlay ||
9128 gameMode == IcsPlayingWhite ||
9129 gameMode == IcsPlayingBlack ||
9130 gameMode == BeginningOfGame) {
9132 sprintf(buf, "result %s {%s}\n", PGNResult(result),
9134 if (first.pr != NoProc) {
9135 SendToProgram(buf, &first);
9137 if (second.pr != NoProc &&
9138 gameMode == TwoMachinesPlay) {
9139 SendToProgram(buf, &second);
9144 if (appData.icsActive) {
9145 if (appData.quietPlay &&
9146 (gameMode == IcsPlayingWhite ||
9147 gameMode == IcsPlayingBlack)) {
9148 SendToICS(ics_prefix);
9149 SendToICS("set shout 1\n");
9151 nextGameMode = IcsIdle;
9152 ics_user_moved = FALSE;
9153 /* clean up premove. It's ugly when the game has ended and the
9154 * premove highlights are still on the board.
9158 ClearPremoveHighlights();
9159 DrawPosition(FALSE, boards[currentMove]);
9161 if (whosays == GE_ICS) {
9164 if (gameMode == IcsPlayingWhite)
9166 else if(gameMode == IcsPlayingBlack)
9170 if (gameMode == IcsPlayingBlack)
9172 else if(gameMode == IcsPlayingWhite)
9179 PlayIcsUnfinishedSound();
9182 } else if (gameMode == EditGame ||
9183 gameMode == PlayFromGameFile ||
9184 gameMode == AnalyzeMode ||
9185 gameMode == AnalyzeFile) {
9186 nextGameMode = gameMode;
9188 nextGameMode = EndOfGame;
9193 nextGameMode = gameMode;
9196 if (appData.noChessProgram) {
9197 gameMode = nextGameMode;
9199 endingGame = 0; /* [HGM] crash */
9204 /* Put first chess program into idle state */
9205 if (first.pr != NoProc &&
9206 (gameMode == MachinePlaysWhite ||
9207 gameMode == MachinePlaysBlack ||
9208 gameMode == TwoMachinesPlay ||
9209 gameMode == IcsPlayingWhite ||
9210 gameMode == IcsPlayingBlack ||
9211 gameMode == BeginningOfGame)) {
9212 SendToProgram("force\n", &first);
9213 if (first.usePing) {
9215 sprintf(buf, "ping %d\n", ++first.lastPing);
9216 SendToProgram(buf, &first);
9219 } else if (result != GameUnfinished || nextGameMode == IcsIdle) {
9220 /* Kill off first chess program */
9221 if (first.isr != NULL)
9222 RemoveInputSource(first.isr);
9225 if (first.pr != NoProc) {
9227 DoSleep( appData.delayBeforeQuit );
9228 SendToProgram("quit\n", &first);
9229 DoSleep( appData.delayAfterQuit );
9230 DestroyChildProcess(first.pr, first.useSigterm);
9235 /* Put second chess program into idle state */
9236 if (second.pr != NoProc &&
9237 gameMode == TwoMachinesPlay) {
9238 SendToProgram("force\n", &second);
9239 if (second.usePing) {
9241 sprintf(buf, "ping %d\n", ++second.lastPing);
9242 SendToProgram(buf, &second);
9245 } else if (result != GameUnfinished || nextGameMode == IcsIdle) {
9246 /* Kill off second chess program */
9247 if (second.isr != NULL)
9248 RemoveInputSource(second.isr);
9251 if (second.pr != NoProc) {
9252 DoSleep( appData.delayBeforeQuit );
9253 SendToProgram("quit\n", &second);
9254 DoSleep( appData.delayAfterQuit );
9255 DestroyChildProcess(second.pr, second.useSigterm);
9260 if (matchMode && gameMode == TwoMachinesPlay) {
9263 if (first.twoMachinesColor[0] == 'w') {
9270 if (first.twoMachinesColor[0] == 'b') {
9279 if (matchGame < appData.matchGames) {
9281 if(appData.sameColorGames <= 1) { /* [HGM] alternate: suppress color swap */
9282 tmp = first.twoMachinesColor;
9283 first.twoMachinesColor = second.twoMachinesColor;
9284 second.twoMachinesColor = tmp;
9286 gameMode = nextGameMode;
9288 if(appData.matchPause>10000 || appData.matchPause<10)
9289 appData.matchPause = 10000; /* [HGM] make pause adjustable */
9290 ScheduleDelayedEvent(NextMatchGame, appData.matchPause);
9291 endingGame = 0; /* [HGM] crash */
9294 gameMode = nextGameMode;
9295 sprintf(buf, _("Match %s vs. %s: final score %d-%d-%d"),
9296 first.tidy, second.tidy,
9297 first.matchWins, second.matchWins,
9298 appData.matchGames - (first.matchWins + second.matchWins));
9299 popupRequested++; // [HGM] crash: postpone to after resetting endingGame
9302 if ((gameMode == AnalyzeMode || gameMode == AnalyzeFile) &&
9303 !(nextGameMode == AnalyzeMode || nextGameMode == AnalyzeFile))
9305 gameMode = nextGameMode;
9307 endingGame = 0; /* [HGM] crash */
9308 if(popupRequested) { // [HGM] crash: this calls GameEnds recursively through ExitEvent! Make it a harmless tail recursion.
9309 if(matchMode == TRUE) DisplayFatalError(buf, 0, 0); else {
9310 matchMode = FALSE; appData.matchGames = matchGame = 0;
9316 /* Assumes program was just initialized (initString sent).
9317 Leaves program in force mode. */
9319 FeedMovesToProgram(cps, upto)
9320 ChessProgramState *cps;
9325 if (appData.debugMode)
9326 fprintf(debugFP, "Feeding %smoves %d through %d to %s chess program\n",
9327 startedFromSetupPosition ? "position and " : "",
9328 backwardMostMove, upto, cps->which);
9329 if(currentlyInitializedVariant != gameInfo.variant) { char buf[MSG_SIZ];
9330 // [HGM] variantswitch: make engine aware of new variant
9331 if(cps->protocolVersion > 1 && StrStr(cps->variants, VariantName(gameInfo.variant)) == NULL)
9332 return; // [HGM] refrain from feeding moves altogether if variant is unsupported!
9333 sprintf(buf, "variant %s\n", VariantName(gameInfo.variant));
9334 SendToProgram(buf, cps);
9335 currentlyInitializedVariant = gameInfo.variant;
9337 SendToProgram("force\n", cps);
9338 if (startedFromSetupPosition) {
9339 SendBoard(cps, backwardMostMove);
9340 if (appData.debugMode) {
9341 fprintf(debugFP, "feedMoves\n");
9344 for (i = backwardMostMove; i < upto; i++) {
9345 SendMoveToProgram(i, cps);
9351 ResurrectChessProgram()
9353 /* The chess program may have exited.
9354 If so, restart it and feed it all the moves made so far. */
9356 if (appData.noChessProgram || first.pr != NoProc) return;
9358 StartChessProgram(&first);
9359 InitChessProgram(&first, FALSE);
9360 FeedMovesToProgram(&first, currentMove);
9362 if (!first.sendTime) {
9363 /* can't tell gnuchess what its clock should read,
9364 so we bow to its notion. */
9366 timeRemaining[0][currentMove] = whiteTimeRemaining;
9367 timeRemaining[1][currentMove] = blackTimeRemaining;
9370 if ((gameMode == AnalyzeMode || gameMode == AnalyzeFile ||
9371 appData.icsEngineAnalyze) && first.analysisSupport) {
9372 SendToProgram("analyze\n", &first);
9373 first.analyzing = TRUE;
9386 if (appData.debugMode) {
9387 fprintf(debugFP, "Reset(%d, %d) from gameMode %d\n",
9388 redraw, init, gameMode);
9390 CleanupTail(); // [HGM] vari: delete any stored variations
9391 pausing = pauseExamInvalid = FALSE;
9392 startedFromSetupPosition = blackPlaysFirst = FALSE;
9394 whiteFlag = blackFlag = FALSE;
9395 userOfferedDraw = FALSE;
9396 hintRequested = bookRequested = FALSE;
9397 first.maybeThinking = FALSE;
9398 second.maybeThinking = FALSE;
9399 first.bookSuspend = FALSE; // [HGM] book
9400 second.bookSuspend = FALSE;
9401 thinkOutput[0] = NULLCHAR;
9402 lastHint[0] = NULLCHAR;
9403 ClearGameInfo(&gameInfo);
9404 gameInfo.variant = StringToVariant(appData.variant);
9405 ics_user_moved = ics_clock_paused = FALSE;
9406 ics_getting_history = H_FALSE;
9408 white_holding[0] = black_holding[0] = NULLCHAR;
9409 ClearProgramStats();
9410 opponentKibitzes = FALSE; // [HGM] kibitz: do not reserve space in engine-output window in zippy mode
9414 flipView = appData.flipView;
9415 ClearPremoveHighlights();
9417 alarmSounded = FALSE;
9419 GameEnds((ChessMove) 0, NULL, GE_PLAYER);
9420 if(appData.serverMovesName != NULL) {
9421 /* [HGM] prepare to make moves file for broadcasting */
9422 clock_t t = clock();
9423 if(serverMoves != NULL) fclose(serverMoves);
9424 serverMoves = fopen(appData.serverMovesName, "r");
9425 if(serverMoves != NULL) {
9426 fclose(serverMoves);
9427 /* delay 15 sec before overwriting, so all clients can see end */
9428 while(clock()-t < appData.serverPause*CLOCKS_PER_SEC);
9430 serverMoves = fopen(appData.serverMovesName, "w");
9434 gameMode = BeginningOfGame;
9436 if(appData.icsActive) gameInfo.variant = VariantNormal;
9437 currentMove = forwardMostMove = backwardMostMove = 0;
9438 InitPosition(redraw);
9439 for (i = 0; i < MAX_MOVES; i++) {
9440 if (commentList[i] != NULL) {
9441 free(commentList[i]);
9442 commentList[i] = NULL;
9446 timeRemaining[0][0] = whiteTimeRemaining;
9447 timeRemaining[1][0] = blackTimeRemaining;
9448 if (first.pr == NULL) {
9449 StartChessProgram(&first);
9452 InitChessProgram(&first, startedFromSetupPosition);
9455 DisplayMessage("", "");
9456 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
9457 lastSavedGame = 0; // [HGM] save: make sure next game counts as unsaved
9464 if (!AutoPlayOneMove())
9466 if (matchMode || appData.timeDelay == 0)
9468 if (appData.timeDelay < 0 || gameMode == AnalyzeFile)
9470 StartLoadGameTimer((long)(1000.0 * appData.timeDelay));
9479 int fromX, fromY, toX, toY;
9481 if (appData.debugMode) {
9482 fprintf(debugFP, "AutoPlayOneMove(): current %d\n", currentMove);
9485 if (gameMode != PlayFromGameFile)
9488 if (currentMove >= forwardMostMove) {
9489 gameMode = EditGame;
9492 /* [AS] Clear current move marker at the end of a game */
9493 /* HistorySet(parseList, backwardMostMove, forwardMostMove, -1); */
9498 toX = moveList[currentMove][2] - AAA;
9499 toY = moveList[currentMove][3] - ONE;
9501 if (moveList[currentMove][1] == '@') {
9502 if (appData.highlightLastMove) {
9503 SetHighlights(-1, -1, toX, toY);
9506 fromX = moveList[currentMove][0] - AAA;
9507 fromY = moveList[currentMove][1] - ONE;
9509 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove); /* [AS] */
9511 AnimateMove(boards[currentMove], fromX, fromY, toX, toY);
9513 if (appData.highlightLastMove) {
9514 SetHighlights(fromX, fromY, toX, toY);
9517 DisplayMove(currentMove);
9518 SendMoveToProgram(currentMove++, &first);
9519 DisplayBothClocks();
9520 DrawPosition(FALSE, boards[currentMove]);
9521 // [HGM] PV info: always display, routine tests if empty
9522 DisplayComment(currentMove - 1, commentList[currentMove]);
9528 LoadGameOneMove(readAhead)
9529 ChessMove readAhead;
9531 int fromX = 0, fromY = 0, toX = 0, toY = 0, done;
9532 char promoChar = NULLCHAR;
9537 if (gameMode != PlayFromGameFile && gameMode != AnalyzeFile &&
9538 gameMode != AnalyzeMode && gameMode != Training) {
9543 yyboardindex = forwardMostMove;
9544 if (readAhead != (ChessMove)0) {
9545 moveType = readAhead;
9547 if (gameFileFP == NULL)
9549 moveType = (ChessMove) yylex();
9555 if (appData.debugMode)
9556 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
9559 /* append the comment but don't display it */
9560 AppendComment(currentMove, p, FALSE);
9563 case WhiteCapturesEnPassant:
9564 case BlackCapturesEnPassant:
9565 case WhitePromotion:
9566 case BlackPromotion:
9567 case WhiteNonPromotion:
9568 case BlackNonPromotion:
9570 case WhiteKingSideCastle:
9571 case WhiteQueenSideCastle:
9572 case BlackKingSideCastle:
9573 case BlackQueenSideCastle:
9574 case WhiteKingSideCastleWild:
9575 case WhiteQueenSideCastleWild:
9576 case BlackKingSideCastleWild:
9577 case BlackQueenSideCastleWild:
9579 case WhiteHSideCastleFR:
9580 case WhiteASideCastleFR:
9581 case BlackHSideCastleFR:
9582 case BlackASideCastleFR:
9584 if (appData.debugMode)
9585 fprintf(debugFP, "Parsed %s into %s\n", yy_text, currentMoveString);
9586 fromX = currentMoveString[0] - AAA;
9587 fromY = currentMoveString[1] - ONE;
9588 toX = currentMoveString[2] - AAA;
9589 toY = currentMoveString[3] - ONE;
9590 promoChar = currentMoveString[4];
9595 if (appData.debugMode)
9596 fprintf(debugFP, "Parsed %s into %s\n", yy_text, currentMoveString);
9597 fromX = moveType == WhiteDrop ?
9598 (int) CharToPiece(ToUpper(currentMoveString[0])) :
9599 (int) CharToPiece(ToLower(currentMoveString[0]));
9601 toX = currentMoveString[2] - AAA;
9602 toY = currentMoveString[3] - ONE;
9608 case GameUnfinished:
9609 if (appData.debugMode)
9610 fprintf(debugFP, "Parsed game end: %s\n", yy_text);
9611 p = strchr(yy_text, '{');
9612 if (p == NULL) p = strchr(yy_text, '(');
9615 if (p[0] == '0' || p[0] == '1' || p[0] == '*') p = "";
9617 q = strchr(p, *p == '{' ? '}' : ')');
9618 if (q != NULL) *q = NULLCHAR;
9621 GameEnds(moveType, p, GE_FILE);
9623 if (cmailMsgLoaded) {
9625 flipView = WhiteOnMove(currentMove);
9626 if (moveType == GameUnfinished) flipView = !flipView;
9627 if (appData.debugMode)
9628 fprintf(debugFP, "Setting flipView to %d\n", flipView) ;
9632 case (ChessMove) 0: /* end of file */
9633 if (appData.debugMode)
9634 fprintf(debugFP, "Parser hit end of file\n");
9635 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
9641 if (WhiteOnMove(currentMove)) {
9642 GameEnds(BlackWins, "Black mates", GE_FILE);
9644 GameEnds(WhiteWins, "White mates", GE_FILE);
9648 GameEnds(GameIsDrawn, "Stalemate", GE_FILE);
9655 if (lastLoadGameStart == GNUChessGame) {
9656 /* GNUChessGames have numbers, but they aren't move numbers */
9657 if (appData.debugMode)
9658 fprintf(debugFP, "Parser ignoring: '%s' (%d)\n",
9659 yy_text, (int) moveType);
9660 return LoadGameOneMove((ChessMove)0); /* tail recursion */
9662 /* else fall thru */
9667 /* Reached start of next game in file */
9668 if (appData.debugMode)
9669 fprintf(debugFP, "Parsed start of next game: %s\n", yy_text);
9670 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
9676 if (WhiteOnMove(currentMove)) {
9677 GameEnds(BlackWins, "Black mates", GE_FILE);
9679 GameEnds(WhiteWins, "White mates", GE_FILE);
9683 GameEnds(GameIsDrawn, "Stalemate", GE_FILE);
9689 case PositionDiagram: /* should not happen; ignore */
9690 case ElapsedTime: /* ignore */
9691 case NAG: /* ignore */
9692 if (appData.debugMode)
9693 fprintf(debugFP, "Parser ignoring: '%s' (%d)\n",
9694 yy_text, (int) moveType);
9695 return LoadGameOneMove((ChessMove)0); /* tail recursion */
9698 if (appData.testLegality) {
9699 if (appData.debugMode)
9700 fprintf(debugFP, "Parsed IllegalMove: %s\n", yy_text);
9701 sprintf(move, _("Illegal move: %d.%s%s"),
9702 (forwardMostMove / 2) + 1,
9703 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
9704 DisplayError(move, 0);
9707 if (appData.debugMode)
9708 fprintf(debugFP, "Parsed %s into IllegalMove %s\n",
9709 yy_text, currentMoveString);
9710 fromX = currentMoveString[0] - AAA;
9711 fromY = currentMoveString[1] - ONE;
9712 toX = currentMoveString[2] - AAA;
9713 toY = currentMoveString[3] - ONE;
9714 promoChar = currentMoveString[4];
9719 if (appData.debugMode)
9720 fprintf(debugFP, "Parsed AmbiguousMove: %s\n", yy_text);
9721 sprintf(move, _("Ambiguous move: %d.%s%s"),
9722 (forwardMostMove / 2) + 1,
9723 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
9724 DisplayError(move, 0);
9729 case ImpossibleMove:
9730 if (appData.debugMode)
9731 fprintf(debugFP, "Parsed ImpossibleMove (type = %d): %s\n", moveType, yy_text);
9732 sprintf(move, _("Illegal move: %d.%s%s"),
9733 (forwardMostMove / 2) + 1,
9734 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
9735 DisplayError(move, 0);
9741 if (appData.matchMode || (appData.timeDelay == 0 && !pausing)) {
9742 DrawPosition(FALSE, boards[currentMove]);
9743 DisplayBothClocks();
9744 if (!appData.matchMode) // [HGM] PV info: routine tests if empty
9745 DisplayComment(currentMove - 1, commentList[currentMove]);
9747 (void) StopLoadGameTimer();
9749 cmailOldMove = forwardMostMove;
9752 /* currentMoveString is set as a side-effect of yylex */
9753 strcat(currentMoveString, "\n");
9754 strcpy(moveList[forwardMostMove], currentMoveString);
9756 thinkOutput[0] = NULLCHAR;
9757 MakeMove(fromX, fromY, toX, toY, promoChar);
9758 currentMove = forwardMostMove;
9763 /* Load the nth game from the given file */
9765 LoadGameFromFile(filename, n, title, useList)
9769 /*Boolean*/ int useList;
9774 if (strcmp(filename, "-") == 0) {
9778 f = fopen(filename, "rb");
9780 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
9781 DisplayError(buf, errno);
9785 if (fseek(f, 0, 0) == -1) {
9786 /* f is not seekable; probably a pipe */
9789 if (useList && n == 0) {
9790 int error = GameListBuild(f);
9792 DisplayError(_("Cannot build game list"), error);
9793 } else if (!ListEmpty(&gameList) &&
9794 ((ListGame *) gameList.tailPred)->number > 1) {
9795 GameListPopUp(f, title);
9802 return LoadGame(f, n, title, FALSE);
9807 MakeRegisteredMove()
9809 int fromX, fromY, toX, toY;
9811 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
9812 switch (cmailMoveType[lastLoadGameNumber - 1]) {
9815 if (appData.debugMode)
9816 fprintf(debugFP, "Restoring %s for game %d\n",
9817 cmailMove[lastLoadGameNumber - 1], lastLoadGameNumber);
9819 thinkOutput[0] = NULLCHAR;
9820 strcpy(moveList[currentMove], cmailMove[lastLoadGameNumber - 1]);
9821 fromX = cmailMove[lastLoadGameNumber - 1][0] - AAA;
9822 fromY = cmailMove[lastLoadGameNumber - 1][1] - ONE;
9823 toX = cmailMove[lastLoadGameNumber - 1][2] - AAA;
9824 toY = cmailMove[lastLoadGameNumber - 1][3] - ONE;
9825 promoChar = cmailMove[lastLoadGameNumber - 1][4];
9826 MakeMove(fromX, fromY, toX, toY, promoChar);
9827 ShowMove(fromX, fromY, toX, toY);
9829 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
9836 if (WhiteOnMove(currentMove)) {
9837 GameEnds(BlackWins, "Black mates", GE_PLAYER);
9839 GameEnds(WhiteWins, "White mates", GE_PLAYER);
9844 GameEnds(GameIsDrawn, "Stalemate", GE_PLAYER);
9851 if (WhiteOnMove(currentMove)) {
9852 GameEnds(BlackWins, "White resigns", GE_PLAYER);
9854 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
9859 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
9870 /* Wrapper around LoadGame for use when a Cmail message is loaded */
9872 CmailLoadGame(f, gameNumber, title, useList)
9880 if (gameNumber > nCmailGames) {
9881 DisplayError(_("No more games in this message"), 0);
9884 if (f == lastLoadGameFP) {
9885 int offset = gameNumber - lastLoadGameNumber;
9887 cmailMsg[0] = NULLCHAR;
9888 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
9889 cmailMoveRegistered[lastLoadGameNumber - 1] = FALSE;
9890 nCmailMovesRegistered--;
9892 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
9893 if (cmailResult[lastLoadGameNumber - 1] == CMAIL_NEW_RESULT) {
9894 cmailResult[lastLoadGameNumber - 1] = CMAIL_NOT_RESULT;
9897 if (! RegisterMove()) return FALSE;
9901 retVal = LoadGame(f, gameNumber, title, useList);
9903 /* Make move registered during previous look at this game, if any */
9904 MakeRegisteredMove();
9906 if (cmailCommentList[lastLoadGameNumber - 1] != NULL) {
9907 commentList[currentMove]
9908 = StrSave(cmailCommentList[lastLoadGameNumber - 1]);
9909 DisplayComment(currentMove - 1, commentList[currentMove]);
9915 /* Support for LoadNextGame, LoadPreviousGame, ReloadSameGame */
9920 int gameNumber = lastLoadGameNumber + offset;
9921 if (lastLoadGameFP == NULL) {
9922 DisplayError(_("No game has been loaded yet"), 0);
9925 if (gameNumber <= 0) {
9926 DisplayError(_("Can't back up any further"), 0);
9929 if (cmailMsgLoaded) {
9930 return CmailLoadGame(lastLoadGameFP, gameNumber,
9931 lastLoadGameTitle, lastLoadGameUseList);
9933 return LoadGame(lastLoadGameFP, gameNumber,
9934 lastLoadGameTitle, lastLoadGameUseList);
9940 /* Load the nth game from open file f */
9942 LoadGame(f, gameNumber, title, useList)
9950 int gn = gameNumber;
9951 ListGame *lg = NULL;
9954 GameMode oldGameMode;
9955 VariantClass oldVariant = gameInfo.variant; /* [HGM] PGNvariant */
9957 if (appData.debugMode)
9958 fprintf(debugFP, "LoadGame(): on entry, gameMode %d\n", gameMode);
9960 if (gameMode == Training )
9961 SetTrainingModeOff();
9963 oldGameMode = gameMode;
9964 if (gameMode != BeginningOfGame) {
9969 if (lastLoadGameFP != NULL && lastLoadGameFP != f) {
9970 fclose(lastLoadGameFP);
9974 lg = (ListGame *) ListElem(&gameList, gameNumber-1);
9977 fseek(f, lg->offset, 0);
9978 GameListHighlight(gameNumber);
9982 DisplayError(_("Game number out of range"), 0);
9987 if (fseek(f, 0, 0) == -1) {
9988 if (f == lastLoadGameFP ?
9989 gameNumber == lastLoadGameNumber + 1 :
9993 DisplayError(_("Can't seek on game file"), 0);
9999 lastLoadGameNumber = gameNumber;
10000 strcpy(lastLoadGameTitle, title);
10001 lastLoadGameUseList = useList;
10005 if (lg && lg->gameInfo.white && lg->gameInfo.black) {
10006 snprintf(buf, sizeof(buf), "%s vs. %s", lg->gameInfo.white,
10007 lg->gameInfo.black);
10009 } else if (*title != NULLCHAR) {
10010 if (gameNumber > 1) {
10011 sprintf(buf, "%s %d", title, gameNumber);
10014 DisplayTitle(title);
10018 if (gameMode != AnalyzeFile && gameMode != AnalyzeMode) {
10019 gameMode = PlayFromGameFile;
10023 currentMove = forwardMostMove = backwardMostMove = 0;
10024 CopyBoard(boards[0], initialPosition);
10028 * Skip the first gn-1 games in the file.
10029 * Also skip over anything that precedes an identifiable
10030 * start of game marker, to avoid being confused by
10031 * garbage at the start of the file. Currently
10032 * recognized start of game markers are the move number "1",
10033 * the pattern "gnuchess .* game", the pattern
10034 * "^[#;%] [^ ]* game file", and a PGN tag block.
10035 * A game that starts with one of the latter two patterns
10036 * will also have a move number 1, possibly
10037 * following a position diagram.
10038 * 5-4-02: Let's try being more lenient and allowing a game to
10039 * start with an unnumbered move. Does that break anything?
10041 cm = lastLoadGameStart = (ChessMove) 0;
10043 yyboardindex = forwardMostMove;
10044 cm = (ChessMove) yylex();
10046 case (ChessMove) 0:
10047 if (cmailMsgLoaded) {
10048 nCmailGames = CMAIL_MAX_GAMES - gn;
10051 DisplayError(_("Game not found in file"), 0);
10058 lastLoadGameStart = cm;
10061 case MoveNumberOne:
10062 switch (lastLoadGameStart) {
10067 case MoveNumberOne:
10068 case (ChessMove) 0:
10069 gn--; /* count this game */
10070 lastLoadGameStart = cm;
10079 switch (lastLoadGameStart) {
10082 case MoveNumberOne:
10083 case (ChessMove) 0:
10084 gn--; /* count this game */
10085 lastLoadGameStart = cm;
10088 lastLoadGameStart = cm; /* game counted already */
10096 yyboardindex = forwardMostMove;
10097 cm = (ChessMove) yylex();
10098 } while (cm == PGNTag || cm == Comment);
10105 if (cmailMsgLoaded && (CMAIL_MAX_GAMES == lastLoadGameNumber)) {
10106 if ( cmailResult[CMAIL_MAX_GAMES - gn - 1]
10107 != CMAIL_OLD_RESULT) {
10109 cmailResult[ CMAIL_MAX_GAMES
10110 - gn - 1] = CMAIL_OLD_RESULT;
10116 /* Only a NormalMove can be at the start of a game
10117 * without a position diagram. */
10118 if (lastLoadGameStart == (ChessMove) 0) {
10120 lastLoadGameStart = MoveNumberOne;
10129 if (appData.debugMode)
10130 fprintf(debugFP, "Parsed game start '%s' (%d)\n", yy_text, (int) cm);
10132 if (cm == XBoardGame) {
10133 /* Skip any header junk before position diagram and/or move 1 */
10135 yyboardindex = forwardMostMove;
10136 cm = (ChessMove) yylex();
10138 if (cm == (ChessMove) 0 ||
10139 cm == GNUChessGame || cm == XBoardGame) {
10140 /* Empty game; pretend end-of-file and handle later */
10141 cm = (ChessMove) 0;
10145 if (cm == MoveNumberOne || cm == PositionDiagram ||
10146 cm == PGNTag || cm == Comment)
10149 } else if (cm == GNUChessGame) {
10150 if (gameInfo.event != NULL) {
10151 free(gameInfo.event);
10153 gameInfo.event = StrSave(yy_text);
10156 startedFromSetupPosition = FALSE;
10157 while (cm == PGNTag) {
10158 if (appData.debugMode)
10159 fprintf(debugFP, "Parsed PGNTag: %s\n", yy_text);
10160 err = ParsePGNTag(yy_text, &gameInfo);
10161 if (!err) numPGNTags++;
10163 /* [HGM] PGNvariant: automatically switch to variant given in PGN tag */
10164 if(gameInfo.variant != oldVariant) {
10165 startedFromPositionFile = FALSE; /* [HGM] loadPos: variant switch likely makes position invalid */
10166 ResetFrontEnd(); // [HGM] might need other bitmaps. Cannot use Reset() because it clears gameInfo :-(
10167 InitPosition(TRUE);
10168 oldVariant = gameInfo.variant;
10169 if (appData.debugMode)
10170 fprintf(debugFP, "New variant %d\n", (int) oldVariant);
10174 if (gameInfo.fen != NULL) {
10175 Board initial_position;
10176 startedFromSetupPosition = TRUE;
10177 if (!ParseFEN(initial_position, &blackPlaysFirst, gameInfo.fen)) {
10179 DisplayError(_("Bad FEN position in file"), 0);
10182 CopyBoard(boards[0], initial_position);
10183 if (blackPlaysFirst) {
10184 currentMove = forwardMostMove = backwardMostMove = 1;
10185 CopyBoard(boards[1], initial_position);
10186 strcpy(moveList[0], "");
10187 strcpy(parseList[0], "");
10188 timeRemaining[0][1] = whiteTimeRemaining;
10189 timeRemaining[1][1] = blackTimeRemaining;
10190 if (commentList[0] != NULL) {
10191 commentList[1] = commentList[0];
10192 commentList[0] = NULL;
10195 currentMove = forwardMostMove = backwardMostMove = 0;
10197 /* [HGM] copy FEN attributes as well. Bugfix 4.3.14m and 4.3.15e: moved to after 'blackPlaysFirst' */
10199 initialRulePlies = FENrulePlies;
10200 for( i=0; i< nrCastlingRights; i++ )
10201 initialRights[i] = initial_position[CASTLING][i];
10203 yyboardindex = forwardMostMove;
10204 free(gameInfo.fen);
10205 gameInfo.fen = NULL;
10208 yyboardindex = forwardMostMove;
10209 cm = (ChessMove) yylex();
10211 /* Handle comments interspersed among the tags */
10212 while (cm == Comment) {
10214 if (appData.debugMode)
10215 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
10217 AppendComment(currentMove, p, FALSE);
10218 yyboardindex = forwardMostMove;
10219 cm = (ChessMove) yylex();
10223 /* don't rely on existence of Event tag since if game was
10224 * pasted from clipboard the Event tag may not exist
10226 if (numPGNTags > 0){
10228 if (gameInfo.variant == VariantNormal) {
10229 VariantClass v = StringToVariant(gameInfo.event);
10230 // [HGM] do not recognize variants from event tag that were introduced after supporting variant tag
10231 if(v < VariantShogi) gameInfo.variant = v;
10234 if( appData.autoDisplayTags ) {
10235 tags = PGNTags(&gameInfo);
10236 TagsPopUp(tags, CmailMsg());
10241 /* Make something up, but don't display it now */
10246 if (cm == PositionDiagram) {
10249 Board initial_position;
10251 if (appData.debugMode)
10252 fprintf(debugFP, "Parsed PositionDiagram: %s\n", yy_text);
10254 if (!startedFromSetupPosition) {
10256 for (i = BOARD_HEIGHT - 1; i >= 0; i--)
10257 for (j = BOARD_LEFT; j < BOARD_RGHT; p++)
10267 initial_position[i][j++] = CharToPiece(*p);
10270 while (*p == ' ' || *p == '\t' ||
10271 *p == '\n' || *p == '\r') p++;
10273 if (strncmp(p, "black", strlen("black"))==0)
10274 blackPlaysFirst = TRUE;
10276 blackPlaysFirst = FALSE;
10277 startedFromSetupPosition = TRUE;
10279 CopyBoard(boards[0], initial_position);
10280 if (blackPlaysFirst) {
10281 currentMove = forwardMostMove = backwardMostMove = 1;
10282 CopyBoard(boards[1], initial_position);
10283 strcpy(moveList[0], "");
10284 strcpy(parseList[0], "");
10285 timeRemaining[0][1] = whiteTimeRemaining;
10286 timeRemaining[1][1] = blackTimeRemaining;
10287 if (commentList[0] != NULL) {
10288 commentList[1] = commentList[0];
10289 commentList[0] = NULL;
10292 currentMove = forwardMostMove = backwardMostMove = 0;
10295 yyboardindex = forwardMostMove;
10296 cm = (ChessMove) yylex();
10299 if (first.pr == NoProc) {
10300 StartChessProgram(&first);
10302 InitChessProgram(&first, FALSE);
10303 SendToProgram("force\n", &first);
10304 if (startedFromSetupPosition) {
10305 SendBoard(&first, forwardMostMove);
10306 if (appData.debugMode) {
10307 fprintf(debugFP, "Load Game\n");
10309 DisplayBothClocks();
10312 /* [HGM] server: flag to write setup moves in broadcast file as one */
10313 loadFlag = appData.suppressLoadMoves;
10315 while (cm == Comment) {
10317 if (appData.debugMode)
10318 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
10320 AppendComment(currentMove, p, FALSE);
10321 yyboardindex = forwardMostMove;
10322 cm = (ChessMove) yylex();
10325 if ((cm == (ChessMove) 0 && lastLoadGameStart != (ChessMove) 0) ||
10326 cm == WhiteWins || cm == BlackWins ||
10327 cm == GameIsDrawn || cm == GameUnfinished) {
10328 DisplayMessage("", _("No moves in game"));
10329 if (cmailMsgLoaded) {
10330 if (appData.debugMode)
10331 fprintf(debugFP, "Setting flipView to %d.\n", FALSE);
10335 DrawPosition(FALSE, boards[currentMove]);
10336 DisplayBothClocks();
10337 gameMode = EditGame;
10344 // [HGM] PV info: routine tests if comment empty
10345 if (!matchMode && (pausing || appData.timeDelay != 0)) {
10346 DisplayComment(currentMove - 1, commentList[currentMove]);
10348 if (!matchMode && appData.timeDelay != 0)
10349 DrawPosition(FALSE, boards[currentMove]);
10351 if (gameMode == AnalyzeFile || gameMode == AnalyzeMode) {
10352 programStats.ok_to_send = 1;
10355 /* if the first token after the PGN tags is a move
10356 * and not move number 1, retrieve it from the parser
10358 if (cm != MoveNumberOne)
10359 LoadGameOneMove(cm);
10361 /* load the remaining moves from the file */
10362 while (LoadGameOneMove((ChessMove)0)) {
10363 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
10364 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
10367 /* rewind to the start of the game */
10368 currentMove = backwardMostMove;
10370 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
10372 if (oldGameMode == AnalyzeFile ||
10373 oldGameMode == AnalyzeMode) {
10374 AnalyzeFileEvent();
10377 if (matchMode || appData.timeDelay == 0) {
10379 gameMode = EditGame;
10381 } else if (appData.timeDelay > 0) {
10382 AutoPlayGameLoop();
10385 if (appData.debugMode)
10386 fprintf(debugFP, "LoadGame(): on exit, gameMode %d\n", gameMode);
10388 loadFlag = 0; /* [HGM] true game starts */
10392 /* Support for LoadNextPosition, LoadPreviousPosition, ReloadSamePosition */
10394 ReloadPosition(offset)
10397 int positionNumber = lastLoadPositionNumber + offset;
10398 if (lastLoadPositionFP == NULL) {
10399 DisplayError(_("No position has been loaded yet"), 0);
10402 if (positionNumber <= 0) {
10403 DisplayError(_("Can't back up any further"), 0);
10406 return LoadPosition(lastLoadPositionFP, positionNumber,
10407 lastLoadPositionTitle);
10410 /* Load the nth position from the given file */
10412 LoadPositionFromFile(filename, n, title)
10420 if (strcmp(filename, "-") == 0) {
10421 return LoadPosition(stdin, n, "stdin");
10423 f = fopen(filename, "rb");
10425 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
10426 DisplayError(buf, errno);
10429 return LoadPosition(f, n, title);
10434 /* Load the nth position from the given open file, and close it */
10436 LoadPosition(f, positionNumber, title)
10438 int positionNumber;
10441 char *p, line[MSG_SIZ];
10442 Board initial_position;
10443 int i, j, fenMode, pn;
10445 if (gameMode == Training )
10446 SetTrainingModeOff();
10448 if (gameMode != BeginningOfGame) {
10449 Reset(FALSE, TRUE);
10451 if (lastLoadPositionFP != NULL && lastLoadPositionFP != f) {
10452 fclose(lastLoadPositionFP);
10454 if (positionNumber == 0) positionNumber = 1;
10455 lastLoadPositionFP = f;
10456 lastLoadPositionNumber = positionNumber;
10457 strcpy(lastLoadPositionTitle, title);
10458 if (first.pr == NoProc) {
10459 StartChessProgram(&first);
10460 InitChessProgram(&first, FALSE);
10462 pn = positionNumber;
10463 if (positionNumber < 0) {
10464 /* Negative position number means to seek to that byte offset */
10465 if (fseek(f, -positionNumber, 0) == -1) {
10466 DisplayError(_("Can't seek on position file"), 0);
10471 if (fseek(f, 0, 0) == -1) {
10472 if (f == lastLoadPositionFP ?
10473 positionNumber == lastLoadPositionNumber + 1 :
10474 positionNumber == 1) {
10477 DisplayError(_("Can't seek on position file"), 0);
10482 /* See if this file is FEN or old-style xboard */
10483 if (fgets(line, MSG_SIZ, f) == NULL) {
10484 DisplayError(_("Position not found in file"), 0);
10487 // [HGM] FEN can begin with digit, any piece letter valid in this variant, or a + for Shogi promoted pieces
10488 fenMode = line[0] >= '0' && line[0] <= '9' || line[0] == '+' || CharToPiece(line[0]) != EmptySquare;
10491 if (fenMode || line[0] == '#') pn--;
10493 /* skip positions before number pn */
10494 if (fgets(line, MSG_SIZ, f) == NULL) {
10496 DisplayError(_("Position not found in file"), 0);
10499 if (fenMode || line[0] == '#') pn--;
10504 if (!ParseFEN(initial_position, &blackPlaysFirst, line)) {
10505 DisplayError(_("Bad FEN position in file"), 0);
10509 (void) fgets(line, MSG_SIZ, f);
10510 (void) fgets(line, MSG_SIZ, f);
10512 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
10513 (void) fgets(line, MSG_SIZ, f);
10514 for (p = line, j = BOARD_LEFT; j < BOARD_RGHT; p++) {
10517 initial_position[i][j++] = CharToPiece(*p);
10521 blackPlaysFirst = FALSE;
10523 (void) fgets(line, MSG_SIZ, f);
10524 if (strncmp(line, "black", strlen("black"))==0)
10525 blackPlaysFirst = TRUE;
10528 startedFromSetupPosition = TRUE;
10530 SendToProgram("force\n", &first);
10531 CopyBoard(boards[0], initial_position);
10532 if (blackPlaysFirst) {
10533 currentMove = forwardMostMove = backwardMostMove = 1;
10534 strcpy(moveList[0], "");
10535 strcpy(parseList[0], "");
10536 CopyBoard(boards[1], initial_position);
10537 DisplayMessage("", _("Black to play"));
10539 currentMove = forwardMostMove = backwardMostMove = 0;
10540 DisplayMessage("", _("White to play"));
10542 initialRulePlies = FENrulePlies; /* [HGM] copy FEN attributes as well */
10543 SendBoard(&first, forwardMostMove);
10544 if (appData.debugMode) {
10546 for(i=0;i<2;i++){for(j=0;j<6;j++)fprintf(debugFP, " %d", boards[i][CASTLING][j]);fprintf(debugFP,"\n");}
10547 for(j=0;j<6;j++)fprintf(debugFP, " %d", initialRights[j]);fprintf(debugFP,"\n");
10548 fprintf(debugFP, "Load Position\n");
10551 if (positionNumber > 1) {
10552 sprintf(line, "%s %d", title, positionNumber);
10553 DisplayTitle(line);
10555 DisplayTitle(title);
10557 gameMode = EditGame;
10560 timeRemaining[0][1] = whiteTimeRemaining;
10561 timeRemaining[1][1] = blackTimeRemaining;
10562 DrawPosition(FALSE, boards[currentMove]);
10569 CopyPlayerNameIntoFileName(dest, src)
10572 while (*src != NULLCHAR && *src != ',') {
10577 *(*dest)++ = *src++;
10582 char *DefaultFileName(ext)
10585 static char def[MSG_SIZ];
10588 if (gameInfo.white != NULL && gameInfo.white[0] != '-') {
10590 CopyPlayerNameIntoFileName(&p, gameInfo.white);
10592 CopyPlayerNameIntoFileName(&p, gameInfo.black);
10601 /* Save the current game to the given file */
10603 SaveGameToFile(filename, append)
10610 if (strcmp(filename, "-") == 0) {
10611 return SaveGame(stdout, 0, NULL);
10613 f = fopen(filename, append ? "a" : "w");
10615 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
10616 DisplayError(buf, errno);
10619 return SaveGame(f, 0, NULL);
10628 static char buf[MSG_SIZ];
10631 p = strchr(str, ' ');
10632 if (p == NULL) return str;
10633 strncpy(buf, str, p - str);
10634 buf[p - str] = NULLCHAR;
10638 #define PGN_MAX_LINE 75
10640 #define PGN_SIDE_WHITE 0
10641 #define PGN_SIDE_BLACK 1
10644 static int FindFirstMoveOutOfBook( int side )
10648 if( backwardMostMove == 0 && ! startedFromSetupPosition) {
10649 int index = backwardMostMove;
10650 int has_book_hit = 0;
10652 if( (index % 2) != side ) {
10656 while( index < forwardMostMove ) {
10657 /* Check to see if engine is in book */
10658 int depth = pvInfoList[index].depth;
10659 int score = pvInfoList[index].score;
10665 else if( score == 0 && depth == 63 ) {
10666 in_book = 1; /* Zappa */
10668 else if( score == 2 && depth == 99 ) {
10669 in_book = 1; /* Abrok */
10672 has_book_hit += in_book;
10688 void GetOutOfBookInfo( char * buf )
10692 int offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
10694 oob[0] = FindFirstMoveOutOfBook( PGN_SIDE_WHITE );
10695 oob[1] = FindFirstMoveOutOfBook( PGN_SIDE_BLACK );
10699 if( oob[0] >= 0 || oob[1] >= 0 ) {
10700 for( i=0; i<2; i++ ) {
10704 if( i > 0 && oob[0] >= 0 ) {
10705 strcat( buf, " " );
10708 sprintf( buf+strlen(buf), "%d%s. ", (idx - offset)/2 + 1, idx & 1 ? ".." : "" );
10709 sprintf( buf+strlen(buf), "%s%.2f",
10710 pvInfoList[idx].score >= 0 ? "+" : "",
10711 pvInfoList[idx].score / 100.0 );
10717 /* Save game in PGN style and close the file */
10722 int i, offset, linelen, newblock;
10726 int movelen, numlen, blank;
10727 char move_buffer[100]; /* [AS] Buffer for move+PV info */
10729 offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
10731 tm = time((time_t *) NULL);
10733 PrintPGNTags(f, &gameInfo);
10735 if (backwardMostMove > 0 || startedFromSetupPosition) {
10736 char *fen = PositionToFEN(backwardMostMove, NULL);
10737 fprintf(f, "[FEN \"%s\"]\n[SetUp \"1\"]\n", fen);
10738 fprintf(f, "\n{--------------\n");
10739 PrintPosition(f, backwardMostMove);
10740 fprintf(f, "--------------}\n");
10744 /* [AS] Out of book annotation */
10745 if( appData.saveOutOfBookInfo ) {
10748 GetOutOfBookInfo( buf );
10750 if( buf[0] != '\0' ) {
10751 fprintf( f, "[%s \"%s\"]\n", PGN_OUT_OF_BOOK, buf );
10758 i = backwardMostMove;
10762 while (i < forwardMostMove) {
10763 /* Print comments preceding this move */
10764 if (commentList[i] != NULL) {
10765 if (linelen > 0) fprintf(f, "\n");
10766 fprintf(f, "%s", commentList[i]);
10771 /* Format move number */
10772 if ((i % 2) == 0) {
10773 sprintf(numtext, "%d.", (i - offset)/2 + 1);
10776 sprintf(numtext, "%d...", (i - offset)/2 + 1);
10778 numtext[0] = NULLCHAR;
10781 numlen = strlen(numtext);
10784 /* Print move number */
10785 blank = linelen > 0 && numlen > 0;
10786 if (linelen + (blank ? 1 : 0) + numlen > PGN_MAX_LINE) {
10795 fprintf(f, "%s", numtext);
10799 strcpy(move_buffer, SavePart(parseList[i])); // [HGM] pgn: print move via buffer, so it can be edited
10800 movelen = strlen(move_buffer); /* [HGM] pgn: line-break point before move */
10803 blank = linelen > 0 && movelen > 0;
10804 if (linelen + (blank ? 1 : 0) + movelen > PGN_MAX_LINE) {
10813 fprintf(f, "%s", move_buffer);
10814 linelen += movelen;
10816 /* [AS] Add PV info if present */
10817 if( i >= 0 && appData.saveExtendedInfoInPGN && pvInfoList[i].depth > 0 ) {
10818 /* [HGM] add time */
10819 char buf[MSG_SIZ]; int seconds;
10821 seconds = (pvInfoList[i].time+5)/10; // deci-seconds, rounded to nearest
10823 if( seconds <= 0) buf[0] = 0; else
10824 if( seconds < 30 ) sprintf(buf, " %3.1f%c", seconds/10., 0); else {
10825 seconds = (seconds + 4)/10; // round to full seconds
10826 if( seconds < 60 ) sprintf(buf, " %d%c", seconds, 0); else
10827 sprintf(buf, " %d:%02d%c", seconds/60, seconds%60, 0);
10830 sprintf( move_buffer, "{%s%.2f/%d%s}",
10831 pvInfoList[i].score >= 0 ? "+" : "",
10832 pvInfoList[i].score / 100.0,
10833 pvInfoList[i].depth,
10836 movelen = strlen(move_buffer); /* [HGM] pgn: line-break point after move */
10838 /* Print score/depth */
10839 blank = linelen > 0 && movelen > 0;
10840 if (linelen + (blank ? 1 : 0) + movelen > PGN_MAX_LINE) {
10849 fprintf(f, "%s", move_buffer);
10850 linelen += movelen;
10856 /* Start a new line */
10857 if (linelen > 0) fprintf(f, "\n");
10859 /* Print comments after last move */
10860 if (commentList[i] != NULL) {
10861 fprintf(f, "%s\n", commentList[i]);
10865 if (gameInfo.resultDetails != NULL &&
10866 gameInfo.resultDetails[0] != NULLCHAR) {
10867 fprintf(f, "{%s} %s\n\n", gameInfo.resultDetails,
10868 PGNResult(gameInfo.result));
10870 fprintf(f, "%s\n\n", PGNResult(gameInfo.result));
10874 lastSavedGame = GameCheckSum(); // [HGM] save: remember ID of last saved game to prevent double saving
10878 /* Save game in old style and close the file */
10880 SaveGameOldStyle(f)
10886 tm = time((time_t *) NULL);
10888 fprintf(f, "# %s game file -- %s", programName, ctime(&tm));
10891 if (backwardMostMove > 0 || startedFromSetupPosition) {
10892 fprintf(f, "\n[--------------\n");
10893 PrintPosition(f, backwardMostMove);
10894 fprintf(f, "--------------]\n");
10899 i = backwardMostMove;
10900 offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
10902 while (i < forwardMostMove) {
10903 if (commentList[i] != NULL) {
10904 fprintf(f, "[%s]\n", commentList[i]);
10907 if ((i % 2) == 1) {
10908 fprintf(f, "%d. ... %s\n", (i - offset)/2 + 1, parseList[i]);
10911 fprintf(f, "%d. %s ", (i - offset)/2 + 1, parseList[i]);
10913 if (commentList[i] != NULL) {
10917 if (i >= forwardMostMove) {
10921 fprintf(f, "%s\n", parseList[i]);
10926 if (commentList[i] != NULL) {
10927 fprintf(f, "[%s]\n", commentList[i]);
10930 /* This isn't really the old style, but it's close enough */
10931 if (gameInfo.resultDetails != NULL &&
10932 gameInfo.resultDetails[0] != NULLCHAR) {
10933 fprintf(f, "%s (%s)\n\n", PGNResult(gameInfo.result),
10934 gameInfo.resultDetails);
10936 fprintf(f, "%s\n\n", PGNResult(gameInfo.result));
10943 /* Save the current game to open file f and close the file */
10945 SaveGame(f, dummy, dummy2)
10950 if (gameMode == EditPosition) EditPositionDone(TRUE);
10951 lastSavedGame = GameCheckSum(); // [HGM] save: remember ID of last saved game to prevent double saving
10952 if (appData.oldSaveStyle)
10953 return SaveGameOldStyle(f);
10955 return SaveGamePGN(f);
10958 /* Save the current position to the given file */
10960 SavePositionToFile(filename)
10966 if (strcmp(filename, "-") == 0) {
10967 return SavePosition(stdout, 0, NULL);
10969 f = fopen(filename, "a");
10971 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
10972 DisplayError(buf, errno);
10975 SavePosition(f, 0, NULL);
10981 /* Save the current position to the given open file and close the file */
10983 SavePosition(f, dummy, dummy2)
10991 if (gameMode == EditPosition) EditPositionDone(TRUE);
10992 if (appData.oldSaveStyle) {
10993 tm = time((time_t *) NULL);
10995 fprintf(f, "# %s position file -- %s", programName, ctime(&tm));
10997 fprintf(f, "[--------------\n");
10998 PrintPosition(f, currentMove);
10999 fprintf(f, "--------------]\n");
11001 fen = PositionToFEN(currentMove, NULL);
11002 fprintf(f, "%s\n", fen);
11010 ReloadCmailMsgEvent(unregister)
11014 static char *inFilename = NULL;
11015 static char *outFilename;
11017 struct stat inbuf, outbuf;
11020 /* Any registered moves are unregistered if unregister is set, */
11021 /* i.e. invoked by the signal handler */
11023 for (i = 0; i < CMAIL_MAX_GAMES; i ++) {
11024 cmailMoveRegistered[i] = FALSE;
11025 if (cmailCommentList[i] != NULL) {
11026 free(cmailCommentList[i]);
11027 cmailCommentList[i] = NULL;
11030 nCmailMovesRegistered = 0;
11033 for (i = 0; i < CMAIL_MAX_GAMES; i ++) {
11034 cmailResult[i] = CMAIL_NOT_RESULT;
11038 if (inFilename == NULL) {
11039 /* Because the filenames are static they only get malloced once */
11040 /* and they never get freed */
11041 inFilename = (char *) malloc(strlen(appData.cmailGameName) + 9);
11042 sprintf(inFilename, "%s.game.in", appData.cmailGameName);
11044 outFilename = (char *) malloc(strlen(appData.cmailGameName) + 5);
11045 sprintf(outFilename, "%s.out", appData.cmailGameName);
11048 status = stat(outFilename, &outbuf);
11050 cmailMailedMove = FALSE;
11052 status = stat(inFilename, &inbuf);
11053 cmailMailedMove = (inbuf.st_mtime < outbuf.st_mtime);
11056 /* LoadGameFromFile(CMAIL_MAX_GAMES) with cmailMsgLoaded == TRUE
11057 counts the games, notes how each one terminated, etc.
11059 It would be nice to remove this kludge and instead gather all
11060 the information while building the game list. (And to keep it
11061 in the game list nodes instead of having a bunch of fixed-size
11062 parallel arrays.) Note this will require getting each game's
11063 termination from the PGN tags, as the game list builder does
11064 not process the game moves. --mann
11066 cmailMsgLoaded = TRUE;
11067 LoadGameFromFile(inFilename, CMAIL_MAX_GAMES, "", FALSE);
11069 /* Load first game in the file or popup game menu */
11070 LoadGameFromFile(inFilename, 0, appData.cmailGameName, TRUE);
11072 #endif /* !WIN32 */
11080 char string[MSG_SIZ];
11082 if ( cmailMailedMove
11083 || (cmailResult[lastLoadGameNumber - 1] == CMAIL_OLD_RESULT)) {
11084 return TRUE; /* Allow free viewing */
11087 /* Unregister move to ensure that we don't leave RegisterMove */
11088 /* with the move registered when the conditions for registering no */
11090 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
11091 cmailMoveRegistered[lastLoadGameNumber - 1] = FALSE;
11092 nCmailMovesRegistered --;
11094 if (cmailCommentList[lastLoadGameNumber - 1] != NULL)
11096 free(cmailCommentList[lastLoadGameNumber - 1]);
11097 cmailCommentList[lastLoadGameNumber - 1] = NULL;
11101 if (cmailOldMove == -1) {
11102 DisplayError(_("You have edited the game history.\nUse Reload Same Game and make your move again."), 0);
11106 if (currentMove > cmailOldMove + 1) {
11107 DisplayError(_("You have entered too many moves.\nBack up to the correct position and try again."), 0);
11111 if (currentMove < cmailOldMove) {
11112 DisplayError(_("Displayed position is not current.\nStep forward to the correct position and try again."), 0);
11116 if (forwardMostMove > currentMove) {
11117 /* Silently truncate extra moves */
11121 if ( (currentMove == cmailOldMove + 1)
11122 || ( (currentMove == cmailOldMove)
11123 && ( (cmailMoveType[lastLoadGameNumber - 1] == CMAIL_ACCEPT)
11124 || (cmailMoveType[lastLoadGameNumber - 1] == CMAIL_RESIGN)))) {
11125 if (gameInfo.result != GameUnfinished) {
11126 cmailResult[lastLoadGameNumber - 1] = CMAIL_NEW_RESULT;
11129 if (commentList[currentMove] != NULL) {
11130 cmailCommentList[lastLoadGameNumber - 1]
11131 = StrSave(commentList[currentMove]);
11133 strcpy(cmailMove[lastLoadGameNumber - 1], moveList[currentMove - 1]);
11135 if (appData.debugMode)
11136 fprintf(debugFP, "Saving %s for game %d\n",
11137 cmailMove[lastLoadGameNumber - 1], lastLoadGameNumber);
11140 "%s.game.out.%d", appData.cmailGameName, lastLoadGameNumber);
11142 f = fopen(string, "w");
11143 if (appData.oldSaveStyle) {
11144 SaveGameOldStyle(f); /* also closes the file */
11146 sprintf(string, "%s.pos.out", appData.cmailGameName);
11147 f = fopen(string, "w");
11148 SavePosition(f, 0, NULL); /* also closes the file */
11150 fprintf(f, "{--------------\n");
11151 PrintPosition(f, currentMove);
11152 fprintf(f, "--------------}\n\n");
11154 SaveGame(f, 0, NULL); /* also closes the file*/
11157 cmailMoveRegistered[lastLoadGameNumber - 1] = TRUE;
11158 nCmailMovesRegistered ++;
11159 } else if (nCmailGames == 1) {
11160 DisplayError(_("You have not made a move yet"), 0);
11171 static char *partCommandString = "cmail -xv%s -remail -game %s 2>&1";
11172 FILE *commandOutput;
11173 char buffer[MSG_SIZ], msg[MSG_SIZ], string[MSG_SIZ];
11174 int nBytes = 0; /* Suppress warnings on uninitialized variables */
11180 if (! cmailMsgLoaded) {
11181 DisplayError(_("The cmail message is not loaded.\nUse Reload CMail Message and make your move again."), 0);
11185 if (nCmailGames == nCmailResults) {
11186 DisplayError(_("No unfinished games"), 0);
11190 #if CMAIL_PROHIBIT_REMAIL
11191 if (cmailMailedMove) {
11192 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);
11193 DisplayError(msg, 0);
11198 if (! (cmailMailedMove || RegisterMove())) return;
11200 if ( cmailMailedMove
11201 || (nCmailMovesRegistered + nCmailResults == nCmailGames)) {
11202 sprintf(string, partCommandString,
11203 appData.debugMode ? " -v" : "", appData.cmailGameName);
11204 commandOutput = popen(string, "r");
11206 if (commandOutput == NULL) {
11207 DisplayError(_("Failed to invoke cmail"), 0);
11209 for (nBuffers = 0; (! feof(commandOutput)); nBuffers ++) {
11210 nBytes = fread(buffer, 1, MSG_SIZ - 1, commandOutput);
11212 if (nBuffers > 1) {
11213 (void) memcpy(msg, buffer + nBytes, MSG_SIZ - nBytes - 1);
11214 (void) memcpy(msg + MSG_SIZ - nBytes - 1, buffer, nBytes);
11215 nBytes = MSG_SIZ - 1;
11217 (void) memcpy(msg, buffer, nBytes);
11219 *(msg + nBytes) = '\0'; /* \0 for end-of-string*/
11221 if(StrStr(msg, "Mailed cmail message to ") != NULL) {
11222 cmailMailedMove = TRUE; /* Prevent >1 moves */
11225 for (i = 0; i < nCmailGames; i ++) {
11226 if (cmailResult[i] == CMAIL_NOT_RESULT) {
11231 && ( (arcDir = (char *) getenv("CMAIL_ARCDIR"))
11233 sprintf(buffer, "%s/%s.%s.archive",
11235 appData.cmailGameName,
11237 LoadGameFromFile(buffer, 1, buffer, FALSE);
11238 cmailMsgLoaded = FALSE;
11242 DisplayInformation(msg);
11243 pclose(commandOutput);
11246 if ((*cmailMsg) != '\0') {
11247 DisplayInformation(cmailMsg);
11252 #endif /* !WIN32 */
11261 int prependComma = 0;
11263 char string[MSG_SIZ]; /* Space for game-list */
11266 if (!cmailMsgLoaded) return "";
11268 if (cmailMailedMove) {
11269 sprintf(cmailMsg, _("Waiting for reply from opponent\n"));
11271 /* Create a list of games left */
11272 sprintf(string, "[");
11273 for (i = 0; i < nCmailGames; i ++) {
11274 if (! ( cmailMoveRegistered[i]
11275 || (cmailResult[i] == CMAIL_OLD_RESULT))) {
11276 if (prependComma) {
11277 sprintf(number, ",%d", i + 1);
11279 sprintf(number, "%d", i + 1);
11283 strcat(string, number);
11286 strcat(string, "]");
11288 if (nCmailMovesRegistered + nCmailResults == 0) {
11289 switch (nCmailGames) {
11292 _("Still need to make move for game\n"));
11297 _("Still need to make moves for both games\n"));
11302 _("Still need to make moves for all %d games\n"),
11307 switch (nCmailGames - nCmailMovesRegistered - nCmailResults) {
11310 _("Still need to make a move for game %s\n"),
11315 if (nCmailResults == nCmailGames) {
11316 sprintf(cmailMsg, _("No unfinished games\n"));
11318 sprintf(cmailMsg, _("Ready to send mail\n"));
11324 _("Still need to make moves for games %s\n"),
11336 if (gameMode == Training)
11337 SetTrainingModeOff();
11340 cmailMsgLoaded = FALSE;
11341 if (appData.icsActive) {
11342 SendToICS(ics_prefix);
11343 SendToICS("refresh\n");
11353 /* Give up on clean exit */
11357 /* Keep trying for clean exit */
11361 if (appData.icsActive && appData.colorize) Colorize(ColorNone, FALSE);
11363 if (telnetISR != NULL) {
11364 RemoveInputSource(telnetISR);
11366 if (icsPR != NoProc) {
11367 DestroyChildProcess(icsPR, TRUE);
11370 /* [HGM] crash: leave writing PGN and position entirely to GameEnds() */
11371 GameEnds(gameInfo.result, gameInfo.resultDetails==NULL ? "xboard exit" : gameInfo.resultDetails, GE_PLAYER);
11373 /* [HGM] crash: the above GameEnds() is a dud if another one was running */
11374 /* make sure this other one finishes before killing it! */
11375 if(endingGame) { int count = 0;
11376 if(appData.debugMode) fprintf(debugFP, "ExitEvent() during GameEnds(), wait\n");
11377 while(endingGame && count++ < 10) DoSleep(1);
11378 if(appData.debugMode && endingGame) fprintf(debugFP, "GameEnds() seems stuck, proceed exiting\n");
11381 /* Kill off chess programs */
11382 if (first.pr != NoProc) {
11385 DoSleep( appData.delayBeforeQuit );
11386 SendToProgram("quit\n", &first);
11387 DoSleep( appData.delayAfterQuit );
11388 DestroyChildProcess(first.pr, 10 /* [AS] first.useSigterm */ );
11390 if (second.pr != NoProc) {
11391 DoSleep( appData.delayBeforeQuit );
11392 SendToProgram("quit\n", &second);
11393 DoSleep( appData.delayAfterQuit );
11394 DestroyChildProcess(second.pr, 10 /* [AS] second.useSigterm */ );
11396 if (first.isr != NULL) {
11397 RemoveInputSource(first.isr);
11399 if (second.isr != NULL) {
11400 RemoveInputSource(second.isr);
11403 ShutDownFrontEnd();
11410 if (appData.debugMode)
11411 fprintf(debugFP, "PauseEvent(): pausing %d\n", pausing);
11415 if (gameMode == MachinePlaysWhite ||
11416 gameMode == MachinePlaysBlack) {
11419 DisplayBothClocks();
11421 if (gameMode == PlayFromGameFile) {
11422 if (appData.timeDelay >= 0)
11423 AutoPlayGameLoop();
11424 } else if (gameMode == IcsExamining && pauseExamInvalid) {
11425 Reset(FALSE, TRUE);
11426 SendToICS(ics_prefix);
11427 SendToICS("refresh\n");
11428 } else if (currentMove < forwardMostMove) {
11429 ForwardInner(forwardMostMove);
11431 pauseExamInvalid = FALSE;
11433 switch (gameMode) {
11437 pauseExamForwardMostMove = forwardMostMove;
11438 pauseExamInvalid = FALSE;
11441 case IcsPlayingWhite:
11442 case IcsPlayingBlack:
11446 case PlayFromGameFile:
11447 (void) StopLoadGameTimer();
11451 case BeginningOfGame:
11452 if (appData.icsActive) return;
11453 /* else fall through */
11454 case MachinePlaysWhite:
11455 case MachinePlaysBlack:
11456 case TwoMachinesPlay:
11457 if (forwardMostMove == 0)
11458 return; /* don't pause if no one has moved */
11459 if ((gameMode == MachinePlaysWhite &&
11460 !WhiteOnMove(forwardMostMove)) ||
11461 (gameMode == MachinePlaysBlack &&
11462 WhiteOnMove(forwardMostMove))) {
11475 char title[MSG_SIZ];
11477 if (currentMove < 1 || parseList[currentMove - 1][0] == NULLCHAR) {
11478 strcpy(title, _("Edit comment"));
11480 sprintf(title, _("Edit comment on %d.%s%s"), (currentMove - 1) / 2 + 1,
11481 WhiteOnMove(currentMove - 1) ? " " : ".. ",
11482 parseList[currentMove - 1]);
11485 EditCommentPopUp(currentMove, title, commentList[currentMove]);
11492 char *tags = PGNTags(&gameInfo);
11493 EditTagsPopUp(tags);
11500 if (appData.noChessProgram || gameMode == AnalyzeMode)
11503 if (gameMode != AnalyzeFile) {
11504 if (!appData.icsEngineAnalyze) {
11506 if (gameMode != EditGame) return;
11508 ResurrectChessProgram();
11509 SendToProgram("analyze\n", &first);
11510 first.analyzing = TRUE;
11511 /*first.maybeThinking = TRUE;*/
11512 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
11513 EngineOutputPopUp();
11515 if (!appData.icsEngineAnalyze) gameMode = AnalyzeMode;
11520 StartAnalysisClock();
11521 GetTimeMark(&lastNodeCountTime);
11528 if (appData.noChessProgram || gameMode == AnalyzeFile)
11531 if (gameMode != AnalyzeMode) {
11533 if (gameMode != EditGame) return;
11534 ResurrectChessProgram();
11535 SendToProgram("analyze\n", &first);
11536 first.analyzing = TRUE;
11537 /*first.maybeThinking = TRUE;*/
11538 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
11539 EngineOutputPopUp();
11541 gameMode = AnalyzeFile;
11546 StartAnalysisClock();
11547 GetTimeMark(&lastNodeCountTime);
11552 MachineWhiteEvent()
11555 char *bookHit = NULL;
11557 if (appData.noChessProgram || (gameMode == MachinePlaysWhite))
11561 if (gameMode == PlayFromGameFile ||
11562 gameMode == TwoMachinesPlay ||
11563 gameMode == Training ||
11564 gameMode == AnalyzeMode ||
11565 gameMode == EndOfGame)
11568 if (gameMode == EditPosition)
11569 EditPositionDone(TRUE);
11571 if (!WhiteOnMove(currentMove)) {
11572 DisplayError(_("It is not White's turn"), 0);
11576 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile)
11579 if (gameMode == EditGame || gameMode == AnalyzeMode ||
11580 gameMode == AnalyzeFile)
11583 ResurrectChessProgram(); /* in case it isn't running */
11584 if(gameMode == BeginningOfGame) { /* [HGM] time odds: to get right odds in human mode */
11585 gameMode = MachinePlaysWhite;
11588 gameMode = MachinePlaysWhite;
11592 sprintf(buf, "%s vs. %s", gameInfo.white, gameInfo.black);
11594 if (first.sendName) {
11595 sprintf(buf, "name %s\n", gameInfo.black);
11596 SendToProgram(buf, &first);
11598 if (first.sendTime) {
11599 if (first.useColors) {
11600 SendToProgram("black\n", &first); /*gnu kludge*/
11602 SendTimeRemaining(&first, TRUE);
11604 if (first.useColors) {
11605 SendToProgram("white\n", &first); // [HGM] book: send 'go' separately
11607 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: send go or retrieve book move
11608 SetMachineThinkingEnables();
11609 first.maybeThinking = TRUE;
11613 if (appData.autoFlipView && !flipView) {
11614 flipView = !flipView;
11615 DrawPosition(FALSE, NULL);
11616 DisplayBothClocks(); // [HGM] logo: clocks might have to be exchanged;
11619 if(bookHit) { // [HGM] book: simulate book reply
11620 static char bookMove[MSG_SIZ]; // a bit generous?
11622 programStats.nodes = programStats.depth = programStats.time =
11623 programStats.score = programStats.got_only_move = 0;
11624 sprintf(programStats.movelist, "%s (xbook)", bookHit);
11626 strcpy(bookMove, "move ");
11627 strcat(bookMove, bookHit);
11628 HandleMachineMove(bookMove, &first);
11633 MachineBlackEvent()
11636 char *bookHit = NULL;
11638 if (appData.noChessProgram || (gameMode == MachinePlaysBlack))
11642 if (gameMode == PlayFromGameFile ||
11643 gameMode == TwoMachinesPlay ||
11644 gameMode == Training ||
11645 gameMode == AnalyzeMode ||
11646 gameMode == EndOfGame)
11649 if (gameMode == EditPosition)
11650 EditPositionDone(TRUE);
11652 if (WhiteOnMove(currentMove)) {
11653 DisplayError(_("It is not Black's turn"), 0);
11657 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile)
11660 if (gameMode == EditGame || gameMode == AnalyzeMode ||
11661 gameMode == AnalyzeFile)
11664 ResurrectChessProgram(); /* in case it isn't running */
11665 gameMode = MachinePlaysBlack;
11669 sprintf(buf, "%s vs. %s", gameInfo.white, gameInfo.black);
11671 if (first.sendName) {
11672 sprintf(buf, "name %s\n", gameInfo.white);
11673 SendToProgram(buf, &first);
11675 if (first.sendTime) {
11676 if (first.useColors) {
11677 SendToProgram("white\n", &first); /*gnu kludge*/
11679 SendTimeRemaining(&first, FALSE);
11681 if (first.useColors) {
11682 SendToProgram("black\n", &first); // [HGM] book: 'go' sent separately
11684 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: send go or retrieve book move
11685 SetMachineThinkingEnables();
11686 first.maybeThinking = TRUE;
11689 if (appData.autoFlipView && flipView) {
11690 flipView = !flipView;
11691 DrawPosition(FALSE, NULL);
11692 DisplayBothClocks(); // [HGM] logo: clocks might have to be exchanged;
11694 if(bookHit) { // [HGM] book: simulate book reply
11695 static char bookMove[MSG_SIZ]; // a bit generous?
11697 programStats.nodes = programStats.depth = programStats.time =
11698 programStats.score = programStats.got_only_move = 0;
11699 sprintf(programStats.movelist, "%s (xbook)", bookHit);
11701 strcpy(bookMove, "move ");
11702 strcat(bookMove, bookHit);
11703 HandleMachineMove(bookMove, &first);
11709 DisplayTwoMachinesTitle()
11712 if (appData.matchGames > 0) {
11713 if (first.twoMachinesColor[0] == 'w') {
11714 sprintf(buf, "%s vs. %s (%d-%d-%d)",
11715 gameInfo.white, gameInfo.black,
11716 first.matchWins, second.matchWins,
11717 matchGame - 1 - (first.matchWins + second.matchWins));
11719 sprintf(buf, "%s vs. %s (%d-%d-%d)",
11720 gameInfo.white, gameInfo.black,
11721 second.matchWins, first.matchWins,
11722 matchGame - 1 - (first.matchWins + second.matchWins));
11725 sprintf(buf, "%s vs. %s", gameInfo.white, gameInfo.black);
11731 TwoMachinesEvent P((void))
11735 ChessProgramState *onmove;
11736 char *bookHit = NULL;
11738 if (appData.noChessProgram) return;
11740 switch (gameMode) {
11741 case TwoMachinesPlay:
11743 case MachinePlaysWhite:
11744 case MachinePlaysBlack:
11745 if (WhiteOnMove(forwardMostMove) == (gameMode == MachinePlaysWhite)) {
11746 DisplayError(_("Wait until your turn,\nor select Move Now"), 0);
11750 case BeginningOfGame:
11751 case PlayFromGameFile:
11754 if (gameMode != EditGame) return;
11757 EditPositionDone(TRUE);
11768 // forwardMostMove = currentMove;
11769 TruncateGame(); // [HGM] vari: MachineWhite and MachineBlack do this...
11770 ResurrectChessProgram(); /* in case first program isn't running */
11772 if (second.pr == NULL) {
11773 StartChessProgram(&second);
11774 if (second.protocolVersion == 1) {
11775 TwoMachinesEventIfReady();
11777 /* kludge: allow timeout for initial "feature" command */
11779 DisplayMessage("", _("Starting second chess program"));
11780 ScheduleDelayedEvent(TwoMachinesEventIfReady, FEATURE_TIMEOUT);
11784 DisplayMessage("", "");
11785 InitChessProgram(&second, FALSE);
11786 SendToProgram("force\n", &second);
11787 if (startedFromSetupPosition) {
11788 SendBoard(&second, backwardMostMove);
11789 if (appData.debugMode) {
11790 fprintf(debugFP, "Two Machines\n");
11793 for (i = backwardMostMove; i < forwardMostMove; i++) {
11794 SendMoveToProgram(i, &second);
11797 gameMode = TwoMachinesPlay;
11801 DisplayTwoMachinesTitle();
11803 if ((first.twoMachinesColor[0] == 'w') == WhiteOnMove(forwardMostMove)) {
11809 SendToProgram(first.computerString, &first);
11810 if (first.sendName) {
11811 sprintf(buf, "name %s\n", second.tidy);
11812 SendToProgram(buf, &first);
11814 SendToProgram(second.computerString, &second);
11815 if (second.sendName) {
11816 sprintf(buf, "name %s\n", first.tidy);
11817 SendToProgram(buf, &second);
11821 if (!first.sendTime || !second.sendTime) {
11822 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
11823 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
11825 if (onmove->sendTime) {
11826 if (onmove->useColors) {
11827 SendToProgram(onmove->other->twoMachinesColor, onmove); /*gnu kludge*/
11829 SendTimeRemaining(onmove, WhiteOnMove(forwardMostMove));
11831 if (onmove->useColors) {
11832 SendToProgram(onmove->twoMachinesColor, onmove);
11834 bookHit = SendMoveToBookUser(forwardMostMove-1, onmove, TRUE); // [HGM] book: send go or retrieve book move
11835 // SendToProgram("go\n", onmove);
11836 onmove->maybeThinking = TRUE;
11837 SetMachineThinkingEnables();
11841 if(bookHit) { // [HGM] book: simulate book reply
11842 static char bookMove[MSG_SIZ]; // a bit generous?
11844 programStats.nodes = programStats.depth = programStats.time =
11845 programStats.score = programStats.got_only_move = 0;
11846 sprintf(programStats.movelist, "%s (xbook)", bookHit);
11848 strcpy(bookMove, "move ");
11849 strcat(bookMove, bookHit);
11850 savedMessage = bookMove; // args for deferred call
11851 savedState = onmove;
11852 ScheduleDelayedEvent(DeferredBookMove, 1);
11859 if (gameMode == Training) {
11860 SetTrainingModeOff();
11861 gameMode = PlayFromGameFile;
11862 DisplayMessage("", _("Training mode off"));
11864 gameMode = Training;
11865 animateTraining = appData.animate;
11867 /* make sure we are not already at the end of the game */
11868 if (currentMove < forwardMostMove) {
11869 SetTrainingModeOn();
11870 DisplayMessage("", _("Training mode on"));
11872 gameMode = PlayFromGameFile;
11873 DisplayError(_("Already at end of game"), 0);
11882 if (!appData.icsActive) return;
11883 switch (gameMode) {
11884 case IcsPlayingWhite:
11885 case IcsPlayingBlack:
11888 case BeginningOfGame:
11896 EditPositionDone(TRUE);
11909 gameMode = IcsIdle;
11920 switch (gameMode) {
11922 SetTrainingModeOff();
11924 case MachinePlaysWhite:
11925 case MachinePlaysBlack:
11926 case BeginningOfGame:
11927 SendToProgram("force\n", &first);
11928 SetUserThinkingEnables();
11930 case PlayFromGameFile:
11931 (void) StopLoadGameTimer();
11932 if (gameFileFP != NULL) {
11937 EditPositionDone(TRUE);
11942 SendToProgram("force\n", &first);
11944 case TwoMachinesPlay:
11945 GameEnds((ChessMove) 0, NULL, GE_PLAYER);
11946 ResurrectChessProgram();
11947 SetUserThinkingEnables();
11950 ResurrectChessProgram();
11952 case IcsPlayingBlack:
11953 case IcsPlayingWhite:
11954 DisplayError(_("Warning: You are still playing a game"), 0);
11957 DisplayError(_("Warning: You are still observing a game"), 0);
11960 DisplayError(_("Warning: You are still examining a game"), 0);
11971 first.offeredDraw = second.offeredDraw = 0;
11973 if (gameMode == PlayFromGameFile) {
11974 whiteTimeRemaining = timeRemaining[0][currentMove];
11975 blackTimeRemaining = timeRemaining[1][currentMove];
11979 if (gameMode == MachinePlaysWhite ||
11980 gameMode == MachinePlaysBlack ||
11981 gameMode == TwoMachinesPlay ||
11982 gameMode == EndOfGame) {
11983 i = forwardMostMove;
11984 while (i > currentMove) {
11985 SendToProgram("undo\n", &first);
11988 whiteTimeRemaining = timeRemaining[0][currentMove];
11989 blackTimeRemaining = timeRemaining[1][currentMove];
11990 DisplayBothClocks();
11991 if (whiteFlag || blackFlag) {
11992 whiteFlag = blackFlag = 0;
11997 gameMode = EditGame;
12004 EditPositionEvent()
12006 if (gameMode == EditPosition) {
12012 if (gameMode != EditGame) return;
12014 gameMode = EditPosition;
12017 if (currentMove > 0)
12018 CopyBoard(boards[0], boards[currentMove]);
12020 blackPlaysFirst = !WhiteOnMove(currentMove);
12022 currentMove = forwardMostMove = backwardMostMove = 0;
12023 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
12030 /* [DM] icsEngineAnalyze - possible call from other functions */
12031 if (appData.icsEngineAnalyze) {
12032 appData.icsEngineAnalyze = FALSE;
12034 DisplayMessage("",_("Close ICS engine analyze..."));
12036 if (first.analysisSupport && first.analyzing) {
12037 SendToProgram("exit\n", &first);
12038 first.analyzing = FALSE;
12040 thinkOutput[0] = NULLCHAR;
12044 EditPositionDone(Boolean fakeRights)
12046 int king = gameInfo.variant == VariantKnightmate ? WhiteUnicorn : WhiteKing;
12048 startedFromSetupPosition = TRUE;
12049 InitChessProgram(&first, FALSE);
12050 if(fakeRights) { // [HGM] suppress this if we just pasted a FEN.
12051 boards[0][EP_STATUS] = EP_NONE;
12052 boards[0][CASTLING][2] = boards[0][CASTLING][5] = BOARD_WIDTH>>1;
12053 if(boards[0][0][BOARD_WIDTH>>1] == king) {
12054 boards[0][CASTLING][1] = boards[0][0][BOARD_LEFT] == WhiteRook ? 0 : NoRights;
12055 boards[0][CASTLING][0] = boards[0][0][BOARD_RGHT-1] == WhiteRook ? BOARD_RGHT-1 : NoRights;
12056 } else boards[0][CASTLING][2] = NoRights;
12057 if(boards[0][BOARD_HEIGHT-1][BOARD_WIDTH>>1] == WHITE_TO_BLACK king) {
12058 boards[0][CASTLING][4] = boards[0][BOARD_HEIGHT-1][BOARD_LEFT] == BlackRook ? 0 : NoRights;
12059 boards[0][CASTLING][3] = boards[0][BOARD_HEIGHT-1][BOARD_RGHT-1] == BlackRook ? BOARD_RGHT-1 : NoRights;
12060 } else boards[0][CASTLING][5] = NoRights;
12062 SendToProgram("force\n", &first);
12063 if (blackPlaysFirst) {
12064 strcpy(moveList[0], "");
12065 strcpy(parseList[0], "");
12066 currentMove = forwardMostMove = backwardMostMove = 1;
12067 CopyBoard(boards[1], boards[0]);
12069 currentMove = forwardMostMove = backwardMostMove = 0;
12071 SendBoard(&first, forwardMostMove);
12072 if (appData.debugMode) {
12073 fprintf(debugFP, "EditPosDone\n");
12076 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
12077 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
12078 gameMode = EditGame;
12080 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
12081 ClearHighlights(); /* [AS] */
12084 /* Pause for `ms' milliseconds */
12085 /* !! Ugh, this is a kludge. Fix it sometime. --tpm */
12095 } while (SubtractTimeMarks(&m2, &m1) < ms);
12098 /* !! Ugh, this is a kludge. Fix it sometime. --tpm */
12100 SendMultiLineToICS(buf)
12103 char temp[MSG_SIZ+1], *p;
12110 strncpy(temp, buf, len);
12115 if (*p == '\n' || *p == '\r')
12120 strcat(temp, "\n");
12122 SendToPlayer(temp, strlen(temp));
12126 SetWhiteToPlayEvent()
12128 if (gameMode == EditPosition) {
12129 blackPlaysFirst = FALSE;
12130 DisplayBothClocks(); /* works because currentMove is 0 */
12131 } else if (gameMode == IcsExamining) {
12132 SendToICS(ics_prefix);
12133 SendToICS("tomove white\n");
12138 SetBlackToPlayEvent()
12140 if (gameMode == EditPosition) {
12141 blackPlaysFirst = TRUE;
12142 currentMove = 1; /* kludge */
12143 DisplayBothClocks();
12145 } else if (gameMode == IcsExamining) {
12146 SendToICS(ics_prefix);
12147 SendToICS("tomove black\n");
12152 EditPositionMenuEvent(selection, x, y)
12153 ChessSquare selection;
12157 ChessSquare piece = boards[0][y][x];
12159 if (gameMode != EditPosition && gameMode != IcsExamining) return;
12161 switch (selection) {
12163 if (gameMode == IcsExamining && ics_type == ICS_FICS) {
12164 SendToICS(ics_prefix);
12165 SendToICS("bsetup clear\n");
12166 } else if (gameMode == IcsExamining && ics_type == ICS_ICC) {
12167 SendToICS(ics_prefix);
12168 SendToICS("clearboard\n");
12170 for (x = 0; x < BOARD_WIDTH; x++) { ChessSquare p = EmptySquare;
12171 if(x == BOARD_LEFT-1 || x == BOARD_RGHT) p = (ChessSquare) 0; /* [HGM] holdings */
12172 for (y = 0; y < BOARD_HEIGHT; y++) {
12173 if (gameMode == IcsExamining) {
12174 if (boards[currentMove][y][x] != EmptySquare) {
12175 sprintf(buf, "%sx@%c%c\n", ics_prefix,
12180 boards[0][y][x] = p;
12185 if (gameMode == EditPosition) {
12186 DrawPosition(FALSE, boards[0]);
12191 SetWhiteToPlayEvent();
12195 SetBlackToPlayEvent();
12199 if (gameMode == IcsExamining) {
12200 if (x < BOARD_LEFT || x >= BOARD_RGHT) break; // [HGM] holdings
12201 sprintf(buf, "%sx@%c%c\n", ics_prefix, AAA + x, ONE + y);
12204 if(x < BOARD_LEFT || x >= BOARD_RGHT) {
12205 if(x == BOARD_LEFT-2) {
12206 if(y < BOARD_HEIGHT-1-gameInfo.holdingsSize) break;
12207 boards[0][y][1] = 0;
12209 if(x == BOARD_RGHT+1) {
12210 if(y >= gameInfo.holdingsSize) break;
12211 boards[0][y][BOARD_WIDTH-2] = 0;
12214 boards[0][y][x] = EmptySquare;
12215 DrawPosition(FALSE, boards[0]);
12220 if(piece >= (int)WhitePawn && piece < (int)WhiteMan ||
12221 piece >= (int)BlackPawn && piece < (int)BlackMan ) {
12222 selection = (ChessSquare) (PROMOTED piece);
12223 } else if(piece == EmptySquare) selection = WhiteSilver;
12224 else selection = (ChessSquare)((int)piece - 1);
12228 if(piece > (int)WhiteMan && piece <= (int)WhiteKing ||
12229 piece > (int)BlackMan && piece <= (int)BlackKing ) {
12230 selection = (ChessSquare) (DEMOTED piece);
12231 } else if(piece == EmptySquare) selection = BlackSilver;
12232 else selection = (ChessSquare)((int)piece + 1);
12237 if(gameInfo.variant == VariantShatranj ||
12238 gameInfo.variant == VariantXiangqi ||
12239 gameInfo.variant == VariantCourier ||
12240 gameInfo.variant == VariantMakruk )
12241 selection = (ChessSquare)((int)selection - (int)WhiteQueen + (int)WhiteFerz);
12246 if(gameInfo.variant == VariantXiangqi)
12247 selection = (ChessSquare)((int)selection - (int)WhiteKing + (int)WhiteWazir);
12248 if(gameInfo.variant == VariantKnightmate)
12249 selection = (ChessSquare)((int)selection - (int)WhiteKing + (int)WhiteUnicorn);
12252 if (gameMode == IcsExamining) {
12253 if (x < BOARD_LEFT || x >= BOARD_RGHT) break; // [HGM] holdings
12254 sprintf(buf, "%s%c@%c%c\n", ics_prefix,
12255 PieceToChar(selection), AAA + x, ONE + y);
12258 if(x < BOARD_LEFT || x >= BOARD_RGHT) {
12260 if(x == BOARD_LEFT-2 && selection >= BlackPawn) {
12261 n = PieceToNumber(selection - BlackPawn);
12262 if(n >= gameInfo.holdingsSize) { n = 0; selection = BlackPawn; }
12263 boards[0][BOARD_HEIGHT-1-n][0] = selection;
12264 boards[0][BOARD_HEIGHT-1-n][1]++;
12266 if(x == BOARD_RGHT+1 && selection < BlackPawn) {
12267 n = PieceToNumber(selection);
12268 if(n >= gameInfo.holdingsSize) { n = 0; selection = WhitePawn; }
12269 boards[0][n][BOARD_WIDTH-1] = selection;
12270 boards[0][n][BOARD_WIDTH-2]++;
12273 boards[0][y][x] = selection;
12274 DrawPosition(TRUE, boards[0]);
12282 DropMenuEvent(selection, x, y)
12283 ChessSquare selection;
12286 ChessMove moveType;
12288 switch (gameMode) {
12289 case IcsPlayingWhite:
12290 case MachinePlaysBlack:
12291 if (!WhiteOnMove(currentMove)) {
12292 DisplayMoveError(_("It is Black's turn"));
12295 moveType = WhiteDrop;
12297 case IcsPlayingBlack:
12298 case MachinePlaysWhite:
12299 if (WhiteOnMove(currentMove)) {
12300 DisplayMoveError(_("It is White's turn"));
12303 moveType = BlackDrop;
12306 moveType = WhiteOnMove(currentMove) ? WhiteDrop : BlackDrop;
12312 if (moveType == BlackDrop && selection < BlackPawn) {
12313 selection = (ChessSquare) ((int) selection
12314 + (int) BlackPawn - (int) WhitePawn);
12316 if (boards[currentMove][y][x] != EmptySquare) {
12317 DisplayMoveError(_("That square is occupied"));
12321 FinishMove(moveType, (int) selection, DROP_RANK, x, y, NULLCHAR);
12327 /* Accept a pending offer of any kind from opponent */
12329 if (appData.icsActive) {
12330 SendToICS(ics_prefix);
12331 SendToICS("accept\n");
12332 } else if (cmailMsgLoaded) {
12333 if (currentMove == cmailOldMove &&
12334 commentList[cmailOldMove] != NULL &&
12335 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
12336 "Black offers a draw" : "White offers a draw")) {
12338 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
12339 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_ACCEPT;
12341 DisplayError(_("There is no pending offer on this move"), 0);
12342 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
12345 /* Not used for offers from chess program */
12352 /* Decline a pending offer of any kind from opponent */
12354 if (appData.icsActive) {
12355 SendToICS(ics_prefix);
12356 SendToICS("decline\n");
12357 } else if (cmailMsgLoaded) {
12358 if (currentMove == cmailOldMove &&
12359 commentList[cmailOldMove] != NULL &&
12360 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
12361 "Black offers a draw" : "White offers a draw")) {
12363 AppendComment(cmailOldMove, "Draw declined", TRUE);
12364 DisplayComment(cmailOldMove - 1, "Draw declined");
12367 DisplayError(_("There is no pending offer on this move"), 0);
12370 /* Not used for offers from chess program */
12377 /* Issue ICS rematch command */
12378 if (appData.icsActive) {
12379 SendToICS(ics_prefix);
12380 SendToICS("rematch\n");
12387 /* Call your opponent's flag (claim a win on time) */
12388 if (appData.icsActive) {
12389 SendToICS(ics_prefix);
12390 SendToICS("flag\n");
12392 switch (gameMode) {
12395 case MachinePlaysWhite:
12398 GameEnds(GameIsDrawn, "Both players ran out of time",
12401 GameEnds(BlackWins, "Black wins on time", GE_PLAYER);
12403 DisplayError(_("Your opponent is not out of time"), 0);
12406 case MachinePlaysBlack:
12409 GameEnds(GameIsDrawn, "Both players ran out of time",
12412 GameEnds(WhiteWins, "White wins on time", GE_PLAYER);
12414 DisplayError(_("Your opponent is not out of time"), 0);
12424 /* Offer draw or accept pending draw offer from opponent */
12426 if (appData.icsActive) {
12427 /* Note: tournament rules require draw offers to be
12428 made after you make your move but before you punch
12429 your clock. Currently ICS doesn't let you do that;
12430 instead, you immediately punch your clock after making
12431 a move, but you can offer a draw at any time. */
12433 SendToICS(ics_prefix);
12434 SendToICS("draw\n");
12435 userOfferedDraw = TRUE; // [HGM] drawclaim: also set flag in ICS play
12436 } else if (cmailMsgLoaded) {
12437 if (currentMove == cmailOldMove &&
12438 commentList[cmailOldMove] != NULL &&
12439 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
12440 "Black offers a draw" : "White offers a draw")) {
12441 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
12442 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_ACCEPT;
12443 } else if (currentMove == cmailOldMove + 1) {
12444 char *offer = WhiteOnMove(cmailOldMove) ?
12445 "White offers a draw" : "Black offers a draw";
12446 AppendComment(currentMove, offer, TRUE);
12447 DisplayComment(currentMove - 1, offer);
12448 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_DRAW;
12450 DisplayError(_("You must make your move before offering a draw"), 0);
12451 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
12453 } else if (first.offeredDraw) {
12454 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
12456 if (first.sendDrawOffers) {
12457 SendToProgram("draw\n", &first);
12458 userOfferedDraw = TRUE;
12466 /* Offer Adjourn or accept pending Adjourn offer from opponent */
12468 if (appData.icsActive) {
12469 SendToICS(ics_prefix);
12470 SendToICS("adjourn\n");
12472 /* Currently GNU Chess doesn't offer or accept Adjourns */
12480 /* Offer Abort or accept pending Abort offer from opponent */
12482 if (appData.icsActive) {
12483 SendToICS(ics_prefix);
12484 SendToICS("abort\n");
12486 GameEnds(GameUnfinished, "Game aborted", GE_PLAYER);
12493 /* Resign. You can do this even if it's not your turn. */
12495 if (appData.icsActive) {
12496 SendToICS(ics_prefix);
12497 SendToICS("resign\n");
12499 switch (gameMode) {
12500 case MachinePlaysWhite:
12501 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
12503 case MachinePlaysBlack:
12504 GameEnds(BlackWins, "White resigns", GE_PLAYER);
12507 if (cmailMsgLoaded) {
12509 if (WhiteOnMove(cmailOldMove)) {
12510 GameEnds(BlackWins, "White resigns", GE_PLAYER);
12512 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
12514 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_RESIGN;
12525 StopObservingEvent()
12527 /* Stop observing current games */
12528 SendToICS(ics_prefix);
12529 SendToICS("unobserve\n");
12533 StopExaminingEvent()
12535 /* Stop observing current game */
12536 SendToICS(ics_prefix);
12537 SendToICS("unexamine\n");
12541 ForwardInner(target)
12546 if (appData.debugMode)
12547 fprintf(debugFP, "ForwardInner(%d), current %d, forward %d\n",
12548 target, currentMove, forwardMostMove);
12550 if (gameMode == EditPosition)
12553 if (gameMode == PlayFromGameFile && !pausing)
12556 if (gameMode == IcsExamining && pausing)
12557 limit = pauseExamForwardMostMove;
12559 limit = forwardMostMove;
12561 if (target > limit) target = limit;
12563 if (target > 0 && moveList[target - 1][0]) {
12564 int fromX, fromY, toX, toY;
12565 toX = moveList[target - 1][2] - AAA;
12566 toY = moveList[target - 1][3] - ONE;
12567 if (moveList[target - 1][1] == '@') {
12568 if (appData.highlightLastMove) {
12569 SetHighlights(-1, -1, toX, toY);
12572 fromX = moveList[target - 1][0] - AAA;
12573 fromY = moveList[target - 1][1] - ONE;
12574 if (target == currentMove + 1) {
12575 AnimateMove(boards[currentMove], fromX, fromY, toX, toY);
12577 if (appData.highlightLastMove) {
12578 SetHighlights(fromX, fromY, toX, toY);
12582 if (gameMode == EditGame || gameMode == AnalyzeMode ||
12583 gameMode == Training || gameMode == PlayFromGameFile ||
12584 gameMode == AnalyzeFile) {
12585 while (currentMove < target) {
12586 SendMoveToProgram(currentMove++, &first);
12589 currentMove = target;
12592 if (gameMode == EditGame || gameMode == EndOfGame) {
12593 whiteTimeRemaining = timeRemaining[0][currentMove];
12594 blackTimeRemaining = timeRemaining[1][currentMove];
12596 DisplayBothClocks();
12597 DisplayMove(currentMove - 1);
12598 DrawPosition(FALSE, boards[currentMove]);
12599 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
12600 if ( !matchMode && gameMode != Training) { // [HGM] PV info: routine tests if empty
12601 DisplayComment(currentMove - 1, commentList[currentMove]);
12609 if (gameMode == IcsExamining && !pausing) {
12610 SendToICS(ics_prefix);
12611 SendToICS("forward\n");
12613 ForwardInner(currentMove + 1);
12620 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
12621 /* to optimze, we temporarily turn off analysis mode while we feed
12622 * the remaining moves to the engine. Otherwise we get analysis output
12625 if (first.analysisSupport) {
12626 SendToProgram("exit\nforce\n", &first);
12627 first.analyzing = FALSE;
12631 if (gameMode == IcsExamining && !pausing) {
12632 SendToICS(ics_prefix);
12633 SendToICS("forward 999999\n");
12635 ForwardInner(forwardMostMove);
12638 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
12639 /* we have fed all the moves, so reactivate analysis mode */
12640 SendToProgram("analyze\n", &first);
12641 first.analyzing = TRUE;
12642 /*first.maybeThinking = TRUE;*/
12643 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
12648 BackwardInner(target)
12651 int full_redraw = TRUE; /* [AS] Was FALSE, had to change it! */
12653 if (appData.debugMode)
12654 fprintf(debugFP, "BackwardInner(%d), current %d, forward %d\n",
12655 target, currentMove, forwardMostMove);
12657 if (gameMode == EditPosition) return;
12658 if (currentMove <= backwardMostMove) {
12660 DrawPosition(full_redraw, boards[currentMove]);
12663 if (gameMode == PlayFromGameFile && !pausing)
12666 if (moveList[target][0]) {
12667 int fromX, fromY, toX, toY;
12668 toX = moveList[target][2] - AAA;
12669 toY = moveList[target][3] - ONE;
12670 if (moveList[target][1] == '@') {
12671 if (appData.highlightLastMove) {
12672 SetHighlights(-1, -1, toX, toY);
12675 fromX = moveList[target][0] - AAA;
12676 fromY = moveList[target][1] - ONE;
12677 if (target == currentMove - 1) {
12678 AnimateMove(boards[currentMove], toX, toY, fromX, fromY);
12680 if (appData.highlightLastMove) {
12681 SetHighlights(fromX, fromY, toX, toY);
12685 if (gameMode == EditGame || gameMode==AnalyzeMode ||
12686 gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
12687 while (currentMove > target) {
12688 SendToProgram("undo\n", &first);
12692 currentMove = target;
12695 if (gameMode == EditGame || gameMode == EndOfGame) {
12696 whiteTimeRemaining = timeRemaining[0][currentMove];
12697 blackTimeRemaining = timeRemaining[1][currentMove];
12699 DisplayBothClocks();
12700 DisplayMove(currentMove - 1);
12701 DrawPosition(full_redraw, boards[currentMove]);
12702 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
12703 // [HGM] PV info: routine tests if comment empty
12704 DisplayComment(currentMove - 1, commentList[currentMove]);
12710 if (gameMode == IcsExamining && !pausing) {
12711 SendToICS(ics_prefix);
12712 SendToICS("backward\n");
12714 BackwardInner(currentMove - 1);
12721 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
12722 /* to optimize, we temporarily turn off analysis mode while we undo
12723 * all the moves. Otherwise we get analysis output after each undo.
12725 if (first.analysisSupport) {
12726 SendToProgram("exit\nforce\n", &first);
12727 first.analyzing = FALSE;
12731 if (gameMode == IcsExamining && !pausing) {
12732 SendToICS(ics_prefix);
12733 SendToICS("backward 999999\n");
12735 BackwardInner(backwardMostMove);
12738 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
12739 /* we have fed all the moves, so reactivate analysis mode */
12740 SendToProgram("analyze\n", &first);
12741 first.analyzing = TRUE;
12742 /*first.maybeThinking = TRUE;*/
12743 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
12750 if (gameMode == PlayFromGameFile && !pausing) PauseEvent();
12751 if (to >= forwardMostMove) to = forwardMostMove;
12752 if (to <= backwardMostMove) to = backwardMostMove;
12753 if (to < currentMove) {
12761 RevertEvent(Boolean annotate)
12763 if(PopTail(annotate)) { // [HGM] vari: restore old game tail
12766 if (gameMode != IcsExamining) {
12767 DisplayError(_("You are not examining a game"), 0);
12771 DisplayError(_("You can't revert while pausing"), 0);
12774 SendToICS(ics_prefix);
12775 SendToICS("revert\n");
12781 switch (gameMode) {
12782 case MachinePlaysWhite:
12783 case MachinePlaysBlack:
12784 if (WhiteOnMove(forwardMostMove) == (gameMode == MachinePlaysWhite)) {
12785 DisplayError(_("Wait until your turn,\nor select Move Now"), 0);
12788 if (forwardMostMove < 2) return;
12789 currentMove = forwardMostMove = forwardMostMove - 2;
12790 whiteTimeRemaining = timeRemaining[0][currentMove];
12791 blackTimeRemaining = timeRemaining[1][currentMove];
12792 DisplayBothClocks();
12793 DisplayMove(currentMove - 1);
12794 ClearHighlights();/*!! could figure this out*/
12795 DrawPosition(TRUE, boards[currentMove]); /* [AS] Changed to full redraw! */
12796 SendToProgram("remove\n", &first);
12797 /*first.maybeThinking = TRUE;*/ /* GNU Chess does not ponder here */
12800 case BeginningOfGame:
12804 case IcsPlayingWhite:
12805 case IcsPlayingBlack:
12806 if (WhiteOnMove(forwardMostMove) == (gameMode == IcsPlayingWhite)) {
12807 SendToICS(ics_prefix);
12808 SendToICS("takeback 2\n");
12810 SendToICS(ics_prefix);
12811 SendToICS("takeback 1\n");
12820 ChessProgramState *cps;
12822 switch (gameMode) {
12823 case MachinePlaysWhite:
12824 if (!WhiteOnMove(forwardMostMove)) {
12825 DisplayError(_("It is your turn"), 0);
12830 case MachinePlaysBlack:
12831 if (WhiteOnMove(forwardMostMove)) {
12832 DisplayError(_("It is your turn"), 0);
12837 case TwoMachinesPlay:
12838 if (WhiteOnMove(forwardMostMove) ==
12839 (first.twoMachinesColor[0] == 'w')) {
12845 case BeginningOfGame:
12849 SendToProgram("?\n", cps);
12853 TruncateGameEvent()
12856 if (gameMode != EditGame) return;
12863 CleanupTail(); // [HGM] vari: only keep current variation if we explicitly truncate
12864 if (forwardMostMove > currentMove) {
12865 if (gameInfo.resultDetails != NULL) {
12866 free(gameInfo.resultDetails);
12867 gameInfo.resultDetails = NULL;
12868 gameInfo.result = GameUnfinished;
12870 forwardMostMove = currentMove;
12871 HistorySet(parseList, backwardMostMove, forwardMostMove,
12879 if (appData.noChessProgram) return;
12880 switch (gameMode) {
12881 case MachinePlaysWhite:
12882 if (WhiteOnMove(forwardMostMove)) {
12883 DisplayError(_("Wait until your turn"), 0);
12887 case BeginningOfGame:
12888 case MachinePlaysBlack:
12889 if (!WhiteOnMove(forwardMostMove)) {
12890 DisplayError(_("Wait until your turn"), 0);
12895 DisplayError(_("No hint available"), 0);
12898 SendToProgram("hint\n", &first);
12899 hintRequested = TRUE;
12905 if (appData.noChessProgram) return;
12906 switch (gameMode) {
12907 case MachinePlaysWhite:
12908 if (WhiteOnMove(forwardMostMove)) {
12909 DisplayError(_("Wait until your turn"), 0);
12913 case BeginningOfGame:
12914 case MachinePlaysBlack:
12915 if (!WhiteOnMove(forwardMostMove)) {
12916 DisplayError(_("Wait until your turn"), 0);
12921 EditPositionDone(TRUE);
12923 case TwoMachinesPlay:
12928 SendToProgram("bk\n", &first);
12929 bookOutput[0] = NULLCHAR;
12930 bookRequested = TRUE;
12936 char *tags = PGNTags(&gameInfo);
12937 TagsPopUp(tags, CmailMsg());
12941 /* end button procedures */
12944 PrintPosition(fp, move)
12950 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
12951 for (j = BOARD_LEFT; j < BOARD_RGHT; j++) {
12952 char c = PieceToChar(boards[move][i][j]);
12953 fputc(c == 'x' ? '.' : c, fp);
12954 fputc(j == BOARD_RGHT - 1 ? '\n' : ' ', fp);
12957 if ((gameMode == EditPosition) ? !blackPlaysFirst : (move % 2 == 0))
12958 fprintf(fp, "white to play\n");
12960 fprintf(fp, "black to play\n");
12967 if (gameInfo.white != NULL) {
12968 fprintf(fp, "\t%s vs. %s\n", gameInfo.white, gameInfo.black);
12974 /* Find last component of program's own name, using some heuristics */
12976 TidyProgramName(prog, host, buf)
12977 char *prog, *host, buf[MSG_SIZ];
12980 int local = (strcmp(host, "localhost") == 0);
12981 while (!local && (p = strchr(prog, ';')) != NULL) {
12983 while (*p == ' ') p++;
12986 if (*prog == '"' || *prog == '\'') {
12987 q = strchr(prog + 1, *prog);
12989 q = strchr(prog, ' ');
12991 if (q == NULL) q = prog + strlen(prog);
12993 while (p >= prog && *p != '/' && *p != '\\') p--;
12995 if(p == prog && *p == '"') p++;
12996 if (q - p >= 4 && StrCaseCmp(q - 4, ".exe") == 0) q -= 4;
12997 memcpy(buf, p, q - p);
12998 buf[q - p] = NULLCHAR;
13006 TimeControlTagValue()
13009 if (!appData.clockMode) {
13011 } else if (movesPerSession > 0) {
13012 sprintf(buf, "%d/%ld", movesPerSession, timeControl/1000);
13013 } else if (timeIncrement == 0) {
13014 sprintf(buf, "%ld", timeControl/1000);
13016 sprintf(buf, "%ld+%ld", timeControl/1000, timeIncrement/1000);
13018 return StrSave(buf);
13024 /* This routine is used only for certain modes */
13025 VariantClass v = gameInfo.variant;
13026 ChessMove r = GameUnfinished;
13029 if(gameMode == EditGame) { // [HGM] vari: do not erase result on EditGame
13030 r = gameInfo.result;
13031 p = gameInfo.resultDetails;
13032 gameInfo.resultDetails = NULL;
13034 ClearGameInfo(&gameInfo);
13035 gameInfo.variant = v;
13037 switch (gameMode) {
13038 case MachinePlaysWhite:
13039 gameInfo.event = StrSave( appData.pgnEventHeader );
13040 gameInfo.site = StrSave(HostName());
13041 gameInfo.date = PGNDate();
13042 gameInfo.round = StrSave("-");
13043 gameInfo.white = StrSave(first.tidy);
13044 gameInfo.black = StrSave(UserName());
13045 gameInfo.timeControl = TimeControlTagValue();
13048 case MachinePlaysBlack:
13049 gameInfo.event = StrSave( appData.pgnEventHeader );
13050 gameInfo.site = StrSave(HostName());
13051 gameInfo.date = PGNDate();
13052 gameInfo.round = StrSave("-");
13053 gameInfo.white = StrSave(UserName());
13054 gameInfo.black = StrSave(first.tidy);
13055 gameInfo.timeControl = TimeControlTagValue();
13058 case TwoMachinesPlay:
13059 gameInfo.event = StrSave( appData.pgnEventHeader );
13060 gameInfo.site = StrSave(HostName());
13061 gameInfo.date = PGNDate();
13062 if (matchGame > 0) {
13064 sprintf(buf, "%d", matchGame);
13065 gameInfo.round = StrSave(buf);
13067 gameInfo.round = StrSave("-");
13069 if (first.twoMachinesColor[0] == 'w') {
13070 gameInfo.white = StrSave(first.tidy);
13071 gameInfo.black = StrSave(second.tidy);
13073 gameInfo.white = StrSave(second.tidy);
13074 gameInfo.black = StrSave(first.tidy);
13076 gameInfo.timeControl = TimeControlTagValue();
13080 gameInfo.event = StrSave("Edited game");
13081 gameInfo.site = StrSave(HostName());
13082 gameInfo.date = PGNDate();
13083 gameInfo.round = StrSave("-");
13084 gameInfo.white = StrSave("-");
13085 gameInfo.black = StrSave("-");
13086 gameInfo.result = r;
13087 gameInfo.resultDetails = p;
13091 gameInfo.event = StrSave("Edited position");
13092 gameInfo.site = StrSave(HostName());
13093 gameInfo.date = PGNDate();
13094 gameInfo.round = StrSave("-");
13095 gameInfo.white = StrSave("-");
13096 gameInfo.black = StrSave("-");
13099 case IcsPlayingWhite:
13100 case IcsPlayingBlack:
13105 case PlayFromGameFile:
13106 gameInfo.event = StrSave("Game from non-PGN file");
13107 gameInfo.site = StrSave(HostName());
13108 gameInfo.date = PGNDate();
13109 gameInfo.round = StrSave("-");
13110 gameInfo.white = StrSave("?");
13111 gameInfo.black = StrSave("?");
13120 ReplaceComment(index, text)
13126 while (*text == '\n') text++;
13127 len = strlen(text);
13128 while (len > 0 && text[len - 1] == '\n') len--;
13130 if (commentList[index] != NULL)
13131 free(commentList[index]);
13134 commentList[index] = NULL;
13137 if( *text == '{' && strchr(text, '}') || // [HGM] braces: if certainy malformed, put braces
13138 *text == '[' && strchr(text, ']') || // otherwise hope the user knows what he is doing
13139 *text == '(' && strchr(text, ')')) { // (perhaps check if this parses as comment-only?)
13140 commentList[index] = (char *) malloc(len + 2);
13141 strncpy(commentList[index], text, len);
13142 commentList[index][len] = '\n';
13143 commentList[index][len + 1] = NULLCHAR;
13145 // [HGM] braces: if text does not start with known OK delimiter, put braces around it.
13147 commentList[index] = (char *) malloc(len + 6);
13148 strcpy(commentList[index], "{\n");
13149 strncpy(commentList[index]+2, text, len);
13150 commentList[index][len+2] = NULLCHAR;
13151 while(p = strchr(commentList[index], '}')) *p = ')'; // kill all } to make it one comment
13152 strcat(commentList[index], "\n}\n");
13166 if (ch == '\r') continue;
13168 } while (ch != '\0');
13172 AppendComment(index, text, addBraces)
13175 Boolean addBraces; // [HGM] braces: tells if we should add {}
13180 if(appData.debugMode) fprintf(debugFP, "Append: in='%s' %d\n", text, addBraces); fflush(debugFP);
13181 text = GetInfoFromComment( index, text ); /* [HGM] PV time: strip PV info from comment */
13184 while (*text == '\n') text++;
13185 len = strlen(text);
13186 while (len > 0 && text[len - 1] == '\n') len--;
13188 if (len == 0) return;
13190 if (commentList[index] != NULL) {
13191 old = commentList[index];
13192 oldlen = strlen(old);
13193 while(commentList[index][oldlen-1] == '\n')
13194 commentList[index][--oldlen] = NULLCHAR;
13195 commentList[index] = (char *) malloc(oldlen + len + 6); // might waste 4
13196 strcpy(commentList[index], old);
13198 // [HGM] braces: join "{A\n}\n" + "{\nB}" as "{A\nB\n}"
13199 if(commentList[index][oldlen-1] == '}' && (text[0] == '{' || addBraces)) {
13200 if(addBraces) addBraces = FALSE; else { text++; len--; }
13201 while (*text == '\n') { text++; len--; }
13202 commentList[index][--oldlen] = NULLCHAR;
13204 if(addBraces) strcat(commentList[index], "\n{\n");
13205 else strcat(commentList[index], "\n");
13206 strcat(commentList[index], text);
13207 if(addBraces) strcat(commentList[index], "\n}\n");
13208 else strcat(commentList[index], "\n");
13210 commentList[index] = (char *) malloc(len + 6); // perhaps wastes 4...
13212 strcpy(commentList[index], "{\n");
13213 else commentList[index][0] = NULLCHAR;
13214 strcat(commentList[index], text);
13215 strcat(commentList[index], "\n");
13216 if(addBraces) strcat(commentList[index], "}\n");
13220 static char * FindStr( char * text, char * sub_text )
13222 char * result = strstr( text, sub_text );
13224 if( result != NULL ) {
13225 result += strlen( sub_text );
13231 /* [AS] Try to extract PV info from PGN comment */
13232 /* [HGM] PV time: and then remove it, to prevent it appearing twice */
13233 char *GetInfoFromComment( int index, char * text )
13237 if( text != NULL && index > 0 ) {
13240 int time = -1, sec = 0, deci;
13241 char * s_eval = FindStr( text, "[%eval " );
13242 char * s_emt = FindStr( text, "[%emt " );
13244 if( s_eval != NULL || s_emt != NULL ) {
13248 if( s_eval != NULL ) {
13249 if( sscanf( s_eval, "%d,%d%c", &score, &depth, &delim ) != 3 ) {
13253 if( delim != ']' ) {
13258 if( s_emt != NULL ) {
13263 /* We expect something like: [+|-]nnn.nn/dd */
13266 if(*text != '{') return text; // [HGM] braces: must be normal comment
13268 sep = strchr( text, '/' );
13269 if( sep == NULL || sep < (text+4) ) {
13273 time = -1; sec = -1; deci = -1;
13274 if( sscanf( text+1, "%d.%d/%d %d:%d", &score, &score_lo, &depth, &time, &sec ) != 5 &&
13275 sscanf( text+1, "%d.%d/%d %d.%d", &score, &score_lo, &depth, &time, &deci ) != 5 &&
13276 sscanf( text+1, "%d.%d/%d %d", &score, &score_lo, &depth, &time ) != 4 &&
13277 sscanf( text+1, "%d.%d/%d", &score, &score_lo, &depth ) != 3 ) {
13281 if( score_lo < 0 || score_lo >= 100 ) {
13285 if(sec >= 0) time = 600*time + 10*sec; else
13286 if(deci >= 0) time = 10*time + deci; else time *= 10; // deci-sec
13288 score = score >= 0 ? score*100 + score_lo : score*100 - score_lo;
13290 /* [HGM] PV time: now locate end of PV info */
13291 while( *++sep >= '0' && *sep <= '9'); // strip depth
13293 while( *++sep >= '0' && *sep <= '9'); // strip time
13295 while( *++sep >= '0' && *sep <= '9'); // strip seconds
13297 while( *++sep >= '0' && *sep <= '9'); // strip fractional seconds
13298 while(*sep == ' ') sep++;
13309 pvInfoList[index-1].depth = depth;
13310 pvInfoList[index-1].score = score;
13311 pvInfoList[index-1].time = 10*time; // centi-sec
13312 if(*sep == '}') *sep = 0; else *--sep = '{';
13318 SendToProgram(message, cps)
13320 ChessProgramState *cps;
13322 int count, outCount, error;
13325 if (cps->pr == NULL) return;
13328 if (appData.debugMode) {
13331 fprintf(debugFP, "%ld >%-6s: %s",
13332 SubtractTimeMarks(&now, &programStartTime),
13333 cps->which, message);
13336 count = strlen(message);
13337 outCount = OutputToProcess(cps->pr, message, count, &error);
13338 if (outCount < count && !exiting
13339 && !endingGame) { /* [HGM] crash: to not hang GameEnds() writing to deceased engines */
13340 sprintf(buf, _("Error writing to %s chess program"), cps->which);
13341 if(gameInfo.resultDetails==NULL) { /* [HGM] crash: if game in progress, give reason for abort */
13342 if((signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
13343 gameInfo.result = GameIsDrawn; /* [HGM] accept exit as draw claim */
13344 sprintf(buf, "%s program exits in draw position (%s)", cps->which, cps->program);
13346 gameInfo.result = cps->twoMachinesColor[0]=='w' ? BlackWins : WhiteWins;
13348 gameInfo.resultDetails = StrSave(buf);
13350 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, error, 1); else errorExitStatus = 1;
13355 ReceiveFromProgram(isr, closure, message, count, error)
13356 InputSourceRef isr;
13364 ChessProgramState *cps = (ChessProgramState *)closure;
13366 if (isr != cps->isr) return; /* Killed intentionally */
13370 _("Error: %s chess program (%s) exited unexpectedly"),
13371 cps->which, cps->program);
13372 if(gameInfo.resultDetails==NULL) { /* [HGM] crash: if game in progress, give reason for abort */
13373 if((signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
13374 gameInfo.result = GameIsDrawn; /* [HGM] accept exit as draw claim */
13375 sprintf(buf, _("%s program exits in draw position (%s)"), cps->which, cps->program);
13377 gameInfo.result = cps->twoMachinesColor[0]=='w' ? BlackWins : WhiteWins;
13379 gameInfo.resultDetails = StrSave(buf);
13381 RemoveInputSource(cps->isr);
13382 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, 0, 1); else errorExitStatus = 1;
13385 _("Error reading from %s chess program (%s)"),
13386 cps->which, cps->program);
13387 RemoveInputSource(cps->isr);
13389 /* [AS] Program is misbehaving badly... kill it */
13390 if( count == -2 ) {
13391 DestroyChildProcess( cps->pr, 9 );
13395 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, error, 1); else errorExitStatus = 1;
13400 if ((end_str = strchr(message, '\r')) != NULL)
13401 *end_str = NULLCHAR;
13402 if ((end_str = strchr(message, '\n')) != NULL)
13403 *end_str = NULLCHAR;
13405 if (appData.debugMode) {
13406 TimeMark now; int print = 1;
13407 char *quote = ""; char c; int i;
13409 if(appData.engineComments != 1) { /* [HGM] debug: decide if protocol-violating output is written */
13410 char start = message[0];
13411 if(start >='A' && start <= 'Z') start += 'a' - 'A'; // be tolerant to capitalizing
13412 if(sscanf(message, "%d%c%d%d%d", &i, &c, &i, &i, &i) != 5 &&
13413 sscanf(message, "move %c", &c)!=1 && sscanf(message, "offer%c", &c)!=1 &&
13414 sscanf(message, "resign%c", &c)!=1 && sscanf(message, "feature %c", &c)!=1 &&
13415 sscanf(message, "error %c", &c)!=1 && sscanf(message, "illegal %c", &c)!=1 &&
13416 sscanf(message, "tell%c", &c)!=1 && sscanf(message, "0-1 %c", &c)!=1 &&
13417 sscanf(message, "1-0 %c", &c)!=1 && sscanf(message, "1/2-1/2 %c", &c)!=1 &&
13418 sscanf(message, "pong %c", &c)!=1 && start != '#') {
13419 quote = appData.engineComments == 2 ? "# " : "### NON-COMPLIANT! ### ";
13420 print = (appData.engineComments >= 2);
13422 message[0] = start; // restore original message
13426 fprintf(debugFP, "%ld <%-6s: %s%s\n",
13427 SubtractTimeMarks(&now, &programStartTime), cps->which,
13433 /* [DM] if icsEngineAnalyze is active we block all whisper and kibitz output, because nobody want to see this */
13434 if (appData.icsEngineAnalyze) {
13435 if (strstr(message, "whisper") != NULL ||
13436 strstr(message, "kibitz") != NULL ||
13437 strstr(message, "tellics") != NULL) return;
13440 HandleMachineMove(message, cps);
13445 SendTimeControl(cps, mps, tc, inc, sd, st)
13446 ChessProgramState *cps;
13447 int mps, inc, sd, st;
13453 if( timeControl_2 > 0 ) {
13454 if( (gameMode == MachinePlaysBlack) || (gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b') ) {
13455 tc = timeControl_2;
13458 tc /= cps->timeOdds; /* [HGM] time odds: apply before telling engine */
13459 inc /= cps->timeOdds;
13460 st /= cps->timeOdds;
13462 seconds = (tc / 1000) % 60; /* [HGM] displaced to after applying odds */
13465 /* Set exact time per move, normally using st command */
13466 if (cps->stKludge) {
13467 /* GNU Chess 4 has no st command; uses level in a nonstandard way */
13469 if (seconds == 0) {
13470 sprintf(buf, "level 1 %d\n", st/60);
13472 sprintf(buf, "level 1 %d:%02d\n", st/60, seconds);
13475 sprintf(buf, "st %d\n", st);
13478 /* Set conventional or incremental time control, using level command */
13479 if (seconds == 0) {
13480 /* Note old gnuchess bug -- minutes:seconds used to not work.
13481 Fixed in later versions, but still avoid :seconds
13482 when seconds is 0. */
13483 sprintf(buf, "level %d %ld %d\n", mps, tc/60000, inc/1000);
13485 sprintf(buf, "level %d %ld:%02d %d\n", mps, tc/60000,
13486 seconds, inc/1000);
13489 SendToProgram(buf, cps);
13491 /* Orthoganally (except for GNU Chess 4), limit time to st seconds */
13492 /* Orthogonally, limit search to given depth */
13494 if (cps->sdKludge) {
13495 sprintf(buf, "depth\n%d\n", sd);
13497 sprintf(buf, "sd %d\n", sd);
13499 SendToProgram(buf, cps);
13502 if(cps->nps > 0) { /* [HGM] nps */
13503 if(cps->supportsNPS == FALSE) cps->nps = -1; // don't use if engine explicitly says not supported!
13505 sprintf(buf, "nps %d\n", cps->nps);
13506 SendToProgram(buf, cps);
13511 ChessProgramState *WhitePlayer()
13512 /* [HGM] return pointer to 'first' or 'second', depending on who plays white */
13514 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b' ||
13515 gameMode == BeginningOfGame || gameMode == MachinePlaysBlack)
13521 SendTimeRemaining(cps, machineWhite)
13522 ChessProgramState *cps;
13523 int /*boolean*/ machineWhite;
13525 char message[MSG_SIZ];
13528 /* Note: this routine must be called when the clocks are stopped
13529 or when they have *just* been set or switched; otherwise
13530 it will be off by the time since the current tick started.
13532 if (machineWhite) {
13533 time = whiteTimeRemaining / 10;
13534 otime = blackTimeRemaining / 10;
13536 time = blackTimeRemaining / 10;
13537 otime = whiteTimeRemaining / 10;
13539 /* [HGM] translate opponent's time by time-odds factor */
13540 otime = (otime * cps->other->timeOdds) / cps->timeOdds;
13541 if (appData.debugMode) {
13542 fprintf(debugFP, "time odds: %f %f \n", cps->timeOdds, cps->other->timeOdds);
13545 if (time <= 0) time = 1;
13546 if (otime <= 0) otime = 1;
13548 sprintf(message, "time %ld\n", time);
13549 SendToProgram(message, cps);
13551 sprintf(message, "otim %ld\n", otime);
13552 SendToProgram(message, cps);
13556 BoolFeature(p, name, loc, cps)
13560 ChessProgramState *cps;
13563 int len = strlen(name);
13565 if (strncmp((*p), name, len) == 0 && (*p)[len] == '=') {
13567 sscanf(*p, "%d", &val);
13569 while (**p && **p != ' ') (*p)++;
13570 sprintf(buf, "accepted %s\n", name);
13571 SendToProgram(buf, cps);
13578 IntFeature(p, name, loc, cps)
13582 ChessProgramState *cps;
13585 int len = strlen(name);
13586 if (strncmp((*p), name, len) == 0 && (*p)[len] == '=') {
13588 sscanf(*p, "%d", loc);
13589 while (**p && **p != ' ') (*p)++;
13590 sprintf(buf, "accepted %s\n", name);
13591 SendToProgram(buf, cps);
13598 StringFeature(p, name, loc, cps)
13602 ChessProgramState *cps;
13605 int len = strlen(name);
13606 if (strncmp((*p), name, len) == 0
13607 && (*p)[len] == '=' && (*p)[len+1] == '\"') {
13609 sscanf(*p, "%[^\"]", loc);
13610 while (**p && **p != '\"') (*p)++;
13611 if (**p == '\"') (*p)++;
13612 sprintf(buf, "accepted %s\n", name);
13613 SendToProgram(buf, cps);
13620 ParseOption(Option *opt, ChessProgramState *cps)
13621 // [HGM] options: process the string that defines an engine option, and determine
13622 // name, type, default value, and allowed value range
13624 char *p, *q, buf[MSG_SIZ];
13625 int n, min = (-1)<<31, max = 1<<31, def;
13627 if(p = strstr(opt->name, " -spin ")) {
13628 if((n = sscanf(p, " -spin %d %d %d", &def, &min, &max)) < 3 ) return FALSE;
13629 if(max < min) max = min; // enforce consistency
13630 if(def < min) def = min;
13631 if(def > max) def = max;
13636 } else if((p = strstr(opt->name, " -slider "))) {
13637 // for now -slider is a synonym for -spin, to already provide compatibility with future polyglots
13638 if((n = sscanf(p, " -slider %d %d %d", &def, &min, &max)) < 3 ) return FALSE;
13639 if(max < min) max = min; // enforce consistency
13640 if(def < min) def = min;
13641 if(def > max) def = max;
13645 opt->type = Spin; // Slider;
13646 } else if((p = strstr(opt->name, " -string "))) {
13647 opt->textValue = p+9;
13648 opt->type = TextBox;
13649 } else if((p = strstr(opt->name, " -file "))) {
13650 // for now -file is a synonym for -string, to already provide compatibility with future polyglots
13651 opt->textValue = p+7;
13652 opt->type = TextBox; // FileName;
13653 } else if((p = strstr(opt->name, " -path "))) {
13654 // for now -file is a synonym for -string, to already provide compatibility with future polyglots
13655 opt->textValue = p+7;
13656 opt->type = TextBox; // PathName;
13657 } else if(p = strstr(opt->name, " -check ")) {
13658 if(sscanf(p, " -check %d", &def) < 1) return FALSE;
13659 opt->value = (def != 0);
13660 opt->type = CheckBox;
13661 } else if(p = strstr(opt->name, " -combo ")) {
13662 opt->textValue = (char*) (&cps->comboList[cps->comboCnt]); // cheat with pointer type
13663 cps->comboList[cps->comboCnt++] = q = p+8; // holds possible choices
13664 if(*q == '*') cps->comboList[cps->comboCnt-1]++;
13665 opt->value = n = 0;
13666 while(q = StrStr(q, " /// ")) {
13667 n++; *q = 0; // count choices, and null-terminate each of them
13669 if(*q == '*') { // remember default, which is marked with * prefix
13673 cps->comboList[cps->comboCnt++] = q;
13675 cps->comboList[cps->comboCnt++] = NULL;
13677 opt->type = ComboBox;
13678 } else if(p = strstr(opt->name, " -button")) {
13679 opt->type = Button;
13680 } else if(p = strstr(opt->name, " -save")) {
13681 opt->type = SaveButton;
13682 } else return FALSE;
13683 *p = 0; // terminate option name
13684 // now look if the command-line options define a setting for this engine option.
13685 if(cps->optionSettings && cps->optionSettings[0])
13686 p = strstr(cps->optionSettings, opt->name); else p = NULL;
13687 if(p && (p == cps->optionSettings || p[-1] == ',')) {
13688 sprintf(buf, "option %s", p);
13689 if(p = strstr(buf, ",")) *p = 0;
13691 SendToProgram(buf, cps);
13697 FeatureDone(cps, val)
13698 ChessProgramState* cps;
13701 DelayedEventCallback cb = GetDelayedEvent();
13702 if ((cb == InitBackEnd3 && cps == &first) ||
13703 (cb == TwoMachinesEventIfReady && cps == &second)) {
13704 CancelDelayedEvent();
13705 ScheduleDelayedEvent(cb, val ? 1 : 3600000);
13707 cps->initDone = val;
13710 /* Parse feature command from engine */
13712 ParseFeatures(args, cps)
13714 ChessProgramState *cps;
13722 while (*p == ' ') p++;
13723 if (*p == NULLCHAR) return;
13725 if (BoolFeature(&p, "setboard", &cps->useSetboard, cps)) continue;
13726 if (BoolFeature(&p, "time", &cps->sendTime, cps)) continue;
13727 if (BoolFeature(&p, "draw", &cps->sendDrawOffers, cps)) continue;
13728 if (BoolFeature(&p, "sigint", &cps->useSigint, cps)) continue;
13729 if (BoolFeature(&p, "sigterm", &cps->useSigterm, cps)) continue;
13730 if (BoolFeature(&p, "reuse", &val, cps)) {
13731 /* Engine can disable reuse, but can't enable it if user said no */
13732 if (!val) cps->reuse = FALSE;
13735 if (BoolFeature(&p, "analyze", &cps->analysisSupport, cps)) continue;
13736 if (StringFeature(&p, "myname", &cps->tidy, cps)) {
13737 if (gameMode == TwoMachinesPlay) {
13738 DisplayTwoMachinesTitle();
13744 if (StringFeature(&p, "variants", &cps->variants, cps)) continue;
13745 if (BoolFeature(&p, "san", &cps->useSAN, cps)) continue;
13746 if (BoolFeature(&p, "ping", &cps->usePing, cps)) continue;
13747 if (BoolFeature(&p, "playother", &cps->usePlayother, cps)) continue;
13748 if (BoolFeature(&p, "colors", &cps->useColors, cps)) continue;
13749 if (BoolFeature(&p, "usermove", &cps->useUsermove, cps)) continue;
13750 if (BoolFeature(&p, "ics", &cps->sendICS, cps)) continue;
13751 if (BoolFeature(&p, "name", &cps->sendName, cps)) continue;
13752 if (BoolFeature(&p, "pause", &val, cps)) continue; /* unused at present */
13753 if (IntFeature(&p, "done", &val, cps)) {
13754 FeatureDone(cps, val);
13757 /* Added by Tord: */
13758 if (BoolFeature(&p, "fen960", &cps->useFEN960, cps)) continue;
13759 if (BoolFeature(&p, "oocastle", &cps->useOOCastle, cps)) continue;
13760 /* End of additions by Tord */
13762 /* [HGM] added features: */
13763 if (BoolFeature(&p, "debug", &cps->debug, cps)) continue;
13764 if (BoolFeature(&p, "nps", &cps->supportsNPS, cps)) continue;
13765 if (IntFeature(&p, "level", &cps->maxNrOfSessions, cps)) continue;
13766 if (BoolFeature(&p, "memory", &cps->memSize, cps)) continue;
13767 if (BoolFeature(&p, "smp", &cps->maxCores, cps)) continue;
13768 if (StringFeature(&p, "egt", &cps->egtFormats, cps)) continue;
13769 if (StringFeature(&p, "option", &(cps->option[cps->nrOptions].name), cps)) {
13770 if(!ParseOption(&(cps->option[cps->nrOptions++]), cps)) { // [HGM] options: add option feature
13771 sprintf(buf, "rejected option %s\n", cps->option[--cps->nrOptions].name);
13772 SendToProgram(buf, cps);
13775 if(cps->nrOptions >= MAX_OPTIONS) {
13777 sprintf(buf, "%s engine has too many options\n", cps->which);
13778 DisplayError(buf, 0);
13782 /* End of additions by HGM */
13784 /* unknown feature: complain and skip */
13786 while (*q && *q != '=') q++;
13787 sprintf(buf, "rejected %.*s\n", (int)(q-p), p);
13788 SendToProgram(buf, cps);
13794 while (*p && *p != '\"') p++;
13795 if (*p == '\"') p++;
13797 while (*p && *p != ' ') p++;
13805 PeriodicUpdatesEvent(newState)
13808 if (newState == appData.periodicUpdates)
13811 appData.periodicUpdates=newState;
13813 /* Display type changes, so update it now */
13814 // DisplayAnalysis();
13816 /* Get the ball rolling again... */
13818 AnalysisPeriodicEvent(1);
13819 StartAnalysisClock();
13824 PonderNextMoveEvent(newState)
13827 if (newState == appData.ponderNextMove) return;
13828 if (gameMode == EditPosition) EditPositionDone(TRUE);
13830 SendToProgram("hard\n", &first);
13831 if (gameMode == TwoMachinesPlay) {
13832 SendToProgram("hard\n", &second);
13835 SendToProgram("easy\n", &first);
13836 thinkOutput[0] = NULLCHAR;
13837 if (gameMode == TwoMachinesPlay) {
13838 SendToProgram("easy\n", &second);
13841 appData.ponderNextMove = newState;
13845 NewSettingEvent(option, feature, command, value)
13847 int option, value, *feature;
13851 if (gameMode == EditPosition) EditPositionDone(TRUE);
13852 sprintf(buf, "%s%s %d\n", (option ? "option ": ""), command, value);
13853 if(feature == NULL || *feature) SendToProgram(buf, &first);
13854 if (gameMode == TwoMachinesPlay) {
13855 if(feature == NULL || feature[(int*)&second - (int*)&first]) SendToProgram(buf, &second);
13860 ShowThinkingEvent()
13861 // [HGM] thinking: this routine is now also called from "Options -> Engine..." popup
13863 static int oldState = 2; // kludge alert! Neither true nor fals, so first time oldState is always updated
13864 int newState = appData.showThinking
13865 // [HGM] thinking: other features now need thinking output as well
13866 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp();
13868 if (oldState == newState) return;
13869 oldState = newState;
13870 if (gameMode == EditPosition) EditPositionDone(TRUE);
13872 SendToProgram("post\n", &first);
13873 if (gameMode == TwoMachinesPlay) {
13874 SendToProgram("post\n", &second);
13877 SendToProgram("nopost\n", &first);
13878 thinkOutput[0] = NULLCHAR;
13879 if (gameMode == TwoMachinesPlay) {
13880 SendToProgram("nopost\n", &second);
13883 // appData.showThinking = newState; // [HGM] thinking: responsible option should already have be changed when calling this routine!
13887 AskQuestionEvent(title, question, replyPrefix, which)
13888 char *title; char *question; char *replyPrefix; char *which;
13890 ProcRef pr = (which[0] == '1') ? first.pr : second.pr;
13891 if (pr == NoProc) return;
13892 AskQuestion(title, question, replyPrefix, pr);
13896 DisplayMove(moveNumber)
13899 char message[MSG_SIZ];
13901 char cpThinkOutput[MSG_SIZ];
13903 if(appData.noGUI) return; // [HGM] fast: suppress display of moves
13905 if (moveNumber == forwardMostMove - 1 ||
13906 gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
13908 safeStrCpy(cpThinkOutput, thinkOutput, sizeof(cpThinkOutput));
13910 if (strchr(cpThinkOutput, '\n')) {
13911 *strchr(cpThinkOutput, '\n') = NULLCHAR;
13914 *cpThinkOutput = NULLCHAR;
13917 /* [AS] Hide thinking from human user */
13918 if( appData.hideThinkingFromHuman && gameMode != TwoMachinesPlay ) {
13919 *cpThinkOutput = NULLCHAR;
13920 if( thinkOutput[0] != NULLCHAR ) {
13923 for( i=0; i<=hiddenThinkOutputState; i++ ) {
13924 cpThinkOutput[i] = '.';
13926 cpThinkOutput[i] = NULLCHAR;
13927 hiddenThinkOutputState = (hiddenThinkOutputState + 1) % 3;
13931 if (moveNumber == forwardMostMove - 1 &&
13932 gameInfo.resultDetails != NULL) {
13933 if (gameInfo.resultDetails[0] == NULLCHAR) {
13934 sprintf(res, " %s", PGNResult(gameInfo.result));
13936 sprintf(res, " {%s} %s",
13937 T_(gameInfo.resultDetails), PGNResult(gameInfo.result));
13943 if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
13944 DisplayMessage(res, cpThinkOutput);
13946 sprintf(message, "%d.%s%s%s", moveNumber / 2 + 1,
13947 WhiteOnMove(moveNumber) ? " " : ".. ",
13948 parseList[moveNumber], res);
13949 DisplayMessage(message, cpThinkOutput);
13954 DisplayComment(moveNumber, text)
13958 char title[MSG_SIZ];
13959 char buf[8000]; // comment can be long!
13962 if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
13963 strcpy(title, "Comment");
13965 sprintf(title, "Comment on %d.%s%s", moveNumber / 2 + 1,
13966 WhiteOnMove(moveNumber) ? " " : ".. ",
13967 parseList[moveNumber]);
13969 // [HGM] PV info: display PV info together with (or as) comment
13970 if(moveNumber >= 0 && (depth = pvInfoList[moveNumber].depth) > 0) {
13971 if(text == NULL) text = "";
13972 score = pvInfoList[moveNumber].score;
13973 sprintf(buf, "%s%.2f/%d %d\n%s", score>0 ? "+" : "", score/100.,
13974 depth, (pvInfoList[moveNumber].time+50)/100, text);
13977 if (text != NULL && (appData.autoDisplayComment || commentUp))
13978 CommentPopUp(title, text);
13981 /* This routine sends a ^C interrupt to gnuchess, to awaken it if it
13982 * might be busy thinking or pondering. It can be omitted if your
13983 * gnuchess is configured to stop thinking immediately on any user
13984 * input. However, that gnuchess feature depends on the FIONREAD
13985 * ioctl, which does not work properly on some flavors of Unix.
13989 ChessProgramState *cps;
13992 if (!cps->useSigint) return;
13993 if (appData.noChessProgram || (cps->pr == NoProc)) return;
13994 switch (gameMode) {
13995 case MachinePlaysWhite:
13996 case MachinePlaysBlack:
13997 case TwoMachinesPlay:
13998 case IcsPlayingWhite:
13999 case IcsPlayingBlack:
14002 /* Skip if we know it isn't thinking */
14003 if (!cps->maybeThinking) return;
14004 if (appData.debugMode)
14005 fprintf(debugFP, "Interrupting %s\n", cps->which);
14006 InterruptChildProcess(cps->pr);
14007 cps->maybeThinking = FALSE;
14012 #endif /*ATTENTION*/
14018 if (whiteTimeRemaining <= 0) {
14021 if (appData.icsActive) {
14022 if (appData.autoCallFlag &&
14023 gameMode == IcsPlayingBlack && !blackFlag) {
14024 SendToICS(ics_prefix);
14025 SendToICS("flag\n");
14029 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Both flags fell"));
14031 if(gameMode != TwoMachinesPlay) DisplayTitle(_("White's flag fell"));
14032 if (appData.autoCallFlag) {
14033 GameEnds(BlackWins, "Black wins on time", GE_XBOARD);
14040 if (blackTimeRemaining <= 0) {
14043 if (appData.icsActive) {
14044 if (appData.autoCallFlag &&
14045 gameMode == IcsPlayingWhite && !whiteFlag) {
14046 SendToICS(ics_prefix);
14047 SendToICS("flag\n");
14051 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Both flags fell"));
14053 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Black's flag fell"));
14054 if (appData.autoCallFlag) {
14055 GameEnds(WhiteWins, "White wins on time", GE_XBOARD);
14068 if (!appData.clockMode || appData.icsActive || searchTime || // [HGM] st: no inc in st mode
14069 gameMode == PlayFromGameFile || forwardMostMove == 0) return;
14072 * add time to clocks when time control is achieved ([HGM] now also used for increment)
14074 if ( !WhiteOnMove(forwardMostMove) ) {
14075 /* White made time control */
14076 lastWhite -= whiteTimeRemaining; // [HGM] contains start time, socalculate thinking time
14077 whiteTimeRemaining += GetTimeQuota((forwardMostMove-whiteStartMove-1)/2, lastWhite, whiteTC)
14078 /* [HGM] time odds: correct new time quota for time odds! */
14079 / WhitePlayer()->timeOdds;
14080 lastBlack = blackTimeRemaining; // [HGM] leave absolute time (after quota), so next switch we can us it to calculate thinking time
14082 lastBlack -= blackTimeRemaining;
14083 /* Black made time control */
14084 blackTimeRemaining += GetTimeQuota((forwardMostMove-blackStartMove-1)/2, lastBlack, blackTC)
14085 / WhitePlayer()->other->timeOdds;
14086 lastWhite = whiteTimeRemaining;
14091 DisplayBothClocks()
14093 int wom = gameMode == EditPosition ?
14094 !blackPlaysFirst : WhiteOnMove(currentMove);
14095 DisplayWhiteClock(whiteTimeRemaining, wom);
14096 DisplayBlackClock(blackTimeRemaining, !wom);
14100 /* Timekeeping seems to be a portability nightmare. I think everyone
14101 has ftime(), but I'm really not sure, so I'm including some ifdefs
14102 to use other calls if you don't. Clocks will be less accurate if
14103 you have neither ftime nor gettimeofday.
14106 /* VS 2008 requires the #include outside of the function */
14107 #if !HAVE_GETTIMEOFDAY && HAVE_FTIME
14108 #include <sys/timeb.h>
14111 /* Get the current time as a TimeMark */
14116 #if HAVE_GETTIMEOFDAY
14118 struct timeval timeVal;
14119 struct timezone timeZone;
14121 gettimeofday(&timeVal, &timeZone);
14122 tm->sec = (long) timeVal.tv_sec;
14123 tm->ms = (int) (timeVal.tv_usec / 1000L);
14125 #else /*!HAVE_GETTIMEOFDAY*/
14128 // include <sys/timeb.h> / moved to just above start of function
14129 struct timeb timeB;
14132 tm->sec = (long) timeB.time;
14133 tm->ms = (int) timeB.millitm;
14135 #else /*!HAVE_FTIME && !HAVE_GETTIMEOFDAY*/
14136 tm->sec = (long) time(NULL);
14142 /* Return the difference in milliseconds between two
14143 time marks. We assume the difference will fit in a long!
14146 SubtractTimeMarks(tm2, tm1)
14147 TimeMark *tm2, *tm1;
14149 return 1000L*(tm2->sec - tm1->sec) +
14150 (long) (tm2->ms - tm1->ms);
14155 * Code to manage the game clocks.
14157 * In tournament play, black starts the clock and then white makes a move.
14158 * We give the human user a slight advantage if he is playing white---the
14159 * clocks don't run until he makes his first move, so it takes zero time.
14160 * Also, we don't account for network lag, so we could get out of sync
14161 * with GNU Chess's clock -- but then, referees are always right.
14164 static TimeMark tickStartTM;
14165 static long intendedTickLength;
14168 NextTickLength(timeRemaining)
14169 long timeRemaining;
14171 long nominalTickLength, nextTickLength;
14173 if (timeRemaining > 0L && timeRemaining <= 10000L)
14174 nominalTickLength = 100L;
14176 nominalTickLength = 1000L;
14177 nextTickLength = timeRemaining % nominalTickLength;
14178 if (nextTickLength <= 0) nextTickLength += nominalTickLength;
14180 return nextTickLength;
14183 /* Adjust clock one minute up or down */
14185 AdjustClock(Boolean which, int dir)
14187 if(which) blackTimeRemaining += 60000*dir;
14188 else whiteTimeRemaining += 60000*dir;
14189 DisplayBothClocks();
14192 /* Stop clocks and reset to a fresh time control */
14196 (void) StopClockTimer();
14197 if (appData.icsActive) {
14198 whiteTimeRemaining = blackTimeRemaining = 0;
14199 } else if (searchTime) {
14200 whiteTimeRemaining = 1000*searchTime / WhitePlayer()->timeOdds;
14201 blackTimeRemaining = 1000*searchTime / WhitePlayer()->other->timeOdds;
14202 } else { /* [HGM] correct new time quote for time odds */
14203 whiteTC = blackTC = fullTimeControlString;
14204 whiteTimeRemaining = GetTimeQuota(-1, 0, whiteTC) / WhitePlayer()->timeOdds;
14205 blackTimeRemaining = GetTimeQuota(-1, 0, blackTC) / WhitePlayer()->other->timeOdds;
14207 if (whiteFlag || blackFlag) {
14209 whiteFlag = blackFlag = FALSE;
14211 lastWhite = lastBlack = whiteStartMove = blackStartMove = 0;
14212 DisplayBothClocks();
14215 #define FUDGE 25 /* 25ms = 1/40 sec; should be plenty even for 50 Hz clocks */
14217 /* Decrement running clock by amount of time that has passed */
14221 long timeRemaining;
14222 long lastTickLength, fudge;
14225 if (!appData.clockMode) return;
14226 if (gameMode==AnalyzeMode || gameMode == AnalyzeFile) return;
14230 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
14232 /* Fudge if we woke up a little too soon */
14233 fudge = intendedTickLength - lastTickLength;
14234 if (fudge < 0 || fudge > FUDGE) fudge = 0;
14236 if (WhiteOnMove(forwardMostMove)) {
14237 if(whiteNPS >= 0) lastTickLength = 0;
14238 timeRemaining = whiteTimeRemaining -= lastTickLength;
14239 if(timeRemaining < 0) {
14240 GetTimeQuota((forwardMostMove-whiteStartMove-1)/2, 0, whiteTC); // sets suddenDeath & nextSession;
14241 if(suddenDeath) { // [HGM] if we run out of a non-last incremental session, go to the next
14242 whiteStartMove = forwardMostMove; whiteTC = nextSession;
14243 lastWhite= timeRemaining = whiteTimeRemaining += GetTimeQuota(-1, 0, whiteTC);
14246 DisplayWhiteClock(whiteTimeRemaining - fudge,
14247 WhiteOnMove(currentMove < forwardMostMove ? currentMove : forwardMostMove));
14249 if(blackNPS >= 0) lastTickLength = 0;
14250 timeRemaining = blackTimeRemaining -= lastTickLength;
14251 if(timeRemaining < 0) { // [HGM] if we run out of a non-last incremental session, go to the next
14252 GetTimeQuota((forwardMostMove-blackStartMove-1)/2, 0, blackTC);
14254 blackStartMove = forwardMostMove;
14255 lastBlack = timeRemaining = blackTimeRemaining += GetTimeQuota(-1, 0, blackTC=nextSession);
14258 DisplayBlackClock(blackTimeRemaining - fudge,
14259 !WhiteOnMove(currentMove < forwardMostMove ? currentMove : forwardMostMove));
14261 if (CheckFlags()) return;
14264 intendedTickLength = NextTickLength(timeRemaining - fudge) + fudge;
14265 StartClockTimer(intendedTickLength);
14267 /* if the time remaining has fallen below the alarm threshold, sound the
14268 * alarm. if the alarm has sounded and (due to a takeback or time control
14269 * with increment) the time remaining has increased to a level above the
14270 * threshold, reset the alarm so it can sound again.
14273 if (appData.icsActive && appData.icsAlarm) {
14275 /* make sure we are dealing with the user's clock */
14276 if (!( ((gameMode == IcsPlayingWhite) && WhiteOnMove(currentMove)) ||
14277 ((gameMode == IcsPlayingBlack) && !WhiteOnMove(currentMove))
14280 if (alarmSounded && (timeRemaining > appData.icsAlarmTime)) {
14281 alarmSounded = FALSE;
14282 } else if (!alarmSounded && (timeRemaining <= appData.icsAlarmTime)) {
14284 alarmSounded = TRUE;
14290 /* A player has just moved, so stop the previously running
14291 clock and (if in clock mode) start the other one.
14292 We redisplay both clocks in case we're in ICS mode, because
14293 ICS gives us an update to both clocks after every move.
14294 Note that this routine is called *after* forwardMostMove
14295 is updated, so the last fractional tick must be subtracted
14296 from the color that is *not* on move now.
14299 SwitchClocks(int newMoveNr)
14301 long lastTickLength;
14303 int flagged = FALSE;
14307 if (StopClockTimer() && appData.clockMode) {
14308 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
14309 if (!WhiteOnMove(forwardMostMove)) {
14310 if(blackNPS >= 0) lastTickLength = 0;
14311 blackTimeRemaining -= lastTickLength;
14312 /* [HGM] PGNtime: save time for PGN file if engine did not give it */
14313 // if(pvInfoList[forwardMostMove-1].time == -1)
14314 pvInfoList[forwardMostMove-1].time = // use GUI time
14315 (timeRemaining[1][forwardMostMove-1] - blackTimeRemaining)/10;
14317 if(whiteNPS >= 0) lastTickLength = 0;
14318 whiteTimeRemaining -= lastTickLength;
14319 /* [HGM] PGNtime: save time for PGN file if engine did not give it */
14320 // if(pvInfoList[forwardMostMove-1].time == -1)
14321 pvInfoList[forwardMostMove-1].time =
14322 (timeRemaining[0][forwardMostMove-1] - whiteTimeRemaining)/10;
14324 flagged = CheckFlags();
14326 forwardMostMove = newMoveNr; // [HGM] race: change stm when no timer interrupt scheduled
14327 CheckTimeControl();
14329 if (flagged || !appData.clockMode) return;
14331 switch (gameMode) {
14332 case MachinePlaysBlack:
14333 case MachinePlaysWhite:
14334 case BeginningOfGame:
14335 if (pausing) return;
14339 case PlayFromGameFile:
14347 if (searchTime) { // [HGM] st: set clock of player that has to move to max time
14348 if(WhiteOnMove(forwardMostMove))
14349 whiteTimeRemaining = 1000*searchTime / WhitePlayer()->timeOdds;
14350 else blackTimeRemaining = 1000*searchTime / WhitePlayer()->other->timeOdds;
14354 intendedTickLength = NextTickLength(WhiteOnMove(forwardMostMove) ?
14355 whiteTimeRemaining : blackTimeRemaining);
14356 StartClockTimer(intendedTickLength);
14360 /* Stop both clocks */
14364 long lastTickLength;
14367 if (!StopClockTimer()) return;
14368 if (!appData.clockMode) return;
14372 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
14373 if (WhiteOnMove(forwardMostMove)) {
14374 if(whiteNPS >= 0) lastTickLength = 0;
14375 whiteTimeRemaining -= lastTickLength;
14376 DisplayWhiteClock(whiteTimeRemaining, WhiteOnMove(currentMove));
14378 if(blackNPS >= 0) lastTickLength = 0;
14379 blackTimeRemaining -= lastTickLength;
14380 DisplayBlackClock(blackTimeRemaining, !WhiteOnMove(currentMove));
14385 /* Start clock of player on move. Time may have been reset, so
14386 if clock is already running, stop and restart it. */
14390 (void) StopClockTimer(); /* in case it was running already */
14391 DisplayBothClocks();
14392 if (CheckFlags()) return;
14394 if (!appData.clockMode) return;
14395 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) return;
14397 GetTimeMark(&tickStartTM);
14398 intendedTickLength = NextTickLength(WhiteOnMove(forwardMostMove) ?
14399 whiteTimeRemaining : blackTimeRemaining);
14401 /* [HGM] nps: figure out nps factors, by determining which engine plays white and/or black once and for all */
14402 whiteNPS = blackNPS = -1;
14403 if(gameMode == MachinePlaysWhite || gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w'
14404 || appData.zippyPlay && gameMode == IcsPlayingBlack) // first (perhaps only) engine has white
14405 whiteNPS = first.nps;
14406 if(gameMode == MachinePlaysBlack || gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b'
14407 || appData.zippyPlay && gameMode == IcsPlayingWhite) // first (perhaps only) engine has black
14408 blackNPS = first.nps;
14409 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b') // second only used in Two-Machines mode
14410 whiteNPS = second.nps;
14411 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w')
14412 blackNPS = second.nps;
14413 if(appData.debugMode) fprintf(debugFP, "nps: w=%d, b=%d\n", whiteNPS, blackNPS);
14415 StartClockTimer(intendedTickLength);
14422 long second, minute, hour, day;
14424 static char buf[32];
14426 if (ms > 0 && ms <= 9900) {
14427 /* convert milliseconds to tenths, rounding up */
14428 double tenths = floor( ((double)(ms + 99L)) / 100.00 );
14430 sprintf(buf, " %03.1f ", tenths/10.0);
14434 /* convert milliseconds to seconds, rounding up */
14435 /* use floating point to avoid strangeness of integer division
14436 with negative dividends on many machines */
14437 second = (long) floor(((double) (ms + 999L)) / 1000.0);
14444 day = second / (60 * 60 * 24);
14445 second = second % (60 * 60 * 24);
14446 hour = second / (60 * 60);
14447 second = second % (60 * 60);
14448 minute = second / 60;
14449 second = second % 60;
14452 sprintf(buf, " %s%ld:%02ld:%02ld:%02ld ",
14453 sign, day, hour, minute, second);
14455 sprintf(buf, " %s%ld:%02ld:%02ld ", sign, hour, minute, second);
14457 sprintf(buf, " %s%2ld:%02ld ", sign, minute, second);
14464 * This is necessary because some C libraries aren't ANSI C compliant yet.
14467 StrStr(string, match)
14468 char *string, *match;
14472 length = strlen(match);
14474 for (i = strlen(string) - length; i >= 0; i--, string++)
14475 if (!strncmp(match, string, length))
14482 StrCaseStr(string, match)
14483 char *string, *match;
14487 length = strlen(match);
14489 for (i = strlen(string) - length; i >= 0; i--, string++) {
14490 for (j = 0; j < length; j++) {
14491 if (ToLower(match[j]) != ToLower(string[j]))
14494 if (j == length) return string;
14508 c1 = ToLower(*s1++);
14509 c2 = ToLower(*s2++);
14510 if (c1 > c2) return 1;
14511 if (c1 < c2) return -1;
14512 if (c1 == NULLCHAR) return 0;
14521 return isupper(c) ? tolower(c) : c;
14529 return islower(c) ? toupper(c) : c;
14531 #endif /* !_amigados */
14539 if ((ret = (char *) malloc(strlen(s) + 1))) {
14546 StrSavePtr(s, savePtr)
14547 char *s, **savePtr;
14552 if ((*savePtr = (char *) malloc(strlen(s) + 1))) {
14553 strcpy(*savePtr, s);
14565 clock = time((time_t *)NULL);
14566 tm = localtime(&clock);
14567 sprintf(buf, "%04d.%02d.%02d",
14568 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
14569 return StrSave(buf);
14574 PositionToFEN(move, overrideCastling)
14576 char *overrideCastling;
14578 int i, j, fromX, fromY, toX, toY;
14585 whiteToPlay = (gameMode == EditPosition) ?
14586 !blackPlaysFirst : (move % 2 == 0);
14589 /* Piece placement data */
14590 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
14592 for (j = BOARD_LEFT; j < BOARD_RGHT; j++) {
14593 if (boards[move][i][j] == EmptySquare) {
14595 } else { ChessSquare piece = boards[move][i][j];
14596 if (emptycount > 0) {
14597 if(emptycount<10) /* [HGM] can be >= 10 */
14598 *p++ = '0' + emptycount;
14599 else { *p++ = '0' + emptycount/10; *p++ = '0' + emptycount%10; }
14602 if(PieceToChar(piece) == '+') {
14603 /* [HGM] write promoted pieces as '+<unpromoted>' (Shogi) */
14605 piece = (ChessSquare)(DEMOTED piece);
14607 *p++ = PieceToChar(piece);
14609 /* [HGM] flag promoted pieces as '<promoted>~' (Crazyhouse) */
14610 p[-1] = PieceToChar((ChessSquare)(DEMOTED piece));
14615 if (emptycount > 0) {
14616 if(emptycount<10) /* [HGM] can be >= 10 */
14617 *p++ = '0' + emptycount;
14618 else { *p++ = '0' + emptycount/10; *p++ = '0' + emptycount%10; }
14625 /* [HGM] print Crazyhouse or Shogi holdings */
14626 if( gameInfo.holdingsWidth ) {
14627 *(p-1) = '['; /* if we wanted to support BFEN, this could be '/' */
14629 for(i=0; i<gameInfo.holdingsSize; i++) { /* white holdings */
14630 piece = boards[move][i][BOARD_WIDTH-1];
14631 if( piece != EmptySquare )
14632 for(j=0; j<(int) boards[move][i][BOARD_WIDTH-2]; j++)
14633 *p++ = PieceToChar(piece);
14635 for(i=0; i<gameInfo.holdingsSize; i++) { /* black holdings */
14636 piece = boards[move][BOARD_HEIGHT-i-1][0];
14637 if( piece != EmptySquare )
14638 for(j=0; j<(int) boards[move][BOARD_HEIGHT-i-1][1]; j++)
14639 *p++ = PieceToChar(piece);
14642 if( q == p ) *p++ = '-';
14648 *p++ = whiteToPlay ? 'w' : 'b';
14651 if(q = overrideCastling) { // [HGM] FRC: override castling & e.p fields for non-compliant engines
14652 while(*p++ = *q++); if(q != overrideCastling+1) p[-1] = ' '; else --p;
14654 if(nrCastlingRights) {
14656 if(gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom) {
14657 /* [HGM] write directly from rights */
14658 if(boards[move][CASTLING][2] != NoRights &&
14659 boards[move][CASTLING][0] != NoRights )
14660 *p++ = boards[move][CASTLING][0] + AAA + 'A' - 'a';
14661 if(boards[move][CASTLING][2] != NoRights &&
14662 boards[move][CASTLING][1] != NoRights )
14663 *p++ = boards[move][CASTLING][1] + AAA + 'A' - 'a';
14664 if(boards[move][CASTLING][5] != NoRights &&
14665 boards[move][CASTLING][3] != NoRights )
14666 *p++ = boards[move][CASTLING][3] + AAA;
14667 if(boards[move][CASTLING][5] != NoRights &&
14668 boards[move][CASTLING][4] != NoRights )
14669 *p++ = boards[move][CASTLING][4] + AAA;
14672 /* [HGM] write true castling rights */
14673 if( nrCastlingRights == 6 ) {
14674 if(boards[move][CASTLING][0] == BOARD_RGHT-1 &&
14675 boards[move][CASTLING][2] != NoRights ) *p++ = 'K';
14676 if(boards[move][CASTLING][1] == BOARD_LEFT &&
14677 boards[move][CASTLING][2] != NoRights ) *p++ = 'Q';
14678 if(boards[move][CASTLING][3] == BOARD_RGHT-1 &&
14679 boards[move][CASTLING][5] != NoRights ) *p++ = 'k';
14680 if(boards[move][CASTLING][4] == BOARD_LEFT &&
14681 boards[move][CASTLING][5] != NoRights ) *p++ = 'q';
14684 if (q == p) *p++ = '-'; /* No castling rights */
14688 if(gameInfo.variant != VariantShogi && gameInfo.variant != VariantXiangqi &&
14689 gameInfo.variant != VariantShatranj && gameInfo.variant != VariantCourier && gameInfo.variant != VariantMakruk ) {
14690 /* En passant target square */
14691 if (move > backwardMostMove) {
14692 fromX = moveList[move - 1][0] - AAA;
14693 fromY = moveList[move - 1][1] - ONE;
14694 toX = moveList[move - 1][2] - AAA;
14695 toY = moveList[move - 1][3] - ONE;
14696 if (fromY == (whiteToPlay ? BOARD_HEIGHT-2 : 1) &&
14697 toY == (whiteToPlay ? BOARD_HEIGHT-4 : 3) &&
14698 boards[move][toY][toX] == (whiteToPlay ? BlackPawn : WhitePawn) &&
14700 /* 2-square pawn move just happened */
14702 *p++ = whiteToPlay ? '6'+BOARD_HEIGHT-8 : '3';
14706 } else if(move == backwardMostMove) {
14707 // [HGM] perhaps we should always do it like this, and forget the above?
14708 if((signed char)boards[move][EP_STATUS] >= 0) {
14709 *p++ = boards[move][EP_STATUS] + AAA;
14710 *p++ = whiteToPlay ? '6'+BOARD_HEIGHT-8 : '3';
14721 /* [HGM] find reversible plies */
14722 { int i = 0, j=move;
14724 if (appData.debugMode) { int k;
14725 fprintf(debugFP, "write FEN 50-move: %d %d %d\n", initialRulePlies, forwardMostMove, backwardMostMove);
14726 for(k=backwardMostMove; k<=forwardMostMove; k++)
14727 fprintf(debugFP, "e%d. p=%d\n", k, (signed char)boards[k][EP_STATUS]);
14731 while(j > backwardMostMove && (signed char)boards[j][EP_STATUS] <= EP_NONE) j--,i++;
14732 if( j == backwardMostMove ) i += initialRulePlies;
14733 sprintf(p, "%d ", i);
14734 p += i>=100 ? 4 : i >= 10 ? 3 : 2;
14736 /* Fullmove number */
14737 sprintf(p, "%d", (move / 2) + 1);
14739 return StrSave(buf);
14743 ParseFEN(board, blackPlaysFirst, fen)
14745 int *blackPlaysFirst;
14755 /* [HGM] by default clear Crazyhouse holdings, if present */
14756 if(gameInfo.holdingsWidth) {
14757 for(i=0; i<BOARD_HEIGHT; i++) {
14758 board[i][0] = EmptySquare; /* black holdings */
14759 board[i][BOARD_WIDTH-1] = EmptySquare; /* white holdings */
14760 board[i][1] = (ChessSquare) 0; /* black counts */
14761 board[i][BOARD_WIDTH-2] = (ChessSquare) 0; /* white counts */
14765 /* Piece placement data */
14766 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
14769 if (*p == '/' || *p == ' ' || (*p == '[' && i == 0) ) {
14770 if (*p == '/') p++;
14771 emptycount = gameInfo.boardWidth - j;
14772 while (emptycount--)
14773 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
14775 #if(BOARD_FILES >= 10)
14776 } else if(*p=='x' || *p=='X') { /* [HGM] X means 10 */
14777 p++; emptycount=10;
14778 if (j + emptycount > gameInfo.boardWidth) return FALSE;
14779 while (emptycount--)
14780 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
14782 } else if (isdigit(*p)) {
14783 emptycount = *p++ - '0';
14784 while(isdigit(*p)) emptycount = 10*emptycount + *p++ - '0'; /* [HGM] allow > 9 */
14785 if (j + emptycount > gameInfo.boardWidth) return FALSE;
14786 while (emptycount--)
14787 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
14788 } else if (*p == '+' || isalpha(*p)) {
14789 if (j >= gameInfo.boardWidth) return FALSE;
14791 piece = CharToPiece(*++p);
14792 if(piece == EmptySquare) return FALSE; /* unknown piece */
14793 piece = (ChessSquare) (PROMOTED piece ); p++;
14794 if(PieceToChar(piece) != '+') return FALSE; /* unpromotable piece */
14795 } else piece = CharToPiece(*p++);
14797 if(piece==EmptySquare) return FALSE; /* unknown piece */
14798 if(*p == '~') { /* [HGM] make it a promoted piece for Crazyhouse */
14799 piece = (ChessSquare) (PROMOTED piece);
14800 if(PieceToChar(piece) != '~') return FALSE; /* cannot be a promoted piece */
14803 board[i][(j++)+gameInfo.holdingsWidth] = piece;
14809 while (*p == '/' || *p == ' ') p++;
14811 /* [HGM] look for Crazyhouse holdings here */
14812 while(*p==' ') p++;
14813 if( gameInfo.holdingsWidth && p[-1] == '/' || *p == '[') {
14815 if(*p == '-' ) *p++; /* empty holdings */ else {
14816 if( !gameInfo.holdingsWidth ) return FALSE; /* no room to put holdings! */
14817 /* if we would allow FEN reading to set board size, we would */
14818 /* have to add holdings and shift the board read so far here */
14819 while( (piece = CharToPiece(*p) ) != EmptySquare ) {
14821 if((int) piece >= (int) BlackPawn ) {
14822 i = (int)piece - (int)BlackPawn;
14823 i = PieceToNumber((ChessSquare)i);
14824 if( i >= gameInfo.holdingsSize ) return FALSE;
14825 board[BOARD_HEIGHT-1-i][0] = piece; /* black holdings */
14826 board[BOARD_HEIGHT-1-i][1]++; /* black counts */
14828 i = (int)piece - (int)WhitePawn;
14829 i = PieceToNumber((ChessSquare)i);
14830 if( i >= gameInfo.holdingsSize ) return FALSE;
14831 board[i][BOARD_WIDTH-1] = piece; /* white holdings */
14832 board[i][BOARD_WIDTH-2]++; /* black holdings */
14836 if(*p == ']') *p++;
14839 while(*p == ' ') p++;
14843 if(appData.colorNickNames) {
14844 if( c == appData.colorNickNames[0] ) c = 'w'; else
14845 if( c == appData.colorNickNames[1] ) c = 'b';
14849 *blackPlaysFirst = FALSE;
14852 *blackPlaysFirst = TRUE;
14858 /* [HGM] We NO LONGER ignore the rest of the FEN notation */
14859 /* return the extra info in global variiables */
14861 /* set defaults in case FEN is incomplete */
14862 board[EP_STATUS] = EP_UNKNOWN;
14863 for(i=0; i<nrCastlingRights; i++ ) {
14864 board[CASTLING][i] =
14865 gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom ? NoRights : initialRights[i];
14866 } /* assume possible unless obviously impossible */
14867 if(initialRights[0]!=NoRights && board[castlingRank[0]][initialRights[0]] != WhiteRook) board[CASTLING][0] = NoRights;
14868 if(initialRights[1]!=NoRights && board[castlingRank[1]][initialRights[1]] != WhiteRook) board[CASTLING][1] = NoRights;
14869 if(initialRights[2]!=NoRights && board[castlingRank[2]][initialRights[2]] != WhiteUnicorn
14870 && board[castlingRank[2]][initialRights[2]] != WhiteKing) board[CASTLING][2] = NoRights;
14871 if(initialRights[3]!=NoRights && board[castlingRank[3]][initialRights[3]] != BlackRook) board[CASTLING][3] = NoRights;
14872 if(initialRights[4]!=NoRights && board[castlingRank[4]][initialRights[4]] != BlackRook) board[CASTLING][4] = NoRights;
14873 if(initialRights[5]!=NoRights && board[castlingRank[5]][initialRights[5]] != BlackUnicorn
14874 && board[castlingRank[5]][initialRights[5]] != BlackKing) board[CASTLING][5] = NoRights;
14877 while(*p==' ') p++;
14878 if(nrCastlingRights) {
14879 if(*p=='K' || *p=='Q' || *p=='k' || *p=='q' || *p=='-') {
14880 /* castling indicator present, so default becomes no castlings */
14881 for(i=0; i<nrCastlingRights; i++ ) {
14882 board[CASTLING][i] = NoRights;
14885 while(*p=='K' || *p=='Q' || *p=='k' || *p=='q' || *p=='-' ||
14886 (gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom) &&
14887 ( *p >= 'a' && *p < 'a' + gameInfo.boardWidth) ||
14888 ( *p >= 'A' && *p < 'A' + gameInfo.boardWidth) ) {
14889 char c = *p++; int whiteKingFile=NoRights, blackKingFile=NoRights;
14891 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) {
14892 if(board[BOARD_HEIGHT-1][i] == BlackKing) blackKingFile = i;
14893 if(board[0 ][i] == WhiteKing) whiteKingFile = i;
14895 if(gameInfo.variant == VariantTwoKings || gameInfo.variant == VariantKnightmate)
14896 whiteKingFile = blackKingFile = BOARD_WIDTH >> 1; // for these variant scanning fails
14897 if(whiteKingFile == NoRights || board[0][whiteKingFile] != WhiteUnicorn
14898 && board[0][whiteKingFile] != WhiteKing) whiteKingFile = NoRights;
14899 if(blackKingFile == NoRights || board[BOARD_HEIGHT-1][blackKingFile] != BlackUnicorn
14900 && board[BOARD_HEIGHT-1][blackKingFile] != BlackKing) blackKingFile = NoRights;
14903 for(i=BOARD_RGHT-1; board[0][i]!=WhiteRook && i>whiteKingFile; i--);
14904 board[CASTLING][0] = i != whiteKingFile ? i : NoRights;
14905 board[CASTLING][2] = whiteKingFile;
14908 for(i=BOARD_LEFT; i<BOARD_RGHT && board[0][i]!=WhiteRook && i<whiteKingFile; i++);
14909 board[CASTLING][1] = i != whiteKingFile ? i : NoRights;
14910 board[CASTLING][2] = whiteKingFile;
14913 for(i=BOARD_RGHT-1; board[BOARD_HEIGHT-1][i]!=BlackRook && i>blackKingFile; i--);
14914 board[CASTLING][3] = i != blackKingFile ? i : NoRights;
14915 board[CASTLING][5] = blackKingFile;
14918 for(i=BOARD_LEFT; i<BOARD_RGHT && board[BOARD_HEIGHT-1][i]!=BlackRook && i<blackKingFile; i++);
14919 board[CASTLING][4] = i != blackKingFile ? i : NoRights;
14920 board[CASTLING][5] = blackKingFile;
14923 default: /* FRC castlings */
14924 if(c >= 'a') { /* black rights */
14925 for(i=BOARD_LEFT; i<BOARD_RGHT; i++)
14926 if(board[BOARD_HEIGHT-1][i] == BlackKing) break;
14927 if(i == BOARD_RGHT) break;
14928 board[CASTLING][5] = i;
14930 if(board[BOARD_HEIGHT-1][c] < BlackPawn ||
14931 board[BOARD_HEIGHT-1][c] >= BlackKing ) break;
14933 board[CASTLING][3] = c;
14935 board[CASTLING][4] = c;
14936 } else { /* white rights */
14937 for(i=BOARD_LEFT; i<BOARD_RGHT; i++)
14938 if(board[0][i] == WhiteKing) break;
14939 if(i == BOARD_RGHT) break;
14940 board[CASTLING][2] = i;
14941 c -= AAA - 'a' + 'A';
14942 if(board[0][c] >= WhiteKing) break;
14944 board[CASTLING][0] = c;
14946 board[CASTLING][1] = c;
14950 for(i=0; i<nrCastlingRights; i++)
14951 if(board[CASTLING][i] != NoRights) initialRights[i] = board[CASTLING][i];
14952 if (appData.debugMode) {
14953 fprintf(debugFP, "FEN castling rights:");
14954 for(i=0; i<nrCastlingRights; i++)
14955 fprintf(debugFP, " %d", board[CASTLING][i]);
14956 fprintf(debugFP, "\n");
14959 while(*p==' ') p++;
14962 /* read e.p. field in games that know e.p. capture */
14963 if(gameInfo.variant != VariantShogi && gameInfo.variant != VariantXiangqi &&
14964 gameInfo.variant != VariantShatranj && gameInfo.variant != VariantCourier && gameInfo.variant != VariantMakruk ) {
14966 p++; board[EP_STATUS] = EP_NONE;
14968 char c = *p++ - AAA;
14970 if(c < BOARD_LEFT || c >= BOARD_RGHT) return TRUE;
14971 if(*p >= '0' && *p <='9') *p++;
14972 board[EP_STATUS] = c;
14977 if(sscanf(p, "%d", &i) == 1) {
14978 FENrulePlies = i; /* 50-move ply counter */
14979 /* (The move number is still ignored) */
14986 EditPositionPasteFEN(char *fen)
14989 Board initial_position;
14991 if (!ParseFEN(initial_position, &blackPlaysFirst, fen)) {
14992 DisplayError(_("Bad FEN position in clipboard"), 0);
14995 int savedBlackPlaysFirst = blackPlaysFirst;
14996 EditPositionEvent();
14997 blackPlaysFirst = savedBlackPlaysFirst;
14998 CopyBoard(boards[0], initial_position);
14999 initialRulePlies = FENrulePlies; /* [HGM] copy FEN attributes as well */
15000 EditPositionDone(FALSE); // [HGM] fake: do not fake rights if we had FEN
15001 DisplayBothClocks();
15002 DrawPosition(FALSE, boards[currentMove]);
15007 static char cseq[12] = "\\ ";
15009 Boolean set_cont_sequence(char *new_seq)
15014 // handle bad attempts to set the sequence
15016 return 0; // acceptable error - no debug
15018 len = strlen(new_seq);
15019 ret = (len > 0) && (len < sizeof(cseq));
15021 strcpy(cseq, new_seq);
15022 else if (appData.debugMode)
15023 fprintf(debugFP, "Invalid continuation sequence \"%s\" (maximum length is: %u)\n", new_seq, (unsigned) sizeof(cseq)-1);
15028 reformat a source message so words don't cross the width boundary. internal
15029 newlines are not removed. returns the wrapped size (no null character unless
15030 included in source message). If dest is NULL, only calculate the size required
15031 for the dest buffer. lp argument indicats line position upon entry, and it's
15032 passed back upon exit.
15034 int wrap(char *dest, char *src, int count, int width, int *lp)
15036 int len, i, ansi, cseq_len, line, old_line, old_i, old_len, clen;
15038 cseq_len = strlen(cseq);
15039 old_line = line = *lp;
15040 ansi = len = clen = 0;
15042 for (i=0; i < count; i++)
15044 if (src[i] == '\033')
15047 // if we hit the width, back up
15048 if (!ansi && (line >= width) && src[i] != '\n' && src[i] != ' ')
15050 // store i & len in case the word is too long
15051 old_i = i, old_len = len;
15053 // find the end of the last word
15054 while (i && src[i] != ' ' && src[i] != '\n')
15060 // word too long? restore i & len before splitting it
15061 if ((old_i-i+clen) >= width)
15068 if (i && src[i-1] == ' ')
15071 if (src[i] != ' ' && src[i] != '\n')
15078 // now append the newline and continuation sequence
15083 strncpy(dest+len, cseq, cseq_len);
15091 dest[len] = src[i];
15095 if (src[i] == '\n')
15100 if (dest && appData.debugMode)
15102 fprintf(debugFP, "wrap(count:%d,width:%d,line:%d,len:%d,*lp:%d,src: ",
15103 count, width, line, len, *lp);
15104 show_bytes(debugFP, src, count);
15105 fprintf(debugFP, "\ndest: ");
15106 show_bytes(debugFP, dest, len);
15107 fprintf(debugFP, "\n");
15109 *lp = dest ? line : old_line;
15114 // [HGM] vari: routines for shelving variations
15117 PushTail(int firstMove, int lastMove)
15119 int i, j, nrMoves = lastMove - firstMove;
15121 if(appData.icsActive) { // only in local mode
15122 forwardMostMove = currentMove; // mimic old ICS behavior
15125 if(storedGames >= MAX_VARIATIONS-1) return;
15127 // push current tail of game on stack
15128 savedResult[storedGames] = gameInfo.result;
15129 savedDetails[storedGames] = gameInfo.resultDetails;
15130 gameInfo.resultDetails = NULL;
15131 savedFirst[storedGames] = firstMove;
15132 savedLast [storedGames] = lastMove;
15133 savedFramePtr[storedGames] = framePtr;
15134 framePtr -= nrMoves; // reserve space for the boards
15135 for(i=nrMoves; i>=1; i--) { // copy boards to stack, working downwards, in case of overlap
15136 CopyBoard(boards[framePtr+i], boards[firstMove+i]);
15137 for(j=0; j<MOVE_LEN; j++)
15138 moveList[framePtr+i][j] = moveList[firstMove+i-1][j];
15139 for(j=0; j<2*MOVE_LEN; j++)
15140 parseList[framePtr+i][j] = parseList[firstMove+i-1][j];
15141 timeRemaining[0][framePtr+i] = timeRemaining[0][firstMove+i];
15142 timeRemaining[1][framePtr+i] = timeRemaining[1][firstMove+i];
15143 pvInfoList[framePtr+i] = pvInfoList[firstMove+i-1];
15144 pvInfoList[firstMove+i-1].depth = 0;
15145 commentList[framePtr+i] = commentList[firstMove+i];
15146 commentList[firstMove+i] = NULL;
15150 forwardMostMove = firstMove; // truncate game so we can start variation
15151 if(storedGames == 1) GreyRevert(FALSE);
15155 PopTail(Boolean annotate)
15158 char buf[8000], moveBuf[20];
15160 if(appData.icsActive) return FALSE; // only in local mode
15161 if(!storedGames) return FALSE; // sanity
15162 CommentPopDown(); // make sure no stale variation comments to the destroyed line can remain open
15165 ToNrEvent(savedFirst[storedGames]); // sets currentMove
15166 nrMoves = savedLast[storedGames] - currentMove;
15169 if(!WhiteOnMove(currentMove)) sprintf(buf, "(%d...", currentMove+2>>1);
15170 else strcpy(buf, "(");
15171 for(i=currentMove; i<forwardMostMove; i++) {
15173 sprintf(moveBuf, " %d. %s", i+2>>1, SavePart(parseList[i]));
15174 else sprintf(moveBuf, " %s", SavePart(parseList[i]));
15175 strcat(buf, moveBuf);
15176 if(commentList[i]) { strcat(buf, " "); strcat(buf, commentList[i]); }
15177 if(!--cnt) { strcat(buf, "\n"); cnt = 10; }
15181 for(i=1; i<=nrMoves; i++) { // copy last variation back
15182 CopyBoard(boards[currentMove+i], boards[framePtr+i]);
15183 for(j=0; j<MOVE_LEN; j++)
15184 moveList[currentMove+i-1][j] = moveList[framePtr+i][j];
15185 for(j=0; j<2*MOVE_LEN; j++)
15186 parseList[currentMove+i-1][j] = parseList[framePtr+i][j];
15187 timeRemaining[0][currentMove+i] = timeRemaining[0][framePtr+i];
15188 timeRemaining[1][currentMove+i] = timeRemaining[1][framePtr+i];
15189 pvInfoList[currentMove+i-1] = pvInfoList[framePtr+i];
15190 if(commentList[currentMove+i]) free(commentList[currentMove+i]);
15191 commentList[currentMove+i] = commentList[framePtr+i];
15192 commentList[framePtr+i] = NULL;
15194 if(annotate) AppendComment(currentMove+1, buf, FALSE);
15195 framePtr = savedFramePtr[storedGames];
15196 gameInfo.result = savedResult[storedGames];
15197 if(gameInfo.resultDetails != NULL) {
15198 free(gameInfo.resultDetails);
15200 gameInfo.resultDetails = savedDetails[storedGames];
15201 forwardMostMove = currentMove + nrMoves;
15202 if(storedGames == 0) GreyRevert(TRUE);
15208 { // remove all shelved variations
15210 for(i=0; i<storedGames; i++) {
15211 if(savedDetails[i])
15212 free(savedDetails[i]);
15213 savedDetails[i] = NULL;
15215 for(i=framePtr; i<MAX_MOVES; i++) {
15216 if(commentList[i]) free(commentList[i]);
15217 commentList[i] = NULL;
15219 framePtr = MAX_MOVES-1;
15224 LoadVariation(int index, char *text)
15225 { // [HGM] vari: shelve previous line and load new variation, parsed from text around text[index]
15226 char *p = text, *start = NULL, *end = NULL, wait = NULLCHAR;
15227 int level = 0, move;
15229 if(gameMode != EditGame && gameMode != AnalyzeMode) return;
15230 // first find outermost bracketing variation
15231 while(*p) { // hope I got this right... Non-nesting {} and [] can screen each other and nesting ()
15232 if(!wait) { // while inside [] pr {}, ignore everyting except matching closing ]}
15233 if(*p == '{') wait = '}'; else
15234 if(*p == '[') wait = ']'; else
15235 if(*p == '(' && level++ == 0 && p-text < index) start = p+1;
15236 if(*p == ')' && level > 0 && --level == 0 && p-text > index && end == NULL) end = p-1;
15238 if(*p == wait) wait = NULLCHAR; // closing ]} found
15241 if(!start || !end) return; // no variation found, or syntax error in PGN: ignore click
15242 if(appData.debugMode) fprintf(debugFP, "at move %d load variation '%s'\n", currentMove, start);
15243 end[1] = NULLCHAR; // clip off comment beyond variation
15244 ToNrEvent(currentMove-1);
15245 PushTail(currentMove, forwardMostMove); // shelve main variation. This truncates game
15246 // kludge: use ParsePV() to append variation to game
15247 move = currentMove;
15248 ParsePV(start, TRUE);
15249 forwardMostMove = endPV; endPV = -1; currentMove = move; // cleanup what ParsePV did
15250 ClearPremoveHighlights();
15252 ToNrEvent(currentMove+1);