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, 2011, 2012, 2013, 2014, 2015 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 int flock(int f, int code);
63 # define EGBB_NAME "egbbdll64.dll"
65 # define EGBB_NAME "egbbdll.dll"
70 # include <sys/file.h>
75 # define EGBB_NAME "egbbso64.so"
77 # define EGBB_NAME "egbbso.so"
79 // kludge to allow Windows code in back-end by converting it to corresponding Linux code
81 # define HMODULE void *
82 # define LoadLibrary(x) dlopen(x, RTLD_LAZY)
83 # define GetProcAddress dlsym
93 #include <sys/types.h>
102 #else /* not STDC_HEADERS */
105 # else /* not HAVE_STRING_H */
106 # include <strings.h>
107 # endif /* not HAVE_STRING_H */
108 #endif /* not STDC_HEADERS */
111 # include <sys/fcntl.h>
112 #else /* not HAVE_SYS_FCNTL_H */
115 # endif /* HAVE_FCNTL_H */
116 #endif /* not HAVE_SYS_FCNTL_H */
118 #if TIME_WITH_SYS_TIME
119 # include <sys/time.h>
123 # include <sys/time.h>
129 #if defined(_amigados) && !defined(__GNUC__)
134 extern int gettimeofday(struct timeval *, struct timezone *);
142 #include "frontend.h"
149 #include "backendz.h"
150 #include "evalgraph.h"
151 #include "engineoutput.h"
155 # define _(s) gettext (s)
156 # define N_(s) gettext_noop (s)
157 # define T_(s) gettext(s)
170 int establish P((void));
171 void read_from_player P((InputSourceRef isr, VOIDSTAR closure,
172 char *buf, int count, int error));
173 void read_from_ics P((InputSourceRef isr, VOIDSTAR closure,
174 char *buf, int count, int error));
175 void SendToICS P((char *s));
176 void SendToICSDelayed P((char *s, long msdelay));
177 void SendMoveToICS P((ChessMove moveType, int fromX, int fromY, int toX, int toY, char promoChar));
178 void HandleMachineMove P((char *message, ChessProgramState *cps));
179 int AutoPlayOneMove P((void));
180 int LoadGameOneMove P((ChessMove readAhead));
181 int LoadGameFromFile P((char *filename, int n, char *title, int useList));
182 int LoadPositionFromFile P((char *filename, int n, char *title));
183 int SavePositionToFile P((char *filename));
184 void MakeMove P((int fromX, int fromY, int toX, int toY, int promoChar));
185 void ShowMove P((int fromX, int fromY, int toX, int toY));
186 int FinishMove P((ChessMove moveType, int fromX, int fromY, int toX, int toY,
187 /*char*/int promoChar));
188 void BackwardInner P((int target));
189 void ForwardInner P((int target));
190 int Adjudicate P((ChessProgramState *cps));
191 void GameEnds P((ChessMove result, char *resultDetails, int whosays));
192 void EditPositionDone P((Boolean fakeRights));
193 void PrintOpponents P((FILE *fp));
194 void PrintPosition P((FILE *fp, int move));
195 void StartChessProgram P((ChessProgramState *cps));
196 void SendToProgram P((char *message, ChessProgramState *cps));
197 void SendMoveToProgram P((int moveNum, ChessProgramState *cps));
198 void ReceiveFromProgram P((InputSourceRef isr, VOIDSTAR closure,
199 char *buf, int count, int error));
200 void SendTimeControl P((ChessProgramState *cps,
201 int mps, long tc, int inc, int sd, int st));
202 char *TimeControlTagValue P((void));
203 void Attention P((ChessProgramState *cps));
204 void FeedMovesToProgram P((ChessProgramState *cps, int upto));
205 int ResurrectChessProgram P((void));
206 void DisplayComment P((int moveNumber, char *text));
207 void DisplayMove P((int moveNumber));
209 void ParseGameHistory P((char *game));
210 void ParseBoard12 P((char *string));
211 void KeepAlive P((void));
212 void StartClocks P((void));
213 void SwitchClocks P((int nr));
214 void StopClocks P((void));
215 void ResetClocks P((void));
216 char *PGNDate P((void));
217 void SetGameInfo P((void));
218 int RegisterMove P((void));
219 void MakeRegisteredMove P((void));
220 void TruncateGame P((void));
221 int looking_at P((char *, int *, char *));
222 void CopyPlayerNameIntoFileName P((char **, char *));
223 char *SavePart P((char *));
224 int SaveGameOldStyle P((FILE *));
225 int SaveGamePGN P((FILE *));
226 int CheckFlags P((void));
227 long NextTickLength P((long));
228 void CheckTimeControl P((void));
229 void show_bytes P((FILE *, char *, int));
230 int string_to_rating P((char *str));
231 void ParseFeatures P((char* args, ChessProgramState *cps));
232 void InitBackEnd3 P((void));
233 void FeatureDone P((ChessProgramState* cps, int val));
234 void InitChessProgram P((ChessProgramState *cps, int setup));
235 void OutputKibitz(int window, char *text);
236 int PerpetualChase(int first, int last);
237 int EngineOutputIsUp();
238 void InitDrawingSizes(int x, int y);
239 void NextMatchGame P((void));
240 int NextTourneyGame P((int nr, int *swap));
241 int Pairing P((int nr, int nPlayers, int *w, int *b, int *sync));
242 FILE *WriteTourneyFile P((char *results, FILE *f));
243 void DisplayTwoMachinesTitle P(());
244 static void ExcludeClick P((int index));
245 void ToggleSecond P((void));
246 void PauseEngine P((ChessProgramState *cps));
247 static int NonStandardBoardSize P((VariantClass v, int w, int h, int s));
250 extern void ConsoleCreate();
253 ChessProgramState *WhitePlayer();
254 int VerifyDisplayMode P(());
256 char *GetInfoFromComment( int, char * ); // [HGM] PV time: returns stripped comment
257 void InitEngineUCI( const char * iniDir, ChessProgramState * cps ); // [HGM] moved here from winboard.c
258 char *ProbeBook P((int moveNr, char *book)); // [HGM] book: returns a book move
259 char *SendMoveToBookUser P((int nr, ChessProgramState *cps, int initial)); // [HGM] book
260 void ics_update_width P((int new_width));
261 extern char installDir[MSG_SIZ];
262 VariantClass startVariant; /* [HGM] nicks: initial variant */
265 extern int tinyLayout, smallLayout;
266 ChessProgramStats programStats;
267 char lastPV[2][2*MSG_SIZ]; /* [HGM] pv: last PV in thinking output of each engine */
269 static int exiting = 0; /* [HGM] moved to top */
270 static int setboardSpoiledMachineBlack = 0 /*, errorExitFlag = 0*/;
271 int startedFromPositionFile = FALSE; Board filePosition; /* [HGM] loadPos */
272 Board partnerBoard; /* [HGM] bughouse: for peeking at partner game */
273 int partnerHighlight[2];
274 Boolean partnerBoardValid = 0;
275 char partnerStatus[MSG_SIZ];
277 Boolean originalFlip;
278 Boolean twoBoards = 0;
279 char endingGame = 0; /* [HGM] crash: flag to prevent recursion of GameEnds() */
280 int whiteNPS, blackNPS; /* [HGM] nps: for easily making clocks aware of NPS */
281 VariantClass currentlyInitializedVariant; /* [HGM] variantswitch */
282 int lastIndex = 0; /* [HGM] autoinc: last game/position used in match mode */
283 Boolean connectionAlive;/* [HGM] alive: ICS connection status from probing */
284 int opponentKibitzes;
285 int lastSavedGame; /* [HGM] save: ID of game */
286 char chatPartner[MAX_CHAT][MSG_SIZ]; /* [HGM] chat: list of chatting partners */
287 extern int chatCount;
289 char marker[BOARD_RANKS][BOARD_FILES]; /* [HGM] marks for target squares */
290 char legal[BOARD_RANKS][BOARD_FILES]; /* [HGM] legal target squares */
291 char lastMsg[MSG_SIZ];
292 char lastTalker[MSG_SIZ];
293 ChessSquare pieceSweep = EmptySquare;
294 ChessSquare promoSweep = EmptySquare, defaultPromoChoice;
295 int promoDefaultAltered;
296 int keepInfo = 0; /* [HGM] to protect PGN tags in auto-step game analysis */
297 static int initPing = -1;
298 int border; /* [HGM] width of board rim, needed to size seek graph */
299 char bestMove[MSG_SIZ];
300 int solvingTime, totalTime;
302 /* States for ics_getting_history */
304 #define H_REQUESTED 1
305 #define H_GOT_REQ_HEADER 2
306 #define H_GOT_UNREQ_HEADER 3
307 #define H_GETTING_MOVES 4
308 #define H_GOT_UNWANTED_HEADER 5
310 /* whosays values for GameEnds */
319 /* Maximum number of games in a cmail message */
320 #define CMAIL_MAX_GAMES 20
322 /* Different types of move when calling RegisterMove */
324 #define CMAIL_RESIGN 1
326 #define CMAIL_ACCEPT 3
328 /* Different types of result to remember for each game */
329 #define CMAIL_NOT_RESULT 0
330 #define CMAIL_OLD_RESULT 1
331 #define CMAIL_NEW_RESULT 2
333 /* Telnet protocol constants */
344 safeStrCpy (char *dst, const char *src, size_t count)
347 assert( dst != NULL );
348 assert( src != NULL );
351 for(i=0; i<count; i++) if((dst[i] = src[i]) == NULLCHAR) break;
352 if( i == count && dst[count-1] != NULLCHAR)
354 dst[ count-1 ] = '\0'; // make sure incomplete copy still null-terminated
355 if(appData.debugMode)
356 fprintf(debugFP, "safeStrCpy: copying %s into %s didn't work, not enough space %d\n",src,dst, (int)count);
362 /* Some compiler can't cast u64 to double
363 * This function do the job for us:
365 * We use the highest bit for cast, this only
366 * works if the highest bit is not
367 * in use (This should not happen)
369 * We used this for all compiler
372 u64ToDouble (u64 value)
375 u64 tmp = value & u64Const(0x7fffffffffffffff);
376 r = (double)(s64)tmp;
377 if (value & u64Const(0x8000000000000000))
378 r += 9.2233720368547758080e18; /* 2^63 */
382 /* Fake up flags for now, as we aren't keeping track of castling
383 availability yet. [HGM] Change of logic: the flag now only
384 indicates the type of castlings allowed by the rule of the game.
385 The actual rights themselves are maintained in the array
386 castlingRights, as part of the game history, and are not probed
392 int flags = F_ALL_CASTLE_OK;
393 if ((index % 2) == 0) flags |= F_WHITE_ON_MOVE;
394 switch (gameInfo.variant) {
396 flags &= ~F_ALL_CASTLE_OK;
397 case VariantGiveaway: // [HGM] moved this case label one down: seems Giveaway does have castling on ICC!
398 flags |= F_IGNORE_CHECK;
400 flags |= F_MANDATORY_CAPTURE; //[HGM] losers: sets flag so TestLegality rejects non-capts if capts exist
403 flags |= F_IGNORE_CHECK | F_ATOMIC_CAPTURE;
405 case VariantKriegspiel:
406 flags |= F_KRIEGSPIEL_CAPTURE;
408 case VariantCapaRandom:
409 case VariantFischeRandom:
410 flags |= F_FRC_TYPE_CASTLING; /* [HGM] enable this through flag */
411 case VariantNoCastle:
412 case VariantShatranj:
417 flags &= ~F_ALL_CASTLE_OK;
420 case VariantChuChess:
422 flags |= F_NULL_MOVE;
427 if(appData.fischerCastling) flags |= F_FRC_TYPE_CASTLING, flags &= ~F_ALL_CASTLE_OK; // [HGM] fischer
431 FILE *gameFileFP, *debugFP, *serverFP;
432 char *currentDebugFile; // [HGM] debug split: to remember name
435 [AS] Note: sometimes, the sscanf() function is used to parse the input
436 into a fixed-size buffer. Because of this, we must be prepared to
437 receive strings as long as the size of the input buffer, which is currently
438 set to 4K for Windows and 8K for the rest.
439 So, we must either allocate sufficiently large buffers here, or
440 reduce the size of the input buffer in the input reading part.
443 char cmailMove[CMAIL_MAX_GAMES][MOVE_LEN], cmailMsg[MSG_SIZ];
444 char bookOutput[MSG_SIZ*10], thinkOutput[MSG_SIZ*10], lastHint[MSG_SIZ];
445 char thinkOutput1[MSG_SIZ*10];
447 ChessProgramState first, second, pairing;
449 /* premove variables */
452 int premoveFromX = 0;
453 int premoveFromY = 0;
454 int premovePromoChar = 0;
456 Boolean alarmSounded;
457 /* end premove variables */
459 char *ics_prefix = "$";
460 enum ICS_TYPE ics_type = ICS_GENERIC;
462 int currentMove = 0, forwardMostMove = 0, backwardMostMove = 0;
463 int pauseExamForwardMostMove = 0;
464 int nCmailGames = 0, nCmailResults = 0, nCmailMovesRegistered = 0;
465 int cmailMoveRegistered[CMAIL_MAX_GAMES], cmailResult[CMAIL_MAX_GAMES];
466 int cmailMsgLoaded = FALSE, cmailMailedMove = FALSE;
467 int cmailOldMove = -1, firstMove = TRUE, flipView = FALSE;
468 int blackPlaysFirst = FALSE, startedFromSetupPosition = FALSE;
469 int searchTime = 0, pausing = FALSE, pauseExamInvalid = FALSE;
470 int whiteFlag = FALSE, blackFlag = FALSE;
471 int userOfferedDraw = FALSE;
472 int ics_user_moved = 0, ics_gamenum = -1, ics_getting_history = H_FALSE;
473 int matchMode = FALSE, hintRequested = FALSE, bookRequested = FALSE;
474 int cmailMoveType[CMAIL_MAX_GAMES];
475 long ics_clock_paused = 0;
476 ProcRef icsPR = NoProc, cmailPR = NoProc;
477 InputSourceRef telnetISR = NULL, fromUserISR = NULL, cmailISR = NULL;
478 GameMode gameMode = BeginningOfGame;
479 char moveList[MAX_MOVES][MOVE_LEN], parseList[MAX_MOVES][MOVE_LEN * 2];
480 char *commentList[MAX_MOVES], *cmailCommentList[CMAIL_MAX_GAMES];
481 ChessProgramStats_Move pvInfoList[MAX_MOVES]; /* [AS] Info about engine thinking */
482 int hiddenThinkOutputState = 0; /* [AS] */
483 int adjudicateLossThreshold = 0; /* [AS] Automatic adjudication */
484 int adjudicateLossPlies = 6;
485 char white_holding[64], black_holding[64];
486 TimeMark lastNodeCountTime;
487 long lastNodeCount=0;
488 int shiftKey, controlKey; // [HGM] set by mouse handler
490 int have_sent_ICS_logon = 0;
492 int suddenDeath, whiteStartMove, blackStartMove; /* [HGM] for implementation of 'any per time' sessions, as in first part of byoyomi TC */
493 long whiteTimeRemaining, blackTimeRemaining, timeControl, timeIncrement, lastWhite, lastBlack, activePartnerTime;
494 Boolean adjustedClock;
495 long timeControl_2; /* [AS] Allow separate time controls */
496 char *fullTimeControlString = NULL, *nextSession, *whiteTC, *blackTC, activePartner; /* [HGM] secondary TC: merge of MPS, TC and inc */
497 long timeRemaining[2][MAX_MOVES];
498 int matchGame = 0, nextGame = 0, roundNr = 0;
499 Boolean waitingForGame = FALSE, startingEngine = FALSE;
500 TimeMark programStartTime, pauseStart;
501 char ics_handle[MSG_SIZ];
502 int have_set_title = 0;
504 /* animateTraining preserves the state of appData.animate
505 * when Training mode is activated. This allows the
506 * response to be animated when appData.animate == TRUE and
507 * appData.animateDragging == TRUE.
509 Boolean animateTraining;
515 Board boards[MAX_MOVES];
516 /* [HGM] Following 7 needed for accurate legality tests: */
517 signed char castlingRank[BOARD_FILES]; // and corresponding ranks
518 signed char initialRights[BOARD_FILES];
519 int nrCastlingRights; // For TwoKings, or to implement castling-unknown status
520 int initialRulePlies, FENrulePlies;
521 FILE *serverMoves = NULL; // next two for broadcasting (/serverMoves option)
523 Boolean shuffleOpenings;
524 int mute; // mute all sounds
526 // [HGM] vari: next 12 to save and restore variations
527 #define MAX_VARIATIONS 10
528 int framePtr = MAX_MOVES-1; // points to free stack entry
530 int savedFirst[MAX_VARIATIONS];
531 int savedLast[MAX_VARIATIONS];
532 int savedFramePtr[MAX_VARIATIONS];
533 char *savedDetails[MAX_VARIATIONS];
534 ChessMove savedResult[MAX_VARIATIONS];
536 void PushTail P((int firstMove, int lastMove));
537 Boolean PopTail P((Boolean annotate));
538 void PushInner P((int firstMove, int lastMove));
539 void PopInner P((Boolean annotate));
540 void CleanupTail P((void));
542 ChessSquare FIDEArray[2][BOARD_FILES] = {
543 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
544 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
545 { BlackRook, BlackKnight, BlackBishop, BlackQueen,
546 BlackKing, BlackBishop, BlackKnight, BlackRook }
549 ChessSquare twoKingsArray[2][BOARD_FILES] = {
550 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
551 WhiteKing, WhiteKing, WhiteKnight, WhiteRook },
552 { BlackRook, BlackKnight, BlackBishop, BlackQueen,
553 BlackKing, BlackKing, BlackKnight, BlackRook }
556 ChessSquare KnightmateArray[2][BOARD_FILES] = {
557 { WhiteRook, WhiteMan, WhiteBishop, WhiteQueen,
558 WhiteUnicorn, WhiteBishop, WhiteMan, WhiteRook },
559 { BlackRook, BlackMan, BlackBishop, BlackQueen,
560 BlackUnicorn, BlackBishop, BlackMan, BlackRook }
563 ChessSquare SpartanArray[2][BOARD_FILES] = {
564 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
565 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
566 { BlackAlfil, BlackMarshall, BlackKing, BlackDragon,
567 BlackDragon, BlackKing, BlackAngel, BlackAlfil }
570 ChessSquare fairyArray[2][BOARD_FILES] = { /* [HGM] Queen side differs from King side */
571 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
572 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
573 { BlackCardinal, BlackAlfil, BlackMarshall, BlackAngel,
574 BlackKing, BlackMarshall, BlackAlfil, BlackCardinal }
577 ChessSquare ShatranjArray[2][BOARD_FILES] = { /* [HGM] (movGen knows about Shatranj Q and P) */
578 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteKing,
579 WhiteFerz, WhiteAlfil, WhiteKnight, WhiteRook },
580 { BlackRook, BlackKnight, BlackAlfil, BlackKing,
581 BlackFerz, BlackAlfil, BlackKnight, BlackRook }
584 ChessSquare makrukArray[2][BOARD_FILES] = { /* [HGM] (movGen knows about Shatranj Q and P) */
585 { WhiteRook, WhiteKnight, WhiteMan, WhiteKing,
586 WhiteFerz, WhiteMan, WhiteKnight, WhiteRook },
587 { BlackRook, BlackKnight, BlackMan, BlackFerz,
588 BlackKing, BlackMan, BlackKnight, BlackRook }
591 ChessSquare aseanArray[2][BOARD_FILES] = { /* [HGM] (movGen knows about Shatranj Q and P) */
592 { WhiteRook, WhiteKnight, WhiteMan, WhiteFerz,
593 WhiteKing, WhiteMan, WhiteKnight, WhiteRook },
594 { BlackRook, BlackKnight, BlackMan, BlackFerz,
595 BlackKing, BlackMan, BlackKnight, BlackRook }
598 ChessSquare lionArray[2][BOARD_FILES] = {
599 { WhiteRook, WhiteLion, WhiteBishop, WhiteQueen,
600 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
601 { BlackRook, BlackLion, BlackBishop, BlackQueen,
602 BlackKing, BlackBishop, BlackKnight, BlackRook }
606 #if (BOARD_FILES>=10)
607 ChessSquare ShogiArray[2][BOARD_FILES] = {
608 { WhiteQueen, WhiteKnight, WhiteFerz, WhiteWazir,
609 WhiteKing, WhiteWazir, WhiteFerz, WhiteKnight, WhiteQueen },
610 { BlackQueen, BlackKnight, BlackFerz, BlackWazir,
611 BlackKing, BlackWazir, BlackFerz, BlackKnight, BlackQueen }
614 ChessSquare XiangqiArray[2][BOARD_FILES] = {
615 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteFerz,
616 WhiteWazir, WhiteFerz, WhiteAlfil, WhiteKnight, WhiteRook },
617 { BlackRook, BlackKnight, BlackAlfil, BlackFerz,
618 BlackWazir, BlackFerz, BlackAlfil, BlackKnight, BlackRook }
621 ChessSquare CapablancaArray[2][BOARD_FILES] = {
622 { WhiteRook, WhiteKnight, WhiteAngel, WhiteBishop, WhiteQueen,
623 WhiteKing, WhiteBishop, WhiteMarshall, WhiteKnight, WhiteRook },
624 { BlackRook, BlackKnight, BlackAngel, BlackBishop, BlackQueen,
625 BlackKing, BlackBishop, BlackMarshall, BlackKnight, BlackRook }
628 ChessSquare GreatArray[2][BOARD_FILES] = {
629 { WhiteDragon, WhiteKnight, WhiteAlfil, WhiteGrasshopper, WhiteKing,
630 WhiteSilver, WhiteCardinal, WhiteAlfil, WhiteKnight, WhiteDragon },
631 { BlackDragon, BlackKnight, BlackAlfil, BlackGrasshopper, BlackKing,
632 BlackSilver, BlackCardinal, BlackAlfil, BlackKnight, BlackDragon },
635 ChessSquare JanusArray[2][BOARD_FILES] = {
636 { WhiteRook, WhiteAngel, WhiteKnight, WhiteBishop, WhiteKing,
637 WhiteQueen, WhiteBishop, WhiteKnight, WhiteAngel, WhiteRook },
638 { BlackRook, BlackAngel, BlackKnight, BlackBishop, BlackKing,
639 BlackQueen, BlackBishop, BlackKnight, BlackAngel, BlackRook }
642 ChessSquare GrandArray[2][BOARD_FILES] = {
643 { EmptySquare, WhiteKnight, WhiteBishop, WhiteQueen, WhiteKing,
644 WhiteMarshall, WhiteAngel, WhiteBishop, WhiteKnight, EmptySquare },
645 { EmptySquare, BlackKnight, BlackBishop, BlackQueen, BlackKing,
646 BlackMarshall, BlackAngel, BlackBishop, BlackKnight, EmptySquare }
649 ChessSquare ChuChessArray[2][BOARD_FILES] = {
650 { WhiteMan, WhiteKnight, WhiteBishop, WhiteCardinal, WhiteLion,
651 WhiteQueen, WhiteDragon, WhiteBishop, WhiteKnight, WhiteMan },
652 { BlackMan, BlackKnight, BlackBishop, BlackDragon, BlackQueen,
653 BlackLion, BlackCardinal, BlackBishop, BlackKnight, BlackMan }
657 ChessSquare GothicArray[2][BOARD_FILES] = {
658 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen, WhiteMarshall,
659 WhiteKing, WhiteAngel, WhiteBishop, WhiteKnight, WhiteRook },
660 { BlackRook, BlackKnight, BlackBishop, BlackQueen, BlackMarshall,
661 BlackKing, BlackAngel, BlackBishop, BlackKnight, BlackRook }
664 #define GothicArray CapablancaArray
668 ChessSquare FalconArray[2][BOARD_FILES] = {
669 { WhiteRook, WhiteKnight, WhiteBishop, WhiteFalcon, WhiteQueen,
670 WhiteKing, WhiteFalcon, WhiteBishop, WhiteKnight, WhiteRook },
671 { BlackRook, BlackKnight, BlackBishop, BlackFalcon, BlackQueen,
672 BlackKing, BlackFalcon, BlackBishop, BlackKnight, BlackRook }
675 #define FalconArray CapablancaArray
678 #else // !(BOARD_FILES>=10)
679 #define XiangqiPosition FIDEArray
680 #define CapablancaArray FIDEArray
681 #define GothicArray FIDEArray
682 #define GreatArray FIDEArray
683 #endif // !(BOARD_FILES>=10)
685 #if (BOARD_FILES>=12)
686 ChessSquare CourierArray[2][BOARD_FILES] = {
687 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteBishop, WhiteMan, WhiteKing,
688 WhiteFerz, WhiteWazir, WhiteBishop, WhiteAlfil, WhiteKnight, WhiteRook },
689 { BlackRook, BlackKnight, BlackAlfil, BlackBishop, BlackMan, BlackKing,
690 BlackFerz, BlackWazir, BlackBishop, BlackAlfil, BlackKnight, BlackRook }
692 ChessSquare ChuArray[6][BOARD_FILES] = {
693 { WhiteLance, WhiteUnicorn, WhiteMan, WhiteFerz, WhiteWazir, WhiteKing,
694 WhiteAlfil, WhiteWazir, WhiteFerz, WhiteMan, WhiteUnicorn, WhiteLance },
695 { BlackLance, BlackUnicorn, BlackMan, BlackFerz, BlackWazir, BlackAlfil,
696 BlackKing, BlackWazir, BlackFerz, BlackMan, BlackUnicorn, BlackLance },
697 { WhiteCannon, EmptySquare, WhiteBishop, EmptySquare, WhiteNightrider, WhiteMarshall,
698 WhiteAngel, WhiteNightrider, EmptySquare, WhiteBishop, EmptySquare, WhiteCannon },
699 { BlackCannon, EmptySquare, BlackBishop, EmptySquare, BlackNightrider, BlackAngel,
700 BlackMarshall, BlackNightrider, EmptySquare, BlackBishop, EmptySquare, BlackCannon },
701 { WhiteFalcon, WhiteSilver, WhiteRook, WhiteCardinal, WhiteDragon, WhiteLion,
702 WhiteQueen, WhiteDragon, WhiteCardinal, WhiteRook, WhiteSilver, WhiteFalcon },
703 { BlackFalcon, BlackSilver, BlackRook, BlackCardinal, BlackDragon, BlackQueen,
704 BlackLion, BlackDragon, BlackCardinal, BlackRook, BlackSilver, BlackFalcon }
706 #else // !(BOARD_FILES>=12)
707 #define CourierArray CapablancaArray
708 #define ChuArray CapablancaArray
709 #endif // !(BOARD_FILES>=12)
712 Board initialPosition;
715 /* Convert str to a rating. Checks for special cases of "----",
717 "++++", etc. Also strips ()'s */
719 string_to_rating (char *str)
721 while(*str && !isdigit(*str)) ++str;
723 return 0; /* One of the special "no rating" cases */
731 /* Init programStats */
732 programStats.movelist[0] = 0;
733 programStats.depth = 0;
734 programStats.nr_moves = 0;
735 programStats.moves_left = 0;
736 programStats.nodes = 0;
737 programStats.time = -1; // [HGM] PGNtime: make invalid to recognize engine output
738 programStats.score = 0;
739 programStats.got_only_move = 0;
740 programStats.got_fail = 0;
741 programStats.line_is_book = 0;
746 { // [HGM] moved some code here from InitBackend1 that has to be done after both engines have contributed their settings
747 if (appData.firstPlaysBlack) {
748 first.twoMachinesColor = "black\n";
749 second.twoMachinesColor = "white\n";
751 first.twoMachinesColor = "white\n";
752 second.twoMachinesColor = "black\n";
755 first.other = &second;
756 second.other = &first;
759 if(appData.timeOddsMode) {
760 norm = appData.timeOdds[0];
761 if(norm > appData.timeOdds[1]) norm = appData.timeOdds[1];
763 first.timeOdds = appData.timeOdds[0]/norm;
764 second.timeOdds = appData.timeOdds[1]/norm;
767 if(programVersion) free(programVersion);
768 if (appData.noChessProgram) {
769 programVersion = (char*) malloc(5 + strlen(PACKAGE_STRING));
770 sprintf(programVersion, "%s", PACKAGE_STRING);
772 /* [HGM] tidy: use tidy name, in stead of full pathname (which was probably a bug due to / vs \ ) */
773 programVersion = (char*) malloc(8 + strlen(PACKAGE_STRING) + strlen(first.tidy));
774 sprintf(programVersion, "%s + %s", PACKAGE_STRING, first.tidy);
779 UnloadEngine (ChessProgramState *cps)
781 /* Kill off first chess program */
782 if (cps->isr != NULL)
783 RemoveInputSource(cps->isr);
786 if (cps->pr != NoProc) {
788 DoSleep( appData.delayBeforeQuit );
789 SendToProgram("quit\n", cps);
790 DestroyChildProcess(cps->pr, 4 + cps->useSigterm);
793 if(appData.debugMode) fprintf(debugFP, "Unload %s\n", cps->which);
797 ClearOptions (ChessProgramState *cps)
800 cps->nrOptions = cps->comboCnt = 0;
801 for(i=0; i<MAX_OPTIONS; i++) {
802 cps->option[i].min = cps->option[i].max = cps->option[i].value = 0;
803 cps->option[i].textValue = 0;
807 char *engineNames[] = {
808 /* TRANSLATORS: "first" is the first of possible two chess engines. It is inserted into strings
809 such as "%s engine" / "%s chess program" / "%s machine" - all meaning the same thing */
811 /* TRANSLATORS: "second" is the second of possible two chess engines. It is inserted into strings
812 such as "%s engine" / "%s chess program" / "%s machine" - all meaning the same thing */
817 InitEngine (ChessProgramState *cps, int n)
818 { // [HGM] all engine initialiation put in a function that does one engine
822 cps->which = engineNames[n];
823 cps->maybeThinking = FALSE;
827 cps->sendDrawOffers = 1;
829 cps->program = appData.chessProgram[n];
830 cps->host = appData.host[n];
831 cps->dir = appData.directory[n];
832 cps->initString = appData.engInitString[n];
833 cps->computerString = appData.computerString[n];
834 cps->useSigint = TRUE;
835 cps->useSigterm = TRUE;
836 cps->reuse = appData.reuse[n];
837 cps->nps = appData.NPS[n]; // [HGM] nps: copy nodes per second
838 cps->useSetboard = FALSE;
840 cps->usePing = FALSE;
843 cps->usePlayother = FALSE;
844 cps->useColors = TRUE;
845 cps->useUsermove = FALSE;
846 cps->sendICS = FALSE;
847 cps->sendName = appData.icsActive;
848 cps->sdKludge = FALSE;
849 cps->stKludge = FALSE;
850 if(cps->tidy == NULL) cps->tidy = (char*) malloc(MSG_SIZ);
851 TidyProgramName(cps->program, cps->host, cps->tidy);
853 ASSIGN(cps->variants, appData.noChessProgram ? "" : appData.variant);
854 cps->analysisSupport = 2; /* detect */
855 cps->analyzing = FALSE;
856 cps->initDone = FALSE;
858 cps->pseudo = appData.pseudo[n];
860 /* New features added by Tord: */
861 cps->useFEN960 = FALSE;
862 cps->useOOCastle = TRUE;
863 /* End of new features added by Tord. */
864 cps->fenOverride = appData.fenOverride[n];
866 /* [HGM] time odds: set factor for each machine */
867 cps->timeOdds = appData.timeOdds[n];
869 /* [HGM] secondary TC: how to handle sessions that do not fit in 'level'*/
870 cps->accumulateTC = appData.accumulateTC[n];
871 cps->maxNrOfSessions = 1;
876 cps->drawDepth = appData.drawDepth[n];
877 cps->supportsNPS = UNKNOWN;
878 cps->memSize = FALSE;
879 cps->maxCores = FALSE;
880 ASSIGN(cps->egtFormats, "");
883 cps->optionSettings = appData.engOptions[n];
885 cps->scoreIsAbsolute = appData.scoreIsAbsolute[n]; /* [AS] */
886 cps->isUCI = appData.isUCI[n]; /* [AS] */
887 cps->hasOwnBookUCI = appData.hasOwnBookUCI[n]; /* [AS] */
890 if (appData.protocolVersion[n] > PROTOVER
891 || appData.protocolVersion[n] < 1)
896 len = snprintf(buf, MSG_SIZ, _("protocol version %d not supported"),
897 appData.protocolVersion[n]);
898 if( (len >= MSG_SIZ) && appData.debugMode )
899 fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
901 DisplayFatalError(buf, 0, 2);
905 cps->protocolVersion = appData.protocolVersion[n];
908 InitEngineUCI( installDir, cps ); // [HGM] moved here from winboard.c, to make available in xboard
909 ParseFeatures(appData.featureDefaults, cps);
912 ChessProgramState *savCps;
920 if(WaitForEngine(savCps, LoadEngine)) return;
921 CommonEngineInit(); // recalculate time odds
922 if(gameInfo.variant != StringToVariant(appData.variant)) {
923 // we changed variant when loading the engine; this forces us to reset
924 Reset(TRUE, savCps != &first);
925 oldMode = BeginningOfGame; // to prevent restoring old mode
927 InitChessProgram(savCps, FALSE);
928 if(gameMode == EditGame) SendToProgram("force\n", savCps); // in EditGame mode engine must be in force mode
929 DisplayMessage("", "");
930 if (startedFromSetupPosition) SendBoard(savCps, backwardMostMove);
931 for (i = backwardMostMove; i < currentMove; i++) SendMoveToProgram(i, savCps);
934 if(oldMode == AnalyzeMode) AnalyzeModeEvent();
938 ReplaceEngine (ChessProgramState *cps, int n)
940 oldMode = gameMode; // remember mode, so it can be restored after loading sequence is complete
942 if(oldMode != BeginningOfGame) EditGameEvent();
945 appData.noChessProgram = FALSE;
946 appData.clockMode = TRUE;
949 if(n) return; // only startup first engine immediately; second can wait
950 savCps = cps; // parameter to LoadEngine passed as globals, to allow scheduled calling :-(
954 extern char *engineName, *engineDir, *engineChoice, *engineLine, *nickName, *params;
955 extern Boolean isUCI, hasBook, storeVariant, v1, addToList, useNick;
957 static char resetOptions[] =
958 "-reuse -firstIsUCI false -firstHasOwnBookUCI true -firstTimeOdds 1 "
959 "-firstInitString \"" INIT_STRING "\" -firstComputerString \"" COMPUTER_STRING "\" "
960 "-firstFeatures \"\" -firstLogo \"\" -firstAccumulateTC 1 -fd \".\" "
961 "-firstOptions \"\" -firstNPS -1 -fn \"\" -firstScoreAbs false";
964 FloatToFront(char **list, char *engineLine)
966 char buf[MSG_SIZ], tidy[MSG_SIZ], *p = buf, *q, *r = buf;
968 if(appData.recentEngines <= 0) return;
969 TidyProgramName(engineLine, "localhost", tidy+1);
970 tidy[0] = buf[0] = '\n'; strcat(tidy, "\n");
971 strncpy(buf+1, *list, MSG_SIZ-50);
972 if(p = strstr(buf, tidy)) { // tidy name appears in list
973 q = strchr(++p, '\n'); if(q == NULL) return; // malformed, don't touch
974 while(*p++ = *++q); // squeeze out
976 strcat(tidy, buf+1); // put list behind tidy name
977 p = tidy + 1; while(q = strchr(p, '\n')) i++, r = p, p = q + 1; // count entries in new list
978 if(i > appData.recentEngines) *r = NULLCHAR; // if maximum rached, strip off last
979 ASSIGN(*list, tidy+1);
982 char *insert, *wbOptions; // point in ChessProgramNames were we should insert new engine
985 Load (ChessProgramState *cps, int i)
987 char *p, *q, buf[MSG_SIZ], command[MSG_SIZ], buf2[MSG_SIZ], buf3[MSG_SIZ], jar;
988 if(engineLine && engineLine[0]) { // an engine was selected from the combo box
989 snprintf(buf, MSG_SIZ, "-fcp %s", engineLine);
990 SwapEngines(i); // kludge to parse -f* / -first* like it is -s* / -second*
991 ParseArgsFromString(resetOptions); appData.pvSAN[0] = FALSE;
992 FREE(appData.fenOverride[0]); appData.fenOverride[0] = NULL;
993 appData.firstProtocolVersion = PROTOVER;
994 ParseArgsFromString(buf);
996 ReplaceEngine(cps, i);
997 FloatToFront(&appData.recentEngineList, engineLine);
1001 while(q = strchr(p, SLASH)) p = q+1;
1002 if(*p== NULLCHAR) { DisplayError(_("You did not specify the engine executable"), 0); return; }
1003 if(engineDir[0] != NULLCHAR) {
1004 ASSIGN(appData.directory[i], engineDir); p = engineName;
1005 } else if(p != engineName) { // derive directory from engine path, when not given
1007 ASSIGN(appData.directory[i], engineName);
1009 if(SLASH == '/' && p - engineName > 1) *(p -= 2) = '.'; // for XBoard use ./exeName as command after split!
1010 } else { ASSIGN(appData.directory[i], "."); }
1011 jar = (strstr(p, ".jar") == p + strlen(p) - 4);
1013 if(strchr(p, ' ') && !strchr(p, '"')) snprintf(buf2, MSG_SIZ, "\"%s\"", p), p = buf2; // quote if it contains spaces
1014 snprintf(command, MSG_SIZ, "%s %s", p, params);
1017 if(jar) { snprintf(buf3, MSG_SIZ, "java -jar %s", p); p = buf3; }
1018 ASSIGN(appData.chessProgram[i], p);
1019 appData.isUCI[i] = isUCI;
1020 appData.protocolVersion[i] = v1 ? 1 : PROTOVER;
1021 appData.hasOwnBookUCI[i] = hasBook;
1022 if(!nickName[0]) useNick = FALSE;
1023 if(useNick) ASSIGN(appData.pgnName[i], nickName);
1027 q = firstChessProgramNames;
1028 if(nickName[0]) snprintf(buf, MSG_SIZ, "\"%s\" -fcp ", nickName); else buf[0] = NULLCHAR;
1029 quote = strchr(p, '"') ? '\'' : '"'; // use single quotes around engine command if it contains double quotes
1030 snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), "%c%s%c -fd \"%s\"%s%s%s%s%s%s%s%s\n",
1031 quote, p, quote, appData.directory[i],
1032 useNick ? " -fn \"" : "",
1033 useNick ? nickName : "",
1034 useNick ? "\"" : "",
1035 v1 ? " -firstProtocolVersion 1" : "",
1036 hasBook ? "" : " -fNoOwnBookUCI",
1037 isUCI ? (isUCI == TRUE ? " -fUCI" : gameInfo.variant == VariantShogi ? " -fUSI" : " -fUCCI") : "",
1038 storeVariant ? " -variant " : "",
1039 storeVariant ? VariantName(gameInfo.variant) : "");
1040 if(wbOptions && wbOptions[0]) snprintf(buf+strlen(buf)-1, MSG_SIZ-strlen(buf), " %s\n", wbOptions);
1041 firstChessProgramNames = malloc(len = strlen(q) + strlen(buf) + 1);
1042 if(insert != q) insert[-1] = NULLCHAR;
1043 snprintf(firstChessProgramNames, len, "%s\n%s%s", q, buf, insert);
1045 FloatToFront(&appData.recentEngineList, buf);
1047 ReplaceEngine(cps, i);
1053 int matched, min, sec;
1055 * Parse timeControl resource
1057 if (!ParseTimeControl(appData.timeControl, appData.timeIncrement,
1058 appData.movesPerSession)) {
1060 snprintf(buf, sizeof(buf), _("bad timeControl option %s"), appData.timeControl);
1061 DisplayFatalError(buf, 0, 2);
1065 * Parse searchTime resource
1067 if (*appData.searchTime != NULLCHAR) {
1068 matched = sscanf(appData.searchTime, "%d:%d", &min, &sec);
1070 searchTime = min * 60;
1071 } else if (matched == 2) {
1072 searchTime = min * 60 + sec;
1075 snprintf(buf, sizeof(buf), _("bad searchTime option %s"), appData.searchTime);
1076 DisplayFatalError(buf, 0, 2);
1085 ShowThinkingEvent(); // [HGM] thinking: make sure post/nopost state is set according to options
1086 startVariant = StringToVariant(appData.variant); // [HGM] nicks: remember original variant
1088 GetTimeMark(&programStartTime);
1089 srandom((programStartTime.ms + 1000*programStartTime.sec)*0x1001001); // [HGM] book: makes sure random is unpredictabe to msec level
1090 appData.seedBase = random() + (random()<<15);
1091 pauseStart = programStartTime; pauseStart.sec -= 100; // [HGM] matchpause: fake a pause that has long since ended
1093 ClearProgramStats();
1094 programStats.ok_to_send = 1;
1095 programStats.seen_stat = 0;
1098 * Initialize game list
1104 * Internet chess server status
1106 if (appData.icsActive) {
1107 appData.matchMode = FALSE;
1108 appData.matchGames = 0;
1110 appData.noChessProgram = !appData.zippyPlay;
1112 appData.zippyPlay = FALSE;
1113 appData.zippyTalk = FALSE;
1114 appData.noChessProgram = TRUE;
1116 if (*appData.icsHelper != NULLCHAR) {
1117 appData.useTelnet = TRUE;
1118 appData.telnetProgram = appData.icsHelper;
1121 appData.zippyTalk = appData.zippyPlay = FALSE;
1124 /* [AS] Initialize pv info list [HGM] and game state */
1128 for( i=0; i<=framePtr; i++ ) {
1129 pvInfoList[i].depth = -1;
1130 boards[i][EP_STATUS] = EP_NONE;
1131 for( j=0; j<BOARD_FILES-2; j++ ) boards[i][CASTLING][j] = NoRights;
1137 /* [AS] Adjudication threshold */
1138 adjudicateLossThreshold = appData.adjudicateLossThreshold;
1140 InitEngine(&first, 0);
1141 InitEngine(&second, 1);
1144 pairing.which = "pairing"; // pairing engine
1145 pairing.pr = NoProc;
1147 pairing.program = appData.pairingEngine;
1148 pairing.host = "localhost";
1151 if (appData.icsActive) {
1152 appData.clockMode = TRUE; /* changes dynamically in ICS mode */
1153 } else if (appData.noChessProgram) { // [HGM] st: searchTime mode now also is clockMode
1154 appData.clockMode = FALSE;
1155 first.sendTime = second.sendTime = 0;
1159 /* Override some settings from environment variables, for backward
1160 compatibility. Unfortunately it's not feasible to have the env
1161 vars just set defaults, at least in xboard. Ugh.
1163 if (appData.icsActive && (appData.zippyPlay || appData.zippyTalk)) {
1168 if (!appData.icsActive) {
1172 /* Check for variants that are supported only in ICS mode,
1173 or not at all. Some that are accepted here nevertheless
1174 have bugs; see comments below.
1176 VariantClass variant = StringToVariant(appData.variant);
1178 case VariantBughouse: /* need four players and two boards */
1179 case VariantKriegspiel: /* need to hide pieces and move details */
1180 /* case VariantFischeRandom: (Fabien: moved below) */
1181 len = snprintf(buf,MSG_SIZ, _("Variant %s supported only in ICS mode"), appData.variant);
1182 if( (len >= MSG_SIZ) && appData.debugMode )
1183 fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
1185 DisplayFatalError(buf, 0, 2);
1188 case VariantUnknown:
1189 case VariantLoadable:
1199 len = snprintf(buf, MSG_SIZ, _("Unknown variant name %s"), appData.variant);
1200 if( (len >= MSG_SIZ) && appData.debugMode )
1201 fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
1203 DisplayFatalError(buf, 0, 2);
1206 case VariantNormal: /* definitely works! */
1207 if(strcmp(appData.variant, "normal") && !appData.noChessProgram) { // [HGM] hope this is an engine-defined variant
1208 safeStrCpy(engineVariant, appData.variant, MSG_SIZ);
1211 case VariantXiangqi: /* [HGM] repetition rules not implemented */
1212 case VariantFairy: /* [HGM] TestLegality definitely off! */
1213 case VariantGothic: /* [HGM] should work */
1214 case VariantCapablanca: /* [HGM] should work */
1215 case VariantCourier: /* [HGM] initial forced moves not implemented */
1216 case VariantShogi: /* [HGM] could still mate with pawn drop */
1217 case VariantChu: /* [HGM] experimental */
1218 case VariantKnightmate: /* [HGM] should work */
1219 case VariantCylinder: /* [HGM] untested */
1220 case VariantFalcon: /* [HGM] untested */
1221 case VariantCrazyhouse: /* holdings not shown, ([HGM] fixed that!)
1222 offboard interposition not understood */
1223 case VariantWildCastle: /* pieces not automatically shuffled */
1224 case VariantNoCastle: /* pieces not automatically shuffled */
1225 case VariantFischeRandom: /* [HGM] works and shuffles pieces */
1226 case VariantLosers: /* should work except for win condition,
1227 and doesn't know captures are mandatory */
1228 case VariantSuicide: /* should work except for win condition,
1229 and doesn't know captures are mandatory */
1230 case VariantGiveaway: /* should work except for win condition,
1231 and doesn't know captures are mandatory */
1232 case VariantTwoKings: /* should work */
1233 case VariantAtomic: /* should work except for win condition */
1234 case Variant3Check: /* should work except for win condition */
1235 case VariantShatranj: /* should work except for all win conditions */
1236 case VariantMakruk: /* should work except for draw countdown */
1237 case VariantASEAN : /* should work except for draw countdown */
1238 case VariantBerolina: /* might work if TestLegality is off */
1239 case VariantCapaRandom: /* should work */
1240 case VariantJanus: /* should work */
1241 case VariantSuper: /* experimental */
1242 case VariantGreat: /* experimental, requires legality testing to be off */
1243 case VariantSChess: /* S-Chess, should work */
1244 case VariantGrand: /* should work */
1245 case VariantSpartan: /* should work */
1246 case VariantLion: /* should work */
1247 case VariantChuChess: /* should work */
1255 NextIntegerFromString (char ** str, long * value)
1260 while( *s == ' ' || *s == '\t' ) {
1266 if( *s >= '0' && *s <= '9' ) {
1267 while( *s >= '0' && *s <= '9' ) {
1268 *value = *value * 10 + (*s - '0');
1281 NextTimeControlFromString (char ** str, long * value)
1284 int result = NextIntegerFromString( str, &temp );
1287 *value = temp * 60; /* Minutes */
1288 if( **str == ':' ) {
1290 result = NextIntegerFromString( str, &temp );
1291 *value += temp; /* Seconds */
1299 NextSessionFromString (char ** str, int *moves, long * tc, long *inc, int *incType)
1300 { /* [HGM] routine added to read '+moves/time' for secondary time control. */
1301 int result = -1, type = 0; long temp, temp2;
1303 if(**str != ':') return -1; // old params remain in force!
1305 if(**str == '*') type = *(*str)++, temp = 0; // sandclock TC
1306 if( NextIntegerFromString( str, &temp ) ) return -1;
1307 if(type) { *moves = 0; *tc = temp * 500; *inc = temp * 1000; *incType = '*'; return 0; }
1310 /* time only: incremental or sudden-death time control */
1311 if(**str == '+') { /* increment follows; read it */
1313 if(**str == '!') type = *(*str)++; // Bronstein TC
1314 if(result = NextIntegerFromString( str, &temp2)) return -1;
1315 *inc = temp2 * 1000;
1316 if(**str == '.') { // read fraction of increment
1317 char *start = ++(*str);
1318 if(result = NextIntegerFromString( str, &temp2)) return -1;
1320 while(start++ < *str) temp2 /= 10;
1324 *moves = 0; *tc = temp * 1000; *incType = type;
1328 (*str)++; /* classical time control */
1329 result = NextIntegerFromString( str, &temp2); // NOTE: already converted to seconds by ParseTimeControl()
1341 GetTimeQuota (int movenr, int lastUsed, char *tcString)
1342 { /* [HGM] get time to add from the multi-session time-control string */
1343 int incType, moves=1; /* kludge to force reading of first session */
1344 long time, increment;
1347 if(!s || !*s) return 0; // empty TC string means we ran out of the last sudden-death version
1349 if(moves) NextSessionFromString(&s, &moves, &time, &increment, &incType);
1350 nextSession = s; suddenDeath = moves == 0 && increment == 0;
1351 if(movenr == -1) return time; /* last move before new session */
1352 if(incType == '*') increment = 0; else // for sandclock, time is added while not thinking
1353 if(incType == '!' && lastUsed < increment) increment = lastUsed;
1354 if(!moves) return increment; /* current session is incremental */
1355 if(movenr >= 0) movenr -= moves; /* we already finished this session */
1356 } while(movenr >= -1); /* try again for next session */
1358 return 0; // no new time quota on this move
1362 ParseTimeControl (char *tc, float ti, int mps)
1366 char buf[MSG_SIZ], buf2[MSG_SIZ], *mytc = tc;
1369 if(ti >= 0 && !strchr(tc, '+') && !strchr(tc, '/') ) mps = 0;
1370 if(!strchr(tc, '+') && !strchr(tc, '/') && sscanf(tc, "%d:%d", &min, &sec) >= 1)
1371 sprintf(mytc=buf2, "%d", 60*min+sec); // convert 'classical' min:sec tc string to seconds
1375 snprintf(buf, MSG_SIZ, ":%d/%s+%g", mps, mytc, ti);
1377 snprintf(buf, MSG_SIZ, ":%s+%g", mytc, ti);
1380 snprintf(buf, MSG_SIZ, ":%d/%s", mps, mytc);
1382 snprintf(buf, MSG_SIZ, ":%s", mytc);
1384 fullTimeControlString = StrSave(buf); // this should now be in PGN format
1386 if( NextTimeControlFromString( &tc, &tc1 ) != 0 ) {
1391 /* Parse second time control */
1394 if( NextTimeControlFromString( &tc, &tc2 ) != 0 ) {
1402 timeControl_2 = tc2 * 1000;
1412 timeControl = tc1 * 1000;
1415 timeIncrement = ti * 1000; /* convert to ms */
1416 movesPerSession = 0;
1419 movesPerSession = mps;
1427 if (appData.debugMode) {
1428 # ifdef __GIT_VERSION
1429 fprintf(debugFP, "Version: %s (%s)\n", programVersion, __GIT_VERSION);
1431 fprintf(debugFP, "Version: %s\n", programVersion);
1434 ASSIGN(currentDebugFile, appData.nameOfDebugFile); // [HGM] debug split: remember initial name in use
1436 set_cont_sequence(appData.wrapContSeq);
1437 if (appData.matchGames > 0) {
1438 appData.matchMode = TRUE;
1439 } else if (appData.matchMode) {
1440 appData.matchGames = 1;
1442 if(appData.matchMode && appData.sameColorGames > 0) /* [HGM] alternate: overrule matchGames */
1443 appData.matchGames = appData.sameColorGames;
1444 if(appData.rewindIndex > 1) { /* [HGM] autoinc: rewind implies auto-increment and overrules given index */
1445 if(appData.loadPositionIndex >= 0) appData.loadPositionIndex = -1;
1446 if(appData.loadGameIndex >= 0) appData.loadGameIndex = -1;
1449 if (appData.noChessProgram || first.protocolVersion == 1) {
1452 /* kludge: allow timeout for initial "feature" commands */
1454 DisplayMessage("", _("Starting chess program"));
1455 ScheduleDelayedEvent(InitBackEnd3, FEATURE_TIMEOUT);
1460 CalculateIndex (int index, int gameNr)
1461 { // [HGM] autoinc: absolute way to determine load index from game number (taking auto-inc and rewind into account)
1463 if(index > 0) return index; // fixed nmber
1464 if(index == 0) return 1;
1465 res = (index == -1 ? gameNr : (gameNr-1)/2 + 1); // autoinc
1466 if(appData.rewindIndex > 0) res = (res-1) % appData.rewindIndex + 1; // rewind
1471 LoadGameOrPosition (int gameNr)
1472 { // [HGM] taken out of MatchEvent and NextMatchGame (to combine it)
1473 if (*appData.loadGameFile != NULLCHAR) {
1474 if (!LoadGameFromFile(appData.loadGameFile,
1475 CalculateIndex(appData.loadGameIndex, gameNr),
1476 appData.loadGameFile, FALSE)) {
1477 DisplayFatalError(_("Bad game file"), 0, 1);
1480 } else if (*appData.loadPositionFile != NULLCHAR) {
1481 if (!LoadPositionFromFile(appData.loadPositionFile,
1482 CalculateIndex(appData.loadPositionIndex, gameNr),
1483 appData.loadPositionFile)) {
1484 DisplayFatalError(_("Bad position file"), 0, 1);
1492 ReserveGame (int gameNr, char resChar)
1494 FILE *tf = fopen(appData.tourneyFile, "r+");
1495 char *p, *q, c, buf[MSG_SIZ];
1496 if(tf == NULL) { nextGame = appData.matchGames + 1; return; } // kludge to terminate match
1497 safeStrCpy(buf, lastMsg, MSG_SIZ);
1498 DisplayMessage(_("Pick new game"), "");
1499 flock(fileno(tf), LOCK_EX); // lock the tourney file while we are messing with it
1500 ParseArgsFromFile(tf);
1501 p = q = appData.results;
1502 if(appData.debugMode) {
1503 char *r = appData.participants;
1504 fprintf(debugFP, "results = '%s'\n", p);
1505 while(*r) fprintf(debugFP, *r >= ' ' ? "%c" : "\\%03o", *r), r++;
1506 fprintf(debugFP, "\n");
1508 while(*q && *q != ' ') q++; // get first un-played game (could be beyond end!)
1510 q = malloc(strlen(p) + 2); // could be arbitrary long, but allow to extend by one!
1511 safeStrCpy(q, p, strlen(p) + 2);
1512 if(gameNr >= 0) q[gameNr] = resChar; // replace '*' with result
1513 if(appData.debugMode) fprintf(debugFP, "pick next game from '%s': %d\n", q, nextGame);
1514 if(nextGame <= appData.matchGames && resChar != ' ' && !abortMatch) { // reserve next game if tourney not yet done
1515 if(q[nextGame] == NULLCHAR) q[nextGame+1] = NULLCHAR; // append one char
1518 fseek(tf, -(strlen(p)+4), SEEK_END);
1520 if(c != '"') // depending on DOS or Unix line endings we can be one off
1521 fseek(tf, -(strlen(p)+2), SEEK_END);
1522 else fseek(tf, -(strlen(p)+3), SEEK_END);
1523 fprintf(tf, "%s\"\n", q); fclose(tf); // update, and flush by closing
1524 DisplayMessage(buf, "");
1525 free(p); appData.results = q;
1526 if(nextGame <= appData.matchGames && resChar != ' ' && !abortMatch &&
1527 (gameNr < 0 || nextGame / appData.defaultMatchGames != gameNr / appData.defaultMatchGames)) {
1528 int round = appData.defaultMatchGames * appData.tourneyType;
1529 if(gameNr < 0 || appData.tourneyType < 1 || // gauntlet engine can always stay loaded as first engine
1530 appData.tourneyType > 1 && nextGame/round != gameNr/round) // in multi-gauntlet change only after round
1531 UnloadEngine(&first); // next game belongs to other pairing;
1532 UnloadEngine(&second); // already unload the engines, so TwoMachinesEvent will load new ones.
1534 if(appData.debugMode) fprintf(debugFP, "Reserved, next=%d, nr=%d\n", nextGame, gameNr);
1538 MatchEvent (int mode)
1539 { // [HGM] moved out of InitBackend3, to make it callable when match starts through menu
1541 if(matchMode) { // already in match mode: switch it off
1543 if(!appData.tourneyFile[0]) appData.matchGames = matchGame; // kludge to let match terminate after next game.
1546 // if(gameMode != BeginningOfGame) {
1547 // DisplayError(_("You can only start a match from the initial position."), 0);
1551 if(mode == 2) appData.matchGames = appData.defaultMatchGames;
1552 /* Set up machine vs. machine match */
1554 NextTourneyGame(-1, &dummy); // sets appData.matchGames if this is tourney, to make sure ReserveGame knows it
1555 if(appData.tourneyFile[0]) {
1557 if(nextGame > appData.matchGames) {
1559 if(strchr(appData.results, '*') == NULL) {
1561 appData.tourneyCycles++;
1562 if(f = WriteTourneyFile(appData.results, NULL)) { // make a tourney file with increased number of cycles
1564 NextTourneyGame(-1, &dummy);
1566 if(nextGame <= appData.matchGames) {
1567 DisplayNote(_("You restarted an already completed tourney.\nOne more cycle will now be added to it.\nGames commence in 10 sec."));
1569 ScheduleDelayedEvent(NextMatchGame, 10000);
1574 snprintf(buf, MSG_SIZ, _("All games in tourney '%s' are already played or playing"), appData.tourneyFile);
1575 DisplayError(buf, 0);
1576 appData.tourneyFile[0] = 0;
1580 if (appData.noChessProgram) { // [HGM] in tourney engines are loaded automatically
1581 DisplayFatalError(_("Can't have a match with no chess programs"),
1586 matchGame = roundNr = 1;
1587 first.matchWins = second.matchWins = 0; // [HGM] match: needed in later matches
1591 char *comboLine = NULL; // [HGM] recent: WinBoard's first-engine combobox line
1594 InitBackEnd3 P((void))
1596 GameMode initialMode;
1600 if(!appData.icsActive && !appData.noChessProgram && !appData.matchMode && // mode involves only first engine
1601 !strcmp(appData.variant, "normal") && // no explicit variant request
1602 appData.NrRanks == -1 && appData.NrFiles == -1 && appData.holdingsSize == -1 && // no size overrides requested
1603 !SupportedVariant(first.variants, VariantNormal, 8, 8, 0, first.protocolVersion, "") && // but 'normal' won't work with engine
1604 !SupportedVariant(first.variants, VariantFischeRandom, 8, 8, 0, first.protocolVersion, "") ) { // nor will Chess960
1605 char c, *q = first.variants, *p = strchr(q, ',');
1606 if(p) *p = NULLCHAR;
1607 if(StringToVariant(q) != VariantUnknown) { // the engine can play a recognized variant, however
1609 if(sscanf(q, "%dx%d+%d_%c", &w, &h, &s, &c) == 4) // get size overrides the engine needs with it (if any)
1610 appData.NrFiles = w, appData.NrRanks = h, appData.holdingsSize = s, q = strchr(q, '_') + 1;
1611 ASSIGN(appData.variant, q); // fake user requested the first variant played by the engine
1612 Reset(TRUE, FALSE); // and re-initialize
1617 InitChessProgram(&first, startedFromSetupPosition);
1619 if(!appData.noChessProgram) { /* [HGM] tidy: redo program version to use name from myname feature */
1620 free(programVersion);
1621 programVersion = (char*) malloc(8 + strlen(PACKAGE_STRING) + strlen(first.tidy));
1622 sprintf(programVersion, "%s + %s", PACKAGE_STRING, first.tidy);
1623 FloatToFront(&appData.recentEngineList, comboLine ? comboLine : appData.firstChessProgram);
1626 if (appData.icsActive) {
1628 /* [DM] Make a console window if needed [HGM] merged ifs */
1634 if (*appData.icsCommPort != NULLCHAR)
1635 len = snprintf(buf, MSG_SIZ, _("Could not open comm port %s"),
1636 appData.icsCommPort);
1638 len = snprintf(buf, MSG_SIZ, _("Could not connect to host %s, port %s"),
1639 appData.icsHost, appData.icsPort);
1641 if( (len >= MSG_SIZ) && appData.debugMode )
1642 fprintf(debugFP, "InitBackEnd3: buffer truncated.\n");
1644 DisplayFatalError(buf, err, 1);
1649 AddInputSource(icsPR, FALSE, read_from_ics, &telnetISR);
1651 AddInputSource(NoProc, FALSE, read_from_player, &fromUserISR);
1652 if(appData.keepAlive) // [HGM] alive: schedule sending of dummy 'date' command
1653 ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
1654 } else if (appData.noChessProgram) {
1660 if (*appData.cmailGameName != NULLCHAR) {
1662 OpenLoopback(&cmailPR);
1664 AddInputSource(cmailPR, FALSE, CmailSigHandlerCallBack, &cmailISR);
1668 DisplayMessage("", "");
1669 if (StrCaseCmp(appData.initialMode, "") == 0) {
1670 initialMode = BeginningOfGame;
1671 if(!appData.icsActive && appData.noChessProgram) { // [HGM] could be fall-back
1672 gameMode = MachinePlaysBlack; // "Machine Black" might have been implicitly highlighted
1673 ModeHighlight(); // make sure XBoard knows it is highlighted, so it will un-highlight it
1674 gameMode = BeginningOfGame; // in case BeginningOfGame now means "Edit Position"
1677 } else if (StrCaseCmp(appData.initialMode, "TwoMachines") == 0) {
1678 initialMode = TwoMachinesPlay;
1679 } else if (StrCaseCmp(appData.initialMode, "AnalyzeFile") == 0) {
1680 initialMode = AnalyzeFile;
1681 } else if (StrCaseCmp(appData.initialMode, "Analysis") == 0) {
1682 initialMode = AnalyzeMode;
1683 } else if (StrCaseCmp(appData.initialMode, "MachineWhite") == 0) {
1684 initialMode = MachinePlaysWhite;
1685 } else if (StrCaseCmp(appData.initialMode, "MachineBlack") == 0) {
1686 initialMode = MachinePlaysBlack;
1687 } else if (StrCaseCmp(appData.initialMode, "EditGame") == 0) {
1688 initialMode = EditGame;
1689 } else if (StrCaseCmp(appData.initialMode, "EditPosition") == 0) {
1690 initialMode = EditPosition;
1691 } else if (StrCaseCmp(appData.initialMode, "Training") == 0) {
1692 initialMode = Training;
1694 len = snprintf(buf, MSG_SIZ, _("Unknown initialMode %s"), appData.initialMode);
1695 if( (len >= MSG_SIZ) && appData.debugMode )
1696 fprintf(debugFP, "InitBackEnd3: buffer truncated.\n");
1698 DisplayFatalError(buf, 0, 2);
1702 if (appData.matchMode) {
1703 if(appData.tourneyFile[0]) { // start tourney from command line
1705 if(f = fopen(appData.tourneyFile, "r")) {
1706 ParseArgsFromFile(f); // make sure tourney parmeters re known
1708 appData.clockMode = TRUE;
1710 } else appData.tourneyFile[0] = NULLCHAR; // for now ignore bad tourney file
1713 } else if (*appData.cmailGameName != NULLCHAR) {
1714 /* Set up cmail mode */
1715 ReloadCmailMsgEvent(TRUE);
1717 /* Set up other modes */
1718 if (initialMode == AnalyzeFile) {
1719 if (*appData.loadGameFile == NULLCHAR) {
1720 DisplayFatalError(_("AnalyzeFile mode requires a game file"), 0, 1);
1724 if (*appData.loadGameFile != NULLCHAR) {
1725 (void) LoadGameFromFile(appData.loadGameFile,
1726 appData.loadGameIndex,
1727 appData.loadGameFile, TRUE);
1728 } else if (*appData.loadPositionFile != NULLCHAR) {
1729 (void) LoadPositionFromFile(appData.loadPositionFile,
1730 appData.loadPositionIndex,
1731 appData.loadPositionFile);
1732 /* [HGM] try to make self-starting even after FEN load */
1733 /* to allow automatic setup of fairy variants with wtm */
1734 if(initialMode == BeginningOfGame && !blackPlaysFirst) {
1735 gameMode = BeginningOfGame;
1736 setboardSpoiledMachineBlack = 1;
1738 /* [HGM] loadPos: make that every new game uses the setup */
1739 /* from file as long as we do not switch variant */
1740 if(!blackPlaysFirst) {
1741 startedFromPositionFile = TRUE;
1742 CopyBoard(filePosition, boards[0]);
1745 if (initialMode == AnalyzeMode) {
1746 if (appData.noChessProgram) {
1747 DisplayFatalError(_("Analysis mode requires a chess engine"), 0, 2);
1750 if (appData.icsActive) {
1751 DisplayFatalError(_("Analysis mode does not work with ICS mode"),0,2);
1755 } else if (initialMode == AnalyzeFile) {
1756 appData.showThinking = TRUE; // [HGM] thinking: moved out of ShowThinkingEvent
1757 ShowThinkingEvent();
1759 AnalysisPeriodicEvent(1);
1760 } else if (initialMode == MachinePlaysWhite) {
1761 if (appData.noChessProgram) {
1762 DisplayFatalError(_("MachineWhite mode requires a chess engine"),
1766 if (appData.icsActive) {
1767 DisplayFatalError(_("MachineWhite mode does not work with ICS mode"),
1771 MachineWhiteEvent();
1772 } else if (initialMode == MachinePlaysBlack) {
1773 if (appData.noChessProgram) {
1774 DisplayFatalError(_("MachineBlack mode requires a chess engine"),
1778 if (appData.icsActive) {
1779 DisplayFatalError(_("MachineBlack mode does not work with ICS mode"),
1783 MachineBlackEvent();
1784 } else if (initialMode == TwoMachinesPlay) {
1785 if (appData.noChessProgram) {
1786 DisplayFatalError(_("TwoMachines mode requires a chess engine"),
1790 if (appData.icsActive) {
1791 DisplayFatalError(_("TwoMachines mode does not work with ICS mode"),
1796 } else if (initialMode == EditGame) {
1798 } else if (initialMode == EditPosition) {
1799 EditPositionEvent();
1800 } else if (initialMode == Training) {
1801 if (*appData.loadGameFile == NULLCHAR) {
1802 DisplayFatalError(_("Training mode requires a game file"), 0, 2);
1811 HistorySet (char movelist[][2*MOVE_LEN], int first, int last, int current)
1813 DisplayBook(current+1);
1815 MoveHistorySet( movelist, first, last, current, pvInfoList );
1817 EvalGraphSet( first, last, current, pvInfoList );
1819 MakeEngineOutputTitle();
1823 * Establish will establish a contact to a remote host.port.
1824 * Sets icsPR to a ProcRef for a process (or pseudo-process)
1825 * used to talk to the host.
1826 * Returns 0 if okay, error code if not.
1833 if (*appData.icsCommPort != NULLCHAR) {
1834 /* Talk to the host through a serial comm port */
1835 return OpenCommPort(appData.icsCommPort, &icsPR);
1837 } else if (*appData.gateway != NULLCHAR) {
1838 if (*appData.remoteShell == NULLCHAR) {
1839 /* Use the rcmd protocol to run telnet program on a gateway host */
1840 snprintf(buf, sizeof(buf), "%s %s %s",
1841 appData.telnetProgram, appData.icsHost, appData.icsPort);
1842 return OpenRcmd(appData.gateway, appData.remoteUser, buf, &icsPR);
1845 /* Use the rsh program to run telnet program on a gateway host */
1846 if (*appData.remoteUser == NULLCHAR) {
1847 snprintf(buf, sizeof(buf), "%s %s %s %s %s", appData.remoteShell,
1848 appData.gateway, appData.telnetProgram,
1849 appData.icsHost, appData.icsPort);
1851 snprintf(buf, sizeof(buf), "%s %s -l %s %s %s %s",
1852 appData.remoteShell, appData.gateway,
1853 appData.remoteUser, appData.telnetProgram,
1854 appData.icsHost, appData.icsPort);
1856 return StartChildProcess(buf, "", &icsPR);
1859 } else if (appData.useTelnet) {
1860 return OpenTelnet(appData.icsHost, appData.icsPort, &icsPR);
1863 /* TCP socket interface differs somewhat between
1864 Unix and NT; handle details in the front end.
1866 return OpenTCP(appData.icsHost, appData.icsPort, &icsPR);
1871 EscapeExpand (char *p, char *q)
1872 { // [HGM] initstring: routine to shape up string arguments
1873 while(*p++ = *q++) if(p[-1] == '\\')
1875 case 'n': p[-1] = '\n'; break;
1876 case 'r': p[-1] = '\r'; break;
1877 case 't': p[-1] = '\t'; break;
1878 case '\\': p[-1] = '\\'; break;
1879 case 0: *p = 0; return;
1880 default: p[-1] = q[-1]; break;
1885 show_bytes (FILE *fp, char *buf, int count)
1888 if (*buf < 040 || *(unsigned char *) buf > 0177) {
1889 fprintf(fp, "\\%03o", *buf & 0xff);
1898 /* Returns an errno value */
1900 OutputMaybeTelnet (ProcRef pr, char *message, int count, int *outError)
1902 char buf[8192], *p, *q, *buflim;
1903 int left, newcount, outcount;
1905 if (*appData.icsCommPort != NULLCHAR || appData.useTelnet ||
1906 *appData.gateway != NULLCHAR) {
1907 if (appData.debugMode) {
1908 fprintf(debugFP, ">ICS: ");
1909 show_bytes(debugFP, message, count);
1910 fprintf(debugFP, "\n");
1912 return OutputToProcess(pr, message, count, outError);
1915 buflim = &buf[sizeof(buf)-1]; /* allow 1 byte for expanding last char */
1922 if (appData.debugMode) {
1923 fprintf(debugFP, ">ICS: ");
1924 show_bytes(debugFP, buf, newcount);
1925 fprintf(debugFP, "\n");
1927 outcount = OutputToProcess(pr, buf, newcount, outError);
1928 if (outcount < newcount) return -1; /* to be sure */
1935 } else if (((unsigned char) *p) == TN_IAC) {
1936 *q++ = (char) TN_IAC;
1943 if (appData.debugMode) {
1944 fprintf(debugFP, ">ICS: ");
1945 show_bytes(debugFP, buf, newcount);
1946 fprintf(debugFP, "\n");
1948 outcount = OutputToProcess(pr, buf, newcount, outError);
1949 if (outcount < newcount) return -1; /* to be sure */
1954 read_from_player (InputSourceRef isr, VOIDSTAR closure, char *message, int count, int error)
1956 int outError, outCount;
1957 static int gotEof = 0;
1960 /* Pass data read from player on to ICS */
1963 outCount = OutputMaybeTelnet(icsPR, message, count, &outError);
1964 if (outCount < count) {
1965 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1967 if(have_sent_ICS_logon == 2) {
1968 if(ini = fopen(appData.icsLogon, "w")) { // save first two lines (presumably username & password) on init script file
1969 fprintf(ini, "%s", message);
1970 have_sent_ICS_logon = 3;
1972 have_sent_ICS_logon = 1;
1973 } else if(have_sent_ICS_logon == 3) {
1974 fprintf(ini, "%s", message);
1976 have_sent_ICS_logon = 1;
1978 } else if (count < 0) {
1979 RemoveInputSource(isr);
1980 DisplayFatalError(_("Error reading from keyboard"), error, 1);
1981 } else if (gotEof++ > 0) {
1982 RemoveInputSource(isr);
1983 DisplayFatalError(_("Got end of file from keyboard"), 0, 0);
1989 { // [HGM] alive: periodically send dummy (date) command to ICS to prevent time-out
1990 if(!connectionAlive) DisplayFatalError("No response from ICS", 0, 1);
1991 connectionAlive = FALSE; // only sticks if no response to 'date' command.
1992 SendToICS("date\n");
1993 if(appData.keepAlive) ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
1996 /* added routine for printf style output to ics */
1998 ics_printf (char *format, ...)
2000 char buffer[MSG_SIZ];
2003 va_start(args, format);
2004 vsnprintf(buffer, sizeof(buffer), format, args);
2005 buffer[sizeof(buffer)-1] = '\0';
2013 int count, outCount, outError;
2015 if (icsPR == NoProc) return;
2018 outCount = OutputMaybeTelnet(icsPR, s, count, &outError);
2019 if (outCount < count) {
2020 DisplayFatalError(_("Error writing to ICS"), outError, 1);
2024 /* This is used for sending logon scripts to the ICS. Sending
2025 without a delay causes problems when using timestamp on ICC
2026 (at least on my machine). */
2028 SendToICSDelayed (char *s, long msdelay)
2030 int count, outCount, outError;
2032 if (icsPR == NoProc) return;
2035 if (appData.debugMode) {
2036 fprintf(debugFP, ">ICS: ");
2037 show_bytes(debugFP, s, count);
2038 fprintf(debugFP, "\n");
2040 outCount = OutputToProcessDelayed(icsPR, s, count, &outError,
2042 if (outCount < count) {
2043 DisplayFatalError(_("Error writing to ICS"), outError, 1);
2048 /* Remove all highlighting escape sequences in s
2049 Also deletes any suffix starting with '('
2052 StripHighlightAndTitle (char *s)
2054 static char retbuf[MSG_SIZ];
2057 while (*s != NULLCHAR) {
2058 while (*s == '\033') {
2059 while (*s != NULLCHAR && !isalpha(*s)) s++;
2060 if (*s != NULLCHAR) s++;
2062 while (*s != NULLCHAR && *s != '\033') {
2063 if (*s == '(' || *s == '[') {
2074 /* Remove all highlighting escape sequences in s */
2076 StripHighlight (char *s)
2078 static char retbuf[MSG_SIZ];
2081 while (*s != NULLCHAR) {
2082 while (*s == '\033') {
2083 while (*s != NULLCHAR && !isalpha(*s)) s++;
2084 if (*s != NULLCHAR) s++;
2086 while (*s != NULLCHAR && *s != '\033') {
2094 char engineVariant[MSG_SIZ];
2095 char *variantNames[] = VARIANT_NAMES;
2097 VariantName (VariantClass v)
2099 if(v == VariantUnknown || *engineVariant) return engineVariant;
2100 return variantNames[v];
2104 /* Identify a variant from the strings the chess servers use or the
2105 PGN Variant tag names we use. */
2107 StringToVariant (char *e)
2111 VariantClass v = VariantNormal;
2112 int i, found = FALSE;
2113 char buf[MSG_SIZ], c;
2118 /* [HGM] skip over optional board-size prefixes */
2119 if( sscanf(e, "%dx%d_%c", &i, &i, &c) == 3 ||
2120 sscanf(e, "%dx%d+%d_%c", &i, &i, &i, &c) == 4 ) {
2121 while( *e++ != '_');
2124 if(StrCaseStr(e, "misc/")) { // [HGM] on FICS, misc/shogi is not shogi
2128 for (i=0; i<sizeof(variantNames)/sizeof(char*); i++) {
2129 if (p = StrCaseStr(e, variantNames[i])) {
2130 if(p && i >= VariantShogi && (p != e || isalpha(p[strlen(variantNames[i])]))) continue;
2131 v = (VariantClass) i;
2138 if ((StrCaseStr(e, "fischer") && StrCaseStr(e, "random"))
2139 || StrCaseStr(e, "wild/fr")
2140 || StrCaseStr(e, "frc") || StrCaseStr(e, "960")) {
2141 v = VariantFischeRandom;
2142 } else if ((i = 4, p = StrCaseStr(e, "wild")) ||
2143 (i = 1, p = StrCaseStr(e, "w"))) {
2145 while (*p && (isspace(*p) || *p == '(' || *p == '/')) p++;
2152 case 0: /* FICS only, actually */
2154 /* Castling legal even if K starts on d-file */
2155 v = VariantWildCastle;
2160 /* Castling illegal even if K & R happen to start in
2161 normal positions. */
2162 v = VariantNoCastle;
2175 /* Castling legal iff K & R start in normal positions */
2181 /* Special wilds for position setup; unclear what to do here */
2182 v = VariantLoadable;
2185 /* Bizarre ICC game */
2186 v = VariantTwoKings;
2189 v = VariantKriegspiel;
2195 v = VariantFischeRandom;
2198 v = VariantCrazyhouse;
2201 v = VariantBughouse;
2207 /* Not quite the same as FICS suicide! */
2208 v = VariantGiveaway;
2214 v = VariantShatranj;
2217 /* Temporary names for future ICC types. The name *will* change in
2218 the next xboard/WinBoard release after ICC defines it. */
2256 v = VariantCapablanca;
2259 v = VariantKnightmate;
2265 v = VariantCylinder;
2271 v = VariantCapaRandom;
2274 v = VariantBerolina;
2286 /* Found "wild" or "w" in the string but no number;
2287 must assume it's normal chess. */
2291 len = snprintf(buf, MSG_SIZ, _("Unknown wild type %d"), wnum);
2292 if( (len >= MSG_SIZ) && appData.debugMode )
2293 fprintf(debugFP, "StringToVariant: buffer truncated.\n");
2295 DisplayError(buf, 0);
2301 if (appData.debugMode) {
2302 fprintf(debugFP, "recognized '%s' (%d) as variant %s\n",
2303 e, wnum, VariantName(v));
2308 static int leftover_start = 0, leftover_len = 0;
2309 char star_match[STAR_MATCH_N][MSG_SIZ];
2311 /* Test whether pattern is present at &buf[*index]; if so, return TRUE,
2312 advance *index beyond it, and set leftover_start to the new value of
2313 *index; else return FALSE. If pattern contains the character '*', it
2314 matches any sequence of characters not containing '\r', '\n', or the
2315 character following the '*' (if any), and the matched sequence(s) are
2316 copied into star_match.
2319 looking_at ( char *buf, int *index, char *pattern)
2321 char *bufp = &buf[*index], *patternp = pattern;
2323 char *matchp = star_match[0];
2326 if (*patternp == NULLCHAR) {
2327 *index = leftover_start = bufp - buf;
2331 if (*bufp == NULLCHAR) return FALSE;
2332 if (*patternp == '*') {
2333 if (*bufp == *(patternp + 1)) {
2335 matchp = star_match[++star_count];
2339 } else if (*bufp == '\n' || *bufp == '\r') {
2341 if (*patternp == NULLCHAR)
2346 *matchp++ = *bufp++;
2350 if (*patternp != *bufp) return FALSE;
2357 SendToPlayer (char *data, int length)
2359 int error, outCount;
2360 outCount = OutputToProcess(NoProc, data, length, &error);
2361 if (outCount < length) {
2362 DisplayFatalError(_("Error writing to display"), error, 1);
2367 PackHolding (char packed[], char *holding)
2377 switch (runlength) {
2388 sprintf(q, "%d", runlength);
2400 /* Telnet protocol requests from the front end */
2402 TelnetRequest (unsigned char ddww, unsigned char option)
2404 unsigned char msg[3];
2405 int outCount, outError;
2407 if (*appData.icsCommPort != NULLCHAR || appData.useTelnet) return;
2409 if (appData.debugMode) {
2410 char buf1[8], buf2[8], *ddwwStr, *optionStr;
2426 snprintf(buf1,sizeof(buf1)/sizeof(buf1[0]), "%d", ddww);
2435 snprintf(buf2,sizeof(buf2)/sizeof(buf2[0]), "%d", option);
2438 fprintf(debugFP, ">%s %s ", ddwwStr, optionStr);
2443 outCount = OutputToProcess(icsPR, (char *)msg, 3, &outError);
2445 DisplayFatalError(_("Error writing to ICS"), outError, 1);
2452 if (!appData.icsActive) return;
2453 TelnetRequest(TN_DO, TN_ECHO);
2459 if (!appData.icsActive) return;
2460 TelnetRequest(TN_DONT, TN_ECHO);
2464 CopyHoldings (Board board, char *holdings, ChessSquare lowestPiece)
2466 /* put the holdings sent to us by the server on the board holdings area */
2467 int i, j, holdingsColumn, holdingsStartRow, direction, countsColumn;
2471 if(gameInfo.holdingsWidth < 2) return;
2472 if(gameInfo.variant != VariantBughouse && board[HOLDINGS_SET])
2473 return; // prevent overwriting by pre-board holdings
2475 if( (int)lowestPiece >= BlackPawn ) {
2478 holdingsStartRow = BOARD_HEIGHT-1;
2481 holdingsColumn = BOARD_WIDTH-1;
2482 countsColumn = BOARD_WIDTH-2;
2483 holdingsStartRow = 0;
2487 for(i=0; i<BOARD_HEIGHT; i++) { /* clear holdings */
2488 board[i][holdingsColumn] = EmptySquare;
2489 board[i][countsColumn] = (ChessSquare) 0;
2491 while( (p=*holdings++) != NULLCHAR ) {
2492 piece = CharToPiece( ToUpper(p) );
2493 if(piece == EmptySquare) continue;
2494 /*j = (int) piece - (int) WhitePawn;*/
2495 j = PieceToNumber(piece);
2496 if(j >= gameInfo.holdingsSize) continue; /* ignore pieces that do not fit */
2497 if(j < 0) continue; /* should not happen */
2498 piece = (ChessSquare) ( (int)piece + (int)lowestPiece );
2499 board[holdingsStartRow+j*direction][holdingsColumn] = piece;
2500 board[holdingsStartRow+j*direction][countsColumn]++;
2506 VariantSwitch (Board board, VariantClass newVariant)
2508 int newHoldingsWidth, newWidth = 8, newHeight = 8, i, j;
2509 static Board oldBoard;
2511 startedFromPositionFile = FALSE;
2512 if(gameInfo.variant == newVariant) return;
2514 /* [HGM] This routine is called each time an assignment is made to
2515 * gameInfo.variant during a game, to make sure the board sizes
2516 * are set to match the new variant. If that means adding or deleting
2517 * holdings, we shift the playing board accordingly
2518 * This kludge is needed because in ICS observe mode, we get boards
2519 * of an ongoing game without knowing the variant, and learn about the
2520 * latter only later. This can be because of the move list we requested,
2521 * in which case the game history is refilled from the beginning anyway,
2522 * but also when receiving holdings of a crazyhouse game. In the latter
2523 * case we want to add those holdings to the already received position.
2527 if (appData.debugMode) {
2528 fprintf(debugFP, "Switch board from %s to %s\n",
2529 VariantName(gameInfo.variant), VariantName(newVariant));
2530 setbuf(debugFP, NULL);
2532 shuffleOpenings = 0; /* [HGM] shuffle */
2533 gameInfo.holdingsSize = 5; /* [HGM] prepare holdings */
2537 newWidth = 9; newHeight = 9;
2538 gameInfo.holdingsSize = 7;
2539 case VariantBughouse:
2540 case VariantCrazyhouse:
2541 newHoldingsWidth = 2; break;
2545 newHoldingsWidth = 2;
2546 gameInfo.holdingsSize = 8;
2549 case VariantCapablanca:
2550 case VariantCapaRandom:
2553 newHoldingsWidth = gameInfo.holdingsSize = 0;
2556 if(newWidth != gameInfo.boardWidth ||
2557 newHeight != gameInfo.boardHeight ||
2558 newHoldingsWidth != gameInfo.holdingsWidth ) {
2560 /* shift position to new playing area, if needed */
2561 if(newHoldingsWidth > gameInfo.holdingsWidth) {
2562 for(i=0; i<BOARD_HEIGHT; i++)
2563 for(j=BOARD_RGHT-1; j>=BOARD_LEFT; j--)
2564 board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
2566 for(i=0; i<newHeight; i++) {
2567 board[i][0] = board[i][newWidth+2*newHoldingsWidth-1] = EmptySquare;
2568 board[i][1] = board[i][newWidth+2*newHoldingsWidth-2] = (ChessSquare) 0;
2570 } else if(newHoldingsWidth < gameInfo.holdingsWidth) {
2571 for(i=0; i<BOARD_HEIGHT; i++)
2572 for(j=BOARD_LEFT; j<BOARD_RGHT; j++)
2573 board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
2576 board[HOLDINGS_SET] = 0;
2577 gameInfo.boardWidth = newWidth;
2578 gameInfo.boardHeight = newHeight;
2579 gameInfo.holdingsWidth = newHoldingsWidth;
2580 gameInfo.variant = newVariant;
2581 InitDrawingSizes(-2, 0);
2582 } else gameInfo.variant = newVariant;
2583 CopyBoard(oldBoard, board); // remember correctly formatted board
2584 InitPosition(FALSE); /* this sets up board[0], but also other stuff */
2585 DrawPosition(TRUE, currentMove ? boards[currentMove] : oldBoard);
2588 static int loggedOn = FALSE;
2590 /*-- Game start info cache: --*/
2592 char gs_kind[MSG_SIZ];
2593 static char player1Name[128] = "";
2594 static char player2Name[128] = "";
2595 static char cont_seq[] = "\n\\ ";
2596 static int player1Rating = -1;
2597 static int player2Rating = -1;
2598 /*----------------------------*/
2600 ColorClass curColor = ColorNormal;
2601 int suppressKibitz = 0;
2604 Boolean soughtPending = FALSE;
2605 Boolean seekGraphUp;
2606 #define MAX_SEEK_ADS 200
2608 char *seekAdList[MAX_SEEK_ADS];
2609 int ratingList[MAX_SEEK_ADS], xList[MAX_SEEK_ADS], yList[MAX_SEEK_ADS], seekNrList[MAX_SEEK_ADS], zList[MAX_SEEK_ADS];
2610 float tcList[MAX_SEEK_ADS];
2611 char colorList[MAX_SEEK_ADS];
2612 int nrOfSeekAds = 0;
2613 int minRating = 1010, maxRating = 2800;
2614 int hMargin = 10, vMargin = 20, h, w;
2615 extern int squareSize, lineGap;
2620 int x, y, color = 0, r = ratingList[i]; float tc = tcList[i];
2621 xList[i] = yList[i] = -100; // outside graph, so cannot be clicked
2622 if(r < minRating+100 && r >=0 ) r = minRating+100;
2623 if(r > maxRating) r = maxRating;
2624 if(tc < 1.f) tc = 1.f;
2625 if(tc > 95.f) tc = 95.f;
2626 x = (w-hMargin-squareSize/8-7)* log(tc)/log(95.) + hMargin;
2627 y = ((double)r - minRating)/(maxRating - minRating)
2628 * (h-vMargin-squareSize/8-1) + vMargin;
2629 if(ratingList[i] < 0) y = vMargin + squareSize/4;
2630 if(strstr(seekAdList[i], " u ")) color = 1;
2631 if(!strstr(seekAdList[i], "lightning") && // for now all wilds same color
2632 !strstr(seekAdList[i], "bullet") &&
2633 !strstr(seekAdList[i], "blitz") &&
2634 !strstr(seekAdList[i], "standard") ) color = 2;
2635 if(strstr(seekAdList[i], "(C) ")) color |= SQUARE; // plot computer seeks as squares
2636 DrawSeekDot(xList[i]=x+3*(color&~SQUARE), yList[i]=h-1-y, colorList[i]=color);
2640 PlotSingleSeekAd (int i)
2646 AddAd (char *handle, char *rating, int base, int inc, char rated, char *type, int nr, Boolean plot)
2648 char buf[MSG_SIZ], *ext = "";
2649 VariantClass v = StringToVariant(type);
2650 if(strstr(type, "wild")) {
2651 ext = type + 4; // append wild number
2652 if(v == VariantFischeRandom) type = "chess960"; else
2653 if(v == VariantLoadable) type = "setup"; else
2654 type = VariantName(v);
2656 snprintf(buf, MSG_SIZ, "%s (%s) %d %d %c %s%s", handle, rating, base, inc, rated, type, ext);
2657 if(nrOfSeekAds < MAX_SEEK_ADS-1) {
2658 if(seekAdList[nrOfSeekAds]) free(seekAdList[nrOfSeekAds]);
2659 ratingList[nrOfSeekAds] = -1; // for if seeker has no rating
2660 sscanf(rating, "%d", &ratingList[nrOfSeekAds]);
2661 tcList[nrOfSeekAds] = base + (2./3.)*inc;
2662 seekNrList[nrOfSeekAds] = nr;
2663 zList[nrOfSeekAds] = 0;
2664 seekAdList[nrOfSeekAds++] = StrSave(buf);
2665 if(plot) PlotSingleSeekAd(nrOfSeekAds-1);
2670 EraseSeekDot (int i)
2672 int x = xList[i], y = yList[i], d=squareSize/4, k;
2673 DrawSeekBackground(x-squareSize/8, y-squareSize/8, x+squareSize/8+1, y+squareSize/8+1);
2674 if(x < hMargin+d) DrawSeekAxis(hMargin, y-squareSize/8, hMargin, y+squareSize/8+1);
2675 // now replot every dot that overlapped
2676 for(k=0; k<nrOfSeekAds; k++) if(k != i) {
2677 int xx = xList[k], yy = yList[k];
2678 if(xx <= x+d && xx > x-d && yy <= y+d && yy > y-d)
2679 DrawSeekDot(xx, yy, colorList[k]);
2684 RemoveSeekAd (int nr)
2687 for(i=0; i<nrOfSeekAds; i++) if(seekNrList[i] == nr) {
2689 if(seekAdList[i]) free(seekAdList[i]);
2690 seekAdList[i] = seekAdList[--nrOfSeekAds];
2691 seekNrList[i] = seekNrList[nrOfSeekAds];
2692 ratingList[i] = ratingList[nrOfSeekAds];
2693 colorList[i] = colorList[nrOfSeekAds];
2694 tcList[i] = tcList[nrOfSeekAds];
2695 xList[i] = xList[nrOfSeekAds];
2696 yList[i] = yList[nrOfSeekAds];
2697 zList[i] = zList[nrOfSeekAds];
2698 seekAdList[nrOfSeekAds] = NULL;
2704 MatchSoughtLine (char *line)
2706 char handle[MSG_SIZ], rating[MSG_SIZ], type[MSG_SIZ];
2707 int nr, base, inc, u=0; char dummy;
2709 if(sscanf(line, "%d %s %s %d %d rated %s", &nr, rating, handle, &base, &inc, type) == 6 ||
2710 sscanf(line, "%d %s %s %s %d %d rated %c", &nr, rating, handle, type, &base, &inc, &dummy) == 7 ||
2712 (sscanf(line, "%d %s %s %d %d unrated %s", &nr, rating, handle, &base, &inc, type) == 6 ||
2713 sscanf(line, "%d %s %s %s %d %d unrated %c", &nr, rating, handle, type, &base, &inc, &dummy) == 7) ) {
2714 // match: compact and save the line
2715 AddAd(handle, rating, base, inc, u ? 'u' : 'r', type, nr, FALSE);
2725 if(!seekGraphUp) return FALSE;
2726 h = BOARD_HEIGHT * (squareSize + lineGap) + lineGap + 2*border;
2727 w = BOARD_WIDTH * (squareSize + lineGap) + lineGap + 2*border;
2729 DrawSeekBackground(0, 0, w, h);
2730 DrawSeekAxis(hMargin, h-1-vMargin, w-5, h-1-vMargin);
2731 DrawSeekAxis(hMargin, h-1-vMargin, hMargin, 5);
2732 for(i=0; i<4000; i+= 100) if(i>=minRating && i<maxRating) {
2733 int yy =((double)i - minRating)/(maxRating - minRating)*(h-vMargin-squareSize/8-1) + vMargin;
2735 DrawSeekAxis(hMargin-5, yy, hMargin+5*(i%500==0), yy); // rating ticks
2738 snprintf(buf, MSG_SIZ, "%d", i);
2739 DrawSeekText(buf, hMargin+squareSize/8+7, yy);
2742 DrawSeekText("unrated", hMargin+squareSize/8+7, h-1-vMargin-squareSize/4);
2743 for(i=1; i<100; i+=(i<10?1:5)) {
2744 int xx = (w-hMargin-squareSize/8-7)* log((double)i)/log(95.) + hMargin;
2745 DrawSeekAxis(xx, h-1-vMargin, xx, h-6-vMargin-3*(i%10==0)); // TC ticks
2746 if(i<=5 || (i>40 ? i%20 : i%10) == 0) {
2748 snprintf(buf, MSG_SIZ, "%d", i);
2749 DrawSeekText(buf, xx-2-3*(i>9), h-1-vMargin/2);
2752 for(i=0; i<nrOfSeekAds; i++) PlotSeekAd(i);
2757 SeekGraphClick (ClickType click, int x, int y, int moving)
2759 static int lastDown = 0, displayed = 0, lastSecond;
2760 if(y < 0) return FALSE;
2761 if(!(appData.seekGraph && appData.icsActive && loggedOn &&
2762 (gameMode == BeginningOfGame || gameMode == IcsIdle))) {
2763 if(!seekGraphUp) return FALSE;
2764 seekGraphUp = FALSE; // seek graph is up when it shouldn't be: take it down
2765 DrawPosition(TRUE, NULL);
2768 if(!seekGraphUp) { // initiate cration of seek graph by requesting seek-ad list
2769 if(click == Release || moving) return FALSE;
2771 soughtPending = TRUE;
2772 SendToICS(ics_prefix);
2773 SendToICS("sought\n"); // should this be "sought all"?
2774 } else { // issue challenge based on clicked ad
2775 int dist = 10000; int i, closest = 0, second = 0;
2776 for(i=0; i<nrOfSeekAds; i++) {
2777 int d = (x-xList[i])*(x-xList[i]) + (y-yList[i])*(y-yList[i]) + zList[i];
2778 if(d < dist) { dist = d; closest = i; }
2779 second += (d - zList[i] < 120); // count in-range ads
2780 if(click == Press && moving != 1 && zList[i]>0) zList[i] *= 0.8; // age priority
2784 second = (second > 1);
2785 if(displayed != closest || second != lastSecond) {
2786 DisplayMessage(second ? "!" : "", seekAdList[closest]);
2787 lastSecond = second; displayed = closest;
2789 if(click == Press) {
2790 if(moving == 2) zList[closest] = 100; // right-click; push to back on press
2793 } // on press 'hit', only show info
2794 if(moving == 2) return TRUE; // ignore right up-clicks on dot
2795 snprintf(buf, MSG_SIZ, "play %d\n", seekNrList[closest]);
2796 SendToICS(ics_prefix);
2798 return TRUE; // let incoming board of started game pop down the graph
2799 } else if(click == Release) { // release 'miss' is ignored
2800 zList[lastDown] = 100; // make future selection of the rejected ad more difficult
2801 if(moving == 2) { // right up-click
2802 nrOfSeekAds = 0; // refresh graph
2803 soughtPending = TRUE;
2804 SendToICS(ics_prefix);
2805 SendToICS("sought\n"); // should this be "sought all"?
2808 } else if(moving) { if(displayed >= 0) DisplayMessage("", ""); displayed = -1; return TRUE; }
2809 // press miss or release hit 'pop down' seek graph
2810 seekGraphUp = FALSE;
2811 DrawPosition(TRUE, NULL);
2817 read_from_ics (InputSourceRef isr, VOIDSTAR closure, char *data, int count, int error)
2819 #define BUF_SIZE (16*1024) /* overflowed at 8K with "inchannel 1" on FICS? */
2820 #define STARTED_NONE 0
2821 #define STARTED_MOVES 1
2822 #define STARTED_BOARD 2
2823 #define STARTED_OBSERVE 3
2824 #define STARTED_HOLDINGS 4
2825 #define STARTED_CHATTER 5
2826 #define STARTED_COMMENT 6
2827 #define STARTED_MOVES_NOHIDE 7
2829 static int started = STARTED_NONE;
2830 static char parse[20000];
2831 static int parse_pos = 0;
2832 static char buf[BUF_SIZE + 1];
2833 static int firstTime = TRUE, intfSet = FALSE;
2834 static ColorClass prevColor = ColorNormal;
2835 static int savingComment = FALSE;
2836 static int cmatch = 0; // continuation sequence match
2843 int backup; /* [DM] For zippy color lines */
2845 char talker[MSG_SIZ]; // [HGM] chat
2846 int channel, collective=0;
2848 connectionAlive = TRUE; // [HGM] alive: I think, therefore I am...
2850 if (appData.debugMode) {
2852 fprintf(debugFP, "<ICS: ");
2853 show_bytes(debugFP, data, count);
2854 fprintf(debugFP, "\n");
2858 if (appData.debugMode) { int f = forwardMostMove;
2859 fprintf(debugFP, "ics input %d, castling = %d %d %d %d %d %d\n", f,
2860 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
2861 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
2864 /* If last read ended with a partial line that we couldn't parse,
2865 prepend it to the new read and try again. */
2866 if (leftover_len > 0) {
2867 for (i=0; i<leftover_len; i++)
2868 buf[i] = buf[leftover_start + i];
2871 /* copy new characters into the buffer */
2872 bp = buf + leftover_len;
2873 buf_len=leftover_len;
2874 for (i=0; i<count; i++)
2877 if (data[i] == '\r')
2880 // join lines split by ICS?
2881 if (!appData.noJoin)
2884 Joining just consists of finding matches against the
2885 continuation sequence, and discarding that sequence
2886 if found instead of copying it. So, until a match
2887 fails, there's nothing to do since it might be the
2888 complete sequence, and thus, something we don't want
2891 if (data[i] == cont_seq[cmatch])
2894 if (cmatch == strlen(cont_seq))
2896 cmatch = 0; // complete match. just reset the counter
2899 it's possible for the ICS to not include the space
2900 at the end of the last word, making our [correct]
2901 join operation fuse two separate words. the server
2902 does this when the space occurs at the width setting.
2904 if (!buf_len || buf[buf_len-1] != ' ')
2915 match failed, so we have to copy what matched before
2916 falling through and copying this character. In reality,
2917 this will only ever be just the newline character, but
2918 it doesn't hurt to be precise.
2920 strncpy(bp, cont_seq, cmatch);
2932 buf[buf_len] = NULLCHAR;
2933 // next_out = leftover_len; // [HGM] should we set this to 0, and not print it in advance?
2938 while (i < buf_len) {
2939 /* Deal with part of the TELNET option negotiation
2940 protocol. We refuse to do anything beyond the
2941 defaults, except that we allow the WILL ECHO option,
2942 which ICS uses to turn off password echoing when we are
2943 directly connected to it. We reject this option
2944 if localLineEditing mode is on (always on in xboard)
2945 and we are talking to port 23, which might be a real
2946 telnet server that will try to keep WILL ECHO on permanently.
2948 if (buf_len - i >= 3 && (unsigned char) buf[i] == TN_IAC) {
2949 static int remoteEchoOption = FALSE; /* telnet ECHO option */
2950 unsigned char option;
2952 switch ((unsigned char) buf[++i]) {
2954 if (appData.debugMode)
2955 fprintf(debugFP, "\n<WILL ");
2956 switch (option = (unsigned char) buf[++i]) {
2958 if (appData.debugMode)
2959 fprintf(debugFP, "ECHO ");
2960 /* Reply only if this is a change, according
2961 to the protocol rules. */
2962 if (remoteEchoOption) break;
2963 if (appData.localLineEditing &&
2964 atoi(appData.icsPort) == TN_PORT) {
2965 TelnetRequest(TN_DONT, TN_ECHO);
2968 TelnetRequest(TN_DO, TN_ECHO);
2969 remoteEchoOption = TRUE;
2973 if (appData.debugMode)
2974 fprintf(debugFP, "%d ", option);
2975 /* Whatever this is, we don't want it. */
2976 TelnetRequest(TN_DONT, option);
2981 if (appData.debugMode)
2982 fprintf(debugFP, "\n<WONT ");
2983 switch (option = (unsigned char) buf[++i]) {
2985 if (appData.debugMode)
2986 fprintf(debugFP, "ECHO ");
2987 /* Reply only if this is a change, according
2988 to the protocol rules. */
2989 if (!remoteEchoOption) break;
2991 TelnetRequest(TN_DONT, TN_ECHO);
2992 remoteEchoOption = FALSE;
2995 if (appData.debugMode)
2996 fprintf(debugFP, "%d ", (unsigned char) option);
2997 /* Whatever this is, it must already be turned
2998 off, because we never agree to turn on
2999 anything non-default, so according to the
3000 protocol rules, we don't reply. */
3005 if (appData.debugMode)
3006 fprintf(debugFP, "\n<DO ");
3007 switch (option = (unsigned char) buf[++i]) {
3009 /* Whatever this is, we refuse to do it. */
3010 if (appData.debugMode)
3011 fprintf(debugFP, "%d ", option);
3012 TelnetRequest(TN_WONT, option);
3017 if (appData.debugMode)
3018 fprintf(debugFP, "\n<DONT ");
3019 switch (option = (unsigned char) buf[++i]) {
3021 if (appData.debugMode)
3022 fprintf(debugFP, "%d ", option);
3023 /* Whatever this is, we are already not doing
3024 it, because we never agree to do anything
3025 non-default, so according to the protocol
3026 rules, we don't reply. */
3031 if (appData.debugMode)
3032 fprintf(debugFP, "\n<IAC ");
3033 /* Doubled IAC; pass it through */
3037 if (appData.debugMode)
3038 fprintf(debugFP, "\n<%d ", (unsigned char) buf[i]);
3039 /* Drop all other telnet commands on the floor */
3042 if (oldi > next_out)
3043 SendToPlayer(&buf[next_out], oldi - next_out);
3049 /* OK, this at least will *usually* work */
3050 if (!loggedOn && looking_at(buf, &i, "ics%")) {
3054 if (loggedOn && !intfSet) {
3055 if (ics_type == ICS_ICC) {
3056 snprintf(str, MSG_SIZ,
3057 "/set-quietly interface %s\n/set-quietly style 12\n",
3059 if(appData.seekGraph && appData.autoRefresh) // [HGM] seekgraph
3060 strcat(str, "/set-2 51 1\n/set seek 1\n");
3061 } else if (ics_type == ICS_CHESSNET) {
3062 snprintf(str, MSG_SIZ, "/style 12\n");
3064 safeStrCpy(str, "alias $ @\n$set interface ", sizeof(str)/sizeof(str[0]));
3065 strcat(str, programVersion);
3066 strcat(str, "\n$iset startpos 1\n$iset ms 1\n");
3067 if(appData.seekGraph && appData.autoRefresh) // [HGM] seekgraph
3068 strcat(str, "$iset seekremove 1\n$set seek 1\n");
3070 strcat(str, "$iset nohighlight 1\n");
3072 strcat(str, "$iset lock 1\n$style 12\n");
3075 NotifyFrontendLogin();
3079 if (started == STARTED_COMMENT) {
3080 /* Accumulate characters in comment */
3081 parse[parse_pos++] = buf[i];
3082 if (buf[i] == '\n') {
3083 parse[parse_pos] = NULLCHAR;
3084 if(chattingPartner>=0) {
3086 snprintf(mess, MSG_SIZ, "%s%s", talker, parse);
3087 OutputChatMessage(chattingPartner, mess);
3088 if(collective == 1) { // broadcasted talk also goes to private chatbox of talker
3090 talker[strlen(talker+1)-1] = NULLCHAR; // strip closing delimiter
3091 for(p=0; p<MAX_CHAT; p++) if(!StrCaseCmp(talker+1, chatPartner[p])) {
3092 snprintf(mess, MSG_SIZ, "%s: %s", chatPartner[chattingPartner], parse);
3093 OutputChatMessage(p, mess);
3097 chattingPartner = -1;
3098 if(collective != 3) next_out = i+1; // [HGM] suppress printing in ICS window
3101 if(!suppressKibitz) // [HGM] kibitz
3102 AppendComment(forwardMostMove, StripHighlight(parse), TRUE);
3103 else { // [HGM kibitz: divert memorized engine kibitz to engine-output window
3104 int nrDigit = 0, nrAlph = 0, j;
3105 if(parse_pos > MSG_SIZ - 30) // defuse unreasonably long input
3106 { parse_pos = MSG_SIZ-30; parse[parse_pos - 1] = '\n'; }
3107 parse[parse_pos] = NULLCHAR;
3108 // try to be smart: if it does not look like search info, it should go to
3109 // ICS interaction window after all, not to engine-output window.
3110 for(j=0; j<parse_pos; j++) { // count letters and digits
3111 nrDigit += (parse[j] >= '0' && parse[j] <= '9');
3112 nrAlph += (parse[j] >= 'a' && parse[j] <= 'z');
3113 nrAlph += (parse[j] >= 'A' && parse[j] <= 'Z');
3115 if(nrAlph < 9*nrDigit) { // if more than 10% digit we assume search info
3116 int depth=0; float score;
3117 if(sscanf(parse, "!!! %f/%d", &score, &depth) == 2 && depth>0) {
3118 // [HGM] kibitz: save kibitzed opponent info for PGN and eval graph
3119 pvInfoList[forwardMostMove-1].depth = depth;
3120 pvInfoList[forwardMostMove-1].score = 100*score;
3122 OutputKibitz(suppressKibitz, parse);
3125 if(gameMode == IcsObserving) // restore original ICS messages
3126 /* TRANSLATORS: to 'kibitz' is to send a message to all players and the game observers */
3127 snprintf(tmp, MSG_SIZ, "%s kibitzes: %s", star_match[0], parse);
3129 /* TRANSLATORS: to 'kibitz' is to send a message to all players and the game observers */
3130 snprintf(tmp, MSG_SIZ, _("your opponent kibitzes: %s"), parse);
3131 SendToPlayer(tmp, strlen(tmp));
3133 next_out = i+1; // [HGM] suppress printing in ICS window
3135 started = STARTED_NONE;
3137 /* Don't match patterns against characters in comment */
3142 if (started == STARTED_CHATTER) {
3143 if (buf[i] != '\n') {
3144 /* Don't match patterns against characters in chatter */
3148 started = STARTED_NONE;
3149 if(suppressKibitz) next_out = i+1;
3152 /* Kludge to deal with rcmd protocol */
3153 if (firstTime && looking_at(buf, &i, "\001*")) {
3154 DisplayFatalError(&buf[1], 0, 1);
3160 if (!loggedOn && looking_at(buf, &i, "chessclub.com")) {
3163 if (appData.debugMode)
3164 fprintf(debugFP, "ics_type %d\n", ics_type);
3167 if (!loggedOn && looking_at(buf, &i, "freechess.org")) {
3168 ics_type = ICS_FICS;
3170 if (appData.debugMode)
3171 fprintf(debugFP, "ics_type %d\n", ics_type);
3174 if (!loggedOn && looking_at(buf, &i, "chess.net")) {
3175 ics_type = ICS_CHESSNET;
3177 if (appData.debugMode)
3178 fprintf(debugFP, "ics_type %d\n", ics_type);
3183 (looking_at(buf, &i, "\"*\" is *a registered name") ||
3184 looking_at(buf, &i, "Logging you in as \"*\"") ||
3185 looking_at(buf, &i, "will be \"*\""))) {
3186 safeStrCpy(ics_handle, star_match[0], sizeof(ics_handle)/sizeof(ics_handle[0]));
3190 if (loggedOn && !have_set_title && ics_handle[0] != NULLCHAR) {
3192 snprintf(buf, sizeof(buf), "%s@%s", ics_handle, appData.icsHost);
3193 DisplayIcsInteractionTitle(buf);
3194 have_set_title = TRUE;
3197 /* skip finger notes */
3198 if (started == STARTED_NONE &&
3199 ((buf[i] == ' ' && isdigit(buf[i+1])) ||
3200 (buf[i] == '1' && buf[i+1] == '0')) &&
3201 buf[i+2] == ':' && buf[i+3] == ' ') {
3202 started = STARTED_CHATTER;
3208 // [HGM] seekgraph: recognize sought lines and end-of-sought message
3209 if(appData.seekGraph) {
3210 if(soughtPending && MatchSoughtLine(buf+i)) {
3211 i = strstr(buf+i, "rated") - buf;
3212 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3213 next_out = leftover_start = i;
3214 started = STARTED_CHATTER;
3215 suppressKibitz = TRUE;
3218 if((gameMode == IcsIdle || gameMode == BeginningOfGame)
3219 && looking_at(buf, &i, "* ads displayed")) {
3220 soughtPending = FALSE;
3225 if(appData.autoRefresh) {
3226 if(looking_at(buf, &i, "* (*) seeking * * * * *\"play *\" to respond)\n")) {
3227 int s = (ics_type == ICS_ICC); // ICC format differs
3229 AddAd(star_match[0], star_match[1], atoi(star_match[2+s]), atoi(star_match[3+s]),
3230 star_match[4+s][0], star_match[5-3*s], atoi(star_match[7]), TRUE);
3231 looking_at(buf, &i, "*% "); // eat prompt
3232 if(oldi > 0 && buf[oldi-1] == '\n') oldi--; // suppress preceding LF, if any
3233 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3234 next_out = i; // suppress
3237 if(looking_at(buf, &i, "\nAds removed: *\n") || looking_at(buf, &i, "\031(51 * *\031)")) {
3238 char *p = star_match[0];
3240 if(seekGraphUp) RemoveSeekAd(atoi(p));
3241 while(*p && *p++ != ' '); // next
3243 looking_at(buf, &i, "*% "); // eat prompt
3244 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3251 /* skip formula vars */
3252 if (started == STARTED_NONE &&
3253 buf[i] == 'f' && isdigit(buf[i+1]) && buf[i+2] == ':') {
3254 started = STARTED_CHATTER;
3259 // [HGM] kibitz: try to recognize opponent engine-score kibitzes, to divert them to engine-output window
3260 if (appData.autoKibitz && started == STARTED_NONE &&
3261 !appData.icsEngineAnalyze && // [HGM] [DM] ICS analyze
3262 (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack || gameMode == IcsObserving)) {
3263 if((looking_at(buf, &i, "\n* kibitzes: ") || looking_at(buf, &i, "\n* whispers: ") ||
3264 looking_at(buf, &i, "* kibitzes: ") || looking_at(buf, &i, "* whispers: ")) &&
3265 (StrStr(star_match[0], gameInfo.white) == star_match[0] ||
3266 StrStr(star_match[0], gameInfo.black) == star_match[0] )) { // kibitz of self or opponent
3267 suppressKibitz = TRUE;
3268 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3270 if((StrStr(star_match[0], gameInfo.white) == star_match[0]
3271 && (gameMode == IcsPlayingWhite)) ||
3272 (StrStr(star_match[0], gameInfo.black) == star_match[0]
3273 && (gameMode == IcsPlayingBlack)) ) // opponent kibitz
3274 started = STARTED_CHATTER; // own kibitz we simply discard
3276 started = STARTED_COMMENT; // make sure it will be collected in parse[]
3277 parse_pos = 0; parse[0] = NULLCHAR;
3278 savingComment = TRUE;
3279 suppressKibitz = gameMode != IcsObserving ? 2 :
3280 (StrStr(star_match[0], gameInfo.white) == NULL) + 1;
3284 if((looking_at(buf, &i, "\nkibitzed to *\n") || looking_at(buf, &i, "kibitzed to *\n") ||
3285 looking_at(buf, &i, "\n(kibitzed to *\n") || looking_at(buf, &i, "(kibitzed to *\n"))
3286 && atoi(star_match[0])) {
3287 // suppress the acknowledgements of our own autoKibitz
3289 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3290 if(p = strchr(star_match[0], ' ')) p[1] = NULLCHAR; // clip off "players)" on FICS
3291 SendToPlayer(star_match[0], strlen(star_match[0]));
3292 if(looking_at(buf, &i, "*% ")) // eat prompt
3293 suppressKibitz = FALSE;
3297 } // [HGM] kibitz: end of patch
3299 if(looking_at(buf, &i, "* rating adjustment: * --> *\n")) continue;
3301 // [HGM] chat: intercept tells by users for which we have an open chat window
3303 if(started == STARTED_NONE && (looking_at(buf, &i, "* tells you:") || looking_at(buf, &i, "* says:") ||
3304 looking_at(buf, &i, "* whispers:") ||
3305 looking_at(buf, &i, "* kibitzes:") ||
3306 looking_at(buf, &i, "* shouts:") ||
3307 looking_at(buf, &i, "* c-shouts:") ||
3308 looking_at(buf, &i, "--> * ") ||
3309 looking_at(buf, &i, "*(*):") && (sscanf(star_match[1], "%d", &channel),1) ||
3310 looking_at(buf, &i, "*(*)(*):") && (sscanf(star_match[2], "%d", &channel),1) ||
3311 looking_at(buf, &i, "*(*)(*)(*):") && (sscanf(star_match[3], "%d", &channel),1) ||
3312 looking_at(buf, &i, "*(*)(*)(*)(*):") && sscanf(star_match[4], "%d", &channel) == 1 )) {
3314 sscanf(star_match[0], "%[^(]", talker+1); // strip (C) or (U) off ICS handle
3315 chattingPartner = -1; collective = 0;
3317 if(channel >= 0) // channel broadcast; look if there is a chatbox for this channel
3318 for(p=0; p<MAX_CHAT; p++) {
3320 if(chatPartner[p][0] >= '0' && chatPartner[p][0] <= '9' && channel == atoi(chatPartner[p])) {
3321 talker[0] = '['; strcat(talker, "] ");
3322 Colorize((channel == 1 ? ColorChannel1 : ColorChannel), FALSE);
3323 chattingPartner = p; break;
3326 if(buf[i-3] == 'e') // kibitz; look if there is a KIBITZ chatbox
3327 for(p=0; p<MAX_CHAT; p++) {
3329 if(!strcmp("kibitzes", chatPartner[p])) {
3330 talker[0] = '['; strcat(talker, "] ");
3331 chattingPartner = p; break;
3334 if(buf[i-3] == 'r') // whisper; look if there is a WHISPER chatbox
3335 for(p=0; p<MAX_CHAT; p++) {
3337 if(!strcmp("whispers", chatPartner[p])) {
3338 talker[0] = '['; strcat(talker, "] ");
3339 chattingPartner = p; break;
3342 if(buf[i-3] == 't' || buf[oldi+2] == '>') {// shout, c-shout or it; look if there is a 'shouts' chatbox
3343 if(buf[i-8] == '-' && buf[i-3] == 't')
3344 for(p=0; p<MAX_CHAT; p++) { // c-shout; check if dedicatesd c-shout box exists
3346 if(!strcmp("c-shouts", chatPartner[p])) {
3347 talker[0] = '('; strcat(talker, ") "); Colorize(ColorSShout, FALSE);
3348 chattingPartner = p; break;
3351 if(chattingPartner < 0)
3352 for(p=0; p<MAX_CHAT; p++) {
3354 if(!strcmp("shouts", chatPartner[p])) {
3355 if(buf[oldi+2] == '>') { talker[0] = '<'; strcat(talker, "> "); Colorize(ColorShout, FALSE); }
3356 else if(buf[i-8] == '-') { talker[0] = '('; strcat(talker, ") "); Colorize(ColorSShout, FALSE); }
3357 else { talker[0] = '['; strcat(talker, "] "); Colorize(ColorShout, FALSE); }
3358 chattingPartner = p; break;
3362 if(chattingPartner<0) // if not, look if there is a chatbox for this indivdual
3363 for(p=0; p<MAX_CHAT; p++) if(!StrCaseCmp(talker+1, chatPartner[p])) {
3365 Colorize(ColorTell, FALSE);
3366 if(collective) safeStrCpy(talker, "broadcasts: ", MSG_SIZ);
3368 chattingPartner = p; break;
3370 if(chattingPartner<0) i = oldi, safeStrCpy(lastTalker, talker+1, MSG_SIZ); else {
3371 Colorize(curColor, TRUE); // undo the bogus colorations we just made to trigger the souds
3372 started = STARTED_COMMENT;
3373 parse_pos = 0; parse[0] = NULLCHAR;
3374 savingComment = 3 + chattingPartner; // counts as TRUE
3375 if(collective == 3) i = oldi; else {
3376 suppressKibitz = TRUE;
3377 if(oldi > 0 && buf[oldi-1] == '\n') oldi--;
3378 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3382 } // [HGM] chat: end of patch
3385 if (appData.zippyTalk || appData.zippyPlay) {
3386 /* [DM] Backup address for color zippy lines */
3388 if (loggedOn == TRUE)
3389 if (ZippyControl(buf, &backup) || ZippyConverse(buf, &backup) ||
3390 (appData.zippyPlay && ZippyMatch(buf, &backup)));
3392 } // [DM] 'else { ' deleted
3394 /* Regular tells and says */
3395 (tkind = 1, looking_at(buf, &i, "* tells you: ")) ||
3396 looking_at(buf, &i, "* (your partner) tells you: ") ||
3397 looking_at(buf, &i, "* says: ") ||
3398 /* Don't color "message" or "messages" output */
3399 (tkind = 5, looking_at(buf, &i, "*. * (*:*): ")) ||
3400 looking_at(buf, &i, "*. * at *:*: ") ||
3401 looking_at(buf, &i, "--* (*:*): ") ||
3402 /* Message notifications (same color as tells) */
3403 looking_at(buf, &i, "* has left a message ") ||
3404 looking_at(buf, &i, "* just sent you a message:\n") ||
3405 /* Whispers and kibitzes */
3406 (tkind = 2, looking_at(buf, &i, "* whispers: ")) ||
3407 looking_at(buf, &i, "* kibitzes: ") ||
3409 (tkind = 3, looking_at(buf, &i, "*(*: "))) {
3411 if (tkind == 1 && strchr(star_match[0], ':')) {
3412 /* Avoid "tells you:" spoofs in channels */
3415 if (star_match[0][0] == NULLCHAR ||
3416 strchr(star_match[0], ' ') ||
3417 (tkind == 3 && strchr(star_match[1], ' '))) {
3418 /* Reject bogus matches */
3421 if (appData.colorize) {
3422 if (oldi > next_out) {
3423 SendToPlayer(&buf[next_out], oldi - next_out);
3428 Colorize(ColorTell, FALSE);
3429 curColor = ColorTell;
3432 Colorize(ColorKibitz, FALSE);
3433 curColor = ColorKibitz;
3436 p = strrchr(star_match[1], '(');
3443 Colorize(ColorChannel1, FALSE);
3444 curColor = ColorChannel1;
3446 Colorize(ColorChannel, FALSE);
3447 curColor = ColorChannel;
3451 curColor = ColorNormal;
3455 if (started == STARTED_NONE && appData.autoComment &&
3456 (gameMode == IcsObserving ||
3457 gameMode == IcsPlayingWhite ||
3458 gameMode == IcsPlayingBlack)) {
3459 parse_pos = i - oldi;
3460 memcpy(parse, &buf[oldi], parse_pos);
3461 parse[parse_pos] = NULLCHAR;
3462 started = STARTED_COMMENT;
3463 savingComment = TRUE;
3464 } else if(collective != 3) {
3465 started = STARTED_CHATTER;
3466 savingComment = FALSE;
3473 if (looking_at(buf, &i, "* s-shouts: ") ||
3474 looking_at(buf, &i, "* c-shouts: ")) {
3475 if (appData.colorize) {
3476 if (oldi > next_out) {
3477 SendToPlayer(&buf[next_out], oldi - next_out);
3480 Colorize(ColorSShout, FALSE);
3481 curColor = ColorSShout;
3484 started = STARTED_CHATTER;
3488 if (looking_at(buf, &i, "--->")) {
3493 if (looking_at(buf, &i, "* shouts: ") ||
3494 looking_at(buf, &i, "--> ")) {
3495 if (appData.colorize) {
3496 if (oldi > next_out) {
3497 SendToPlayer(&buf[next_out], oldi - next_out);
3500 Colorize(ColorShout, FALSE);
3501 curColor = ColorShout;
3504 started = STARTED_CHATTER;
3508 if (looking_at( buf, &i, "Challenge:")) {
3509 if (appData.colorize) {
3510 if (oldi > next_out) {
3511 SendToPlayer(&buf[next_out], oldi - next_out);
3514 Colorize(ColorChallenge, FALSE);
3515 curColor = ColorChallenge;
3521 if (looking_at(buf, &i, "* offers you") ||
3522 looking_at(buf, &i, "* offers to be") ||
3523 looking_at(buf, &i, "* would like to") ||
3524 looking_at(buf, &i, "* requests to") ||
3525 looking_at(buf, &i, "Your opponent offers") ||
3526 looking_at(buf, &i, "Your opponent requests")) {
3528 if (appData.colorize) {
3529 if (oldi > next_out) {
3530 SendToPlayer(&buf[next_out], oldi - next_out);
3533 Colorize(ColorRequest, FALSE);
3534 curColor = ColorRequest;
3539 if (looking_at(buf, &i, "* (*) seeking")) {
3540 if (appData.colorize) {
3541 if (oldi > next_out) {
3542 SendToPlayer(&buf[next_out], oldi - next_out);
3545 Colorize(ColorSeek, FALSE);
3546 curColor = ColorSeek;
3551 if(i < backup) { i = backup; continue; } // [HGM] for if ZippyControl matches, but the colorie code doesn't
3553 if (looking_at(buf, &i, "\\ ")) {
3554 if (prevColor != ColorNormal) {
3555 if (oldi > next_out) {
3556 SendToPlayer(&buf[next_out], oldi - next_out);
3559 Colorize(prevColor, TRUE);
3560 curColor = prevColor;
3562 if (savingComment) {
3563 parse_pos = i - oldi;
3564 memcpy(parse, &buf[oldi], parse_pos);
3565 parse[parse_pos] = NULLCHAR;
3566 started = STARTED_COMMENT;
3567 if(savingComment >= 3) // [HGM] chat: continuation of line for chat box
3568 chattingPartner = savingComment - 3; // kludge to remember the box
3570 started = STARTED_CHATTER;
3575 if (looking_at(buf, &i, "Black Strength :") ||
3576 looking_at(buf, &i, "<<< style 10 board >>>") ||
3577 looking_at(buf, &i, "<10>") ||
3578 looking_at(buf, &i, "#@#")) {
3579 /* Wrong board style */
3581 SendToICS(ics_prefix);
3582 SendToICS("set style 12\n");
3583 SendToICS(ics_prefix);
3584 SendToICS("refresh\n");
3588 if (looking_at(buf, &i, "login:")) {
3589 if (!have_sent_ICS_logon) {
3591 have_sent_ICS_logon = 1;
3592 else // no init script was found
3593 have_sent_ICS_logon = (appData.autoCreateLogon ? 2 : 1); // flag that we should capture username + password
3594 } else { // we have sent (or created) the InitScript, but apparently the ICS rejected it
3595 have_sent_ICS_logon = (appData.autoCreateLogon ? 2 : 1); // request creation of a new script
3600 if (ics_getting_history != H_GETTING_MOVES /*smpos kludge*/ &&
3601 (looking_at(buf, &i, "\n<12> ") ||
3602 looking_at(buf, &i, "<12> "))) {
3604 if (oldi > next_out) {
3605 SendToPlayer(&buf[next_out], oldi - next_out);
3608 started = STARTED_BOARD;
3613 if ((started == STARTED_NONE && looking_at(buf, &i, "\n<b1> ")) ||
3614 looking_at(buf, &i, "<b1> ")) {
3615 if (oldi > next_out) {
3616 SendToPlayer(&buf[next_out], oldi - next_out);
3619 started = STARTED_HOLDINGS;
3624 if (looking_at(buf, &i, "* *vs. * *--- *")) {
3626 /* Header for a move list -- first line */
3628 switch (ics_getting_history) {
3632 case BeginningOfGame:
3633 /* User typed "moves" or "oldmoves" while we
3634 were idle. Pretend we asked for these
3635 moves and soak them up so user can step
3636 through them and/or save them.
3639 gameMode = IcsObserving;
3642 ics_getting_history = H_GOT_UNREQ_HEADER;
3644 case EditGame: /*?*/
3645 case EditPosition: /*?*/
3646 /* Should above feature work in these modes too? */
3647 /* For now it doesn't */
3648 ics_getting_history = H_GOT_UNWANTED_HEADER;
3651 ics_getting_history = H_GOT_UNWANTED_HEADER;
3656 /* Is this the right one? */
3657 if (gameInfo.white && gameInfo.black &&
3658 strcmp(gameInfo.white, star_match[0]) == 0 &&
3659 strcmp(gameInfo.black, star_match[2]) == 0) {
3661 ics_getting_history = H_GOT_REQ_HEADER;
3664 case H_GOT_REQ_HEADER:
3665 case H_GOT_UNREQ_HEADER:
3666 case H_GOT_UNWANTED_HEADER:
3667 case H_GETTING_MOVES:
3668 /* Should not happen */
3669 DisplayError(_("Error gathering move list: two headers"), 0);
3670 ics_getting_history = H_FALSE;
3674 /* Save player ratings into gameInfo if needed */
3675 if ((ics_getting_history == H_GOT_REQ_HEADER ||
3676 ics_getting_history == H_GOT_UNREQ_HEADER) &&
3677 (gameInfo.whiteRating == -1 ||
3678 gameInfo.blackRating == -1)) {
3680 gameInfo.whiteRating = string_to_rating(star_match[1]);
3681 gameInfo.blackRating = string_to_rating(star_match[3]);
3682 if (appData.debugMode)
3683 fprintf(debugFP, "Ratings from header: W %d, B %d\n",
3684 gameInfo.whiteRating, gameInfo.blackRating);
3689 if (looking_at(buf, &i,
3690 "* * match, initial time: * minute*, increment: * second")) {
3691 /* Header for a move list -- second line */
3692 /* Initial board will follow if this is a wild game */
3693 if (gameInfo.event != NULL) free(gameInfo.event);
3694 snprintf(str, MSG_SIZ, "ICS %s %s match", star_match[0], star_match[1]);
3695 gameInfo.event = StrSave(str);
3696 /* [HGM] we switched variant. Translate boards if needed. */
3697 VariantSwitch(boards[currentMove], StringToVariant(gameInfo.event));
3701 if (looking_at(buf, &i, "Move ")) {
3702 /* Beginning of a move list */
3703 switch (ics_getting_history) {
3705 /* Normally should not happen */
3706 /* Maybe user hit reset while we were parsing */
3709 /* Happens if we are ignoring a move list that is not
3710 * the one we just requested. Common if the user
3711 * tries to observe two games without turning off
3714 case H_GETTING_MOVES:
3715 /* Should not happen */
3716 DisplayError(_("Error gathering move list: nested"), 0);
3717 ics_getting_history = H_FALSE;
3719 case H_GOT_REQ_HEADER:
3720 ics_getting_history = H_GETTING_MOVES;
3721 started = STARTED_MOVES;
3723 if (oldi > next_out) {
3724 SendToPlayer(&buf[next_out], oldi - next_out);
3727 case H_GOT_UNREQ_HEADER:
3728 ics_getting_history = H_GETTING_MOVES;
3729 started = STARTED_MOVES_NOHIDE;
3732 case H_GOT_UNWANTED_HEADER:
3733 ics_getting_history = H_FALSE;
3739 if (looking_at(buf, &i, "% ") ||
3740 ((started == STARTED_MOVES || started == STARTED_MOVES_NOHIDE)
3741 && looking_at(buf, &i, "}*"))) { char *bookHit = NULL; // [HGM] book
3742 if(soughtPending && nrOfSeekAds) { // [HGM] seekgraph: on ICC sought-list has no termination line
3743 soughtPending = FALSE;
3747 if(suppressKibitz) next_out = i;
3748 savingComment = FALSE;
3752 case STARTED_MOVES_NOHIDE:
3753 memcpy(&parse[parse_pos], &buf[oldi], i - oldi);
3754 parse[parse_pos + i - oldi] = NULLCHAR;
3755 ParseGameHistory(parse);
3757 if (appData.zippyPlay && first.initDone) {
3758 FeedMovesToProgram(&first, forwardMostMove);
3759 if (gameMode == IcsPlayingWhite) {
3760 if (WhiteOnMove(forwardMostMove)) {
3761 if (first.sendTime) {
3762 if (first.useColors) {
3763 SendToProgram("black\n", &first);
3765 SendTimeRemaining(&first, TRUE);
3767 if (first.useColors) {
3768 SendToProgram("white\n", &first); // [HGM] book: made sending of "go\n" book dependent
3770 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: probe book for initial pos
3771 first.maybeThinking = TRUE;
3773 if (first.usePlayother) {
3774 if (first.sendTime) {
3775 SendTimeRemaining(&first, TRUE);
3777 SendToProgram("playother\n", &first);
3783 } else if (gameMode == IcsPlayingBlack) {
3784 if (!WhiteOnMove(forwardMostMove)) {
3785 if (first.sendTime) {
3786 if (first.useColors) {
3787 SendToProgram("white\n", &first);
3789 SendTimeRemaining(&first, FALSE);
3791 if (first.useColors) {
3792 SendToProgram("black\n", &first);
3794 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE);
3795 first.maybeThinking = TRUE;
3797 if (first.usePlayother) {
3798 if (first.sendTime) {
3799 SendTimeRemaining(&first, FALSE);
3801 SendToProgram("playother\n", &first);
3810 if (gameMode == IcsObserving && ics_gamenum == -1) {
3811 /* Moves came from oldmoves or moves command
3812 while we weren't doing anything else.
3814 currentMove = forwardMostMove;
3815 ClearHighlights();/*!!could figure this out*/
3816 flipView = appData.flipView;
3817 DrawPosition(TRUE, boards[currentMove]);
3818 DisplayBothClocks();
3819 snprintf(str, MSG_SIZ, "%s %s %s",
3820 gameInfo.white, _("vs."), gameInfo.black);
3824 /* Moves were history of an active game */
3825 if (gameInfo.resultDetails != NULL) {
3826 free(gameInfo.resultDetails);
3827 gameInfo.resultDetails = NULL;
3830 HistorySet(parseList, backwardMostMove,
3831 forwardMostMove, currentMove-1);
3832 DisplayMove(currentMove - 1);
3833 if (started == STARTED_MOVES) next_out = i;
3834 started = STARTED_NONE;
3835 ics_getting_history = H_FALSE;
3838 case STARTED_OBSERVE:
3839 started = STARTED_NONE;
3840 SendToICS(ics_prefix);
3841 SendToICS("refresh\n");
3847 if(bookHit) { // [HGM] book: simulate book reply
3848 static char bookMove[MSG_SIZ]; // a bit generous?
3850 programStats.nodes = programStats.depth = programStats.time =
3851 programStats.score = programStats.got_only_move = 0;
3852 sprintf(programStats.movelist, "%s (xbook)", bookHit);
3854 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
3855 strcat(bookMove, bookHit);
3856 HandleMachineMove(bookMove, &first);
3861 if ((started == STARTED_MOVES || started == STARTED_BOARD ||
3862 started == STARTED_HOLDINGS ||
3863 started == STARTED_MOVES_NOHIDE) && i >= leftover_len) {
3864 /* Accumulate characters in move list or board */
3865 parse[parse_pos++] = buf[i];
3868 /* Start of game messages. Mostly we detect start of game
3869 when the first board image arrives. On some versions
3870 of the ICS, though, we need to do a "refresh" after starting
3871 to observe in order to get the current board right away. */
3872 if (looking_at(buf, &i, "Adding game * to observation list")) {
3873 started = STARTED_OBSERVE;
3877 /* Handle auto-observe */
3878 if (appData.autoObserve &&
3879 (gameMode == IcsIdle || gameMode == BeginningOfGame) &&
3880 looking_at(buf, &i, "Game notification: * (*) vs. * (*)")) {
3882 /* Choose the player that was highlighted, if any. */
3883 if (star_match[0][0] == '\033' ||
3884 star_match[1][0] != '\033') {
3885 player = star_match[0];
3887 player = star_match[2];
3889 snprintf(str, MSG_SIZ, "%sobserve %s\n",
3890 ics_prefix, StripHighlightAndTitle(player));
3893 /* Save ratings from notify string */
3894 safeStrCpy(player1Name, star_match[0], sizeof(player1Name)/sizeof(player1Name[0]));
3895 player1Rating = string_to_rating(star_match[1]);
3896 safeStrCpy(player2Name, star_match[2], sizeof(player2Name)/sizeof(player2Name[0]));
3897 player2Rating = string_to_rating(star_match[3]);
3899 if (appData.debugMode)
3901 "Ratings from 'Game notification:' %s %d, %s %d\n",
3902 player1Name, player1Rating,
3903 player2Name, player2Rating);
3908 /* Deal with automatic examine mode after a game,
3909 and with IcsObserving -> IcsExamining transition */
3910 if (looking_at(buf, &i, "Entering examine mode for game *") ||
3911 looking_at(buf, &i, "has made you an examiner of game *")) {
3913 int gamenum = atoi(star_match[0]);
3914 if ((gameMode == IcsIdle || gameMode == IcsObserving) &&
3915 gamenum == ics_gamenum) {
3916 /* We were already playing or observing this game;
3917 no need to refetch history */
3918 gameMode = IcsExamining;
3920 pauseExamForwardMostMove = forwardMostMove;
3921 } else if (currentMove < forwardMostMove) {
3922 ForwardInner(forwardMostMove);
3925 /* I don't think this case really can happen */
3926 SendToICS(ics_prefix);
3927 SendToICS("refresh\n");
3932 /* Error messages */
3933 // if (ics_user_moved) {
3934 if (1) { // [HGM] old way ignored error after move type in; ics_user_moved is not set then!
3935 if (looking_at(buf, &i, "Illegal move") ||
3936 looking_at(buf, &i, "Not a legal move") ||
3937 looking_at(buf, &i, "Your king is in check") ||
3938 looking_at(buf, &i, "It isn't your turn") ||
3939 looking_at(buf, &i, "It is not your move")) {
3941 if (ics_user_moved && forwardMostMove > backwardMostMove) { // only backup if we already moved
3942 currentMove = forwardMostMove-1;
3943 DisplayMove(currentMove - 1); /* before DMError */
3944 DrawPosition(FALSE, boards[currentMove]);
3945 SwitchClocks(forwardMostMove-1); // [HGM] race
3946 DisplayBothClocks();
3948 DisplayMoveError(_("Illegal move (rejected by ICS)")); // [HGM] but always relay error msg
3954 if (looking_at(buf, &i, "still have time") ||
3955 looking_at(buf, &i, "not out of time") ||
3956 looking_at(buf, &i, "either player is out of time") ||
3957 looking_at(buf, &i, "has timeseal; checking")) {
3958 /* We must have called his flag a little too soon */
3959 whiteFlag = blackFlag = FALSE;
3963 if (looking_at(buf, &i, "added * seconds to") ||
3964 looking_at(buf, &i, "seconds were added to")) {
3965 /* Update the clocks */
3966 SendToICS(ics_prefix);
3967 SendToICS("refresh\n");
3971 if (!ics_clock_paused && looking_at(buf, &i, "clock paused")) {
3972 ics_clock_paused = TRUE;
3977 if (ics_clock_paused && looking_at(buf, &i, "clock resumed")) {
3978 ics_clock_paused = FALSE;
3983 /* Grab player ratings from the Creating: message.
3984 Note we have to check for the special case when
3985 the ICS inserts things like [white] or [black]. */
3986 if (looking_at(buf, &i, "Creating: * (*)* * (*)") ||
3987 looking_at(buf, &i, "Creating: * (*) [*] * (*)")) {
3989 0 player 1 name (not necessarily white)
3991 2 empty, white, or black (IGNORED)
3992 3 player 2 name (not necessarily black)
3995 The names/ratings are sorted out when the game
3996 actually starts (below).
3998 safeStrCpy(player1Name, StripHighlightAndTitle(star_match[0]), sizeof(player1Name)/sizeof(player1Name[0]));
3999 player1Rating = string_to_rating(star_match[1]);
4000 safeStrCpy(player2Name, StripHighlightAndTitle(star_match[3]), sizeof(player2Name)/sizeof(player2Name[0]));
4001 player2Rating = string_to_rating(star_match[4]);
4003 if (appData.debugMode)
4005 "Ratings from 'Creating:' %s %d, %s %d\n",
4006 player1Name, player1Rating,
4007 player2Name, player2Rating);
4012 /* Improved generic start/end-of-game messages */
4013 if ((tkind=0, looking_at(buf, &i, "{Game * (* vs. *) *}*")) ||
4014 (tkind=1, looking_at(buf, &i, "{Game * (*(*) vs. *(*)) *}*"))){
4015 /* If tkind == 0: */
4016 /* star_match[0] is the game number */
4017 /* [1] is the white player's name */
4018 /* [2] is the black player's name */
4019 /* For end-of-game: */
4020 /* [3] is the reason for the game end */
4021 /* [4] is a PGN end game-token, preceded by " " */
4022 /* For start-of-game: */
4023 /* [3] begins with "Creating" or "Continuing" */
4024 /* [4] is " *" or empty (don't care). */
4025 int gamenum = atoi(star_match[0]);
4026 char *whitename, *blackname, *why, *endtoken;
4027 ChessMove endtype = EndOfFile;
4030 whitename = star_match[1];
4031 blackname = star_match[2];
4032 why = star_match[3];
4033 endtoken = star_match[4];
4035 whitename = star_match[1];
4036 blackname = star_match[3];
4037 why = star_match[5];
4038 endtoken = star_match[6];
4041 /* Game start messages */
4042 if (strncmp(why, "Creating ", 9) == 0 ||
4043 strncmp(why, "Continuing ", 11) == 0) {
4044 gs_gamenum = gamenum;
4045 safeStrCpy(gs_kind, strchr(why, ' ') + 1,sizeof(gs_kind)/sizeof(gs_kind[0]));
4046 if(ics_gamenum == -1) // [HGM] only if we are not already involved in a game (because gin=1 sends us such messages)
4047 VariantSwitch(boards[currentMove], StringToVariant(gs_kind)); // [HGM] variantswitch: even before we get first board
4049 if (appData.zippyPlay) {
4050 ZippyGameStart(whitename, blackname);
4053 partnerBoardValid = FALSE; // [HGM] bughouse
4057 /* Game end messages */
4058 if (gameMode == IcsIdle || gameMode == BeginningOfGame ||
4059 ics_gamenum != gamenum) {
4062 while (endtoken[0] == ' ') endtoken++;
4063 switch (endtoken[0]) {
4066 endtype = GameUnfinished;
4069 endtype = BlackWins;
4072 if (endtoken[1] == '/')
4073 endtype = GameIsDrawn;
4075 endtype = WhiteWins;
4078 GameEnds(endtype, why, GE_ICS);
4080 if (appData.zippyPlay && first.initDone) {
4081 ZippyGameEnd(endtype, why);
4082 if (first.pr == NoProc) {
4083 /* Start the next process early so that we'll
4084 be ready for the next challenge */
4085 StartChessProgram(&first);
4087 /* Send "new" early, in case this command takes
4088 a long time to finish, so that we'll be ready
4089 for the next challenge. */
4090 gameInfo.variant = VariantNormal; // [HGM] variantswitch: suppress sending of 'variant'
4094 if(appData.bgObserve && partnerBoardValid) DrawPosition(TRUE, partnerBoard);
4098 if (looking_at(buf, &i, "Removing game * from observation") ||
4099 looking_at(buf, &i, "no longer observing game *") ||
4100 looking_at(buf, &i, "Game * (*) has no examiners")) {
4101 if (gameMode == IcsObserving &&
4102 atoi(star_match[0]) == ics_gamenum)
4104 /* icsEngineAnalyze */
4105 if (appData.icsEngineAnalyze) {
4112 ics_user_moved = FALSE;
4117 if (looking_at(buf, &i, "no longer examining game *")) {
4118 if (gameMode == IcsExamining &&
4119 atoi(star_match[0]) == ics_gamenum)
4123 ics_user_moved = FALSE;
4128 /* Advance leftover_start past any newlines we find,
4129 so only partial lines can get reparsed */
4130 if (looking_at(buf, &i, "\n")) {
4131 prevColor = curColor;
4132 if (curColor != ColorNormal) {
4133 if (oldi > next_out) {
4134 SendToPlayer(&buf[next_out], oldi - next_out);
4137 Colorize(ColorNormal, FALSE);
4138 curColor = ColorNormal;
4140 if (started == STARTED_BOARD) {
4141 started = STARTED_NONE;
4142 parse[parse_pos] = NULLCHAR;
4143 ParseBoard12(parse);
4146 /* Send premove here */
4147 if (appData.premove) {
4149 if (currentMove == 0 &&
4150 gameMode == IcsPlayingWhite &&
4151 appData.premoveWhite) {
4152 snprintf(str, MSG_SIZ, "%s\n", appData.premoveWhiteText);
4153 if (appData.debugMode)
4154 fprintf(debugFP, "Sending premove:\n");
4156 } else if (currentMove == 1 &&
4157 gameMode == IcsPlayingBlack &&
4158 appData.premoveBlack) {
4159 snprintf(str, MSG_SIZ, "%s\n", appData.premoveBlackText);
4160 if (appData.debugMode)
4161 fprintf(debugFP, "Sending premove:\n");
4163 } else if (gotPremove) {
4165 ClearPremoveHighlights();
4166 if (appData.debugMode)
4167 fprintf(debugFP, "Sending premove:\n");
4168 UserMoveEvent(premoveFromX, premoveFromY,
4169 premoveToX, premoveToY,
4174 /* Usually suppress following prompt */
4175 if (!(forwardMostMove == 0 && gameMode == IcsExamining)) {
4176 while(looking_at(buf, &i, "\n")); // [HGM] skip empty lines
4177 if (looking_at(buf, &i, "*% ")) {
4178 savingComment = FALSE;
4183 } else if (started == STARTED_HOLDINGS) {
4185 char new_piece[MSG_SIZ];
4186 started = STARTED_NONE;
4187 parse[parse_pos] = NULLCHAR;
4188 if (appData.debugMode)
4189 fprintf(debugFP, "Parsing holdings: %s, currentMove = %d\n",
4190 parse, currentMove);
4191 if (sscanf(parse, " game %d", &gamenum) == 1) {
4192 if(gamenum == ics_gamenum) { // [HGM] bughouse: old code if part of foreground game
4193 if (gameInfo.variant == VariantNormal) {
4194 /* [HGM] We seem to switch variant during a game!
4195 * Presumably no holdings were displayed, so we have
4196 * to move the position two files to the right to
4197 * create room for them!
4199 VariantClass newVariant;
4200 switch(gameInfo.boardWidth) { // base guess on board width
4201 case 9: newVariant = VariantShogi; break;
4202 case 10: newVariant = VariantGreat; break;
4203 default: newVariant = VariantCrazyhouse; break;
4205 VariantSwitch(boards[currentMove], newVariant); /* temp guess */
4206 /* Get a move list just to see the header, which
4207 will tell us whether this is really bug or zh */
4208 if (ics_getting_history == H_FALSE) {
4209 ics_getting_history = H_REQUESTED;
4210 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4214 new_piece[0] = NULLCHAR;
4215 sscanf(parse, "game %d white [%s black [%s <- %s",
4216 &gamenum, white_holding, black_holding,
4218 white_holding[strlen(white_holding)-1] = NULLCHAR;
4219 black_holding[strlen(black_holding)-1] = NULLCHAR;
4220 /* [HGM] copy holdings to board holdings area */
4221 CopyHoldings(boards[forwardMostMove], white_holding, WhitePawn);
4222 CopyHoldings(boards[forwardMostMove], black_holding, BlackPawn);
4223 boards[forwardMostMove][HOLDINGS_SET] = 1; // flag holdings as set
4225 if (appData.zippyPlay && first.initDone) {
4226 ZippyHoldings(white_holding, black_holding,
4230 if (tinyLayout || smallLayout) {
4231 char wh[16], bh[16];
4232 PackHolding(wh, white_holding);
4233 PackHolding(bh, black_holding);
4234 snprintf(str, MSG_SIZ, "[%s-%s] %s-%s", wh, bh,
4235 gameInfo.white, gameInfo.black);
4237 snprintf(str, MSG_SIZ, "%s [%s] %s %s [%s]",
4238 gameInfo.white, white_holding, _("vs."),
4239 gameInfo.black, black_holding);
4241 if(!partnerUp) // [HGM] bughouse: when peeking at partner game we already know what he captured...
4242 DrawPosition(FALSE, boards[currentMove]);
4244 } else if(appData.bgObserve) { // [HGM] bughouse: holdings of other game => background
4245 sscanf(parse, "game %d white [%s black [%s <- %s",
4246 &gamenum, white_holding, black_holding,
4248 white_holding[strlen(white_holding)-1] = NULLCHAR;
4249 black_holding[strlen(black_holding)-1] = NULLCHAR;
4250 /* [HGM] copy holdings to partner-board holdings area */
4251 CopyHoldings(partnerBoard, white_holding, WhitePawn);
4252 CopyHoldings(partnerBoard, black_holding, BlackPawn);
4253 if(twoBoards) { partnerUp = 1; flipView = !flipView; } // [HGM] dual: always draw
4254 if(partnerUp) DrawPosition(FALSE, partnerBoard);
4255 if(twoBoards) { partnerUp = 0; flipView = !flipView; }
4258 /* Suppress following prompt */
4259 if (looking_at(buf, &i, "*% ")) {
4260 if(strchr(star_match[0], 7)) SendToPlayer("\007", 1); // Bell(); // FICS fuses bell for next board with prompt in zh captures
4261 savingComment = FALSE;
4269 i++; /* skip unparsed character and loop back */
4272 if (started != STARTED_MOVES && started != STARTED_BOARD && !suppressKibitz && // [HGM] kibitz
4273 // started != STARTED_HOLDINGS && i > next_out) { // [HGM] should we compare to leftover_start in stead of i?
4274 // SendToPlayer(&buf[next_out], i - next_out);
4275 started != STARTED_HOLDINGS && leftover_start > next_out) {
4276 SendToPlayer(&buf[next_out], leftover_start - next_out);
4280 leftover_len = buf_len - leftover_start;
4281 /* if buffer ends with something we couldn't parse,
4282 reparse it after appending the next read */
4284 } else if (count == 0) {
4285 RemoveInputSource(isr);
4286 DisplayFatalError(_("Connection closed by ICS"), 0, 0);
4288 DisplayFatalError(_("Error reading from ICS"), error, 1);
4293 /* Board style 12 looks like this:
4295 <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
4297 * The "<12> " is stripped before it gets to this routine. The two
4298 * trailing 0's (flip state and clock ticking) are later addition, and
4299 * some chess servers may not have them, or may have only the first.
4300 * Additional trailing fields may be added in the future.
4303 #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"
4305 #define RELATION_OBSERVING_PLAYED 0
4306 #define RELATION_OBSERVING_STATIC -2 /* examined, oldmoves, or smoves */
4307 #define RELATION_PLAYING_MYMOVE 1
4308 #define RELATION_PLAYING_NOTMYMOVE -1
4309 #define RELATION_EXAMINING 2
4310 #define RELATION_ISOLATED_BOARD -3
4311 #define RELATION_STARTING_POSITION -4 /* FICS only */
4314 ParseBoard12 (char *string)
4318 char *bookHit = NULL; // [HGM] book
4320 GameMode newGameMode;
4321 int gamenum, newGame, newMove, relation, basetime, increment, ics_flip = 0;
4322 int j, k, n, moveNum, white_stren, black_stren, white_time, black_time;
4323 int double_push, castle_ws, castle_wl, castle_bs, castle_bl, irrev_count;
4324 char to_play, board_chars[200];
4325 char move_str[MSG_SIZ], str[MSG_SIZ], elapsed_time[MSG_SIZ];
4326 char black[32], white[32];
4328 int prevMove = currentMove;
4331 int fromX, fromY, toX, toY;
4333 int ranks=1, files=0; /* [HGM] ICS80: allow variable board size */
4334 Boolean weird = FALSE, reqFlag = FALSE;
4336 fromX = fromY = toX = toY = -1;
4340 if (appData.debugMode)
4341 fprintf(debugFP, "Parsing board: %s\n", string);
4343 move_str[0] = NULLCHAR;
4344 elapsed_time[0] = NULLCHAR;
4345 { /* [HGM] figure out how many ranks and files the board has, for ICS extension used by Capablanca server */
4347 while(i < 199 && (string[i] != ' ' || string[i+2] != ' ')) {
4348 if(string[i] == ' ') { ranks++; files = 0; }
4350 if(!strchr(" -pnbrqkPNBRQK" , string[i])) weird = TRUE; // test for fairies
4353 for(j = 0; j <i; j++) board_chars[j] = string[j];
4354 board_chars[i] = '\0';
4357 n = sscanf(string, PATTERN, &to_play, &double_push,
4358 &castle_ws, &castle_wl, &castle_bs, &castle_bl, &irrev_count,
4359 &gamenum, white, black, &relation, &basetime, &increment,
4360 &white_stren, &black_stren, &white_time, &black_time,
4361 &moveNum, str, elapsed_time, move_str, &ics_flip,
4365 snprintf(str, MSG_SIZ, _("Failed to parse board string:\n\"%s\""), string);
4366 DisplayError(str, 0);
4370 /* Convert the move number to internal form */
4371 moveNum = (moveNum - 1) * 2;
4372 if (to_play == 'B') moveNum++;
4373 if (moveNum > framePtr) { // [HGM] vari: do not run into saved variations
4374 DisplayFatalError(_("Game too long; increase MAX_MOVES and recompile"),
4380 case RELATION_OBSERVING_PLAYED:
4381 case RELATION_OBSERVING_STATIC:
4382 if (gamenum == -1) {
4383 /* Old ICC buglet */
4384 relation = RELATION_OBSERVING_STATIC;
4386 newGameMode = IcsObserving;
4388 case RELATION_PLAYING_MYMOVE:
4389 case RELATION_PLAYING_NOTMYMOVE:
4391 ((relation == RELATION_PLAYING_MYMOVE) == (to_play == 'W')) ?
4392 IcsPlayingWhite : IcsPlayingBlack;
4393 soughtPending =FALSE; // [HGM] seekgraph: solve race condition
4395 case RELATION_EXAMINING:
4396 newGameMode = IcsExamining;
4398 case RELATION_ISOLATED_BOARD:
4400 /* Just display this board. If user was doing something else,
4401 we will forget about it until the next board comes. */
4402 newGameMode = IcsIdle;
4404 case RELATION_STARTING_POSITION:
4405 newGameMode = gameMode;
4409 if((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
4410 gameMode == IcsObserving && appData.dualBoard) // also allow use of second board for observing two games
4411 && newGameMode == IcsObserving && gamenum != ics_gamenum && appData.bgObserve) {
4412 // [HGM] bughouse: don't act on alien boards while we play. Just parse the board and save it */
4413 int fac = strchr(elapsed_time, '.') ? 1 : 1000;
4414 static int lastBgGame = -1;
4416 for (k = 0; k < ranks; k++) {
4417 for (j = 0; j < files; j++)
4418 board[k][j+gameInfo.holdingsWidth] = CharToPiece(board_chars[(ranks-1-k)*(files+1) + j]);
4419 if(gameInfo.holdingsWidth > 1) {
4420 board[k][0] = board[k][BOARD_WIDTH-1] = EmptySquare;
4421 board[k][1] = board[k][BOARD_WIDTH-2] = (ChessSquare) 0;;
4424 CopyBoard(partnerBoard, board);
4425 if(toSqr = strchr(str, '/')) { // extract highlights from long move
4426 partnerBoard[EP_STATUS-3] = toSqr[1] - AAA; // kludge: hide highlighting info in board
4427 partnerBoard[EP_STATUS-4] = toSqr[2] - ONE;
4428 } else partnerBoard[EP_STATUS-4] = partnerBoard[EP_STATUS-3] = -1;
4429 if(toSqr = strchr(str, '-')) {
4430 partnerBoard[EP_STATUS-1] = toSqr[1] - AAA;
4431 partnerBoard[EP_STATUS-2] = toSqr[2] - ONE;
4432 } else partnerBoard[EP_STATUS-1] = partnerBoard[EP_STATUS-2] = -1;
4433 if(appData.dualBoard && !twoBoards) { twoBoards = 1; InitDrawingSizes(-2,0); }
4434 if(twoBoards) { partnerUp = 1; flipView = !flipView; } // [HGM] dual
4435 if(partnerUp) DrawPosition(FALSE, partnerBoard);
4437 DisplayWhiteClock(white_time*fac, to_play == 'W');
4438 DisplayBlackClock(black_time*fac, to_play != 'W');
4439 activePartner = to_play;
4440 if(gamenum != lastBgGame) {
4442 snprintf(buf, MSG_SIZ, "%s %s %s", white, _("vs."), black);
4445 lastBgGame = gamenum;
4446 activePartnerTime = to_play == 'W' ? white_time*fac : black_time*fac;
4447 partnerUp = 0; flipView = !flipView; } // [HGM] dual
4448 snprintf(partnerStatus, MSG_SIZ,"W: %d:%02d B: %d:%02d (%d-%d) %c", white_time*fac/60000, (white_time*fac%60000)/1000,
4449 (black_time*fac/60000), (black_time*fac%60000)/1000, white_stren, black_stren, to_play);
4450 if(!twoBoards) DisplayMessage(partnerStatus, "");
4451 partnerBoardValid = TRUE;
4455 if(appData.dualBoard && appData.bgObserve) {
4456 if((newGameMode == IcsPlayingWhite || newGameMode == IcsPlayingBlack) && moveNum == 1)
4457 SendToICS(ics_prefix), SendToICS("pobserve\n");
4458 else if(newGameMode == IcsObserving && (gameMode == BeginningOfGame || gameMode == IcsIdle)) {
4460 snprintf(buf, MSG_SIZ, "%spobserve %s\n", ics_prefix, white);
4465 /* Modify behavior for initial board display on move listing
4468 switch (ics_getting_history) {
4472 case H_GOT_REQ_HEADER:
4473 case H_GOT_UNREQ_HEADER:
4474 /* This is the initial position of the current game */
4475 gamenum = ics_gamenum;
4476 moveNum = 0; /* old ICS bug workaround */
4477 if (to_play == 'B') {
4478 startedFromSetupPosition = TRUE;
4479 blackPlaysFirst = TRUE;
4481 if (forwardMostMove == 0) forwardMostMove = 1;
4482 if (backwardMostMove == 0) backwardMostMove = 1;
4483 if (currentMove == 0) currentMove = 1;
4485 newGameMode = gameMode;
4486 relation = RELATION_STARTING_POSITION; /* ICC needs this */
4488 case H_GOT_UNWANTED_HEADER:
4489 /* This is an initial board that we don't want */
4491 case H_GETTING_MOVES:
4492 /* Should not happen */
4493 DisplayError(_("Error gathering move list: extra board"), 0);
4494 ics_getting_history = H_FALSE;
4498 if (gameInfo.boardHeight != ranks || gameInfo.boardWidth != files ||
4499 move_str[1] == '@' && !gameInfo.holdingsWidth ||
4500 weird && (int)gameInfo.variant < (int)VariantShogi) {
4501 /* [HGM] We seem to have switched variant unexpectedly
4502 * Try to guess new variant from board size
4504 VariantClass newVariant = VariantFairy; // if 8x8, but fairies present
4505 if(ranks == 8 && files == 10) newVariant = VariantCapablanca; else
4506 if(ranks == 10 && files == 9) newVariant = VariantXiangqi; else
4507 if(ranks == 8 && files == 12) newVariant = VariantCourier; else
4508 if(ranks == 9 && files == 9) newVariant = VariantShogi; else
4509 if(ranks == 10 && files == 10) newVariant = VariantGrand; else
4510 if(!weird) newVariant = move_str[1] == '@' ? VariantCrazyhouse : VariantNormal;
4511 VariantSwitch(boards[currentMove], newVariant); /* temp guess */
4512 /* Get a move list just to see the header, which
4513 will tell us whether this is really bug or zh */
4514 if (ics_getting_history == H_FALSE) {
4515 ics_getting_history = H_REQUESTED; reqFlag = TRUE;
4516 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4521 /* Take action if this is the first board of a new game, or of a
4522 different game than is currently being displayed. */
4523 if (gamenum != ics_gamenum || newGameMode != gameMode ||
4524 relation == RELATION_ISOLATED_BOARD) {
4526 /* Forget the old game and get the history (if any) of the new one */
4527 if (gameMode != BeginningOfGame) {
4531 if (appData.autoRaiseBoard) BoardToTop();
4533 if (gamenum == -1) {
4534 newGameMode = IcsIdle;
4535 } else if ((moveNum > 0 || newGameMode == IcsObserving) && newGameMode != IcsIdle &&
4536 appData.getMoveList && !reqFlag) {
4537 /* Need to get game history */
4538 ics_getting_history = H_REQUESTED;
4539 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4543 /* Initially flip the board to have black on the bottom if playing
4544 black or if the ICS flip flag is set, but let the user change
4545 it with the Flip View button. */
4546 flipView = appData.autoFlipView ?
4547 (newGameMode == IcsPlayingBlack) || ics_flip :
4550 /* Done with values from previous mode; copy in new ones */
4551 gameMode = newGameMode;
4553 ics_gamenum = gamenum;
4554 if (gamenum == gs_gamenum) {
4555 int klen = strlen(gs_kind);
4556 if (gs_kind[klen - 1] == '.') gs_kind[klen - 1] = NULLCHAR;
4557 snprintf(str, MSG_SIZ, "ICS %s", gs_kind);
4558 gameInfo.event = StrSave(str);
4560 gameInfo.event = StrSave("ICS game");
4562 gameInfo.site = StrSave(appData.icsHost);
4563 gameInfo.date = PGNDate();
4564 gameInfo.round = StrSave("-");
4565 gameInfo.white = StrSave(white);
4566 gameInfo.black = StrSave(black);
4567 timeControl = basetime * 60 * 1000;
4569 timeIncrement = increment * 1000;
4570 movesPerSession = 0;
4571 gameInfo.timeControl = TimeControlTagValue();
4572 VariantSwitch(boards[currentMove], StringToVariant(gameInfo.event) );
4573 if (appData.debugMode) {
4574 fprintf(debugFP, "ParseBoard says variant = '%s'\n", gameInfo.event);
4575 fprintf(debugFP, "recognized as %s\n", VariantName(gameInfo.variant));
4576 setbuf(debugFP, NULL);
4579 gameInfo.outOfBook = NULL;
4581 /* Do we have the ratings? */
4582 if (strcmp(player1Name, white) == 0 &&
4583 strcmp(player2Name, black) == 0) {
4584 if (appData.debugMode)
4585 fprintf(debugFP, "Remembered ratings: W %d, B %d\n",
4586 player1Rating, player2Rating);
4587 gameInfo.whiteRating = player1Rating;
4588 gameInfo.blackRating = player2Rating;
4589 } else if (strcmp(player2Name, white) == 0 &&
4590 strcmp(player1Name, black) == 0) {
4591 if (appData.debugMode)
4592 fprintf(debugFP, "Remembered ratings: W %d, B %d\n",
4593 player2Rating, player1Rating);
4594 gameInfo.whiteRating = player2Rating;
4595 gameInfo.blackRating = player1Rating;
4597 player1Name[0] = player2Name[0] = NULLCHAR;
4599 /* Silence shouts if requested */
4600 if (appData.quietPlay &&
4601 (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)) {
4602 SendToICS(ics_prefix);
4603 SendToICS("set shout 0\n");
4607 /* Deal with midgame name changes */
4609 if (!gameInfo.white || strcmp(gameInfo.white, white) != 0) {
4610 if (gameInfo.white) free(gameInfo.white);
4611 gameInfo.white = StrSave(white);
4613 if (!gameInfo.black || strcmp(gameInfo.black, black) != 0) {
4614 if (gameInfo.black) free(gameInfo.black);
4615 gameInfo.black = StrSave(black);
4619 /* Throw away game result if anything actually changes in examine mode */
4620 if (gameMode == IcsExamining && !newGame) {
4621 gameInfo.result = GameUnfinished;
4622 if (gameInfo.resultDetails != NULL) {
4623 free(gameInfo.resultDetails);
4624 gameInfo.resultDetails = NULL;
4628 /* In pausing && IcsExamining mode, we ignore boards coming
4629 in if they are in a different variation than we are. */
4630 if (pauseExamInvalid) return;
4631 if (pausing && gameMode == IcsExamining) {
4632 if (moveNum <= pauseExamForwardMostMove) {
4633 pauseExamInvalid = TRUE;
4634 forwardMostMove = pauseExamForwardMostMove;
4639 if (appData.debugMode) {
4640 fprintf(debugFP, "load %dx%d board\n", files, ranks);
4642 /* Parse the board */
4643 for (k = 0; k < ranks; k++) {
4644 for (j = 0; j < files; j++)
4645 board[k][j+gameInfo.holdingsWidth] = CharToPiece(board_chars[(ranks-1-k)*(files+1) + j]);
4646 if(gameInfo.holdingsWidth > 1) {
4647 board[k][0] = board[k][BOARD_WIDTH-1] = EmptySquare;
4648 board[k][1] = board[k][BOARD_WIDTH-2] = (ChessSquare) 0;;
4651 if(moveNum==0 && gameInfo.variant == VariantSChess) {
4652 board[5][BOARD_RGHT+1] = WhiteAngel;
4653 board[6][BOARD_RGHT+1] = WhiteMarshall;
4654 board[1][0] = BlackMarshall;
4655 board[2][0] = BlackAngel;
4656 board[1][1] = board[2][1] = board[5][BOARD_RGHT] = board[6][BOARD_RGHT] = 1;
4658 CopyBoard(boards[moveNum], board);
4659 boards[moveNum][HOLDINGS_SET] = 0; // [HGM] indicate holdings not set
4661 startedFromSetupPosition =
4662 !CompareBoards(board, initialPosition);
4663 if(startedFromSetupPosition)
4664 initialRulePlies = irrev_count; /* [HGM] 50-move counter offset */
4667 /* [HGM] Set castling rights. Take the outermost Rooks,
4668 to make it also work for FRC opening positions. Note that board12
4669 is really defective for later FRC positions, as it has no way to
4670 indicate which Rook can castle if they are on the same side of King.
4671 For the initial position we grant rights to the outermost Rooks,
4672 and remember thos rights, and we then copy them on positions
4673 later in an FRC game. This means WB might not recognize castlings with
4674 Rooks that have moved back to their original position as illegal,
4675 but in ICS mode that is not its job anyway.
4677 if(moveNum == 0 || gameInfo.variant != VariantFischeRandom)
4678 { int i, j; ChessSquare wKing = WhiteKing, bKing = BlackKing;
4680 for(i=BOARD_LEFT, j=NoRights; i<BOARD_RGHT; i++)
4681 if(board[0][i] == WhiteRook) j = i;
4682 initialRights[0] = boards[moveNum][CASTLING][0] = (castle_ws == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4683 for(i=BOARD_RGHT-1, j=NoRights; i>=BOARD_LEFT; i--)
4684 if(board[0][i] == WhiteRook) j = i;
4685 initialRights[1] = boards[moveNum][CASTLING][1] = (castle_wl == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4686 for(i=BOARD_LEFT, j=NoRights; i<BOARD_RGHT; i++)
4687 if(board[BOARD_HEIGHT-1][i] == BlackRook) j = i;
4688 initialRights[3] = boards[moveNum][CASTLING][3] = (castle_bs == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4689 for(i=BOARD_RGHT-1, j=NoRights; i>=BOARD_LEFT; i--)
4690 if(board[BOARD_HEIGHT-1][i] == BlackRook) j = i;
4691 initialRights[4] = boards[moveNum][CASTLING][4] = (castle_bl == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4693 boards[moveNum][CASTLING][2] = boards[moveNum][CASTLING][5] = NoRights;
4694 if(gameInfo.variant == VariantKnightmate) { wKing = WhiteUnicorn; bKing = BlackUnicorn; }
4695 for(k=BOARD_LEFT; k<BOARD_RGHT; k++)
4696 if(board[0][k] == wKing) initialRights[2] = boards[moveNum][CASTLING][2] = k;
4697 for(k=BOARD_LEFT; k<BOARD_RGHT; k++)
4698 if(board[BOARD_HEIGHT-1][k] == bKing)
4699 initialRights[5] = boards[moveNum][CASTLING][5] = k;
4700 if(gameInfo.variant == VariantTwoKings) {
4701 // In TwoKings looking for a King does not work, so always give castling rights to a King on e1/e8
4702 if(board[0][4] == wKing) initialRights[2] = boards[moveNum][CASTLING][2] = 4;
4703 if(board[BOARD_HEIGHT-1][4] == bKing) initialRights[5] = boards[moveNum][CASTLING][5] = 4;
4706 r = boards[moveNum][CASTLING][0] = initialRights[0];
4707 if(board[0][r] != WhiteRook) boards[moveNum][CASTLING][0] = NoRights;
4708 r = boards[moveNum][CASTLING][1] = initialRights[1];
4709 if(board[0][r] != WhiteRook) boards[moveNum][CASTLING][1] = NoRights;
4710 r = boards[moveNum][CASTLING][3] = initialRights[3];
4711 if(board[BOARD_HEIGHT-1][r] != BlackRook) boards[moveNum][CASTLING][3] = NoRights;
4712 r = boards[moveNum][CASTLING][4] = initialRights[4];
4713 if(board[BOARD_HEIGHT-1][r] != BlackRook) boards[moveNum][CASTLING][4] = NoRights;
4714 /* wildcastle kludge: always assume King has rights */
4715 r = boards[moveNum][CASTLING][2] = initialRights[2];
4716 r = boards[moveNum][CASTLING][5] = initialRights[5];
4718 /* [HGM] e.p. rights. Assume that ICS sends file number here? */
4719 boards[moveNum][EP_STATUS] = EP_NONE;
4720 if(str[0] == 'P') boards[moveNum][EP_STATUS] = EP_PAWN_MOVE;
4721 if(strchr(move_str, 'x')) boards[moveNum][EP_STATUS] = EP_CAPTURE;
4722 if(double_push != -1) boards[moveNum][EP_STATUS] = double_push + BOARD_LEFT;
4725 if (ics_getting_history == H_GOT_REQ_HEADER ||
4726 ics_getting_history == H_GOT_UNREQ_HEADER) {
4727 /* This was an initial position from a move list, not
4728 the current position */
4732 /* Update currentMove and known move number limits */
4733 newMove = newGame || moveNum > forwardMostMove;
4736 forwardMostMove = backwardMostMove = currentMove = moveNum;
4737 if (gameMode == IcsExamining && moveNum == 0) {
4738 /* Workaround for ICS limitation: we are not told the wild
4739 type when starting to examine a game. But if we ask for
4740 the move list, the move list header will tell us */
4741 ics_getting_history = H_REQUESTED;
4742 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4745 } else if (moveNum == forwardMostMove + 1 || moveNum == forwardMostMove
4746 || (moveNum < forwardMostMove && moveNum >= backwardMostMove)) {
4748 /* [DM] If we found takebacks during icsEngineAnalyze try send to engine */
4749 /* [HGM] applied this also to an engine that is silently watching */
4750 if (appData.zippyPlay && moveNum < forwardMostMove && first.initDone &&
4751 (gameMode == IcsObserving || gameMode == IcsExamining) &&
4752 gameInfo.variant == currentlyInitializedVariant) {
4753 takeback = forwardMostMove - moveNum;
4754 for (i = 0; i < takeback; i++) {
4755 if (appData.debugMode) fprintf(debugFP, "take back move\n");
4756 SendToProgram("undo\n", &first);
4761 forwardMostMove = moveNum;
4762 if (!pausing || currentMove > forwardMostMove)
4763 currentMove = forwardMostMove;
4765 /* New part of history that is not contiguous with old part */
4766 if (pausing && gameMode == IcsExamining) {
4767 pauseExamInvalid = TRUE;
4768 forwardMostMove = pauseExamForwardMostMove;
4771 if (gameMode == IcsExamining && moveNum > 0 && appData.getMoveList) {
4773 if(appData.zippyPlay && forwardMostMove > 0 && first.initDone) {
4774 // [HGM] when we will receive the move list we now request, it will be
4775 // fed to the engine from the first move on. So if the engine is not
4776 // in the initial position now, bring it there.
4777 InitChessProgram(&first, 0);
4780 ics_getting_history = H_REQUESTED;
4781 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4784 forwardMostMove = backwardMostMove = currentMove = moveNum;
4787 /* Update the clocks */
4788 if (strchr(elapsed_time, '.')) {
4790 timeRemaining[0][moveNum] = whiteTimeRemaining = white_time;
4791 timeRemaining[1][moveNum] = blackTimeRemaining = black_time;
4793 /* Time is in seconds */
4794 timeRemaining[0][moveNum] = whiteTimeRemaining = white_time * 1000;
4795 timeRemaining[1][moveNum] = blackTimeRemaining = black_time * 1000;
4800 if (appData.zippyPlay && newGame &&
4801 gameMode != IcsObserving && gameMode != IcsIdle &&
4802 gameMode != IcsExamining)
4803 ZippyFirstBoard(moveNum, basetime, increment);
4806 /* Put the move on the move list, first converting
4807 to canonical algebraic form. */
4809 if (appData.debugMode) {
4810 int f = forwardMostMove;
4811 fprintf(debugFP, "parseboard %d, castling = %d %d %d %d %d %d\n", f,
4812 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
4813 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
4814 fprintf(debugFP, "accepted move %s from ICS, parse it.\n", move_str);
4815 fprintf(debugFP, "moveNum = %d\n", moveNum);
4816 fprintf(debugFP, "board = %d-%d x %d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT);
4817 setbuf(debugFP, NULL);
4819 if (moveNum <= backwardMostMove) {
4820 /* We don't know what the board looked like before
4822 safeStrCpy(parseList[moveNum - 1], move_str, sizeof(parseList[moveNum - 1])/sizeof(parseList[moveNum - 1][0]));
4823 strcat(parseList[moveNum - 1], " ");
4824 strcat(parseList[moveNum - 1], elapsed_time);
4825 moveList[moveNum - 1][0] = NULLCHAR;
4826 } else if (strcmp(move_str, "none") == 0) {
4827 // [HGM] long SAN: swapped order; test for 'none' before parsing move
4828 /* Again, we don't know what the board looked like;
4829 this is really the start of the game. */
4830 parseList[moveNum - 1][0] = NULLCHAR;
4831 moveList[moveNum - 1][0] = NULLCHAR;
4832 backwardMostMove = moveNum;
4833 startedFromSetupPosition = TRUE;
4834 fromX = fromY = toX = toY = -1;
4836 // [HGM] long SAN: if legality-testing is off, disambiguation might not work or give wrong move.
4837 // So we parse the long-algebraic move string in stead of the SAN move
4838 int valid; char buf[MSG_SIZ], *prom;
4840 if(gameInfo.variant == VariantShogi && !strchr(move_str, '=') && !strchr(move_str, '@'))
4841 strcat(move_str, "="); // if ICS does not say 'promote' on non-drop, we defer.
4842 // str looks something like "Q/a1-a2"; kill the slash
4844 snprintf(buf, MSG_SIZ,"%c%s", str[0], str+2);
4845 else safeStrCpy(buf, str, sizeof(buf)/sizeof(buf[0])); // might be castling
4846 if((prom = strstr(move_str, "=")) && !strstr(buf, "="))
4847 strcat(buf, prom); // long move lacks promo specification!
4848 if(!appData.testLegality && move_str[1] != '@') { // drops never ambiguous (parser chokes on long form!)
4849 if(appData.debugMode)
4850 fprintf(debugFP, "replaced ICS move '%s' by '%s'\n", move_str, buf);
4851 safeStrCpy(move_str, buf, MSG_SIZ);
4853 valid = ParseOneMove(move_str, moveNum - 1, &moveType,
4854 &fromX, &fromY, &toX, &toY, &promoChar)
4855 || ParseOneMove(buf, moveNum - 1, &moveType,
4856 &fromX, &fromY, &toX, &toY, &promoChar);
4857 // end of long SAN patch
4859 (void) CoordsToAlgebraic(boards[moveNum - 1],
4860 PosFlags(moveNum - 1),
4861 fromY, fromX, toY, toX, promoChar,
4862 parseList[moveNum-1]);
4863 switch (MateTest(boards[moveNum], PosFlags(moveNum)) ) {
4869 if(!IS_SHOGI(gameInfo.variant))
4870 strcat(parseList[moveNum - 1], "+");
4873 case MT_STAINMATE: // [HGM] xq: for notation stalemate that wins counts as checkmate
4874 strcat(parseList[moveNum - 1], "#");
4877 strcat(parseList[moveNum - 1], " ");
4878 strcat(parseList[moveNum - 1], elapsed_time);
4879 /* currentMoveString is set as a side-effect of ParseOneMove */
4880 if(gameInfo.variant == VariantShogi && currentMoveString[4]) currentMoveString[4] = '^';
4881 safeStrCpy(moveList[moveNum - 1], currentMoveString, sizeof(moveList[moveNum - 1])/sizeof(moveList[moveNum - 1][0]));
4882 strcat(moveList[moveNum - 1], "\n");
4884 if(gameInfo.holdingsWidth && !appData.disguise && gameInfo.variant != VariantSuper && gameInfo.variant != VariantGreat
4885 && gameInfo.variant != VariantGrand&& gameInfo.variant != VariantSChess) // inherit info that ICS does not give from previous board
4886 for(k=0; k<ranks; k++) for(j=BOARD_LEFT; j<BOARD_RGHT; j++) {
4887 ChessSquare old, new = boards[moveNum][k][j];
4888 if(fromY == DROP_RANK && k==toY && j==toX) continue; // dropped pieces always stand for themselves
4889 old = (k==toY && j==toX) ? boards[moveNum-1][fromY][fromX] : boards[moveNum-1][k][j]; // trace back mover
4890 if(old == new) continue;
4891 if(old == PROMOTED new) boards[moveNum][k][j] = old; // prevent promoted pieces to revert to primordial ones
4892 else if(new == WhiteWazir || new == BlackWazir) {
4893 if(old < WhiteCannon || old >= BlackPawn && old < BlackCannon)
4894 boards[moveNum][k][j] = PROMOTED old; // choose correct type of Gold in promotion
4895 else boards[moveNum][k][j] = old; // preserve type of Gold
4896 } else if((old == WhitePawn || old == BlackPawn) && new != EmptySquare) // Pawn promotions (but not e.p.capture!)
4897 boards[moveNum][k][j] = PROMOTED new; // use non-primordial representation of chosen piece
4900 /* Move from ICS was illegal!? Punt. */
4901 if (appData.debugMode) {
4902 fprintf(debugFP, "Illegal move from ICS '%s'\n", move_str);
4903 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
4905 safeStrCpy(parseList[moveNum - 1], move_str, sizeof(parseList[moveNum - 1])/sizeof(parseList[moveNum - 1][0]));
4906 strcat(parseList[moveNum - 1], " ");
4907 strcat(parseList[moveNum - 1], elapsed_time);
4908 moveList[moveNum - 1][0] = NULLCHAR;
4909 fromX = fromY = toX = toY = -1;
4912 if (appData.debugMode) {
4913 fprintf(debugFP, "Move parsed to '%s'\n", parseList[moveNum - 1]);
4914 setbuf(debugFP, NULL);
4918 /* Send move to chess program (BEFORE animating it). */
4919 if (appData.zippyPlay && !newGame && newMove &&
4920 (!appData.getMoveList || backwardMostMove == 0) && first.initDone) {
4922 if ((gameMode == IcsPlayingWhite && WhiteOnMove(moveNum)) ||
4923 (gameMode == IcsPlayingBlack && !WhiteOnMove(moveNum))) {
4924 if (moveList[moveNum - 1][0] == NULLCHAR) {
4925 snprintf(str, MSG_SIZ, _("Couldn't parse move \"%s\" from ICS"),
4927 DisplayError(str, 0);
4929 if (first.sendTime) {
4930 SendTimeRemaining(&first, gameMode == IcsPlayingWhite);
4932 bookHit = SendMoveToBookUser(moveNum - 1, &first, FALSE); // [HGM] book
4933 if (firstMove && !bookHit) {
4935 if (first.useColors) {
4936 SendToProgram(gameMode == IcsPlayingWhite ?
4938 "black\ngo\n", &first);
4940 SendToProgram("go\n", &first);
4942 first.maybeThinking = TRUE;
4945 } else if (gameMode == IcsObserving || gameMode == IcsExamining) {
4946 if (moveList[moveNum - 1][0] == NULLCHAR) {
4947 snprintf(str, MSG_SIZ, _("Couldn't parse move \"%s\" from ICS"), move_str);
4948 DisplayError(str, 0);
4950 if(gameInfo.variant == currentlyInitializedVariant) // [HGM] refrain sending moves engine can't understand!
4951 SendMoveToProgram(moveNum - 1, &first);
4958 if (moveNum > 0 && !gotPremove && !appData.noGUI) {
4959 /* If move comes from a remote source, animate it. If it
4960 isn't remote, it will have already been animated. */
4961 if (!pausing && !ics_user_moved && prevMove == moveNum - 1) {
4962 AnimateMove(boards[moveNum - 1], fromX, fromY, toX, toY);
4964 if (!pausing && appData.highlightLastMove) {
4965 SetHighlights(fromX, fromY, toX, toY);
4969 /* Start the clocks */
4970 whiteFlag = blackFlag = FALSE;
4971 appData.clockMode = !(basetime == 0 && increment == 0);
4973 ics_clock_paused = TRUE;
4975 } else if (ticking == 1) {
4976 ics_clock_paused = FALSE;
4978 if (gameMode == IcsIdle ||
4979 relation == RELATION_OBSERVING_STATIC ||
4980 relation == RELATION_EXAMINING ||
4982 DisplayBothClocks();
4986 /* Display opponents and material strengths */
4987 if (gameInfo.variant != VariantBughouse &&
4988 gameInfo.variant != VariantCrazyhouse && !appData.noGUI) {
4989 if (tinyLayout || smallLayout) {
4990 if(gameInfo.variant == VariantNormal)
4991 snprintf(str, MSG_SIZ, "%s(%d) %s(%d) {%d %d}",
4992 gameInfo.white, white_stren, gameInfo.black, black_stren,
4993 basetime, increment);
4995 snprintf(str, MSG_SIZ, "%s(%d) %s(%d) {%d %d w%d}",
4996 gameInfo.white, white_stren, gameInfo.black, black_stren,
4997 basetime, increment, (int) gameInfo.variant);
4999 if(gameInfo.variant == VariantNormal)
5000 snprintf(str, MSG_SIZ, "%s (%d) %s %s (%d) {%d %d}",
5001 gameInfo.white, white_stren, _("vs."), gameInfo.black, black_stren,
5002 basetime, increment);
5004 snprintf(str, MSG_SIZ, "%s (%d) %s %s (%d) {%d %d %s}",
5005 gameInfo.white, white_stren, _("vs."), gameInfo.black, black_stren,
5006 basetime, increment, VariantName(gameInfo.variant));
5009 if (appData.debugMode) {
5010 fprintf(debugFP, "Display title '%s, gameInfo.variant = %d'\n", str, gameInfo.variant);
5015 /* Display the board */
5016 if (!pausing && !appData.noGUI) {
5018 if (appData.premove)
5020 ((gameMode == IcsPlayingWhite) && (WhiteOnMove(currentMove))) ||
5021 ((gameMode == IcsPlayingBlack) && (!WhiteOnMove(currentMove))))
5022 ClearPremoveHighlights();
5024 j = seekGraphUp; seekGraphUp = FALSE; // [HGM] seekgraph: when we draw a board, it overwrites the seek graph
5025 if(partnerUp) { flipView = originalFlip; partnerUp = FALSE; j = TRUE; } // [HGM] bughouse: restore view
5026 DrawPosition(j, boards[currentMove]);
5028 DisplayMove(moveNum - 1);
5029 if (appData.ringBellAfterMoves && /*!ics_user_moved*/ // [HGM] use absolute method to recognize own move
5030 !((gameMode == IcsPlayingWhite) && (!WhiteOnMove(moveNum)) ||
5031 (gameMode == IcsPlayingBlack) && (WhiteOnMove(moveNum)) ) ) {
5032 if(newMove) RingBell(); else PlayIcsUnfinishedSound();
5036 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
5038 if(bookHit) { // [HGM] book: simulate book reply
5039 static char bookMove[MSG_SIZ]; // a bit generous?
5041 programStats.nodes = programStats.depth = programStats.time =
5042 programStats.score = programStats.got_only_move = 0;
5043 sprintf(programStats.movelist, "%s (xbook)", bookHit);
5045 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
5046 strcat(bookMove, bookHit);
5047 HandleMachineMove(bookMove, &first);
5056 if (appData.icsActive && gameMode != IcsIdle && ics_gamenum > 0) {
5057 ics_getting_history = H_REQUESTED;
5058 snprintf(buf, MSG_SIZ, "%smoves %d\n", ics_prefix, ics_gamenum);
5064 SendToBoth (char *msg)
5065 { // to make it easy to keep two engines in step in dual analysis
5066 SendToProgram(msg, &first);
5067 if(second.analyzing) SendToProgram(msg, &second);
5071 AnalysisPeriodicEvent (int force)
5073 if (((programStats.ok_to_send == 0 || programStats.line_is_book)
5074 && !force) || !appData.periodicUpdates)
5077 /* Send . command to Crafty to collect stats */
5080 /* Don't send another until we get a response (this makes
5081 us stop sending to old Crafty's which don't understand
5082 the "." command (sending illegal cmds resets node count & time,
5083 which looks bad)) */
5084 programStats.ok_to_send = 0;
5088 ics_update_width (int new_width)
5090 ics_printf("set width %d\n", new_width);
5094 SendMoveToProgram (int moveNum, ChessProgramState *cps)
5098 if(moveList[moveNum][1] == '@' && moveList[moveNum][0] == '@') {
5099 if(gameInfo.variant == VariantLion || gameInfo.variant == VariantChuChess || gameInfo.variant == VariantChu) {
5100 sprintf(buf, "%s@@@@\n", cps->useUsermove ? "usermove " : "");
5101 SendToProgram(buf, cps);
5104 // null move in variant where engine does not understand it (for analysis purposes)
5105 SendBoard(cps, moveNum + 1); // send position after move in stead.
5108 if (cps->useUsermove) {
5109 SendToProgram("usermove ", cps);
5113 if ((space = strchr(parseList[moveNum], ' ')) != NULL) {
5114 int len = space - parseList[moveNum];
5115 memcpy(buf, parseList[moveNum], len);
5117 buf[len] = NULLCHAR;
5119 snprintf(buf, MSG_SIZ,"%s\n", parseList[moveNum]);
5121 SendToProgram(buf, cps);
5123 if(cps->alphaRank) { /* [HGM] shogi: temporarily convert to shogi coordinates before sending */
5124 AlphaRank(moveList[moveNum], 4);
5125 SendToProgram(moveList[moveNum], cps);
5126 AlphaRank(moveList[moveNum], 4); // and back
5128 /* Added by Tord: Send castle moves in "O-O" in FRC games if required by
5129 * the engine. It would be nice to have a better way to identify castle
5131 if(appData.fischerCastling && cps->useOOCastle) {
5132 int fromX = moveList[moveNum][0] - AAA;
5133 int fromY = moveList[moveNum][1] - ONE;
5134 int toX = moveList[moveNum][2] - AAA;
5135 int toY = moveList[moveNum][3] - ONE;
5136 if((boards[moveNum][fromY][fromX] == WhiteKing
5137 && boards[moveNum][toY][toX] == WhiteRook)
5138 || (boards[moveNum][fromY][fromX] == BlackKing
5139 && boards[moveNum][toY][toX] == BlackRook)) {
5140 if(toX > fromX) SendToProgram("O-O\n", cps);
5141 else SendToProgram("O-O-O\n", cps);
5143 else SendToProgram(moveList[moveNum], cps);
5145 if(moveList[moveNum][4] == ';') { // [HGM] lion: move is double-step over intermediate square
5146 char *m = moveList[moveNum];
5147 if((boards[moveNum][m[6]-ONE][m[5]-AAA] < BlackPawn) == (boards[moveNum][m[1]-ONE][m[0]-AAA] < BlackPawn)) // move is kludge to indicate castling
5148 snprintf(buf, MSG_SIZ, "%c%d%c%d,%c%d%c%d\n", m[0], m[1] - '0', // convert to two moves
5151 m[2] + (m[0] > m[5] ? 1 : -1), m[3] - '0');
5153 snprintf(buf, MSG_SIZ, "%c%d%c%d,%c%d%c%d\n", m[0], m[1] - '0', // convert to two moves
5157 SendToProgram(buf, cps);
5159 if(BOARD_HEIGHT > 10) { // [HGM] big: convert ranks to double-digit where needed
5160 if(moveList[moveNum][1] == '@' && (BOARD_HEIGHT < 16 || moveList[moveNum][0] <= 'Z')) { // drop move
5161 if(moveList[moveNum][0]== '@') snprintf(buf, MSG_SIZ, "@@@@\n"); else
5162 snprintf(buf, MSG_SIZ, "%c@%c%d%s", moveList[moveNum][0],
5163 moveList[moveNum][2], moveList[moveNum][3] - '0', moveList[moveNum]+4);
5165 snprintf(buf, MSG_SIZ, "%c%d%c%d%s", moveList[moveNum][0], moveList[moveNum][1] - '0',
5166 moveList[moveNum][2], moveList[moveNum][3] - '0', moveList[moveNum]+4);
5167 SendToProgram(buf, cps);
5169 else SendToProgram(moveList[moveNum], cps);
5170 /* End of additions by Tord */
5173 /* [HGM] setting up the opening has brought engine in force mode! */
5174 /* Send 'go' if we are in a mode where machine should play. */
5175 if( (moveNum == 0 && setboardSpoiledMachineBlack && cps == &first) &&
5176 (gameMode == TwoMachinesPlay ||
5178 gameMode == IcsPlayingBlack || gameMode == IcsPlayingWhite ||
5180 gameMode == MachinePlaysBlack || gameMode == MachinePlaysWhite) ) {
5181 SendToProgram("go\n", cps);
5182 if (appData.debugMode) {
5183 fprintf(debugFP, "(extra)\n");
5186 setboardSpoiledMachineBlack = 0;
5190 SendMoveToICS (ChessMove moveType, int fromX, int fromY, int toX, int toY, char promoChar)
5192 char user_move[MSG_SIZ];
5195 if(gameInfo.variant == VariantSChess && promoChar) {
5196 snprintf(suffix, 4, "=%c", toX == BOARD_WIDTH<<1 ? ToUpper(promoChar) : ToLower(promoChar));
5197 if(moveType == NormalMove) moveType = WhitePromotion; // kludge to do gating
5198 } else suffix[0] = NULLCHAR;
5202 snprintf(user_move, MSG_SIZ, _("say Internal error; bad moveType %d (%d,%d-%d,%d)"),
5203 (int)moveType, fromX, fromY, toX, toY);
5204 DisplayError(user_move + strlen("say "), 0);
5206 case WhiteKingSideCastle:
5207 case BlackKingSideCastle:
5208 case WhiteQueenSideCastleWild:
5209 case BlackQueenSideCastleWild:
5211 case WhiteHSideCastleFR:
5212 case BlackHSideCastleFR:
5214 snprintf(user_move, MSG_SIZ, "o-o%s\n", suffix);
5216 case WhiteQueenSideCastle:
5217 case BlackQueenSideCastle:
5218 case WhiteKingSideCastleWild:
5219 case BlackKingSideCastleWild:
5221 case WhiteASideCastleFR:
5222 case BlackASideCastleFR:
5224 snprintf(user_move, MSG_SIZ, "o-o-o%s\n",suffix);
5226 case WhiteNonPromotion:
5227 case BlackNonPromotion:
5228 sprintf(user_move, "%c%c%c%c==\n", AAA + fromX, ONE + fromY, AAA + toX, ONE + toY);
5230 case WhitePromotion:
5231 case BlackPromotion:
5232 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier ||
5233 gameInfo.variant == VariantMakruk)
5234 snprintf(user_move, MSG_SIZ, "%c%c%c%c=%c\n",
5235 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
5236 PieceToChar(WhiteFerz));
5237 else if(gameInfo.variant == VariantGreat)
5238 snprintf(user_move, MSG_SIZ,"%c%c%c%c=%c\n",
5239 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
5240 PieceToChar(WhiteMan));
5242 snprintf(user_move, MSG_SIZ, "%c%c%c%c=%c\n",
5243 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
5249 snprintf(user_move, MSG_SIZ, "%c@%c%c\n",
5250 ToUpper(PieceToChar((ChessSquare) fromX)),
5251 AAA + toX, ONE + toY);
5253 case IllegalMove: /* could be a variant we don't quite understand */
5254 if(fromY == DROP_RANK) goto drop; // We need 'IllegalDrop' move type?
5256 case WhiteCapturesEnPassant:
5257 case BlackCapturesEnPassant:
5258 snprintf(user_move, MSG_SIZ,"%c%c%c%c\n",
5259 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY);
5262 SendToICS(user_move);
5263 if(appData.keepAlive) // [HGM] alive: schedule sending of dummy 'date' command
5264 ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
5269 { // [HGM] upload: send entire stored game to ICS as long-algebraic moves.
5270 int i, last = forwardMostMove; // make sure ICS reply cannot pre-empt us by clearing fmm
5271 static char *castlingStrings[4] = { "none", "kside", "qside", "both" };
5272 if(gameMode == IcsObserving || gameMode == IcsPlayingBlack || gameMode == IcsPlayingWhite) {
5273 DisplayError(_("You cannot do this while you are playing or observing"), 0);
5276 if(gameMode != IcsExamining) { // is this ever not the case?
5277 char buf[MSG_SIZ], *p, *fen, command[MSG_SIZ], bsetup = 0;
5279 if(ics_type == ICS_ICC) { // on ICC match ourselves in applicable variant
5280 snprintf(command,MSG_SIZ, "match %s", ics_handle);
5281 } else { // on FICS we must first go to general examine mode
5282 safeStrCpy(command, "examine\nbsetup", sizeof(command)/sizeof(command[0])); // and specify variant within it with bsetups
5284 if(gameInfo.variant != VariantNormal) {
5285 // try figure out wild number, as xboard names are not always valid on ICS
5286 for(i=1; i<=36; i++) {
5287 snprintf(buf, MSG_SIZ, "wild/%d", i);
5288 if(StringToVariant(buf) == gameInfo.variant) break;
5290 if(i<=36 && ics_type == ICS_ICC) snprintf(buf, MSG_SIZ,"%s w%d\n", command, i);
5291 else if(i == 22) snprintf(buf,MSG_SIZ, "%s fr\n", command);
5292 else snprintf(buf, MSG_SIZ,"%s %s\n", command, VariantName(gameInfo.variant));
5293 } else snprintf(buf, MSG_SIZ,"%s\n", ics_type == ICS_ICC ? command : "examine\n"); // match yourself or examine
5294 SendToICS(ics_prefix);
5296 if(startedFromSetupPosition || backwardMostMove != 0) {
5297 fen = PositionToFEN(backwardMostMove, NULL, 1);
5298 if(ics_type == ICS_ICC) { // on ICC we can simply send a complete FEN to set everything
5299 snprintf(buf, MSG_SIZ,"loadfen %s\n", fen);
5301 } else { // FICS: everything has to set by separate bsetup commands
5302 p = strchr(fen, ' '); p[0] = NULLCHAR; // cut after board
5303 snprintf(buf, MSG_SIZ,"bsetup fen %s\n", fen);
5305 if(!WhiteOnMove(backwardMostMove)) {
5306 SendToICS("bsetup tomove black\n");
5308 i = (strchr(p+3, 'K') != NULL) + 2*(strchr(p+3, 'Q') != NULL);
5309 snprintf(buf, MSG_SIZ,"bsetup wcastle %s\n", castlingStrings[i]);
5311 i = (strchr(p+3, 'k') != NULL) + 2*(strchr(p+3, 'q') != NULL);
5312 snprintf(buf, MSG_SIZ, "bsetup bcastle %s\n", castlingStrings[i]);
5314 i = boards[backwardMostMove][EP_STATUS];
5315 if(i >= 0) { // set e.p.
5316 snprintf(buf, MSG_SIZ,"bsetup eppos %c\n", i+AAA);
5322 if(bsetup || ics_type != ICS_ICC && gameInfo.variant != VariantNormal)
5323 SendToICS("bsetup done\n"); // switch to normal examining.
5325 for(i = backwardMostMove; i<last; i++) {
5327 snprintf(buf, sizeof(buf)/sizeof(buf[0]),"%s\n", parseList[i]);
5328 if((*buf == 'b' || *buf == 'B') && buf[1] == 'x') { // work-around for stupid FICS bug, which thinks bxc3 can be a Bishop move
5329 int len = strlen(moveList[i]);
5330 snprintf(buf, sizeof(buf)/sizeof(buf[0]),"%s", moveList[i]); // use long algebraic
5331 if(!isdigit(buf[len-2])) snprintf(buf+len-2, 20-len, "=%c\n", ToUpper(buf[len-2])); // promotion must have '=' in ICS format
5335 SendToICS(ics_prefix);
5336 SendToICS(ics_type == ICS_ICC ? "tag result Game in progress\n" : "commit\n");
5339 int killX = -1, killY = -1, kill2X = -1, kill2Y = -1; // [HGM] lion: used for passing e.p. capture square to MakeMove
5343 CoordsToComputerAlgebraic (int rf, int ff, int rt, int ft, char promoChar, char move[9])
5345 if (rf == DROP_RANK) {
5346 if(ff == EmptySquare) sprintf(move, "@@@@\n"); else // [HGM] pass
5347 sprintf(move, "%c@%c%c\n",
5348 ToUpper(PieceToChar((ChessSquare) ff)), AAA + ft, ONE + rt);
5350 if (promoChar == 'x' || promoChar == NULLCHAR) {
5351 sprintf(move, "%c%c%c%c\n",
5352 AAA + ff, ONE + rf, AAA + ft, ONE + rt);
5353 if(killX >= 0 && killY >= 0) {
5354 sprintf(move+4, ";%c%c\n", AAA + killX, ONE + killY);
5355 if(kill2X >= 0 && kill2Y >= 0) sprintf(move+7, "%c%c\n", AAA + killX, ONE + killY);
5358 sprintf(move, "%c%c%c%c%c\n",
5359 AAA + ff, ONE + rf, AAA + ft, ONE + rt, promoChar);
5365 ProcessICSInitScript (FILE *f)
5369 while (fgets(buf, MSG_SIZ, f)) {
5370 SendToICSDelayed(buf,(long)appData.msLoginDelay);
5377 static int lastX, lastY, lastLeftX, lastLeftY, selectFlag;
5379 static ClickType lastClickType;
5382 Partner (ChessSquare *p)
5383 { // change piece into promotion partner if one shogi-promotes to the other
5384 int stride = gameInfo.variant == VariantChu ? 22 : 11;
5385 ChessSquare partner;
5386 partner = (*p/stride & 1 ? *p - stride : *p + stride);
5387 if(PieceToChar(*p) != '+' && PieceToChar(partner) != '+') return 0;
5395 ChessSquare king = WhiteKing, pawn = WhitePawn, last = promoSweep;
5396 static int toggleFlag;
5397 if(gameInfo.variant == VariantKnightmate) king = WhiteUnicorn;
5398 if(gameInfo.variant == VariantSuicide || gameInfo.variant == VariantGiveaway) king = EmptySquare;
5399 if(promoSweep >= BlackPawn) king = WHITE_TO_BLACK king, pawn = WHITE_TO_BLACK pawn;
5400 if(gameInfo.variant == VariantSpartan && pawn == BlackPawn) pawn = BlackLance, king = EmptySquare;
5401 if(fromY != BOARD_HEIGHT-2 && fromY != 1 && gameInfo.variant != VariantChuChess) pawn = EmptySquare;
5402 if(!step) toggleFlag = Partner(&last); // piece has shogi-promotion
5404 if(step && !(toggleFlag && Partner(&promoSweep))) promoSweep -= step;
5405 if(promoSweep == EmptySquare) promoSweep = BlackPawn; // wrap
5406 else if((int)promoSweep == -1) promoSweep = WhiteKing;
5407 else if(promoSweep == BlackPawn && step < 0 && !toggleFlag) promoSweep = WhitePawn;
5408 else if(promoSweep == WhiteKing && step > 0 && !toggleFlag) promoSweep = BlackKing;
5409 if(!step) step = -1;
5410 } while(PieceToChar(promoSweep) == '.' || PieceToChar(promoSweep) == '~' || promoSweep == pawn ||
5411 !toggleFlag && PieceToChar(promoSweep) == '+' || // skip promoted versions of other
5412 appData.testLegality && (promoSweep == king || gameInfo.variant != VariantChuChess &&
5413 (promoSweep == WhiteLion || promoSweep == BlackLion)));
5415 int victim = boards[currentMove][toY][toX];
5416 boards[currentMove][toY][toX] = promoSweep;
5417 DrawPosition(FALSE, boards[currentMove]);
5418 boards[currentMove][toY][toX] = victim;
5420 ChangeDragPiece(promoSweep);
5424 PromoScroll (int x, int y)
5428 if(promoSweep == EmptySquare || !appData.sweepSelect) return FALSE;
5429 if(abs(x - lastX) < 25 && abs(y - lastY) < 25) return FALSE;
5430 if( y > lastY + 2 ) step = -1; else if(y < lastY - 2) step = 1;
5431 if(!step) return FALSE;
5432 lastX = x; lastY = y;
5433 if((promoSweep < BlackPawn) == flipView) step = -step;
5434 if(step > 0) selectFlag = 1;
5435 if(!selectFlag) Sweep(step);
5440 NextPiece (int step)
5442 ChessSquare piece = boards[currentMove][toY][toX];
5445 if(pieceSweep == EmptySquare) pieceSweep = WhitePawn; // wrap
5446 if((int)pieceSweep == -1) pieceSweep = BlackKing;
5447 if(!step) step = -1;
5448 } while(PieceToChar(pieceSweep) == '.');
5449 boards[currentMove][toY][toX] = pieceSweep;
5450 DrawPosition(FALSE, boards[currentMove]);
5451 boards[currentMove][toY][toX] = piece;
5453 /* [HGM] Shogi move preprocessor: swap digits for letters, vice versa */
5455 AlphaRank (char *move, int n)
5457 // char *p = move, c; int x, y;
5459 if (appData.debugMode) {
5460 fprintf(debugFP, "alphaRank(%s,%d)\n", move, n);
5464 move[2]>='0' && move[2]<='9' &&
5465 move[3]>='a' && move[3]<='x' ) {
5467 move[2] = BOARD_RGHT -1 - (move[2]-'1') + AAA;
5468 move[3] = BOARD_HEIGHT-1 - (move[3]-'a') + ONE;
5470 if(move[0]>='0' && move[0]<='9' &&
5471 move[1]>='a' && move[1]<='x' &&
5472 move[2]>='0' && move[2]<='9' &&
5473 move[3]>='a' && move[3]<='x' ) {
5474 /* input move, Shogi -> normal */
5475 move[0] = BOARD_RGHT -1 - (move[0]-'1') + AAA;
5476 move[1] = BOARD_HEIGHT-1 - (move[1]-'a') + ONE;
5477 move[2] = BOARD_RGHT -1 - (move[2]-'1') + AAA;
5478 move[3] = BOARD_HEIGHT-1 - (move[3]-'a') + ONE;
5481 move[3]>='0' && move[3]<='9' &&
5482 move[2]>='a' && move[2]<='x' ) {
5484 move[2] = BOARD_RGHT - 1 - (move[2]-AAA) + '1';
5485 move[3] = BOARD_HEIGHT-1 - (move[3]-ONE) + 'a';
5488 move[0]>='a' && move[0]<='x' &&
5489 move[3]>='0' && move[3]<='9' &&
5490 move[2]>='a' && move[2]<='x' ) {
5491 /* output move, normal -> Shogi */
5492 move[0] = BOARD_RGHT - 1 - (move[0]-AAA) + '1';
5493 move[1] = BOARD_HEIGHT-1 - (move[1]-ONE) + 'a';
5494 move[2] = BOARD_RGHT - 1 - (move[2]-AAA) + '1';
5495 move[3] = BOARD_HEIGHT-1 - (move[3]-ONE) + 'a';
5496 if(move[4] == PieceToChar(BlackQueen)) move[4] = '+';
5498 if (appData.debugMode) {
5499 fprintf(debugFP, " out = '%s'\n", move);
5503 char yy_textstr[8000];
5505 /* Parser for moves from gnuchess, ICS, or user typein box */
5507 ParseOneMove (char *move, int moveNum, ChessMove *moveType, int *fromX, int *fromY, int *toX, int *toY, char *promoChar)
5509 *moveType = yylexstr(moveNum, move, yy_textstr, sizeof yy_textstr);
5511 switch (*moveType) {
5512 case WhitePromotion:
5513 case BlackPromotion:
5514 case WhiteNonPromotion:
5515 case BlackNonPromotion:
5518 case WhiteCapturesEnPassant:
5519 case BlackCapturesEnPassant:
5520 case WhiteKingSideCastle:
5521 case WhiteQueenSideCastle:
5522 case BlackKingSideCastle:
5523 case BlackQueenSideCastle:
5524 case WhiteKingSideCastleWild:
5525 case WhiteQueenSideCastleWild:
5526 case BlackKingSideCastleWild:
5527 case BlackQueenSideCastleWild:
5528 /* Code added by Tord: */
5529 case WhiteHSideCastleFR:
5530 case WhiteASideCastleFR:
5531 case BlackHSideCastleFR:
5532 case BlackASideCastleFR:
5533 /* End of code added by Tord */
5534 case IllegalMove: /* bug or odd chess variant */
5535 if(currentMoveString[1] == '@') { // illegal drop
5536 *fromX = WhiteOnMove(moveNum) ?
5537 (int) CharToPiece(ToUpper(currentMoveString[0])) :
5538 (int) CharToPiece(ToLower(currentMoveString[0]));
5541 *fromX = currentMoveString[0] - AAA;
5542 *fromY = currentMoveString[1] - ONE;
5543 *toX = currentMoveString[2] - AAA;
5544 *toY = currentMoveString[3] - ONE;
5545 *promoChar = currentMoveString[4];
5546 if (*fromX < BOARD_LEFT || *fromX >= BOARD_RGHT || *fromY < 0 || *fromY >= BOARD_HEIGHT ||
5547 *toX < BOARD_LEFT || *toX >= BOARD_RGHT || *toY < 0 || *toY >= BOARD_HEIGHT) {
5548 if (appData.debugMode) {
5549 fprintf(debugFP, "Off-board move (%d,%d)-(%d,%d)%c, type = %d\n", *fromX, *fromY, *toX, *toY, *promoChar, *moveType);
5551 *fromX = *fromY = *toX = *toY = 0;
5554 if (appData.testLegality) {
5555 return (*moveType != IllegalMove);
5557 return !(*fromX == *toX && *fromY == *toY && killX < 0) && boards[moveNum][*fromY][*fromX] != EmptySquare &&
5558 // [HGM] lion: if this is a double move we are less critical
5559 WhiteOnMove(moveNum) == (boards[moveNum][*fromY][*fromX] < BlackPawn);
5564 *fromX = *moveType == WhiteDrop ?
5565 (int) CharToPiece(ToUpper(currentMoveString[0])) :
5566 (int) CharToPiece(ToLower(currentMoveString[0]));
5569 *toX = currentMoveString[2] - AAA;
5570 *toY = currentMoveString[3] - ONE;
5571 *promoChar = NULLCHAR;
5575 case ImpossibleMove:
5585 if (appData.debugMode) {
5586 fprintf(debugFP, "Impossible move %s, type = %d\n", currentMoveString, *moveType);
5589 *fromX = *fromY = *toX = *toY = 0;
5590 *promoChar = NULLCHAR;
5595 Boolean pushed = FALSE;
5596 char *lastParseAttempt;
5599 ParsePV (char *pv, Boolean storeComments, Boolean atEnd)
5600 { // Parse a string of PV moves, and append to current game, behind forwardMostMove
5601 int fromX, fromY, toX, toY; char promoChar;
5606 lastParseAttempt = pv; if(!*pv) return; // turns out we crash when we parse an empty PV
5607 if ((gameMode == AnalyzeMode || gameMode == AnalyzeFile) && currentMove < forwardMostMove) {
5608 PushInner(currentMove, forwardMostMove); // [HGM] engine might not be thinking on forwardMost position!
5611 endPV = forwardMostMove;
5613 while(*pv == ' ' || *pv == '\n' || *pv == '\t') pv++; // must still read away whitespace
5614 if(nr == 0 && !storeComments && *pv == '(') pv++; // first (ponder) move can be in parentheses
5615 lastParseAttempt = pv;
5616 valid = ParseOneMove(pv, endPV, &moveType, &fromX, &fromY, &toX, &toY, &promoChar);
5617 if(!valid && nr == 0 &&
5618 ParseOneMove(pv, endPV-1, &moveType, &fromX, &fromY, &toX, &toY, &promoChar)){
5619 nr++; moveType = Comment; // First move has been played; kludge to make sure we continue
5620 // Hande case where played move is different from leading PV move
5621 CopyBoard(boards[endPV+1], boards[endPV-1]); // tentatively unplay last game move
5622 CopyBoard(boards[endPV+2], boards[endPV-1]); // and play first move of PV
5623 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[endPV+2]);
5624 if(!CompareBoards(boards[endPV], boards[endPV+2])) {
5625 endPV += 2; // if position different, keep this
5626 moveList[endPV-1][0] = fromX + AAA;
5627 moveList[endPV-1][1] = fromY + ONE;
5628 moveList[endPV-1][2] = toX + AAA;
5629 moveList[endPV-1][3] = toY + ONE;
5630 parseList[endPV-1][0] = NULLCHAR;
5631 safeStrCpy(moveList[endPV-2], "_0_0", sizeof(moveList[endPV-2])/sizeof(moveList[endPV-2][0])); // suppress premove highlight on takeback move
5634 pv = strstr(pv, yy_textstr) + strlen(yy_textstr); // skip what we parsed
5635 if(nr == 0 && !storeComments && *pv == ')') pv++; // closing parenthesis of ponder move;
5636 if(moveType == Comment && storeComments) AppendComment(endPV, yy_textstr, FALSE);
5637 if(moveType == Comment || moveType == NAG || moveType == ElapsedTime) {
5638 valid++; // allow comments in PV
5642 if(endPV+1 > framePtr) break; // no space, truncate
5645 CopyBoard(boards[endPV], boards[endPV-1]);
5646 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[endPV]);
5647 CoordsToComputerAlgebraic(fromY, fromX, toY, toX, promoChar, moveList[endPV - 1]);
5648 strncat(moveList[endPV-1], "\n", MOVE_LEN);
5649 CoordsToAlgebraic(boards[endPV - 1],
5650 PosFlags(endPV - 1),
5651 fromY, fromX, toY, toX, promoChar,
5652 parseList[endPV - 1]);
5654 if(atEnd == 2) return; // used hidden, for PV conversion
5655 currentMove = (atEnd || endPV == forwardMostMove) ? endPV : forwardMostMove + 1;
5656 if(currentMove == forwardMostMove) ClearPremoveHighlights(); else
5657 SetPremoveHighlights(moveList[currentMove-1][0]-AAA, moveList[currentMove-1][1]-ONE,
5658 moveList[currentMove-1][2]-AAA, moveList[currentMove-1][3]-ONE);
5659 DrawPosition(TRUE, boards[currentMove]);
5663 MultiPV (ChessProgramState *cps)
5664 { // check if engine supports MultiPV, and if so, return the number of the option that sets it
5666 for(i=0; i<cps->nrOptions; i++)
5667 if(!strcmp(cps->option[i].name, "MultiPV") && cps->option[i].type == Spin)
5672 Boolean extendGame; // signals to UnLoadPV() if walked part of PV has to be appended to game
5675 LoadMultiPV (int x, int y, char *buf, int index, int *start, int *end, int pane)
5677 int startPV, multi, lineStart, origIndex = index;
5678 char *p, buf2[MSG_SIZ];
5679 ChessProgramState *cps = (pane ? &second : &first);
5681 if(index < 0 || index >= strlen(buf)) return FALSE; // sanity
5682 lastX = x; lastY = y;
5683 while(index > 0 && buf[index-1] != '\n') index--; // beginning of line
5684 lineStart = startPV = index;
5685 while(buf[index] != '\n') if(buf[index++] == '\t') startPV = index;
5686 if(index == startPV && (p = StrCaseStr(buf+index, "PV="))) startPV = p - buf + 3;
5688 do{ while(buf[index] && buf[index] != '\n') index++;
5689 } while(buf[index] == '\n' && buf[index+1] == '\\' && buf[index+2] == ' ' && index++); // join kibitzed PV continuation line
5691 if(lineStart == 0 && gameMode == AnalyzeMode && (multi = MultiPV(cps)) >= 0) {
5692 int n = cps->option[multi].value;
5693 if(origIndex > 17 && origIndex < 24) { if(n>1) n--; } else if(origIndex > index - 6) n++;
5694 snprintf(buf2, MSG_SIZ, "option MultiPV=%d\n", n);
5695 if(cps->option[multi].value != n) SendToProgram(buf2, cps);
5696 cps->option[multi].value = n;
5699 } else if(strstr(buf+lineStart, "exclude:") == buf+lineStart) { // exclude moves clicked
5700 ExcludeClick(origIndex - lineStart);
5702 } else if(!strncmp(buf+lineStart, "dep\t", 4)) { // column headers clicked
5703 Collapse(origIndex - lineStart);
5706 ParsePV(buf+startPV, FALSE, gameMode != AnalyzeMode);
5707 *start = startPV; *end = index-1;
5708 extendGame = (gameMode == AnalyzeMode && appData.autoExtend && origIndex - startPV < 5);
5715 static char buf[10*MSG_SIZ];
5716 int i, k=0, savedEnd=endPV, saveFMM = forwardMostMove;
5718 if(forwardMostMove < endPV) PushInner(forwardMostMove, endPV); // shelve PV of PV-walk
5719 ParsePV(pv, FALSE, 2); // this appends PV to game, suppressing any display of it
5720 for(i = forwardMostMove; i<endPV; i++){
5721 if(i&1) snprintf(buf+k, 10*MSG_SIZ-k, "%s ", parseList[i]);
5722 else snprintf(buf+k, 10*MSG_SIZ-k, "%d. %s ", i/2 + 1, parseList[i]);
5725 snprintf(buf+k, 10*MSG_SIZ-k, "%s", lastParseAttempt); // if we ran into stuff that could not be parsed, print it verbatim
5726 if(pushed) { PopInner(0); pushed = FALSE; } // restore game continuation shelved by ParsePV
5727 if(forwardMostMove < savedEnd) { PopInner(0); forwardMostMove = saveFMM; } // PopInner would set fmm to endPV!
5733 LoadPV (int x, int y)
5734 { // called on right mouse click to load PV
5735 int which = gameMode == TwoMachinesPlay && (WhiteOnMove(forwardMostMove) == (second.twoMachinesColor[0] == 'w'));
5736 lastX = x; lastY = y;
5737 ParsePV(lastPV[which], FALSE, TRUE); // load the PV of the thinking engine in the boards array.
5745 int oldFMM = forwardMostMove; // N.B.: this was currentMove before PV was loaded!
5746 if(endPV < 0) return;
5747 if(appData.autoCopyPV) CopyFENToClipboard();
5749 if(extendGame && currentMove > forwardMostMove) {
5750 Boolean saveAnimate = appData.animate;
5752 if(shiftKey && storedGames < MAX_VARIATIONS-2) { // wants to start variation, and there is space
5753 if(storedGames == 1) GreyRevert(FALSE); // we already pushed the tail, so just make it official
5754 } else storedGames--; // abandon shelved tail of original game
5757 forwardMostMove = currentMove;
5758 currentMove = oldFMM;
5759 appData.animate = FALSE;
5760 ToNrEvent(forwardMostMove);
5761 appData.animate = saveAnimate;
5763 currentMove = forwardMostMove;
5764 if(pushed) { PopInner(0); pushed = FALSE; } // restore shelved game continuation
5765 ClearPremoveHighlights();
5766 DrawPosition(TRUE, boards[currentMove]);
5770 MovePV (int x, int y, int h)
5771 { // step through PV based on mouse coordinates (called on mouse move)
5772 int margin = h>>3, step = 0, threshold = (pieceSweep == EmptySquare ? 10 : 15);
5774 // we must somehow check if right button is still down (might be released off board!)
5775 if(endPV < 0 && pieceSweep == EmptySquare) return; // needed in XBoard because lastX/Y is shared :-(
5776 if(abs(x - lastX) < threshold && abs(y - lastY) < threshold) return;
5777 if( y > lastY + 2 ) step = -1; else if(y < lastY - 2) step = 1;
5779 lastX = x; lastY = y;
5781 if(pieceSweep != EmptySquare) { NextPiece(step); return; }
5782 if(endPV < 0) return;
5783 if(y < margin) step = 1; else
5784 if(y > h - margin) step = -1;
5785 if(currentMove + step > endPV || currentMove + step < forwardMostMove) step = 0;
5786 currentMove += step;
5787 if(currentMove == forwardMostMove) ClearPremoveHighlights(); else
5788 SetPremoveHighlights(moveList[currentMove-1][0]-AAA, moveList[currentMove-1][1]-ONE,
5789 moveList[currentMove-1][2]-AAA, moveList[currentMove-1][3]-ONE);
5790 DrawPosition(FALSE, boards[currentMove]);
5794 // [HGM] shuffle: a general way to suffle opening setups, applicable to arbitrary variants.
5795 // All positions will have equal probability, but the current method will not provide a unique
5796 // numbering scheme for arrays that contain 3 or more pieces of the same kind.
5802 int piecesLeft[(int)BlackPawn];
5803 int seed, nrOfShuffles;
5806 GetPositionNumber ()
5807 { // sets global variable seed
5810 seed = appData.defaultFrcPosition;
5811 if(seed < 0) { // randomize based on time for negative FRC position numbers
5812 for(i=0; i<50; i++) seed += random();
5813 seed = random() ^ random() >> 8 ^ random() << 8;
5814 if(seed<0) seed = -seed;
5819 put (Board board, int pieceType, int rank, int n, int shade)
5820 // put the piece on the (n-1)-th empty squares of the given shade
5824 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) {
5825 if( (((i-BOARD_LEFT)&1)+1) & shade && board[rank][i] == EmptySquare && n-- == 0) {
5826 board[rank][i] = (ChessSquare) pieceType;
5827 squaresLeft[((i-BOARD_LEFT)&1) + 1]--;
5829 piecesLeft[pieceType]--;
5838 AddOnePiece (Board board, int pieceType, int rank, int shade)
5839 // calculate where the next piece goes, (any empty square), and put it there
5843 i = seed % squaresLeft[shade];
5844 nrOfShuffles *= squaresLeft[shade];
5845 seed /= squaresLeft[shade];
5846 put(board, pieceType, rank, i, shade);
5850 AddTwoPieces (Board board, int pieceType, int rank)
5851 // calculate where the next 2 identical pieces go, (any empty square), and put it there
5853 int i, n=squaresLeft[ANY], j=n-1, k;
5855 k = n*(n-1)/2; // nr of possibilities, not counting permutations
5856 i = seed % k; // pick one
5859 while(i >= j) i -= j--;
5860 j = n - 1 - j; i += j;
5861 put(board, pieceType, rank, j, ANY);
5862 put(board, pieceType, rank, i, ANY);
5866 SetUpShuffle (Board board, int number)
5870 GetPositionNumber(); nrOfShuffles = 1;
5872 squaresLeft[DARK] = (BOARD_RGHT - BOARD_LEFT + 1)/2;
5873 squaresLeft[ANY] = BOARD_RGHT - BOARD_LEFT;
5874 squaresLeft[LITE] = squaresLeft[ANY] - squaresLeft[DARK];
5876 for(p = 0; p<=(int)WhiteKing; p++) piecesLeft[p] = 0;
5878 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // count pieces and clear board
5879 p = (int) board[0][i];
5880 if(p < (int) BlackPawn) piecesLeft[p] ++;
5881 board[0][i] = EmptySquare;
5884 if(PosFlags(0) & F_ALL_CASTLE_OK) {
5885 // shuffles restricted to allow normal castling put KRR first
5886 if(piecesLeft[(int)WhiteKing]) // King goes rightish of middle
5887 put(board, WhiteKing, 0, (gameInfo.boardWidth+1)/2, ANY);
5888 else if(piecesLeft[(int)WhiteUnicorn]) // in Knightmate Unicorn castles
5889 put(board, WhiteUnicorn, 0, (gameInfo.boardWidth+1)/2, ANY);
5890 if(piecesLeft[(int)WhiteRook]) // First supply a Rook for K-side castling
5891 put(board, WhiteRook, 0, gameInfo.boardWidth-2, ANY);
5892 if(piecesLeft[(int)WhiteRook]) // Then supply a Rook for Q-side castling
5893 put(board, WhiteRook, 0, 0, ANY);
5894 // in variants with super-numerary Kings and Rooks, we leave these for the shuffle
5897 if(((BOARD_RGHT-BOARD_LEFT) & 1) == 0)
5898 // only for even boards make effort to put pairs of colorbound pieces on opposite colors
5899 for(p = (int) WhiteKing; p > (int) WhitePawn; p--) {
5900 if(p != (int) WhiteBishop && p != (int) WhiteFerz && p != (int) WhiteAlfil) continue;
5901 while(piecesLeft[p] >= 2) {
5902 AddOnePiece(board, p, 0, LITE);
5903 AddOnePiece(board, p, 0, DARK);
5905 // Odd color-bound pieces are shuffled with the rest (to not run out of paired squares)
5908 for(p = (int) WhiteKing - 2; p > (int) WhitePawn; p--) {
5909 // Remaining pieces (non-colorbound, or odd color bound) can be put anywhere
5910 // but we leave King and Rooks for last, to possibly obey FRC restriction
5911 if(p == (int)WhiteRook) continue;
5912 while(piecesLeft[p] >= 2) AddTwoPieces(board, p, 0); // add in pairs, for not counting permutations
5913 if(piecesLeft[p]) AddOnePiece(board, p, 0, ANY); // add the odd piece
5916 // now everything is placed, except perhaps King (Unicorn) and Rooks
5918 if(PosFlags(0) & F_FRC_TYPE_CASTLING) {
5919 // Last King gets castling rights
5920 while(piecesLeft[(int)WhiteUnicorn]) {
5921 i = put(board, WhiteUnicorn, 0, piecesLeft[(int)WhiteRook]/2, ANY);
5922 initialRights[2] = initialRights[5] = board[CASTLING][2] = board[CASTLING][5] = i;
5925 while(piecesLeft[(int)WhiteKing]) {
5926 i = put(board, WhiteKing, 0, piecesLeft[(int)WhiteRook]/2, ANY);
5927 initialRights[2] = initialRights[5] = board[CASTLING][2] = board[CASTLING][5] = i;
5932 while(piecesLeft[(int)WhiteKing]) AddOnePiece(board, WhiteKing, 0, ANY);
5933 while(piecesLeft[(int)WhiteUnicorn]) AddOnePiece(board, WhiteUnicorn, 0, ANY);
5936 // Only Rooks can be left; simply place them all
5937 while(piecesLeft[(int)WhiteRook]) {
5938 i = put(board, WhiteRook, 0, 0, ANY);
5939 if(PosFlags(0) & F_FRC_TYPE_CASTLING) { // first and last Rook get FRC castling rights
5942 initialRights[1] = initialRights[4] = board[CASTLING][1] = board[CASTLING][4] = i;
5944 initialRights[0] = initialRights[3] = board[CASTLING][0] = board[CASTLING][3] = i;
5947 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // copy black from white
5948 board[BOARD_HEIGHT-1][i] = (int) board[0][i] < BlackPawn ? WHITE_TO_BLACK board[0][i] : EmptySquare;
5951 if(number >= 0) appData.defaultFrcPosition %= nrOfShuffles; // normalize
5955 ptclen (const char *s, char *escapes)
5958 if(!*escapes) return strlen(s);
5959 while(*s) n += (*s != ':' && !strchr(escapes, *s)), s++;
5964 SetCharTableEsc (unsigned char *table, const char * map, char * escapes)
5965 /* [HGM] moved here from winboard.c because of its general usefulness */
5966 /* Basically a safe strcpy that uses the last character as King */
5968 int result = FALSE; int NrPieces;
5970 if( map != NULL && (NrPieces=ptclen(map, escapes)) <= (int) EmptySquare
5971 && NrPieces >= 12 && !(NrPieces&1)) {
5972 int i, j = 0; /* [HGM] Accept even length from 12 to 88 */
5974 for( i=0; i<(int) EmptySquare; i++ ) table[i] = '.';
5975 for( i=0; i<NrPieces/2-1; i++ ) {
5977 if(map[j] == ':' && *escapes) i = CHUPROMOTED WhitePawn, j++;
5978 table[i] = map[j++];
5979 if(p = strchr(escapes, map[j])) j++, table[i] += 64*(p - escapes + 1);
5981 table[(int) WhiteKing] = map[j++];
5982 for( i=0; i<NrPieces/2-1; i++ ) {
5984 if(map[j] == ':' && *escapes) i = CHUPROMOTED WhitePawn, j++;
5985 table[WHITE_TO_BLACK i] = map[j++];
5986 if(p = strchr(escapes, map[j])) j++, table[WHITE_TO_BLACK i] += 64*(p - escapes + 1);
5988 table[(int) BlackKing] = map[j++];
5997 SetCharTable (unsigned char *table, const char * map)
5999 return SetCharTableEsc(table, map, "");
6003 Prelude (Board board)
6004 { // [HGM] superchess: random selection of exo-pieces
6005 int i, j, k; ChessSquare p;
6006 static ChessSquare exoPieces[4] = { WhiteAngel, WhiteMarshall, WhiteSilver, WhiteLance };
6008 GetPositionNumber(); // use FRC position number
6010 if(appData.pieceToCharTable != NULL) { // select pieces to participate from given char table
6011 SetCharTable(pieceToChar, appData.pieceToCharTable);
6012 for(i=(int)WhiteQueen+1, j=0; i<(int)WhiteKing && j<4; i++)
6013 if(PieceToChar((ChessSquare)i) != '.') exoPieces[j++] = (ChessSquare) i;
6016 j = seed%4; seed /= 4;
6017 p = board[0][BOARD_LEFT+j]; board[0][BOARD_LEFT+j] = EmptySquare; k = PieceToNumber(p);
6018 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
6019 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
6020 j = seed%3 + (seed%3 >= j); seed /= 3;
6021 p = board[0][BOARD_LEFT+j]; board[0][BOARD_LEFT+j] = EmptySquare; k = PieceToNumber(p);
6022 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
6023 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
6024 j = seed%3; seed /= 3;
6025 p = board[0][BOARD_LEFT+j+5]; board[0][BOARD_LEFT+j+5] = EmptySquare; k = PieceToNumber(p);
6026 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
6027 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
6028 j = seed%2 + (seed%2 >= j); seed /= 2;
6029 p = board[0][BOARD_LEFT+j+5]; board[0][BOARD_LEFT+j+5] = EmptySquare; k = PieceToNumber(p);
6030 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
6031 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
6032 j = seed%4; seed /= 4; put(board, exoPieces[3], 0, j, ANY);
6033 j = seed%3; seed /= 3; put(board, exoPieces[2], 0, j, ANY);
6034 j = seed%2; seed /= 2; put(board, exoPieces[1], 0, j, ANY);
6035 put(board, exoPieces[0], 0, 0, ANY);
6036 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) board[BOARD_HEIGHT-1][i] = WHITE_TO_BLACK board[0][i];
6040 InitPosition (int redraw)
6042 ChessSquare (* pieces)[BOARD_FILES];
6043 int i, j, pawnRow=1, pieceRows=1, overrule,
6044 oldx = gameInfo.boardWidth,
6045 oldy = gameInfo.boardHeight,
6046 oldh = gameInfo.holdingsWidth;
6049 if(appData.icsActive) shuffleOpenings = appData.fischerCastling = FALSE; // [HGM] shuffle: in ICS mode, only shuffle on ICS request
6051 /* [AS] Initialize pv info list [HGM] and game status */
6053 for( i=0; i<=framePtr; i++ ) { // [HGM] vari: spare saved variations
6054 pvInfoList[i].depth = 0;
6055 boards[i][EP_STATUS] = EP_NONE;
6056 for( j=0; j<BOARD_FILES-2; j++ ) boards[i][CASTLING][j] = NoRights;
6059 initialRulePlies = 0; /* 50-move counter start */
6061 castlingRank[0] = castlingRank[1] = castlingRank[2] = 0;
6062 castlingRank[3] = castlingRank[4] = castlingRank[5] = BOARD_HEIGHT-1;
6066 /* [HGM] logic here is completely changed. In stead of full positions */
6067 /* the initialized data only consist of the two backranks. The switch */
6068 /* selects which one we will use, which is than copied to the Board */
6069 /* initialPosition, which for the rest is initialized by Pawns and */
6070 /* empty squares. This initial position is then copied to boards[0], */
6071 /* possibly after shuffling, so that it remains available. */
6073 gameInfo.holdingsWidth = 0; /* default board sizes */
6074 gameInfo.boardWidth = 8;
6075 gameInfo.boardHeight = 8;
6076 gameInfo.holdingsSize = 0;
6077 nrCastlingRights = -1; /* [HGM] Kludge to indicate default should be used */
6078 for(i=0; i<BOARD_FILES-6; i++)
6079 initialPosition[CASTLING][i] = initialRights[i] = NoRights; /* but no rights yet */
6080 initialPosition[EP_STATUS] = EP_NONE;
6081 initialPosition[TOUCHED_W] = initialPosition[TOUCHED_B] = 0;
6082 SetCharTable(pieceToChar, "PNBRQ...........Kpnbrq...........k");
6083 if(startVariant == gameInfo.variant) // [HGM] nicks: enable nicknames in original variant
6084 SetCharTable(pieceNickName, appData.pieceNickNames);
6085 else SetCharTable(pieceNickName, "............");
6088 switch (gameInfo.variant) {
6089 case VariantFischeRandom:
6090 shuffleOpenings = TRUE;
6091 appData.fischerCastling = TRUE;
6094 case VariantShatranj:
6095 pieces = ShatranjArray;
6096 nrCastlingRights = 0;
6097 SetCharTable(pieceToChar, "PN.R.QB...Kpn.r.qb...k");
6100 pieces = makrukArray;
6101 nrCastlingRights = 0;
6102 SetCharTable(pieceToChar, "PN.R.M....SKpn.r.m....sk");
6105 pieces = aseanArray;
6106 nrCastlingRights = 0;
6107 SetCharTable(pieceToChar, "PN.R.Q....BKpn.r.q....bk");
6109 case VariantTwoKings:
6110 pieces = twoKingsArray;
6113 pieces = GrandArray;
6114 nrCastlingRights = 0;
6115 SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
6116 gameInfo.boardWidth = 10;
6117 gameInfo.boardHeight = 10;
6118 gameInfo.holdingsSize = 7;
6120 case VariantCapaRandom:
6121 shuffleOpenings = TRUE;
6122 appData.fischerCastling = TRUE;
6123 case VariantCapablanca:
6124 pieces = CapablancaArray;
6125 gameInfo.boardWidth = 10;
6126 SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
6129 pieces = GothicArray;
6130 gameInfo.boardWidth = 10;
6131 SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
6134 SetCharTable(pieceToChar, "PNBRQ..HEKpnbrq..hek");
6135 gameInfo.holdingsSize = 7;
6136 for(i=0; i<BOARD_FILES; i++) initialPosition[VIRGIN][i] = VIRGIN_W | VIRGIN_B;
6139 pieces = JanusArray;
6140 gameInfo.boardWidth = 10;
6141 SetCharTable(pieceToChar, "PNBRQ..JKpnbrq..jk");
6142 nrCastlingRights = 6;
6143 initialPosition[CASTLING][0] = initialRights[0] = BOARD_RGHT-1;
6144 initialPosition[CASTLING][1] = initialRights[1] = BOARD_LEFT;
6145 initialPosition[CASTLING][2] = initialRights[2] =(BOARD_WIDTH-1)>>1;
6146 initialPosition[CASTLING][3] = initialRights[3] = BOARD_RGHT-1;
6147 initialPosition[CASTLING][4] = initialRights[4] = BOARD_LEFT;
6148 initialPosition[CASTLING][5] = initialRights[5] =(BOARD_WIDTH-1)>>1;
6151 pieces = FalconArray;
6152 gameInfo.boardWidth = 10;
6153 SetCharTable(pieceToChar, "PNBRQ............FKpnbrq............fk");
6155 case VariantXiangqi:
6156 pieces = XiangqiArray;
6157 gameInfo.boardWidth = 9;
6158 gameInfo.boardHeight = 10;
6159 nrCastlingRights = 0;
6160 SetCharTable(pieceToChar, "PH.R.AE..K.C.ph.r.ae..k.c.");
6163 pieces = ShogiArray;
6164 gameInfo.boardWidth = 9;
6165 gameInfo.boardHeight = 9;
6166 gameInfo.holdingsSize = 7;
6167 nrCastlingRights = 0;
6168 SetCharTable(pieceToChar, "PNBRLS...G.++++++Kpnbrls...g.++++++k");
6171 pieces = ChuArray; pieceRows = 3;
6172 gameInfo.boardWidth = 12;
6173 gameInfo.boardHeight = 12;
6174 nrCastlingRights = 0;
6175 SetCharTableEsc(pieceToChar, "P.BRQSEXOGCATHD.VMLIFN:+.++.++++++++++.+++++K"
6176 "p.brqsexogcathd.vmlifn:+.++.++++++++++.+++++k", SUFFIXES);
6178 case VariantCourier:
6179 pieces = CourierArray;
6180 gameInfo.boardWidth = 12;
6181 nrCastlingRights = 0;
6182 SetCharTable(pieceToChar, "PNBR.FE..WMKpnbr.fe..wmk");
6184 case VariantKnightmate:
6185 pieces = KnightmateArray;
6186 SetCharTable(pieceToChar, "P.BRQ.....M.........K.p.brq.....m.........k.");
6188 case VariantSpartan:
6189 pieces = SpartanArray;
6190 SetCharTable(pieceToChar, "PNBRQ................K......lwg.....c...h..k");
6194 SetCharTable(pieceToChar, "PNBRQ................LKpnbrq................lk");
6196 case VariantChuChess:
6197 pieces = ChuChessArray;
6198 gameInfo.boardWidth = 10;
6199 gameInfo.boardHeight = 10;
6200 SetCharTable(pieceToChar, "PNBRQ.....M.+++......LKpnbrq.....m.+++......lk");
6203 pieces = fairyArray;
6204 SetCharTable(pieceToChar, "PNBRQFEACWMOHIJGDVLSUKpnbrqfeacwmohijgdvlsuk");
6207 pieces = GreatArray;
6208 gameInfo.boardWidth = 10;
6209 SetCharTable(pieceToChar, "PN....E...S..HWGMKpn....e...s..hwgmk");
6210 gameInfo.holdingsSize = 8;
6214 SetCharTable(pieceToChar, "PNBRQ..SE.......V.AKpnbrq..se.......v.ak");
6215 gameInfo.holdingsSize = 8;
6216 startedFromSetupPosition = TRUE;
6218 case VariantCrazyhouse:
6219 case VariantBughouse:
6221 SetCharTable(pieceToChar, "PNBRQ.......~~~~Kpnbrq.......~~~~k");
6222 gameInfo.holdingsSize = 5;
6224 case VariantWildCastle:
6226 /* !!?shuffle with kings guaranteed to be on d or e file */
6227 shuffleOpenings = 1;
6229 case VariantNoCastle:
6231 nrCastlingRights = 0;
6232 /* !!?unconstrained back-rank shuffle */
6233 shuffleOpenings = 1;
6238 if(appData.NrFiles >= 0) {
6239 if(gameInfo.boardWidth != appData.NrFiles) overrule++;
6240 gameInfo.boardWidth = appData.NrFiles;
6242 if(appData.NrRanks >= 0) {
6243 gameInfo.boardHeight = appData.NrRanks;
6245 if(appData.holdingsSize >= 0) {
6246 i = appData.holdingsSize;
6247 if(i > gameInfo.boardHeight) i = gameInfo.boardHeight;
6248 gameInfo.holdingsSize = i;
6250 if(gameInfo.holdingsSize) gameInfo.holdingsWidth = 2;
6251 if(BOARD_HEIGHT > BOARD_RANKS || BOARD_WIDTH > BOARD_FILES)
6252 DisplayFatalError(_("Recompile to support this BOARD_RANKS or BOARD_FILES!"), 0, 2);
6254 pawnRow = gameInfo.boardHeight - 7; /* seems to work in all common variants */
6255 if(pawnRow < 1) pawnRow = 1;
6256 if(gameInfo.variant == VariantMakruk || gameInfo.variant == VariantASEAN ||
6257 gameInfo.variant == VariantGrand || gameInfo.variant == VariantChuChess) pawnRow = 2;
6258 if(gameInfo.variant == VariantChu) pawnRow = 3;
6260 /* User pieceToChar list overrules defaults */
6261 if(appData.pieceToCharTable != NULL)
6262 SetCharTableEsc(pieceToChar, appData.pieceToCharTable, SUFFIXES);
6264 for( j=0; j<BOARD_WIDTH; j++ ) { ChessSquare s = EmptySquare;
6266 if(j==BOARD_LEFT-1 || j==BOARD_RGHT)
6267 s = (ChessSquare) 0; /* account holding counts in guard band */
6268 for( i=0; i<BOARD_HEIGHT; i++ )
6269 initialPosition[i][j] = s;
6271 if(j < BOARD_LEFT || j >= BOARD_RGHT || overrule) continue;
6272 initialPosition[gameInfo.variant == VariantGrand || gameInfo.variant == VariantChuChess][j] = pieces[0][j-gameInfo.holdingsWidth];
6273 initialPosition[pawnRow][j] = WhitePawn;
6274 initialPosition[BOARD_HEIGHT-pawnRow-1][j] = gameInfo.variant == VariantSpartan ? BlackLance : BlackPawn;
6275 if(gameInfo.variant == VariantXiangqi) {
6277 initialPosition[pawnRow][j] =
6278 initialPosition[BOARD_HEIGHT-pawnRow-1][j] = EmptySquare;
6279 if(j==BOARD_LEFT+1 || j>=BOARD_RGHT-2) {
6280 initialPosition[2][j] = WhiteCannon;
6281 initialPosition[BOARD_HEIGHT-3][j] = BlackCannon;
6285 if(gameInfo.variant == VariantChu) {
6286 if(j == (BOARD_WIDTH-2)/3 || j == BOARD_WIDTH - (BOARD_WIDTH+1)/3)
6287 initialPosition[pawnRow+1][j] = WhiteCobra,
6288 initialPosition[BOARD_HEIGHT-pawnRow-2][j] = BlackCobra;
6289 for(i=1; i<pieceRows; i++) {
6290 initialPosition[i][j] = pieces[2*i][j-gameInfo.holdingsWidth];
6291 initialPosition[BOARD_HEIGHT-1-i][j] = pieces[2*i+1][j-gameInfo.holdingsWidth];
6294 if(gameInfo.variant == VariantGrand || gameInfo.variant == VariantChuChess) {
6295 if(j==BOARD_LEFT || j>=BOARD_RGHT-1) {
6296 initialPosition[0][j] = WhiteRook;
6297 initialPosition[BOARD_HEIGHT-1][j] = BlackRook;
6300 initialPosition[BOARD_HEIGHT-1-(gameInfo.variant == VariantGrand || gameInfo.variant == VariantChuChess)][j] = pieces[1][j-gameInfo.holdingsWidth];
6302 if(gameInfo.variant == VariantChuChess) initialPosition[0][BOARD_WIDTH/2] = WhiteKing, initialPosition[BOARD_HEIGHT-1][BOARD_WIDTH/2-1] = BlackKing;
6303 if( (gameInfo.variant == VariantShogi) && !overrule ) {
6306 initialPosition[1][j] = WhiteBishop;
6307 initialPosition[BOARD_HEIGHT-2][j] = BlackRook;
6309 initialPosition[1][j] = WhiteRook;
6310 initialPosition[BOARD_HEIGHT-2][j] = BlackBishop;
6313 if( nrCastlingRights == -1) {
6314 /* [HGM] Build normal castling rights (must be done after board sizing!) */
6315 /* This sets default castling rights from none to normal corners */
6316 /* Variants with other castling rights must set them themselves above */
6317 nrCastlingRights = 6;
6319 initialPosition[CASTLING][0] = initialRights[0] = BOARD_RGHT-1;
6320 initialPosition[CASTLING][1] = initialRights[1] = BOARD_LEFT;
6321 initialPosition[CASTLING][2] = initialRights[2] = BOARD_WIDTH>>1;
6322 initialPosition[CASTLING][3] = initialRights[3] = BOARD_RGHT-1;
6323 initialPosition[CASTLING][4] = initialRights[4] = BOARD_LEFT;
6324 initialPosition[CASTLING][5] = initialRights[5] = BOARD_WIDTH>>1;
6327 if(gameInfo.variant == VariantSuper) Prelude(initialPosition);
6328 if(gameInfo.variant == VariantGreat) { // promotion commoners
6329 initialPosition[PieceToNumber(WhiteMan)][BOARD_WIDTH-1] = WhiteMan;
6330 initialPosition[PieceToNumber(WhiteMan)][BOARD_WIDTH-2] = 9;
6331 initialPosition[BOARD_HEIGHT-1-PieceToNumber(WhiteMan)][0] = BlackMan;
6332 initialPosition[BOARD_HEIGHT-1-PieceToNumber(WhiteMan)][1] = 9;
6334 if( gameInfo.variant == VariantSChess ) {
6335 initialPosition[1][0] = BlackMarshall;
6336 initialPosition[2][0] = BlackAngel;
6337 initialPosition[6][BOARD_WIDTH-1] = WhiteMarshall;
6338 initialPosition[5][BOARD_WIDTH-1] = WhiteAngel;
6339 initialPosition[1][1] = initialPosition[2][1] =
6340 initialPosition[6][BOARD_WIDTH-2] = initialPosition[5][BOARD_WIDTH-2] = 1;
6342 if (appData.debugMode) {
6343 fprintf(debugFP, "shuffleOpenings = %d\n", shuffleOpenings);
6345 if(shuffleOpenings) {
6346 SetUpShuffle(initialPosition, appData.defaultFrcPosition);
6347 startedFromSetupPosition = TRUE;
6349 if(startedFromPositionFile) {
6350 /* [HGM] loadPos: use PositionFile for every new game */
6351 CopyBoard(initialPosition, filePosition);
6352 for(i=0; i<nrCastlingRights; i++)
6353 initialRights[i] = filePosition[CASTLING][i];
6354 startedFromSetupPosition = TRUE;
6357 CopyBoard(boards[0], initialPosition);
6359 if(oldx != gameInfo.boardWidth ||
6360 oldy != gameInfo.boardHeight ||
6361 oldv != gameInfo.variant ||
6362 oldh != gameInfo.holdingsWidth
6364 InitDrawingSizes(-2 ,0);
6366 oldv = gameInfo.variant;
6368 DrawPosition(TRUE, boards[currentMove]);
6372 SendBoard (ChessProgramState *cps, int moveNum)
6374 char message[MSG_SIZ];
6376 if (cps->useSetboard) {
6377 char* fen = PositionToFEN(moveNum, cps->fenOverride, 1);
6378 snprintf(message, MSG_SIZ,"setboard %s\n", fen);
6379 SendToProgram(message, cps);
6384 int i, j, left=0, right=BOARD_WIDTH;
6385 /* Kludge to set black to move, avoiding the troublesome and now
6386 * deprecated "black" command.
6388 if (!WhiteOnMove(moveNum)) // [HGM] but better a deprecated command than an illegal move...
6389 SendToProgram(boards[0][1][BOARD_LEFT] == WhitePawn ? "a2a3\n" : "black\n", cps);
6391 if(!cps->extendedEdit) left = BOARD_LEFT, right = BOARD_RGHT; // only board proper
6393 SendToProgram("edit\n", cps);
6394 SendToProgram("#\n", cps);
6395 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
6396 bp = &boards[moveNum][i][left];
6397 for (j = left; j < right; j++, bp++) {
6398 if(j == BOARD_LEFT-1 || j == BOARD_RGHT) continue;
6399 if ((int) *bp < (int) BlackPawn) {
6400 if(j == BOARD_RGHT+1)
6401 snprintf(message, MSG_SIZ, "%c@%d\n", PieceToChar(*bp), bp[-1]);
6402 else snprintf(message, MSG_SIZ, "%c%c%c\n", PieceToChar(*bp), AAA + j, ONE + i);
6403 if(message[0] == '+' || message[0] == '~') {
6404 snprintf(message, MSG_SIZ,"%c%c%c+\n",
6405 PieceToChar((ChessSquare)(DEMOTED *bp)),
6408 if(cps->alphaRank) { /* [HGM] shogi: translate coords */
6409 message[1] = BOARD_RGHT - 1 - j + '1';
6410 message[2] = BOARD_HEIGHT - 1 - i + 'a';
6412 SendToProgram(message, cps);
6417 SendToProgram("c\n", cps);
6418 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
6419 bp = &boards[moveNum][i][left];
6420 for (j = left; j < right; j++, bp++) {
6421 if(j == BOARD_LEFT-1 || j == BOARD_RGHT) continue;
6422 if (((int) *bp != (int) EmptySquare)
6423 && ((int) *bp >= (int) BlackPawn)) {
6424 if(j == BOARD_LEFT-2)
6425 snprintf(message, MSG_SIZ, "%c@%d\n", ToUpper(PieceToChar(*bp)), bp[1]);
6426 else snprintf(message,MSG_SIZ, "%c%c%c\n", ToUpper(PieceToChar(*bp)),
6428 if(message[0] == '+' || message[0] == '~') {
6429 snprintf(message, MSG_SIZ,"%c%c%c+\n",
6430 PieceToChar((ChessSquare)(DEMOTED *bp)),
6433 if(cps->alphaRank) { /* [HGM] shogi: translate coords */
6434 message[1] = BOARD_RGHT - 1 - j + '1';
6435 message[2] = BOARD_HEIGHT - 1 - i + 'a';
6437 SendToProgram(message, cps);
6442 SendToProgram(".\n", cps);
6444 setboardSpoiledMachineBlack = 0; /* [HGM] assume WB 4.2.7 already solves this after sending setboard */
6447 char exclusionHeader[MSG_SIZ];
6448 int exCnt, excludePtr;
6449 typedef struct { int ff, fr, tf, tr, pc, mark; } Exclusion;
6450 static Exclusion excluTab[200];
6451 static char excludeMap[(BOARD_RANKS*BOARD_FILES*BOARD_RANKS*BOARD_FILES+7)/8]; // [HGM] exclude: bitmap for excluced moves
6457 for(j=0; j<(BOARD_RANKS*BOARD_FILES*BOARD_RANKS*BOARD_FILES+7)/8; j++) excludeMap[j] = s;
6458 exclusionHeader[19] = s ? '-' : '+'; // update tail state
6464 safeStrCpy(exclusionHeader, "exclude: none best +tail \n", MSG_SIZ);
6465 excludePtr = 24; exCnt = 0;
6470 UpdateExcludeHeader (int fromY, int fromX, int toY, int toX, char promoChar, char state)
6471 { // search given move in table of header moves, to know where it is listed (and add if not there), and update state
6472 char buf[2*MOVE_LEN], *p;
6473 Exclusion *e = excluTab;
6475 for(i=0; i<exCnt; i++)
6476 if(e[i].ff == fromX && e[i].fr == fromY &&
6477 e[i].tf == toX && e[i].tr == toY && e[i].pc == promoChar) break;
6478 if(i == exCnt) { // was not in exclude list; add it
6479 CoordsToAlgebraic(boards[currentMove], PosFlags(currentMove), fromY, fromX, toY, toX, promoChar, buf);
6480 if(strlen(exclusionHeader + excludePtr) < strlen(buf)) { // no space to write move
6481 if(state != exclusionHeader[19]) exclusionHeader[19] = '*'; // tail is now in mixed state
6484 e[i].ff = fromX; e[i].fr = fromY; e[i].tf = toX; e[i].tr = toY; e[i].pc = promoChar;
6485 excludePtr++; e[i].mark = excludePtr++;
6486 for(p=buf; *p; p++) exclusionHeader[excludePtr++] = *p; // copy move
6489 exclusionHeader[e[i].mark] = state;
6493 ExcludeOneMove (int fromY, int fromX, int toY, int toX, char promoChar, char state)
6494 { // include or exclude the given move, as specified by state ('+' or '-'), or toggle
6498 if((signed char)promoChar == -1) { // kludge to indicate best move
6499 if(!ParseOneMove(lastPV[0], currentMove, &moveType, &fromX, &fromY, &toX, &toY, &promoChar)) // get current best move from last PV
6500 return 1; // if unparsable, abort
6502 // update exclusion map (resolving toggle by consulting existing state)
6503 k=(BOARD_FILES*fromY+fromX)*BOARD_RANKS*BOARD_FILES + (BOARD_FILES*toY+toX);
6505 if(state == '*') state = (excludeMap[k] & 1<<j ? '+' : '-'); // toggle
6506 if(state == '-' && !promoChar) // only non-promotions get marked as excluded, to allow exclusion of under-promotions
6507 excludeMap[k] |= 1<<j;
6508 else excludeMap[k] &= ~(1<<j);
6510 UpdateExcludeHeader(fromY, fromX, toY, toX, promoChar, state);
6512 snprintf(buf, MSG_SIZ, "%sclude ", state == '+' ? "in" : "ex");
6513 CoordsToComputerAlgebraic(fromY, fromX, toY, toX, promoChar, buf+8);
6515 return (state == '+');
6519 ExcludeClick (int index)
6522 Exclusion *e = excluTab;
6523 if(index < 25) { // none, best or tail clicked
6524 if(index < 13) { // none: include all
6525 WriteMap(0); // clear map
6526 for(i=0; i<exCnt; i++) exclusionHeader[excluTab[i].mark] = '+'; // and moves
6527 SendToBoth("include all\n"); // and inform engine
6528 } else if(index > 18) { // tail
6529 if(exclusionHeader[19] == '-') { // tail was excluded
6530 SendToBoth("include all\n");
6531 WriteMap(0); // clear map completely
6532 // now re-exclude selected moves
6533 for(i=0; i<exCnt; i++) if(exclusionHeader[e[i].mark] == '-')
6534 ExcludeOneMove(e[i].fr, e[i].ff, e[i].tr, e[i].tf, e[i].pc, '-');
6535 } else { // tail was included or in mixed state
6536 SendToBoth("exclude all\n");
6537 WriteMap(0xFF); // fill map completely
6538 // now re-include selected moves
6539 j = 0; // count them
6540 for(i=0; i<exCnt; i++) if(exclusionHeader[e[i].mark] == '+')
6541 ExcludeOneMove(e[i].fr, e[i].ff, e[i].tr, e[i].tf, e[i].pc, '+'), j++;
6542 if(!j) ExcludeOneMove(0, 0, 0, 0, -1, '+'); // if no moves were selected, keep best
6545 ExcludeOneMove(0, 0, 0, 0, -1, '-'); // exclude it
6548 for(i=0; i<exCnt; i++) if(i == exCnt-1 || excluTab[i+1].mark > index) {
6549 char *p=exclusionHeader + excluTab[i].mark; // do trust header more than map (promotions!)
6550 ExcludeOneMove(e[i].fr, e[i].ff, e[i].tr, e[i].tf, e[i].pc, *p == '+' ? '-' : '+');
6557 DefaultPromoChoice (int white)
6560 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier ||
6561 gameInfo.variant == VariantMakruk)
6562 result = WhiteFerz; // no choice
6563 else if(gameInfo.variant == VariantASEAN)
6564 result = WhiteRook; // no choice
6565 else if(gameInfo.variant == VariantSuicide || gameInfo.variant == VariantGiveaway)
6566 result= WhiteKing; // in Suicide Q is the last thing we want
6567 else if(gameInfo.variant == VariantSpartan)
6568 result = white ? WhiteQueen : WhiteAngel;
6569 else result = WhiteQueen;
6570 if(!white) result = WHITE_TO_BLACK result;
6574 static int autoQueen; // [HGM] oneclick
6577 HasPromotionChoice (int fromX, int fromY, int toX, int toY, char *promoChoice, int sweepSelect)
6579 /* [HGM] rewritten IsPromotion to only flag promotions that offer a choice */
6580 /* [HGM] add Shogi promotions */
6581 int promotionZoneSize=1, highestPromotingPiece = (int)WhitePawn;
6582 ChessSquare piece, partner;
6586 if(fromX < BOARD_LEFT || fromX >= BOARD_RGHT) return FALSE; // drop
6587 if(toX < BOARD_LEFT || toX >= BOARD_RGHT) return FALSE; // move into holdings
6589 if(gameMode == EditPosition || gameInfo.variant == VariantXiangqi || // no promotions
6590 !(fromX >=0 && fromY >= 0 && toX >= 0 && toY >= 0) ) // invalid move
6593 piece = boards[currentMove][fromY][fromX];
6594 if(gameInfo.variant == VariantChu) {
6595 int p = piece >= BlackPawn ? BLACK_TO_WHITE piece : piece;
6596 promotionZoneSize = BOARD_HEIGHT/3;
6597 highestPromotingPiece = (p >= WhiteLion || PieceToChar(piece + 22) == '.') ? WhitePawn : WhiteLion;
6598 } else if(gameInfo.variant == VariantShogi) {
6599 promotionZoneSize = BOARD_HEIGHT/3 +(BOARD_HEIGHT == 8);
6600 highestPromotingPiece = (int)WhiteAlfil;
6601 } else if(gameInfo.variant == VariantMakruk || gameInfo.variant == VariantGrand || gameInfo.variant == VariantChuChess) {
6602 promotionZoneSize = 3;
6605 // Treat Lance as Pawn when it is not representing Amazon or Lance
6606 if(gameInfo.variant != VariantSuper && gameInfo.variant != VariantChu) {
6607 if(piece == WhiteLance) piece = WhitePawn; else
6608 if(piece == BlackLance) piece = BlackPawn;
6611 // next weed out all moves that do not touch the promotion zone at all
6612 if((int)piece >= BlackPawn) {
6613 if(toY >= promotionZoneSize && fromY >= promotionZoneSize)
6615 if(fromY < promotionZoneSize && gameInfo.variant == VariantChuChess) return FALSE;
6616 highestPromotingPiece = WHITE_TO_BLACK highestPromotingPiece;
6618 if( toY < BOARD_HEIGHT - promotionZoneSize &&
6619 fromY < BOARD_HEIGHT - promotionZoneSize) return FALSE;
6620 if(fromY >= BOARD_HEIGHT - promotionZoneSize && gameInfo.variant == VariantChuChess)
6624 if( (int)piece > highestPromotingPiece ) return FALSE; // non-promoting piece
6626 // weed out mandatory Shogi promotions
6627 if(gameInfo.variant == VariantShogi) {
6628 if(piece >= BlackPawn) {
6629 if(toY == 0 && piece == BlackPawn ||
6630 toY == 0 && piece == BlackQueen ||
6631 toY <= 1 && piece == BlackKnight) {
6636 if(toY == BOARD_HEIGHT-1 && piece == WhitePawn ||
6637 toY == BOARD_HEIGHT-1 && piece == WhiteQueen ||
6638 toY >= BOARD_HEIGHT-2 && piece == WhiteKnight) {
6645 // weed out obviously illegal Pawn moves
6646 if(appData.testLegality && (piece == WhitePawn || piece == BlackPawn) ) {
6647 if(toX > fromX+1 || toX < fromX-1) return FALSE; // wide
6648 if(piece == WhitePawn && toY != fromY+1) return FALSE; // deep
6649 if(piece == BlackPawn && toY != fromY-1) return FALSE; // deep
6650 if(fromX != toX && gameInfo.variant == VariantShogi) return FALSE;
6651 // note we are not allowed to test for valid (non-)capture, due to premove
6654 // we either have a choice what to promote to, or (in Shogi) whether to promote
6655 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier ||
6656 gameInfo.variant == VariantMakruk) {
6657 ChessSquare p=BlackFerz; // no choice
6658 while(p < EmptySquare) { //but make sure we use piece that exists
6659 *promoChoice = PieceToChar(p++);
6660 if(*promoChoice != '.') break;
6664 // no sense asking what we must promote to if it is going to explode...
6665 if(gameInfo.variant == VariantAtomic && boards[currentMove][toY][toX] != EmptySquare) {
6666 *promoChoice = PieceToChar(BlackQueen); // Queen as good as any
6669 // give caller the default choice even if we will not make it
6670 *promoChoice = ToLower(PieceToChar(defaultPromoChoice));
6671 partner = piece; // pieces can promote if the pieceToCharTable says so
6672 if(IS_SHOGI(gameInfo.variant)) *promoChoice = (defaultPromoChoice == piece && sweepSelect ? '=' : '+'); // obsolete?
6673 else if(Partner(&partner)) *promoChoice = (defaultPromoChoice == piece && sweepSelect ? NULLCHAR : '+');
6674 if( sweepSelect && gameInfo.variant != VariantGreat
6675 && gameInfo.variant != VariantGrand
6676 && gameInfo.variant != VariantSuper) return FALSE;
6677 if(autoQueen) return FALSE; // predetermined
6679 // suppress promotion popup on illegal moves that are not premoves
6680 premove = gameMode == IcsPlayingWhite && !WhiteOnMove(currentMove) ||
6681 gameMode == IcsPlayingBlack && WhiteOnMove(currentMove);
6682 if(appData.testLegality && !premove) {
6683 moveType = LegalityTest(boards[currentMove], PosFlags(currentMove),
6684 fromY, fromX, toY, toX, IS_SHOGI(gameInfo.variant) || gameInfo.variant == VariantChuChess ? '+' : NULLCHAR);
6685 if(moveType == IllegalMove) *promoChoice = NULLCHAR; // could be the fact we promoted was illegal
6686 if(moveType != WhitePromotion && moveType != BlackPromotion)
6694 InPalace (int row, int column)
6695 { /* [HGM] for Xiangqi */
6696 if( (row < 3 || row > BOARD_HEIGHT-4) &&
6697 column < (BOARD_WIDTH + 4)/2 &&
6698 column > (BOARD_WIDTH - 5)/2 ) return TRUE;
6703 PieceForSquare (int x, int y)
6705 if (x < 0 || x >= BOARD_WIDTH || y < 0 || y >= BOARD_HEIGHT)
6708 return boards[currentMove][y][x];
6712 OKToStartUserMove (int x, int y)
6714 ChessSquare from_piece;
6717 if (matchMode) return FALSE;
6718 if (gameMode == EditPosition) return TRUE;
6720 if (x >= 0 && y >= 0)
6721 from_piece = boards[currentMove][y][x];
6723 from_piece = EmptySquare;
6725 if (from_piece == EmptySquare) return FALSE;
6727 white_piece = (int)from_piece >= (int)WhitePawn &&
6728 (int)from_piece < (int)BlackPawn; /* [HGM] can be > King! */
6732 case TwoMachinesPlay:
6740 case MachinePlaysWhite:
6741 case IcsPlayingBlack:
6742 if (appData.zippyPlay) return FALSE;
6744 DisplayMoveError(_("You are playing Black"));
6749 case MachinePlaysBlack:
6750 case IcsPlayingWhite:
6751 if (appData.zippyPlay) return FALSE;
6753 DisplayMoveError(_("You are playing White"));
6758 case PlayFromGameFile:
6759 if(!shiftKey || !appData.variations) return FALSE; // [HGM] allow starting variation in this mode
6761 if (!white_piece && WhiteOnMove(currentMove)) {
6762 DisplayMoveError(_("It is White's turn"));
6765 if (white_piece && !WhiteOnMove(currentMove)) {
6766 DisplayMoveError(_("It is Black's turn"));
6769 if (cmailMsgLoaded && (currentMove < cmailOldMove)) {
6770 /* Editing correspondence game history */
6771 /* Could disallow this or prompt for confirmation */
6776 case BeginningOfGame:
6777 if (appData.icsActive) return FALSE;
6778 if (!appData.noChessProgram) {
6780 DisplayMoveError(_("You are playing White"));
6787 if (!white_piece && WhiteOnMove(currentMove)) {
6788 DisplayMoveError(_("It is White's turn"));
6791 if (white_piece && !WhiteOnMove(currentMove)) {
6792 DisplayMoveError(_("It is Black's turn"));
6801 if (currentMove != forwardMostMove && gameMode != AnalyzeMode
6802 && gameMode != EditGame // [HGM] vari: treat as AnalyzeMode
6803 && gameMode != PlayFromGameFile // [HGM] as EditGame, with protected main line
6804 && gameMode != AnalyzeFile && gameMode != Training) {
6805 DisplayMoveError(_("Displayed position is not current"));
6812 OnlyMove (int *x, int *y, Boolean captures)
6814 DisambiguateClosure cl;
6815 if (appData.zippyPlay || !appData.testLegality) return FALSE;
6817 case MachinePlaysBlack:
6818 case IcsPlayingWhite:
6819 case BeginningOfGame:
6820 if(!WhiteOnMove(currentMove)) return FALSE;
6822 case MachinePlaysWhite:
6823 case IcsPlayingBlack:
6824 if(WhiteOnMove(currentMove)) return FALSE;
6831 cl.pieceIn = EmptySquare;
6836 cl.promoCharIn = NULLCHAR;
6837 Disambiguate(boards[currentMove], PosFlags(currentMove), &cl);
6838 if( cl.kind == NormalMove ||
6839 cl.kind == AmbiguousMove && captures && cl.captures == 1 ||
6840 cl.kind == WhitePromotion || cl.kind == BlackPromotion ||
6841 cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) {
6848 if(cl.kind != ImpossibleMove) return FALSE;
6849 cl.pieceIn = EmptySquare;
6854 cl.promoCharIn = NULLCHAR;
6855 Disambiguate(boards[currentMove], PosFlags(currentMove), &cl);
6856 if( cl.kind == NormalMove ||
6857 cl.kind == AmbiguousMove && captures && cl.captures == 1 ||
6858 cl.kind == WhitePromotion || cl.kind == BlackPromotion ||
6859 cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) {
6864 autoQueen = TRUE; // act as if autoQueen on when we click to-square
6870 FILE *lastLoadGameFP = NULL, *lastLoadPositionFP = NULL;
6871 int lastLoadGameNumber = 0, lastLoadPositionNumber = 0;
6872 int lastLoadGameUseList = FALSE;
6873 char lastLoadGameTitle[MSG_SIZ], lastLoadPositionTitle[MSG_SIZ];
6874 ChessMove lastLoadGameStart = EndOfFile;
6876 Boolean addToBookFlag;
6879 UserMoveEvent(int fromX, int fromY, int toX, int toY, int promoChar)
6883 int ff=fromX, rf=fromY, ft=toX, rt=toY;
6885 /* Check if the user is playing in turn. This is complicated because we
6886 let the user "pick up" a piece before it is his turn. So the piece he
6887 tried to pick up may have been captured by the time he puts it down!
6888 Therefore we use the color the user is supposed to be playing in this
6889 test, not the color of the piece that is currently on the starting
6890 square---except in EditGame mode, where the user is playing both
6891 sides; fortunately there the capture race can't happen. (It can
6892 now happen in IcsExamining mode, but that's just too bad. The user
6893 will get a somewhat confusing message in that case.)
6898 case TwoMachinesPlay:
6902 /* We switched into a game mode where moves are not accepted,
6903 perhaps while the mouse button was down. */
6906 case MachinePlaysWhite:
6907 /* User is moving for Black */
6908 if (WhiteOnMove(currentMove)) {
6909 DisplayMoveError(_("It is White's turn"));
6914 case MachinePlaysBlack:
6915 /* User is moving for White */
6916 if (!WhiteOnMove(currentMove)) {
6917 DisplayMoveError(_("It is Black's turn"));
6922 case PlayFromGameFile:
6923 if(!shiftKey ||!appData.variations) return; // [HGM] only variations
6926 case BeginningOfGame:
6929 if(fromY == DROP_RANK) break; // [HGM] drop moves (entered through move type-in) are automatically assigned to side-to-move
6930 if ((int) boards[currentMove][fromY][fromX] >= (int) BlackPawn &&
6931 (int) boards[currentMove][fromY][fromX] < (int) EmptySquare) {
6932 /* User is moving for Black */
6933 if (WhiteOnMove(currentMove)) {
6934 DisplayMoveError(_("It is White's turn"));
6938 /* User is moving for White */
6939 if (!WhiteOnMove(currentMove)) {
6940 DisplayMoveError(_("It is Black's turn"));
6946 case IcsPlayingBlack:
6947 /* User is moving for Black */
6948 if (WhiteOnMove(currentMove)) {
6949 if (!appData.premove) {
6950 DisplayMoveError(_("It is White's turn"));
6951 } else if (toX >= 0 && toY >= 0) {
6954 premoveFromX = fromX;
6955 premoveFromY = fromY;
6956 premovePromoChar = promoChar;
6958 if (appData.debugMode)
6959 fprintf(debugFP, "Got premove: fromX %d,"
6960 "fromY %d, toX %d, toY %d\n",
6961 fromX, fromY, toX, toY);
6967 case IcsPlayingWhite:
6968 /* User is moving for White */
6969 if (!WhiteOnMove(currentMove)) {
6970 if (!appData.premove) {
6971 DisplayMoveError(_("It is Black's turn"));
6972 } else if (toX >= 0 && toY >= 0) {
6975 premoveFromX = fromX;
6976 premoveFromY = fromY;
6977 premovePromoChar = promoChar;
6979 if (appData.debugMode)
6980 fprintf(debugFP, "Got premove: fromX %d,"
6981 "fromY %d, toX %d, toY %d\n",
6982 fromX, fromY, toX, toY);
6992 /* EditPosition, empty square, or different color piece;
6993 click-click move is possible */
6994 if (toX == -2 || toY == -2) {
6995 boards[0][fromY][fromX] = (boards[0][fromY][fromX] == EmptySquare ? DarkSquare : EmptySquare);
6996 DrawPosition(FALSE, boards[currentMove]);
6998 } else if (toX >= 0 && toY >= 0) {
6999 if(!appData.pieceMenu && toX == fromX && toY == fromY && boards[0][rf][ff] != EmptySquare) {
7000 ChessSquare q, p = boards[0][rf][ff];
7001 if(p >= BlackPawn) p = BLACK_TO_WHITE p;
7002 if(CHUPROMOTED p < BlackPawn) p = q = CHUPROMOTED boards[0][rf][ff];
7003 else p = CHUDEMOTED (q = boards[0][rf][ff]);
7004 if(PieceToChar(q) == '+') gatingPiece = p;
7006 boards[0][toY][toX] = boards[0][fromY][fromX];
7007 if(fromX == BOARD_LEFT-2) { // handle 'moves' out of holdings
7008 if(boards[0][fromY][0] != EmptySquare) {
7009 if(boards[0][fromY][1]) boards[0][fromY][1]--;
7010 if(boards[0][fromY][1] == 0) boards[0][fromY][0] = EmptySquare;
7013 if(fromX == BOARD_RGHT+1) {
7014 if(boards[0][fromY][BOARD_WIDTH-1] != EmptySquare) {
7015 if(boards[0][fromY][BOARD_WIDTH-2]) boards[0][fromY][BOARD_WIDTH-2]--;
7016 if(boards[0][fromY][BOARD_WIDTH-2] == 0) boards[0][fromY][BOARD_WIDTH-1] = EmptySquare;
7019 boards[0][fromY][fromX] = gatingPiece;
7020 DrawPosition(FALSE, boards[currentMove]);
7026 if((toX < 0 || toY < 0) && (fromY != DROP_RANK || fromX != EmptySquare)) return;
7027 pup = boards[currentMove][toY][toX];
7029 /* [HGM] If move started in holdings, it means a drop. Convert to standard form */
7030 if( (fromX == BOARD_LEFT-2 || fromX == BOARD_RGHT+1) && fromY != DROP_RANK ) {
7031 if( pup != EmptySquare ) return;
7032 moveType = WhiteOnMove(currentMove) ? WhiteDrop : BlackDrop;
7033 if(appData.debugMode) fprintf(debugFP, "Drop move %d, curr=%d, x=%d,y=%d, p=%d\n",
7034 moveType, currentMove, fromX, fromY, boards[currentMove][fromY][fromX]);
7035 // holdings might not be sent yet in ICS play; we have to figure out which piece belongs here
7036 if(fromX == 0) fromY = BOARD_HEIGHT-1 - fromY; // black holdings upside-down
7037 fromX = fromX ? WhitePawn : BlackPawn; // first piece type in selected holdings
7038 while(PieceToChar(fromX) == '.' || PieceToChar(fromX) == '+' || PieceToNumber(fromX) != fromY && fromX != (int) EmptySquare) fromX++;
7042 /* [HGM] always test for legality, to get promotion info */
7043 moveType = LegalityTest(boards[currentMove], PosFlags(currentMove),
7044 fromY, fromX, toY, toX, promoChar);
7046 if(fromY == DROP_RANK && fromX == EmptySquare && (gameMode == AnalyzeMode || gameMode == EditGame || PosFlags(0) & F_NULL_MOVE)) moveType = NormalMove;
7048 /* [HGM] but possibly ignore an IllegalMove result */
7049 if (appData.testLegality) {
7050 if (moveType == IllegalMove || moveType == ImpossibleMove) {
7051 DisplayMoveError(_("Illegal move"));
7056 if(doubleClick && gameMode == AnalyzeMode) { // [HGM] exclude: move entered with double-click on from square is for exclusion, not playing
7057 if(ExcludeOneMove(fromY, fromX, toY, toX, promoChar, '*')) // toggle
7058 ClearPremoveHighlights(); // was included
7059 else ClearHighlights(), SetPremoveHighlights(ff, rf, ft, rt); // exclusion indicated by premove highlights
7063 if(addToBookFlag) { // adding moves to book
7064 char buf[MSG_SIZ], move[MSG_SIZ];
7065 CoordsToAlgebraic(boards[currentMove], PosFlags(currentMove), fromY, fromX, toY, toX, promoChar, move);
7066 if(killX >= 0) snprintf(move, MSG_SIZ, "%c%dx%c%d-%c%d", fromX + AAA, fromY + ONE - '0', killX + AAA, killY + ONE - '0', toX + AAA, toY + ONE - '0');
7067 snprintf(buf, MSG_SIZ, " 0.0%% 1 %s\n", move);
7069 addToBookFlag = FALSE;
7074 FinishMove(moveType, fromX, fromY, toX, toY, promoChar);
7077 /* Common tail of UserMoveEvent and DropMenuEvent */
7079 FinishMove (ChessMove moveType, int fromX, int fromY, int toX, int toY, int promoChar)
7083 if((gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand) && promoChar != NULLCHAR) {
7084 // [HGM] superchess: suppress promotions to non-available piece (but P always allowed)
7085 int k = PieceToNumber(CharToPiece(ToUpper(promoChar)));
7086 if(WhiteOnMove(currentMove)) {
7087 if(!boards[currentMove][k][BOARD_WIDTH-2]) return 0;
7089 if(!boards[currentMove][BOARD_HEIGHT-1-k][1]) return 0;
7093 /* [HGM] <popupFix> kludge to avoid having to know the exact promotion
7094 move type in caller when we know the move is a legal promotion */
7095 if(moveType == NormalMove && promoChar)
7096 moveType = WhiteOnMove(currentMove) ? WhitePromotion : BlackPromotion;
7098 /* [HGM] <popupFix> The following if has been moved here from
7099 UserMoveEvent(). Because it seemed to belong here (why not allow
7100 piece drops in training games?), and because it can only be
7101 performed after it is known to what we promote. */
7102 if (gameMode == Training) {
7103 /* compare the move played on the board to the next move in the
7104 * game. If they match, display the move and the opponent's response.
7105 * If they don't match, display an error message.
7109 CopyBoard(testBoard, boards[currentMove]);
7110 ApplyMove(fromX, fromY, toX, toY, promoChar, testBoard);
7112 if (CompareBoards(testBoard, boards[currentMove+1])) {
7113 ForwardInner(currentMove+1);
7115 /* Autoplay the opponent's response.
7116 * if appData.animate was TRUE when Training mode was entered,
7117 * the response will be animated.
7119 saveAnimate = appData.animate;
7120 appData.animate = animateTraining;
7121 ForwardInner(currentMove+1);
7122 appData.animate = saveAnimate;
7124 /* check for the end of the game */
7125 if (currentMove >= forwardMostMove) {
7126 gameMode = PlayFromGameFile;
7128 SetTrainingModeOff();
7129 DisplayInformation(_("End of game"));
7132 DisplayError(_("Incorrect move"), 0);
7137 /* Ok, now we know that the move is good, so we can kill
7138 the previous line in Analysis Mode */
7139 if ((gameMode == AnalyzeMode || gameMode == EditGame || gameMode == PlayFromGameFile && appData.variations && shiftKey)
7140 && currentMove < forwardMostMove) {
7141 if(appData.variations && shiftKey) PushTail(currentMove, forwardMostMove); // [HGM] vari: save tail of game
7142 else forwardMostMove = currentMove;
7147 /* If we need the chess program but it's dead, restart it */
7148 ResurrectChessProgram();
7150 /* A user move restarts a paused game*/
7154 thinkOutput[0] = NULLCHAR;
7156 MakeMove(fromX, fromY, toX, toY, promoChar); /*updates forwardMostMove*/
7158 if(Adjudicate(NULL)) { // [HGM] adjudicate: take care of automatic game end
7159 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
7163 if (gameMode == BeginningOfGame) {
7164 if (appData.noChessProgram) {
7165 gameMode = EditGame;
7169 gameMode = MachinePlaysBlack;
7172 snprintf(buf, MSG_SIZ, "%s %s %s", gameInfo.white, _("vs."), gameInfo.black);
7174 if (first.sendName) {
7175 snprintf(buf, MSG_SIZ,"name %s\n", gameInfo.white);
7176 SendToProgram(buf, &first);
7183 /* Relay move to ICS or chess engine */
7184 if (appData.icsActive) {
7185 if (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
7186 gameMode == IcsExamining) {
7187 if(userOfferedDraw && (signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
7188 SendToICS(ics_prefix); // [HGM] drawclaim: send caim and move on one line for FICS
7190 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
7192 // also send plain move, in case ICS does not understand atomic claims
7193 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
7197 if (first.sendTime && (gameMode == BeginningOfGame ||
7198 gameMode == MachinePlaysWhite ||
7199 gameMode == MachinePlaysBlack)) {
7200 SendTimeRemaining(&first, gameMode != MachinePlaysBlack);
7202 if (gameMode != EditGame && gameMode != PlayFromGameFile && gameMode != AnalyzeMode) {
7203 // [HGM] book: if program might be playing, let it use book
7204 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, FALSE);
7205 first.maybeThinking = TRUE;
7206 } else if(fromY == DROP_RANK && fromX == EmptySquare) {
7207 if(!first.useSetboard) SendToProgram("undo\n", &first); // kludge to change stm in engines that do not support setboard
7208 SendBoard(&first, currentMove+1);
7209 if(second.analyzing) {
7210 if(!second.useSetboard) SendToProgram("undo\n", &second);
7211 SendBoard(&second, currentMove+1);
7214 SendMoveToProgram(forwardMostMove-1, &first);
7215 if(second.analyzing) SendMoveToProgram(forwardMostMove-1, &second);
7217 if (currentMove == cmailOldMove + 1) {
7218 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
7222 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
7226 if(appData.testLegality)
7227 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
7233 if (WhiteOnMove(currentMove)) {
7234 GameEnds(BlackWins, "Black mates", GE_PLAYER);
7236 GameEnds(WhiteWins, "White mates", GE_PLAYER);
7240 GameEnds(GameIsDrawn, "Stalemate", GE_PLAYER);
7245 case MachinePlaysBlack:
7246 case MachinePlaysWhite:
7247 /* disable certain menu options while machine is thinking */
7248 SetMachineThinkingEnables();
7255 userOfferedDraw = FALSE; // [HGM] drawclaim: after move made, and tested for claimable draw
7256 promoDefaultAltered = FALSE; // [HGM] fall back on default choice
7258 if(bookHit) { // [HGM] book: simulate book reply
7259 static char bookMove[MSG_SIZ]; // a bit generous?
7261 programStats.nodes = programStats.depth = programStats.time =
7262 programStats.score = programStats.got_only_move = 0;
7263 sprintf(programStats.movelist, "%s (xbook)", bookHit);
7265 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
7266 strcat(bookMove, bookHit);
7267 HandleMachineMove(bookMove, &first);
7273 MarkByFEN(char *fen)
7276 if(!appData.markers || !appData.highlightDragging) return;
7277 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) legal[r][f] = 0;
7278 r=BOARD_HEIGHT-1; f=BOARD_LEFT;
7282 if(*fen == 'M') legal[r][f] = 2; else // request promotion choice
7283 if(*fen >= 'A' && *fen <= 'Z') legal[r][f] = 1; else
7284 if(*fen >= 'a' && *fen <= 'z') *fen += 'A' - 'a';
7285 if(*fen == '/' && f > BOARD_LEFT) f = BOARD_LEFT, r--; else
7286 if(*fen == 'T') marker[r][f++] = 0; else
7287 if(*fen == 'Y') marker[r][f++] = 1; else
7288 if(*fen == 'G') marker[r][f++] = 3; else
7289 if(*fen == 'B') marker[r][f++] = 4; else
7290 if(*fen == 'C') marker[r][f++] = 5; else
7291 if(*fen == 'M') marker[r][f++] = 6; else
7292 if(*fen == 'W') marker[r][f++] = 7; else
7293 if(*fen == 'D') marker[r][f++] = 8; else
7294 if(*fen == 'R') marker[r][f++] = 2; else {
7295 while(*fen <= '9' && *fen >= '0') s = 10*s + *fen++ - '0';
7298 while(f >= BOARD_RGHT) f -= BOARD_RGHT - BOARD_LEFT, r--;
7302 DrawPosition(TRUE, NULL);
7305 static char baseMarker[BOARD_RANKS][BOARD_FILES], baseLegal[BOARD_RANKS][BOARD_FILES];
7308 Mark (Board board, int flags, ChessMove kind, int rf, int ff, int rt, int ft, VOIDSTAR closure)
7310 typedef char Markers[BOARD_RANKS][BOARD_FILES];
7311 Markers *m = (Markers *) closure;
7312 if(rf == fromY && ff == fromX && (killX < 0 ? !(rt == rf && ft == ff) && legNr & 1 : rt == killY && ft == killX || legNr & 2))
7313 (*m)[rt][ft] = 1 + (board[rt][ft] != EmptySquare
7314 || kind == WhiteCapturesEnPassant
7315 || kind == BlackCapturesEnPassant) + 3*(kind == FirstLeg && killX < 0), legal[rt][ft] = 1;
7316 else if(flags & F_MANDATORY_CAPTURE && board[rt][ft] != EmptySquare) (*m)[rt][ft] = 3, legal[rt][ft] = 1;
7319 static int hoverSavedValid;
7322 MarkTargetSquares (int clear)
7325 if(clear) { // no reason to ever suppress clearing
7326 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) sum += marker[y][x], marker[y][x] = 0;
7327 hoverSavedValid = 0;
7328 if(!sum) return; // nothing was cleared,no redraw needed
7331 if(!appData.markers || !appData.highlightDragging || appData.icsActive && gameInfo.variant < VariantShogi ||
7332 !appData.testLegality && !pieceDefs || gameMode == EditPosition) return;
7333 GenLegal(boards[currentMove], PosFlags(currentMove), Mark, (void*) marker, EmptySquare);
7334 if(PosFlags(0) & F_MANDATORY_CAPTURE) {
7335 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) if(marker[y][x]>1) capt++;
7337 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) if(marker[y][x] == 1) marker[y][x] = 0;
7340 DrawPosition(FALSE, NULL);
7344 Explode (Board board, int fromX, int fromY, int toX, int toY)
7346 if(gameInfo.variant == VariantAtomic &&
7347 (board[toY][toX] != EmptySquare || // capture?
7348 toX != fromX && (board[fromY][fromX] == WhitePawn || // e.p. ?
7349 board[fromY][fromX] == BlackPawn )
7351 AnimateAtomicCapture(board, fromX, fromY, toX, toY);
7357 ChessSquare gatingPiece = EmptySquare; // exported to front-end, for dragging
7360 CanPromote (ChessSquare piece, int y)
7362 int zone = (gameInfo.variant == VariantChuChess ? 3 : 1);
7363 if(gameMode == EditPosition) return FALSE; // no promotions when editing position
7364 // some variants have fixed promotion piece, no promotion at all, or another selection mechanism
7365 if(IS_SHOGI(gameInfo.variant) || gameInfo.variant == VariantXiangqi ||
7366 gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat ||
7367 gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier ||
7368 gameInfo.variant == VariantMakruk) return FALSE;
7369 return (piece == BlackPawn && y <= zone ||
7370 piece == WhitePawn && y >= BOARD_HEIGHT-1-zone ||
7371 piece == BlackLance && y <= zone ||
7372 piece == WhiteLance && y >= BOARD_HEIGHT-1-zone );
7376 HoverEvent (int xPix, int yPix, int x, int y)
7378 static int oldX = -1, oldY = -1, oldFromX = -1, oldFromY = -1;
7380 if(!first.highlight) return;
7381 if(fromX != oldFromX || fromY != oldFromY) oldX = oldY = -1; // kludge to fake entry on from-click
7382 if(x == oldX && y == oldY) return; // only do something if we enter new square
7383 oldFromX = fromX; oldFromY = fromY;
7384 if(oldX == -1 && oldY == -1 && x == fromX && y == fromY) { // record markings after from-change
7385 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++)
7386 baseMarker[r][f] = marker[r][f], baseLegal[r][f] = legal[r][f];
7387 hoverSavedValid = 1;
7388 } else if(oldX != x || oldY != y) {
7389 // [HGM] lift: entered new to-square; redraw arrow, and inform engine
7390 if(hoverSavedValid) // don't restore markers that are supposed to be cleared
7391 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++)
7392 marker[r][f] = baseMarker[r][f], legal[r][f] = baseLegal[r][f];
7393 if((marker[y][x] == 2 || marker[y][x] == 6) && legal[y][x]) {
7395 snprintf(buf, MSG_SIZ, "hover %c%d\n", x + AAA, y + ONE - '0');
7396 SendToProgram(buf, &first);
7399 // SetHighlights(fromX, fromY, x, y);
7403 void ReportClick(char *action, int x, int y)
7405 char buf[MSG_SIZ]; // Inform engine of what user does
7407 if(action[0] == 'l') // mark any target square of a lifted piece as legal to-square, clear markers
7408 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++)
7409 legal[r][f] = !pieceDefs || !appData.markers, marker[r][f] = 0;
7410 if(!first.highlight || gameMode == EditPosition) return;
7411 snprintf(buf, MSG_SIZ, "%s %c%d%s\n", action, x+AAA, y+ONE-'0', controlKey && action[0]=='p' ? "," : "");
7412 SendToProgram(buf, &first);
7415 Boolean right; // instructs front-end to use button-1 events as if they were button 3
7418 LeftClick (ClickType clickType, int xPix, int yPix)
7421 Boolean saveAnimate;
7422 static int second = 0, promotionChoice = 0, clearFlag = 0, sweepSelecting = 0;
7423 char promoChoice = NULLCHAR;
7425 static TimeMark lastClickTime, prevClickTime;
7427 x = EventToSquare(xPix, BOARD_WIDTH);
7428 y = EventToSquare(yPix, BOARD_HEIGHT);
7429 if (!flipView && y >= 0) {
7430 y = BOARD_HEIGHT - 1 - y;
7432 if (flipView && x >= 0) {
7433 x = BOARD_WIDTH - 1 - x;
7436 if(appData.monoMouse && gameMode == EditPosition && fromX < 0 && clickType == Press && boards[currentMove][y][x] == EmptySquare) {
7438 RightClick(clickType, xPix, yPix, &dummy, &dummy);
7443 if(SeekGraphClick(clickType, xPix, yPix, 0)) return;
7445 prevClickTime = lastClickTime; GetTimeMark(&lastClickTime);
7447 if (clickType == Press) ErrorPopDown();
7448 lastClickType = clickType, lastLeftX = xPix, lastLeftY = yPix; // [HGM] alien: remember state
7450 if(promoSweep != EmptySquare) { // up-click during sweep-select of promo-piece
7451 defaultPromoChoice = promoSweep;
7452 promoSweep = EmptySquare; // terminate sweep
7453 promoDefaultAltered = TRUE;
7454 if(!selectFlag && !sweepSelecting && (x != toX || y != toY)) x = fromX, y = fromY; // and fake up-click on same square if we were still selecting
7457 if(promotionChoice) { // we are waiting for a click to indicate promotion piece
7458 if(clickType == Release) return; // ignore upclick of click-click destination
7459 promotionChoice = FALSE; // only one chance: if click not OK it is interpreted as cancel
7460 if(appData.debugMode) fprintf(debugFP, "promotion click, x=%d, y=%d\n", x, y);
7461 if(gameInfo.holdingsWidth &&
7462 (WhiteOnMove(currentMove)
7463 ? x == BOARD_WIDTH-1 && y < gameInfo.holdingsSize && y >= 0
7464 : x == 0 && y >= BOARD_HEIGHT - gameInfo.holdingsSize && y < BOARD_HEIGHT) ) {
7465 // click in right holdings, for determining promotion piece
7466 ChessSquare p = boards[currentMove][y][x];
7467 if(appData.debugMode) fprintf(debugFP, "square contains %d\n", (int)p);
7468 if(p == WhitePawn || p == BlackPawn) p = EmptySquare; // [HGM] Pawns could be valid as deferral
7469 if(p != EmptySquare || gameInfo.variant == VariantGrand && toY != 0 && toY != BOARD_HEIGHT-1) { // [HGM] grand: empty square means defer
7470 FinishMove(NormalMove, fromX, fromY, toX, toY, p==EmptySquare ? NULLCHAR : ToLower(PieceToChar(p)));
7475 DrawPosition(FALSE, boards[currentMove]);
7479 /* [HGM] holdings: next 5 lines: ignore all clicks between board and holdings */
7480 if(clickType == Press
7481 && ( x == BOARD_LEFT-1 || x == BOARD_RGHT
7482 || x == BOARD_LEFT-2 && y < BOARD_HEIGHT-gameInfo.holdingsSize
7483 || x == BOARD_RGHT+1 && y >= gameInfo.holdingsSize) )
7486 if(gotPremove && x == premoveFromX && y == premoveFromY && clickType == Release) {
7487 // could be static click on premove from-square: abort premove
7489 ClearPremoveHighlights();
7492 if(clickType == Press && fromX == x && fromY == y && promoDefaultAltered && SubtractTimeMarks(&lastClickTime, &prevClickTime) >= 200)
7493 fromX = fromY = -1; // second click on piece after altering default promo piece treated as first click
7495 if(!promoDefaultAltered) { // determine default promotion piece, based on the side the user is moving for
7496 int side = (gameMode == IcsPlayingWhite || gameMode == MachinePlaysBlack ||
7497 gameMode != MachinePlaysWhite && gameMode != IcsPlayingBlack && WhiteOnMove(currentMove));
7498 defaultPromoChoice = DefaultPromoChoice(side);
7501 autoQueen = appData.alwaysPromoteToQueen;
7505 gatingPiece = EmptySquare;
7506 if (clickType != Press) {
7507 if(dragging) { // [HGM] from-square must have been reset due to game end since last press
7508 DragPieceEnd(xPix, yPix); dragging = 0;
7509 DrawPosition(FALSE, NULL);
7513 doubleClick = FALSE;
7514 if(gameMode == AnalyzeMode && (pausing || controlKey) && first.excludeMoves) { // use pause state to exclude moves
7515 doubleClick = TRUE; gatingPiece = boards[currentMove][y][x];
7517 fromX = x; fromY = y; toX = toY = killX = killY = -1;
7518 if(!appData.oneClick || !OnlyMove(&x, &y, FALSE) ||
7519 // even if only move, we treat as normal when this would trigger a promotion popup, to allow sweep selection
7520 appData.sweepSelect && CanPromote(boards[currentMove][fromY][fromX], fromY) && originalY != y) {
7522 if (OKToStartUserMove(fromX, fromY)) {
7524 ReportClick("lift", x, y);
7525 MarkTargetSquares(0);
7526 if(gameMode == EditPosition && controlKey) gatingPiece = boards[currentMove][fromY][fromX];
7527 DragPieceBegin(xPix, yPix, FALSE); dragging = 1;
7528 if(appData.sweepSelect && CanPromote(piece = boards[currentMove][fromY][fromX], fromY)) {
7529 promoSweep = defaultPromoChoice;
7530 selectFlag = 0; lastX = xPix; lastY = yPix;
7531 Sweep(0); // Pawn that is going to promote: preview promotion piece
7532 DisplayMessage("", _("Pull pawn backwards to under-promote"));
7534 if (appData.highlightDragging) {
7535 SetHighlights(fromX, fromY, -1, -1);
7539 } else fromX = fromY = -1;
7543 printf("to click %d,%d\n",x,y);
7545 if (clickType == Press && gameMode != EditPosition) {
7550 // ignore off-board to clicks
7551 if(y < 0 || x < 0) return;
7553 /* Check if clicking again on the same color piece */
7554 fromP = boards[currentMove][fromY][fromX];
7555 toP = boards[currentMove][y][x];
7556 frc = appData.fischerCastling || gameInfo.variant == VariantSChess;
7557 if( (killX < 0 || x != fromX || y != fromY) && // [HGM] lion: do not interpret igui as deselect!
7558 marker[y][x] == 0 && // if engine told we can move to here, do it even if own piece
7559 ((WhitePawn <= fromP && fromP <= WhiteKing &&
7560 WhitePawn <= toP && toP <= WhiteKing &&
7561 !(fromP == WhiteKing && toP == WhiteRook && frc) &&
7562 !(fromP == WhiteRook && toP == WhiteKing && frc)) ||
7563 (BlackPawn <= fromP && fromP <= BlackKing &&
7564 BlackPawn <= toP && toP <= BlackKing &&
7565 !(fromP == BlackRook && toP == BlackKing && frc) && // allow also RxK as FRC castling
7566 !(fromP == BlackKing && toP == BlackRook && frc)))) {
7567 /* Clicked again on same color piece -- changed his mind */
7568 second = (x == fromX && y == fromY);
7570 if(second && gameMode == AnalyzeMode && SubtractTimeMarks(&lastClickTime, &prevClickTime) < 200) {
7571 second = FALSE; // first double-click rather than scond click
7572 doubleClick = first.excludeMoves; // used by UserMoveEvent to recognize exclude moves
7574 promoDefaultAltered = FALSE;
7575 MarkTargetSquares(1);
7576 if(!(second && appData.oneClick && OnlyMove(&x, &y, TRUE))) {
7577 if (appData.highlightDragging) {
7578 SetHighlights(x, y, -1, -1);
7582 if (OKToStartUserMove(x, y)) {
7583 if(gameInfo.variant == VariantSChess && // S-Chess: back-rank piece selected after holdings means gating
7584 (fromX == BOARD_LEFT-2 || fromX == BOARD_RGHT+1) &&
7585 y == (toP < BlackPawn ? 0 : BOARD_HEIGHT-1))
7586 gatingPiece = boards[currentMove][fromY][fromX];
7587 else gatingPiece = doubleClick ? fromP : EmptySquare;
7589 fromY = y; dragging = 1;
7590 if(!second) ReportClick("lift", x, y);
7591 MarkTargetSquares(0);
7592 DragPieceBegin(xPix, yPix, FALSE);
7593 if(appData.sweepSelect && CanPromote(piece = boards[currentMove][y][x], y)) {
7594 promoSweep = defaultPromoChoice;
7595 selectFlag = 0; lastX = xPix; lastY = yPix;
7596 Sweep(0); // Pawn that is going to promote: preview promotion piece
7600 if(x == fromX && y == fromY) return; // if OnlyMove altered (x,y) we go on
7603 // ignore clicks on holdings
7604 if(x < BOARD_LEFT || x >= BOARD_RGHT) return;
7606 printf("A type=%d\n",clickType);
7608 if(x == fromX && y == fromY && gameMode == EditPosition && SubtractTimeMarks(&lastClickTime, &prevClickTime) < 200) {
7609 gatingPiece = boards[currentMove][fromY][fromX]; // prepare to copy rather than move
7613 if (clickType == Release && x == fromX && y == fromY && killX < 0 && !sweepSelecting) {
7614 DragPieceEnd(xPix, yPix); dragging = 0;
7616 // a deferred attempt to click-click move an empty square on top of a piece
7617 boards[currentMove][y][x] = EmptySquare;
7619 DrawPosition(FALSE, boards[currentMove]);
7620 fromX = fromY = -1; clearFlag = 0;
7623 if (appData.animateDragging) {
7624 /* Undo animation damage if any */
7625 DrawPosition(FALSE, NULL);
7628 /* Second up/down in same square; just abort move */
7631 gatingPiece = EmptySquare;
7632 MarkTargetSquares(1);
7635 ClearPremoveHighlights();
7637 /* First upclick in same square; start click-click mode */
7638 SetHighlights(x, y, -1, -1);
7645 if(gameMode != EditPosition && !appData.testLegality && !legal[y][x] &&
7646 fromX >= BOARD_LEFT && fromX < BOARD_RGHT && (x != killX || y != killY) && !sweepSelecting) {
7647 if(dragging) DragPieceEnd(xPix, yPix), dragging = 0;
7648 DisplayMessage(_("only marked squares are legal"),"");
7649 DrawPosition(TRUE, NULL);
7650 return; // ignore to-click
7652 printf("(%d,%d)-(%d,%d) %d %d\n",fromX,fromY,toX,toY,x,y);
7653 /* we now have a different from- and (possibly off-board) to-square */
7654 /* Completed move */
7655 if(!sweepSelecting) {
7660 piece = boards[currentMove][fromY][fromX];
7662 saveAnimate = appData.animate;
7663 if (clickType == Press) {
7664 if(gameInfo.variant == VariantChuChess && piece != WhitePawn && piece != BlackPawn) defaultPromoChoice = piece;
7665 if(gameMode == EditPosition && boards[currentMove][fromY][fromX] == EmptySquare) {
7666 // must be Edit Position mode with empty-square selected
7667 fromX = x; fromY = y; DragPieceBegin(xPix, yPix, FALSE); dragging = 1; // consider this a new attempt to drag
7668 if(x >= BOARD_LEFT && x < BOARD_RGHT) clearFlag = 1; // and defer click-click move of empty-square to up-click
7671 if(dragging == 2) { // [HGM] lion: just turn buttonless drag into normal drag, and let release to the job
7674 if(x == killX && y == killY) { // second click on this square, which was selected as first-leg target
7675 killX = killY = -1; // this informs us no second leg is coming, so treat as to-click without intermediate
7677 if(marker[y][x] == 5) return; // [HGM] lion: to-click on cyan square; defer action to release
7678 if(legal[y][x] == 2 || HasPromotionChoice(fromX, fromY, toX, toY, &promoChoice, FALSE)) {
7679 if(appData.sweepSelect) {
7680 promoSweep = defaultPromoChoice;
7681 if(gameInfo.variant != VariantChuChess && PieceToChar(CHUPROMOTED piece) == '+') promoSweep = CHUPROMOTED piece;
7682 selectFlag = 0; lastX = xPix; lastY = yPix;
7683 Sweep(0); // Pawn that is going to promote: preview promotion piece
7685 DisplayMessage("", _("Pull pawn backwards to under-promote"));
7686 MarkTargetSquares(1);
7688 return; // promo popup appears on up-click
7690 /* Finish clickclick move */
7691 if (appData.animate || appData.highlightLastMove) {
7692 SetHighlights(fromX, fromY, toX, toY);
7696 } else if(sweepSelecting) { // this must be the up-click corresponding to the down-click that started the sweep
7697 sweepSelecting = 0; appData.animate = FALSE; // do not animate, a selected piece already on to-square
7698 if (appData.animate || appData.highlightLastMove) {
7699 SetHighlights(fromX, fromY, toX, toY);
7705 // [HGM] this must be done after the move is made, as with arrow it could lead to a board redraw with piece still on from square
7706 /* Finish drag move */
7707 if (appData.highlightLastMove) {
7708 SetHighlights(fromX, fromY, toX, toY);
7713 if(gameInfo.variant == VariantChuChess && piece != WhitePawn && piece != BlackPawn) defaultPromoChoice = piece;
7714 if(marker[y][x] == 5) { // [HGM] lion: this was the release of a to-click or drag on a cyan square
7715 dragging *= 2; // flag button-less dragging if we are dragging
7716 MarkTargetSquares(1);
7717 if(x == killX && y == killY) killX = kill2X, killY = kill2Y, kill2X = kill2Y = -1; // cancel last kill
7719 kill2X = killX; kill2Y = killY;
7720 killX = x; killY = y; //remeber this square as intermediate
7721 ReportClick("put", x, y); // and inform engine
7722 ReportClick("lift", x, y);
7723 MarkTargetSquares(0);
7727 DragPieceEnd(xPix, yPix); dragging = 0;
7728 /* Don't animate move and drag both */
7729 appData.animate = FALSE;
7732 // moves into holding are invalid for now (except in EditPosition, adapting to-square)
7733 if(x >= 0 && x < BOARD_LEFT || x >= BOARD_RGHT) {
7734 ChessSquare piece = boards[currentMove][fromY][fromX];
7735 if(gameMode == EditPosition && piece != EmptySquare &&
7736 fromX >= BOARD_LEFT && fromX < BOARD_RGHT) {
7739 if(x == BOARD_LEFT-2 && piece >= BlackPawn) {
7740 n = PieceToNumber(piece - (int)BlackPawn);
7741 if(n >= gameInfo.holdingsSize) { n = 0; piece = BlackPawn; }
7742 boards[currentMove][BOARD_HEIGHT-1 - n][0] = piece;
7743 boards[currentMove][BOARD_HEIGHT-1 - n][1]++;
7745 if(x == BOARD_RGHT+1 && piece < BlackPawn) {
7746 n = PieceToNumber(piece);
7747 if(n >= gameInfo.holdingsSize) { n = 0; piece = WhitePawn; }
7748 boards[currentMove][n][BOARD_WIDTH-1] = piece;
7749 boards[currentMove][n][BOARD_WIDTH-2]++;
7751 boards[currentMove][fromY][fromX] = EmptySquare;
7755 MarkTargetSquares(1);
7756 DrawPosition(TRUE, boards[currentMove]);
7760 // off-board moves should not be highlighted
7761 if(x < 0 || y < 0) ClearHighlights();
7762 else ReportClick("put", x, y);
7764 if(gatingPiece != EmptySquare && gameInfo.variant == VariantSChess) promoChoice = ToLower(PieceToChar(gatingPiece));
7766 if(legal[toY][toX] == 2) promoChoice = ToLower(PieceToChar(defaultPromoChoice)); // highlight-induced promotion
7768 if (legal[toY][toX] == 2 && !appData.sweepSelect || HasPromotionChoice(fromX, fromY, toX, toY, &promoChoice, appData.sweepSelect)) {
7769 SetHighlights(fromX, fromY, toX, toY);
7770 MarkTargetSquares(1);
7771 if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand) {
7772 // [HGM] super: promotion to captured piece selected from holdings
7773 ChessSquare p = boards[currentMove][fromY][fromX], q = boards[currentMove][toY][toX];
7774 promotionChoice = TRUE;
7775 // kludge follows to temporarily execute move on display, without promoting yet
7776 boards[currentMove][fromY][fromX] = EmptySquare; // move Pawn to 8th rank
7777 boards[currentMove][toY][toX] = p;
7778 DrawPosition(FALSE, boards[currentMove]);
7779 boards[currentMove][fromY][fromX] = p; // take back, but display stays
7780 boards[currentMove][toY][toX] = q;
7781 DisplayMessage("Click in holdings to choose piece", "");
7784 PromotionPopUp(promoChoice);
7786 int oldMove = currentMove;
7787 UserMoveEvent(fromX, fromY, toX, toY, promoChoice);
7788 if (!appData.highlightLastMove || gotPremove) ClearHighlights();
7789 if (gotPremove) SetPremoveHighlights(fromX, fromY, toX, toY);
7790 if(saveAnimate && !appData.animate && currentMove != oldMove && // drag-move was performed
7791 Explode(boards[currentMove-1], fromX, fromY, toX, toY))
7792 DrawPosition(TRUE, boards[currentMove]);
7793 MarkTargetSquares(1);
7796 appData.animate = saveAnimate;
7797 if (appData.animate || appData.animateDragging) {
7798 /* Undo animation damage if needed */
7799 DrawPosition(FALSE, NULL);
7804 RightClick (ClickType action, int x, int y, int *fromX, int *fromY)
7805 { // front-end-free part taken out of PieceMenuPopup
7806 int whichMenu; int xSqr, ySqr;
7808 if(seekGraphUp) { // [HGM] seekgraph
7809 if(action == Press) SeekGraphClick(Press, x, y, 2); // 2 indicates right-click: no pop-down on miss
7810 if(action == Release) SeekGraphClick(Release, x, y, 2); // and no challenge on hit
7814 if((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)
7815 && !appData.zippyPlay && appData.bgObserve) { // [HGM] bughouse: show background game
7816 if(!partnerBoardValid) return -2; // suppress display of uninitialized boards
7817 if( appData.dualBoard) return -2; // [HGM] dual: is already displayed
7818 if(action == Press) {
7819 originalFlip = flipView;
7820 flipView = !flipView; // temporarily flip board to see game from partners perspective
7821 DrawPosition(TRUE, partnerBoard);
7822 DisplayMessage(partnerStatus, "");
7824 } else if(action == Release) {
7825 flipView = originalFlip;
7826 DrawPosition(TRUE, boards[currentMove]);
7832 xSqr = EventToSquare(x, BOARD_WIDTH);
7833 ySqr = EventToSquare(y, BOARD_HEIGHT);
7834 if (action == Release) {
7835 if(pieceSweep != EmptySquare) {
7836 EditPositionMenuEvent(pieceSweep, toX, toY);
7837 pieceSweep = EmptySquare;
7838 } else UnLoadPV(); // [HGM] pv
7840 if (action != Press) return -2; // return code to be ignored
7843 if(xSqr < BOARD_LEFT || xSqr >= BOARD_RGHT) return -1;
7845 if (xSqr == BOARD_LEFT-1 || xSqr == BOARD_RGHT) return -1;
7846 if (xSqr < 0 || ySqr < 0) return -1;
7847 if(appData.pieceMenu) { whichMenu = 0; break; } // edit-position menu
7848 pieceSweep = shiftKey ? BlackPawn : WhitePawn; // [HGM] sweep: prepare selecting piece by mouse sweep
7849 toX = xSqr; toY = ySqr; lastX = x, lastY = y;
7850 if(flipView) toX = BOARD_WIDTH - 1 - toX; else toY = BOARD_HEIGHT - 1 - toY;
7854 if(!appData.icsEngineAnalyze) return -1;
7855 case IcsPlayingWhite:
7856 case IcsPlayingBlack:
7857 if(!appData.zippyPlay) goto noZip;
7860 case MachinePlaysWhite:
7861 case MachinePlaysBlack:
7862 case TwoMachinesPlay: // [HGM] pv: use for showing PV
7863 if (!appData.dropMenu) {
7865 return 2; // flag front-end to grab mouse events
7867 if(gameMode == TwoMachinesPlay || gameMode == AnalyzeMode ||
7868 gameMode == AnalyzeFile || gameMode == IcsObserving) return -1;
7871 if (xSqr < 0 || ySqr < 0) return -1;
7872 if (!appData.dropMenu || appData.testLegality &&
7873 gameInfo.variant != VariantBughouse &&
7874 gameInfo.variant != VariantCrazyhouse) return -1;
7875 whichMenu = 1; // drop menu
7881 if (((*fromX = xSqr) < 0) ||
7882 ((*fromY = ySqr) < 0)) {
7883 *fromX = *fromY = -1;
7887 *fromX = BOARD_WIDTH - 1 - *fromX;
7889 *fromY = BOARD_HEIGHT - 1 - *fromY;
7895 SendProgramStatsToFrontend (ChessProgramState * cps, ChessProgramStats * cpstats)
7897 // char * hint = lastHint;
7898 FrontEndProgramStats stats;
7900 stats.which = cps == &first ? 0 : 1;
7901 stats.depth = cpstats->depth;
7902 stats.nodes = cpstats->nodes;
7903 stats.score = cpstats->score;
7904 stats.time = cpstats->time;
7905 stats.pv = cpstats->movelist;
7906 stats.hint = lastHint;
7907 stats.an_move_index = 0;
7908 stats.an_move_count = 0;
7910 if( gameMode == AnalyzeMode || gameMode == AnalyzeFile ) {
7911 stats.hint = cpstats->move_name;
7912 stats.an_move_index = cpstats->nr_moves - cpstats->moves_left;
7913 stats.an_move_count = cpstats->nr_moves;
7916 if(stats.pv && stats.pv[0]) safeStrCpy(lastPV[stats.which], stats.pv, sizeof(lastPV[stats.which])/sizeof(lastPV[stats.which][0])); // [HGM] pv: remember last PV of each
7918 SetProgramStats( &stats );
7922 ClearEngineOutputPane (int which)
7924 static FrontEndProgramStats dummyStats;
7925 dummyStats.which = which;
7926 dummyStats.pv = "#";
7927 SetProgramStats( &dummyStats );
7930 #define MAXPLAYERS 500
7933 TourneyStandings (int display)
7935 int i, w, b, color, wScore, bScore, dummy, nr=0, nPlayers=0;
7936 int score[MAXPLAYERS], ranking[MAXPLAYERS], points[MAXPLAYERS], games[MAXPLAYERS];
7937 char result, *p, *names[MAXPLAYERS];
7939 if(appData.tourneyType < 0 && !strchr(appData.results, '*'))
7940 return strdup(_("Swiss tourney finished")); // standings of Swiss yet TODO
7941 names[0] = p = strdup(appData.participants);
7942 while(p = strchr(p, '\n')) *p++ = NULLCHAR, names[++nPlayers] = p; // count participants
7944 for(i=0; i<nPlayers; i++) score[i] = games[i] = 0;
7946 while(result = appData.results[nr]) {
7947 color = Pairing(nr, nPlayers, &w, &b, &dummy);
7948 if(!(color ^ matchGame & 1)) { dummy = w; w = b; b = dummy; }
7949 wScore = bScore = 0;
7951 case '+': wScore = 2; break;
7952 case '-': bScore = 2; break;
7953 case '=': wScore = bScore = 1; break;
7955 case '*': return strdup("busy"); // tourney not finished
7963 if(appData.tourneyType > 0) nPlayers = appData.tourneyType; // in gauntlet, list only gauntlet engine(s)
7964 for(w=0; w<nPlayers; w++) {
7966 for(i=0; i<nPlayers; i++) if(score[i] > bScore) bScore = score[i], b = i;
7967 ranking[w] = b; points[w] = bScore; score[b] = -2;
7969 p = malloc(nPlayers*34+1);
7970 for(w=0; w<nPlayers && w<display; w++)
7971 sprintf(p+34*w, "%2d. %5.1f/%-3d %-19.19s\n", w+1, points[w]/2., games[ranking[w]], names[ranking[w]]);
7977 Count (Board board, int pCnt[], int *nW, int *nB, int *wStale, int *bStale, int *bishopColor)
7978 { // count all piece types
7980 *nB = *nW = *wStale = *bStale = *bishopColor = 0;
7981 for(p=WhitePawn; p<=EmptySquare; p++) pCnt[p] = 0;
7982 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
7985 if(p == WhitePawn && r == BOARD_HEIGHT-1) (*wStale)++; else
7986 if(p == BlackPawn && r == 0) (*bStale)++; // count last-Rank Pawns (XQ) separately
7987 if(p <= WhiteKing) (*nW)++; else if(p <= BlackKing) (*nB)++;
7988 if(p == WhiteBishop || p == WhiteFerz || p == WhiteAlfil ||
7989 p == BlackBishop || p == BlackFerz || p == BlackAlfil )
7990 *bishopColor |= 1 << ((f^r)&1); // track square color of color-bound pieces
7995 SufficientDefence (int pCnt[], int side, int nMine, int nHis)
7997 int myPawns = pCnt[WhitePawn+side]; // my total Pawn count;
7998 int majorDefense = pCnt[BlackRook-side] + pCnt[BlackCannon-side] + pCnt[BlackKnight-side];
8000 nMine -= pCnt[WhiteFerz+side] + pCnt[WhiteAlfil+side]; // discount defenders
8001 if(nMine - myPawns > 2) return FALSE; // no trivial draws with more than 1 major
8002 if(myPawns == 2 && nMine == 3) // KPP
8003 return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] >= 3;
8004 if(myPawns == 1 && nMine == 2) // KP
8005 return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] + pCnt[BlackPawn-side] >= 1;
8006 if(myPawns == 1 && nMine == 3 && pCnt[WhiteKnight+side]) // KHP
8007 return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side]*2 >= 5;
8008 if(myPawns) return FALSE;
8009 if(pCnt[WhiteRook+side])
8010 return pCnt[BlackRook-side] ||
8011 pCnt[BlackCannon-side] && (pCnt[BlackFerz-side] >= 2 || pCnt[BlackAlfil-side] >= 2) ||
8012 pCnt[BlackKnight-side] && pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] > 2 ||
8013 pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] >= 4;
8014 if(pCnt[WhiteCannon+side]) {
8015 if(pCnt[WhiteFerz+side] + myPawns == 0) return TRUE; // Cannon needs platform
8016 return majorDefense || pCnt[BlackAlfil-side] >= 2;
8018 if(pCnt[WhiteKnight+side])
8019 return majorDefense || pCnt[BlackFerz-side] >= 2 || pCnt[BlackAlfil-side] + pCnt[BlackPawn-side] >= 1;
8024 MatingPotential (int pCnt[], int side, int nMine, int nHis, int stale, int bisColor)
8026 VariantClass v = gameInfo.variant;
8028 if(v == VariantShogi || v == VariantCrazyhouse || v == VariantBughouse) return TRUE; // drop games always winnable
8029 if(v == VariantShatranj) return TRUE; // always winnable through baring
8030 if(v == VariantLosers || v == VariantSuicide || v == VariantGiveaway) return TRUE;
8031 if(v == Variant3Check || v == VariantAtomic) return nMine > 1; // can win through checking / exploding King
8033 if(v == VariantXiangqi) {
8034 int majors = 5*pCnt[BlackKnight-side] + 7*pCnt[BlackCannon-side] + 7*pCnt[BlackRook-side];
8036 nMine -= pCnt[WhiteFerz+side] + pCnt[WhiteAlfil+side] + stale; // discount defensive pieces and back-rank Pawns
8037 if(nMine + stale == 1) return (pCnt[BlackFerz-side] > 1 && pCnt[BlackKnight-side] > 0); // bare K can stalemate KHAA (!)
8038 if(nMine > 2) return TRUE; // if we don't have P, H or R, we must have CC
8039 if(nMine == 2 && pCnt[WhiteCannon+side] == 0) return TRUE; // We have at least one P, H or R
8040 // if we get here, we must have KC... or KP..., possibly with additional A, E or last-rank P
8041 if(stale) // we have at least one last-rank P plus perhaps C
8042 return majors // KPKX
8043 || pCnt[BlackFerz-side] && pCnt[BlackFerz-side] + pCnt[WhiteCannon+side] + stale > 2; // KPKAA, KPPKA and KCPKA
8045 return pCnt[WhiteFerz+side] // KCAK
8046 || pCnt[WhiteAlfil+side] && pCnt[BlackRook-side] + pCnt[BlackCannon-side] + pCnt[BlackFerz-side] // KCEKA, KCEKX (X!=H)
8047 || majors + (12*pCnt[BlackFerz-side] | 6*pCnt[BlackAlfil-side]) > 16; // KCKAA, KCKAX, KCKEEX, KCKEXX (XX!=HH), KCKXXX
8048 // TO DO: cases wih an unpromoted f-Pawn acting as platform for an opponent Cannon
8050 } else if(v == VariantKnightmate) {
8051 if(nMine == 1) return FALSE;
8052 if(nMine == 2 && nHis == 1 && pCnt[WhiteBishop+side] + pCnt[WhiteFerz+side] + pCnt[WhiteKnight+side]) return FALSE; // KBK is only draw
8053 } else if(pCnt[WhiteKing] == 1 && pCnt[BlackKing] == 1) { // other variants with orthodox Kings
8054 int nBishops = pCnt[WhiteBishop+side] + pCnt[WhiteFerz+side];
8056 if(nMine == 1) return FALSE; // bare King
8057 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
8058 nMine += (nBishops > 0) - nBishops; // By now all Bishops (and Ferz) on like-colored squares, so count as one
8059 if(nMine > 2 && nMine != pCnt[WhiteAlfil+side] + 1) return TRUE; // At least two pieces, not all Alfils
8060 // by now we have King + 1 piece (or multiple Bishops on the same color)
8061 if(pCnt[WhiteKnight+side])
8062 return (pCnt[BlackKnight-side] + pCnt[BlackBishop-side] + pCnt[BlackMan-side] +
8063 pCnt[BlackWazir-side] + pCnt[BlackSilver-side] + bisColor // KNKN, KNKB, KNKF, KNKE, KNKW, KNKM, KNKS
8064 || nHis > 3); // be sure to cover suffocation mates in corner (e.g. KNKQCA)
8066 return (pCnt[BlackKnight-side]); // KBKN, KFKN
8067 if(pCnt[WhiteAlfil+side])
8068 return (nHis > 2); // Alfils can in general not reach a corner square, but there might be edge (suffocation) mates
8069 if(pCnt[WhiteWazir+side])
8070 return (pCnt[BlackKnight-side] + pCnt[BlackWazir-side] + pCnt[BlackAlfil-side]); // KWKN, KWKW, KWKE
8077 CompareWithRights (Board b1, Board b2)
8080 if(!CompareBoards(b1, b2)) return FALSE;
8081 if(b1[EP_STATUS] != b2[EP_STATUS]) return FALSE;
8082 /* compare castling rights */
8083 if( b1[CASTLING][2] != b2[CASTLING][2] && (b2[CASTLING][0] != NoRights || b2[CASTLING][1] != NoRights) )
8084 rights++; /* King lost rights, while rook still had them */
8085 if( b1[CASTLING][2] != NoRights ) { /* king has rights */
8086 if( b1[CASTLING][0] != b2[CASTLING][0] || b1[CASTLING][1] != b2[CASTLING][1] )
8087 rights++; /* but at least one rook lost them */
8089 if( b1[CASTLING][5] != b1[CASTLING][5] && (b2[CASTLING][3] != NoRights || b2[CASTLING][4] != NoRights) )
8091 if( b1[CASTLING][5] != NoRights ) {
8092 if( b1[CASTLING][3] != b2[CASTLING][3] || b1[CASTLING][4] != b2[CASTLING][4] )
8099 Adjudicate (ChessProgramState *cps)
8100 { // [HGM] some adjudications useful with buggy engines
8101 // [HGM] adjudicate: made into separate routine, which now can be called after every move
8102 // In any case it determnes if the game is a claimable draw (filling in EP_STATUS).
8103 // Actually ending the game is now based on the additional internal condition canAdjudicate.
8104 // Only when the game is ended, and the opponent is a computer, this opponent gets the move relayed.
8105 int k, drop, count = 0; static int bare = 1;
8106 ChessProgramState *engineOpponent = (gameMode == TwoMachinesPlay ? cps->other : (cps ? NULL : &first));
8107 Boolean canAdjudicate = !appData.icsActive;
8109 // most tests only when we understand the game, i.e. legality-checking on
8110 if( appData.testLegality )
8111 { /* [HGM] Some more adjudications for obstinate engines */
8112 int nrW, nrB, bishopColor, staleW, staleB, nr[EmptySquare+1], i;
8113 static int moveCount = 6;
8115 char *reason = NULL;
8117 /* Count what is on board. */
8118 Count(boards[forwardMostMove], nr, &nrW, &nrB, &staleW, &staleB, &bishopColor);
8120 /* Some material-based adjudications that have to be made before stalemate test */
8121 if(gameInfo.variant == VariantAtomic && nr[WhiteKing] + nr[BlackKing] < 2) {
8122 // [HGM] atomic: stm must have lost his King on previous move, as destroying own K is illegal
8123 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE; // make claimable as if stm is checkmated
8124 if(canAdjudicate && appData.checkMates) {
8126 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets move
8127 GameEnds( WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins,
8128 "Xboard adjudication: King destroyed", GE_XBOARD );
8133 /* Bare King in Shatranj (loses) or Losers (wins) */
8134 if( nrW == 1 || nrB == 1) {
8135 if( gameInfo.variant == VariantLosers) { // [HGM] losers: bare King wins (stm must have it first)
8136 boards[forwardMostMove][EP_STATUS] = EP_WINS; // mark as win, so it becomes claimable
8137 if(canAdjudicate && appData.checkMates) {
8139 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets to see move
8140 GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
8141 "Xboard adjudication: Bare king", GE_XBOARD );
8145 if( gameInfo.variant == VariantShatranj && --bare < 0)
8147 boards[forwardMostMove][EP_STATUS] = EP_WINS; // make claimable as win for stm
8148 if(canAdjudicate && appData.checkMates) {
8149 /* but only adjudicate if adjudication enabled */
8151 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets move
8152 GameEnds( nrW > 1 ? WhiteWins : nrB > 1 ? BlackWins : GameIsDrawn,
8153 "Xboard adjudication: Bare king", GE_XBOARD );
8160 // don't wait for engine to announce game end if we can judge ourselves
8161 switch (MateTest(boards[forwardMostMove], PosFlags(forwardMostMove)) ) {
8163 if(gameInfo.variant == Variant3Check) { // [HGM] 3check: when in check, test if 3rd time
8164 int i, checkCnt = 0; // (should really be done by making nr of checks part of game state)
8165 for(i=forwardMostMove-2; i>=backwardMostMove; i-=2) {
8166 if(MateTest(boards[i], PosFlags(i)) == MT_CHECK)
8169 reason = "Xboard adjudication: 3rd check";
8170 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE;
8181 reason = "Xboard adjudication: Stalemate";
8182 if((signed char)boards[forwardMostMove][EP_STATUS] != EP_CHECKMATE) { // [HGM] don't touch win through baring or K-capt
8183 boards[forwardMostMove][EP_STATUS] = EP_STALEMATE; // default result for stalemate is draw
8184 if(gameInfo.variant == VariantLosers || gameInfo.variant == VariantGiveaway) // [HGM] losers:
8185 boards[forwardMostMove][EP_STATUS] = EP_WINS; // in these variants stalemated is always a win
8186 else if(gameInfo.variant == VariantSuicide) // in suicide it depends
8187 boards[forwardMostMove][EP_STATUS] = nrW == nrB ? EP_STALEMATE :
8188 ((nrW < nrB) != WhiteOnMove(forwardMostMove) ?
8189 EP_CHECKMATE : EP_WINS);
8190 else if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantXiangqi || gameInfo.variant == VariantShogi)
8191 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE; // and in these variants being stalemated loses
8195 reason = "Xboard adjudication: Checkmate";
8196 boards[forwardMostMove][EP_STATUS] = (gameInfo.variant == VariantLosers ? EP_WINS : EP_CHECKMATE);
8197 if(gameInfo.variant == VariantShogi) {
8198 if(forwardMostMove > backwardMostMove
8199 && moveList[forwardMostMove-1][1] == '@'
8200 && CharToPiece(ToUpper(moveList[forwardMostMove-1][0])) == WhitePawn) {
8201 reason = "XBoard adjudication: pawn-drop mate";
8202 boards[forwardMostMove][EP_STATUS] = EP_WINS;
8208 switch(i = (signed char)boards[forwardMostMove][EP_STATUS]) {
8210 result = GameIsDrawn; break;
8212 result = WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins; break;
8214 result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins; break;
8218 if(canAdjudicate && appData.checkMates && result) { // [HGM] mates: adjudicate finished games if requested
8220 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
8221 GameEnds( result, reason, GE_XBOARD );
8225 /* Next absolutely insufficient mating material. */
8226 if(!MatingPotential(nr, WhitePawn, nrW, nrB, staleW, bishopColor) &&
8227 !MatingPotential(nr, BlackPawn, nrB, nrW, staleB, bishopColor))
8228 { /* includes KBK, KNK, KK of KBKB with like Bishops */
8230 /* always flag draws, for judging claims */
8231 boards[forwardMostMove][EP_STATUS] = EP_INSUF_DRAW;
8233 if(canAdjudicate && appData.materialDraws) {
8234 /* but only adjudicate them if adjudication enabled */
8235 if(engineOpponent) {
8236 SendToProgram("force\n", engineOpponent); // suppress reply
8237 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see last move */
8239 GameEnds( GameIsDrawn, "Xboard adjudication: Insufficient mating material", GE_XBOARD );
8244 /* Then some trivial draws (only adjudicate, cannot be claimed) */
8245 if(gameInfo.variant == VariantXiangqi ?
8246 SufficientDefence(nr, WhitePawn, nrW, nrB) && SufficientDefence(nr, BlackPawn, nrB, nrW)
8248 ( nr[WhiteRook] == 1 && nr[BlackRook] == 1 /* KRKR */
8249 || nr[WhiteQueen] && nr[BlackQueen]==1 /* KQKQ */
8250 || nr[WhiteKnight]==2 || nr[BlackKnight]==2 /* KNNK */
8251 || nr[WhiteKnight]+nr[WhiteBishop] == 1 && nr[BlackKnight]+nr[BlackBishop] == 1 /* KBKN, KBKB, KNKN */
8253 if(--moveCount < 0 && appData.trivialDraws && canAdjudicate)
8254 { /* if the first 3 moves do not show a tactical win, declare draw */
8255 if(engineOpponent) {
8256 SendToProgram("force\n", engineOpponent); // suppress reply
8257 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
8259 GameEnds( GameIsDrawn, "Xboard adjudication: Trivial draw", GE_XBOARD );
8262 } else moveCount = 6;
8265 // Repetition draws and 50-move rule can be applied independently of legality testing
8267 /* Check for rep-draws */
8269 drop = gameInfo.holdingsSize && (gameInfo.variant != VariantSuper && gameInfo.variant != VariantSChess
8270 && gameInfo.variant != VariantGreat && gameInfo.variant != VariantGrand);
8271 for(k = forwardMostMove-2;
8272 k>=backwardMostMove && k>=forwardMostMove-100 && (drop ||
8273 (signed char)boards[k][EP_STATUS] < EP_UNKNOWN &&
8274 (signed char)boards[k+2][EP_STATUS] <= EP_NONE && (signed char)boards[k+1][EP_STATUS] <= EP_NONE);
8277 if(CompareBoards(boards[k], boards[forwardMostMove])) {
8278 /* compare castling rights */
8279 if( boards[forwardMostMove][CASTLING][2] != boards[k][CASTLING][2] &&
8280 (boards[k][CASTLING][0] != NoRights || boards[k][CASTLING][1] != NoRights) )
8281 rights++; /* King lost rights, while rook still had them */
8282 if( boards[forwardMostMove][CASTLING][2] != NoRights ) { /* king has rights */
8283 if( boards[forwardMostMove][CASTLING][0] != boards[k][CASTLING][0] ||
8284 boards[forwardMostMove][CASTLING][1] != boards[k][CASTLING][1] )
8285 rights++; /* but at least one rook lost them */
8287 if( boards[forwardMostMove][CASTLING][5] != boards[k][CASTLING][5] &&
8288 (boards[k][CASTLING][3] != NoRights || boards[k][CASTLING][4] != NoRights) )
8290 if( boards[forwardMostMove][CASTLING][5] != NoRights ) {
8291 if( boards[forwardMostMove][CASTLING][3] != boards[k][CASTLING][3] ||
8292 boards[forwardMostMove][CASTLING][4] != boards[k][CASTLING][4] )
8295 if( rights == 0 && ++count > appData.drawRepeats-2 && canAdjudicate
8296 && appData.drawRepeats > 1) {
8297 /* adjudicate after user-specified nr of repeats */
8298 int result = GameIsDrawn;
8299 char *details = "XBoard adjudication: repetition draw";
8300 if((gameInfo.variant == VariantXiangqi || gameInfo.variant == VariantShogi) && appData.testLegality) {
8301 // [HGM] xiangqi: check for forbidden perpetuals
8302 int m, ourPerpetual = 1, hisPerpetual = 1;
8303 for(m=forwardMostMove; m>k; m-=2) {
8304 if(MateTest(boards[m], PosFlags(m)) != MT_CHECK)
8305 ourPerpetual = 0; // the current mover did not always check
8306 if(MateTest(boards[m-1], PosFlags(m-1)) != MT_CHECK)
8307 hisPerpetual = 0; // the opponent did not always check
8309 if(appData.debugMode) fprintf(debugFP, "XQ perpetual test, our=%d, his=%d\n",
8310 ourPerpetual, hisPerpetual);
8311 if(ourPerpetual && !hisPerpetual) { // we are actively checking him: forfeit
8312 result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
8313 details = "Xboard adjudication: perpetual checking";
8315 if(hisPerpetual && !ourPerpetual) { // he is checking us, but did not repeat yet
8316 break; // (or we would have caught him before). Abort repetition-checking loop.
8318 if(gameInfo.variant == VariantShogi) { // in Shogi other repetitions are draws
8319 if(BOARD_HEIGHT == 5 && BOARD_RGHT - BOARD_LEFT == 5) { // but in mini-Shogi gote wins!
8321 details = "Xboard adjudication: repetition";
8323 } else // it must be XQ
8324 // Now check for perpetual chases
8325 if(!ourPerpetual && !hisPerpetual) { // no perpetual check, test for chase
8326 hisPerpetual = PerpetualChase(k, forwardMostMove);
8327 ourPerpetual = PerpetualChase(k+1, forwardMostMove);
8328 if(ourPerpetual && !hisPerpetual) { // we are actively chasing him: forfeit
8329 static char resdet[MSG_SIZ];
8330 result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
8332 snprintf(resdet, MSG_SIZ, "Xboard adjudication: perpetual chasing of %c%c", ourPerpetual>>8, ourPerpetual&255);
8334 if(hisPerpetual && !ourPerpetual) // he is chasing us, but did not repeat yet
8335 break; // Abort repetition-checking loop.
8337 // if neither of us is checking or chasing all the time, or both are, it is draw
8339 if(engineOpponent) {
8340 SendToProgram("force\n", engineOpponent); // suppress reply
8341 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
8343 GameEnds( result, details, GE_XBOARD );
8346 if( rights == 0 && count > 1 ) /* occurred 2 or more times before */
8347 boards[forwardMostMove][EP_STATUS] = EP_REP_DRAW;
8351 /* Now we test for 50-move draws. Determine ply count */
8352 count = forwardMostMove;
8353 /* look for last irreversble move */
8354 while( (signed char)boards[count][EP_STATUS] <= EP_NONE && count > backwardMostMove )
8356 /* if we hit starting position, add initial plies */
8357 if( count == backwardMostMove )
8358 count -= initialRulePlies;
8359 count = forwardMostMove - count;
8360 if(gameInfo.variant == VariantXiangqi && ( count >= 100 || count >= 2*appData.ruleMoves ) ) {
8361 // adjust reversible move counter for checks in Xiangqi
8362 int i = forwardMostMove - count, inCheck = 0, lastCheck;
8363 if(i < backwardMostMove) i = backwardMostMove;
8364 while(i <= forwardMostMove) {
8365 lastCheck = inCheck; // check evasion does not count
8366 inCheck = (MateTest(boards[i], PosFlags(i)) == MT_CHECK);
8367 if(inCheck || lastCheck) count--; // check does not count
8372 boards[forwardMostMove][EP_STATUS] = EP_RULE_DRAW;
8373 /* this is used to judge if draw claims are legal */
8374 if(canAdjudicate && appData.ruleMoves > 0 && count >= 2*appData.ruleMoves) {
8375 if(engineOpponent) {
8376 SendToProgram("force\n", engineOpponent); // suppress reply
8377 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
8379 GameEnds( GameIsDrawn, "Xboard adjudication: 50-move rule", GE_XBOARD );
8383 /* if draw offer is pending, treat it as a draw claim
8384 * when draw condition present, to allow engines a way to
8385 * claim draws before making their move to avoid a race
8386 * condition occurring after their move
8388 if((gameMode == TwoMachinesPlay ? second.offeredDraw : userOfferedDraw) || first.offeredDraw ) {
8390 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_RULE_DRAW)
8391 p = "Draw claim: 50-move rule";
8392 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_REP_DRAW)
8393 p = "Draw claim: 3-fold repetition";
8394 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_INSUF_DRAW)
8395 p = "Draw claim: insufficient mating material";
8396 if( p != NULL && canAdjudicate) {
8397 if(engineOpponent) {
8398 SendToProgram("force\n", engineOpponent); // suppress reply
8399 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
8401 GameEnds( GameIsDrawn, p, GE_XBOARD );
8406 if( canAdjudicate && appData.adjudicateDrawMoves > 0 && forwardMostMove > (2*appData.adjudicateDrawMoves) ) {
8407 if(engineOpponent) {
8408 SendToProgram("force\n", engineOpponent); // suppress reply
8409 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
8411 GameEnds( GameIsDrawn, "Xboard adjudication: long game", GE_XBOARD );
8417 typedef int (CDECL *PPROBE_EGBB) (int player, int *piece, int *square);
8418 typedef int (CDECL *PLOAD_EGBB) (char *path, int cache_size, int load_options);
8419 static int egbbCode[] = { 6, 5, 4, 3, 2, 1 };
8424 int pieces[10], squares[10], cnt=0, r, f, res;
8426 static PPROBE_EGBB probeBB;
8427 if(!appData.testLegality) return 10;
8428 if(BOARD_HEIGHT != 8 || BOARD_RGHT-BOARD_LEFT != 8) return 12;
8429 if(gameInfo.holdingsSize && gameInfo.variant != VariantSuper && gameInfo.variant != VariantSChess) return 12;
8430 if(loaded == 2 && forwardMostMove < 2) loaded = 0; // retry on new game
8431 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
8432 ChessSquare piece = boards[forwardMostMove][r][f];
8433 int black = (piece >= BlackPawn);
8434 int type = piece - black*BlackPawn;
8435 if(piece == EmptySquare) continue;
8436 if(type != WhiteKing && type > WhiteQueen) return 12; // unorthodox piece
8437 if(type == WhiteKing) type = WhiteQueen + 1;
8438 type = egbbCode[type];
8439 squares[cnt] = r*(BOARD_RGHT - BOARD_LEFT) + f - BOARD_LEFT;
8440 pieces[cnt] = type + black*6;
8441 if(++cnt > 5) return 11;
8443 pieces[cnt] = squares[cnt] = 0;
8445 if(loaded == 2) return 13; // loading failed before
8447 char *p, *path = strstr(appData.egtFormats, "scorpio:"), buf[MSG_SIZ];
8450 loaded = 2; // prepare for failure
8451 if(!path) return 13; // no egbb installed
8452 strncpy(buf, path + 8, MSG_SIZ);
8453 if(p = strchr(buf, ',')) *p = NULLCHAR; else p = buf + strlen(buf);
8454 snprintf(p, MSG_SIZ - strlen(buf), "%c%s", SLASH, EGBB_NAME);
8455 lib = LoadLibrary(buf);
8456 if(!lib) { DisplayError(_("could not load EGBB library"), 0); return 13; }
8457 loadBB = (PLOAD_EGBB) GetProcAddress(lib, "load_egbb_xmen");
8458 probeBB = (PPROBE_EGBB) GetProcAddress(lib, "probe_egbb_xmen");
8459 if(!loadBB || !probeBB) { DisplayError(_("wrong EGBB version"), 0); return 13; }
8460 p[1] = NULLCHAR; loadBB(buf, 64*1028, 2); // 2 = SMART_LOAD
8461 loaded = 1; // success!
8463 res = probeBB(forwardMostMove & 1, pieces, squares);
8464 return res > 0 ? 1 : res < 0 ? -1 : 0;
8468 SendMoveToBookUser (int moveNr, ChessProgramState *cps, int initial)
8469 { // [HGM] book: this routine intercepts moves to simulate book replies
8470 char *bookHit = NULL;
8472 if(cps->drawDepth && BitbaseProbe() == 0) { // [HG} egbb: reduce depth in drawn position
8474 snprintf(buf, MSG_SIZ, "sd %d\n", cps->drawDepth);
8475 SendToProgram(buf, cps);
8477 //first determine if the incoming move brings opponent into his book
8478 if(appData.usePolyglotBook && (cps == &first ? !appData.firstHasOwnBookUCI : !appData.secondHasOwnBookUCI))
8479 bookHit = ProbeBook(moveNr+1, appData.polyglotBook); // returns move
8480 if(appData.debugMode) fprintf(debugFP, "book hit = %s\n", bookHit ? bookHit : "(NULL)");
8481 if(bookHit != NULL && !cps->bookSuspend) {
8482 // make sure opponent is not going to reply after receiving move to book position
8483 SendToProgram("force\n", cps);
8484 cps->bookSuspend = TRUE; // flag indicating it has to be restarted
8486 if(bookHit) setboardSpoiledMachineBlack = FALSE; // suppress 'go' in SendMoveToProgram
8487 if(!initial) SendMoveToProgram(moveNr, cps); // with hit on initial position there is no move
8488 // now arrange restart after book miss
8490 // after a book hit we never send 'go', and the code after the call to this routine
8491 // has '&& !bookHit' added to suppress potential sending there (based on 'firstMove').
8492 char buf[MSG_SIZ], *move = bookHit;
8494 int fromX, fromY, toX, toY;
8498 if (ParseOneMove(bookHit, forwardMostMove, &moveType,
8499 &fromX, &fromY, &toX, &toY, &promoChar)) {
8500 (void) CoordsToAlgebraic(boards[forwardMostMove],
8501 PosFlags(forwardMostMove),
8502 fromY, fromX, toY, toX, promoChar, move);
8504 if(appData.debugMode) fprintf(debugFP, "Book move could not be parsed\n");
8508 snprintf(buf, MSG_SIZ, "%s%s\n", (cps->useUsermove ? "usermove " : ""), move); // force book move into program supposed to play it
8509 SendToProgram(buf, cps);
8510 if(!initial) firstMove = FALSE; // normally we would clear the firstMove condition after return & sending 'go'
8511 } else if(initial) { // 'go' was needed irrespective of firstMove, and it has to be done in this routine
8512 SendToProgram("go\n", cps);
8513 cps->bookSuspend = FALSE; // after a 'go' we are never suspended
8514 } else { // 'go' might be sent based on 'firstMove' after this routine returns
8515 if(cps->bookSuspend && !firstMove) // 'go' needed, and it will not be done after we return
8516 SendToProgram("go\n", cps);
8517 cps->bookSuspend = FALSE; // anyhow, we will not be suspended after a miss
8519 return bookHit; // notify caller of hit, so it can take action to send move to opponent
8523 LoadError (char *errmess, ChessProgramState *cps)
8524 { // unloads engine and switches back to -ncp mode if it was first
8525 if(cps->initDone) return FALSE;
8526 cps->isr = NULL; // this should suppress further error popups from breaking pipes
8527 DestroyChildProcess(cps->pr, 9 ); // just to be sure
8530 appData.noChessProgram = TRUE;
8531 gameMode = MachinePlaysBlack; ModeHighlight(); // kludge to unmark Machine Black menu
8532 gameMode = BeginningOfGame; ModeHighlight();
8535 if(GetDelayedEvent()) CancelDelayedEvent(), ThawUI(); // [HGM] cancel remaining loading effort scheduled after feature timeout
8536 DisplayMessage("", ""); // erase waiting message
8537 if(errmess) DisplayError(errmess, 0); // announce reason, if given
8542 ChessProgramState *savedState;
8544 DeferredBookMove (void)
8546 if(savedState->lastPing != savedState->lastPong)
8547 ScheduleDelayedEvent(DeferredBookMove, 10);
8549 HandleMachineMove(savedMessage, savedState);
8552 static int savedWhitePlayer, savedBlackPlayer, pairingReceived;
8553 static ChessProgramState *stalledEngine;
8554 static char stashedInputMove[MSG_SIZ];
8557 HandleMachineMove (char *message, ChessProgramState *cps)
8559 static char firstLeg[20];
8560 char machineMove[MSG_SIZ], buf1[MSG_SIZ*10], buf2[MSG_SIZ];
8561 char realname[MSG_SIZ];
8562 int fromX, fromY, toX, toY;
8564 char promoChar, roar;
8566 int machineWhite, oldError;
8569 if(cps == &pairing && sscanf(message, "%d-%d", &savedWhitePlayer, &savedBlackPlayer) == 2) {
8570 // [HGM] pairing: Mega-hack! Pairing engine also uses this routine (so it could give other WB commands).
8571 if(savedWhitePlayer == 0 || savedBlackPlayer == 0) {
8572 DisplayError(_("Invalid pairing from pairing engine"), 0);
8575 pairingReceived = 1;
8577 return; // Skim the pairing messages here.
8580 oldError = cps->userError; cps->userError = 0;
8582 FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book hit
8584 * Kludge to ignore BEL characters
8586 while (*message == '\007') message++;
8589 * [HGM] engine debug message: ignore lines starting with '#' character
8591 if(cps->debug && *message == '#') return;
8594 * Look for book output
8596 if (cps == &first && bookRequested) {
8597 if (message[0] == '\t' || message[0] == ' ') {
8598 /* Part of the book output is here; append it */
8599 strcat(bookOutput, message);
8600 strcat(bookOutput, " \n");
8602 } else if (bookOutput[0] != NULLCHAR) {
8603 /* All of book output has arrived; display it */
8604 char *p = bookOutput;
8605 while (*p != NULLCHAR) {
8606 if (*p == '\t') *p = ' ';
8609 DisplayInformation(bookOutput);
8610 bookRequested = FALSE;
8611 /* Fall through to parse the current output */
8616 * Look for machine move.
8618 if ((sscanf(message, "%s %s %s", buf1, buf2, machineMove) == 3 && strcmp(buf2, "...") == 0) ||
8619 (sscanf(message, "%s %s", buf1, machineMove) == 2 && strcmp(buf1, "move") == 0))
8621 if(pausing && !cps->pause) { // for pausing engine that does not support 'pause', we stash its move for processing when we resume.
8622 if(appData.debugMode) fprintf(debugFP, "pause %s engine after move\n", cps->which);
8623 safeStrCpy(stashedInputMove, message, MSG_SIZ);
8624 stalledEngine = cps;
8625 if(appData.ponderNextMove) { // bring opponent out of ponder
8626 if(gameMode == TwoMachinesPlay) {
8627 if(cps->other->pause)
8628 PauseEngine(cps->other);
8630 SendToProgram("easy\n", cps->other);
8637 /* This method is only useful on engines that support ping */
8638 if (cps->lastPing != cps->lastPong) {
8639 if (gameMode == BeginningOfGame) {
8640 /* Extra move from before last new; ignore */
8641 if (appData.debugMode) {
8642 fprintf(debugFP, "Ignoring extra move from %s\n", cps->which);
8645 if (appData.debugMode) {
8646 fprintf(debugFP, "Undoing extra move from %s, gameMode %d\n",
8647 cps->which, gameMode);
8650 SendToProgram("undo\n", cps);
8656 case BeginningOfGame:
8657 /* Extra move from before last reset; ignore */
8658 if (appData.debugMode) {
8659 fprintf(debugFP, "Ignoring extra move from %s\n", cps->which);
8666 /* Extra move after we tried to stop. The mode test is
8667 not a reliable way of detecting this problem, but it's
8668 the best we can do on engines that don't support ping.
8670 if (appData.debugMode) {
8671 fprintf(debugFP, "Undoing extra move from %s, gameMode %d\n",
8672 cps->which, gameMode);
8674 SendToProgram("undo\n", cps);
8677 case MachinePlaysWhite:
8678 case IcsPlayingWhite:
8679 machineWhite = TRUE;
8682 case MachinePlaysBlack:
8683 case IcsPlayingBlack:
8684 machineWhite = FALSE;
8687 case TwoMachinesPlay:
8688 machineWhite = (cps->twoMachinesColor[0] == 'w');
8691 if (WhiteOnMove(forwardMostMove) != machineWhite) {
8692 if (appData.debugMode) {
8694 "Ignoring move out of turn by %s, gameMode %d"
8695 ", forwardMost %d\n",
8696 cps->which, gameMode, forwardMostMove);
8701 if(cps->alphaRank) AlphaRank(machineMove, 4);
8703 // [HGM] lion: (some very limited) support for Alien protocol
8704 killX = killY = kill2X = kill2Y = -1;
8705 if(machineMove[strlen(machineMove)-1] == ',') { // move ends in coma: non-final leg of composite move
8706 safeStrCpy(firstLeg, machineMove, 20); // just remember it for processing when second leg arrives
8709 if(p = strchr(machineMove, ',')) { // we got both legs in one (happens on book move)
8710 safeStrCpy(firstLeg, machineMove, 20); // kludge: fake we received the first leg earlier, and clip it off
8711 safeStrCpy(machineMove, firstLeg + (p - machineMove) + 1, 20);
8713 if(firstLeg[0]) { // there was a previous leg;
8714 // only support case where same piece makes two step
8715 char buf[20], *p = machineMove+1, *q = buf+1, f;
8716 safeStrCpy(buf, machineMove, 20);
8717 while(isdigit(*q)) q++; // find start of to-square
8718 safeStrCpy(machineMove, firstLeg, 20);
8719 while(isdigit(*p)) p++; // to-square of first leg (which is now copied to machineMove)
8720 if(*p == *buf) // if first-leg to not equal to second-leg from first leg says unmodified (assume it ia King move of castling)
8721 safeStrCpy(p, q, 20); // glue to-square of second leg to from-square of first, to process over-all move
8722 sscanf(buf, "%c%d", &f, &killY); killX = f - AAA; killY -= ONE - '0'; // pass intermediate square to MakeMove in global
8723 firstLeg[0] = NULLCHAR;
8726 if (!ParseOneMove(machineMove, forwardMostMove, &moveType,
8727 &fromX, &fromY, &toX, &toY, &promoChar)) {
8728 /* Machine move could not be parsed; ignore it. */
8729 snprintf(buf1, MSG_SIZ*10, _("Illegal move \"%s\" from %s machine"),
8730 machineMove, _(cps->which));
8731 DisplayMoveError(buf1);
8732 snprintf(buf1, MSG_SIZ*10, "Xboard: Forfeit due to invalid move: %s (%c%c%c%c via %c%c, %c%c) res=%d",
8733 machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, killX+AAA, killY+ONE, kill2X+AAA, kill2Y+ONE, moveType);
8734 if (gameMode == TwoMachinesPlay) {
8735 GameEnds(machineWhite ? BlackWins : WhiteWins,
8741 /* [HGM] Apparently legal, but so far only tested with EP_UNKOWN */
8742 /* So we have to redo legality test with true e.p. status here, */
8743 /* to make sure an illegal e.p. capture does not slip through, */
8744 /* to cause a forfeit on a justified illegal-move complaint */
8745 /* of the opponent. */
8746 if( gameMode==TwoMachinesPlay && appData.testLegality ) {
8748 moveType = LegalityTest(boards[forwardMostMove], PosFlags(forwardMostMove),
8749 fromY, fromX, toY, toX, promoChar);
8750 if(moveType == IllegalMove) {
8751 snprintf(buf1, MSG_SIZ*10, "Xboard: Forfeit due to illegal move: %s (%c%c%c%c)%c",
8752 machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, 0);
8753 GameEnds(machineWhite ? BlackWins : WhiteWins,
8756 } else if(!appData.fischerCastling)
8757 /* [HGM] Kludge to handle engines that send FRC-style castling
8758 when they shouldn't (like TSCP-Gothic) */
8760 case WhiteASideCastleFR:
8761 case BlackASideCastleFR:
8763 currentMoveString[2]++;
8765 case WhiteHSideCastleFR:
8766 case BlackHSideCastleFR:
8768 currentMoveString[2]--;
8770 default: ; // nothing to do, but suppresses warning of pedantic compilers
8773 hintRequested = FALSE;
8774 lastHint[0] = NULLCHAR;
8775 bookRequested = FALSE;
8776 /* Program may be pondering now */
8777 cps->maybeThinking = TRUE;
8778 if (cps->sendTime == 2) cps->sendTime = 1;
8779 if (cps->offeredDraw) cps->offeredDraw--;
8781 /* [AS] Save move info*/
8782 pvInfoList[ forwardMostMove ].score = programStats.score;
8783 pvInfoList[ forwardMostMove ].depth = programStats.depth;
8784 pvInfoList[ forwardMostMove ].time = programStats.time; // [HGM] PGNtime: take time from engine stats
8786 MakeMove(fromX, fromY, toX, toY, promoChar);/*updates forwardMostMove*/
8788 /* Test suites abort the 'game' after one move */
8789 if(*appData.finger) {
8791 char *fen = PositionToFEN(backwardMostMove, NULL, 0); // no counts in EPD
8792 if(!f) f = fopen(appData.finger, "w");
8793 if(f) fprintf(f, "%s bm %s;\n", fen, parseList[backwardMostMove]), fflush(f);
8794 else { DisplayFatalError("Bad output file", errno, 0); return; }
8796 GameEnds(GameUnfinished, NULL, GE_XBOARD);
8799 if(solvingTime >= 0) {
8800 snprintf(buf1, MSG_SIZ, "%d. %4.2fs\n", matchGame, solvingTime/100.);
8801 totalTime += solvingTime; first.matchWins++;
8803 snprintf(buf1, MSG_SIZ, "%d. wrong (%s)\n", matchGame, parseList[backwardMostMove]);
8806 OutputKibitz(2, buf1);
8807 GameEnds(GameUnfinished, NULL, GE_XBOARD);
8810 /* [AS] Adjudicate game if needed (note: remember that forwardMostMove now points past the last move) */
8811 if( gameMode == TwoMachinesPlay && appData.adjudicateLossThreshold != 0 && forwardMostMove >= adjudicateLossPlies ) {
8814 while( count < adjudicateLossPlies ) {
8815 int score = pvInfoList[ forwardMostMove - count - 1 ].score;
8818 score = -score; /* Flip score for winning side */
8821 if( score > appData.adjudicateLossThreshold ) {
8828 if( count >= adjudicateLossPlies ) {
8829 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
8831 GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
8832 "Xboard adjudication",
8839 if(Adjudicate(cps)) {
8840 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
8841 return; // [HGM] adjudicate: for all automatic game ends
8845 if ((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack) &&
8847 if(cps->offeredDraw && (signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
8848 SendToICS(ics_prefix); // [HGM] drawclaim: send caim and move on one line for FICS
8850 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
8852 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
8854 if(appData.autoKibitz && !appData.icsEngineAnalyze ) { /* [HGM] kibitz: send most-recent PV info to ICS */
8855 char buf[3*MSG_SIZ];
8857 snprintf(buf, 3*MSG_SIZ, "kibitz !!! %+.2f/%d (%.2f sec, %u nodes, %.0f knps) PV=%s\n",
8858 programStats.score / 100.,
8860 programStats.time / 100.,
8861 (unsigned int)programStats.nodes,
8862 (unsigned int)programStats.nodes / (10*abs(programStats.time) + 1.),
8863 programStats.movelist);
8869 /* [AS] Clear stats for next move */
8870 ClearProgramStats();
8871 thinkOutput[0] = NULLCHAR;
8872 hiddenThinkOutputState = 0;
8875 if (gameMode == TwoMachinesPlay) {
8876 /* [HGM] relaying draw offers moved to after reception of move */
8877 /* and interpreting offer as claim if it brings draw condition */
8878 if (cps->offeredDraw == 1 && cps->other->sendDrawOffers) {
8879 SendToProgram("draw\n", cps->other);
8881 if (cps->other->sendTime) {
8882 SendTimeRemaining(cps->other,
8883 cps->other->twoMachinesColor[0] == 'w');
8885 bookHit = SendMoveToBookUser(forwardMostMove-1, cps->other, FALSE);
8886 if (firstMove && !bookHit) {
8888 if (cps->other->useColors) {
8889 SendToProgram(cps->other->twoMachinesColor, cps->other);
8891 SendToProgram("go\n", cps->other);
8893 cps->other->maybeThinking = TRUE;
8896 roar = (killX >= 0 && IS_LION(boards[forwardMostMove][toY][toX]));
8898 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
8900 if (!pausing && appData.ringBellAfterMoves) {
8901 if(!roar) RingBell();
8905 * Reenable menu items that were disabled while
8906 * machine was thinking
8908 if (gameMode != TwoMachinesPlay)
8909 SetUserThinkingEnables();
8911 // [HGM] book: after book hit opponent has received move and is now in force mode
8912 // force the book reply into it, and then fake that it outputted this move by jumping
8913 // back to the beginning of HandleMachineMove, with cps toggled and message set to this move
8915 static char bookMove[MSG_SIZ]; // a bit generous?
8917 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
8918 strcat(bookMove, bookHit);
8921 programStats.nodes = programStats.depth = programStats.time =
8922 programStats.score = programStats.got_only_move = 0;
8923 sprintf(programStats.movelist, "%s (xbook)", bookHit);
8925 if(cps->lastPing != cps->lastPong) {
8926 savedMessage = message; // args for deferred call
8928 ScheduleDelayedEvent(DeferredBookMove, 10);
8937 /* Set special modes for chess engines. Later something general
8938 * could be added here; for now there is just one kludge feature,
8939 * needed because Crafty 15.10 and earlier don't ignore SIGINT
8940 * when "xboard" is given as an interactive command.
8942 if (strncmp(message, "kibitz Hello from Crafty", 24) == 0) {
8943 cps->useSigint = FALSE;
8944 cps->useSigterm = FALSE;
8946 if (strncmp(message, "feature ", 8) == 0) { // [HGM] moved forward to pre-empt non-compliant commands
8947 ParseFeatures(message+8, cps);
8948 return; // [HGM] This return was missing, causing option features to be recognized as non-compliant commands!
8951 if (!strncmp(message, "setup ", 6) &&
8952 (!appData.testLegality || gameInfo.variant == VariantFairy || gameInfo.variant == VariantUnknown ||
8953 NonStandardBoardSize(gameInfo.variant, gameInfo.boardWidth, gameInfo.boardHeight, gameInfo.holdingsSize))
8954 ) { // [HGM] allow first engine to define opening position
8955 int dummy, w, h, hand, s=6; char buf[MSG_SIZ], varName[MSG_SIZ];
8956 if(appData.icsActive || forwardMostMove != 0 || cps != &first) return;
8958 if(sscanf(message, "setup (%s", buf) == 1) {
8959 s = 8 + strlen(buf), buf[s-9] = NULLCHAR, SetCharTableEsc(pieceToChar, buf, SUFFIXES);
8960 ASSIGN(appData.pieceToCharTable, buf);
8962 dummy = sscanf(message+s, "%dx%d+%d_%s", &w, &h, &hand, varName);
8964 while(message[s] && message[s++] != ' ');
8965 if(BOARD_HEIGHT != h || BOARD_WIDTH != w + 4*(hand != 0) || gameInfo.holdingsSize != hand ||
8966 dummy == 4 && gameInfo.variant != StringToVariant(varName) ) { // engine wants to change board format or variant
8967 appData.NrFiles = w; appData.NrRanks = h; appData.holdingsSize = hand;
8968 if(dummy == 4) gameInfo.variant = StringToVariant(varName); // parent variant
8969 InitPosition(1); // calls InitDrawingSizes to let new parameters take effect
8970 if(*buf) SetCharTableEsc(pieceToChar, buf, SUFFIXES); // do again, for it was spoiled by InitPosition
8971 startedFromSetupPosition = FALSE;
8974 if(startedFromSetupPosition) return;
8975 ParseFEN(boards[0], &dummy, message+s, FALSE);
8976 DrawPosition(TRUE, boards[0]);
8977 CopyBoard(initialPosition, boards[0]);
8978 startedFromSetupPosition = TRUE;
8981 if(sscanf(message, "piece %s %s", buf2, buf1) == 2) {
8982 ChessSquare piece = WhitePawn;
8983 char *p=buf2, *q, *s = SUFFIXES, ID = *p;
8984 if(*p == '+') piece = CHUPROMOTED WhitePawn, ID = *++p;
8985 if(q = strchr(s, p[1])) ID += 64*(q - s + 1), p++;
8986 piece += CharToPiece(ID) - WhitePawn;
8987 if(cps != &first || appData.testLegality && *engineVariant == NULLCHAR
8988 /* always accept definition of */ && piece != WhiteFalcon && piece != BlackFalcon
8989 /* wild-card pieces. */ && piece != WhiteCobra && piece != BlackCobra
8990 /* For variants we don't have */ && gameInfo.variant != VariantBerolina
8991 /* correct rules for, we cannot */ && gameInfo.variant != VariantCylinder
8992 /* enforce legality on our own! */ && gameInfo.variant != VariantUnknown
8993 && gameInfo.variant != VariantGreat
8994 && gameInfo.variant != VariantFairy ) return;
8995 if(piece < EmptySquare) {
8997 ASSIGN(pieceDesc[piece], buf1);
8998 if(isupper(*p) && p[1] == '&') { ASSIGN(pieceDesc[WHITE_TO_BLACK piece], buf1); }
9002 /* [HGM] Allow engine to set up a position. Don't ask me why one would
9003 * want this, I was asked to put it in, and obliged.
9005 if (!strncmp(message, "setboard ", 9)) {
9006 Board initial_position;
9008 GameEnds(GameUnfinished, "Engine aborts game", GE_XBOARD);
9010 if (!ParseFEN(initial_position, &blackPlaysFirst, message + 9, FALSE)) {
9011 DisplayError(_("Bad FEN received from engine"), 0);
9015 CopyBoard(boards[0], initial_position);
9016 initialRulePlies = FENrulePlies;
9017 if(blackPlaysFirst) gameMode = MachinePlaysWhite;
9018 else gameMode = MachinePlaysBlack;
9019 DrawPosition(FALSE, boards[currentMove]);
9025 * Look for communication commands
9027 if (!strncmp(message, "telluser ", 9)) {
9028 if(message[9] == '\\' && message[10] == '\\')
9029 EscapeExpand(message+9, message+11); // [HGM] esc: allow escape sequences in popup box
9031 DisplayNote(message + 9);
9034 if (!strncmp(message, "tellusererror ", 14)) {
9036 if(message[14] == '\\' && message[15] == '\\')
9037 EscapeExpand(message+14, message+16); // [HGM] esc: allow escape sequences in popup box
9039 DisplayError(message + 14, 0);
9042 if (!strncmp(message, "tellopponent ", 13)) {
9043 if (appData.icsActive) {
9045 snprintf(buf1, sizeof(buf1), "%ssay %s\n", ics_prefix, message + 13);
9049 DisplayNote(message + 13);
9053 if (!strncmp(message, "tellothers ", 11)) {
9054 if (appData.icsActive) {
9056 snprintf(buf1, sizeof(buf1), "%swhisper %s\n", ics_prefix, message + 11);
9059 } else if(appData.autoComment) AppendComment (forwardMostMove, message + 11, 1); // in local mode, add as move comment
9062 if (!strncmp(message, "tellall ", 8)) {
9063 if (appData.icsActive) {
9065 snprintf(buf1, sizeof(buf1), "%skibitz %s\n", ics_prefix, message + 8);
9069 DisplayNote(message + 8);
9073 if (strncmp(message, "warning", 7) == 0) {
9074 /* Undocumented feature, use tellusererror in new code */
9075 DisplayError(message, 0);
9078 if (sscanf(message, "askuser %s %[^\n]", buf1, buf2) == 2) {
9079 safeStrCpy(realname, cps->tidy, sizeof(realname)/sizeof(realname[0]));
9080 strcat(realname, " query");
9081 AskQuestion(realname, buf2, buf1, cps->pr);
9084 /* Commands from the engine directly to ICS. We don't allow these to be
9085 * sent until we are logged on. Crafty kibitzes have been known to
9086 * interfere with the login process.
9089 if (!strncmp(message, "tellics ", 8)) {
9090 SendToICS(message + 8);
9094 if (!strncmp(message, "tellicsnoalias ", 15)) {
9095 SendToICS(ics_prefix);
9096 SendToICS(message + 15);
9100 /* The following are for backward compatibility only */
9101 if (!strncmp(message,"whisper",7) || !strncmp(message,"kibitz",6) ||
9102 !strncmp(message,"draw",4) || !strncmp(message,"tell",3)) {
9103 SendToICS(ics_prefix);
9109 if (sscanf(message, "pong %d", &cps->lastPong) == 1) {
9110 if(initPing == cps->lastPong) {
9111 if(gameInfo.variant == VariantUnknown) {
9112 DisplayError(_("Engine did not send setup for non-standard variant"), 0);
9113 *engineVariant = NULLCHAR; appData.variant = VariantNormal; // back to normal as error recovery?
9114 GameEnds(GameUnfinished, NULL, GE_XBOARD);
9120 if(!strncmp(message, "highlight ", 10)) {
9121 if(appData.testLegality && !*engineVariant && appData.markers) return;
9122 MarkByFEN(message+10); // [HGM] alien: allow engine to mark board squares
9125 if(!strncmp(message, "click ", 6)) {
9126 char f, c=0; int x, y; // [HGM] alien: allow engine to finish user moves (i.e. engine-driven one-click moving)
9127 if(appData.testLegality || !appData.oneClick) return;
9128 sscanf(message+6, "%c%d%c", &f, &y, &c);
9129 x = f - 'a' + BOARD_LEFT, y -= ONE - '0';
9130 if(flipView) x = BOARD_WIDTH-1 - x; else y = BOARD_HEIGHT-1 - y;
9131 x = x*squareSize + (x+1)*lineGap + squareSize/2;
9132 y = y*squareSize + (y+1)*lineGap + squareSize/2;
9133 f = first.highlight; first.highlight = 0; // kludge to suppress lift/put in response to own clicks
9134 if(lastClickType == Press) // if button still down, fake release on same square, to be ready for next click
9135 LeftClick(Release, lastLeftX, lastLeftY);
9136 controlKey = (c == ',');
9137 LeftClick(Press, x, y);
9138 LeftClick(Release, x, y);
9139 first.highlight = f;
9143 * If the move is illegal, cancel it and redraw the board.
9144 * Also deal with other error cases. Matching is rather loose
9145 * here to accommodate engines written before the spec.
9147 if (strncmp(message + 1, "llegal move", 11) == 0 ||
9148 strncmp(message, "Error", 5) == 0) {
9149 if (StrStr(message, "name") ||
9150 StrStr(message, "rating") || StrStr(message, "?") ||
9151 StrStr(message, "result") || StrStr(message, "board") ||
9152 StrStr(message, "bk") || StrStr(message, "computer") ||
9153 StrStr(message, "variant") || StrStr(message, "hint") ||
9154 StrStr(message, "random") || StrStr(message, "depth") ||
9155 StrStr(message, "accepted")) {
9158 if (StrStr(message, "protover")) {
9159 /* Program is responding to input, so it's apparently done
9160 initializing, and this error message indicates it is
9161 protocol version 1. So we don't need to wait any longer
9162 for it to initialize and send feature commands. */
9163 FeatureDone(cps, 1);
9164 cps->protocolVersion = 1;
9167 cps->maybeThinking = FALSE;
9169 if (StrStr(message, "draw")) {
9170 /* Program doesn't have "draw" command */
9171 cps->sendDrawOffers = 0;
9174 if (cps->sendTime != 1 &&
9175 (StrStr(message, "time") || StrStr(message, "otim"))) {
9176 /* Program apparently doesn't have "time" or "otim" command */
9180 if (StrStr(message, "analyze")) {
9181 cps->analysisSupport = FALSE;
9182 cps->analyzing = FALSE;
9183 // Reset(FALSE, TRUE); // [HGM] this caused discrepancy between display and internal state!
9184 EditGameEvent(); // [HGM] try to preserve loaded game
9185 snprintf(buf2,MSG_SIZ, _("%s does not support analysis"), cps->tidy);
9186 DisplayError(buf2, 0);
9189 if (StrStr(message, "(no matching move)st")) {
9190 /* Special kludge for GNU Chess 4 only */
9191 cps->stKludge = TRUE;
9192 SendTimeControl(cps, movesPerSession, timeControl,
9193 timeIncrement, appData.searchDepth,
9197 if (StrStr(message, "(no matching move)sd")) {
9198 /* Special kludge for GNU Chess 4 only */
9199 cps->sdKludge = TRUE;
9200 SendTimeControl(cps, movesPerSession, timeControl,
9201 timeIncrement, appData.searchDepth,
9205 if (!StrStr(message, "llegal")) {
9208 if (gameMode == BeginningOfGame || gameMode == EndOfGame ||
9209 gameMode == IcsIdle) return;
9210 if (forwardMostMove <= backwardMostMove) return;
9211 if (pausing) PauseEvent();
9212 if(appData.forceIllegal) {
9213 // [HGM] illegal: machine refused move; force position after move into it
9214 SendToProgram("force\n", cps);
9215 if(!cps->useSetboard) { // hideous kludge on kludge, because SendBoard sucks.
9216 // we have a real problem now, as SendBoard will use the a2a3 kludge
9217 // when black is to move, while there might be nothing on a2 or black
9218 // might already have the move. So send the board as if white has the move.
9219 // But first we must change the stm of the engine, as it refused the last move
9220 SendBoard(cps, 0); // always kludgeless, as white is to move on boards[0]
9221 if(WhiteOnMove(forwardMostMove)) {
9222 SendToProgram("a7a6\n", cps); // for the engine black still had the move
9223 SendBoard(cps, forwardMostMove); // kludgeless board
9225 SendToProgram("a2a3\n", cps); // for the engine white still had the move
9226 CopyBoard(boards[forwardMostMove+1], boards[forwardMostMove]);
9227 SendBoard(cps, forwardMostMove+1); // kludgeless board
9229 } else SendBoard(cps, forwardMostMove); // FEN case, also sets stm properly
9230 if(gameMode == MachinePlaysWhite || gameMode == MachinePlaysBlack ||
9231 gameMode == TwoMachinesPlay)
9232 SendToProgram("go\n", cps);
9235 if (gameMode == PlayFromGameFile) {
9236 /* Stop reading this game file */
9237 gameMode = EditGame;
9240 /* [HGM] illegal-move claim should forfeit game when Xboard */
9241 /* only passes fully legal moves */
9242 if( appData.testLegality && gameMode == TwoMachinesPlay ) {
9243 GameEnds( cps->twoMachinesColor[0] == 'w' ? BlackWins : WhiteWins,
9244 "False illegal-move claim", GE_XBOARD );
9245 return; // do not take back move we tested as valid
9247 currentMove = forwardMostMove-1;
9248 DisplayMove(currentMove-1); /* before DisplayMoveError */
9249 SwitchClocks(forwardMostMove-1); // [HGM] race
9250 DisplayBothClocks();
9251 snprintf(buf1, 10*MSG_SIZ, _("Illegal move \"%s\" (rejected by %s chess program)"),
9252 parseList[currentMove], _(cps->which));
9253 DisplayMoveError(buf1);
9254 DrawPosition(FALSE, boards[currentMove]);
9256 SetUserThinkingEnables();
9259 if (strncmp(message, "time", 4) == 0 && StrStr(message, "Illegal")) {
9260 /* Program has a broken "time" command that
9261 outputs a string not ending in newline.
9265 if (cps->pseudo) { // [HGM] pseudo-engine, granted unusual powers
9266 if (sscanf(message, "wtime %ld\n", &whiteTimeRemaining) == 1 || // adjust clock times
9267 sscanf(message, "btime %ld\n", &blackTimeRemaining) == 1 ) return;
9271 * If chess program startup fails, exit with an error message.
9272 * Attempts to recover here are futile. [HGM] Well, we try anyway
9274 if ((StrStr(message, "unknown host") != NULL)
9275 || (StrStr(message, "No remote directory") != NULL)
9276 || (StrStr(message, "not found") != NULL)
9277 || (StrStr(message, "No such file") != NULL)
9278 || (StrStr(message, "can't alloc") != NULL)
9279 || (StrStr(message, "Permission denied") != NULL)) {
9281 cps->maybeThinking = FALSE;
9282 snprintf(buf1, sizeof(buf1), _("Failed to start %s chess program %s on %s: %s\n"),
9283 _(cps->which), cps->program, cps->host, message);
9284 RemoveInputSource(cps->isr);
9285 if(appData.icsActive) DisplayFatalError(buf1, 0, 1); else {
9286 if(LoadError(oldError ? NULL : buf1, cps)) return; // error has then been handled by LoadError
9287 if(!oldError) DisplayError(buf1, 0); // if reason neatly announced, suppress general error popup
9293 * Look for hint output
9295 if (sscanf(message, "Hint: %s", buf1) == 1) {
9296 if (cps == &first && hintRequested) {
9297 hintRequested = FALSE;
9298 if (ParseOneMove(buf1, forwardMostMove, &moveType,
9299 &fromX, &fromY, &toX, &toY, &promoChar)) {
9300 (void) CoordsToAlgebraic(boards[forwardMostMove],
9301 PosFlags(forwardMostMove),
9302 fromY, fromX, toY, toX, promoChar, buf1);
9303 snprintf(buf2, sizeof(buf2), _("Hint: %s"), buf1);
9304 DisplayInformation(buf2);
9306 /* Hint move could not be parsed!? */
9307 snprintf(buf2, sizeof(buf2),
9308 _("Illegal hint move \"%s\"\nfrom %s chess program"),
9309 buf1, _(cps->which));
9310 DisplayError(buf2, 0);
9313 safeStrCpy(lastHint, buf1, sizeof(lastHint)/sizeof(lastHint[0]));
9319 * Ignore other messages if game is not in progress
9321 if (gameMode == BeginningOfGame || gameMode == EndOfGame ||
9322 gameMode == IcsIdle || cps->lastPing != cps->lastPong) return;
9325 * look for win, lose, draw, or draw offer
9327 if (strncmp(message, "1-0", 3) == 0) {
9328 char *p, *q, *r = "";
9329 p = strchr(message, '{');
9337 GameEnds(WhiteWins, r, GE_ENGINE1 + (cps != &first)); /* [HGM] pass claimer indication for claim test */
9339 } else if (strncmp(message, "0-1", 3) == 0) {
9340 char *p, *q, *r = "";
9341 p = strchr(message, '{');
9349 /* Kludge for Arasan 4.1 bug */
9350 if (strcmp(r, "Black resigns") == 0) {
9351 GameEnds(WhiteWins, r, GE_ENGINE1 + (cps != &first));
9354 GameEnds(BlackWins, r, GE_ENGINE1 + (cps != &first));
9356 } else if (strncmp(message, "1/2", 3) == 0) {
9357 char *p, *q, *r = "";
9358 p = strchr(message, '{');
9367 GameEnds(GameIsDrawn, r, GE_ENGINE1 + (cps != &first));
9370 } else if (strncmp(message, "White resign", 12) == 0) {
9371 GameEnds(BlackWins, "White resigns", GE_ENGINE1 + (cps != &first));
9373 } else if (strncmp(message, "Black resign", 12) == 0) {
9374 GameEnds(WhiteWins, "Black resigns", GE_ENGINE1 + (cps != &first));
9376 } else if (strncmp(message, "White matches", 13) == 0 ||
9377 strncmp(message, "Black matches", 13) == 0 ) {
9378 /* [HGM] ignore GNUShogi noises */
9380 } else if (strncmp(message, "White", 5) == 0 &&
9381 message[5] != '(' &&
9382 StrStr(message, "Black") == NULL) {
9383 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
9385 } else if (strncmp(message, "Black", 5) == 0 &&
9386 message[5] != '(') {
9387 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
9389 } else if (strcmp(message, "resign") == 0 ||
9390 strcmp(message, "computer resigns") == 0) {
9392 case MachinePlaysBlack:
9393 case IcsPlayingBlack:
9394 GameEnds(WhiteWins, "Black resigns", GE_ENGINE);
9396 case MachinePlaysWhite:
9397 case IcsPlayingWhite:
9398 GameEnds(BlackWins, "White resigns", GE_ENGINE);
9400 case TwoMachinesPlay:
9401 if (cps->twoMachinesColor[0] == 'w')
9402 GameEnds(BlackWins, "White resigns", GE_ENGINE1 + (cps != &first));
9404 GameEnds(WhiteWins, "Black resigns", GE_ENGINE1 + (cps != &first));
9411 } else if (strncmp(message, "opponent mates", 14) == 0) {
9413 case MachinePlaysBlack:
9414 case IcsPlayingBlack:
9415 GameEnds(WhiteWins, "White mates", GE_ENGINE);
9417 case MachinePlaysWhite:
9418 case IcsPlayingWhite:
9419 GameEnds(BlackWins, "Black mates", GE_ENGINE);
9421 case TwoMachinesPlay:
9422 if (cps->twoMachinesColor[0] == 'w')
9423 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
9425 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
9432 } else if (strncmp(message, "computer mates", 14) == 0) {
9434 case MachinePlaysBlack:
9435 case IcsPlayingBlack:
9436 GameEnds(BlackWins, "Black mates", GE_ENGINE1);
9438 case MachinePlaysWhite:
9439 case IcsPlayingWhite:
9440 GameEnds(WhiteWins, "White mates", GE_ENGINE);
9442 case TwoMachinesPlay:
9443 if (cps->twoMachinesColor[0] == 'w')
9444 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
9446 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
9453 } else if (strncmp(message, "checkmate", 9) == 0) {
9454 if (WhiteOnMove(forwardMostMove)) {
9455 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
9457 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
9460 } else if (strstr(message, "Draw") != NULL ||
9461 strstr(message, "game is a draw") != NULL) {
9462 GameEnds(GameIsDrawn, "Draw", GE_ENGINE1 + (cps != &first));
9464 } else if (strstr(message, "offer") != NULL &&
9465 strstr(message, "draw") != NULL) {
9467 if (appData.zippyPlay && first.initDone) {
9468 /* Relay offer to ICS */
9469 SendToICS(ics_prefix);
9470 SendToICS("draw\n");
9473 cps->offeredDraw = 2; /* valid until this engine moves twice */
9474 if (gameMode == TwoMachinesPlay) {
9475 if (cps->other->offeredDraw) {
9476 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
9477 /* [HGM] in two-machine mode we delay relaying draw offer */
9478 /* until after we also have move, to see if it is really claim */
9480 } else if (gameMode == MachinePlaysWhite ||
9481 gameMode == MachinePlaysBlack) {
9482 if (userOfferedDraw) {
9483 DisplayInformation(_("Machine accepts your draw offer"));
9484 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
9486 DisplayInformation(_("Machine offers a draw.\nSelect Action / Draw to accept."));
9493 * Look for thinking output
9495 if ( appData.showThinking // [HGM] thinking: test all options that cause this output
9496 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp()
9498 int plylev, mvleft, mvtot, curscore, time;
9499 char mvname[MOVE_LEN];
9503 int prefixHint = FALSE;
9504 mvname[0] = NULLCHAR;
9507 case MachinePlaysBlack:
9508 case IcsPlayingBlack:
9509 if (WhiteOnMove(forwardMostMove)) prefixHint = TRUE;
9511 case MachinePlaysWhite:
9512 case IcsPlayingWhite:
9513 if (!WhiteOnMove(forwardMostMove)) prefixHint = TRUE;
9518 case IcsObserving: /* [DM] icsEngineAnalyze */
9519 if (!appData.icsEngineAnalyze) ignore = TRUE;
9521 case TwoMachinesPlay:
9522 if ((cps->twoMachinesColor[0] == 'w') != WhiteOnMove(forwardMostMove)) {
9532 ChessProgramStats tempStats = programStats; // [HGM] info: filter out info lines
9534 if (sscanf(message, "%d%c %d %d " u64Display " %[^\n]\n",
9535 &plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5) {
9537 if(nodes>>32 == u64Const(0xFFFFFFFF)) // [HGM] negative node count read
9538 nodes += u64Const(0x100000000);
9540 if (plyext != ' ' && plyext != '\t') {
9544 /* [AS] Negate score if machine is playing black and reporting absolute scores */
9545 if( cps->scoreIsAbsolute &&
9546 ( gameMode == MachinePlaysBlack ||
9547 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b' ||
9548 gameMode == IcsPlayingBlack || // [HGM] also add other situations where engine should report black POV
9549 (gameMode == AnalyzeMode || gameMode == AnalyzeFile || gameMode == IcsObserving && appData.icsEngineAnalyze) &&
9550 !WhiteOnMove(currentMove)
9553 curscore = -curscore;
9556 if(appData.pvSAN[cps==&second]) pv = PvToSAN(buf1);
9558 if(*bestMove) { // rememer time best EPD move was first found
9559 int ff1, tf1, fr1, tr1, ff2, tf2, fr2, tr2; char pp1, pp2;
9561 int ok = ParseOneMove(bestMove, forwardMostMove, &mt, &ff1, &fr1, &tf1, &tr1, &pp1);
9562 ok &= ParseOneMove(pv, forwardMostMove, &mt, &ff2, &fr2, &tf2, &tr2, &pp2);
9563 solvingTime = (ok && ff1==ff2 && fr1==fr2 && tf1==tf2 && tr1==tr2 && pp1==pp2 ? time : -1);
9566 if(serverMoves && (time > 100 || time == 0 && plylev > 7)) {
9569 snprintf(buf, MSG_SIZ, "%s", appData.serverMovesName);
9570 buf[strlen(buf)-1] = gameMode == MachinePlaysWhite ? 'w' :
9571 gameMode == MachinePlaysBlack ? 'b' : cps->twoMachinesColor[0];
9572 if(appData.debugMode) fprintf(debugFP, "write PV on file '%s'\n", buf);
9573 if(f = fopen(buf, "w")) { // export PV to applicable PV file
9574 fprintf(f, "%5.2f/%-2d %s", curscore/100., plylev, pv);
9578 /* TRANSLATORS: PV = principal variation, the variation the chess engine thinks is the best for everyone */
9579 DisplayError(_("failed writing PV"), 0);
9582 tempStats.depth = plylev;
9583 tempStats.nodes = nodes;
9584 tempStats.time = time;
9585 tempStats.score = curscore;
9586 tempStats.got_only_move = 0;
9588 if(cps->nps >= 0) { /* [HGM] nps: use engine nodes or time to decrement clock */
9591 if(cps->nps == 0) ticklen = 10*time; // use engine reported time
9592 else ticklen = (1000. * u64ToDouble(nodes)) / cps->nps; // convert node count to time
9593 if(WhiteOnMove(forwardMostMove) && (gameMode == MachinePlaysWhite ||
9594 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'w'))
9595 whiteTimeRemaining = timeRemaining[0][forwardMostMove] - ticklen;
9596 if(!WhiteOnMove(forwardMostMove) && (gameMode == MachinePlaysBlack ||
9597 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b'))
9598 blackTimeRemaining = timeRemaining[1][forwardMostMove] - ticklen;
9601 /* Buffer overflow protection */
9602 if (pv[0] != NULLCHAR) {
9603 if (strlen(pv) >= sizeof(tempStats.movelist)
9604 && appData.debugMode) {
9606 "PV is too long; using the first %u bytes.\n",
9607 (unsigned) sizeof(tempStats.movelist) - 1);
9610 safeStrCpy( tempStats.movelist, pv, sizeof(tempStats.movelist)/sizeof(tempStats.movelist[0]) );
9612 sprintf(tempStats.movelist, " no PV\n");
9615 if (tempStats.seen_stat) {
9616 tempStats.ok_to_send = 1;
9619 if (strchr(tempStats.movelist, '(') != NULL) {
9620 tempStats.line_is_book = 1;
9621 tempStats.nr_moves = 0;
9622 tempStats.moves_left = 0;
9624 tempStats.line_is_book = 0;
9627 if(tempStats.score != 0 || tempStats.nodes != 0 || tempStats.time != 0)
9628 programStats = tempStats; // [HGM] info: only set stats if genuine PV and not an info line
9630 SendProgramStatsToFrontend( cps, &tempStats );
9633 [AS] Protect the thinkOutput buffer from overflow... this
9634 is only useful if buf1 hasn't overflowed first!
9636 snprintf(thinkOutput, sizeof(thinkOutput)/sizeof(thinkOutput[0]), "[%d]%c%+.2f %s%s",
9638 (gameMode == TwoMachinesPlay ?
9639 ToUpper(cps->twoMachinesColor[0]) : ' '),
9640 ((double) curscore) / 100.0,
9641 prefixHint ? lastHint : "",
9642 prefixHint ? " " : "" );
9644 if( buf1[0] != NULLCHAR ) {
9645 unsigned max_len = sizeof(thinkOutput) - strlen(thinkOutput) - 1;
9647 if( strlen(pv) > max_len ) {
9648 if( appData.debugMode) {
9649 fprintf(debugFP,"PV is too long for thinkOutput, truncating.\n");
9651 pv[max_len+1] = '\0';
9654 strcat( thinkOutput, pv);
9657 if (currentMove == forwardMostMove || gameMode == AnalyzeMode
9658 || gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
9659 DisplayMove(currentMove - 1);
9663 } else if ((p=StrStr(message, "(only move)")) != NULL) {
9664 /* crafty (9.25+) says "(only move) <move>"
9665 * if there is only 1 legal move
9667 sscanf(p, "(only move) %s", buf1);
9668 snprintf(thinkOutput, sizeof(thinkOutput)/sizeof(thinkOutput[0]), "%s (only move)", buf1);
9669 sprintf(programStats.movelist, "%s (only move)", buf1);
9670 programStats.depth = 1;
9671 programStats.nr_moves = 1;
9672 programStats.moves_left = 1;
9673 programStats.nodes = 1;
9674 programStats.time = 1;
9675 programStats.got_only_move = 1;
9677 /* Not really, but we also use this member to
9678 mean "line isn't going to change" (Crafty
9679 isn't searching, so stats won't change) */
9680 programStats.line_is_book = 1;
9682 SendProgramStatsToFrontend( cps, &programStats );
9684 if (currentMove == forwardMostMove || gameMode==AnalyzeMode ||
9685 gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
9686 DisplayMove(currentMove - 1);
9689 } else if (sscanf(message,"stat01: %d " u64Display " %d %d %d %s",
9690 &time, &nodes, &plylev, &mvleft,
9691 &mvtot, mvname) >= 5) {
9692 /* The stat01: line is from Crafty (9.29+) in response
9693 to the "." command */
9694 programStats.seen_stat = 1;
9695 cps->maybeThinking = TRUE;
9697 if (programStats.got_only_move || !appData.periodicUpdates)
9700 programStats.depth = plylev;
9701 programStats.time = time;
9702 programStats.nodes = nodes;
9703 programStats.moves_left = mvleft;
9704 programStats.nr_moves = mvtot;
9705 safeStrCpy(programStats.move_name, mvname, sizeof(programStats.move_name)/sizeof(programStats.move_name[0]));
9706 programStats.ok_to_send = 1;
9707 programStats.movelist[0] = '\0';
9709 SendProgramStatsToFrontend( cps, &programStats );
9713 } else if (strncmp(message,"++",2) == 0) {
9714 /* Crafty 9.29+ outputs this */
9715 programStats.got_fail = 2;
9718 } else if (strncmp(message,"--",2) == 0) {
9719 /* Crafty 9.29+ outputs this */
9720 programStats.got_fail = 1;
9723 } else if (thinkOutput[0] != NULLCHAR &&
9724 strncmp(message, " ", 4) == 0) {
9725 unsigned message_len;
9728 while (*p && *p == ' ') p++;
9730 message_len = strlen( p );
9732 /* [AS] Avoid buffer overflow */
9733 if( sizeof(thinkOutput) - strlen(thinkOutput) - 1 > message_len ) {
9734 strcat(thinkOutput, " ");
9735 strcat(thinkOutput, p);
9738 if( sizeof(programStats.movelist) - strlen(programStats.movelist) - 1 > message_len ) {
9739 strcat(programStats.movelist, " ");
9740 strcat(programStats.movelist, p);
9743 if (currentMove == forwardMostMove || gameMode==AnalyzeMode ||
9744 gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
9745 DisplayMove(currentMove - 1);
9753 if (sscanf(message, "%d%c %d %d " u64Display " %[^\n]\n",
9754 &plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5)
9756 ChessProgramStats cpstats;
9758 if (plyext != ' ' && plyext != '\t') {
9762 /* [AS] Negate score if machine is playing black and reporting absolute scores */
9763 if( cps->scoreIsAbsolute && ((gameMode == MachinePlaysBlack) || (gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b')) ) {
9764 curscore = -curscore;
9767 cpstats.depth = plylev;
9768 cpstats.nodes = nodes;
9769 cpstats.time = time;
9770 cpstats.score = curscore;
9771 cpstats.got_only_move = 0;
9772 cpstats.movelist[0] = '\0';
9774 if (buf1[0] != NULLCHAR) {
9775 safeStrCpy( cpstats.movelist, buf1, sizeof(cpstats.movelist)/sizeof(cpstats.movelist[0]) );
9778 cpstats.ok_to_send = 0;
9779 cpstats.line_is_book = 0;
9780 cpstats.nr_moves = 0;
9781 cpstats.moves_left = 0;
9783 SendProgramStatsToFrontend( cps, &cpstats );
9790 /* Parse a game score from the character string "game", and
9791 record it as the history of the current game. The game
9792 score is NOT assumed to start from the standard position.
9793 The display is not updated in any way.
9796 ParseGameHistory (char *game)
9799 int fromX, fromY, toX, toY, boardIndex;
9804 if (appData.debugMode)
9805 fprintf(debugFP, "Parsing game history: %s\n", game);
9807 if (gameInfo.event == NULL) gameInfo.event = StrSave("ICS game");
9808 gameInfo.site = StrSave(appData.icsHost);
9809 gameInfo.date = PGNDate();
9810 gameInfo.round = StrSave("-");
9812 /* Parse out names of players */
9813 while (*game == ' ') game++;
9815 while (*game != ' ') *p++ = *game++;
9817 gameInfo.white = StrSave(buf);
9818 while (*game == ' ') game++;
9820 while (*game != ' ' && *game != '\n') *p++ = *game++;
9822 gameInfo.black = StrSave(buf);
9825 boardIndex = blackPlaysFirst ? 1 : 0;
9828 yyboardindex = boardIndex;
9829 moveType = (ChessMove) Myylex();
9831 case IllegalMove: /* maybe suicide chess, etc. */
9832 if (appData.debugMode) {
9833 fprintf(debugFP, "Illegal move from ICS: '%s'\n", yy_text);
9834 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
9835 setbuf(debugFP, NULL);
9837 case WhitePromotion:
9838 case BlackPromotion:
9839 case WhiteNonPromotion:
9840 case BlackNonPromotion:
9843 case WhiteCapturesEnPassant:
9844 case BlackCapturesEnPassant:
9845 case WhiteKingSideCastle:
9846 case WhiteQueenSideCastle:
9847 case BlackKingSideCastle:
9848 case BlackQueenSideCastle:
9849 case WhiteKingSideCastleWild:
9850 case WhiteQueenSideCastleWild:
9851 case BlackKingSideCastleWild:
9852 case BlackQueenSideCastleWild:
9854 case WhiteHSideCastleFR:
9855 case WhiteASideCastleFR:
9856 case BlackHSideCastleFR:
9857 case BlackASideCastleFR:
9859 fromX = currentMoveString[0] - AAA;
9860 fromY = currentMoveString[1] - ONE;
9861 toX = currentMoveString[2] - AAA;
9862 toY = currentMoveString[3] - ONE;
9863 promoChar = currentMoveString[4];
9867 if(currentMoveString[0] == '@') continue; // no null moves in ICS mode!
9868 fromX = moveType == WhiteDrop ?
9869 (int) CharToPiece(ToUpper(currentMoveString[0])) :
9870 (int) CharToPiece(ToLower(currentMoveString[0]));
9872 toX = currentMoveString[2] - AAA;
9873 toY = currentMoveString[3] - ONE;
9874 promoChar = NULLCHAR;
9878 snprintf(buf, MSG_SIZ, _("Ambiguous move in ICS output: \"%s\""), yy_text);
9879 if (appData.debugMode) {
9880 fprintf(debugFP, "Ambiguous move from ICS: '%s'\n", yy_text);
9881 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
9882 setbuf(debugFP, NULL);
9884 DisplayError(buf, 0);
9886 case ImpossibleMove:
9888 snprintf(buf, MSG_SIZ, _("Illegal move in ICS output: \"%s\""), yy_text);
9889 if (appData.debugMode) {
9890 fprintf(debugFP, "Impossible move from ICS: '%s'\n", yy_text);
9891 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
9892 setbuf(debugFP, NULL);
9894 DisplayError(buf, 0);
9897 if (boardIndex < backwardMostMove) {
9898 /* Oops, gap. How did that happen? */
9899 DisplayError(_("Gap in move list"), 0);
9902 backwardMostMove = blackPlaysFirst ? 1 : 0;
9903 if (boardIndex > forwardMostMove) {
9904 forwardMostMove = boardIndex;
9908 if (boardIndex > (blackPlaysFirst ? 1 : 0)) {
9909 strcat(parseList[boardIndex-1], " ");
9910 strcat(parseList[boardIndex-1], yy_text);
9922 case GameUnfinished:
9923 if (gameMode == IcsExamining) {
9924 if (boardIndex < backwardMostMove) {
9925 /* Oops, gap. How did that happen? */
9928 backwardMostMove = blackPlaysFirst ? 1 : 0;
9931 gameInfo.result = moveType;
9932 p = strchr(yy_text, '{');
9933 if (p == NULL) p = strchr(yy_text, '(');
9936 if (p[0] == '0' || p[0] == '1' || p[0] == '*') p = "";
9938 q = strchr(p, *p == '{' ? '}' : ')');
9939 if (q != NULL) *q = NULLCHAR;
9942 while(q = strchr(p, '\n')) *q = ' '; // [HGM] crush linefeeds in result message
9943 gameInfo.resultDetails = StrSave(p);
9946 if (boardIndex >= forwardMostMove &&
9947 !(gameMode == IcsObserving && ics_gamenum == -1)) {
9948 backwardMostMove = blackPlaysFirst ? 1 : 0;
9951 (void) CoordsToAlgebraic(boards[boardIndex], PosFlags(boardIndex),
9952 fromY, fromX, toY, toX, promoChar,
9953 parseList[boardIndex]);
9954 CopyBoard(boards[boardIndex + 1], boards[boardIndex]);
9955 /* currentMoveString is set as a side-effect of yylex */
9956 safeStrCpy(moveList[boardIndex], currentMoveString, sizeof(moveList[boardIndex])/sizeof(moveList[boardIndex][0]));
9957 strcat(moveList[boardIndex], "\n");
9959 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[boardIndex]);
9960 switch (MateTest(boards[boardIndex], PosFlags(boardIndex)) ) {
9966 if(!IS_SHOGI(gameInfo.variant))
9967 strcat(parseList[boardIndex - 1], "+");
9971 strcat(parseList[boardIndex - 1], "#");
9978 /* Apply a move to the given board */
9980 ApplyMove (int fromX, int fromY, int toX, int toY, int promoChar, Board board)
9982 ChessSquare captured = board[toY][toX], piece, pawn, king, killed, killed2; int p, rookX, oldEP, epRank, berolina = 0;
9983 int promoRank = gameInfo.variant == VariantMakruk || gameInfo.variant == VariantGrand || gameInfo.variant == VariantChuChess ? 3 : 1;
9985 /* [HGM] compute & store e.p. status and castling rights for new position */
9986 /* we can always do that 'in place', now pointers to these rights are passed to ApplyMove */
9988 if(gameInfo.variant == VariantBerolina) berolina = EP_BEROLIN_A;
9989 oldEP = (signed char)board[EP_FILE]; epRank = board[EP_RANK];
9990 board[EP_STATUS] = EP_NONE;
9991 board[EP_FILE] = board[EP_RANK] = 100;
9993 if (fromY == DROP_RANK) {
9995 if(fromX == EmptySquare) { // [HGM] pass: empty drop encodes null move; nothing to change.
9996 board[EP_STATUS] = EP_CAPTURE; // null move considered irreversible
9999 piece = board[toY][toX] = (ChessSquare) fromX;
10001 // ChessSquare victim;
10004 if( killX >= 0 && killY >= 0 ) { // [HGM] lion: Lion trampled over something
10005 // victim = board[killY][killX],
10006 killed = board[killY][killX],
10007 board[killY][killX] = EmptySquare,
10008 board[EP_STATUS] = EP_CAPTURE;
10009 if( kill2X >= 0 && kill2Y >= 0)
10010 killed2 = board[kill2Y][kill2X], board[kill2Y][kill2X] = EmptySquare;
10013 if( board[toY][toX] != EmptySquare ) {
10014 board[EP_STATUS] = EP_CAPTURE;
10015 if( (fromX != toX || fromY != toY) && // not igui!
10016 (captured == WhiteLion && board[fromY][fromX] != BlackLion ||
10017 captured == BlackLion && board[fromY][fromX] != WhiteLion ) ) { // [HGM] lion: Chu Lion-capture rules
10018 board[EP_STATUS] = EP_IRON_LION; // non-Lion x Lion: no counter-strike allowed
10022 pawn = board[fromY][fromX];
10023 if( pawn == WhiteLance || pawn == BlackLance ) {
10024 if( gameInfo.variant != VariantSuper && gameInfo.variant != VariantChu ) {
10025 if(gameInfo.variant == VariantSpartan) board[EP_STATUS] = EP_PAWN_MOVE; // in Spartan no e.p. rights must be set
10026 else pawn += WhitePawn - WhiteLance; // Lance is Pawn-like in most variants, so let Pawn code treat it by this kludge
10029 if( pawn == WhitePawn ) {
10030 if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
10031 board[EP_STATUS] = EP_PAWN_MOVE;
10032 if( toY-fromY>=2) {
10033 board[EP_FILE] = (fromX + toX)/2; board[EP_RANK] = toY - 1 | 128*(toY - fromY > 2);
10034 if(toX>BOARD_LEFT && board[toY][toX-1] == BlackPawn &&
10035 gameInfo.variant != VariantBerolina || toX < fromX)
10036 board[EP_STATUS] = toX | berolina;
10037 if(toX<BOARD_RGHT-1 && board[toY][toX+1] == BlackPawn &&
10038 gameInfo.variant != VariantBerolina || toX > fromX)
10039 board[EP_STATUS] = toX;
10042 if( pawn == BlackPawn ) {
10043 if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
10044 board[EP_STATUS] = EP_PAWN_MOVE;
10045 if( toY-fromY<= -2) {
10046 board[EP_FILE] = (fromX + toX)/2; board[EP_RANK] = toY + 1 | 128*(fromY - toY > 2);
10047 if(toX>BOARD_LEFT && board[toY][toX-1] == WhitePawn &&
10048 gameInfo.variant != VariantBerolina || toX < fromX)
10049 board[EP_STATUS] = toX | berolina;
10050 if(toX<BOARD_RGHT-1 && board[toY][toX+1] == WhitePawn &&
10051 gameInfo.variant != VariantBerolina || toX > fromX)
10052 board[EP_STATUS] = toX;
10056 if(fromY == 0) board[TOUCHED_W] |= 1<<fromX; else // new way to keep track of virginity
10057 if(fromY == BOARD_HEIGHT-1) board[TOUCHED_B] |= 1<<fromX;
10058 if(toY == 0) board[TOUCHED_W] |= 1<<toX; else
10059 if(toY == BOARD_HEIGHT-1) board[TOUCHED_B] |= 1<<toX;
10061 for(i=0; i<nrCastlingRights; i++) {
10062 if(board[CASTLING][i] == fromX && castlingRank[i] == fromY ||
10063 board[CASTLING][i] == toX && castlingRank[i] == toY
10064 ) board[CASTLING][i] = NoRights; // revoke for moved or captured piece
10067 if(gameInfo.variant == VariantSChess) { // update virginity
10068 if(fromY == 0) board[VIRGIN][fromX] &= ~VIRGIN_W; // loss by moving
10069 if(fromY == BOARD_HEIGHT-1) board[VIRGIN][fromX] &= ~VIRGIN_B;
10070 if(toY == 0) board[VIRGIN][toX] &= ~VIRGIN_W; // loss by capture
10071 if(toY == BOARD_HEIGHT-1) board[VIRGIN][toX] &= ~VIRGIN_B;
10074 if (fromX == toX && fromY == toY) return;
10076 piece = board[fromY][fromX]; /* [HGM] remember, for Shogi promotion */
10077 king = piece < (int) BlackPawn ? WhiteKing : BlackKing; /* [HGM] Knightmate simplify testing for castling */
10078 if(gameInfo.variant == VariantKnightmate)
10079 king += (int) WhiteUnicorn - (int) WhiteKing;
10081 if(pieceDesc[piece] && killX >= 0 && strchr(pieceDesc[piece], 'O') // Betza castling-enabled
10082 && (piece < BlackPawn ? killed < BlackPawn : killed >= BlackPawn)) { // and tramples own
10083 board[toY][toX] = piece; board[fromY][fromX] = EmptySquare;
10084 board[toY][toX + (killX < fromX ? 1 : -1)] = killed;
10085 board[EP_STATUS] = EP_NONE; // capture was fake!
10087 /* Code added by Tord: */
10088 /* FRC castling assumed when king captures friendly rook. [HGM] or RxK for S-Chess */
10089 if (board[fromY][fromX] == WhiteKing && board[toY][toX] == WhiteRook ||
10090 board[fromY][fromX] == WhiteRook && board[toY][toX] == WhiteKing) {
10091 board[EP_STATUS] = EP_NONE; // capture was fake!
10092 board[fromY][fromX] = EmptySquare;
10093 board[toY][toX] = EmptySquare;
10094 if((toX > fromX) != (piece == WhiteRook)) {
10095 board[0][BOARD_RGHT-2] = WhiteKing; board[0][BOARD_RGHT-3] = WhiteRook;
10097 board[0][BOARD_LEFT+2] = WhiteKing; board[0][BOARD_LEFT+3] = WhiteRook;
10099 } else if (board[fromY][fromX] == BlackKing && board[toY][toX] == BlackRook ||
10100 board[fromY][fromX] == BlackRook && board[toY][toX] == BlackKing) {
10101 board[EP_STATUS] = EP_NONE;
10102 board[fromY][fromX] = EmptySquare;
10103 board[toY][toX] = EmptySquare;
10104 if((toX > fromX) != (piece == BlackRook)) {
10105 board[BOARD_HEIGHT-1][BOARD_RGHT-2] = BlackKing; board[BOARD_HEIGHT-1][BOARD_RGHT-3] = BlackRook;
10107 board[BOARD_HEIGHT-1][BOARD_LEFT+2] = BlackKing; board[BOARD_HEIGHT-1][BOARD_LEFT+3] = BlackRook;
10109 /* End of code added by Tord */
10111 } else if (board[fromY][fromX] == king
10112 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
10113 && toY == fromY && toX > fromX+1) {
10114 for(rookX=fromX+1; board[toY][rookX] == EmptySquare && rookX < BOARD_RGHT-1; rookX++); // castle with nearest piece
10115 board[fromY][toX-1] = board[fromY][rookX];
10116 board[fromY][rookX] = EmptySquare;
10117 board[fromY][fromX] = EmptySquare;
10118 board[toY][toX] = king;
10119 } else if (board[fromY][fromX] == king
10120 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
10121 && toY == fromY && toX < fromX-1) {
10122 for(rookX=fromX-1; board[toY][rookX] == EmptySquare && rookX > 0; rookX--); // castle with nearest piece
10123 board[fromY][toX+1] = board[fromY][rookX];
10124 board[fromY][rookX] = EmptySquare;
10125 board[fromY][fromX] = EmptySquare;
10126 board[toY][toX] = king;
10127 } else if ((board[fromY][fromX] == WhitePawn && gameInfo.variant != VariantXiangqi ||
10128 board[fromY][fromX] == WhiteLance && gameInfo.variant != VariantSuper && gameInfo.variant != VariantChu)
10129 && toY >= BOARD_HEIGHT-promoRank && promoChar // defaulting to Q is done elsewhere
10131 /* white pawn promotion */
10132 board[toY][toX] = CharToPiece(ToUpper(promoChar));
10133 if(board[toY][toX] < WhiteCannon && PieceToChar(PROMOTED board[toY][toX]) == '~') /* [HGM] use shadow piece (if available) */
10134 board[toY][toX] = (ChessSquare) (PROMOTED board[toY][toX]);
10135 board[fromY][fromX] = EmptySquare;
10136 } else if ((fromY >= BOARD_HEIGHT>>1)
10137 && (oldEP == toX || oldEP == EP_UNKNOWN || appData.testLegality || abs(toX - fromX) > 4)
10139 && gameInfo.variant != VariantXiangqi
10140 && gameInfo.variant != VariantBerolina
10141 && (pawn == WhitePawn)
10142 && (board[toY][toX] == EmptySquare)) {
10143 board[fromY][fromX] = EmptySquare;
10144 board[toY][toX] = piece;
10145 if(toY == epRank - 128 + 1)
10146 captured = board[toY - 2][toX], board[toY - 2][toX] = EmptySquare;
10148 captured = board[toY - 1][toX], board[toY - 1][toX] = EmptySquare;
10149 } else if ((fromY == BOARD_HEIGHT-4)
10151 && gameInfo.variant == VariantBerolina
10152 && (board[fromY][fromX] == WhitePawn)
10153 && (board[toY][toX] == EmptySquare)) {
10154 board[fromY][fromX] = EmptySquare;
10155 board[toY][toX] = WhitePawn;
10156 if(oldEP & EP_BEROLIN_A) {
10157 captured = board[fromY][fromX-1];
10158 board[fromY][fromX-1] = EmptySquare;
10159 }else{ captured = board[fromY][fromX+1];
10160 board[fromY][fromX+1] = EmptySquare;
10162 } else if (board[fromY][fromX] == king
10163 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
10164 && toY == fromY && toX > fromX+1) {
10165 for(rookX=toX+1; board[toY][rookX] == EmptySquare && rookX < BOARD_RGHT - 1; rookX++);
10166 board[fromY][toX-1] = board[fromY][rookX];
10167 board[fromY][rookX] = EmptySquare;
10168 board[fromY][fromX] = EmptySquare;
10169 board[toY][toX] = king;
10170 } else if (board[fromY][fromX] == king
10171 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
10172 && toY == fromY && toX < fromX-1) {
10173 for(rookX=toX-1; board[toY][rookX] == EmptySquare && rookX > 0; rookX--);
10174 board[fromY][toX+1] = board[fromY][rookX];
10175 board[fromY][rookX] = EmptySquare;
10176 board[fromY][fromX] = EmptySquare;
10177 board[toY][toX] = king;
10178 } else if (fromY == 7 && fromX == 3
10179 && board[fromY][fromX] == BlackKing
10180 && toY == 7 && toX == 5) {
10181 board[fromY][fromX] = EmptySquare;
10182 board[toY][toX] = BlackKing;
10183 board[fromY][7] = EmptySquare;
10184 board[toY][4] = BlackRook;
10185 } else if (fromY == 7 && fromX == 3
10186 && board[fromY][fromX] == BlackKing
10187 && toY == 7 && toX == 1) {
10188 board[fromY][fromX] = EmptySquare;
10189 board[toY][toX] = BlackKing;
10190 board[fromY][0] = EmptySquare;
10191 board[toY][2] = BlackRook;
10192 } else if ((board[fromY][fromX] == BlackPawn && gameInfo.variant != VariantXiangqi ||
10193 board[fromY][fromX] == BlackLance && gameInfo.variant != VariantSuper && gameInfo.variant != VariantChu)
10194 && toY < promoRank && promoChar
10196 /* black pawn promotion */
10197 board[toY][toX] = CharToPiece(ToLower(promoChar));
10198 if(board[toY][toX] < BlackCannon && PieceToChar(PROMOTED board[toY][toX]) == '~') /* [HGM] use shadow piece (if available) */
10199 board[toY][toX] = (ChessSquare) (PROMOTED board[toY][toX]);
10200 board[fromY][fromX] = EmptySquare;
10201 } else if ((fromY < BOARD_HEIGHT>>1)
10202 && (oldEP == toX || oldEP == EP_UNKNOWN || appData.testLegality || abs(toX - fromX) > 4)
10204 && gameInfo.variant != VariantXiangqi
10205 && gameInfo.variant != VariantBerolina
10206 && (pawn == BlackPawn)
10207 && (board[toY][toX] == EmptySquare)) {
10208 board[fromY][fromX] = EmptySquare;
10209 board[toY][toX] = piece;
10210 if(toY == epRank - 128 - 1)
10211 captured = board[toY + 2][toX], board[toY + 2][toX] = EmptySquare;
10213 captured = board[toY + 1][toX], board[toY + 1][toX] = EmptySquare;
10214 } else if ((fromY == 3)
10216 && gameInfo.variant == VariantBerolina
10217 && (board[fromY][fromX] == BlackPawn)
10218 && (board[toY][toX] == EmptySquare)) {
10219 board[fromY][fromX] = EmptySquare;
10220 board[toY][toX] = BlackPawn;
10221 if(oldEP & EP_BEROLIN_A) {
10222 captured = board[fromY][fromX-1];
10223 board[fromY][fromX-1] = EmptySquare;
10224 }else{ captured = board[fromY][fromX+1];
10225 board[fromY][fromX+1] = EmptySquare;
10228 ChessSquare piece = board[fromY][fromX]; // [HGM] lion: allow for igui (where from == to)
10229 board[fromY][fromX] = EmptySquare;
10230 board[toY][toX] = piece;
10234 if (gameInfo.holdingsWidth != 0) {
10236 /* !!A lot more code needs to be written to support holdings */
10237 /* [HGM] OK, so I have written it. Holdings are stored in the */
10238 /* penultimate board files, so they are automaticlly stored */
10239 /* in the game history. */
10240 if (fromY == DROP_RANK || gameInfo.variant == VariantSChess
10241 && promoChar && piece != WhitePawn && piece != BlackPawn) {
10242 /* Delete from holdings, by decreasing count */
10243 /* and erasing image if necessary */
10244 p = fromY == DROP_RANK ? (int) fromX : CharToPiece(piece > BlackPawn ? ToLower(promoChar) : ToUpper(promoChar));
10245 if(p < (int) BlackPawn) { /* white drop */
10246 p -= (int)WhitePawn;
10247 p = PieceToNumber((ChessSquare)p);
10248 if(p >= gameInfo.holdingsSize) p = 0;
10249 if(--board[p][BOARD_WIDTH-2] <= 0)
10250 board[p][BOARD_WIDTH-1] = EmptySquare;
10251 if((int)board[p][BOARD_WIDTH-2] < 0)
10252 board[p][BOARD_WIDTH-2] = 0;
10253 } else { /* black drop */
10254 p -= (int)BlackPawn;
10255 p = PieceToNumber((ChessSquare)p);
10256 if(p >= gameInfo.holdingsSize) p = 0;
10257 if(--board[BOARD_HEIGHT-1-p][1] <= 0)
10258 board[BOARD_HEIGHT-1-p][0] = EmptySquare;
10259 if((int)board[BOARD_HEIGHT-1-p][1] < 0)
10260 board[BOARD_HEIGHT-1-p][1] = 0;
10263 if (captured != EmptySquare && gameInfo.holdingsSize > 0
10264 && gameInfo.variant != VariantBughouse && gameInfo.variant != VariantSChess ) {
10265 /* [HGM] holdings: Add to holdings, if holdings exist */
10266 if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand) {
10267 // [HGM] superchess: suppress flipping color of captured pieces by reverse pre-flip
10268 captured = (int) captured >= (int) BlackPawn ? BLACK_TO_WHITE captured : WHITE_TO_BLACK captured;
10270 p = (int) captured;
10271 if (p >= (int) BlackPawn) {
10272 p -= (int)BlackPawn;
10273 if(DEMOTED p >= 0 && PieceToChar(p) == '+') {
10274 /* Restore shogi-promoted piece to its original first */
10275 captured = (ChessSquare) (DEMOTED captured);
10278 p = PieceToNumber((ChessSquare)p);
10279 if(p >= gameInfo.holdingsSize) { p = 0; captured = BlackPawn; }
10280 board[p][BOARD_WIDTH-2]++;
10281 board[p][BOARD_WIDTH-1] = BLACK_TO_WHITE captured;
10283 p -= (int)WhitePawn;
10284 if(DEMOTED p >= 0 && PieceToChar(p) == '+') {
10285 captured = (ChessSquare) (DEMOTED captured);
10288 p = PieceToNumber((ChessSquare)p);
10289 if(p >= gameInfo.holdingsSize) { p = 0; captured = WhitePawn; }
10290 board[BOARD_HEIGHT-1-p][1]++;
10291 board[BOARD_HEIGHT-1-p][0] = WHITE_TO_BLACK captured;
10294 } else if (gameInfo.variant == VariantAtomic) {
10295 if (captured != EmptySquare) {
10297 for (y = toY-1; y <= toY+1; y++) {
10298 for (x = toX-1; x <= toX+1; x++) {
10299 if (y >= 0 && y < BOARD_HEIGHT && x >= BOARD_LEFT && x < BOARD_RGHT &&
10300 board[y][x] != WhitePawn && board[y][x] != BlackPawn) {
10301 board[y][x] = EmptySquare;
10305 board[toY][toX] = EmptySquare;
10309 if(gameInfo.variant == VariantSChess && promoChar != NULLCHAR && promoChar != '=' && piece != WhitePawn && piece != BlackPawn) {
10310 board[fromY][fromX] = CharToPiece(piece < BlackPawn ? ToUpper(promoChar) : ToLower(promoChar)); // S-Chess gating
10312 if(promoChar == '+') {
10313 /* [HGM] Shogi-style promotions, to piece implied by original (Might overwrite ordinary Pawn promotion) */
10314 board[toY][toX] = (ChessSquare) (CHUPROMOTED piece);
10315 if(gameInfo.variant == VariantChuChess && (piece == WhiteKnight || piece == BlackKnight))
10316 board[toY][toX] = piece + WhiteLion - WhiteKnight; // adjust Knight promotions to Lion
10317 } else if(!appData.testLegality && promoChar != NULLCHAR && promoChar != '=') { // without legality testing, unconditionally believe promoChar
10318 ChessSquare newPiece = CharToPiece(piece < BlackPawn ? ToUpper(promoChar) : ToLower(promoChar));
10319 if((newPiece <= WhiteMan || newPiece >= BlackPawn && newPiece <= BlackMan) // unpromoted piece specified
10320 && pieceToChar[PROMOTED newPiece] == '~') newPiece = PROMOTED newPiece; // but promoted version available
10321 board[toY][toX] = newPiece;
10323 if((gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand)
10324 && promoChar != NULLCHAR && gameInfo.holdingsSize) {
10325 // [HGM] superchess: take promotion piece out of holdings
10326 int k = PieceToNumber(CharToPiece(ToUpper(promoChar)));
10327 if((int)piece < (int)BlackPawn) { // determine stm from piece color
10328 if(!--board[k][BOARD_WIDTH-2])
10329 board[k][BOARD_WIDTH-1] = EmptySquare;
10331 if(!--board[BOARD_HEIGHT-1-k][1])
10332 board[BOARD_HEIGHT-1-k][0] = EmptySquare;
10337 /* Updates forwardMostMove */
10339 MakeMove (int fromX, int fromY, int toX, int toY, int promoChar)
10341 int x = toX, y = toY;
10342 char *s = parseList[forwardMostMove];
10343 ChessSquare p = boards[forwardMostMove][toY][toX];
10344 // forwardMostMove++; // [HGM] bare: moved downstream
10346 if(killX >= 0 && killY >= 0) x = killX, y = killY; // [HGM] lion: make SAN move to intermediate square, if there is one
10347 (void) CoordsToAlgebraic(boards[forwardMostMove],
10348 PosFlags(forwardMostMove),
10349 fromY, fromX, y, x, promoChar,
10351 if(killX >= 0 && killY >= 0)
10352 sprintf(s + strlen(s), "%c%c%d", p == EmptySquare || toX == fromX && toY == fromY ? '-' : 'x', toX + AAA, toY + ONE - '0');
10354 if(serverMoves != NULL) { /* [HGM] write moves on file for broadcasting (should be separate routine, really) */
10355 int timeLeft; static int lastLoadFlag=0; int king, piece;
10356 piece = boards[forwardMostMove][fromY][fromX];
10357 king = piece < (int) BlackPawn ? WhiteKing : BlackKing;
10358 if(gameInfo.variant == VariantKnightmate)
10359 king += (int) WhiteUnicorn - (int) WhiteKing;
10360 if(forwardMostMove == 0) {
10361 if(gameMode == MachinePlaysBlack || gameMode == BeginningOfGame)
10362 fprintf(serverMoves, "%s;", UserName());
10363 else if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b')
10364 fprintf(serverMoves, "%s;", second.tidy);
10365 fprintf(serverMoves, "%s;", first.tidy);
10366 if(gameMode == MachinePlaysWhite)
10367 fprintf(serverMoves, "%s;", UserName());
10368 else if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w')
10369 fprintf(serverMoves, "%s;", second.tidy);
10370 } else fprintf(serverMoves, loadFlag|lastLoadFlag ? ":" : ";");
10371 lastLoadFlag = loadFlag;
10373 fprintf(serverMoves, "%c%c:%c%c", AAA+fromX, ONE+fromY, AAA+toX, ONE+toY);
10374 // print castling suffix
10375 if( toY == fromY && piece == king ) {
10377 fprintf(serverMoves, ":%c%c:%c%c", AAA+BOARD_RGHT-1, ONE+fromY, AAA+toX-1,ONE+toY);
10379 fprintf(serverMoves, ":%c%c:%c%c", AAA+BOARD_LEFT, ONE+fromY, AAA+toX+1,ONE+toY);
10382 if( (boards[forwardMostMove][fromY][fromX] == WhitePawn ||
10383 boards[forwardMostMove][fromY][fromX] == BlackPawn ) &&
10384 boards[forwardMostMove][toY][toX] == EmptySquare
10385 && fromX != toX && fromY != toY)
10386 fprintf(serverMoves, ":%c%c:%c%c", AAA+fromX, ONE+fromY, AAA+toX, ONE+fromY);
10387 // promotion suffix
10388 if(promoChar != NULLCHAR) {
10389 if(fromY == 0 || fromY == BOARD_HEIGHT-1)
10390 fprintf(serverMoves, ":%c%c:%c%c", WhiteOnMove(forwardMostMove) ? 'w' : 'b',
10391 ToLower(promoChar), AAA+fromX, ONE+fromY); // Seirawan gating
10392 else fprintf(serverMoves, ":%c:%c%c", ToLower(promoChar), AAA+toX, ONE+toY);
10395 char buf[MOVE_LEN*2], *p; int len;
10396 fprintf(serverMoves, "/%d/%d",
10397 pvInfoList[forwardMostMove].depth, pvInfoList[forwardMostMove].score);
10398 if(forwardMostMove+1 & 1) timeLeft = whiteTimeRemaining/1000;
10399 else timeLeft = blackTimeRemaining/1000;
10400 fprintf(serverMoves, "/%d", timeLeft);
10401 strncpy(buf, parseList[forwardMostMove], MOVE_LEN*2);
10402 if(p = strchr(buf, '/')) *p = NULLCHAR; else
10403 if(p = strchr(buf, '=')) *p = NULLCHAR;
10404 len = strlen(buf); if(len > 1 && buf[len-2] != '-') buf[len-2] = NULLCHAR; // strip to-square
10405 fprintf(serverMoves, "/%s", buf);
10407 fflush(serverMoves);
10410 if (forwardMostMove+1 > framePtr) { // [HGM] vari: do not run into saved variations..
10411 GameEnds(GameUnfinished, _("Game too long; increase MAX_MOVES and recompile"), GE_XBOARD);
10414 UnLoadPV(); // [HGM] pv: if we are looking at a PV, abort this
10415 if (commentList[forwardMostMove+1] != NULL) {
10416 free(commentList[forwardMostMove+1]);
10417 commentList[forwardMostMove+1] = NULL;
10419 CopyBoard(boards[forwardMostMove+1], boards[forwardMostMove]);
10420 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[forwardMostMove+1]);
10421 // forwardMostMove++; // [HGM] bare: moved to after ApplyMove, to make sure clock interrupt finds complete board
10422 SwitchClocks(forwardMostMove+1); // [HGM] race: incrementing move nr inside
10423 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
10424 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
10425 adjustedClock = FALSE;
10426 gameInfo.result = GameUnfinished;
10427 if (gameInfo.resultDetails != NULL) {
10428 free(gameInfo.resultDetails);
10429 gameInfo.resultDetails = NULL;
10431 CoordsToComputerAlgebraic(fromY, fromX, toY, toX, promoChar,
10432 moveList[forwardMostMove - 1]);
10433 switch (MateTest(boards[forwardMostMove], PosFlags(forwardMostMove)) ) {
10439 if(!IS_SHOGI(gameInfo.variant))
10440 strcat(parseList[forwardMostMove - 1], "+");
10444 strcat(parseList[forwardMostMove - 1], "#");
10449 /* Updates currentMove if not pausing */
10451 ShowMove (int fromX, int fromY, int toX, int toY)
10453 int instant = (gameMode == PlayFromGameFile) ?
10454 (matchMode || (appData.timeDelay == 0 && !pausing)) : pausing;
10455 if(appData.noGUI) return;
10456 if (!pausing || gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
10458 if (forwardMostMove == currentMove + 1) {
10459 AnimateMove(boards[forwardMostMove - 1],
10460 fromX, fromY, toX, toY);
10463 currentMove = forwardMostMove;
10466 killX = killY = -1; // [HGM] lion: used up
10468 if (instant) return;
10470 DisplayMove(currentMove - 1);
10471 if (!pausing || gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
10472 if (appData.highlightLastMove) { // [HGM] moved to after DrawPosition, as with arrow it could redraw old board
10473 SetHighlights(fromX, fromY, toX, toY);
10476 DrawPosition(FALSE, boards[currentMove]);
10477 DisplayBothClocks();
10478 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
10482 SendEgtPath (ChessProgramState *cps)
10483 { /* [HGM] EGT: match formats given in feature with those given by user, and send info for each match */
10484 char buf[MSG_SIZ], name[MSG_SIZ], *p;
10486 if((p = cps->egtFormats) == NULL || appData.egtFormats == NULL) return;
10489 char c, *q = name+1, *r, *s;
10491 name[0] = ','; // extract next format name from feature and copy with prefixed ','
10492 while(*p && *p != ',') *q++ = *p++;
10493 *q++ = ':'; *q = 0;
10494 if( appData.defaultPathEGTB && appData.defaultPathEGTB[0] &&
10495 strcmp(name, ",nalimov:") == 0 ) {
10496 // take nalimov path from the menu-changeable option first, if it is defined
10497 snprintf(buf, MSG_SIZ, "egtpath nalimov %s\n", appData.defaultPathEGTB);
10498 SendToProgram(buf,cps); // send egtbpath command for nalimov
10500 if( (s = StrStr(appData.egtFormats, name+1)) == appData.egtFormats ||
10501 (s = StrStr(appData.egtFormats, name)) != NULL) {
10502 // format name occurs amongst user-supplied formats, at beginning or immediately after comma
10503 s = r = StrStr(s, ":") + 1; // beginning of path info
10504 while(*r && *r != ',') r++; // path info is everything upto next ';' or end of string
10505 c = *r; *r = 0; // temporarily null-terminate path info
10506 *--q = 0; // strip of trailig ':' from name
10507 snprintf(buf, MSG_SIZ, "egtpath %s %s\n", name+1, s);
10509 SendToProgram(buf,cps); // send egtbpath command for this format
10511 if(*p == ',') p++; // read away comma to position for next format name
10516 NonStandardBoardSize (VariantClass v, int boardWidth, int boardHeight, int holdingsSize)
10518 int width = 8, height = 8, holdings = 0; // most common sizes
10519 if( v == VariantUnknown || *engineVariant) return 0; // engine-defined name never needs prefix
10520 // correct the deviations default for each variant
10521 if( v == VariantXiangqi ) width = 9, height = 10;
10522 if( v == VariantShogi ) width = 9, height = 9, holdings = 7;
10523 if( v == VariantBughouse || v == VariantCrazyhouse) holdings = 5;
10524 if( v == VariantCapablanca || v == VariantCapaRandom ||
10525 v == VariantGothic || v == VariantFalcon || v == VariantJanus )
10527 if( v == VariantCourier ) width = 12;
10528 if( v == VariantSuper ) holdings = 8;
10529 if( v == VariantGreat ) width = 10, holdings = 8;
10530 if( v == VariantSChess ) holdings = 7;
10531 if( v == VariantGrand ) width = 10, height = 10, holdings = 7;
10532 if( v == VariantChuChess) width = 10, height = 10;
10533 if( v == VariantChu ) width = 12, height = 12;
10534 return boardWidth >= 0 && boardWidth != width || // -1 is default,
10535 boardHeight >= 0 && boardHeight != height || // and thus by definition OK
10536 holdingsSize >= 0 && holdingsSize != holdings;
10539 char variantError[MSG_SIZ];
10542 SupportedVariant (char *list, VariantClass v, int boardWidth, int boardHeight, int holdingsSize, int proto, char *engine)
10543 { // returns error message (recognizable by upper-case) if engine does not support the variant
10544 char *p, *variant = VariantName(v);
10545 static char b[MSG_SIZ];
10546 if(NonStandardBoardSize(v, boardWidth, boardHeight, holdingsSize)) { /* [HGM] make prefix for non-standard board size. */
10547 snprintf(b, MSG_SIZ, "%dx%d+%d_%s", boardWidth, boardHeight,
10548 holdingsSize, variant); // cook up sized variant name
10549 /* [HGM] varsize: try first if this deviant size variant is specifically known */
10550 if(StrStr(list, b) == NULL) {
10551 // specific sized variant not known, check if general sizing allowed
10552 if(proto != 1 && StrStr(list, "boardsize") == NULL) {
10553 snprintf(variantError, MSG_SIZ, "Board size %dx%d+%d not supported by %s",
10554 boardWidth, boardHeight, holdingsSize, engine);
10557 /* [HGM] here we really should compare with the maximum supported board size */
10559 } else snprintf(b, MSG_SIZ,"%s", variant);
10560 if(proto == 1) return b; // for protocol 1 we cannot check and hope for the best
10561 p = StrStr(list, b);
10562 while(p && (p != list && p[-1] != ',' || p[strlen(b)] && p[strlen(b)] != ',') ) p = StrStr(p+1, b);
10564 // occurs not at all in list, or only as sub-string
10565 snprintf(variantError, MSG_SIZ, _("Variant %s not supported by %s"), b, engine);
10566 if(p = StrStr(list, b)) { // handle requesting parent variant when only size-overridden is supported
10567 int l = strlen(variantError);
10569 while(p != list && p[-1] != ',') p--;
10570 q = strchr(p, ',');
10571 if(q) *q = NULLCHAR;
10572 snprintf(variantError + l, MSG_SIZ - l, _(", but %s is"), p);
10581 InitChessProgram (ChessProgramState *cps, int setup)
10582 /* setup needed to setup FRC opening position */
10584 char buf[MSG_SIZ], *b;
10585 if (appData.noChessProgram) return;
10586 hintRequested = FALSE;
10587 bookRequested = FALSE;
10589 ParseFeatures(appData.features[cps == &second], cps); // [HGM] allow user to overrule features
10590 /* [HGM] some new WB protocol commands to configure engine are sent now, if engine supports them */
10591 /* moved to before sending initstring in 4.3.15, so Polyglot can delay UCI 'isready' to recepton of 'new' */
10592 if(cps->memSize) { /* [HGM] memory */
10593 snprintf(buf, MSG_SIZ, "memory %d\n", appData.defaultHashSize + appData.defaultCacheSizeEGTB);
10594 SendToProgram(buf, cps);
10596 SendEgtPath(cps); /* [HGM] EGT */
10597 if(cps->maxCores) { /* [HGM] SMP: (protocol specified must be last settings command before new!) */
10598 snprintf(buf, MSG_SIZ, "cores %d\n", appData.smpCores);
10599 SendToProgram(buf, cps);
10602 setboardSpoiledMachineBlack = FALSE;
10603 SendToProgram(cps->initString, cps);
10604 if (gameInfo.variant != VariantNormal &&
10605 gameInfo.variant != VariantLoadable
10606 /* [HGM] also send variant if board size non-standard */
10607 || gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0) {
10609 b = SupportedVariant(cps->variants, gameInfo.variant, gameInfo.boardWidth,
10610 gameInfo.boardHeight, gameInfo.holdingsSize, cps->protocolVersion, cps->tidy);
10613 char c, *q = cps->variants, *p = strchr(q, ',');
10614 if(p) *p = NULLCHAR;
10615 v = StringToVariant(q);
10616 DisplayError(variantError, 0);
10617 if(v != VariantUnknown && cps == &first) {
10619 if(sscanf(q, "%dx%d+%d_%c", &w, &h, &s, &c) == 4) // get size overrides the engine needs with it (if any)
10620 appData.NrFiles = w, appData.NrRanks = h, appData.holdingsSize = s, q = strchr(q, '_') + 1;
10621 ASSIGN(appData.variant, q);
10622 Reset(TRUE, FALSE);
10628 snprintf(buf, MSG_SIZ, "variant %s\n", b);
10629 SendToProgram(buf, cps);
10631 currentlyInitializedVariant = gameInfo.variant;
10633 /* [HGM] send opening position in FRC to first engine */
10635 SendToProgram("force\n", cps);
10637 /* engine is now in force mode! Set flag to wake it up after first move. */
10638 setboardSpoiledMachineBlack = 1;
10641 if (cps->sendICS) {
10642 snprintf(buf, sizeof(buf), "ics %s\n", appData.icsActive ? appData.icsHost : "-");
10643 SendToProgram(buf, cps);
10645 cps->maybeThinking = FALSE;
10646 cps->offeredDraw = 0;
10647 if (!appData.icsActive) {
10648 SendTimeControl(cps, movesPerSession, timeControl,
10649 timeIncrement, appData.searchDepth,
10652 if (appData.showThinking
10653 // [HGM] thinking: four options require thinking output to be sent
10654 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp()
10656 SendToProgram("post\n", cps);
10658 SendToProgram("hard\n", cps);
10659 if (!appData.ponderNextMove) {
10660 /* Warning: "easy" is a toggle in GNU Chess, so don't send
10661 it without being sure what state we are in first. "hard"
10662 is not a toggle, so that one is OK.
10664 SendToProgram("easy\n", cps);
10666 if (cps->usePing) {
10667 snprintf(buf, MSG_SIZ, "ping %d\n", initPing = ++cps->lastPing);
10668 SendToProgram(buf, cps);
10670 cps->initDone = TRUE;
10671 ClearEngineOutputPane(cps == &second);
10676 ResendOptions (ChessProgramState *cps)
10677 { // send the stored value of the options
10680 Option *opt = cps->option;
10681 for(i=0; i<cps->nrOptions; i++, opt++) {
10682 switch(opt->type) {
10686 snprintf(buf, MSG_SIZ, "option %s=%d\n", opt->name, opt->value);
10689 snprintf(buf, MSG_SIZ, "option %s=%s\n", opt->name, opt->choice[opt->value]);
10692 snprintf(buf, MSG_SIZ, "option %s=%s\n", opt->name, opt->textValue);
10698 SendToProgram(buf, cps);
10703 StartChessProgram (ChessProgramState *cps)
10708 if (appData.noChessProgram) return;
10709 cps->initDone = FALSE;
10711 if (strcmp(cps->host, "localhost") == 0) {
10712 err = StartChildProcess(cps->program, cps->dir, &cps->pr);
10713 } else if (*appData.remoteShell == NULLCHAR) {
10714 err = OpenRcmd(cps->host, appData.remoteUser, cps->program, &cps->pr);
10716 if (*appData.remoteUser == NULLCHAR) {
10717 snprintf(buf, sizeof(buf), "%s %s %s", appData.remoteShell, cps->host,
10720 snprintf(buf, sizeof(buf), "%s %s -l %s %s", appData.remoteShell,
10721 cps->host, appData.remoteUser, cps->program);
10723 err = StartChildProcess(buf, "", &cps->pr);
10727 snprintf(buf, MSG_SIZ, _("Startup failure on '%s'"), cps->program);
10728 DisplayError(buf, err); // [HGM] bit of a rough kludge: ignore failure, (which XBoard would do anyway), and let I/O discover it
10729 if(cps != &first) return;
10730 appData.noChessProgram = TRUE;
10733 // DisplayFatalError(buf, err, 1);
10734 // cps->pr = NoProc;
10735 // cps->isr = NULL;
10739 cps->isr = AddInputSource(cps->pr, TRUE, ReceiveFromProgram, cps);
10740 if (cps->protocolVersion > 1) {
10741 snprintf(buf, MSG_SIZ, "xboard\nprotover %d\n", cps->protocolVersion);
10742 if(!cps->reload) { // do not clear options when reloading because of -xreuse
10743 cps->nrOptions = 0; // [HGM] options: clear all engine-specific options
10744 cps->comboCnt = 0; // and values of combo boxes
10746 SendToProgram(buf, cps);
10747 if(cps->reload) ResendOptions(cps);
10749 SendToProgram("xboard\n", cps);
10754 TwoMachinesEventIfReady P((void))
10756 static int curMess = 0;
10757 if (first.lastPing != first.lastPong) {
10758 if(curMess != 1) DisplayMessage("", _("Waiting for first chess program")); curMess = 1;
10759 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10); // [HGM] fast: lowered from 1000
10762 if (second.lastPing != second.lastPong) {
10763 if(curMess != 2) DisplayMessage("", _("Waiting for second chess program")); curMess = 2;
10764 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10); // [HGM] fast: lowered from 1000
10767 DisplayMessage("", ""); curMess = 0;
10768 TwoMachinesEvent();
10772 MakeName (char *template)
10776 static char buf[MSG_SIZ];
10780 clock = time((time_t *)NULL);
10781 tm = localtime(&clock);
10783 while(*p++ = *template++) if(p[-1] == '%') {
10784 switch(*template++) {
10785 case 0: *p = 0; return buf;
10786 case 'Y': i = tm->tm_year+1900; break;
10787 case 'y': i = tm->tm_year-100; break;
10788 case 'M': i = tm->tm_mon+1; break;
10789 case 'd': i = tm->tm_mday; break;
10790 case 'h': i = tm->tm_hour; break;
10791 case 'm': i = tm->tm_min; break;
10792 case 's': i = tm->tm_sec; break;
10795 snprintf(p-1, MSG_SIZ-10 - (p - buf), "%02d", i); p += strlen(p);
10801 CountPlayers (char *p)
10804 while(p = strchr(p, '\n')) p++, n++; // count participants
10809 WriteTourneyFile (char *results, FILE *f)
10810 { // write tournament parameters on tourneyFile; on success return the stream pointer for closing
10811 if(f == NULL) f = fopen(appData.tourneyFile, "w");
10812 if(f == NULL) DisplayError(_("Could not write on tourney file"), 0); else {
10813 // create a file with tournament description
10814 fprintf(f, "-participants {%s}\n", appData.participants);
10815 fprintf(f, "-seedBase %d\n", appData.seedBase);
10816 fprintf(f, "-tourneyType %d\n", appData.tourneyType);
10817 fprintf(f, "-tourneyCycles %d\n", appData.tourneyCycles);
10818 fprintf(f, "-defaultMatchGames %d\n", appData.defaultMatchGames);
10819 fprintf(f, "-syncAfterRound %s\n", appData.roundSync ? "true" : "false");
10820 fprintf(f, "-syncAfterCycle %s\n", appData.cycleSync ? "true" : "false");
10821 fprintf(f, "-saveGameFile \"%s\"\n", appData.saveGameFile);
10822 fprintf(f, "-loadGameFile \"%s\"\n", appData.loadGameFile);
10823 fprintf(f, "-loadGameIndex %d\n", appData.loadGameIndex);
10824 fprintf(f, "-loadPositionFile \"%s\"\n", appData.loadPositionFile);
10825 fprintf(f, "-loadPositionIndex %d\n", appData.loadPositionIndex);
10826 fprintf(f, "-rewindIndex %d\n", appData.rewindIndex);
10827 fprintf(f, "-usePolyglotBook %s\n", appData.usePolyglotBook ? "true" : "false");
10828 fprintf(f, "-polyglotBook \"%s\"\n", appData.polyglotBook);
10829 fprintf(f, "-bookDepth %d\n", appData.bookDepth);
10830 fprintf(f, "-bookVariation %d\n", appData.bookStrength);
10831 fprintf(f, "-discourageOwnBooks %s\n", appData.defNoBook ? "true" : "false");
10832 fprintf(f, "-defaultHashSize %d\n", appData.defaultHashSize);
10833 fprintf(f, "-defaultCacheSizeEGTB %d\n", appData.defaultCacheSizeEGTB);
10834 fprintf(f, "-ponderNextMove %s\n", appData.ponderNextMove ? "true" : "false");
10835 fprintf(f, "-smpCores %d\n", appData.smpCores);
10837 fprintf(f, "-searchTime \"%d:%02d\"\n", searchTime/60, searchTime%60);
10839 fprintf(f, "-mps %d\n", appData.movesPerSession);
10840 fprintf(f, "-tc %s\n", appData.timeControl);
10841 fprintf(f, "-inc %.2f\n", appData.timeIncrement);
10843 fprintf(f, "-results \"%s\"\n", results);
10848 char *command[MAXENGINES], *mnemonic[MAXENGINES];
10851 Substitute (char *participants, int expunge)
10853 int i, changed, changes=0, nPlayers=0;
10854 char *p, *q, *r, buf[MSG_SIZ];
10855 if(participants == NULL) return;
10856 if(appData.tourneyFile[0] == NULLCHAR) { free(participants); return; }
10857 r = p = participants; q = appData.participants;
10858 while(*p && *p == *q) {
10859 if(*p == '\n') r = p+1, nPlayers++;
10862 if(*p) { // difference
10863 while(*p && *p++ != '\n');
10864 while(*q && *q++ != '\n');
10865 changed = nPlayers;
10866 changes = 1 + (strcmp(p, q) != 0);
10868 if(changes == 1) { // a single engine mnemonic was changed
10869 q = r; while(*q) nPlayers += (*q++ == '\n');
10870 p = buf; while(*r && (*p = *r++) != '\n') p++;
10872 NamesToList(firstChessProgramNames, command, mnemonic, "all");
10873 for(i=1; mnemonic[i]; i++) if(!strcmp(buf, mnemonic[i])) break;
10874 if(mnemonic[i]) { // The substitute is valid
10876 if(appData.tourneyFile[0] && (f = fopen(appData.tourneyFile, "r+")) ) {
10877 flock(fileno(f), LOCK_EX);
10878 ParseArgsFromFile(f);
10879 fseek(f, 0, SEEK_SET);
10880 FREE(appData.participants); appData.participants = participants;
10881 if(expunge) { // erase results of replaced engine
10882 int len = strlen(appData.results), w, b, dummy;
10883 for(i=0; i<len; i++) {
10884 Pairing(i, nPlayers, &w, &b, &dummy);
10885 if((w == changed || b == changed) && appData.results[i] == '*') {
10886 DisplayError(_("You cannot replace an engine while it is engaged!\nTerminate its game first."), 0);
10891 for(i=0; i<len; i++) {
10892 Pairing(i, nPlayers, &w, &b, &dummy);
10893 if(w == changed || b == changed) appData.results[i] = ' '; // mark as not played
10896 WriteTourneyFile(appData.results, f);
10897 fclose(f); // release lock
10900 } else DisplayError(_("No engine with the name you gave is installed"), 0);
10902 if(changes == 0) DisplayError(_("First change an engine by editing the participants list\nof the Tournament Options dialog"), 0);
10903 if(changes > 1) DisplayError(_("You can only change one engine at the time"), 0);
10904 free(participants);
10909 CheckPlayers (char *participants)
10912 char buf[MSG_SIZ], *p;
10913 NamesToList(firstChessProgramNames, command, mnemonic, "all");
10914 while(p = strchr(participants, '\n')) {
10916 for(i=1; mnemonic[i]; i++) if(!strcmp(participants, mnemonic[i])) break;
10918 snprintf(buf, MSG_SIZ, _("No engine %s is installed"), participants);
10920 DisplayError(buf, 0);
10924 participants = p + 1;
10930 CreateTourney (char *name)
10933 if(matchMode && strcmp(name, appData.tourneyFile)) {
10934 ASSIGN(name, appData.tourneyFile); //do not allow change of tourneyfile while playing
10936 if(name[0] == NULLCHAR) {
10937 if(appData.participants[0])
10938 DisplayError(_("You must supply a tournament file,\nfor storing the tourney progress"), 0);
10941 f = fopen(name, "r");
10942 if(f) { // file exists
10943 ASSIGN(appData.tourneyFile, name);
10944 ParseArgsFromFile(f); // parse it
10946 if(!appData.participants[0]) return 0; // ignore tourney file if non-existing & no participants
10947 if(CountPlayers(appData.participants) < (appData.tourneyType>0 ? appData.tourneyType+1 : 2)) {
10948 DisplayError(_("Not enough participants"), 0);
10951 if(CheckPlayers(appData.participants)) return 0;
10952 ASSIGN(appData.tourneyFile, name);
10953 if(appData.tourneyType < 0) appData.defaultMatchGames = 1; // Swiss forces games/pairing = 1
10954 if((f = WriteTourneyFile("", NULL)) == NULL) return 0;
10957 appData.noChessProgram = FALSE;
10958 appData.clockMode = TRUE;
10964 NamesToList (char *names, char **engineList, char **engineMnemonic, char *group)
10966 char buf[MSG_SIZ], *p, *q;
10967 int i=1, header, skip, all = !strcmp(group, "all"), depth = 0;
10968 insert = names; // afterwards, this global will point just after last retrieved engine line or group end in the 'names'
10969 skip = !all && group[0]; // if group requested, we start in skip mode
10970 for(;*names && depth >= 0 && i < MAXENGINES-1; names = p) {
10971 p = names; q = buf; header = 0;
10972 while(*p && *p != '\n') *q++ = *p++;
10974 if(*p == '\n') p++;
10975 if(buf[0] == '#') {
10976 if(strstr(buf, "# end") == buf) { if(!--depth) insert = p; continue; } // leave group, and suppress printing label
10977 depth++; // we must be entering a new group
10978 if(all) continue; // suppress printing group headers when complete list requested
10980 if(skip && !strcmp(group, buf)) { depth = 0; skip = FALSE; } // start when we reach requested group
10982 if(depth != header && !all || skip) continue; // skip contents of group (but print first-level header)
10983 if(engineList[i]) free(engineList[i]);
10984 engineList[i] = strdup(buf);
10985 if(buf[0] != '#') insert = p, TidyProgramName(engineList[i], "localhost", buf); // group headers not tidied
10986 if(engineMnemonic[i]) free(engineMnemonic[i]);
10987 if((q = strstr(engineList[i]+2, "variant")) && q[-2]== ' ' && (q[-1]=='/' || q[-1]=='-') && (q[7]==' ' || q[7]=='=')) {
10989 sscanf(q + 8, "%s", buf + strlen(buf));
10992 engineMnemonic[i] = strdup(buf);
10995 engineList[i] = engineMnemonic[i] = NULL;
10999 // following implemented as macro to avoid type limitations
11000 #define SWAP(item, temp) temp = appData.item[0]; appData.item[0] = appData.item[n]; appData.item[n] = temp;
11003 SwapEngines (int n)
11004 { // swap settings for first engine and other engine (so far only some selected options)
11009 SWAP(chessProgram, p)
11011 SWAP(hasOwnBookUCI, h)
11012 SWAP(protocolVersion, h)
11014 SWAP(scoreIsAbsolute, h)
11019 SWAP(engOptions, p)
11020 SWAP(engInitString, p)
11021 SWAP(computerString, p)
11023 SWAP(fenOverride, p)
11025 SWAP(accumulateTC, h)
11032 GetEngineLine (char *s, int n)
11036 extern char *icsNames;
11037 if(!s || !*s) return 0;
11038 NamesToList(n >= 10 ? icsNames : firstChessProgramNames, command, mnemonic, "all");
11039 for(i=1; mnemonic[i]; i++) if(!strcmp(s, mnemonic[i])) break;
11040 if(!mnemonic[i]) return 0;
11041 if(n == 11) return 1; // just testing if there was a match
11042 snprintf(buf, MSG_SIZ, "-%s %s", n == 10 ? "icshost" : "fcp", command[i]);
11043 if(n == 1) SwapEngines(n);
11044 ParseArgsFromString(buf);
11045 if(n == 1) SwapEngines(n);
11046 if(n == 0 && *appData.secondChessProgram == NULLCHAR) {
11047 SwapEngines(1); // set second same as first if not yet set (to suppress WB startup dialog)
11048 ParseArgsFromString(buf);
11054 SetPlayer (int player, char *p)
11055 { // [HGM] find the engine line of the partcipant given by number, and parse its options.
11057 char buf[MSG_SIZ], *engineName;
11058 for(i=0; i<player; i++) p = strchr(p, '\n') + 1;
11059 engineName = strdup(p); if(p = strchr(engineName, '\n')) *p = NULLCHAR;
11060 for(i=1; command[i]; i++) if(!strcmp(mnemonic[i], engineName)) break;
11062 snprintf(buf, MSG_SIZ, "-fcp %s", command[i]);
11063 ParseArgsFromString(resetOptions); appData.fenOverride[0] = NULL; appData.pvSAN[0] = FALSE;
11064 appData.firstHasOwnBookUCI = !appData.defNoBook; appData.protocolVersion[0] = PROTOVER;
11065 ParseArgsFromString(buf);
11066 } else { // no engine with this nickname is installed!
11067 snprintf(buf, MSG_SIZ, _("No engine %s is installed"), engineName);
11068 ReserveGame(nextGame, ' '); // unreserve game and drop out of match mode with error
11069 matchMode = FALSE; appData.matchGames = matchGame = roundNr = 0;
11071 DisplayError(buf, 0);
11078 char *recentEngines;
11081 RecentEngineEvent (int nr)
11084 // SwapEngines(1); // bump first to second
11085 // ReplaceEngine(&second, 1); // and load it there
11086 NamesToList(firstChessProgramNames, command, mnemonic, "all"); // get mnemonics of installed engines
11087 n = SetPlayer(nr, recentEngines); // select new (using original menu order!)
11088 if(mnemonic[n]) { // if somehow the engine with the selected nickname is no longer found in the list, we skip
11089 ReplaceEngine(&first, 0);
11090 FloatToFront(&appData.recentEngineList, command[n]);
11095 Pairing (int nr, int nPlayers, int *whitePlayer, int *blackPlayer, int *syncInterval)
11096 { // determine players from game number
11097 int curCycle, curRound, curPairing, gamesPerCycle, gamesPerRound, roundsPerCycle=1, pairingsPerRound=1;
11099 if(appData.tourneyType == 0) {
11100 roundsPerCycle = (nPlayers - 1) | 1;
11101 pairingsPerRound = nPlayers / 2;
11102 } else if(appData.tourneyType > 0) {
11103 roundsPerCycle = nPlayers - appData.tourneyType;
11104 pairingsPerRound = appData.tourneyType;
11106 gamesPerRound = pairingsPerRound * appData.defaultMatchGames;
11107 gamesPerCycle = gamesPerRound * roundsPerCycle;
11108 appData.matchGames = gamesPerCycle * appData.tourneyCycles - 1; // fake like all games are one big match
11109 curCycle = nr / gamesPerCycle; nr %= gamesPerCycle;
11110 curRound = nr / gamesPerRound; nr %= gamesPerRound;
11111 curPairing = nr / appData.defaultMatchGames; nr %= appData.defaultMatchGames;
11112 matchGame = nr + curCycle * appData.defaultMatchGames + 1; // fake game nr that loads correct game or position from file
11113 roundNr = (curCycle * roundsPerCycle + curRound) * appData.defaultMatchGames + nr + 1;
11115 if(appData.cycleSync) *syncInterval = gamesPerCycle;
11116 if(appData.roundSync) *syncInterval = gamesPerRound;
11118 if(appData.debugMode) fprintf(debugFP, "cycle=%d, round=%d, pairing=%d curGame=%d\n", curCycle, curRound, curPairing, matchGame);
11120 if(appData.tourneyType == 0) {
11121 if(curPairing == (nPlayers-1)/2 ) {
11122 *whitePlayer = curRound;
11123 *blackPlayer = nPlayers - 1; // this is the 'bye' when nPlayer is odd
11125 *whitePlayer = curRound - (nPlayers-1)/2 + curPairing;
11126 if(*whitePlayer < 0) *whitePlayer += nPlayers-1+(nPlayers&1);
11127 *blackPlayer = curRound + (nPlayers-1)/2 - curPairing;
11128 if(*blackPlayer >= nPlayers-1+(nPlayers&1)) *blackPlayer -= nPlayers-1+(nPlayers&1);
11130 } else if(appData.tourneyType > 1) {
11131 *blackPlayer = curPairing; // in multi-gauntlet, assign gauntlet engines to second, so first an be kept loaded during round
11132 *whitePlayer = curRound + appData.tourneyType;
11133 } else if(appData.tourneyType > 0) {
11134 *whitePlayer = curPairing;
11135 *blackPlayer = curRound + appData.tourneyType;
11138 // take care of white/black alternation per round.
11139 // For cycles and games this is already taken care of by default, derived from matchGame!
11140 return curRound & 1;
11144 NextTourneyGame (int nr, int *swapColors)
11145 { // !!!major kludge!!! fiddle appData settings to get everything in order for next tourney game
11147 int whitePlayer, blackPlayer, firstBusy=1000000000, syncInterval = 0, nPlayers, OK = 1;
11149 if(appData.tourneyFile[0] == NULLCHAR) return 1; // no tourney, always allow next game
11150 tf = fopen(appData.tourneyFile, "r");
11151 if(tf == NULL) { DisplayFatalError(_("Bad tournament file"), 0, 1); return 0; }
11152 ParseArgsFromFile(tf); fclose(tf);
11153 InitTimeControls(); // TC might be altered from tourney file
11155 nPlayers = CountPlayers(appData.participants); // count participants
11156 if(appData.tourneyType < 0) syncInterval = nPlayers/2; else
11157 *swapColors = Pairing(nr<0 ? 0 : nr, nPlayers, &whitePlayer, &blackPlayer, &syncInterval);
11160 p = q = appData.results;
11161 while(*q) if(*q++ == '*' || q[-1] == ' ') { firstBusy = q - p - 1; break; }
11162 if(firstBusy/syncInterval < (nextGame/syncInterval)) {
11163 DisplayMessage(_("Waiting for other game(s)"),"");
11164 waitingForGame = TRUE;
11165 ScheduleDelayedEvent(NextMatchGame, 1000); // wait for all games of previous round to finish
11168 waitingForGame = FALSE;
11171 if(appData.tourneyType < 0) {
11172 if(nr>=0 && !pairingReceived) {
11174 if(pairing.pr == NoProc) {
11175 if(!appData.pairingEngine[0]) {
11176 DisplayFatalError(_("No pairing engine specified"), 0, 1);
11179 StartChessProgram(&pairing); // starts the pairing engine
11181 snprintf(buf, 1<<16, "results %d %s\n", nPlayers, appData.results);
11182 SendToProgram(buf, &pairing);
11183 snprintf(buf, 1<<16, "pairing %d\n", nr+1);
11184 SendToProgram(buf, &pairing);
11185 return 0; // wait for pairing engine to answer (which causes NextTourneyGame to be called again...
11187 pairingReceived = 0; // ... so we continue here
11189 appData.matchGames = appData.tourneyCycles * syncInterval - 1;
11190 whitePlayer = savedWhitePlayer-1; blackPlayer = savedBlackPlayer-1;
11191 matchGame = 1; roundNr = nr / syncInterval + 1;
11194 if(first.pr != NoProc && second.pr != NoProc || nr<0) return 1; // engines already loaded
11196 // redefine engines, engine dir, etc.
11197 NamesToList(firstChessProgramNames, command, mnemonic, "all"); // get mnemonics of installed engines
11198 if(first.pr == NoProc) {
11199 if(!SetPlayer(whitePlayer, appData.participants)) OK = 0; // find white player amongst it, and parse its engine line
11200 InitEngine(&first, 0); // initialize ChessProgramStates based on new settings.
11202 if(second.pr == NoProc) {
11204 if(!SetPlayer(blackPlayer, appData.participants)) OK = 0; // find black player amongst it, and parse its engine line
11205 SwapEngines(1); // and make that valid for second engine by swapping
11206 InitEngine(&second, 1);
11208 CommonEngineInit(); // after this TwoMachinesEvent will create correct engine processes
11209 UpdateLogos(FALSE); // leave display to ModeHiglight()
11215 { // performs game initialization that does not invoke engines, and then tries to start the game
11216 int res, firstWhite, swapColors = 0;
11217 if(!NextTourneyGame(nextGame, &swapColors)) return; // this sets matchGame, -fcp / -scp and other options for next game, if needed
11218 if(matchMode && appData.debugMode) { // [HGM] debug split: game is part of a match; we might have to create a debug file just for this game
11220 snprintf(buf, MSG_SIZ, appData.nameOfDebugFile, nextGame+1); // expand name of debug file with %d in it
11221 if(strcmp(buf, currentDebugFile)) { // name has changed
11222 FILE *f = fopen(buf, "w");
11223 if(f) { // if opening the new file failed, just keep using the old one
11224 ASSIGN(currentDebugFile, buf);
11228 if(appData.serverFileName) {
11229 if(serverFP) fclose(serverFP);
11230 serverFP = fopen(appData.serverFileName, "w");
11231 if(serverFP && first.pr != NoProc) fprintf(serverFP, "StartChildProcess (dir=\".\") .\\%s\n", first.tidy);
11232 if(serverFP && second.pr != NoProc) fprintf(serverFP, "StartChildProcess (dir=\".\") .\\%s\n", second.tidy);
11236 firstWhite = appData.firstPlaysBlack ^ (matchGame & 1 | appData.sameColorGames > 1); // non-incremental default
11237 firstWhite ^= swapColors; // reverses if NextTourneyGame says we are in an odd round
11238 first.twoMachinesColor = firstWhite ? "white\n" : "black\n"; // perform actual color assignement
11239 second.twoMachinesColor = firstWhite ? "black\n" : "white\n";
11240 appData.noChessProgram = (first.pr == NoProc); // kludge to prevent Reset from starting up chess program
11241 if(appData.loadGameIndex == -2) srandom(appData.seedBase + 68163*(nextGame & ~1)); // deterministic seed to force same opening
11242 Reset(FALSE, first.pr != NoProc);
11243 res = LoadGameOrPosition(matchGame); // setup game
11244 appData.noChessProgram = FALSE; // LoadGameOrPosition might call Reset too!
11245 if(!res) return; // abort when bad game/pos file
11246 TwoMachinesEvent();
11250 UserAdjudicationEvent (int result)
11252 ChessMove gameResult = GameIsDrawn;
11255 gameResult = WhiteWins;
11257 else if( result < 0 ) {
11258 gameResult = BlackWins;
11261 if( gameMode == TwoMachinesPlay ) {
11262 GameEnds( gameResult, "User adjudication", GE_XBOARD );
11267 // [HGM] save: calculate checksum of game to make games easily identifiable
11269 StringCheckSum (char *s)
11272 if(s==NULL) return 0;
11273 while(*s) i = i*259 + *s++;
11281 for(i=backwardMostMove; i<forwardMostMove; i++) {
11282 sum += pvInfoList[i].depth;
11283 sum += StringCheckSum(parseList[i]);
11284 sum += StringCheckSum(commentList[i]);
11287 if(i>1 && sum==0) sum++; // make sure never zero for non-empty game
11288 return sum + StringCheckSum(commentList[i]);
11289 } // end of save patch
11292 GameEnds (ChessMove result, char *resultDetails, int whosays)
11294 GameMode nextGameMode;
11296 char buf[MSG_SIZ], popupRequested = 0, *ranking = NULL;
11298 if(endingGame) return; /* [HGM] crash: forbid recursion */
11300 if(twoBoards) { // [HGM] dual: switch back to one board
11301 twoBoards = partnerUp = 0; InitDrawingSizes(-2, 0);
11302 DrawPosition(TRUE, partnerBoard); // observed game becomes foreground
11304 if (appData.debugMode) {
11305 fprintf(debugFP, "GameEnds(%d, %s, %d)\n",
11306 result, resultDetails ? resultDetails : "(null)", whosays);
11309 fromX = fromY = killX = killY = -1; // [HGM] abort any move the user is entering. // [HGM] lion
11311 if(pausing) PauseEvent(); // can happen when we abort a paused game (New Game or Quit)
11313 if (appData.icsActive && (whosays == GE_ENGINE || whosays >= GE_ENGINE1)) {
11314 /* If we are playing on ICS, the server decides when the
11315 game is over, but the engine can offer to draw, claim
11319 if (appData.zippyPlay && first.initDone) {
11320 if (result == GameIsDrawn) {
11321 /* In case draw still needs to be claimed */
11322 SendToICS(ics_prefix);
11323 SendToICS("draw\n");
11324 } else if (StrCaseStr(resultDetails, "resign")) {
11325 SendToICS(ics_prefix);
11326 SendToICS("resign\n");
11330 endingGame = 0; /* [HGM] crash */
11334 /* If we're loading the game from a file, stop */
11335 if (whosays == GE_FILE) {
11336 (void) StopLoadGameTimer();
11340 /* Cancel draw offers */
11341 first.offeredDraw = second.offeredDraw = 0;
11343 /* If this is an ICS game, only ICS can really say it's done;
11344 if not, anyone can. */
11345 isIcsGame = (gameMode == IcsPlayingWhite ||
11346 gameMode == IcsPlayingBlack ||
11347 gameMode == IcsObserving ||
11348 gameMode == IcsExamining);
11350 if (!isIcsGame || whosays == GE_ICS) {
11351 /* OK -- not an ICS game, or ICS said it was done */
11353 if (!isIcsGame && !appData.noChessProgram)
11354 SetUserThinkingEnables();
11356 /* [HGM] if a machine claims the game end we verify this claim */
11357 if(gameMode == TwoMachinesPlay && appData.testClaims) {
11358 if(appData.testLegality && whosays >= GE_ENGINE1 ) {
11360 ChessMove trueResult = (ChessMove) -1;
11362 claimer = whosays == GE_ENGINE1 ? /* color of claimer */
11363 first.twoMachinesColor[0] :
11364 second.twoMachinesColor[0] ;
11366 // [HGM] losers: because the logic is becoming a bit hairy, determine true result first
11367 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_CHECKMATE) {
11368 /* [HGM] verify: engine mate claims accepted if they were flagged */
11369 trueResult = WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins;
11371 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_WINS) { // added code for games where being mated is a win
11372 /* [HGM] verify: engine mate claims accepted if they were flagged */
11373 trueResult = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
11375 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_STALEMATE) { // only used to indicate draws now
11376 trueResult = GameIsDrawn; // default; in variants where stalemate loses, Status is CHECKMATE
11379 // now verify win claims, but not in drop games, as we don't understand those yet
11380 if( (gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper
11381 || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand) &&
11382 (result == WhiteWins && claimer == 'w' ||
11383 result == BlackWins && claimer == 'b' ) ) { // case to verify: engine claims own win
11384 if (appData.debugMode) {
11385 fprintf(debugFP, "result=%d sp=%d move=%d\n",
11386 result, (signed char)boards[forwardMostMove][EP_STATUS], forwardMostMove);
11388 if(result != trueResult) {
11389 snprintf(buf, MSG_SIZ, "False win claim: '%s'", resultDetails);
11390 result = claimer == 'w' ? BlackWins : WhiteWins;
11391 resultDetails = buf;
11394 if( result == GameIsDrawn && (signed char)boards[forwardMostMove][EP_STATUS] > EP_DRAWS
11395 && (forwardMostMove <= backwardMostMove ||
11396 (signed char)boards[forwardMostMove-1][EP_STATUS] > EP_DRAWS ||
11397 (claimer=='b')==(forwardMostMove&1))
11399 /* [HGM] verify: draws that were not flagged are false claims */
11400 snprintf(buf, MSG_SIZ, "False draw claim: '%s'", resultDetails);
11401 result = claimer == 'w' ? BlackWins : WhiteWins;
11402 resultDetails = buf;
11404 /* (Claiming a loss is accepted no questions asked!) */
11405 } else if(matchMode && result == GameIsDrawn && !strcmp(resultDetails, "Engine Abort Request")) {
11406 forwardMostMove = backwardMostMove; // [HGM] delete game to surpress saving
11407 result = GameUnfinished;
11408 if(!*appData.tourneyFile) matchGame--; // replay even in plain match
11410 /* [HGM] bare: don't allow bare King to win */
11411 if((gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper
11412 || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand)
11413 && gameInfo.variant != VariantLosers && gameInfo.variant != VariantGiveaway
11414 && gameInfo.variant != VariantSuicide // [HGM] losers: except in losers, of course...
11415 && result != GameIsDrawn)
11416 { int i, j, k=0, color = (result==WhiteWins ? (int)WhitePawn : (int)BlackPawn);
11417 for(j=BOARD_LEFT; j<BOARD_RGHT; j++) for(i=0; i<BOARD_HEIGHT; i++) {
11418 int p = (signed char)boards[forwardMostMove][i][j] - color;
11419 if(p >= 0 && p <= (int)WhiteKing) k++;
11421 if (appData.debugMode) {
11422 fprintf(debugFP, "GE(%d, %s, %d) bare king k=%d color=%d\n",
11423 result, resultDetails ? resultDetails : "(null)", whosays, k, color);
11426 result = GameIsDrawn;
11427 snprintf(buf, MSG_SIZ, "%s but bare king", resultDetails);
11428 resultDetails = buf;
11434 if(serverMoves != NULL && !loadFlag) { char c = '=';
11435 if(result==WhiteWins) c = '+';
11436 if(result==BlackWins) c = '-';
11437 if(resultDetails != NULL)
11438 fprintf(serverMoves, ";%c;%s\n", c, resultDetails), fflush(serverMoves);
11440 if (resultDetails != NULL) {
11441 gameInfo.result = result;
11442 gameInfo.resultDetails = StrSave(resultDetails);
11444 /* display last move only if game was not loaded from file */
11445 if ((whosays != GE_FILE) && (currentMove == forwardMostMove))
11446 DisplayMove(currentMove - 1);
11448 if (forwardMostMove != 0) {
11449 if (gameMode != PlayFromGameFile && gameMode != EditGame
11450 && lastSavedGame != GameCheckSum() // [HGM] save: suppress duplicates
11452 if (*appData.saveGameFile != NULLCHAR) {
11453 if(result == GameUnfinished && matchMode && *appData.tourneyFile)
11454 AutoSaveGame(); // [HGM] protect tourney PGN from aborted games, and prompt for name instead
11456 SaveGameToFile(appData.saveGameFile, TRUE);
11457 } else if (appData.autoSaveGames) {
11458 if(gameMode != IcsObserving || !appData.onlyOwn) AutoSaveGame();
11460 if (*appData.savePositionFile != NULLCHAR) {
11461 SavePositionToFile(appData.savePositionFile);
11463 AddGameToBook(FALSE); // Only does something during Monte-Carlo book building
11467 /* Tell program how game ended in case it is learning */
11468 /* [HGM] Moved this to after saving the PGN, just in case */
11469 /* engine died and we got here through time loss. In that */
11470 /* case we will get a fatal error writing the pipe, which */
11471 /* would otherwise lose us the PGN. */
11472 /* [HGM] crash: not needed anymore, but doesn't hurt; */
11473 /* output during GameEnds should never be fatal anymore */
11474 if (gameMode == MachinePlaysWhite ||
11475 gameMode == MachinePlaysBlack ||
11476 gameMode == TwoMachinesPlay ||
11477 gameMode == IcsPlayingWhite ||
11478 gameMode == IcsPlayingBlack ||
11479 gameMode == BeginningOfGame) {
11481 snprintf(buf, MSG_SIZ, "result %s {%s}\n", PGNResult(result),
11483 if (first.pr != NoProc) {
11484 SendToProgram(buf, &first);
11486 if (second.pr != NoProc &&
11487 gameMode == TwoMachinesPlay) {
11488 SendToProgram(buf, &second);
11493 if (appData.icsActive) {
11494 if (appData.quietPlay &&
11495 (gameMode == IcsPlayingWhite ||
11496 gameMode == IcsPlayingBlack)) {
11497 SendToICS(ics_prefix);
11498 SendToICS("set shout 1\n");
11500 nextGameMode = IcsIdle;
11501 ics_user_moved = FALSE;
11502 /* clean up premove. It's ugly when the game has ended and the
11503 * premove highlights are still on the board.
11506 gotPremove = FALSE;
11507 ClearPremoveHighlights();
11508 DrawPosition(FALSE, boards[currentMove]);
11510 if (whosays == GE_ICS) {
11513 if (gameMode == IcsPlayingWhite)
11515 else if(gameMode == IcsPlayingBlack)
11516 PlayIcsLossSound();
11519 if (gameMode == IcsPlayingBlack)
11521 else if(gameMode == IcsPlayingWhite)
11522 PlayIcsLossSound();
11525 PlayIcsDrawSound();
11528 PlayIcsUnfinishedSound();
11531 if(appData.quitNext) { ExitEvent(0); return; }
11532 } else if (gameMode == EditGame ||
11533 gameMode == PlayFromGameFile ||
11534 gameMode == AnalyzeMode ||
11535 gameMode == AnalyzeFile) {
11536 nextGameMode = gameMode;
11538 nextGameMode = EndOfGame;
11543 nextGameMode = gameMode;
11546 if (appData.noChessProgram) {
11547 gameMode = nextGameMode;
11549 endingGame = 0; /* [HGM] crash */
11554 /* Put first chess program into idle state */
11555 if (first.pr != NoProc &&
11556 (gameMode == MachinePlaysWhite ||
11557 gameMode == MachinePlaysBlack ||
11558 gameMode == TwoMachinesPlay ||
11559 gameMode == IcsPlayingWhite ||
11560 gameMode == IcsPlayingBlack ||
11561 gameMode == BeginningOfGame)) {
11562 SendToProgram("force\n", &first);
11563 if (first.usePing) {
11565 snprintf(buf, MSG_SIZ, "ping %d\n", ++first.lastPing);
11566 SendToProgram(buf, &first);
11569 } else if (result != GameUnfinished || nextGameMode == IcsIdle) {
11570 /* Kill off first chess program */
11571 if (first.isr != NULL)
11572 RemoveInputSource(first.isr);
11575 if (first.pr != NoProc) {
11577 DoSleep( appData.delayBeforeQuit );
11578 SendToProgram("quit\n", &first);
11579 DestroyChildProcess(first.pr, 4 + first.useSigterm);
11580 first.reload = TRUE;
11584 if (second.reuse) {
11585 /* Put second chess program into idle state */
11586 if (second.pr != NoProc &&
11587 gameMode == TwoMachinesPlay) {
11588 SendToProgram("force\n", &second);
11589 if (second.usePing) {
11591 snprintf(buf, MSG_SIZ, "ping %d\n", ++second.lastPing);
11592 SendToProgram(buf, &second);
11595 } else if (result != GameUnfinished || nextGameMode == IcsIdle) {
11596 /* Kill off second chess program */
11597 if (second.isr != NULL)
11598 RemoveInputSource(second.isr);
11601 if (second.pr != NoProc) {
11602 DoSleep( appData.delayBeforeQuit );
11603 SendToProgram("quit\n", &second);
11604 DestroyChildProcess(second.pr, 4 + second.useSigterm);
11605 second.reload = TRUE;
11607 second.pr = NoProc;
11610 if (matchMode && (gameMode == TwoMachinesPlay || (waitingForGame || startingEngine) && exiting)) {
11611 char resChar = '=';
11615 if (first.twoMachinesColor[0] == 'w') {
11618 second.matchWins++;
11623 if (first.twoMachinesColor[0] == 'b') {
11626 second.matchWins++;
11629 case GameUnfinished:
11635 if(exiting) resChar = ' '; // quit while waiting for round sync: unreserve already reserved game
11636 if(appData.tourneyFile[0]){ // [HGM] we are in a tourney; update tourney file with game result
11637 if(appData.afterGame && appData.afterGame[0]) RunCommand(appData.afterGame);
11638 ReserveGame(nextGame, resChar); // sets nextGame
11639 if(nextGame > appData.matchGames) appData.tourneyFile[0] = 0, ranking = TourneyStandings(3); // tourney is done
11640 else ranking = strdup("busy"); //suppress popup when aborted but not finished
11641 } else roundNr = nextGame = matchGame + 1; // normal match, just increment; round equals matchGame
11643 if (nextGame <= appData.matchGames && !abortMatch) {
11644 gameMode = nextGameMode;
11645 matchGame = nextGame; // this will be overruled in tourney mode!
11646 GetTimeMark(&pauseStart); // [HGM] matchpause: stipulate a pause
11647 ScheduleDelayedEvent(NextMatchGame, 10); // but start game immediately (as it will wait out the pause itself)
11648 endingGame = 0; /* [HGM] crash */
11651 gameMode = nextGameMode;
11652 snprintf(buf, MSG_SIZ, _("Match %s vs. %s: final score %d-%d-%d"),
11653 first.tidy, second.tidy,
11654 first.matchWins, second.matchWins,
11655 appData.matchGames - (first.matchWins + second.matchWins));
11656 if(!appData.tourneyFile[0]) matchGame++, DisplayTwoMachinesTitle(); // [HGM] update result in window title
11657 if(ranking && strcmp(ranking, "busy") && appData.afterTourney && appData.afterTourney[0]) RunCommand(appData.afterTourney);
11658 popupRequested++; // [HGM] crash: postpone to after resetting endingGame
11659 if (appData.firstPlaysBlack) { // [HGM] match: back to original for next match
11660 first.twoMachinesColor = "black\n";
11661 second.twoMachinesColor = "white\n";
11663 first.twoMachinesColor = "white\n";
11664 second.twoMachinesColor = "black\n";
11668 if ((gameMode == AnalyzeMode || gameMode == AnalyzeFile) &&
11669 !(nextGameMode == AnalyzeMode || nextGameMode == AnalyzeFile))
11671 gameMode = nextGameMode;
11673 endingGame = 0; /* [HGM] crash */
11674 if(popupRequested) { // [HGM] crash: this calls GameEnds recursively through ExitEvent! Make it a harmless tail recursion.
11675 if(matchMode == TRUE) { // match through command line: exit with or without popup
11677 ToNrEvent(forwardMostMove);
11678 if(strcmp(ranking, "busy")) DisplayFatalError(ranking, 0, 0);
11680 } else DisplayFatalError(buf, 0, 0);
11681 } else { // match through menu; just stop, with or without popup
11682 matchMode = FALSE; appData.matchGames = matchGame = roundNr = 0;
11685 if(strcmp(ranking, "busy")) DisplayNote(ranking);
11686 } else DisplayNote(buf);
11688 if(ranking) free(ranking);
11692 /* Assumes program was just initialized (initString sent).
11693 Leaves program in force mode. */
11695 FeedMovesToProgram (ChessProgramState *cps, int upto)
11699 if (appData.debugMode)
11700 fprintf(debugFP, "Feeding %smoves %d through %d to %s chess program\n",
11701 startedFromSetupPosition ? "position and " : "",
11702 backwardMostMove, upto, cps->which);
11703 if(currentlyInitializedVariant != gameInfo.variant) {
11705 // [HGM] variantswitch: make engine aware of new variant
11706 if(!SupportedVariant(cps->variants, gameInfo.variant, gameInfo.boardWidth,
11707 gameInfo.boardHeight, gameInfo.holdingsSize, cps->protocolVersion, ""))
11708 return; // [HGM] refrain from feeding moves altogether if variant is unsupported!
11709 snprintf(buf, MSG_SIZ, "variant %s\n", VariantName(gameInfo.variant));
11710 SendToProgram(buf, cps);
11711 currentlyInitializedVariant = gameInfo.variant;
11713 SendToProgram("force\n", cps);
11714 if (startedFromSetupPosition) {
11715 SendBoard(cps, backwardMostMove);
11716 if (appData.debugMode) {
11717 fprintf(debugFP, "feedMoves\n");
11720 for (i = backwardMostMove; i < upto; i++) {
11721 SendMoveToProgram(i, cps);
11727 ResurrectChessProgram ()
11729 /* The chess program may have exited.
11730 If so, restart it and feed it all the moves made so far. */
11731 static int doInit = 0;
11733 if (appData.noChessProgram) return 1;
11735 if(matchMode /*&& appData.tourneyFile[0]*/) { // [HGM] tourney: make sure we get features after engine replacement. (Should we always do this?)
11736 if(WaitForEngine(&first, TwoMachinesEventIfReady)) { doInit = 1; return 0; } // request to do init on next visit, because we started engine
11737 if(!doInit) return 1; // this replaces testing first.pr != NoProc, which is true when we get here, but first time no reason to abort
11738 doInit = 0; // we fell through (first time after starting the engine); make sure it doesn't happen again
11740 if (first.pr != NoProc) return 1;
11741 StartChessProgram(&first);
11743 InitChessProgram(&first, FALSE);
11744 FeedMovesToProgram(&first, currentMove);
11746 if (!first.sendTime) {
11747 /* can't tell gnuchess what its clock should read,
11748 so we bow to its notion. */
11750 timeRemaining[0][currentMove] = whiteTimeRemaining;
11751 timeRemaining[1][currentMove] = blackTimeRemaining;
11754 if ((gameMode == AnalyzeMode || gameMode == AnalyzeFile ||
11755 appData.icsEngineAnalyze) && first.analysisSupport) {
11756 SendToProgram("analyze\n", &first);
11757 first.analyzing = TRUE;
11763 * Button procedures
11766 Reset (int redraw, int init)
11770 if (appData.debugMode) {
11771 fprintf(debugFP, "Reset(%d, %d) from gameMode %d\n",
11772 redraw, init, gameMode);
11774 pieceDefs = FALSE; // [HGM] gen: reset engine-defined piece moves
11775 for(i=0; i<EmptySquare; i++) { FREE(pieceDesc[i]); pieceDesc[i] = NULL; }
11776 CleanupTail(); // [HGM] vari: delete any stored variations
11777 CommentPopDown(); // [HGM] make sure no comments to the previous game keep hanging on
11778 pausing = pauseExamInvalid = FALSE;
11779 startedFromSetupPosition = blackPlaysFirst = FALSE;
11781 whiteFlag = blackFlag = FALSE;
11782 userOfferedDraw = FALSE;
11783 hintRequested = bookRequested = FALSE;
11784 first.maybeThinking = FALSE;
11785 second.maybeThinking = FALSE;
11786 first.bookSuspend = FALSE; // [HGM] book
11787 second.bookSuspend = FALSE;
11788 thinkOutput[0] = NULLCHAR;
11789 lastHint[0] = NULLCHAR;
11790 ClearGameInfo(&gameInfo);
11791 gameInfo.variant = StringToVariant(appData.variant);
11792 if(gameInfo.variant == VariantNormal && strcmp(appData.variant, "normal")) gameInfo.variant = VariantUnknown;
11793 ics_user_moved = ics_clock_paused = FALSE;
11794 ics_getting_history = H_FALSE;
11796 white_holding[0] = black_holding[0] = NULLCHAR;
11797 ClearProgramStats();
11798 opponentKibitzes = FALSE; // [HGM] kibitz: do not reserve space in engine-output window in zippy mode
11802 flipView = appData.flipView;
11803 ClearPremoveHighlights();
11804 gotPremove = FALSE;
11805 alarmSounded = FALSE;
11806 killX = killY = -1; // [HGM] lion
11808 GameEnds(EndOfFile, NULL, GE_PLAYER);
11809 if(appData.serverMovesName != NULL) {
11810 /* [HGM] prepare to make moves file for broadcasting */
11811 clock_t t = clock();
11812 if(serverMoves != NULL) fclose(serverMoves);
11813 serverMoves = fopen(appData.serverMovesName, "r");
11814 if(serverMoves != NULL) {
11815 fclose(serverMoves);
11816 /* delay 15 sec before overwriting, so all clients can see end */
11817 while(clock()-t < appData.serverPause*CLOCKS_PER_SEC);
11819 serverMoves = fopen(appData.serverMovesName, "w");
11823 gameMode = BeginningOfGame;
11825 if(appData.icsActive) gameInfo.variant = VariantNormal;
11826 currentMove = forwardMostMove = backwardMostMove = 0;
11827 MarkTargetSquares(1);
11828 InitPosition(redraw);
11829 for (i = 0; i < MAX_MOVES; i++) {
11830 if (commentList[i] != NULL) {
11831 free(commentList[i]);
11832 commentList[i] = NULL;
11836 timeRemaining[0][0] = whiteTimeRemaining;
11837 timeRemaining[1][0] = blackTimeRemaining;
11839 if (first.pr == NoProc) {
11840 StartChessProgram(&first);
11843 InitChessProgram(&first, startedFromSetupPosition);
11846 DisplayMessage("", "");
11847 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
11848 lastSavedGame = 0; // [HGM] save: make sure next game counts as unsaved
11849 ClearMap(); // [HGM] exclude: invalidate map
11853 AutoPlayGameLoop ()
11856 if (!AutoPlayOneMove())
11858 if (matchMode || appData.timeDelay == 0)
11860 if (appData.timeDelay < 0)
11862 StartLoadGameTimer((long)(1000.0f * appData.timeDelay));
11870 ReloadGame(1); // next game
11876 int fromX, fromY, toX, toY;
11878 if (appData.debugMode) {
11879 fprintf(debugFP, "AutoPlayOneMove(): current %d\n", currentMove);
11882 if (gameMode != PlayFromGameFile && gameMode != AnalyzeFile)
11885 if (gameMode == AnalyzeFile && currentMove > backwardMostMove && programStats.depth) {
11886 pvInfoList[currentMove].depth = programStats.depth;
11887 pvInfoList[currentMove].score = programStats.score;
11888 pvInfoList[currentMove].time = 0;
11889 if(currentMove < forwardMostMove) AppendComment(currentMove+1, lastPV[0], 2);
11890 else { // append analysis of final position as comment
11892 snprintf(buf, MSG_SIZ, "{final score %+4.2f/%d}", programStats.score/100., programStats.depth);
11893 AppendComment(currentMove, buf, 3); // the 3 prevents stripping of the score/depth!
11895 programStats.depth = 0;
11898 if (currentMove >= forwardMostMove) {
11899 if(gameMode == AnalyzeFile) {
11900 if(appData.loadGameIndex == -1) {
11901 GameEnds(gameInfo.result, gameInfo.resultDetails ? gameInfo.resultDetails : "", GE_FILE);
11902 ScheduleDelayedEvent(AnalyzeNextGame, 10);
11904 ExitAnalyzeMode(); SendToProgram("force\n", &first);
11907 // gameMode = EndOfGame;
11908 // ModeHighlight();
11910 /* [AS] Clear current move marker at the end of a game */
11911 /* HistorySet(parseList, backwardMostMove, forwardMostMove, -1); */
11916 toX = moveList[currentMove][2] - AAA;
11917 toY = moveList[currentMove][3] - ONE;
11919 if (moveList[currentMove][1] == '@') {
11920 if (appData.highlightLastMove) {
11921 SetHighlights(-1, -1, toX, toY);
11924 int viaX = moveList[currentMove][5] - AAA;
11925 int viaY = moveList[currentMove][6] - ONE;
11926 fromX = moveList[currentMove][0] - AAA;
11927 fromY = moveList[currentMove][1] - ONE;
11929 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove); /* [AS] */
11931 if(moveList[currentMove][4] == ';') { // multi-leg
11932 ChessSquare piece = boards[currentMove][viaY][viaX];
11933 AnimateMove(boards[currentMove], fromX, fromY, viaX, viaY);
11934 boards[currentMove][viaY][viaX] = boards[currentMove][fromY][fromX];
11935 AnimateMove(boards[currentMove], fromX=viaX, fromY=viaY, toX, toY);
11936 boards[currentMove][viaY][viaX] = piece;
11938 AnimateMove(boards[currentMove], fromX, fromY, toX, toY);
11940 if (appData.highlightLastMove) {
11941 SetHighlights(fromX, fromY, toX, toY);
11944 DisplayMove(currentMove);
11945 SendMoveToProgram(currentMove++, &first);
11946 DisplayBothClocks();
11947 DrawPosition(FALSE, boards[currentMove]);
11948 // [HGM] PV info: always display, routine tests if empty
11949 DisplayComment(currentMove - 1, commentList[currentMove]);
11955 LoadGameOneMove (ChessMove readAhead)
11957 int fromX = 0, fromY = 0, toX = 0, toY = 0, done;
11958 char promoChar = NULLCHAR;
11959 ChessMove moveType;
11960 char move[MSG_SIZ];
11963 if (gameMode != PlayFromGameFile && gameMode != AnalyzeFile &&
11964 gameMode != AnalyzeMode && gameMode != Training) {
11969 yyboardindex = forwardMostMove;
11970 if (readAhead != EndOfFile) {
11971 moveType = readAhead;
11973 if (gameFileFP == NULL)
11975 moveType = (ChessMove) Myylex();
11979 switch (moveType) {
11981 if (appData.debugMode)
11982 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
11985 /* append the comment but don't display it */
11986 AppendComment(currentMove, p, FALSE);
11989 case WhiteCapturesEnPassant:
11990 case BlackCapturesEnPassant:
11991 case WhitePromotion:
11992 case BlackPromotion:
11993 case WhiteNonPromotion:
11994 case BlackNonPromotion:
11997 case WhiteKingSideCastle:
11998 case WhiteQueenSideCastle:
11999 case BlackKingSideCastle:
12000 case BlackQueenSideCastle:
12001 case WhiteKingSideCastleWild:
12002 case WhiteQueenSideCastleWild:
12003 case BlackKingSideCastleWild:
12004 case BlackQueenSideCastleWild:
12006 case WhiteHSideCastleFR:
12007 case WhiteASideCastleFR:
12008 case BlackHSideCastleFR:
12009 case BlackASideCastleFR:
12011 if (appData.debugMode)
12012 fprintf(debugFP, "Parsed %s into %s\n", yy_text, currentMoveString);
12013 fromX = currentMoveString[0] - AAA;
12014 fromY = currentMoveString[1] - ONE;
12015 toX = currentMoveString[2] - AAA;
12016 toY = currentMoveString[3] - ONE;
12017 promoChar = currentMoveString[4];
12018 if(promoChar == ';') promoChar = NULLCHAR;
12023 if (appData.debugMode)
12024 fprintf(debugFP, "Parsed %s into %s\n", yy_text, currentMoveString);
12025 fromX = moveType == WhiteDrop ?
12026 (int) CharToPiece(ToUpper(currentMoveString[0])) :
12027 (int) CharToPiece(ToLower(currentMoveString[0]));
12029 toX = currentMoveString[2] - AAA;
12030 toY = currentMoveString[3] - ONE;
12036 case GameUnfinished:
12037 if (appData.debugMode)
12038 fprintf(debugFP, "Parsed game end: %s\n", yy_text);
12039 p = strchr(yy_text, '{');
12040 if (p == NULL) p = strchr(yy_text, '(');
12043 if (p[0] == '0' || p[0] == '1' || p[0] == '*') p = "";
12045 q = strchr(p, *p == '{' ? '}' : ')');
12046 if (q != NULL) *q = NULLCHAR;
12049 while(q = strchr(p, '\n')) *q = ' '; // [HGM] crush linefeeds in result message
12050 GameEnds(moveType, p, GE_FILE);
12052 if (cmailMsgLoaded) {
12054 flipView = WhiteOnMove(currentMove);
12055 if (moveType == GameUnfinished) flipView = !flipView;
12056 if (appData.debugMode)
12057 fprintf(debugFP, "Setting flipView to %d\n", flipView) ;
12062 if (appData.debugMode)
12063 fprintf(debugFP, "Parser hit end of file\n");
12064 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
12070 if (WhiteOnMove(currentMove)) {
12071 GameEnds(BlackWins, "Black mates", GE_FILE);
12073 GameEnds(WhiteWins, "White mates", GE_FILE);
12077 GameEnds(GameIsDrawn, "Stalemate", GE_FILE);
12083 case MoveNumberOne:
12084 if (lastLoadGameStart == GNUChessGame) {
12085 /* GNUChessGames have numbers, but they aren't move numbers */
12086 if (appData.debugMode)
12087 fprintf(debugFP, "Parser ignoring: '%s' (%d)\n",
12088 yy_text, (int) moveType);
12089 return LoadGameOneMove(EndOfFile); /* tail recursion */
12091 /* else fall thru */
12096 /* Reached start of next game in file */
12097 if (appData.debugMode)
12098 fprintf(debugFP, "Parsed start of next game: %s\n", yy_text);
12099 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
12105 if (WhiteOnMove(currentMove)) {
12106 GameEnds(BlackWins, "Black mates", GE_FILE);
12108 GameEnds(WhiteWins, "White mates", GE_FILE);
12112 GameEnds(GameIsDrawn, "Stalemate", GE_FILE);
12118 case PositionDiagram: /* should not happen; ignore */
12119 case ElapsedTime: /* ignore */
12120 case NAG: /* ignore */
12121 if (appData.debugMode)
12122 fprintf(debugFP, "Parser ignoring: '%s' (%d)\n",
12123 yy_text, (int) moveType);
12124 return LoadGameOneMove(EndOfFile); /* tail recursion */
12127 if (appData.testLegality) {
12128 if (appData.debugMode)
12129 fprintf(debugFP, "Parsed IllegalMove: %s\n", yy_text);
12130 snprintf(move, MSG_SIZ, _("Illegal move: %d.%s%s"),
12131 (forwardMostMove / 2) + 1,
12132 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
12133 DisplayError(move, 0);
12136 if (appData.debugMode)
12137 fprintf(debugFP, "Parsed %s into IllegalMove %s\n",
12138 yy_text, currentMoveString);
12139 if(currentMoveString[1] == '@') {
12140 fromX = CharToPiece(WhiteOnMove(currentMove) ? ToUpper(currentMoveString[0]) : ToLower(currentMoveString[0]));
12143 fromX = currentMoveString[0] - AAA;
12144 fromY = currentMoveString[1] - ONE;
12146 toX = currentMoveString[2] - AAA;
12147 toY = currentMoveString[3] - ONE;
12148 promoChar = currentMoveString[4];
12152 case AmbiguousMove:
12153 if (appData.debugMode)
12154 fprintf(debugFP, "Parsed AmbiguousMove: %s\n", yy_text);
12155 snprintf(move, MSG_SIZ, _("Ambiguous move: %d.%s%s"),
12156 (forwardMostMove / 2) + 1,
12157 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
12158 DisplayError(move, 0);
12163 case ImpossibleMove:
12164 if (appData.debugMode)
12165 fprintf(debugFP, "Parsed ImpossibleMove (type = %d): %s\n", moveType, yy_text);
12166 snprintf(move, MSG_SIZ, _("Illegal move: %d.%s%s"),
12167 (forwardMostMove / 2) + 1,
12168 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
12169 DisplayError(move, 0);
12175 if (appData.matchMode || (appData.timeDelay == 0 && !pausing)) {
12176 DrawPosition(FALSE, boards[currentMove]);
12177 DisplayBothClocks();
12178 if (!appData.matchMode) // [HGM] PV info: routine tests if empty
12179 DisplayComment(currentMove - 1, commentList[currentMove]);
12181 (void) StopLoadGameTimer();
12183 cmailOldMove = forwardMostMove;
12186 /* currentMoveString is set as a side-effect of yylex */
12188 thinkOutput[0] = NULLCHAR;
12189 MakeMove(fromX, fromY, toX, toY, promoChar);
12190 killX = killY = -1; // [HGM] lion: used up
12191 currentMove = forwardMostMove;
12196 /* Load the nth game from the given file */
12198 LoadGameFromFile (char *filename, int n, char *title, int useList)
12203 if (strcmp(filename, "-") == 0) {
12207 f = fopen(filename, "rb");
12209 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
12210 DisplayError(buf, errno);
12214 if (fseek(f, 0, 0) == -1) {
12215 /* f is not seekable; probably a pipe */
12218 if (useList && n == 0) {
12219 int error = GameListBuild(f);
12221 DisplayError(_("Cannot build game list"), error);
12222 } else if (!ListEmpty(&gameList) &&
12223 ((ListGame *) gameList.tailPred)->number > 1) {
12224 GameListPopUp(f, title);
12231 return LoadGame(f, n, title, FALSE);
12236 MakeRegisteredMove ()
12238 int fromX, fromY, toX, toY;
12240 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
12241 switch (cmailMoveType[lastLoadGameNumber - 1]) {
12244 if (appData.debugMode)
12245 fprintf(debugFP, "Restoring %s for game %d\n",
12246 cmailMove[lastLoadGameNumber - 1], lastLoadGameNumber);
12248 thinkOutput[0] = NULLCHAR;
12249 safeStrCpy(moveList[currentMove], cmailMove[lastLoadGameNumber - 1], sizeof(moveList[currentMove])/sizeof(moveList[currentMove][0]));
12250 fromX = cmailMove[lastLoadGameNumber - 1][0] - AAA;
12251 fromY = cmailMove[lastLoadGameNumber - 1][1] - ONE;
12252 toX = cmailMove[lastLoadGameNumber - 1][2] - AAA;
12253 toY = cmailMove[lastLoadGameNumber - 1][3] - ONE;
12254 promoChar = cmailMove[lastLoadGameNumber - 1][4];
12255 MakeMove(fromX, fromY, toX, toY, promoChar);
12256 ShowMove(fromX, fromY, toX, toY);
12258 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
12265 if (WhiteOnMove(currentMove)) {
12266 GameEnds(BlackWins, "Black mates", GE_PLAYER);
12268 GameEnds(WhiteWins, "White mates", GE_PLAYER);
12273 GameEnds(GameIsDrawn, "Stalemate", GE_PLAYER);
12280 if (WhiteOnMove(currentMove)) {
12281 GameEnds(BlackWins, "White resigns", GE_PLAYER);
12283 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
12288 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
12299 /* Wrapper around LoadGame for use when a Cmail message is loaded */
12301 CmailLoadGame (FILE *f, int gameNumber, char *title, int useList)
12305 if (gameNumber > nCmailGames) {
12306 DisplayError(_("No more games in this message"), 0);
12309 if (f == lastLoadGameFP) {
12310 int offset = gameNumber - lastLoadGameNumber;
12312 cmailMsg[0] = NULLCHAR;
12313 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
12314 cmailMoveRegistered[lastLoadGameNumber - 1] = FALSE;
12315 nCmailMovesRegistered--;
12317 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
12318 if (cmailResult[lastLoadGameNumber - 1] == CMAIL_NEW_RESULT) {
12319 cmailResult[lastLoadGameNumber - 1] = CMAIL_NOT_RESULT;
12322 if (! RegisterMove()) return FALSE;
12326 retVal = LoadGame(f, gameNumber, title, useList);
12328 /* Make move registered during previous look at this game, if any */
12329 MakeRegisteredMove();
12331 if (cmailCommentList[lastLoadGameNumber - 1] != NULL) {
12332 commentList[currentMove]
12333 = StrSave(cmailCommentList[lastLoadGameNumber - 1]);
12334 DisplayComment(currentMove - 1, commentList[currentMove]);
12340 /* Support for LoadNextGame, LoadPreviousGame, ReloadSameGame */
12342 ReloadGame (int offset)
12344 int gameNumber = lastLoadGameNumber + offset;
12345 if (lastLoadGameFP == NULL) {
12346 DisplayError(_("No game has been loaded yet"), 0);
12349 if (gameNumber <= 0) {
12350 DisplayError(_("Can't back up any further"), 0);
12353 if (cmailMsgLoaded) {
12354 return CmailLoadGame(lastLoadGameFP, gameNumber,
12355 lastLoadGameTitle, lastLoadGameUseList);
12357 return LoadGame(lastLoadGameFP, gameNumber,
12358 lastLoadGameTitle, lastLoadGameUseList);
12362 int keys[EmptySquare+1];
12365 PositionMatches (Board b1, Board b2)
12368 switch(appData.searchMode) {
12369 case 1: return CompareWithRights(b1, b2);
12371 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
12372 if(b2[r][f] != EmptySquare && b1[r][f] != b2[r][f]) return FALSE;
12376 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
12377 if((b2[r][f] == WhitePawn || b2[r][f] == BlackPawn) && b1[r][f] != b2[r][f]) return FALSE;
12378 sum += keys[b1[r][f]] - keys[b2[r][f]];
12382 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
12383 sum += keys[b1[r][f]] - keys[b2[r][f]];
12395 int pieceList[256], quickBoard[256];
12396 ChessSquare pieceType[256] = { EmptySquare };
12397 Board soughtBoard, reverseBoard, flipBoard, rotateBoard;
12398 int counts[EmptySquare], minSought[EmptySquare], minReverse[EmptySquare], maxSought[EmptySquare], maxReverse[EmptySquare];
12399 int soughtTotal, turn;
12400 Boolean epOK, flipSearch;
12403 unsigned char piece, to;
12406 #define DSIZE (250000)
12408 Move initialSpace[DSIZE+1000]; // gamble on that game will not be more than 500 moves
12409 Move *moveDatabase = initialSpace;
12410 unsigned int movePtr, dataSize = DSIZE;
12413 MakePieceList (Board board, int *counts)
12415 int r, f, n=Q_PROMO, total=0;
12416 for(r=0;r<EmptySquare;r++) counts[r] = 0; // piece-type counts
12417 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
12418 int sq = f + (r<<4);
12419 if(board[r][f] == EmptySquare) quickBoard[sq] = 0; else {
12420 quickBoard[sq] = ++n;
12422 pieceType[n] = board[r][f];
12423 counts[board[r][f]]++;
12424 if(board[r][f] == WhiteKing) pieceList[1] = n; else
12425 if(board[r][f] == BlackKing) pieceList[2] = n; // remember which are Kings, for castling
12429 epOK = gameInfo.variant != VariantXiangqi && gameInfo.variant != VariantBerolina;
12434 PackMove (int fromX, int fromY, int toX, int toY, ChessSquare promoPiece)
12436 int sq = fromX + (fromY<<4);
12437 int piece = quickBoard[sq], rook;
12438 quickBoard[sq] = 0;
12439 moveDatabase[movePtr].to = pieceList[piece] = sq = toX + (toY<<4);
12440 if(piece == pieceList[1] && fromY == toY) {
12441 if((toX > fromX+1 || toX < fromX-1) && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1) {
12442 int from = toX>fromX ? BOARD_RGHT-1 : BOARD_LEFT;
12443 moveDatabase[movePtr++].piece = Q_WCASTL;
12444 quickBoard[sq] = piece;
12445 piece = quickBoard[from]; quickBoard[from] = 0;
12446 moveDatabase[movePtr].to = pieceList[piece] = sq = toX>fromX ? sq-1 : sq+1;
12447 } else if((rook = quickBoard[sq]) && pieceType[rook] == WhiteRook) { // FRC castling
12448 quickBoard[sq] = 0; // remove Rook
12449 moveDatabase[movePtr].to = sq = (toX>fromX ? BOARD_RGHT-2 : BOARD_LEFT+2); // King to-square
12450 moveDatabase[movePtr++].piece = Q_WCASTL;
12451 quickBoard[sq] = pieceList[1]; // put King
12453 moveDatabase[movePtr].to = pieceList[rook] = sq = toX>fromX ? sq-1 : sq+1;
12456 if(piece == pieceList[2] && fromY == toY) {
12457 if((toX > fromX+1 || toX < fromX-1) && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1) {
12458 int from = (toX>fromX ? BOARD_RGHT-1 : BOARD_LEFT) + (BOARD_HEIGHT-1 <<4);
12459 moveDatabase[movePtr++].piece = Q_BCASTL;
12460 quickBoard[sq] = piece;
12461 piece = quickBoard[from]; quickBoard[from] = 0;
12462 moveDatabase[movePtr].to = pieceList[piece] = sq = toX>fromX ? sq-1 : sq+1;
12463 } else if((rook = quickBoard[sq]) && pieceType[rook] == BlackRook) { // FRC castling
12464 quickBoard[sq] = 0; // remove Rook
12465 moveDatabase[movePtr].to = sq = (toX>fromX ? BOARD_RGHT-2 : BOARD_LEFT+2);
12466 moveDatabase[movePtr++].piece = Q_BCASTL;
12467 quickBoard[sq] = pieceList[2]; // put King
12469 moveDatabase[movePtr].to = pieceList[rook] = sq = toX>fromX ? sq-1 : sq+1;
12472 if(epOK && (pieceType[piece] == WhitePawn || pieceType[piece] == BlackPawn) && fromX != toX && quickBoard[sq] == 0) {
12473 quickBoard[(fromY<<4)+toX] = 0;
12474 moveDatabase[movePtr].piece = Q_EP;
12475 moveDatabase[movePtr++].to = (fromY<<4)+toX;
12476 moveDatabase[movePtr].to = sq;
12478 if(promoPiece != pieceType[piece]) {
12479 moveDatabase[movePtr++].piece = Q_PROMO;
12480 moveDatabase[movePtr].to = pieceType[piece] = (int) promoPiece;
12482 moveDatabase[movePtr].piece = piece;
12483 quickBoard[sq] = piece;
12488 PackGame (Board board)
12490 Move *newSpace = NULL;
12491 moveDatabase[movePtr].piece = 0; // terminate previous game
12492 if(movePtr > dataSize) {
12493 if(appData.debugMode) fprintf(debugFP, "move-cache overflow, enlarge to %d MB\n", dataSize/128);
12494 dataSize *= 8; // increase size by factor 8 (512KB -> 4MB -> 32MB -> 256MB -> 2GB)
12495 if(dataSize) newSpace = (Move*) calloc(dataSize + 1000, sizeof(Move));
12498 Move *p = moveDatabase, *q = newSpace;
12499 for(i=0; i<movePtr; i++) *q++ = *p++; // copy to newly allocated space
12500 if(dataSize > 8*DSIZE) free(moveDatabase); // and free old space (if it was allocated)
12501 moveDatabase = newSpace;
12502 } else { // calloc failed, we must be out of memory. Too bad...
12503 dataSize = 0; // prevent calloc events for all subsequent games
12504 return 0; // and signal this one isn't cached
12508 MakePieceList(board, counts);
12513 QuickCompare (Board board, int *minCounts, int *maxCounts)
12514 { // compare according to search mode
12516 switch(appData.searchMode)
12518 case 1: // exact position match
12519 if(!(turn & board[EP_STATUS-1])) return FALSE; // wrong side to move
12520 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
12521 if(board[r][f] != pieceType[quickBoard[(r<<4)+f]]) return FALSE;
12524 case 2: // can have extra material on empty squares
12525 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
12526 if(board[r][f] == EmptySquare) continue;
12527 if(board[r][f] != pieceType[quickBoard[(r<<4)+f]]) return FALSE;
12530 case 3: // material with exact Pawn structure
12531 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
12532 if(board[r][f] != WhitePawn && board[r][f] != BlackPawn) continue;
12533 if(board[r][f] != pieceType[quickBoard[(r<<4)+f]]) return FALSE;
12534 } // fall through to material comparison
12535 case 4: // exact material
12536 for(r=0; r<EmptySquare; r++) if(counts[r] != maxCounts[r]) return FALSE;
12538 case 6: // material range with given imbalance
12539 for(r=0; r<BlackPawn; r++) if(counts[r] - minCounts[r] != counts[r+BlackPawn] - minCounts[r+BlackPawn]) return FALSE;
12540 // fall through to range comparison
12541 case 5: // material range
12542 for(r=0; r<EmptySquare; r++) if(counts[r] < minCounts[r] || counts[r] > maxCounts[r]) return FALSE;
12548 QuickScan (Board board, Move *move)
12549 { // reconstruct game,and compare all positions in it
12550 int cnt=0, stretch=0, found = -1, total = MakePieceList(board, counts);
12552 int piece = move->piece;
12553 int to = move->to, from = pieceList[piece];
12554 if(found < 0) { // if already found just scan to game end for final piece count
12555 if(QuickCompare(soughtBoard, minSought, maxSought) ||
12556 appData.ignoreColors && QuickCompare(reverseBoard, minReverse, maxReverse) ||
12557 flipSearch && (QuickCompare(flipBoard, minSought, maxSought) ||
12558 appData.ignoreColors && QuickCompare(rotateBoard, minReverse, maxReverse))
12560 static int lastCounts[EmptySquare+1];
12562 if(stretch) for(i=0; i<EmptySquare; i++) if(lastCounts[i] != counts[i]) { stretch = 0; break; } // reset if material changes
12563 if(stretch++ == 0) for(i=0; i<EmptySquare; i++) lastCounts[i] = counts[i]; // remember actual material
12564 } else stretch = 0;
12565 if(stretch && (appData.searchMode == 1 || stretch >= appData.stretch)) found = cnt + 1 - stretch;
12566 if(found >= 0 && !appData.minPieces) return found;
12568 if(piece <= Q_PROMO) { // special moves encoded by otherwise invalid piece numbers 1-4
12569 if(!piece) return (appData.minPieces && (total < appData.minPieces || total > appData.maxPieces) ? -1 : found);
12570 if(piece == Q_PROMO) { // promotion, encoded as (Q_PROMO, to) + (piece, promoType)
12571 piece = (++move)->piece;
12572 from = pieceList[piece];
12573 counts[pieceType[piece]]--;
12574 pieceType[piece] = (ChessSquare) move->to;
12575 counts[move->to]++;
12576 } else if(piece == Q_EP) { // e.p. capture, encoded as (Q_EP, ep-sqr) + (piece, to)
12577 counts[pieceType[quickBoard[to]]]--;
12578 quickBoard[to] = 0; total--;
12581 } else if(piece <= Q_BCASTL) { // castling, encoded as (Q_XCASTL, king-to) + (rook, rook-to)
12582 piece = pieceList[piece]; // first two elements of pieceList contain King numbers
12583 from = pieceList[piece]; // so this must be King
12584 quickBoard[from] = 0;
12585 pieceList[piece] = to;
12586 from = pieceList[(++move)->piece]; // for FRC this has to be done here
12587 quickBoard[from] = 0; // rook
12588 quickBoard[to] = piece;
12589 to = move->to; piece = move->piece;
12593 if(appData.searchMode > 2) counts[pieceType[quickBoard[to]]]--; // account capture
12594 if((total -= (quickBoard[to] != 0)) < soughtTotal && found < 0) return -1; // piece count dropped below what we search for
12595 quickBoard[from] = 0;
12597 quickBoard[to] = piece;
12598 pieceList[piece] = to;
12608 flipSearch = FALSE;
12609 CopyBoard(soughtBoard, boards[currentMove]);
12610 soughtTotal = MakePieceList(soughtBoard, maxSought);
12611 soughtBoard[EP_STATUS-1] = (currentMove & 1) + 1;
12612 if(currentMove == 0 && gameMode == EditPosition) soughtBoard[EP_STATUS-1] = blackPlaysFirst + 1; // (!)
12613 CopyBoard(reverseBoard, boards[currentMove]);
12614 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
12615 int piece = boards[currentMove][BOARD_HEIGHT-1-r][f];
12616 if(piece < BlackPawn) piece += BlackPawn; else if(piece < EmptySquare) piece -= BlackPawn; // color-flip
12617 reverseBoard[r][f] = piece;
12619 reverseBoard[EP_STATUS-1] = soughtBoard[EP_STATUS-1] ^ 3;
12620 for(r=0; r<6; r++) reverseBoard[CASTLING][r] = boards[currentMove][CASTLING][(r+3)%6];
12621 if(appData.findMirror && appData.searchMode <= 3 && (!nrCastlingRights
12622 || (boards[currentMove][CASTLING][2] == NoRights ||
12623 boards[currentMove][CASTLING][0] == NoRights && boards[currentMove][CASTLING][1] == NoRights )
12624 && (boards[currentMove][CASTLING][5] == NoRights ||
12625 boards[currentMove][CASTLING][3] == NoRights && boards[currentMove][CASTLING][4] == NoRights ) )
12628 CopyBoard(flipBoard, soughtBoard);
12629 CopyBoard(rotateBoard, reverseBoard);
12630 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
12631 flipBoard[r][f] = soughtBoard[r][BOARD_WIDTH-1-f];
12632 rotateBoard[r][f] = reverseBoard[r][BOARD_WIDTH-1-f];
12635 for(r=0; r<BlackPawn; r++) maxReverse[r] = maxSought[r+BlackPawn], maxReverse[r+BlackPawn] = maxSought[r];
12636 if(appData.searchMode >= 5) {
12637 for(r=BOARD_HEIGHT/2; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) soughtBoard[r][f] = EmptySquare;
12638 MakePieceList(soughtBoard, minSought);
12639 for(r=0; r<BlackPawn; r++) minReverse[r] = minSought[r+BlackPawn], minReverse[r+BlackPawn] = minSought[r];
12641 if(gameInfo.variant == VariantCrazyhouse || gameInfo.variant == VariantShogi || gameInfo.variant == VariantBughouse)
12642 soughtTotal = 0; // in drop games nr of pieces does not fall monotonously
12645 GameInfo dummyInfo;
12646 static int creatingBook;
12649 GameContainsPosition (FILE *f, ListGame *lg)
12651 int next, btm=0, plyNr=0, scratch=forwardMostMove+2&~1;
12652 int fromX, fromY, toX, toY;
12654 static int initDone=FALSE;
12656 // weed out games based on numerical tag comparison
12657 if(lg->gameInfo.variant != gameInfo.variant) return -1; // wrong variant
12658 if(appData.eloThreshold1 && (lg->gameInfo.whiteRating < appData.eloThreshold1 && lg->gameInfo.blackRating < appData.eloThreshold1)) return -1;
12659 if(appData.eloThreshold2 && (lg->gameInfo.whiteRating < appData.eloThreshold2 || lg->gameInfo.blackRating < appData.eloThreshold2)) return -1;
12660 if(appData.dateThreshold && (!lg->gameInfo.date || atoi(lg->gameInfo.date) < appData.dateThreshold)) return -1;
12662 for(next = WhitePawn; next<EmptySquare; next++) keys[next] = random()>>8 ^ random()<<6 ^random()<<20;
12665 if(lg->gameInfo.fen) ParseFEN(boards[scratch], &btm, lg->gameInfo.fen, FALSE);
12666 else CopyBoard(boards[scratch], initialPosition); // default start position
12669 if((next = QuickScan( boards[scratch], &moveDatabase[lg->moves] )) < 0) return -1; // quick scan rules out it is there
12670 if(appData.searchMode >= 4) return next; // for material searches, trust QuickScan.
12673 if(PositionMatches(boards[scratch], boards[currentMove])) return plyNr;
12674 fseek(f, lg->offset, 0);
12677 yyboardindex = scratch;
12678 quickFlag = plyNr+1;
12683 if(plyNr) return -1; // after we have seen moves, any tags will be start of next game
12689 if(plyNr) return -1; // after we have seen moves, this is for new game
12692 case AmbiguousMove: // we cannot reconstruct the game beyond these two
12693 case ImpossibleMove:
12694 case WhiteWins: // game ends here with these four
12697 case GameUnfinished:
12701 if(appData.testLegality) return -1;
12702 case WhiteCapturesEnPassant:
12703 case BlackCapturesEnPassant:
12704 case WhitePromotion:
12705 case BlackPromotion:
12706 case WhiteNonPromotion:
12707 case BlackNonPromotion:
12710 case WhiteKingSideCastle:
12711 case WhiteQueenSideCastle:
12712 case BlackKingSideCastle:
12713 case BlackQueenSideCastle:
12714 case WhiteKingSideCastleWild:
12715 case WhiteQueenSideCastleWild:
12716 case BlackKingSideCastleWild:
12717 case BlackQueenSideCastleWild:
12718 case WhiteHSideCastleFR:
12719 case WhiteASideCastleFR:
12720 case BlackHSideCastleFR:
12721 case BlackASideCastleFR:
12722 fromX = currentMoveString[0] - AAA;
12723 fromY = currentMoveString[1] - ONE;
12724 toX = currentMoveString[2] - AAA;
12725 toY = currentMoveString[3] - ONE;
12726 promoChar = currentMoveString[4];
12730 fromX = next == WhiteDrop ?
12731 (int) CharToPiece(ToUpper(currentMoveString[0])) :
12732 (int) CharToPiece(ToLower(currentMoveString[0]));
12734 toX = currentMoveString[2] - AAA;
12735 toY = currentMoveString[3] - ONE;
12739 // Move encountered; peform it. We need to shuttle between two boards, as even/odd index determines side to move
12741 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[scratch]);
12742 if(PositionMatches(boards[scratch], boards[currentMove])) return plyNr;
12743 if(appData.ignoreColors && PositionMatches(boards[scratch], reverseBoard)) return plyNr;
12744 if(appData.findMirror) {
12745 if(PositionMatches(boards[scratch], flipBoard)) return plyNr;
12746 if(appData.ignoreColors && PositionMatches(boards[scratch], rotateBoard)) return plyNr;
12751 /* Load the nth game from open file f */
12753 LoadGame (FILE *f, int gameNumber, char *title, int useList)
12757 int gn = gameNumber;
12758 ListGame *lg = NULL;
12759 int numPGNTags = 0;
12761 GameMode oldGameMode;
12762 VariantClass v, oldVariant = gameInfo.variant; /* [HGM] PGNvariant */
12763 char oldName[MSG_SIZ];
12765 safeStrCpy(oldName, engineVariant, MSG_SIZ); v = oldVariant;
12767 if (appData.debugMode)
12768 fprintf(debugFP, "LoadGame(): on entry, gameMode %d\n", gameMode);
12770 if (gameMode == Training )
12771 SetTrainingModeOff();
12773 oldGameMode = gameMode;
12774 if (gameMode != BeginningOfGame) {
12775 Reset(FALSE, TRUE);
12777 killX = killY = -1; // [HGM] lion: in case we did not Reset
12780 if (lastLoadGameFP != NULL && lastLoadGameFP != f) {
12781 fclose(lastLoadGameFP);
12785 lg = (ListGame *) ListElem(&gameList, gameNumber-1);
12788 fseek(f, lg->offset, 0);
12789 GameListHighlight(gameNumber);
12790 pos = lg->position;
12794 if(oldGameMode == AnalyzeFile && appData.loadGameIndex == -1)
12795 appData.loadGameIndex = 0; // [HGM] suppress error message if we reach file end after auto-stepping analysis
12797 DisplayError(_("Game number out of range"), 0);
12802 if (fseek(f, 0, 0) == -1) {
12803 if (f == lastLoadGameFP ?
12804 gameNumber == lastLoadGameNumber + 1 :
12808 DisplayError(_("Can't seek on game file"), 0);
12813 lastLoadGameFP = f;
12814 lastLoadGameNumber = gameNumber;
12815 safeStrCpy(lastLoadGameTitle, title, sizeof(lastLoadGameTitle)/sizeof(lastLoadGameTitle[0]));
12816 lastLoadGameUseList = useList;
12820 if (lg && lg->gameInfo.white && lg->gameInfo.black) {
12821 snprintf(buf, sizeof(buf), "%s %s %s", lg->gameInfo.white, _("vs."),
12822 lg->gameInfo.black);
12824 } else if (*title != NULLCHAR) {
12825 if (gameNumber > 1) {
12826 snprintf(buf, MSG_SIZ, "%s %d", title, gameNumber);
12829 DisplayTitle(title);
12833 if (gameMode != AnalyzeFile && gameMode != AnalyzeMode) {
12834 gameMode = PlayFromGameFile;
12838 currentMove = forwardMostMove = backwardMostMove = 0;
12839 CopyBoard(boards[0], initialPosition);
12843 * Skip the first gn-1 games in the file.
12844 * Also skip over anything that precedes an identifiable
12845 * start of game marker, to avoid being confused by
12846 * garbage at the start of the file. Currently
12847 * recognized start of game markers are the move number "1",
12848 * the pattern "gnuchess .* game", the pattern
12849 * "^[#;%] [^ ]* game file", and a PGN tag block.
12850 * A game that starts with one of the latter two patterns
12851 * will also have a move number 1, possibly
12852 * following a position diagram.
12853 * 5-4-02: Let's try being more lenient and allowing a game to
12854 * start with an unnumbered move. Does that break anything?
12856 cm = lastLoadGameStart = EndOfFile;
12858 yyboardindex = forwardMostMove;
12859 cm = (ChessMove) Myylex();
12862 if (cmailMsgLoaded) {
12863 nCmailGames = CMAIL_MAX_GAMES - gn;
12866 DisplayError(_("Game not found in file"), 0);
12873 lastLoadGameStart = cm;
12876 case MoveNumberOne:
12877 switch (lastLoadGameStart) {
12882 case MoveNumberOne:
12884 gn--; /* count this game */
12885 lastLoadGameStart = cm;
12894 switch (lastLoadGameStart) {
12897 case MoveNumberOne:
12899 gn--; /* count this game */
12900 lastLoadGameStart = cm;
12903 lastLoadGameStart = cm; /* game counted already */
12911 yyboardindex = forwardMostMove;
12912 cm = (ChessMove) Myylex();
12913 } while (cm == PGNTag || cm == Comment);
12920 if (cmailMsgLoaded && (CMAIL_MAX_GAMES == lastLoadGameNumber)) {
12921 if ( cmailResult[CMAIL_MAX_GAMES - gn - 1]
12922 != CMAIL_OLD_RESULT) {
12924 cmailResult[ CMAIL_MAX_GAMES
12925 - gn - 1] = CMAIL_OLD_RESULT;
12932 /* Only a NormalMove can be at the start of a game
12933 * without a position diagram. */
12934 if (lastLoadGameStart == EndOfFile ) {
12936 lastLoadGameStart = MoveNumberOne;
12945 if (appData.debugMode)
12946 fprintf(debugFP, "Parsed game start '%s' (%d)\n", yy_text, (int) cm);
12948 if (cm == XBoardGame) {
12949 /* Skip any header junk before position diagram and/or move 1 */
12951 yyboardindex = forwardMostMove;
12952 cm = (ChessMove) Myylex();
12954 if (cm == EndOfFile ||
12955 cm == GNUChessGame || cm == XBoardGame) {
12956 /* Empty game; pretend end-of-file and handle later */
12961 if (cm == MoveNumberOne || cm == PositionDiagram ||
12962 cm == PGNTag || cm == Comment)
12965 } else if (cm == GNUChessGame) {
12966 if (gameInfo.event != NULL) {
12967 free(gameInfo.event);
12969 gameInfo.event = StrSave(yy_text);
12972 startedFromSetupPosition = FALSE;
12973 while (cm == PGNTag) {
12974 if (appData.debugMode)
12975 fprintf(debugFP, "Parsed PGNTag: %s\n", yy_text);
12976 err = ParsePGNTag(yy_text, &gameInfo);
12977 if (!err) numPGNTags++;
12979 /* [HGM] PGNvariant: automatically switch to variant given in PGN tag */
12980 if(gameInfo.variant != oldVariant && (gameInfo.variant != VariantNormal || gameInfo.variantName == NULL || *gameInfo.variantName == NULLCHAR)) {
12981 startedFromPositionFile = FALSE; /* [HGM] loadPos: variant switch likely makes position invalid */
12982 ResetFrontEnd(); // [HGM] might need other bitmaps. Cannot use Reset() because it clears gameInfo :-(
12983 InitPosition(TRUE);
12984 oldVariant = gameInfo.variant;
12985 if (appData.debugMode)
12986 fprintf(debugFP, "New variant %d\n", (int) oldVariant);
12990 if (gameInfo.fen != NULL) {
12991 Board initial_position;
12992 startedFromSetupPosition = TRUE;
12993 if (!ParseFEN(initial_position, &blackPlaysFirst, gameInfo.fen, TRUE)) {
12995 DisplayError(_("Bad FEN position in file"), 0);
12998 CopyBoard(boards[0], initial_position);
12999 if(*engineVariant) // [HGM] for now, assume FEN in engine-defined variant game is default initial position
13000 CopyBoard(initialPosition, initial_position);
13001 if (blackPlaysFirst) {
13002 currentMove = forwardMostMove = backwardMostMove = 1;
13003 CopyBoard(boards[1], initial_position);
13004 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
13005 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
13006 timeRemaining[0][1] = whiteTimeRemaining;
13007 timeRemaining[1][1] = blackTimeRemaining;
13008 if (commentList[0] != NULL) {
13009 commentList[1] = commentList[0];
13010 commentList[0] = NULL;
13013 currentMove = forwardMostMove = backwardMostMove = 0;
13015 /* [HGM] copy FEN attributes as well. Bugfix 4.3.14m and 4.3.15e: moved to after 'blackPlaysFirst' */
13017 initialRulePlies = FENrulePlies;
13018 for( i=0; i< nrCastlingRights; i++ )
13019 initialRights[i] = initial_position[CASTLING][i];
13021 yyboardindex = forwardMostMove;
13022 free(gameInfo.fen);
13023 gameInfo.fen = NULL;
13026 yyboardindex = forwardMostMove;
13027 cm = (ChessMove) Myylex();
13029 /* Handle comments interspersed among the tags */
13030 while (cm == Comment) {
13032 if (appData.debugMode)
13033 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
13035 AppendComment(currentMove, p, FALSE);
13036 yyboardindex = forwardMostMove;
13037 cm = (ChessMove) Myylex();
13041 /* don't rely on existence of Event tag since if game was
13042 * pasted from clipboard the Event tag may not exist
13044 if (numPGNTags > 0){
13046 if (gameInfo.variant == VariantNormal) {
13047 VariantClass v = StringToVariant(gameInfo.event);
13048 // [HGM] do not recognize variants from event tag that were introduced after supporting variant tag
13049 if(v < VariantShogi) gameInfo.variant = v;
13052 if( appData.autoDisplayTags ) {
13053 tags = PGNTags(&gameInfo);
13054 TagsPopUp(tags, CmailMsg());
13059 /* Make something up, but don't display it now */
13064 if (cm == PositionDiagram) {
13067 Board initial_position;
13069 if (appData.debugMode)
13070 fprintf(debugFP, "Parsed PositionDiagram: %s\n", yy_text);
13072 if (!startedFromSetupPosition) {
13074 for (i = BOARD_HEIGHT - 1; i >= 0; i--)
13075 for (j = BOARD_LEFT; j < BOARD_RGHT; p++)
13086 initial_position[i][j++] = CharToPiece(*p);
13089 while (*p == ' ' || *p == '\t' ||
13090 *p == '\n' || *p == '\r') p++;
13092 if (strncmp(p, "black", strlen("black"))==0)
13093 blackPlaysFirst = TRUE;
13095 blackPlaysFirst = FALSE;
13096 startedFromSetupPosition = TRUE;
13098 CopyBoard(boards[0], initial_position);
13099 if (blackPlaysFirst) {
13100 currentMove = forwardMostMove = backwardMostMove = 1;
13101 CopyBoard(boards[1], initial_position);
13102 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
13103 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
13104 timeRemaining[0][1] = whiteTimeRemaining;
13105 timeRemaining[1][1] = blackTimeRemaining;
13106 if (commentList[0] != NULL) {
13107 commentList[1] = commentList[0];
13108 commentList[0] = NULL;
13111 currentMove = forwardMostMove = backwardMostMove = 0;
13114 yyboardindex = forwardMostMove;
13115 cm = (ChessMove) Myylex();
13118 if(!creatingBook) {
13119 if (first.pr == NoProc) {
13120 StartChessProgram(&first);
13122 InitChessProgram(&first, FALSE);
13123 if(gameInfo.variant == VariantUnknown && *oldName) {
13124 safeStrCpy(engineVariant, oldName, MSG_SIZ);
13125 gameInfo.variant = v;
13127 SendToProgram("force\n", &first);
13128 if (startedFromSetupPosition) {
13129 SendBoard(&first, forwardMostMove);
13130 if (appData.debugMode) {
13131 fprintf(debugFP, "Load Game\n");
13133 DisplayBothClocks();
13137 /* [HGM] server: flag to write setup moves in broadcast file as one */
13138 loadFlag = appData.suppressLoadMoves;
13140 while (cm == Comment) {
13142 if (appData.debugMode)
13143 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
13145 AppendComment(currentMove, p, FALSE);
13146 yyboardindex = forwardMostMove;
13147 cm = (ChessMove) Myylex();
13150 if ((cm == EndOfFile && lastLoadGameStart != EndOfFile ) ||
13151 cm == WhiteWins || cm == BlackWins ||
13152 cm == GameIsDrawn || cm == GameUnfinished) {
13153 DisplayMessage("", _("No moves in game"));
13154 if (cmailMsgLoaded) {
13155 if (appData.debugMode)
13156 fprintf(debugFP, "Setting flipView to %d.\n", FALSE);
13160 DrawPosition(FALSE, boards[currentMove]);
13161 DisplayBothClocks();
13162 gameMode = EditGame;
13169 // [HGM] PV info: routine tests if comment empty
13170 if (!matchMode && (pausing || appData.timeDelay != 0)) {
13171 DisplayComment(currentMove - 1, commentList[currentMove]);
13173 if (!matchMode && appData.timeDelay != 0)
13174 DrawPosition(FALSE, boards[currentMove]);
13176 if (gameMode == AnalyzeFile || gameMode == AnalyzeMode) {
13177 programStats.ok_to_send = 1;
13180 /* if the first token after the PGN tags is a move
13181 * and not move number 1, retrieve it from the parser
13183 if (cm != MoveNumberOne)
13184 LoadGameOneMove(cm);
13186 /* load the remaining moves from the file */
13187 while (LoadGameOneMove(EndOfFile)) {
13188 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
13189 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
13192 /* rewind to the start of the game */
13193 currentMove = backwardMostMove;
13195 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
13197 if (oldGameMode == AnalyzeFile) {
13198 appData.loadGameIndex = -1; // [HGM] order auto-stepping through games
13199 AnalyzeFileEvent();
13201 if (oldGameMode == AnalyzeMode) {
13202 AnalyzeFileEvent();
13205 if(gameInfo.result == GameUnfinished && gameInfo.resultDetails && appData.clockMode) {
13206 long int w, b; // [HGM] adjourn: restore saved clock times
13207 char *p = strstr(gameInfo.resultDetails, "(Clocks:");
13208 if(p && sscanf(p+8, "%ld,%ld", &w, &b) == 2) {
13209 timeRemaining[0][forwardMostMove] = whiteTimeRemaining = 1000*w + 500;
13210 timeRemaining[1][forwardMostMove] = blackTimeRemaining = 1000*b + 500;
13214 if(creatingBook) return TRUE;
13215 if (!matchMode && pos > 0) {
13216 ToNrEvent(pos); // [HGM] no autoplay if selected on position
13218 if (matchMode || appData.timeDelay == 0) {
13220 } else if (appData.timeDelay > 0) {
13221 AutoPlayGameLoop();
13224 if (appData.debugMode)
13225 fprintf(debugFP, "LoadGame(): on exit, gameMode %d\n", gameMode);
13227 loadFlag = 0; /* [HGM] true game starts */
13231 /* Support for LoadNextPosition, LoadPreviousPosition, ReloadSamePosition */
13233 ReloadPosition (int offset)
13235 int positionNumber = lastLoadPositionNumber + offset;
13236 if (lastLoadPositionFP == NULL) {
13237 DisplayError(_("No position has been loaded yet"), 0);
13240 if (positionNumber <= 0) {
13241 DisplayError(_("Can't back up any further"), 0);
13244 return LoadPosition(lastLoadPositionFP, positionNumber,
13245 lastLoadPositionTitle);
13248 /* Load the nth position from the given file */
13250 LoadPositionFromFile (char *filename, int n, char *title)
13255 if (strcmp(filename, "-") == 0) {
13256 return LoadPosition(stdin, n, "stdin");
13258 f = fopen(filename, "rb");
13260 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
13261 DisplayError(buf, errno);
13264 return LoadPosition(f, n, title);
13269 /* Load the nth position from the given open file, and close it */
13271 LoadPosition (FILE *f, int positionNumber, char *title)
13273 char *p, line[MSG_SIZ];
13274 Board initial_position;
13275 int i, j, fenMode, pn;
13277 if (gameMode == Training )
13278 SetTrainingModeOff();
13280 if (gameMode != BeginningOfGame) {
13281 Reset(FALSE, TRUE);
13283 if (lastLoadPositionFP != NULL && lastLoadPositionFP != f) {
13284 fclose(lastLoadPositionFP);
13286 if (positionNumber == 0) positionNumber = 1;
13287 lastLoadPositionFP = f;
13288 lastLoadPositionNumber = positionNumber;
13289 safeStrCpy(lastLoadPositionTitle, title, sizeof(lastLoadPositionTitle)/sizeof(lastLoadPositionTitle[0]));
13290 if (first.pr == NoProc && !appData.noChessProgram) {
13291 StartChessProgram(&first);
13292 InitChessProgram(&first, FALSE);
13294 pn = positionNumber;
13295 if (positionNumber < 0) {
13296 /* Negative position number means to seek to that byte offset */
13297 if (fseek(f, -positionNumber, 0) == -1) {
13298 DisplayError(_("Can't seek on position file"), 0);
13303 if (fseek(f, 0, 0) == -1) {
13304 if (f == lastLoadPositionFP ?
13305 positionNumber == lastLoadPositionNumber + 1 :
13306 positionNumber == 1) {
13309 DisplayError(_("Can't seek on position file"), 0);
13314 /* See if this file is FEN or old-style xboard */
13315 if (fgets(line, MSG_SIZ, f) == NULL) {
13316 DisplayError(_("Position not found in file"), 0);
13319 // [HGM] FEN can begin with digit, any piece letter valid in this variant, or a + for Shogi promoted pieces (or * for blackout)
13320 fenMode = line[0] >= '0' && line[0] <= '9' || line[0] == '+' || line[0] == '*' || CharToPiece(line[0]) != EmptySquare;
13323 if (fenMode || line[0] == '#') pn--;
13325 /* skip positions before number pn */
13326 if (fgets(line, MSG_SIZ, f) == NULL) {
13328 DisplayError(_("Position not found in file"), 0);
13331 if (fenMode || line[0] == '#') pn--;
13337 if (!ParseFEN(initial_position, &blackPlaysFirst, line, TRUE)) {
13338 DisplayError(_("Bad FEN position in file"), 0);
13341 if((p = strstr(line, ";")) && (p = strstr(p+1, "bm "))) { // EPD with best move
13342 sscanf(p+3, "%s", bestMove);
13343 } else *bestMove = NULLCHAR;
13345 (void) fgets(line, MSG_SIZ, f);
13346 (void) fgets(line, MSG_SIZ, f);
13348 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
13349 (void) fgets(line, MSG_SIZ, f);
13350 for (p = line, j = BOARD_LEFT; j < BOARD_RGHT; p++) {
13353 initial_position[i][j++] = CharToPiece(*p);
13357 blackPlaysFirst = FALSE;
13359 (void) fgets(line, MSG_SIZ, f);
13360 if (strncmp(line, "black", strlen("black"))==0)
13361 blackPlaysFirst = TRUE;
13364 startedFromSetupPosition = TRUE;
13366 CopyBoard(boards[0], initial_position);
13367 if (blackPlaysFirst) {
13368 currentMove = forwardMostMove = backwardMostMove = 1;
13369 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
13370 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
13371 CopyBoard(boards[1], initial_position);
13372 DisplayMessage("", _("Black to play"));
13374 currentMove = forwardMostMove = backwardMostMove = 0;
13375 DisplayMessage("", _("White to play"));
13377 initialRulePlies = FENrulePlies; /* [HGM] copy FEN attributes as well */
13378 if(first.pr != NoProc) { // [HGM] in tourney-mode a position can be loaded before the chess engine is installed
13379 SendToProgram("force\n", &first);
13380 SendBoard(&first, forwardMostMove);
13382 if (appData.debugMode) {
13384 for(i=0;i<2;i++){for(j=0;j<6;j++)fprintf(debugFP, " %d", boards[i][CASTLING][j]);fprintf(debugFP,"\n");}
13385 for(j=0;j<6;j++)fprintf(debugFP, " %d", initialRights[j]);fprintf(debugFP,"\n");
13386 fprintf(debugFP, "Load Position\n");
13389 if (positionNumber > 1) {
13390 snprintf(line, MSG_SIZ, "%s %d", title, positionNumber);
13391 DisplayTitle(line);
13393 DisplayTitle(title);
13395 gameMode = EditGame;
13398 timeRemaining[0][1] = whiteTimeRemaining;
13399 timeRemaining[1][1] = blackTimeRemaining;
13400 DrawPosition(FALSE, boards[currentMove]);
13407 CopyPlayerNameIntoFileName (char **dest, char *src)
13409 while (*src != NULLCHAR && *src != ',') {
13414 *(*dest)++ = *src++;
13420 DefaultFileName (char *ext)
13422 static char def[MSG_SIZ];
13425 if (gameInfo.white != NULL && gameInfo.white[0] != '-') {
13427 CopyPlayerNameIntoFileName(&p, gameInfo.white);
13429 CopyPlayerNameIntoFileName(&p, gameInfo.black);
13431 safeStrCpy(p, ext, MSG_SIZ-2-strlen(gameInfo.white)-strlen(gameInfo.black));
13438 /* Save the current game to the given file */
13440 SaveGameToFile (char *filename, int append)
13444 int result, i, t,tot=0;
13446 if (strcmp(filename, "-") == 0) {
13447 return SaveGame(stdout, 0, NULL);
13449 for(i=0; i<10; i++) { // upto 10 tries
13450 f = fopen(filename, append ? "a" : "w");
13451 if(f && i) fprintf(f, "[Delay \"%d retries, %d msec\"]\n",i,tot);
13452 if(f || errno != 13) break;
13453 DoSleep(t = 5 + random()%11); // wait 5-15 msec
13457 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
13458 DisplayError(buf, errno);
13461 safeStrCpy(buf, lastMsg, MSG_SIZ);
13462 DisplayMessage(_("Waiting for access to save file"), "");
13463 flock(fileno(f), LOCK_EX); // [HGM] lock: lock file while we are writing
13464 DisplayMessage(_("Saving game"), "");
13465 if(lseek(fileno(f), 0, SEEK_END) == -1) DisplayError(_("Bad Seek"), errno); // better safe than sorry...
13466 result = SaveGame(f, 0, NULL);
13467 DisplayMessage(buf, "");
13474 SavePart (char *str)
13476 static char buf[MSG_SIZ];
13479 p = strchr(str, ' ');
13480 if (p == NULL) return str;
13481 strncpy(buf, str, p - str);
13482 buf[p - str] = NULLCHAR;
13486 #define PGN_MAX_LINE 75
13488 #define PGN_SIDE_WHITE 0
13489 #define PGN_SIDE_BLACK 1
13492 FindFirstMoveOutOfBook (int side)
13496 if( backwardMostMove == 0 && ! startedFromSetupPosition) {
13497 int index = backwardMostMove;
13498 int has_book_hit = 0;
13500 if( (index % 2) != side ) {
13504 while( index < forwardMostMove ) {
13505 /* Check to see if engine is in book */
13506 int depth = pvInfoList[index].depth;
13507 int score = pvInfoList[index].score;
13513 else if( score == 0 && depth == 63 ) {
13514 in_book = 1; /* Zappa */
13516 else if( score == 2 && depth == 99 ) {
13517 in_book = 1; /* Abrok */
13520 has_book_hit += in_book;
13536 GetOutOfBookInfo (char * buf)
13540 int offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
13542 oob[0] = FindFirstMoveOutOfBook( PGN_SIDE_WHITE );
13543 oob[1] = FindFirstMoveOutOfBook( PGN_SIDE_BLACK );
13547 if( oob[0] >= 0 || oob[1] >= 0 ) {
13548 for( i=0; i<2; i++ ) {
13552 if( i > 0 && oob[0] >= 0 ) {
13553 strcat( buf, " " );
13556 sprintf( buf+strlen(buf), "%d%s. ", (idx - offset)/2 + 1, idx & 1 ? ".." : "" );
13557 sprintf( buf+strlen(buf), "%s%.2f",
13558 pvInfoList[idx].score >= 0 ? "+" : "",
13559 pvInfoList[idx].score / 100.0 );
13565 /* Save game in PGN style */
13567 SaveGamePGN2 (FILE *f)
13569 int i, offset, linelen, newblock;
13572 int movelen, numlen, blank;
13573 char move_buffer[100]; /* [AS] Buffer for move+PV info */
13575 offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
13577 PrintPGNTags(f, &gameInfo);
13579 if(appData.numberTag && matchMode) fprintf(f, "[Number \"%d\"]\n", nextGame+1); // [HGM] number tag
13581 if (backwardMostMove > 0 || startedFromSetupPosition) {
13582 char *fen = PositionToFEN(backwardMostMove, NULL, 1);
13583 fprintf(f, "[FEN \"%s\"]\n[SetUp \"1\"]\n", fen);
13584 fprintf(f, "\n{--------------\n");
13585 PrintPosition(f, backwardMostMove);
13586 fprintf(f, "--------------}\n");
13590 /* [AS] Out of book annotation */
13591 if( appData.saveOutOfBookInfo ) {
13594 GetOutOfBookInfo( buf );
13596 if( buf[0] != '\0' ) {
13597 fprintf( f, "[%s \"%s\"]\n", PGN_OUT_OF_BOOK, buf );
13604 i = backwardMostMove;
13608 while (i < forwardMostMove) {
13609 /* Print comments preceding this move */
13610 if (commentList[i] != NULL) {
13611 if (linelen > 0) fprintf(f, "\n");
13612 fprintf(f, "%s", commentList[i]);
13617 /* Format move number */
13619 snprintf(numtext, sizeof(numtext)/sizeof(numtext[0]),"%d.", (i - offset)/2 + 1);
13622 snprintf(numtext, sizeof(numtext)/sizeof(numtext[0]), "%d...", (i - offset)/2 + 1);
13624 numtext[0] = NULLCHAR;
13626 numlen = strlen(numtext);
13629 /* Print move number */
13630 blank = linelen > 0 && numlen > 0;
13631 if (linelen + (blank ? 1 : 0) + numlen > PGN_MAX_LINE) {
13640 fprintf(f, "%s", numtext);
13644 safeStrCpy(move_buffer, SavePart(parseList[i]), sizeof(move_buffer)/sizeof(move_buffer[0])); // [HGM] pgn: print move via buffer, so it can be edited
13645 movelen = strlen(move_buffer); /* [HGM] pgn: line-break point before move */
13648 blank = linelen > 0 && movelen > 0;
13649 if (linelen + (blank ? 1 : 0) + movelen > PGN_MAX_LINE) {
13658 fprintf(f, "%s", move_buffer);
13659 linelen += movelen;
13661 /* [AS] Add PV info if present */
13662 if( i >= 0 && appData.saveExtendedInfoInPGN && pvInfoList[i].depth > 0 ) {
13663 /* [HGM] add time */
13664 char buf[MSG_SIZ]; int seconds;
13666 seconds = (pvInfoList[i].time+5)/10; // deci-seconds, rounded to nearest
13672 snprintf(buf, MSG_SIZ, " %3.1f%c", seconds/10., 0);
13675 seconds = (seconds + 4)/10; // round to full seconds
13677 snprintf(buf, MSG_SIZ, " %d%c", seconds, 0);
13679 snprintf(buf, MSG_SIZ, " %d:%02d%c", seconds/60, seconds%60, 0);
13682 snprintf( move_buffer, sizeof(move_buffer)/sizeof(move_buffer[0]),"{%s%.2f/%d%s}",
13683 pvInfoList[i].score >= 0 ? "+" : "",
13684 pvInfoList[i].score / 100.0,
13685 pvInfoList[i].depth,
13688 movelen = strlen(move_buffer); /* [HGM] pgn: line-break point after move */
13690 /* Print score/depth */
13691 blank = linelen > 0 && movelen > 0;
13692 if (linelen + (blank ? 1 : 0) + movelen > PGN_MAX_LINE) {
13701 fprintf(f, "%s", move_buffer);
13702 linelen += movelen;
13708 /* Start a new line */
13709 if (linelen > 0) fprintf(f, "\n");
13711 /* Print comments after last move */
13712 if (commentList[i] != NULL) {
13713 fprintf(f, "%s\n", commentList[i]);
13717 if (gameInfo.resultDetails != NULL &&
13718 gameInfo.resultDetails[0] != NULLCHAR) {
13719 char buf[MSG_SIZ], *p = gameInfo.resultDetails;
13720 if(gameInfo.result == GameUnfinished && appData.clockMode &&
13721 (gameMode == MachinePlaysWhite || gameMode == MachinePlaysBlack || gameMode == TwoMachinesPlay)) // [HGM] adjourn: save clock settings
13722 snprintf(buf, MSG_SIZ, "%s (Clocks: %ld, %ld)", p, whiteTimeRemaining/1000, blackTimeRemaining/1000), p = buf;
13723 fprintf(f, "{%s} %s\n\n", p, PGNResult(gameInfo.result));
13725 fprintf(f, "%s\n\n", PGNResult(gameInfo.result));
13729 /* Save game in PGN style and close the file */
13731 SaveGamePGN (FILE *f)
13735 lastSavedGame = GameCheckSum(); // [HGM] save: remember ID of last saved game to prevent double saving
13739 /* Save game in old style and close the file */
13741 SaveGameOldStyle (FILE *f)
13746 tm = time((time_t *) NULL);
13748 fprintf(f, "# %s game file -- %s", programName, ctime(&tm));
13751 if (backwardMostMove > 0 || startedFromSetupPosition) {
13752 fprintf(f, "\n[--------------\n");
13753 PrintPosition(f, backwardMostMove);
13754 fprintf(f, "--------------]\n");
13759 i = backwardMostMove;
13760 offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
13762 while (i < forwardMostMove) {
13763 if (commentList[i] != NULL) {
13764 fprintf(f, "[%s]\n", commentList[i]);
13767 if ((i % 2) == 1) {
13768 fprintf(f, "%d. ... %s\n", (i - offset)/2 + 1, parseList[i]);
13771 fprintf(f, "%d. %s ", (i - offset)/2 + 1, parseList[i]);
13773 if (commentList[i] != NULL) {
13777 if (i >= forwardMostMove) {
13781 fprintf(f, "%s\n", parseList[i]);
13786 if (commentList[i] != NULL) {
13787 fprintf(f, "[%s]\n", commentList[i]);
13790 /* This isn't really the old style, but it's close enough */
13791 if (gameInfo.resultDetails != NULL &&
13792 gameInfo.resultDetails[0] != NULLCHAR) {
13793 fprintf(f, "%s (%s)\n\n", PGNResult(gameInfo.result),
13794 gameInfo.resultDetails);
13796 fprintf(f, "%s\n\n", PGNResult(gameInfo.result));
13803 /* Save the current game to open file f and close the file */
13805 SaveGame (FILE *f, int dummy, char *dummy2)
13807 if (gameMode == EditPosition) EditPositionDone(TRUE);
13808 lastSavedGame = GameCheckSum(); // [HGM] save: remember ID of last saved game to prevent double saving
13809 if (appData.oldSaveStyle)
13810 return SaveGameOldStyle(f);
13812 return SaveGamePGN(f);
13815 /* Save the current position to the given file */
13817 SavePositionToFile (char *filename)
13822 if (strcmp(filename, "-") == 0) {
13823 return SavePosition(stdout, 0, NULL);
13825 f = fopen(filename, "a");
13827 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
13828 DisplayError(buf, errno);
13831 safeStrCpy(buf, lastMsg, MSG_SIZ);
13832 DisplayMessage(_("Waiting for access to save file"), "");
13833 flock(fileno(f), LOCK_EX); // [HGM] lock
13834 DisplayMessage(_("Saving position"), "");
13835 lseek(fileno(f), 0, SEEK_END); // better safe than sorry...
13836 SavePosition(f, 0, NULL);
13837 DisplayMessage(buf, "");
13843 /* Save the current position to the given open file and close the file */
13845 SavePosition (FILE *f, int dummy, char *dummy2)
13850 if (gameMode == EditPosition) EditPositionDone(TRUE);
13851 if (appData.oldSaveStyle) {
13852 tm = time((time_t *) NULL);
13854 fprintf(f, "# %s position file -- %s", programName, ctime(&tm));
13856 fprintf(f, "[--------------\n");
13857 PrintPosition(f, currentMove);
13858 fprintf(f, "--------------]\n");
13860 fen = PositionToFEN(currentMove, NULL, 1);
13861 fprintf(f, "%s\n", fen);
13869 ReloadCmailMsgEvent (int unregister)
13872 static char *inFilename = NULL;
13873 static char *outFilename;
13875 struct stat inbuf, outbuf;
13878 /* Any registered moves are unregistered if unregister is set, */
13879 /* i.e. invoked by the signal handler */
13881 for (i = 0; i < CMAIL_MAX_GAMES; i ++) {
13882 cmailMoveRegistered[i] = FALSE;
13883 if (cmailCommentList[i] != NULL) {
13884 free(cmailCommentList[i]);
13885 cmailCommentList[i] = NULL;
13888 nCmailMovesRegistered = 0;
13891 for (i = 0; i < CMAIL_MAX_GAMES; i ++) {
13892 cmailResult[i] = CMAIL_NOT_RESULT;
13896 if (inFilename == NULL) {
13897 /* Because the filenames are static they only get malloced once */
13898 /* and they never get freed */
13899 inFilename = (char *) malloc(strlen(appData.cmailGameName) + 9);
13900 sprintf(inFilename, "%s.game.in", appData.cmailGameName);
13902 outFilename = (char *) malloc(strlen(appData.cmailGameName) + 5);
13903 sprintf(outFilename, "%s.out", appData.cmailGameName);
13906 status = stat(outFilename, &outbuf);
13908 cmailMailedMove = FALSE;
13910 status = stat(inFilename, &inbuf);
13911 cmailMailedMove = (inbuf.st_mtime < outbuf.st_mtime);
13914 /* LoadGameFromFile(CMAIL_MAX_GAMES) with cmailMsgLoaded == TRUE
13915 counts the games, notes how each one terminated, etc.
13917 It would be nice to remove this kludge and instead gather all
13918 the information while building the game list. (And to keep it
13919 in the game list nodes instead of having a bunch of fixed-size
13920 parallel arrays.) Note this will require getting each game's
13921 termination from the PGN tags, as the game list builder does
13922 not process the game moves. --mann
13924 cmailMsgLoaded = TRUE;
13925 LoadGameFromFile(inFilename, CMAIL_MAX_GAMES, "", FALSE);
13927 /* Load first game in the file or popup game menu */
13928 LoadGameFromFile(inFilename, 0, appData.cmailGameName, TRUE);
13930 #endif /* !WIN32 */
13938 char string[MSG_SIZ];
13940 if ( cmailMailedMove
13941 || (cmailResult[lastLoadGameNumber - 1] == CMAIL_OLD_RESULT)) {
13942 return TRUE; /* Allow free viewing */
13945 /* Unregister move to ensure that we don't leave RegisterMove */
13946 /* with the move registered when the conditions for registering no */
13948 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
13949 cmailMoveRegistered[lastLoadGameNumber - 1] = FALSE;
13950 nCmailMovesRegistered --;
13952 if (cmailCommentList[lastLoadGameNumber - 1] != NULL)
13954 free(cmailCommentList[lastLoadGameNumber - 1]);
13955 cmailCommentList[lastLoadGameNumber - 1] = NULL;
13959 if (cmailOldMove == -1) {
13960 DisplayError(_("You have edited the game history.\nUse Reload Same Game and make your move again."), 0);
13964 if (currentMove > cmailOldMove + 1) {
13965 DisplayError(_("You have entered too many moves.\nBack up to the correct position and try again."), 0);
13969 if (currentMove < cmailOldMove) {
13970 DisplayError(_("Displayed position is not current.\nStep forward to the correct position and try again."), 0);
13974 if (forwardMostMove > currentMove) {
13975 /* Silently truncate extra moves */
13979 if ( (currentMove == cmailOldMove + 1)
13980 || ( (currentMove == cmailOldMove)
13981 && ( (cmailMoveType[lastLoadGameNumber - 1] == CMAIL_ACCEPT)
13982 || (cmailMoveType[lastLoadGameNumber - 1] == CMAIL_RESIGN)))) {
13983 if (gameInfo.result != GameUnfinished) {
13984 cmailResult[lastLoadGameNumber - 1] = CMAIL_NEW_RESULT;
13987 if (commentList[currentMove] != NULL) {
13988 cmailCommentList[lastLoadGameNumber - 1]
13989 = StrSave(commentList[currentMove]);
13991 safeStrCpy(cmailMove[lastLoadGameNumber - 1], moveList[currentMove - 1], sizeof(cmailMove[lastLoadGameNumber - 1])/sizeof(cmailMove[lastLoadGameNumber - 1][0]));
13993 if (appData.debugMode)
13994 fprintf(debugFP, "Saving %s for game %d\n",
13995 cmailMove[lastLoadGameNumber - 1], lastLoadGameNumber);
13997 snprintf(string, MSG_SIZ, "%s.game.out.%d", appData.cmailGameName, lastLoadGameNumber);
13999 f = fopen(string, "w");
14000 if (appData.oldSaveStyle) {
14001 SaveGameOldStyle(f); /* also closes the file */
14003 snprintf(string, MSG_SIZ, "%s.pos.out", appData.cmailGameName);
14004 f = fopen(string, "w");
14005 SavePosition(f, 0, NULL); /* also closes the file */
14007 fprintf(f, "{--------------\n");
14008 PrintPosition(f, currentMove);
14009 fprintf(f, "--------------}\n\n");
14011 SaveGame(f, 0, NULL); /* also closes the file*/
14014 cmailMoveRegistered[lastLoadGameNumber - 1] = TRUE;
14015 nCmailMovesRegistered ++;
14016 } else if (nCmailGames == 1) {
14017 DisplayError(_("You have not made a move yet"), 0);
14028 static char *partCommandString = "cmail -xv%s -remail -game %s 2>&1";
14029 FILE *commandOutput;
14030 char buffer[MSG_SIZ], msg[MSG_SIZ], string[MSG_SIZ];
14031 int nBytes = 0; /* Suppress warnings on uninitialized variables */
14037 if (! cmailMsgLoaded) {
14038 DisplayError(_("The cmail message is not loaded.\nUse Reload CMail Message and make your move again."), 0);
14042 if (nCmailGames == nCmailResults) {
14043 DisplayError(_("No unfinished games"), 0);
14047 #if CMAIL_PROHIBIT_REMAIL
14048 if (cmailMailedMove) {
14049 snprintf(msg, MSG_SIZ, _("You have already mailed a move.\nWait until a move arrives from your opponent.\nTo resend the same move, type\n\"cmail -remail -game %s\"\non the command line."), appData.cmailGameName);
14050 DisplayError(msg, 0);
14055 if (! (cmailMailedMove || RegisterMove())) return;
14057 if ( cmailMailedMove
14058 || (nCmailMovesRegistered + nCmailResults == nCmailGames)) {
14059 snprintf(string, MSG_SIZ, partCommandString,
14060 appData.debugMode ? " -v" : "", appData.cmailGameName);
14061 commandOutput = popen(string, "r");
14063 if (commandOutput == NULL) {
14064 DisplayError(_("Failed to invoke cmail"), 0);
14066 for (nBuffers = 0; (! feof(commandOutput)); nBuffers ++) {
14067 nBytes = fread(buffer, 1, MSG_SIZ - 1, commandOutput);
14069 if (nBuffers > 1) {
14070 (void) memcpy(msg, buffer + nBytes, MSG_SIZ - nBytes - 1);
14071 (void) memcpy(msg + MSG_SIZ - nBytes - 1, buffer, nBytes);
14072 nBytes = MSG_SIZ - 1;
14074 (void) memcpy(msg, buffer, nBytes);
14076 *(msg + nBytes) = '\0'; /* \0 for end-of-string*/
14078 if(StrStr(msg, "Mailed cmail message to ") != NULL) {
14079 cmailMailedMove = TRUE; /* Prevent >1 moves */
14082 for (i = 0; i < nCmailGames; i ++) {
14083 if (cmailResult[i] == CMAIL_NOT_RESULT) {
14088 && ( (arcDir = (char *) getenv("CMAIL_ARCDIR"))
14090 snprintf(buffer, MSG_SIZ, "%s/%s.%s.archive",
14092 appData.cmailGameName,
14094 LoadGameFromFile(buffer, 1, buffer, FALSE);
14095 cmailMsgLoaded = FALSE;
14099 DisplayInformation(msg);
14100 pclose(commandOutput);
14103 if ((*cmailMsg) != '\0') {
14104 DisplayInformation(cmailMsg);
14109 #endif /* !WIN32 */
14118 int prependComma = 0;
14120 char string[MSG_SIZ]; /* Space for game-list */
14123 if (!cmailMsgLoaded) return "";
14125 if (cmailMailedMove) {
14126 snprintf(cmailMsg, MSG_SIZ, _("Waiting for reply from opponent\n"));
14128 /* Create a list of games left */
14129 snprintf(string, MSG_SIZ, "[");
14130 for (i = 0; i < nCmailGames; i ++) {
14131 if (! ( cmailMoveRegistered[i]
14132 || (cmailResult[i] == CMAIL_OLD_RESULT))) {
14133 if (prependComma) {
14134 snprintf(number, sizeof(number)/sizeof(number[0]), ",%d", i + 1);
14136 snprintf(number, sizeof(number)/sizeof(number[0]), "%d", i + 1);
14140 strcat(string, number);
14143 strcat(string, "]");
14145 if (nCmailMovesRegistered + nCmailResults == 0) {
14146 switch (nCmailGames) {
14148 snprintf(cmailMsg, MSG_SIZ, _("Still need to make move for game\n"));
14152 snprintf(cmailMsg, MSG_SIZ, _("Still need to make moves for both games\n"));
14156 snprintf(cmailMsg, MSG_SIZ, _("Still need to make moves for all %d games\n"),
14161 switch (nCmailGames - nCmailMovesRegistered - nCmailResults) {
14163 snprintf(cmailMsg, MSG_SIZ, _("Still need to make a move for game %s\n"),
14168 if (nCmailResults == nCmailGames) {
14169 snprintf(cmailMsg, MSG_SIZ, _("No unfinished games\n"));
14171 snprintf(cmailMsg, MSG_SIZ, _("Ready to send mail\n"));
14176 snprintf(cmailMsg, MSG_SIZ, _("Still need to make moves for games %s\n"),
14188 if (gameMode == Training)
14189 SetTrainingModeOff();
14192 cmailMsgLoaded = FALSE;
14193 if (appData.icsActive) {
14194 SendToICS(ics_prefix);
14195 SendToICS("refresh\n");
14200 ExitEvent (int status)
14204 /* Give up on clean exit */
14208 /* Keep trying for clean exit */
14212 if (appData.icsActive) printf("\n"); // [HGM] end on new line after closing XBoard
14213 if (appData.icsActive && appData.colorize) Colorize(ColorNone, FALSE);
14215 if (telnetISR != NULL) {
14216 RemoveInputSource(telnetISR);
14218 if (icsPR != NoProc) {
14219 DestroyChildProcess(icsPR, TRUE);
14222 /* [HGM] crash: leave writing PGN and position entirely to GameEnds() */
14223 GameEnds(gameInfo.result, gameInfo.resultDetails==NULL ? "xboard exit" : gameInfo.resultDetails, GE_PLAYER);
14225 /* [HGM] crash: the above GameEnds() is a dud if another one was running */
14226 /* make sure this other one finishes before killing it! */
14227 if(endingGame) { int count = 0;
14228 if(appData.debugMode) fprintf(debugFP, "ExitEvent() during GameEnds(), wait\n");
14229 while(endingGame && count++ < 10) DoSleep(1);
14230 if(appData.debugMode && endingGame) fprintf(debugFP, "GameEnds() seems stuck, proceed exiting\n");
14233 /* Kill off chess programs */
14234 if (first.pr != NoProc) {
14237 DoSleep( appData.delayBeforeQuit );
14238 SendToProgram("quit\n", &first);
14239 DestroyChildProcess(first.pr, 4 + first.useSigterm /* [AS] first.useSigterm */ );
14241 if (second.pr != NoProc) {
14242 DoSleep( appData.delayBeforeQuit );
14243 SendToProgram("quit\n", &second);
14244 DestroyChildProcess(second.pr, 4 + second.useSigterm /* [AS] second.useSigterm */ );
14246 if (first.isr != NULL) {
14247 RemoveInputSource(first.isr);
14249 if (second.isr != NULL) {
14250 RemoveInputSource(second.isr);
14253 if (pairing.pr != NoProc) SendToProgram("quit\n", &pairing);
14254 if (pairing.isr != NULL) RemoveInputSource(pairing.isr);
14256 ShutDownFrontEnd();
14261 PauseEngine (ChessProgramState *cps)
14263 SendToProgram("pause\n", cps);
14268 UnPauseEngine (ChessProgramState *cps)
14270 SendToProgram("resume\n", cps);
14277 if (appData.debugMode)
14278 fprintf(debugFP, "PauseEvent(): pausing %d\n", pausing);
14282 if(stalledEngine) { // [HGM] pause: resume game by releasing withheld move
14284 if(gameMode == TwoMachinesPlay) { // we might have to make the opponent resume pondering
14285 if(stalledEngine->other->pause == 2) UnPauseEngine(stalledEngine->other);
14286 else if(appData.ponderNextMove) SendToProgram("hard\n", stalledEngine->other);
14288 if(appData.ponderNextMove) SendToProgram("hard\n", stalledEngine);
14289 HandleMachineMove(stashedInputMove, stalledEngine);
14290 stalledEngine = NULL;
14293 if (gameMode == MachinePlaysWhite ||
14294 gameMode == TwoMachinesPlay ||
14295 gameMode == MachinePlaysBlack) { // the thinking engine must have used pause mode, or it would have been stalledEngine
14296 if(first.pause) UnPauseEngine(&first);
14297 else if(appData.ponderNextMove) SendToProgram("hard\n", &first);
14298 if(second.pause) UnPauseEngine(&second);
14299 else if(gameMode == TwoMachinesPlay && appData.ponderNextMove) SendToProgram("hard\n", &second);
14302 DisplayBothClocks();
14304 if (gameMode == PlayFromGameFile) {
14305 if (appData.timeDelay >= 0)
14306 AutoPlayGameLoop();
14307 } else if (gameMode == IcsExamining && pauseExamInvalid) {
14308 Reset(FALSE, TRUE);
14309 SendToICS(ics_prefix);
14310 SendToICS("refresh\n");
14311 } else if (currentMove < forwardMostMove && gameMode != AnalyzeMode) {
14312 ForwardInner(forwardMostMove);
14314 pauseExamInvalid = FALSE;
14316 switch (gameMode) {
14320 pauseExamForwardMostMove = forwardMostMove;
14321 pauseExamInvalid = FALSE;
14324 case IcsPlayingWhite:
14325 case IcsPlayingBlack:
14329 case PlayFromGameFile:
14330 (void) StopLoadGameTimer();
14334 case BeginningOfGame:
14335 if (appData.icsActive) return;
14336 /* else fall through */
14337 case MachinePlaysWhite:
14338 case MachinePlaysBlack:
14339 case TwoMachinesPlay:
14340 if (forwardMostMove == 0)
14341 return; /* don't pause if no one has moved */
14342 if(gameMode == TwoMachinesPlay) { // [HGM] pause: stop clocks if engine can be paused immediately
14343 ChessProgramState *onMove = (WhiteOnMove(forwardMostMove) == (first.twoMachinesColor[0] == 'w') ? &first : &second);
14344 if(onMove->pause) { // thinking engine can be paused
14345 PauseEngine(onMove); // do it
14346 if(onMove->other->pause) // pondering opponent can always be paused immediately
14347 PauseEngine(onMove->other);
14349 SendToProgram("easy\n", onMove->other);
14351 } else if(appData.ponderNextMove) SendToProgram("easy\n", onMove); // pre-emptively bring out of ponder
14352 } else if(gameMode == (WhiteOnMove(forwardMostMove) ? MachinePlaysWhite : MachinePlaysBlack)) { // engine on move
14354 PauseEngine(&first);
14356 } else if(appData.ponderNextMove) SendToProgram("easy\n", &first); // pre-emptively bring out of ponder
14357 } else { // human on move, pause pondering by either method
14359 PauseEngine(&first);
14360 else if(appData.ponderNextMove)
14361 SendToProgram("easy\n", &first);
14364 // if no immediate pausing is possible, wait for engine to move, and stop clocks then
14374 EditCommentEvent ()
14376 char title[MSG_SIZ];
14378 if (currentMove < 1 || parseList[currentMove - 1][0] == NULLCHAR) {
14379 safeStrCpy(title, _("Edit comment"), sizeof(title)/sizeof(title[0]));
14381 snprintf(title, MSG_SIZ, _("Edit comment on %d.%s%s"), (currentMove - 1) / 2 + 1,
14382 WhiteOnMove(currentMove - 1) ? " " : ".. ",
14383 parseList[currentMove - 1]);
14386 EditCommentPopUp(currentMove, title, commentList[currentMove]);
14393 char *tags = PGNTags(&gameInfo);
14395 EditTagsPopUp(tags, NULL);
14402 if(second.analyzing) {
14403 SendToProgram("exit\n", &second);
14404 second.analyzing = FALSE;
14406 if (second.pr == NoProc) StartChessProgram(&second);
14407 InitChessProgram(&second, FALSE);
14408 FeedMovesToProgram(&second, currentMove);
14410 SendToProgram("analyze\n", &second);
14411 second.analyzing = TRUE;
14415 /* Toggle ShowThinking */
14417 ToggleShowThinking()
14419 appData.showThinking = !appData.showThinking;
14420 ShowThinkingEvent();
14424 AnalyzeModeEvent ()
14428 if (!first.analysisSupport) {
14429 snprintf(buf, sizeof(buf), _("%s does not support analysis"), first.tidy);
14430 DisplayError(buf, 0);
14433 /* [DM] icsEngineAnalyze [HGM] This is horrible code; reverse the gameMode and isEngineAnalyze tests! */
14434 if (appData.icsActive) {
14435 if (gameMode != IcsObserving) {
14436 snprintf(buf, MSG_SIZ, _("You are not observing a game"));
14437 DisplayError(buf, 0);
14439 if (appData.icsEngineAnalyze) {
14440 if (appData.debugMode)
14441 fprintf(debugFP, "Found unexpected active ICS engine analyze \n");
14447 /* if enable, user wants to disable icsEngineAnalyze */
14448 if (appData.icsEngineAnalyze) {
14453 appData.icsEngineAnalyze = TRUE;
14454 if (appData.debugMode)
14455 fprintf(debugFP, "ICS engine analyze starting... \n");
14458 if (gameMode == AnalyzeMode) { ToggleSecond(); return 0; }
14459 if (appData.noChessProgram || gameMode == AnalyzeMode)
14462 if (gameMode != AnalyzeFile) {
14463 if (!appData.icsEngineAnalyze) {
14465 if (gameMode != EditGame) return 0;
14467 if (!appData.showThinking) ToggleShowThinking();
14468 ResurrectChessProgram();
14469 SendToProgram("analyze\n", &first);
14470 first.analyzing = TRUE;
14471 /*first.maybeThinking = TRUE;*/
14472 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
14473 EngineOutputPopUp();
14475 if (!appData.icsEngineAnalyze) {
14476 gameMode = AnalyzeMode;
14477 ClearEngineOutputPane(0); // [TK] exclude: to print exclusion/multipv header
14483 StartAnalysisClock();
14484 GetTimeMark(&lastNodeCountTime);
14490 AnalyzeFileEvent ()
14492 if (appData.noChessProgram || gameMode == AnalyzeFile)
14495 if (!first.analysisSupport) {
14497 snprintf(buf, sizeof(buf), _("%s does not support analysis"), first.tidy);
14498 DisplayError(buf, 0);
14502 if (gameMode != AnalyzeMode) {
14503 keepInfo = 1; // mere annotating should not alter PGN tags
14506 if (gameMode != EditGame) return;
14507 if (!appData.showThinking) ToggleShowThinking();
14508 ResurrectChessProgram();
14509 SendToProgram("analyze\n", &first);
14510 first.analyzing = TRUE;
14511 /*first.maybeThinking = TRUE;*/
14512 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
14513 EngineOutputPopUp();
14515 gameMode = AnalyzeFile;
14519 StartAnalysisClock();
14520 GetTimeMark(&lastNodeCountTime);
14522 if(appData.timeDelay > 0) StartLoadGameTimer((long)(1000.0f * appData.timeDelay));
14523 AnalysisPeriodicEvent(1);
14527 MachineWhiteEvent ()
14530 char *bookHit = NULL;
14532 if (appData.noChessProgram || (gameMode == MachinePlaysWhite))
14536 if (gameMode == PlayFromGameFile ||
14537 gameMode == TwoMachinesPlay ||
14538 gameMode == Training ||
14539 gameMode == AnalyzeMode ||
14540 gameMode == EndOfGame)
14543 if (gameMode == EditPosition)
14544 EditPositionDone(TRUE);
14546 if (!WhiteOnMove(currentMove)) {
14547 DisplayError(_("It is not White's turn"), 0);
14551 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile)
14554 if (gameMode == EditGame || gameMode == AnalyzeMode ||
14555 gameMode == AnalyzeFile)
14558 ResurrectChessProgram(); /* in case it isn't running */
14559 if(gameMode == BeginningOfGame) { /* [HGM] time odds: to get right odds in human mode */
14560 gameMode = MachinePlaysWhite;
14563 gameMode = MachinePlaysWhite;
14567 snprintf(buf, MSG_SIZ, "%s %s %s", gameInfo.white, _("vs."), gameInfo.black);
14569 if (first.sendName) {
14570 snprintf(buf, MSG_SIZ, "name %s\n", gameInfo.black);
14571 SendToProgram(buf, &first);
14573 if (first.sendTime) {
14574 if (first.useColors) {
14575 SendToProgram("black\n", &first); /*gnu kludge*/
14577 SendTimeRemaining(&first, TRUE);
14579 if (first.useColors) {
14580 SendToProgram("white\n", &first); // [HGM] book: send 'go' separately
14582 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: send go or retrieve book move
14583 SetMachineThinkingEnables();
14584 first.maybeThinking = TRUE;
14588 if (appData.autoFlipView && !flipView) {
14589 flipView = !flipView;
14590 DrawPosition(FALSE, NULL);
14591 DisplayBothClocks(); // [HGM] logo: clocks might have to be exchanged;
14594 if(bookHit) { // [HGM] book: simulate book reply
14595 static char bookMove[MSG_SIZ]; // a bit generous?
14597 programStats.nodes = programStats.depth = programStats.time =
14598 programStats.score = programStats.got_only_move = 0;
14599 sprintf(programStats.movelist, "%s (xbook)", bookHit);
14601 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
14602 strcat(bookMove, bookHit);
14603 HandleMachineMove(bookMove, &first);
14608 MachineBlackEvent ()
14611 char *bookHit = NULL;
14613 if (appData.noChessProgram || (gameMode == MachinePlaysBlack))
14617 if (gameMode == PlayFromGameFile ||
14618 gameMode == TwoMachinesPlay ||
14619 gameMode == Training ||
14620 gameMode == AnalyzeMode ||
14621 gameMode == EndOfGame)
14624 if (gameMode == EditPosition)
14625 EditPositionDone(TRUE);
14627 if (WhiteOnMove(currentMove)) {
14628 DisplayError(_("It is not Black's turn"), 0);
14632 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile)
14635 if (gameMode == EditGame || gameMode == AnalyzeMode ||
14636 gameMode == AnalyzeFile)
14639 ResurrectChessProgram(); /* in case it isn't running */
14640 gameMode = MachinePlaysBlack;
14644 snprintf(buf, MSG_SIZ, "%s %s %s", gameInfo.white, _("vs."), gameInfo.black);
14646 if (first.sendName) {
14647 snprintf(buf, MSG_SIZ, "name %s\n", gameInfo.white);
14648 SendToProgram(buf, &first);
14650 if (first.sendTime) {
14651 if (first.useColors) {
14652 SendToProgram("white\n", &first); /*gnu kludge*/
14654 SendTimeRemaining(&first, FALSE);
14656 if (first.useColors) {
14657 SendToProgram("black\n", &first); // [HGM] book: 'go' sent separately
14659 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: send go or retrieve book move
14660 SetMachineThinkingEnables();
14661 first.maybeThinking = TRUE;
14664 if (appData.autoFlipView && flipView) {
14665 flipView = !flipView;
14666 DrawPosition(FALSE, NULL);
14667 DisplayBothClocks(); // [HGM] logo: clocks might have to be exchanged;
14669 if(bookHit) { // [HGM] book: simulate book reply
14670 static char bookMove[MSG_SIZ]; // a bit generous?
14672 programStats.nodes = programStats.depth = programStats.time =
14673 programStats.score = programStats.got_only_move = 0;
14674 sprintf(programStats.movelist, "%s (xbook)", bookHit);
14676 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
14677 strcat(bookMove, bookHit);
14678 HandleMachineMove(bookMove, &first);
14684 DisplayTwoMachinesTitle ()
14687 if (appData.matchGames > 0) {
14688 if(appData.tourneyFile[0]) {
14689 snprintf(buf, MSG_SIZ, "%s %s %s (%d/%d%s)",
14690 gameInfo.white, _("vs."), gameInfo.black,
14691 nextGame+1, appData.matchGames+1,
14692 appData.tourneyType>0 ? "gt" : appData.tourneyType<0 ? "sw" : "rr");
14694 if (first.twoMachinesColor[0] == 'w') {
14695 snprintf(buf, MSG_SIZ, "%s %s %s (%d-%d-%d)",
14696 gameInfo.white, _("vs."), gameInfo.black,
14697 first.matchWins, second.matchWins,
14698 matchGame - 1 - (first.matchWins + second.matchWins));
14700 snprintf(buf, MSG_SIZ, "%s %s %s (%d-%d-%d)",
14701 gameInfo.white, _("vs."), gameInfo.black,
14702 second.matchWins, first.matchWins,
14703 matchGame - 1 - (first.matchWins + second.matchWins));
14706 snprintf(buf, MSG_SIZ, "%s %s %s", gameInfo.white, _("vs."), gameInfo.black);
14712 SettingsMenuIfReady ()
14714 if (second.lastPing != second.lastPong) {
14715 DisplayMessage("", _("Waiting for second chess program"));
14716 ScheduleDelayedEvent(SettingsMenuIfReady, 10); // [HGM] fast: lowered from 1000
14720 DisplayMessage("", "");
14721 SettingsPopUp(&second);
14725 WaitForEngine (ChessProgramState *cps, DelayedEventCallback retry)
14728 if (cps->pr == NoProc) {
14729 StartChessProgram(cps);
14730 if (cps->protocolVersion == 1) {
14732 ScheduleDelayedEvent(retry, 1); // Do this also through timeout to avoid recursive calling of 'retry'
14734 /* kludge: allow timeout for initial "feature" command */
14735 if(retry != TwoMachinesEventIfReady) FreezeUI();
14736 snprintf(buf, MSG_SIZ, _("Starting %s chess program"), _(cps->which));
14737 DisplayMessage("", buf);
14738 ScheduleDelayedEvent(retry, FEATURE_TIMEOUT);
14746 TwoMachinesEvent P((void))
14750 ChessProgramState *onmove;
14751 char *bookHit = NULL;
14752 static int stalling = 0;
14756 if (appData.noChessProgram) return;
14758 switch (gameMode) {
14759 case TwoMachinesPlay:
14761 case MachinePlaysWhite:
14762 case MachinePlaysBlack:
14763 if (WhiteOnMove(forwardMostMove) == (gameMode == MachinePlaysWhite)) {
14764 DisplayError(_("Wait until your turn,\nor select 'Move Now'."), 0);
14768 case BeginningOfGame:
14769 case PlayFromGameFile:
14772 if (gameMode != EditGame) return;
14775 EditPositionDone(TRUE);
14786 // forwardMostMove = currentMove;
14787 TruncateGame(); // [HGM] vari: MachineWhite and MachineBlack do this...
14788 startingEngine = TRUE;
14790 if(!ResurrectChessProgram()) return; /* in case first program isn't running (unbalances its ping due to InitChessProgram!) */
14792 if(!first.initDone && GetDelayedEvent() == TwoMachinesEventIfReady) return; // [HGM] engine #1 still waiting for feature timeout
14793 if(first.lastPing != first.lastPong) { // [HGM] wait till we are sure first engine has set up position
14794 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10);
14797 if(WaitForEngine(&second, TwoMachinesEventIfReady)) return; // (if needed:) started up second engine, so wait for features
14799 if(!SupportedVariant(second.variants, gameInfo.variant, gameInfo.boardWidth,
14800 gameInfo.boardHeight, gameInfo.holdingsSize, second.protocolVersion, second.tidy)) {
14801 startingEngine = matchMode = FALSE;
14802 DisplayError("second engine does not play this", 0);
14803 gameMode = TwoMachinesPlay; ModeHighlight(); // Needed to make sure menu item is unchecked
14804 EditGameEvent(); // switch back to EditGame mode
14809 InitChessProgram(&second, FALSE); // unbalances ping of second engine
14810 SendToProgram("force\n", &second);
14812 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10);
14815 GetTimeMark(&now); // [HGM] matchpause: implement match pause after engine load
14816 if(appData.matchPause>10000 || appData.matchPause<10)
14817 appData.matchPause = 10000; /* [HGM] make pause adjustable */
14818 wait = SubtractTimeMarks(&now, &pauseStart);
14819 if(wait < appData.matchPause) {
14820 ScheduleDelayedEvent(TwoMachinesEventIfReady, appData.matchPause - wait);
14823 // we are now committed to starting the game
14825 DisplayMessage("", "");
14826 if (startedFromSetupPosition) {
14827 SendBoard(&second, backwardMostMove);
14828 if (appData.debugMode) {
14829 fprintf(debugFP, "Two Machines\n");
14832 for (i = backwardMostMove; i < forwardMostMove; i++) {
14833 SendMoveToProgram(i, &second);
14836 gameMode = TwoMachinesPlay;
14837 pausing = startingEngine = FALSE;
14838 ModeHighlight(); // [HGM] logo: this triggers display update of logos
14840 DisplayTwoMachinesTitle();
14842 if ((first.twoMachinesColor[0] == 'w') == WhiteOnMove(forwardMostMove)) {
14847 if(appData.debugMode) fprintf(debugFP, "New game (%d): %s-%s (%c)\n", matchGame, first.tidy, second.tidy, first.twoMachinesColor[0]);
14848 SendToProgram(first.computerString, &first);
14849 if (first.sendName) {
14850 snprintf(buf, MSG_SIZ, "name %s\n", second.tidy);
14851 SendToProgram(buf, &first);
14853 SendToProgram(second.computerString, &second);
14854 if (second.sendName) {
14855 snprintf(buf, MSG_SIZ, "name %s\n", first.tidy);
14856 SendToProgram(buf, &second);
14860 if (!first.sendTime || !second.sendTime) {
14861 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
14862 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
14864 if (onmove->sendTime) {
14865 if (onmove->useColors) {
14866 SendToProgram(onmove->other->twoMachinesColor, onmove); /*gnu kludge*/
14868 SendTimeRemaining(onmove, WhiteOnMove(forwardMostMove));
14870 if (onmove->useColors) {
14871 SendToProgram(onmove->twoMachinesColor, onmove);
14873 bookHit = SendMoveToBookUser(forwardMostMove-1, onmove, TRUE); // [HGM] book: send go or retrieve book move
14874 // SendToProgram("go\n", onmove);
14875 onmove->maybeThinking = TRUE;
14876 SetMachineThinkingEnables();
14880 if(bookHit) { // [HGM] book: simulate book reply
14881 static char bookMove[MSG_SIZ]; // a bit generous?
14883 programStats.nodes = programStats.depth = programStats.time =
14884 programStats.score = programStats.got_only_move = 0;
14885 sprintf(programStats.movelist, "%s (xbook)", bookHit);
14887 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
14888 strcat(bookMove, bookHit);
14889 savedMessage = bookMove; // args for deferred call
14890 savedState = onmove;
14891 ScheduleDelayedEvent(DeferredBookMove, 1);
14898 if (gameMode == Training) {
14899 SetTrainingModeOff();
14900 gameMode = PlayFromGameFile;
14901 DisplayMessage("", _("Training mode off"));
14903 gameMode = Training;
14904 animateTraining = appData.animate;
14906 /* make sure we are not already at the end of the game */
14907 if (currentMove < forwardMostMove) {
14908 SetTrainingModeOn();
14909 DisplayMessage("", _("Training mode on"));
14911 gameMode = PlayFromGameFile;
14912 DisplayError(_("Already at end of game"), 0);
14921 if (!appData.icsActive) return;
14922 switch (gameMode) {
14923 case IcsPlayingWhite:
14924 case IcsPlayingBlack:
14927 case BeginningOfGame:
14935 EditPositionDone(TRUE);
14948 gameMode = IcsIdle;
14958 switch (gameMode) {
14960 SetTrainingModeOff();
14962 case MachinePlaysWhite:
14963 case MachinePlaysBlack:
14964 case BeginningOfGame:
14965 SendToProgram("force\n", &first);
14966 SetUserThinkingEnables();
14968 case PlayFromGameFile:
14969 (void) StopLoadGameTimer();
14970 if (gameFileFP != NULL) {
14975 EditPositionDone(TRUE);
14980 SendToProgram("force\n", &first);
14982 case TwoMachinesPlay:
14983 GameEnds(EndOfFile, NULL, GE_PLAYER);
14984 ResurrectChessProgram();
14985 SetUserThinkingEnables();
14988 ResurrectChessProgram();
14990 case IcsPlayingBlack:
14991 case IcsPlayingWhite:
14992 DisplayError(_("Warning: You are still playing a game"), 0);
14995 DisplayError(_("Warning: You are still observing a game"), 0);
14998 DisplayError(_("Warning: You are still examining a game"), 0);
15009 first.offeredDraw = second.offeredDraw = 0;
15011 if (gameMode == PlayFromGameFile) {
15012 whiteTimeRemaining = timeRemaining[0][currentMove];
15013 blackTimeRemaining = timeRemaining[1][currentMove];
15017 if (gameMode == MachinePlaysWhite ||
15018 gameMode == MachinePlaysBlack ||
15019 gameMode == TwoMachinesPlay ||
15020 gameMode == EndOfGame) {
15021 i = forwardMostMove;
15022 while (i > currentMove) {
15023 SendToProgram("undo\n", &first);
15026 if(!adjustedClock) {
15027 whiteTimeRemaining = timeRemaining[0][currentMove];
15028 blackTimeRemaining = timeRemaining[1][currentMove];
15029 DisplayBothClocks();
15031 if (whiteFlag || blackFlag) {
15032 whiteFlag = blackFlag = 0;
15037 gameMode = EditGame;
15044 EditPositionEvent ()
15046 if (gameMode == EditPosition) {
15052 if (gameMode != EditGame) return;
15054 gameMode = EditPosition;
15057 if (currentMove > 0)
15058 CopyBoard(boards[0], boards[currentMove]);
15060 blackPlaysFirst = !WhiteOnMove(currentMove);
15062 currentMove = forwardMostMove = backwardMostMove = 0;
15063 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
15065 if(!appData.pieceMenu) DisplayMessage(_("Click clock to clear board"), "");
15071 /* [DM] icsEngineAnalyze - possible call from other functions */
15072 if (appData.icsEngineAnalyze) {
15073 appData.icsEngineAnalyze = FALSE;
15075 DisplayMessage("",_("Close ICS engine analyze..."));
15077 if (first.analysisSupport && first.analyzing) {
15078 SendToBoth("exit\n");
15079 first.analyzing = second.analyzing = FALSE;
15081 thinkOutput[0] = NULLCHAR;
15085 EditPositionDone (Boolean fakeRights)
15087 int king = gameInfo.variant == VariantKnightmate ? WhiteUnicorn : WhiteKing;
15089 startedFromSetupPosition = TRUE;
15090 InitChessProgram(&first, FALSE);
15091 if(fakeRights) { // [HGM] suppress this if we just pasted a FEN.
15092 boards[0][EP_STATUS] = EP_NONE;
15093 boards[0][CASTLING][2] = boards[0][CASTLING][5] = BOARD_WIDTH>>1;
15094 if(boards[0][0][BOARD_WIDTH>>1] == king) {
15095 boards[0][CASTLING][1] = boards[0][0][BOARD_LEFT] == WhiteRook ? BOARD_LEFT : NoRights;
15096 boards[0][CASTLING][0] = boards[0][0][BOARD_RGHT-1] == WhiteRook ? BOARD_RGHT-1 : NoRights;
15097 } else boards[0][CASTLING][2] = NoRights;
15098 if(boards[0][BOARD_HEIGHT-1][BOARD_WIDTH>>1] == WHITE_TO_BLACK king) {
15099 boards[0][CASTLING][4] = boards[0][BOARD_HEIGHT-1][BOARD_LEFT] == BlackRook ? BOARD_LEFT : NoRights;
15100 boards[0][CASTLING][3] = boards[0][BOARD_HEIGHT-1][BOARD_RGHT-1] == BlackRook ? BOARD_RGHT-1 : NoRights;
15101 } else boards[0][CASTLING][5] = NoRights;
15102 if(gameInfo.variant == VariantSChess) {
15104 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // pieces in their original position are assumed virgin
15105 boards[0][VIRGIN][i] = 0;
15106 if(boards[0][0][i] == FIDEArray[0][i-BOARD_LEFT]) boards[0][VIRGIN][i] |= VIRGIN_W;
15107 if(boards[0][BOARD_HEIGHT-1][i] == FIDEArray[1][i-BOARD_LEFT]) boards[0][VIRGIN][i] |= VIRGIN_B;
15111 SendToProgram("force\n", &first);
15112 if (blackPlaysFirst) {
15113 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
15114 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
15115 currentMove = forwardMostMove = backwardMostMove = 1;
15116 CopyBoard(boards[1], boards[0]);
15118 currentMove = forwardMostMove = backwardMostMove = 0;
15120 SendBoard(&first, forwardMostMove);
15121 if (appData.debugMode) {
15122 fprintf(debugFP, "EditPosDone\n");
15125 DisplayMessage("", "");
15126 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
15127 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
15128 gameMode = EditGame;
15130 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
15131 ClearHighlights(); /* [AS] */
15134 /* Pause for `ms' milliseconds */
15135 /* !! Ugh, this is a kludge. Fix it sometime. --tpm */
15137 TimeDelay (long ms)
15144 } while (SubtractTimeMarks(&m2, &m1) < ms);
15147 /* !! Ugh, this is a kludge. Fix it sometime. --tpm */
15149 SendMultiLineToICS (char *buf)
15151 char temp[MSG_SIZ+1], *p;
15158 strncpy(temp, buf, len);
15163 if (*p == '\n' || *p == '\r')
15168 strcat(temp, "\n");
15170 SendToPlayer(temp, strlen(temp));
15174 SetWhiteToPlayEvent ()
15176 if (gameMode == EditPosition) {
15177 blackPlaysFirst = FALSE;
15178 DisplayBothClocks(); /* works because currentMove is 0 */
15179 } else if (gameMode == IcsExamining) {
15180 SendToICS(ics_prefix);
15181 SendToICS("tomove white\n");
15186 SetBlackToPlayEvent ()
15188 if (gameMode == EditPosition) {
15189 blackPlaysFirst = TRUE;
15190 currentMove = 1; /* kludge */
15191 DisplayBothClocks();
15193 } else if (gameMode == IcsExamining) {
15194 SendToICS(ics_prefix);
15195 SendToICS("tomove black\n");
15200 EditPositionMenuEvent (ChessSquare selection, int x, int y)
15203 ChessSquare piece = boards[0][y][x];
15204 static Board erasedBoard, currentBoard, menuBoard, nullBoard;
15205 static int lastVariant;
15207 if (gameMode != EditPosition && gameMode != IcsExamining) return;
15209 switch (selection) {
15211 fromX = fromY = killX = killY = -1; // [HGM] abort any move entry in progress
15212 MarkTargetSquares(1);
15213 CopyBoard(currentBoard, boards[0]);
15214 CopyBoard(menuBoard, initialPosition);
15215 if (gameMode == IcsExamining && ics_type == ICS_FICS) {
15216 SendToICS(ics_prefix);
15217 SendToICS("bsetup clear\n");
15218 } else if (gameMode == IcsExamining && ics_type == ICS_ICC) {
15219 SendToICS(ics_prefix);
15220 SendToICS("clearboard\n");
15223 for (x = 0; x < BOARD_WIDTH; x++) { ChessSquare p = EmptySquare;
15224 if(x == BOARD_LEFT-1 || x == BOARD_RGHT) p = (ChessSquare) 0; /* [HGM] holdings */
15225 for (y = 0; y < BOARD_HEIGHT; y++) {
15226 if (gameMode == IcsExamining) {
15227 if (boards[currentMove][y][x] != EmptySquare) {
15228 snprintf(buf, MSG_SIZ, "%sx@%c%c\n", ics_prefix,
15232 } else if(boards[0][y][x] != DarkSquare) {
15233 if(boards[0][y][x] != p) nonEmpty++;
15234 boards[0][y][x] = p;
15238 if(gameMode != IcsExamining) { // [HGM] editpos: cycle trough boards
15240 for(r = 0; r < BOARD_HEIGHT; r++) {
15241 for(x = BOARD_LEFT; x < BOARD_RGHT; x++) { // create 'menu board' by removing duplicates
15242 ChessSquare p = menuBoard[r][x];
15243 for(y = x + 1; y < BOARD_RGHT; y++) if(menuBoard[r][y] == p) menuBoard[r][y] = EmptySquare;
15246 DisplayMessage("Clicking clock again restores position", "");
15247 if(gameInfo.variant != lastVariant) lastVariant = gameInfo.variant, CopyBoard(erasedBoard, boards[0]);
15248 if(!nonEmpty) { // asked to clear an empty board
15249 CopyBoard(boards[0], menuBoard);
15251 if(CompareBoards(currentBoard, menuBoard)) { // asked to clear an empty board
15252 CopyBoard(boards[0], initialPosition);
15254 if(CompareBoards(currentBoard, initialPosition) && !CompareBoards(currentBoard, erasedBoard)
15255 && !CompareBoards(nullBoard, erasedBoard)) {
15256 CopyBoard(boards[0], erasedBoard);
15258 CopyBoard(erasedBoard, currentBoard);
15262 if (gameMode == EditPosition) {
15263 DrawPosition(FALSE, boards[0]);
15268 SetWhiteToPlayEvent();
15272 SetBlackToPlayEvent();
15276 if (gameMode == IcsExamining) {
15277 if (x < BOARD_LEFT || x >= BOARD_RGHT) break; // [HGM] holdings
15278 snprintf(buf, MSG_SIZ, "%sx@%c%c\n", ics_prefix, AAA + x, ONE + y);
15281 if(x < BOARD_LEFT || x >= BOARD_RGHT) {
15282 if(x == BOARD_LEFT-2) {
15283 if(y < BOARD_HEIGHT-1-gameInfo.holdingsSize) break;
15284 boards[0][y][1] = 0;
15286 if(x == BOARD_RGHT+1) {
15287 if(y >= gameInfo.holdingsSize) break;
15288 boards[0][y][BOARD_WIDTH-2] = 0;
15291 boards[0][y][x] = EmptySquare;
15292 DrawPosition(FALSE, boards[0]);
15297 if(piece >= (int)WhitePawn && piece < (int)WhiteMan ||
15298 piece >= (int)BlackPawn && piece < (int)BlackMan ) {
15299 selection = (ChessSquare) (PROMOTED piece);
15300 } else if(piece == EmptySquare) selection = WhiteSilver;
15301 else selection = (ChessSquare)((int)piece - 1);
15305 if(piece > (int)WhiteMan && piece <= (int)WhiteKing ||
15306 piece > (int)BlackMan && piece <= (int)BlackKing ) {
15307 selection = (ChessSquare) (DEMOTED piece);
15308 } else if(piece == EmptySquare) selection = BlackSilver;
15309 else selection = (ChessSquare)((int)piece + 1);
15314 if(gameInfo.variant == VariantShatranj ||
15315 gameInfo.variant == VariantXiangqi ||
15316 gameInfo.variant == VariantCourier ||
15317 gameInfo.variant == VariantASEAN ||
15318 gameInfo.variant == VariantMakruk )
15319 selection = (ChessSquare)((int)selection - (int)WhiteQueen + (int)WhiteFerz);
15324 if(gameInfo.variant == VariantXiangqi)
15325 selection = (ChessSquare)((int)selection - (int)WhiteKing + (int)WhiteWazir);
15326 if(gameInfo.variant == VariantKnightmate)
15327 selection = (ChessSquare)((int)selection - (int)WhiteKing + (int)WhiteUnicorn);
15330 if (gameMode == IcsExamining) {
15331 if (x < BOARD_LEFT || x >= BOARD_RGHT) break; // [HGM] holdings
15332 snprintf(buf, MSG_SIZ, "%s%c@%c%c\n", ics_prefix,
15333 PieceToChar(selection), AAA + x, ONE + y);
15336 if(x < BOARD_LEFT || x >= BOARD_RGHT) {
15338 if(x == BOARD_LEFT-2 && selection >= BlackPawn) {
15339 n = PieceToNumber(selection - BlackPawn);
15340 if(n >= gameInfo.holdingsSize) { n = 0; selection = BlackPawn; }
15341 boards[0][BOARD_HEIGHT-1-n][0] = selection;
15342 boards[0][BOARD_HEIGHT-1-n][1]++;
15344 if(x == BOARD_RGHT+1 && selection < BlackPawn) {
15345 n = PieceToNumber(selection);
15346 if(n >= gameInfo.holdingsSize) { n = 0; selection = WhitePawn; }
15347 boards[0][n][BOARD_WIDTH-1] = selection;
15348 boards[0][n][BOARD_WIDTH-2]++;
15351 boards[0][y][x] = selection;
15352 DrawPosition(TRUE, boards[0]);
15354 fromX = fromY = -1;
15362 DropMenuEvent (ChessSquare selection, int x, int y)
15364 ChessMove moveType;
15366 switch (gameMode) {
15367 case IcsPlayingWhite:
15368 case MachinePlaysBlack:
15369 if (!WhiteOnMove(currentMove)) {
15370 DisplayMoveError(_("It is Black's turn"));
15373 moveType = WhiteDrop;
15375 case IcsPlayingBlack:
15376 case MachinePlaysWhite:
15377 if (WhiteOnMove(currentMove)) {
15378 DisplayMoveError(_("It is White's turn"));
15381 moveType = BlackDrop;
15384 moveType = WhiteOnMove(currentMove) ? WhiteDrop : BlackDrop;
15390 if (moveType == BlackDrop && selection < BlackPawn) {
15391 selection = (ChessSquare) ((int) selection
15392 + (int) BlackPawn - (int) WhitePawn);
15394 if (boards[currentMove][y][x] != EmptySquare) {
15395 DisplayMoveError(_("That square is occupied"));
15399 FinishMove(moveType, (int) selection, DROP_RANK, x, y, NULLCHAR);
15405 /* Accept a pending offer of any kind from opponent */
15407 if (appData.icsActive) {
15408 SendToICS(ics_prefix);
15409 SendToICS("accept\n");
15410 } else if (cmailMsgLoaded) {
15411 if (currentMove == cmailOldMove &&
15412 commentList[cmailOldMove] != NULL &&
15413 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
15414 "Black offers a draw" : "White offers a draw")) {
15416 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
15417 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_ACCEPT;
15419 DisplayError(_("There is no pending offer on this move"), 0);
15420 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
15423 /* Not used for offers from chess program */
15430 /* Decline a pending offer of any kind from opponent */
15432 if (appData.icsActive) {
15433 SendToICS(ics_prefix);
15434 SendToICS("decline\n");
15435 } else if (cmailMsgLoaded) {
15436 if (currentMove == cmailOldMove &&
15437 commentList[cmailOldMove] != NULL &&
15438 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
15439 "Black offers a draw" : "White offers a draw")) {
15441 AppendComment(cmailOldMove, "Draw declined", TRUE);
15442 DisplayComment(cmailOldMove - 1, "Draw declined");
15445 DisplayError(_("There is no pending offer on this move"), 0);
15448 /* Not used for offers from chess program */
15455 /* Issue ICS rematch command */
15456 if (appData.icsActive) {
15457 SendToICS(ics_prefix);
15458 SendToICS("rematch\n");
15465 /* Call your opponent's flag (claim a win on time) */
15466 if (appData.icsActive) {
15467 SendToICS(ics_prefix);
15468 SendToICS("flag\n");
15470 switch (gameMode) {
15473 case MachinePlaysWhite:
15476 GameEnds(GameIsDrawn, "Both players ran out of time",
15479 GameEnds(BlackWins, "Black wins on time", GE_PLAYER);
15481 DisplayError(_("Your opponent is not out of time"), 0);
15484 case MachinePlaysBlack:
15487 GameEnds(GameIsDrawn, "Both players ran out of time",
15490 GameEnds(WhiteWins, "White wins on time", GE_PLAYER);
15492 DisplayError(_("Your opponent is not out of time"), 0);
15500 ClockClick (int which)
15501 { // [HGM] code moved to back-end from winboard.c
15502 if(which) { // black clock
15503 if (gameMode == EditPosition || gameMode == IcsExamining) {
15504 if(!appData.pieceMenu && blackPlaysFirst) EditPositionMenuEvent(ClearBoard, 0, 0);
15505 SetBlackToPlayEvent();
15506 } else if ((gameMode == AnalyzeMode || gameMode == EditGame ||
15507 gameMode == MachinePlaysBlack && PosFlags(0) & F_NULL_MOVE && !blackFlag && !shiftKey) && WhiteOnMove(currentMove)) {
15508 UserMoveEvent((int)EmptySquare, DROP_RANK, 0, 0, 0); // [HGM] multi-move: if not out of time, enters null move
15509 } else if (shiftKey) {
15510 AdjustClock(which, -1);
15511 } else if (gameMode == IcsPlayingWhite ||
15512 gameMode == MachinePlaysBlack) {
15515 } else { // white clock
15516 if (gameMode == EditPosition || gameMode == IcsExamining) {
15517 if(!appData.pieceMenu && !blackPlaysFirst) EditPositionMenuEvent(ClearBoard, 0, 0);
15518 SetWhiteToPlayEvent();
15519 } else if ((gameMode == AnalyzeMode || gameMode == EditGame ||
15520 gameMode == MachinePlaysWhite && PosFlags(0) & F_NULL_MOVE && !whiteFlag && !shiftKey) && !WhiteOnMove(currentMove)) {
15521 UserMoveEvent((int)EmptySquare, DROP_RANK, 0, 0, 0); // [HGM] multi-move
15522 } else if (shiftKey) {
15523 AdjustClock(which, -1);
15524 } else if (gameMode == IcsPlayingBlack ||
15525 gameMode == MachinePlaysWhite) {
15534 /* Offer draw or accept pending draw offer from opponent */
15536 if (appData.icsActive) {
15537 /* Note: tournament rules require draw offers to be
15538 made after you make your move but before you punch
15539 your clock. Currently ICS doesn't let you do that;
15540 instead, you immediately punch your clock after making
15541 a move, but you can offer a draw at any time. */
15543 SendToICS(ics_prefix);
15544 SendToICS("draw\n");
15545 userOfferedDraw = TRUE; // [HGM] drawclaim: also set flag in ICS play
15546 } else if (cmailMsgLoaded) {
15547 if (currentMove == cmailOldMove &&
15548 commentList[cmailOldMove] != NULL &&
15549 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
15550 "Black offers a draw" : "White offers a draw")) {
15551 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
15552 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_ACCEPT;
15553 } else if (currentMove == cmailOldMove + 1) {
15554 char *offer = WhiteOnMove(cmailOldMove) ?
15555 "White offers a draw" : "Black offers a draw";
15556 AppendComment(currentMove, offer, TRUE);
15557 DisplayComment(currentMove - 1, offer);
15558 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_DRAW;
15560 DisplayError(_("You must make your move before offering a draw"), 0);
15561 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
15563 } else if (first.offeredDraw) {
15564 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
15566 if (first.sendDrawOffers) {
15567 SendToProgram("draw\n", &first);
15568 userOfferedDraw = TRUE;
15576 /* Offer Adjourn or accept pending Adjourn offer from opponent */
15578 if (appData.icsActive) {
15579 SendToICS(ics_prefix);
15580 SendToICS("adjourn\n");
15582 /* Currently GNU Chess doesn't offer or accept Adjourns */
15590 /* Offer Abort or accept pending Abort offer from opponent */
15592 if (appData.icsActive) {
15593 SendToICS(ics_prefix);
15594 SendToICS("abort\n");
15596 GameEnds(GameUnfinished, "Game aborted", GE_PLAYER);
15603 /* Resign. You can do this even if it's not your turn. */
15605 if (appData.icsActive) {
15606 SendToICS(ics_prefix);
15607 SendToICS("resign\n");
15609 switch (gameMode) {
15610 case MachinePlaysWhite:
15611 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
15613 case MachinePlaysBlack:
15614 GameEnds(BlackWins, "White resigns", GE_PLAYER);
15617 if (cmailMsgLoaded) {
15619 if (WhiteOnMove(cmailOldMove)) {
15620 GameEnds(BlackWins, "White resigns", GE_PLAYER);
15622 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
15624 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_RESIGN;
15635 StopObservingEvent ()
15637 /* Stop observing current games */
15638 SendToICS(ics_prefix);
15639 SendToICS("unobserve\n");
15643 StopExaminingEvent ()
15645 /* Stop observing current game */
15646 SendToICS(ics_prefix);
15647 SendToICS("unexamine\n");
15651 ForwardInner (int target)
15653 int limit; int oldSeekGraphUp = seekGraphUp;
15655 if (appData.debugMode)
15656 fprintf(debugFP, "ForwardInner(%d), current %d, forward %d\n",
15657 target, currentMove, forwardMostMove);
15659 if (gameMode == EditPosition)
15662 seekGraphUp = FALSE;
15663 MarkTargetSquares(1);
15664 fromX = fromY = killX = killY = -1; // [HGM] abort any move entry in progress
15666 if (gameMode == PlayFromGameFile && !pausing)
15669 if (gameMode == IcsExamining && pausing)
15670 limit = pauseExamForwardMostMove;
15672 limit = forwardMostMove;
15674 if (target > limit) target = limit;
15676 if (target > 0 && moveList[target - 1][0]) {
15677 int fromX, fromY, toX, toY;
15678 toX = moveList[target - 1][2] - AAA;
15679 toY = moveList[target - 1][3] - ONE;
15680 if (moveList[target - 1][1] == '@') {
15681 if (appData.highlightLastMove) {
15682 SetHighlights(-1, -1, toX, toY);
15685 int viaX = moveList[target - 1][5] - AAA;
15686 int viaY = moveList[target - 1][6] - ONE;
15687 fromX = moveList[target - 1][0] - AAA;
15688 fromY = moveList[target - 1][1] - ONE;
15689 if (target == currentMove + 1) {
15690 if(moveList[target - 1][4] == ';') { // multi-leg
15691 ChessSquare piece = boards[currentMove][viaY][viaX];
15692 AnimateMove(boards[currentMove], fromX, fromY, viaX, viaY);
15693 boards[currentMove][viaY][viaX] = boards[currentMove][fromY][fromX];
15694 AnimateMove(boards[currentMove], viaX, viaY, toX, toY);
15695 boards[currentMove][viaY][viaX] = piece;
15697 AnimateMove(boards[currentMove], fromX, fromY, toX, toY);
15699 if (appData.highlightLastMove) {
15700 SetHighlights(fromX, fromY, toX, toY);
15704 if (gameMode == EditGame || gameMode == AnalyzeMode ||
15705 gameMode == Training || gameMode == PlayFromGameFile ||
15706 gameMode == AnalyzeFile) {
15707 while (currentMove < target) {
15708 if(second.analyzing) SendMoveToProgram(currentMove, &second);
15709 SendMoveToProgram(currentMove++, &first);
15712 currentMove = target;
15715 if (gameMode == EditGame || gameMode == EndOfGame) {
15716 whiteTimeRemaining = timeRemaining[0][currentMove];
15717 blackTimeRemaining = timeRemaining[1][currentMove];
15719 DisplayBothClocks();
15720 DisplayMove(currentMove - 1);
15721 DrawPosition(oldSeekGraphUp, boards[currentMove]);
15722 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
15723 if ( !matchMode && gameMode != Training) { // [HGM] PV info: routine tests if empty
15724 DisplayComment(currentMove - 1, commentList[currentMove]);
15726 ClearMap(); // [HGM] exclude: invalidate map
15733 if (gameMode == IcsExamining && !pausing) {
15734 SendToICS(ics_prefix);
15735 SendToICS("forward\n");
15737 ForwardInner(currentMove + 1);
15744 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
15745 /* to optimze, we temporarily turn off analysis mode while we feed
15746 * the remaining moves to the engine. Otherwise we get analysis output
15749 if (first.analysisSupport) {
15750 SendToProgram("exit\nforce\n", &first);
15751 first.analyzing = FALSE;
15755 if (gameMode == IcsExamining && !pausing) {
15756 SendToICS(ics_prefix);
15757 SendToICS("forward 999999\n");
15759 ForwardInner(forwardMostMove);
15762 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
15763 /* we have fed all the moves, so reactivate analysis mode */
15764 SendToProgram("analyze\n", &first);
15765 first.analyzing = TRUE;
15766 /*first.maybeThinking = TRUE;*/
15767 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
15772 BackwardInner (int target)
15774 int full_redraw = TRUE; /* [AS] Was FALSE, had to change it! */
15776 if (appData.debugMode)
15777 fprintf(debugFP, "BackwardInner(%d), current %d, forward %d\n",
15778 target, currentMove, forwardMostMove);
15780 if (gameMode == EditPosition) return;
15781 seekGraphUp = FALSE;
15782 MarkTargetSquares(1);
15783 fromX = fromY = killX = killY = -1; // [HGM] abort any move entry in progress
15784 if (currentMove <= backwardMostMove) {
15786 DrawPosition(full_redraw, boards[currentMove]);
15789 if (gameMode == PlayFromGameFile && !pausing)
15792 if (moveList[target][0]) {
15793 int fromX, fromY, toX, toY;
15794 toX = moveList[target][2] - AAA;
15795 toY = moveList[target][3] - ONE;
15796 if (moveList[target][1] == '@') {
15797 if (appData.highlightLastMove) {
15798 SetHighlights(-1, -1, toX, toY);
15801 fromX = moveList[target][0] - AAA;
15802 fromY = moveList[target][1] - ONE;
15803 if (target == currentMove - 1) {
15804 AnimateMove(boards[currentMove], toX, toY, fromX, fromY);
15806 if (appData.highlightLastMove) {
15807 SetHighlights(fromX, fromY, toX, toY);
15811 if (gameMode == EditGame || gameMode==AnalyzeMode ||
15812 gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
15813 while (currentMove > target) {
15814 if(moveList[currentMove-1][1] == '@' && moveList[currentMove-1][0] == '@') {
15815 // null move cannot be undone. Reload program with move history before it.
15817 for(i=target; i>backwardMostMove; i--) { // seek back to start or previous null move
15818 if(moveList[i-1][1] == '@' && moveList[i-1][0] == '@') break;
15820 SendBoard(&first, i);
15821 if(second.analyzing) SendBoard(&second, i);
15822 for(currentMove=i; currentMove<target; currentMove++) {
15823 SendMoveToProgram(currentMove, &first);
15824 if(second.analyzing) SendMoveToProgram(currentMove, &second);
15828 SendToBoth("undo\n");
15832 currentMove = target;
15835 if (gameMode == EditGame || gameMode == EndOfGame) {
15836 whiteTimeRemaining = timeRemaining[0][currentMove];
15837 blackTimeRemaining = timeRemaining[1][currentMove];
15839 DisplayBothClocks();
15840 DisplayMove(currentMove - 1);
15841 DrawPosition(full_redraw, boards[currentMove]);
15842 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
15843 // [HGM] PV info: routine tests if comment empty
15844 DisplayComment(currentMove - 1, commentList[currentMove]);
15845 ClearMap(); // [HGM] exclude: invalidate map
15851 if (gameMode == IcsExamining && !pausing) {
15852 SendToICS(ics_prefix);
15853 SendToICS("backward\n");
15855 BackwardInner(currentMove - 1);
15862 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
15863 /* to optimize, we temporarily turn off analysis mode while we undo
15864 * all the moves. Otherwise we get analysis output after each undo.
15866 if (first.analysisSupport) {
15867 SendToProgram("exit\nforce\n", &first);
15868 first.analyzing = FALSE;
15872 if (gameMode == IcsExamining && !pausing) {
15873 SendToICS(ics_prefix);
15874 SendToICS("backward 999999\n");
15876 BackwardInner(backwardMostMove);
15879 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
15880 /* we have fed all the moves, so reactivate analysis mode */
15881 SendToProgram("analyze\n", &first);
15882 first.analyzing = TRUE;
15883 /*first.maybeThinking = TRUE;*/
15884 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
15891 if (gameMode == PlayFromGameFile && !pausing) PauseEvent();
15892 if (to >= forwardMostMove) to = forwardMostMove;
15893 if (to <= backwardMostMove) to = backwardMostMove;
15894 if (to < currentMove) {
15902 RevertEvent (Boolean annotate)
15904 if(PopTail(annotate)) { // [HGM] vari: restore old game tail
15907 if (gameMode != IcsExamining) {
15908 DisplayError(_("You are not examining a game"), 0);
15912 DisplayError(_("You can't revert while pausing"), 0);
15915 SendToICS(ics_prefix);
15916 SendToICS("revert\n");
15920 RetractMoveEvent ()
15922 switch (gameMode) {
15923 case MachinePlaysWhite:
15924 case MachinePlaysBlack:
15925 if (WhiteOnMove(forwardMostMove) == (gameMode == MachinePlaysWhite)) {
15926 DisplayError(_("Wait until your turn,\nor select 'Move Now'."), 0);
15929 if (forwardMostMove < 2) return;
15930 currentMove = forwardMostMove = forwardMostMove - 2;
15931 whiteTimeRemaining = timeRemaining[0][currentMove];
15932 blackTimeRemaining = timeRemaining[1][currentMove];
15933 DisplayBothClocks();
15934 DisplayMove(currentMove - 1);
15935 ClearHighlights();/*!! could figure this out*/
15936 DrawPosition(TRUE, boards[currentMove]); /* [AS] Changed to full redraw! */
15937 SendToProgram("remove\n", &first);
15938 /*first.maybeThinking = TRUE;*/ /* GNU Chess does not ponder here */
15941 case BeginningOfGame:
15945 case IcsPlayingWhite:
15946 case IcsPlayingBlack:
15947 if (WhiteOnMove(forwardMostMove) == (gameMode == IcsPlayingWhite)) {
15948 SendToICS(ics_prefix);
15949 SendToICS("takeback 2\n");
15951 SendToICS(ics_prefix);
15952 SendToICS("takeback 1\n");
15961 ChessProgramState *cps;
15963 switch (gameMode) {
15964 case MachinePlaysWhite:
15965 if (!WhiteOnMove(forwardMostMove)) {
15966 DisplayError(_("It is your turn"), 0);
15971 case MachinePlaysBlack:
15972 if (WhiteOnMove(forwardMostMove)) {
15973 DisplayError(_("It is your turn"), 0);
15978 case TwoMachinesPlay:
15979 if (WhiteOnMove(forwardMostMove) ==
15980 (first.twoMachinesColor[0] == 'w')) {
15986 case BeginningOfGame:
15990 SendToProgram("?\n", cps);
15994 TruncateGameEvent ()
15997 if (gameMode != EditGame) return;
16004 CleanupTail(); // [HGM] vari: only keep current variation if we explicitly truncate
16005 if (forwardMostMove > currentMove) {
16006 if (gameInfo.resultDetails != NULL) {
16007 free(gameInfo.resultDetails);
16008 gameInfo.resultDetails = NULL;
16009 gameInfo.result = GameUnfinished;
16011 forwardMostMove = currentMove;
16012 HistorySet(parseList, backwardMostMove, forwardMostMove,
16020 if (appData.noChessProgram) return;
16021 switch (gameMode) {
16022 case MachinePlaysWhite:
16023 if (WhiteOnMove(forwardMostMove)) {
16024 DisplayError(_("Wait until your turn."), 0);
16028 case BeginningOfGame:
16029 case MachinePlaysBlack:
16030 if (!WhiteOnMove(forwardMostMove)) {
16031 DisplayError(_("Wait until your turn."), 0);
16036 DisplayError(_("No hint available"), 0);
16039 SendToProgram("hint\n", &first);
16040 hintRequested = TRUE;
16044 SaveSelected (FILE *g, int dummy, char *dummy2)
16046 ListGame * lg = (ListGame *) gameList.head;
16050 if( !(f = GameFile()) || ((ListGame *) gameList.tailPred)->number <= 0 ) {
16051 DisplayError(_("Game list not loaded or empty"), 0);
16055 creatingBook = TRUE; // suppresses stuff during load game
16057 /* Get list size */
16058 for (nItem = 1; nItem <= ((ListGame *) gameList.tailPred)->number; nItem++){
16059 if(lg->position >= 0) { // selected?
16060 LoadGame(f, nItem, "", TRUE);
16061 SaveGamePGN2(g); // leaves g open
16064 lg = (ListGame *) lg->node.succ;
16068 creatingBook = FALSE;
16076 ListGame * lg = (ListGame *) gameList.head;
16079 static int secondTime = FALSE;
16081 if( !(f = GameFile()) || ((ListGame *) gameList.tailPred)->number <= 0 ) {
16082 DisplayError(_("Game list not loaded or empty"), 0);
16086 if(!secondTime && (g = fopen(appData.polyglotBook, "r"))) {
16089 DisplayNote(_("Book file exists! Try again for overwrite."));
16093 creatingBook = TRUE;
16094 secondTime = FALSE;
16096 /* Get list size */
16097 for (nItem = 1; nItem <= ((ListGame *) gameList.tailPred)->number; nItem++){
16098 if(lg->position >= 0) {
16099 LoadGame(f, nItem, "", TRUE);
16100 AddGameToBook(TRUE);
16103 lg = (ListGame *) lg->node.succ;
16106 creatingBook = FALSE;
16113 if (appData.noChessProgram) return;
16114 switch (gameMode) {
16115 case MachinePlaysWhite:
16116 if (WhiteOnMove(forwardMostMove)) {
16117 DisplayError(_("Wait until your turn."), 0);
16121 case BeginningOfGame:
16122 case MachinePlaysBlack:
16123 if (!WhiteOnMove(forwardMostMove)) {
16124 DisplayError(_("Wait until your turn."), 0);
16129 EditPositionDone(TRUE);
16131 case TwoMachinesPlay:
16136 SendToProgram("bk\n", &first);
16137 bookOutput[0] = NULLCHAR;
16138 bookRequested = TRUE;
16144 char *tags = PGNTags(&gameInfo);
16145 TagsPopUp(tags, CmailMsg());
16149 /* end button procedures */
16152 PrintPosition (FILE *fp, int move)
16156 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
16157 for (j = BOARD_LEFT; j < BOARD_RGHT; j++) {
16158 char c = PieceToChar(boards[move][i][j]);
16159 fputc(c == 'x' ? '.' : c, fp);
16160 fputc(j == BOARD_RGHT - 1 ? '\n' : ' ', fp);
16163 if ((gameMode == EditPosition) ? !blackPlaysFirst : (move % 2 == 0))
16164 fprintf(fp, "white to play\n");
16166 fprintf(fp, "black to play\n");
16170 PrintOpponents (FILE *fp)
16172 if (gameInfo.white != NULL) {
16173 fprintf(fp, "\t%s vs. %s\n", gameInfo.white, gameInfo.black);
16179 /* Find last component of program's own name, using some heuristics */
16181 TidyProgramName (char *prog, char *host, char buf[MSG_SIZ])
16184 int local = (strcmp(host, "localhost") == 0);
16185 while (!local && (p = strchr(prog, ';')) != NULL) {
16187 while (*p == ' ') p++;
16190 if (*prog == '"' || *prog == '\'') {
16191 q = strchr(prog + 1, *prog);
16193 q = strchr(prog, ' ');
16195 if (q == NULL) q = prog + strlen(prog);
16197 while (p >= prog && *p != '/' && *p != '\\') p--;
16199 if(p == prog && *p == '"') p++;
16201 if (q - p >= 4 && StrCaseCmp(q - 4, ".exe") == 0) *q = c, q -= 4; else *q = c;
16202 memcpy(buf, p, q - p);
16203 buf[q - p] = NULLCHAR;
16211 TimeControlTagValue ()
16214 if (!appData.clockMode) {
16215 safeStrCpy(buf, "-", sizeof(buf)/sizeof(buf[0]));
16216 } else if (movesPerSession > 0) {
16217 snprintf(buf, MSG_SIZ, "%d/%ld", movesPerSession, timeControl/1000);
16218 } else if (timeIncrement == 0) {
16219 snprintf(buf, MSG_SIZ, "%ld", timeControl/1000);
16221 snprintf(buf, MSG_SIZ, "%ld+%ld", timeControl/1000, timeIncrement/1000);
16223 return StrSave(buf);
16229 /* This routine is used only for certain modes */
16230 VariantClass v = gameInfo.variant;
16231 ChessMove r = GameUnfinished;
16234 if(keepInfo) return;
16236 if(gameMode == EditGame) { // [HGM] vari: do not erase result on EditGame
16237 r = gameInfo.result;
16238 p = gameInfo.resultDetails;
16239 gameInfo.resultDetails = NULL;
16241 ClearGameInfo(&gameInfo);
16242 gameInfo.variant = v;
16244 switch (gameMode) {
16245 case MachinePlaysWhite:
16246 gameInfo.event = StrSave( appData.pgnEventHeader );
16247 gameInfo.site = StrSave(HostName());
16248 gameInfo.date = PGNDate();
16249 gameInfo.round = StrSave("-");
16250 gameInfo.white = StrSave(first.tidy);
16251 gameInfo.black = StrSave(UserName());
16252 gameInfo.timeControl = TimeControlTagValue();
16255 case MachinePlaysBlack:
16256 gameInfo.event = StrSave( appData.pgnEventHeader );
16257 gameInfo.site = StrSave(HostName());
16258 gameInfo.date = PGNDate();
16259 gameInfo.round = StrSave("-");
16260 gameInfo.white = StrSave(UserName());
16261 gameInfo.black = StrSave(first.tidy);
16262 gameInfo.timeControl = TimeControlTagValue();
16265 case TwoMachinesPlay:
16266 gameInfo.event = StrSave( appData.pgnEventHeader );
16267 gameInfo.site = StrSave(HostName());
16268 gameInfo.date = PGNDate();
16271 snprintf(buf, MSG_SIZ, "%d", roundNr);
16272 gameInfo.round = StrSave(buf);
16274 gameInfo.round = StrSave("-");
16276 if (first.twoMachinesColor[0] == 'w') {
16277 gameInfo.white = StrSave(appData.pgnName[0][0] ? appData.pgnName[0] : first.tidy);
16278 gameInfo.black = StrSave(appData.pgnName[1][0] ? appData.pgnName[1] : second.tidy);
16280 gameInfo.white = StrSave(appData.pgnName[1][0] ? appData.pgnName[1] : second.tidy);
16281 gameInfo.black = StrSave(appData.pgnName[0][0] ? appData.pgnName[0] : first.tidy);
16283 gameInfo.timeControl = TimeControlTagValue();
16287 gameInfo.event = StrSave("Edited game");
16288 gameInfo.site = StrSave(HostName());
16289 gameInfo.date = PGNDate();
16290 gameInfo.round = StrSave("-");
16291 gameInfo.white = StrSave("-");
16292 gameInfo.black = StrSave("-");
16293 gameInfo.result = r;
16294 gameInfo.resultDetails = p;
16298 gameInfo.event = StrSave("Edited position");
16299 gameInfo.site = StrSave(HostName());
16300 gameInfo.date = PGNDate();
16301 gameInfo.round = StrSave("-");
16302 gameInfo.white = StrSave("-");
16303 gameInfo.black = StrSave("-");
16306 case IcsPlayingWhite:
16307 case IcsPlayingBlack:
16312 case PlayFromGameFile:
16313 gameInfo.event = StrSave("Game from non-PGN file");
16314 gameInfo.site = StrSave(HostName());
16315 gameInfo.date = PGNDate();
16316 gameInfo.round = StrSave("-");
16317 gameInfo.white = StrSave("?");
16318 gameInfo.black = StrSave("?");
16327 ReplaceComment (int index, char *text)
16333 if(index && sscanf(text, "%f/%d", &score, &len) == 2 &&
16334 pvInfoList[index-1].depth == len &&
16335 fabs(pvInfoList[index-1].score - score*100.) < 0.5 &&
16336 (p = strchr(text, '\n'))) text = p; // [HGM] strip off first line with PV info, if any
16337 while (*text == '\n') text++;
16338 len = strlen(text);
16339 while (len > 0 && text[len - 1] == '\n') len--;
16341 if (commentList[index] != NULL)
16342 free(commentList[index]);
16345 commentList[index] = NULL;
16348 if( *text == '{' && strchr(text, '}') || // [HGM] braces: if certainy malformed, put braces
16349 *text == '[' && strchr(text, ']') || // otherwise hope the user knows what he is doing
16350 *text == '(' && strchr(text, ')')) { // (perhaps check if this parses as comment-only?)
16351 commentList[index] = (char *) malloc(len + 2);
16352 strncpy(commentList[index], text, len);
16353 commentList[index][len] = '\n';
16354 commentList[index][len + 1] = NULLCHAR;
16356 // [HGM] braces: if text does not start with known OK delimiter, put braces around it.
16358 commentList[index] = (char *) malloc(len + 7);
16359 safeStrCpy(commentList[index], "{\n", 3);
16360 safeStrCpy(commentList[index]+2, text, len+1);
16361 commentList[index][len+2] = NULLCHAR;
16362 while(p = strchr(commentList[index], '}')) *p = ')'; // kill all } to make it one comment
16363 strcat(commentList[index], "\n}\n");
16368 CrushCRs (char *text)
16376 if (ch == '\r') continue;
16378 } while (ch != '\0');
16382 AppendComment (int index, char *text, Boolean addBraces)
16383 /* addBraces tells if we should add {} */
16388 if(appData.debugMode) fprintf(debugFP, "Append: in='%s' %d\n", text, addBraces);
16389 if(addBraces == 3) addBraces = 0; else // force appending literally
16390 text = GetInfoFromComment( index, text ); /* [HGM] PV time: strip PV info from comment */
16393 while (*text == '\n') text++;
16394 len = strlen(text);
16395 while (len > 0 && text[len - 1] == '\n') len--;
16396 text[len] = NULLCHAR;
16398 if (len == 0) return;
16400 if (commentList[index] != NULL) {
16401 Boolean addClosingBrace = addBraces;
16402 old = commentList[index];
16403 oldlen = strlen(old);
16404 while(commentList[index][oldlen-1] == '\n')
16405 commentList[index][--oldlen] = NULLCHAR;
16406 commentList[index] = (char *) malloc(oldlen + len + 6); // might waste 4
16407 safeStrCpy(commentList[index], old, oldlen + len + 6);
16409 // [HGM] braces: join "{A\n}\n" + "{\nB}" as "{A\nB\n}"
16410 if(commentList[index][oldlen-1] == '}' && (text[0] == '{' || addBraces == TRUE)) {
16411 if(addBraces == TRUE) addBraces = FALSE; else { text++; len--; }
16412 while (*text == '\n') { text++; len--; }
16413 commentList[index][--oldlen] = NULLCHAR;
16415 if(addBraces) strcat(commentList[index], addBraces == 2 ? "\n(" : "\n{\n");
16416 else strcat(commentList[index], "\n");
16417 strcat(commentList[index], text);
16418 if(addClosingBrace) strcat(commentList[index], addClosingBrace == 2 ? ")\n" : "\n}\n");
16419 else strcat(commentList[index], "\n");
16421 commentList[index] = (char *) malloc(len + 6); // perhaps wastes 4...
16423 safeStrCpy(commentList[index], addBraces == 2 ? "(" : "{\n", 3);
16424 else commentList[index][0] = NULLCHAR;
16425 strcat(commentList[index], text);
16426 strcat(commentList[index], addBraces == 2 ? ")\n" : "\n");
16427 if(addBraces == TRUE) strcat(commentList[index], "}\n");
16432 FindStr (char * text, char * sub_text)
16434 char * result = strstr( text, sub_text );
16436 if( result != NULL ) {
16437 result += strlen( sub_text );
16443 /* [AS] Try to extract PV info from PGN comment */
16444 /* [HGM] PV time: and then remove it, to prevent it appearing twice */
16446 GetInfoFromComment (int index, char * text)
16448 char * sep = text, *p;
16450 if( text != NULL && index > 0 ) {
16453 int time = -1, sec = 0, deci;
16454 char * s_eval = FindStr( text, "[%eval " );
16455 char * s_emt = FindStr( text, "[%emt " );
16457 if( s_eval != NULL || s_emt != NULL ) {
16459 if(0) { // [HGM] this code is not finished, and could actually be detrimental
16464 if( s_eval != NULL ) {
16465 if( sscanf( s_eval, "%d,%d%c", &score, &depth, &delim ) != 3 ) {
16469 if( delim != ']' ) {
16474 if( s_emt != NULL ) {
16479 /* We expect something like: [+|-]nnn.nn/dd */
16482 if(*text != '{') return text; // [HGM] braces: must be normal comment
16484 sep = strchr( text, '/' );
16485 if( sep == NULL || sep < (text+4) ) {
16490 if(!strncmp(p+1, "final score ", 12)) p += 12, index++; else
16491 if(p[1] == '(') { // comment starts with PV
16492 p = strchr(p, ')'); // locate end of PV
16493 if(p == NULL || sep < p+5) return text;
16494 // at this point we have something like "{(.*) +0.23/6 ..."
16495 p = text; while(*++p != ')') p[-1] = *p; p[-1] = ')';
16496 *p = '\n'; while(*p == ' ' || *p == '\n') p++; *--p = '{';
16497 // we now moved the brace to behind the PV: "(.*) {+0.23/6 ..."
16499 time = -1; sec = -1; deci = -1;
16500 if( sscanf( p+1, "%d.%d/%d %d:%d", &score, &score_lo, &depth, &time, &sec ) != 5 &&
16501 sscanf( p+1, "%d.%d/%d %d.%d", &score, &score_lo, &depth, &time, &deci ) != 5 &&
16502 sscanf( p+1, "%d.%d/%d %d", &score, &score_lo, &depth, &time ) != 4 &&
16503 sscanf( p+1, "%d.%d/%d", &score, &score_lo, &depth ) != 3 ) {
16507 if( score_lo < 0 || score_lo >= 100 ) {
16511 if(sec >= 0) time = 600*time + 10*sec; else
16512 if(deci >= 0) time = 10*time + deci; else time *= 10; // deci-sec
16514 score = score > 0 || !score & p[1] != '-' ? score*100 + score_lo : score*100 - score_lo;
16516 /* [HGM] PV time: now locate end of PV info */
16517 while( *++sep >= '0' && *sep <= '9'); // strip depth
16519 while( *++sep >= '0' && *sep <= '9' || *sep == '\n'); // strip time
16521 while( *++sep >= '0' && *sep <= '9'); // strip seconds
16523 while( *++sep >= '0' && *sep <= '9'); // strip fractional seconds
16524 while(*sep == ' ' || *sep == '\n' || *sep == '\r') sep++;
16535 pvInfoList[index-1].depth = depth;
16536 pvInfoList[index-1].score = score;
16537 pvInfoList[index-1].time = 10*time; // centi-sec
16538 if(*sep == '}') *sep = 0; else *--sep = '{';
16539 if(p != text) { while(*p++ = *sep++); sep = text; } // squeeze out space between PV and comment, and return both
16545 SendToProgram (char *message, ChessProgramState *cps)
16547 int count, outCount, error;
16550 if (cps->pr == NoProc) return;
16553 if (appData.debugMode) {
16556 fprintf(debugFP, "%ld >%-6s: %s",
16557 SubtractTimeMarks(&now, &programStartTime),
16558 cps->which, message);
16560 fprintf(serverFP, "%ld >%-6s: %s",
16561 SubtractTimeMarks(&now, &programStartTime),
16562 cps->which, message), fflush(serverFP);
16565 count = strlen(message);
16566 outCount = OutputToProcess(cps->pr, message, count, &error);
16567 if (outCount < count && !exiting
16568 && !endingGame) { /* [HGM] crash: to not hang GameEnds() writing to deceased engines */
16569 if(!cps->initDone) return; // [HGM] should not generate fatal error during engine load
16570 snprintf(buf, MSG_SIZ, _("Error writing to %s chess program"), _(cps->which));
16571 if(gameInfo.resultDetails==NULL) { /* [HGM] crash: if game in progress, give reason for abort */
16572 if((signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
16573 snprintf(buf, MSG_SIZ, _("%s program exits in draw position (%s)"), _(cps->which), cps->program);
16574 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(GameIsDrawn, buf, GE_XBOARD); return; }
16575 gameInfo.result = GameIsDrawn; /* [HGM] accept exit as draw claim */
16577 ChessMove res = cps->twoMachinesColor[0]=='w' ? BlackWins : WhiteWins;
16578 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(res, buf, GE_XBOARD); return; }
16579 gameInfo.result = res;
16581 gameInfo.resultDetails = StrSave(buf);
16583 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; return; }
16584 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, error, 1); else errorExitStatus = 1;
16589 ReceiveFromProgram (InputSourceRef isr, VOIDSTAR closure, char *message, int count, int error)
16593 ChessProgramState *cps = (ChessProgramState *)closure;
16595 if (isr != cps->isr) return; /* Killed intentionally */
16598 RemoveInputSource(cps->isr);
16599 snprintf(buf, MSG_SIZ, _("Error: %s chess program (%s) exited unexpectedly"),
16600 _(cps->which), cps->program);
16601 if(LoadError(cps->userError ? NULL : buf, cps)) return; // [HGM] should not generate fatal error during engine load
16602 if(gameInfo.resultDetails==NULL) { /* [HGM] crash: if game in progress, give reason for abort */
16603 if((signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
16604 snprintf(buf, MSG_SIZ, _("%s program exits in draw position (%s)"), _(cps->which), cps->program);
16605 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(GameIsDrawn, buf, GE_XBOARD); return; }
16606 gameInfo.result = GameIsDrawn; /* [HGM] accept exit as draw claim */
16608 ChessMove res = cps->twoMachinesColor[0]=='w' ? BlackWins : WhiteWins;
16609 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(res, buf, GE_XBOARD); return; }
16610 gameInfo.result = res;
16612 gameInfo.resultDetails = StrSave(buf);
16614 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; return; }
16615 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, 0, 1); else errorExitStatus = 1;
16617 snprintf(buf, MSG_SIZ, _("Error reading from %s chess program (%s)"),
16618 _(cps->which), cps->program);
16619 RemoveInputSource(cps->isr);
16621 /* [AS] Program is misbehaving badly... kill it */
16622 if( count == -2 ) {
16623 DestroyChildProcess( cps->pr, 9 );
16627 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, error, 1); else errorExitStatus = 1;
16632 if ((end_str = strchr(message, '\r')) != NULL)
16633 *end_str = NULLCHAR;
16634 if ((end_str = strchr(message, '\n')) != NULL)
16635 *end_str = NULLCHAR;
16637 if (appData.debugMode) {
16638 TimeMark now; int print = 1;
16639 char *quote = ""; char c; int i;
16641 if(appData.engineComments != 1) { /* [HGM] debug: decide if protocol-violating output is written */
16642 char start = message[0];
16643 if(start >='A' && start <= 'Z') start += 'a' - 'A'; // be tolerant to capitalizing
16644 if(sscanf(message, "%d%c%d%d%d", &i, &c, &i, &i, &i) != 5 &&
16645 sscanf(message, "move %c", &c)!=1 && sscanf(message, "offer%c", &c)!=1 &&
16646 sscanf(message, "resign%c", &c)!=1 && sscanf(message, "feature %c", &c)!=1 &&
16647 sscanf(message, "error %c", &c)!=1 && sscanf(message, "illegal %c", &c)!=1 &&
16648 sscanf(message, "tell%c", &c)!=1 && sscanf(message, "0-1 %c", &c)!=1 &&
16649 sscanf(message, "1-0 %c", &c)!=1 && sscanf(message, "1/2-1/2 %c", &c)!=1 &&
16650 sscanf(message, "setboard %c", &c)!=1 && sscanf(message, "setup %c", &c)!=1 &&
16651 sscanf(message, "hint: %c", &c)!=1 &&
16652 sscanf(message, "pong %c", &c)!=1 && start != '#') {
16653 quote = appData.engineComments == 2 ? "# " : "### NON-COMPLIANT! ### ";
16654 print = (appData.engineComments >= 2);
16656 message[0] = start; // restore original message
16660 fprintf(debugFP, "%ld <%-6s: %s%s\n",
16661 SubtractTimeMarks(&now, &programStartTime), cps->which,
16665 fprintf(serverFP, "%ld <%-6s: %s%s\n",
16666 SubtractTimeMarks(&now, &programStartTime), cps->which,
16668 message), fflush(serverFP);
16672 /* [DM] if icsEngineAnalyze is active we block all whisper and kibitz output, because nobody want to see this */
16673 if (appData.icsEngineAnalyze) {
16674 if (strstr(message, "whisper") != NULL ||
16675 strstr(message, "kibitz") != NULL ||
16676 strstr(message, "tellics") != NULL) return;
16679 HandleMachineMove(message, cps);
16684 SendTimeControl (ChessProgramState *cps, int mps, long tc, int inc, int sd, int st)
16689 if( timeControl_2 > 0 ) {
16690 if( (gameMode == MachinePlaysBlack) || (gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b') ) {
16691 tc = timeControl_2;
16694 tc /= cps->timeOdds; /* [HGM] time odds: apply before telling engine */
16695 inc /= cps->timeOdds;
16696 st /= cps->timeOdds;
16698 seconds = (tc / 1000) % 60; /* [HGM] displaced to after applying odds */
16701 /* Set exact time per move, normally using st command */
16702 if (cps->stKludge) {
16703 /* GNU Chess 4 has no st command; uses level in a nonstandard way */
16705 if (seconds == 0) {
16706 snprintf(buf, MSG_SIZ, "level 1 %d\n", st/60);
16708 snprintf(buf, MSG_SIZ, "level 1 %d:%02d\n", st/60, seconds);
16711 snprintf(buf, MSG_SIZ, "st %d\n", st);
16714 /* Set conventional or incremental time control, using level command */
16715 if (seconds == 0) {
16716 /* Note old gnuchess bug -- minutes:seconds used to not work.
16717 Fixed in later versions, but still avoid :seconds
16718 when seconds is 0. */
16719 snprintf(buf, MSG_SIZ, "level %d %ld %g\n", mps, tc/60000, inc/1000.);
16721 snprintf(buf, MSG_SIZ, "level %d %ld:%02d %g\n", mps, tc/60000,
16722 seconds, inc/1000.);
16725 SendToProgram(buf, cps);
16727 /* Orthoganally (except for GNU Chess 4), limit time to st seconds */
16728 /* Orthogonally, limit search to given depth */
16730 if (cps->sdKludge) {
16731 snprintf(buf, MSG_SIZ, "depth\n%d\n", sd);
16733 snprintf(buf, MSG_SIZ, "sd %d\n", sd);
16735 SendToProgram(buf, cps);
16738 if(cps->nps >= 0) { /* [HGM] nps */
16739 if(cps->supportsNPS == FALSE)
16740 cps->nps = -1; // don't use if engine explicitly says not supported!
16742 snprintf(buf, MSG_SIZ, "nps %d\n", cps->nps);
16743 SendToProgram(buf, cps);
16748 ChessProgramState *
16750 /* [HGM] return pointer to 'first' or 'second', depending on who plays white */
16752 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b' ||
16753 gameMode == BeginningOfGame || gameMode == MachinePlaysBlack)
16759 SendTimeRemaining (ChessProgramState *cps, int machineWhite)
16761 char message[MSG_SIZ];
16764 /* Note: this routine must be called when the clocks are stopped
16765 or when they have *just* been set or switched; otherwise
16766 it will be off by the time since the current tick started.
16768 if (machineWhite) {
16769 time = whiteTimeRemaining / 10;
16770 otime = blackTimeRemaining / 10;
16772 time = blackTimeRemaining / 10;
16773 otime = whiteTimeRemaining / 10;
16775 /* [HGM] translate opponent's time by time-odds factor */
16776 otime = (otime * cps->other->timeOdds) / cps->timeOdds;
16778 if (time <= 0) time = 1;
16779 if (otime <= 0) otime = 1;
16781 snprintf(message, MSG_SIZ, "time %ld\n", time);
16782 SendToProgram(message, cps);
16784 snprintf(message, MSG_SIZ, "otim %ld\n", otime);
16785 SendToProgram(message, cps);
16789 EngineDefinedVariant (ChessProgramState *cps, int n)
16790 { // return name of n-th unknown variant that engine supports
16791 static char buf[MSG_SIZ];
16792 char *p, *s = cps->variants;
16793 if(!s) return NULL;
16794 do { // parse string from variants feature
16796 p = strchr(s, ',');
16797 if(p) *p = NULLCHAR;
16798 v = StringToVariant(s);
16799 if(v == VariantNormal && strcmp(s, "normal") && !strstr(s, "_normal")) v = VariantUnknown; // garbage is recognized as normal
16800 if(v == VariantUnknown) { // non-standard variant in list of engine-supported variants
16801 if(!strcmp(s, "tenjiku") || !strcmp(s, "dai") || !strcmp(s, "dada") || // ignore Alien-Edition variants
16802 !strcmp(s, "maka") || !strcmp(s, "tai") || !strcmp(s, "kyoku") ||
16803 !strcmp(s, "checkers") || !strcmp(s, "go") || !strcmp(s, "reversi") ||
16804 !strcmp(s, "dark") || !strcmp(s, "alien") || !strcmp(s, "multi") || !strcmp(s, "amazons") ) n++;
16805 if(--n < 0) safeStrCpy(buf, s, MSG_SIZ);
16808 if(n < 0) return buf;
16814 BoolFeature (char **p, char *name, int *loc, ChessProgramState *cps)
16817 int len = strlen(name);
16820 if (strncmp((*p), name, len) == 0 && (*p)[len] == '=') {
16822 sscanf(*p, "%d", &val);
16824 while (**p && **p != ' ')
16826 snprintf(buf, MSG_SIZ, "accepted %s\n", name);
16827 SendToProgram(buf, cps);
16834 IntFeature (char **p, char *name, int *loc, ChessProgramState *cps)
16837 int len = strlen(name);
16838 if (strncmp((*p), name, len) == 0 && (*p)[len] == '=') {
16840 sscanf(*p, "%d", loc);
16841 while (**p && **p != ' ') (*p)++;
16842 snprintf(buf, MSG_SIZ, "accepted %s\n", name);
16843 SendToProgram(buf, cps);
16850 StringFeature (char **p, char *name, char **loc, ChessProgramState *cps)
16853 int len = strlen(name);
16854 if (strncmp((*p), name, len) == 0
16855 && (*p)[len] == '=' && (*p)[len+1] == '\"') {
16857 ASSIGN(*loc, *p); // kludge alert: assign rest of line just to be sure allocation is large enough so that sscanf below always fits
16858 sscanf(*p, "%[^\"]", *loc);
16859 while (**p && **p != '\"') (*p)++;
16860 if (**p == '\"') (*p)++;
16861 snprintf(buf, MSG_SIZ, "accepted %s\n", name);
16862 SendToProgram(buf, cps);
16869 ParseOption (Option *opt, ChessProgramState *cps)
16870 // [HGM] options: process the string that defines an engine option, and determine
16871 // name, type, default value, and allowed value range
16873 char *p, *q, buf[MSG_SIZ];
16874 int n, min = (-1)<<31, max = 1<<31, def;
16876 if(p = strstr(opt->name, " -spin ")) {
16877 if((n = sscanf(p, " -spin %d %d %d", &def, &min, &max)) < 3 ) return FALSE;
16878 if(max < min) max = min; // enforce consistency
16879 if(def < min) def = min;
16880 if(def > max) def = max;
16885 } else if((p = strstr(opt->name, " -slider "))) {
16886 // for now -slider is a synonym for -spin, to already provide compatibility with future polyglots
16887 if((n = sscanf(p, " -slider %d %d %d", &def, &min, &max)) < 3 ) return FALSE;
16888 if(max < min) max = min; // enforce consistency
16889 if(def < min) def = min;
16890 if(def > max) def = max;
16894 opt->type = Spin; // Slider;
16895 } else if((p = strstr(opt->name, " -string "))) {
16896 opt->textValue = p+9;
16897 opt->type = TextBox;
16898 } else if((p = strstr(opt->name, " -file "))) {
16899 // for now -file is a synonym for -string, to already provide compatibility with future polyglots
16900 opt->textValue = p+7;
16901 opt->type = FileName; // FileName;
16902 } else if((p = strstr(opt->name, " -path "))) {
16903 // for now -file is a synonym for -string, to already provide compatibility with future polyglots
16904 opt->textValue = p+7;
16905 opt->type = PathName; // PathName;
16906 } else if(p = strstr(opt->name, " -check ")) {
16907 if(sscanf(p, " -check %d", &def) < 1) return FALSE;
16908 opt->value = (def != 0);
16909 opt->type = CheckBox;
16910 } else if(p = strstr(opt->name, " -combo ")) {
16911 opt->textValue = (char*) (opt->choice = &cps->comboList[cps->comboCnt]); // cheat with pointer type
16912 cps->comboList[cps->comboCnt++] = q = p+8; // holds possible choices
16913 if(*q == '*') cps->comboList[cps->comboCnt-1]++;
16914 opt->value = n = 0;
16915 while(q = StrStr(q, " /// ")) {
16916 n++; *q = 0; // count choices, and null-terminate each of them
16918 if(*q == '*') { // remember default, which is marked with * prefix
16922 cps->comboList[cps->comboCnt++] = q;
16924 cps->comboList[cps->comboCnt++] = NULL;
16926 opt->type = ComboBox;
16927 } else if(p = strstr(opt->name, " -button")) {
16928 opt->type = Button;
16929 } else if(p = strstr(opt->name, " -save")) {
16930 opt->type = SaveButton;
16931 } else return FALSE;
16932 *p = 0; // terminate option name
16933 // now look if the command-line options define a setting for this engine option.
16934 if(cps->optionSettings && cps->optionSettings[0])
16935 p = strstr(cps->optionSettings, opt->name); else p = NULL;
16936 if(p && (p == cps->optionSettings || p[-1] == ',')) {
16937 snprintf(buf, MSG_SIZ, "option %s", p);
16938 if(p = strstr(buf, ",")) *p = 0;
16939 if(q = strchr(buf, '=')) switch(opt->type) {
16941 for(n=0; n<opt->max; n++)
16942 if(!strcmp(((char**)opt->textValue)[n], q+1)) opt->value = n;
16945 safeStrCpy(opt->textValue, q+1, MSG_SIZ - (opt->textValue - opt->name));
16949 opt->value = atoi(q+1);
16954 SendToProgram(buf, cps);
16960 FeatureDone (ChessProgramState *cps, int val)
16962 DelayedEventCallback cb = GetDelayedEvent();
16963 if ((cb == InitBackEnd3 && cps == &first) ||
16964 (cb == SettingsMenuIfReady && cps == &second) ||
16965 (cb == LoadEngine) ||
16966 (cb == TwoMachinesEventIfReady)) {
16967 CancelDelayedEvent();
16968 ScheduleDelayedEvent(cb, val ? 1 : 3600000);
16970 cps->initDone = val;
16971 if(val) cps->reload = FALSE;
16974 /* Parse feature command from engine */
16976 ParseFeatures (char *args, ChessProgramState *cps)
16984 while (*p == ' ') p++;
16985 if (*p == NULLCHAR) return;
16987 if (BoolFeature(&p, "setboard", &cps->useSetboard, cps)) continue;
16988 if (BoolFeature(&p, "xedit", &cps->extendedEdit, cps)) continue;
16989 if (BoolFeature(&p, "time", &cps->sendTime, cps)) continue;
16990 if (BoolFeature(&p, "draw", &cps->sendDrawOffers, cps)) continue;
16991 if (BoolFeature(&p, "sigint", &cps->useSigint, cps)) continue;
16992 if (BoolFeature(&p, "sigterm", &cps->useSigterm, cps)) continue;
16993 if (BoolFeature(&p, "reuse", &val, cps)) {
16994 /* Engine can disable reuse, but can't enable it if user said no */
16995 if (!val) cps->reuse = FALSE;
16998 if (BoolFeature(&p, "analyze", &cps->analysisSupport, cps)) continue;
16999 if (StringFeature(&p, "myname", &cps->tidy, cps)) {
17000 if (gameMode == TwoMachinesPlay) {
17001 DisplayTwoMachinesTitle();
17007 if (StringFeature(&p, "variants", &cps->variants, cps)) continue;
17008 if (BoolFeature(&p, "san", &cps->useSAN, cps)) continue;
17009 if (BoolFeature(&p, "ping", &cps->usePing, cps)) continue;
17010 if (BoolFeature(&p, "playother", &cps->usePlayother, cps)) continue;
17011 if (BoolFeature(&p, "colors", &cps->useColors, cps)) continue;
17012 if (BoolFeature(&p, "usermove", &cps->useUsermove, cps)) continue;
17013 if (BoolFeature(&p, "exclude", &cps->excludeMoves, cps)) continue;
17014 if (BoolFeature(&p, "ics", &cps->sendICS, cps)) continue;
17015 if (BoolFeature(&p, "name", &cps->sendName, cps)) continue;
17016 if (BoolFeature(&p, "pause", &cps->pause, cps)) continue; // [HGM] pause
17017 if (IntFeature(&p, "done", &val, cps)) {
17018 FeatureDone(cps, val);
17021 /* Added by Tord: */
17022 if (BoolFeature(&p, "fen960", &cps->useFEN960, cps)) continue;
17023 if (BoolFeature(&p, "oocastle", &cps->useOOCastle, cps)) continue;
17024 /* End of additions by Tord */
17026 /* [HGM] added features: */
17027 if (BoolFeature(&p, "highlight", &cps->highlight, cps)) continue;
17028 if (BoolFeature(&p, "debug", &cps->debug, cps)) continue;
17029 if (BoolFeature(&p, "nps", &cps->supportsNPS, cps)) continue;
17030 if (IntFeature(&p, "level", &cps->maxNrOfSessions, cps)) continue;
17031 if (BoolFeature(&p, "memory", &cps->memSize, cps)) continue;
17032 if (BoolFeature(&p, "smp", &cps->maxCores, cps)) continue;
17033 if (StringFeature(&p, "egt", &cps->egtFormats, cps)) continue;
17034 if (StringFeature(&p, "option", &q, cps)) { // read to freshly allocated temp buffer first
17035 if(cps->reload) { FREE(q); q = NULL; continue; } // we are reloading because of xreuse
17036 FREE(cps->option[cps->nrOptions].name);
17037 cps->option[cps->nrOptions].name = q; q = NULL;
17038 if(!ParseOption(&(cps->option[cps->nrOptions++]), cps)) { // [HGM] options: add option feature
17039 snprintf(buf, MSG_SIZ, "rejected option %s\n", cps->option[--cps->nrOptions].name);
17040 SendToProgram(buf, cps);
17043 if(cps->nrOptions >= MAX_OPTIONS) {
17045 snprintf(buf, MSG_SIZ, _("%s engine has too many options\n"), _(cps->which));
17046 DisplayError(buf, 0);
17050 /* End of additions by HGM */
17052 /* unknown feature: complain and skip */
17054 while (*q && *q != '=') q++;
17055 snprintf(buf, MSG_SIZ,"rejected %.*s\n", (int)(q-p), p);
17056 SendToProgram(buf, cps);
17062 while (*p && *p != '\"') p++;
17063 if (*p == '\"') p++;
17065 while (*p && *p != ' ') p++;
17073 PeriodicUpdatesEvent (int newState)
17075 if (newState == appData.periodicUpdates)
17078 appData.periodicUpdates=newState;
17080 /* Display type changes, so update it now */
17081 // DisplayAnalysis();
17083 /* Get the ball rolling again... */
17085 AnalysisPeriodicEvent(1);
17086 StartAnalysisClock();
17091 PonderNextMoveEvent (int newState)
17093 if (newState == appData.ponderNextMove) return;
17094 if (gameMode == EditPosition) EditPositionDone(TRUE);
17096 SendToProgram("hard\n", &first);
17097 if (gameMode == TwoMachinesPlay) {
17098 SendToProgram("hard\n", &second);
17101 SendToProgram("easy\n", &first);
17102 thinkOutput[0] = NULLCHAR;
17103 if (gameMode == TwoMachinesPlay) {
17104 SendToProgram("easy\n", &second);
17107 appData.ponderNextMove = newState;
17111 NewSettingEvent (int option, int *feature, char *command, int value)
17115 if (gameMode == EditPosition) EditPositionDone(TRUE);
17116 snprintf(buf, MSG_SIZ,"%s%s %d\n", (option ? "option ": ""), command, value);
17117 if(feature == NULL || *feature) SendToProgram(buf, &first);
17118 if (gameMode == TwoMachinesPlay) {
17119 if(feature == NULL || feature[(int*)&second - (int*)&first]) SendToProgram(buf, &second);
17124 ShowThinkingEvent ()
17125 // [HGM] thinking: this routine is now also called from "Options -> Engine..." popup
17127 static int oldState = 2; // kludge alert! Neither true nor fals, so first time oldState is always updated
17128 int newState = appData.showThinking
17129 // [HGM] thinking: other features now need thinking output as well
17130 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp();
17132 if (oldState == newState) return;
17133 oldState = newState;
17134 if (gameMode == EditPosition) EditPositionDone(TRUE);
17136 SendToProgram("post\n", &first);
17137 if (gameMode == TwoMachinesPlay) {
17138 SendToProgram("post\n", &second);
17141 SendToProgram("nopost\n", &first);
17142 thinkOutput[0] = NULLCHAR;
17143 if (gameMode == TwoMachinesPlay) {
17144 SendToProgram("nopost\n", &second);
17147 // appData.showThinking = newState; // [HGM] thinking: responsible option should already have be changed when calling this routine!
17151 AskQuestionEvent (char *title, char *question, char *replyPrefix, char *which)
17153 ProcRef pr = (which[0] == '1') ? first.pr : second.pr;
17154 if (pr == NoProc) return;
17155 AskQuestion(title, question, replyPrefix, pr);
17159 TypeInEvent (char firstChar)
17161 if ((gameMode == BeginningOfGame && !appData.icsActive) ||
17162 gameMode == MachinePlaysWhite || gameMode == MachinePlaysBlack ||
17163 gameMode == AnalyzeMode || gameMode == EditGame ||
17164 gameMode == EditPosition || gameMode == IcsExamining ||
17165 gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
17166 isdigit(firstChar) && // [HGM] movenum: allow typing in of move nr in 'passive' modes
17167 ( gameMode == AnalyzeFile || gameMode == PlayFromGameFile ||
17168 gameMode == IcsObserving || gameMode == TwoMachinesPlay ) ||
17169 gameMode == Training) PopUpMoveDialog(firstChar);
17173 TypeInDoneEvent (char *move)
17176 int n, fromX, fromY, toX, toY;
17178 ChessMove moveType;
17181 if(gameMode == EditPosition && ParseFEN(board, &n, move, TRUE) ) {
17182 EditPositionPasteFEN(move);
17185 // [HGM] movenum: allow move number to be typed in any mode
17186 if(sscanf(move, "%d", &n) == 1 && n != 0 ) {
17190 // undocumented kludge: allow command-line option to be typed in!
17191 // (potentially fatal, and does not implement the effect of the option.)
17192 // should only be used for options that are values on which future decisions will be made,
17193 // and definitely not on options that would be used during initialization.
17194 if(strstr(move, "!!! -") == move) {
17195 ParseArgsFromString(move+4);
17199 if (gameMode != EditGame && currentMove != forwardMostMove &&
17200 gameMode != Training) {
17201 DisplayMoveError(_("Displayed move is not current"));
17203 int ok = ParseOneMove(move, gameMode == EditPosition ? blackPlaysFirst : currentMove,
17204 &moveType, &fromX, &fromY, &toX, &toY, &promoChar);
17205 if(!ok && move[0] >= 'a') { move[0] += 'A' - 'a'; ok = 2; } // [HGM] try also capitalized
17206 if (ok==1 || ok && ParseOneMove(move, gameMode == EditPosition ? blackPlaysFirst : currentMove,
17207 &moveType, &fromX, &fromY, &toX, &toY, &promoChar)) {
17208 UserMoveEvent(fromX, fromY, toX, toY, promoChar);
17210 DisplayMoveError(_("Could not parse move"));
17216 DisplayMove (int moveNumber)
17218 char message[MSG_SIZ];
17220 char cpThinkOutput[MSG_SIZ];
17222 if(appData.noGUI) return; // [HGM] fast: suppress display of moves
17224 if (moveNumber == forwardMostMove - 1 ||
17225 gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
17227 safeStrCpy(cpThinkOutput, thinkOutput, sizeof(cpThinkOutput)/sizeof(cpThinkOutput[0]));
17229 if (strchr(cpThinkOutput, '\n')) {
17230 *strchr(cpThinkOutput, '\n') = NULLCHAR;
17233 *cpThinkOutput = NULLCHAR;
17236 /* [AS] Hide thinking from human user */
17237 if( appData.hideThinkingFromHuman && gameMode != TwoMachinesPlay ) {
17238 *cpThinkOutput = NULLCHAR;
17239 if( thinkOutput[0] != NULLCHAR ) {
17242 for( i=0; i<=hiddenThinkOutputState; i++ ) {
17243 cpThinkOutput[i] = '.';
17245 cpThinkOutput[i] = NULLCHAR;
17246 hiddenThinkOutputState = (hiddenThinkOutputState + 1) % 3;
17250 if (moveNumber == forwardMostMove - 1 &&
17251 gameInfo.resultDetails != NULL) {
17252 if (gameInfo.resultDetails[0] == NULLCHAR) {
17253 snprintf(res, MSG_SIZ, " %s", PGNResult(gameInfo.result));
17255 snprintf(res, MSG_SIZ, " {%s} %s",
17256 T_(gameInfo.resultDetails), PGNResult(gameInfo.result));
17262 if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
17263 DisplayMessage(res, cpThinkOutput);
17265 snprintf(message, MSG_SIZ, "%d.%s%s%s", moveNumber / 2 + 1,
17266 WhiteOnMove(moveNumber) ? " " : ".. ",
17267 parseList[moveNumber], res);
17268 DisplayMessage(message, cpThinkOutput);
17273 DisplayComment (int moveNumber, char *text)
17275 char title[MSG_SIZ];
17277 if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
17278 safeStrCpy(title, "Comment", sizeof(title)/sizeof(title[0]));
17280 snprintf(title,MSG_SIZ, "Comment on %d.%s%s", moveNumber / 2 + 1,
17281 WhiteOnMove(moveNumber) ? " " : ".. ",
17282 parseList[moveNumber]);
17284 if (text != NULL && (appData.autoDisplayComment || commentUp))
17285 CommentPopUp(title, text);
17288 /* This routine sends a ^C interrupt to gnuchess, to awaken it if it
17289 * might be busy thinking or pondering. It can be omitted if your
17290 * gnuchess is configured to stop thinking immediately on any user
17291 * input. However, that gnuchess feature depends on the FIONREAD
17292 * ioctl, which does not work properly on some flavors of Unix.
17295 Attention (ChessProgramState *cps)
17298 if (!cps->useSigint) return;
17299 if (appData.noChessProgram || (cps->pr == NoProc)) return;
17300 switch (gameMode) {
17301 case MachinePlaysWhite:
17302 case MachinePlaysBlack:
17303 case TwoMachinesPlay:
17304 case IcsPlayingWhite:
17305 case IcsPlayingBlack:
17308 /* Skip if we know it isn't thinking */
17309 if (!cps->maybeThinking) return;
17310 if (appData.debugMode)
17311 fprintf(debugFP, "Interrupting %s\n", cps->which);
17312 InterruptChildProcess(cps->pr);
17313 cps->maybeThinking = FALSE;
17318 #endif /*ATTENTION*/
17324 if (whiteTimeRemaining <= 0) {
17327 if (appData.icsActive) {
17328 if (appData.autoCallFlag &&
17329 gameMode == IcsPlayingBlack && !blackFlag) {
17330 SendToICS(ics_prefix);
17331 SendToICS("flag\n");
17335 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Both flags fell"));
17337 if(gameMode != TwoMachinesPlay) DisplayTitle(_("White's flag fell"));
17338 if (appData.autoCallFlag) {
17339 GameEnds(BlackWins, "Black wins on time", GE_XBOARD);
17346 if (blackTimeRemaining <= 0) {
17349 if (appData.icsActive) {
17350 if (appData.autoCallFlag &&
17351 gameMode == IcsPlayingWhite && !whiteFlag) {
17352 SendToICS(ics_prefix);
17353 SendToICS("flag\n");
17357 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Both flags fell"));
17359 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Black's flag fell"));
17360 if (appData.autoCallFlag) {
17361 GameEnds(WhiteWins, "White wins on time", GE_XBOARD);
17372 CheckTimeControl ()
17374 if (!appData.clockMode || appData.icsActive || searchTime || // [HGM] st: no inc in st mode
17375 gameMode == PlayFromGameFile || forwardMostMove == 0) return;
17378 * add time to clocks when time control is achieved ([HGM] now also used for increment)
17380 if ( !WhiteOnMove(forwardMostMove) ) {
17381 /* White made time control */
17382 lastWhite -= whiteTimeRemaining; // [HGM] contains start time, socalculate thinking time
17383 whiteTimeRemaining += GetTimeQuota((forwardMostMove-whiteStartMove-1)/2, lastWhite, whiteTC)
17384 /* [HGM] time odds: correct new time quota for time odds! */
17385 / WhitePlayer()->timeOdds;
17386 lastBlack = blackTimeRemaining; // [HGM] leave absolute time (after quota), so next switch we can us it to calculate thinking time
17388 lastBlack -= blackTimeRemaining;
17389 /* Black made time control */
17390 blackTimeRemaining += GetTimeQuota((forwardMostMove-blackStartMove-1)/2, lastBlack, blackTC)
17391 / WhitePlayer()->other->timeOdds;
17392 lastWhite = whiteTimeRemaining;
17397 DisplayBothClocks ()
17399 int wom = gameMode == EditPosition ?
17400 !blackPlaysFirst : WhiteOnMove(currentMove);
17401 DisplayWhiteClock(whiteTimeRemaining, wom);
17402 DisplayBlackClock(blackTimeRemaining, !wom);
17406 /* Timekeeping seems to be a portability nightmare. I think everyone
17407 has ftime(), but I'm really not sure, so I'm including some ifdefs
17408 to use other calls if you don't. Clocks will be less accurate if
17409 you have neither ftime nor gettimeofday.
17412 /* VS 2008 requires the #include outside of the function */
17413 #if !HAVE_GETTIMEOFDAY && HAVE_FTIME
17414 #include <sys/timeb.h>
17417 /* Get the current time as a TimeMark */
17419 GetTimeMark (TimeMark *tm)
17421 #if HAVE_GETTIMEOFDAY
17423 struct timeval timeVal;
17424 struct timezone timeZone;
17426 gettimeofday(&timeVal, &timeZone);
17427 tm->sec = (long) timeVal.tv_sec;
17428 tm->ms = (int) (timeVal.tv_usec / 1000L);
17430 #else /*!HAVE_GETTIMEOFDAY*/
17433 // include <sys/timeb.h> / moved to just above start of function
17434 struct timeb timeB;
17437 tm->sec = (long) timeB.time;
17438 tm->ms = (int) timeB.millitm;
17440 #else /*!HAVE_FTIME && !HAVE_GETTIMEOFDAY*/
17441 tm->sec = (long) time(NULL);
17447 /* Return the difference in milliseconds between two
17448 time marks. We assume the difference will fit in a long!
17451 SubtractTimeMarks (TimeMark *tm2, TimeMark *tm1)
17453 return 1000L*(tm2->sec - tm1->sec) +
17454 (long) (tm2->ms - tm1->ms);
17459 * Code to manage the game clocks.
17461 * In tournament play, black starts the clock and then white makes a move.
17462 * We give the human user a slight advantage if he is playing white---the
17463 * clocks don't run until he makes his first move, so it takes zero time.
17464 * Also, we don't account for network lag, so we could get out of sync
17465 * with GNU Chess's clock -- but then, referees are always right.
17468 static TimeMark tickStartTM;
17469 static long intendedTickLength;
17472 NextTickLength (long timeRemaining)
17474 long nominalTickLength, nextTickLength;
17476 if (timeRemaining > 0L && timeRemaining <= 10000L)
17477 nominalTickLength = 100L;
17479 nominalTickLength = 1000L;
17480 nextTickLength = timeRemaining % nominalTickLength;
17481 if (nextTickLength <= 0) nextTickLength += nominalTickLength;
17483 return nextTickLength;
17486 /* Adjust clock one minute up or down */
17488 AdjustClock (Boolean which, int dir)
17490 if(appData.autoCallFlag) { DisplayError(_("Clock adjustment not allowed in auto-flag mode"), 0); return; }
17491 if(which) blackTimeRemaining += 60000*dir;
17492 else whiteTimeRemaining += 60000*dir;
17493 DisplayBothClocks();
17494 adjustedClock = TRUE;
17497 /* Stop clocks and reset to a fresh time control */
17501 (void) StopClockTimer();
17502 if (appData.icsActive) {
17503 whiteTimeRemaining = blackTimeRemaining = 0;
17504 } else if (searchTime) {
17505 whiteTimeRemaining = 1000*searchTime / WhitePlayer()->timeOdds;
17506 blackTimeRemaining = 1000*searchTime / WhitePlayer()->other->timeOdds;
17507 } else { /* [HGM] correct new time quote for time odds */
17508 whiteTC = blackTC = fullTimeControlString;
17509 whiteTimeRemaining = GetTimeQuota(-1, 0, whiteTC) / WhitePlayer()->timeOdds;
17510 blackTimeRemaining = GetTimeQuota(-1, 0, blackTC) / WhitePlayer()->other->timeOdds;
17512 if (whiteFlag || blackFlag) {
17514 whiteFlag = blackFlag = FALSE;
17516 lastWhite = lastBlack = whiteStartMove = blackStartMove = 0;
17517 DisplayBothClocks();
17518 adjustedClock = FALSE;
17521 #define FUDGE 25 /* 25ms = 1/40 sec; should be plenty even for 50 Hz clocks */
17523 /* Decrement running clock by amount of time that has passed */
17527 long timeRemaining;
17528 long lastTickLength, fudge;
17531 if (!appData.clockMode) return;
17532 if (gameMode==AnalyzeMode || gameMode == AnalyzeFile) return;
17536 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
17538 /* Fudge if we woke up a little too soon */
17539 fudge = intendedTickLength - lastTickLength;
17540 if (fudge < 0 || fudge > FUDGE) fudge = 0;
17542 if (WhiteOnMove(forwardMostMove)) {
17543 if(whiteNPS >= 0) lastTickLength = 0;
17544 timeRemaining = whiteTimeRemaining -= lastTickLength;
17545 if(timeRemaining < 0 && !appData.icsActive) {
17546 GetTimeQuota((forwardMostMove-whiteStartMove-1)/2, 0, whiteTC); // sets suddenDeath & nextSession;
17547 if(suddenDeath) { // [HGM] if we run out of a non-last incremental session, go to the next
17548 whiteStartMove = forwardMostMove; whiteTC = nextSession;
17549 lastWhite= timeRemaining = whiteTimeRemaining += GetTimeQuota(-1, 0, whiteTC);
17552 DisplayWhiteClock(whiteTimeRemaining - fudge,
17553 WhiteOnMove(currentMove < forwardMostMove ? currentMove : forwardMostMove));
17555 if(blackNPS >= 0) lastTickLength = 0;
17556 timeRemaining = blackTimeRemaining -= lastTickLength;
17557 if(timeRemaining < 0 && !appData.icsActive) { // [HGM] if we run out of a non-last incremental session, go to the next
17558 GetTimeQuota((forwardMostMove-blackStartMove-1)/2, 0, blackTC);
17560 blackStartMove = forwardMostMove;
17561 lastBlack = timeRemaining = blackTimeRemaining += GetTimeQuota(-1, 0, blackTC=nextSession);
17564 DisplayBlackClock(blackTimeRemaining - fudge,
17565 !WhiteOnMove(currentMove < forwardMostMove ? currentMove : forwardMostMove));
17567 if (CheckFlags()) return;
17569 if(twoBoards) { // count down secondary board's clocks as well
17570 activePartnerTime -= lastTickLength;
17572 if(activePartner == 'W')
17573 DisplayWhiteClock(activePartnerTime, TRUE); // the counting clock is always the highlighted one!
17575 DisplayBlackClock(activePartnerTime, TRUE);
17580 intendedTickLength = NextTickLength(timeRemaining - fudge) + fudge;
17581 StartClockTimer(intendedTickLength);
17583 /* if the time remaining has fallen below the alarm threshold, sound the
17584 * alarm. if the alarm has sounded and (due to a takeback or time control
17585 * with increment) the time remaining has increased to a level above the
17586 * threshold, reset the alarm so it can sound again.
17589 if (appData.icsActive && appData.icsAlarm) {
17591 /* make sure we are dealing with the user's clock */
17592 if (!( ((gameMode == IcsPlayingWhite) && WhiteOnMove(currentMove)) ||
17593 ((gameMode == IcsPlayingBlack) && !WhiteOnMove(currentMove))
17596 if (alarmSounded && (timeRemaining > appData.icsAlarmTime)) {
17597 alarmSounded = FALSE;
17598 } else if (!alarmSounded && (timeRemaining <= appData.icsAlarmTime)) {
17600 alarmSounded = TRUE;
17606 /* A player has just moved, so stop the previously running
17607 clock and (if in clock mode) start the other one.
17608 We redisplay both clocks in case we're in ICS mode, because
17609 ICS gives us an update to both clocks after every move.
17610 Note that this routine is called *after* forwardMostMove
17611 is updated, so the last fractional tick must be subtracted
17612 from the color that is *not* on move now.
17615 SwitchClocks (int newMoveNr)
17617 long lastTickLength;
17619 int flagged = FALSE;
17623 if (StopClockTimer() && appData.clockMode) {
17624 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
17625 if (!WhiteOnMove(forwardMostMove)) {
17626 if(blackNPS >= 0) lastTickLength = 0;
17627 blackTimeRemaining -= lastTickLength;
17628 /* [HGM] PGNtime: save time for PGN file if engine did not give it */
17629 // if(pvInfoList[forwardMostMove].time == -1)
17630 pvInfoList[forwardMostMove].time = // use GUI time
17631 (timeRemaining[1][forwardMostMove-1] - blackTimeRemaining)/10;
17633 if(whiteNPS >= 0) lastTickLength = 0;
17634 whiteTimeRemaining -= lastTickLength;
17635 /* [HGM] PGNtime: save time for PGN file if engine did not give it */
17636 // if(pvInfoList[forwardMostMove].time == -1)
17637 pvInfoList[forwardMostMove].time =
17638 (timeRemaining[0][forwardMostMove-1] - whiteTimeRemaining)/10;
17640 flagged = CheckFlags();
17642 forwardMostMove = newMoveNr; // [HGM] race: change stm when no timer interrupt scheduled
17643 CheckTimeControl();
17645 if (flagged || !appData.clockMode) return;
17647 switch (gameMode) {
17648 case MachinePlaysBlack:
17649 case MachinePlaysWhite:
17650 case BeginningOfGame:
17651 if (pausing) return;
17655 case PlayFromGameFile:
17663 if (searchTime) { // [HGM] st: set clock of player that has to move to max time
17664 if(WhiteOnMove(forwardMostMove))
17665 whiteTimeRemaining = 1000*searchTime / WhitePlayer()->timeOdds;
17666 else blackTimeRemaining = 1000*searchTime / WhitePlayer()->other->timeOdds;
17670 intendedTickLength = NextTickLength(WhiteOnMove(forwardMostMove) ?
17671 whiteTimeRemaining : blackTimeRemaining);
17672 StartClockTimer(intendedTickLength);
17676 /* Stop both clocks */
17680 long lastTickLength;
17683 if (!StopClockTimer()) return;
17684 if (!appData.clockMode) return;
17688 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
17689 if (WhiteOnMove(forwardMostMove)) {
17690 if(whiteNPS >= 0) lastTickLength = 0;
17691 whiteTimeRemaining -= lastTickLength;
17692 DisplayWhiteClock(whiteTimeRemaining, WhiteOnMove(currentMove));
17694 if(blackNPS >= 0) lastTickLength = 0;
17695 blackTimeRemaining -= lastTickLength;
17696 DisplayBlackClock(blackTimeRemaining, !WhiteOnMove(currentMove));
17701 /* Start clock of player on move. Time may have been reset, so
17702 if clock is already running, stop and restart it. */
17706 (void) StopClockTimer(); /* in case it was running already */
17707 DisplayBothClocks();
17708 if (CheckFlags()) return;
17710 if (!appData.clockMode) return;
17711 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) return;
17713 GetTimeMark(&tickStartTM);
17714 intendedTickLength = NextTickLength(WhiteOnMove(forwardMostMove) ?
17715 whiteTimeRemaining : blackTimeRemaining);
17717 /* [HGM] nps: figure out nps factors, by determining which engine plays white and/or black once and for all */
17718 whiteNPS = blackNPS = -1;
17719 if(gameMode == MachinePlaysWhite || gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w'
17720 || appData.zippyPlay && gameMode == IcsPlayingBlack) // first (perhaps only) engine has white
17721 whiteNPS = first.nps;
17722 if(gameMode == MachinePlaysBlack || gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b'
17723 || appData.zippyPlay && gameMode == IcsPlayingWhite) // first (perhaps only) engine has black
17724 blackNPS = first.nps;
17725 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b') // second only used in Two-Machines mode
17726 whiteNPS = second.nps;
17727 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w')
17728 blackNPS = second.nps;
17729 if(appData.debugMode) fprintf(debugFP, "nps: w=%d, b=%d\n", whiteNPS, blackNPS);
17731 StartClockTimer(intendedTickLength);
17735 TimeString (long ms)
17737 long second, minute, hour, day;
17739 static char buf[32];
17741 if (ms > 0 && ms <= 9900) {
17742 /* convert milliseconds to tenths, rounding up */
17743 double tenths = floor( ((double)(ms + 99L)) / 100.00 );
17745 snprintf(buf,sizeof(buf)/sizeof(buf[0]), " %03.1f ", tenths/10.0);
17749 /* convert milliseconds to seconds, rounding up */
17750 /* use floating point to avoid strangeness of integer division
17751 with negative dividends on many machines */
17752 second = (long) floor(((double) (ms + 999L)) / 1000.0);
17759 day = second / (60 * 60 * 24);
17760 second = second % (60 * 60 * 24);
17761 hour = second / (60 * 60);
17762 second = second % (60 * 60);
17763 minute = second / 60;
17764 second = second % 60;
17767 snprintf(buf, sizeof(buf)/sizeof(buf[0]), " %s%ld:%02ld:%02ld:%02ld ",
17768 sign, day, hour, minute, second);
17770 snprintf(buf, sizeof(buf)/sizeof(buf[0]), " %s%ld:%02ld:%02ld ", sign, hour, minute, second);
17772 snprintf(buf, sizeof(buf)/sizeof(buf[0]), " %s%2ld:%02ld ", sign, minute, second);
17779 * This is necessary because some C libraries aren't ANSI C compliant yet.
17782 StrStr (char *string, char *match)
17786 length = strlen(match);
17788 for (i = strlen(string) - length; i >= 0; i--, string++)
17789 if (!strncmp(match, string, length))
17796 StrCaseStr (char *string, char *match)
17800 length = strlen(match);
17802 for (i = strlen(string) - length; i >= 0; i--, string++) {
17803 for (j = 0; j < length; j++) {
17804 if (ToLower(match[j]) != ToLower(string[j]))
17807 if (j == length) return string;
17815 StrCaseCmp (char *s1, char *s2)
17820 c1 = ToLower(*s1++);
17821 c2 = ToLower(*s2++);
17822 if (c1 > c2) return 1;
17823 if (c1 < c2) return -1;
17824 if (c1 == NULLCHAR) return 0;
17832 return isupper(c) ? tolower(c) : c;
17839 return islower(c) ? toupper(c) : c;
17841 #endif /* !_amigados */
17848 if ((ret = (char *) malloc(strlen(s) + 1)))
17850 safeStrCpy(ret, s, strlen(s)+1);
17856 StrSavePtr (char *s, char **savePtr)
17861 if ((*savePtr = (char *) malloc(strlen(s) + 1))) {
17862 safeStrCpy(*savePtr, s, strlen(s)+1);
17874 clock = time((time_t *)NULL);
17875 tm = localtime(&clock);
17876 snprintf(buf, MSG_SIZ, "%04d.%02d.%02d",
17877 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
17878 return StrSave(buf);
17883 PositionToFEN (int move, char *overrideCastling, int moveCounts)
17885 int i, j, fromX, fromY, toX, toY;
17892 whiteToPlay = (gameMode == EditPosition) ?
17893 !blackPlaysFirst : (move % 2 == 0);
17896 /* Piece placement data */
17897 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
17898 if(MSG_SIZ - (p - buf) < BOARD_RGHT - BOARD_LEFT + 20) { *p = 0; return StrSave(buf); }
17900 for (j = BOARD_LEFT; j < BOARD_RGHT; j++) {
17901 if (boards[move][i][j] == EmptySquare) {
17903 } else { ChessSquare piece = boards[move][i][j];
17904 if (emptycount > 0) {
17905 if(emptycount<10) /* [HGM] can be >= 10 */
17906 *p++ = '0' + emptycount;
17907 else { *p++ = '0' + emptycount/10; *p++ = '0' + emptycount%10; }
17910 if(PieceToChar(piece) == '+') {
17911 /* [HGM] write promoted pieces as '+<unpromoted>' (Shogi) */
17913 piece = (ChessSquare)(CHUDEMOTED piece);
17915 *p++ = (piece == DarkSquare ? '*' : PieceToChar(piece));
17916 if(*p = PieceSuffix(piece)) p++;
17918 /* [HGM] flag promoted pieces as '<promoted>~' (Crazyhouse) */
17919 p[-1] = PieceToChar((ChessSquare)(CHUDEMOTED piece));
17924 if (emptycount > 0) {
17925 if(emptycount<10) /* [HGM] can be >= 10 */
17926 *p++ = '0' + emptycount;
17927 else { *p++ = '0' + emptycount/10; *p++ = '0' + emptycount%10; }
17934 /* [HGM] print Crazyhouse or Shogi holdings */
17935 if( gameInfo.holdingsWidth ) {
17936 *(p-1) = '['; /* if we wanted to support BFEN, this could be '/' */
17938 for(i=0; i<gameInfo.holdingsSize; i++) { /* white holdings */
17939 piece = boards[move][i][BOARD_WIDTH-1];
17940 if( piece != EmptySquare )
17941 for(j=0; j<(int) boards[move][i][BOARD_WIDTH-2]; j++)
17942 *p++ = PieceToChar(piece);
17944 for(i=0; i<gameInfo.holdingsSize; i++) { /* black holdings */
17945 piece = boards[move][BOARD_HEIGHT-i-1][0];
17946 if( piece != EmptySquare )
17947 for(j=0; j<(int) boards[move][BOARD_HEIGHT-i-1][1]; j++)
17948 *p++ = PieceToChar(piece);
17951 if( q == p ) *p++ = '-';
17957 *p++ = whiteToPlay ? 'w' : 'b';
17960 if(q = overrideCastling) { // [HGM] FRC: override castling & e.p fields for non-compliant engines
17961 while(*p++ = *q++); if(q != overrideCastling+1) p[-1] = ' '; else --p;
17963 if(nrCastlingRights) {
17964 int handW=0, handB=0;
17965 if(gameInfo.variant == VariantSChess) { // for S-Chess, all virgin backrank pieces must be listed
17966 for(i=0; i<BOARD_HEIGHT; i++) handW += boards[move][i][BOARD_RGHT]; // count white held pieces
17967 for(i=0; i<BOARD_HEIGHT; i++) handB += boards[move][i][BOARD_LEFT-1]; // count black held pieces
17970 if(appData.fischerCastling) {
17971 if(handW) { // in shuffle S-Chess simply dump all virgin pieces
17972 for(i=BOARD_RGHT-1; i>=BOARD_LEFT; i--)
17973 if(boards[move][VIRGIN][i] & VIRGIN_W) *p++ = i + AAA + 'A' - 'a';
17975 /* [HGM] write directly from rights */
17976 if(boards[move][CASTLING][2] != NoRights &&
17977 boards[move][CASTLING][0] != NoRights )
17978 *p++ = boards[move][CASTLING][0] + AAA + 'A' - 'a';
17979 if(boards[move][CASTLING][2] != NoRights &&
17980 boards[move][CASTLING][1] != NoRights )
17981 *p++ = boards[move][CASTLING][1] + AAA + 'A' - 'a';
17984 for(i=BOARD_RGHT-1; i>=BOARD_LEFT; i--)
17985 if(boards[move][VIRGIN][i] & VIRGIN_B) *p++ = i + AAA;
17987 if(boards[move][CASTLING][5] != NoRights &&
17988 boards[move][CASTLING][3] != NoRights )
17989 *p++ = boards[move][CASTLING][3] + AAA;
17990 if(boards[move][CASTLING][5] != NoRights &&
17991 boards[move][CASTLING][4] != NoRights )
17992 *p++ = boards[move][CASTLING][4] + AAA;
17996 /* [HGM] write true castling rights */
17997 if( nrCastlingRights == 6 ) {
17999 if(boards[move][CASTLING][0] == BOARD_RGHT-1 &&
18000 boards[move][CASTLING][2] != NoRights ) k = 1, *p++ = 'K';
18001 q = (boards[move][CASTLING][1] == BOARD_LEFT &&
18002 boards[move][CASTLING][2] != NoRights );
18003 if(handW) { // for S-Chess with pieces in hand, list virgin pieces between K and Q
18004 for(i=BOARD_RGHT-1-k; i>=BOARD_LEFT+q; i--)
18005 if((boards[move][0][i] != WhiteKing || k+q == 0) &&
18006 boards[move][VIRGIN][i] & VIRGIN_W) *p++ = i + AAA + 'A' - 'a';
18010 if(boards[move][CASTLING][3] == BOARD_RGHT-1 &&
18011 boards[move][CASTLING][5] != NoRights ) k = 1, *p++ = 'k';
18012 q = (boards[move][CASTLING][4] == BOARD_LEFT &&
18013 boards[move][CASTLING][5] != NoRights );
18015 for(i=BOARD_RGHT-1-k; i>=BOARD_LEFT+q; i--)
18016 if((boards[move][BOARD_HEIGHT-1][i] != BlackKing || k+q == 0) &&
18017 boards[move][VIRGIN][i] & VIRGIN_B) *p++ = i + AAA;
18022 if (q == p) *p++ = '-'; /* No castling rights */
18026 if(gameInfo.variant != VariantShogi && gameInfo.variant != VariantXiangqi &&
18027 gameInfo.variant != VariantShatranj && gameInfo.variant != VariantCourier &&
18028 gameInfo.variant != VariantMakruk && gameInfo.variant != VariantASEAN ) {
18029 /* En passant target square */
18030 if (move > backwardMostMove) {
18031 fromX = moveList[move - 1][0] - AAA;
18032 fromY = moveList[move - 1][1] - ONE;
18033 toX = moveList[move - 1][2] - AAA;
18034 toY = moveList[move - 1][3] - ONE;
18035 if (fromY == (whiteToPlay ? BOARD_HEIGHT-2 : 1) &&
18036 toY == (whiteToPlay ? BOARD_HEIGHT-4 : 3) &&
18037 boards[move][toY][toX] == (whiteToPlay ? BlackPawn : WhitePawn) &&
18039 /* 2-square pawn move just happened */
18041 *p++ = whiteToPlay ? '6'+BOARD_HEIGHT-8 : '3';
18045 } else if(move == backwardMostMove) {
18046 // [HGM] perhaps we should always do it like this, and forget the above?
18047 if((signed char)boards[move][EP_STATUS] >= 0) {
18048 *p++ = boards[move][EP_STATUS] + AAA;
18049 *p++ = whiteToPlay ? '6'+BOARD_HEIGHT-8 : '3';
18061 { int i = 0, j=move;
18063 /* [HGM] find reversible plies */
18064 if (appData.debugMode) { int k;
18065 fprintf(debugFP, "write FEN 50-move: %d %d %d\n", initialRulePlies, forwardMostMove, backwardMostMove);
18066 for(k=backwardMostMove; k<=forwardMostMove; k++)
18067 fprintf(debugFP, "e%d. p=%d\n", k, (signed char)boards[k][EP_STATUS]);
18071 while(j > backwardMostMove && (signed char)boards[j][EP_STATUS] <= EP_NONE) j--,i++;
18072 if( j == backwardMostMove ) i += initialRulePlies;
18073 sprintf(p, "%d ", i);
18074 p += i>=100 ? 4 : i >= 10 ? 3 : 2;
18076 /* Fullmove number */
18077 sprintf(p, "%d", (move / 2) + 1);
18078 } else *--p = NULLCHAR;
18080 return StrSave(buf);
18084 ParseFEN (Board board, int *blackPlaysFirst, char *fen, Boolean autoSize)
18086 int i, j, k, w=0, subst=0, shuffle=0, wKingRank = -1, bKingRank = -1;
18088 int emptycount, virgin[BOARD_FILES];
18093 /* Piece placement data */
18094 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
18097 if (*p == '/' || *p == ' ' || *p == '[' ) {
18099 emptycount = gameInfo.boardWidth - j;
18100 while (emptycount--)
18101 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
18102 if (*p == '/') p++;
18103 else if(autoSize) { // we stumbled unexpectedly into end of board
18104 for(k=i; k<BOARD_HEIGHT; k++) { // too few ranks; shift towards bottom
18105 for(j=0; j<BOARD_WIDTH; j++) board[k-i][j] = board[k][j];
18107 appData.NrRanks = gameInfo.boardHeight - i; i=0;
18110 #if(BOARD_FILES >= 10)*0
18111 } else if(*p=='x' || *p=='X') { /* [HGM] X means 10 */
18112 p++; emptycount=10;
18113 if (j + emptycount > gameInfo.boardWidth) return FALSE;
18114 while (emptycount--)
18115 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
18117 } else if (*p == '*') {
18118 board[i][(j++)+gameInfo.holdingsWidth] = DarkSquare; p++;
18119 } else if (isdigit(*p)) {
18120 emptycount = *p++ - '0';
18121 while(isdigit(*p)) emptycount = 10*emptycount + *p++ - '0'; /* [HGM] allow > 9 */
18122 if (j + emptycount > gameInfo.boardWidth) return FALSE;
18123 while (emptycount--)
18124 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
18125 } else if (*p == '<') {
18126 if(i == BOARD_HEIGHT-1) shuffle = 1;
18127 else if (i != 0 || !shuffle) return FALSE;
18129 } else if (shuffle && *p == '>') {
18130 p++; // for now ignore closing shuffle range, and assume rank-end
18131 } else if (*p == '?') {
18132 if (j >= gameInfo.boardWidth) return FALSE;
18133 if (i != 0 && i != BOARD_HEIGHT-1) return FALSE; // only on back-rank
18134 board[i][(j++)+gameInfo.holdingsWidth] = ClearBoard; p++; subst++; // placeHolder
18135 } else if (*p == '+' || isalpha(*p)) {
18136 char *q, *s = SUFFIXES;
18137 if (j >= gameInfo.boardWidth) return FALSE;
18140 if(q = strchr(s, p[1])) p++;
18141 piece = CharToPiece(c + (q ? 64*(q - s + 1) : 0));
18142 if(piece == EmptySquare) return FALSE; /* unknown piece */
18143 piece = (ChessSquare) (CHUPROMOTED piece ); p++;
18144 if(PieceToChar(piece) != '+') return FALSE; /* unpromotable piece */
18147 if(q = strchr(s, *p)) p++;
18148 piece = CharToPiece(c + (q ? 64*(q - s + 1) : 0));
18151 if(piece==EmptySquare) return FALSE; /* unknown piece */
18152 if(*p == '~') { /* [HGM] make it a promoted piece for Crazyhouse */
18153 piece = (ChessSquare) (PROMOTED piece);
18154 if(PieceToChar(piece) != '~') return FALSE; /* cannot be a promoted piece */
18157 board[i][(j++)+gameInfo.holdingsWidth] = piece;
18158 if(piece == WhiteKing) wKingRank = i;
18159 if(piece == BlackKing) bKingRank = i;
18165 while (*p == '/' || *p == ' ') p++;
18167 if(autoSize) appData.NrFiles = w, InitPosition(TRUE);
18169 /* [HGM] by default clear Crazyhouse holdings, if present */
18170 if(gameInfo.holdingsWidth) {
18171 for(i=0; i<BOARD_HEIGHT; i++) {
18172 board[i][0] = EmptySquare; /* black holdings */
18173 board[i][BOARD_WIDTH-1] = EmptySquare; /* white holdings */
18174 board[i][1] = (ChessSquare) 0; /* black counts */
18175 board[i][BOARD_WIDTH-2] = (ChessSquare) 0; /* white counts */
18179 /* [HGM] look for Crazyhouse holdings here */
18180 while(*p==' ') p++;
18181 if( gameInfo.holdingsWidth && p[-1] == '/' || *p == '[') {
18182 int swap=0, wcnt=0, bcnt=0;
18184 if(*p == '<') swap++, p++;
18185 if(*p == '-' ) p++; /* empty holdings */ else {
18186 if( !gameInfo.holdingsWidth ) return FALSE; /* no room to put holdings! */
18187 /* if we would allow FEN reading to set board size, we would */
18188 /* have to add holdings and shift the board read so far here */
18189 while( (piece = CharToPiece(*p) ) != EmptySquare ) {
18191 if((int) piece >= (int) BlackPawn ) {
18192 i = (int)piece - (int)BlackPawn;
18193 i = PieceToNumber((ChessSquare)i);
18194 if( i >= gameInfo.holdingsSize ) return FALSE;
18195 board[BOARD_HEIGHT-1-i][0] = piece; /* black holdings */
18196 board[BOARD_HEIGHT-1-i][1]++; /* black counts */
18199 i = (int)piece - (int)WhitePawn;
18200 i = PieceToNumber((ChessSquare)i);
18201 if( i >= gameInfo.holdingsSize ) return FALSE;
18202 board[i][BOARD_WIDTH-1] = piece; /* white holdings */
18203 board[i][BOARD_WIDTH-2]++; /* black holdings */
18207 if(subst) { // substitute back-rank question marks by holdings pieces
18208 for(j=BOARD_LEFT; j<BOARD_RGHT; j++) {
18209 int k, m, n = bcnt + 1;
18210 if(board[0][j] == ClearBoard) {
18211 if(!wcnt) return FALSE;
18213 for(k=0, m=n; k<gameInfo.holdingsSize; k++) if((m -= board[k][BOARD_WIDTH-2]) < 0) {
18214 board[0][j] = board[k][BOARD_WIDTH-1]; wcnt--;
18215 if(--board[k][BOARD_WIDTH-2] == 0) board[k][BOARD_WIDTH-1] = EmptySquare;
18219 if(board[BOARD_HEIGHT-1][j] == ClearBoard) {
18220 if(!bcnt) return FALSE;
18221 if(n >= bcnt) n = rand() % bcnt; // use same randomization for black and white if possible
18222 for(k=0, m=n; k<gameInfo.holdingsSize; k++) if((n -= board[BOARD_HEIGHT-1-k][1]) < 0) {
18223 board[BOARD_HEIGHT-1][j] = board[BOARD_HEIGHT-1-k][0]; bcnt--;
18224 if(--board[BOARD_HEIGHT-1-k][1] == 0) board[BOARD_HEIGHT-1-k][0] = EmptySquare;
18235 if(subst) return FALSE; // substitution requested, but no holdings
18237 while(*p == ' ') p++;
18241 if(appData.colorNickNames) {
18242 if( c == appData.colorNickNames[0] ) c = 'w'; else
18243 if( c == appData.colorNickNames[1] ) c = 'b';
18247 *blackPlaysFirst = FALSE;
18250 *blackPlaysFirst = TRUE;
18256 /* [HGM] We NO LONGER ignore the rest of the FEN notation */
18257 /* return the extra info in global variiables */
18259 while(*p==' ') p++;
18261 if(!isdigit(*p) && *p != '-') { // we seem to have castling rights. Make sure they are on the rank the King actually is.
18262 if(wKingRank >= 0) for(i=0; i<3; i++) castlingRank[i] = wKingRank;
18263 if(bKingRank >= 0) for(i=3; i<6; i++) castlingRank[i] = bKingRank;
18266 /* set defaults in case FEN is incomplete */
18267 board[EP_STATUS] = EP_UNKNOWN;
18268 for(i=0; i<nrCastlingRights; i++ ) {
18269 board[CASTLING][i] =
18270 appData.fischerCastling ? NoRights : initialRights[i];
18271 } /* assume possible unless obviously impossible */
18272 if(initialRights[0]!=NoRights && board[castlingRank[0]][initialRights[0]] != WhiteRook) board[CASTLING][0] = NoRights;
18273 if(initialRights[1]!=NoRights && board[castlingRank[1]][initialRights[1]] != WhiteRook) board[CASTLING][1] = NoRights;
18274 if(initialRights[2]!=NoRights && board[castlingRank[2]][initialRights[2]] != WhiteUnicorn
18275 && board[castlingRank[2]][initialRights[2]] != WhiteKing) board[CASTLING][2] = NoRights;
18276 if(initialRights[3]!=NoRights && board[castlingRank[3]][initialRights[3]] != BlackRook) board[CASTLING][3] = NoRights;
18277 if(initialRights[4]!=NoRights && board[castlingRank[4]][initialRights[4]] != BlackRook) board[CASTLING][4] = NoRights;
18278 if(initialRights[5]!=NoRights && board[castlingRank[5]][initialRights[5]] != BlackUnicorn
18279 && board[castlingRank[5]][initialRights[5]] != BlackKing) board[CASTLING][5] = NoRights;
18282 if(nrCastlingRights) {
18284 if(gameInfo.variant == VariantSChess) for(i=0; i<BOARD_FILES; i++) virgin[i] = 0;
18285 if(*p >= 'A' && *p <= 'Z' || *p >= 'a' && *p <= 'z' || *p=='-') {
18286 /* castling indicator present, so default becomes no castlings */
18287 for(i=0; i<nrCastlingRights; i++ ) {
18288 board[CASTLING][i] = NoRights;
18291 while(*p=='K' || *p=='Q' || *p=='k' || *p=='q' || *p=='-' ||
18292 (appData.fischerCastling || gameInfo.variant == VariantSChess) &&
18293 ( *p >= 'a' && *p < 'a' + gameInfo.boardWidth) ||
18294 ( *p >= 'A' && *p < 'A' + gameInfo.boardWidth) ) {
18295 int c = *p++, whiteKingFile=NoRights, blackKingFile=NoRights;
18297 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) {
18298 if(board[castlingRank[5]][i] == BlackKing) blackKingFile = i;
18299 if(board[castlingRank[2]][i] == WhiteKing) whiteKingFile = i;
18301 if(gameInfo.variant == VariantTwoKings || gameInfo.variant == VariantKnightmate)
18302 whiteKingFile = blackKingFile = BOARD_WIDTH >> 1; // for these variant scanning fails
18303 if(whiteKingFile == NoRights || board[0][whiteKingFile] != WhiteUnicorn
18304 && board[0][whiteKingFile] != WhiteKing) whiteKingFile = NoRights;
18305 if(blackKingFile == NoRights || board[BOARD_HEIGHT-1][blackKingFile] != BlackUnicorn
18306 && board[BOARD_HEIGHT-1][blackKingFile] != BlackKing) blackKingFile = NoRights;
18309 for(i=BOARD_RGHT-1; board[castlingRank[2]][i]!=WhiteRook && i>whiteKingFile; i--);
18310 board[CASTLING][0] = i != whiteKingFile ? i : NoRights;
18311 board[CASTLING][2] = whiteKingFile;
18312 if(board[CASTLING][0] != NoRights) virgin[board[CASTLING][0]] |= VIRGIN_W;
18313 if(board[CASTLING][2] != NoRights) virgin[board[CASTLING][2]] |= VIRGIN_W;
18314 if(whiteKingFile != BOARD_WIDTH>>1|| i != BOARD_RGHT-1) fischer = 1;
18317 for(i=BOARD_LEFT; i<BOARD_RGHT && board[castlingRank[2]][i]!=WhiteRook && i<whiteKingFile; i++);
18318 board[CASTLING][1] = i != whiteKingFile ? i : NoRights;
18319 board[CASTLING][2] = whiteKingFile;
18320 if(board[CASTLING][1] != NoRights) virgin[board[CASTLING][1]] |= VIRGIN_W;
18321 if(board[CASTLING][2] != NoRights) virgin[board[CASTLING][2]] |= VIRGIN_W;
18322 if(whiteKingFile != BOARD_WIDTH>>1|| i != BOARD_LEFT) fischer = 1;
18325 for(i=BOARD_RGHT-1; board[castlingRank[5]][i]!=BlackRook && i>blackKingFile; i--);
18326 board[CASTLING][3] = i != blackKingFile ? i : NoRights;
18327 board[CASTLING][5] = blackKingFile;
18328 if(board[CASTLING][3] != NoRights) virgin[board[CASTLING][3]] |= VIRGIN_B;
18329 if(board[CASTLING][5] != NoRights) virgin[board[CASTLING][5]] |= VIRGIN_B;
18330 if(blackKingFile != BOARD_WIDTH>>1|| i != BOARD_RGHT-1) fischer = 1;
18333 for(i=BOARD_LEFT; i<BOARD_RGHT && board[castlingRank[5]][i]!=BlackRook && i<blackKingFile; i++);
18334 board[CASTLING][4] = i != blackKingFile ? i : NoRights;
18335 board[CASTLING][5] = blackKingFile;
18336 if(board[CASTLING][4] != NoRights) virgin[board[CASTLING][4]] |= VIRGIN_B;
18337 if(board[CASTLING][5] != NoRights) virgin[board[CASTLING][5]] |= VIRGIN_B;
18338 if(blackKingFile != BOARD_WIDTH>>1|| i != BOARD_LEFT) fischer = 1;
18341 default: /* FRC castlings */
18342 if(c >= 'a') { /* black rights */
18343 if(gameInfo.variant == VariantSChess) { virgin[c-AAA] |= VIRGIN_B; break; } // in S-Chess castlings are always kq, so just virginity
18344 for(i=BOARD_LEFT; i<BOARD_RGHT; i++)
18345 if(board[BOARD_HEIGHT-1][i] == BlackKing) break;
18346 if(i == BOARD_RGHT) break;
18347 board[CASTLING][5] = i;
18349 if(board[BOARD_HEIGHT-1][c] < BlackPawn ||
18350 board[BOARD_HEIGHT-1][c] >= BlackKing ) break;
18352 board[CASTLING][3] = c;
18354 board[CASTLING][4] = c;
18355 } else { /* white rights */
18356 if(gameInfo.variant == VariantSChess) { virgin[c-AAA-'A'+'a'] |= VIRGIN_W; break; } // in S-Chess castlings are always KQ
18357 for(i=BOARD_LEFT; i<BOARD_RGHT; i++)
18358 if(board[0][i] == WhiteKing) break;
18359 if(i == BOARD_RGHT) break;
18360 board[CASTLING][2] = i;
18361 c -= AAA - 'a' + 'A';
18362 if(board[0][c] >= WhiteKing) break;
18364 board[CASTLING][0] = c;
18366 board[CASTLING][1] = c;
18370 for(i=0; i<nrCastlingRights; i++)
18371 if(board[CASTLING][i] != NoRights) initialRights[i] = board[CASTLING][i];
18372 if(gameInfo.variant == VariantSChess)
18373 for(i=0; i<BOARD_FILES; i++) board[VIRGIN][i] = shuffle ? VIRGIN_W | VIRGIN_B : virgin[i]; // when shuffling assume all virgin
18374 if(fischer && shuffle) appData.fischerCastling = TRUE;
18375 if (appData.debugMode) {
18376 fprintf(debugFP, "FEN castling rights:");
18377 for(i=0; i<nrCastlingRights; i++)
18378 fprintf(debugFP, " %d", board[CASTLING][i]);
18379 fprintf(debugFP, "\n");
18382 while(*p==' ') p++;
18385 if(shuffle) SetUpShuffle(board, appData.defaultFrcPosition);
18387 /* read e.p. field in games that know e.p. capture */
18388 if(gameInfo.variant != VariantShogi && gameInfo.variant != VariantXiangqi &&
18389 gameInfo.variant != VariantShatranj && gameInfo.variant != VariantCourier &&
18390 gameInfo.variant != VariantMakruk && gameInfo.variant != VariantASEAN ) {
18392 p++; board[EP_STATUS] = EP_NONE;
18394 char c = *p++ - AAA;
18396 if(c < BOARD_LEFT || c >= BOARD_RGHT) return TRUE;
18397 if(*p >= '0' && *p <='9') p++;
18398 board[EP_STATUS] = c;
18403 if(sscanf(p, "%d", &i) == 1) {
18404 FENrulePlies = i; /* 50-move ply counter */
18405 /* (The move number is still ignored) */
18412 EditPositionPasteFEN (char *fen)
18415 Board initial_position;
18417 if (!ParseFEN(initial_position, &blackPlaysFirst, fen, TRUE)) {
18418 DisplayError(_("Bad FEN position in clipboard"), 0);
18421 int savedBlackPlaysFirst = blackPlaysFirst;
18422 EditPositionEvent();
18423 blackPlaysFirst = savedBlackPlaysFirst;
18424 CopyBoard(boards[0], initial_position);
18425 initialRulePlies = FENrulePlies; /* [HGM] copy FEN attributes as well */
18426 EditPositionDone(FALSE); // [HGM] fake: do not fake rights if we had FEN
18427 DisplayBothClocks();
18428 DrawPosition(FALSE, boards[currentMove]);
18433 static char cseq[12] = "\\ ";
18436 set_cont_sequence (char *new_seq)
18441 // handle bad attempts to set the sequence
18443 return 0; // acceptable error - no debug
18445 len = strlen(new_seq);
18446 ret = (len > 0) && (len < sizeof(cseq));
18448 safeStrCpy(cseq, new_seq, sizeof(cseq)/sizeof(cseq[0]));
18449 else if (appData.debugMode)
18450 fprintf(debugFP, "Invalid continuation sequence \"%s\" (maximum length is: %u)\n", new_seq, (unsigned) sizeof(cseq)-1);
18455 reformat a source message so words don't cross the width boundary. internal
18456 newlines are not removed. returns the wrapped size (no null character unless
18457 included in source message). If dest is NULL, only calculate the size required
18458 for the dest buffer. lp argument indicats line position upon entry, and it's
18459 passed back upon exit.
18462 wrap (char *dest, char *src, int count, int width, int *lp)
18464 int len, i, ansi, cseq_len, line, old_line, old_i, old_len, clen;
18466 cseq_len = strlen(cseq);
18467 old_line = line = *lp;
18468 ansi = len = clen = 0;
18470 for (i=0; i < count; i++)
18472 if (src[i] == '\033')
18475 // if we hit the width, back up
18476 if (!ansi && (line >= width) && src[i] != '\n' && src[i] != ' ')
18478 // store i & len in case the word is too long
18479 old_i = i, old_len = len;
18481 // find the end of the last word
18482 while (i && src[i] != ' ' && src[i] != '\n')
18488 // word too long? restore i & len before splitting it
18489 if ((old_i-i+clen) >= width)
18496 if (i && src[i-1] == ' ')
18499 if (src[i] != ' ' && src[i] != '\n')
18506 // now append the newline and continuation sequence
18511 strncpy(dest+len, cseq, cseq_len);
18519 dest[len] = src[i];
18523 if (src[i] == '\n')
18528 if (dest && appData.debugMode)
18530 fprintf(debugFP, "wrap(count:%d,width:%d,line:%d,len:%d,*lp:%d,src: ",
18531 count, width, line, len, *lp);
18532 show_bytes(debugFP, src, count);
18533 fprintf(debugFP, "\ndest: ");
18534 show_bytes(debugFP, dest, len);
18535 fprintf(debugFP, "\n");
18537 *lp = dest ? line : old_line;
18542 // [HGM] vari: routines for shelving variations
18543 Boolean modeRestore = FALSE;
18546 PushInner (int firstMove, int lastMove)
18548 int i, j, nrMoves = lastMove - firstMove;
18550 // push current tail of game on stack
18551 savedResult[storedGames] = gameInfo.result;
18552 savedDetails[storedGames] = gameInfo.resultDetails;
18553 gameInfo.resultDetails = NULL;
18554 savedFirst[storedGames] = firstMove;
18555 savedLast [storedGames] = lastMove;
18556 savedFramePtr[storedGames] = framePtr;
18557 framePtr -= nrMoves; // reserve space for the boards
18558 for(i=nrMoves; i>=1; i--) { // copy boards to stack, working downwards, in case of overlap
18559 CopyBoard(boards[framePtr+i], boards[firstMove+i]);
18560 for(j=0; j<MOVE_LEN; j++)
18561 moveList[framePtr+i][j] = moveList[firstMove+i-1][j];
18562 for(j=0; j<2*MOVE_LEN; j++)
18563 parseList[framePtr+i][j] = parseList[firstMove+i-1][j];
18564 timeRemaining[0][framePtr+i] = timeRemaining[0][firstMove+i];
18565 timeRemaining[1][framePtr+i] = timeRemaining[1][firstMove+i];
18566 pvInfoList[framePtr+i] = pvInfoList[firstMove+i-1];
18567 pvInfoList[firstMove+i-1].depth = 0;
18568 commentList[framePtr+i] = commentList[firstMove+i];
18569 commentList[firstMove+i] = NULL;
18573 forwardMostMove = firstMove; // truncate game so we can start variation
18577 PushTail (int firstMove, int lastMove)
18579 if(appData.icsActive) { // only in local mode
18580 forwardMostMove = currentMove; // mimic old ICS behavior
18583 if(storedGames >= MAX_VARIATIONS-2) return; // leave one for PV-walk
18585 PushInner(firstMove, lastMove);
18586 if(storedGames == 1) GreyRevert(FALSE);
18587 if(gameMode == PlayFromGameFile) gameMode = EditGame, modeRestore = TRUE;
18591 PopInner (Boolean annotate)
18594 char buf[8000], moveBuf[20];
18596 ToNrEvent(savedFirst[storedGames-1]); // sets currentMove
18597 storedGames--; // do this after ToNrEvent, to make sure HistorySet will refresh entire game after PopInner returns
18598 nrMoves = savedLast[storedGames] - currentMove;
18601 if(!WhiteOnMove(currentMove))
18602 snprintf(buf, sizeof(buf)/sizeof(buf[0]),"(%d...", (currentMove+2)>>1);
18603 else safeStrCpy(buf, "(", sizeof(buf)/sizeof(buf[0]));
18604 for(i=currentMove; i<forwardMostMove; i++) {
18606 snprintf(moveBuf, sizeof(moveBuf)/sizeof(moveBuf[0]), " %d. %s", (i+2)>>1, SavePart(parseList[i]));
18607 else snprintf(moveBuf, sizeof(moveBuf)/sizeof(moveBuf[0])," %s", SavePart(parseList[i]));
18608 strcat(buf, moveBuf);
18609 if(commentList[i]) { strcat(buf, " "); strcat(buf, commentList[i]); }
18610 if(!--cnt) { strcat(buf, "\n"); cnt = 10; }
18614 for(i=1; i<=nrMoves; i++) { // copy last variation back
18615 CopyBoard(boards[currentMove+i], boards[framePtr+i]);
18616 for(j=0; j<MOVE_LEN; j++)
18617 moveList[currentMove+i-1][j] = moveList[framePtr+i][j];
18618 for(j=0; j<2*MOVE_LEN; j++)
18619 parseList[currentMove+i-1][j] = parseList[framePtr+i][j];
18620 timeRemaining[0][currentMove+i] = timeRemaining[0][framePtr+i];
18621 timeRemaining[1][currentMove+i] = timeRemaining[1][framePtr+i];
18622 pvInfoList[currentMove+i-1] = pvInfoList[framePtr+i];
18623 if(commentList[currentMove+i]) free(commentList[currentMove+i]);
18624 commentList[currentMove+i] = commentList[framePtr+i];
18625 commentList[framePtr+i] = NULL;
18627 if(annotate) AppendComment(currentMove+1, buf, FALSE);
18628 framePtr = savedFramePtr[storedGames];
18629 gameInfo.result = savedResult[storedGames];
18630 if(gameInfo.resultDetails != NULL) {
18631 free(gameInfo.resultDetails);
18633 gameInfo.resultDetails = savedDetails[storedGames];
18634 forwardMostMove = currentMove + nrMoves;
18638 PopTail (Boolean annotate)
18640 if(appData.icsActive) return FALSE; // only in local mode
18641 if(!storedGames) return FALSE; // sanity
18642 CommentPopDown(); // make sure no stale variation comments to the destroyed line can remain open
18644 PopInner(annotate);
18645 if(currentMove < forwardMostMove) ForwardEvent(); else
18646 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
18648 if(storedGames == 0) { GreyRevert(TRUE); if(modeRestore) modeRestore = FALSE, gameMode = PlayFromGameFile; }
18654 { // remove all shelved variations
18656 for(i=0; i<storedGames; i++) {
18657 if(savedDetails[i])
18658 free(savedDetails[i]);
18659 savedDetails[i] = NULL;
18661 for(i=framePtr; i<MAX_MOVES; i++) {
18662 if(commentList[i]) free(commentList[i]);
18663 commentList[i] = NULL;
18665 framePtr = MAX_MOVES-1;
18670 LoadVariation (int index, char *text)
18671 { // [HGM] vari: shelve previous line and load new variation, parsed from text around text[index]
18672 char *p = text, *start = NULL, *end = NULL, wait = NULLCHAR;
18673 int level = 0, move;
18675 if(gameMode != EditGame && gameMode != AnalyzeMode && gameMode != PlayFromGameFile) return;
18676 // first find outermost bracketing variation
18677 while(*p) { // hope I got this right... Non-nesting {} and [] can screen each other and nesting ()
18678 if(!wait) { // while inside [] pr {}, ignore everyting except matching closing ]}
18679 if(*p == '{') wait = '}'; else
18680 if(*p == '[') wait = ']'; else
18681 if(*p == '(' && level++ == 0 && p-text < index) start = p+1;
18682 if(*p == ')' && level > 0 && --level == 0 && p-text > index && end == NULL) end = p-1;
18684 if(*p == wait) wait = NULLCHAR; // closing ]} found
18687 if(!start || !end) return; // no variation found, or syntax error in PGN: ignore click
18688 if(appData.debugMode) fprintf(debugFP, "at move %d load variation '%s'\n", currentMove, start);
18689 end[1] = NULLCHAR; // clip off comment beyond variation
18690 ToNrEvent(currentMove-1);
18691 PushTail(currentMove, forwardMostMove); // shelve main variation. This truncates game
18692 // kludge: use ParsePV() to append variation to game
18693 move = currentMove;
18694 ParsePV(start, TRUE, TRUE);
18695 forwardMostMove = endPV; endPV = -1; currentMove = move; // cleanup what ParsePV did
18696 ClearPremoveHighlights();
18698 ToNrEvent(currentMove+1);
18704 char *p, *q, buf[MSG_SIZ];
18705 if(engineLine && engineLine[0]) { // a theme was selected from the listbox
18706 snprintf(buf, MSG_SIZ, "-theme %s", engineLine);
18707 ParseArgsFromString(buf);
18708 ActivateTheme(TRUE); // also redo colors
18712 if(*p && !strchr(p, '"')) // theme name specified and well-formed; add settings to theme list
18715 q = appData.themeNames;
18716 snprintf(buf, MSG_SIZ, "\"%s\"", nickName);
18717 if(appData.useBitmaps) {
18718 snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), " -ubt true -lbtf \"%s\" -dbtf \"%s\" -lbtm %d -dbtm %d",
18719 appData.liteBackTextureFile, appData.darkBackTextureFile,
18720 appData.liteBackTextureMode,
18721 appData.darkBackTextureMode );
18723 snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), " -ubt false -lsc %s -dsc %s",
18724 Col2Text(2), // lightSquareColor
18725 Col2Text(3) ); // darkSquareColor
18727 if(appData.useBorder) {
18728 snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), " -ub true -border \"%s\"",
18731 snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), " -ub false");
18733 if(appData.useFont) {
18734 snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), " -upf true -pf \"%s\" -fptc \"%s\" -fpfcw %s -fpbcb %s",
18735 appData.renderPiecesWithFont,
18736 appData.fontToPieceTable,
18737 Col2Text(9), // appData.fontBackColorWhite
18738 Col2Text(10) ); // appData.fontForeColorBlack
18740 snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), " -upf false -pid \"%s\"",
18741 appData.pieceDirectory);
18742 if(!appData.pieceDirectory[0])
18743 snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), " -wpc %s -bpc %s",
18744 Col2Text(0), // whitePieceColor
18745 Col2Text(1) ); // blackPieceColor
18747 snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), " -hsc %s -phc %s\n",
18748 Col2Text(4), // highlightSquareColor
18749 Col2Text(5) ); // premoveHighlightColor
18750 appData.themeNames = malloc(len = strlen(q) + strlen(buf) + 1);
18751 if(insert != q) insert[-1] = NULLCHAR;
18752 snprintf(appData.themeNames, len, "%s\n%s%s", q, buf, insert);
18755 ActivateTheme(FALSE);