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 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);
75 #include <sys/types.h>
84 #else /* not STDC_HEADERS */
87 # else /* not HAVE_STRING_H */
89 # endif /* not HAVE_STRING_H */
90 #endif /* not STDC_HEADERS */
93 # include <sys/fcntl.h>
94 #else /* not HAVE_SYS_FCNTL_H */
97 # endif /* HAVE_FCNTL_H */
98 #endif /* not HAVE_SYS_FCNTL_H */
100 #if TIME_WITH_SYS_TIME
101 # include <sys/time.h>
105 # include <sys/time.h>
111 #if defined(_amigados) && !defined(__GNUC__)
116 extern int gettimeofday(struct timeval *, struct timezone *);
124 #include "frontend.h"
131 #include "backendz.h"
135 # define _(s) gettext (s)
136 # define N_(s) gettext_noop (s)
137 # define T_(s) gettext(s)
150 int establish P((void));
151 void read_from_player P((InputSourceRef isr, VOIDSTAR closure,
152 char *buf, int count, int error));
153 void read_from_ics P((InputSourceRef isr, VOIDSTAR closure,
154 char *buf, int count, int error));
155 void ics_printf P((char *format, ...));
156 void SendToICS P((char *s));
157 void SendToICSDelayed P((char *s, long msdelay));
158 void SendMoveToICS P((ChessMove moveType, int fromX, int fromY, int toX, int toY, char promoChar));
159 void HandleMachineMove P((char *message, ChessProgramState *cps));
160 int AutoPlayOneMove P((void));
161 int LoadGameOneMove P((ChessMove readAhead));
162 int LoadGameFromFile P((char *filename, int n, char *title, int useList));
163 int LoadPositionFromFile P((char *filename, int n, char *title));
164 int SavePositionToFile P((char *filename));
165 void MakeMove P((int fromX, int fromY, int toX, int toY, int promoChar));
166 void ShowMove P((int fromX, int fromY, int toX, int toY));
167 int FinishMove P((ChessMove moveType, int fromX, int fromY, int toX, int toY,
168 /*char*/int promoChar));
169 void BackwardInner P((int target));
170 void ForwardInner P((int target));
171 int Adjudicate P((ChessProgramState *cps));
172 void GameEnds P((ChessMove result, char *resultDetails, int whosays));
173 void EditPositionDone P((Boolean fakeRights));
174 void PrintOpponents P((FILE *fp));
175 void PrintPosition P((FILE *fp, int move));
176 void StartChessProgram P((ChessProgramState *cps));
177 void SendToProgram P((char *message, ChessProgramState *cps));
178 void SendMoveToProgram P((int moveNum, ChessProgramState *cps));
179 void ReceiveFromProgram P((InputSourceRef isr, VOIDSTAR closure,
180 char *buf, int count, int error));
181 void SendTimeControl P((ChessProgramState *cps,
182 int mps, long tc, int inc, int sd, int st));
183 char *TimeControlTagValue P((void));
184 void Attention P((ChessProgramState *cps));
185 void FeedMovesToProgram P((ChessProgramState *cps, int upto));
186 int ResurrectChessProgram P((void));
187 void DisplayComment P((int moveNumber, char *text));
188 void DisplayMove P((int moveNumber));
190 void ParseGameHistory P((char *game));
191 void ParseBoard12 P((char *string));
192 void KeepAlive P((void));
193 void StartClocks P((void));
194 void SwitchClocks P((int nr));
195 void StopClocks P((void));
196 void ResetClocks P((void));
197 char *PGNDate P((void));
198 void SetGameInfo P((void));
199 int RegisterMove P((void));
200 void MakeRegisteredMove P((void));
201 void TruncateGame P((void));
202 int looking_at P((char *, int *, char *));
203 void CopyPlayerNameIntoFileName P((char **, char *));
204 char *SavePart P((char *));
205 int SaveGameOldStyle P((FILE *));
206 int SaveGamePGN P((FILE *));
207 int CheckFlags P((void));
208 long NextTickLength P((long));
209 void CheckTimeControl P((void));
210 void show_bytes P((FILE *, char *, int));
211 int string_to_rating P((char *str));
212 void ParseFeatures P((char* args, ChessProgramState *cps));
213 void InitBackEnd3 P((void));
214 void FeatureDone P((ChessProgramState* cps, int val));
215 void InitChessProgram P((ChessProgramState *cps, int setup));
216 void OutputKibitz(int window, char *text);
217 int PerpetualChase(int first, int last);
218 int EngineOutputIsUp();
219 void InitDrawingSizes(int x, int y);
220 void NextMatchGame P((void));
221 int NextTourneyGame P((int nr, int *swap));
222 int Pairing P((int nr, int nPlayers, int *w, int *b, int *sync));
223 FILE *WriteTourneyFile P((char *results, FILE *f));
224 void DisplayTwoMachinesTitle P(());
227 extern void ConsoleCreate();
230 ChessProgramState *WhitePlayer();
231 void InsertIntoMemo P((int which, char *text)); // [HGM] kibitz: in engineo.c
232 int VerifyDisplayMode P(());
234 char *GetInfoFromComment( int, char * ); // [HGM] PV time: returns stripped comment
235 void InitEngineUCI( const char * iniDir, ChessProgramState * cps ); // [HGM] moved here from winboard.c
236 char *ProbeBook P((int moveNr, char *book)); // [HGM] book: returns a book move
237 char *SendMoveToBookUser P((int nr, ChessProgramState *cps, int initial)); // [HGM] book
238 void ics_update_width P((int new_width));
239 extern char installDir[MSG_SIZ];
240 VariantClass startVariant; /* [HGM] nicks: initial variant */
243 extern int tinyLayout, smallLayout;
244 ChessProgramStats programStats;
245 char lastPV[2][2*MSG_SIZ]; /* [HGM] pv: last PV in thinking output of each engine */
247 static int exiting = 0; /* [HGM] moved to top */
248 static int setboardSpoiledMachineBlack = 0 /*, errorExitFlag = 0*/;
249 int startedFromPositionFile = FALSE; Board filePosition; /* [HGM] loadPos */
250 Board partnerBoard; /* [HGM] bughouse: for peeking at partner game */
251 int partnerHighlight[2];
252 Boolean partnerBoardValid = 0;
253 char partnerStatus[MSG_SIZ];
255 Boolean originalFlip;
256 Boolean twoBoards = 0;
257 char endingGame = 0; /* [HGM] crash: flag to prevent recursion of GameEnds() */
258 int whiteNPS, blackNPS; /* [HGM] nps: for easily making clocks aware of NPS */
259 VariantClass currentlyInitializedVariant; /* [HGM] variantswitch */
260 int lastIndex = 0; /* [HGM] autoinc: last game/position used in match mode */
261 Boolean connectionAlive;/* [HGM] alive: ICS connection status from probing */
262 int opponentKibitzes;
263 int lastSavedGame; /* [HGM] save: ID of game */
264 char chatPartner[MAX_CHAT][MSG_SIZ]; /* [HGM] chat: list of chatting partners */
265 extern int chatCount;
267 char marker[BOARD_RANKS][BOARD_FILES]; /* [HGM] marks for target squares */
268 char lastMsg[MSG_SIZ];
269 ChessSquare pieceSweep = EmptySquare;
270 ChessSquare promoSweep = EmptySquare, defaultPromoChoice;
271 int promoDefaultAltered;
273 /* States for ics_getting_history */
275 #define H_REQUESTED 1
276 #define H_GOT_REQ_HEADER 2
277 #define H_GOT_UNREQ_HEADER 3
278 #define H_GETTING_MOVES 4
279 #define H_GOT_UNWANTED_HEADER 5
281 /* whosays values for GameEnds */
290 /* Maximum number of games in a cmail message */
291 #define CMAIL_MAX_GAMES 20
293 /* Different types of move when calling RegisterMove */
295 #define CMAIL_RESIGN 1
297 #define CMAIL_ACCEPT 3
299 /* Different types of result to remember for each game */
300 #define CMAIL_NOT_RESULT 0
301 #define CMAIL_OLD_RESULT 1
302 #define CMAIL_NEW_RESULT 2
304 /* Telnet protocol constants */
315 safeStrCpy (char *dst, const char *src, size_t count)
318 assert( dst != NULL );
319 assert( src != NULL );
322 for(i=0; i<count; i++) if((dst[i] = src[i]) == NULLCHAR) break;
323 if( i == count && dst[count-1] != NULLCHAR)
325 dst[ count-1 ] = '\0'; // make sure incomplete copy still null-terminated
326 if(appData.debugMode)
327 fprintf(debugFP, "safeStrCpy: copying %s into %s didn't work, not enough space %d\n",src,dst, (int)count);
333 /* Some compiler can't cast u64 to double
334 * This function do the job for us:
336 * We use the highest bit for cast, this only
337 * works if the highest bit is not
338 * in use (This should not happen)
340 * We used this for all compiler
343 u64ToDouble (u64 value)
346 u64 tmp = value & u64Const(0x7fffffffffffffff);
347 r = (double)(s64)tmp;
348 if (value & u64Const(0x8000000000000000))
349 r += 9.2233720368547758080e18; /* 2^63 */
353 /* Fake up flags for now, as we aren't keeping track of castling
354 availability yet. [HGM] Change of logic: the flag now only
355 indicates the type of castlings allowed by the rule of the game.
356 The actual rights themselves are maintained in the array
357 castlingRights, as part of the game history, and are not probed
363 int flags = F_ALL_CASTLE_OK;
364 if ((index % 2) == 0) flags |= F_WHITE_ON_MOVE;
365 switch (gameInfo.variant) {
367 flags &= ~F_ALL_CASTLE_OK;
368 case VariantGiveaway: // [HGM] moved this case label one down: seems Giveaway does have castling on ICC!
369 flags |= F_IGNORE_CHECK;
371 flags |= F_MANDATORY_CAPTURE; //[HGM] losers: sets flag so TestLegality rejects non-capts if capts exist
374 flags |= F_IGNORE_CHECK | F_ATOMIC_CAPTURE;
376 case VariantKriegspiel:
377 flags |= F_KRIEGSPIEL_CAPTURE;
379 case VariantCapaRandom:
380 case VariantFischeRandom:
381 flags |= F_FRC_TYPE_CASTLING; /* [HGM] enable this through flag */
382 case VariantNoCastle:
383 case VariantShatranj:
387 flags &= ~F_ALL_CASTLE_OK;
395 FILE *gameFileFP, *debugFP, *serverFP;
396 char *currentDebugFile; // [HGM] debug split: to remember name
399 [AS] Note: sometimes, the sscanf() function is used to parse the input
400 into a fixed-size buffer. Because of this, we must be prepared to
401 receive strings as long as the size of the input buffer, which is currently
402 set to 4K for Windows and 8K for the rest.
403 So, we must either allocate sufficiently large buffers here, or
404 reduce the size of the input buffer in the input reading part.
407 char cmailMove[CMAIL_MAX_GAMES][MOVE_LEN], cmailMsg[MSG_SIZ];
408 char bookOutput[MSG_SIZ*10], thinkOutput[MSG_SIZ*10], lastHint[MSG_SIZ];
409 char thinkOutput1[MSG_SIZ*10];
411 ChessProgramState first, second, pairing;
413 /* premove variables */
416 int premoveFromX = 0;
417 int premoveFromY = 0;
418 int premovePromoChar = 0;
420 Boolean alarmSounded;
421 /* end premove variables */
423 char *ics_prefix = "$";
424 int ics_type = ICS_GENERIC;
426 int currentMove = 0, forwardMostMove = 0, backwardMostMove = 0;
427 int pauseExamForwardMostMove = 0;
428 int nCmailGames = 0, nCmailResults = 0, nCmailMovesRegistered = 0;
429 int cmailMoveRegistered[CMAIL_MAX_GAMES], cmailResult[CMAIL_MAX_GAMES];
430 int cmailMsgLoaded = FALSE, cmailMailedMove = FALSE;
431 int cmailOldMove = -1, firstMove = TRUE, flipView = FALSE;
432 int blackPlaysFirst = FALSE, startedFromSetupPosition = FALSE;
433 int searchTime = 0, pausing = FALSE, pauseExamInvalid = FALSE;
434 int whiteFlag = FALSE, blackFlag = FALSE;
435 int userOfferedDraw = FALSE;
436 int ics_user_moved = 0, ics_gamenum = -1, ics_getting_history = H_FALSE;
437 int matchMode = FALSE, hintRequested = FALSE, bookRequested = FALSE;
438 int cmailMoveType[CMAIL_MAX_GAMES];
439 long ics_clock_paused = 0;
440 ProcRef icsPR = NoProc, cmailPR = NoProc;
441 InputSourceRef telnetISR = NULL, fromUserISR = NULL, cmailISR = NULL;
442 GameMode gameMode = BeginningOfGame;
443 char moveList[MAX_MOVES][MOVE_LEN], parseList[MAX_MOVES][MOVE_LEN * 2];
444 char *commentList[MAX_MOVES], *cmailCommentList[CMAIL_MAX_GAMES];
445 ChessProgramStats_Move pvInfoList[MAX_MOVES]; /* [AS] Info about engine thinking */
446 int hiddenThinkOutputState = 0; /* [AS] */
447 int adjudicateLossThreshold = 0; /* [AS] Automatic adjudication */
448 int adjudicateLossPlies = 6;
449 char white_holding[64], black_holding[64];
450 TimeMark lastNodeCountTime;
451 long lastNodeCount=0;
452 int shiftKey; // [HGM] set by mouse handler
454 int have_sent_ICS_logon = 0;
456 int suddenDeath, whiteStartMove, blackStartMove; /* [HGM] for implementation of 'any per time' sessions, as in first part of byoyomi TC */
457 long whiteTimeRemaining, blackTimeRemaining, timeControl, timeIncrement, lastWhite, lastBlack;
458 Boolean adjustedClock;
459 long timeControl_2; /* [AS] Allow separate time controls */
460 char *fullTimeControlString = NULL, *nextSession, *whiteTC, *blackTC; /* [HGM] secondary TC: merge of MPS, TC and inc */
461 long timeRemaining[2][MAX_MOVES];
462 int matchGame = 0, nextGame = 0, roundNr = 0;
463 Boolean waitingForGame = FALSE;
464 TimeMark programStartTime, pauseStart;
465 char ics_handle[MSG_SIZ];
466 int have_set_title = 0;
468 /* animateTraining preserves the state of appData.animate
469 * when Training mode is activated. This allows the
470 * response to be animated when appData.animate == TRUE and
471 * appData.animateDragging == TRUE.
473 Boolean animateTraining;
479 Board boards[MAX_MOVES];
480 /* [HGM] Following 7 needed for accurate legality tests: */
481 signed char castlingRank[BOARD_FILES]; // and corresponding ranks
482 signed char initialRights[BOARD_FILES];
483 int nrCastlingRights; // For TwoKings, or to implement castling-unknown status
484 int initialRulePlies, FENrulePlies;
485 FILE *serverMoves = NULL; // next two for broadcasting (/serverMoves option)
487 Boolean shuffleOpenings;
488 int mute; // mute all sounds
490 // [HGM] vari: next 12 to save and restore variations
491 #define MAX_VARIATIONS 10
492 int framePtr = MAX_MOVES-1; // points to free stack entry
494 int savedFirst[MAX_VARIATIONS];
495 int savedLast[MAX_VARIATIONS];
496 int savedFramePtr[MAX_VARIATIONS];
497 char *savedDetails[MAX_VARIATIONS];
498 ChessMove savedResult[MAX_VARIATIONS];
500 void PushTail P((int firstMove, int lastMove));
501 Boolean PopTail P((Boolean annotate));
502 void PushInner P((int firstMove, int lastMove));
503 void PopInner P((Boolean annotate));
504 void CleanupTail P((void));
506 ChessSquare FIDEArray[2][BOARD_FILES] = {
507 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
508 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
509 { BlackRook, BlackKnight, BlackBishop, BlackQueen,
510 BlackKing, BlackBishop, BlackKnight, BlackRook }
513 ChessSquare twoKingsArray[2][BOARD_FILES] = {
514 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
515 WhiteKing, WhiteKing, WhiteKnight, WhiteRook },
516 { BlackRook, BlackKnight, BlackBishop, BlackQueen,
517 BlackKing, BlackKing, BlackKnight, BlackRook }
520 ChessSquare KnightmateArray[2][BOARD_FILES] = {
521 { WhiteRook, WhiteMan, WhiteBishop, WhiteQueen,
522 WhiteUnicorn, WhiteBishop, WhiteMan, WhiteRook },
523 { BlackRook, BlackMan, BlackBishop, BlackQueen,
524 BlackUnicorn, BlackBishop, BlackMan, BlackRook }
527 ChessSquare SpartanArray[2][BOARD_FILES] = {
528 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
529 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
530 { BlackAlfil, BlackMarshall, BlackKing, BlackDragon,
531 BlackDragon, BlackKing, BlackAngel, BlackAlfil }
534 ChessSquare fairyArray[2][BOARD_FILES] = { /* [HGM] Queen side differs from King side */
535 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
536 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
537 { BlackCardinal, BlackAlfil, BlackMarshall, BlackAngel,
538 BlackKing, BlackMarshall, BlackAlfil, BlackCardinal }
541 ChessSquare ShatranjArray[2][BOARD_FILES] = { /* [HGM] (movGen knows about Shatranj Q and P) */
542 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteKing,
543 WhiteFerz, WhiteAlfil, WhiteKnight, WhiteRook },
544 { BlackRook, BlackKnight, BlackAlfil, BlackKing,
545 BlackFerz, BlackAlfil, BlackKnight, BlackRook }
548 ChessSquare makrukArray[2][BOARD_FILES] = { /* [HGM] (movGen knows about Shatranj Q and P) */
549 { WhiteRook, WhiteKnight, WhiteMan, WhiteKing,
550 WhiteFerz, WhiteMan, WhiteKnight, WhiteRook },
551 { BlackRook, BlackKnight, BlackMan, BlackFerz,
552 BlackKing, BlackMan, BlackKnight, BlackRook }
556 #if (BOARD_FILES>=10)
557 ChessSquare ShogiArray[2][BOARD_FILES] = {
558 { WhiteQueen, WhiteKnight, WhiteFerz, WhiteWazir,
559 WhiteKing, WhiteWazir, WhiteFerz, WhiteKnight, WhiteQueen },
560 { BlackQueen, BlackKnight, BlackFerz, BlackWazir,
561 BlackKing, BlackWazir, BlackFerz, BlackKnight, BlackQueen }
564 ChessSquare XiangqiArray[2][BOARD_FILES] = {
565 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteFerz,
566 WhiteWazir, WhiteFerz, WhiteAlfil, WhiteKnight, WhiteRook },
567 { BlackRook, BlackKnight, BlackAlfil, BlackFerz,
568 BlackWazir, BlackFerz, BlackAlfil, BlackKnight, BlackRook }
571 ChessSquare CapablancaArray[2][BOARD_FILES] = {
572 { WhiteRook, WhiteKnight, WhiteAngel, WhiteBishop, WhiteQueen,
573 WhiteKing, WhiteBishop, WhiteMarshall, WhiteKnight, WhiteRook },
574 { BlackRook, BlackKnight, BlackAngel, BlackBishop, BlackQueen,
575 BlackKing, BlackBishop, BlackMarshall, BlackKnight, BlackRook }
578 ChessSquare GreatArray[2][BOARD_FILES] = {
579 { WhiteDragon, WhiteKnight, WhiteAlfil, WhiteGrasshopper, WhiteKing,
580 WhiteSilver, WhiteCardinal, WhiteAlfil, WhiteKnight, WhiteDragon },
581 { BlackDragon, BlackKnight, BlackAlfil, BlackGrasshopper, BlackKing,
582 BlackSilver, BlackCardinal, BlackAlfil, BlackKnight, BlackDragon },
585 ChessSquare JanusArray[2][BOARD_FILES] = {
586 { WhiteRook, WhiteAngel, WhiteKnight, WhiteBishop, WhiteKing,
587 WhiteQueen, WhiteBishop, WhiteKnight, WhiteAngel, WhiteRook },
588 { BlackRook, BlackAngel, BlackKnight, BlackBishop, BlackKing,
589 BlackQueen, BlackBishop, BlackKnight, BlackAngel, BlackRook }
592 ChessSquare GrandArray[2][BOARD_FILES] = {
593 { EmptySquare, WhiteKnight, WhiteBishop, WhiteQueen, WhiteKing,
594 WhiteMarshall, WhiteAngel, WhiteBishop, WhiteKnight, EmptySquare },
595 { EmptySquare, BlackKnight, BlackBishop, BlackQueen, BlackKing,
596 BlackMarshall, BlackAngel, BlackBishop, BlackKnight, EmptySquare }
600 ChessSquare GothicArray[2][BOARD_FILES] = {
601 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen, WhiteMarshall,
602 WhiteKing, WhiteAngel, WhiteBishop, WhiteKnight, WhiteRook },
603 { BlackRook, BlackKnight, BlackBishop, BlackQueen, BlackMarshall,
604 BlackKing, BlackAngel, BlackBishop, BlackKnight, BlackRook }
607 #define GothicArray CapablancaArray
611 ChessSquare FalconArray[2][BOARD_FILES] = {
612 { WhiteRook, WhiteKnight, WhiteBishop, WhiteFalcon, WhiteQueen,
613 WhiteKing, WhiteFalcon, WhiteBishop, WhiteKnight, WhiteRook },
614 { BlackRook, BlackKnight, BlackBishop, BlackFalcon, BlackQueen,
615 BlackKing, BlackFalcon, BlackBishop, BlackKnight, BlackRook }
618 #define FalconArray CapablancaArray
621 #else // !(BOARD_FILES>=10)
622 #define XiangqiPosition FIDEArray
623 #define CapablancaArray FIDEArray
624 #define GothicArray FIDEArray
625 #define GreatArray FIDEArray
626 #endif // !(BOARD_FILES>=10)
628 #if (BOARD_FILES>=12)
629 ChessSquare CourierArray[2][BOARD_FILES] = {
630 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteBishop, WhiteMan, WhiteKing,
631 WhiteFerz, WhiteWazir, WhiteBishop, WhiteAlfil, WhiteKnight, WhiteRook },
632 { BlackRook, BlackKnight, BlackAlfil, BlackBishop, BlackMan, BlackKing,
633 BlackFerz, BlackWazir, BlackBishop, BlackAlfil, BlackKnight, BlackRook }
635 #else // !(BOARD_FILES>=12)
636 #define CourierArray CapablancaArray
637 #endif // !(BOARD_FILES>=12)
640 Board initialPosition;
643 /* Convert str to a rating. Checks for special cases of "----",
645 "++++", etc. Also strips ()'s */
647 string_to_rating (char *str)
649 while(*str && !isdigit(*str)) ++str;
651 return 0; /* One of the special "no rating" cases */
659 /* Init programStats */
660 programStats.movelist[0] = 0;
661 programStats.depth = 0;
662 programStats.nr_moves = 0;
663 programStats.moves_left = 0;
664 programStats.nodes = 0;
665 programStats.time = -1; // [HGM] PGNtime: make invalid to recognize engine output
666 programStats.score = 0;
667 programStats.got_only_move = 0;
668 programStats.got_fail = 0;
669 programStats.line_is_book = 0;
674 { // [HGM] moved some code here from InitBackend1 that has to be done after both engines have contributed their settings
675 if (appData.firstPlaysBlack) {
676 first.twoMachinesColor = "black\n";
677 second.twoMachinesColor = "white\n";
679 first.twoMachinesColor = "white\n";
680 second.twoMachinesColor = "black\n";
683 first.other = &second;
684 second.other = &first;
687 if(appData.timeOddsMode) {
688 norm = appData.timeOdds[0];
689 if(norm > appData.timeOdds[1]) norm = appData.timeOdds[1];
691 first.timeOdds = appData.timeOdds[0]/norm;
692 second.timeOdds = appData.timeOdds[1]/norm;
695 if(programVersion) free(programVersion);
696 if (appData.noChessProgram) {
697 programVersion = (char*) malloc(5 + strlen(PACKAGE_STRING));
698 sprintf(programVersion, "%s", PACKAGE_STRING);
700 /* [HGM] tidy: use tidy name, in stead of full pathname (which was probably a bug due to / vs \ ) */
701 programVersion = (char*) malloc(8 + strlen(PACKAGE_STRING) + strlen(first.tidy));
702 sprintf(programVersion, "%s + %s", PACKAGE_STRING, first.tidy);
707 UnloadEngine (ChessProgramState *cps)
709 /* Kill off first chess program */
710 if (cps->isr != NULL)
711 RemoveInputSource(cps->isr);
714 if (cps->pr != NoProc) {
716 DoSleep( appData.delayBeforeQuit );
717 SendToProgram("quit\n", cps);
718 DoSleep( appData.delayAfterQuit );
719 DestroyChildProcess(cps->pr, cps->useSigterm);
722 if(appData.debugMode) fprintf(debugFP, "Unload %s\n", cps->which);
726 ClearOptions (ChessProgramState *cps)
729 cps->nrOptions = cps->comboCnt = 0;
730 for(i=0; i<MAX_OPTIONS; i++) {
731 cps->option[i].min = cps->option[i].max = cps->option[i].value = 0;
732 cps->option[i].textValue = 0;
736 char *engineNames[] = {
737 /* TRANSLATORS: "first" is the first of possible two chess engines. It is inserted into strings
738 such as "%s engine" / "%s chess program" / "%s machine" - all meaning the same thing */
740 /* TRANSLATORS: "second" is the second of possible two chess engines. It is inserted into strings
741 such as "%s engine" / "%s chess program" / "%s machine" - all meaning the same thing */
746 InitEngine (ChessProgramState *cps, int n)
747 { // [HGM] all engine initialiation put in a function that does one engine
751 cps->which = engineNames[n];
752 cps->maybeThinking = FALSE;
756 cps->sendDrawOffers = 1;
758 cps->program = appData.chessProgram[n];
759 cps->host = appData.host[n];
760 cps->dir = appData.directory[n];
761 cps->initString = appData.engInitString[n];
762 cps->computerString = appData.computerString[n];
763 cps->useSigint = TRUE;
764 cps->useSigterm = TRUE;
765 cps->reuse = appData.reuse[n];
766 cps->nps = appData.NPS[n]; // [HGM] nps: copy nodes per second
767 cps->useSetboard = FALSE;
769 cps->usePing = FALSE;
772 cps->usePlayother = FALSE;
773 cps->useColors = TRUE;
774 cps->useUsermove = FALSE;
775 cps->sendICS = FALSE;
776 cps->sendName = appData.icsActive;
777 cps->sdKludge = FALSE;
778 cps->stKludge = FALSE;
779 TidyProgramName(cps->program, cps->host, cps->tidy);
781 safeStrCpy(cps->variants, appData.variant, MSG_SIZ);
782 cps->analysisSupport = 2; /* detect */
783 cps->analyzing = FALSE;
784 cps->initDone = FALSE;
786 /* New features added by Tord: */
787 cps->useFEN960 = FALSE;
788 cps->useOOCastle = TRUE;
789 /* End of new features added by Tord. */
790 cps->fenOverride = appData.fenOverride[n];
792 /* [HGM] time odds: set factor for each machine */
793 cps->timeOdds = appData.timeOdds[n];
795 /* [HGM] secondary TC: how to handle sessions that do not fit in 'level'*/
796 cps->accumulateTC = appData.accumulateTC[n];
797 cps->maxNrOfSessions = 1;
802 cps->supportsNPS = UNKNOWN;
803 cps->memSize = FALSE;
804 cps->maxCores = FALSE;
805 cps->egtFormats[0] = NULLCHAR;
808 cps->optionSettings = appData.engOptions[n];
810 cps->scoreIsAbsolute = appData.scoreIsAbsolute[n]; /* [AS] */
811 cps->isUCI = appData.isUCI[n]; /* [AS] */
812 cps->hasOwnBookUCI = appData.hasOwnBookUCI[n]; /* [AS] */
814 if (appData.protocolVersion[n] > PROTOVER
815 || appData.protocolVersion[n] < 1)
820 len = snprintf(buf, MSG_SIZ, _("protocol version %d not supported"),
821 appData.protocolVersion[n]);
822 if( (len >= MSG_SIZ) && appData.debugMode )
823 fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
825 DisplayFatalError(buf, 0, 2);
829 cps->protocolVersion = appData.protocolVersion[n];
832 InitEngineUCI( installDir, cps ); // [HGM] moved here from winboard.c, to make available in xboard
833 ParseFeatures(appData.featureDefaults, cps);
836 ChessProgramState *savCps;
842 if(WaitForEngine(savCps, LoadEngine)) return;
843 CommonEngineInit(); // recalculate time odds
844 if(gameInfo.variant != StringToVariant(appData.variant)) {
845 // we changed variant when loading the engine; this forces us to reset
846 Reset(TRUE, savCps != &first);
847 EditGameEvent(); // for consistency with other path, as Reset changes mode
849 InitChessProgram(savCps, FALSE);
850 SendToProgram("force\n", savCps);
851 DisplayMessage("", "");
852 if (startedFromSetupPosition) SendBoard(savCps, backwardMostMove);
853 for (i = backwardMostMove; i < forwardMostMove; i++) SendMoveToProgram(i, savCps);
859 ReplaceEngine (ChessProgramState *cps, int n)
863 appData.noChessProgram = FALSE;
864 appData.clockMode = TRUE;
867 if(n) return; // only startup first engine immediately; second can wait
868 savCps = cps; // parameter to LoadEngine passed as globals, to allow scheduled calling :-(
872 extern char *engineName, *engineDir, *engineChoice, *engineLine, *nickName, *params;
873 extern Boolean isUCI, hasBook, storeVariant, v1, addToList, useNick;
875 static char resetOptions[] =
876 "-reuse -firstIsUCI false -firstHasOwnBookUCI true -firstTimeOdds 1 "
877 "-firstInitString \"" INIT_STRING "\" -firstComputerString \"" COMPUTER_STRING "\" "
878 "-firstOptions \"\" -firstNPS -1 -fn \"\"";
881 FloatToFront(char **list, char *engineLine)
883 char buf[MSG_SIZ], tidy[MSG_SIZ], *p = buf, *q, *r = buf;
885 if(appData.recentEngines <= 0) return;
886 TidyProgramName(engineLine, "localhost", tidy+1);
887 tidy[0] = buf[0] = '\n'; strcat(tidy, "\n");
888 strncpy(buf+1, *list, MSG_SIZ-50);
889 if(p = strstr(buf, tidy)) { // tidy name appears in list
890 q = strchr(++p, '\n'); if(q == NULL) return; // malformed, don't touch
891 while(*p++ = *++q); // squeeze out
893 strcat(tidy, buf+1); // put list behind tidy name
894 p = tidy + 1; while(q = strchr(p, '\n')) i++, r = p, p = q + 1; // count entries in new list
895 if(i > appData.recentEngines) *r = NULLCHAR; // if maximum rached, strip off last
896 ASSIGN(*list, tidy+1);
900 Load (ChessProgramState *cps, int i)
902 char *p, *q, buf[MSG_SIZ], command[MSG_SIZ], buf2[MSG_SIZ];
903 if(engineLine && engineLine[0]) { // an engine was selected from the combo box
904 snprintf(buf, MSG_SIZ, "-fcp %s", engineLine);
905 SwapEngines(i); // kludge to parse -f* / -first* like it is -s* / -second*
906 ParseArgsFromString(resetOptions); appData.fenOverride[0] = NULL; appData.pvSAN[0] = FALSE;
907 appData.firstProtocolVersion = PROTOVER;
908 ParseArgsFromString(buf);
910 ReplaceEngine(cps, i);
911 FloatToFront(&appData.recentEngineList, engineLine);
915 while(q = strchr(p, SLASH)) p = q+1;
916 if(*p== NULLCHAR) { DisplayError(_("You did not specify the engine executable"), 0); return; }
917 if(engineDir[0] != NULLCHAR)
918 appData.directory[i] = engineDir;
919 else if(p != engineName) { // derive directory from engine path, when not given
921 appData.directory[i] = strdup(engineName);
923 if(SLASH == '/' && p - engineName > 1) *(p -= 2) = '.'; // for XBoard use ./exeName as command after split!
924 } else appData.directory[i] = ".";
926 if(strchr(p, ' ') && !strchr(p, '"')) snprintf(buf2, MSG_SIZ, "\"%s\"", p), p = buf2; // quote if it contains spaces
927 snprintf(command, MSG_SIZ, "%s %s", p, params);
930 appData.chessProgram[i] = strdup(p);
931 appData.isUCI[i] = isUCI;
932 appData.protocolVersion[i] = v1 ? 1 : PROTOVER;
933 appData.hasOwnBookUCI[i] = hasBook;
934 if(!nickName[0]) useNick = FALSE;
935 if(useNick) ASSIGN(appData.pgnName[i], nickName);
939 q = firstChessProgramNames;
940 if(nickName[0]) snprintf(buf, MSG_SIZ, "\"%s\" -fcp ", nickName); else buf[0] = NULLCHAR;
941 quote = strchr(p, '"') ? '\'' : '"'; // use single quotes around engine command if it contains double quotes
942 snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), "%c%s%c -fd \"%s\"%s%s%s%s%s%s%s%s\n",
943 quote, p, quote, appData.directory[i],
944 useNick ? " -fn \"" : "",
945 useNick ? nickName : "",
947 v1 ? " -firstProtocolVersion 1" : "",
948 hasBook ? "" : " -fNoOwnBookUCI",
949 isUCI ? (isUCI == TRUE ? " -fUCI" : gameInfo.variant == VariantShogi ? " -fUSI" : " -fUCCI") : "",
950 storeVariant ? " -variant " : "",
951 storeVariant ? VariantName(gameInfo.variant) : "");
952 firstChessProgramNames = malloc(len = strlen(q) + strlen(buf) + 1);
953 snprintf(firstChessProgramNames, len, "%s%s", q, buf);
955 FloatToFront(&appData.recentEngineList, buf);
957 ReplaceEngine(cps, i);
963 int matched, min, sec;
965 * Parse timeControl resource
967 if (!ParseTimeControl(appData.timeControl, appData.timeIncrement,
968 appData.movesPerSession)) {
970 snprintf(buf, sizeof(buf), _("bad timeControl option %s"), appData.timeControl);
971 DisplayFatalError(buf, 0, 2);
975 * Parse searchTime resource
977 if (*appData.searchTime != NULLCHAR) {
978 matched = sscanf(appData.searchTime, "%d:%d", &min, &sec);
980 searchTime = min * 60;
981 } else if (matched == 2) {
982 searchTime = min * 60 + sec;
985 snprintf(buf, sizeof(buf), _("bad searchTime option %s"), appData.searchTime);
986 DisplayFatalError(buf, 0, 2);
995 ShowThinkingEvent(); // [HGM] thinking: make sure post/nopost state is set according to options
996 startVariant = StringToVariant(appData.variant); // [HGM] nicks: remember original variant
998 GetTimeMark(&programStartTime);
999 srandom((programStartTime.ms + 1000*programStartTime.sec)*0x1001001); // [HGM] book: makes sure random is unpredictabe to msec level
1000 appData.seedBase = random() + (random()<<15);
1001 pauseStart = programStartTime; pauseStart.sec -= 100; // [HGM] matchpause: fake a pause that has long since ended
1003 ClearProgramStats();
1004 programStats.ok_to_send = 1;
1005 programStats.seen_stat = 0;
1008 * Initialize game list
1014 * Internet chess server status
1016 if (appData.icsActive) {
1017 appData.matchMode = FALSE;
1018 appData.matchGames = 0;
1020 appData.noChessProgram = !appData.zippyPlay;
1022 appData.zippyPlay = FALSE;
1023 appData.zippyTalk = FALSE;
1024 appData.noChessProgram = TRUE;
1026 if (*appData.icsHelper != NULLCHAR) {
1027 appData.useTelnet = TRUE;
1028 appData.telnetProgram = appData.icsHelper;
1031 appData.zippyTalk = appData.zippyPlay = FALSE;
1034 /* [AS] Initialize pv info list [HGM] and game state */
1038 for( i=0; i<=framePtr; i++ ) {
1039 pvInfoList[i].depth = -1;
1040 boards[i][EP_STATUS] = EP_NONE;
1041 for( j=0; j<BOARD_FILES-2; j++ ) boards[i][CASTLING][j] = NoRights;
1047 /* [AS] Adjudication threshold */
1048 adjudicateLossThreshold = appData.adjudicateLossThreshold;
1050 InitEngine(&first, 0);
1051 InitEngine(&second, 1);
1054 pairing.which = "pairing"; // pairing engine
1055 pairing.pr = NoProc;
1057 pairing.program = appData.pairingEngine;
1058 pairing.host = "localhost";
1061 if (appData.icsActive) {
1062 appData.clockMode = TRUE; /* changes dynamically in ICS mode */
1063 } else if (appData.noChessProgram) { // [HGM] st: searchTime mode now also is clockMode
1064 appData.clockMode = FALSE;
1065 first.sendTime = second.sendTime = 0;
1069 /* Override some settings from environment variables, for backward
1070 compatibility. Unfortunately it's not feasible to have the env
1071 vars just set defaults, at least in xboard. Ugh.
1073 if (appData.icsActive && (appData.zippyPlay || appData.zippyTalk)) {
1078 if (!appData.icsActive) {
1082 /* Check for variants that are supported only in ICS mode,
1083 or not at all. Some that are accepted here nevertheless
1084 have bugs; see comments below.
1086 VariantClass variant = StringToVariant(appData.variant);
1088 case VariantBughouse: /* need four players and two boards */
1089 case VariantKriegspiel: /* need to hide pieces and move details */
1090 /* case VariantFischeRandom: (Fabien: moved below) */
1091 len = snprintf(buf,MSG_SIZ, _("Variant %s supported only in ICS mode"), appData.variant);
1092 if( (len >= MSG_SIZ) && appData.debugMode )
1093 fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
1095 DisplayFatalError(buf, 0, 2);
1098 case VariantUnknown:
1099 case VariantLoadable:
1109 len = snprintf(buf, MSG_SIZ, _("Unknown variant name %s"), appData.variant);
1110 if( (len >= MSG_SIZ) && appData.debugMode )
1111 fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
1113 DisplayFatalError(buf, 0, 2);
1116 case VariantXiangqi: /* [HGM] repetition rules not implemented */
1117 case VariantFairy: /* [HGM] TestLegality definitely off! */
1118 case VariantGothic: /* [HGM] should work */
1119 case VariantCapablanca: /* [HGM] should work */
1120 case VariantCourier: /* [HGM] initial forced moves not implemented */
1121 case VariantShogi: /* [HGM] could still mate with pawn drop */
1122 case VariantKnightmate: /* [HGM] should work */
1123 case VariantCylinder: /* [HGM] untested */
1124 case VariantFalcon: /* [HGM] untested */
1125 case VariantCrazyhouse: /* holdings not shown, ([HGM] fixed that!)
1126 offboard interposition not understood */
1127 case VariantNormal: /* definitely works! */
1128 case VariantWildCastle: /* pieces not automatically shuffled */
1129 case VariantNoCastle: /* pieces not automatically shuffled */
1130 case VariantFischeRandom: /* [HGM] works and shuffles pieces */
1131 case VariantLosers: /* should work except for win condition,
1132 and doesn't know captures are mandatory */
1133 case VariantSuicide: /* should work except for win condition,
1134 and doesn't know captures are mandatory */
1135 case VariantGiveaway: /* should work except for win condition,
1136 and doesn't know captures are mandatory */
1137 case VariantTwoKings: /* should work */
1138 case VariantAtomic: /* should work except for win condition */
1139 case Variant3Check: /* should work except for win condition */
1140 case VariantShatranj: /* should work except for all win conditions */
1141 case VariantMakruk: /* should work except for draw countdown */
1142 case VariantBerolina: /* might work if TestLegality is off */
1143 case VariantCapaRandom: /* should work */
1144 case VariantJanus: /* should work */
1145 case VariantSuper: /* experimental */
1146 case VariantGreat: /* experimental, requires legality testing to be off */
1147 case VariantSChess: /* S-Chess, should work */
1148 case VariantGrand: /* should work */
1149 case VariantSpartan: /* should work */
1157 NextIntegerFromString (char ** str, long * value)
1162 while( *s == ' ' || *s == '\t' ) {
1168 if( *s >= '0' && *s <= '9' ) {
1169 while( *s >= '0' && *s <= '9' ) {
1170 *value = *value * 10 + (*s - '0');
1183 NextTimeControlFromString (char ** str, long * value)
1186 int result = NextIntegerFromString( str, &temp );
1189 *value = temp * 60; /* Minutes */
1190 if( **str == ':' ) {
1192 result = NextIntegerFromString( str, &temp );
1193 *value += temp; /* Seconds */
1201 NextSessionFromString (char ** str, int *moves, long * tc, long *inc, int *incType)
1202 { /* [HGM] routine added to read '+moves/time' for secondary time control. */
1203 int result = -1, type = 0; long temp, temp2;
1205 if(**str != ':') return -1; // old params remain in force!
1207 if(**str == '*') type = *(*str)++, temp = 0; // sandclock TC
1208 if( NextIntegerFromString( str, &temp ) ) return -1;
1209 if(type) { *moves = 0; *tc = temp * 500; *inc = temp * 1000; *incType = '*'; return 0; }
1212 /* time only: incremental or sudden-death time control */
1213 if(**str == '+') { /* increment follows; read it */
1215 if(**str == '!') type = *(*str)++; // Bronstein TC
1216 if(result = NextIntegerFromString( str, &temp2)) return -1;
1217 *inc = temp2 * 1000;
1218 if(**str == '.') { // read fraction of increment
1219 char *start = ++(*str);
1220 if(result = NextIntegerFromString( str, &temp2)) return -1;
1222 while(start++ < *str) temp2 /= 10;
1226 *moves = 0; *tc = temp * 1000; *incType = type;
1230 (*str)++; /* classical time control */
1231 result = NextIntegerFromString( str, &temp2); // NOTE: already converted to seconds by ParseTimeControl()
1243 GetTimeQuota (int movenr, int lastUsed, char *tcString)
1244 { /* [HGM] get time to add from the multi-session time-control string */
1245 int incType, moves=1; /* kludge to force reading of first session */
1246 long time, increment;
1249 if(!*s) return 0; // empty TC string means we ran out of the last sudden-death version
1251 if(moves) NextSessionFromString(&s, &moves, &time, &increment, &incType);
1252 nextSession = s; suddenDeath = moves == 0 && increment == 0;
1253 if(movenr == -1) return time; /* last move before new session */
1254 if(incType == '*') increment = 0; else // for sandclock, time is added while not thinking
1255 if(incType == '!' && lastUsed < increment) increment = lastUsed;
1256 if(!moves) return increment; /* current session is incremental */
1257 if(movenr >= 0) movenr -= moves; /* we already finished this session */
1258 } while(movenr >= -1); /* try again for next session */
1260 return 0; // no new time quota on this move
1264 ParseTimeControl (char *tc, float ti, int mps)
1268 char buf[MSG_SIZ], buf2[MSG_SIZ], *mytc = tc;
1271 if(ti >= 0 && !strchr(tc, '+') && !strchr(tc, '/') ) mps = 0;
1272 if(!strchr(tc, '+') && !strchr(tc, '/') && sscanf(tc, "%d:%d", &min, &sec) >= 1)
1273 sprintf(mytc=buf2, "%d", 60*min+sec); // convert 'classical' min:sec tc string to seconds
1277 snprintf(buf, MSG_SIZ, ":%d/%s+%g", mps, mytc, ti);
1279 snprintf(buf, MSG_SIZ, ":%s+%g", mytc, ti);
1282 snprintf(buf, MSG_SIZ, ":%d/%s", mps, mytc);
1284 snprintf(buf, MSG_SIZ, ":%s", mytc);
1286 fullTimeControlString = StrSave(buf); // this should now be in PGN format
1288 if( NextTimeControlFromString( &tc, &tc1 ) != 0 ) {
1293 /* Parse second time control */
1296 if( NextTimeControlFromString( &tc, &tc2 ) != 0 ) {
1304 timeControl_2 = tc2 * 1000;
1314 timeControl = tc1 * 1000;
1317 timeIncrement = ti * 1000; /* convert to ms */
1318 movesPerSession = 0;
1321 movesPerSession = mps;
1329 if (appData.debugMode) {
1330 fprintf(debugFP, "%s\n", programVersion);
1332 ASSIGN(currentDebugFile, appData.nameOfDebugFile); // [HGM] debug split: remember initial name in use
1334 set_cont_sequence(appData.wrapContSeq);
1335 if (appData.matchGames > 0) {
1336 appData.matchMode = TRUE;
1337 } else if (appData.matchMode) {
1338 appData.matchGames = 1;
1340 if(appData.matchMode && appData.sameColorGames > 0) /* [HGM] alternate: overrule matchGames */
1341 appData.matchGames = appData.sameColorGames;
1342 if(appData.rewindIndex > 1) { /* [HGM] autoinc: rewind implies auto-increment and overrules given index */
1343 if(appData.loadPositionIndex >= 0) appData.loadPositionIndex = -1;
1344 if(appData.loadGameIndex >= 0) appData.loadGameIndex = -1;
1347 if (appData.noChessProgram || first.protocolVersion == 1) {
1350 /* kludge: allow timeout for initial "feature" commands */
1352 DisplayMessage("", _("Starting chess program"));
1353 ScheduleDelayedEvent(InitBackEnd3, FEATURE_TIMEOUT);
1358 CalculateIndex (int index, int gameNr)
1359 { // [HGM] autoinc: absolute way to determine load index from game number (taking auto-inc and rewind into account)
1361 if(index > 0) return index; // fixed nmber
1362 if(index == 0) return 1;
1363 res = (index == -1 ? gameNr : (gameNr-1)/2 + 1); // autoinc
1364 if(appData.rewindIndex > 0) res = (res-1) % appData.rewindIndex + 1; // rewind
1369 LoadGameOrPosition (int gameNr)
1370 { // [HGM] taken out of MatchEvent and NextMatchGame (to combine it)
1371 if (*appData.loadGameFile != NULLCHAR) {
1372 if (!LoadGameFromFile(appData.loadGameFile,
1373 CalculateIndex(appData.loadGameIndex, gameNr),
1374 appData.loadGameFile, FALSE)) {
1375 DisplayFatalError(_("Bad game file"), 0, 1);
1378 } else if (*appData.loadPositionFile != NULLCHAR) {
1379 if (!LoadPositionFromFile(appData.loadPositionFile,
1380 CalculateIndex(appData.loadPositionIndex, gameNr),
1381 appData.loadPositionFile)) {
1382 DisplayFatalError(_("Bad position file"), 0, 1);
1390 ReserveGame (int gameNr, char resChar)
1392 FILE *tf = fopen(appData.tourneyFile, "r+");
1393 char *p, *q, c, buf[MSG_SIZ];
1394 if(tf == NULL) { nextGame = appData.matchGames + 1; return; } // kludge to terminate match
1395 safeStrCpy(buf, lastMsg, MSG_SIZ);
1396 DisplayMessage(_("Pick new game"), "");
1397 flock(fileno(tf), LOCK_EX); // lock the tourney file while we are messing with it
1398 ParseArgsFromFile(tf);
1399 p = q = appData.results;
1400 if(appData.debugMode) {
1401 char *r = appData.participants;
1402 fprintf(debugFP, "results = '%s'\n", p);
1403 while(*r) fprintf(debugFP, *r >= ' ' ? "%c" : "\\%03o", *r), r++;
1404 fprintf(debugFP, "\n");
1406 while(*q && *q != ' ') q++; // get first un-played game (could be beyond end!)
1408 q = malloc(strlen(p) + 2); // could be arbitrary long, but allow to extend by one!
1409 safeStrCpy(q, p, strlen(p) + 2);
1410 if(gameNr >= 0) q[gameNr] = resChar; // replace '*' with result
1411 if(appData.debugMode) fprintf(debugFP, "pick next game from '%s': %d\n", q, nextGame);
1412 if(nextGame <= appData.matchGames && resChar != ' ' && !abortMatch) { // reserve next game if tourney not yet done
1413 if(q[nextGame] == NULLCHAR) q[nextGame+1] = NULLCHAR; // append one char
1416 fseek(tf, -(strlen(p)+4), SEEK_END);
1418 if(c != '"') // depending on DOS or Unix line endings we can be one off
1419 fseek(tf, -(strlen(p)+2), SEEK_END);
1420 else fseek(tf, -(strlen(p)+3), SEEK_END);
1421 fprintf(tf, "%s\"\n", q); fclose(tf); // update, and flush by closing
1422 DisplayMessage(buf, "");
1423 free(p); appData.results = q;
1424 if(nextGame <= appData.matchGames && resChar != ' ' && !abortMatch &&
1425 (gameNr < 0 || nextGame / appData.defaultMatchGames != gameNr / appData.defaultMatchGames)) {
1426 int round = appData.defaultMatchGames * appData.tourneyType;
1427 if(gameNr < 0 || appData.tourneyType < 1 || // gauntlet engine can always stay loaded as first engine
1428 appData.tourneyType > 1 && nextGame/round != gameNr/round) // in multi-gauntlet change only after round
1429 UnloadEngine(&first); // next game belongs to other pairing;
1430 UnloadEngine(&second); // already unload the engines, so TwoMachinesEvent will load new ones.
1432 if(appData.debugMode) fprintf(debugFP, "Reserved, next=%d, nr=%d\n", nextGame, gameNr);
1436 MatchEvent (int mode)
1437 { // [HGM] moved out of InitBackend3, to make it callable when match starts through menu
1439 if(matchMode) { // already in match mode: switch it off
1441 if(!appData.tourneyFile[0]) appData.matchGames = matchGame; // kludge to let match terminate after next game.
1444 // if(gameMode != BeginningOfGame) {
1445 // DisplayError(_("You can only start a match from the initial position."), 0);
1449 if(mode == 2) appData.matchGames = appData.defaultMatchGames;
1450 /* Set up machine vs. machine match */
1452 NextTourneyGame(-1, &dummy); // sets appData.matchGames if this is tourney, to make sure ReserveGame knows it
1453 if(appData.tourneyFile[0]) {
1455 if(nextGame > appData.matchGames) {
1457 if(strchr(appData.results, '*') == NULL) {
1459 appData.tourneyCycles++;
1460 if(f = WriteTourneyFile(appData.results, NULL)) { // make a tourney file with increased number of cycles
1462 NextTourneyGame(-1, &dummy);
1464 if(nextGame <= appData.matchGames) {
1465 DisplayNote(_("You restarted an already completed tourney\nOne more cycle will now be added to it\nGames commence in 10 sec"));
1467 ScheduleDelayedEvent(NextMatchGame, 10000);
1472 snprintf(buf, MSG_SIZ, _("All games in tourney '%s' are already played or playing"), appData.tourneyFile);
1473 DisplayError(buf, 0);
1474 appData.tourneyFile[0] = 0;
1478 if (appData.noChessProgram) { // [HGM] in tourney engines are loaded automatically
1479 DisplayFatalError(_("Can't have a match with no chess programs"),
1484 matchGame = roundNr = 1;
1485 first.matchWins = second.matchWins = 0; // [HGM] match: needed in later matches
1489 char *comboLine = NULL; // [HGM] recent: WinBoard's first-engine combobox line
1492 InitBackEnd3 P((void))
1494 GameMode initialMode;
1498 InitChessProgram(&first, startedFromSetupPosition);
1500 if(!appData.noChessProgram) { /* [HGM] tidy: redo program version to use name from myname feature */
1501 free(programVersion);
1502 programVersion = (char*) malloc(8 + strlen(PACKAGE_STRING) + strlen(first.tidy));
1503 sprintf(programVersion, "%s + %s", PACKAGE_STRING, first.tidy);
1504 FloatToFront(&appData.recentEngineList, comboLine ? comboLine : appData.firstChessProgram);
1507 if (appData.icsActive) {
1509 /* [DM] Make a console window if needed [HGM] merged ifs */
1515 if (*appData.icsCommPort != NULLCHAR)
1516 len = snprintf(buf, MSG_SIZ, _("Could not open comm port %s"),
1517 appData.icsCommPort);
1519 len = snprintf(buf, MSG_SIZ, _("Could not connect to host %s, port %s"),
1520 appData.icsHost, appData.icsPort);
1522 if( (len >= MSG_SIZ) && appData.debugMode )
1523 fprintf(debugFP, "InitBackEnd3: buffer truncated.\n");
1525 DisplayFatalError(buf, err, 1);
1530 AddInputSource(icsPR, FALSE, read_from_ics, &telnetISR);
1532 AddInputSource(NoProc, FALSE, read_from_player, &fromUserISR);
1533 if(appData.keepAlive) // [HGM] alive: schedule sending of dummy 'date' command
1534 ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
1535 } else if (appData.noChessProgram) {
1541 if (*appData.cmailGameName != NULLCHAR) {
1543 OpenLoopback(&cmailPR);
1545 AddInputSource(cmailPR, FALSE, CmailSigHandlerCallBack, &cmailISR);
1549 DisplayMessage("", "");
1550 if (StrCaseCmp(appData.initialMode, "") == 0) {
1551 initialMode = BeginningOfGame;
1552 if(!appData.icsActive && appData.noChessProgram) { // [HGM] could be fall-back
1553 gameMode = MachinePlaysBlack; // "Machine Black" might have been implicitly highlighted
1554 ModeHighlight(); // make sure XBoard knows it is highlighted, so it will un-highlight it
1555 gameMode = BeginningOfGame; // in case BeginningOfGame now means "Edit Position"
1558 } else if (StrCaseCmp(appData.initialMode, "TwoMachines") == 0) {
1559 initialMode = TwoMachinesPlay;
1560 } else if (StrCaseCmp(appData.initialMode, "AnalyzeFile") == 0) {
1561 initialMode = AnalyzeFile;
1562 } else if (StrCaseCmp(appData.initialMode, "Analysis") == 0) {
1563 initialMode = AnalyzeMode;
1564 } else if (StrCaseCmp(appData.initialMode, "MachineWhite") == 0) {
1565 initialMode = MachinePlaysWhite;
1566 } else if (StrCaseCmp(appData.initialMode, "MachineBlack") == 0) {
1567 initialMode = MachinePlaysBlack;
1568 } else if (StrCaseCmp(appData.initialMode, "EditGame") == 0) {
1569 initialMode = EditGame;
1570 } else if (StrCaseCmp(appData.initialMode, "EditPosition") == 0) {
1571 initialMode = EditPosition;
1572 } else if (StrCaseCmp(appData.initialMode, "Training") == 0) {
1573 initialMode = Training;
1575 len = snprintf(buf, MSG_SIZ, _("Unknown initialMode %s"), appData.initialMode);
1576 if( (len >= MSG_SIZ) && appData.debugMode )
1577 fprintf(debugFP, "InitBackEnd3: buffer truncated.\n");
1579 DisplayFatalError(buf, 0, 2);
1583 if (appData.matchMode) {
1584 if(appData.tourneyFile[0]) { // start tourney from command line
1586 if(f = fopen(appData.tourneyFile, "r")) {
1587 ParseArgsFromFile(f); // make sure tourney parmeters re known
1589 appData.clockMode = TRUE;
1591 } else appData.tourneyFile[0] = NULLCHAR; // for now ignore bad tourney file
1594 } else if (*appData.cmailGameName != NULLCHAR) {
1595 /* Set up cmail mode */
1596 ReloadCmailMsgEvent(TRUE);
1598 /* Set up other modes */
1599 if (initialMode == AnalyzeFile) {
1600 if (*appData.loadGameFile == NULLCHAR) {
1601 DisplayFatalError(_("AnalyzeFile mode requires a game file"), 0, 1);
1605 if (*appData.loadGameFile != NULLCHAR) {
1606 (void) LoadGameFromFile(appData.loadGameFile,
1607 appData.loadGameIndex,
1608 appData.loadGameFile, TRUE);
1609 } else if (*appData.loadPositionFile != NULLCHAR) {
1610 (void) LoadPositionFromFile(appData.loadPositionFile,
1611 appData.loadPositionIndex,
1612 appData.loadPositionFile);
1613 /* [HGM] try to make self-starting even after FEN load */
1614 /* to allow automatic setup of fairy variants with wtm */
1615 if(initialMode == BeginningOfGame && !blackPlaysFirst) {
1616 gameMode = BeginningOfGame;
1617 setboardSpoiledMachineBlack = 1;
1619 /* [HGM] loadPos: make that every new game uses the setup */
1620 /* from file as long as we do not switch variant */
1621 if(!blackPlaysFirst) {
1622 startedFromPositionFile = TRUE;
1623 CopyBoard(filePosition, boards[0]);
1626 if (initialMode == AnalyzeMode) {
1627 if (appData.noChessProgram) {
1628 DisplayFatalError(_("Analysis mode requires a chess engine"), 0, 2);
1631 if (appData.icsActive) {
1632 DisplayFatalError(_("Analysis mode does not work with ICS mode"),0,2);
1636 } else if (initialMode == AnalyzeFile) {
1637 appData.showThinking = TRUE; // [HGM] thinking: moved out of ShowThinkingEvent
1638 ShowThinkingEvent();
1640 AnalysisPeriodicEvent(1);
1641 } else if (initialMode == MachinePlaysWhite) {
1642 if (appData.noChessProgram) {
1643 DisplayFatalError(_("MachineWhite mode requires a chess engine"),
1647 if (appData.icsActive) {
1648 DisplayFatalError(_("MachineWhite mode does not work with ICS mode"),
1652 MachineWhiteEvent();
1653 } else if (initialMode == MachinePlaysBlack) {
1654 if (appData.noChessProgram) {
1655 DisplayFatalError(_("MachineBlack mode requires a chess engine"),
1659 if (appData.icsActive) {
1660 DisplayFatalError(_("MachineBlack mode does not work with ICS mode"),
1664 MachineBlackEvent();
1665 } else if (initialMode == TwoMachinesPlay) {
1666 if (appData.noChessProgram) {
1667 DisplayFatalError(_("TwoMachines mode requires a chess engine"),
1671 if (appData.icsActive) {
1672 DisplayFatalError(_("TwoMachines mode does not work with ICS mode"),
1677 } else if (initialMode == EditGame) {
1679 } else if (initialMode == EditPosition) {
1680 EditPositionEvent();
1681 } else if (initialMode == Training) {
1682 if (*appData.loadGameFile == NULLCHAR) {
1683 DisplayFatalError(_("Training mode requires a game file"), 0, 2);
1692 HistorySet (char movelist[][2*MOVE_LEN], int first, int last, int current)
1694 DisplayBook(current+1);
1696 MoveHistorySet( movelist, first, last, current, pvInfoList );
1698 EvalGraphSet( first, last, current, pvInfoList );
1700 MakeEngineOutputTitle();
1704 * Establish will establish a contact to a remote host.port.
1705 * Sets icsPR to a ProcRef for a process (or pseudo-process)
1706 * used to talk to the host.
1707 * Returns 0 if okay, error code if not.
1714 if (*appData.icsCommPort != NULLCHAR) {
1715 /* Talk to the host through a serial comm port */
1716 return OpenCommPort(appData.icsCommPort, &icsPR);
1718 } else if (*appData.gateway != NULLCHAR) {
1719 if (*appData.remoteShell == NULLCHAR) {
1720 /* Use the rcmd protocol to run telnet program on a gateway host */
1721 snprintf(buf, sizeof(buf), "%s %s %s",
1722 appData.telnetProgram, appData.icsHost, appData.icsPort);
1723 return OpenRcmd(appData.gateway, appData.remoteUser, buf, &icsPR);
1726 /* Use the rsh program to run telnet program on a gateway host */
1727 if (*appData.remoteUser == NULLCHAR) {
1728 snprintf(buf, sizeof(buf), "%s %s %s %s %s", appData.remoteShell,
1729 appData.gateway, appData.telnetProgram,
1730 appData.icsHost, appData.icsPort);
1732 snprintf(buf, sizeof(buf), "%s %s -l %s %s %s %s",
1733 appData.remoteShell, appData.gateway,
1734 appData.remoteUser, appData.telnetProgram,
1735 appData.icsHost, appData.icsPort);
1737 return StartChildProcess(buf, "", &icsPR);
1740 } else if (appData.useTelnet) {
1741 return OpenTelnet(appData.icsHost, appData.icsPort, &icsPR);
1744 /* TCP socket interface differs somewhat between
1745 Unix and NT; handle details in the front end.
1747 return OpenTCP(appData.icsHost, appData.icsPort, &icsPR);
1752 EscapeExpand (char *p, char *q)
1753 { // [HGM] initstring: routine to shape up string arguments
1754 while(*p++ = *q++) if(p[-1] == '\\')
1756 case 'n': p[-1] = '\n'; break;
1757 case 'r': p[-1] = '\r'; break;
1758 case 't': p[-1] = '\t'; break;
1759 case '\\': p[-1] = '\\'; break;
1760 case 0: *p = 0; return;
1761 default: p[-1] = q[-1]; break;
1766 show_bytes (FILE *fp, char *buf, int count)
1769 if (*buf < 040 || *(unsigned char *) buf > 0177) {
1770 fprintf(fp, "\\%03o", *buf & 0xff);
1779 /* Returns an errno value */
1781 OutputMaybeTelnet (ProcRef pr, char *message, int count, int *outError)
1783 char buf[8192], *p, *q, *buflim;
1784 int left, newcount, outcount;
1786 if (*appData.icsCommPort != NULLCHAR || appData.useTelnet ||
1787 *appData.gateway != NULLCHAR) {
1788 if (appData.debugMode) {
1789 fprintf(debugFP, ">ICS: ");
1790 show_bytes(debugFP, message, count);
1791 fprintf(debugFP, "\n");
1793 return OutputToProcess(pr, message, count, outError);
1796 buflim = &buf[sizeof(buf)-1]; /* allow 1 byte for expanding last char */
1803 if (appData.debugMode) {
1804 fprintf(debugFP, ">ICS: ");
1805 show_bytes(debugFP, buf, newcount);
1806 fprintf(debugFP, "\n");
1808 outcount = OutputToProcess(pr, buf, newcount, outError);
1809 if (outcount < newcount) return -1; /* to be sure */
1816 } else if (((unsigned char) *p) == TN_IAC) {
1817 *q++ = (char) TN_IAC;
1824 if (appData.debugMode) {
1825 fprintf(debugFP, ">ICS: ");
1826 show_bytes(debugFP, buf, newcount);
1827 fprintf(debugFP, "\n");
1829 outcount = OutputToProcess(pr, buf, newcount, outError);
1830 if (outcount < newcount) return -1; /* to be sure */
1835 read_from_player (InputSourceRef isr, VOIDSTAR closure, char *message, int count, int error)
1837 int outError, outCount;
1838 static int gotEof = 0;
1840 /* Pass data read from player on to ICS */
1843 outCount = OutputMaybeTelnet(icsPR, message, count, &outError);
1844 if (outCount < count) {
1845 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1847 } else if (count < 0) {
1848 RemoveInputSource(isr);
1849 DisplayFatalError(_("Error reading from keyboard"), error, 1);
1850 } else if (gotEof++ > 0) {
1851 RemoveInputSource(isr);
1852 DisplayFatalError(_("Got end of file from keyboard"), 0, 0);
1858 { // [HGM] alive: periodically send dummy (date) command to ICS to prevent time-out
1859 if(!connectionAlive) DisplayFatalError("No response from ICS", 0, 1);
1860 connectionAlive = FALSE; // only sticks if no response to 'date' command.
1861 SendToICS("date\n");
1862 if(appData.keepAlive) ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
1865 /* added routine for printf style output to ics */
1867 ics_printf (char *format, ...)
1869 char buffer[MSG_SIZ];
1872 va_start(args, format);
1873 vsnprintf(buffer, sizeof(buffer), format, args);
1874 buffer[sizeof(buffer)-1] = '\0';
1882 int count, outCount, outError;
1884 if (icsPR == NoProc) return;
1887 outCount = OutputMaybeTelnet(icsPR, s, count, &outError);
1888 if (outCount < count) {
1889 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1893 /* This is used for sending logon scripts to the ICS. Sending
1894 without a delay causes problems when using timestamp on ICC
1895 (at least on my machine). */
1897 SendToICSDelayed (char *s, long msdelay)
1899 int count, outCount, outError;
1901 if (icsPR == NoProc) return;
1904 if (appData.debugMode) {
1905 fprintf(debugFP, ">ICS: ");
1906 show_bytes(debugFP, s, count);
1907 fprintf(debugFP, "\n");
1909 outCount = OutputToProcessDelayed(icsPR, s, count, &outError,
1911 if (outCount < count) {
1912 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1917 /* Remove all highlighting escape sequences in s
1918 Also deletes any suffix starting with '('
1921 StripHighlightAndTitle (char *s)
1923 static char retbuf[MSG_SIZ];
1926 while (*s != NULLCHAR) {
1927 while (*s == '\033') {
1928 while (*s != NULLCHAR && !isalpha(*s)) s++;
1929 if (*s != NULLCHAR) s++;
1931 while (*s != NULLCHAR && *s != '\033') {
1932 if (*s == '(' || *s == '[') {
1943 /* Remove all highlighting escape sequences in s */
1945 StripHighlight (char *s)
1947 static char retbuf[MSG_SIZ];
1950 while (*s != NULLCHAR) {
1951 while (*s == '\033') {
1952 while (*s != NULLCHAR && !isalpha(*s)) s++;
1953 if (*s != NULLCHAR) s++;
1955 while (*s != NULLCHAR && *s != '\033') {
1963 char *variantNames[] = VARIANT_NAMES;
1965 VariantName (VariantClass v)
1967 return variantNames[v];
1971 /* Identify a variant from the strings the chess servers use or the
1972 PGN Variant tag names we use. */
1974 StringToVariant (char *e)
1978 VariantClass v = VariantNormal;
1979 int i, found = FALSE;
1985 /* [HGM] skip over optional board-size prefixes */
1986 if( sscanf(e, "%dx%d_", &i, &i) == 2 ||
1987 sscanf(e, "%dx%d+%d_", &i, &i, &i) == 3 ) {
1988 while( *e++ != '_');
1991 if(StrCaseStr(e, "misc/")) { // [HGM] on FICS, misc/shogi is not shogi
1995 for (i=0; i<sizeof(variantNames)/sizeof(char*); i++) {
1996 if (StrCaseStr(e, variantNames[i])) {
1997 v = (VariantClass) i;
2004 if ((StrCaseStr(e, "fischer") && StrCaseStr(e, "random"))
2005 || StrCaseStr(e, "wild/fr")
2006 || StrCaseStr(e, "frc") || StrCaseStr(e, "960")) {
2007 v = VariantFischeRandom;
2008 } else if ((i = 4, p = StrCaseStr(e, "wild")) ||
2009 (i = 1, p = StrCaseStr(e, "w"))) {
2011 while (*p && (isspace(*p) || *p == '(' || *p == '/')) p++;
2018 case 0: /* FICS only, actually */
2020 /* Castling legal even if K starts on d-file */
2021 v = VariantWildCastle;
2026 /* Castling illegal even if K & R happen to start in
2027 normal positions. */
2028 v = VariantNoCastle;
2041 /* Castling legal iff K & R start in normal positions */
2047 /* Special wilds for position setup; unclear what to do here */
2048 v = VariantLoadable;
2051 /* Bizarre ICC game */
2052 v = VariantTwoKings;
2055 v = VariantKriegspiel;
2061 v = VariantFischeRandom;
2064 v = VariantCrazyhouse;
2067 v = VariantBughouse;
2073 /* Not quite the same as FICS suicide! */
2074 v = VariantGiveaway;
2080 v = VariantShatranj;
2083 /* Temporary names for future ICC types. The name *will* change in
2084 the next xboard/WinBoard release after ICC defines it. */
2122 v = VariantCapablanca;
2125 v = VariantKnightmate;
2131 v = VariantCylinder;
2137 v = VariantCapaRandom;
2140 v = VariantBerolina;
2152 /* Found "wild" or "w" in the string but no number;
2153 must assume it's normal chess. */
2157 len = snprintf(buf, MSG_SIZ, _("Unknown wild type %d"), wnum);
2158 if( (len >= MSG_SIZ) && appData.debugMode )
2159 fprintf(debugFP, "StringToVariant: buffer truncated.\n");
2161 DisplayError(buf, 0);
2167 if (appData.debugMode) {
2168 fprintf(debugFP, _("recognized '%s' (%d) as variant %s\n"),
2169 e, wnum, VariantName(v));
2174 static int leftover_start = 0, leftover_len = 0;
2175 char star_match[STAR_MATCH_N][MSG_SIZ];
2177 /* Test whether pattern is present at &buf[*index]; if so, return TRUE,
2178 advance *index beyond it, and set leftover_start to the new value of
2179 *index; else return FALSE. If pattern contains the character '*', it
2180 matches any sequence of characters not containing '\r', '\n', or the
2181 character following the '*' (if any), and the matched sequence(s) are
2182 copied into star_match.
2185 looking_at ( char *buf, int *index, char *pattern)
2187 char *bufp = &buf[*index], *patternp = pattern;
2189 char *matchp = star_match[0];
2192 if (*patternp == NULLCHAR) {
2193 *index = leftover_start = bufp - buf;
2197 if (*bufp == NULLCHAR) return FALSE;
2198 if (*patternp == '*') {
2199 if (*bufp == *(patternp + 1)) {
2201 matchp = star_match[++star_count];
2205 } else if (*bufp == '\n' || *bufp == '\r') {
2207 if (*patternp == NULLCHAR)
2212 *matchp++ = *bufp++;
2216 if (*patternp != *bufp) return FALSE;
2223 SendToPlayer (char *data, int length)
2225 int error, outCount;
2226 outCount = OutputToProcess(NoProc, data, length, &error);
2227 if (outCount < length) {
2228 DisplayFatalError(_("Error writing to display"), error, 1);
2233 PackHolding (char packed[], char *holding)
2243 switch (runlength) {
2254 sprintf(q, "%d", runlength);
2266 /* Telnet protocol requests from the front end */
2268 TelnetRequest (unsigned char ddww, unsigned char option)
2270 unsigned char msg[3];
2271 int outCount, outError;
2273 if (*appData.icsCommPort != NULLCHAR || appData.useTelnet) return;
2275 if (appData.debugMode) {
2276 char buf1[8], buf2[8], *ddwwStr, *optionStr;
2292 snprintf(buf1,sizeof(buf1)/sizeof(buf1[0]), "%d", ddww);
2301 snprintf(buf2,sizeof(buf2)/sizeof(buf2[0]), "%d", option);
2304 fprintf(debugFP, ">%s %s ", ddwwStr, optionStr);
2309 outCount = OutputToProcess(icsPR, (char *)msg, 3, &outError);
2311 DisplayFatalError(_("Error writing to ICS"), outError, 1);
2318 if (!appData.icsActive) return;
2319 TelnetRequest(TN_DO, TN_ECHO);
2325 if (!appData.icsActive) return;
2326 TelnetRequest(TN_DONT, TN_ECHO);
2330 CopyHoldings (Board board, char *holdings, ChessSquare lowestPiece)
2332 /* put the holdings sent to us by the server on the board holdings area */
2333 int i, j, holdingsColumn, holdingsStartRow, direction, countsColumn;
2337 if(gameInfo.holdingsWidth < 2) return;
2338 if(gameInfo.variant != VariantBughouse && board[HOLDINGS_SET])
2339 return; // prevent overwriting by pre-board holdings
2341 if( (int)lowestPiece >= BlackPawn ) {
2344 holdingsStartRow = BOARD_HEIGHT-1;
2347 holdingsColumn = BOARD_WIDTH-1;
2348 countsColumn = BOARD_WIDTH-2;
2349 holdingsStartRow = 0;
2353 for(i=0; i<BOARD_HEIGHT; i++) { /* clear holdings */
2354 board[i][holdingsColumn] = EmptySquare;
2355 board[i][countsColumn] = (ChessSquare) 0;
2357 while( (p=*holdings++) != NULLCHAR ) {
2358 piece = CharToPiece( ToUpper(p) );
2359 if(piece == EmptySquare) continue;
2360 /*j = (int) piece - (int) WhitePawn;*/
2361 j = PieceToNumber(piece);
2362 if(j >= gameInfo.holdingsSize) continue; /* ignore pieces that do not fit */
2363 if(j < 0) continue; /* should not happen */
2364 piece = (ChessSquare) ( (int)piece + (int)lowestPiece );
2365 board[holdingsStartRow+j*direction][holdingsColumn] = piece;
2366 board[holdingsStartRow+j*direction][countsColumn]++;
2372 VariantSwitch (Board board, VariantClass newVariant)
2374 int newHoldingsWidth, newWidth = 8, newHeight = 8, i, j;
2375 static Board oldBoard;
2377 startedFromPositionFile = FALSE;
2378 if(gameInfo.variant == newVariant) return;
2380 /* [HGM] This routine is called each time an assignment is made to
2381 * gameInfo.variant during a game, to make sure the board sizes
2382 * are set to match the new variant. If that means adding or deleting
2383 * holdings, we shift the playing board accordingly
2384 * This kludge is needed because in ICS observe mode, we get boards
2385 * of an ongoing game without knowing the variant, and learn about the
2386 * latter only later. This can be because of the move list we requested,
2387 * in which case the game history is refilled from the beginning anyway,
2388 * but also when receiving holdings of a crazyhouse game. In the latter
2389 * case we want to add those holdings to the already received position.
2393 if (appData.debugMode) {
2394 fprintf(debugFP, "Switch board from %s to %s\n",
2395 VariantName(gameInfo.variant), VariantName(newVariant));
2396 setbuf(debugFP, NULL);
2398 shuffleOpenings = 0; /* [HGM] shuffle */
2399 gameInfo.holdingsSize = 5; /* [HGM] prepare holdings */
2403 newWidth = 9; newHeight = 9;
2404 gameInfo.holdingsSize = 7;
2405 case VariantBughouse:
2406 case VariantCrazyhouse:
2407 newHoldingsWidth = 2; break;
2411 newHoldingsWidth = 2;
2412 gameInfo.holdingsSize = 8;
2415 case VariantCapablanca:
2416 case VariantCapaRandom:
2419 newHoldingsWidth = gameInfo.holdingsSize = 0;
2422 if(newWidth != gameInfo.boardWidth ||
2423 newHeight != gameInfo.boardHeight ||
2424 newHoldingsWidth != gameInfo.holdingsWidth ) {
2426 /* shift position to new playing area, if needed */
2427 if(newHoldingsWidth > gameInfo.holdingsWidth) {
2428 for(i=0; i<BOARD_HEIGHT; i++)
2429 for(j=BOARD_RGHT-1; j>=BOARD_LEFT; j--)
2430 board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
2432 for(i=0; i<newHeight; i++) {
2433 board[i][0] = board[i][newWidth+2*newHoldingsWidth-1] = EmptySquare;
2434 board[i][1] = board[i][newWidth+2*newHoldingsWidth-2] = (ChessSquare) 0;
2436 } else if(newHoldingsWidth < gameInfo.holdingsWidth) {
2437 for(i=0; i<BOARD_HEIGHT; i++)
2438 for(j=BOARD_LEFT; j<BOARD_RGHT; j++)
2439 board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
2442 gameInfo.boardWidth = newWidth;
2443 gameInfo.boardHeight = newHeight;
2444 gameInfo.holdingsWidth = newHoldingsWidth;
2445 gameInfo.variant = newVariant;
2446 InitDrawingSizes(-2, 0);
2447 } else gameInfo.variant = newVariant;
2448 CopyBoard(oldBoard, board); // remember correctly formatted board
2449 InitPosition(FALSE); /* this sets up board[0], but also other stuff */
2450 DrawPosition(TRUE, currentMove ? boards[currentMove] : oldBoard);
2453 static int loggedOn = FALSE;
2455 /*-- Game start info cache: --*/
2457 char gs_kind[MSG_SIZ];
2458 static char player1Name[128] = "";
2459 static char player2Name[128] = "";
2460 static char cont_seq[] = "\n\\ ";
2461 static int player1Rating = -1;
2462 static int player2Rating = -1;
2463 /*----------------------------*/
2465 ColorClass curColor = ColorNormal;
2466 int suppressKibitz = 0;
2469 Boolean soughtPending = FALSE;
2470 Boolean seekGraphUp;
2471 #define MAX_SEEK_ADS 200
2473 char *seekAdList[MAX_SEEK_ADS];
2474 int ratingList[MAX_SEEK_ADS], xList[MAX_SEEK_ADS], yList[MAX_SEEK_ADS], seekNrList[MAX_SEEK_ADS], zList[MAX_SEEK_ADS];
2475 float tcList[MAX_SEEK_ADS];
2476 char colorList[MAX_SEEK_ADS];
2477 int nrOfSeekAds = 0;
2478 int minRating = 1010, maxRating = 2800;
2479 int hMargin = 10, vMargin = 20, h, w;
2480 extern int squareSize, lineGap;
2485 int x, y, color = 0, r = ratingList[i]; float tc = tcList[i];
2486 xList[i] = yList[i] = -100; // outside graph, so cannot be clicked
2487 if(r < minRating+100 && r >=0 ) r = minRating+100;
2488 if(r > maxRating) r = maxRating;
2489 if(tc < 1.) tc = 1.;
2490 if(tc > 95.) tc = 95.;
2491 x = (w-hMargin-squareSize/8-7)* log(tc)/log(95.) + hMargin;
2492 y = ((double)r - minRating)/(maxRating - minRating)
2493 * (h-vMargin-squareSize/8-1) + vMargin;
2494 if(ratingList[i] < 0) y = vMargin + squareSize/4;
2495 if(strstr(seekAdList[i], " u ")) color = 1;
2496 if(!strstr(seekAdList[i], "lightning") && // for now all wilds same color
2497 !strstr(seekAdList[i], "bullet") &&
2498 !strstr(seekAdList[i], "blitz") &&
2499 !strstr(seekAdList[i], "standard") ) color = 2;
2500 if(strstr(seekAdList[i], "(C) ")) color |= SQUARE; // plot computer seeks as squares
2501 DrawSeekDot(xList[i]=x+3*(color&~SQUARE), yList[i]=h-1-y, colorList[i]=color);
2505 AddAd (char *handle, char *rating, int base, int inc, char rated, char *type, int nr, Boolean plot)
2507 char buf[MSG_SIZ], *ext = "";
2508 VariantClass v = StringToVariant(type);
2509 if(strstr(type, "wild")) {
2510 ext = type + 4; // append wild number
2511 if(v == VariantFischeRandom) type = "chess960"; else
2512 if(v == VariantLoadable) type = "setup"; else
2513 type = VariantName(v);
2515 snprintf(buf, MSG_SIZ, "%s (%s) %d %d %c %s%s", handle, rating, base, inc, rated, type, ext);
2516 if(nrOfSeekAds < MAX_SEEK_ADS-1) {
2517 if(seekAdList[nrOfSeekAds]) free(seekAdList[nrOfSeekAds]);
2518 ratingList[nrOfSeekAds] = -1; // for if seeker has no rating
2519 sscanf(rating, "%d", &ratingList[nrOfSeekAds]);
2520 tcList[nrOfSeekAds] = base + (2./3.)*inc;
2521 seekNrList[nrOfSeekAds] = nr;
2522 zList[nrOfSeekAds] = 0;
2523 seekAdList[nrOfSeekAds++] = StrSave(buf);
2524 if(plot) PlotSeekAd(nrOfSeekAds-1);
2529 EraseSeekDot (int i)
2531 int x = xList[i], y = yList[i], d=squareSize/4, k;
2532 DrawSeekBackground(x-squareSize/8, y-squareSize/8, x+squareSize/8+1, y+squareSize/8+1);
2533 if(x < hMargin+d) DrawSeekAxis(hMargin, y-squareSize/8, hMargin, y+squareSize/8+1);
2534 // now replot every dot that overlapped
2535 for(k=0; k<nrOfSeekAds; k++) if(k != i) {
2536 int xx = xList[k], yy = yList[k];
2537 if(xx <= x+d && xx > x-d && yy <= y+d && yy > y-d)
2538 DrawSeekDot(xx, yy, colorList[k]);
2543 RemoveSeekAd (int nr)
2546 for(i=0; i<nrOfSeekAds; i++) if(seekNrList[i] == nr) {
2548 if(seekAdList[i]) free(seekAdList[i]);
2549 seekAdList[i] = seekAdList[--nrOfSeekAds];
2550 seekNrList[i] = seekNrList[nrOfSeekAds];
2551 ratingList[i] = ratingList[nrOfSeekAds];
2552 colorList[i] = colorList[nrOfSeekAds];
2553 tcList[i] = tcList[nrOfSeekAds];
2554 xList[i] = xList[nrOfSeekAds];
2555 yList[i] = yList[nrOfSeekAds];
2556 zList[i] = zList[nrOfSeekAds];
2557 seekAdList[nrOfSeekAds] = NULL;
2563 MatchSoughtLine (char *line)
2565 char handle[MSG_SIZ], rating[MSG_SIZ], type[MSG_SIZ];
2566 int nr, base, inc, u=0; char dummy;
2568 if(sscanf(line, "%d %s %s %d %d rated %s", &nr, rating, handle, &base, &inc, type) == 6 ||
2569 sscanf(line, "%d %s %s %s %d %d rated %c", &nr, rating, handle, type, &base, &inc, &dummy) == 7 ||
2571 (sscanf(line, "%d %s %s %d %d unrated %s", &nr, rating, handle, &base, &inc, type) == 6 ||
2572 sscanf(line, "%d %s %s %s %d %d unrated %c", &nr, rating, handle, type, &base, &inc, &dummy) == 7) ) {
2573 // match: compact and save the line
2574 AddAd(handle, rating, base, inc, u ? 'u' : 'r', type, nr, FALSE);
2584 if(!seekGraphUp) return FALSE;
2585 h = BOARD_HEIGHT * (squareSize + lineGap) + lineGap;
2586 w = BOARD_WIDTH * (squareSize + lineGap) + lineGap;
2588 DrawSeekBackground(0, 0, w, h);
2589 DrawSeekAxis(hMargin, h-1-vMargin, w-5, h-1-vMargin);
2590 DrawSeekAxis(hMargin, h-1-vMargin, hMargin, 5);
2591 for(i=0; i<4000; i+= 100) if(i>=minRating && i<maxRating) {
2592 int yy =((double)i - minRating)/(maxRating - minRating)*(h-vMargin-squareSize/8-1) + vMargin;
2594 DrawSeekAxis(hMargin+5*(i%500==0), yy, hMargin-5, yy); // rating ticks
2597 snprintf(buf, MSG_SIZ, "%d", i);
2598 DrawSeekText(buf, hMargin+squareSize/8+7, yy);
2601 DrawSeekText("unrated", hMargin+squareSize/8+7, h-1-vMargin-squareSize/4);
2602 for(i=1; i<100; i+=(i<10?1:5)) {
2603 int xx = (w-hMargin-squareSize/8-7)* log((double)i)/log(95.) + hMargin;
2604 DrawSeekAxis(xx, h-1-vMargin, xx, h-6-vMargin-3*(i%10==0)); // TC ticks
2605 if(i<=5 || (i>40 ? i%20 : i%10) == 0) {
2607 snprintf(buf, MSG_SIZ, "%d", i);
2608 DrawSeekText(buf, xx-2-3*(i>9), h-1-vMargin/2);
2611 for(i=0; i<nrOfSeekAds; i++) PlotSeekAd(i);
2616 SeekGraphClick (ClickType click, int x, int y, int moving)
2618 static int lastDown = 0, displayed = 0, lastSecond;
2619 if(y < 0) return FALSE;
2620 if(!(appData.seekGraph && appData.icsActive && loggedOn &&
2621 (gameMode == BeginningOfGame || gameMode == IcsIdle))) {
2622 if(!seekGraphUp) return FALSE;
2623 seekGraphUp = FALSE; // seek graph is up when it shouldn't be: take it down
2624 DrawPosition(TRUE, NULL);
2627 if(!seekGraphUp) { // initiate cration of seek graph by requesting seek-ad list
2628 if(click == Release || moving) return FALSE;
2630 soughtPending = TRUE;
2631 SendToICS(ics_prefix);
2632 SendToICS("sought\n"); // should this be "sought all"?
2633 } else { // issue challenge based on clicked ad
2634 int dist = 10000; int i, closest = 0, second = 0;
2635 for(i=0; i<nrOfSeekAds; i++) {
2636 int d = (x-xList[i])*(x-xList[i]) + (y-yList[i])*(y-yList[i]) + zList[i];
2637 if(d < dist) { dist = d; closest = i; }
2638 second += (d - zList[i] < 120); // count in-range ads
2639 if(click == Press && moving != 1 && zList[i]>0) zList[i] *= 0.8; // age priority
2643 second = (second > 1);
2644 if(displayed != closest || second != lastSecond) {
2645 DisplayMessage(second ? "!" : "", seekAdList[closest]);
2646 lastSecond = second; displayed = closest;
2648 if(click == Press) {
2649 if(moving == 2) zList[closest] = 100; // right-click; push to back on press
2652 } // on press 'hit', only show info
2653 if(moving == 2) return TRUE; // ignore right up-clicks on dot
2654 snprintf(buf, MSG_SIZ, "play %d\n", seekNrList[closest]);
2655 SendToICS(ics_prefix);
2657 return TRUE; // let incoming board of started game pop down the graph
2658 } else if(click == Release) { // release 'miss' is ignored
2659 zList[lastDown] = 100; // make future selection of the rejected ad more difficult
2660 if(moving == 2) { // right up-click
2661 nrOfSeekAds = 0; // refresh graph
2662 soughtPending = TRUE;
2663 SendToICS(ics_prefix);
2664 SendToICS("sought\n"); // should this be "sought all"?
2667 } else if(moving) { if(displayed >= 0) DisplayMessage("", ""); displayed = -1; return TRUE; }
2668 // press miss or release hit 'pop down' seek graph
2669 seekGraphUp = FALSE;
2670 DrawPosition(TRUE, NULL);
2676 read_from_ics (InputSourceRef isr, VOIDSTAR closure, char *data, int count, int error)
2678 #define BUF_SIZE (16*1024) /* overflowed at 8K with "inchannel 1" on FICS? */
2679 #define STARTED_NONE 0
2680 #define STARTED_MOVES 1
2681 #define STARTED_BOARD 2
2682 #define STARTED_OBSERVE 3
2683 #define STARTED_HOLDINGS 4
2684 #define STARTED_CHATTER 5
2685 #define STARTED_COMMENT 6
2686 #define STARTED_MOVES_NOHIDE 7
2688 static int started = STARTED_NONE;
2689 static char parse[20000];
2690 static int parse_pos = 0;
2691 static char buf[BUF_SIZE + 1];
2692 static int firstTime = TRUE, intfSet = FALSE;
2693 static ColorClass prevColor = ColorNormal;
2694 static int savingComment = FALSE;
2695 static int cmatch = 0; // continuation sequence match
2702 int backup; /* [DM] For zippy color lines */
2704 char talker[MSG_SIZ]; // [HGM] chat
2707 connectionAlive = TRUE; // [HGM] alive: I think, therefore I am...
2709 if (appData.debugMode) {
2711 fprintf(debugFP, "<ICS: ");
2712 show_bytes(debugFP, data, count);
2713 fprintf(debugFP, "\n");
2717 if (appData.debugMode) { int f = forwardMostMove;
2718 fprintf(debugFP, "ics input %d, castling = %d %d %d %d %d %d\n", f,
2719 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
2720 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
2723 /* If last read ended with a partial line that we couldn't parse,
2724 prepend it to the new read and try again. */
2725 if (leftover_len > 0) {
2726 for (i=0; i<leftover_len; i++)
2727 buf[i] = buf[leftover_start + i];
2730 /* copy new characters into the buffer */
2731 bp = buf + leftover_len;
2732 buf_len=leftover_len;
2733 for (i=0; i<count; i++)
2736 if (data[i] == '\r')
2739 // join lines split by ICS?
2740 if (!appData.noJoin)
2743 Joining just consists of finding matches against the
2744 continuation sequence, and discarding that sequence
2745 if found instead of copying it. So, until a match
2746 fails, there's nothing to do since it might be the
2747 complete sequence, and thus, something we don't want
2750 if (data[i] == cont_seq[cmatch])
2753 if (cmatch == strlen(cont_seq))
2755 cmatch = 0; // complete match. just reset the counter
2758 it's possible for the ICS to not include the space
2759 at the end of the last word, making our [correct]
2760 join operation fuse two separate words. the server
2761 does this when the space occurs at the width setting.
2763 if (!buf_len || buf[buf_len-1] != ' ')
2774 match failed, so we have to copy what matched before
2775 falling through and copying this character. In reality,
2776 this will only ever be just the newline character, but
2777 it doesn't hurt to be precise.
2779 strncpy(bp, cont_seq, cmatch);
2791 buf[buf_len] = NULLCHAR;
2792 // next_out = leftover_len; // [HGM] should we set this to 0, and not print it in advance?
2797 while (i < buf_len) {
2798 /* Deal with part of the TELNET option negotiation
2799 protocol. We refuse to do anything beyond the
2800 defaults, except that we allow the WILL ECHO option,
2801 which ICS uses to turn off password echoing when we are
2802 directly connected to it. We reject this option
2803 if localLineEditing mode is on (always on in xboard)
2804 and we are talking to port 23, which might be a real
2805 telnet server that will try to keep WILL ECHO on permanently.
2807 if (buf_len - i >= 3 && (unsigned char) buf[i] == TN_IAC) {
2808 static int remoteEchoOption = FALSE; /* telnet ECHO option */
2809 unsigned char option;
2811 switch ((unsigned char) buf[++i]) {
2813 if (appData.debugMode)
2814 fprintf(debugFP, "\n<WILL ");
2815 switch (option = (unsigned char) buf[++i]) {
2817 if (appData.debugMode)
2818 fprintf(debugFP, "ECHO ");
2819 /* Reply only if this is a change, according
2820 to the protocol rules. */
2821 if (remoteEchoOption) break;
2822 if (appData.localLineEditing &&
2823 atoi(appData.icsPort) == TN_PORT) {
2824 TelnetRequest(TN_DONT, TN_ECHO);
2827 TelnetRequest(TN_DO, TN_ECHO);
2828 remoteEchoOption = TRUE;
2832 if (appData.debugMode)
2833 fprintf(debugFP, "%d ", option);
2834 /* Whatever this is, we don't want it. */
2835 TelnetRequest(TN_DONT, option);
2840 if (appData.debugMode)
2841 fprintf(debugFP, "\n<WONT ");
2842 switch (option = (unsigned char) buf[++i]) {
2844 if (appData.debugMode)
2845 fprintf(debugFP, "ECHO ");
2846 /* Reply only if this is a change, according
2847 to the protocol rules. */
2848 if (!remoteEchoOption) break;
2850 TelnetRequest(TN_DONT, TN_ECHO);
2851 remoteEchoOption = FALSE;
2854 if (appData.debugMode)
2855 fprintf(debugFP, "%d ", (unsigned char) option);
2856 /* Whatever this is, it must already be turned
2857 off, because we never agree to turn on
2858 anything non-default, so according to the
2859 protocol rules, we don't reply. */
2864 if (appData.debugMode)
2865 fprintf(debugFP, "\n<DO ");
2866 switch (option = (unsigned char) buf[++i]) {
2868 /* Whatever this is, we refuse to do it. */
2869 if (appData.debugMode)
2870 fprintf(debugFP, "%d ", option);
2871 TelnetRequest(TN_WONT, option);
2876 if (appData.debugMode)
2877 fprintf(debugFP, "\n<DONT ");
2878 switch (option = (unsigned char) buf[++i]) {
2880 if (appData.debugMode)
2881 fprintf(debugFP, "%d ", option);
2882 /* Whatever this is, we are already not doing
2883 it, because we never agree to do anything
2884 non-default, so according to the protocol
2885 rules, we don't reply. */
2890 if (appData.debugMode)
2891 fprintf(debugFP, "\n<IAC ");
2892 /* Doubled IAC; pass it through */
2896 if (appData.debugMode)
2897 fprintf(debugFP, "\n<%d ", (unsigned char) buf[i]);
2898 /* Drop all other telnet commands on the floor */
2901 if (oldi > next_out)
2902 SendToPlayer(&buf[next_out], oldi - next_out);
2908 /* OK, this at least will *usually* work */
2909 if (!loggedOn && looking_at(buf, &i, "ics%")) {
2913 if (loggedOn && !intfSet) {
2914 if (ics_type == ICS_ICC) {
2915 snprintf(str, MSG_SIZ,
2916 "/set-quietly interface %s\n/set-quietly style 12\n",
2918 if(appData.seekGraph && appData.autoRefresh) // [HGM] seekgraph
2919 strcat(str, "/set-2 51 1\n/set seek 1\n");
2920 } else if (ics_type == ICS_CHESSNET) {
2921 snprintf(str, MSG_SIZ, "/style 12\n");
2923 safeStrCpy(str, "alias $ @\n$set interface ", sizeof(str)/sizeof(str[0]));
2924 strcat(str, programVersion);
2925 strcat(str, "\n$iset startpos 1\n$iset ms 1\n");
2926 if(appData.seekGraph && appData.autoRefresh) // [HGM] seekgraph
2927 strcat(str, "$iset seekremove 1\n$set seek 1\n");
2929 strcat(str, "$iset nohighlight 1\n");
2931 strcat(str, "$iset lock 1\n$style 12\n");
2934 NotifyFrontendLogin();
2938 if (started == STARTED_COMMENT) {
2939 /* Accumulate characters in comment */
2940 parse[parse_pos++] = buf[i];
2941 if (buf[i] == '\n') {
2942 parse[parse_pos] = NULLCHAR;
2943 if(chattingPartner>=0) {
2945 snprintf(mess, MSG_SIZ, "%s%s", talker, parse);
2946 OutputChatMessage(chattingPartner, mess);
2947 chattingPartner = -1;
2948 next_out = i+1; // [HGM] suppress printing in ICS window
2950 if(!suppressKibitz) // [HGM] kibitz
2951 AppendComment(forwardMostMove, StripHighlight(parse), TRUE);
2952 else { // [HGM kibitz: divert memorized engine kibitz to engine-output window
2953 int nrDigit = 0, nrAlph = 0, j;
2954 if(parse_pos > MSG_SIZ - 30) // defuse unreasonably long input
2955 { parse_pos = MSG_SIZ-30; parse[parse_pos - 1] = '\n'; }
2956 parse[parse_pos] = NULLCHAR;
2957 // try to be smart: if it does not look like search info, it should go to
2958 // ICS interaction window after all, not to engine-output window.
2959 for(j=0; j<parse_pos; j++) { // count letters and digits
2960 nrDigit += (parse[j] >= '0' && parse[j] <= '9');
2961 nrAlph += (parse[j] >= 'a' && parse[j] <= 'z');
2962 nrAlph += (parse[j] >= 'A' && parse[j] <= 'Z');
2964 if(nrAlph < 9*nrDigit) { // if more than 10% digit we assume search info
2965 int depth=0; float score;
2966 if(sscanf(parse, "!!! %f/%d", &score, &depth) == 2 && depth>0) {
2967 // [HGM] kibitz: save kibitzed opponent info for PGN and eval graph
2968 pvInfoList[forwardMostMove-1].depth = depth;
2969 pvInfoList[forwardMostMove-1].score = 100*score;
2971 OutputKibitz(suppressKibitz, parse);
2974 if(gameMode == IcsObserving) // restore original ICS messages
2975 snprintf(tmp, MSG_SIZ, "%s kibitzes: %s", star_match[0], parse);
2977 snprintf(tmp, MSG_SIZ, _("your opponent kibitzes: %s"), parse);
2978 SendToPlayer(tmp, strlen(tmp));
2980 next_out = i+1; // [HGM] suppress printing in ICS window
2982 started = STARTED_NONE;
2984 /* Don't match patterns against characters in comment */
2989 if (started == STARTED_CHATTER) {
2990 if (buf[i] != '\n') {
2991 /* Don't match patterns against characters in chatter */
2995 started = STARTED_NONE;
2996 if(suppressKibitz) next_out = i+1;
2999 /* Kludge to deal with rcmd protocol */
3000 if (firstTime && looking_at(buf, &i, "\001*")) {
3001 DisplayFatalError(&buf[1], 0, 1);
3007 if (!loggedOn && looking_at(buf, &i, "chessclub.com")) {
3010 if (appData.debugMode)
3011 fprintf(debugFP, "ics_type %d\n", ics_type);
3014 if (!loggedOn && looking_at(buf, &i, "freechess.org")) {
3015 ics_type = ICS_FICS;
3017 if (appData.debugMode)
3018 fprintf(debugFP, "ics_type %d\n", ics_type);
3021 if (!loggedOn && looking_at(buf, &i, "chess.net")) {
3022 ics_type = ICS_CHESSNET;
3024 if (appData.debugMode)
3025 fprintf(debugFP, "ics_type %d\n", ics_type);
3030 (looking_at(buf, &i, "\"*\" is *a registered name") ||
3031 looking_at(buf, &i, "Logging you in as \"*\"") ||
3032 looking_at(buf, &i, "will be \"*\""))) {
3033 safeStrCpy(ics_handle, star_match[0], sizeof(ics_handle)/sizeof(ics_handle[0]));
3037 if (loggedOn && !have_set_title && ics_handle[0] != NULLCHAR) {
3039 snprintf(buf, sizeof(buf), "%s@%s", ics_handle, appData.icsHost);
3040 DisplayIcsInteractionTitle(buf);
3041 have_set_title = TRUE;
3044 /* skip finger notes */
3045 if (started == STARTED_NONE &&
3046 ((buf[i] == ' ' && isdigit(buf[i+1])) ||
3047 (buf[i] == '1' && buf[i+1] == '0')) &&
3048 buf[i+2] == ':' && buf[i+3] == ' ') {
3049 started = STARTED_CHATTER;
3055 // [HGM] seekgraph: recognize sought lines and end-of-sought message
3056 if(appData.seekGraph) {
3057 if(soughtPending && MatchSoughtLine(buf+i)) {
3058 i = strstr(buf+i, "rated") - buf;
3059 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3060 next_out = leftover_start = i;
3061 started = STARTED_CHATTER;
3062 suppressKibitz = TRUE;
3065 if((gameMode == IcsIdle || gameMode == BeginningOfGame)
3066 && looking_at(buf, &i, "* ads displayed")) {
3067 soughtPending = FALSE;
3072 if(appData.autoRefresh) {
3073 if(looking_at(buf, &i, "* (*) seeking * * * * *\"play *\" to respond)\n")) {
3074 int s = (ics_type == ICS_ICC); // ICC format differs
3076 AddAd(star_match[0], star_match[1], atoi(star_match[2+s]), atoi(star_match[3+s]),
3077 star_match[4+s][0], star_match[5-3*s], atoi(star_match[7]), TRUE);
3078 looking_at(buf, &i, "*% "); // eat prompt
3079 if(oldi > 0 && buf[oldi-1] == '\n') oldi--; // suppress preceding LF, if any
3080 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3081 next_out = i; // suppress
3084 if(looking_at(buf, &i, "\nAds removed: *\n") || looking_at(buf, &i, "\031(51 * *\031)")) {
3085 char *p = star_match[0];
3087 if(seekGraphUp) RemoveSeekAd(atoi(p));
3088 while(*p && *p++ != ' '); // next
3090 looking_at(buf, &i, "*% "); // eat prompt
3091 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3098 /* skip formula vars */
3099 if (started == STARTED_NONE &&
3100 buf[i] == 'f' && isdigit(buf[i+1]) && buf[i+2] == ':') {
3101 started = STARTED_CHATTER;
3106 // [HGM] kibitz: try to recognize opponent engine-score kibitzes, to divert them to engine-output window
3107 if (appData.autoKibitz && started == STARTED_NONE &&
3108 !appData.icsEngineAnalyze && // [HGM] [DM] ICS analyze
3109 (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack || gameMode == IcsObserving)) {
3110 if((looking_at(buf, &i, "\n* kibitzes: ") || looking_at(buf, &i, "\n* whispers: ") ||
3111 looking_at(buf, &i, "* kibitzes: ") || looking_at(buf, &i, "* whispers: ")) &&
3112 (StrStr(star_match[0], gameInfo.white) == star_match[0] ||
3113 StrStr(star_match[0], gameInfo.black) == star_match[0] )) { // kibitz of self or opponent
3114 suppressKibitz = TRUE;
3115 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3117 if((StrStr(star_match[0], gameInfo.white) == star_match[0]
3118 && (gameMode == IcsPlayingWhite)) ||
3119 (StrStr(star_match[0], gameInfo.black) == star_match[0]
3120 && (gameMode == IcsPlayingBlack)) ) // opponent kibitz
3121 started = STARTED_CHATTER; // own kibitz we simply discard
3123 started = STARTED_COMMENT; // make sure it will be collected in parse[]
3124 parse_pos = 0; parse[0] = NULLCHAR;
3125 savingComment = TRUE;
3126 suppressKibitz = gameMode != IcsObserving ? 2 :
3127 (StrStr(star_match[0], gameInfo.white) == NULL) + 1;
3131 if((looking_at(buf, &i, "\nkibitzed to *\n") || looking_at(buf, &i, "kibitzed to *\n") ||
3132 looking_at(buf, &i, "\n(kibitzed to *\n") || looking_at(buf, &i, "(kibitzed to *\n"))
3133 && atoi(star_match[0])) {
3134 // suppress the acknowledgements of our own autoKibitz
3136 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3137 if(p = strchr(star_match[0], ' ')) p[1] = NULLCHAR; // clip off "players)" on FICS
3138 SendToPlayer(star_match[0], strlen(star_match[0]));
3139 if(looking_at(buf, &i, "*% ")) // eat prompt
3140 suppressKibitz = FALSE;
3144 } // [HGM] kibitz: end of patch
3146 if(looking_at(buf, &i, "* rating adjustment: * --> *\n")) continue;
3148 // [HGM] chat: intercept tells by users for which we have an open chat window
3150 if(started == STARTED_NONE && (looking_at(buf, &i, "* tells you:") || looking_at(buf, &i, "* says:") ||
3151 looking_at(buf, &i, "* whispers:") ||
3152 looking_at(buf, &i, "* kibitzes:") ||
3153 looking_at(buf, &i, "* shouts:") ||
3154 looking_at(buf, &i, "* c-shouts:") ||
3155 looking_at(buf, &i, "--> * ") ||
3156 looking_at(buf, &i, "*(*):") && (sscanf(star_match[1], "%d", &channel),1) ||
3157 looking_at(buf, &i, "*(*)(*):") && (sscanf(star_match[2], "%d", &channel),1) ||
3158 looking_at(buf, &i, "*(*)(*)(*):") && (sscanf(star_match[3], "%d", &channel),1) ||
3159 looking_at(buf, &i, "*(*)(*)(*)(*):") && sscanf(star_match[4], "%d", &channel) == 1 )) {
3161 sscanf(star_match[0], "%[^(]", talker+1); // strip (C) or (U) off ICS handle
3162 chattingPartner = -1;
3164 if(channel >= 0) // channel broadcast; look if there is a chatbox for this channel
3165 for(p=0; p<MAX_CHAT; p++) {
3166 if(chatPartner[p][0] >= '0' && chatPartner[p][0] <= '9' && channel == atoi(chatPartner[p])) {
3167 talker[0] = '['; strcat(talker, "] ");
3168 Colorize(channel == 1 ? ColorChannel1 : ColorChannel, FALSE);
3169 chattingPartner = p; break;
3172 if(buf[i-3] == 'e') // kibitz; look if there is a KIBITZ chatbox
3173 for(p=0; p<MAX_CHAT; p++) {
3174 if(!strcmp("kibitzes", chatPartner[p])) {
3175 talker[0] = '['; strcat(talker, "] ");
3176 chattingPartner = p; break;
3179 if(buf[i-3] == 'r') // whisper; look if there is a WHISPER chatbox
3180 for(p=0; p<MAX_CHAT; p++) {
3181 if(!strcmp("whispers", chatPartner[p])) {
3182 talker[0] = '['; strcat(talker, "] ");
3183 chattingPartner = p; break;
3186 if(buf[i-3] == 't' || buf[oldi+2] == '>') {// shout, c-shout or it; look if there is a 'shouts' chatbox
3187 if(buf[i-8] == '-' && buf[i-3] == 't')
3188 for(p=0; p<MAX_CHAT; p++) { // c-shout; check if dedicatesd c-shout box exists
3189 if(!strcmp("c-shouts", chatPartner[p])) {
3190 talker[0] = '('; strcat(talker, ") "); Colorize(ColorSShout, FALSE);
3191 chattingPartner = p; break;
3194 if(chattingPartner < 0)
3195 for(p=0; p<MAX_CHAT; p++) {
3196 if(!strcmp("shouts", chatPartner[p])) {
3197 if(buf[oldi+2] == '>') { talker[0] = '<'; strcat(talker, "> "); Colorize(ColorShout, FALSE); }
3198 else if(buf[i-8] == '-') { talker[0] = '('; strcat(talker, ") "); Colorize(ColorSShout, FALSE); }
3199 else { talker[0] = '['; strcat(talker, "] "); Colorize(ColorShout, FALSE); }
3200 chattingPartner = p; break;
3204 if(chattingPartner<0) // if not, look if there is a chatbox for this indivdual
3205 for(p=0; p<MAX_CHAT; p++) if(!StrCaseCmp(talker+1, chatPartner[p])) {
3206 talker[0] = 0; Colorize(ColorTell, FALSE);
3207 chattingPartner = p; break;
3209 if(chattingPartner<0) i = oldi; else {
3210 Colorize(curColor, TRUE); // undo the bogus colorations we just made to trigger the souds
3211 if(oldi > 0 && buf[oldi-1] == '\n') oldi--;
3212 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3213 started = STARTED_COMMENT;
3214 parse_pos = 0; parse[0] = NULLCHAR;
3215 savingComment = 3 + chattingPartner; // counts as TRUE
3216 suppressKibitz = TRUE;
3219 } // [HGM] chat: end of patch
3222 if (appData.zippyTalk || appData.zippyPlay) {
3223 /* [DM] Backup address for color zippy lines */
3225 if (loggedOn == TRUE)
3226 if (ZippyControl(buf, &backup) || ZippyConverse(buf, &backup) ||
3227 (appData.zippyPlay && ZippyMatch(buf, &backup)));
3229 } // [DM] 'else { ' deleted
3231 /* Regular tells and says */
3232 (tkind = 1, looking_at(buf, &i, "* tells you: ")) ||
3233 looking_at(buf, &i, "* (your partner) tells you: ") ||
3234 looking_at(buf, &i, "* says: ") ||
3235 /* Don't color "message" or "messages" output */
3236 (tkind = 5, looking_at(buf, &i, "*. * (*:*): ")) ||
3237 looking_at(buf, &i, "*. * at *:*: ") ||
3238 looking_at(buf, &i, "--* (*:*): ") ||
3239 /* Message notifications (same color as tells) */
3240 looking_at(buf, &i, "* has left a message ") ||
3241 looking_at(buf, &i, "* just sent you a message:\n") ||
3242 /* Whispers and kibitzes */
3243 (tkind = 2, looking_at(buf, &i, "* whispers: ")) ||
3244 looking_at(buf, &i, "* kibitzes: ") ||
3246 (tkind = 3, looking_at(buf, &i, "*(*: "))) {
3248 if (tkind == 1 && strchr(star_match[0], ':')) {
3249 /* Avoid "tells you:" spoofs in channels */
3252 if (star_match[0][0] == NULLCHAR ||
3253 strchr(star_match[0], ' ') ||
3254 (tkind == 3 && strchr(star_match[1], ' '))) {
3255 /* Reject bogus matches */
3258 if (appData.colorize) {
3259 if (oldi > next_out) {
3260 SendToPlayer(&buf[next_out], oldi - next_out);
3265 Colorize(ColorTell, FALSE);
3266 curColor = ColorTell;
3269 Colorize(ColorKibitz, FALSE);
3270 curColor = ColorKibitz;
3273 p = strrchr(star_match[1], '(');
3280 Colorize(ColorChannel1, FALSE);
3281 curColor = ColorChannel1;
3283 Colorize(ColorChannel, FALSE);
3284 curColor = ColorChannel;
3288 curColor = ColorNormal;
3292 if (started == STARTED_NONE && appData.autoComment &&
3293 (gameMode == IcsObserving ||
3294 gameMode == IcsPlayingWhite ||
3295 gameMode == IcsPlayingBlack)) {
3296 parse_pos = i - oldi;
3297 memcpy(parse, &buf[oldi], parse_pos);
3298 parse[parse_pos] = NULLCHAR;
3299 started = STARTED_COMMENT;
3300 savingComment = TRUE;
3302 started = STARTED_CHATTER;
3303 savingComment = FALSE;
3310 if (looking_at(buf, &i, "* s-shouts: ") ||
3311 looking_at(buf, &i, "* c-shouts: ")) {
3312 if (appData.colorize) {
3313 if (oldi > next_out) {
3314 SendToPlayer(&buf[next_out], oldi - next_out);
3317 Colorize(ColorSShout, FALSE);
3318 curColor = ColorSShout;
3321 started = STARTED_CHATTER;
3325 if (looking_at(buf, &i, "--->")) {
3330 if (looking_at(buf, &i, "* shouts: ") ||
3331 looking_at(buf, &i, "--> ")) {
3332 if (appData.colorize) {
3333 if (oldi > next_out) {
3334 SendToPlayer(&buf[next_out], oldi - next_out);
3337 Colorize(ColorShout, FALSE);
3338 curColor = ColorShout;
3341 started = STARTED_CHATTER;
3345 if (looking_at( buf, &i, "Challenge:")) {
3346 if (appData.colorize) {
3347 if (oldi > next_out) {
3348 SendToPlayer(&buf[next_out], oldi - next_out);
3351 Colorize(ColorChallenge, FALSE);
3352 curColor = ColorChallenge;
3358 if (looking_at(buf, &i, "* offers you") ||
3359 looking_at(buf, &i, "* offers to be") ||
3360 looking_at(buf, &i, "* would like to") ||
3361 looking_at(buf, &i, "* requests to") ||
3362 looking_at(buf, &i, "Your opponent offers") ||
3363 looking_at(buf, &i, "Your opponent requests")) {
3365 if (appData.colorize) {
3366 if (oldi > next_out) {
3367 SendToPlayer(&buf[next_out], oldi - next_out);
3370 Colorize(ColorRequest, FALSE);
3371 curColor = ColorRequest;
3376 if (looking_at(buf, &i, "* (*) seeking")) {
3377 if (appData.colorize) {
3378 if (oldi > next_out) {
3379 SendToPlayer(&buf[next_out], oldi - next_out);
3382 Colorize(ColorSeek, FALSE);
3383 curColor = ColorSeek;
3388 if(i < backup) { i = backup; continue; } // [HGM] for if ZippyControl matches, but the colorie code doesn't
3390 if (looking_at(buf, &i, "\\ ")) {
3391 if (prevColor != ColorNormal) {
3392 if (oldi > next_out) {
3393 SendToPlayer(&buf[next_out], oldi - next_out);
3396 Colorize(prevColor, TRUE);
3397 curColor = prevColor;
3399 if (savingComment) {
3400 parse_pos = i - oldi;
3401 memcpy(parse, &buf[oldi], parse_pos);
3402 parse[parse_pos] = NULLCHAR;
3403 started = STARTED_COMMENT;
3404 if(savingComment >= 3) // [HGM] chat: continuation of line for chat box
3405 chattingPartner = savingComment - 3; // kludge to remember the box
3407 started = STARTED_CHATTER;
3412 if (looking_at(buf, &i, "Black Strength :") ||
3413 looking_at(buf, &i, "<<< style 10 board >>>") ||
3414 looking_at(buf, &i, "<10>") ||
3415 looking_at(buf, &i, "#@#")) {
3416 /* Wrong board style */
3418 SendToICS(ics_prefix);
3419 SendToICS("set style 12\n");
3420 SendToICS(ics_prefix);
3421 SendToICS("refresh\n");
3425 if (!have_sent_ICS_logon && looking_at(buf, &i, "login:")) {
3427 have_sent_ICS_logon = 1;
3431 if (ics_getting_history != H_GETTING_MOVES /*smpos kludge*/ &&
3432 (looking_at(buf, &i, "\n<12> ") ||
3433 looking_at(buf, &i, "<12> "))) {
3435 if (oldi > next_out) {
3436 SendToPlayer(&buf[next_out], oldi - next_out);
3439 started = STARTED_BOARD;
3444 if ((started == STARTED_NONE && looking_at(buf, &i, "\n<b1> ")) ||
3445 looking_at(buf, &i, "<b1> ")) {
3446 if (oldi > next_out) {
3447 SendToPlayer(&buf[next_out], oldi - next_out);
3450 started = STARTED_HOLDINGS;
3455 if (looking_at(buf, &i, "* *vs. * *--- *")) {
3457 /* Header for a move list -- first line */
3459 switch (ics_getting_history) {
3463 case BeginningOfGame:
3464 /* User typed "moves" or "oldmoves" while we
3465 were idle. Pretend we asked for these
3466 moves and soak them up so user can step
3467 through them and/or save them.
3470 gameMode = IcsObserving;
3473 ics_getting_history = H_GOT_UNREQ_HEADER;
3475 case EditGame: /*?*/
3476 case EditPosition: /*?*/
3477 /* Should above feature work in these modes too? */
3478 /* For now it doesn't */
3479 ics_getting_history = H_GOT_UNWANTED_HEADER;
3482 ics_getting_history = H_GOT_UNWANTED_HEADER;
3487 /* Is this the right one? */
3488 if (gameInfo.white && gameInfo.black &&
3489 strcmp(gameInfo.white, star_match[0]) == 0 &&
3490 strcmp(gameInfo.black, star_match[2]) == 0) {
3492 ics_getting_history = H_GOT_REQ_HEADER;
3495 case H_GOT_REQ_HEADER:
3496 case H_GOT_UNREQ_HEADER:
3497 case H_GOT_UNWANTED_HEADER:
3498 case H_GETTING_MOVES:
3499 /* Should not happen */
3500 DisplayError(_("Error gathering move list: two headers"), 0);
3501 ics_getting_history = H_FALSE;
3505 /* Save player ratings into gameInfo if needed */
3506 if ((ics_getting_history == H_GOT_REQ_HEADER ||
3507 ics_getting_history == H_GOT_UNREQ_HEADER) &&
3508 (gameInfo.whiteRating == -1 ||
3509 gameInfo.blackRating == -1)) {
3511 gameInfo.whiteRating = string_to_rating(star_match[1]);
3512 gameInfo.blackRating = string_to_rating(star_match[3]);
3513 if (appData.debugMode)
3514 fprintf(debugFP, _("Ratings from header: W %d, B %d\n"),
3515 gameInfo.whiteRating, gameInfo.blackRating);
3520 if (looking_at(buf, &i,
3521 "* * match, initial time: * minute*, increment: * second")) {
3522 /* Header for a move list -- second line */
3523 /* Initial board will follow if this is a wild game */
3524 if (gameInfo.event != NULL) free(gameInfo.event);
3525 snprintf(str, MSG_SIZ, "ICS %s %s match", star_match[0], star_match[1]);
3526 gameInfo.event = StrSave(str);
3527 /* [HGM] we switched variant. Translate boards if needed. */
3528 VariantSwitch(boards[currentMove], StringToVariant(gameInfo.event));
3532 if (looking_at(buf, &i, "Move ")) {
3533 /* Beginning of a move list */
3534 switch (ics_getting_history) {
3536 /* Normally should not happen */
3537 /* Maybe user hit reset while we were parsing */
3540 /* Happens if we are ignoring a move list that is not
3541 * the one we just requested. Common if the user
3542 * tries to observe two games without turning off
3545 case H_GETTING_MOVES:
3546 /* Should not happen */
3547 DisplayError(_("Error gathering move list: nested"), 0);
3548 ics_getting_history = H_FALSE;
3550 case H_GOT_REQ_HEADER:
3551 ics_getting_history = H_GETTING_MOVES;
3552 started = STARTED_MOVES;
3554 if (oldi > next_out) {
3555 SendToPlayer(&buf[next_out], oldi - next_out);
3558 case H_GOT_UNREQ_HEADER:
3559 ics_getting_history = H_GETTING_MOVES;
3560 started = STARTED_MOVES_NOHIDE;
3563 case H_GOT_UNWANTED_HEADER:
3564 ics_getting_history = H_FALSE;
3570 if (looking_at(buf, &i, "% ") ||
3571 ((started == STARTED_MOVES || started == STARTED_MOVES_NOHIDE)
3572 && looking_at(buf, &i, "}*"))) { char *bookHit = NULL; // [HGM] book
3573 if(soughtPending && nrOfSeekAds) { // [HGM] seekgraph: on ICC sought-list has no termination line
3574 soughtPending = FALSE;
3578 if(suppressKibitz) next_out = i;
3579 savingComment = FALSE;
3583 case STARTED_MOVES_NOHIDE:
3584 memcpy(&parse[parse_pos], &buf[oldi], i - oldi);
3585 parse[parse_pos + i - oldi] = NULLCHAR;
3586 ParseGameHistory(parse);
3588 if (appData.zippyPlay && first.initDone) {
3589 FeedMovesToProgram(&first, forwardMostMove);
3590 if (gameMode == IcsPlayingWhite) {
3591 if (WhiteOnMove(forwardMostMove)) {
3592 if (first.sendTime) {
3593 if (first.useColors) {
3594 SendToProgram("black\n", &first);
3596 SendTimeRemaining(&first, TRUE);
3598 if (first.useColors) {
3599 SendToProgram("white\n", &first); // [HGM] book: made sending of "go\n" book dependent
3601 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: probe book for initial pos
3602 first.maybeThinking = TRUE;
3604 if (first.usePlayother) {
3605 if (first.sendTime) {
3606 SendTimeRemaining(&first, TRUE);
3608 SendToProgram("playother\n", &first);
3614 } else if (gameMode == IcsPlayingBlack) {
3615 if (!WhiteOnMove(forwardMostMove)) {
3616 if (first.sendTime) {
3617 if (first.useColors) {
3618 SendToProgram("white\n", &first);
3620 SendTimeRemaining(&first, FALSE);
3622 if (first.useColors) {
3623 SendToProgram("black\n", &first);
3625 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE);
3626 first.maybeThinking = TRUE;
3628 if (first.usePlayother) {
3629 if (first.sendTime) {
3630 SendTimeRemaining(&first, FALSE);
3632 SendToProgram("playother\n", &first);
3641 if (gameMode == IcsObserving && ics_gamenum == -1) {
3642 /* Moves came from oldmoves or moves command
3643 while we weren't doing anything else.
3645 currentMove = forwardMostMove;
3646 ClearHighlights();/*!!could figure this out*/
3647 flipView = appData.flipView;
3648 DrawPosition(TRUE, boards[currentMove]);
3649 DisplayBothClocks();
3650 snprintf(str, MSG_SIZ, "%s %s %s",
3651 gameInfo.white, _("vs."), gameInfo.black);
3655 /* Moves were history of an active game */
3656 if (gameInfo.resultDetails != NULL) {
3657 free(gameInfo.resultDetails);
3658 gameInfo.resultDetails = NULL;
3661 HistorySet(parseList, backwardMostMove,
3662 forwardMostMove, currentMove-1);
3663 DisplayMove(currentMove - 1);
3664 if (started == STARTED_MOVES) next_out = i;
3665 started = STARTED_NONE;
3666 ics_getting_history = H_FALSE;
3669 case STARTED_OBSERVE:
3670 started = STARTED_NONE;
3671 SendToICS(ics_prefix);
3672 SendToICS("refresh\n");
3678 if(bookHit) { // [HGM] book: simulate book reply
3679 static char bookMove[MSG_SIZ]; // a bit generous?
3681 programStats.nodes = programStats.depth = programStats.time =
3682 programStats.score = programStats.got_only_move = 0;
3683 sprintf(programStats.movelist, "%s (xbook)", bookHit);
3685 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
3686 strcat(bookMove, bookHit);
3687 HandleMachineMove(bookMove, &first);
3692 if ((started == STARTED_MOVES || started == STARTED_BOARD ||
3693 started == STARTED_HOLDINGS ||
3694 started == STARTED_MOVES_NOHIDE) && i >= leftover_len) {
3695 /* Accumulate characters in move list or board */
3696 parse[parse_pos++] = buf[i];
3699 /* Start of game messages. Mostly we detect start of game
3700 when the first board image arrives. On some versions
3701 of the ICS, though, we need to do a "refresh" after starting
3702 to observe in order to get the current board right away. */
3703 if (looking_at(buf, &i, "Adding game * to observation list")) {
3704 started = STARTED_OBSERVE;
3708 /* Handle auto-observe */
3709 if (appData.autoObserve &&
3710 (gameMode == IcsIdle || gameMode == BeginningOfGame) &&
3711 looking_at(buf, &i, "Game notification: * (*) vs. * (*)")) {
3713 /* Choose the player that was highlighted, if any. */
3714 if (star_match[0][0] == '\033' ||
3715 star_match[1][0] != '\033') {
3716 player = star_match[0];
3718 player = star_match[2];
3720 snprintf(str, MSG_SIZ, "%sobserve %s\n",
3721 ics_prefix, StripHighlightAndTitle(player));
3724 /* Save ratings from notify string */
3725 safeStrCpy(player1Name, star_match[0], sizeof(player1Name)/sizeof(player1Name[0]));
3726 player1Rating = string_to_rating(star_match[1]);
3727 safeStrCpy(player2Name, star_match[2], sizeof(player2Name)/sizeof(player2Name[0]));
3728 player2Rating = string_to_rating(star_match[3]);
3730 if (appData.debugMode)
3732 "Ratings from 'Game notification:' %s %d, %s %d\n",
3733 player1Name, player1Rating,
3734 player2Name, player2Rating);
3739 /* Deal with automatic examine mode after a game,
3740 and with IcsObserving -> IcsExamining transition */
3741 if (looking_at(buf, &i, "Entering examine mode for game *") ||
3742 looking_at(buf, &i, "has made you an examiner of game *")) {
3744 int gamenum = atoi(star_match[0]);
3745 if ((gameMode == IcsIdle || gameMode == IcsObserving) &&
3746 gamenum == ics_gamenum) {
3747 /* We were already playing or observing this game;
3748 no need to refetch history */
3749 gameMode = IcsExamining;
3751 pauseExamForwardMostMove = forwardMostMove;
3752 } else if (currentMove < forwardMostMove) {
3753 ForwardInner(forwardMostMove);
3756 /* I don't think this case really can happen */
3757 SendToICS(ics_prefix);
3758 SendToICS("refresh\n");
3763 /* Error messages */
3764 // if (ics_user_moved) {
3765 if (1) { // [HGM] old way ignored error after move type in; ics_user_moved is not set then!
3766 if (looking_at(buf, &i, "Illegal move") ||
3767 looking_at(buf, &i, "Not a legal move") ||
3768 looking_at(buf, &i, "Your king is in check") ||
3769 looking_at(buf, &i, "It isn't your turn") ||
3770 looking_at(buf, &i, "It is not your move")) {
3772 if (ics_user_moved && forwardMostMove > backwardMostMove) { // only backup if we already moved
3773 currentMove = forwardMostMove-1;
3774 DisplayMove(currentMove - 1); /* before DMError */
3775 DrawPosition(FALSE, boards[currentMove]);
3776 SwitchClocks(forwardMostMove-1); // [HGM] race
3777 DisplayBothClocks();
3779 DisplayMoveError(_("Illegal move (rejected by ICS)")); // [HGM] but always relay error msg
3785 if (looking_at(buf, &i, "still have time") ||
3786 looking_at(buf, &i, "not out of time") ||
3787 looking_at(buf, &i, "either player is out of time") ||
3788 looking_at(buf, &i, "has timeseal; checking")) {
3789 /* We must have called his flag a little too soon */
3790 whiteFlag = blackFlag = FALSE;
3794 if (looking_at(buf, &i, "added * seconds to") ||
3795 looking_at(buf, &i, "seconds were added to")) {
3796 /* Update the clocks */
3797 SendToICS(ics_prefix);
3798 SendToICS("refresh\n");
3802 if (!ics_clock_paused && looking_at(buf, &i, "clock paused")) {
3803 ics_clock_paused = TRUE;
3808 if (ics_clock_paused && looking_at(buf, &i, "clock resumed")) {
3809 ics_clock_paused = FALSE;
3814 /* Grab player ratings from the Creating: message.
3815 Note we have to check for the special case when
3816 the ICS inserts things like [white] or [black]. */
3817 if (looking_at(buf, &i, "Creating: * (*)* * (*)") ||
3818 looking_at(buf, &i, "Creating: * (*) [*] * (*)")) {
3820 0 player 1 name (not necessarily white)
3822 2 empty, white, or black (IGNORED)
3823 3 player 2 name (not necessarily black)
3826 The names/ratings are sorted out when the game
3827 actually starts (below).
3829 safeStrCpy(player1Name, StripHighlightAndTitle(star_match[0]), sizeof(player1Name)/sizeof(player1Name[0]));
3830 player1Rating = string_to_rating(star_match[1]);
3831 safeStrCpy(player2Name, StripHighlightAndTitle(star_match[3]), sizeof(player2Name)/sizeof(player2Name[0]));
3832 player2Rating = string_to_rating(star_match[4]);
3834 if (appData.debugMode)
3836 "Ratings from 'Creating:' %s %d, %s %d\n",
3837 player1Name, player1Rating,
3838 player2Name, player2Rating);
3843 /* Improved generic start/end-of-game messages */
3844 if ((tkind=0, looking_at(buf, &i, "{Game * (* vs. *) *}*")) ||
3845 (tkind=1, looking_at(buf, &i, "{Game * (*(*) vs. *(*)) *}*"))){
3846 /* If tkind == 0: */
3847 /* star_match[0] is the game number */
3848 /* [1] is the white player's name */
3849 /* [2] is the black player's name */
3850 /* For end-of-game: */
3851 /* [3] is the reason for the game end */
3852 /* [4] is a PGN end game-token, preceded by " " */
3853 /* For start-of-game: */
3854 /* [3] begins with "Creating" or "Continuing" */
3855 /* [4] is " *" or empty (don't care). */
3856 int gamenum = atoi(star_match[0]);
3857 char *whitename, *blackname, *why, *endtoken;
3858 ChessMove endtype = EndOfFile;
3861 whitename = star_match[1];
3862 blackname = star_match[2];
3863 why = star_match[3];
3864 endtoken = star_match[4];
3866 whitename = star_match[1];
3867 blackname = star_match[3];
3868 why = star_match[5];
3869 endtoken = star_match[6];
3872 /* Game start messages */
3873 if (strncmp(why, "Creating ", 9) == 0 ||
3874 strncmp(why, "Continuing ", 11) == 0) {
3875 gs_gamenum = gamenum;
3876 safeStrCpy(gs_kind, strchr(why, ' ') + 1,sizeof(gs_kind)/sizeof(gs_kind[0]));
3877 if(ics_gamenum == -1) // [HGM] only if we are not already involved in a game (because gin=1 sends us such messages)
3878 VariantSwitch(boards[currentMove], StringToVariant(gs_kind)); // [HGM] variantswitch: even before we get first board
3880 if (appData.zippyPlay) {
3881 ZippyGameStart(whitename, blackname);
3884 partnerBoardValid = FALSE; // [HGM] bughouse
3888 /* Game end messages */
3889 if (gameMode == IcsIdle || gameMode == BeginningOfGame ||
3890 ics_gamenum != gamenum) {
3893 while (endtoken[0] == ' ') endtoken++;
3894 switch (endtoken[0]) {
3897 endtype = GameUnfinished;
3900 endtype = BlackWins;
3903 if (endtoken[1] == '/')
3904 endtype = GameIsDrawn;
3906 endtype = WhiteWins;
3909 GameEnds(endtype, why, GE_ICS);
3911 if (appData.zippyPlay && first.initDone) {
3912 ZippyGameEnd(endtype, why);
3913 if (first.pr == NoProc) {
3914 /* Start the next process early so that we'll
3915 be ready for the next challenge */
3916 StartChessProgram(&first);
3918 /* Send "new" early, in case this command takes
3919 a long time to finish, so that we'll be ready
3920 for the next challenge. */
3921 gameInfo.variant = VariantNormal; // [HGM] variantswitch: suppress sending of 'variant'
3925 if(appData.bgObserve && partnerBoardValid) DrawPosition(TRUE, partnerBoard);
3929 if (looking_at(buf, &i, "Removing game * from observation") ||
3930 looking_at(buf, &i, "no longer observing game *") ||
3931 looking_at(buf, &i, "Game * (*) has no examiners")) {
3932 if (gameMode == IcsObserving &&
3933 atoi(star_match[0]) == ics_gamenum)
3935 /* icsEngineAnalyze */
3936 if (appData.icsEngineAnalyze) {
3943 ics_user_moved = FALSE;
3948 if (looking_at(buf, &i, "no longer examining game *")) {
3949 if (gameMode == IcsExamining &&
3950 atoi(star_match[0]) == ics_gamenum)
3954 ics_user_moved = FALSE;
3959 /* Advance leftover_start past any newlines we find,
3960 so only partial lines can get reparsed */
3961 if (looking_at(buf, &i, "\n")) {
3962 prevColor = curColor;
3963 if (curColor != ColorNormal) {
3964 if (oldi > next_out) {
3965 SendToPlayer(&buf[next_out], oldi - next_out);
3968 Colorize(ColorNormal, FALSE);
3969 curColor = ColorNormal;
3971 if (started == STARTED_BOARD) {
3972 started = STARTED_NONE;
3973 parse[parse_pos] = NULLCHAR;
3974 ParseBoard12(parse);
3977 /* Send premove here */
3978 if (appData.premove) {
3980 if (currentMove == 0 &&
3981 gameMode == IcsPlayingWhite &&
3982 appData.premoveWhite) {
3983 snprintf(str, MSG_SIZ, "%s\n", appData.premoveWhiteText);
3984 if (appData.debugMode)
3985 fprintf(debugFP, "Sending premove:\n");
3987 } else if (currentMove == 1 &&
3988 gameMode == IcsPlayingBlack &&
3989 appData.premoveBlack) {
3990 snprintf(str, MSG_SIZ, "%s\n", appData.premoveBlackText);
3991 if (appData.debugMode)
3992 fprintf(debugFP, "Sending premove:\n");
3994 } else if (gotPremove) {
3996 ClearPremoveHighlights();
3997 if (appData.debugMode)
3998 fprintf(debugFP, "Sending premove:\n");
3999 UserMoveEvent(premoveFromX, premoveFromY,
4000 premoveToX, premoveToY,
4005 /* Usually suppress following prompt */
4006 if (!(forwardMostMove == 0 && gameMode == IcsExamining)) {
4007 while(looking_at(buf, &i, "\n")); // [HGM] skip empty lines
4008 if (looking_at(buf, &i, "*% ")) {
4009 savingComment = FALSE;
4014 } else if (started == STARTED_HOLDINGS) {
4016 char new_piece[MSG_SIZ];
4017 started = STARTED_NONE;
4018 parse[parse_pos] = NULLCHAR;
4019 if (appData.debugMode)
4020 fprintf(debugFP, "Parsing holdings: %s, currentMove = %d\n",
4021 parse, currentMove);
4022 if (sscanf(parse, " game %d", &gamenum) == 1) {
4023 if(gamenum == ics_gamenum) { // [HGM] bughouse: old code if part of foreground game
4024 if (gameInfo.variant == VariantNormal) {
4025 /* [HGM] We seem to switch variant during a game!
4026 * Presumably no holdings were displayed, so we have
4027 * to move the position two files to the right to
4028 * create room for them!
4030 VariantClass newVariant;
4031 switch(gameInfo.boardWidth) { // base guess on board width
4032 case 9: newVariant = VariantShogi; break;
4033 case 10: newVariant = VariantGreat; break;
4034 default: newVariant = VariantCrazyhouse; break;
4036 VariantSwitch(boards[currentMove], newVariant); /* temp guess */
4037 /* Get a move list just to see the header, which
4038 will tell us whether this is really bug or zh */
4039 if (ics_getting_history == H_FALSE) {
4040 ics_getting_history = H_REQUESTED;
4041 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4045 new_piece[0] = NULLCHAR;
4046 sscanf(parse, "game %d white [%s black [%s <- %s",
4047 &gamenum, white_holding, black_holding,
4049 white_holding[strlen(white_holding)-1] = NULLCHAR;
4050 black_holding[strlen(black_holding)-1] = NULLCHAR;
4051 /* [HGM] copy holdings to board holdings area */
4052 CopyHoldings(boards[forwardMostMove], white_holding, WhitePawn);
4053 CopyHoldings(boards[forwardMostMove], black_holding, BlackPawn);
4054 boards[forwardMostMove][HOLDINGS_SET] = 1; // flag holdings as set
4056 if (appData.zippyPlay && first.initDone) {
4057 ZippyHoldings(white_holding, black_holding,
4061 if (tinyLayout || smallLayout) {
4062 char wh[16], bh[16];
4063 PackHolding(wh, white_holding);
4064 PackHolding(bh, black_holding);
4065 snprintf(str, MSG_SIZ, "[%s-%s] %s-%s", wh, bh,
4066 gameInfo.white, gameInfo.black);
4068 snprintf(str, MSG_SIZ, "%s [%s] %s %s [%s]",
4069 gameInfo.white, white_holding, _("vs."),
4070 gameInfo.black, black_holding);
4072 if(!partnerUp) // [HGM] bughouse: when peeking at partner game we already know what he captured...
4073 DrawPosition(FALSE, boards[currentMove]);
4075 } else if(appData.bgObserve) { // [HGM] bughouse: holdings of other game => background
4076 sscanf(parse, "game %d white [%s black [%s <- %s",
4077 &gamenum, white_holding, black_holding,
4079 white_holding[strlen(white_holding)-1] = NULLCHAR;
4080 black_holding[strlen(black_holding)-1] = NULLCHAR;
4081 /* [HGM] copy holdings to partner-board holdings area */
4082 CopyHoldings(partnerBoard, white_holding, WhitePawn);
4083 CopyHoldings(partnerBoard, black_holding, BlackPawn);
4084 if(twoBoards) { partnerUp = 1; flipView = !flipView; } // [HGM] dual: always draw
4085 if(partnerUp) DrawPosition(FALSE, partnerBoard);
4086 if(twoBoards) { partnerUp = 0; flipView = !flipView; }
4089 /* Suppress following prompt */
4090 if (looking_at(buf, &i, "*% ")) {
4091 if(strchr(star_match[0], 7)) SendToPlayer("\007", 1); // Bell(); // FICS fuses bell for next board with prompt in zh captures
4092 savingComment = FALSE;
4100 i++; /* skip unparsed character and loop back */
4103 if (started != STARTED_MOVES && started != STARTED_BOARD && !suppressKibitz && // [HGM] kibitz
4104 // started != STARTED_HOLDINGS && i > next_out) { // [HGM] should we compare to leftover_start in stead of i?
4105 // SendToPlayer(&buf[next_out], i - next_out);
4106 started != STARTED_HOLDINGS && leftover_start > next_out) {
4107 SendToPlayer(&buf[next_out], leftover_start - next_out);
4111 leftover_len = buf_len - leftover_start;
4112 /* if buffer ends with something we couldn't parse,
4113 reparse it after appending the next read */
4115 } else if (count == 0) {
4116 RemoveInputSource(isr);
4117 DisplayFatalError(_("Connection closed by ICS"), 0, 0);
4119 DisplayFatalError(_("Error reading from ICS"), error, 1);
4124 /* Board style 12 looks like this:
4126 <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
4128 * The "<12> " is stripped before it gets to this routine. The two
4129 * trailing 0's (flip state and clock ticking) are later addition, and
4130 * some chess servers may not have them, or may have only the first.
4131 * Additional trailing fields may be added in the future.
4134 #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"
4136 #define RELATION_OBSERVING_PLAYED 0
4137 #define RELATION_OBSERVING_STATIC -2 /* examined, oldmoves, or smoves */
4138 #define RELATION_PLAYING_MYMOVE 1
4139 #define RELATION_PLAYING_NOTMYMOVE -1
4140 #define RELATION_EXAMINING 2
4141 #define RELATION_ISOLATED_BOARD -3
4142 #define RELATION_STARTING_POSITION -4 /* FICS only */
4145 ParseBoard12 (char *string)
4147 GameMode newGameMode;
4148 int gamenum, newGame, newMove, relation, basetime, increment, ics_flip = 0, i;
4149 int j, k, n, moveNum, white_stren, black_stren, white_time, black_time, takeback;
4150 int double_push, castle_ws, castle_wl, castle_bs, castle_bl, irrev_count;
4151 char to_play, board_chars[200];
4152 char move_str[MSG_SIZ], str[MSG_SIZ], elapsed_time[MSG_SIZ];
4153 char black[32], white[32];
4155 int prevMove = currentMove;
4158 int fromX, fromY, toX, toY;
4160 int ranks=1, files=0; /* [HGM] ICS80: allow variable board size */
4161 char *bookHit = NULL; // [HGM] book
4162 Boolean weird = FALSE, reqFlag = FALSE;
4164 fromX = fromY = toX = toY = -1;
4168 if (appData.debugMode)
4169 fprintf(debugFP, _("Parsing board: %s\n"), string);
4171 move_str[0] = NULLCHAR;
4172 elapsed_time[0] = NULLCHAR;
4173 { /* [HGM] figure out how many ranks and files the board has, for ICS extension used by Capablanca server */
4175 while(i < 199 && (string[i] != ' ' || string[i+2] != ' ')) {
4176 if(string[i] == ' ') { ranks++; files = 0; }
4178 if(!strchr(" -pnbrqkPNBRQK" , string[i])) weird = TRUE; // test for fairies
4181 for(j = 0; j <i; j++) board_chars[j] = string[j];
4182 board_chars[i] = '\0';
4185 n = sscanf(string, PATTERN, &to_play, &double_push,
4186 &castle_ws, &castle_wl, &castle_bs, &castle_bl, &irrev_count,
4187 &gamenum, white, black, &relation, &basetime, &increment,
4188 &white_stren, &black_stren, &white_time, &black_time,
4189 &moveNum, str, elapsed_time, move_str, &ics_flip,
4193 snprintf(str, MSG_SIZ, _("Failed to parse board string:\n\"%s\""), string);
4194 DisplayError(str, 0);
4198 /* Convert the move number to internal form */
4199 moveNum = (moveNum - 1) * 2;
4200 if (to_play == 'B') moveNum++;
4201 if (moveNum > framePtr) { // [HGM] vari: do not run into saved variations
4202 DisplayFatalError(_("Game too long; increase MAX_MOVES and recompile"),
4208 case RELATION_OBSERVING_PLAYED:
4209 case RELATION_OBSERVING_STATIC:
4210 if (gamenum == -1) {
4211 /* Old ICC buglet */
4212 relation = RELATION_OBSERVING_STATIC;
4214 newGameMode = IcsObserving;
4216 case RELATION_PLAYING_MYMOVE:
4217 case RELATION_PLAYING_NOTMYMOVE:
4219 ((relation == RELATION_PLAYING_MYMOVE) == (to_play == 'W')) ?
4220 IcsPlayingWhite : IcsPlayingBlack;
4221 soughtPending =FALSE; // [HGM] seekgraph: solve race condition
4223 case RELATION_EXAMINING:
4224 newGameMode = IcsExamining;
4226 case RELATION_ISOLATED_BOARD:
4228 /* Just display this board. If user was doing something else,
4229 we will forget about it until the next board comes. */
4230 newGameMode = IcsIdle;
4232 case RELATION_STARTING_POSITION:
4233 newGameMode = gameMode;
4237 if((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)
4238 && newGameMode == IcsObserving && gamenum != ics_gamenum && appData.bgObserve) {
4239 // [HGM] bughouse: don't act on alien boards while we play. Just parse the board and save it */
4241 for (k = 0; k < ranks; k++) {
4242 for (j = 0; j < files; j++)
4243 board[k][j+gameInfo.holdingsWidth] = CharToPiece(board_chars[(ranks-1-k)*(files+1) + j]);
4244 if(gameInfo.holdingsWidth > 1) {
4245 board[k][0] = board[k][BOARD_WIDTH-1] = EmptySquare;
4246 board[k][1] = board[k][BOARD_WIDTH-2] = (ChessSquare) 0;;
4249 CopyBoard(partnerBoard, board);
4250 if(toSqr = strchr(str, '/')) { // extract highlights from long move
4251 partnerBoard[EP_STATUS-3] = toSqr[1] - AAA; // kludge: hide highlighting info in board
4252 partnerBoard[EP_STATUS-4] = toSqr[2] - ONE;
4253 } else partnerBoard[EP_STATUS-4] = partnerBoard[EP_STATUS-3] = -1;
4254 if(toSqr = strchr(str, '-')) {
4255 partnerBoard[EP_STATUS-1] = toSqr[1] - AAA;
4256 partnerBoard[EP_STATUS-2] = toSqr[2] - ONE;
4257 } else partnerBoard[EP_STATUS-1] = partnerBoard[EP_STATUS-2] = -1;
4258 if(appData.dualBoard && !twoBoards) { twoBoards = 1; InitDrawingSizes(-2,0); }
4259 if(twoBoards) { partnerUp = 1; flipView = !flipView; } // [HGM] dual
4260 if(partnerUp) DrawPosition(FALSE, partnerBoard);
4261 if(twoBoards) { partnerUp = 0; flipView = !flipView; } // [HGM] dual
4262 snprintf(partnerStatus, MSG_SIZ,"W: %d:%02d B: %d:%02d (%d-%d) %c", white_time/60000, (white_time%60000)/1000,
4263 (black_time/60000), (black_time%60000)/1000, white_stren, black_stren, to_play);
4264 DisplayMessage(partnerStatus, "");
4265 partnerBoardValid = TRUE;
4269 /* Modify behavior for initial board display on move listing
4272 switch (ics_getting_history) {
4276 case H_GOT_REQ_HEADER:
4277 case H_GOT_UNREQ_HEADER:
4278 /* This is the initial position of the current game */
4279 gamenum = ics_gamenum;
4280 moveNum = 0; /* old ICS bug workaround */
4281 if (to_play == 'B') {
4282 startedFromSetupPosition = TRUE;
4283 blackPlaysFirst = TRUE;
4285 if (forwardMostMove == 0) forwardMostMove = 1;
4286 if (backwardMostMove == 0) backwardMostMove = 1;
4287 if (currentMove == 0) currentMove = 1;
4289 newGameMode = gameMode;
4290 relation = RELATION_STARTING_POSITION; /* ICC needs this */
4292 case H_GOT_UNWANTED_HEADER:
4293 /* This is an initial board that we don't want */
4295 case H_GETTING_MOVES:
4296 /* Should not happen */
4297 DisplayError(_("Error gathering move list: extra board"), 0);
4298 ics_getting_history = H_FALSE;
4302 if (gameInfo.boardHeight != ranks || gameInfo.boardWidth != files ||
4303 weird && (int)gameInfo.variant < (int)VariantShogi) {
4304 /* [HGM] We seem to have switched variant unexpectedly
4305 * Try to guess new variant from board size
4307 VariantClass newVariant = VariantFairy; // if 8x8, but fairies present
4308 if(ranks == 8 && files == 10) newVariant = VariantCapablanca; else
4309 if(ranks == 10 && files == 9) newVariant = VariantXiangqi; else
4310 if(ranks == 8 && files == 12) newVariant = VariantCourier; else
4311 if(ranks == 9 && files == 9) newVariant = VariantShogi; else
4312 if(!weird) newVariant = VariantNormal;
4313 VariantSwitch(boards[currentMove], newVariant); /* temp guess */
4314 /* Get a move list just to see the header, which
4315 will tell us whether this is really bug or zh */
4316 if (ics_getting_history == H_FALSE) {
4317 ics_getting_history = H_REQUESTED; reqFlag = TRUE;
4318 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4323 /* Take action if this is the first board of a new game, or of a
4324 different game than is currently being displayed. */
4325 if (gamenum != ics_gamenum || newGameMode != gameMode ||
4326 relation == RELATION_ISOLATED_BOARD) {
4328 /* Forget the old game and get the history (if any) of the new one */
4329 if (gameMode != BeginningOfGame) {
4333 if (appData.autoRaiseBoard) BoardToTop();
4335 if (gamenum == -1) {
4336 newGameMode = IcsIdle;
4337 } else if ((moveNum > 0 || newGameMode == IcsObserving) && newGameMode != IcsIdle &&
4338 appData.getMoveList && !reqFlag) {
4339 /* Need to get game history */
4340 ics_getting_history = H_REQUESTED;
4341 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4345 /* Initially flip the board to have black on the bottom if playing
4346 black or if the ICS flip flag is set, but let the user change
4347 it with the Flip View button. */
4348 flipView = appData.autoFlipView ?
4349 (newGameMode == IcsPlayingBlack) || ics_flip :
4352 /* Done with values from previous mode; copy in new ones */
4353 gameMode = newGameMode;
4355 ics_gamenum = gamenum;
4356 if (gamenum == gs_gamenum) {
4357 int klen = strlen(gs_kind);
4358 if (gs_kind[klen - 1] == '.') gs_kind[klen - 1] = NULLCHAR;
4359 snprintf(str, MSG_SIZ, "ICS %s", gs_kind);
4360 gameInfo.event = StrSave(str);
4362 gameInfo.event = StrSave("ICS game");
4364 gameInfo.site = StrSave(appData.icsHost);
4365 gameInfo.date = PGNDate();
4366 gameInfo.round = StrSave("-");
4367 gameInfo.white = StrSave(white);
4368 gameInfo.black = StrSave(black);
4369 timeControl = basetime * 60 * 1000;
4371 timeIncrement = increment * 1000;
4372 movesPerSession = 0;
4373 gameInfo.timeControl = TimeControlTagValue();
4374 VariantSwitch(boards[currentMove], StringToVariant(gameInfo.event) );
4375 if (appData.debugMode) {
4376 fprintf(debugFP, "ParseBoard says variant = '%s'\n", gameInfo.event);
4377 fprintf(debugFP, "recognized as %s\n", VariantName(gameInfo.variant));
4378 setbuf(debugFP, NULL);
4381 gameInfo.outOfBook = NULL;
4383 /* Do we have the ratings? */
4384 if (strcmp(player1Name, white) == 0 &&
4385 strcmp(player2Name, black) == 0) {
4386 if (appData.debugMode)
4387 fprintf(debugFP, "Remembered ratings: W %d, B %d\n",
4388 player1Rating, player2Rating);
4389 gameInfo.whiteRating = player1Rating;
4390 gameInfo.blackRating = player2Rating;
4391 } else if (strcmp(player2Name, white) == 0 &&
4392 strcmp(player1Name, black) == 0) {
4393 if (appData.debugMode)
4394 fprintf(debugFP, "Remembered ratings: W %d, B %d\n",
4395 player2Rating, player1Rating);
4396 gameInfo.whiteRating = player2Rating;
4397 gameInfo.blackRating = player1Rating;
4399 player1Name[0] = player2Name[0] = NULLCHAR;
4401 /* Silence shouts if requested */
4402 if (appData.quietPlay &&
4403 (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)) {
4404 SendToICS(ics_prefix);
4405 SendToICS("set shout 0\n");
4409 /* Deal with midgame name changes */
4411 if (!gameInfo.white || strcmp(gameInfo.white, white) != 0) {
4412 if (gameInfo.white) free(gameInfo.white);
4413 gameInfo.white = StrSave(white);
4415 if (!gameInfo.black || strcmp(gameInfo.black, black) != 0) {
4416 if (gameInfo.black) free(gameInfo.black);
4417 gameInfo.black = StrSave(black);
4421 /* Throw away game result if anything actually changes in examine mode */
4422 if (gameMode == IcsExamining && !newGame) {
4423 gameInfo.result = GameUnfinished;
4424 if (gameInfo.resultDetails != NULL) {
4425 free(gameInfo.resultDetails);
4426 gameInfo.resultDetails = NULL;
4430 /* In pausing && IcsExamining mode, we ignore boards coming
4431 in if they are in a different variation than we are. */
4432 if (pauseExamInvalid) return;
4433 if (pausing && gameMode == IcsExamining) {
4434 if (moveNum <= pauseExamForwardMostMove) {
4435 pauseExamInvalid = TRUE;
4436 forwardMostMove = pauseExamForwardMostMove;
4441 if (appData.debugMode) {
4442 fprintf(debugFP, "load %dx%d board\n", files, ranks);
4444 /* Parse the board */
4445 for (k = 0; k < ranks; k++) {
4446 for (j = 0; j < files; j++)
4447 board[k][j+gameInfo.holdingsWidth] = CharToPiece(board_chars[(ranks-1-k)*(files+1) + j]);
4448 if(gameInfo.holdingsWidth > 1) {
4449 board[k][0] = board[k][BOARD_WIDTH-1] = EmptySquare;
4450 board[k][1] = board[k][BOARD_WIDTH-2] = (ChessSquare) 0;;
4453 if(moveNum==0 && gameInfo.variant == VariantSChess) {
4454 board[5][BOARD_RGHT+1] = WhiteAngel;
4455 board[6][BOARD_RGHT+1] = WhiteMarshall;
4456 board[1][0] = BlackMarshall;
4457 board[2][0] = BlackAngel;
4458 board[1][1] = board[2][1] = board[5][BOARD_RGHT] = board[6][BOARD_RGHT] = 1;
4460 CopyBoard(boards[moveNum], board);
4461 boards[moveNum][HOLDINGS_SET] = 0; // [HGM] indicate holdings not set
4463 startedFromSetupPosition =
4464 !CompareBoards(board, initialPosition);
4465 if(startedFromSetupPosition)
4466 initialRulePlies = irrev_count; /* [HGM] 50-move counter offset */
4469 /* [HGM] Set castling rights. Take the outermost Rooks,
4470 to make it also work for FRC opening positions. Note that board12
4471 is really defective for later FRC positions, as it has no way to
4472 indicate which Rook can castle if they are on the same side of King.
4473 For the initial position we grant rights to the outermost Rooks,
4474 and remember thos rights, and we then copy them on positions
4475 later in an FRC game. This means WB might not recognize castlings with
4476 Rooks that have moved back to their original position as illegal,
4477 but in ICS mode that is not its job anyway.
4479 if(moveNum == 0 || gameInfo.variant != VariantFischeRandom)
4480 { int i, j; ChessSquare wKing = WhiteKing, bKing = BlackKing;
4482 for(i=BOARD_LEFT, j=NoRights; i<BOARD_RGHT; i++)
4483 if(board[0][i] == WhiteRook) j = i;
4484 initialRights[0] = boards[moveNum][CASTLING][0] = (castle_ws == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4485 for(i=BOARD_RGHT-1, j=NoRights; i>=BOARD_LEFT; i--)
4486 if(board[0][i] == WhiteRook) j = i;
4487 initialRights[1] = boards[moveNum][CASTLING][1] = (castle_wl == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4488 for(i=BOARD_LEFT, j=NoRights; i<BOARD_RGHT; i++)
4489 if(board[BOARD_HEIGHT-1][i] == BlackRook) j = i;
4490 initialRights[3] = boards[moveNum][CASTLING][3] = (castle_bs == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4491 for(i=BOARD_RGHT-1, j=NoRights; i>=BOARD_LEFT; i--)
4492 if(board[BOARD_HEIGHT-1][i] == BlackRook) j = i;
4493 initialRights[4] = boards[moveNum][CASTLING][4] = (castle_bl == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4495 boards[moveNum][CASTLING][2] = boards[moveNum][CASTLING][5] = NoRights;
4496 if(gameInfo.variant == VariantKnightmate) { wKing = WhiteUnicorn; bKing = BlackUnicorn; }
4497 for(k=BOARD_LEFT; k<BOARD_RGHT; k++)
4498 if(board[0][k] == wKing) initialRights[2] = boards[moveNum][CASTLING][2] = k;
4499 for(k=BOARD_LEFT; k<BOARD_RGHT; k++)
4500 if(board[BOARD_HEIGHT-1][k] == bKing)
4501 initialRights[5] = boards[moveNum][CASTLING][5] = k;
4502 if(gameInfo.variant == VariantTwoKings) {
4503 // In TwoKings looking for a King does not work, so always give castling rights to a King on e1/e8
4504 if(board[0][4] == wKing) initialRights[2] = boards[moveNum][CASTLING][2] = 4;
4505 if(board[BOARD_HEIGHT-1][4] == bKing) initialRights[5] = boards[moveNum][CASTLING][5] = 4;
4508 r = boards[moveNum][CASTLING][0] = initialRights[0];
4509 if(board[0][r] != WhiteRook) boards[moveNum][CASTLING][0] = NoRights;
4510 r = boards[moveNum][CASTLING][1] = initialRights[1];
4511 if(board[0][r] != WhiteRook) boards[moveNum][CASTLING][1] = NoRights;
4512 r = boards[moveNum][CASTLING][3] = initialRights[3];
4513 if(board[BOARD_HEIGHT-1][r] != BlackRook) boards[moveNum][CASTLING][3] = NoRights;
4514 r = boards[moveNum][CASTLING][4] = initialRights[4];
4515 if(board[BOARD_HEIGHT-1][r] != BlackRook) boards[moveNum][CASTLING][4] = NoRights;
4516 /* wildcastle kludge: always assume King has rights */
4517 r = boards[moveNum][CASTLING][2] = initialRights[2];
4518 r = boards[moveNum][CASTLING][5] = initialRights[5];
4520 /* [HGM] e.p. rights. Assume that ICS sends file number here? */
4521 boards[moveNum][EP_STATUS] = EP_NONE;
4522 if(str[0] == 'P') boards[moveNum][EP_STATUS] = EP_PAWN_MOVE;
4523 if(strchr(move_str, 'x')) boards[moveNum][EP_STATUS] = EP_CAPTURE;
4524 if(double_push != -1) boards[moveNum][EP_STATUS] = double_push + BOARD_LEFT;
4527 if (ics_getting_history == H_GOT_REQ_HEADER ||
4528 ics_getting_history == H_GOT_UNREQ_HEADER) {
4529 /* This was an initial position from a move list, not
4530 the current position */
4534 /* Update currentMove and known move number limits */
4535 newMove = newGame || moveNum > forwardMostMove;
4538 forwardMostMove = backwardMostMove = currentMove = moveNum;
4539 if (gameMode == IcsExamining && moveNum == 0) {
4540 /* Workaround for ICS limitation: we are not told the wild
4541 type when starting to examine a game. But if we ask for
4542 the move list, the move list header will tell us */
4543 ics_getting_history = H_REQUESTED;
4544 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4547 } else if (moveNum == forwardMostMove + 1 || moveNum == forwardMostMove
4548 || (moveNum < forwardMostMove && moveNum >= backwardMostMove)) {
4550 /* [DM] If we found takebacks during icsEngineAnalyze try send to engine */
4551 /* [HGM] applied this also to an engine that is silently watching */
4552 if (appData.zippyPlay && moveNum < forwardMostMove && first.initDone &&
4553 (gameMode == IcsObserving || gameMode == IcsExamining) &&
4554 gameInfo.variant == currentlyInitializedVariant) {
4555 takeback = forwardMostMove - moveNum;
4556 for (i = 0; i < takeback; i++) {
4557 if (appData.debugMode) fprintf(debugFP, "take back move\n");
4558 SendToProgram("undo\n", &first);
4563 forwardMostMove = moveNum;
4564 if (!pausing || currentMove > forwardMostMove)
4565 currentMove = forwardMostMove;
4567 /* New part of history that is not contiguous with old part */
4568 if (pausing && gameMode == IcsExamining) {
4569 pauseExamInvalid = TRUE;
4570 forwardMostMove = pauseExamForwardMostMove;
4573 if (gameMode == IcsExamining && moveNum > 0 && appData.getMoveList) {
4575 if(appData.zippyPlay && forwardMostMove > 0 && first.initDone) {
4576 // [HGM] when we will receive the move list we now request, it will be
4577 // fed to the engine from the first move on. So if the engine is not
4578 // in the initial position now, bring it there.
4579 InitChessProgram(&first, 0);
4582 ics_getting_history = H_REQUESTED;
4583 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4586 forwardMostMove = backwardMostMove = currentMove = moveNum;
4589 /* Update the clocks */
4590 if (strchr(elapsed_time, '.')) {
4592 timeRemaining[0][moveNum] = whiteTimeRemaining = white_time;
4593 timeRemaining[1][moveNum] = blackTimeRemaining = black_time;
4595 /* Time is in seconds */
4596 timeRemaining[0][moveNum] = whiteTimeRemaining = white_time * 1000;
4597 timeRemaining[1][moveNum] = blackTimeRemaining = black_time * 1000;
4602 if (appData.zippyPlay && newGame &&
4603 gameMode != IcsObserving && gameMode != IcsIdle &&
4604 gameMode != IcsExamining)
4605 ZippyFirstBoard(moveNum, basetime, increment);
4608 /* Put the move on the move list, first converting
4609 to canonical algebraic form. */
4611 if (appData.debugMode) {
4612 if (appData.debugMode) { int f = forwardMostMove;
4613 fprintf(debugFP, "parseboard %d, castling = %d %d %d %d %d %d\n", f,
4614 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
4615 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
4617 fprintf(debugFP, "accepted move %s from ICS, parse it.\n", move_str);
4618 fprintf(debugFP, "moveNum = %d\n", moveNum);
4619 fprintf(debugFP, "board = %d-%d x %d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT);
4620 setbuf(debugFP, NULL);
4622 if (moveNum <= backwardMostMove) {
4623 /* We don't know what the board looked like before
4625 safeStrCpy(parseList[moveNum - 1], move_str, sizeof(parseList[moveNum - 1])/sizeof(parseList[moveNum - 1][0]));
4626 strcat(parseList[moveNum - 1], " ");
4627 strcat(parseList[moveNum - 1], elapsed_time);
4628 moveList[moveNum - 1][0] = NULLCHAR;
4629 } else if (strcmp(move_str, "none") == 0) {
4630 // [HGM] long SAN: swapped order; test for 'none' before parsing move
4631 /* Again, we don't know what the board looked like;
4632 this is really the start of the game. */
4633 parseList[moveNum - 1][0] = NULLCHAR;
4634 moveList[moveNum - 1][0] = NULLCHAR;
4635 backwardMostMove = moveNum;
4636 startedFromSetupPosition = TRUE;
4637 fromX = fromY = toX = toY = -1;
4639 // [HGM] long SAN: if legality-testing is off, disambiguation might not work or give wrong move.
4640 // So we parse the long-algebraic move string in stead of the SAN move
4641 int valid; char buf[MSG_SIZ], *prom;
4643 if(gameInfo.variant == VariantShogi && !strchr(move_str, '=') && !strchr(move_str, '@'))
4644 strcat(move_str, "="); // if ICS does not say 'promote' on non-drop, we defer.
4645 // str looks something like "Q/a1-a2"; kill the slash
4647 snprintf(buf, MSG_SIZ,"%c%s", str[0], str+2);
4648 else safeStrCpy(buf, str, sizeof(buf)/sizeof(buf[0])); // might be castling
4649 if((prom = strstr(move_str, "=")) && !strstr(buf, "="))
4650 strcat(buf, prom); // long move lacks promo specification!
4651 if(!appData.testLegality && move_str[1] != '@') { // drops never ambiguous (parser chokes on long form!)
4652 if(appData.debugMode)
4653 fprintf(debugFP, "replaced ICS move '%s' by '%s'\n", move_str, buf);
4654 safeStrCpy(move_str, buf, MSG_SIZ);
4656 valid = ParseOneMove(move_str, moveNum - 1, &moveType,
4657 &fromX, &fromY, &toX, &toY, &promoChar)
4658 || ParseOneMove(buf, moveNum - 1, &moveType,
4659 &fromX, &fromY, &toX, &toY, &promoChar);
4660 // end of long SAN patch
4662 (void) CoordsToAlgebraic(boards[moveNum - 1],
4663 PosFlags(moveNum - 1),
4664 fromY, fromX, toY, toX, promoChar,
4665 parseList[moveNum-1]);
4666 switch (MateTest(boards[moveNum], PosFlags(moveNum)) ) {
4672 if(gameInfo.variant != VariantShogi)
4673 strcat(parseList[moveNum - 1], "+");
4676 case MT_STAINMATE: // [HGM] xq: for notation stalemate that wins counts as checkmate
4677 strcat(parseList[moveNum - 1], "#");
4680 strcat(parseList[moveNum - 1], " ");
4681 strcat(parseList[moveNum - 1], elapsed_time);
4682 /* currentMoveString is set as a side-effect of ParseOneMove */
4683 if(gameInfo.variant == VariantShogi && currentMoveString[4]) currentMoveString[4] = '^';
4684 safeStrCpy(moveList[moveNum - 1], currentMoveString, sizeof(moveList[moveNum - 1])/sizeof(moveList[moveNum - 1][0]));
4685 strcat(moveList[moveNum - 1], "\n");
4687 if(gameInfo.holdingsWidth && !appData.disguise && gameInfo.variant != VariantSuper && gameInfo.variant != VariantGreat
4688 && gameInfo.variant != VariantGrand&& gameInfo.variant != VariantSChess) // inherit info that ICS does not give from previous board
4689 for(k=0; k<ranks; k++) for(j=BOARD_LEFT; j<BOARD_RGHT; j++) {
4690 ChessSquare old, new = boards[moveNum][k][j];
4691 if(fromY == DROP_RANK && k==toY && j==toX) continue; // dropped pieces always stand for themselves
4692 old = (k==toY && j==toX) ? boards[moveNum-1][fromY][fromX] : boards[moveNum-1][k][j]; // trace back mover
4693 if(old == new) continue;
4694 if(old == PROMOTED new) boards[moveNum][k][j] = old; // prevent promoted pieces to revert to primordial ones
4695 else if(new == WhiteWazir || new == BlackWazir) {
4696 if(old < WhiteCannon || old >= BlackPawn && old < BlackCannon)
4697 boards[moveNum][k][j] = PROMOTED old; // choose correct type of Gold in promotion
4698 else boards[moveNum][k][j] = old; // preserve type of Gold
4699 } else if((old == WhitePawn || old == BlackPawn) && new != EmptySquare) // Pawn promotions (but not e.p.capture!)
4700 boards[moveNum][k][j] = PROMOTED new; // use non-primordial representation of chosen piece
4703 /* Move from ICS was illegal!? Punt. */
4704 if (appData.debugMode) {
4705 fprintf(debugFP, "Illegal move from ICS '%s'\n", move_str);
4706 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
4708 safeStrCpy(parseList[moveNum - 1], move_str, sizeof(parseList[moveNum - 1])/sizeof(parseList[moveNum - 1][0]));
4709 strcat(parseList[moveNum - 1], " ");
4710 strcat(parseList[moveNum - 1], elapsed_time);
4711 moveList[moveNum - 1][0] = NULLCHAR;
4712 fromX = fromY = toX = toY = -1;
4715 if (appData.debugMode) {
4716 fprintf(debugFP, "Move parsed to '%s'\n", parseList[moveNum - 1]);
4717 setbuf(debugFP, NULL);
4721 /* Send move to chess program (BEFORE animating it). */
4722 if (appData.zippyPlay && !newGame && newMove &&
4723 (!appData.getMoveList || backwardMostMove == 0) && first.initDone) {
4725 if ((gameMode == IcsPlayingWhite && WhiteOnMove(moveNum)) ||
4726 (gameMode == IcsPlayingBlack && !WhiteOnMove(moveNum))) {
4727 if (moveList[moveNum - 1][0] == NULLCHAR) {
4728 snprintf(str, MSG_SIZ, _("Couldn't parse move \"%s\" from ICS"),
4730 DisplayError(str, 0);
4732 if (first.sendTime) {
4733 SendTimeRemaining(&first, gameMode == IcsPlayingWhite);
4735 bookHit = SendMoveToBookUser(moveNum - 1, &first, FALSE); // [HGM] book
4736 if (firstMove && !bookHit) {
4738 if (first.useColors) {
4739 SendToProgram(gameMode == IcsPlayingWhite ?
4741 "black\ngo\n", &first);
4743 SendToProgram("go\n", &first);
4745 first.maybeThinking = TRUE;
4748 } else if (gameMode == IcsObserving || gameMode == IcsExamining) {
4749 if (moveList[moveNum - 1][0] == NULLCHAR) {
4750 snprintf(str, MSG_SIZ, _("Couldn't parse move \"%s\" from ICS"), move_str);
4751 DisplayError(str, 0);
4753 if(gameInfo.variant == currentlyInitializedVariant) // [HGM] refrain sending moves engine can't understand!
4754 SendMoveToProgram(moveNum - 1, &first);
4761 if (moveNum > 0 && !gotPremove && !appData.noGUI) {
4762 /* If move comes from a remote source, animate it. If it
4763 isn't remote, it will have already been animated. */
4764 if (!pausing && !ics_user_moved && prevMove == moveNum - 1) {
4765 AnimateMove(boards[moveNum - 1], fromX, fromY, toX, toY);
4767 if (!pausing && appData.highlightLastMove) {
4768 SetHighlights(fromX, fromY, toX, toY);
4772 /* Start the clocks */
4773 whiteFlag = blackFlag = FALSE;
4774 appData.clockMode = !(basetime == 0 && increment == 0);
4776 ics_clock_paused = TRUE;
4778 } else if (ticking == 1) {
4779 ics_clock_paused = FALSE;
4781 if (gameMode == IcsIdle ||
4782 relation == RELATION_OBSERVING_STATIC ||
4783 relation == RELATION_EXAMINING ||
4785 DisplayBothClocks();
4789 /* Display opponents and material strengths */
4790 if (gameInfo.variant != VariantBughouse &&
4791 gameInfo.variant != VariantCrazyhouse && !appData.noGUI) {
4792 if (tinyLayout || smallLayout) {
4793 if(gameInfo.variant == VariantNormal)
4794 snprintf(str, MSG_SIZ, "%s(%d) %s(%d) {%d %d}",
4795 gameInfo.white, white_stren, gameInfo.black, black_stren,
4796 basetime, increment);
4798 snprintf(str, MSG_SIZ, "%s(%d) %s(%d) {%d %d w%d}",
4799 gameInfo.white, white_stren, gameInfo.black, black_stren,
4800 basetime, increment, (int) gameInfo.variant);
4802 if(gameInfo.variant == VariantNormal)
4803 snprintf(str, MSG_SIZ, "%s (%d) %s %s (%d) {%d %d}",
4804 gameInfo.white, white_stren, _("vs."), gameInfo.black, black_stren,
4805 basetime, increment);
4807 snprintf(str, MSG_SIZ, "%s (%d) %s %s (%d) {%d %d %s}",
4808 gameInfo.white, white_stren, _("vs."), gameInfo.black, black_stren,
4809 basetime, increment, VariantName(gameInfo.variant));
4812 if (appData.debugMode) {
4813 fprintf(debugFP, "Display title '%s, gameInfo.variant = %d'\n", str, gameInfo.variant);
4818 /* Display the board */
4819 if (!pausing && !appData.noGUI) {
4821 if (appData.premove)
4823 ((gameMode == IcsPlayingWhite) && (WhiteOnMove(currentMove))) ||
4824 ((gameMode == IcsPlayingBlack) && (!WhiteOnMove(currentMove))))
4825 ClearPremoveHighlights();
4827 j = seekGraphUp; seekGraphUp = FALSE; // [HGM] seekgraph: when we draw a board, it overwrites the seek graph
4828 if(partnerUp) { flipView = originalFlip; partnerUp = FALSE; j = TRUE; } // [HGM] bughouse: restore view
4829 DrawPosition(j, boards[currentMove]);
4831 DisplayMove(moveNum - 1);
4832 if (appData.ringBellAfterMoves && /*!ics_user_moved*/ // [HGM] use absolute method to recognize own move
4833 !((gameMode == IcsPlayingWhite) && (!WhiteOnMove(moveNum)) ||
4834 (gameMode == IcsPlayingBlack) && (WhiteOnMove(moveNum)) ) ) {
4835 if(newMove) RingBell(); else PlayIcsUnfinishedSound();
4839 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
4841 if(bookHit) { // [HGM] book: simulate book reply
4842 static char bookMove[MSG_SIZ]; // a bit generous?
4844 programStats.nodes = programStats.depth = programStats.time =
4845 programStats.score = programStats.got_only_move = 0;
4846 sprintf(programStats.movelist, "%s (xbook)", bookHit);
4848 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
4849 strcat(bookMove, bookHit);
4850 HandleMachineMove(bookMove, &first);
4859 if (appData.icsActive && gameMode != IcsIdle && ics_gamenum > 0) {
4860 ics_getting_history = H_REQUESTED;
4861 snprintf(buf, MSG_SIZ, "%smoves %d\n", ics_prefix, ics_gamenum);
4867 AnalysisPeriodicEvent (int force)
4869 if (((programStats.ok_to_send == 0 || programStats.line_is_book)
4870 && !force) || !appData.periodicUpdates)
4873 /* Send . command to Crafty to collect stats */
4874 SendToProgram(".\n", &first);
4876 /* Don't send another until we get a response (this makes
4877 us stop sending to old Crafty's which don't understand
4878 the "." command (sending illegal cmds resets node count & time,
4879 which looks bad)) */
4880 programStats.ok_to_send = 0;
4884 ics_update_width (int new_width)
4886 ics_printf("set width %d\n", new_width);
4890 SendMoveToProgram (int moveNum, ChessProgramState *cps)
4894 if(moveList[moveNum][1] == '@' && moveList[moveNum][0] == '@') {
4895 // null move in variant where engine does not understand it (for analysis purposes)
4896 SendBoard(cps, moveNum + 1); // send position after move in stead.
4899 if (cps->useUsermove) {
4900 SendToProgram("usermove ", cps);
4904 if ((space = strchr(parseList[moveNum], ' ')) != NULL) {
4905 int len = space - parseList[moveNum];
4906 memcpy(buf, parseList[moveNum], len);
4908 buf[len] = NULLCHAR;
4910 snprintf(buf, MSG_SIZ,"%s\n", parseList[moveNum]);
4912 SendToProgram(buf, cps);
4914 if(cps->alphaRank) { /* [HGM] shogi: temporarily convert to shogi coordinates before sending */
4915 AlphaRank(moveList[moveNum], 4);
4916 SendToProgram(moveList[moveNum], cps);
4917 AlphaRank(moveList[moveNum], 4); // and back
4919 /* Added by Tord: Send castle moves in "O-O" in FRC games if required by
4920 * the engine. It would be nice to have a better way to identify castle
4922 if((gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom)
4923 && cps->useOOCastle) {
4924 int fromX = moveList[moveNum][0] - AAA;
4925 int fromY = moveList[moveNum][1] - ONE;
4926 int toX = moveList[moveNum][2] - AAA;
4927 int toY = moveList[moveNum][3] - ONE;
4928 if((boards[moveNum][fromY][fromX] == WhiteKing
4929 && boards[moveNum][toY][toX] == WhiteRook)
4930 || (boards[moveNum][fromY][fromX] == BlackKing
4931 && boards[moveNum][toY][toX] == BlackRook)) {
4932 if(toX > fromX) SendToProgram("O-O\n", cps);
4933 else SendToProgram("O-O-O\n", cps);
4935 else SendToProgram(moveList[moveNum], cps);
4937 if(BOARD_HEIGHT > 10) { // [HGM] big: convert ranks to double-digit where needed
4938 if(moveList[moveNum][1] == '@' && (BOARD_HEIGHT < 16 || moveList[moveNum][0] <= 'Z')) { // drop move
4939 if(moveList[moveNum][0]== '@') snprintf(buf, MSG_SIZ, "@@@@\n"); else
4940 snprintf(buf, MSG_SIZ, "%c@%c%d%s", moveList[moveNum][0],
4941 moveList[moveNum][2], moveList[moveNum][3] - '0', moveList[moveNum]+4);
4943 snprintf(buf, MSG_SIZ, "%c%d%c%d%s", moveList[moveNum][0], moveList[moveNum][1] - '0',
4944 moveList[moveNum][2], moveList[moveNum][3] - '0', moveList[moveNum]+4);
4945 SendToProgram(buf, cps);
4947 else SendToProgram(moveList[moveNum], cps);
4948 /* End of additions by Tord */
4951 /* [HGM] setting up the opening has brought engine in force mode! */
4952 /* Send 'go' if we are in a mode where machine should play. */
4953 if( (moveNum == 0 && setboardSpoiledMachineBlack && cps == &first) &&
4954 (gameMode == TwoMachinesPlay ||
4956 gameMode == IcsPlayingBlack || gameMode == IcsPlayingWhite ||
4958 gameMode == MachinePlaysBlack || gameMode == MachinePlaysWhite) ) {
4959 SendToProgram("go\n", cps);
4960 if (appData.debugMode) {
4961 fprintf(debugFP, "(extra)\n");
4964 setboardSpoiledMachineBlack = 0;
4968 SendMoveToICS (ChessMove moveType, int fromX, int fromY, int toX, int toY, char promoChar)
4970 char user_move[MSG_SIZ];
4973 if(gameInfo.variant == VariantSChess && promoChar) {
4974 snprintf(suffix, 4, "=%c", toX == BOARD_WIDTH<<1 ? ToUpper(promoChar) : ToLower(promoChar));
4975 if(moveType == NormalMove) moveType = WhitePromotion; // kludge to do gating
4976 } else suffix[0] = NULLCHAR;
4980 snprintf(user_move, MSG_SIZ, _("say Internal error; bad moveType %d (%d,%d-%d,%d)"),
4981 (int)moveType, fromX, fromY, toX, toY);
4982 DisplayError(user_move + strlen("say "), 0);
4984 case WhiteKingSideCastle:
4985 case BlackKingSideCastle:
4986 case WhiteQueenSideCastleWild:
4987 case BlackQueenSideCastleWild:
4989 case WhiteHSideCastleFR:
4990 case BlackHSideCastleFR:
4992 snprintf(user_move, MSG_SIZ, "o-o%s\n", suffix);
4994 case WhiteQueenSideCastle:
4995 case BlackQueenSideCastle:
4996 case WhiteKingSideCastleWild:
4997 case BlackKingSideCastleWild:
4999 case WhiteASideCastleFR:
5000 case BlackASideCastleFR:
5002 snprintf(user_move, MSG_SIZ, "o-o-o%s\n",suffix);
5004 case WhiteNonPromotion:
5005 case BlackNonPromotion:
5006 sprintf(user_move, "%c%c%c%c==\n", AAA + fromX, ONE + fromY, AAA + toX, ONE + toY);
5008 case WhitePromotion:
5009 case BlackPromotion:
5010 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
5011 snprintf(user_move, MSG_SIZ, "%c%c%c%c=%c\n",
5012 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
5013 PieceToChar(WhiteFerz));
5014 else if(gameInfo.variant == VariantGreat)
5015 snprintf(user_move, MSG_SIZ,"%c%c%c%c=%c\n",
5016 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
5017 PieceToChar(WhiteMan));
5019 snprintf(user_move, MSG_SIZ, "%c%c%c%c=%c\n",
5020 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
5026 snprintf(user_move, MSG_SIZ, "%c@%c%c\n",
5027 ToUpper(PieceToChar((ChessSquare) fromX)),
5028 AAA + toX, ONE + toY);
5030 case IllegalMove: /* could be a variant we don't quite understand */
5031 if(fromY == DROP_RANK) goto drop; // We need 'IllegalDrop' move type?
5033 case WhiteCapturesEnPassant:
5034 case BlackCapturesEnPassant:
5035 snprintf(user_move, MSG_SIZ,"%c%c%c%c\n",
5036 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY);
5039 SendToICS(user_move);
5040 if(appData.keepAlive) // [HGM] alive: schedule sending of dummy 'date' command
5041 ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
5046 { // [HGM] upload: send entire stored game to ICS as long-algebraic moves.
5047 int i, last = forwardMostMove; // make sure ICS reply cannot pre-empt us by clearing fmm
5048 static char *castlingStrings[4] = { "none", "kside", "qside", "both" };
5049 if(gameMode == IcsObserving || gameMode == IcsPlayingBlack || gameMode == IcsPlayingWhite) {
5050 DisplayError(_("You cannot do this while you are playing or observing"), 0);
5053 if(gameMode != IcsExamining) { // is this ever not the case?
5054 char buf[MSG_SIZ], *p, *fen, command[MSG_SIZ], bsetup = 0;
5056 if(ics_type == ICS_ICC) { // on ICC match ourselves in applicable variant
5057 snprintf(command,MSG_SIZ, "match %s", ics_handle);
5058 } else { // on FICS we must first go to general examine mode
5059 safeStrCpy(command, "examine\nbsetup", sizeof(command)/sizeof(command[0])); // and specify variant within it with bsetups
5061 if(gameInfo.variant != VariantNormal) {
5062 // try figure out wild number, as xboard names are not always valid on ICS
5063 for(i=1; i<=36; i++) {
5064 snprintf(buf, MSG_SIZ, "wild/%d", i);
5065 if(StringToVariant(buf) == gameInfo.variant) break;
5067 if(i<=36 && ics_type == ICS_ICC) snprintf(buf, MSG_SIZ,"%s w%d\n", command, i);
5068 else if(i == 22) snprintf(buf,MSG_SIZ, "%s fr\n", command);
5069 else snprintf(buf, MSG_SIZ,"%s %s\n", command, VariantName(gameInfo.variant));
5070 } else snprintf(buf, MSG_SIZ,"%s\n", ics_type == ICS_ICC ? command : "examine\n"); // match yourself or examine
5071 SendToICS(ics_prefix);
5073 if(startedFromSetupPosition || backwardMostMove != 0) {
5074 fen = PositionToFEN(backwardMostMove, NULL);
5075 if(ics_type == ICS_ICC) { // on ICC we can simply send a complete FEN to set everything
5076 snprintf(buf, MSG_SIZ,"loadfen %s\n", fen);
5078 } else { // FICS: everything has to set by separate bsetup commands
5079 p = strchr(fen, ' '); p[0] = NULLCHAR; // cut after board
5080 snprintf(buf, MSG_SIZ,"bsetup fen %s\n", fen);
5082 if(!WhiteOnMove(backwardMostMove)) {
5083 SendToICS("bsetup tomove black\n");
5085 i = (strchr(p+3, 'K') != NULL) + 2*(strchr(p+3, 'Q') != NULL);
5086 snprintf(buf, MSG_SIZ,"bsetup wcastle %s\n", castlingStrings[i]);
5088 i = (strchr(p+3, 'k') != NULL) + 2*(strchr(p+3, 'q') != NULL);
5089 snprintf(buf, MSG_SIZ, "bsetup bcastle %s\n", castlingStrings[i]);
5091 i = boards[backwardMostMove][EP_STATUS];
5092 if(i >= 0) { // set e.p.
5093 snprintf(buf, MSG_SIZ,"bsetup eppos %c\n", i+AAA);
5099 if(bsetup || ics_type != ICS_ICC && gameInfo.variant != VariantNormal)
5100 SendToICS("bsetup done\n"); // switch to normal examining.
5102 for(i = backwardMostMove; i<last; i++) {
5104 snprintf(buf, sizeof(buf)/sizeof(buf[0]),"%s\n", parseList[i]);
5107 SendToICS(ics_prefix);
5108 SendToICS(ics_type == ICS_ICC ? "tag result Game in progress\n" : "commit\n");
5112 CoordsToComputerAlgebraic (int rf, int ff, int rt, int ft, char promoChar, char move[7])
5114 if (rf == DROP_RANK) {
5115 if(ff == EmptySquare) sprintf(move, "@@@@\n"); else // [HGM] pass
5116 sprintf(move, "%c@%c%c\n",
5117 ToUpper(PieceToChar((ChessSquare) ff)), AAA + ft, ONE + rt);
5119 if (promoChar == 'x' || promoChar == NULLCHAR) {
5120 sprintf(move, "%c%c%c%c\n",
5121 AAA + ff, ONE + rf, AAA + ft, ONE + rt);
5123 sprintf(move, "%c%c%c%c%c\n",
5124 AAA + ff, ONE + rf, AAA + ft, ONE + rt, promoChar);
5130 ProcessICSInitScript (FILE *f)
5134 while (fgets(buf, MSG_SIZ, f)) {
5135 SendToICSDelayed(buf,(long)appData.msLoginDelay);
5142 static int lastX, lastY, selectFlag, dragging;
5147 ChessSquare king = WhiteKing, pawn = WhitePawn, last = promoSweep;
5148 if(gameInfo.variant == VariantKnightmate) king = WhiteUnicorn;
5149 if(gameInfo.variant == VariantSuicide || gameInfo.variant == VariantGiveaway) king = EmptySquare;
5150 if(promoSweep >= BlackPawn) king = WHITE_TO_BLACK king, pawn = WHITE_TO_BLACK pawn;
5151 if(gameInfo.variant == VariantSpartan && pawn == BlackPawn) pawn = BlackLance, king = EmptySquare;
5152 if(fromY != BOARD_HEIGHT-2 && fromY != 1) pawn = EmptySquare;
5155 if(promoSweep == EmptySquare) promoSweep = BlackPawn; // wrap
5156 else if((int)promoSweep == -1) promoSweep = WhiteKing;
5157 else if(promoSweep == BlackPawn && step < 0) promoSweep = WhitePawn;
5158 else if(promoSweep == WhiteKing && step > 0) promoSweep = BlackKing;
5159 if(!step) step = -1;
5160 } while(PieceToChar(promoSweep) == '.' || PieceToChar(promoSweep) == '~' || promoSweep == pawn ||
5161 appData.testLegality && (promoSweep == king ||
5162 gameInfo.variant == VariantShogi && promoSweep != PROMOTED last && last != PROMOTED promoSweep && last != promoSweep));
5163 ChangeDragPiece(promoSweep);
5167 PromoScroll (int x, int y)
5171 if(promoSweep == EmptySquare || !appData.sweepSelect) return FALSE;
5172 if(abs(x - lastX) < 25 && abs(y - lastY) < 25) return FALSE;
5173 if( y > lastY + 2 ) step = -1; else if(y < lastY - 2) step = 1;
5174 if(!step) return FALSE;
5175 lastX = x; lastY = y;
5176 if((promoSweep < BlackPawn) == flipView) step = -step;
5177 if(step > 0) selectFlag = 1;
5178 if(!selectFlag) Sweep(step);
5183 NextPiece (int step)
5185 ChessSquare piece = boards[currentMove][toY][toX];
5188 if(pieceSweep == EmptySquare) pieceSweep = WhitePawn; // wrap
5189 if((int)pieceSweep == -1) pieceSweep = BlackKing;
5190 if(!step) step = -1;
5191 } while(PieceToChar(pieceSweep) == '.');
5192 boards[currentMove][toY][toX] = pieceSweep;
5193 DrawPosition(FALSE, boards[currentMove]);
5194 boards[currentMove][toY][toX] = piece;
5196 /* [HGM] Shogi move preprocessor: swap digits for letters, vice versa */
5198 AlphaRank (char *move, int n)
5200 // char *p = move, c; int x, y;
5202 if (appData.debugMode) {
5203 fprintf(debugFP, "alphaRank(%s,%d)\n", move, n);
5207 move[2]>='0' && move[2]<='9' &&
5208 move[3]>='a' && move[3]<='x' ) {
5210 move[2] = BOARD_RGHT -1 - (move[2]-'1') + AAA;
5211 move[3] = BOARD_HEIGHT-1 - (move[3]-'a') + ONE;
5213 if(move[0]>='0' && move[0]<='9' &&
5214 move[1]>='a' && move[1]<='x' &&
5215 move[2]>='0' && move[2]<='9' &&
5216 move[3]>='a' && move[3]<='x' ) {
5217 /* input move, Shogi -> normal */
5218 move[0] = BOARD_RGHT -1 - (move[0]-'1') + AAA;
5219 move[1] = BOARD_HEIGHT-1 - (move[1]-'a') + ONE;
5220 move[2] = BOARD_RGHT -1 - (move[2]-'1') + AAA;
5221 move[3] = BOARD_HEIGHT-1 - (move[3]-'a') + ONE;
5224 move[3]>='0' && move[3]<='9' &&
5225 move[2]>='a' && move[2]<='x' ) {
5227 move[2] = BOARD_RGHT - 1 - (move[2]-AAA) + '1';
5228 move[3] = BOARD_HEIGHT-1 - (move[3]-ONE) + 'a';
5231 move[0]>='a' && move[0]<='x' &&
5232 move[3]>='0' && move[3]<='9' &&
5233 move[2]>='a' && move[2]<='x' ) {
5234 /* output move, normal -> Shogi */
5235 move[0] = BOARD_RGHT - 1 - (move[0]-AAA) + '1';
5236 move[1] = BOARD_HEIGHT-1 - (move[1]-ONE) + 'a';
5237 move[2] = BOARD_RGHT - 1 - (move[2]-AAA) + '1';
5238 move[3] = BOARD_HEIGHT-1 - (move[3]-ONE) + 'a';
5239 if(move[4] == PieceToChar(BlackQueen)) move[4] = '+';
5241 if (appData.debugMode) {
5242 fprintf(debugFP, " out = '%s'\n", move);
5246 char yy_textstr[8000];
5248 /* Parser for moves from gnuchess, ICS, or user typein box */
5250 ParseOneMove (char *move, int moveNum, ChessMove *moveType, int *fromX, int *fromY, int *toX, int *toY, char *promoChar)
5252 *moveType = yylexstr(moveNum, move, yy_textstr, sizeof yy_textstr);
5254 switch (*moveType) {
5255 case WhitePromotion:
5256 case BlackPromotion:
5257 case WhiteNonPromotion:
5258 case BlackNonPromotion:
5260 case WhiteCapturesEnPassant:
5261 case BlackCapturesEnPassant:
5262 case WhiteKingSideCastle:
5263 case WhiteQueenSideCastle:
5264 case BlackKingSideCastle:
5265 case BlackQueenSideCastle:
5266 case WhiteKingSideCastleWild:
5267 case WhiteQueenSideCastleWild:
5268 case BlackKingSideCastleWild:
5269 case BlackQueenSideCastleWild:
5270 /* Code added by Tord: */
5271 case WhiteHSideCastleFR:
5272 case WhiteASideCastleFR:
5273 case BlackHSideCastleFR:
5274 case BlackASideCastleFR:
5275 /* End of code added by Tord */
5276 case IllegalMove: /* bug or odd chess variant */
5277 *fromX = currentMoveString[0] - AAA;
5278 *fromY = currentMoveString[1] - ONE;
5279 *toX = currentMoveString[2] - AAA;
5280 *toY = currentMoveString[3] - ONE;
5281 *promoChar = currentMoveString[4];
5282 if (*fromX < BOARD_LEFT || *fromX >= BOARD_RGHT || *fromY < 0 || *fromY >= BOARD_HEIGHT ||
5283 *toX < BOARD_LEFT || *toX >= BOARD_RGHT || *toY < 0 || *toY >= BOARD_HEIGHT) {
5284 if (appData.debugMode) {
5285 fprintf(debugFP, "Off-board move (%d,%d)-(%d,%d)%c, type = %d\n", *fromX, *fromY, *toX, *toY, *promoChar, *moveType);
5287 *fromX = *fromY = *toX = *toY = 0;
5290 if (appData.testLegality) {
5291 return (*moveType != IllegalMove);
5293 return !(*fromX == *toX && *fromY == *toY) && boards[moveNum][*fromY][*fromX] != EmptySquare &&
5294 WhiteOnMove(moveNum) == (boards[moveNum][*fromY][*fromX] < BlackPawn);
5299 *fromX = *moveType == WhiteDrop ?
5300 (int) CharToPiece(ToUpper(currentMoveString[0])) :
5301 (int) CharToPiece(ToLower(currentMoveString[0]));
5303 *toX = currentMoveString[2] - AAA;
5304 *toY = currentMoveString[3] - ONE;
5305 *promoChar = NULLCHAR;
5309 case ImpossibleMove:
5319 if (appData.debugMode) {
5320 fprintf(debugFP, "Impossible move %s, type = %d\n", currentMoveString, *moveType);
5323 *fromX = *fromY = *toX = *toY = 0;
5324 *promoChar = NULLCHAR;
5329 Boolean pushed = FALSE;
5330 char *lastParseAttempt;
5333 ParsePV (char *pv, Boolean storeComments, Boolean atEnd)
5334 { // Parse a string of PV moves, and append to current game, behind forwardMostMove
5335 int fromX, fromY, toX, toY; char promoChar;
5340 if (gameMode == AnalyzeMode && currentMove < forwardMostMove) {
5341 PushInner(currentMove, forwardMostMove); // [HGM] engine might not be thinking on forwardMost position!
5344 endPV = forwardMostMove;
5346 while(*pv == ' ' || *pv == '\n' || *pv == '\t') pv++; // must still read away whitespace
5347 if(nr == 0 && !storeComments && *pv == '(') pv++; // first (ponder) move can be in parentheses
5348 lastParseAttempt = pv;
5349 valid = ParseOneMove(pv, endPV, &moveType, &fromX, &fromY, &toX, &toY, &promoChar);
5350 if(!valid && nr == 0 &&
5351 ParseOneMove(pv, endPV-1, &moveType, &fromX, &fromY, &toX, &toY, &promoChar)){
5352 nr++; moveType = Comment; // First move has been played; kludge to make sure we continue
5353 // Hande case where played move is different from leading PV move
5354 CopyBoard(boards[endPV+1], boards[endPV-1]); // tentatively unplay last game move
5355 CopyBoard(boards[endPV+2], boards[endPV-1]); // and play first move of PV
5356 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[endPV+2]);
5357 if(!CompareBoards(boards[endPV], boards[endPV+2])) {
5358 endPV += 2; // if position different, keep this
5359 moveList[endPV-1][0] = fromX + AAA;
5360 moveList[endPV-1][1] = fromY + ONE;
5361 moveList[endPV-1][2] = toX + AAA;
5362 moveList[endPV-1][3] = toY + ONE;
5363 parseList[endPV-1][0] = NULLCHAR;
5364 safeStrCpy(moveList[endPV-2], "_0_0", sizeof(moveList[endPV-2])/sizeof(moveList[endPV-2][0])); // suppress premove highlight on takeback move
5367 pv = strstr(pv, yy_textstr) + strlen(yy_textstr); // skip what we parsed
5368 if(nr == 0 && !storeComments && *pv == ')') pv++; // closing parenthesis of ponder move;
5369 if(moveType == Comment && storeComments) AppendComment(endPV, yy_textstr, FALSE);
5370 if(moveType == Comment || moveType == NAG || moveType == ElapsedTime) {
5371 valid++; // allow comments in PV
5375 if(endPV+1 > framePtr) break; // no space, truncate
5378 CopyBoard(boards[endPV], boards[endPV-1]);
5379 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[endPV]);
5380 CoordsToComputerAlgebraic(fromY, fromX, toY, toX, promoChar, moveList[endPV - 1]);
5381 strncat(moveList[endPV-1], "\n", MOVE_LEN);
5382 CoordsToAlgebraic(boards[endPV - 1],
5383 PosFlags(endPV - 1),
5384 fromY, fromX, toY, toX, promoChar,
5385 parseList[endPV - 1]);
5387 if(atEnd == 2) return; // used hidden, for PV conversion
5388 currentMove = (atEnd || endPV == forwardMostMove) ? endPV : forwardMostMove + 1;
5389 if(currentMove == forwardMostMove) ClearPremoveHighlights(); else
5390 SetPremoveHighlights(moveList[currentMove-1][0]-AAA, moveList[currentMove-1][1]-ONE,
5391 moveList[currentMove-1][2]-AAA, moveList[currentMove-1][3]-ONE);
5392 DrawPosition(TRUE, boards[currentMove]);
5396 MultiPV (ChessProgramState *cps)
5397 { // check if engine supports MultiPV, and if so, return the number of the option that sets it
5399 for(i=0; i<cps->nrOptions; i++)
5400 if(!strcmp(cps->option[i].name, "MultiPV") && cps->option[i].type == Spin)
5406 LoadMultiPV (int x, int y, char *buf, int index, int *start, int *end)
5408 int startPV, multi, lineStart, origIndex = index;
5409 char *p, buf2[MSG_SIZ];
5411 if(index < 0 || index >= strlen(buf)) return FALSE; // sanity
5412 lastX = x; lastY = y;
5413 while(index > 0 && buf[index-1] != '\n') index--; // beginning of line
5414 lineStart = startPV = index;
5415 while(buf[index] != '\n') if(buf[index++] == '\t') startPV = index;
5416 if(index == startPV && (p = StrCaseStr(buf+index, "PV="))) startPV = p - buf + 3;
5418 do{ while(buf[index] && buf[index] != '\n') index++;
5419 } while(buf[index] == '\n' && buf[index+1] == '\\' && buf[index+2] == ' ' && index++); // join kibitzed PV continuation line
5421 if(lineStart == 0 && gameMode == AnalyzeMode && (multi = MultiPV(&first)) >= 0) {
5422 int n = first.option[multi].value;
5423 if(origIndex > 17 && origIndex < 24) { if(n>1) n--; } else if(origIndex > index - 6) n++;
5424 snprintf(buf2, MSG_SIZ, "option MultiPV=%d\n", n);
5425 if(first.option[multi].value != n) SendToProgram(buf2, &first);
5426 first.option[multi].value = n;
5429 } else if(strstr(buf+lineStart, "exclude:") == buf+lineStart) { // exclude moves clicked
5430 DisplayNote("Yes!");
5433 ParsePV(buf+startPV, FALSE, gameMode != AnalyzeMode);
5434 *start = startPV; *end = index-1;
5441 static char buf[10*MSG_SIZ];
5442 int i, k=0, savedEnd=endPV, saveFMM = forwardMostMove;
5444 if(forwardMostMove < endPV) PushInner(forwardMostMove, endPV);
5445 ParsePV(pv, FALSE, 2); // this appends PV to game, suppressing any display of it
5446 for(i = forwardMostMove; i<endPV; i++){
5447 if(i&1) snprintf(buf+k, 10*MSG_SIZ-k, "%s ", parseList[i]);
5448 else snprintf(buf+k, 10*MSG_SIZ-k, "%d. %s ", i/2 + 1, parseList[i]);
5451 snprintf(buf+k, 10*MSG_SIZ-k, "%s", lastParseAttempt); // if we ran into stuff that could not be parsed, print it verbatim
5452 if(forwardMostMove < savedEnd) { PopInner(0); forwardMostMove = saveFMM; } // PopInner would set fmm to endPV!
5458 LoadPV (int x, int y)
5459 { // called on right mouse click to load PV
5460 int which = gameMode == TwoMachinesPlay && (WhiteOnMove(forwardMostMove) == (second.twoMachinesColor[0] == 'w'));
5461 lastX = x; lastY = y;
5462 ParsePV(lastPV[which], FALSE, TRUE); // load the PV of the thinking engine in the boards array.
5469 int oldFMM = forwardMostMove; // N.B.: this was currentMove before PV was loaded!
5470 if(endPV < 0) return;
5471 if(appData.autoCopyPV) CopyFENToClipboard();
5473 if(gameMode == AnalyzeMode && currentMove > forwardMostMove) {
5474 Boolean saveAnimate = appData.animate;
5476 if(shiftKey && storedGames < MAX_VARIATIONS-2) { // wants to start variation, and there is space
5477 if(storedGames == 1) GreyRevert(FALSE); // we already pushed the tail, so just make it official
5478 } else storedGames--; // abandon shelved tail of original game
5481 forwardMostMove = currentMove;
5482 currentMove = oldFMM;
5483 appData.animate = FALSE;
5484 ToNrEvent(forwardMostMove);
5485 appData.animate = saveAnimate;
5487 currentMove = forwardMostMove;
5488 if(pushed) { PopInner(0); pushed = FALSE; } // restore shelved game continuation
5489 ClearPremoveHighlights();
5490 DrawPosition(TRUE, boards[currentMove]);
5494 MovePV (int x, int y, int h)
5495 { // step through PV based on mouse coordinates (called on mouse move)
5496 int margin = h>>3, step = 0, threshold = (pieceSweep == EmptySquare ? 10 : 15);
5498 // we must somehow check if right button is still down (might be released off board!)
5499 if(endPV < 0 && pieceSweep == EmptySquare) return; // needed in XBoard because lastX/Y is shared :-(
5500 if(abs(x - lastX) < threshold && abs(y - lastY) < threshold) return;
5501 if( y > lastY + 2 ) step = -1; else if(y < lastY - 2) step = 1;
5503 lastX = x; lastY = y;
5505 if(pieceSweep != EmptySquare) { NextPiece(step); return; }
5506 if(endPV < 0) return;
5507 if(y < margin) step = 1; else
5508 if(y > h - margin) step = -1;
5509 if(currentMove + step > endPV || currentMove + step < forwardMostMove) step = 0;
5510 currentMove += step;
5511 if(currentMove == forwardMostMove) ClearPremoveHighlights(); else
5512 SetPremoveHighlights(moveList[currentMove-1][0]-AAA, moveList[currentMove-1][1]-ONE,
5513 moveList[currentMove-1][2]-AAA, moveList[currentMove-1][3]-ONE);
5514 DrawPosition(FALSE, boards[currentMove]);
5518 // [HGM] shuffle: a general way to suffle opening setups, applicable to arbitrary variants.
5519 // All positions will have equal probability, but the current method will not provide a unique
5520 // numbering scheme for arrays that contain 3 or more pieces of the same kind.
5526 int piecesLeft[(int)BlackPawn];
5527 int seed, nrOfShuffles;
5530 GetPositionNumber ()
5531 { // sets global variable seed
5534 seed = appData.defaultFrcPosition;
5535 if(seed < 0) { // randomize based on time for negative FRC position numbers
5536 for(i=0; i<50; i++) seed += random();
5537 seed = random() ^ random() >> 8 ^ random() << 8;
5538 if(seed<0) seed = -seed;
5543 put (Board board, int pieceType, int rank, int n, int shade)
5544 // put the piece on the (n-1)-th empty squares of the given shade
5548 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) {
5549 if( (((i-BOARD_LEFT)&1)+1) & shade && board[rank][i] == EmptySquare && n-- == 0) {
5550 board[rank][i] = (ChessSquare) pieceType;
5551 squaresLeft[((i-BOARD_LEFT)&1) + 1]--;
5553 piecesLeft[pieceType]--;
5562 AddOnePiece (Board board, int pieceType, int rank, int shade)
5563 // calculate where the next piece goes, (any empty square), and put it there
5567 i = seed % squaresLeft[shade];
5568 nrOfShuffles *= squaresLeft[shade];
5569 seed /= squaresLeft[shade];
5570 put(board, pieceType, rank, i, shade);
5574 AddTwoPieces (Board board, int pieceType, int rank)
5575 // calculate where the next 2 identical pieces go, (any empty square), and put it there
5577 int i, n=squaresLeft[ANY], j=n-1, k;
5579 k = n*(n-1)/2; // nr of possibilities, not counting permutations
5580 i = seed % k; // pick one
5583 while(i >= j) i -= j--;
5584 j = n - 1 - j; i += j;
5585 put(board, pieceType, rank, j, ANY);
5586 put(board, pieceType, rank, i, ANY);
5590 SetUpShuffle (Board board, int number)
5594 GetPositionNumber(); nrOfShuffles = 1;
5596 squaresLeft[DARK] = (BOARD_RGHT - BOARD_LEFT + 1)/2;
5597 squaresLeft[ANY] = BOARD_RGHT - BOARD_LEFT;
5598 squaresLeft[LITE] = squaresLeft[ANY] - squaresLeft[DARK];
5600 for(p = 0; p<=(int)WhiteKing; p++) piecesLeft[p] = 0;
5602 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // count pieces and clear board
5603 p = (int) board[0][i];
5604 if(p < (int) BlackPawn) piecesLeft[p] ++;
5605 board[0][i] = EmptySquare;
5608 if(PosFlags(0) & F_ALL_CASTLE_OK) {
5609 // shuffles restricted to allow normal castling put KRR first
5610 if(piecesLeft[(int)WhiteKing]) // King goes rightish of middle
5611 put(board, WhiteKing, 0, (gameInfo.boardWidth+1)/2, ANY);
5612 else if(piecesLeft[(int)WhiteUnicorn]) // in Knightmate Unicorn castles
5613 put(board, WhiteUnicorn, 0, (gameInfo.boardWidth+1)/2, ANY);
5614 if(piecesLeft[(int)WhiteRook]) // First supply a Rook for K-side castling
5615 put(board, WhiteRook, 0, gameInfo.boardWidth-2, ANY);
5616 if(piecesLeft[(int)WhiteRook]) // Then supply a Rook for Q-side castling
5617 put(board, WhiteRook, 0, 0, ANY);
5618 // in variants with super-numerary Kings and Rooks, we leave these for the shuffle
5621 if(((BOARD_RGHT-BOARD_LEFT) & 1) == 0)
5622 // only for even boards make effort to put pairs of colorbound pieces on opposite colors
5623 for(p = (int) WhiteKing; p > (int) WhitePawn; p--) {
5624 if(p != (int) WhiteBishop && p != (int) WhiteFerz && p != (int) WhiteAlfil) continue;
5625 while(piecesLeft[p] >= 2) {
5626 AddOnePiece(board, p, 0, LITE);
5627 AddOnePiece(board, p, 0, DARK);
5629 // Odd color-bound pieces are shuffled with the rest (to not run out of paired squares)
5632 for(p = (int) WhiteKing - 2; p > (int) WhitePawn; p--) {
5633 // Remaining pieces (non-colorbound, or odd color bound) can be put anywhere
5634 // but we leave King and Rooks for last, to possibly obey FRC restriction
5635 if(p == (int)WhiteRook) continue;
5636 while(piecesLeft[p] >= 2) AddTwoPieces(board, p, 0); // add in pairs, for not counting permutations
5637 if(piecesLeft[p]) AddOnePiece(board, p, 0, ANY); // add the odd piece
5640 // now everything is placed, except perhaps King (Unicorn) and Rooks
5642 if(PosFlags(0) & F_FRC_TYPE_CASTLING) {
5643 // Last King gets castling rights
5644 while(piecesLeft[(int)WhiteUnicorn]) {
5645 i = put(board, WhiteUnicorn, 0, piecesLeft[(int)WhiteRook]/2, ANY);
5646 initialRights[2] = initialRights[5] = board[CASTLING][2] = board[CASTLING][5] = i;
5649 while(piecesLeft[(int)WhiteKing]) {
5650 i = put(board, WhiteKing, 0, piecesLeft[(int)WhiteRook]/2, ANY);
5651 initialRights[2] = initialRights[5] = board[CASTLING][2] = board[CASTLING][5] = i;
5656 while(piecesLeft[(int)WhiteKing]) AddOnePiece(board, WhiteKing, 0, ANY);
5657 while(piecesLeft[(int)WhiteUnicorn]) AddOnePiece(board, WhiteUnicorn, 0, ANY);
5660 // Only Rooks can be left; simply place them all
5661 while(piecesLeft[(int)WhiteRook]) {
5662 i = put(board, WhiteRook, 0, 0, ANY);
5663 if(PosFlags(0) & F_FRC_TYPE_CASTLING) { // first and last Rook get FRC castling rights
5666 initialRights[1] = initialRights[4] = board[CASTLING][1] = board[CASTLING][4] = i;
5668 initialRights[0] = initialRights[3] = board[CASTLING][0] = board[CASTLING][3] = i;
5671 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // copy black from white
5672 board[BOARD_HEIGHT-1][i] = (int) board[0][i] < BlackPawn ? WHITE_TO_BLACK board[0][i] : EmptySquare;
5675 if(number >= 0) appData.defaultFrcPosition %= nrOfShuffles; // normalize
5679 SetCharTable (char *table, const char * map)
5680 /* [HGM] moved here from winboard.c because of its general usefulness */
5681 /* Basically a safe strcpy that uses the last character as King */
5683 int result = FALSE; int NrPieces;
5685 if( map != NULL && (NrPieces=strlen(map)) <= (int) EmptySquare
5686 && NrPieces >= 12 && !(NrPieces&1)) {
5687 int i; /* [HGM] Accept even length from 12 to 34 */
5689 for( i=0; i<(int) EmptySquare; i++ ) table[i] = '.';
5690 for( i=0; i<NrPieces/2-1; i++ ) {
5692 table[i + (int)BlackPawn - (int) WhitePawn] = map[i+NrPieces/2];
5694 table[(int) WhiteKing] = map[NrPieces/2-1];
5695 table[(int) BlackKing] = map[NrPieces-1];
5704 Prelude (Board board)
5705 { // [HGM] superchess: random selection of exo-pieces
5706 int i, j, k; ChessSquare p;
5707 static ChessSquare exoPieces[4] = { WhiteAngel, WhiteMarshall, WhiteSilver, WhiteLance };
5709 GetPositionNumber(); // use FRC position number
5711 if(appData.pieceToCharTable != NULL) { // select pieces to participate from given char table
5712 SetCharTable(pieceToChar, appData.pieceToCharTable);
5713 for(i=(int)WhiteQueen+1, j=0; i<(int)WhiteKing && j<4; i++)
5714 if(PieceToChar((ChessSquare)i) != '.') exoPieces[j++] = (ChessSquare) i;
5717 j = seed%4; seed /= 4;
5718 p = board[0][BOARD_LEFT+j]; board[0][BOARD_LEFT+j] = EmptySquare; k = PieceToNumber(p);
5719 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5720 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5721 j = seed%3 + (seed%3 >= j); seed /= 3;
5722 p = board[0][BOARD_LEFT+j]; board[0][BOARD_LEFT+j] = EmptySquare; k = PieceToNumber(p);
5723 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5724 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5725 j = seed%3; seed /= 3;
5726 p = board[0][BOARD_LEFT+j+5]; board[0][BOARD_LEFT+j+5] = EmptySquare; k = PieceToNumber(p);
5727 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5728 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5729 j = seed%2 + (seed%2 >= j); seed /= 2;
5730 p = board[0][BOARD_LEFT+j+5]; board[0][BOARD_LEFT+j+5] = EmptySquare; k = PieceToNumber(p);
5731 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5732 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5733 j = seed%4; seed /= 4; put(board, exoPieces[3], 0, j, ANY);
5734 j = seed%3; seed /= 3; put(board, exoPieces[2], 0, j, ANY);
5735 j = seed%2; seed /= 2; put(board, exoPieces[1], 0, j, ANY);
5736 put(board, exoPieces[0], 0, 0, ANY);
5737 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) board[BOARD_HEIGHT-1][i] = WHITE_TO_BLACK board[0][i];
5741 InitPosition (int redraw)
5743 ChessSquare (* pieces)[BOARD_FILES];
5744 int i, j, pawnRow, overrule,
5745 oldx = gameInfo.boardWidth,
5746 oldy = gameInfo.boardHeight,
5747 oldh = gameInfo.holdingsWidth;
5750 if(appData.icsActive) shuffleOpenings = FALSE; // [HGM] shuffle: in ICS mode, only shuffle on ICS request
5752 /* [AS] Initialize pv info list [HGM] and game status */
5754 for( i=0; i<=framePtr; i++ ) { // [HGM] vari: spare saved variations
5755 pvInfoList[i].depth = 0;
5756 boards[i][EP_STATUS] = EP_NONE;
5757 for( j=0; j<BOARD_FILES-2; j++ ) boards[i][CASTLING][j] = NoRights;
5760 initialRulePlies = 0; /* 50-move counter start */
5762 castlingRank[0] = castlingRank[1] = castlingRank[2] = 0;
5763 castlingRank[3] = castlingRank[4] = castlingRank[5] = BOARD_HEIGHT-1;
5767 /* [HGM] logic here is completely changed. In stead of full positions */
5768 /* the initialized data only consist of the two backranks. The switch */
5769 /* selects which one we will use, which is than copied to the Board */
5770 /* initialPosition, which for the rest is initialized by Pawns and */
5771 /* empty squares. This initial position is then copied to boards[0], */
5772 /* possibly after shuffling, so that it remains available. */
5774 gameInfo.holdingsWidth = 0; /* default board sizes */
5775 gameInfo.boardWidth = 8;
5776 gameInfo.boardHeight = 8;
5777 gameInfo.holdingsSize = 0;
5778 nrCastlingRights = -1; /* [HGM] Kludge to indicate default should be used */
5779 for(i=0; i<BOARD_FILES-2; i++)
5780 initialPosition[CASTLING][i] = initialRights[i] = NoRights; /* but no rights yet */
5781 initialPosition[EP_STATUS] = EP_NONE;
5782 SetCharTable(pieceToChar, "PNBRQ...........Kpnbrq...........k");
5783 if(startVariant == gameInfo.variant) // [HGM] nicks: enable nicknames in original variant
5784 SetCharTable(pieceNickName, appData.pieceNickNames);
5785 else SetCharTable(pieceNickName, "............");
5788 switch (gameInfo.variant) {
5789 case VariantFischeRandom:
5790 shuffleOpenings = TRUE;
5793 case VariantShatranj:
5794 pieces = ShatranjArray;
5795 nrCastlingRights = 0;
5796 SetCharTable(pieceToChar, "PN.R.QB...Kpn.r.qb...k");
5799 pieces = makrukArray;
5800 nrCastlingRights = 0;
5801 startedFromSetupPosition = TRUE;
5802 SetCharTable(pieceToChar, "PN.R.M....SKpn.r.m....sk");
5804 case VariantTwoKings:
5805 pieces = twoKingsArray;
5808 pieces = GrandArray;
5809 nrCastlingRights = 0;
5810 SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
5811 gameInfo.boardWidth = 10;
5812 gameInfo.boardHeight = 10;
5813 gameInfo.holdingsSize = 7;
5815 case VariantCapaRandom:
5816 shuffleOpenings = TRUE;
5817 case VariantCapablanca:
5818 pieces = CapablancaArray;
5819 gameInfo.boardWidth = 10;
5820 SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
5823 pieces = GothicArray;
5824 gameInfo.boardWidth = 10;
5825 SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
5828 SetCharTable(pieceToChar, "PNBRQ..HEKpnbrq..hek");
5829 gameInfo.holdingsSize = 7;
5832 pieces = JanusArray;
5833 gameInfo.boardWidth = 10;
5834 SetCharTable(pieceToChar, "PNBRQ..JKpnbrq..jk");
5835 nrCastlingRights = 6;
5836 initialPosition[CASTLING][0] = initialRights[0] = BOARD_RGHT-1;
5837 initialPosition[CASTLING][1] = initialRights[1] = BOARD_LEFT;
5838 initialPosition[CASTLING][2] = initialRights[2] =(BOARD_WIDTH-1)>>1;
5839 initialPosition[CASTLING][3] = initialRights[3] = BOARD_RGHT-1;
5840 initialPosition[CASTLING][4] = initialRights[4] = BOARD_LEFT;
5841 initialPosition[CASTLING][5] = initialRights[5] =(BOARD_WIDTH-1)>>1;
5844 pieces = FalconArray;
5845 gameInfo.boardWidth = 10;
5846 SetCharTable(pieceToChar, "PNBRQ.............FKpnbrq.............fk");
5848 case VariantXiangqi:
5849 pieces = XiangqiArray;
5850 gameInfo.boardWidth = 9;
5851 gameInfo.boardHeight = 10;
5852 nrCastlingRights = 0;
5853 SetCharTable(pieceToChar, "PH.R.AE..K.C.ph.r.ae..k.c.");
5856 pieces = ShogiArray;
5857 gameInfo.boardWidth = 9;
5858 gameInfo.boardHeight = 9;
5859 gameInfo.holdingsSize = 7;
5860 nrCastlingRights = 0;
5861 SetCharTable(pieceToChar, "PNBRLS...G.++++++Kpnbrls...g.++++++k");
5863 case VariantCourier:
5864 pieces = CourierArray;
5865 gameInfo.boardWidth = 12;
5866 nrCastlingRights = 0;
5867 SetCharTable(pieceToChar, "PNBR.FE..WMKpnbr.fe..wmk");
5869 case VariantKnightmate:
5870 pieces = KnightmateArray;
5871 SetCharTable(pieceToChar, "P.BRQ.....M.........K.p.brq.....m.........k.");
5873 case VariantSpartan:
5874 pieces = SpartanArray;
5875 SetCharTable(pieceToChar, "PNBRQ................K......lwg.....c...h..k");
5878 pieces = fairyArray;
5879 SetCharTable(pieceToChar, "PNBRQFEACWMOHIJGDVLSUKpnbrqfeacwmohijgdvlsuk");
5882 pieces = GreatArray;
5883 gameInfo.boardWidth = 10;
5884 SetCharTable(pieceToChar, "PN....E...S..HWGMKpn....e...s..hwgmk");
5885 gameInfo.holdingsSize = 8;
5889 SetCharTable(pieceToChar, "PNBRQ..SE.......V.AKpnbrq..se.......v.ak");
5890 gameInfo.holdingsSize = 8;
5891 startedFromSetupPosition = TRUE;
5893 case VariantCrazyhouse:
5894 case VariantBughouse:
5896 SetCharTable(pieceToChar, "PNBRQ.......~~~~Kpnbrq.......~~~~k");
5897 gameInfo.holdingsSize = 5;
5899 case VariantWildCastle:
5901 /* !!?shuffle with kings guaranteed to be on d or e file */
5902 shuffleOpenings = 1;
5904 case VariantNoCastle:
5906 nrCastlingRights = 0;
5907 /* !!?unconstrained back-rank shuffle */
5908 shuffleOpenings = 1;
5913 if(appData.NrFiles >= 0) {
5914 if(gameInfo.boardWidth != appData.NrFiles) overrule++;
5915 gameInfo.boardWidth = appData.NrFiles;
5917 if(appData.NrRanks >= 0) {
5918 gameInfo.boardHeight = appData.NrRanks;
5920 if(appData.holdingsSize >= 0) {
5921 i = appData.holdingsSize;
5922 if(i > gameInfo.boardHeight) i = gameInfo.boardHeight;
5923 gameInfo.holdingsSize = i;
5925 if(gameInfo.holdingsSize) gameInfo.holdingsWidth = 2;
5926 if(BOARD_HEIGHT > BOARD_RANKS || BOARD_WIDTH > BOARD_FILES)
5927 DisplayFatalError(_("Recompile to support this BOARD_RANKS or BOARD_FILES!"), 0, 2);
5929 pawnRow = gameInfo.boardHeight - 7; /* seems to work in all common variants */
5930 if(pawnRow < 1) pawnRow = 1;
5931 if(gameInfo.variant == VariantMakruk || gameInfo.variant == VariantGrand) pawnRow = 2;
5933 /* User pieceToChar list overrules defaults */
5934 if(appData.pieceToCharTable != NULL)
5935 SetCharTable(pieceToChar, appData.pieceToCharTable);
5937 for( j=0; j<BOARD_WIDTH; j++ ) { ChessSquare s = EmptySquare;
5939 if(j==BOARD_LEFT-1 || j==BOARD_RGHT)
5940 s = (ChessSquare) 0; /* account holding counts in guard band */
5941 for( i=0; i<BOARD_HEIGHT; i++ )
5942 initialPosition[i][j] = s;
5944 if(j < BOARD_LEFT || j >= BOARD_RGHT || overrule) continue;
5945 initialPosition[gameInfo.variant == VariantGrand][j] = pieces[0][j-gameInfo.holdingsWidth];
5946 initialPosition[pawnRow][j] = WhitePawn;
5947 initialPosition[BOARD_HEIGHT-pawnRow-1][j] = gameInfo.variant == VariantSpartan ? BlackLance : BlackPawn;
5948 if(gameInfo.variant == VariantXiangqi) {
5950 initialPosition[pawnRow][j] =
5951 initialPosition[BOARD_HEIGHT-pawnRow-1][j] = EmptySquare;
5952 if(j==BOARD_LEFT+1 || j>=BOARD_RGHT-2) {
5953 initialPosition[2][j] = WhiteCannon;
5954 initialPosition[BOARD_HEIGHT-3][j] = BlackCannon;
5958 if(gameInfo.variant == VariantGrand) {
5959 if(j==BOARD_LEFT || j>=BOARD_RGHT-1) {
5960 initialPosition[0][j] = WhiteRook;
5961 initialPosition[BOARD_HEIGHT-1][j] = BlackRook;
5964 initialPosition[BOARD_HEIGHT-1-(gameInfo.variant == VariantGrand)][j] = pieces[1][j-gameInfo.holdingsWidth];
5966 if( (gameInfo.variant == VariantShogi) && !overrule ) {
5969 initialPosition[1][j] = WhiteBishop;
5970 initialPosition[BOARD_HEIGHT-2][j] = BlackRook;
5972 initialPosition[1][j] = WhiteRook;
5973 initialPosition[BOARD_HEIGHT-2][j] = BlackBishop;
5976 if( nrCastlingRights == -1) {
5977 /* [HGM] Build normal castling rights (must be done after board sizing!) */
5978 /* This sets default castling rights from none to normal corners */
5979 /* Variants with other castling rights must set them themselves above */
5980 nrCastlingRights = 6;
5982 initialPosition[CASTLING][0] = initialRights[0] = BOARD_RGHT-1;
5983 initialPosition[CASTLING][1] = initialRights[1] = BOARD_LEFT;
5984 initialPosition[CASTLING][2] = initialRights[2] = BOARD_WIDTH>>1;
5985 initialPosition[CASTLING][3] = initialRights[3] = BOARD_RGHT-1;
5986 initialPosition[CASTLING][4] = initialRights[4] = BOARD_LEFT;
5987 initialPosition[CASTLING][5] = initialRights[5] = BOARD_WIDTH>>1;
5990 if(gameInfo.variant == VariantSuper) Prelude(initialPosition);
5991 if(gameInfo.variant == VariantGreat) { // promotion commoners
5992 initialPosition[PieceToNumber(WhiteMan)][BOARD_WIDTH-1] = WhiteMan;
5993 initialPosition[PieceToNumber(WhiteMan)][BOARD_WIDTH-2] = 9;
5994 initialPosition[BOARD_HEIGHT-1-PieceToNumber(WhiteMan)][0] = BlackMan;
5995 initialPosition[BOARD_HEIGHT-1-PieceToNumber(WhiteMan)][1] = 9;
5997 if( gameInfo.variant == VariantSChess ) {
5998 initialPosition[1][0] = BlackMarshall;
5999 initialPosition[2][0] = BlackAngel;
6000 initialPosition[6][BOARD_WIDTH-1] = WhiteMarshall;
6001 initialPosition[5][BOARD_WIDTH-1] = WhiteAngel;
6002 initialPosition[1][1] = initialPosition[2][1] =
6003 initialPosition[6][BOARD_WIDTH-2] = initialPosition[5][BOARD_WIDTH-2] = 1;
6005 if (appData.debugMode) {
6006 fprintf(debugFP, "shuffleOpenings = %d\n", shuffleOpenings);
6008 if(shuffleOpenings) {
6009 SetUpShuffle(initialPosition, appData.defaultFrcPosition);
6010 startedFromSetupPosition = TRUE;
6012 if(startedFromPositionFile) {
6013 /* [HGM] loadPos: use PositionFile for every new game */
6014 CopyBoard(initialPosition, filePosition);
6015 for(i=0; i<nrCastlingRights; i++)
6016 initialRights[i] = filePosition[CASTLING][i];
6017 startedFromSetupPosition = TRUE;
6020 CopyBoard(boards[0], initialPosition);
6022 if(oldx != gameInfo.boardWidth ||
6023 oldy != gameInfo.boardHeight ||
6024 oldv != gameInfo.variant ||
6025 oldh != gameInfo.holdingsWidth
6027 InitDrawingSizes(-2 ,0);
6029 oldv = gameInfo.variant;
6031 DrawPosition(TRUE, boards[currentMove]);
6035 SendBoard (ChessProgramState *cps, int moveNum)
6037 char message[MSG_SIZ];
6039 if (cps->useSetboard) {
6040 char* fen = PositionToFEN(moveNum, cps->fenOverride);
6041 snprintf(message, MSG_SIZ,"setboard %s\n", fen);
6042 SendToProgram(message, cps);
6047 int i, j, left=0, right=BOARD_WIDTH;
6048 /* Kludge to set black to move, avoiding the troublesome and now
6049 * deprecated "black" command.
6051 if (!WhiteOnMove(moveNum)) // [HGM] but better a deprecated command than an illegal move...
6052 SendToProgram(boards[0][1][BOARD_LEFT] == WhitePawn ? "a2a3\n" : "black\n", cps);
6054 if(!cps->extendedEdit) left = BOARD_LEFT, right = BOARD_RGHT; // only board proper
6056 SendToProgram("edit\n", cps);
6057 SendToProgram("#\n", cps);
6058 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
6059 bp = &boards[moveNum][i][left];
6060 for (j = left; j < right; j++, bp++) {
6061 if(j == BOARD_LEFT-1 || j == BOARD_RGHT) continue;
6062 if ((int) *bp < (int) BlackPawn) {
6063 if(j == BOARD_RGHT+1)
6064 snprintf(message, MSG_SIZ, "%c@%d\n", PieceToChar(*bp), bp[-1]);
6065 else snprintf(message, MSG_SIZ, "%c%c%c\n", PieceToChar(*bp), AAA + j, ONE + i);
6066 if(message[0] == '+' || message[0] == '~') {
6067 snprintf(message, MSG_SIZ,"%c%c%c+\n",
6068 PieceToChar((ChessSquare)(DEMOTED *bp)),
6071 if(cps->alphaRank) { /* [HGM] shogi: translate coords */
6072 message[1] = BOARD_RGHT - 1 - j + '1';
6073 message[2] = BOARD_HEIGHT - 1 - i + 'a';
6075 SendToProgram(message, cps);
6080 SendToProgram("c\n", cps);
6081 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
6082 bp = &boards[moveNum][i][left];
6083 for (j = left; j < right; j++, bp++) {
6084 if(j == BOARD_LEFT-1 || j == BOARD_RGHT) continue;
6085 if (((int) *bp != (int) EmptySquare)
6086 && ((int) *bp >= (int) BlackPawn)) {
6087 if(j == BOARD_LEFT-2)
6088 snprintf(message, MSG_SIZ, "%c@%d\n", ToUpper(PieceToChar(*bp)), bp[1]);
6089 else snprintf(message,MSG_SIZ, "%c%c%c\n", ToUpper(PieceToChar(*bp)),
6091 if(message[0] == '+' || message[0] == '~') {
6092 snprintf(message, MSG_SIZ,"%c%c%c+\n",
6093 PieceToChar((ChessSquare)(DEMOTED *bp)),
6096 if(cps->alphaRank) { /* [HGM] shogi: translate coords */
6097 message[1] = BOARD_RGHT - 1 - j + '1';
6098 message[2] = BOARD_HEIGHT - 1 - i + 'a';
6100 SendToProgram(message, cps);
6105 SendToProgram(".\n", cps);
6107 setboardSpoiledMachineBlack = 0; /* [HGM] assume WB 4.2.7 already solves this after sending setboard */
6110 char exclusionHeader[MSG_SIZ];
6111 int exCnt, excludePtr, mappedMove = -1;
6112 typedef struct { int ff, fr, tf, tr, pc, mark; } Exclusion;
6113 static Exclusion excluTab[200];
6114 static char excludeMap[(BOARD_RANKS*BOARD_FILES*BOARD_RANKS*BOARD_FILES+7)/8]; // [HGM] exclude: bitmap for excluced moves
6121 for(j=0; j<(BOARD_RANKS*BOARD_FILES*BOARD_RANKS*BOARD_FILES+7)/8; j++) excludeMap[j] = 0;
6122 safeStrCpy(exclusionHeader, "exclude: none best tail \n", MSG_SIZ);
6123 excludePtr = 24; exCnt = 0;
6127 UpdateExcludeHeader (int fromY, int fromX, int toY, int toX, char promoChar, int incl)
6129 char buf[2*MOVE_LEN], *p, c = incl ? '+' : '-';
6130 Exclusion *e = excluTab;
6132 for(i=0; i<exCnt; i++)
6133 if(e[i].ff == fromX && e[i].fr == fromY &&
6134 e[i].tf == toX && e[i].tr == toY && e[i].pc == promoChar) break;
6135 if(i == exCnt) { // was not in exclude list; add it
6136 CoordsToAlgebraic(boards[currentMove], PosFlags(currentMove), fromY, fromX, toY, toX, promoChar, buf);
6137 if(strlen(exclusionHeader + excludePtr) < strlen(buf)) { // no space to write move
6138 if(c != exclusionHeader[19]) exclusionHeader[19] = '*'; // tail is now in mixed state
6141 e[i].ff = fromX; e[i].fr = fromY; e[i].tf = toX; e[i].tr = toY; e[i].pc = promoChar;
6142 excludePtr++; e[i].mark = excludePtr++;
6143 for(p=buf; *p; p++) exclusionHeader[excludePtr++] = *p; // copy move
6146 exclusionHeader[e[i].mark] = c;
6150 DefaultPromoChoice (int white)
6153 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
6154 result = WhiteFerz; // no choice
6155 else if(gameInfo.variant == VariantSuicide || gameInfo.variant == VariantGiveaway)
6156 result= WhiteKing; // in Suicide Q is the last thing we want
6157 else if(gameInfo.variant == VariantSpartan)
6158 result = white ? WhiteQueen : WhiteAngel;
6159 else result = WhiteQueen;
6160 if(!white) result = WHITE_TO_BLACK result;
6164 static int autoQueen; // [HGM] oneclick
6167 HasPromotionChoice (int fromX, int fromY, int toX, int toY, char *promoChoice, int sweepSelect)
6169 /* [HGM] rewritten IsPromotion to only flag promotions that offer a choice */
6170 /* [HGM] add Shogi promotions */
6171 int promotionZoneSize=1, highestPromotingPiece = (int)WhitePawn;
6176 if(fromX < BOARD_LEFT || fromX >= BOARD_RGHT) return FALSE; // drop
6177 if(toX < BOARD_LEFT || toX >= BOARD_RGHT) return FALSE; // move into holdings
6179 if(gameMode == EditPosition || gameInfo.variant == VariantXiangqi || // no promotions
6180 !(fromX >=0 && fromY >= 0 && toX >= 0 && toY >= 0) ) // invalid move
6183 piece = boards[currentMove][fromY][fromX];
6184 if(gameInfo.variant == VariantShogi) {
6185 promotionZoneSize = BOARD_HEIGHT/3;
6186 highestPromotingPiece = (int)WhiteFerz;
6187 } else if(gameInfo.variant == VariantMakruk || gameInfo.variant == VariantGrand) {
6188 promotionZoneSize = 3;
6191 // Treat Lance as Pawn when it is not representing Amazon
6192 if(gameInfo.variant != VariantSuper) {
6193 if(piece == WhiteLance) piece = WhitePawn; else
6194 if(piece == BlackLance) piece = BlackPawn;
6197 // next weed out all moves that do not touch the promotion zone at all
6198 if((int)piece >= BlackPawn) {
6199 if(toY >= promotionZoneSize && fromY >= promotionZoneSize)
6201 highestPromotingPiece = WHITE_TO_BLACK highestPromotingPiece;
6203 if( toY < BOARD_HEIGHT - promotionZoneSize &&
6204 fromY < BOARD_HEIGHT - promotionZoneSize) return FALSE;
6207 if( (int)piece > highestPromotingPiece ) return FALSE; // non-promoting piece
6209 // weed out mandatory Shogi promotions
6210 if(gameInfo.variant == VariantShogi) {
6211 if(piece >= BlackPawn) {
6212 if(toY == 0 && piece == BlackPawn ||
6213 toY == 0 && piece == BlackQueen ||
6214 toY <= 1 && piece == BlackKnight) {
6219 if(toY == BOARD_HEIGHT-1 && piece == WhitePawn ||
6220 toY == BOARD_HEIGHT-1 && piece == WhiteQueen ||
6221 toY >= BOARD_HEIGHT-2 && piece == WhiteKnight) {
6228 // weed out obviously illegal Pawn moves
6229 if(appData.testLegality && (piece == WhitePawn || piece == BlackPawn) ) {
6230 if(toX > fromX+1 || toX < fromX-1) return FALSE; // wide
6231 if(piece == WhitePawn && toY != fromY+1) return FALSE; // deep
6232 if(piece == BlackPawn && toY != fromY-1) return FALSE; // deep
6233 if(fromX != toX && gameInfo.variant == VariantShogi) return FALSE;
6234 // note we are not allowed to test for valid (non-)capture, due to premove
6237 // we either have a choice what to promote to, or (in Shogi) whether to promote
6238 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk) {
6239 *promoChoice = PieceToChar(BlackFerz); // no choice
6242 // no sense asking what we must promote to if it is going to explode...
6243 if(gameInfo.variant == VariantAtomic && boards[currentMove][toY][toX] != EmptySquare) {
6244 *promoChoice = PieceToChar(BlackQueen); // Queen as good as any
6247 // give caller the default choice even if we will not make it
6248 *promoChoice = ToLower(PieceToChar(defaultPromoChoice));
6249 if(gameInfo.variant == VariantShogi) *promoChoice = (defaultPromoChoice == piece ? '=' : '+');
6250 if( sweepSelect && gameInfo.variant != VariantGreat
6251 && gameInfo.variant != VariantGrand
6252 && gameInfo.variant != VariantSuper) return FALSE;
6253 if(autoQueen) return FALSE; // predetermined
6255 // suppress promotion popup on illegal moves that are not premoves
6256 premove = gameMode == IcsPlayingWhite && !WhiteOnMove(currentMove) ||
6257 gameMode == IcsPlayingBlack && WhiteOnMove(currentMove);
6258 if(appData.testLegality && !premove) {
6259 moveType = LegalityTest(boards[currentMove], PosFlags(currentMove),
6260 fromY, fromX, toY, toX, gameInfo.variant == VariantShogi ? '+' : NULLCHAR);
6261 if(moveType != WhitePromotion && moveType != BlackPromotion)
6269 InPalace (int row, int column)
6270 { /* [HGM] for Xiangqi */
6271 if( (row < 3 || row > BOARD_HEIGHT-4) &&
6272 column < (BOARD_WIDTH + 4)/2 &&
6273 column > (BOARD_WIDTH - 5)/2 ) return TRUE;
6278 PieceForSquare (int x, int y)
6280 if (x < 0 || x >= BOARD_WIDTH || y < 0 || y >= BOARD_HEIGHT)
6283 return boards[currentMove][y][x];
6287 OKToStartUserMove (int x, int y)
6289 ChessSquare from_piece;
6292 if (matchMode) return FALSE;
6293 if (gameMode == EditPosition) return TRUE;
6295 if (x >= 0 && y >= 0)
6296 from_piece = boards[currentMove][y][x];
6298 from_piece = EmptySquare;
6300 if (from_piece == EmptySquare) return FALSE;
6302 white_piece = (int)from_piece >= (int)WhitePawn &&
6303 (int)from_piece < (int)BlackPawn; /* [HGM] can be > King! */
6307 case TwoMachinesPlay:
6315 case MachinePlaysWhite:
6316 case IcsPlayingBlack:
6317 if (appData.zippyPlay) return FALSE;
6319 DisplayMoveError(_("You are playing Black"));
6324 case MachinePlaysBlack:
6325 case IcsPlayingWhite:
6326 if (appData.zippyPlay) return FALSE;
6328 DisplayMoveError(_("You are playing White"));
6333 case PlayFromGameFile:
6334 if(!shiftKey || !appData.variations) return FALSE; // [HGM] allow starting variation in this mode
6336 if (!white_piece && WhiteOnMove(currentMove)) {
6337 DisplayMoveError(_("It is White's turn"));
6340 if (white_piece && !WhiteOnMove(currentMove)) {
6341 DisplayMoveError(_("It is Black's turn"));
6344 if (cmailMsgLoaded && (currentMove < cmailOldMove)) {
6345 /* Editing correspondence game history */
6346 /* Could disallow this or prompt for confirmation */
6351 case BeginningOfGame:
6352 if (appData.icsActive) return FALSE;
6353 if (!appData.noChessProgram) {
6355 DisplayMoveError(_("You are playing White"));
6362 if (!white_piece && WhiteOnMove(currentMove)) {
6363 DisplayMoveError(_("It is White's turn"));
6366 if (white_piece && !WhiteOnMove(currentMove)) {
6367 DisplayMoveError(_("It is Black's turn"));
6376 if (currentMove != forwardMostMove && gameMode != AnalyzeMode
6377 && gameMode != EditGame // [HGM] vari: treat as AnalyzeMode
6378 && gameMode != PlayFromGameFile // [HGM] as EditGame, with protected main line
6379 && gameMode != AnalyzeFile && gameMode != Training) {
6380 DisplayMoveError(_("Displayed position is not current"));
6387 OnlyMove (int *x, int *y, Boolean captures)
6389 DisambiguateClosure cl;
6390 if (appData.zippyPlay || !appData.testLegality) return FALSE;
6392 case MachinePlaysBlack:
6393 case IcsPlayingWhite:
6394 case BeginningOfGame:
6395 if(!WhiteOnMove(currentMove)) return FALSE;
6397 case MachinePlaysWhite:
6398 case IcsPlayingBlack:
6399 if(WhiteOnMove(currentMove)) return FALSE;
6406 cl.pieceIn = EmptySquare;
6411 cl.promoCharIn = NULLCHAR;
6412 Disambiguate(boards[currentMove], PosFlags(currentMove), &cl);
6413 if( cl.kind == NormalMove ||
6414 cl.kind == AmbiguousMove && captures && cl.captures == 1 ||
6415 cl.kind == WhitePromotion || cl.kind == BlackPromotion ||
6416 cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) {
6423 if(cl.kind != ImpossibleMove) return FALSE;
6424 cl.pieceIn = EmptySquare;
6429 cl.promoCharIn = NULLCHAR;
6430 Disambiguate(boards[currentMove], PosFlags(currentMove), &cl);
6431 if( cl.kind == NormalMove ||
6432 cl.kind == AmbiguousMove && captures && cl.captures == 1 ||
6433 cl.kind == WhitePromotion || cl.kind == BlackPromotion ||
6434 cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) {
6439 autoQueen = TRUE; // act as if autoQueen on when we click to-square
6445 FILE *lastLoadGameFP = NULL, *lastLoadPositionFP = NULL;
6446 int lastLoadGameNumber = 0, lastLoadPositionNumber = 0;
6447 int lastLoadGameUseList = FALSE;
6448 char lastLoadGameTitle[MSG_SIZ], lastLoadPositionTitle[MSG_SIZ];
6449 ChessMove lastLoadGameStart = EndOfFile;
6453 UserMoveEvent(int fromX, int fromY, int toX, int toY, int promoChar)
6456 ChessSquare pdown, pup;
6457 int ff=fromX, rf=fromY, ft=toX, rt=toY;
6460 /* Check if the user is playing in turn. This is complicated because we
6461 let the user "pick up" a piece before it is his turn. So the piece he
6462 tried to pick up may have been captured by the time he puts it down!
6463 Therefore we use the color the user is supposed to be playing in this
6464 test, not the color of the piece that is currently on the starting
6465 square---except in EditGame mode, where the user is playing both
6466 sides; fortunately there the capture race can't happen. (It can
6467 now happen in IcsExamining mode, but that's just too bad. The user
6468 will get a somewhat confusing message in that case.)
6473 case TwoMachinesPlay:
6477 /* We switched into a game mode where moves are not accepted,
6478 perhaps while the mouse button was down. */
6481 case MachinePlaysWhite:
6482 /* User is moving for Black */
6483 if (WhiteOnMove(currentMove)) {
6484 DisplayMoveError(_("It is White's turn"));
6489 case MachinePlaysBlack:
6490 /* User is moving for White */
6491 if (!WhiteOnMove(currentMove)) {
6492 DisplayMoveError(_("It is Black's turn"));
6497 case PlayFromGameFile:
6498 if(!shiftKey ||!appData.variations) return; // [HGM] only variations
6501 case BeginningOfGame:
6504 if(fromY == DROP_RANK) break; // [HGM] drop moves (entered through move type-in) are automatically assigned to side-to-move
6505 if ((int) boards[currentMove][fromY][fromX] >= (int) BlackPawn &&
6506 (int) boards[currentMove][fromY][fromX] < (int) EmptySquare) {
6507 /* User is moving for Black */
6508 if (WhiteOnMove(currentMove)) {
6509 DisplayMoveError(_("It is White's turn"));
6513 /* User is moving for White */
6514 if (!WhiteOnMove(currentMove)) {
6515 DisplayMoveError(_("It is Black's turn"));
6521 case IcsPlayingBlack:
6522 /* User is moving for Black */
6523 if (WhiteOnMove(currentMove)) {
6524 if (!appData.premove) {
6525 DisplayMoveError(_("It is White's turn"));
6526 } else if (toX >= 0 && toY >= 0) {
6529 premoveFromX = fromX;
6530 premoveFromY = fromY;
6531 premovePromoChar = promoChar;
6533 if (appData.debugMode)
6534 fprintf(debugFP, "Got premove: fromX %d,"
6535 "fromY %d, toX %d, toY %d\n",
6536 fromX, fromY, toX, toY);
6542 case IcsPlayingWhite:
6543 /* User is moving for White */
6544 if (!WhiteOnMove(currentMove)) {
6545 if (!appData.premove) {
6546 DisplayMoveError(_("It is Black's turn"));
6547 } else if (toX >= 0 && toY >= 0) {
6550 premoveFromX = fromX;
6551 premoveFromY = fromY;
6552 premovePromoChar = promoChar;
6554 if (appData.debugMode)
6555 fprintf(debugFP, "Got premove: fromX %d,"
6556 "fromY %d, toX %d, toY %d\n",
6557 fromX, fromY, toX, toY);
6567 /* EditPosition, empty square, or different color piece;
6568 click-click move is possible */
6569 if (toX == -2 || toY == -2) {
6570 boards[0][fromY][fromX] = EmptySquare;
6571 DrawPosition(FALSE, boards[currentMove]);
6573 } else if (toX >= 0 && toY >= 0) {
6574 boards[0][toY][toX] = boards[0][fromY][fromX];
6575 if(fromX == BOARD_LEFT-2) { // handle 'moves' out of holdings
6576 if(boards[0][fromY][0] != EmptySquare) {
6577 if(boards[0][fromY][1]) boards[0][fromY][1]--;
6578 if(boards[0][fromY][1] == 0) boards[0][fromY][0] = EmptySquare;
6581 if(fromX == BOARD_RGHT+1) {
6582 if(boards[0][fromY][BOARD_WIDTH-1] != EmptySquare) {
6583 if(boards[0][fromY][BOARD_WIDTH-2]) boards[0][fromY][BOARD_WIDTH-2]--;
6584 if(boards[0][fromY][BOARD_WIDTH-2] == 0) boards[0][fromY][BOARD_WIDTH-1] = EmptySquare;
6587 boards[0][fromY][fromX] = EmptySquare;
6588 DrawPosition(FALSE, boards[currentMove]);
6594 if(doubleClick && (toX == -2 || toY == -2)) { // [HGM] exclude: off-board move means exclude all
6595 int i; // note that drop moves still have holdings coords as from-square at this point
6596 ChessMove moveType; char pc;
6597 for(i=0; i<(BOARD_RANKS*BOARD_FILES*BOARD_RANKS*BOARD_FILES+7)/8; i++) excludeMap[i] = -(toY == -2);
6598 mappedMove = currentMove;
6599 if(toY != -2) { SendToProgram("include all\n", &first); return; }
6600 SendToProgram("exclude all\n", &first);
6601 i = ParseOneMove(lastPV[0], currentMove, &moveType, &fromX, &fromY, &toX, &toY, &pc);
6602 ff=fromX, rf=fromY, ft=toX, rt=toY, promoChar = pc; // make copy that will survive drop encoding
6603 if(!i) return; // kludge: continue with move changed to engine's last-reported best, so it gets included again.
6606 if(toX < 0 || toY < 0) return;
6607 pdown = boards[currentMove][fromY][fromX];
6608 pup = boards[currentMove][toY][toX];
6610 /* [HGM] If move started in holdings, it means a drop. Convert to standard form */
6611 if( (fromX == BOARD_LEFT-2 || fromX == BOARD_RGHT+1) && fromY != DROP_RANK ) {
6612 if( pup != EmptySquare ) return;
6613 moveType = WhiteOnMove(currentMove) ? WhiteDrop : BlackDrop;
6614 if(appData.debugMode) fprintf(debugFP, "Drop move %d, curr=%d, x=%d,y=%d, p=%d\n",
6615 moveType, currentMove, fromX, fromY, boards[currentMove][fromY][fromX]);
6616 // holdings might not be sent yet in ICS play; we have to figure out which piece belongs here
6617 if(fromX == 0) fromY = BOARD_HEIGHT-1 - fromY; // black holdings upside-down
6618 fromX = fromX ? WhitePawn : BlackPawn; // first piece type in selected holdings
6619 while(PieceToChar(fromX) == '.' || PieceToNumber(fromX) != fromY && fromX != (int) EmptySquare) fromX++;
6623 /* [HGM] always test for legality, to get promotion info */
6624 moveType = LegalityTest(boards[currentMove], PosFlags(currentMove),
6625 fromY, fromX, toY, toX, promoChar);
6627 if(fromY == DROP_RANK && fromX == EmptySquare && (gameMode == AnalyzeMode || gameMode == EditGame)) moveType = NormalMove;
6629 /* [HGM] but possibly ignore an IllegalMove result */
6630 if (appData.testLegality) {
6631 if (moveType == IllegalMove || moveType == ImpossibleMove) {
6632 DisplayMoveError(_("Illegal move"));
6637 if(doubleClick) { // [HGM] exclude: move entered with double-click on from square is for exclusion, not playing
6638 int i=(BOARD_FILES*rf+ff)*BOARD_RANKS*BOARD_FILES + (BOARD_FILES*rt+ft), j;
6640 if(mappedMove != currentMove) ClearMap();
6642 snprintf(buf, MSG_SIZ, "%sclude ", excludeMap[i] & 1<<j ? "in" : "ex");
6643 if(excludeMap[i] & 1<<j) ClearPremoveHighlights();
6644 else ClearHighlights(), SetPremoveHighlights(ff, rf, ft, rt);
6645 if(!promoChar) excludeMap[i] ^= 1<<j;
6646 mappedMove = currentMove;
6647 CoordsToComputerAlgebraic(fromY, fromX, toY, toX, promoChar, buf+8);
6648 SendToProgram(buf, &first);
6649 UpdateExcludeHeader(fromY, fromX, toY, toX, promoChar, buf[0] == 'i');
6653 FinishMove(moveType, fromX, fromY, toX, toY, promoChar);
6656 /* Common tail of UserMoveEvent and DropMenuEvent */
6658 FinishMove (ChessMove moveType, int fromX, int fromY, int toX, int toY, int promoChar)
6662 if((gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand) && promoChar != NULLCHAR) {
6663 // [HGM] superchess: suppress promotions to non-available piece (but P always allowed)
6664 int k = PieceToNumber(CharToPiece(ToUpper(promoChar)));
6665 if(WhiteOnMove(currentMove)) {
6666 if(!boards[currentMove][k][BOARD_WIDTH-2]) return 0;
6668 if(!boards[currentMove][BOARD_HEIGHT-1-k][1]) return 0;
6672 /* [HGM] <popupFix> kludge to avoid having to know the exact promotion
6673 move type in caller when we know the move is a legal promotion */
6674 if(moveType == NormalMove && promoChar)
6675 moveType = WhiteOnMove(currentMove) ? WhitePromotion : BlackPromotion;
6677 /* [HGM] <popupFix> The following if has been moved here from
6678 UserMoveEvent(). Because it seemed to belong here (why not allow
6679 piece drops in training games?), and because it can only be
6680 performed after it is known to what we promote. */
6681 if (gameMode == Training) {
6682 /* compare the move played on the board to the next move in the
6683 * game. If they match, display the move and the opponent's response.
6684 * If they don't match, display an error message.
6688 CopyBoard(testBoard, boards[currentMove]);
6689 ApplyMove(fromX, fromY, toX, toY, promoChar, testBoard);
6691 if (CompareBoards(testBoard, boards[currentMove+1])) {
6692 ForwardInner(currentMove+1);
6694 /* Autoplay the opponent's response.
6695 * if appData.animate was TRUE when Training mode was entered,
6696 * the response will be animated.
6698 saveAnimate = appData.animate;
6699 appData.animate = animateTraining;
6700 ForwardInner(currentMove+1);
6701 appData.animate = saveAnimate;
6703 /* check for the end of the game */
6704 if (currentMove >= forwardMostMove) {
6705 gameMode = PlayFromGameFile;
6707 SetTrainingModeOff();
6708 DisplayInformation(_("End of game"));
6711 DisplayError(_("Incorrect move"), 0);
6716 /* Ok, now we know that the move is good, so we can kill
6717 the previous line in Analysis Mode */
6718 if ((gameMode == AnalyzeMode || gameMode == EditGame || gameMode == PlayFromGameFile && appData.variations && shiftKey)
6719 && currentMove < forwardMostMove) {
6720 if(appData.variations && shiftKey) PushTail(currentMove, forwardMostMove); // [HGM] vari: save tail of game
6721 else forwardMostMove = currentMove;
6726 /* If we need the chess program but it's dead, restart it */
6727 ResurrectChessProgram();
6729 /* A user move restarts a paused game*/
6733 thinkOutput[0] = NULLCHAR;
6735 MakeMove(fromX, fromY, toX, toY, promoChar); /*updates forwardMostMove*/
6737 if(Adjudicate(NULL)) { // [HGM] adjudicate: take care of automatic game end
6738 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6742 if (gameMode == BeginningOfGame) {
6743 if (appData.noChessProgram) {
6744 gameMode = EditGame;
6748 gameMode = MachinePlaysBlack;
6751 snprintf(buf, MSG_SIZ, "%s %s %s", gameInfo.white, _("vs."), gameInfo.black);
6753 if (first.sendName) {
6754 snprintf(buf, MSG_SIZ,"name %s\n", gameInfo.white);
6755 SendToProgram(buf, &first);
6762 /* Relay move to ICS or chess engine */
6763 if (appData.icsActive) {
6764 if (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
6765 gameMode == IcsExamining) {
6766 if(userOfferedDraw && (signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
6767 SendToICS(ics_prefix); // [HGM] drawclaim: send caim and move on one line for FICS
6769 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
6771 // also send plain move, in case ICS does not understand atomic claims
6772 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
6776 if (first.sendTime && (gameMode == BeginningOfGame ||
6777 gameMode == MachinePlaysWhite ||
6778 gameMode == MachinePlaysBlack)) {
6779 SendTimeRemaining(&first, gameMode != MachinePlaysBlack);
6781 if (gameMode != EditGame && gameMode != PlayFromGameFile) {
6782 // [HGM] book: if program might be playing, let it use book
6783 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, FALSE);
6784 first.maybeThinking = TRUE;
6785 } else if(fromY == DROP_RANK && fromX == EmptySquare) {
6786 if(!first.useSetboard) SendToProgram("undo\n", &first); // kludge to change stm in engines that do not support setboard
6787 SendBoard(&first, currentMove+1);
6788 } else SendMoveToProgram(forwardMostMove-1, &first);
6789 if (currentMove == cmailOldMove + 1) {
6790 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
6794 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6798 if(appData.testLegality)
6799 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
6805 if (WhiteOnMove(currentMove)) {
6806 GameEnds(BlackWins, "Black mates", GE_PLAYER);
6808 GameEnds(WhiteWins, "White mates", GE_PLAYER);
6812 GameEnds(GameIsDrawn, "Stalemate", GE_PLAYER);
6817 case MachinePlaysBlack:
6818 case MachinePlaysWhite:
6819 /* disable certain menu options while machine is thinking */
6820 SetMachineThinkingEnables();
6827 userOfferedDraw = FALSE; // [HGM] drawclaim: after move made, and tested for claimable draw
6828 promoDefaultAltered = FALSE; // [HGM] fall back on default choice
6830 if(bookHit) { // [HGM] book: simulate book reply
6831 static char bookMove[MSG_SIZ]; // a bit generous?
6833 programStats.nodes = programStats.depth = programStats.time =
6834 programStats.score = programStats.got_only_move = 0;
6835 sprintf(programStats.movelist, "%s (xbook)", bookHit);
6837 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
6838 strcat(bookMove, bookHit);
6839 HandleMachineMove(bookMove, &first);
6845 Mark (Board board, int flags, ChessMove kind, int rf, int ff, int rt, int ft, VOIDSTAR closure)
6847 typedef char Markers[BOARD_RANKS][BOARD_FILES];
6848 Markers *m = (Markers *) closure;
6849 if(rf == fromY && ff == fromX)
6850 (*m)[rt][ft] = 1 + (board[rt][ft] != EmptySquare
6851 || kind == WhiteCapturesEnPassant
6852 || kind == BlackCapturesEnPassant);
6853 else if(flags & F_MANDATORY_CAPTURE && board[rt][ft] != EmptySquare) (*m)[rt][ft] = 3;
6857 MarkTargetSquares (int clear)
6860 if(!appData.markers || !appData.highlightDragging || appData.icsActive && gameInfo.variant < VariantShogi ||
6861 !appData.testLegality || gameMode == EditPosition) return;
6863 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) marker[y][x] = 0;
6866 GenLegal(boards[currentMove], PosFlags(currentMove), Mark, (void*) marker, EmptySquare);
6867 if(PosFlags(0) & F_MANDATORY_CAPTURE) {
6868 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) if(marker[y][x]>1) capt++;
6870 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) if(marker[y][x] == 1) marker[y][x] = 0;
6873 DrawPosition(TRUE, NULL);
6877 Explode (Board board, int fromX, int fromY, int toX, int toY)
6879 if(gameInfo.variant == VariantAtomic &&
6880 (board[toY][toX] != EmptySquare || // capture?
6881 toX != fromX && (board[fromY][fromX] == WhitePawn || // e.p. ?
6882 board[fromY][fromX] == BlackPawn )
6884 AnimateAtomicCapture(board, fromX, fromY, toX, toY);
6890 ChessSquare gatingPiece = EmptySquare; // exported to front-end, for dragging
6893 CanPromote (ChessSquare piece, int y)
6895 if(gameMode == EditPosition) return FALSE; // no promotions when editing position
6896 // some variants have fixed promotion piece, no promotion at all, or another selection mechanism
6897 if(gameInfo.variant == VariantShogi || gameInfo.variant == VariantXiangqi ||
6898 gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat ||
6899 gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier ||
6900 gameInfo.variant == VariantMakruk) return FALSE;
6901 return (piece == BlackPawn && y == 1 ||
6902 piece == WhitePawn && y == BOARD_HEIGHT-2 ||
6903 piece == BlackLance && y == 1 ||
6904 piece == WhiteLance && y == BOARD_HEIGHT-2 );
6908 LeftClick (ClickType clickType, int xPix, int yPix)
6911 Boolean saveAnimate;
6912 static int second = 0, promotionChoice = 0, clearFlag = 0;
6913 char promoChoice = NULLCHAR;
6915 static TimeMark lastClickTime, prevClickTime;
6917 if(SeekGraphClick(clickType, xPix, yPix, 0)) return;
6919 prevClickTime = lastClickTime; GetTimeMark(&lastClickTime);
6921 if (clickType == Press) ErrorPopDown();
6923 x = EventToSquare(xPix, BOARD_WIDTH);
6924 y = EventToSquare(yPix, BOARD_HEIGHT);
6925 if (!flipView && y >= 0) {
6926 y = BOARD_HEIGHT - 1 - y;
6928 if (flipView && x >= 0) {
6929 x = BOARD_WIDTH - 1 - x;
6932 if(promoSweep != EmptySquare) { // up-click during sweep-select of promo-piece
6933 defaultPromoChoice = promoSweep;
6934 promoSweep = EmptySquare; // terminate sweep
6935 promoDefaultAltered = TRUE;
6936 if(!selectFlag && (x != toX || y != toY)) x = fromX, y = fromY; // and fake up-click on same square if we were still selecting
6939 if(promotionChoice) { // we are waiting for a click to indicate promotion piece
6940 if(clickType == Release) return; // ignore upclick of click-click destination
6941 promotionChoice = FALSE; // only one chance: if click not OK it is interpreted as cancel
6942 if(appData.debugMode) fprintf(debugFP, "promotion click, x=%d, y=%d\n", x, y);
6943 if(gameInfo.holdingsWidth &&
6944 (WhiteOnMove(currentMove)
6945 ? x == BOARD_WIDTH-1 && y < gameInfo.holdingsSize && y >= 0
6946 : x == 0 && y >= BOARD_HEIGHT - gameInfo.holdingsSize && y < BOARD_HEIGHT) ) {
6947 // click in right holdings, for determining promotion piece
6948 ChessSquare p = boards[currentMove][y][x];
6949 if(appData.debugMode) fprintf(debugFP, "square contains %d\n", (int)p);
6950 if(p == WhitePawn || p == BlackPawn) p = EmptySquare; // [HGM] Pawns could be valid as deferral
6951 if(p != EmptySquare || gameInfo.variant == VariantGrand && toY != 0 && toY != BOARD_HEIGHT-1) { // [HGM] grand: empty square means defer
6952 FinishMove(NormalMove, fromX, fromY, toX, toY, p==EmptySquare ? NULLCHAR : ToLower(PieceToChar(p)));
6957 DrawPosition(FALSE, boards[currentMove]);
6961 /* [HGM] holdings: next 5 lines: ignore all clicks between board and holdings */
6962 if(clickType == Press
6963 && ( x == BOARD_LEFT-1 || x == BOARD_RGHT
6964 || x == BOARD_LEFT-2 && y < BOARD_HEIGHT-gameInfo.holdingsSize
6965 || x == BOARD_RGHT+1 && y >= gameInfo.holdingsSize) )
6968 if(gotPremove && x == premoveFromX && y == premoveFromY && clickType == Release) {
6969 // could be static click on premove from-square: abort premove
6971 ClearPremoveHighlights();
6974 if(clickType == Press && fromX == x && fromY == y && promoDefaultAltered && SubtractTimeMarks(&lastClickTime, &prevClickTime) >= 200)
6975 fromX = fromY = -1; // second click on piece after altering default promo piece treated as first click
6977 if(!promoDefaultAltered) { // determine default promotion piece, based on the side the user is moving for
6978 int side = (gameMode == IcsPlayingWhite || gameMode == MachinePlaysBlack ||
6979 gameMode != MachinePlaysWhite && gameMode != IcsPlayingBlack && WhiteOnMove(currentMove));
6980 defaultPromoChoice = DefaultPromoChoice(side);
6983 autoQueen = appData.alwaysPromoteToQueen;
6987 gatingPiece = EmptySquare;
6988 if (clickType != Press) {
6989 if(dragging) { // [HGM] from-square must have been reset due to game end since last press
6990 DragPieceEnd(xPix, yPix); dragging = 0;
6991 DrawPosition(FALSE, NULL);
6995 doubleClick = FALSE;
6996 fromX = x; fromY = y; toX = toY = -1;
6997 if(!appData.oneClick || !OnlyMove(&x, &y, FALSE) ||
6998 // even if only move, we treat as normal when this would trigger a promotion popup, to allow sweep selection
6999 appData.sweepSelect && CanPromote(boards[currentMove][fromY][fromX], fromY) && originalY != y) {
7001 if (OKToStartUserMove(fromX, fromY)) {
7003 MarkTargetSquares(0);
7004 DragPieceBegin(xPix, yPix, FALSE); dragging = 1;
7005 if(appData.sweepSelect && CanPromote(piece = boards[currentMove][fromY][fromX], fromY)) {
7006 promoSweep = defaultPromoChoice;
7007 selectFlag = 0; lastX = xPix; lastY = yPix;
7008 Sweep(0); // Pawn that is going to promote: preview promotion piece
7009 DisplayMessage("", _("Pull pawn backwards to under-promote"));
7011 if (appData.highlightDragging) {
7012 SetHighlights(fromX, fromY, -1, -1);
7014 } else fromX = fromY = -1;
7020 if (clickType == Press && gameMode != EditPosition) {
7025 // ignore off-board to clicks
7026 if(y < 0 || x < 0) return;
7028 /* Check if clicking again on the same color piece */
7029 fromP = boards[currentMove][fromY][fromX];
7030 toP = boards[currentMove][y][x];
7031 frc = gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom || gameInfo.variant == VariantSChess;
7032 if ((WhitePawn <= fromP && fromP <= WhiteKing &&
7033 WhitePawn <= toP && toP <= WhiteKing &&
7034 !(fromP == WhiteKing && toP == WhiteRook && frc) &&
7035 !(fromP == WhiteRook && toP == WhiteKing && frc)) ||
7036 (BlackPawn <= fromP && fromP <= BlackKing &&
7037 BlackPawn <= toP && toP <= BlackKing &&
7038 !(fromP == BlackRook && toP == BlackKing && frc) && // allow also RxK as FRC castling
7039 !(fromP == BlackKing && toP == BlackRook && frc))) {
7040 /* Clicked again on same color piece -- changed his mind */
7041 second = (x == fromX && y == fromY);
7042 if(second && gameMode == AnalyzeMode && SubtractTimeMarks(&lastClickTime, &prevClickTime) < 200) {
7043 second = FALSE; // first double-click rather than scond click
7044 doubleClick = first.excludeMoves; // used by UserMoveEvent to recognize exclude moves
7046 promoDefaultAltered = FALSE;
7047 MarkTargetSquares(1);
7048 if(!second || appData.oneClick && !OnlyMove(&x, &y, TRUE)) {
7049 if (appData.highlightDragging) {
7050 SetHighlights(x, y, -1, -1);
7054 if (OKToStartUserMove(x, y)) {
7055 if(gameInfo.variant == VariantSChess && // S-Chess: back-rank piece selected after holdings means gating
7056 (fromX == BOARD_LEFT-2 || fromX == BOARD_RGHT+1) &&
7057 y == (toP < BlackPawn ? 0 : BOARD_HEIGHT-1))
7058 gatingPiece = boards[currentMove][fromY][fromX];
7059 else gatingPiece = doubleClick ? fromP : EmptySquare;
7061 fromY = y; dragging = 1;
7062 MarkTargetSquares(0);
7063 DragPieceBegin(xPix, yPix, FALSE);
7064 if(appData.sweepSelect && CanPromote(piece = boards[currentMove][y][x], y)) {
7065 promoSweep = defaultPromoChoice;
7066 selectFlag = 0; lastX = xPix; lastY = yPix;
7067 Sweep(0); // Pawn that is going to promote: preview promotion piece
7071 if(x == fromX && y == fromY) return; // if OnlyMove altered (x,y) we go on
7074 // ignore clicks on holdings
7075 if(x < BOARD_LEFT || x >= BOARD_RGHT) return;
7078 if (clickType == Release && x == fromX && y == fromY) {
7079 DragPieceEnd(xPix, yPix); dragging = 0;
7081 // a deferred attempt to click-click move an empty square on top of a piece
7082 boards[currentMove][y][x] = EmptySquare;
7084 DrawPosition(FALSE, boards[currentMove]);
7085 fromX = fromY = -1; clearFlag = 0;
7088 if (appData.animateDragging) {
7089 /* Undo animation damage if any */
7090 DrawPosition(FALSE, NULL);
7093 /* Second up/down in same square; just abort move */
7096 gatingPiece = EmptySquare;
7099 ClearPremoveHighlights();
7101 /* First upclick in same square; start click-click mode */
7102 SetHighlights(x, y, -1, -1);
7109 /* we now have a different from- and (possibly off-board) to-square */
7110 /* Completed move */
7113 saveAnimate = appData.animate;
7114 MarkTargetSquares(1);
7115 if (clickType == Press) {
7116 if(gameMode == EditPosition && boards[currentMove][fromY][fromX] == EmptySquare) {
7117 // must be Edit Position mode with empty-square selected
7118 fromX = x; fromY = y; DragPieceBegin(xPix, yPix, FALSE); dragging = 1; // consider this a new attempt to drag
7119 if(x >= BOARD_LEFT && x < BOARD_RGHT) clearFlag = 1; // and defer click-click move of empty-square to up-click
7122 if(appData.sweepSelect && HasPromotionChoice(fromX, fromY, toX, toY, &promoChoice, FALSE)) {
7123 ChessSquare piece = boards[currentMove][fromY][fromX];
7124 DragPieceBegin(xPix, yPix, TRUE); dragging = 1;
7125 promoSweep = defaultPromoChoice;
7126 if(PieceToChar(PROMOTED piece) == '+') promoSweep = PROMOTED piece;
7127 selectFlag = 0; lastX = xPix; lastY = yPix;
7128 Sweep(0); // Pawn that is going to promote: preview promotion piece
7129 DisplayMessage("", _("Pull pawn backwards to under-promote"));
7130 DrawPosition(FALSE, boards[currentMove]);
7133 /* Finish clickclick move */
7134 if (appData.animate || appData.highlightLastMove) {
7135 SetHighlights(fromX, fromY, toX, toY);
7140 /* Finish drag move */
7141 if (appData.highlightLastMove) {
7142 SetHighlights(fromX, fromY, toX, toY);
7146 DragPieceEnd(xPix, yPix); dragging = 0;
7147 /* Don't animate move and drag both */
7148 appData.animate = FALSE;
7151 // moves into holding are invalid for now (except in EditPosition, adapting to-square)
7152 if(x >= 0 && x < BOARD_LEFT || x >= BOARD_RGHT) {
7153 ChessSquare piece = boards[currentMove][fromY][fromX];
7154 if(gameMode == EditPosition && piece != EmptySquare &&
7155 fromX >= BOARD_LEFT && fromX < BOARD_RGHT) {
7158 if(x == BOARD_LEFT-2 && piece >= BlackPawn) {
7159 n = PieceToNumber(piece - (int)BlackPawn);
7160 if(n >= gameInfo.holdingsSize) { n = 0; piece = BlackPawn; }
7161 boards[currentMove][BOARD_HEIGHT-1 - n][0] = piece;
7162 boards[currentMove][BOARD_HEIGHT-1 - n][1]++;
7164 if(x == BOARD_RGHT+1 && piece < BlackPawn) {
7165 n = PieceToNumber(piece);
7166 if(n >= gameInfo.holdingsSize) { n = 0; piece = WhitePawn; }
7167 boards[currentMove][n][BOARD_WIDTH-1] = piece;
7168 boards[currentMove][n][BOARD_WIDTH-2]++;
7170 boards[currentMove][fromY][fromX] = EmptySquare;
7174 DrawPosition(TRUE, boards[currentMove]);
7178 // off-board moves should not be highlighted
7179 if(x < 0 || y < 0) ClearHighlights();
7181 if(gatingPiece != EmptySquare && gameInfo.variant == VariantSChess) promoChoice = ToLower(PieceToChar(gatingPiece));
7183 if (HasPromotionChoice(fromX, fromY, toX, toY, &promoChoice, appData.sweepSelect)) {
7184 SetHighlights(fromX, fromY, toX, toY);
7185 if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand) {
7186 // [HGM] super: promotion to captured piece selected from holdings
7187 ChessSquare p = boards[currentMove][fromY][fromX], q = boards[currentMove][toY][toX];
7188 promotionChoice = TRUE;
7189 // kludge follows to temporarily execute move on display, without promoting yet
7190 boards[currentMove][fromY][fromX] = EmptySquare; // move Pawn to 8th rank
7191 boards[currentMove][toY][toX] = p;
7192 DrawPosition(FALSE, boards[currentMove]);
7193 boards[currentMove][fromY][fromX] = p; // take back, but display stays
7194 boards[currentMove][toY][toX] = q;
7195 DisplayMessage("Click in holdings to choose piece", "");
7200 int oldMove = currentMove;
7201 UserMoveEvent(fromX, fromY, toX, toY, promoChoice);
7202 if (!appData.highlightLastMove || gotPremove) ClearHighlights();
7203 if (gotPremove) SetPremoveHighlights(fromX, fromY, toX, toY);
7204 if(saveAnimate && !appData.animate && currentMove != oldMove && // drag-move was performed
7205 Explode(boards[currentMove-1], fromX, fromY, toX, toY))
7206 DrawPosition(TRUE, boards[currentMove]);
7209 appData.animate = saveAnimate;
7210 if (appData.animate || appData.animateDragging) {
7211 /* Undo animation damage if needed */
7212 DrawPosition(FALSE, NULL);
7217 RightClick (ClickType action, int x, int y, int *fromX, int *fromY)
7218 { // front-end-free part taken out of PieceMenuPopup
7219 int whichMenu; int xSqr, ySqr;
7221 if(seekGraphUp) { // [HGM] seekgraph
7222 if(action == Press) SeekGraphClick(Press, x, y, 2); // 2 indicates right-click: no pop-down on miss
7223 if(action == Release) SeekGraphClick(Release, x, y, 2); // and no challenge on hit
7227 if((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)
7228 && !appData.zippyPlay && appData.bgObserve) { // [HGM] bughouse: show background game
7229 if(!partnerBoardValid) return -2; // suppress display of uninitialized boards
7230 if( appData.dualBoard) return -2; // [HGM] dual: is already displayed
7231 if(action == Press) {
7232 originalFlip = flipView;
7233 flipView = !flipView; // temporarily flip board to see game from partners perspective
7234 DrawPosition(TRUE, partnerBoard);
7235 DisplayMessage(partnerStatus, "");
7237 } else if(action == Release) {
7238 flipView = originalFlip;
7239 DrawPosition(TRUE, boards[currentMove]);
7245 xSqr = EventToSquare(x, BOARD_WIDTH);
7246 ySqr = EventToSquare(y, BOARD_HEIGHT);
7247 if (action == Release) {
7248 if(pieceSweep != EmptySquare) {
7249 EditPositionMenuEvent(pieceSweep, toX, toY);
7250 pieceSweep = EmptySquare;
7251 } else UnLoadPV(); // [HGM] pv
7253 if (action != Press) return -2; // return code to be ignored
7256 if(xSqr < BOARD_LEFT || xSqr >= BOARD_RGHT) return -1;
7258 if (xSqr == BOARD_LEFT-1 || xSqr == BOARD_RGHT) return -1;
7259 if (xSqr < 0 || ySqr < 0) return -1;
7260 if(appData.pieceMenu) { whichMenu = 0; break; } // edit-position menu
7261 pieceSweep = shiftKey ? BlackPawn : WhitePawn; // [HGM] sweep: prepare selecting piece by mouse sweep
7262 toX = xSqr; toY = ySqr; lastX = x, lastY = y;
7263 if(flipView) toX = BOARD_WIDTH - 1 - toX; else toY = BOARD_HEIGHT - 1 - toY;
7267 if(!appData.icsEngineAnalyze) return -1;
7268 case IcsPlayingWhite:
7269 case IcsPlayingBlack:
7270 if(!appData.zippyPlay) goto noZip;
7273 case MachinePlaysWhite:
7274 case MachinePlaysBlack:
7275 case TwoMachinesPlay: // [HGM] pv: use for showing PV
7276 if (!appData.dropMenu) {
7278 return 2; // flag front-end to grab mouse events
7280 if(gameMode == TwoMachinesPlay || gameMode == AnalyzeMode ||
7281 gameMode == AnalyzeFile || gameMode == IcsObserving) return -1;
7284 if (xSqr < 0 || ySqr < 0) return -1;
7285 if (!appData.dropMenu || appData.testLegality &&
7286 gameInfo.variant != VariantBughouse &&
7287 gameInfo.variant != VariantCrazyhouse) return -1;
7288 whichMenu = 1; // drop menu
7294 if (((*fromX = xSqr) < 0) ||
7295 ((*fromY = ySqr) < 0)) {
7296 *fromX = *fromY = -1;
7300 *fromX = BOARD_WIDTH - 1 - *fromX;
7302 *fromY = BOARD_HEIGHT - 1 - *fromY;
7308 SendProgramStatsToFrontend (ChessProgramState * cps, ChessProgramStats * cpstats)
7310 // char * hint = lastHint;
7311 FrontEndProgramStats stats;
7313 stats.which = cps == &first ? 0 : 1;
7314 stats.depth = cpstats->depth;
7315 stats.nodes = cpstats->nodes;
7316 stats.score = cpstats->score;
7317 stats.time = cpstats->time;
7318 stats.pv = cpstats->movelist;
7319 stats.hint = lastHint;
7320 stats.an_move_index = 0;
7321 stats.an_move_count = 0;
7323 if( gameMode == AnalyzeMode || gameMode == AnalyzeFile ) {
7324 stats.hint = cpstats->move_name;
7325 stats.an_move_index = cpstats->nr_moves - cpstats->moves_left;
7326 stats.an_move_count = cpstats->nr_moves;
7329 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
7331 SetProgramStats( &stats );
7335 ClearEngineOutputPane (int which)
7337 static FrontEndProgramStats dummyStats;
7338 dummyStats.which = which;
7339 dummyStats.pv = "#";
7340 SetProgramStats( &dummyStats );
7343 #define MAXPLAYERS 500
7346 TourneyStandings (int display)
7348 int i, w, b, color, wScore, bScore, dummy, nr=0, nPlayers=0;
7349 int score[MAXPLAYERS], ranking[MAXPLAYERS], points[MAXPLAYERS], games[MAXPLAYERS];
7350 char result, *p, *names[MAXPLAYERS];
7352 if(appData.tourneyType < 0 && !strchr(appData.results, '*'))
7353 return strdup(_("Swiss tourney finished")); // standings of Swiss yet TODO
7354 names[0] = p = strdup(appData.participants);
7355 while(p = strchr(p, '\n')) *p++ = NULLCHAR, names[++nPlayers] = p; // count participants
7357 for(i=0; i<nPlayers; i++) score[i] = games[i] = 0;
7359 while(result = appData.results[nr]) {
7360 color = Pairing(nr, nPlayers, &w, &b, &dummy);
7361 if(!(color ^ matchGame & 1)) { dummy = w; w = b; b = dummy; }
7362 wScore = bScore = 0;
7364 case '+': wScore = 2; break;
7365 case '-': bScore = 2; break;
7366 case '=': wScore = bScore = 1; break;
7368 case '*': return strdup("busy"); // tourney not finished
7376 if(appData.tourneyType > 0) nPlayers = appData.tourneyType; // in gauntlet, list only gauntlet engine(s)
7377 for(w=0; w<nPlayers; w++) {
7379 for(i=0; i<nPlayers; i++) if(score[i] > bScore) bScore = score[i], b = i;
7380 ranking[w] = b; points[w] = bScore; score[b] = -2;
7382 p = malloc(nPlayers*34+1);
7383 for(w=0; w<nPlayers && w<display; w++)
7384 sprintf(p+34*w, "%2d. %5.1f/%-3d %-19.19s\n", w+1, points[w]/2., games[ranking[w]], names[ranking[w]]);
7390 Count (Board board, int pCnt[], int *nW, int *nB, int *wStale, int *bStale, int *bishopColor)
7391 { // count all piece types
7393 *nB = *nW = *wStale = *bStale = *bishopColor = 0;
7394 for(p=WhitePawn; p<=EmptySquare; p++) pCnt[p] = 0;
7395 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
7398 if(p == WhitePawn && r == BOARD_HEIGHT-1) (*wStale)++; else
7399 if(p == BlackPawn && r == 0) (*bStale)++; // count last-Rank Pawns (XQ) separately
7400 if(p <= WhiteKing) (*nW)++; else if(p <= BlackKing) (*nB)++;
7401 if(p == WhiteBishop || p == WhiteFerz || p == WhiteAlfil ||
7402 p == BlackBishop || p == BlackFerz || p == BlackAlfil )
7403 *bishopColor |= 1 << ((f^r)&1); // track square color of color-bound pieces
7408 SufficientDefence (int pCnt[], int side, int nMine, int nHis)
7410 int myPawns = pCnt[WhitePawn+side]; // my total Pawn count;
7411 int majorDefense = pCnt[BlackRook-side] + pCnt[BlackCannon-side] + pCnt[BlackKnight-side];
7413 nMine -= pCnt[WhiteFerz+side] + pCnt[WhiteAlfil+side]; // discount defenders
7414 if(nMine - myPawns > 2) return FALSE; // no trivial draws with more than 1 major
7415 if(myPawns == 2 && nMine == 3) // KPP
7416 return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] >= 3;
7417 if(myPawns == 1 && nMine == 2) // KP
7418 return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] + pCnt[BlackPawn-side] >= 1;
7419 if(myPawns == 1 && nMine == 3 && pCnt[WhiteKnight+side]) // KHP
7420 return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side]*2 >= 5;
7421 if(myPawns) return FALSE;
7422 if(pCnt[WhiteRook+side])
7423 return pCnt[BlackRook-side] ||
7424 pCnt[BlackCannon-side] && (pCnt[BlackFerz-side] >= 2 || pCnt[BlackAlfil-side] >= 2) ||
7425 pCnt[BlackKnight-side] && pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] > 2 ||
7426 pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] >= 4;
7427 if(pCnt[WhiteCannon+side]) {
7428 if(pCnt[WhiteFerz+side] + myPawns == 0) return TRUE; // Cannon needs platform
7429 return majorDefense || pCnt[BlackAlfil-side] >= 2;
7431 if(pCnt[WhiteKnight+side])
7432 return majorDefense || pCnt[BlackFerz-side] >= 2 || pCnt[BlackAlfil-side] + pCnt[BlackPawn-side] >= 1;
7437 MatingPotential (int pCnt[], int side, int nMine, int nHis, int stale, int bisColor)
7439 VariantClass v = gameInfo.variant;
7441 if(v == VariantShogi || v == VariantCrazyhouse || v == VariantBughouse) return TRUE; // drop games always winnable
7442 if(v == VariantShatranj) return TRUE; // always winnable through baring
7443 if(v == VariantLosers || v == VariantSuicide || v == VariantGiveaway) return TRUE;
7444 if(v == Variant3Check || v == VariantAtomic) return nMine > 1; // can win through checking / exploding King
7446 if(v == VariantXiangqi) {
7447 int majors = 5*pCnt[BlackKnight-side] + 7*pCnt[BlackCannon-side] + 7*pCnt[BlackRook-side];
7449 nMine -= pCnt[WhiteFerz+side] + pCnt[WhiteAlfil+side] + stale; // discount defensive pieces and back-rank Pawns
7450 if(nMine + stale == 1) return (pCnt[BlackFerz-side] > 1 && pCnt[BlackKnight-side] > 0); // bare K can stalemate KHAA (!)
7451 if(nMine > 2) return TRUE; // if we don't have P, H or R, we must have CC
7452 if(nMine == 2 && pCnt[WhiteCannon+side] == 0) return TRUE; // We have at least one P, H or R
7453 // if we get here, we must have KC... or KP..., possibly with additional A, E or last-rank P
7454 if(stale) // we have at least one last-rank P plus perhaps C
7455 return majors // KPKX
7456 || pCnt[BlackFerz-side] && pCnt[BlackFerz-side] + pCnt[WhiteCannon+side] + stale > 2; // KPKAA, KPPKA and KCPKA
7458 return pCnt[WhiteFerz+side] // KCAK
7459 || pCnt[WhiteAlfil+side] && pCnt[BlackRook-side] + pCnt[BlackCannon-side] + pCnt[BlackFerz-side] // KCEKA, KCEKX (X!=H)
7460 || majors + (12*pCnt[BlackFerz-side] | 6*pCnt[BlackAlfil-side]) > 16; // KCKAA, KCKAX, KCKEEX, KCKEXX (XX!=HH), KCKXXX
7461 // TO DO: cases wih an unpromoted f-Pawn acting as platform for an opponent Cannon
7463 } else if(pCnt[WhiteKing] == 1 && pCnt[BlackKing] == 1) { // other variants with orthodox Kings
7464 int nBishops = pCnt[WhiteBishop+side] + pCnt[WhiteFerz+side];
7466 if(nMine == 1) return FALSE; // bare King
7467 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
7468 nMine += (nBishops > 0) - nBishops; // By now all Bishops (and Ferz) on like-colored squares, so count as one
7469 if(nMine > 2 && nMine != pCnt[WhiteAlfil+side] + 1) return TRUE; // At least two pieces, not all Alfils
7470 // by now we have King + 1 piece (or multiple Bishops on the same color)
7471 if(pCnt[WhiteKnight+side])
7472 return (pCnt[BlackKnight-side] + pCnt[BlackBishop-side] + pCnt[BlackMan-side] +
7473 pCnt[BlackWazir-side] + pCnt[BlackSilver-side] + bisColor // KNKN, KNKB, KNKF, KNKE, KNKW, KNKM, KNKS
7474 || nHis > 3); // be sure to cover suffocation mates in corner (e.g. KNKQCA)
7476 return (pCnt[BlackKnight-side]); // KBKN, KFKN
7477 if(pCnt[WhiteAlfil+side])
7478 return (nHis > 2); // Alfils can in general not reach a corner square, but there might be edge (suffocation) mates
7479 if(pCnt[WhiteWazir+side])
7480 return (pCnt[BlackKnight-side] + pCnt[BlackWazir-side] + pCnt[BlackAlfil-side]); // KWKN, KWKW, KWKE
7487 CompareWithRights (Board b1, Board b2)
7490 if(!CompareBoards(b1, b2)) return FALSE;
7491 if(b1[EP_STATUS] != b2[EP_STATUS]) return FALSE;
7492 /* compare castling rights */
7493 if( b1[CASTLING][2] != b2[CASTLING][2] && (b2[CASTLING][0] != NoRights || b2[CASTLING][1] != NoRights) )
7494 rights++; /* King lost rights, while rook still had them */
7495 if( b1[CASTLING][2] != NoRights ) { /* king has rights */
7496 if( b1[CASTLING][0] != b2[CASTLING][0] || b1[CASTLING][1] != b2[CASTLING][1] )
7497 rights++; /* but at least one rook lost them */
7499 if( b1[CASTLING][5] != b1[CASTLING][5] && (b2[CASTLING][3] != NoRights || b2[CASTLING][4] != NoRights) )
7501 if( b1[CASTLING][5] != NoRights ) {
7502 if( b1[CASTLING][3] != b2[CASTLING][3] || b1[CASTLING][4] != b2[CASTLING][4] )
7509 Adjudicate (ChessProgramState *cps)
7510 { // [HGM] some adjudications useful with buggy engines
7511 // [HGM] adjudicate: made into separate routine, which now can be called after every move
7512 // In any case it determnes if the game is a claimable draw (filling in EP_STATUS).
7513 // Actually ending the game is now based on the additional internal condition canAdjudicate.
7514 // Only when the game is ended, and the opponent is a computer, this opponent gets the move relayed.
7515 int k, count = 0; static int bare = 1;
7516 ChessProgramState *engineOpponent = (gameMode == TwoMachinesPlay ? cps->other : (cps ? NULL : &first));
7517 Boolean canAdjudicate = !appData.icsActive;
7519 // most tests only when we understand the game, i.e. legality-checking on
7520 if( appData.testLegality )
7521 { /* [HGM] Some more adjudications for obstinate engines */
7522 int nrW, nrB, bishopColor, staleW, staleB, nr[EmptySquare+1], i;
7523 static int moveCount = 6;
7525 char *reason = NULL;
7527 /* Count what is on board. */
7528 Count(boards[forwardMostMove], nr, &nrW, &nrB, &staleW, &staleB, &bishopColor);
7530 /* Some material-based adjudications that have to be made before stalemate test */
7531 if(gameInfo.variant == VariantAtomic && nr[WhiteKing] + nr[BlackKing] < 2) {
7532 // [HGM] atomic: stm must have lost his King on previous move, as destroying own K is illegal
7533 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE; // make claimable as if stm is checkmated
7534 if(canAdjudicate && appData.checkMates) {
7536 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets move
7537 GameEnds( WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins,
7538 "Xboard adjudication: King destroyed", GE_XBOARD );
7543 /* Bare King in Shatranj (loses) or Losers (wins) */
7544 if( nrW == 1 || nrB == 1) {
7545 if( gameInfo.variant == VariantLosers) { // [HGM] losers: bare King wins (stm must have it first)
7546 boards[forwardMostMove][EP_STATUS] = EP_WINS; // mark as win, so it becomes claimable
7547 if(canAdjudicate && appData.checkMates) {
7549 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets to see move
7550 GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
7551 "Xboard adjudication: Bare king", GE_XBOARD );
7555 if( gameInfo.variant == VariantShatranj && --bare < 0)
7557 boards[forwardMostMove][EP_STATUS] = EP_WINS; // make claimable as win for stm
7558 if(canAdjudicate && appData.checkMates) {
7559 /* but only adjudicate if adjudication enabled */
7561 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets move
7562 GameEnds( nrW > 1 ? WhiteWins : nrB > 1 ? BlackWins : GameIsDrawn,
7563 "Xboard adjudication: Bare king", GE_XBOARD );
7570 // don't wait for engine to announce game end if we can judge ourselves
7571 switch (MateTest(boards[forwardMostMove], PosFlags(forwardMostMove)) ) {
7573 if(gameInfo.variant == Variant3Check) { // [HGM] 3check: when in check, test if 3rd time
7574 int i, checkCnt = 0; // (should really be done by making nr of checks part of game state)
7575 for(i=forwardMostMove-2; i>=backwardMostMove; i-=2) {
7576 if(MateTest(boards[i], PosFlags(i)) == MT_CHECK)
7579 reason = "Xboard adjudication: 3rd check";
7580 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE;
7590 reason = "Xboard adjudication: Stalemate";
7591 if((signed char)boards[forwardMostMove][EP_STATUS] != EP_CHECKMATE) { // [HGM] don't touch win through baring or K-capt
7592 boards[forwardMostMove][EP_STATUS] = EP_STALEMATE; // default result for stalemate is draw
7593 if(gameInfo.variant == VariantLosers || gameInfo.variant == VariantGiveaway) // [HGM] losers:
7594 boards[forwardMostMove][EP_STATUS] = EP_WINS; // in these variants stalemated is always a win
7595 else if(gameInfo.variant == VariantSuicide) // in suicide it depends
7596 boards[forwardMostMove][EP_STATUS] = nrW == nrB ? EP_STALEMATE :
7597 ((nrW < nrB) != WhiteOnMove(forwardMostMove) ?
7598 EP_CHECKMATE : EP_WINS);
7599 else if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantXiangqi)
7600 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE; // and in these variants being stalemated loses
7604 reason = "Xboard adjudication: Checkmate";
7605 boards[forwardMostMove][EP_STATUS] = (gameInfo.variant == VariantLosers ? EP_WINS : EP_CHECKMATE);
7609 switch(i = (signed char)boards[forwardMostMove][EP_STATUS]) {
7611 result = GameIsDrawn; break;
7613 result = WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins; break;
7615 result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins; break;
7619 if(canAdjudicate && appData.checkMates && result) { // [HGM] mates: adjudicate finished games if requested
7621 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7622 GameEnds( result, reason, GE_XBOARD );
7626 /* Next absolutely insufficient mating material. */
7627 if(!MatingPotential(nr, WhitePawn, nrW, nrB, staleW, bishopColor) &&
7628 !MatingPotential(nr, BlackPawn, nrB, nrW, staleB, bishopColor))
7629 { /* includes KBK, KNK, KK of KBKB with like Bishops */
7631 /* always flag draws, for judging claims */
7632 boards[forwardMostMove][EP_STATUS] = EP_INSUF_DRAW;
7634 if(canAdjudicate && appData.materialDraws) {
7635 /* but only adjudicate them if adjudication enabled */
7636 if(engineOpponent) {
7637 SendToProgram("force\n", engineOpponent); // suppress reply
7638 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see last move */
7640 GameEnds( GameIsDrawn, "Xboard adjudication: Insufficient mating material", GE_XBOARD );
7645 /* Then some trivial draws (only adjudicate, cannot be claimed) */
7646 if(gameInfo.variant == VariantXiangqi ?
7647 SufficientDefence(nr, WhitePawn, nrW, nrB) && SufficientDefence(nr, BlackPawn, nrB, nrW)
7649 ( nr[WhiteRook] == 1 && nr[BlackRook] == 1 /* KRKR */
7650 || nr[WhiteQueen] && nr[BlackQueen]==1 /* KQKQ */
7651 || nr[WhiteKnight]==2 || nr[BlackKnight]==2 /* KNNK */
7652 || nr[WhiteKnight]+nr[WhiteBishop] == 1 && nr[BlackKnight]+nr[BlackBishop] == 1 /* KBKN, KBKB, KNKN */
7654 if(--moveCount < 0 && appData.trivialDraws && canAdjudicate)
7655 { /* if the first 3 moves do not show a tactical win, declare draw */
7656 if(engineOpponent) {
7657 SendToProgram("force\n", engineOpponent); // suppress reply
7658 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7660 GameEnds( GameIsDrawn, "Xboard adjudication: Trivial draw", GE_XBOARD );
7663 } else moveCount = 6;
7666 // Repetition draws and 50-move rule can be applied independently of legality testing
7668 /* Check for rep-draws */
7670 for(k = forwardMostMove-2;
7671 k>=backwardMostMove && k>=forwardMostMove-100 &&
7672 (signed char)boards[k][EP_STATUS] < EP_UNKNOWN &&
7673 (signed char)boards[k+2][EP_STATUS] <= EP_NONE && (signed char)boards[k+1][EP_STATUS] <= EP_NONE;
7676 if(CompareBoards(boards[k], boards[forwardMostMove])) {
7677 /* compare castling rights */
7678 if( boards[forwardMostMove][CASTLING][2] != boards[k][CASTLING][2] &&
7679 (boards[k][CASTLING][0] != NoRights || boards[k][CASTLING][1] != NoRights) )
7680 rights++; /* King lost rights, while rook still had them */
7681 if( boards[forwardMostMove][CASTLING][2] != NoRights ) { /* king has rights */
7682 if( boards[forwardMostMove][CASTLING][0] != boards[k][CASTLING][0] ||
7683 boards[forwardMostMove][CASTLING][1] != boards[k][CASTLING][1] )
7684 rights++; /* but at least one rook lost them */
7686 if( boards[forwardMostMove][CASTLING][5] != boards[k][CASTLING][5] &&
7687 (boards[k][CASTLING][3] != NoRights || boards[k][CASTLING][4] != NoRights) )
7689 if( boards[forwardMostMove][CASTLING][5] != NoRights ) {
7690 if( boards[forwardMostMove][CASTLING][3] != boards[k][CASTLING][3] ||
7691 boards[forwardMostMove][CASTLING][4] != boards[k][CASTLING][4] )
7694 if( rights == 0 && ++count > appData.drawRepeats-2 && canAdjudicate
7695 && appData.drawRepeats > 1) {
7696 /* adjudicate after user-specified nr of repeats */
7697 int result = GameIsDrawn;
7698 char *details = "XBoard adjudication: repetition draw";
7699 if(gameInfo.variant == VariantXiangqi && appData.testLegality) {
7700 // [HGM] xiangqi: check for forbidden perpetuals
7701 int m, ourPerpetual = 1, hisPerpetual = 1;
7702 for(m=forwardMostMove; m>k; m-=2) {
7703 if(MateTest(boards[m], PosFlags(m)) != MT_CHECK)
7704 ourPerpetual = 0; // the current mover did not always check
7705 if(MateTest(boards[m-1], PosFlags(m-1)) != MT_CHECK)
7706 hisPerpetual = 0; // the opponent did not always check
7708 if(appData.debugMode) fprintf(debugFP, "XQ perpetual test, our=%d, his=%d\n",
7709 ourPerpetual, hisPerpetual);
7710 if(ourPerpetual && !hisPerpetual) { // we are actively checking him: forfeit
7711 result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
7712 details = "Xboard adjudication: perpetual checking";
7714 if(hisPerpetual && !ourPerpetual) { // he is checking us, but did not repeat yet
7715 break; // (or we would have caught him before). Abort repetition-checking loop.
7717 // Now check for perpetual chases
7718 if(!ourPerpetual && !hisPerpetual) { // no perpetual check, test for chase
7719 hisPerpetual = PerpetualChase(k, forwardMostMove);
7720 ourPerpetual = PerpetualChase(k+1, forwardMostMove);
7721 if(ourPerpetual && !hisPerpetual) { // we are actively chasing him: forfeit
7722 static char resdet[MSG_SIZ];
7723 result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
7725 snprintf(resdet, MSG_SIZ, "Xboard adjudication: perpetual chasing of %c%c", ourPerpetual>>8, ourPerpetual&255);
7727 if(hisPerpetual && !ourPerpetual) // he is chasing us, but did not repeat yet
7728 break; // Abort repetition-checking loop.
7730 // if neither of us is checking or chasing all the time, or both are, it is draw
7732 if(engineOpponent) {
7733 SendToProgram("force\n", engineOpponent); // suppress reply
7734 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7736 GameEnds( result, details, GE_XBOARD );
7739 if( rights == 0 && count > 1 ) /* occurred 2 or more times before */
7740 boards[forwardMostMove][EP_STATUS] = EP_REP_DRAW;
7744 /* Now we test for 50-move draws. Determine ply count */
7745 count = forwardMostMove;
7746 /* look for last irreversble move */
7747 while( (signed char)boards[count][EP_STATUS] <= EP_NONE && count > backwardMostMove )
7749 /* if we hit starting position, add initial plies */
7750 if( count == backwardMostMove )
7751 count -= initialRulePlies;
7752 count = forwardMostMove - count;
7753 if(gameInfo.variant == VariantXiangqi && ( count >= 100 || count >= 2*appData.ruleMoves ) ) {
7754 // adjust reversible move counter for checks in Xiangqi
7755 int i = forwardMostMove - count, inCheck = 0, lastCheck;
7756 if(i < backwardMostMove) i = backwardMostMove;
7757 while(i <= forwardMostMove) {
7758 lastCheck = inCheck; // check evasion does not count
7759 inCheck = (MateTest(boards[i], PosFlags(i)) == MT_CHECK);
7760 if(inCheck || lastCheck) count--; // check does not count
7765 boards[forwardMostMove][EP_STATUS] = EP_RULE_DRAW;
7766 /* this is used to judge if draw claims are legal */
7767 if(canAdjudicate && appData.ruleMoves > 0 && count >= 2*appData.ruleMoves) {
7768 if(engineOpponent) {
7769 SendToProgram("force\n", engineOpponent); // suppress reply
7770 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7772 GameEnds( GameIsDrawn, "Xboard adjudication: 50-move rule", GE_XBOARD );
7776 /* if draw offer is pending, treat it as a draw claim
7777 * when draw condition present, to allow engines a way to
7778 * claim draws before making their move to avoid a race
7779 * condition occurring after their move
7781 if((gameMode == TwoMachinesPlay ? second.offeredDraw : userOfferedDraw) || first.offeredDraw ) {
7783 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_RULE_DRAW)
7784 p = "Draw claim: 50-move rule";
7785 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_REP_DRAW)
7786 p = "Draw claim: 3-fold repetition";
7787 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_INSUF_DRAW)
7788 p = "Draw claim: insufficient mating material";
7789 if( p != NULL && canAdjudicate) {
7790 if(engineOpponent) {
7791 SendToProgram("force\n", engineOpponent); // suppress reply
7792 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7794 GameEnds( GameIsDrawn, p, GE_XBOARD );
7799 if( canAdjudicate && appData.adjudicateDrawMoves > 0 && forwardMostMove > (2*appData.adjudicateDrawMoves) ) {
7800 if(engineOpponent) {
7801 SendToProgram("force\n", engineOpponent); // suppress reply
7802 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7804 GameEnds( GameIsDrawn, "Xboard adjudication: long game", GE_XBOARD );
7811 SendMoveToBookUser (int moveNr, ChessProgramState *cps, int initial)
7812 { // [HGM] book: this routine intercepts moves to simulate book replies
7813 char *bookHit = NULL;
7815 //first determine if the incoming move brings opponent into his book
7816 if(appData.usePolyglotBook && (cps == &first ? !appData.firstHasOwnBookUCI : !appData.secondHasOwnBookUCI))
7817 bookHit = ProbeBook(moveNr+1, appData.polyglotBook); // returns move
7818 if(appData.debugMode) fprintf(debugFP, "book hit = %s\n", bookHit ? bookHit : "(NULL)");
7819 if(bookHit != NULL && !cps->bookSuspend) {
7820 // make sure opponent is not going to reply after receiving move to book position
7821 SendToProgram("force\n", cps);
7822 cps->bookSuspend = TRUE; // flag indicating it has to be restarted
7824 if(!initial) SendMoveToProgram(moveNr, cps); // with hit on initial position there is no move
7825 // now arrange restart after book miss
7827 // after a book hit we never send 'go', and the code after the call to this routine
7828 // has '&& !bookHit' added to suppress potential sending there (based on 'firstMove').
7829 char buf[MSG_SIZ], *move = bookHit;
7831 int fromX, fromY, toX, toY;
7835 if (ParseOneMove(bookHit, forwardMostMove, &moveType,
7836 &fromX, &fromY, &toX, &toY, &promoChar)) {
7837 (void) CoordsToAlgebraic(boards[forwardMostMove],
7838 PosFlags(forwardMostMove),
7839 fromY, fromX, toY, toX, promoChar, move);
7841 if(appData.debugMode) fprintf(debugFP, "Book move could not be parsed\n");
7845 snprintf(buf, MSG_SIZ, "%s%s\n", (cps->useUsermove ? "usermove " : ""), move); // force book move into program supposed to play it
7846 SendToProgram(buf, cps);
7847 if(!initial) firstMove = FALSE; // normally we would clear the firstMove condition after return & sending 'go'
7848 } else if(initial) { // 'go' was needed irrespective of firstMove, and it has to be done in this routine
7849 SendToProgram("go\n", cps);
7850 cps->bookSuspend = FALSE; // after a 'go' we are never suspended
7851 } else { // 'go' might be sent based on 'firstMove' after this routine returns
7852 if(cps->bookSuspend && !firstMove) // 'go' needed, and it will not be done after we return
7853 SendToProgram("go\n", cps);
7854 cps->bookSuspend = FALSE; // anyhow, we will not be suspended after a miss
7856 return bookHit; // notify caller of hit, so it can take action to send move to opponent
7860 ChessProgramState *savedState;
7862 DeferredBookMove (void)
7864 if(savedState->lastPing != savedState->lastPong)
7865 ScheduleDelayedEvent(DeferredBookMove, 10);
7867 HandleMachineMove(savedMessage, savedState);
7870 static int savedWhitePlayer, savedBlackPlayer, pairingReceived;
7873 HandleMachineMove (char *message, ChessProgramState *cps)
7875 char machineMove[MSG_SIZ], buf1[MSG_SIZ*10], buf2[MSG_SIZ];
7876 char realname[MSG_SIZ];
7877 int fromX, fromY, toX, toY;
7884 if(cps == &pairing && sscanf(message, "%d-%d", &savedWhitePlayer, &savedBlackPlayer) == 2) {
7885 // [HGM] pairing: Mega-hack! Pairing engine also uses this routine (so it could give other WB commands).
7886 if(savedWhitePlayer == 0 || savedBlackPlayer == 0) {
7887 DisplayError(_("Invalid pairing from pairing engine"), 0);
7890 pairingReceived = 1;
7892 return; // Skim the pairing messages here.
7897 FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book hit
7899 * Kludge to ignore BEL characters
7901 while (*message == '\007') message++;
7904 * [HGM] engine debug message: ignore lines starting with '#' character
7906 if(cps->debug && *message == '#') return;
7909 * Look for book output
7911 if (cps == &first && bookRequested) {
7912 if (message[0] == '\t' || message[0] == ' ') {
7913 /* Part of the book output is here; append it */
7914 strcat(bookOutput, message);
7915 strcat(bookOutput, " \n");
7917 } else if (bookOutput[0] != NULLCHAR) {
7918 /* All of book output has arrived; display it */
7919 char *p = bookOutput;
7920 while (*p != NULLCHAR) {
7921 if (*p == '\t') *p = ' ';
7924 DisplayInformation(bookOutput);
7925 bookRequested = FALSE;
7926 /* Fall through to parse the current output */
7931 * Look for machine move.
7933 if ((sscanf(message, "%s %s %s", buf1, buf2, machineMove) == 3 && strcmp(buf2, "...") == 0) ||
7934 (sscanf(message, "%s %s", buf1, machineMove) == 2 && strcmp(buf1, "move") == 0))
7936 /* This method is only useful on engines that support ping */
7937 if (cps->lastPing != cps->lastPong) {
7938 if (gameMode == BeginningOfGame) {
7939 /* Extra move from before last new; ignore */
7940 if (appData.debugMode) {
7941 fprintf(debugFP, "Ignoring extra move from %s\n", cps->which);
7944 if (appData.debugMode) {
7945 fprintf(debugFP, "Undoing extra move from %s, gameMode %d\n",
7946 cps->which, gameMode);
7949 SendToProgram("undo\n", cps);
7955 case BeginningOfGame:
7956 /* Extra move from before last reset; ignore */
7957 if (appData.debugMode) {
7958 fprintf(debugFP, "Ignoring extra move from %s\n", cps->which);
7965 /* Extra move after we tried to stop. The mode test is
7966 not a reliable way of detecting this problem, but it's
7967 the best we can do on engines that don't support ping.
7969 if (appData.debugMode) {
7970 fprintf(debugFP, "Undoing extra move from %s, gameMode %d\n",
7971 cps->which, gameMode);
7973 SendToProgram("undo\n", cps);
7976 case MachinePlaysWhite:
7977 case IcsPlayingWhite:
7978 machineWhite = TRUE;
7981 case MachinePlaysBlack:
7982 case IcsPlayingBlack:
7983 machineWhite = FALSE;
7986 case TwoMachinesPlay:
7987 machineWhite = (cps->twoMachinesColor[0] == 'w');
7990 if (WhiteOnMove(forwardMostMove) != machineWhite) {
7991 if (appData.debugMode) {
7993 "Ignoring move out of turn by %s, gameMode %d"
7994 ", forwardMost %d\n",
7995 cps->which, gameMode, forwardMostMove);
8000 if(cps->alphaRank) AlphaRank(machineMove, 4);
8001 if (!ParseOneMove(machineMove, forwardMostMove, &moveType,
8002 &fromX, &fromY, &toX, &toY, &promoChar)) {
8003 /* Machine move could not be parsed; ignore it. */
8004 snprintf(buf1, MSG_SIZ*10, _("Illegal move \"%s\" from %s machine"),
8005 machineMove, _(cps->which));
8006 DisplayError(buf1, 0);
8007 snprintf(buf1, MSG_SIZ*10, "Xboard: Forfeit due to invalid move: %s (%c%c%c%c) res=%d",
8008 machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, moveType);
8009 if (gameMode == TwoMachinesPlay) {
8010 GameEnds(machineWhite ? BlackWins : WhiteWins,
8016 /* [HGM] Apparently legal, but so far only tested with EP_UNKOWN */
8017 /* So we have to redo legality test with true e.p. status here, */
8018 /* to make sure an illegal e.p. capture does not slip through, */
8019 /* to cause a forfeit on a justified illegal-move complaint */
8020 /* of the opponent. */
8021 if( gameMode==TwoMachinesPlay && appData.testLegality ) {
8023 moveType = LegalityTest(boards[forwardMostMove], PosFlags(forwardMostMove),
8024 fromY, fromX, toY, toX, promoChar);
8025 if(moveType == IllegalMove) {
8026 snprintf(buf1, MSG_SIZ*10, "Xboard: Forfeit due to illegal move: %s (%c%c%c%c)%c",
8027 machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, 0);
8028 GameEnds(machineWhite ? BlackWins : WhiteWins,
8031 } else if(gameInfo.variant != VariantFischeRandom && gameInfo.variant != VariantCapaRandom)
8032 /* [HGM] Kludge to handle engines that send FRC-style castling
8033 when they shouldn't (like TSCP-Gothic) */
8035 case WhiteASideCastleFR:
8036 case BlackASideCastleFR:
8038 currentMoveString[2]++;
8040 case WhiteHSideCastleFR:
8041 case BlackHSideCastleFR:
8043 currentMoveString[2]--;
8045 default: ; // nothing to do, but suppresses warning of pedantic compilers
8048 hintRequested = FALSE;
8049 lastHint[0] = NULLCHAR;
8050 bookRequested = FALSE;
8051 /* Program may be pondering now */
8052 cps->maybeThinking = TRUE;
8053 if (cps->sendTime == 2) cps->sendTime = 1;
8054 if (cps->offeredDraw) cps->offeredDraw--;
8056 /* [AS] Save move info*/
8057 pvInfoList[ forwardMostMove ].score = programStats.score;
8058 pvInfoList[ forwardMostMove ].depth = programStats.depth;
8059 pvInfoList[ forwardMostMove ].time = programStats.time; // [HGM] PGNtime: take time from engine stats
8061 MakeMove(fromX, fromY, toX, toY, promoChar);/*updates forwardMostMove*/
8063 /* [AS] Adjudicate game if needed (note: remember that forwardMostMove now points past the last move) */
8064 if( gameMode == TwoMachinesPlay && adjudicateLossThreshold != 0 && forwardMostMove >= adjudicateLossPlies ) {
8067 while( count < adjudicateLossPlies ) {
8068 int score = pvInfoList[ forwardMostMove - count - 1 ].score;
8071 score = -score; /* Flip score for winning side */
8074 if( score > adjudicateLossThreshold ) {
8081 if( count >= adjudicateLossPlies ) {
8082 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
8084 GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
8085 "Xboard adjudication",
8092 if(Adjudicate(cps)) {
8093 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
8094 return; // [HGM] adjudicate: for all automatic game ends
8098 if ((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack) &&
8100 if(cps->offeredDraw && (signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
8101 SendToICS(ics_prefix); // [HGM] drawclaim: send caim and move on one line for FICS
8103 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
8105 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
8107 if(appData.autoKibitz && !appData.icsEngineAnalyze ) { /* [HGM] kibitz: send most-recent PV info to ICS */
8108 char buf[3*MSG_SIZ];
8110 snprintf(buf, 3*MSG_SIZ, "kibitz !!! %+.2f/%d (%.2f sec, %u nodes, %.0f knps) PV=%s\n",
8111 programStats.score / 100.,
8113 programStats.time / 100.,
8114 (unsigned int)programStats.nodes,
8115 (unsigned int)programStats.nodes / (10*abs(programStats.time) + 1.),
8116 programStats.movelist);
8118 if(appData.debugMode) fprintf(debugFP, "nodes = %d, %lld\n", (int) programStats.nodes, programStats.nodes);
8123 /* [AS] Clear stats for next move */
8124 ClearProgramStats();
8125 thinkOutput[0] = NULLCHAR;
8126 hiddenThinkOutputState = 0;
8129 if (gameMode == TwoMachinesPlay) {
8130 /* [HGM] relaying draw offers moved to after reception of move */
8131 /* and interpreting offer as claim if it brings draw condition */
8132 if (cps->offeredDraw == 1 && cps->other->sendDrawOffers) {
8133 SendToProgram("draw\n", cps->other);
8135 if (cps->other->sendTime) {
8136 SendTimeRemaining(cps->other,
8137 cps->other->twoMachinesColor[0] == 'w');
8139 bookHit = SendMoveToBookUser(forwardMostMove-1, cps->other, FALSE);
8140 if (firstMove && !bookHit) {
8142 if (cps->other->useColors) {
8143 SendToProgram(cps->other->twoMachinesColor, cps->other);
8145 SendToProgram("go\n", cps->other);
8147 cps->other->maybeThinking = TRUE;
8150 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
8152 if (!pausing && appData.ringBellAfterMoves) {
8157 * Reenable menu items that were disabled while
8158 * machine was thinking
8160 if (gameMode != TwoMachinesPlay)
8161 SetUserThinkingEnables();
8163 // [HGM] book: after book hit opponent has received move and is now in force mode
8164 // force the book reply into it, and then fake that it outputted this move by jumping
8165 // back to the beginning of HandleMachineMove, with cps toggled and message set to this move
8167 static char bookMove[MSG_SIZ]; // a bit generous?
8169 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
8170 strcat(bookMove, bookHit);
8173 programStats.nodes = programStats.depth = programStats.time =
8174 programStats.score = programStats.got_only_move = 0;
8175 sprintf(programStats.movelist, "%s (xbook)", bookHit);
8177 if(cps->lastPing != cps->lastPong) {
8178 savedMessage = message; // args for deferred call
8180 ScheduleDelayedEvent(DeferredBookMove, 10);
8189 /* Set special modes for chess engines. Later something general
8190 * could be added here; for now there is just one kludge feature,
8191 * needed because Crafty 15.10 and earlier don't ignore SIGINT
8192 * when "xboard" is given as an interactive command.
8194 if (strncmp(message, "kibitz Hello from Crafty", 24) == 0) {
8195 cps->useSigint = FALSE;
8196 cps->useSigterm = FALSE;
8198 if (strncmp(message, "feature ", 8) == 0) { // [HGM] moved forward to pre-empt non-compliant commands
8199 ParseFeatures(message+8, cps);
8200 return; // [HGM] This return was missing, causing option features to be recognized as non-compliant commands!
8203 if ((!appData.testLegality || gameInfo.variant == VariantFairy) &&
8204 !strncmp(message, "setup ", 6)) { // [HGM] allow first engine to define opening position
8205 int dummy, s=6; char buf[MSG_SIZ];
8206 if(appData.icsActive || forwardMostMove != 0 || cps != &first) return;
8207 if(sscanf(message, "setup (%s", buf) == 1) s = 8 + strlen(buf), buf[s-9] = NULLCHAR, SetCharTable(pieceToChar, buf);
8208 if(startedFromSetupPosition) return;
8209 ParseFEN(boards[0], &dummy, message+s);
8210 DrawPosition(TRUE, boards[0]);
8211 startedFromSetupPosition = TRUE;
8214 /* [HGM] Allow engine to set up a position. Don't ask me why one would
8215 * want this, I was asked to put it in, and obliged.
8217 if (!strncmp(message, "setboard ", 9)) {
8218 Board initial_position;
8220 GameEnds(GameUnfinished, "Engine aborts game", GE_XBOARD);
8222 if (!ParseFEN(initial_position, &blackPlaysFirst, message + 9)) {
8223 DisplayError(_("Bad FEN received from engine"), 0);
8227 CopyBoard(boards[0], initial_position);
8228 initialRulePlies = FENrulePlies;
8229 if(blackPlaysFirst) gameMode = MachinePlaysWhite;
8230 else gameMode = MachinePlaysBlack;
8231 DrawPosition(FALSE, boards[currentMove]);
8237 * Look for communication commands
8239 if (!strncmp(message, "telluser ", 9)) {
8240 if(message[9] == '\\' && message[10] == '\\')
8241 EscapeExpand(message+9, message+11); // [HGM] esc: allow escape sequences in popup box
8243 DisplayNote(message + 9);
8246 if (!strncmp(message, "tellusererror ", 14)) {
8248 if(message[14] == '\\' && message[15] == '\\')
8249 EscapeExpand(message+14, message+16); // [HGM] esc: allow escape sequences in popup box
8251 DisplayError(message + 14, 0);
8254 if (!strncmp(message, "tellopponent ", 13)) {
8255 if (appData.icsActive) {
8257 snprintf(buf1, sizeof(buf1), "%ssay %s\n", ics_prefix, message + 13);
8261 DisplayNote(message + 13);
8265 if (!strncmp(message, "tellothers ", 11)) {
8266 if (appData.icsActive) {
8268 snprintf(buf1, sizeof(buf1), "%swhisper %s\n", ics_prefix, message + 11);
8274 if (!strncmp(message, "tellall ", 8)) {
8275 if (appData.icsActive) {
8277 snprintf(buf1, sizeof(buf1), "%skibitz %s\n", ics_prefix, message + 8);
8281 DisplayNote(message + 8);
8285 if (strncmp(message, "warning", 7) == 0) {
8286 /* Undocumented feature, use tellusererror in new code */
8287 DisplayError(message, 0);
8290 if (sscanf(message, "askuser %s %[^\n]", buf1, buf2) == 2) {
8291 safeStrCpy(realname, cps->tidy, sizeof(realname)/sizeof(realname[0]));
8292 strcat(realname, " query");
8293 AskQuestion(realname, buf2, buf1, cps->pr);
8296 /* Commands from the engine directly to ICS. We don't allow these to be
8297 * sent until we are logged on. Crafty kibitzes have been known to
8298 * interfere with the login process.
8301 if (!strncmp(message, "tellics ", 8)) {
8302 SendToICS(message + 8);
8306 if (!strncmp(message, "tellicsnoalias ", 15)) {
8307 SendToICS(ics_prefix);
8308 SendToICS(message + 15);
8312 /* The following are for backward compatibility only */
8313 if (!strncmp(message,"whisper",7) || !strncmp(message,"kibitz",6) ||
8314 !strncmp(message,"draw",4) || !strncmp(message,"tell",3)) {
8315 SendToICS(ics_prefix);
8321 if (sscanf(message, "pong %d", &cps->lastPong) == 1) {
8325 * If the move is illegal, cancel it and redraw the board.
8326 * Also deal with other error cases. Matching is rather loose
8327 * here to accommodate engines written before the spec.
8329 if (strncmp(message + 1, "llegal move", 11) == 0 ||
8330 strncmp(message, "Error", 5) == 0) {
8331 if (StrStr(message, "name") ||
8332 StrStr(message, "rating") || StrStr(message, "?") ||
8333 StrStr(message, "result") || StrStr(message, "board") ||
8334 StrStr(message, "bk") || StrStr(message, "computer") ||
8335 StrStr(message, "variant") || StrStr(message, "hint") ||
8336 StrStr(message, "random") || StrStr(message, "depth") ||
8337 StrStr(message, "accepted")) {
8340 if (StrStr(message, "protover")) {
8341 /* Program is responding to input, so it's apparently done
8342 initializing, and this error message indicates it is
8343 protocol version 1. So we don't need to wait any longer
8344 for it to initialize and send feature commands. */
8345 FeatureDone(cps, 1);
8346 cps->protocolVersion = 1;
8349 cps->maybeThinking = FALSE;
8351 if (StrStr(message, "draw")) {
8352 /* Program doesn't have "draw" command */
8353 cps->sendDrawOffers = 0;
8356 if (cps->sendTime != 1 &&
8357 (StrStr(message, "time") || StrStr(message, "otim"))) {
8358 /* Program apparently doesn't have "time" or "otim" command */
8362 if (StrStr(message, "analyze")) {
8363 cps->analysisSupport = FALSE;
8364 cps->analyzing = FALSE;
8365 // Reset(FALSE, TRUE); // [HGM] this caused discrepancy between display and internal state!
8366 EditGameEvent(); // [HGM] try to preserve loaded game
8367 snprintf(buf2,MSG_SIZ, _("%s does not support analysis"), cps->tidy);
8368 DisplayError(buf2, 0);
8371 if (StrStr(message, "(no matching move)st")) {
8372 /* Special kludge for GNU Chess 4 only */
8373 cps->stKludge = TRUE;
8374 SendTimeControl(cps, movesPerSession, timeControl,
8375 timeIncrement, appData.searchDepth,
8379 if (StrStr(message, "(no matching move)sd")) {
8380 /* Special kludge for GNU Chess 4 only */
8381 cps->sdKludge = TRUE;
8382 SendTimeControl(cps, movesPerSession, timeControl,
8383 timeIncrement, appData.searchDepth,
8387 if (!StrStr(message, "llegal")) {
8390 if (gameMode == BeginningOfGame || gameMode == EndOfGame ||
8391 gameMode == IcsIdle) return;
8392 if (forwardMostMove <= backwardMostMove) return;
8393 if (pausing) PauseEvent();
8394 if(appData.forceIllegal) {
8395 // [HGM] illegal: machine refused move; force position after move into it
8396 SendToProgram("force\n", cps);
8397 if(!cps->useSetboard) { // hideous kludge on kludge, because SendBoard sucks.
8398 // we have a real problem now, as SendBoard will use the a2a3 kludge
8399 // when black is to move, while there might be nothing on a2 or black
8400 // might already have the move. So send the board as if white has the move.
8401 // But first we must change the stm of the engine, as it refused the last move
8402 SendBoard(cps, 0); // always kludgeless, as white is to move on boards[0]
8403 if(WhiteOnMove(forwardMostMove)) {
8404 SendToProgram("a7a6\n", cps); // for the engine black still had the move
8405 SendBoard(cps, forwardMostMove); // kludgeless board
8407 SendToProgram("a2a3\n", cps); // for the engine white still had the move
8408 CopyBoard(boards[forwardMostMove+1], boards[forwardMostMove]);
8409 SendBoard(cps, forwardMostMove+1); // kludgeless board
8411 } else SendBoard(cps, forwardMostMove); // FEN case, also sets stm properly
8412 if(gameMode == MachinePlaysWhite || gameMode == MachinePlaysBlack ||
8413 gameMode == TwoMachinesPlay)
8414 SendToProgram("go\n", cps);
8417 if (gameMode == PlayFromGameFile) {
8418 /* Stop reading this game file */
8419 gameMode = EditGame;
8422 /* [HGM] illegal-move claim should forfeit game when Xboard */
8423 /* only passes fully legal moves */
8424 if( appData.testLegality && gameMode == TwoMachinesPlay ) {
8425 GameEnds( cps->twoMachinesColor[0] == 'w' ? BlackWins : WhiteWins,
8426 "False illegal-move claim", GE_XBOARD );
8427 return; // do not take back move we tested as valid
8429 currentMove = forwardMostMove-1;
8430 DisplayMove(currentMove-1); /* before DisplayMoveError */
8431 SwitchClocks(forwardMostMove-1); // [HGM] race
8432 DisplayBothClocks();
8433 snprintf(buf1, 10*MSG_SIZ, _("Illegal move \"%s\" (rejected by %s chess program)"),
8434 parseList[currentMove], _(cps->which));
8435 DisplayMoveError(buf1);
8436 DrawPosition(FALSE, boards[currentMove]);
8438 SetUserThinkingEnables();
8441 if (strncmp(message, "time", 4) == 0 && StrStr(message, "Illegal")) {
8442 /* Program has a broken "time" command that
8443 outputs a string not ending in newline.
8449 * If chess program startup fails, exit with an error message.
8450 * Attempts to recover here are futile. [HGM] Well, we try anyway
8452 if ((StrStr(message, "unknown host") != NULL)
8453 || (StrStr(message, "No remote directory") != NULL)
8454 || (StrStr(message, "not found") != NULL)
8455 || (StrStr(message, "No such file") != NULL)
8456 || (StrStr(message, "can't alloc") != NULL)
8457 || (StrStr(message, "Permission denied") != NULL)) {
8459 cps->maybeThinking = FALSE;
8460 snprintf(buf1, sizeof(buf1), _("Failed to start %s chess program %s on %s: %s\n"),
8461 _(cps->which), cps->program, cps->host, message);
8462 RemoveInputSource(cps->isr);
8463 if(appData.icsActive) DisplayFatalError(buf1, 0, 1); else {
8465 DestroyChildProcess(cps->pr, 9 ); // just to be sure
8468 appData.noChessProgram = TRUE;
8469 gameMode = MachinePlaysBlack; ModeHighlight(); // kludge to unmark Machine Black menu
8470 gameMode = BeginningOfGame; ModeHighlight();
8473 if(GetDelayedEvent()) CancelDelayedEvent(), ThawUI(); // [HGM] cancel remaining loading effort scheduled after feature timeout
8474 DisplayMessage("", ""); // erase waiting message
8475 DisplayError(buf1, 0);
8481 * Look for hint output
8483 if (sscanf(message, "Hint: %s", buf1) == 1) {
8484 if (cps == &first && hintRequested) {
8485 hintRequested = FALSE;
8486 if (ParseOneMove(buf1, forwardMostMove, &moveType,
8487 &fromX, &fromY, &toX, &toY, &promoChar)) {
8488 (void) CoordsToAlgebraic(boards[forwardMostMove],
8489 PosFlags(forwardMostMove),
8490 fromY, fromX, toY, toX, promoChar, buf1);
8491 snprintf(buf2, sizeof(buf2), _("Hint: %s"), buf1);
8492 DisplayInformation(buf2);
8494 /* Hint move could not be parsed!? */
8495 snprintf(buf2, sizeof(buf2),
8496 _("Illegal hint move \"%s\"\nfrom %s chess program"),
8497 buf1, _(cps->which));
8498 DisplayError(buf2, 0);
8501 safeStrCpy(lastHint, buf1, sizeof(lastHint)/sizeof(lastHint[0]));
8507 * Ignore other messages if game is not in progress
8509 if (gameMode == BeginningOfGame || gameMode == EndOfGame ||
8510 gameMode == IcsIdle || cps->lastPing != cps->lastPong) return;
8513 * look for win, lose, draw, or draw offer
8515 if (strncmp(message, "1-0", 3) == 0) {
8516 char *p, *q, *r = "";
8517 p = strchr(message, '{');
8525 GameEnds(WhiteWins, r, GE_ENGINE1 + (cps != &first)); /* [HGM] pass claimer indication for claim test */
8527 } else if (strncmp(message, "0-1", 3) == 0) {
8528 char *p, *q, *r = "";
8529 p = strchr(message, '{');
8537 /* Kludge for Arasan 4.1 bug */
8538 if (strcmp(r, "Black resigns") == 0) {
8539 GameEnds(WhiteWins, r, GE_ENGINE1 + (cps != &first));
8542 GameEnds(BlackWins, r, GE_ENGINE1 + (cps != &first));
8544 } else if (strncmp(message, "1/2", 3) == 0) {
8545 char *p, *q, *r = "";
8546 p = strchr(message, '{');
8555 GameEnds(GameIsDrawn, r, GE_ENGINE1 + (cps != &first));
8558 } else if (strncmp(message, "White resign", 12) == 0) {
8559 GameEnds(BlackWins, "White resigns", GE_ENGINE1 + (cps != &first));
8561 } else if (strncmp(message, "Black resign", 12) == 0) {
8562 GameEnds(WhiteWins, "Black resigns", GE_ENGINE1 + (cps != &first));
8564 } else if (strncmp(message, "White matches", 13) == 0 ||
8565 strncmp(message, "Black matches", 13) == 0 ) {
8566 /* [HGM] ignore GNUShogi noises */
8568 } else if (strncmp(message, "White", 5) == 0 &&
8569 message[5] != '(' &&
8570 StrStr(message, "Black") == NULL) {
8571 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
8573 } else if (strncmp(message, "Black", 5) == 0 &&
8574 message[5] != '(') {
8575 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
8577 } else if (strcmp(message, "resign") == 0 ||
8578 strcmp(message, "computer resigns") == 0) {
8580 case MachinePlaysBlack:
8581 case IcsPlayingBlack:
8582 GameEnds(WhiteWins, "Black resigns", GE_ENGINE);
8584 case MachinePlaysWhite:
8585 case IcsPlayingWhite:
8586 GameEnds(BlackWins, "White resigns", GE_ENGINE);
8588 case TwoMachinesPlay:
8589 if (cps->twoMachinesColor[0] == 'w')
8590 GameEnds(BlackWins, "White resigns", GE_ENGINE1 + (cps != &first));
8592 GameEnds(WhiteWins, "Black resigns", GE_ENGINE1 + (cps != &first));
8599 } else if (strncmp(message, "opponent mates", 14) == 0) {
8601 case MachinePlaysBlack:
8602 case IcsPlayingBlack:
8603 GameEnds(WhiteWins, "White mates", GE_ENGINE);
8605 case MachinePlaysWhite:
8606 case IcsPlayingWhite:
8607 GameEnds(BlackWins, "Black mates", GE_ENGINE);
8609 case TwoMachinesPlay:
8610 if (cps->twoMachinesColor[0] == 'w')
8611 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
8613 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
8620 } else if (strncmp(message, "computer mates", 14) == 0) {
8622 case MachinePlaysBlack:
8623 case IcsPlayingBlack:
8624 GameEnds(BlackWins, "Black mates", GE_ENGINE1);
8626 case MachinePlaysWhite:
8627 case IcsPlayingWhite:
8628 GameEnds(WhiteWins, "White mates", GE_ENGINE);
8630 case TwoMachinesPlay:
8631 if (cps->twoMachinesColor[0] == 'w')
8632 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
8634 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
8641 } else if (strncmp(message, "checkmate", 9) == 0) {
8642 if (WhiteOnMove(forwardMostMove)) {
8643 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
8645 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
8648 } else if (strstr(message, "Draw") != NULL ||
8649 strstr(message, "game is a draw") != NULL) {
8650 GameEnds(GameIsDrawn, "Draw", GE_ENGINE1 + (cps != &first));
8652 } else if (strstr(message, "offer") != NULL &&
8653 strstr(message, "draw") != NULL) {
8655 if (appData.zippyPlay && first.initDone) {
8656 /* Relay offer to ICS */
8657 SendToICS(ics_prefix);
8658 SendToICS("draw\n");
8661 cps->offeredDraw = 2; /* valid until this engine moves twice */
8662 if (gameMode == TwoMachinesPlay) {
8663 if (cps->other->offeredDraw) {
8664 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
8665 /* [HGM] in two-machine mode we delay relaying draw offer */
8666 /* until after we also have move, to see if it is really claim */
8668 } else if (gameMode == MachinePlaysWhite ||
8669 gameMode == MachinePlaysBlack) {
8670 if (userOfferedDraw) {
8671 DisplayInformation(_("Machine accepts your draw offer"));
8672 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
8674 DisplayInformation(_("Machine offers a draw\nSelect Action / Draw to agree"));
8681 * Look for thinking output
8683 if ( appData.showThinking // [HGM] thinking: test all options that cause this output
8684 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp()
8686 int plylev, mvleft, mvtot, curscore, time;
8687 char mvname[MOVE_LEN];
8691 int prefixHint = FALSE;
8692 mvname[0] = NULLCHAR;
8695 case MachinePlaysBlack:
8696 case IcsPlayingBlack:
8697 if (WhiteOnMove(forwardMostMove)) prefixHint = TRUE;
8699 case MachinePlaysWhite:
8700 case IcsPlayingWhite:
8701 if (!WhiteOnMove(forwardMostMove)) prefixHint = TRUE;
8706 case IcsObserving: /* [DM] icsEngineAnalyze */
8707 if (!appData.icsEngineAnalyze) ignore = TRUE;
8709 case TwoMachinesPlay:
8710 if ((cps->twoMachinesColor[0] == 'w') != WhiteOnMove(forwardMostMove)) {
8720 ChessProgramStats tempStats = programStats; // [HGM] info: filter out info lines
8722 if (sscanf(message, "%d%c %d %d " u64Display " %[^\n]\n",
8723 &plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5) {
8725 if (plyext != ' ' && plyext != '\t') {
8729 /* [AS] Negate score if machine is playing black and reporting absolute scores */
8730 if( cps->scoreIsAbsolute &&
8731 ( gameMode == MachinePlaysBlack ||
8732 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b' ||
8733 gameMode == IcsPlayingBlack || // [HGM] also add other situations where engine should report black POV
8734 (gameMode == AnalyzeMode || gameMode == AnalyzeFile || gameMode == IcsObserving && appData.icsEngineAnalyze) &&
8735 !WhiteOnMove(currentMove)
8738 curscore = -curscore;
8741 if(appData.pvSAN[cps==&second]) pv = PvToSAN(buf1);
8743 if(serverMoves && (time > 100 || time == 0 && plylev > 7)) {
8746 snprintf(buf, MSG_SIZ, "%s", appData.serverMovesName);
8747 buf[strlen(buf)-1] = gameMode == MachinePlaysWhite ? 'w' :
8748 gameMode == MachinePlaysBlack ? 'b' : cps->twoMachinesColor[0];
8749 if(appData.debugMode) fprintf(debugFP, "write PV on file '%s'\n", buf);
8750 if(f = fopen(buf, "w")) { // export PV to applicable PV file
8751 fprintf(f, "%5.2f/%-2d %s", curscore/100., plylev, pv);
8753 } else DisplayError(_("failed writing PV"), 0);
8756 tempStats.depth = plylev;
8757 tempStats.nodes = nodes;
8758 tempStats.time = time;
8759 tempStats.score = curscore;
8760 tempStats.got_only_move = 0;
8762 if(cps->nps >= 0) { /* [HGM] nps: use engine nodes or time to decrement clock */
8765 if(cps->nps == 0) ticklen = 10*time; // use engine reported time
8766 else ticklen = (1000. * u64ToDouble(nodes)) / cps->nps; // convert node count to time
8767 if(WhiteOnMove(forwardMostMove) && (gameMode == MachinePlaysWhite ||
8768 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'w'))
8769 whiteTimeRemaining = timeRemaining[0][forwardMostMove] - ticklen;
8770 if(!WhiteOnMove(forwardMostMove) && (gameMode == MachinePlaysBlack ||
8771 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b'))
8772 blackTimeRemaining = timeRemaining[1][forwardMostMove] - ticklen;
8775 /* Buffer overflow protection */
8776 if (pv[0] != NULLCHAR) {
8777 if (strlen(pv) >= sizeof(tempStats.movelist)
8778 && appData.debugMode) {
8780 "PV is too long; using the first %u bytes.\n",
8781 (unsigned) sizeof(tempStats.movelist) - 1);
8784 safeStrCpy( tempStats.movelist, pv, sizeof(tempStats.movelist)/sizeof(tempStats.movelist[0]) );
8786 sprintf(tempStats.movelist, " no PV\n");
8789 if (tempStats.seen_stat) {
8790 tempStats.ok_to_send = 1;
8793 if (strchr(tempStats.movelist, '(') != NULL) {
8794 tempStats.line_is_book = 1;
8795 tempStats.nr_moves = 0;
8796 tempStats.moves_left = 0;
8798 tempStats.line_is_book = 0;
8801 if(tempStats.score != 0 || tempStats.nodes != 0 || tempStats.time != 0)
8802 programStats = tempStats; // [HGM] info: only set stats if genuine PV and not an info line
8804 SendProgramStatsToFrontend( cps, &tempStats );
8807 [AS] Protect the thinkOutput buffer from overflow... this
8808 is only useful if buf1 hasn't overflowed first!
8810 snprintf(thinkOutput, sizeof(thinkOutput)/sizeof(thinkOutput[0]), "[%d]%c%+.2f %s%s",
8812 (gameMode == TwoMachinesPlay ?
8813 ToUpper(cps->twoMachinesColor[0]) : ' '),
8814 ((double) curscore) / 100.0,
8815 prefixHint ? lastHint : "",
8816 prefixHint ? " " : "" );
8818 if( buf1[0] != NULLCHAR ) {
8819 unsigned max_len = sizeof(thinkOutput) - strlen(thinkOutput) - 1;
8821 if( strlen(pv) > max_len ) {
8822 if( appData.debugMode) {
8823 fprintf(debugFP,"PV is too long for thinkOutput, truncating.\n");
8825 pv[max_len+1] = '\0';
8828 strcat( thinkOutput, pv);
8831 if (currentMove == forwardMostMove || gameMode == AnalyzeMode
8832 || gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
8833 DisplayMove(currentMove - 1);
8837 } else if ((p=StrStr(message, "(only move)")) != NULL) {
8838 /* crafty (9.25+) says "(only move) <move>"
8839 * if there is only 1 legal move
8841 sscanf(p, "(only move) %s", buf1);
8842 snprintf(thinkOutput, sizeof(thinkOutput)/sizeof(thinkOutput[0]), "%s (only move)", buf1);
8843 sprintf(programStats.movelist, "%s (only move)", buf1);
8844 programStats.depth = 1;
8845 programStats.nr_moves = 1;
8846 programStats.moves_left = 1;
8847 programStats.nodes = 1;
8848 programStats.time = 1;
8849 programStats.got_only_move = 1;
8851 /* Not really, but we also use this member to
8852 mean "line isn't going to change" (Crafty
8853 isn't searching, so stats won't change) */
8854 programStats.line_is_book = 1;
8856 SendProgramStatsToFrontend( cps, &programStats );
8858 if (currentMove == forwardMostMove || gameMode==AnalyzeMode ||
8859 gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
8860 DisplayMove(currentMove - 1);
8863 } else if (sscanf(message,"stat01: %d " u64Display " %d %d %d %s",
8864 &time, &nodes, &plylev, &mvleft,
8865 &mvtot, mvname) >= 5) {
8866 /* The stat01: line is from Crafty (9.29+) in response
8867 to the "." command */
8868 programStats.seen_stat = 1;
8869 cps->maybeThinking = TRUE;
8871 if (programStats.got_only_move || !appData.periodicUpdates)
8874 programStats.depth = plylev;
8875 programStats.time = time;
8876 programStats.nodes = nodes;
8877 programStats.moves_left = mvleft;
8878 programStats.nr_moves = mvtot;
8879 safeStrCpy(programStats.move_name, mvname, sizeof(programStats.move_name)/sizeof(programStats.move_name[0]));
8880 programStats.ok_to_send = 1;
8881 programStats.movelist[0] = '\0';
8883 SendProgramStatsToFrontend( cps, &programStats );
8887 } else if (strncmp(message,"++",2) == 0) {
8888 /* Crafty 9.29+ outputs this */
8889 programStats.got_fail = 2;
8892 } else if (strncmp(message,"--",2) == 0) {
8893 /* Crafty 9.29+ outputs this */
8894 programStats.got_fail = 1;
8897 } else if (thinkOutput[0] != NULLCHAR &&
8898 strncmp(message, " ", 4) == 0) {
8899 unsigned message_len;
8902 while (*p && *p == ' ') p++;
8904 message_len = strlen( p );
8906 /* [AS] Avoid buffer overflow */
8907 if( sizeof(thinkOutput) - strlen(thinkOutput) - 1 > message_len ) {
8908 strcat(thinkOutput, " ");
8909 strcat(thinkOutput, p);
8912 if( sizeof(programStats.movelist) - strlen(programStats.movelist) - 1 > message_len ) {
8913 strcat(programStats.movelist, " ");
8914 strcat(programStats.movelist, p);
8917 if (currentMove == forwardMostMove || gameMode==AnalyzeMode ||
8918 gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
8919 DisplayMove(currentMove - 1);
8927 if (sscanf(message, "%d%c %d %d " u64Display " %[^\n]\n",
8928 &plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5)
8930 ChessProgramStats cpstats;
8932 if (plyext != ' ' && plyext != '\t') {
8936 /* [AS] Negate score if machine is playing black and reporting absolute scores */
8937 if( cps->scoreIsAbsolute && ((gameMode == MachinePlaysBlack) || (gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b')) ) {
8938 curscore = -curscore;
8941 cpstats.depth = plylev;
8942 cpstats.nodes = nodes;
8943 cpstats.time = time;
8944 cpstats.score = curscore;
8945 cpstats.got_only_move = 0;
8946 cpstats.movelist[0] = '\0';
8948 if (buf1[0] != NULLCHAR) {
8949 safeStrCpy( cpstats.movelist, buf1, sizeof(cpstats.movelist)/sizeof(cpstats.movelist[0]) );
8952 cpstats.ok_to_send = 0;
8953 cpstats.line_is_book = 0;
8954 cpstats.nr_moves = 0;
8955 cpstats.moves_left = 0;
8957 SendProgramStatsToFrontend( cps, &cpstats );
8964 /* Parse a game score from the character string "game", and
8965 record it as the history of the current game. The game
8966 score is NOT assumed to start from the standard position.
8967 The display is not updated in any way.
8970 ParseGameHistory (char *game)
8973 int fromX, fromY, toX, toY, boardIndex;
8978 if (appData.debugMode)
8979 fprintf(debugFP, "Parsing game history: %s\n", game);
8981 if (gameInfo.event == NULL) gameInfo.event = StrSave("ICS game");
8982 gameInfo.site = StrSave(appData.icsHost);
8983 gameInfo.date = PGNDate();
8984 gameInfo.round = StrSave("-");
8986 /* Parse out names of players */
8987 while (*game == ' ') game++;
8989 while (*game != ' ') *p++ = *game++;
8991 gameInfo.white = StrSave(buf);
8992 while (*game == ' ') game++;
8994 while (*game != ' ' && *game != '\n') *p++ = *game++;
8996 gameInfo.black = StrSave(buf);
8999 boardIndex = blackPlaysFirst ? 1 : 0;
9002 yyboardindex = boardIndex;
9003 moveType = (ChessMove) Myylex();
9005 case IllegalMove: /* maybe suicide chess, etc. */
9006 if (appData.debugMode) {
9007 fprintf(debugFP, "Illegal move from ICS: '%s'\n", yy_text);
9008 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
9009 setbuf(debugFP, NULL);
9011 case WhitePromotion:
9012 case BlackPromotion:
9013 case WhiteNonPromotion:
9014 case BlackNonPromotion:
9016 case WhiteCapturesEnPassant:
9017 case BlackCapturesEnPassant:
9018 case WhiteKingSideCastle:
9019 case WhiteQueenSideCastle:
9020 case BlackKingSideCastle:
9021 case BlackQueenSideCastle:
9022 case WhiteKingSideCastleWild:
9023 case WhiteQueenSideCastleWild:
9024 case BlackKingSideCastleWild:
9025 case BlackQueenSideCastleWild:
9027 case WhiteHSideCastleFR:
9028 case WhiteASideCastleFR:
9029 case BlackHSideCastleFR:
9030 case BlackASideCastleFR:
9032 fromX = currentMoveString[0] - AAA;
9033 fromY = currentMoveString[1] - ONE;
9034 toX = currentMoveString[2] - AAA;
9035 toY = currentMoveString[3] - ONE;
9036 promoChar = currentMoveString[4];
9040 if(currentMoveString[0] == '@') continue; // no null moves in ICS mode!
9041 fromX = moveType == WhiteDrop ?
9042 (int) CharToPiece(ToUpper(currentMoveString[0])) :
9043 (int) CharToPiece(ToLower(currentMoveString[0]));
9045 toX = currentMoveString[2] - AAA;
9046 toY = currentMoveString[3] - ONE;
9047 promoChar = NULLCHAR;
9051 snprintf(buf, MSG_SIZ, _("Ambiguous move in ICS output: \"%s\""), yy_text);
9052 if (appData.debugMode) {
9053 fprintf(debugFP, "Ambiguous move from ICS: '%s'\n", yy_text);
9054 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
9055 setbuf(debugFP, NULL);
9057 DisplayError(buf, 0);
9059 case ImpossibleMove:
9061 snprintf(buf, MSG_SIZ, _("Illegal move in ICS output: \"%s\""), yy_text);
9062 if (appData.debugMode) {
9063 fprintf(debugFP, "Impossible move from ICS: '%s'\n", yy_text);
9064 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
9065 setbuf(debugFP, NULL);
9067 DisplayError(buf, 0);
9070 if (boardIndex < backwardMostMove) {
9071 /* Oops, gap. How did that happen? */
9072 DisplayError(_("Gap in move list"), 0);
9075 backwardMostMove = blackPlaysFirst ? 1 : 0;
9076 if (boardIndex > forwardMostMove) {
9077 forwardMostMove = boardIndex;
9081 if (boardIndex > (blackPlaysFirst ? 1 : 0)) {
9082 strcat(parseList[boardIndex-1], " ");
9083 strcat(parseList[boardIndex-1], yy_text);
9095 case GameUnfinished:
9096 if (gameMode == IcsExamining) {
9097 if (boardIndex < backwardMostMove) {
9098 /* Oops, gap. How did that happen? */
9101 backwardMostMove = blackPlaysFirst ? 1 : 0;
9104 gameInfo.result = moveType;
9105 p = strchr(yy_text, '{');
9106 if (p == NULL) p = strchr(yy_text, '(');
9109 if (p[0] == '0' || p[0] == '1' || p[0] == '*') p = "";
9111 q = strchr(p, *p == '{' ? '}' : ')');
9112 if (q != NULL) *q = NULLCHAR;
9115 while(q = strchr(p, '\n')) *q = ' '; // [HGM] crush linefeeds in result message
9116 gameInfo.resultDetails = StrSave(p);
9119 if (boardIndex >= forwardMostMove &&
9120 !(gameMode == IcsObserving && ics_gamenum == -1)) {
9121 backwardMostMove = blackPlaysFirst ? 1 : 0;
9124 (void) CoordsToAlgebraic(boards[boardIndex], PosFlags(boardIndex),
9125 fromY, fromX, toY, toX, promoChar,
9126 parseList[boardIndex]);
9127 CopyBoard(boards[boardIndex + 1], boards[boardIndex]);
9128 /* currentMoveString is set as a side-effect of yylex */
9129 safeStrCpy(moveList[boardIndex], currentMoveString, sizeof(moveList[boardIndex])/sizeof(moveList[boardIndex][0]));
9130 strcat(moveList[boardIndex], "\n");
9132 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[boardIndex]);
9133 switch (MateTest(boards[boardIndex], PosFlags(boardIndex)) ) {
9139 if(gameInfo.variant != VariantShogi)
9140 strcat(parseList[boardIndex - 1], "+");
9144 strcat(parseList[boardIndex - 1], "#");
9151 /* Apply a move to the given board */
9153 ApplyMove (int fromX, int fromY, int toX, int toY, int promoChar, Board board)
9155 ChessSquare captured = board[toY][toX], piece, king; int p, oldEP = EP_NONE, berolina = 0;
9156 int promoRank = gameInfo.variant == VariantMakruk || gameInfo.variant == VariantGrand ? 3 : 1;
9158 /* [HGM] compute & store e.p. status and castling rights for new position */
9159 /* we can always do that 'in place', now pointers to these rights are passed to ApplyMove */
9161 if(gameInfo.variant == VariantBerolina) berolina = EP_BEROLIN_A;
9162 oldEP = (signed char)board[EP_STATUS];
9163 board[EP_STATUS] = EP_NONE;
9165 if (fromY == DROP_RANK) {
9167 if(fromX == EmptySquare) { // [HGM] pass: empty drop encodes null move; nothing to change.
9168 board[EP_STATUS] = EP_CAPTURE; // null move considered irreversible
9171 piece = board[toY][toX] = (ChessSquare) fromX;
9175 if( board[toY][toX] != EmptySquare )
9176 board[EP_STATUS] = EP_CAPTURE;
9178 if( board[fromY][fromX] == WhiteLance || board[fromY][fromX] == BlackLance ) {
9179 if( gameInfo.variant != VariantSuper && gameInfo.variant != VariantShogi )
9180 board[EP_STATUS] = EP_PAWN_MOVE; // Lance is Pawn-like in most variants
9182 if( board[fromY][fromX] == WhitePawn ) {
9183 if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
9184 board[EP_STATUS] = EP_PAWN_MOVE;
9186 if(toX>BOARD_LEFT && board[toY][toX-1] == BlackPawn &&
9187 gameInfo.variant != VariantBerolina || toX < fromX)
9188 board[EP_STATUS] = toX | berolina;
9189 if(toX<BOARD_RGHT-1 && board[toY][toX+1] == BlackPawn &&
9190 gameInfo.variant != VariantBerolina || toX > fromX)
9191 board[EP_STATUS] = toX;
9194 if( board[fromY][fromX] == BlackPawn ) {
9195 if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
9196 board[EP_STATUS] = EP_PAWN_MOVE;
9197 if( toY-fromY== -2) {
9198 if(toX>BOARD_LEFT && board[toY][toX-1] == WhitePawn &&
9199 gameInfo.variant != VariantBerolina || toX < fromX)
9200 board[EP_STATUS] = toX | berolina;
9201 if(toX<BOARD_RGHT-1 && board[toY][toX+1] == WhitePawn &&
9202 gameInfo.variant != VariantBerolina || toX > fromX)
9203 board[EP_STATUS] = toX;
9207 for(i=0; i<nrCastlingRights; i++) {
9208 if(board[CASTLING][i] == fromX && castlingRank[i] == fromY ||
9209 board[CASTLING][i] == toX && castlingRank[i] == toY
9210 ) board[CASTLING][i] = NoRights; // revoke for moved or captured piece
9213 if (fromX == toX && fromY == toY) return;
9215 piece = board[fromY][fromX]; /* [HGM] remember, for Shogi promotion */
9216 king = piece < (int) BlackPawn ? WhiteKing : BlackKing; /* [HGM] Knightmate simplify testing for castling */
9217 if(gameInfo.variant == VariantKnightmate)
9218 king += (int) WhiteUnicorn - (int) WhiteKing;
9220 /* Code added by Tord: */
9221 /* FRC castling assumed when king captures friendly rook. [HGM] or RxK for S-Chess */
9222 if (board[fromY][fromX] == WhiteKing && board[toY][toX] == WhiteRook ||
9223 board[fromY][fromX] == WhiteRook && board[toY][toX] == WhiteKing) {
9224 board[fromY][fromX] = EmptySquare;
9225 board[toY][toX] = EmptySquare;
9226 if((toX > fromX) != (piece == WhiteRook)) {
9227 board[0][BOARD_RGHT-2] = WhiteKing; board[0][BOARD_RGHT-3] = WhiteRook;
9229 board[0][BOARD_LEFT+2] = WhiteKing; board[0][BOARD_LEFT+3] = WhiteRook;
9231 } else if (board[fromY][fromX] == BlackKing && board[toY][toX] == BlackRook ||
9232 board[fromY][fromX] == BlackRook && board[toY][toX] == BlackKing) {
9233 board[fromY][fromX] = EmptySquare;
9234 board[toY][toX] = EmptySquare;
9235 if((toX > fromX) != (piece == BlackRook)) {
9236 board[BOARD_HEIGHT-1][BOARD_RGHT-2] = BlackKing; board[BOARD_HEIGHT-1][BOARD_RGHT-3] = BlackRook;
9238 board[BOARD_HEIGHT-1][BOARD_LEFT+2] = BlackKing; board[BOARD_HEIGHT-1][BOARD_LEFT+3] = BlackRook;
9240 /* End of code added by Tord */
9242 } else if (board[fromY][fromX] == king
9243 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
9244 && toY == fromY && toX > fromX+1) {
9245 board[fromY][fromX] = EmptySquare;
9246 board[toY][toX] = king;
9247 board[toY][toX-1] = board[fromY][BOARD_RGHT-1];
9248 board[fromY][BOARD_RGHT-1] = EmptySquare;
9249 } else if (board[fromY][fromX] == king
9250 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
9251 && toY == fromY && toX < fromX-1) {
9252 board[fromY][fromX] = EmptySquare;
9253 board[toY][toX] = king;
9254 board[toY][toX+1] = board[fromY][BOARD_LEFT];
9255 board[fromY][BOARD_LEFT] = EmptySquare;
9256 } else if ((board[fromY][fromX] == WhitePawn && gameInfo.variant != VariantXiangqi ||
9257 board[fromY][fromX] == WhiteLance && gameInfo.variant != VariantSuper && gameInfo.variant != VariantShogi)
9258 && toY >= BOARD_HEIGHT-promoRank && promoChar // defaulting to Q is done elsewhere
9260 /* white pawn promotion */
9261 board[toY][toX] = CharToPiece(ToUpper(promoChar));
9262 if(gameInfo.variant==VariantBughouse ||
9263 gameInfo.variant==VariantCrazyhouse) /* [HGM] use shadow piece */
9264 board[toY][toX] = (ChessSquare) (PROMOTED board[toY][toX]);
9265 board[fromY][fromX] = EmptySquare;
9266 } else if ((fromY >= BOARD_HEIGHT>>1)
9268 && gameInfo.variant != VariantXiangqi
9269 && gameInfo.variant != VariantBerolina
9270 && (board[fromY][fromX] == WhitePawn)
9271 && (board[toY][toX] == EmptySquare)) {
9272 board[fromY][fromX] = EmptySquare;
9273 board[toY][toX] = WhitePawn;
9274 captured = board[toY - 1][toX];
9275 board[toY - 1][toX] = EmptySquare;
9276 } else if ((fromY == BOARD_HEIGHT-4)
9278 && gameInfo.variant == VariantBerolina
9279 && (board[fromY][fromX] == WhitePawn)
9280 && (board[toY][toX] == EmptySquare)) {
9281 board[fromY][fromX] = EmptySquare;
9282 board[toY][toX] = WhitePawn;
9283 if(oldEP & EP_BEROLIN_A) {
9284 captured = board[fromY][fromX-1];
9285 board[fromY][fromX-1] = EmptySquare;
9286 }else{ captured = board[fromY][fromX+1];
9287 board[fromY][fromX+1] = EmptySquare;
9289 } else if (board[fromY][fromX] == king
9290 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
9291 && toY == fromY && toX > fromX+1) {
9292 board[fromY][fromX] = EmptySquare;
9293 board[toY][toX] = king;
9294 board[toY][toX-1] = board[fromY][BOARD_RGHT-1];
9295 board[fromY][BOARD_RGHT-1] = EmptySquare;
9296 } else if (board[fromY][fromX] == king
9297 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
9298 && toY == fromY && toX < fromX-1) {
9299 board[fromY][fromX] = EmptySquare;
9300 board[toY][toX] = king;
9301 board[toY][toX+1] = board[fromY][BOARD_LEFT];
9302 board[fromY][BOARD_LEFT] = EmptySquare;
9303 } else if (fromY == 7 && fromX == 3
9304 && board[fromY][fromX] == BlackKing
9305 && toY == 7 && toX == 5) {
9306 board[fromY][fromX] = EmptySquare;
9307 board[toY][toX] = BlackKing;
9308 board[fromY][7] = EmptySquare;
9309 board[toY][4] = BlackRook;
9310 } else if (fromY == 7 && fromX == 3
9311 && board[fromY][fromX] == BlackKing
9312 && toY == 7 && toX == 1) {
9313 board[fromY][fromX] = EmptySquare;
9314 board[toY][toX] = BlackKing;
9315 board[fromY][0] = EmptySquare;
9316 board[toY][2] = BlackRook;
9317 } else if ((board[fromY][fromX] == BlackPawn && gameInfo.variant != VariantXiangqi ||
9318 board[fromY][fromX] == BlackLance && gameInfo.variant != VariantSuper && gameInfo.variant != VariantShogi)
9319 && toY < promoRank && promoChar
9321 /* black pawn promotion */
9322 board[toY][toX] = CharToPiece(ToLower(promoChar));
9323 if(gameInfo.variant==VariantBughouse ||
9324 gameInfo.variant==VariantCrazyhouse) /* [HGM] use shadow piece */
9325 board[toY][toX] = (ChessSquare) (PROMOTED board[toY][toX]);
9326 board[fromY][fromX] = EmptySquare;
9327 } else if ((fromY < BOARD_HEIGHT>>1)
9329 && gameInfo.variant != VariantXiangqi
9330 && gameInfo.variant != VariantBerolina
9331 && (board[fromY][fromX] == BlackPawn)
9332 && (board[toY][toX] == EmptySquare)) {
9333 board[fromY][fromX] = EmptySquare;
9334 board[toY][toX] = BlackPawn;
9335 captured = board[toY + 1][toX];
9336 board[toY + 1][toX] = EmptySquare;
9337 } else if ((fromY == 3)
9339 && gameInfo.variant == VariantBerolina
9340 && (board[fromY][fromX] == BlackPawn)
9341 && (board[toY][toX] == EmptySquare)) {
9342 board[fromY][fromX] = EmptySquare;
9343 board[toY][toX] = BlackPawn;
9344 if(oldEP & EP_BEROLIN_A) {
9345 captured = board[fromY][fromX-1];
9346 board[fromY][fromX-1] = EmptySquare;
9347 }else{ captured = board[fromY][fromX+1];
9348 board[fromY][fromX+1] = EmptySquare;
9351 board[toY][toX] = board[fromY][fromX];
9352 board[fromY][fromX] = EmptySquare;
9356 if (gameInfo.holdingsWidth != 0) {
9358 /* !!A lot more code needs to be written to support holdings */
9359 /* [HGM] OK, so I have written it. Holdings are stored in the */
9360 /* penultimate board files, so they are automaticlly stored */
9361 /* in the game history. */
9362 if (fromY == DROP_RANK || gameInfo.variant == VariantSChess
9363 && promoChar && piece != WhitePawn && piece != BlackPawn) {
9364 /* Delete from holdings, by decreasing count */
9365 /* and erasing image if necessary */
9366 p = fromY == DROP_RANK ? (int) fromX : CharToPiece(piece > BlackPawn ? ToLower(promoChar) : ToUpper(promoChar));
9367 if(p < (int) BlackPawn) { /* white drop */
9368 p -= (int)WhitePawn;
9369 p = PieceToNumber((ChessSquare)p);
9370 if(p >= gameInfo.holdingsSize) p = 0;
9371 if(--board[p][BOARD_WIDTH-2] <= 0)
9372 board[p][BOARD_WIDTH-1] = EmptySquare;
9373 if((int)board[p][BOARD_WIDTH-2] < 0)
9374 board[p][BOARD_WIDTH-2] = 0;
9375 } else { /* black drop */
9376 p -= (int)BlackPawn;
9377 p = PieceToNumber((ChessSquare)p);
9378 if(p >= gameInfo.holdingsSize) p = 0;
9379 if(--board[BOARD_HEIGHT-1-p][1] <= 0)
9380 board[BOARD_HEIGHT-1-p][0] = EmptySquare;
9381 if((int)board[BOARD_HEIGHT-1-p][1] < 0)
9382 board[BOARD_HEIGHT-1-p][1] = 0;
9385 if (captured != EmptySquare && gameInfo.holdingsSize > 0
9386 && gameInfo.variant != VariantBughouse && gameInfo.variant != VariantSChess ) {
9387 /* [HGM] holdings: Add to holdings, if holdings exist */
9388 if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand) {
9389 // [HGM] superchess: suppress flipping color of captured pieces by reverse pre-flip
9390 captured = (int) captured >= (int) BlackPawn ? BLACK_TO_WHITE captured : WHITE_TO_BLACK captured;
9393 if (p >= (int) BlackPawn) {
9394 p -= (int)BlackPawn;
9395 if(gameInfo.variant == VariantShogi && DEMOTED p >= 0) {
9396 /* in Shogi restore piece to its original first */
9397 captured = (ChessSquare) (DEMOTED captured);
9400 p = PieceToNumber((ChessSquare)p);
9401 if(p >= gameInfo.holdingsSize) { p = 0; captured = BlackPawn; }
9402 board[p][BOARD_WIDTH-2]++;
9403 board[p][BOARD_WIDTH-1] = BLACK_TO_WHITE captured;
9405 p -= (int)WhitePawn;
9406 if(gameInfo.variant == VariantShogi && DEMOTED p >= 0) {
9407 captured = (ChessSquare) (DEMOTED captured);
9410 p = PieceToNumber((ChessSquare)p);
9411 if(p >= gameInfo.holdingsSize) { p = 0; captured = WhitePawn; }
9412 board[BOARD_HEIGHT-1-p][1]++;
9413 board[BOARD_HEIGHT-1-p][0] = WHITE_TO_BLACK captured;
9416 } else if (gameInfo.variant == VariantAtomic) {
9417 if (captured != EmptySquare) {
9419 for (y = toY-1; y <= toY+1; y++) {
9420 for (x = toX-1; x <= toX+1; x++) {
9421 if (y >= 0 && y < BOARD_HEIGHT && x >= BOARD_LEFT && x < BOARD_RGHT &&
9422 board[y][x] != WhitePawn && board[y][x] != BlackPawn) {
9423 board[y][x] = EmptySquare;
9427 board[toY][toX] = EmptySquare;
9430 if(gameInfo.variant == VariantSChess && promoChar != NULLCHAR && promoChar != '=' && piece != WhitePawn && piece != BlackPawn) {
9431 board[fromY][fromX] = CharToPiece(piece < BlackPawn ? ToUpper(promoChar) : ToLower(promoChar)); // S-Chess gating
9433 if(promoChar == '+') {
9434 /* [HGM] Shogi-style promotions, to piece implied by original (Might overwrite orinary Pawn promotion) */
9435 board[toY][toX] = (ChessSquare) (PROMOTED piece);
9436 } else if(!appData.testLegality && promoChar != NULLCHAR && promoChar != '=') { // without legality testing, unconditionally believe promoChar
9437 board[toY][toX] = CharToPiece(piece < BlackPawn ? ToUpper(promoChar) : ToLower(promoChar));
9439 if((gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand)
9440 && promoChar != NULLCHAR && gameInfo.holdingsSize) {
9441 // [HGM] superchess: take promotion piece out of holdings
9442 int k = PieceToNumber(CharToPiece(ToUpper(promoChar)));
9443 if((int)piece < (int)BlackPawn) { // determine stm from piece color
9444 if(!--board[k][BOARD_WIDTH-2])
9445 board[k][BOARD_WIDTH-1] = EmptySquare;
9447 if(!--board[BOARD_HEIGHT-1-k][1])
9448 board[BOARD_HEIGHT-1-k][0] = EmptySquare;
9454 /* Updates forwardMostMove */
9456 MakeMove (int fromX, int fromY, int toX, int toY, int promoChar)
9458 // forwardMostMove++; // [HGM] bare: moved downstream
9460 (void) CoordsToAlgebraic(boards[forwardMostMove],
9461 PosFlags(forwardMostMove),
9462 fromY, fromX, toY, toX, promoChar,
9463 parseList[forwardMostMove]);
9465 if(serverMoves != NULL) { /* [HGM] write moves on file for broadcasting (should be separate routine, really) */
9466 int timeLeft; static int lastLoadFlag=0; int king, piece;
9467 piece = boards[forwardMostMove][fromY][fromX];
9468 king = piece < (int) BlackPawn ? WhiteKing : BlackKing;
9469 if(gameInfo.variant == VariantKnightmate)
9470 king += (int) WhiteUnicorn - (int) WhiteKing;
9471 if(forwardMostMove == 0) {
9472 if(gameMode == MachinePlaysBlack || gameMode == BeginningOfGame)
9473 fprintf(serverMoves, "%s;", UserName());
9474 else if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b')
9475 fprintf(serverMoves, "%s;", second.tidy);
9476 fprintf(serverMoves, "%s;", first.tidy);
9477 if(gameMode == MachinePlaysWhite)
9478 fprintf(serverMoves, "%s;", UserName());
9479 else if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w')
9480 fprintf(serverMoves, "%s;", second.tidy);
9481 } else fprintf(serverMoves, loadFlag|lastLoadFlag ? ":" : ";");
9482 lastLoadFlag = loadFlag;
9484 fprintf(serverMoves, "%c%c:%c%c", AAA+fromX, ONE+fromY, AAA+toX, ONE+toY);
9485 // print castling suffix
9486 if( toY == fromY && piece == king ) {
9488 fprintf(serverMoves, ":%c%c:%c%c", AAA+BOARD_RGHT-1, ONE+fromY, AAA+toX-1,ONE+toY);
9490 fprintf(serverMoves, ":%c%c:%c%c", AAA+BOARD_LEFT, ONE+fromY, AAA+toX+1,ONE+toY);
9493 if( (boards[forwardMostMove][fromY][fromX] == WhitePawn ||
9494 boards[forwardMostMove][fromY][fromX] == BlackPawn ) &&
9495 boards[forwardMostMove][toY][toX] == EmptySquare
9496 && fromX != toX && fromY != toY)
9497 fprintf(serverMoves, ":%c%c:%c%c", AAA+fromX, ONE+fromY, AAA+toX, ONE+fromY);
9499 if(promoChar != NULLCHAR)
9500 fprintf(serverMoves, ":%c:%c%c", ToLower(promoChar), AAA+toX, ONE+toY);
9502 char buf[MOVE_LEN*2], *p; int len;
9503 fprintf(serverMoves, "/%d/%d",
9504 pvInfoList[forwardMostMove].depth, pvInfoList[forwardMostMove].score);
9505 if(forwardMostMove+1 & 1) timeLeft = whiteTimeRemaining/1000;
9506 else timeLeft = blackTimeRemaining/1000;
9507 fprintf(serverMoves, "/%d", timeLeft);
9508 strncpy(buf, parseList[forwardMostMove], MOVE_LEN*2);
9509 if(p = strchr(buf, '=')) *p = NULLCHAR;
9510 len = strlen(buf); if(len > 1 && buf[len-2] != '-') buf[len-2] = NULLCHAR; // strip to-square
9511 fprintf(serverMoves, "/%s", buf);
9513 fflush(serverMoves);
9516 if (forwardMostMove+1 > framePtr) { // [HGM] vari: do not run into saved variations..
9517 GameEnds(GameUnfinished, _("Game too long; increase MAX_MOVES and recompile"), GE_XBOARD);
9520 UnLoadPV(); // [HGM] pv: if we are looking at a PV, abort this
9521 if (commentList[forwardMostMove+1] != NULL) {
9522 free(commentList[forwardMostMove+1]);
9523 commentList[forwardMostMove+1] = NULL;
9525 CopyBoard(boards[forwardMostMove+1], boards[forwardMostMove]);
9526 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[forwardMostMove+1]);
9527 // forwardMostMove++; // [HGM] bare: moved to after ApplyMove, to make sure clock interrupt finds complete board
9528 SwitchClocks(forwardMostMove+1); // [HGM] race: incrementing move nr inside
9529 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
9530 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
9531 adjustedClock = FALSE;
9532 gameInfo.result = GameUnfinished;
9533 if (gameInfo.resultDetails != NULL) {
9534 free(gameInfo.resultDetails);
9535 gameInfo.resultDetails = NULL;
9537 CoordsToComputerAlgebraic(fromY, fromX, toY, toX, promoChar,
9538 moveList[forwardMostMove - 1]);
9539 switch (MateTest(boards[forwardMostMove], PosFlags(forwardMostMove)) ) {
9545 if(gameInfo.variant != VariantShogi)
9546 strcat(parseList[forwardMostMove - 1], "+");
9550 strcat(parseList[forwardMostMove - 1], "#");
9556 /* Updates currentMove if not pausing */
9558 ShowMove (int fromX, int fromY, int toX, int toY)
9560 int instant = (gameMode == PlayFromGameFile) ?
9561 (matchMode || (appData.timeDelay == 0 && !pausing)) : pausing;
9562 if(appData.noGUI) return;
9563 if (!pausing || gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
9565 if (forwardMostMove == currentMove + 1) {
9566 AnimateMove(boards[forwardMostMove - 1],
9567 fromX, fromY, toX, toY);
9569 if (appData.highlightLastMove) {
9570 SetHighlights(fromX, fromY, toX, toY);
9573 currentMove = forwardMostMove;
9576 if (instant) return;
9578 DisplayMove(currentMove - 1);
9579 DrawPosition(FALSE, boards[currentMove]);
9580 DisplayBothClocks();
9581 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
9585 SendEgtPath (ChessProgramState *cps)
9586 { /* [HGM] EGT: match formats given in feature with those given by user, and send info for each match */
9587 char buf[MSG_SIZ], name[MSG_SIZ], *p;
9589 if((p = cps->egtFormats) == NULL || appData.egtFormats == NULL) return;
9592 char c, *q = name+1, *r, *s;
9594 name[0] = ','; // extract next format name from feature and copy with prefixed ','
9595 while(*p && *p != ',') *q++ = *p++;
9597 if( appData.defaultPathEGTB && appData.defaultPathEGTB[0] &&
9598 strcmp(name, ",nalimov:") == 0 ) {
9599 // take nalimov path from the menu-changeable option first, if it is defined
9600 snprintf(buf, MSG_SIZ, "egtpath nalimov %s\n", appData.defaultPathEGTB);
9601 SendToProgram(buf,cps); // send egtbpath command for nalimov
9603 if( (s = StrStr(appData.egtFormats, name+1)) == appData.egtFormats ||
9604 (s = StrStr(appData.egtFormats, name)) != NULL) {
9605 // format name occurs amongst user-supplied formats, at beginning or immediately after comma
9606 s = r = StrStr(s, ":") + 1; // beginning of path info
9607 while(*r && *r != ',') r++; // path info is everything upto next ';' or end of string
9608 c = *r; *r = 0; // temporarily null-terminate path info
9609 *--q = 0; // strip of trailig ':' from name
9610 snprintf(buf, MSG_SIZ, "egtpath %s %s\n", name+1, s);
9612 SendToProgram(buf,cps); // send egtbpath command for this format
9614 if(*p == ',') p++; // read away comma to position for next format name
9619 InitChessProgram (ChessProgramState *cps, int setup)
9620 /* setup needed to setup FRC opening position */
9622 char buf[MSG_SIZ], b[MSG_SIZ]; int overruled;
9623 if (appData.noChessProgram) return;
9624 hintRequested = FALSE;
9625 bookRequested = FALSE;
9627 ParseFeatures(appData.features[cps == &second], cps); // [HGM] allow user to overrule features
9628 /* [HGM] some new WB protocol commands to configure engine are sent now, if engine supports them */
9629 /* moved to before sending initstring in 4.3.15, so Polyglot can delay UCI 'isready' to recepton of 'new' */
9630 if(cps->memSize) { /* [HGM] memory */
9631 snprintf(buf, MSG_SIZ, "memory %d\n", appData.defaultHashSize + appData.defaultCacheSizeEGTB);
9632 SendToProgram(buf, cps);
9634 SendEgtPath(cps); /* [HGM] EGT */
9635 if(cps->maxCores) { /* [HGM] SMP: (protocol specified must be last settings command before new!) */
9636 snprintf(buf, MSG_SIZ, "cores %d\n", appData.smpCores);
9637 SendToProgram(buf, cps);
9640 SendToProgram(cps->initString, cps);
9641 if (gameInfo.variant != VariantNormal &&
9642 gameInfo.variant != VariantLoadable
9643 /* [HGM] also send variant if board size non-standard */
9644 || gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0
9646 char *v = VariantName(gameInfo.variant);
9647 if (cps->protocolVersion != 1 && StrStr(cps->variants, v) == NULL) {
9648 /* [HGM] in protocol 1 we have to assume all variants valid */
9649 snprintf(buf, MSG_SIZ, _("Variant %s not supported by %s"), v, cps->tidy);
9650 DisplayFatalError(buf, 0, 1);
9654 /* [HGM] make prefix for non-standard board size. Awkward testing... */
9655 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
9656 if( gameInfo.variant == VariantXiangqi )
9657 overruled = gameInfo.boardWidth != 9 || gameInfo.boardHeight != 10 || gameInfo.holdingsSize != 0;
9658 if( gameInfo.variant == VariantShogi )
9659 overruled = gameInfo.boardWidth != 9 || gameInfo.boardHeight != 9 || gameInfo.holdingsSize != 7;
9660 if( gameInfo.variant == VariantBughouse || gameInfo.variant == VariantCrazyhouse )
9661 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 5;
9662 if( gameInfo.variant == VariantCapablanca || gameInfo.variant == VariantCapaRandom ||
9663 gameInfo.variant == VariantGothic || gameInfo.variant == VariantFalcon || gameInfo.variant == VariantJanus )
9664 overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
9665 if( gameInfo.variant == VariantCourier )
9666 overruled = gameInfo.boardWidth != 12 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
9667 if( gameInfo.variant == VariantSuper )
9668 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 8;
9669 if( gameInfo.variant == VariantGreat )
9670 overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 8;
9671 if( gameInfo.variant == VariantSChess )
9672 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 7;
9673 if( gameInfo.variant == VariantGrand )
9674 overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 10 || gameInfo.holdingsSize != 7;
9677 snprintf(b, MSG_SIZ, "%dx%d+%d_%s", gameInfo.boardWidth, gameInfo.boardHeight,
9678 gameInfo.holdingsSize, VariantName(gameInfo.variant)); // cook up sized variant name
9679 /* [HGM] varsize: try first if this defiant size variant is specifically known */
9680 if(StrStr(cps->variants, b) == NULL) {
9681 // specific sized variant not known, check if general sizing allowed
9682 if (cps->protocolVersion != 1) { // for protocol 1 we cannot check and hope for the best
9683 if(StrStr(cps->variants, "boardsize") == NULL) {
9684 snprintf(buf, MSG_SIZ, "Board size %dx%d+%d not supported by %s",
9685 gameInfo.boardWidth, gameInfo.boardHeight, gameInfo.holdingsSize, cps->tidy);
9686 DisplayFatalError(buf, 0, 1);
9689 /* [HGM] here we really should compare with the maximum supported board size */
9692 } else snprintf(b, MSG_SIZ,"%s", VariantName(gameInfo.variant));
9693 snprintf(buf, MSG_SIZ, "variant %s\n", b);
9694 SendToProgram(buf, cps);
9696 currentlyInitializedVariant = gameInfo.variant;
9698 /* [HGM] send opening position in FRC to first engine */
9700 SendToProgram("force\n", cps);
9702 /* engine is now in force mode! Set flag to wake it up after first move. */
9703 setboardSpoiledMachineBlack = 1;
9707 snprintf(buf, sizeof(buf), "ics %s\n", appData.icsActive ? appData.icsHost : "-");
9708 SendToProgram(buf, cps);
9710 cps->maybeThinking = FALSE;
9711 cps->offeredDraw = 0;
9712 if (!appData.icsActive) {
9713 SendTimeControl(cps, movesPerSession, timeControl,
9714 timeIncrement, appData.searchDepth,
9717 if (appData.showThinking
9718 // [HGM] thinking: four options require thinking output to be sent
9719 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp()
9721 SendToProgram("post\n", cps);
9723 SendToProgram("hard\n", cps);
9724 if (!appData.ponderNextMove) {
9725 /* Warning: "easy" is a toggle in GNU Chess, so don't send
9726 it without being sure what state we are in first. "hard"
9727 is not a toggle, so that one is OK.
9729 SendToProgram("easy\n", cps);
9732 snprintf(buf, MSG_SIZ, "ping %d\n", ++cps->lastPing);
9733 SendToProgram(buf, cps);
9735 cps->initDone = TRUE;
9736 ClearEngineOutputPane(cps == &second);
9741 StartChessProgram (ChessProgramState *cps)
9746 if (appData.noChessProgram) return;
9747 cps->initDone = FALSE;
9749 if (strcmp(cps->host, "localhost") == 0) {
9750 err = StartChildProcess(cps->program, cps->dir, &cps->pr);
9751 } else if (*appData.remoteShell == NULLCHAR) {
9752 err = OpenRcmd(cps->host, appData.remoteUser, cps->program, &cps->pr);
9754 if (*appData.remoteUser == NULLCHAR) {
9755 snprintf(buf, sizeof(buf), "%s %s %s", appData.remoteShell, cps->host,
9758 snprintf(buf, sizeof(buf), "%s %s -l %s %s", appData.remoteShell,
9759 cps->host, appData.remoteUser, cps->program);
9761 err = StartChildProcess(buf, "", &cps->pr);
9765 snprintf(buf, MSG_SIZ, _("Startup failure on '%s'"), cps->program);
9766 DisplayError(buf, err); // [HGM] bit of a rough kludge: ignore failure, (which XBoard would do anyway), and let I/O discover it
9767 if(cps != &first) return;
9768 appData.noChessProgram = TRUE;
9771 // DisplayFatalError(buf, err, 1);
9772 // cps->pr = NoProc;
9777 cps->isr = AddInputSource(cps->pr, TRUE, ReceiveFromProgram, cps);
9778 if (cps->protocolVersion > 1) {
9779 snprintf(buf, MSG_SIZ, "xboard\nprotover %d\n", cps->protocolVersion);
9780 cps->nrOptions = 0; // [HGM] options: clear all engine-specific options
9781 cps->comboCnt = 0; // and values of combo boxes
9782 SendToProgram(buf, cps);
9784 SendToProgram("xboard\n", cps);
9789 TwoMachinesEventIfReady P((void))
9791 static int curMess = 0;
9792 if (first.lastPing != first.lastPong) {
9793 if(curMess != 1) DisplayMessage("", _("Waiting for first chess program")); curMess = 1;
9794 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10); // [HGM] fast: lowered from 1000
9797 if (second.lastPing != second.lastPong) {
9798 if(curMess != 2) DisplayMessage("", _("Waiting for second chess program")); curMess = 2;
9799 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10); // [HGM] fast: lowered from 1000
9802 DisplayMessage("", ""); curMess = 0;
9808 MakeName (char *template)
9812 static char buf[MSG_SIZ];
9816 clock = time((time_t *)NULL);
9817 tm = localtime(&clock);
9819 while(*p++ = *template++) if(p[-1] == '%') {
9820 switch(*template++) {
9821 case 0: *p = 0; return buf;
9822 case 'Y': i = tm->tm_year+1900; break;
9823 case 'y': i = tm->tm_year-100; break;
9824 case 'M': i = tm->tm_mon+1; break;
9825 case 'd': i = tm->tm_mday; break;
9826 case 'h': i = tm->tm_hour; break;
9827 case 'm': i = tm->tm_min; break;
9828 case 's': i = tm->tm_sec; break;
9831 snprintf(p-1, MSG_SIZ-10 - (p - buf), "%02d", i); p += strlen(p);
9837 CountPlayers (char *p)
9840 while(p = strchr(p, '\n')) p++, n++; // count participants
9845 WriteTourneyFile (char *results, FILE *f)
9846 { // write tournament parameters on tourneyFile; on success return the stream pointer for closing
9847 if(f == NULL) f = fopen(appData.tourneyFile, "w");
9848 if(f == NULL) DisplayError(_("Could not write on tourney file"), 0); else {
9849 // create a file with tournament description
9850 fprintf(f, "-participants {%s}\n", appData.participants);
9851 fprintf(f, "-seedBase %d\n", appData.seedBase);
9852 fprintf(f, "-tourneyType %d\n", appData.tourneyType);
9853 fprintf(f, "-tourneyCycles %d\n", appData.tourneyCycles);
9854 fprintf(f, "-defaultMatchGames %d\n", appData.defaultMatchGames);
9855 fprintf(f, "-syncAfterRound %s\n", appData.roundSync ? "true" : "false");
9856 fprintf(f, "-syncAfterCycle %s\n", appData.cycleSync ? "true" : "false");
9857 fprintf(f, "-saveGameFile \"%s\"\n", appData.saveGameFile);
9858 fprintf(f, "-loadGameFile \"%s\"\n", appData.loadGameFile);
9859 fprintf(f, "-loadGameIndex %d\n", appData.loadGameIndex);
9860 fprintf(f, "-loadPositionFile \"%s\"\n", appData.loadPositionFile);
9861 fprintf(f, "-loadPositionIndex %d\n", appData.loadPositionIndex);
9862 fprintf(f, "-rewindIndex %d\n", appData.rewindIndex);
9863 fprintf(f, "-discourageOwnBooks %s\n", appData.defNoBook ? "true" : "false");
9865 fprintf(f, "-searchTime \"%d:%02d\"\n", searchTime/60, searchTime%60);
9867 fprintf(f, "-mps %d\n", appData.movesPerSession);
9868 fprintf(f, "-tc %s\n", appData.timeControl);
9869 fprintf(f, "-inc %.2f\n", appData.timeIncrement);
9871 fprintf(f, "-results \"%s\"\n", results);
9876 char *command[MAXENGINES], *mnemonic[MAXENGINES];
9879 Substitute (char *participants, int expunge)
9881 int i, changed, changes=0, nPlayers=0;
9882 char *p, *q, *r, buf[MSG_SIZ];
9883 if(participants == NULL) return;
9884 if(appData.tourneyFile[0] == NULLCHAR) { free(participants); return; }
9885 r = p = participants; q = appData.participants;
9886 while(*p && *p == *q) {
9887 if(*p == '\n') r = p+1, nPlayers++;
9890 if(*p) { // difference
9891 while(*p && *p++ != '\n');
9892 while(*q && *q++ != '\n');
9894 changes = 1 + (strcmp(p, q) != 0);
9896 if(changes == 1) { // a single engine mnemonic was changed
9897 q = r; while(*q) nPlayers += (*q++ == '\n');
9898 p = buf; while(*r && (*p = *r++) != '\n') p++;
9900 NamesToList(firstChessProgramNames, command, mnemonic, "all");
9901 for(i=1; mnemonic[i]; i++) if(!strcmp(buf, mnemonic[i])) break;
9902 if(mnemonic[i]) { // The substitute is valid
9904 if(appData.tourneyFile[0] && (f = fopen(appData.tourneyFile, "r+")) ) {
9905 flock(fileno(f), LOCK_EX);
9906 ParseArgsFromFile(f);
9907 fseek(f, 0, SEEK_SET);
9908 FREE(appData.participants); appData.participants = participants;
9909 if(expunge) { // erase results of replaced engine
9910 int len = strlen(appData.results), w, b, dummy;
9911 for(i=0; i<len; i++) {
9912 Pairing(i, nPlayers, &w, &b, &dummy);
9913 if((w == changed || b == changed) && appData.results[i] == '*') {
9914 DisplayError(_("You cannot replace an engine while it is engaged!\nTerminate its game first."), 0);
9919 for(i=0; i<len; i++) {
9920 Pairing(i, nPlayers, &w, &b, &dummy);
9921 if(w == changed || b == changed) appData.results[i] = ' '; // mark as not played
9924 WriteTourneyFile(appData.results, f);
9925 fclose(f); // release lock
9928 } else DisplayError(_("No engine with the name you gave is installed"), 0);
9930 if(changes == 0) DisplayError(_("First change an engine by editing the participants list\nof the Tournament Options dialog"), 0);
9931 if(changes > 1) DisplayError(_("You can only change one engine at the time"), 0);
9937 CreateTourney (char *name)
9940 if(matchMode && strcmp(name, appData.tourneyFile)) {
9941 ASSIGN(name, appData.tourneyFile); //do not allow change of tourneyfile while playing
9943 if(name[0] == NULLCHAR) {
9944 if(appData.participants[0])
9945 DisplayError(_("You must supply a tournament file,\nfor storing the tourney progress"), 0);
9948 f = fopen(name, "r");
9949 if(f) { // file exists
9950 ASSIGN(appData.tourneyFile, name);
9951 ParseArgsFromFile(f); // parse it
9953 if(!appData.participants[0]) return 0; // ignore tourney file if non-existing & no participants
9954 if(CountPlayers(appData.participants) < (appData.tourneyType>0 ? appData.tourneyType+1 : 2)) {
9955 DisplayError(_("Not enough participants"), 0);
9958 ASSIGN(appData.tourneyFile, name);
9959 if(appData.tourneyType < 0) appData.defaultMatchGames = 1; // Swiss forces games/pairing = 1
9960 if((f = WriteTourneyFile("", NULL)) == NULL) return 0;
9963 appData.noChessProgram = FALSE;
9964 appData.clockMode = TRUE;
9970 NamesToList (char *names, char **engineList, char **engineMnemonic, char *group)
9972 char buf[MSG_SIZ], *p, *q;
9973 int i=1, header, skip, all = !strcmp(group, "all"), depth = 0;
9974 skip = !all && group[0]; // if group requested, we start in skip mode
9975 for(;*names && depth >= 0 && i < MAXENGINES-1; names = p) {
9976 p = names; q = buf; header = 0;
9977 while(*p && *p != '\n') *q++ = *p++;
9981 if(strstr(buf, "# end") == buf) { depth--; continue; } // leave group, and suppress printing label
9982 depth++; // we must be entering a new group
9983 if(all) continue; // suppress printing group headers when complete list requested
9985 if(skip && !strcmp(group, buf)) { depth = 0; skip = FALSE; } // start when we reach requested group
9987 if(depth != header && !all || skip) continue; // skip contents of group (but print first-level header)
9988 if(engineList[i]) free(engineList[i]);
9989 engineList[i] = strdup(buf);
9990 if(buf[0] != '#') TidyProgramName(engineList[i], "localhost", buf); // group headers not tidied
9991 if(engineMnemonic[i]) free(engineMnemonic[i]);
9992 if((q = strstr(engineList[i]+2, "variant")) && q[-2]== ' ' && (q[-1]=='/' || q[-1]=='-') && (q[7]==' ' || q[7]=='=')) {
9994 sscanf(q + 8, "%s", buf + strlen(buf));
9997 engineMnemonic[i] = strdup(buf);
10000 engineList[i] = engineMnemonic[i] = NULL;
10004 // following implemented as macro to avoid type limitations
10005 #define SWAP(item, temp) temp = appData.item[0]; appData.item[0] = appData.item[n]; appData.item[n] = temp;
10008 SwapEngines (int n)
10009 { // swap settings for first engine and other engine (so far only some selected options)
10014 SWAP(chessProgram, p)
10016 SWAP(hasOwnBookUCI, h)
10017 SWAP(protocolVersion, h)
10019 SWAP(scoreIsAbsolute, h)
10024 SWAP(engOptions, p)
10028 SetPlayer (int player, char *p)
10029 { // [HGM] find the engine line of the partcipant given by number, and parse its options.
10031 char buf[MSG_SIZ], *engineName;
10032 for(i=0; i<player; i++) p = strchr(p, '\n') + 1;
10033 engineName = strdup(p); if(p = strchr(engineName, '\n')) *p = NULLCHAR;
10034 for(i=1; command[i]; i++) if(!strcmp(mnemonic[i], engineName)) break;
10036 snprintf(buf, MSG_SIZ, "-fcp %s", command[i]);
10037 ParseArgsFromString(resetOptions); appData.fenOverride[0] = NULL; appData.pvSAN[0] = FALSE;
10038 appData.firstHasOwnBookUCI = !appData.defNoBook; appData.protocolVersion[0] = PROTOVER;
10039 ParseArgsFromString(buf);
10045 char *recentEngines;
10048 RecentEngineEvent (int nr)
10051 // SwapEngines(1); // bump first to second
10052 // ReplaceEngine(&second, 1); // and load it there
10053 NamesToList(firstChessProgramNames, command, mnemonic, "all"); // get mnemonics of installed engines
10054 n = SetPlayer(nr, recentEngines); // select new (using original menu order!)
10055 if(mnemonic[n]) { // if somehow the engine with the selected nickname is no longer found in the list, we skip
10056 ReplaceEngine(&first, 0);
10057 FloatToFront(&appData.recentEngineList, command[n]);
10062 Pairing (int nr, int nPlayers, int *whitePlayer, int *blackPlayer, int *syncInterval)
10063 { // determine players from game number
10064 int curCycle, curRound, curPairing, gamesPerCycle, gamesPerRound, roundsPerCycle=1, pairingsPerRound=1;
10066 if(appData.tourneyType == 0) {
10067 roundsPerCycle = (nPlayers - 1) | 1;
10068 pairingsPerRound = nPlayers / 2;
10069 } else if(appData.tourneyType > 0) {
10070 roundsPerCycle = nPlayers - appData.tourneyType;
10071 pairingsPerRound = appData.tourneyType;
10073 gamesPerRound = pairingsPerRound * appData.defaultMatchGames;
10074 gamesPerCycle = gamesPerRound * roundsPerCycle;
10075 appData.matchGames = gamesPerCycle * appData.tourneyCycles - 1; // fake like all games are one big match
10076 curCycle = nr / gamesPerCycle; nr %= gamesPerCycle;
10077 curRound = nr / gamesPerRound; nr %= gamesPerRound;
10078 curPairing = nr / appData.defaultMatchGames; nr %= appData.defaultMatchGames;
10079 matchGame = nr + curCycle * appData.defaultMatchGames + 1; // fake game nr that loads correct game or position from file
10080 roundNr = (curCycle * roundsPerCycle + curRound) * appData.defaultMatchGames + nr + 1;
10082 if(appData.cycleSync) *syncInterval = gamesPerCycle;
10083 if(appData.roundSync) *syncInterval = gamesPerRound;
10085 if(appData.debugMode) fprintf(debugFP, "cycle=%d, round=%d, pairing=%d curGame=%d\n", curCycle, curRound, curPairing, matchGame);
10087 if(appData.tourneyType == 0) {
10088 if(curPairing == (nPlayers-1)/2 ) {
10089 *whitePlayer = curRound;
10090 *blackPlayer = nPlayers - 1; // this is the 'bye' when nPlayer is odd
10092 *whitePlayer = curRound - (nPlayers-1)/2 + curPairing;
10093 if(*whitePlayer < 0) *whitePlayer += nPlayers-1+(nPlayers&1);
10094 *blackPlayer = curRound + (nPlayers-1)/2 - curPairing;
10095 if(*blackPlayer >= nPlayers-1+(nPlayers&1)) *blackPlayer -= nPlayers-1+(nPlayers&1);
10097 } else if(appData.tourneyType > 1) {
10098 *blackPlayer = curPairing; // in multi-gauntlet, assign gauntlet engines to second, so first an be kept loaded during round
10099 *whitePlayer = curRound + appData.tourneyType;
10100 } else if(appData.tourneyType > 0) {
10101 *whitePlayer = curPairing;
10102 *blackPlayer = curRound + appData.tourneyType;
10105 // take care of white/black alternation per round.
10106 // For cycles and games this is already taken care of by default, derived from matchGame!
10107 return curRound & 1;
10111 NextTourneyGame (int nr, int *swapColors)
10112 { // !!!major kludge!!! fiddle appData settings to get everything in order for next tourney game
10114 int whitePlayer, blackPlayer, firstBusy=1000000000, syncInterval = 0, nPlayers;
10116 if(appData.tourneyFile[0] == NULLCHAR) return 1; // no tourney, always allow next game
10117 tf = fopen(appData.tourneyFile, "r");
10118 if(tf == NULL) { DisplayFatalError(_("Bad tournament file"), 0, 1); return 0; }
10119 ParseArgsFromFile(tf); fclose(tf);
10120 InitTimeControls(); // TC might be altered from tourney file
10122 nPlayers = CountPlayers(appData.participants); // count participants
10123 if(appData.tourneyType < 0) syncInterval = nPlayers/2; else
10124 *swapColors = Pairing(nr<0 ? 0 : nr, nPlayers, &whitePlayer, &blackPlayer, &syncInterval);
10127 p = q = appData.results;
10128 while(*q) if(*q++ == '*' || q[-1] == ' ') { firstBusy = q - p - 1; break; }
10129 if(firstBusy/syncInterval < (nextGame/syncInterval)) {
10130 DisplayMessage(_("Waiting for other game(s)"),"");
10131 waitingForGame = TRUE;
10132 ScheduleDelayedEvent(NextMatchGame, 1000); // wait for all games of previous round to finish
10135 waitingForGame = FALSE;
10138 if(appData.tourneyType < 0) {
10139 if(nr>=0 && !pairingReceived) {
10141 if(pairing.pr == NoProc) {
10142 if(!appData.pairingEngine[0]) {
10143 DisplayFatalError(_("No pairing engine specified"), 0, 1);
10146 StartChessProgram(&pairing); // starts the pairing engine
10148 snprintf(buf, 1<<16, "results %d %s\n", nPlayers, appData.results);
10149 SendToProgram(buf, &pairing);
10150 snprintf(buf, 1<<16, "pairing %d\n", nr+1);
10151 SendToProgram(buf, &pairing);
10152 return 0; // wait for pairing engine to answer (which causes NextTourneyGame to be called again...
10154 pairingReceived = 0; // ... so we continue here
10156 appData.matchGames = appData.tourneyCycles * syncInterval - 1;
10157 whitePlayer = savedWhitePlayer-1; blackPlayer = savedBlackPlayer-1;
10158 matchGame = 1; roundNr = nr / syncInterval + 1;
10161 if(first.pr != NoProc && second.pr != NoProc || nr<0) return 1; // engines already loaded
10163 // redefine engines, engine dir, etc.
10164 NamesToList(firstChessProgramNames, command, mnemonic, "all"); // get mnemonics of installed engines
10165 if(first.pr == NoProc) {
10166 SetPlayer(whitePlayer, appData.participants); // find white player amongst it, and parse its engine line
10167 InitEngine(&first, 0); // initialize ChessProgramStates based on new settings.
10169 if(second.pr == NoProc) {
10171 SetPlayer(blackPlayer, appData.participants); // find black player amongst it, and parse its engine line
10172 SwapEngines(1); // and make that valid for second engine by swapping
10173 InitEngine(&second, 1);
10175 CommonEngineInit(); // after this TwoMachinesEvent will create correct engine processes
10176 UpdateLogos(FALSE); // leave display to ModeHiglight()
10182 { // performs game initialization that does not invoke engines, and then tries to start the game
10183 int res, firstWhite, swapColors = 0;
10184 if(!NextTourneyGame(nextGame, &swapColors)) return; // this sets matchGame, -fcp / -scp and other options for next game, if needed
10185 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
10187 snprintf(buf, MSG_SIZ, appData.nameOfDebugFile, nextGame+1); // expand name of debug file with %d in it
10188 if(strcmp(buf, currentDebugFile)) { // name has changed
10189 FILE *f = fopen(buf, "w");
10190 if(f) { // if opening the new file failed, just keep using the old one
10191 ASSIGN(currentDebugFile, buf);
10195 if(appData.serverFileName) {
10196 if(serverFP) fclose(serverFP);
10197 serverFP = fopen(appData.serverFileName, "w");
10198 if(serverFP && first.pr != NoProc) fprintf(serverFP, "StartChildProcess (dir=\".\") .\\%s\n", first.tidy);
10199 if(serverFP && second.pr != NoProc) fprintf(serverFP, "StartChildProcess (dir=\".\") .\\%s\n", second.tidy);
10203 firstWhite = appData.firstPlaysBlack ^ (matchGame & 1 | appData.sameColorGames > 1); // non-incremental default
10204 firstWhite ^= swapColors; // reverses if NextTourneyGame says we are in an odd round
10205 first.twoMachinesColor = firstWhite ? "white\n" : "black\n"; // perform actual color assignement
10206 second.twoMachinesColor = firstWhite ? "black\n" : "white\n";
10207 appData.noChessProgram = (first.pr == NoProc); // kludge to prevent Reset from starting up chess program
10208 if(appData.loadGameIndex == -2) srandom(appData.seedBase + 68163*(nextGame & ~1)); // deterministic seed to force same opening
10209 Reset(FALSE, first.pr != NoProc);
10210 res = LoadGameOrPosition(matchGame); // setup game
10211 appData.noChessProgram = FALSE; // LoadGameOrPosition might call Reset too!
10212 if(!res) return; // abort when bad game/pos file
10213 TwoMachinesEvent();
10217 UserAdjudicationEvent (int result)
10219 ChessMove gameResult = GameIsDrawn;
10222 gameResult = WhiteWins;
10224 else if( result < 0 ) {
10225 gameResult = BlackWins;
10228 if( gameMode == TwoMachinesPlay ) {
10229 GameEnds( gameResult, "User adjudication", GE_XBOARD );
10234 // [HGM] save: calculate checksum of game to make games easily identifiable
10236 StringCheckSum (char *s)
10239 if(s==NULL) return 0;
10240 while(*s) i = i*259 + *s++;
10248 for(i=backwardMostMove; i<forwardMostMove; i++) {
10249 sum += pvInfoList[i].depth;
10250 sum += StringCheckSum(parseList[i]);
10251 sum += StringCheckSum(commentList[i]);
10254 if(i>1 && sum==0) sum++; // make sure never zero for non-empty game
10255 return sum + StringCheckSum(commentList[i]);
10256 } // end of save patch
10259 GameEnds (ChessMove result, char *resultDetails, int whosays)
10261 GameMode nextGameMode;
10263 char buf[MSG_SIZ], popupRequested = 0, *ranking = NULL;
10265 if(endingGame) return; /* [HGM] crash: forbid recursion */
10267 if(twoBoards) { // [HGM] dual: switch back to one board
10268 twoBoards = partnerUp = 0; InitDrawingSizes(-2, 0);
10269 DrawPosition(TRUE, partnerBoard); // observed game becomes foreground
10271 if (appData.debugMode) {
10272 fprintf(debugFP, "GameEnds(%d, %s, %d)\n",
10273 result, resultDetails ? resultDetails : "(null)", whosays);
10276 fromX = fromY = -1; // [HGM] abort any move the user is entering.
10278 if (appData.icsActive && (whosays == GE_ENGINE || whosays >= GE_ENGINE1)) {
10279 /* If we are playing on ICS, the server decides when the
10280 game is over, but the engine can offer to draw, claim
10284 if (appData.zippyPlay && first.initDone) {
10285 if (result == GameIsDrawn) {
10286 /* In case draw still needs to be claimed */
10287 SendToICS(ics_prefix);
10288 SendToICS("draw\n");
10289 } else if (StrCaseStr(resultDetails, "resign")) {
10290 SendToICS(ics_prefix);
10291 SendToICS("resign\n");
10295 endingGame = 0; /* [HGM] crash */
10299 /* If we're loading the game from a file, stop */
10300 if (whosays == GE_FILE) {
10301 (void) StopLoadGameTimer();
10305 /* Cancel draw offers */
10306 first.offeredDraw = second.offeredDraw = 0;
10308 /* If this is an ICS game, only ICS can really say it's done;
10309 if not, anyone can. */
10310 isIcsGame = (gameMode == IcsPlayingWhite ||
10311 gameMode == IcsPlayingBlack ||
10312 gameMode == IcsObserving ||
10313 gameMode == IcsExamining);
10315 if (!isIcsGame || whosays == GE_ICS) {
10316 /* OK -- not an ICS game, or ICS said it was done */
10318 if (!isIcsGame && !appData.noChessProgram)
10319 SetUserThinkingEnables();
10321 /* [HGM] if a machine claims the game end we verify this claim */
10322 if(gameMode == TwoMachinesPlay && appData.testClaims) {
10323 if(appData.testLegality && whosays >= GE_ENGINE1 ) {
10325 ChessMove trueResult = (ChessMove) -1;
10327 claimer = whosays == GE_ENGINE1 ? /* color of claimer */
10328 first.twoMachinesColor[0] :
10329 second.twoMachinesColor[0] ;
10331 // [HGM] losers: because the logic is becoming a bit hairy, determine true result first
10332 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_CHECKMATE) {
10333 /* [HGM] verify: engine mate claims accepted if they were flagged */
10334 trueResult = WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins;
10336 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_WINS) { // added code for games where being mated is a win
10337 /* [HGM] verify: engine mate claims accepted if they were flagged */
10338 trueResult = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
10340 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_STALEMATE) { // only used to indicate draws now
10341 trueResult = GameIsDrawn; // default; in variants where stalemate loses, Status is CHECKMATE
10344 // now verify win claims, but not in drop games, as we don't understand those yet
10345 if( (gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper
10346 || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand) &&
10347 (result == WhiteWins && claimer == 'w' ||
10348 result == BlackWins && claimer == 'b' ) ) { // case to verify: engine claims own win
10349 if (appData.debugMode) {
10350 fprintf(debugFP, "result=%d sp=%d move=%d\n",
10351 result, (signed char)boards[forwardMostMove][EP_STATUS], forwardMostMove);
10353 if(result != trueResult) {
10354 snprintf(buf, MSG_SIZ, "False win claim: '%s'", resultDetails);
10355 result = claimer == 'w' ? BlackWins : WhiteWins;
10356 resultDetails = buf;
10359 if( result == GameIsDrawn && (signed char)boards[forwardMostMove][EP_STATUS] > EP_DRAWS
10360 && (forwardMostMove <= backwardMostMove ||
10361 (signed char)boards[forwardMostMove-1][EP_STATUS] > EP_DRAWS ||
10362 (claimer=='b')==(forwardMostMove&1))
10364 /* [HGM] verify: draws that were not flagged are false claims */
10365 snprintf(buf, MSG_SIZ, "False draw claim: '%s'", resultDetails);
10366 result = claimer == 'w' ? BlackWins : WhiteWins;
10367 resultDetails = buf;
10369 /* (Claiming a loss is accepted no questions asked!) */
10371 /* [HGM] bare: don't allow bare King to win */
10372 if((gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper
10373 || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand)
10374 && gameInfo.variant != VariantLosers && gameInfo.variant != VariantGiveaway
10375 && gameInfo.variant != VariantSuicide // [HGM] losers: except in losers, of course...
10376 && result != GameIsDrawn)
10377 { int i, j, k=0, color = (result==WhiteWins ? (int)WhitePawn : (int)BlackPawn);
10378 for(j=BOARD_LEFT; j<BOARD_RGHT; j++) for(i=0; i<BOARD_HEIGHT; i++) {
10379 int p = (signed char)boards[forwardMostMove][i][j] - color;
10380 if(p >= 0 && p <= (int)WhiteKing) k++;
10382 if (appData.debugMode) {
10383 fprintf(debugFP, "GE(%d, %s, %d) bare king k=%d color=%d\n",
10384 result, resultDetails ? resultDetails : "(null)", whosays, k, color);
10387 result = GameIsDrawn;
10388 snprintf(buf, MSG_SIZ, "%s but bare king", resultDetails);
10389 resultDetails = buf;
10395 if(serverMoves != NULL && !loadFlag) { char c = '=';
10396 if(result==WhiteWins) c = '+';
10397 if(result==BlackWins) c = '-';
10398 if(resultDetails != NULL)
10399 fprintf(serverMoves, ";%c;%s\n", c, resultDetails), fflush(serverMoves);
10401 if (resultDetails != NULL) {
10402 gameInfo.result = result;
10403 gameInfo.resultDetails = StrSave(resultDetails);
10405 /* display last move only if game was not loaded from file */
10406 if ((whosays != GE_FILE) && (currentMove == forwardMostMove))
10407 DisplayMove(currentMove - 1);
10409 if (forwardMostMove != 0) {
10410 if (gameMode != PlayFromGameFile && gameMode != EditGame
10411 && lastSavedGame != GameCheckSum() // [HGM] save: suppress duplicates
10413 if (*appData.saveGameFile != NULLCHAR) {
10414 SaveGameToFile(appData.saveGameFile, TRUE);
10415 } else if (appData.autoSaveGames) {
10418 if (*appData.savePositionFile != NULLCHAR) {
10419 SavePositionToFile(appData.savePositionFile);
10424 /* Tell program how game ended in case it is learning */
10425 /* [HGM] Moved this to after saving the PGN, just in case */
10426 /* engine died and we got here through time loss. In that */
10427 /* case we will get a fatal error writing the pipe, which */
10428 /* would otherwise lose us the PGN. */
10429 /* [HGM] crash: not needed anymore, but doesn't hurt; */
10430 /* output during GameEnds should never be fatal anymore */
10431 if (gameMode == MachinePlaysWhite ||
10432 gameMode == MachinePlaysBlack ||
10433 gameMode == TwoMachinesPlay ||
10434 gameMode == IcsPlayingWhite ||
10435 gameMode == IcsPlayingBlack ||
10436 gameMode == BeginningOfGame) {
10438 snprintf(buf, MSG_SIZ, "result %s {%s}\n", PGNResult(result),
10440 if (first.pr != NoProc) {
10441 SendToProgram(buf, &first);
10443 if (second.pr != NoProc &&
10444 gameMode == TwoMachinesPlay) {
10445 SendToProgram(buf, &second);
10450 if (appData.icsActive) {
10451 if (appData.quietPlay &&
10452 (gameMode == IcsPlayingWhite ||
10453 gameMode == IcsPlayingBlack)) {
10454 SendToICS(ics_prefix);
10455 SendToICS("set shout 1\n");
10457 nextGameMode = IcsIdle;
10458 ics_user_moved = FALSE;
10459 /* clean up premove. It's ugly when the game has ended and the
10460 * premove highlights are still on the board.
10463 gotPremove = FALSE;
10464 ClearPremoveHighlights();
10465 DrawPosition(FALSE, boards[currentMove]);
10467 if (whosays == GE_ICS) {
10470 if (gameMode == IcsPlayingWhite)
10472 else if(gameMode == IcsPlayingBlack)
10473 PlayIcsLossSound();
10476 if (gameMode == IcsPlayingBlack)
10478 else if(gameMode == IcsPlayingWhite)
10479 PlayIcsLossSound();
10482 PlayIcsDrawSound();
10485 PlayIcsUnfinishedSound();
10488 } else if (gameMode == EditGame ||
10489 gameMode == PlayFromGameFile ||
10490 gameMode == AnalyzeMode ||
10491 gameMode == AnalyzeFile) {
10492 nextGameMode = gameMode;
10494 nextGameMode = EndOfGame;
10499 nextGameMode = gameMode;
10502 if (appData.noChessProgram) {
10503 gameMode = nextGameMode;
10505 endingGame = 0; /* [HGM] crash */
10510 /* Put first chess program into idle state */
10511 if (first.pr != NoProc &&
10512 (gameMode == MachinePlaysWhite ||
10513 gameMode == MachinePlaysBlack ||
10514 gameMode == TwoMachinesPlay ||
10515 gameMode == IcsPlayingWhite ||
10516 gameMode == IcsPlayingBlack ||
10517 gameMode == BeginningOfGame)) {
10518 SendToProgram("force\n", &first);
10519 if (first.usePing) {
10521 snprintf(buf, MSG_SIZ, "ping %d\n", ++first.lastPing);
10522 SendToProgram(buf, &first);
10525 } else if (result != GameUnfinished || nextGameMode == IcsIdle) {
10526 /* Kill off first chess program */
10527 if (first.isr != NULL)
10528 RemoveInputSource(first.isr);
10531 if (first.pr != NoProc) {
10533 DoSleep( appData.delayBeforeQuit );
10534 SendToProgram("quit\n", &first);
10535 DoSleep( appData.delayAfterQuit );
10536 DestroyChildProcess(first.pr, first.useSigterm);
10540 if (second.reuse) {
10541 /* Put second chess program into idle state */
10542 if (second.pr != NoProc &&
10543 gameMode == TwoMachinesPlay) {
10544 SendToProgram("force\n", &second);
10545 if (second.usePing) {
10547 snprintf(buf, MSG_SIZ, "ping %d\n", ++second.lastPing);
10548 SendToProgram(buf, &second);
10551 } else if (result != GameUnfinished || nextGameMode == IcsIdle) {
10552 /* Kill off second chess program */
10553 if (second.isr != NULL)
10554 RemoveInputSource(second.isr);
10557 if (second.pr != NoProc) {
10558 DoSleep( appData.delayBeforeQuit );
10559 SendToProgram("quit\n", &second);
10560 DoSleep( appData.delayAfterQuit );
10561 DestroyChildProcess(second.pr, second.useSigterm);
10563 second.pr = NoProc;
10566 if (matchMode && (gameMode == TwoMachinesPlay || waitingForGame && exiting)) {
10567 char resChar = '=';
10571 if (first.twoMachinesColor[0] == 'w') {
10574 second.matchWins++;
10579 if (first.twoMachinesColor[0] == 'b') {
10582 second.matchWins++;
10585 case GameUnfinished:
10591 if(waitingForGame) resChar = ' '; // quit while waiting for round sync: unreserve already reserved game
10592 if(appData.tourneyFile[0]){ // [HGM] we are in a tourney; update tourney file with game result
10593 if(appData.afterGame && appData.afterGame[0]) RunCommand(appData.afterGame);
10594 ReserveGame(nextGame, resChar); // sets nextGame
10595 if(nextGame > appData.matchGames) appData.tourneyFile[0] = 0, ranking = TourneyStandings(3); // tourney is done
10596 else ranking = strdup("busy"); //suppress popup when aborted but not finished
10597 } else roundNr = nextGame = matchGame + 1; // normal match, just increment; round equals matchGame
10599 if (nextGame <= appData.matchGames && !abortMatch) {
10600 gameMode = nextGameMode;
10601 matchGame = nextGame; // this will be overruled in tourney mode!
10602 GetTimeMark(&pauseStart); // [HGM] matchpause: stipulate a pause
10603 ScheduleDelayedEvent(NextMatchGame, 10); // but start game immediately (as it will wait out the pause itself)
10604 endingGame = 0; /* [HGM] crash */
10607 gameMode = nextGameMode;
10608 snprintf(buf, MSG_SIZ, _("Match %s vs. %s: final score %d-%d-%d"),
10609 first.tidy, second.tidy,
10610 first.matchWins, second.matchWins,
10611 appData.matchGames - (first.matchWins + second.matchWins));
10612 if(!appData.tourneyFile[0]) matchGame++, DisplayTwoMachinesTitle(); // [HGM] update result in window title
10613 if(ranking && strcmp(ranking, "busy") && appData.afterTourney && appData.afterTourney[0]) RunCommand(appData.afterTourney);
10614 popupRequested++; // [HGM] crash: postpone to after resetting endingGame
10615 if (appData.firstPlaysBlack) { // [HGM] match: back to original for next match
10616 first.twoMachinesColor = "black\n";
10617 second.twoMachinesColor = "white\n";
10619 first.twoMachinesColor = "white\n";
10620 second.twoMachinesColor = "black\n";
10624 if ((gameMode == AnalyzeMode || gameMode == AnalyzeFile) &&
10625 !(nextGameMode == AnalyzeMode || nextGameMode == AnalyzeFile))
10627 gameMode = nextGameMode;
10629 endingGame = 0; /* [HGM] crash */
10630 if(popupRequested) { // [HGM] crash: this calls GameEnds recursively through ExitEvent! Make it a harmless tail recursion.
10631 if(matchMode == TRUE) { // match through command line: exit with or without popup
10633 ToNrEvent(forwardMostMove);
10634 if(strcmp(ranking, "busy")) DisplayFatalError(ranking, 0, 0);
10636 } else DisplayFatalError(buf, 0, 0);
10637 } else { // match through menu; just stop, with or without popup
10638 matchMode = FALSE; appData.matchGames = matchGame = roundNr = 0;
10641 if(strcmp(ranking, "busy")) DisplayNote(ranking);
10642 } else DisplayNote(buf);
10644 if(ranking) free(ranking);
10648 /* Assumes program was just initialized (initString sent).
10649 Leaves program in force mode. */
10651 FeedMovesToProgram (ChessProgramState *cps, int upto)
10655 if (appData.debugMode)
10656 fprintf(debugFP, "Feeding %smoves %d through %d to %s chess program\n",
10657 startedFromSetupPosition ? "position and " : "",
10658 backwardMostMove, upto, cps->which);
10659 if(currentlyInitializedVariant != gameInfo.variant) {
10661 // [HGM] variantswitch: make engine aware of new variant
10662 if(cps->protocolVersion > 1 && StrStr(cps->variants, VariantName(gameInfo.variant)) == NULL)
10663 return; // [HGM] refrain from feeding moves altogether if variant is unsupported!
10664 snprintf(buf, MSG_SIZ, "variant %s\n", VariantName(gameInfo.variant));
10665 SendToProgram(buf, cps);
10666 currentlyInitializedVariant = gameInfo.variant;
10668 SendToProgram("force\n", cps);
10669 if (startedFromSetupPosition) {
10670 SendBoard(cps, backwardMostMove);
10671 if (appData.debugMode) {
10672 fprintf(debugFP, "feedMoves\n");
10675 for (i = backwardMostMove; i < upto; i++) {
10676 SendMoveToProgram(i, cps);
10682 ResurrectChessProgram ()
10684 /* The chess program may have exited.
10685 If so, restart it and feed it all the moves made so far. */
10686 static int doInit = 0;
10688 if (appData.noChessProgram) return 1;
10690 if(matchMode && appData.tourneyFile[0]) { // [HGM] tourney: make sure we get features after engine replacement. (Should we always do this?)
10691 if(WaitForEngine(&first, TwoMachinesEventIfReady)) { doInit = 1; return 0; } // request to do init on next visit
10692 if(!doInit) return 1; // this replaces testing first.pr != NoProc, which is true when we get here, but first time no reason to abort
10693 doInit = 0; // we fell through (first time after starting the engine); make sure it doesn't happen again
10695 if (first.pr != NoProc) return 1;
10696 StartChessProgram(&first);
10698 InitChessProgram(&first, FALSE);
10699 FeedMovesToProgram(&first, currentMove);
10701 if (!first.sendTime) {
10702 /* can't tell gnuchess what its clock should read,
10703 so we bow to its notion. */
10705 timeRemaining[0][currentMove] = whiteTimeRemaining;
10706 timeRemaining[1][currentMove] = blackTimeRemaining;
10709 if ((gameMode == AnalyzeMode || gameMode == AnalyzeFile ||
10710 appData.icsEngineAnalyze) && first.analysisSupport) {
10711 SendToProgram("analyze\n", &first);
10712 first.analyzing = TRUE;
10718 * Button procedures
10721 Reset (int redraw, int init)
10725 if (appData.debugMode) {
10726 fprintf(debugFP, "Reset(%d, %d) from gameMode %d\n",
10727 redraw, init, gameMode);
10729 CleanupTail(); // [HGM] vari: delete any stored variations
10730 CommentPopDown(); // [HGM] make sure no comments to the previous game keep hanging on
10731 pausing = pauseExamInvalid = FALSE;
10732 startedFromSetupPosition = blackPlaysFirst = FALSE;
10734 whiteFlag = blackFlag = FALSE;
10735 userOfferedDraw = FALSE;
10736 hintRequested = bookRequested = FALSE;
10737 first.maybeThinking = FALSE;
10738 second.maybeThinking = FALSE;
10739 first.bookSuspend = FALSE; // [HGM] book
10740 second.bookSuspend = FALSE;
10741 thinkOutput[0] = NULLCHAR;
10742 lastHint[0] = NULLCHAR;
10743 ClearGameInfo(&gameInfo);
10744 gameInfo.variant = StringToVariant(appData.variant);
10745 ics_user_moved = ics_clock_paused = FALSE;
10746 ics_getting_history = H_FALSE;
10748 white_holding[0] = black_holding[0] = NULLCHAR;
10749 ClearProgramStats();
10750 opponentKibitzes = FALSE; // [HGM] kibitz: do not reserve space in engine-output window in zippy mode
10754 flipView = appData.flipView;
10755 ClearPremoveHighlights();
10756 gotPremove = FALSE;
10757 alarmSounded = FALSE;
10759 GameEnds(EndOfFile, NULL, GE_PLAYER);
10760 if(appData.serverMovesName != NULL) {
10761 /* [HGM] prepare to make moves file for broadcasting */
10762 clock_t t = clock();
10763 if(serverMoves != NULL) fclose(serverMoves);
10764 serverMoves = fopen(appData.serverMovesName, "r");
10765 if(serverMoves != NULL) {
10766 fclose(serverMoves);
10767 /* delay 15 sec before overwriting, so all clients can see end */
10768 while(clock()-t < appData.serverPause*CLOCKS_PER_SEC);
10770 serverMoves = fopen(appData.serverMovesName, "w");
10774 gameMode = BeginningOfGame;
10776 if(appData.icsActive) gameInfo.variant = VariantNormal;
10777 currentMove = forwardMostMove = backwardMostMove = 0;
10778 MarkTargetSquares(1);
10779 InitPosition(redraw);
10780 for (i = 0; i < MAX_MOVES; i++) {
10781 if (commentList[i] != NULL) {
10782 free(commentList[i]);
10783 commentList[i] = NULL;
10787 timeRemaining[0][0] = whiteTimeRemaining;
10788 timeRemaining[1][0] = blackTimeRemaining;
10790 if (first.pr == NoProc) {
10791 StartChessProgram(&first);
10794 InitChessProgram(&first, startedFromSetupPosition);
10797 DisplayMessage("", "");
10798 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
10799 lastSavedGame = 0; // [HGM] save: make sure next game counts as unsaved
10800 ClearMap(); // [HGM] exclude: invalidate map
10804 AutoPlayGameLoop ()
10807 if (!AutoPlayOneMove())
10809 if (matchMode || appData.timeDelay == 0)
10811 if (appData.timeDelay < 0)
10813 StartLoadGameTimer((long)(1000.0 * appData.timeDelay));
10822 int fromX, fromY, toX, toY;
10824 if (appData.debugMode) {
10825 fprintf(debugFP, "AutoPlayOneMove(): current %d\n", currentMove);
10828 if (gameMode != PlayFromGameFile && gameMode != AnalyzeFile)
10831 if (gameMode == AnalyzeFile && currentMove > backwardMostMove) {
10832 pvInfoList[currentMove].depth = programStats.depth;
10833 pvInfoList[currentMove].score = programStats.score;
10834 pvInfoList[currentMove].time = 0;
10835 if(currentMove < forwardMostMove) AppendComment(currentMove+1, lastPV[0], 2);
10838 if (currentMove >= forwardMostMove) {
10839 if(gameMode == AnalyzeFile) { ExitAnalyzeMode(); SendToProgram("force\n", &first); }
10840 // gameMode = EndOfGame;
10841 // ModeHighlight();
10843 /* [AS] Clear current move marker at the end of a game */
10844 /* HistorySet(parseList, backwardMostMove, forwardMostMove, -1); */
10849 toX = moveList[currentMove][2] - AAA;
10850 toY = moveList[currentMove][3] - ONE;
10852 if (moveList[currentMove][1] == '@') {
10853 if (appData.highlightLastMove) {
10854 SetHighlights(-1, -1, toX, toY);
10857 fromX = moveList[currentMove][0] - AAA;
10858 fromY = moveList[currentMove][1] - ONE;
10860 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove); /* [AS] */
10862 AnimateMove(boards[currentMove], fromX, fromY, toX, toY);
10864 if (appData.highlightLastMove) {
10865 SetHighlights(fromX, fromY, toX, toY);
10868 DisplayMove(currentMove);
10869 SendMoveToProgram(currentMove++, &first);
10870 DisplayBothClocks();
10871 DrawPosition(FALSE, boards[currentMove]);
10872 // [HGM] PV info: always display, routine tests if empty
10873 DisplayComment(currentMove - 1, commentList[currentMove]);
10879 LoadGameOneMove (ChessMove readAhead)
10881 int fromX = 0, fromY = 0, toX = 0, toY = 0, done;
10882 char promoChar = NULLCHAR;
10883 ChessMove moveType;
10884 char move[MSG_SIZ];
10887 if (gameMode != PlayFromGameFile && gameMode != AnalyzeFile &&
10888 gameMode != AnalyzeMode && gameMode != Training) {
10893 yyboardindex = forwardMostMove;
10894 if (readAhead != EndOfFile) {
10895 moveType = readAhead;
10897 if (gameFileFP == NULL)
10899 moveType = (ChessMove) Myylex();
10903 switch (moveType) {
10905 if (appData.debugMode)
10906 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
10909 /* append the comment but don't display it */
10910 AppendComment(currentMove, p, FALSE);
10913 case WhiteCapturesEnPassant:
10914 case BlackCapturesEnPassant:
10915 case WhitePromotion:
10916 case BlackPromotion:
10917 case WhiteNonPromotion:
10918 case BlackNonPromotion:
10920 case WhiteKingSideCastle:
10921 case WhiteQueenSideCastle:
10922 case BlackKingSideCastle:
10923 case BlackQueenSideCastle:
10924 case WhiteKingSideCastleWild:
10925 case WhiteQueenSideCastleWild:
10926 case BlackKingSideCastleWild:
10927 case BlackQueenSideCastleWild:
10929 case WhiteHSideCastleFR:
10930 case WhiteASideCastleFR:
10931 case BlackHSideCastleFR:
10932 case BlackASideCastleFR:
10934 if (appData.debugMode)
10935 fprintf(debugFP, "Parsed %s into %s\n", yy_text, currentMoveString);
10936 fromX = currentMoveString[0] - AAA;
10937 fromY = currentMoveString[1] - ONE;
10938 toX = currentMoveString[2] - AAA;
10939 toY = currentMoveString[3] - ONE;
10940 promoChar = currentMoveString[4];
10945 if (appData.debugMode)
10946 fprintf(debugFP, "Parsed %s into %s\n", yy_text, currentMoveString);
10947 fromX = moveType == WhiteDrop ?
10948 (int) CharToPiece(ToUpper(currentMoveString[0])) :
10949 (int) CharToPiece(ToLower(currentMoveString[0]));
10951 toX = currentMoveString[2] - AAA;
10952 toY = currentMoveString[3] - ONE;
10958 case GameUnfinished:
10959 if (appData.debugMode)
10960 fprintf(debugFP, "Parsed game end: %s\n", yy_text);
10961 p = strchr(yy_text, '{');
10962 if (p == NULL) p = strchr(yy_text, '(');
10965 if (p[0] == '0' || p[0] == '1' || p[0] == '*') p = "";
10967 q = strchr(p, *p == '{' ? '}' : ')');
10968 if (q != NULL) *q = NULLCHAR;
10971 while(q = strchr(p, '\n')) *q = ' '; // [HGM] crush linefeeds in result message
10972 GameEnds(moveType, p, GE_FILE);
10974 if (cmailMsgLoaded) {
10976 flipView = WhiteOnMove(currentMove);
10977 if (moveType == GameUnfinished) flipView = !flipView;
10978 if (appData.debugMode)
10979 fprintf(debugFP, "Setting flipView to %d\n", flipView) ;
10984 if (appData.debugMode)
10985 fprintf(debugFP, "Parser hit end of file\n");
10986 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
10992 if (WhiteOnMove(currentMove)) {
10993 GameEnds(BlackWins, "Black mates", GE_FILE);
10995 GameEnds(WhiteWins, "White mates", GE_FILE);
10999 GameEnds(GameIsDrawn, "Stalemate", GE_FILE);
11005 case MoveNumberOne:
11006 if (lastLoadGameStart == GNUChessGame) {
11007 /* GNUChessGames have numbers, but they aren't move numbers */
11008 if (appData.debugMode)
11009 fprintf(debugFP, "Parser ignoring: '%s' (%d)\n",
11010 yy_text, (int) moveType);
11011 return LoadGameOneMove(EndOfFile); /* tail recursion */
11013 /* else fall thru */
11018 /* Reached start of next game in file */
11019 if (appData.debugMode)
11020 fprintf(debugFP, "Parsed start of next game: %s\n", yy_text);
11021 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
11027 if (WhiteOnMove(currentMove)) {
11028 GameEnds(BlackWins, "Black mates", GE_FILE);
11030 GameEnds(WhiteWins, "White mates", GE_FILE);
11034 GameEnds(GameIsDrawn, "Stalemate", GE_FILE);
11040 case PositionDiagram: /* should not happen; ignore */
11041 case ElapsedTime: /* ignore */
11042 case NAG: /* ignore */
11043 if (appData.debugMode)
11044 fprintf(debugFP, "Parser ignoring: '%s' (%d)\n",
11045 yy_text, (int) moveType);
11046 return LoadGameOneMove(EndOfFile); /* tail recursion */
11049 if (appData.testLegality) {
11050 if (appData.debugMode)
11051 fprintf(debugFP, "Parsed IllegalMove: %s\n", yy_text);
11052 snprintf(move, MSG_SIZ, _("Illegal move: %d.%s%s"),
11053 (forwardMostMove / 2) + 1,
11054 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
11055 DisplayError(move, 0);
11058 if (appData.debugMode)
11059 fprintf(debugFP, "Parsed %s into IllegalMove %s\n",
11060 yy_text, currentMoveString);
11061 fromX = currentMoveString[0] - AAA;
11062 fromY = currentMoveString[1] - ONE;
11063 toX = currentMoveString[2] - AAA;
11064 toY = currentMoveString[3] - ONE;
11065 promoChar = currentMoveString[4];
11069 case AmbiguousMove:
11070 if (appData.debugMode)
11071 fprintf(debugFP, "Parsed AmbiguousMove: %s\n", yy_text);
11072 snprintf(move, MSG_SIZ, _("Ambiguous move: %d.%s%s"),
11073 (forwardMostMove / 2) + 1,
11074 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
11075 DisplayError(move, 0);
11080 case ImpossibleMove:
11081 if (appData.debugMode)
11082 fprintf(debugFP, "Parsed ImpossibleMove (type = %d): %s\n", moveType, yy_text);
11083 snprintf(move, MSG_SIZ, _("Illegal move: %d.%s%s"),
11084 (forwardMostMove / 2) + 1,
11085 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
11086 DisplayError(move, 0);
11092 if (appData.matchMode || (appData.timeDelay == 0 && !pausing)) {
11093 DrawPosition(FALSE, boards[currentMove]);
11094 DisplayBothClocks();
11095 if (!appData.matchMode) // [HGM] PV info: routine tests if empty
11096 DisplayComment(currentMove - 1, commentList[currentMove]);
11098 (void) StopLoadGameTimer();
11100 cmailOldMove = forwardMostMove;
11103 /* currentMoveString is set as a side-effect of yylex */
11105 thinkOutput[0] = NULLCHAR;
11106 MakeMove(fromX, fromY, toX, toY, promoChar);
11107 currentMove = forwardMostMove;
11112 /* Load the nth game from the given file */
11114 LoadGameFromFile (char *filename, int n, char *title, int useList)
11119 if (strcmp(filename, "-") == 0) {
11123 f = fopen(filename, "rb");
11125 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
11126 DisplayError(buf, errno);
11130 if (fseek(f, 0, 0) == -1) {
11131 /* f is not seekable; probably a pipe */
11134 if (useList && n == 0) {
11135 int error = GameListBuild(f);
11137 DisplayError(_("Cannot build game list"), error);
11138 } else if (!ListEmpty(&gameList) &&
11139 ((ListGame *) gameList.tailPred)->number > 1) {
11140 GameListPopUp(f, title);
11147 return LoadGame(f, n, title, FALSE);
11152 MakeRegisteredMove ()
11154 int fromX, fromY, toX, toY;
11156 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
11157 switch (cmailMoveType[lastLoadGameNumber - 1]) {
11160 if (appData.debugMode)
11161 fprintf(debugFP, "Restoring %s for game %d\n",
11162 cmailMove[lastLoadGameNumber - 1], lastLoadGameNumber);
11164 thinkOutput[0] = NULLCHAR;
11165 safeStrCpy(moveList[currentMove], cmailMove[lastLoadGameNumber - 1], sizeof(moveList[currentMove])/sizeof(moveList[currentMove][0]));
11166 fromX = cmailMove[lastLoadGameNumber - 1][0] - AAA;
11167 fromY = cmailMove[lastLoadGameNumber - 1][1] - ONE;
11168 toX = cmailMove[lastLoadGameNumber - 1][2] - AAA;
11169 toY = cmailMove[lastLoadGameNumber - 1][3] - ONE;
11170 promoChar = cmailMove[lastLoadGameNumber - 1][4];
11171 MakeMove(fromX, fromY, toX, toY, promoChar);
11172 ShowMove(fromX, fromY, toX, toY);
11174 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
11181 if (WhiteOnMove(currentMove)) {
11182 GameEnds(BlackWins, "Black mates", GE_PLAYER);
11184 GameEnds(WhiteWins, "White mates", GE_PLAYER);
11189 GameEnds(GameIsDrawn, "Stalemate", GE_PLAYER);
11196 if (WhiteOnMove(currentMove)) {
11197 GameEnds(BlackWins, "White resigns", GE_PLAYER);
11199 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
11204 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
11215 /* Wrapper around LoadGame for use when a Cmail message is loaded */
11217 CmailLoadGame (FILE *f, int gameNumber, char *title, int useList)
11221 if (gameNumber > nCmailGames) {
11222 DisplayError(_("No more games in this message"), 0);
11225 if (f == lastLoadGameFP) {
11226 int offset = gameNumber - lastLoadGameNumber;
11228 cmailMsg[0] = NULLCHAR;
11229 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
11230 cmailMoveRegistered[lastLoadGameNumber - 1] = FALSE;
11231 nCmailMovesRegistered--;
11233 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
11234 if (cmailResult[lastLoadGameNumber - 1] == CMAIL_NEW_RESULT) {
11235 cmailResult[lastLoadGameNumber - 1] = CMAIL_NOT_RESULT;
11238 if (! RegisterMove()) return FALSE;
11242 retVal = LoadGame(f, gameNumber, title, useList);
11244 /* Make move registered during previous look at this game, if any */
11245 MakeRegisteredMove();
11247 if (cmailCommentList[lastLoadGameNumber - 1] != NULL) {
11248 commentList[currentMove]
11249 = StrSave(cmailCommentList[lastLoadGameNumber - 1]);
11250 DisplayComment(currentMove - 1, commentList[currentMove]);
11256 /* Support for LoadNextGame, LoadPreviousGame, ReloadSameGame */
11258 ReloadGame (int offset)
11260 int gameNumber = lastLoadGameNumber + offset;
11261 if (lastLoadGameFP == NULL) {
11262 DisplayError(_("No game has been loaded yet"), 0);
11265 if (gameNumber <= 0) {
11266 DisplayError(_("Can't back up any further"), 0);
11269 if (cmailMsgLoaded) {
11270 return CmailLoadGame(lastLoadGameFP, gameNumber,
11271 lastLoadGameTitle, lastLoadGameUseList);
11273 return LoadGame(lastLoadGameFP, gameNumber,
11274 lastLoadGameTitle, lastLoadGameUseList);
11278 int keys[EmptySquare+1];
11281 PositionMatches (Board b1, Board b2)
11284 switch(appData.searchMode) {
11285 case 1: return CompareWithRights(b1, b2);
11287 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11288 if(b2[r][f] != EmptySquare && b1[r][f] != b2[r][f]) return FALSE;
11292 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11293 if((b2[r][f] == WhitePawn || b2[r][f] == BlackPawn) && b1[r][f] != b2[r][f]) return FALSE;
11294 sum += keys[b1[r][f]] - keys[b2[r][f]];
11298 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11299 sum += keys[b1[r][f]] - keys[b2[r][f]];
11311 int pieceList[256], quickBoard[256];
11312 ChessSquare pieceType[256] = { EmptySquare };
11313 Board soughtBoard, reverseBoard, flipBoard, rotateBoard;
11314 int counts[EmptySquare], minSought[EmptySquare], minReverse[EmptySquare], maxSought[EmptySquare], maxReverse[EmptySquare];
11315 int soughtTotal, turn;
11316 Boolean epOK, flipSearch;
11319 unsigned char piece, to;
11322 #define DSIZE (250000)
11324 Move initialSpace[DSIZE+1000]; // gamble on that game will not be more than 500 moves
11325 Move *moveDatabase = initialSpace;
11326 unsigned int movePtr, dataSize = DSIZE;
11329 MakePieceList (Board board, int *counts)
11331 int r, f, n=Q_PROMO, total=0;
11332 for(r=0;r<EmptySquare;r++) counts[r] = 0; // piece-type counts
11333 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11334 int sq = f + (r<<4);
11335 if(board[r][f] == EmptySquare) quickBoard[sq] = 0; else {
11336 quickBoard[sq] = ++n;
11338 pieceType[n] = board[r][f];
11339 counts[board[r][f]]++;
11340 if(board[r][f] == WhiteKing) pieceList[1] = n; else
11341 if(board[r][f] == BlackKing) pieceList[2] = n; // remember which are Kings, for castling
11345 epOK = gameInfo.variant != VariantXiangqi && gameInfo.variant != VariantBerolina;
11350 PackMove (int fromX, int fromY, int toX, int toY, ChessSquare promoPiece)
11352 int sq = fromX + (fromY<<4);
11353 int piece = quickBoard[sq];
11354 quickBoard[sq] = 0;
11355 moveDatabase[movePtr].to = pieceList[piece] = sq = toX + (toY<<4);
11356 if(piece == pieceList[1] && fromY == toY && (toX > fromX+1 || toX < fromX-1) && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1) {
11357 int from = toX>fromX ? BOARD_RGHT-1 : BOARD_LEFT;
11358 moveDatabase[movePtr++].piece = Q_WCASTL;
11359 quickBoard[sq] = piece;
11360 piece = quickBoard[from]; quickBoard[from] = 0;
11361 moveDatabase[movePtr].to = pieceList[piece] = sq = toX>fromX ? sq-1 : sq+1;
11363 if(piece == pieceList[2] && fromY == toY && (toX > fromX+1 || toX < fromX-1) && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1) {
11364 int from = (toX>fromX ? BOARD_RGHT-1 : BOARD_LEFT) + (BOARD_HEIGHT-1 <<4);
11365 moveDatabase[movePtr++].piece = Q_BCASTL;
11366 quickBoard[sq] = piece;
11367 piece = quickBoard[from]; quickBoard[from] = 0;
11368 moveDatabase[movePtr].to = pieceList[piece] = sq = toX>fromX ? sq-1 : sq+1;
11370 if(epOK && (pieceType[piece] == WhitePawn || pieceType[piece] == BlackPawn) && fromX != toX && quickBoard[sq] == 0) {
11371 quickBoard[(fromY<<4)+toX] = 0;
11372 moveDatabase[movePtr].piece = Q_EP;
11373 moveDatabase[movePtr++].to = (fromY<<4)+toX;
11374 moveDatabase[movePtr].to = sq;
11376 if(promoPiece != pieceType[piece]) {
11377 moveDatabase[movePtr++].piece = Q_PROMO;
11378 moveDatabase[movePtr].to = pieceType[piece] = (int) promoPiece;
11380 moveDatabase[movePtr].piece = piece;
11381 quickBoard[sq] = piece;
11386 PackGame (Board board)
11388 Move *newSpace = NULL;
11389 moveDatabase[movePtr].piece = 0; // terminate previous game
11390 if(movePtr > dataSize) {
11391 if(appData.debugMode) fprintf(debugFP, "move-cache overflow, enlarge to %d MB\n", dataSize/128);
11392 dataSize *= 8; // increase size by factor 8 (512KB -> 4MB -> 32MB -> 256MB -> 2GB)
11393 if(dataSize) newSpace = (Move*) calloc(dataSize + 1000, sizeof(Move));
11396 Move *p = moveDatabase, *q = newSpace;
11397 for(i=0; i<movePtr; i++) *q++ = *p++; // copy to newly allocated space
11398 if(dataSize > 8*DSIZE) free(moveDatabase); // and free old space (if it was allocated)
11399 moveDatabase = newSpace;
11400 } else { // calloc failed, we must be out of memory. Too bad...
11401 dataSize = 0; // prevent calloc events for all subsequent games
11402 return 0; // and signal this one isn't cached
11406 MakePieceList(board, counts);
11411 QuickCompare (Board board, int *minCounts, int *maxCounts)
11412 { // compare according to search mode
11414 switch(appData.searchMode)
11416 case 1: // exact position match
11417 if(!(turn & board[EP_STATUS-1])) return FALSE; // wrong side to move
11418 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11419 if(board[r][f] != pieceType[quickBoard[(r<<4)+f]]) return FALSE;
11422 case 2: // can have extra material on empty squares
11423 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11424 if(board[r][f] == EmptySquare) continue;
11425 if(board[r][f] != pieceType[quickBoard[(r<<4)+f]]) return FALSE;
11428 case 3: // material with exact Pawn structure
11429 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11430 if(board[r][f] != WhitePawn && board[r][f] != BlackPawn) continue;
11431 if(board[r][f] != pieceType[quickBoard[(r<<4)+f]]) return FALSE;
11432 } // fall through to material comparison
11433 case 4: // exact material
11434 for(r=0; r<EmptySquare; r++) if(counts[r] != maxCounts[r]) return FALSE;
11436 case 6: // material range with given imbalance
11437 for(r=0; r<BlackPawn; r++) if(counts[r] - minCounts[r] != counts[r+BlackPawn] - minCounts[r+BlackPawn]) return FALSE;
11438 // fall through to range comparison
11439 case 5: // material range
11440 for(r=0; r<EmptySquare; r++) if(counts[r] < minCounts[r] || counts[r] > maxCounts[r]) return FALSE;
11446 QuickScan (Board board, Move *move)
11447 { // reconstruct game,and compare all positions in it
11448 int cnt=0, stretch=0, total = MakePieceList(board, counts);
11450 int piece = move->piece;
11451 int to = move->to, from = pieceList[piece];
11452 if(piece <= Q_PROMO) { // special moves encoded by otherwise invalid piece numbers 1-4
11453 if(!piece) return -1;
11454 if(piece == Q_PROMO) { // promotion, encoded as (Q_PROMO, to) + (piece, promoType)
11455 piece = (++move)->piece;
11456 from = pieceList[piece];
11457 counts[pieceType[piece]]--;
11458 pieceType[piece] = (ChessSquare) move->to;
11459 counts[move->to]++;
11460 } else if(piece == Q_EP) { // e.p. capture, encoded as (Q_EP, ep-sqr) + (piece, to)
11461 counts[pieceType[quickBoard[to]]]--;
11462 quickBoard[to] = 0; total--;
11465 } else if(piece <= Q_BCASTL) { // castling, encoded as (Q_XCASTL, king-to) + (rook, rook-to)
11466 piece = pieceList[piece]; // first two elements of pieceList contain King numbers
11467 from = pieceList[piece]; // so this must be King
11468 quickBoard[from] = 0;
11469 quickBoard[to] = piece;
11470 pieceList[piece] = to;
11475 if(appData.searchMode > 2) counts[pieceType[quickBoard[to]]]--; // account capture
11476 if((total -= (quickBoard[to] != 0)) < soughtTotal) return -1; // piece count dropped below what we search for
11477 quickBoard[from] = 0;
11478 quickBoard[to] = piece;
11479 pieceList[piece] = to;
11481 if(QuickCompare(soughtBoard, minSought, maxSought) ||
11482 appData.ignoreColors && QuickCompare(reverseBoard, minReverse, maxReverse) ||
11483 flipSearch && (QuickCompare(flipBoard, minSought, maxSought) ||
11484 appData.ignoreColors && QuickCompare(rotateBoard, minReverse, maxReverse))
11486 static int lastCounts[EmptySquare+1];
11488 if(stretch) for(i=0; i<EmptySquare; i++) if(lastCounts[i] != counts[i]) { stretch = 0; break; } // reset if material changes
11489 if(stretch++ == 0) for(i=0; i<EmptySquare; i++) lastCounts[i] = counts[i]; // remember actual material
11490 } else stretch = 0;
11491 if(stretch && (appData.searchMode == 1 || stretch >= appData.stretch)) return cnt + 1 - stretch;
11500 flipSearch = FALSE;
11501 CopyBoard(soughtBoard, boards[currentMove]);
11502 soughtTotal = MakePieceList(soughtBoard, maxSought);
11503 soughtBoard[EP_STATUS-1] = (currentMove & 1) + 1;
11504 if(currentMove == 0 && gameMode == EditPosition) soughtBoard[EP_STATUS-1] = blackPlaysFirst + 1; // (!)
11505 CopyBoard(reverseBoard, boards[currentMove]);
11506 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11507 int piece = boards[currentMove][BOARD_HEIGHT-1-r][f];
11508 if(piece < BlackPawn) piece += BlackPawn; else if(piece < EmptySquare) piece -= BlackPawn; // color-flip
11509 reverseBoard[r][f] = piece;
11511 reverseBoard[EP_STATUS-1] = soughtBoard[EP_STATUS-1] ^ 3;
11512 for(r=0; r<6; r++) reverseBoard[CASTLING][r] = boards[currentMove][CASTLING][(r+3)%6];
11513 if(appData.findMirror && appData.searchMode <= 3 && (!nrCastlingRights
11514 || (boards[currentMove][CASTLING][2] == NoRights ||
11515 boards[currentMove][CASTLING][0] == NoRights && boards[currentMove][CASTLING][1] == NoRights )
11516 && (boards[currentMove][CASTLING][5] == NoRights ||
11517 boards[currentMove][CASTLING][3] == NoRights && boards[currentMove][CASTLING][4] == NoRights ) )
11520 CopyBoard(flipBoard, soughtBoard);
11521 CopyBoard(rotateBoard, reverseBoard);
11522 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11523 flipBoard[r][f] = soughtBoard[r][BOARD_WIDTH-1-f];
11524 rotateBoard[r][f] = reverseBoard[r][BOARD_WIDTH-1-f];
11527 for(r=0; r<BlackPawn; r++) maxReverse[r] = maxSought[r+BlackPawn], maxReverse[r+BlackPawn] = maxSought[r];
11528 if(appData.searchMode >= 5) {
11529 for(r=BOARD_HEIGHT/2; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) soughtBoard[r][f] = EmptySquare;
11530 MakePieceList(soughtBoard, minSought);
11531 for(r=0; r<BlackPawn; r++) minReverse[r] = minSought[r+BlackPawn], minReverse[r+BlackPawn] = minSought[r];
11533 if(gameInfo.variant == VariantCrazyhouse || gameInfo.variant == VariantShogi || gameInfo.variant == VariantBughouse)
11534 soughtTotal = 0; // in drop games nr of pieces does not fall monotonously
11537 GameInfo dummyInfo;
11540 GameContainsPosition (FILE *f, ListGame *lg)
11542 int next, btm=0, plyNr=0, scratch=forwardMostMove+2&~1;
11543 int fromX, fromY, toX, toY;
11545 static int initDone=FALSE;
11547 // weed out games based on numerical tag comparison
11548 if(lg->gameInfo.variant != gameInfo.variant) return -1; // wrong variant
11549 if(appData.eloThreshold1 && (lg->gameInfo.whiteRating < appData.eloThreshold1 && lg->gameInfo.blackRating < appData.eloThreshold1)) return -1;
11550 if(appData.eloThreshold2 && (lg->gameInfo.whiteRating < appData.eloThreshold2 || lg->gameInfo.blackRating < appData.eloThreshold2)) return -1;
11551 if(appData.dateThreshold && (!lg->gameInfo.date || atoi(lg->gameInfo.date) < appData.dateThreshold)) return -1;
11553 for(next = WhitePawn; next<EmptySquare; next++) keys[next] = random()>>8 ^ random()<<6 ^random()<<20;
11556 if(lg->gameInfo.fen) ParseFEN(boards[scratch], &btm, lg->gameInfo.fen);
11557 else CopyBoard(boards[scratch], initialPosition); // default start position
11560 if((next = QuickScan( boards[scratch], &moveDatabase[lg->moves] )) < 0) return -1; // quick scan rules out it is there
11561 if(appData.searchMode >= 4) return next; // for material searches, trust QuickScan.
11564 if(PositionMatches(boards[scratch], boards[currentMove])) return plyNr;
11565 fseek(f, lg->offset, 0);
11568 yyboardindex = scratch;
11569 quickFlag = plyNr+1;
11574 if(plyNr) return -1; // after we have seen moves, any tags will be start of next game
11580 if(plyNr) return -1; // after we have seen moves, this is for new game
11583 case AmbiguousMove: // we cannot reconstruct the game beyond these two
11584 case ImpossibleMove:
11585 case WhiteWins: // game ends here with these four
11588 case GameUnfinished:
11592 if(appData.testLegality) return -1;
11593 case WhiteCapturesEnPassant:
11594 case BlackCapturesEnPassant:
11595 case WhitePromotion:
11596 case BlackPromotion:
11597 case WhiteNonPromotion:
11598 case BlackNonPromotion:
11600 case WhiteKingSideCastle:
11601 case WhiteQueenSideCastle:
11602 case BlackKingSideCastle:
11603 case BlackQueenSideCastle:
11604 case WhiteKingSideCastleWild:
11605 case WhiteQueenSideCastleWild:
11606 case BlackKingSideCastleWild:
11607 case BlackQueenSideCastleWild:
11608 case WhiteHSideCastleFR:
11609 case WhiteASideCastleFR:
11610 case BlackHSideCastleFR:
11611 case BlackASideCastleFR:
11612 fromX = currentMoveString[0] - AAA;
11613 fromY = currentMoveString[1] - ONE;
11614 toX = currentMoveString[2] - AAA;
11615 toY = currentMoveString[3] - ONE;
11616 promoChar = currentMoveString[4];
11620 fromX = next == WhiteDrop ?
11621 (int) CharToPiece(ToUpper(currentMoveString[0])) :
11622 (int) CharToPiece(ToLower(currentMoveString[0]));
11624 toX = currentMoveString[2] - AAA;
11625 toY = currentMoveString[3] - ONE;
11629 // Move encountered; peform it. We need to shuttle between two boards, as even/odd index determines side to move
11631 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[scratch]);
11632 if(PositionMatches(boards[scratch], boards[currentMove])) return plyNr;
11633 if(appData.ignoreColors && PositionMatches(boards[scratch], reverseBoard)) return plyNr;
11634 if(appData.findMirror) {
11635 if(PositionMatches(boards[scratch], flipBoard)) return plyNr;
11636 if(appData.ignoreColors && PositionMatches(boards[scratch], rotateBoard)) return plyNr;
11641 /* Load the nth game from open file f */
11643 LoadGame (FILE *f, int gameNumber, char *title, int useList)
11647 int gn = gameNumber;
11648 ListGame *lg = NULL;
11649 int numPGNTags = 0;
11651 GameMode oldGameMode;
11652 VariantClass oldVariant = gameInfo.variant; /* [HGM] PGNvariant */
11654 if (appData.debugMode)
11655 fprintf(debugFP, "LoadGame(): on entry, gameMode %d\n", gameMode);
11657 if (gameMode == Training )
11658 SetTrainingModeOff();
11660 oldGameMode = gameMode;
11661 if (gameMode != BeginningOfGame) {
11662 Reset(FALSE, TRUE);
11666 if (lastLoadGameFP != NULL && lastLoadGameFP != f) {
11667 fclose(lastLoadGameFP);
11671 lg = (ListGame *) ListElem(&gameList, gameNumber-1);
11674 fseek(f, lg->offset, 0);
11675 GameListHighlight(gameNumber);
11676 pos = lg->position;
11680 DisplayError(_("Game number out of range"), 0);
11685 if (fseek(f, 0, 0) == -1) {
11686 if (f == lastLoadGameFP ?
11687 gameNumber == lastLoadGameNumber + 1 :
11691 DisplayError(_("Can't seek on game file"), 0);
11696 lastLoadGameFP = f;
11697 lastLoadGameNumber = gameNumber;
11698 safeStrCpy(lastLoadGameTitle, title, sizeof(lastLoadGameTitle)/sizeof(lastLoadGameTitle[0]));
11699 lastLoadGameUseList = useList;
11703 if (lg && lg->gameInfo.white && lg->gameInfo.black) {
11704 snprintf(buf, sizeof(buf), "%s %s %s", lg->gameInfo.white, _("vs."),
11705 lg->gameInfo.black);
11707 } else if (*title != NULLCHAR) {
11708 if (gameNumber > 1) {
11709 snprintf(buf, MSG_SIZ, "%s %d", title, gameNumber);
11712 DisplayTitle(title);
11716 if (gameMode != AnalyzeFile && gameMode != AnalyzeMode) {
11717 gameMode = PlayFromGameFile;
11721 currentMove = forwardMostMove = backwardMostMove = 0;
11722 CopyBoard(boards[0], initialPosition);
11726 * Skip the first gn-1 games in the file.
11727 * Also skip over anything that precedes an identifiable
11728 * start of game marker, to avoid being confused by
11729 * garbage at the start of the file. Currently
11730 * recognized start of game markers are the move number "1",
11731 * the pattern "gnuchess .* game", the pattern
11732 * "^[#;%] [^ ]* game file", and a PGN tag block.
11733 * A game that starts with one of the latter two patterns
11734 * will also have a move number 1, possibly
11735 * following a position diagram.
11736 * 5-4-02: Let's try being more lenient and allowing a game to
11737 * start with an unnumbered move. Does that break anything?
11739 cm = lastLoadGameStart = EndOfFile;
11741 yyboardindex = forwardMostMove;
11742 cm = (ChessMove) Myylex();
11745 if (cmailMsgLoaded) {
11746 nCmailGames = CMAIL_MAX_GAMES - gn;
11749 DisplayError(_("Game not found in file"), 0);
11756 lastLoadGameStart = cm;
11759 case MoveNumberOne:
11760 switch (lastLoadGameStart) {
11765 case MoveNumberOne:
11767 gn--; /* count this game */
11768 lastLoadGameStart = cm;
11777 switch (lastLoadGameStart) {
11780 case MoveNumberOne:
11782 gn--; /* count this game */
11783 lastLoadGameStart = cm;
11786 lastLoadGameStart = cm; /* game counted already */
11794 yyboardindex = forwardMostMove;
11795 cm = (ChessMove) Myylex();
11796 } while (cm == PGNTag || cm == Comment);
11803 if (cmailMsgLoaded && (CMAIL_MAX_GAMES == lastLoadGameNumber)) {
11804 if ( cmailResult[CMAIL_MAX_GAMES - gn - 1]
11805 != CMAIL_OLD_RESULT) {
11807 cmailResult[ CMAIL_MAX_GAMES
11808 - gn - 1] = CMAIL_OLD_RESULT;
11814 /* Only a NormalMove can be at the start of a game
11815 * without a position diagram. */
11816 if (lastLoadGameStart == EndOfFile ) {
11818 lastLoadGameStart = MoveNumberOne;
11827 if (appData.debugMode)
11828 fprintf(debugFP, "Parsed game start '%s' (%d)\n", yy_text, (int) cm);
11830 if (cm == XBoardGame) {
11831 /* Skip any header junk before position diagram and/or move 1 */
11833 yyboardindex = forwardMostMove;
11834 cm = (ChessMove) Myylex();
11836 if (cm == EndOfFile ||
11837 cm == GNUChessGame || cm == XBoardGame) {
11838 /* Empty game; pretend end-of-file and handle later */
11843 if (cm == MoveNumberOne || cm == PositionDiagram ||
11844 cm == PGNTag || cm == Comment)
11847 } else if (cm == GNUChessGame) {
11848 if (gameInfo.event != NULL) {
11849 free(gameInfo.event);
11851 gameInfo.event = StrSave(yy_text);
11854 startedFromSetupPosition = FALSE;
11855 while (cm == PGNTag) {
11856 if (appData.debugMode)
11857 fprintf(debugFP, "Parsed PGNTag: %s\n", yy_text);
11858 err = ParsePGNTag(yy_text, &gameInfo);
11859 if (!err) numPGNTags++;
11861 /* [HGM] PGNvariant: automatically switch to variant given in PGN tag */
11862 if(gameInfo.variant != oldVariant) {
11863 startedFromPositionFile = FALSE; /* [HGM] loadPos: variant switch likely makes position invalid */
11864 ResetFrontEnd(); // [HGM] might need other bitmaps. Cannot use Reset() because it clears gameInfo :-(
11865 InitPosition(TRUE);
11866 oldVariant = gameInfo.variant;
11867 if (appData.debugMode)
11868 fprintf(debugFP, "New variant %d\n", (int) oldVariant);
11872 if (gameInfo.fen != NULL) {
11873 Board initial_position;
11874 startedFromSetupPosition = TRUE;
11875 if (!ParseFEN(initial_position, &blackPlaysFirst, gameInfo.fen)) {
11877 DisplayError(_("Bad FEN position in file"), 0);
11880 CopyBoard(boards[0], initial_position);
11881 if (blackPlaysFirst) {
11882 currentMove = forwardMostMove = backwardMostMove = 1;
11883 CopyBoard(boards[1], initial_position);
11884 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
11885 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
11886 timeRemaining[0][1] = whiteTimeRemaining;
11887 timeRemaining[1][1] = blackTimeRemaining;
11888 if (commentList[0] != NULL) {
11889 commentList[1] = commentList[0];
11890 commentList[0] = NULL;
11893 currentMove = forwardMostMove = backwardMostMove = 0;
11895 /* [HGM] copy FEN attributes as well. Bugfix 4.3.14m and 4.3.15e: moved to after 'blackPlaysFirst' */
11897 initialRulePlies = FENrulePlies;
11898 for( i=0; i< nrCastlingRights; i++ )
11899 initialRights[i] = initial_position[CASTLING][i];
11901 yyboardindex = forwardMostMove;
11902 free(gameInfo.fen);
11903 gameInfo.fen = NULL;
11906 yyboardindex = forwardMostMove;
11907 cm = (ChessMove) Myylex();
11909 /* Handle comments interspersed among the tags */
11910 while (cm == Comment) {
11912 if (appData.debugMode)
11913 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
11915 AppendComment(currentMove, p, FALSE);
11916 yyboardindex = forwardMostMove;
11917 cm = (ChessMove) Myylex();
11921 /* don't rely on existence of Event tag since if game was
11922 * pasted from clipboard the Event tag may not exist
11924 if (numPGNTags > 0){
11926 if (gameInfo.variant == VariantNormal) {
11927 VariantClass v = StringToVariant(gameInfo.event);
11928 // [HGM] do not recognize variants from event tag that were introduced after supporting variant tag
11929 if(v < VariantShogi) gameInfo.variant = v;
11932 if( appData.autoDisplayTags ) {
11933 tags = PGNTags(&gameInfo);
11934 TagsPopUp(tags, CmailMsg());
11939 /* Make something up, but don't display it now */
11944 if (cm == PositionDiagram) {
11947 Board initial_position;
11949 if (appData.debugMode)
11950 fprintf(debugFP, "Parsed PositionDiagram: %s\n", yy_text);
11952 if (!startedFromSetupPosition) {
11954 for (i = BOARD_HEIGHT - 1; i >= 0; i--)
11955 for (j = BOARD_LEFT; j < BOARD_RGHT; p++)
11966 initial_position[i][j++] = CharToPiece(*p);
11969 while (*p == ' ' || *p == '\t' ||
11970 *p == '\n' || *p == '\r') p++;
11972 if (strncmp(p, "black", strlen("black"))==0)
11973 blackPlaysFirst = TRUE;
11975 blackPlaysFirst = FALSE;
11976 startedFromSetupPosition = TRUE;
11978 CopyBoard(boards[0], initial_position);
11979 if (blackPlaysFirst) {
11980 currentMove = forwardMostMove = backwardMostMove = 1;
11981 CopyBoard(boards[1], initial_position);
11982 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
11983 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
11984 timeRemaining[0][1] = whiteTimeRemaining;
11985 timeRemaining[1][1] = blackTimeRemaining;
11986 if (commentList[0] != NULL) {
11987 commentList[1] = commentList[0];
11988 commentList[0] = NULL;
11991 currentMove = forwardMostMove = backwardMostMove = 0;
11994 yyboardindex = forwardMostMove;
11995 cm = (ChessMove) Myylex();
11998 if (first.pr == NoProc) {
11999 StartChessProgram(&first);
12001 InitChessProgram(&first, FALSE);
12002 SendToProgram("force\n", &first);
12003 if (startedFromSetupPosition) {
12004 SendBoard(&first, forwardMostMove);
12005 if (appData.debugMode) {
12006 fprintf(debugFP, "Load Game\n");
12008 DisplayBothClocks();
12011 /* [HGM] server: flag to write setup moves in broadcast file as one */
12012 loadFlag = appData.suppressLoadMoves;
12014 while (cm == Comment) {
12016 if (appData.debugMode)
12017 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
12019 AppendComment(currentMove, p, FALSE);
12020 yyboardindex = forwardMostMove;
12021 cm = (ChessMove) Myylex();
12024 if ((cm == EndOfFile && lastLoadGameStart != EndOfFile ) ||
12025 cm == WhiteWins || cm == BlackWins ||
12026 cm == GameIsDrawn || cm == GameUnfinished) {
12027 DisplayMessage("", _("No moves in game"));
12028 if (cmailMsgLoaded) {
12029 if (appData.debugMode)
12030 fprintf(debugFP, "Setting flipView to %d.\n", FALSE);
12034 DrawPosition(FALSE, boards[currentMove]);
12035 DisplayBothClocks();
12036 gameMode = EditGame;
12043 // [HGM] PV info: routine tests if comment empty
12044 if (!matchMode && (pausing || appData.timeDelay != 0)) {
12045 DisplayComment(currentMove - 1, commentList[currentMove]);
12047 if (!matchMode && appData.timeDelay != 0)
12048 DrawPosition(FALSE, boards[currentMove]);
12050 if (gameMode == AnalyzeFile || gameMode == AnalyzeMode) {
12051 programStats.ok_to_send = 1;
12054 /* if the first token after the PGN tags is a move
12055 * and not move number 1, retrieve it from the parser
12057 if (cm != MoveNumberOne)
12058 LoadGameOneMove(cm);
12060 /* load the remaining moves from the file */
12061 while (LoadGameOneMove(EndOfFile)) {
12062 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
12063 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
12066 /* rewind to the start of the game */
12067 currentMove = backwardMostMove;
12069 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
12071 if (oldGameMode == AnalyzeFile ||
12072 oldGameMode == AnalyzeMode) {
12073 AnalyzeFileEvent();
12076 if (!matchMode && pos >= 0) {
12077 ToNrEvent(pos); // [HGM] no autoplay if selected on position
12079 if (matchMode || appData.timeDelay == 0) {
12081 } else if (appData.timeDelay > 0) {
12082 AutoPlayGameLoop();
12085 if (appData.debugMode)
12086 fprintf(debugFP, "LoadGame(): on exit, gameMode %d\n", gameMode);
12088 loadFlag = 0; /* [HGM] true game starts */
12092 /* Support for LoadNextPosition, LoadPreviousPosition, ReloadSamePosition */
12094 ReloadPosition (int offset)
12096 int positionNumber = lastLoadPositionNumber + offset;
12097 if (lastLoadPositionFP == NULL) {
12098 DisplayError(_("No position has been loaded yet"), 0);
12101 if (positionNumber <= 0) {
12102 DisplayError(_("Can't back up any further"), 0);
12105 return LoadPosition(lastLoadPositionFP, positionNumber,
12106 lastLoadPositionTitle);
12109 /* Load the nth position from the given file */
12111 LoadPositionFromFile (char *filename, int n, char *title)
12116 if (strcmp(filename, "-") == 0) {
12117 return LoadPosition(stdin, n, "stdin");
12119 f = fopen(filename, "rb");
12121 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
12122 DisplayError(buf, errno);
12125 return LoadPosition(f, n, title);
12130 /* Load the nth position from the given open file, and close it */
12132 LoadPosition (FILE *f, int positionNumber, char *title)
12134 char *p, line[MSG_SIZ];
12135 Board initial_position;
12136 int i, j, fenMode, pn;
12138 if (gameMode == Training )
12139 SetTrainingModeOff();
12141 if (gameMode != BeginningOfGame) {
12142 Reset(FALSE, TRUE);
12144 if (lastLoadPositionFP != NULL && lastLoadPositionFP != f) {
12145 fclose(lastLoadPositionFP);
12147 if (positionNumber == 0) positionNumber = 1;
12148 lastLoadPositionFP = f;
12149 lastLoadPositionNumber = positionNumber;
12150 safeStrCpy(lastLoadPositionTitle, title, sizeof(lastLoadPositionTitle)/sizeof(lastLoadPositionTitle[0]));
12151 if (first.pr == NoProc && !appData.noChessProgram) {
12152 StartChessProgram(&first);
12153 InitChessProgram(&first, FALSE);
12155 pn = positionNumber;
12156 if (positionNumber < 0) {
12157 /* Negative position number means to seek to that byte offset */
12158 if (fseek(f, -positionNumber, 0) == -1) {
12159 DisplayError(_("Can't seek on position file"), 0);
12164 if (fseek(f, 0, 0) == -1) {
12165 if (f == lastLoadPositionFP ?
12166 positionNumber == lastLoadPositionNumber + 1 :
12167 positionNumber == 1) {
12170 DisplayError(_("Can't seek on position file"), 0);
12175 /* See if this file is FEN or old-style xboard */
12176 if (fgets(line, MSG_SIZ, f) == NULL) {
12177 DisplayError(_("Position not found in file"), 0);
12180 // [HGM] FEN can begin with digit, any piece letter valid in this variant, or a + for Shogi promoted pieces
12181 fenMode = line[0] >= '0' && line[0] <= '9' || line[0] == '+' || CharToPiece(line[0]) != EmptySquare;
12184 if (fenMode || line[0] == '#') pn--;
12186 /* skip positions before number pn */
12187 if (fgets(line, MSG_SIZ, f) == NULL) {
12189 DisplayError(_("Position not found in file"), 0);
12192 if (fenMode || line[0] == '#') pn--;
12197 if (!ParseFEN(initial_position, &blackPlaysFirst, line)) {
12198 DisplayError(_("Bad FEN position in file"), 0);
12202 (void) fgets(line, MSG_SIZ, f);
12203 (void) fgets(line, MSG_SIZ, f);
12205 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
12206 (void) fgets(line, MSG_SIZ, f);
12207 for (p = line, j = BOARD_LEFT; j < BOARD_RGHT; p++) {
12210 initial_position[i][j++] = CharToPiece(*p);
12214 blackPlaysFirst = FALSE;
12216 (void) fgets(line, MSG_SIZ, f);
12217 if (strncmp(line, "black", strlen("black"))==0)
12218 blackPlaysFirst = TRUE;
12221 startedFromSetupPosition = TRUE;
12223 CopyBoard(boards[0], initial_position);
12224 if (blackPlaysFirst) {
12225 currentMove = forwardMostMove = backwardMostMove = 1;
12226 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
12227 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
12228 CopyBoard(boards[1], initial_position);
12229 DisplayMessage("", _("Black to play"));
12231 currentMove = forwardMostMove = backwardMostMove = 0;
12232 DisplayMessage("", _("White to play"));
12234 initialRulePlies = FENrulePlies; /* [HGM] copy FEN attributes as well */
12235 if(first.pr != NoProc) { // [HGM] in tourney-mode a position can be loaded before the chess engine is installed
12236 SendToProgram("force\n", &first);
12237 SendBoard(&first, forwardMostMove);
12239 if (appData.debugMode) {
12241 for(i=0;i<2;i++){for(j=0;j<6;j++)fprintf(debugFP, " %d", boards[i][CASTLING][j]);fprintf(debugFP,"\n");}
12242 for(j=0;j<6;j++)fprintf(debugFP, " %d", initialRights[j]);fprintf(debugFP,"\n");
12243 fprintf(debugFP, "Load Position\n");
12246 if (positionNumber > 1) {
12247 snprintf(line, MSG_SIZ, "%s %d", title, positionNumber);
12248 DisplayTitle(line);
12250 DisplayTitle(title);
12252 gameMode = EditGame;
12255 timeRemaining[0][1] = whiteTimeRemaining;
12256 timeRemaining[1][1] = blackTimeRemaining;
12257 DrawPosition(FALSE, boards[currentMove]);
12264 CopyPlayerNameIntoFileName (char **dest, char *src)
12266 while (*src != NULLCHAR && *src != ',') {
12271 *(*dest)++ = *src++;
12277 DefaultFileName (char *ext)
12279 static char def[MSG_SIZ];
12282 if (gameInfo.white != NULL && gameInfo.white[0] != '-') {
12284 CopyPlayerNameIntoFileName(&p, gameInfo.white);
12286 CopyPlayerNameIntoFileName(&p, gameInfo.black);
12288 safeStrCpy(p, ext, MSG_SIZ-2-strlen(gameInfo.white)-strlen(gameInfo.black));
12295 /* Save the current game to the given file */
12297 SaveGameToFile (char *filename, int append)
12301 int result, i, t,tot=0;
12303 if (strcmp(filename, "-") == 0) {
12304 return SaveGame(stdout, 0, NULL);
12306 for(i=0; i<10; i++) { // upto 10 tries
12307 f = fopen(filename, append ? "a" : "w");
12308 if(f && i) fprintf(f, "[Delay \"%d retries, %d msec\"]\n",i,tot);
12309 if(f || errno != 13) break;
12310 DoSleep(t = 5 + random()%11); // wait 5-15 msec
12314 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
12315 DisplayError(buf, errno);
12318 safeStrCpy(buf, lastMsg, MSG_SIZ);
12319 DisplayMessage(_("Waiting for access to save file"), "");
12320 flock(fileno(f), LOCK_EX); // [HGM] lock: lock file while we are writing
12321 DisplayMessage(_("Saving game"), "");
12322 if(lseek(fileno(f), 0, SEEK_END) == -1) DisplayError(_("Bad Seek"), errno); // better safe than sorry...
12323 result = SaveGame(f, 0, NULL);
12324 DisplayMessage(buf, "");
12331 SavePart (char *str)
12333 static char buf[MSG_SIZ];
12336 p = strchr(str, ' ');
12337 if (p == NULL) return str;
12338 strncpy(buf, str, p - str);
12339 buf[p - str] = NULLCHAR;
12343 #define PGN_MAX_LINE 75
12345 #define PGN_SIDE_WHITE 0
12346 #define PGN_SIDE_BLACK 1
12349 FindFirstMoveOutOfBook (int side)
12353 if( backwardMostMove == 0 && ! startedFromSetupPosition) {
12354 int index = backwardMostMove;
12355 int has_book_hit = 0;
12357 if( (index % 2) != side ) {
12361 while( index < forwardMostMove ) {
12362 /* Check to see if engine is in book */
12363 int depth = pvInfoList[index].depth;
12364 int score = pvInfoList[index].score;
12370 else if( score == 0 && depth == 63 ) {
12371 in_book = 1; /* Zappa */
12373 else if( score == 2 && depth == 99 ) {
12374 in_book = 1; /* Abrok */
12377 has_book_hit += in_book;
12393 GetOutOfBookInfo (char * buf)
12397 int offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
12399 oob[0] = FindFirstMoveOutOfBook( PGN_SIDE_WHITE );
12400 oob[1] = FindFirstMoveOutOfBook( PGN_SIDE_BLACK );
12404 if( oob[0] >= 0 || oob[1] >= 0 ) {
12405 for( i=0; i<2; i++ ) {
12409 if( i > 0 && oob[0] >= 0 ) {
12410 strcat( buf, " " );
12413 sprintf( buf+strlen(buf), "%d%s. ", (idx - offset)/2 + 1, idx & 1 ? ".." : "" );
12414 sprintf( buf+strlen(buf), "%s%.2f",
12415 pvInfoList[idx].score >= 0 ? "+" : "",
12416 pvInfoList[idx].score / 100.0 );
12422 /* Save game in PGN style and close the file */
12424 SaveGamePGN (FILE *f)
12426 int i, offset, linelen, newblock;
12430 int movelen, numlen, blank;
12431 char move_buffer[100]; /* [AS] Buffer for move+PV info */
12433 offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
12435 tm = time((time_t *) NULL);
12437 PrintPGNTags(f, &gameInfo);
12439 if(appData.numberTag && matchMode) fprintf(f, "[Number \"%d\"]\n", nextGame+1); // [HGM] number tag
12441 if (backwardMostMove > 0 || startedFromSetupPosition) {
12442 char *fen = PositionToFEN(backwardMostMove, NULL);
12443 fprintf(f, "[FEN \"%s\"]\n[SetUp \"1\"]\n", fen);
12444 fprintf(f, "\n{--------------\n");
12445 PrintPosition(f, backwardMostMove);
12446 fprintf(f, "--------------}\n");
12450 /* [AS] Out of book annotation */
12451 if( appData.saveOutOfBookInfo ) {
12454 GetOutOfBookInfo( buf );
12456 if( buf[0] != '\0' ) {
12457 fprintf( f, "[%s \"%s\"]\n", PGN_OUT_OF_BOOK, buf );
12464 i = backwardMostMove;
12468 while (i < forwardMostMove) {
12469 /* Print comments preceding this move */
12470 if (commentList[i] != NULL) {
12471 if (linelen > 0) fprintf(f, "\n");
12472 fprintf(f, "%s", commentList[i]);
12477 /* Format move number */
12479 snprintf(numtext, sizeof(numtext)/sizeof(numtext[0]),"%d.", (i - offset)/2 + 1);
12482 snprintf(numtext, sizeof(numtext)/sizeof(numtext[0]), "%d...", (i - offset)/2 + 1);
12484 numtext[0] = NULLCHAR;
12486 numlen = strlen(numtext);
12489 /* Print move number */
12490 blank = linelen > 0 && numlen > 0;
12491 if (linelen + (blank ? 1 : 0) + numlen > PGN_MAX_LINE) {
12500 fprintf(f, "%s", numtext);
12504 safeStrCpy(move_buffer, SavePart(parseList[i]), sizeof(move_buffer)/sizeof(move_buffer[0])); // [HGM] pgn: print move via buffer, so it can be edited
12505 movelen = strlen(move_buffer); /* [HGM] pgn: line-break point before move */
12508 blank = linelen > 0 && movelen > 0;
12509 if (linelen + (blank ? 1 : 0) + movelen > PGN_MAX_LINE) {
12518 fprintf(f, "%s", move_buffer);
12519 linelen += movelen;
12521 /* [AS] Add PV info if present */
12522 if( i >= 0 && appData.saveExtendedInfoInPGN && pvInfoList[i].depth > 0 ) {
12523 /* [HGM] add time */
12524 char buf[MSG_SIZ]; int seconds;
12526 seconds = (pvInfoList[i].time+5)/10; // deci-seconds, rounded to nearest
12532 snprintf(buf, MSG_SIZ, " %3.1f%c", seconds/10., 0);
12535 seconds = (seconds + 4)/10; // round to full seconds
12537 snprintf(buf, MSG_SIZ, " %d%c", seconds, 0);
12539 snprintf(buf, MSG_SIZ, " %d:%02d%c", seconds/60, seconds%60, 0);
12542 snprintf( move_buffer, sizeof(move_buffer)/sizeof(move_buffer[0]),"{%s%.2f/%d%s}",
12543 pvInfoList[i].score >= 0 ? "+" : "",
12544 pvInfoList[i].score / 100.0,
12545 pvInfoList[i].depth,
12548 movelen = strlen(move_buffer); /* [HGM] pgn: line-break point after move */
12550 /* Print score/depth */
12551 blank = linelen > 0 && movelen > 0;
12552 if (linelen + (blank ? 1 : 0) + movelen > PGN_MAX_LINE) {
12561 fprintf(f, "%s", move_buffer);
12562 linelen += movelen;
12568 /* Start a new line */
12569 if (linelen > 0) fprintf(f, "\n");
12571 /* Print comments after last move */
12572 if (commentList[i] != NULL) {
12573 fprintf(f, "%s\n", commentList[i]);
12577 if (gameInfo.resultDetails != NULL &&
12578 gameInfo.resultDetails[0] != NULLCHAR) {
12579 fprintf(f, "{%s} %s\n\n", gameInfo.resultDetails,
12580 PGNResult(gameInfo.result));
12582 fprintf(f, "%s\n\n", PGNResult(gameInfo.result));
12586 lastSavedGame = GameCheckSum(); // [HGM] save: remember ID of last saved game to prevent double saving
12590 /* Save game in old style and close the file */
12592 SaveGameOldStyle (FILE *f)
12597 tm = time((time_t *) NULL);
12599 fprintf(f, "# %s game file -- %s", programName, ctime(&tm));
12602 if (backwardMostMove > 0 || startedFromSetupPosition) {
12603 fprintf(f, "\n[--------------\n");
12604 PrintPosition(f, backwardMostMove);
12605 fprintf(f, "--------------]\n");
12610 i = backwardMostMove;
12611 offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
12613 while (i < forwardMostMove) {
12614 if (commentList[i] != NULL) {
12615 fprintf(f, "[%s]\n", commentList[i]);
12618 if ((i % 2) == 1) {
12619 fprintf(f, "%d. ... %s\n", (i - offset)/2 + 1, parseList[i]);
12622 fprintf(f, "%d. %s ", (i - offset)/2 + 1, parseList[i]);
12624 if (commentList[i] != NULL) {
12628 if (i >= forwardMostMove) {
12632 fprintf(f, "%s\n", parseList[i]);
12637 if (commentList[i] != NULL) {
12638 fprintf(f, "[%s]\n", commentList[i]);
12641 /* This isn't really the old style, but it's close enough */
12642 if (gameInfo.resultDetails != NULL &&
12643 gameInfo.resultDetails[0] != NULLCHAR) {
12644 fprintf(f, "%s (%s)\n\n", PGNResult(gameInfo.result),
12645 gameInfo.resultDetails);
12647 fprintf(f, "%s\n\n", PGNResult(gameInfo.result));
12654 /* Save the current game to open file f and close the file */
12656 SaveGame (FILE *f, int dummy, char *dummy2)
12658 if (gameMode == EditPosition) EditPositionDone(TRUE);
12659 lastSavedGame = GameCheckSum(); // [HGM] save: remember ID of last saved game to prevent double saving
12660 if (appData.oldSaveStyle)
12661 return SaveGameOldStyle(f);
12663 return SaveGamePGN(f);
12666 /* Save the current position to the given file */
12668 SavePositionToFile (char *filename)
12673 if (strcmp(filename, "-") == 0) {
12674 return SavePosition(stdout, 0, NULL);
12676 f = fopen(filename, "a");
12678 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
12679 DisplayError(buf, errno);
12682 safeStrCpy(buf, lastMsg, MSG_SIZ);
12683 DisplayMessage(_("Waiting for access to save file"), "");
12684 flock(fileno(f), LOCK_EX); // [HGM] lock
12685 DisplayMessage(_("Saving position"), "");
12686 lseek(fileno(f), 0, SEEK_END); // better safe than sorry...
12687 SavePosition(f, 0, NULL);
12688 DisplayMessage(buf, "");
12694 /* Save the current position to the given open file and close the file */
12696 SavePosition (FILE *f, int dummy, char *dummy2)
12701 if (gameMode == EditPosition) EditPositionDone(TRUE);
12702 if (appData.oldSaveStyle) {
12703 tm = time((time_t *) NULL);
12705 fprintf(f, "# %s position file -- %s", programName, ctime(&tm));
12707 fprintf(f, "[--------------\n");
12708 PrintPosition(f, currentMove);
12709 fprintf(f, "--------------]\n");
12711 fen = PositionToFEN(currentMove, NULL);
12712 fprintf(f, "%s\n", fen);
12720 ReloadCmailMsgEvent (int unregister)
12723 static char *inFilename = NULL;
12724 static char *outFilename;
12726 struct stat inbuf, outbuf;
12729 /* Any registered moves are unregistered if unregister is set, */
12730 /* i.e. invoked by the signal handler */
12732 for (i = 0; i < CMAIL_MAX_GAMES; i ++) {
12733 cmailMoveRegistered[i] = FALSE;
12734 if (cmailCommentList[i] != NULL) {
12735 free(cmailCommentList[i]);
12736 cmailCommentList[i] = NULL;
12739 nCmailMovesRegistered = 0;
12742 for (i = 0; i < CMAIL_MAX_GAMES; i ++) {
12743 cmailResult[i] = CMAIL_NOT_RESULT;
12747 if (inFilename == NULL) {
12748 /* Because the filenames are static they only get malloced once */
12749 /* and they never get freed */
12750 inFilename = (char *) malloc(strlen(appData.cmailGameName) + 9);
12751 sprintf(inFilename, "%s.game.in", appData.cmailGameName);
12753 outFilename = (char *) malloc(strlen(appData.cmailGameName) + 5);
12754 sprintf(outFilename, "%s.out", appData.cmailGameName);
12757 status = stat(outFilename, &outbuf);
12759 cmailMailedMove = FALSE;
12761 status = stat(inFilename, &inbuf);
12762 cmailMailedMove = (inbuf.st_mtime < outbuf.st_mtime);
12765 /* LoadGameFromFile(CMAIL_MAX_GAMES) with cmailMsgLoaded == TRUE
12766 counts the games, notes how each one terminated, etc.
12768 It would be nice to remove this kludge and instead gather all
12769 the information while building the game list. (And to keep it
12770 in the game list nodes instead of having a bunch of fixed-size
12771 parallel arrays.) Note this will require getting each game's
12772 termination from the PGN tags, as the game list builder does
12773 not process the game moves. --mann
12775 cmailMsgLoaded = TRUE;
12776 LoadGameFromFile(inFilename, CMAIL_MAX_GAMES, "", FALSE);
12778 /* Load first game in the file or popup game menu */
12779 LoadGameFromFile(inFilename, 0, appData.cmailGameName, TRUE);
12781 #endif /* !WIN32 */
12789 char string[MSG_SIZ];
12791 if ( cmailMailedMove
12792 || (cmailResult[lastLoadGameNumber - 1] == CMAIL_OLD_RESULT)) {
12793 return TRUE; /* Allow free viewing */
12796 /* Unregister move to ensure that we don't leave RegisterMove */
12797 /* with the move registered when the conditions for registering no */
12799 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
12800 cmailMoveRegistered[lastLoadGameNumber - 1] = FALSE;
12801 nCmailMovesRegistered --;
12803 if (cmailCommentList[lastLoadGameNumber - 1] != NULL)
12805 free(cmailCommentList[lastLoadGameNumber - 1]);
12806 cmailCommentList[lastLoadGameNumber - 1] = NULL;
12810 if (cmailOldMove == -1) {
12811 DisplayError(_("You have edited the game history.\nUse Reload Same Game and make your move again."), 0);
12815 if (currentMove > cmailOldMove + 1) {
12816 DisplayError(_("You have entered too many moves.\nBack up to the correct position and try again."), 0);
12820 if (currentMove < cmailOldMove) {
12821 DisplayError(_("Displayed position is not current.\nStep forward to the correct position and try again."), 0);
12825 if (forwardMostMove > currentMove) {
12826 /* Silently truncate extra moves */
12830 if ( (currentMove == cmailOldMove + 1)
12831 || ( (currentMove == cmailOldMove)
12832 && ( (cmailMoveType[lastLoadGameNumber - 1] == CMAIL_ACCEPT)
12833 || (cmailMoveType[lastLoadGameNumber - 1] == CMAIL_RESIGN)))) {
12834 if (gameInfo.result != GameUnfinished) {
12835 cmailResult[lastLoadGameNumber - 1] = CMAIL_NEW_RESULT;
12838 if (commentList[currentMove] != NULL) {
12839 cmailCommentList[lastLoadGameNumber - 1]
12840 = StrSave(commentList[currentMove]);
12842 safeStrCpy(cmailMove[lastLoadGameNumber - 1], moveList[currentMove - 1], sizeof(cmailMove[lastLoadGameNumber - 1])/sizeof(cmailMove[lastLoadGameNumber - 1][0]));
12844 if (appData.debugMode)
12845 fprintf(debugFP, "Saving %s for game %d\n",
12846 cmailMove[lastLoadGameNumber - 1], lastLoadGameNumber);
12848 snprintf(string, MSG_SIZ, "%s.game.out.%d", appData.cmailGameName, lastLoadGameNumber);
12850 f = fopen(string, "w");
12851 if (appData.oldSaveStyle) {
12852 SaveGameOldStyle(f); /* also closes the file */
12854 snprintf(string, MSG_SIZ, "%s.pos.out", appData.cmailGameName);
12855 f = fopen(string, "w");
12856 SavePosition(f, 0, NULL); /* also closes the file */
12858 fprintf(f, "{--------------\n");
12859 PrintPosition(f, currentMove);
12860 fprintf(f, "--------------}\n\n");
12862 SaveGame(f, 0, NULL); /* also closes the file*/
12865 cmailMoveRegistered[lastLoadGameNumber - 1] = TRUE;
12866 nCmailMovesRegistered ++;
12867 } else if (nCmailGames == 1) {
12868 DisplayError(_("You have not made a move yet"), 0);
12879 static char *partCommandString = "cmail -xv%s -remail -game %s 2>&1";
12880 FILE *commandOutput;
12881 char buffer[MSG_SIZ], msg[MSG_SIZ], string[MSG_SIZ];
12882 int nBytes = 0; /* Suppress warnings on uninitialized variables */
12888 if (! cmailMsgLoaded) {
12889 DisplayError(_("The cmail message is not loaded.\nUse Reload CMail Message and make your move again."), 0);
12893 if (nCmailGames == nCmailResults) {
12894 DisplayError(_("No unfinished games"), 0);
12898 #if CMAIL_PROHIBIT_REMAIL
12899 if (cmailMailedMove) {
12900 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);
12901 DisplayError(msg, 0);
12906 if (! (cmailMailedMove || RegisterMove())) return;
12908 if ( cmailMailedMove
12909 || (nCmailMovesRegistered + nCmailResults == nCmailGames)) {
12910 snprintf(string, MSG_SIZ, partCommandString,
12911 appData.debugMode ? " -v" : "", appData.cmailGameName);
12912 commandOutput = popen(string, "r");
12914 if (commandOutput == NULL) {
12915 DisplayError(_("Failed to invoke cmail"), 0);
12917 for (nBuffers = 0; (! feof(commandOutput)); nBuffers ++) {
12918 nBytes = fread(buffer, 1, MSG_SIZ - 1, commandOutput);
12920 if (nBuffers > 1) {
12921 (void) memcpy(msg, buffer + nBytes, MSG_SIZ - nBytes - 1);
12922 (void) memcpy(msg + MSG_SIZ - nBytes - 1, buffer, nBytes);
12923 nBytes = MSG_SIZ - 1;
12925 (void) memcpy(msg, buffer, nBytes);
12927 *(msg + nBytes) = '\0'; /* \0 for end-of-string*/
12929 if(StrStr(msg, "Mailed cmail message to ") != NULL) {
12930 cmailMailedMove = TRUE; /* Prevent >1 moves */
12933 for (i = 0; i < nCmailGames; i ++) {
12934 if (cmailResult[i] == CMAIL_NOT_RESULT) {
12939 && ( (arcDir = (char *) getenv("CMAIL_ARCDIR"))
12941 snprintf(buffer, MSG_SIZ, "%s/%s.%s.archive",
12943 appData.cmailGameName,
12945 LoadGameFromFile(buffer, 1, buffer, FALSE);
12946 cmailMsgLoaded = FALSE;
12950 DisplayInformation(msg);
12951 pclose(commandOutput);
12954 if ((*cmailMsg) != '\0') {
12955 DisplayInformation(cmailMsg);
12960 #endif /* !WIN32 */
12969 int prependComma = 0;
12971 char string[MSG_SIZ]; /* Space for game-list */
12974 if (!cmailMsgLoaded) return "";
12976 if (cmailMailedMove) {
12977 snprintf(cmailMsg, MSG_SIZ, _("Waiting for reply from opponent\n"));
12979 /* Create a list of games left */
12980 snprintf(string, MSG_SIZ, "[");
12981 for (i = 0; i < nCmailGames; i ++) {
12982 if (! ( cmailMoveRegistered[i]
12983 || (cmailResult[i] == CMAIL_OLD_RESULT))) {
12984 if (prependComma) {
12985 snprintf(number, sizeof(number)/sizeof(number[0]), ",%d", i + 1);
12987 snprintf(number, sizeof(number)/sizeof(number[0]), "%d", i + 1);
12991 strcat(string, number);
12994 strcat(string, "]");
12996 if (nCmailMovesRegistered + nCmailResults == 0) {
12997 switch (nCmailGames) {
12999 snprintf(cmailMsg, MSG_SIZ, _("Still need to make move for game\n"));
13003 snprintf(cmailMsg, MSG_SIZ, _("Still need to make moves for both games\n"));
13007 snprintf(cmailMsg, MSG_SIZ, _("Still need to make moves for all %d games\n"),
13012 switch (nCmailGames - nCmailMovesRegistered - nCmailResults) {
13014 snprintf(cmailMsg, MSG_SIZ, _("Still need to make a move for game %s\n"),
13019 if (nCmailResults == nCmailGames) {
13020 snprintf(cmailMsg, MSG_SIZ, _("No unfinished games\n"));
13022 snprintf(cmailMsg, MSG_SIZ, _("Ready to send mail\n"));
13027 snprintf(cmailMsg, MSG_SIZ, _("Still need to make moves for games %s\n"),
13039 if (gameMode == Training)
13040 SetTrainingModeOff();
13043 cmailMsgLoaded = FALSE;
13044 if (appData.icsActive) {
13045 SendToICS(ics_prefix);
13046 SendToICS("refresh\n");
13051 ExitEvent (int status)
13055 /* Give up on clean exit */
13059 /* Keep trying for clean exit */
13063 if (appData.icsActive && appData.colorize) Colorize(ColorNone, FALSE);
13065 if (telnetISR != NULL) {
13066 RemoveInputSource(telnetISR);
13068 if (icsPR != NoProc) {
13069 DestroyChildProcess(icsPR, TRUE);
13072 /* [HGM] crash: leave writing PGN and position entirely to GameEnds() */
13073 GameEnds(gameInfo.result, gameInfo.resultDetails==NULL ? "xboard exit" : gameInfo.resultDetails, GE_PLAYER);
13075 /* [HGM] crash: the above GameEnds() is a dud if another one was running */
13076 /* make sure this other one finishes before killing it! */
13077 if(endingGame) { int count = 0;
13078 if(appData.debugMode) fprintf(debugFP, "ExitEvent() during GameEnds(), wait\n");
13079 while(endingGame && count++ < 10) DoSleep(1);
13080 if(appData.debugMode && endingGame) fprintf(debugFP, "GameEnds() seems stuck, proceed exiting\n");
13083 /* Kill off chess programs */
13084 if (first.pr != NoProc) {
13087 DoSleep( appData.delayBeforeQuit );
13088 SendToProgram("quit\n", &first);
13089 DoSleep( appData.delayAfterQuit );
13090 DestroyChildProcess(first.pr, 10 /* [AS] first.useSigterm */ );
13092 if (second.pr != NoProc) {
13093 DoSleep( appData.delayBeforeQuit );
13094 SendToProgram("quit\n", &second);
13095 DoSleep( appData.delayAfterQuit );
13096 DestroyChildProcess(second.pr, 10 /* [AS] second.useSigterm */ );
13098 if (first.isr != NULL) {
13099 RemoveInputSource(first.isr);
13101 if (second.isr != NULL) {
13102 RemoveInputSource(second.isr);
13105 if (pairing.pr != NoProc) SendToProgram("quit\n", &pairing);
13106 if (pairing.isr != NULL) RemoveInputSource(pairing.isr);
13108 ShutDownFrontEnd();
13115 if (appData.debugMode)
13116 fprintf(debugFP, "PauseEvent(): pausing %d\n", pausing);
13120 if (gameMode == MachinePlaysWhite ||
13121 gameMode == MachinePlaysBlack) {
13124 DisplayBothClocks();
13126 if (gameMode == PlayFromGameFile) {
13127 if (appData.timeDelay >= 0)
13128 AutoPlayGameLoop();
13129 } else if (gameMode == IcsExamining && pauseExamInvalid) {
13130 Reset(FALSE, TRUE);
13131 SendToICS(ics_prefix);
13132 SendToICS("refresh\n");
13133 } else if (currentMove < forwardMostMove) {
13134 ForwardInner(forwardMostMove);
13136 pauseExamInvalid = FALSE;
13138 switch (gameMode) {
13142 pauseExamForwardMostMove = forwardMostMove;
13143 pauseExamInvalid = FALSE;
13146 case IcsPlayingWhite:
13147 case IcsPlayingBlack:
13151 case PlayFromGameFile:
13152 (void) StopLoadGameTimer();
13156 case BeginningOfGame:
13157 if (appData.icsActive) return;
13158 /* else fall through */
13159 case MachinePlaysWhite:
13160 case MachinePlaysBlack:
13161 case TwoMachinesPlay:
13162 if (forwardMostMove == 0)
13163 return; /* don't pause if no one has moved */
13164 if ((gameMode == MachinePlaysWhite &&
13165 !WhiteOnMove(forwardMostMove)) ||
13166 (gameMode == MachinePlaysBlack &&
13167 WhiteOnMove(forwardMostMove))) {
13178 EditCommentEvent ()
13180 char title[MSG_SIZ];
13182 if (currentMove < 1 || parseList[currentMove - 1][0] == NULLCHAR) {
13183 safeStrCpy(title, _("Edit comment"), sizeof(title)/sizeof(title[0]));
13185 snprintf(title, MSG_SIZ, _("Edit comment on %d.%s%s"), (currentMove - 1) / 2 + 1,
13186 WhiteOnMove(currentMove - 1) ? " " : ".. ",
13187 parseList[currentMove - 1]);
13190 EditCommentPopUp(currentMove, title, commentList[currentMove]);
13197 char *tags = PGNTags(&gameInfo);
13199 EditTagsPopUp(tags, NULL);
13204 AnalyzeModeEvent ()
13206 if (appData.noChessProgram || gameMode == AnalyzeMode)
13209 if (gameMode != AnalyzeFile) {
13210 if (!appData.icsEngineAnalyze) {
13212 if (gameMode != EditGame) return;
13214 ResurrectChessProgram();
13215 SendToProgram("analyze\n", &first);
13216 first.analyzing = TRUE;
13217 /*first.maybeThinking = TRUE;*/
13218 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
13219 EngineOutputPopUp();
13221 if (!appData.icsEngineAnalyze) gameMode = AnalyzeMode;
13226 StartAnalysisClock();
13227 GetTimeMark(&lastNodeCountTime);
13232 AnalyzeFileEvent ()
13234 if (appData.noChessProgram || gameMode == AnalyzeFile)
13237 if (gameMode != AnalyzeMode) {
13239 if (gameMode != EditGame) return;
13240 ResurrectChessProgram();
13241 SendToProgram("analyze\n", &first);
13242 first.analyzing = TRUE;
13243 /*first.maybeThinking = TRUE;*/
13244 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
13245 EngineOutputPopUp();
13247 gameMode = AnalyzeFile;
13252 StartAnalysisClock();
13253 GetTimeMark(&lastNodeCountTime);
13255 if(appData.timeDelay > 0) StartLoadGameTimer((long)(1000.0 * appData.timeDelay));
13259 MachineWhiteEvent ()
13262 char *bookHit = NULL;
13264 if (appData.noChessProgram || (gameMode == MachinePlaysWhite))
13268 if (gameMode == PlayFromGameFile ||
13269 gameMode == TwoMachinesPlay ||
13270 gameMode == Training ||
13271 gameMode == AnalyzeMode ||
13272 gameMode == EndOfGame)
13275 if (gameMode == EditPosition)
13276 EditPositionDone(TRUE);
13278 if (!WhiteOnMove(currentMove)) {
13279 DisplayError(_("It is not White's turn"), 0);
13283 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile)
13286 if (gameMode == EditGame || gameMode == AnalyzeMode ||
13287 gameMode == AnalyzeFile)
13290 ResurrectChessProgram(); /* in case it isn't running */
13291 if(gameMode == BeginningOfGame) { /* [HGM] time odds: to get right odds in human mode */
13292 gameMode = MachinePlaysWhite;
13295 gameMode = MachinePlaysWhite;
13299 snprintf(buf, MSG_SIZ, "%s %s %s", gameInfo.white, _("vs."), gameInfo.black);
13301 if (first.sendName) {
13302 snprintf(buf, MSG_SIZ, "name %s\n", gameInfo.black);
13303 SendToProgram(buf, &first);
13305 if (first.sendTime) {
13306 if (first.useColors) {
13307 SendToProgram("black\n", &first); /*gnu kludge*/
13309 SendTimeRemaining(&first, TRUE);
13311 if (first.useColors) {
13312 SendToProgram("white\n", &first); // [HGM] book: send 'go' separately
13314 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: send go or retrieve book move
13315 SetMachineThinkingEnables();
13316 first.maybeThinking = TRUE;
13320 if (appData.autoFlipView && !flipView) {
13321 flipView = !flipView;
13322 DrawPosition(FALSE, NULL);
13323 DisplayBothClocks(); // [HGM] logo: clocks might have to be exchanged;
13326 if(bookHit) { // [HGM] book: simulate book reply
13327 static char bookMove[MSG_SIZ]; // a bit generous?
13329 programStats.nodes = programStats.depth = programStats.time =
13330 programStats.score = programStats.got_only_move = 0;
13331 sprintf(programStats.movelist, "%s (xbook)", bookHit);
13333 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
13334 strcat(bookMove, bookHit);
13335 HandleMachineMove(bookMove, &first);
13340 MachineBlackEvent ()
13343 char *bookHit = NULL;
13345 if (appData.noChessProgram || (gameMode == MachinePlaysBlack))
13349 if (gameMode == PlayFromGameFile ||
13350 gameMode == TwoMachinesPlay ||
13351 gameMode == Training ||
13352 gameMode == AnalyzeMode ||
13353 gameMode == EndOfGame)
13356 if (gameMode == EditPosition)
13357 EditPositionDone(TRUE);
13359 if (WhiteOnMove(currentMove)) {
13360 DisplayError(_("It is not Black's turn"), 0);
13364 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile)
13367 if (gameMode == EditGame || gameMode == AnalyzeMode ||
13368 gameMode == AnalyzeFile)
13371 ResurrectChessProgram(); /* in case it isn't running */
13372 gameMode = MachinePlaysBlack;
13376 snprintf(buf, MSG_SIZ, "%s %s %s", gameInfo.white, _("vs."), gameInfo.black);
13378 if (first.sendName) {
13379 snprintf(buf, MSG_SIZ, "name %s\n", gameInfo.white);
13380 SendToProgram(buf, &first);
13382 if (first.sendTime) {
13383 if (first.useColors) {
13384 SendToProgram("white\n", &first); /*gnu kludge*/
13386 SendTimeRemaining(&first, FALSE);
13388 if (first.useColors) {
13389 SendToProgram("black\n", &first); // [HGM] book: 'go' sent separately
13391 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: send go or retrieve book move
13392 SetMachineThinkingEnables();
13393 first.maybeThinking = TRUE;
13396 if (appData.autoFlipView && flipView) {
13397 flipView = !flipView;
13398 DrawPosition(FALSE, NULL);
13399 DisplayBothClocks(); // [HGM] logo: clocks might have to be exchanged;
13401 if(bookHit) { // [HGM] book: simulate book reply
13402 static char bookMove[MSG_SIZ]; // a bit generous?
13404 programStats.nodes = programStats.depth = programStats.time =
13405 programStats.score = programStats.got_only_move = 0;
13406 sprintf(programStats.movelist, "%s (xbook)", bookHit);
13408 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
13409 strcat(bookMove, bookHit);
13410 HandleMachineMove(bookMove, &first);
13416 DisplayTwoMachinesTitle ()
13419 if (appData.matchGames > 0) {
13420 if(appData.tourneyFile[0]) {
13421 snprintf(buf, MSG_SIZ, "%s %s %s (%d/%d%s)",
13422 gameInfo.white, _("vs."), gameInfo.black,
13423 nextGame+1, appData.matchGames+1,
13424 appData.tourneyType>0 ? "gt" : appData.tourneyType<0 ? "sw" : "rr");
13426 if (first.twoMachinesColor[0] == 'w') {
13427 snprintf(buf, MSG_SIZ, "%s %s %s (%d-%d-%d)",
13428 gameInfo.white, _("vs."), gameInfo.black,
13429 first.matchWins, second.matchWins,
13430 matchGame - 1 - (first.matchWins + second.matchWins));
13432 snprintf(buf, MSG_SIZ, "%s %s %s (%d-%d-%d)",
13433 gameInfo.white, _("vs."), gameInfo.black,
13434 second.matchWins, first.matchWins,
13435 matchGame - 1 - (first.matchWins + second.matchWins));
13438 snprintf(buf, MSG_SIZ, "%s %s %s", gameInfo.white, _("vs."), gameInfo.black);
13444 SettingsMenuIfReady ()
13446 if (second.lastPing != second.lastPong) {
13447 DisplayMessage("", _("Waiting for second chess program"));
13448 ScheduleDelayedEvent(SettingsMenuIfReady, 10); // [HGM] fast: lowered from 1000
13452 DisplayMessage("", "");
13453 SettingsPopUp(&second);
13457 WaitForEngine (ChessProgramState *cps, DelayedEventCallback retry)
13460 if (cps->pr == NoProc) {
13461 StartChessProgram(cps);
13462 if (cps->protocolVersion == 1) {
13465 /* kludge: allow timeout for initial "feature" command */
13467 snprintf(buf, MSG_SIZ, _("Starting %s chess program"), _(cps->which));
13468 DisplayMessage("", buf);
13469 ScheduleDelayedEvent(retry, FEATURE_TIMEOUT);
13477 TwoMachinesEvent P((void))
13481 ChessProgramState *onmove;
13482 char *bookHit = NULL;
13483 static int stalling = 0;
13487 if (appData.noChessProgram) return;
13489 switch (gameMode) {
13490 case TwoMachinesPlay:
13492 case MachinePlaysWhite:
13493 case MachinePlaysBlack:
13494 if (WhiteOnMove(forwardMostMove) == (gameMode == MachinePlaysWhite)) {
13495 DisplayError(_("Wait until your turn,\nor select Move Now"), 0);
13499 case BeginningOfGame:
13500 case PlayFromGameFile:
13503 if (gameMode != EditGame) return;
13506 EditPositionDone(TRUE);
13517 // forwardMostMove = currentMove;
13518 TruncateGame(); // [HGM] vari: MachineWhite and MachineBlack do this...
13520 if(!ResurrectChessProgram()) return; /* in case first program isn't running (unbalances its ping due to InitChessProgram!) */
13522 if(WaitForEngine(&second, TwoMachinesEventIfReady)) return; // (if needed:) started up second engine, so wait for features
13523 if(first.lastPing != first.lastPong) { // [HGM] wait till we are sure first engine has set up position
13524 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10);
13528 InitChessProgram(&second, FALSE); // unbalances ping of second engine
13529 SendToProgram("force\n", &second);
13531 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10);
13534 GetTimeMark(&now); // [HGM] matchpause: implement match pause after engine load
13535 if(appData.matchPause>10000 || appData.matchPause<10)
13536 appData.matchPause = 10000; /* [HGM] make pause adjustable */
13537 wait = SubtractTimeMarks(&now, &pauseStart);
13538 if(wait < appData.matchPause) {
13539 ScheduleDelayedEvent(TwoMachinesEventIfReady, appData.matchPause - wait);
13542 // we are now committed to starting the game
13544 DisplayMessage("", "");
13545 if (startedFromSetupPosition) {
13546 SendBoard(&second, backwardMostMove);
13547 if (appData.debugMode) {
13548 fprintf(debugFP, "Two Machines\n");
13551 for (i = backwardMostMove; i < forwardMostMove; i++) {
13552 SendMoveToProgram(i, &second);
13555 gameMode = TwoMachinesPlay;
13557 ModeHighlight(); // [HGM] logo: this triggers display update of logos
13559 DisplayTwoMachinesTitle();
13561 if ((first.twoMachinesColor[0] == 'w') == WhiteOnMove(forwardMostMove)) {
13566 if(appData.debugMode) fprintf(debugFP, "New game (%d): %s-%s (%c)\n", matchGame, first.tidy, second.tidy, first.twoMachinesColor[0]);
13567 SendToProgram(first.computerString, &first);
13568 if (first.sendName) {
13569 snprintf(buf, MSG_SIZ, "name %s\n", second.tidy);
13570 SendToProgram(buf, &first);
13572 SendToProgram(second.computerString, &second);
13573 if (second.sendName) {
13574 snprintf(buf, MSG_SIZ, "name %s\n", first.tidy);
13575 SendToProgram(buf, &second);
13579 if (!first.sendTime || !second.sendTime) {
13580 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
13581 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
13583 if (onmove->sendTime) {
13584 if (onmove->useColors) {
13585 SendToProgram(onmove->other->twoMachinesColor, onmove); /*gnu kludge*/
13587 SendTimeRemaining(onmove, WhiteOnMove(forwardMostMove));
13589 if (onmove->useColors) {
13590 SendToProgram(onmove->twoMachinesColor, onmove);
13592 bookHit = SendMoveToBookUser(forwardMostMove-1, onmove, TRUE); // [HGM] book: send go or retrieve book move
13593 // SendToProgram("go\n", onmove);
13594 onmove->maybeThinking = TRUE;
13595 SetMachineThinkingEnables();
13599 if(bookHit) { // [HGM] book: simulate book reply
13600 static char bookMove[MSG_SIZ]; // a bit generous?
13602 programStats.nodes = programStats.depth = programStats.time =
13603 programStats.score = programStats.got_only_move = 0;
13604 sprintf(programStats.movelist, "%s (xbook)", bookHit);
13606 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
13607 strcat(bookMove, bookHit);
13608 savedMessage = bookMove; // args for deferred call
13609 savedState = onmove;
13610 ScheduleDelayedEvent(DeferredBookMove, 1);
13617 if (gameMode == Training) {
13618 SetTrainingModeOff();
13619 gameMode = PlayFromGameFile;
13620 DisplayMessage("", _("Training mode off"));
13622 gameMode = Training;
13623 animateTraining = appData.animate;
13625 /* make sure we are not already at the end of the game */
13626 if (currentMove < forwardMostMove) {
13627 SetTrainingModeOn();
13628 DisplayMessage("", _("Training mode on"));
13630 gameMode = PlayFromGameFile;
13631 DisplayError(_("Already at end of game"), 0);
13640 if (!appData.icsActive) return;
13641 switch (gameMode) {
13642 case IcsPlayingWhite:
13643 case IcsPlayingBlack:
13646 case BeginningOfGame:
13654 EditPositionDone(TRUE);
13667 gameMode = IcsIdle;
13677 switch (gameMode) {
13679 SetTrainingModeOff();
13681 case MachinePlaysWhite:
13682 case MachinePlaysBlack:
13683 case BeginningOfGame:
13684 SendToProgram("force\n", &first);
13685 SetUserThinkingEnables();
13687 case PlayFromGameFile:
13688 (void) StopLoadGameTimer();
13689 if (gameFileFP != NULL) {
13694 EditPositionDone(TRUE);
13699 SendToProgram("force\n", &first);
13701 case TwoMachinesPlay:
13702 GameEnds(EndOfFile, NULL, GE_PLAYER);
13703 ResurrectChessProgram();
13704 SetUserThinkingEnables();
13707 ResurrectChessProgram();
13709 case IcsPlayingBlack:
13710 case IcsPlayingWhite:
13711 DisplayError(_("Warning: You are still playing a game"), 0);
13714 DisplayError(_("Warning: You are still observing a game"), 0);
13717 DisplayError(_("Warning: You are still examining a game"), 0);
13728 first.offeredDraw = second.offeredDraw = 0;
13730 if (gameMode == PlayFromGameFile) {
13731 whiteTimeRemaining = timeRemaining[0][currentMove];
13732 blackTimeRemaining = timeRemaining[1][currentMove];
13736 if (gameMode == MachinePlaysWhite ||
13737 gameMode == MachinePlaysBlack ||
13738 gameMode == TwoMachinesPlay ||
13739 gameMode == EndOfGame) {
13740 i = forwardMostMove;
13741 while (i > currentMove) {
13742 SendToProgram("undo\n", &first);
13745 if(!adjustedClock) {
13746 whiteTimeRemaining = timeRemaining[0][currentMove];
13747 blackTimeRemaining = timeRemaining[1][currentMove];
13748 DisplayBothClocks();
13750 if (whiteFlag || blackFlag) {
13751 whiteFlag = blackFlag = 0;
13756 gameMode = EditGame;
13763 EditPositionEvent ()
13765 if (gameMode == EditPosition) {
13771 if (gameMode != EditGame) return;
13773 gameMode = EditPosition;
13776 if (currentMove > 0)
13777 CopyBoard(boards[0], boards[currentMove]);
13779 blackPlaysFirst = !WhiteOnMove(currentMove);
13781 currentMove = forwardMostMove = backwardMostMove = 0;
13782 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
13784 if(!appData.pieceMenu) DisplayMessage(_("Click clock to clear board"), "");
13790 /* [DM] icsEngineAnalyze - possible call from other functions */
13791 if (appData.icsEngineAnalyze) {
13792 appData.icsEngineAnalyze = FALSE;
13794 DisplayMessage("",_("Close ICS engine analyze..."));
13796 if (first.analysisSupport && first.analyzing) {
13797 SendToProgram("exit\n", &first);
13798 first.analyzing = FALSE;
13800 thinkOutput[0] = NULLCHAR;
13804 EditPositionDone (Boolean fakeRights)
13806 int king = gameInfo.variant == VariantKnightmate ? WhiteUnicorn : WhiteKing;
13808 startedFromSetupPosition = TRUE;
13809 InitChessProgram(&first, FALSE);
13810 if(fakeRights) { // [HGM] suppress this if we just pasted a FEN.
13811 boards[0][EP_STATUS] = EP_NONE;
13812 boards[0][CASTLING][2] = boards[0][CASTLING][5] = BOARD_WIDTH>>1;
13813 if(boards[0][0][BOARD_WIDTH>>1] == king) {
13814 boards[0][CASTLING][1] = boards[0][0][BOARD_LEFT] == WhiteRook ? 0 : NoRights;
13815 boards[0][CASTLING][0] = boards[0][0][BOARD_RGHT-1] == WhiteRook ? BOARD_RGHT-1 : NoRights;
13816 } else boards[0][CASTLING][2] = NoRights;
13817 if(boards[0][BOARD_HEIGHT-1][BOARD_WIDTH>>1] == WHITE_TO_BLACK king) {
13818 boards[0][CASTLING][4] = boards[0][BOARD_HEIGHT-1][BOARD_LEFT] == BlackRook ? 0 : NoRights;
13819 boards[0][CASTLING][3] = boards[0][BOARD_HEIGHT-1][BOARD_RGHT-1] == BlackRook ? BOARD_RGHT-1 : NoRights;
13820 } else boards[0][CASTLING][5] = NoRights;
13822 SendToProgram("force\n", &first);
13823 if (blackPlaysFirst) {
13824 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
13825 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
13826 currentMove = forwardMostMove = backwardMostMove = 1;
13827 CopyBoard(boards[1], boards[0]);
13829 currentMove = forwardMostMove = backwardMostMove = 0;
13831 SendBoard(&first, forwardMostMove);
13832 if (appData.debugMode) {
13833 fprintf(debugFP, "EditPosDone\n");
13836 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
13837 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
13838 gameMode = EditGame;
13840 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
13841 ClearHighlights(); /* [AS] */
13844 /* Pause for `ms' milliseconds */
13845 /* !! Ugh, this is a kludge. Fix it sometime. --tpm */
13847 TimeDelay (long ms)
13854 } while (SubtractTimeMarks(&m2, &m1) < ms);
13857 /* !! Ugh, this is a kludge. Fix it sometime. --tpm */
13859 SendMultiLineToICS (char *buf)
13861 char temp[MSG_SIZ+1], *p;
13868 strncpy(temp, buf, len);
13873 if (*p == '\n' || *p == '\r')
13878 strcat(temp, "\n");
13880 SendToPlayer(temp, strlen(temp));
13884 SetWhiteToPlayEvent ()
13886 if (gameMode == EditPosition) {
13887 blackPlaysFirst = FALSE;
13888 DisplayBothClocks(); /* works because currentMove is 0 */
13889 } else if (gameMode == IcsExamining) {
13890 SendToICS(ics_prefix);
13891 SendToICS("tomove white\n");
13896 SetBlackToPlayEvent ()
13898 if (gameMode == EditPosition) {
13899 blackPlaysFirst = TRUE;
13900 currentMove = 1; /* kludge */
13901 DisplayBothClocks();
13903 } else if (gameMode == IcsExamining) {
13904 SendToICS(ics_prefix);
13905 SendToICS("tomove black\n");
13910 EditPositionMenuEvent (ChessSquare selection, int x, int y)
13913 ChessSquare piece = boards[0][y][x];
13915 if (gameMode != EditPosition && gameMode != IcsExamining) return;
13917 switch (selection) {
13919 if (gameMode == IcsExamining && ics_type == ICS_FICS) {
13920 SendToICS(ics_prefix);
13921 SendToICS("bsetup clear\n");
13922 } else if (gameMode == IcsExamining && ics_type == ICS_ICC) {
13923 SendToICS(ics_prefix);
13924 SendToICS("clearboard\n");
13926 for (x = 0; x < BOARD_WIDTH; x++) { ChessSquare p = EmptySquare;
13927 if(x == BOARD_LEFT-1 || x == BOARD_RGHT) p = (ChessSquare) 0; /* [HGM] holdings */
13928 for (y = 0; y < BOARD_HEIGHT; y++) {
13929 if (gameMode == IcsExamining) {
13930 if (boards[currentMove][y][x] != EmptySquare) {
13931 snprintf(buf, MSG_SIZ, "%sx@%c%c\n", ics_prefix,
13936 boards[0][y][x] = p;
13941 if (gameMode == EditPosition) {
13942 DrawPosition(FALSE, boards[0]);
13947 SetWhiteToPlayEvent();
13951 SetBlackToPlayEvent();
13955 if (gameMode == IcsExamining) {
13956 if (x < BOARD_LEFT || x >= BOARD_RGHT) break; // [HGM] holdings
13957 snprintf(buf, MSG_SIZ, "%sx@%c%c\n", ics_prefix, AAA + x, ONE + y);
13960 if(x < BOARD_LEFT || x >= BOARD_RGHT) {
13961 if(x == BOARD_LEFT-2) {
13962 if(y < BOARD_HEIGHT-1-gameInfo.holdingsSize) break;
13963 boards[0][y][1] = 0;
13965 if(x == BOARD_RGHT+1) {
13966 if(y >= gameInfo.holdingsSize) break;
13967 boards[0][y][BOARD_WIDTH-2] = 0;
13970 boards[0][y][x] = EmptySquare;
13971 DrawPosition(FALSE, boards[0]);
13976 if(piece >= (int)WhitePawn && piece < (int)WhiteMan ||
13977 piece >= (int)BlackPawn && piece < (int)BlackMan ) {
13978 selection = (ChessSquare) (PROMOTED piece);
13979 } else if(piece == EmptySquare) selection = WhiteSilver;
13980 else selection = (ChessSquare)((int)piece - 1);
13984 if(piece > (int)WhiteMan && piece <= (int)WhiteKing ||
13985 piece > (int)BlackMan && piece <= (int)BlackKing ) {
13986 selection = (ChessSquare) (DEMOTED piece);
13987 } else if(piece == EmptySquare) selection = BlackSilver;
13988 else selection = (ChessSquare)((int)piece + 1);
13993 if(gameInfo.variant == VariantShatranj ||
13994 gameInfo.variant == VariantXiangqi ||
13995 gameInfo.variant == VariantCourier ||
13996 gameInfo.variant == VariantMakruk )
13997 selection = (ChessSquare)((int)selection - (int)WhiteQueen + (int)WhiteFerz);
14002 if(gameInfo.variant == VariantXiangqi)
14003 selection = (ChessSquare)((int)selection - (int)WhiteKing + (int)WhiteWazir);
14004 if(gameInfo.variant == VariantKnightmate)
14005 selection = (ChessSquare)((int)selection - (int)WhiteKing + (int)WhiteUnicorn);
14008 if (gameMode == IcsExamining) {
14009 if (x < BOARD_LEFT || x >= BOARD_RGHT) break; // [HGM] holdings
14010 snprintf(buf, MSG_SIZ, "%s%c@%c%c\n", ics_prefix,
14011 PieceToChar(selection), AAA + x, ONE + y);
14014 if(x < BOARD_LEFT || x >= BOARD_RGHT) {
14016 if(x == BOARD_LEFT-2 && selection >= BlackPawn) {
14017 n = PieceToNumber(selection - BlackPawn);
14018 if(n >= gameInfo.holdingsSize) { n = 0; selection = BlackPawn; }
14019 boards[0][BOARD_HEIGHT-1-n][0] = selection;
14020 boards[0][BOARD_HEIGHT-1-n][1]++;
14022 if(x == BOARD_RGHT+1 && selection < BlackPawn) {
14023 n = PieceToNumber(selection);
14024 if(n >= gameInfo.holdingsSize) { n = 0; selection = WhitePawn; }
14025 boards[0][n][BOARD_WIDTH-1] = selection;
14026 boards[0][n][BOARD_WIDTH-2]++;
14029 boards[0][y][x] = selection;
14030 DrawPosition(TRUE, boards[0]);
14032 fromX = fromY = -1;
14040 DropMenuEvent (ChessSquare selection, int x, int y)
14042 ChessMove moveType;
14044 switch (gameMode) {
14045 case IcsPlayingWhite:
14046 case MachinePlaysBlack:
14047 if (!WhiteOnMove(currentMove)) {
14048 DisplayMoveError(_("It is Black's turn"));
14051 moveType = WhiteDrop;
14053 case IcsPlayingBlack:
14054 case MachinePlaysWhite:
14055 if (WhiteOnMove(currentMove)) {
14056 DisplayMoveError(_("It is White's turn"));
14059 moveType = BlackDrop;
14062 moveType = WhiteOnMove(currentMove) ? WhiteDrop : BlackDrop;
14068 if (moveType == BlackDrop && selection < BlackPawn) {
14069 selection = (ChessSquare) ((int) selection
14070 + (int) BlackPawn - (int) WhitePawn);
14072 if (boards[currentMove][y][x] != EmptySquare) {
14073 DisplayMoveError(_("That square is occupied"));
14077 FinishMove(moveType, (int) selection, DROP_RANK, x, y, NULLCHAR);
14083 /* Accept a pending offer of any kind from opponent */
14085 if (appData.icsActive) {
14086 SendToICS(ics_prefix);
14087 SendToICS("accept\n");
14088 } else if (cmailMsgLoaded) {
14089 if (currentMove == cmailOldMove &&
14090 commentList[cmailOldMove] != NULL &&
14091 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
14092 "Black offers a draw" : "White offers a draw")) {
14094 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
14095 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_ACCEPT;
14097 DisplayError(_("There is no pending offer on this move"), 0);
14098 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
14101 /* Not used for offers from chess program */
14108 /* Decline a pending offer of any kind from opponent */
14110 if (appData.icsActive) {
14111 SendToICS(ics_prefix);
14112 SendToICS("decline\n");
14113 } else if (cmailMsgLoaded) {
14114 if (currentMove == cmailOldMove &&
14115 commentList[cmailOldMove] != NULL &&
14116 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
14117 "Black offers a draw" : "White offers a draw")) {
14119 AppendComment(cmailOldMove, "Draw declined", TRUE);
14120 DisplayComment(cmailOldMove - 1, "Draw declined");
14123 DisplayError(_("There is no pending offer on this move"), 0);
14126 /* Not used for offers from chess program */
14133 /* Issue ICS rematch command */
14134 if (appData.icsActive) {
14135 SendToICS(ics_prefix);
14136 SendToICS("rematch\n");
14143 /* Call your opponent's flag (claim a win on time) */
14144 if (appData.icsActive) {
14145 SendToICS(ics_prefix);
14146 SendToICS("flag\n");
14148 switch (gameMode) {
14151 case MachinePlaysWhite:
14154 GameEnds(GameIsDrawn, "Both players ran out of time",
14157 GameEnds(BlackWins, "Black wins on time", GE_PLAYER);
14159 DisplayError(_("Your opponent is not out of time"), 0);
14162 case MachinePlaysBlack:
14165 GameEnds(GameIsDrawn, "Both players ran out of time",
14168 GameEnds(WhiteWins, "White wins on time", GE_PLAYER);
14170 DisplayError(_("Your opponent is not out of time"), 0);
14178 ClockClick (int which)
14179 { // [HGM] code moved to back-end from winboard.c
14180 if(which) { // black clock
14181 if (gameMode == EditPosition || gameMode == IcsExamining) {
14182 if(!appData.pieceMenu && blackPlaysFirst) EditPositionMenuEvent(ClearBoard, 0, 0);
14183 SetBlackToPlayEvent();
14184 } else if ((gameMode == AnalyzeMode || gameMode == EditGame) && !blackFlag && WhiteOnMove(currentMove)) {
14185 UserMoveEvent((int)EmptySquare, DROP_RANK, 0, 0, 0); // [HGM] multi-move: if not out of time, enters null move
14186 } else if (shiftKey) {
14187 AdjustClock(which, -1);
14188 } else if (gameMode == IcsPlayingWhite ||
14189 gameMode == MachinePlaysBlack) {
14192 } else { // white clock
14193 if (gameMode == EditPosition || gameMode == IcsExamining) {
14194 if(!appData.pieceMenu && !blackPlaysFirst) EditPositionMenuEvent(ClearBoard, 0, 0);
14195 SetWhiteToPlayEvent();
14196 } else if ((gameMode == AnalyzeMode || gameMode == EditGame) && !whiteFlag && !WhiteOnMove(currentMove)) {
14197 UserMoveEvent((int)EmptySquare, DROP_RANK, 0, 0, 0); // [HGM] multi-move
14198 } else if (shiftKey) {
14199 AdjustClock(which, -1);
14200 } else if (gameMode == IcsPlayingBlack ||
14201 gameMode == MachinePlaysWhite) {
14210 /* Offer draw or accept pending draw offer from opponent */
14212 if (appData.icsActive) {
14213 /* Note: tournament rules require draw offers to be
14214 made after you make your move but before you punch
14215 your clock. Currently ICS doesn't let you do that;
14216 instead, you immediately punch your clock after making
14217 a move, but you can offer a draw at any time. */
14219 SendToICS(ics_prefix);
14220 SendToICS("draw\n");
14221 userOfferedDraw = TRUE; // [HGM] drawclaim: also set flag in ICS play
14222 } else if (cmailMsgLoaded) {
14223 if (currentMove == cmailOldMove &&
14224 commentList[cmailOldMove] != NULL &&
14225 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
14226 "Black offers a draw" : "White offers a draw")) {
14227 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
14228 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_ACCEPT;
14229 } else if (currentMove == cmailOldMove + 1) {
14230 char *offer = WhiteOnMove(cmailOldMove) ?
14231 "White offers a draw" : "Black offers a draw";
14232 AppendComment(currentMove, offer, TRUE);
14233 DisplayComment(currentMove - 1, offer);
14234 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_DRAW;
14236 DisplayError(_("You must make your move before offering a draw"), 0);
14237 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
14239 } else if (first.offeredDraw) {
14240 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
14242 if (first.sendDrawOffers) {
14243 SendToProgram("draw\n", &first);
14244 userOfferedDraw = TRUE;
14252 /* Offer Adjourn or accept pending Adjourn offer from opponent */
14254 if (appData.icsActive) {
14255 SendToICS(ics_prefix);
14256 SendToICS("adjourn\n");
14258 /* Currently GNU Chess doesn't offer or accept Adjourns */
14266 /* Offer Abort or accept pending Abort offer from opponent */
14268 if (appData.icsActive) {
14269 SendToICS(ics_prefix);
14270 SendToICS("abort\n");
14272 GameEnds(GameUnfinished, "Game aborted", GE_PLAYER);
14279 /* Resign. You can do this even if it's not your turn. */
14281 if (appData.icsActive) {
14282 SendToICS(ics_prefix);
14283 SendToICS("resign\n");
14285 switch (gameMode) {
14286 case MachinePlaysWhite:
14287 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
14289 case MachinePlaysBlack:
14290 GameEnds(BlackWins, "White resigns", GE_PLAYER);
14293 if (cmailMsgLoaded) {
14295 if (WhiteOnMove(cmailOldMove)) {
14296 GameEnds(BlackWins, "White resigns", GE_PLAYER);
14298 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
14300 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_RESIGN;
14311 StopObservingEvent ()
14313 /* Stop observing current games */
14314 SendToICS(ics_prefix);
14315 SendToICS("unobserve\n");
14319 StopExaminingEvent ()
14321 /* Stop observing current game */
14322 SendToICS(ics_prefix);
14323 SendToICS("unexamine\n");
14327 ForwardInner (int target)
14329 int limit; int oldSeekGraphUp = seekGraphUp;
14331 if (appData.debugMode)
14332 fprintf(debugFP, "ForwardInner(%d), current %d, forward %d\n",
14333 target, currentMove, forwardMostMove);
14335 if (gameMode == EditPosition)
14338 seekGraphUp = FALSE;
14339 MarkTargetSquares(1);
14341 if (gameMode == PlayFromGameFile && !pausing)
14344 if (gameMode == IcsExamining && pausing)
14345 limit = pauseExamForwardMostMove;
14347 limit = forwardMostMove;
14349 if (target > limit) target = limit;
14351 if (target > 0 && moveList[target - 1][0]) {
14352 int fromX, fromY, toX, toY;
14353 toX = moveList[target - 1][2] - AAA;
14354 toY = moveList[target - 1][3] - ONE;
14355 if (moveList[target - 1][1] == '@') {
14356 if (appData.highlightLastMove) {
14357 SetHighlights(-1, -1, toX, toY);
14360 fromX = moveList[target - 1][0] - AAA;
14361 fromY = moveList[target - 1][1] - ONE;
14362 if (target == currentMove + 1) {
14363 AnimateMove(boards[currentMove], fromX, fromY, toX, toY);
14365 if (appData.highlightLastMove) {
14366 SetHighlights(fromX, fromY, toX, toY);
14370 if (gameMode == EditGame || gameMode == AnalyzeMode ||
14371 gameMode == Training || gameMode == PlayFromGameFile ||
14372 gameMode == AnalyzeFile) {
14373 while (currentMove < target) {
14374 SendMoveToProgram(currentMove++, &first);
14377 currentMove = target;
14380 if (gameMode == EditGame || gameMode == EndOfGame) {
14381 whiteTimeRemaining = timeRemaining[0][currentMove];
14382 blackTimeRemaining = timeRemaining[1][currentMove];
14384 DisplayBothClocks();
14385 DisplayMove(currentMove - 1);
14386 DrawPosition(oldSeekGraphUp, boards[currentMove]);
14387 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
14388 if ( !matchMode && gameMode != Training) { // [HGM] PV info: routine tests if empty
14389 DisplayComment(currentMove - 1, commentList[currentMove]);
14391 ClearMap(); // [HGM] exclude: invalidate map
14398 if (gameMode == IcsExamining && !pausing) {
14399 SendToICS(ics_prefix);
14400 SendToICS("forward\n");
14402 ForwardInner(currentMove + 1);
14409 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
14410 /* to optimze, we temporarily turn off analysis mode while we feed
14411 * the remaining moves to the engine. Otherwise we get analysis output
14414 if (first.analysisSupport) {
14415 SendToProgram("exit\nforce\n", &first);
14416 first.analyzing = FALSE;
14420 if (gameMode == IcsExamining && !pausing) {
14421 SendToICS(ics_prefix);
14422 SendToICS("forward 999999\n");
14424 ForwardInner(forwardMostMove);
14427 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
14428 /* we have fed all the moves, so reactivate analysis mode */
14429 SendToProgram("analyze\n", &first);
14430 first.analyzing = TRUE;
14431 /*first.maybeThinking = TRUE;*/
14432 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
14437 BackwardInner (int target)
14439 int full_redraw = TRUE; /* [AS] Was FALSE, had to change it! */
14441 if (appData.debugMode)
14442 fprintf(debugFP, "BackwardInner(%d), current %d, forward %d\n",
14443 target, currentMove, forwardMostMove);
14445 if (gameMode == EditPosition) return;
14446 seekGraphUp = FALSE;
14447 MarkTargetSquares(1);
14448 if (currentMove <= backwardMostMove) {
14450 DrawPosition(full_redraw, boards[currentMove]);
14453 if (gameMode == PlayFromGameFile && !pausing)
14456 if (moveList[target][0]) {
14457 int fromX, fromY, toX, toY;
14458 toX = moveList[target][2] - AAA;
14459 toY = moveList[target][3] - ONE;
14460 if (moveList[target][1] == '@') {
14461 if (appData.highlightLastMove) {
14462 SetHighlights(-1, -1, toX, toY);
14465 fromX = moveList[target][0] - AAA;
14466 fromY = moveList[target][1] - ONE;
14467 if (target == currentMove - 1) {
14468 AnimateMove(boards[currentMove], toX, toY, fromX, fromY);
14470 if (appData.highlightLastMove) {
14471 SetHighlights(fromX, fromY, toX, toY);
14475 if (gameMode == EditGame || gameMode==AnalyzeMode ||
14476 gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
14477 while (currentMove > target) {
14478 if(moveList[currentMove-1][1] == '@' && moveList[currentMove-1][0] == '@') {
14479 // null move cannot be undone. Reload program with move history before it.
14481 for(i=target; i>backwardMostMove; i--) { // seek back to start or previous null move
14482 if(moveList[i-1][1] == '@' && moveList[i-1][0] == '@') break;
14484 SendBoard(&first, i);
14485 for(currentMove=i; currentMove<target; currentMove++) SendMoveToProgram(currentMove, &first);
14488 SendToProgram("undo\n", &first);
14492 currentMove = target;
14495 if (gameMode == EditGame || gameMode == EndOfGame) {
14496 whiteTimeRemaining = timeRemaining[0][currentMove];
14497 blackTimeRemaining = timeRemaining[1][currentMove];
14499 DisplayBothClocks();
14500 DisplayMove(currentMove - 1);
14501 DrawPosition(full_redraw, boards[currentMove]);
14502 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
14503 // [HGM] PV info: routine tests if comment empty
14504 DisplayComment(currentMove - 1, commentList[currentMove]);
14505 ClearMap(); // [HGM] exclude: invalidate map
14511 if (gameMode == IcsExamining && !pausing) {
14512 SendToICS(ics_prefix);
14513 SendToICS("backward\n");
14515 BackwardInner(currentMove - 1);
14522 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
14523 /* to optimize, we temporarily turn off analysis mode while we undo
14524 * all the moves. Otherwise we get analysis output after each undo.
14526 if (first.analysisSupport) {
14527 SendToProgram("exit\nforce\n", &first);
14528 first.analyzing = FALSE;
14532 if (gameMode == IcsExamining && !pausing) {
14533 SendToICS(ics_prefix);
14534 SendToICS("backward 999999\n");
14536 BackwardInner(backwardMostMove);
14539 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
14540 /* we have fed all the moves, so reactivate analysis mode */
14541 SendToProgram("analyze\n", &first);
14542 first.analyzing = TRUE;
14543 /*first.maybeThinking = TRUE;*/
14544 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
14551 if (gameMode == PlayFromGameFile && !pausing) PauseEvent();
14552 if (to >= forwardMostMove) to = forwardMostMove;
14553 if (to <= backwardMostMove) to = backwardMostMove;
14554 if (to < currentMove) {
14562 RevertEvent (Boolean annotate)
14564 if(PopTail(annotate)) { // [HGM] vari: restore old game tail
14567 if (gameMode != IcsExamining) {
14568 DisplayError(_("You are not examining a game"), 0);
14572 DisplayError(_("You can't revert while pausing"), 0);
14575 SendToICS(ics_prefix);
14576 SendToICS("revert\n");
14580 RetractMoveEvent ()
14582 switch (gameMode) {
14583 case MachinePlaysWhite:
14584 case MachinePlaysBlack:
14585 if (WhiteOnMove(forwardMostMove) == (gameMode == MachinePlaysWhite)) {
14586 DisplayError(_("Wait until your turn,\nor select Move Now"), 0);
14589 if (forwardMostMove < 2) return;
14590 currentMove = forwardMostMove = forwardMostMove - 2;
14591 whiteTimeRemaining = timeRemaining[0][currentMove];
14592 blackTimeRemaining = timeRemaining[1][currentMove];
14593 DisplayBothClocks();
14594 DisplayMove(currentMove - 1);
14595 ClearHighlights();/*!! could figure this out*/
14596 DrawPosition(TRUE, boards[currentMove]); /* [AS] Changed to full redraw! */
14597 SendToProgram("remove\n", &first);
14598 /*first.maybeThinking = TRUE;*/ /* GNU Chess does not ponder here */
14601 case BeginningOfGame:
14605 case IcsPlayingWhite:
14606 case IcsPlayingBlack:
14607 if (WhiteOnMove(forwardMostMove) == (gameMode == IcsPlayingWhite)) {
14608 SendToICS(ics_prefix);
14609 SendToICS("takeback 2\n");
14611 SendToICS(ics_prefix);
14612 SendToICS("takeback 1\n");
14621 ChessProgramState *cps;
14623 switch (gameMode) {
14624 case MachinePlaysWhite:
14625 if (!WhiteOnMove(forwardMostMove)) {
14626 DisplayError(_("It is your turn"), 0);
14631 case MachinePlaysBlack:
14632 if (WhiteOnMove(forwardMostMove)) {
14633 DisplayError(_("It is your turn"), 0);
14638 case TwoMachinesPlay:
14639 if (WhiteOnMove(forwardMostMove) ==
14640 (first.twoMachinesColor[0] == 'w')) {
14646 case BeginningOfGame:
14650 SendToProgram("?\n", cps);
14654 TruncateGameEvent ()
14657 if (gameMode != EditGame) return;
14664 CleanupTail(); // [HGM] vari: only keep current variation if we explicitly truncate
14665 if (forwardMostMove > currentMove) {
14666 if (gameInfo.resultDetails != NULL) {
14667 free(gameInfo.resultDetails);
14668 gameInfo.resultDetails = NULL;
14669 gameInfo.result = GameUnfinished;
14671 forwardMostMove = currentMove;
14672 HistorySet(parseList, backwardMostMove, forwardMostMove,
14680 if (appData.noChessProgram) return;
14681 switch (gameMode) {
14682 case MachinePlaysWhite:
14683 if (WhiteOnMove(forwardMostMove)) {
14684 DisplayError(_("Wait until your turn"), 0);
14688 case BeginningOfGame:
14689 case MachinePlaysBlack:
14690 if (!WhiteOnMove(forwardMostMove)) {
14691 DisplayError(_("Wait until your turn"), 0);
14696 DisplayError(_("No hint available"), 0);
14699 SendToProgram("hint\n", &first);
14700 hintRequested = TRUE;
14706 if (appData.noChessProgram) return;
14707 switch (gameMode) {
14708 case MachinePlaysWhite:
14709 if (WhiteOnMove(forwardMostMove)) {
14710 DisplayError(_("Wait until your turn"), 0);
14714 case BeginningOfGame:
14715 case MachinePlaysBlack:
14716 if (!WhiteOnMove(forwardMostMove)) {
14717 DisplayError(_("Wait until your turn"), 0);
14722 EditPositionDone(TRUE);
14724 case TwoMachinesPlay:
14729 SendToProgram("bk\n", &first);
14730 bookOutput[0] = NULLCHAR;
14731 bookRequested = TRUE;
14737 char *tags = PGNTags(&gameInfo);
14738 TagsPopUp(tags, CmailMsg());
14742 /* end button procedures */
14745 PrintPosition (FILE *fp, int move)
14749 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
14750 for (j = BOARD_LEFT; j < BOARD_RGHT; j++) {
14751 char c = PieceToChar(boards[move][i][j]);
14752 fputc(c == 'x' ? '.' : c, fp);
14753 fputc(j == BOARD_RGHT - 1 ? '\n' : ' ', fp);
14756 if ((gameMode == EditPosition) ? !blackPlaysFirst : (move % 2 == 0))
14757 fprintf(fp, "white to play\n");
14759 fprintf(fp, "black to play\n");
14763 PrintOpponents (FILE *fp)
14765 if (gameInfo.white != NULL) {
14766 fprintf(fp, "\t%s vs. %s\n", gameInfo.white, gameInfo.black);
14772 /* Find last component of program's own name, using some heuristics */
14774 TidyProgramName (char *prog, char *host, char buf[MSG_SIZ])
14777 int local = (strcmp(host, "localhost") == 0);
14778 while (!local && (p = strchr(prog, ';')) != NULL) {
14780 while (*p == ' ') p++;
14783 if (*prog == '"' || *prog == '\'') {
14784 q = strchr(prog + 1, *prog);
14786 q = strchr(prog, ' ');
14788 if (q == NULL) q = prog + strlen(prog);
14790 while (p >= prog && *p != '/' && *p != '\\') p--;
14792 if(p == prog && *p == '"') p++;
14794 if (q - p >= 4 && StrCaseCmp(q - 4, ".exe") == 0) *q = c, q -= 4; else *q = c;
14795 memcpy(buf, p, q - p);
14796 buf[q - p] = NULLCHAR;
14804 TimeControlTagValue ()
14807 if (!appData.clockMode) {
14808 safeStrCpy(buf, "-", sizeof(buf)/sizeof(buf[0]));
14809 } else if (movesPerSession > 0) {
14810 snprintf(buf, MSG_SIZ, "%d/%ld", movesPerSession, timeControl/1000);
14811 } else if (timeIncrement == 0) {
14812 snprintf(buf, MSG_SIZ, "%ld", timeControl/1000);
14814 snprintf(buf, MSG_SIZ, "%ld+%ld", timeControl/1000, timeIncrement/1000);
14816 return StrSave(buf);
14822 /* This routine is used only for certain modes */
14823 VariantClass v = gameInfo.variant;
14824 ChessMove r = GameUnfinished;
14827 if(gameMode == EditGame) { // [HGM] vari: do not erase result on EditGame
14828 r = gameInfo.result;
14829 p = gameInfo.resultDetails;
14830 gameInfo.resultDetails = NULL;
14832 ClearGameInfo(&gameInfo);
14833 gameInfo.variant = v;
14835 switch (gameMode) {
14836 case MachinePlaysWhite:
14837 gameInfo.event = StrSave( appData.pgnEventHeader );
14838 gameInfo.site = StrSave(HostName());
14839 gameInfo.date = PGNDate();
14840 gameInfo.round = StrSave("-");
14841 gameInfo.white = StrSave(first.tidy);
14842 gameInfo.black = StrSave(UserName());
14843 gameInfo.timeControl = TimeControlTagValue();
14846 case MachinePlaysBlack:
14847 gameInfo.event = StrSave( appData.pgnEventHeader );
14848 gameInfo.site = StrSave(HostName());
14849 gameInfo.date = PGNDate();
14850 gameInfo.round = StrSave("-");
14851 gameInfo.white = StrSave(UserName());
14852 gameInfo.black = StrSave(first.tidy);
14853 gameInfo.timeControl = TimeControlTagValue();
14856 case TwoMachinesPlay:
14857 gameInfo.event = StrSave( appData.pgnEventHeader );
14858 gameInfo.site = StrSave(HostName());
14859 gameInfo.date = PGNDate();
14862 snprintf(buf, MSG_SIZ, "%d", roundNr);
14863 gameInfo.round = StrSave(buf);
14865 gameInfo.round = StrSave("-");
14867 if (first.twoMachinesColor[0] == 'w') {
14868 gameInfo.white = StrSave(appData.pgnName[0][0] ? appData.pgnName[0] : first.tidy);
14869 gameInfo.black = StrSave(appData.pgnName[1][0] ? appData.pgnName[1] : second.tidy);
14871 gameInfo.white = StrSave(appData.pgnName[1][0] ? appData.pgnName[1] : second.tidy);
14872 gameInfo.black = StrSave(appData.pgnName[0][0] ? appData.pgnName[0] : first.tidy);
14874 gameInfo.timeControl = TimeControlTagValue();
14878 gameInfo.event = StrSave("Edited game");
14879 gameInfo.site = StrSave(HostName());
14880 gameInfo.date = PGNDate();
14881 gameInfo.round = StrSave("-");
14882 gameInfo.white = StrSave("-");
14883 gameInfo.black = StrSave("-");
14884 gameInfo.result = r;
14885 gameInfo.resultDetails = p;
14889 gameInfo.event = StrSave("Edited position");
14890 gameInfo.site = StrSave(HostName());
14891 gameInfo.date = PGNDate();
14892 gameInfo.round = StrSave("-");
14893 gameInfo.white = StrSave("-");
14894 gameInfo.black = StrSave("-");
14897 case IcsPlayingWhite:
14898 case IcsPlayingBlack:
14903 case PlayFromGameFile:
14904 gameInfo.event = StrSave("Game from non-PGN file");
14905 gameInfo.site = StrSave(HostName());
14906 gameInfo.date = PGNDate();
14907 gameInfo.round = StrSave("-");
14908 gameInfo.white = StrSave("?");
14909 gameInfo.black = StrSave("?");
14918 ReplaceComment (int index, char *text)
14924 if(index && sscanf(text, "%f/%d", &score, &len) == 2 &&
14925 pvInfoList[index-1].depth == len &&
14926 fabs(pvInfoList[index-1].score - score*100.) < 0.5 &&
14927 (p = strchr(text, '\n'))) text = p; // [HGM] strip off first line with PV info, if any
14928 while (*text == '\n') text++;
14929 len = strlen(text);
14930 while (len > 0 && text[len - 1] == '\n') len--;
14932 if (commentList[index] != NULL)
14933 free(commentList[index]);
14936 commentList[index] = NULL;
14939 if( *text == '{' && strchr(text, '}') || // [HGM] braces: if certainy malformed, put braces
14940 *text == '[' && strchr(text, ']') || // otherwise hope the user knows what he is doing
14941 *text == '(' && strchr(text, ')')) { // (perhaps check if this parses as comment-only?)
14942 commentList[index] = (char *) malloc(len + 2);
14943 strncpy(commentList[index], text, len);
14944 commentList[index][len] = '\n';
14945 commentList[index][len + 1] = NULLCHAR;
14947 // [HGM] braces: if text does not start with known OK delimiter, put braces around it.
14949 commentList[index] = (char *) malloc(len + 7);
14950 safeStrCpy(commentList[index], "{\n", 3);
14951 safeStrCpy(commentList[index]+2, text, len+1);
14952 commentList[index][len+2] = NULLCHAR;
14953 while(p = strchr(commentList[index], '}')) *p = ')'; // kill all } to make it one comment
14954 strcat(commentList[index], "\n}\n");
14959 CrushCRs (char *text)
14967 if (ch == '\r') continue;
14969 } while (ch != '\0');
14973 AppendComment (int index, char *text, Boolean addBraces)
14974 /* addBraces tells if we should add {} */
14979 if(appData.debugMode) fprintf(debugFP, "Append: in='%s' %d\n", text, addBraces); fflush(debugFP);
14980 text = GetInfoFromComment( index, text ); /* [HGM] PV time: strip PV info from comment */
14983 while (*text == '\n') text++;
14984 len = strlen(text);
14985 while (len > 0 && text[len - 1] == '\n') len--;
14986 text[len] = NULLCHAR;
14988 if (len == 0) return;
14990 if (commentList[index] != NULL) {
14991 Boolean addClosingBrace = addBraces;
14992 old = commentList[index];
14993 oldlen = strlen(old);
14994 while(commentList[index][oldlen-1] == '\n')
14995 commentList[index][--oldlen] = NULLCHAR;
14996 commentList[index] = (char *) malloc(oldlen + len + 6); // might waste 4
14997 safeStrCpy(commentList[index], old, oldlen + len + 6);
14999 // [HGM] braces: join "{A\n}\n" + "{\nB}" as "{A\nB\n}"
15000 if(commentList[index][oldlen-1] == '}' && (text[0] == '{' || addBraces == TRUE)) {
15001 if(addBraces == TRUE) addBraces = FALSE; else { text++; len--; }
15002 while (*text == '\n') { text++; len--; }
15003 commentList[index][--oldlen] = NULLCHAR;
15005 if(addBraces) strcat(commentList[index], addBraces == 2 ? "\n(" : "\n{\n");
15006 else strcat(commentList[index], "\n");
15007 strcat(commentList[index], text);
15008 if(addClosingBrace) strcat(commentList[index], addClosingBrace == 2 ? ")\n" : "\n}\n");
15009 else strcat(commentList[index], "\n");
15011 commentList[index] = (char *) malloc(len + 6); // perhaps wastes 4...
15013 safeStrCpy(commentList[index], addBraces == 2 ? "(" : "{\n", 3);
15014 else commentList[index][0] = NULLCHAR;
15015 strcat(commentList[index], text);
15016 strcat(commentList[index], addBraces == 2 ? ")\n" : "\n");
15017 if(addBraces == TRUE) strcat(commentList[index], "}\n");
15022 FindStr (char * text, char * sub_text)
15024 char * result = strstr( text, sub_text );
15026 if( result != NULL ) {
15027 result += strlen( sub_text );
15033 /* [AS] Try to extract PV info from PGN comment */
15034 /* [HGM] PV time: and then remove it, to prevent it appearing twice */
15036 GetInfoFromComment (int index, char * text)
15038 char * sep = text, *p;
15040 if( text != NULL && index > 0 ) {
15043 int time = -1, sec = 0, deci;
15044 char * s_eval = FindStr( text, "[%eval " );
15045 char * s_emt = FindStr( text, "[%emt " );
15047 if( s_eval != NULL || s_emt != NULL ) {
15051 if( s_eval != NULL ) {
15052 if( sscanf( s_eval, "%d,%d%c", &score, &depth, &delim ) != 3 ) {
15056 if( delim != ']' ) {
15061 if( s_emt != NULL ) {
15066 /* We expect something like: [+|-]nnn.nn/dd */
15069 if(*text != '{') return text; // [HGM] braces: must be normal comment
15071 sep = strchr( text, '/' );
15072 if( sep == NULL || sep < (text+4) ) {
15077 if(p[1] == '(') { // comment starts with PV
15078 p = strchr(p, ')'); // locate end of PV
15079 if(p == NULL || sep < p+5) return text;
15080 // at this point we have something like "{(.*) +0.23/6 ..."
15081 p = text; while(*++p != ')') p[-1] = *p; p[-1] = ')';
15082 *p = '\n'; while(*p == ' ' || *p == '\n') p++; *--p = '{';
15083 // we now moved the brace to behind the PV: "(.*) {+0.23/6 ..."
15085 time = -1; sec = -1; deci = -1;
15086 if( sscanf( p+1, "%d.%d/%d %d:%d", &score, &score_lo, &depth, &time, &sec ) != 5 &&
15087 sscanf( p+1, "%d.%d/%d %d.%d", &score, &score_lo, &depth, &time, &deci ) != 5 &&
15088 sscanf( p+1, "%d.%d/%d %d", &score, &score_lo, &depth, &time ) != 4 &&
15089 sscanf( p+1, "%d.%d/%d", &score, &score_lo, &depth ) != 3 ) {
15093 if( score_lo < 0 || score_lo >= 100 ) {
15097 if(sec >= 0) time = 600*time + 10*sec; else
15098 if(deci >= 0) time = 10*time + deci; else time *= 10; // deci-sec
15100 score = score >= 0 ? score*100 + score_lo : score*100 - score_lo;
15102 /* [HGM] PV time: now locate end of PV info */
15103 while( *++sep >= '0' && *sep <= '9'); // strip depth
15105 while( *++sep >= '0' && *sep <= '9' || *sep == '\n'); // strip time
15107 while( *++sep >= '0' && *sep <= '9'); // strip seconds
15109 while( *++sep >= '0' && *sep <= '9'); // strip fractional seconds
15110 while(*sep == ' ' || *sep == '\n' || *sep == '\r') sep++;
15121 pvInfoList[index-1].depth = depth;
15122 pvInfoList[index-1].score = score;
15123 pvInfoList[index-1].time = 10*time; // centi-sec
15124 if(*sep == '}') *sep = 0; else *--sep = '{';
15125 if(p != text) { while(*p++ = *sep++); sep = text; } // squeeze out space between PV and comment, and return both
15131 SendToProgram (char *message, ChessProgramState *cps)
15133 int count, outCount, error;
15136 if (cps->pr == NoProc) return;
15139 if (appData.debugMode) {
15142 fprintf(debugFP, "%ld >%-6s: %s",
15143 SubtractTimeMarks(&now, &programStartTime),
15144 cps->which, message);
15146 fprintf(serverFP, "%ld >%-6s: %s",
15147 SubtractTimeMarks(&now, &programStartTime),
15148 cps->which, message), fflush(serverFP);
15151 count = strlen(message);
15152 outCount = OutputToProcess(cps->pr, message, count, &error);
15153 if (outCount < count && !exiting
15154 && !endingGame) { /* [HGM] crash: to not hang GameEnds() writing to deceased engines */
15155 if(!cps->initDone) return; // [HGM] should not generate fatal error during engine load
15156 snprintf(buf, MSG_SIZ, _("Error writing to %s chess program"), _(cps->which));
15157 if(gameInfo.resultDetails==NULL) { /* [HGM] crash: if game in progress, give reason for abort */
15158 if((signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
15159 snprintf(buf, MSG_SIZ, _("%s program exits in draw position (%s)"), _(cps->which), cps->program);
15160 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(GameIsDrawn, buf, GE_XBOARD); return; }
15161 gameInfo.result = GameIsDrawn; /* [HGM] accept exit as draw claim */
15163 ChessMove res = cps->twoMachinesColor[0]=='w' ? BlackWins : WhiteWins;
15164 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(res, buf, GE_XBOARD); return; }
15165 gameInfo.result = res;
15167 gameInfo.resultDetails = StrSave(buf);
15169 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; return; }
15170 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, error, 1); else errorExitStatus = 1;
15175 ReceiveFromProgram (InputSourceRef isr, VOIDSTAR closure, char *message, int count, int error)
15179 ChessProgramState *cps = (ChessProgramState *)closure;
15181 if (isr != cps->isr) return; /* Killed intentionally */
15184 RemoveInputSource(cps->isr);
15185 if(!cps->initDone) return; // [HGM] should not generate fatal error during engine load
15186 snprintf(buf, MSG_SIZ, _("Error: %s chess program (%s) exited unexpectedly"),
15187 _(cps->which), cps->program);
15188 if(gameInfo.resultDetails==NULL) { /* [HGM] crash: if game in progress, give reason for abort */
15189 if((signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
15190 snprintf(buf, MSG_SIZ, _("%s program exits in draw position (%s)"), _(cps->which), cps->program);
15191 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(GameIsDrawn, buf, GE_XBOARD); return; }
15192 gameInfo.result = GameIsDrawn; /* [HGM] accept exit as draw claim */
15194 ChessMove res = cps->twoMachinesColor[0]=='w' ? BlackWins : WhiteWins;
15195 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(res, buf, GE_XBOARD); return; }
15196 gameInfo.result = res;
15198 gameInfo.resultDetails = StrSave(buf);
15200 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; return; }
15201 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, 0, 1); else errorExitStatus = 1;
15203 snprintf(buf, MSG_SIZ, _("Error reading from %s chess program (%s)"),
15204 _(cps->which), cps->program);
15205 RemoveInputSource(cps->isr);
15207 /* [AS] Program is misbehaving badly... kill it */
15208 if( count == -2 ) {
15209 DestroyChildProcess( cps->pr, 9 );
15213 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, error, 1); else errorExitStatus = 1;
15218 if ((end_str = strchr(message, '\r')) != NULL)
15219 *end_str = NULLCHAR;
15220 if ((end_str = strchr(message, '\n')) != NULL)
15221 *end_str = NULLCHAR;
15223 if (appData.debugMode) {
15224 TimeMark now; int print = 1;
15225 char *quote = ""; char c; int i;
15227 if(appData.engineComments != 1) { /* [HGM] debug: decide if protocol-violating output is written */
15228 char start = message[0];
15229 if(start >='A' && start <= 'Z') start += 'a' - 'A'; // be tolerant to capitalizing
15230 if(sscanf(message, "%d%c%d%d%d", &i, &c, &i, &i, &i) != 5 &&
15231 sscanf(message, "move %c", &c)!=1 && sscanf(message, "offer%c", &c)!=1 &&
15232 sscanf(message, "resign%c", &c)!=1 && sscanf(message, "feature %c", &c)!=1 &&
15233 sscanf(message, "error %c", &c)!=1 && sscanf(message, "illegal %c", &c)!=1 &&
15234 sscanf(message, "tell%c", &c)!=1 && sscanf(message, "0-1 %c", &c)!=1 &&
15235 sscanf(message, "1-0 %c", &c)!=1 && sscanf(message, "1/2-1/2 %c", &c)!=1 &&
15236 sscanf(message, "setboard %c", &c)!=1 && sscanf(message, "setup %c", &c)!=1 &&
15237 sscanf(message, "hint: %c", &c)!=1 &&
15238 sscanf(message, "pong %c", &c)!=1 && start != '#') {
15239 quote = appData.engineComments == 2 ? "# " : "### NON-COMPLIANT! ### ";
15240 print = (appData.engineComments >= 2);
15242 message[0] = start; // restore original message
15246 fprintf(debugFP, "%ld <%-6s: %s%s\n",
15247 SubtractTimeMarks(&now, &programStartTime), cps->which,
15251 fprintf(serverFP, "%ld <%-6s: %s%s\n",
15252 SubtractTimeMarks(&now, &programStartTime), cps->which,
15254 message), fflush(serverFP);
15258 /* [DM] if icsEngineAnalyze is active we block all whisper and kibitz output, because nobody want to see this */
15259 if (appData.icsEngineAnalyze) {
15260 if (strstr(message, "whisper") != NULL ||
15261 strstr(message, "kibitz") != NULL ||
15262 strstr(message, "tellics") != NULL) return;
15265 HandleMachineMove(message, cps);
15270 SendTimeControl (ChessProgramState *cps, int mps, long tc, int inc, int sd, int st)
15275 if( timeControl_2 > 0 ) {
15276 if( (gameMode == MachinePlaysBlack) || (gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b') ) {
15277 tc = timeControl_2;
15280 tc /= cps->timeOdds; /* [HGM] time odds: apply before telling engine */
15281 inc /= cps->timeOdds;
15282 st /= cps->timeOdds;
15284 seconds = (tc / 1000) % 60; /* [HGM] displaced to after applying odds */
15287 /* Set exact time per move, normally using st command */
15288 if (cps->stKludge) {
15289 /* GNU Chess 4 has no st command; uses level in a nonstandard way */
15291 if (seconds == 0) {
15292 snprintf(buf, MSG_SIZ, "level 1 %d\n", st/60);
15294 snprintf(buf, MSG_SIZ, "level 1 %d:%02d\n", st/60, seconds);
15297 snprintf(buf, MSG_SIZ, "st %d\n", st);
15300 /* Set conventional or incremental time control, using level command */
15301 if (seconds == 0) {
15302 /* Note old gnuchess bug -- minutes:seconds used to not work.
15303 Fixed in later versions, but still avoid :seconds
15304 when seconds is 0. */
15305 snprintf(buf, MSG_SIZ, "level %d %ld %g\n", mps, tc/60000, inc/1000.);
15307 snprintf(buf, MSG_SIZ, "level %d %ld:%02d %g\n", mps, tc/60000,
15308 seconds, inc/1000.);
15311 SendToProgram(buf, cps);
15313 /* Orthoganally (except for GNU Chess 4), limit time to st seconds */
15314 /* Orthogonally, limit search to given depth */
15316 if (cps->sdKludge) {
15317 snprintf(buf, MSG_SIZ, "depth\n%d\n", sd);
15319 snprintf(buf, MSG_SIZ, "sd %d\n", sd);
15321 SendToProgram(buf, cps);
15324 if(cps->nps >= 0) { /* [HGM] nps */
15325 if(cps->supportsNPS == FALSE)
15326 cps->nps = -1; // don't use if engine explicitly says not supported!
15328 snprintf(buf, MSG_SIZ, "nps %d\n", cps->nps);
15329 SendToProgram(buf, cps);
15334 ChessProgramState *
15336 /* [HGM] return pointer to 'first' or 'second', depending on who plays white */
15338 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b' ||
15339 gameMode == BeginningOfGame || gameMode == MachinePlaysBlack)
15345 SendTimeRemaining (ChessProgramState *cps, int machineWhite)
15347 char message[MSG_SIZ];
15350 /* Note: this routine must be called when the clocks are stopped
15351 or when they have *just* been set or switched; otherwise
15352 it will be off by the time since the current tick started.
15354 if (machineWhite) {
15355 time = whiteTimeRemaining / 10;
15356 otime = blackTimeRemaining / 10;
15358 time = blackTimeRemaining / 10;
15359 otime = whiteTimeRemaining / 10;
15361 /* [HGM] translate opponent's time by time-odds factor */
15362 otime = (otime * cps->other->timeOdds) / cps->timeOdds;
15364 if (time <= 0) time = 1;
15365 if (otime <= 0) otime = 1;
15367 snprintf(message, MSG_SIZ, "time %ld\n", time);
15368 SendToProgram(message, cps);
15370 snprintf(message, MSG_SIZ, "otim %ld\n", otime);
15371 SendToProgram(message, cps);
15375 BoolFeature (char **p, char *name, int *loc, ChessProgramState *cps)
15378 int len = strlen(name);
15381 if (strncmp((*p), name, len) == 0 && (*p)[len] == '=') {
15383 sscanf(*p, "%d", &val);
15385 while (**p && **p != ' ')
15387 snprintf(buf, MSG_SIZ, "accepted %s\n", name);
15388 SendToProgram(buf, cps);
15395 IntFeature (char **p, char *name, int *loc, ChessProgramState *cps)
15398 int len = strlen(name);
15399 if (strncmp((*p), name, len) == 0 && (*p)[len] == '=') {
15401 sscanf(*p, "%d", loc);
15402 while (**p && **p != ' ') (*p)++;
15403 snprintf(buf, MSG_SIZ, "accepted %s\n", name);
15404 SendToProgram(buf, cps);
15411 StringFeature (char **p, char *name, char loc[], ChessProgramState *cps)
15414 int len = strlen(name);
15415 if (strncmp((*p), name, len) == 0
15416 && (*p)[len] == '=' && (*p)[len+1] == '\"') {
15418 sscanf(*p, "%[^\"]", loc);
15419 while (**p && **p != '\"') (*p)++;
15420 if (**p == '\"') (*p)++;
15421 snprintf(buf, MSG_SIZ, "accepted %s\n", name);
15422 SendToProgram(buf, cps);
15429 ParseOption (Option *opt, ChessProgramState *cps)
15430 // [HGM] options: process the string that defines an engine option, and determine
15431 // name, type, default value, and allowed value range
15433 char *p, *q, buf[MSG_SIZ];
15434 int n, min = (-1)<<31, max = 1<<31, def;
15436 if(p = strstr(opt->name, " -spin ")) {
15437 if((n = sscanf(p, " -spin %d %d %d", &def, &min, &max)) < 3 ) return FALSE;
15438 if(max < min) max = min; // enforce consistency
15439 if(def < min) def = min;
15440 if(def > max) def = max;
15445 } else if((p = strstr(opt->name, " -slider "))) {
15446 // for now -slider is a synonym for -spin, to already provide compatibility with future polyglots
15447 if((n = sscanf(p, " -slider %d %d %d", &def, &min, &max)) < 3 ) return FALSE;
15448 if(max < min) max = min; // enforce consistency
15449 if(def < min) def = min;
15450 if(def > max) def = max;
15454 opt->type = Spin; // Slider;
15455 } else if((p = strstr(opt->name, " -string "))) {
15456 opt->textValue = p+9;
15457 opt->type = TextBox;
15458 } else if((p = strstr(opt->name, " -file "))) {
15459 // for now -file is a synonym for -string, to already provide compatibility with future polyglots
15460 opt->textValue = p+7;
15461 opt->type = FileName; // FileName;
15462 } else if((p = strstr(opt->name, " -path "))) {
15463 // for now -file is a synonym for -string, to already provide compatibility with future polyglots
15464 opt->textValue = p+7;
15465 opt->type = PathName; // PathName;
15466 } else if(p = strstr(opt->name, " -check ")) {
15467 if(sscanf(p, " -check %d", &def) < 1) return FALSE;
15468 opt->value = (def != 0);
15469 opt->type = CheckBox;
15470 } else if(p = strstr(opt->name, " -combo ")) {
15471 opt->textValue = (char*) (&cps->comboList[cps->comboCnt]); // cheat with pointer type
15472 cps->comboList[cps->comboCnt++] = q = p+8; // holds possible choices
15473 if(*q == '*') cps->comboList[cps->comboCnt-1]++;
15474 opt->value = n = 0;
15475 while(q = StrStr(q, " /// ")) {
15476 n++; *q = 0; // count choices, and null-terminate each of them
15478 if(*q == '*') { // remember default, which is marked with * prefix
15482 cps->comboList[cps->comboCnt++] = q;
15484 cps->comboList[cps->comboCnt++] = NULL;
15486 opt->type = ComboBox;
15487 } else if(p = strstr(opt->name, " -button")) {
15488 opt->type = Button;
15489 } else if(p = strstr(opt->name, " -save")) {
15490 opt->type = SaveButton;
15491 } else return FALSE;
15492 *p = 0; // terminate option name
15493 // now look if the command-line options define a setting for this engine option.
15494 if(cps->optionSettings && cps->optionSettings[0])
15495 p = strstr(cps->optionSettings, opt->name); else p = NULL;
15496 if(p && (p == cps->optionSettings || p[-1] == ',')) {
15497 snprintf(buf, MSG_SIZ, "option %s", p);
15498 if(p = strstr(buf, ",")) *p = 0;
15499 if(q = strchr(buf, '=')) switch(opt->type) {
15501 for(n=0; n<opt->max; n++)
15502 if(!strcmp(((char**)opt->textValue)[n], q+1)) opt->value = n;
15505 safeStrCpy(opt->textValue, q+1, MSG_SIZ - (opt->textValue - opt->name));
15509 opt->value = atoi(q+1);
15514 SendToProgram(buf, cps);
15520 FeatureDone (ChessProgramState *cps, int val)
15522 DelayedEventCallback cb = GetDelayedEvent();
15523 if ((cb == InitBackEnd3 && cps == &first) ||
15524 (cb == SettingsMenuIfReady && cps == &second) ||
15525 (cb == LoadEngine) ||
15526 (cb == TwoMachinesEventIfReady)) {
15527 CancelDelayedEvent();
15528 ScheduleDelayedEvent(cb, val ? 1 : 3600000);
15530 cps->initDone = val;
15533 /* Parse feature command from engine */
15535 ParseFeatures (char *args, ChessProgramState *cps)
15543 while (*p == ' ') p++;
15544 if (*p == NULLCHAR) return;
15546 if (BoolFeature(&p, "setboard", &cps->useSetboard, cps)) continue;
15547 if (BoolFeature(&p, "xedit", &cps->extendedEdit, cps)) continue;
15548 if (BoolFeature(&p, "time", &cps->sendTime, cps)) continue;
15549 if (BoolFeature(&p, "draw", &cps->sendDrawOffers, cps)) continue;
15550 if (BoolFeature(&p, "sigint", &cps->useSigint, cps)) continue;
15551 if (BoolFeature(&p, "sigterm", &cps->useSigterm, cps)) continue;
15552 if (BoolFeature(&p, "reuse", &val, cps)) {
15553 /* Engine can disable reuse, but can't enable it if user said no */
15554 if (!val) cps->reuse = FALSE;
15557 if (BoolFeature(&p, "analyze", &cps->analysisSupport, cps)) continue;
15558 if (StringFeature(&p, "myname", cps->tidy, cps)) {
15559 if (gameMode == TwoMachinesPlay) {
15560 DisplayTwoMachinesTitle();
15566 if (StringFeature(&p, "variants", cps->variants, cps)) continue;
15567 if (BoolFeature(&p, "san", &cps->useSAN, cps)) continue;
15568 if (BoolFeature(&p, "ping", &cps->usePing, cps)) continue;
15569 if (BoolFeature(&p, "playother", &cps->usePlayother, cps)) continue;
15570 if (BoolFeature(&p, "colors", &cps->useColors, cps)) continue;
15571 if (BoolFeature(&p, "usermove", &cps->useUsermove, cps)) continue;
15572 if (BoolFeature(&p, "exclude", &cps->excludeMoves, cps)) continue;
15573 if (BoolFeature(&p, "ics", &cps->sendICS, cps)) continue;
15574 if (BoolFeature(&p, "name", &cps->sendName, cps)) continue;
15575 if (BoolFeature(&p, "pause", &val, cps)) continue; /* unused at present */
15576 if (IntFeature(&p, "done", &val, cps)) {
15577 FeatureDone(cps, val);
15580 /* Added by Tord: */
15581 if (BoolFeature(&p, "fen960", &cps->useFEN960, cps)) continue;
15582 if (BoolFeature(&p, "oocastle", &cps->useOOCastle, cps)) continue;
15583 /* End of additions by Tord */
15585 /* [HGM] added features: */
15586 if (BoolFeature(&p, "debug", &cps->debug, cps)) continue;
15587 if (BoolFeature(&p, "nps", &cps->supportsNPS, cps)) continue;
15588 if (IntFeature(&p, "level", &cps->maxNrOfSessions, cps)) continue;
15589 if (BoolFeature(&p, "memory", &cps->memSize, cps)) continue;
15590 if (BoolFeature(&p, "smp", &cps->maxCores, cps)) continue;
15591 if (StringFeature(&p, "egt", cps->egtFormats, cps)) continue;
15592 if (StringFeature(&p, "option", cps->option[cps->nrOptions].name, cps)) {
15593 if(!ParseOption(&(cps->option[cps->nrOptions++]), cps)) { // [HGM] options: add option feature
15594 snprintf(buf, MSG_SIZ, "rejected option %s\n", cps->option[--cps->nrOptions].name);
15595 SendToProgram(buf, cps);
15598 if(cps->nrOptions >= MAX_OPTIONS) {
15600 snprintf(buf, MSG_SIZ, _("%s engine has too many options\n"), _(cps->which));
15601 DisplayError(buf, 0);
15605 /* End of additions by HGM */
15607 /* unknown feature: complain and skip */
15609 while (*q && *q != '=') q++;
15610 snprintf(buf, MSG_SIZ,"rejected %.*s\n", (int)(q-p), p);
15611 SendToProgram(buf, cps);
15617 while (*p && *p != '\"') p++;
15618 if (*p == '\"') p++;
15620 while (*p && *p != ' ') p++;
15628 PeriodicUpdatesEvent (int newState)
15630 if (newState == appData.periodicUpdates)
15633 appData.periodicUpdates=newState;
15635 /* Display type changes, so update it now */
15636 // DisplayAnalysis();
15638 /* Get the ball rolling again... */
15640 AnalysisPeriodicEvent(1);
15641 StartAnalysisClock();
15646 PonderNextMoveEvent (int newState)
15648 if (newState == appData.ponderNextMove) return;
15649 if (gameMode == EditPosition) EditPositionDone(TRUE);
15651 SendToProgram("hard\n", &first);
15652 if (gameMode == TwoMachinesPlay) {
15653 SendToProgram("hard\n", &second);
15656 SendToProgram("easy\n", &first);
15657 thinkOutput[0] = NULLCHAR;
15658 if (gameMode == TwoMachinesPlay) {
15659 SendToProgram("easy\n", &second);
15662 appData.ponderNextMove = newState;
15666 NewSettingEvent (int option, int *feature, char *command, int value)
15670 if (gameMode == EditPosition) EditPositionDone(TRUE);
15671 snprintf(buf, MSG_SIZ,"%s%s %d\n", (option ? "option ": ""), command, value);
15672 if(feature == NULL || *feature) SendToProgram(buf, &first);
15673 if (gameMode == TwoMachinesPlay) {
15674 if(feature == NULL || feature[(int*)&second - (int*)&first]) SendToProgram(buf, &second);
15679 ShowThinkingEvent ()
15680 // [HGM] thinking: this routine is now also called from "Options -> Engine..." popup
15682 static int oldState = 2; // kludge alert! Neither true nor fals, so first time oldState is always updated
15683 int newState = appData.showThinking
15684 // [HGM] thinking: other features now need thinking output as well
15685 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp();
15687 if (oldState == newState) return;
15688 oldState = newState;
15689 if (gameMode == EditPosition) EditPositionDone(TRUE);
15691 SendToProgram("post\n", &first);
15692 if (gameMode == TwoMachinesPlay) {
15693 SendToProgram("post\n", &second);
15696 SendToProgram("nopost\n", &first);
15697 thinkOutput[0] = NULLCHAR;
15698 if (gameMode == TwoMachinesPlay) {
15699 SendToProgram("nopost\n", &second);
15702 // appData.showThinking = newState; // [HGM] thinking: responsible option should already have be changed when calling this routine!
15706 AskQuestionEvent (char *title, char *question, char *replyPrefix, char *which)
15708 ProcRef pr = (which[0] == '1') ? first.pr : second.pr;
15709 if (pr == NoProc) return;
15710 AskQuestion(title, question, replyPrefix, pr);
15714 TypeInEvent (char firstChar)
15716 if ((gameMode == BeginningOfGame && !appData.icsActive) ||
15717 gameMode == MachinePlaysWhite || gameMode == MachinePlaysBlack ||
15718 gameMode == AnalyzeMode || gameMode == EditGame ||
15719 gameMode == EditPosition || gameMode == IcsExamining ||
15720 gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
15721 isdigit(firstChar) && // [HGM] movenum: allow typing in of move nr in 'passive' modes
15722 ( gameMode == AnalyzeFile || gameMode == PlayFromGameFile ||
15723 gameMode == IcsObserving || gameMode == TwoMachinesPlay ) ||
15724 gameMode == Training) PopUpMoveDialog(firstChar);
15728 TypeInDoneEvent (char *move)
15731 int n, fromX, fromY, toX, toY;
15733 ChessMove moveType;
15736 if(gameMode == EditPosition && ParseFEN(board, &n, move) ) {
15737 EditPositionPasteFEN(move);
15740 // [HGM] movenum: allow move number to be typed in any mode
15741 if(sscanf(move, "%d", &n) == 1 && n != 0 ) {
15745 // undocumented kludge: allow command-line option to be typed in!
15746 // (potentially fatal, and does not implement the effect of the option.)
15747 // should only be used for options that are values on which future decisions will be made,
15748 // and definitely not on options that would be used during initialization.
15749 if(strstr(move, "!!! -") == move) {
15750 ParseArgsFromString(move+4);
15754 if (gameMode != EditGame && currentMove != forwardMostMove &&
15755 gameMode != Training) {
15756 DisplayMoveError(_("Displayed move is not current"));
15758 int ok = ParseOneMove(move, gameMode == EditPosition ? blackPlaysFirst : currentMove,
15759 &moveType, &fromX, &fromY, &toX, &toY, &promoChar);
15760 if(!ok && move[0] >= 'a') { move[0] += 'A' - 'a'; ok = 2; } // [HGM] try also capitalized
15761 if (ok==1 || ok && ParseOneMove(move, gameMode == EditPosition ? blackPlaysFirst : currentMove,
15762 &moveType, &fromX, &fromY, &toX, &toY, &promoChar)) {
15763 UserMoveEvent(fromX, fromY, toX, toY, promoChar);
15765 DisplayMoveError(_("Could not parse move"));
15771 DisplayMove (int moveNumber)
15773 char message[MSG_SIZ];
15775 char cpThinkOutput[MSG_SIZ];
15777 if(appData.noGUI) return; // [HGM] fast: suppress display of moves
15779 if (moveNumber == forwardMostMove - 1 ||
15780 gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
15782 safeStrCpy(cpThinkOutput, thinkOutput, sizeof(cpThinkOutput)/sizeof(cpThinkOutput[0]));
15784 if (strchr(cpThinkOutput, '\n')) {
15785 *strchr(cpThinkOutput, '\n') = NULLCHAR;
15788 *cpThinkOutput = NULLCHAR;
15791 /* [AS] Hide thinking from human user */
15792 if( appData.hideThinkingFromHuman && gameMode != TwoMachinesPlay ) {
15793 *cpThinkOutput = NULLCHAR;
15794 if( thinkOutput[0] != NULLCHAR ) {
15797 for( i=0; i<=hiddenThinkOutputState; i++ ) {
15798 cpThinkOutput[i] = '.';
15800 cpThinkOutput[i] = NULLCHAR;
15801 hiddenThinkOutputState = (hiddenThinkOutputState + 1) % 3;
15805 if (moveNumber == forwardMostMove - 1 &&
15806 gameInfo.resultDetails != NULL) {
15807 if (gameInfo.resultDetails[0] == NULLCHAR) {
15808 snprintf(res, MSG_SIZ, " %s", PGNResult(gameInfo.result));
15810 snprintf(res, MSG_SIZ, " {%s} %s",
15811 T_(gameInfo.resultDetails), PGNResult(gameInfo.result));
15817 if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
15818 DisplayMessage(res, cpThinkOutput);
15820 snprintf(message, MSG_SIZ, "%d.%s%s%s", moveNumber / 2 + 1,
15821 WhiteOnMove(moveNumber) ? " " : ".. ",
15822 parseList[moveNumber], res);
15823 DisplayMessage(message, cpThinkOutput);
15828 DisplayComment (int moveNumber, char *text)
15830 char title[MSG_SIZ];
15832 if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
15833 safeStrCpy(title, "Comment", sizeof(title)/sizeof(title[0]));
15835 snprintf(title,MSG_SIZ, "Comment on %d.%s%s", moveNumber / 2 + 1,
15836 WhiteOnMove(moveNumber) ? " " : ".. ",
15837 parseList[moveNumber]);
15839 if (text != NULL && (appData.autoDisplayComment || commentUp))
15840 CommentPopUp(title, text);
15843 /* This routine sends a ^C interrupt to gnuchess, to awaken it if it
15844 * might be busy thinking or pondering. It can be omitted if your
15845 * gnuchess is configured to stop thinking immediately on any user
15846 * input. However, that gnuchess feature depends on the FIONREAD
15847 * ioctl, which does not work properly on some flavors of Unix.
15850 Attention (ChessProgramState *cps)
15853 if (!cps->useSigint) return;
15854 if (appData.noChessProgram || (cps->pr == NoProc)) return;
15855 switch (gameMode) {
15856 case MachinePlaysWhite:
15857 case MachinePlaysBlack:
15858 case TwoMachinesPlay:
15859 case IcsPlayingWhite:
15860 case IcsPlayingBlack:
15863 /* Skip if we know it isn't thinking */
15864 if (!cps->maybeThinking) return;
15865 if (appData.debugMode)
15866 fprintf(debugFP, "Interrupting %s\n", cps->which);
15867 InterruptChildProcess(cps->pr);
15868 cps->maybeThinking = FALSE;
15873 #endif /*ATTENTION*/
15879 if (whiteTimeRemaining <= 0) {
15882 if (appData.icsActive) {
15883 if (appData.autoCallFlag &&
15884 gameMode == IcsPlayingBlack && !blackFlag) {
15885 SendToICS(ics_prefix);
15886 SendToICS("flag\n");
15890 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Both flags fell"));
15892 if(gameMode != TwoMachinesPlay) DisplayTitle(_("White's flag fell"));
15893 if (appData.autoCallFlag) {
15894 GameEnds(BlackWins, "Black wins on time", GE_XBOARD);
15901 if (blackTimeRemaining <= 0) {
15904 if (appData.icsActive) {
15905 if (appData.autoCallFlag &&
15906 gameMode == IcsPlayingWhite && !whiteFlag) {
15907 SendToICS(ics_prefix);
15908 SendToICS("flag\n");
15912 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Both flags fell"));
15914 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Black's flag fell"));
15915 if (appData.autoCallFlag) {
15916 GameEnds(WhiteWins, "White wins on time", GE_XBOARD);
15927 CheckTimeControl ()
15929 if (!appData.clockMode || appData.icsActive || searchTime || // [HGM] st: no inc in st mode
15930 gameMode == PlayFromGameFile || forwardMostMove == 0) return;
15933 * add time to clocks when time control is achieved ([HGM] now also used for increment)
15935 if ( !WhiteOnMove(forwardMostMove) ) {
15936 /* White made time control */
15937 lastWhite -= whiteTimeRemaining; // [HGM] contains start time, socalculate thinking time
15938 whiteTimeRemaining += GetTimeQuota((forwardMostMove-whiteStartMove-1)/2, lastWhite, whiteTC)
15939 /* [HGM] time odds: correct new time quota for time odds! */
15940 / WhitePlayer()->timeOdds;
15941 lastBlack = blackTimeRemaining; // [HGM] leave absolute time (after quota), so next switch we can us it to calculate thinking time
15943 lastBlack -= blackTimeRemaining;
15944 /* Black made time control */
15945 blackTimeRemaining += GetTimeQuota((forwardMostMove-blackStartMove-1)/2, lastBlack, blackTC)
15946 / WhitePlayer()->other->timeOdds;
15947 lastWhite = whiteTimeRemaining;
15952 DisplayBothClocks ()
15954 int wom = gameMode == EditPosition ?
15955 !blackPlaysFirst : WhiteOnMove(currentMove);
15956 DisplayWhiteClock(whiteTimeRemaining, wom);
15957 DisplayBlackClock(blackTimeRemaining, !wom);
15961 /* Timekeeping seems to be a portability nightmare. I think everyone
15962 has ftime(), but I'm really not sure, so I'm including some ifdefs
15963 to use other calls if you don't. Clocks will be less accurate if
15964 you have neither ftime nor gettimeofday.
15967 /* VS 2008 requires the #include outside of the function */
15968 #if !HAVE_GETTIMEOFDAY && HAVE_FTIME
15969 #include <sys/timeb.h>
15972 /* Get the current time as a TimeMark */
15974 GetTimeMark (TimeMark *tm)
15976 #if HAVE_GETTIMEOFDAY
15978 struct timeval timeVal;
15979 struct timezone timeZone;
15981 gettimeofday(&timeVal, &timeZone);
15982 tm->sec = (long) timeVal.tv_sec;
15983 tm->ms = (int) (timeVal.tv_usec / 1000L);
15985 #else /*!HAVE_GETTIMEOFDAY*/
15988 // include <sys/timeb.h> / moved to just above start of function
15989 struct timeb timeB;
15992 tm->sec = (long) timeB.time;
15993 tm->ms = (int) timeB.millitm;
15995 #else /*!HAVE_FTIME && !HAVE_GETTIMEOFDAY*/
15996 tm->sec = (long) time(NULL);
16002 /* Return the difference in milliseconds between two
16003 time marks. We assume the difference will fit in a long!
16006 SubtractTimeMarks (TimeMark *tm2, TimeMark *tm1)
16008 return 1000L*(tm2->sec - tm1->sec) +
16009 (long) (tm2->ms - tm1->ms);
16014 * Code to manage the game clocks.
16016 * In tournament play, black starts the clock and then white makes a move.
16017 * We give the human user a slight advantage if he is playing white---the
16018 * clocks don't run until he makes his first move, so it takes zero time.
16019 * Also, we don't account for network lag, so we could get out of sync
16020 * with GNU Chess's clock -- but then, referees are always right.
16023 static TimeMark tickStartTM;
16024 static long intendedTickLength;
16027 NextTickLength (long timeRemaining)
16029 long nominalTickLength, nextTickLength;
16031 if (timeRemaining > 0L && timeRemaining <= 10000L)
16032 nominalTickLength = 100L;
16034 nominalTickLength = 1000L;
16035 nextTickLength = timeRemaining % nominalTickLength;
16036 if (nextTickLength <= 0) nextTickLength += nominalTickLength;
16038 return nextTickLength;
16041 /* Adjust clock one minute up or down */
16043 AdjustClock (Boolean which, int dir)
16045 if(appData.autoCallFlag) { DisplayError(_("Clock adjustment not allowed in auto-flag mode"), 0); return; }
16046 if(which) blackTimeRemaining += 60000*dir;
16047 else whiteTimeRemaining += 60000*dir;
16048 DisplayBothClocks();
16049 adjustedClock = TRUE;
16052 /* Stop clocks and reset to a fresh time control */
16056 (void) StopClockTimer();
16057 if (appData.icsActive) {
16058 whiteTimeRemaining = blackTimeRemaining = 0;
16059 } else if (searchTime) {
16060 whiteTimeRemaining = 1000*searchTime / WhitePlayer()->timeOdds;
16061 blackTimeRemaining = 1000*searchTime / WhitePlayer()->other->timeOdds;
16062 } else { /* [HGM] correct new time quote for time odds */
16063 whiteTC = blackTC = fullTimeControlString;
16064 whiteTimeRemaining = GetTimeQuota(-1, 0, whiteTC) / WhitePlayer()->timeOdds;
16065 blackTimeRemaining = GetTimeQuota(-1, 0, blackTC) / WhitePlayer()->other->timeOdds;
16067 if (whiteFlag || blackFlag) {
16069 whiteFlag = blackFlag = FALSE;
16071 lastWhite = lastBlack = whiteStartMove = blackStartMove = 0;
16072 DisplayBothClocks();
16073 adjustedClock = FALSE;
16076 #define FUDGE 25 /* 25ms = 1/40 sec; should be plenty even for 50 Hz clocks */
16078 /* Decrement running clock by amount of time that has passed */
16082 long timeRemaining;
16083 long lastTickLength, fudge;
16086 if (!appData.clockMode) return;
16087 if (gameMode==AnalyzeMode || gameMode == AnalyzeFile) return;
16091 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
16093 /* Fudge if we woke up a little too soon */
16094 fudge = intendedTickLength - lastTickLength;
16095 if (fudge < 0 || fudge > FUDGE) fudge = 0;
16097 if (WhiteOnMove(forwardMostMove)) {
16098 if(whiteNPS >= 0) lastTickLength = 0;
16099 timeRemaining = whiteTimeRemaining -= lastTickLength;
16100 if(timeRemaining < 0 && !appData.icsActive) {
16101 GetTimeQuota((forwardMostMove-whiteStartMove-1)/2, 0, whiteTC); // sets suddenDeath & nextSession;
16102 if(suddenDeath) { // [HGM] if we run out of a non-last incremental session, go to the next
16103 whiteStartMove = forwardMostMove; whiteTC = nextSession;
16104 lastWhite= timeRemaining = whiteTimeRemaining += GetTimeQuota(-1, 0, whiteTC);
16107 DisplayWhiteClock(whiteTimeRemaining - fudge,
16108 WhiteOnMove(currentMove < forwardMostMove ? currentMove : forwardMostMove));
16110 if(blackNPS >= 0) lastTickLength = 0;
16111 timeRemaining = blackTimeRemaining -= lastTickLength;
16112 if(timeRemaining < 0 && !appData.icsActive) { // [HGM] if we run out of a non-last incremental session, go to the next
16113 GetTimeQuota((forwardMostMove-blackStartMove-1)/2, 0, blackTC);
16115 blackStartMove = forwardMostMove;
16116 lastBlack = timeRemaining = blackTimeRemaining += GetTimeQuota(-1, 0, blackTC=nextSession);
16119 DisplayBlackClock(blackTimeRemaining - fudge,
16120 !WhiteOnMove(currentMove < forwardMostMove ? currentMove : forwardMostMove));
16122 if (CheckFlags()) return;
16125 intendedTickLength = NextTickLength(timeRemaining - fudge) + fudge;
16126 StartClockTimer(intendedTickLength);
16128 /* if the time remaining has fallen below the alarm threshold, sound the
16129 * alarm. if the alarm has sounded and (due to a takeback or time control
16130 * with increment) the time remaining has increased to a level above the
16131 * threshold, reset the alarm so it can sound again.
16134 if (appData.icsActive && appData.icsAlarm) {
16136 /* make sure we are dealing with the user's clock */
16137 if (!( ((gameMode == IcsPlayingWhite) && WhiteOnMove(currentMove)) ||
16138 ((gameMode == IcsPlayingBlack) && !WhiteOnMove(currentMove))
16141 if (alarmSounded && (timeRemaining > appData.icsAlarmTime)) {
16142 alarmSounded = FALSE;
16143 } else if (!alarmSounded && (timeRemaining <= appData.icsAlarmTime)) {
16145 alarmSounded = TRUE;
16151 /* A player has just moved, so stop the previously running
16152 clock and (if in clock mode) start the other one.
16153 We redisplay both clocks in case we're in ICS mode, because
16154 ICS gives us an update to both clocks after every move.
16155 Note that this routine is called *after* forwardMostMove
16156 is updated, so the last fractional tick must be subtracted
16157 from the color that is *not* on move now.
16160 SwitchClocks (int newMoveNr)
16162 long lastTickLength;
16164 int flagged = FALSE;
16168 if (StopClockTimer() && appData.clockMode) {
16169 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
16170 if (!WhiteOnMove(forwardMostMove)) {
16171 if(blackNPS >= 0) lastTickLength = 0;
16172 blackTimeRemaining -= lastTickLength;
16173 /* [HGM] PGNtime: save time for PGN file if engine did not give it */
16174 // if(pvInfoList[forwardMostMove].time == -1)
16175 pvInfoList[forwardMostMove].time = // use GUI time
16176 (timeRemaining[1][forwardMostMove-1] - blackTimeRemaining)/10;
16178 if(whiteNPS >= 0) lastTickLength = 0;
16179 whiteTimeRemaining -= lastTickLength;
16180 /* [HGM] PGNtime: save time for PGN file if engine did not give it */
16181 // if(pvInfoList[forwardMostMove].time == -1)
16182 pvInfoList[forwardMostMove].time =
16183 (timeRemaining[0][forwardMostMove-1] - whiteTimeRemaining)/10;
16185 flagged = CheckFlags();
16187 forwardMostMove = newMoveNr; // [HGM] race: change stm when no timer interrupt scheduled
16188 CheckTimeControl();
16190 if (flagged || !appData.clockMode) return;
16192 switch (gameMode) {
16193 case MachinePlaysBlack:
16194 case MachinePlaysWhite:
16195 case BeginningOfGame:
16196 if (pausing) return;
16200 case PlayFromGameFile:
16208 if (searchTime) { // [HGM] st: set clock of player that has to move to max time
16209 if(WhiteOnMove(forwardMostMove))
16210 whiteTimeRemaining = 1000*searchTime / WhitePlayer()->timeOdds;
16211 else blackTimeRemaining = 1000*searchTime / WhitePlayer()->other->timeOdds;
16215 intendedTickLength = NextTickLength(WhiteOnMove(forwardMostMove) ?
16216 whiteTimeRemaining : blackTimeRemaining);
16217 StartClockTimer(intendedTickLength);
16221 /* Stop both clocks */
16225 long lastTickLength;
16228 if (!StopClockTimer()) return;
16229 if (!appData.clockMode) return;
16233 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
16234 if (WhiteOnMove(forwardMostMove)) {
16235 if(whiteNPS >= 0) lastTickLength = 0;
16236 whiteTimeRemaining -= lastTickLength;
16237 DisplayWhiteClock(whiteTimeRemaining, WhiteOnMove(currentMove));
16239 if(blackNPS >= 0) lastTickLength = 0;
16240 blackTimeRemaining -= lastTickLength;
16241 DisplayBlackClock(blackTimeRemaining, !WhiteOnMove(currentMove));
16246 /* Start clock of player on move. Time may have been reset, so
16247 if clock is already running, stop and restart it. */
16251 (void) StopClockTimer(); /* in case it was running already */
16252 DisplayBothClocks();
16253 if (CheckFlags()) return;
16255 if (!appData.clockMode) return;
16256 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) return;
16258 GetTimeMark(&tickStartTM);
16259 intendedTickLength = NextTickLength(WhiteOnMove(forwardMostMove) ?
16260 whiteTimeRemaining : blackTimeRemaining);
16262 /* [HGM] nps: figure out nps factors, by determining which engine plays white and/or black once and for all */
16263 whiteNPS = blackNPS = -1;
16264 if(gameMode == MachinePlaysWhite || gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w'
16265 || appData.zippyPlay && gameMode == IcsPlayingBlack) // first (perhaps only) engine has white
16266 whiteNPS = first.nps;
16267 if(gameMode == MachinePlaysBlack || gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b'
16268 || appData.zippyPlay && gameMode == IcsPlayingWhite) // first (perhaps only) engine has black
16269 blackNPS = first.nps;
16270 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b') // second only used in Two-Machines mode
16271 whiteNPS = second.nps;
16272 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w')
16273 blackNPS = second.nps;
16274 if(appData.debugMode) fprintf(debugFP, "nps: w=%d, b=%d\n", whiteNPS, blackNPS);
16276 StartClockTimer(intendedTickLength);
16280 TimeString (long ms)
16282 long second, minute, hour, day;
16284 static char buf[32];
16286 if (ms > 0 && ms <= 9900) {
16287 /* convert milliseconds to tenths, rounding up */
16288 double tenths = floor( ((double)(ms + 99L)) / 100.00 );
16290 snprintf(buf,sizeof(buf)/sizeof(buf[0]), " %03.1f ", tenths/10.0);
16294 /* convert milliseconds to seconds, rounding up */
16295 /* use floating point to avoid strangeness of integer division
16296 with negative dividends on many machines */
16297 second = (long) floor(((double) (ms + 999L)) / 1000.0);
16304 day = second / (60 * 60 * 24);
16305 second = second % (60 * 60 * 24);
16306 hour = second / (60 * 60);
16307 second = second % (60 * 60);
16308 minute = second / 60;
16309 second = second % 60;
16312 snprintf(buf, sizeof(buf)/sizeof(buf[0]), " %s%ld:%02ld:%02ld:%02ld ",
16313 sign, day, hour, minute, second);
16315 snprintf(buf, sizeof(buf)/sizeof(buf[0]), " %s%ld:%02ld:%02ld ", sign, hour, minute, second);
16317 snprintf(buf, sizeof(buf)/sizeof(buf[0]), " %s%2ld:%02ld ", sign, minute, second);
16324 * This is necessary because some C libraries aren't ANSI C compliant yet.
16327 StrStr (char *string, char *match)
16331 length = strlen(match);
16333 for (i = strlen(string) - length; i >= 0; i--, string++)
16334 if (!strncmp(match, string, length))
16341 StrCaseStr (char *string, char *match)
16345 length = strlen(match);
16347 for (i = strlen(string) - length; i >= 0; i--, string++) {
16348 for (j = 0; j < length; j++) {
16349 if (ToLower(match[j]) != ToLower(string[j]))
16352 if (j == length) return string;
16360 StrCaseCmp (char *s1, char *s2)
16365 c1 = ToLower(*s1++);
16366 c2 = ToLower(*s2++);
16367 if (c1 > c2) return 1;
16368 if (c1 < c2) return -1;
16369 if (c1 == NULLCHAR) return 0;
16377 return isupper(c) ? tolower(c) : c;
16384 return islower(c) ? toupper(c) : c;
16386 #endif /* !_amigados */
16393 if ((ret = (char *) malloc(strlen(s) + 1)))
16395 safeStrCpy(ret, s, strlen(s)+1);
16401 StrSavePtr (char *s, char **savePtr)
16406 if ((*savePtr = (char *) malloc(strlen(s) + 1))) {
16407 safeStrCpy(*savePtr, s, strlen(s)+1);
16419 clock = time((time_t *)NULL);
16420 tm = localtime(&clock);
16421 snprintf(buf, MSG_SIZ, "%04d.%02d.%02d",
16422 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
16423 return StrSave(buf);
16428 PositionToFEN (int move, char *overrideCastling)
16430 int i, j, fromX, fromY, toX, toY;
16437 whiteToPlay = (gameMode == EditPosition) ?
16438 !blackPlaysFirst : (move % 2 == 0);
16441 /* Piece placement data */
16442 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
16443 if(MSG_SIZ - (p - buf) < BOARD_RGHT - BOARD_LEFT + 20) { *p = 0; return StrSave(buf); }
16445 for (j = BOARD_LEFT; j < BOARD_RGHT; j++) {
16446 if (boards[move][i][j] == EmptySquare) {
16448 } else { ChessSquare piece = boards[move][i][j];
16449 if (emptycount > 0) {
16450 if(emptycount<10) /* [HGM] can be >= 10 */
16451 *p++ = '0' + emptycount;
16452 else { *p++ = '0' + emptycount/10; *p++ = '0' + emptycount%10; }
16455 if(PieceToChar(piece) == '+') {
16456 /* [HGM] write promoted pieces as '+<unpromoted>' (Shogi) */
16458 piece = (ChessSquare)(DEMOTED piece);
16460 *p++ = PieceToChar(piece);
16462 /* [HGM] flag promoted pieces as '<promoted>~' (Crazyhouse) */
16463 p[-1] = PieceToChar((ChessSquare)(DEMOTED piece));
16468 if (emptycount > 0) {
16469 if(emptycount<10) /* [HGM] can be >= 10 */
16470 *p++ = '0' + emptycount;
16471 else { *p++ = '0' + emptycount/10; *p++ = '0' + emptycount%10; }
16478 /* [HGM] print Crazyhouse or Shogi holdings */
16479 if( gameInfo.holdingsWidth ) {
16480 *(p-1) = '['; /* if we wanted to support BFEN, this could be '/' */
16482 for(i=0; i<gameInfo.holdingsSize; i++) { /* white holdings */
16483 piece = boards[move][i][BOARD_WIDTH-1];
16484 if( piece != EmptySquare )
16485 for(j=0; j<(int) boards[move][i][BOARD_WIDTH-2]; j++)
16486 *p++ = PieceToChar(piece);
16488 for(i=0; i<gameInfo.holdingsSize; i++) { /* black holdings */
16489 piece = boards[move][BOARD_HEIGHT-i-1][0];
16490 if( piece != EmptySquare )
16491 for(j=0; j<(int) boards[move][BOARD_HEIGHT-i-1][1]; j++)
16492 *p++ = PieceToChar(piece);
16495 if( q == p ) *p++ = '-';
16501 *p++ = whiteToPlay ? 'w' : 'b';
16504 if(q = overrideCastling) { // [HGM] FRC: override castling & e.p fields for non-compliant engines
16505 while(*p++ = *q++); if(q != overrideCastling+1) p[-1] = ' '; else --p;
16507 if(nrCastlingRights) {
16509 if(gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom) {
16510 /* [HGM] write directly from rights */
16511 if(boards[move][CASTLING][2] != NoRights &&
16512 boards[move][CASTLING][0] != NoRights )
16513 *p++ = boards[move][CASTLING][0] + AAA + 'A' - 'a';
16514 if(boards[move][CASTLING][2] != NoRights &&
16515 boards[move][CASTLING][1] != NoRights )
16516 *p++ = boards[move][CASTLING][1] + AAA + 'A' - 'a';
16517 if(boards[move][CASTLING][5] != NoRights &&
16518 boards[move][CASTLING][3] != NoRights )
16519 *p++ = boards[move][CASTLING][3] + AAA;
16520 if(boards[move][CASTLING][5] != NoRights &&
16521 boards[move][CASTLING][4] != NoRights )
16522 *p++ = boards[move][CASTLING][4] + AAA;
16525 /* [HGM] write true castling rights */
16526 if( nrCastlingRights == 6 ) {
16527 if(boards[move][CASTLING][0] == BOARD_RGHT-1 &&
16528 boards[move][CASTLING][2] != NoRights ) *p++ = 'K';
16529 if(boards[move][CASTLING][1] == BOARD_LEFT &&
16530 boards[move][CASTLING][2] != NoRights ) *p++ = 'Q';
16531 if(boards[move][CASTLING][3] == BOARD_RGHT-1 &&
16532 boards[move][CASTLING][5] != NoRights ) *p++ = 'k';
16533 if(boards[move][CASTLING][4] == BOARD_LEFT &&
16534 boards[move][CASTLING][5] != NoRights ) *p++ = 'q';
16537 if (q == p) *p++ = '-'; /* No castling rights */
16541 if(gameInfo.variant != VariantShogi && gameInfo.variant != VariantXiangqi &&
16542 gameInfo.variant != VariantShatranj && gameInfo.variant != VariantCourier && gameInfo.variant != VariantMakruk ) {
16543 /* En passant target square */
16544 if (move > backwardMostMove) {
16545 fromX = moveList[move - 1][0] - AAA;
16546 fromY = moveList[move - 1][1] - ONE;
16547 toX = moveList[move - 1][2] - AAA;
16548 toY = moveList[move - 1][3] - ONE;
16549 if (fromY == (whiteToPlay ? BOARD_HEIGHT-2 : 1) &&
16550 toY == (whiteToPlay ? BOARD_HEIGHT-4 : 3) &&
16551 boards[move][toY][toX] == (whiteToPlay ? BlackPawn : WhitePawn) &&
16553 /* 2-square pawn move just happened */
16555 *p++ = whiteToPlay ? '6'+BOARD_HEIGHT-8 : '3';
16559 } else if(move == backwardMostMove) {
16560 // [HGM] perhaps we should always do it like this, and forget the above?
16561 if((signed char)boards[move][EP_STATUS] >= 0) {
16562 *p++ = boards[move][EP_STATUS] + AAA;
16563 *p++ = whiteToPlay ? '6'+BOARD_HEIGHT-8 : '3';
16574 /* [HGM] find reversible plies */
16575 { int i = 0, j=move;
16577 if (appData.debugMode) { int k;
16578 fprintf(debugFP, "write FEN 50-move: %d %d %d\n", initialRulePlies, forwardMostMove, backwardMostMove);
16579 for(k=backwardMostMove; k<=forwardMostMove; k++)
16580 fprintf(debugFP, "e%d. p=%d\n", k, (signed char)boards[k][EP_STATUS]);
16584 while(j > backwardMostMove && (signed char)boards[j][EP_STATUS] <= EP_NONE) j--,i++;
16585 if( j == backwardMostMove ) i += initialRulePlies;
16586 sprintf(p, "%d ", i);
16587 p += i>=100 ? 4 : i >= 10 ? 3 : 2;
16589 /* Fullmove number */
16590 sprintf(p, "%d", (move / 2) + 1);
16592 return StrSave(buf);
16596 ParseFEN (Board board, int *blackPlaysFirst, char *fen)
16605 /* [HGM] by default clear Crazyhouse holdings, if present */
16606 if(gameInfo.holdingsWidth) {
16607 for(i=0; i<BOARD_HEIGHT; i++) {
16608 board[i][0] = EmptySquare; /* black holdings */
16609 board[i][BOARD_WIDTH-1] = EmptySquare; /* white holdings */
16610 board[i][1] = (ChessSquare) 0; /* black counts */
16611 board[i][BOARD_WIDTH-2] = (ChessSquare) 0; /* white counts */
16615 /* Piece placement data */
16616 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
16619 if (*p == '/' || *p == ' ' || (*p == '[' && i == 0) ) {
16620 if (*p == '/') p++;
16621 emptycount = gameInfo.boardWidth - j;
16622 while (emptycount--)
16623 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
16625 #if(BOARD_FILES >= 10)
16626 } else if(*p=='x' || *p=='X') { /* [HGM] X means 10 */
16627 p++; emptycount=10;
16628 if (j + emptycount > gameInfo.boardWidth) return FALSE;
16629 while (emptycount--)
16630 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
16632 } else if (isdigit(*p)) {
16633 emptycount = *p++ - '0';
16634 while(isdigit(*p)) emptycount = 10*emptycount + *p++ - '0'; /* [HGM] allow > 9 */
16635 if (j + emptycount > gameInfo.boardWidth) return FALSE;
16636 while (emptycount--)
16637 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
16638 } else if (*p == '+' || isalpha(*p)) {
16639 if (j >= gameInfo.boardWidth) return FALSE;
16641 piece = CharToPiece(*++p);
16642 if(piece == EmptySquare) return FALSE; /* unknown piece */
16643 piece = (ChessSquare) (PROMOTED piece ); p++;
16644 if(PieceToChar(piece) != '+') return FALSE; /* unpromotable piece */
16645 } else piece = CharToPiece(*p++);
16647 if(piece==EmptySquare) return FALSE; /* unknown piece */
16648 if(*p == '~') { /* [HGM] make it a promoted piece for Crazyhouse */
16649 piece = (ChessSquare) (PROMOTED piece);
16650 if(PieceToChar(piece) != '~') return FALSE; /* cannot be a promoted piece */
16653 board[i][(j++)+gameInfo.holdingsWidth] = piece;
16659 while (*p == '/' || *p == ' ') p++;
16661 /* [HGM] look for Crazyhouse holdings here */
16662 while(*p==' ') p++;
16663 if( gameInfo.holdingsWidth && p[-1] == '/' || *p == '[') {
16665 if(*p == '-' ) p++; /* empty holdings */ else {
16666 if( !gameInfo.holdingsWidth ) return FALSE; /* no room to put holdings! */
16667 /* if we would allow FEN reading to set board size, we would */
16668 /* have to add holdings and shift the board read so far here */
16669 while( (piece = CharToPiece(*p) ) != EmptySquare ) {
16671 if((int) piece >= (int) BlackPawn ) {
16672 i = (int)piece - (int)BlackPawn;
16673 i = PieceToNumber((ChessSquare)i);
16674 if( i >= gameInfo.holdingsSize ) return FALSE;
16675 board[BOARD_HEIGHT-1-i][0] = piece; /* black holdings */
16676 board[BOARD_HEIGHT-1-i][1]++; /* black counts */
16678 i = (int)piece - (int)WhitePawn;
16679 i = PieceToNumber((ChessSquare)i);
16680 if( i >= gameInfo.holdingsSize ) return FALSE;
16681 board[i][BOARD_WIDTH-1] = piece; /* white holdings */
16682 board[i][BOARD_WIDTH-2]++; /* black holdings */
16689 while(*p == ' ') p++;
16693 if(appData.colorNickNames) {
16694 if( c == appData.colorNickNames[0] ) c = 'w'; else
16695 if( c == appData.colorNickNames[1] ) c = 'b';
16699 *blackPlaysFirst = FALSE;
16702 *blackPlaysFirst = TRUE;
16708 /* [HGM] We NO LONGER ignore the rest of the FEN notation */
16709 /* return the extra info in global variiables */
16711 /* set defaults in case FEN is incomplete */
16712 board[EP_STATUS] = EP_UNKNOWN;
16713 for(i=0; i<nrCastlingRights; i++ ) {
16714 board[CASTLING][i] =
16715 gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom ? NoRights : initialRights[i];
16716 } /* assume possible unless obviously impossible */
16717 if(initialRights[0]!=NoRights && board[castlingRank[0]][initialRights[0]] != WhiteRook) board[CASTLING][0] = NoRights;
16718 if(initialRights[1]!=NoRights && board[castlingRank[1]][initialRights[1]] != WhiteRook) board[CASTLING][1] = NoRights;
16719 if(initialRights[2]!=NoRights && board[castlingRank[2]][initialRights[2]] != WhiteUnicorn
16720 && board[castlingRank[2]][initialRights[2]] != WhiteKing) board[CASTLING][2] = NoRights;
16721 if(initialRights[3]!=NoRights && board[castlingRank[3]][initialRights[3]] != BlackRook) board[CASTLING][3] = NoRights;
16722 if(initialRights[4]!=NoRights && board[castlingRank[4]][initialRights[4]] != BlackRook) board[CASTLING][4] = NoRights;
16723 if(initialRights[5]!=NoRights && board[castlingRank[5]][initialRights[5]] != BlackUnicorn
16724 && board[castlingRank[5]][initialRights[5]] != BlackKing) board[CASTLING][5] = NoRights;
16727 while(*p==' ') p++;
16728 if(nrCastlingRights) {
16729 if(*p=='K' || *p=='Q' || *p=='k' || *p=='q' || *p=='-') {
16730 /* castling indicator present, so default becomes no castlings */
16731 for(i=0; i<nrCastlingRights; i++ ) {
16732 board[CASTLING][i] = NoRights;
16735 while(*p=='K' || *p=='Q' || *p=='k' || *p=='q' || *p=='-' ||
16736 (gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom) &&
16737 ( *p >= 'a' && *p < 'a' + gameInfo.boardWidth) ||
16738 ( *p >= 'A' && *p < 'A' + gameInfo.boardWidth) ) {
16739 char c = *p++; int whiteKingFile=NoRights, blackKingFile=NoRights;
16741 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) {
16742 if(board[BOARD_HEIGHT-1][i] == BlackKing) blackKingFile = i;
16743 if(board[0 ][i] == WhiteKing) whiteKingFile = i;
16745 if(gameInfo.variant == VariantTwoKings || gameInfo.variant == VariantKnightmate)
16746 whiteKingFile = blackKingFile = BOARD_WIDTH >> 1; // for these variant scanning fails
16747 if(whiteKingFile == NoRights || board[0][whiteKingFile] != WhiteUnicorn
16748 && board[0][whiteKingFile] != WhiteKing) whiteKingFile = NoRights;
16749 if(blackKingFile == NoRights || board[BOARD_HEIGHT-1][blackKingFile] != BlackUnicorn
16750 && board[BOARD_HEIGHT-1][blackKingFile] != BlackKing) blackKingFile = NoRights;
16753 for(i=BOARD_RGHT-1; board[0][i]!=WhiteRook && i>whiteKingFile; i--);
16754 board[CASTLING][0] = i != whiteKingFile ? i : NoRights;
16755 board[CASTLING][2] = whiteKingFile;
16758 for(i=BOARD_LEFT; i<BOARD_RGHT && board[0][i]!=WhiteRook && i<whiteKingFile; i++);
16759 board[CASTLING][1] = i != whiteKingFile ? i : NoRights;
16760 board[CASTLING][2] = whiteKingFile;
16763 for(i=BOARD_RGHT-1; board[BOARD_HEIGHT-1][i]!=BlackRook && i>blackKingFile; i--);
16764 board[CASTLING][3] = i != blackKingFile ? i : NoRights;
16765 board[CASTLING][5] = blackKingFile;
16768 for(i=BOARD_LEFT; i<BOARD_RGHT && board[BOARD_HEIGHT-1][i]!=BlackRook && i<blackKingFile; i++);
16769 board[CASTLING][4] = i != blackKingFile ? i : NoRights;
16770 board[CASTLING][5] = blackKingFile;
16773 default: /* FRC castlings */
16774 if(c >= 'a') { /* black rights */
16775 for(i=BOARD_LEFT; i<BOARD_RGHT; i++)
16776 if(board[BOARD_HEIGHT-1][i] == BlackKing) break;
16777 if(i == BOARD_RGHT) break;
16778 board[CASTLING][5] = i;
16780 if(board[BOARD_HEIGHT-1][c] < BlackPawn ||
16781 board[BOARD_HEIGHT-1][c] >= BlackKing ) break;
16783 board[CASTLING][3] = c;
16785 board[CASTLING][4] = c;
16786 } else { /* white rights */
16787 for(i=BOARD_LEFT; i<BOARD_RGHT; i++)
16788 if(board[0][i] == WhiteKing) break;
16789 if(i == BOARD_RGHT) break;
16790 board[CASTLING][2] = i;
16791 c -= AAA - 'a' + 'A';
16792 if(board[0][c] >= WhiteKing) break;
16794 board[CASTLING][0] = c;
16796 board[CASTLING][1] = c;
16800 for(i=0; i<nrCastlingRights; i++)
16801 if(board[CASTLING][i] != NoRights) initialRights[i] = board[CASTLING][i];
16802 if (appData.debugMode) {
16803 fprintf(debugFP, "FEN castling rights:");
16804 for(i=0; i<nrCastlingRights; i++)
16805 fprintf(debugFP, " %d", board[CASTLING][i]);
16806 fprintf(debugFP, "\n");
16809 while(*p==' ') p++;
16812 /* read e.p. field in games that know e.p. capture */
16813 if(gameInfo.variant != VariantShogi && gameInfo.variant != VariantXiangqi &&
16814 gameInfo.variant != VariantShatranj && gameInfo.variant != VariantCourier && gameInfo.variant != VariantMakruk ) {
16816 p++; board[EP_STATUS] = EP_NONE;
16818 char c = *p++ - AAA;
16820 if(c < BOARD_LEFT || c >= BOARD_RGHT) return TRUE;
16821 if(*p >= '0' && *p <='9') p++;
16822 board[EP_STATUS] = c;
16827 if(sscanf(p, "%d", &i) == 1) {
16828 FENrulePlies = i; /* 50-move ply counter */
16829 /* (The move number is still ignored) */
16836 EditPositionPasteFEN (char *fen)
16839 Board initial_position;
16841 if (!ParseFEN(initial_position, &blackPlaysFirst, fen)) {
16842 DisplayError(_("Bad FEN position in clipboard"), 0);
16845 int savedBlackPlaysFirst = blackPlaysFirst;
16846 EditPositionEvent();
16847 blackPlaysFirst = savedBlackPlaysFirst;
16848 CopyBoard(boards[0], initial_position);
16849 initialRulePlies = FENrulePlies; /* [HGM] copy FEN attributes as well */
16850 EditPositionDone(FALSE); // [HGM] fake: do not fake rights if we had FEN
16851 DisplayBothClocks();
16852 DrawPosition(FALSE, boards[currentMove]);
16857 static char cseq[12] = "\\ ";
16860 set_cont_sequence (char *new_seq)
16865 // handle bad attempts to set the sequence
16867 return 0; // acceptable error - no debug
16869 len = strlen(new_seq);
16870 ret = (len > 0) && (len < sizeof(cseq));
16872 safeStrCpy(cseq, new_seq, sizeof(cseq)/sizeof(cseq[0]));
16873 else if (appData.debugMode)
16874 fprintf(debugFP, "Invalid continuation sequence \"%s\" (maximum length is: %u)\n", new_seq, (unsigned) sizeof(cseq)-1);
16879 reformat a source message so words don't cross the width boundary. internal
16880 newlines are not removed. returns the wrapped size (no null character unless
16881 included in source message). If dest is NULL, only calculate the size required
16882 for the dest buffer. lp argument indicats line position upon entry, and it's
16883 passed back upon exit.
16886 wrap (char *dest, char *src, int count, int width, int *lp)
16888 int len, i, ansi, cseq_len, line, old_line, old_i, old_len, clen;
16890 cseq_len = strlen(cseq);
16891 old_line = line = *lp;
16892 ansi = len = clen = 0;
16894 for (i=0; i < count; i++)
16896 if (src[i] == '\033')
16899 // if we hit the width, back up
16900 if (!ansi && (line >= width) && src[i] != '\n' && src[i] != ' ')
16902 // store i & len in case the word is too long
16903 old_i = i, old_len = len;
16905 // find the end of the last word
16906 while (i && src[i] != ' ' && src[i] != '\n')
16912 // word too long? restore i & len before splitting it
16913 if ((old_i-i+clen) >= width)
16920 if (i && src[i-1] == ' ')
16923 if (src[i] != ' ' && src[i] != '\n')
16930 // now append the newline and continuation sequence
16935 strncpy(dest+len, cseq, cseq_len);
16943 dest[len] = src[i];
16947 if (src[i] == '\n')
16952 if (dest && appData.debugMode)
16954 fprintf(debugFP, "wrap(count:%d,width:%d,line:%d,len:%d,*lp:%d,src: ",
16955 count, width, line, len, *lp);
16956 show_bytes(debugFP, src, count);
16957 fprintf(debugFP, "\ndest: ");
16958 show_bytes(debugFP, dest, len);
16959 fprintf(debugFP, "\n");
16961 *lp = dest ? line : old_line;
16966 // [HGM] vari: routines for shelving variations
16967 Boolean modeRestore = FALSE;
16970 PushInner (int firstMove, int lastMove)
16972 int i, j, nrMoves = lastMove - firstMove;
16974 // push current tail of game on stack
16975 savedResult[storedGames] = gameInfo.result;
16976 savedDetails[storedGames] = gameInfo.resultDetails;
16977 gameInfo.resultDetails = NULL;
16978 savedFirst[storedGames] = firstMove;
16979 savedLast [storedGames] = lastMove;
16980 savedFramePtr[storedGames] = framePtr;
16981 framePtr -= nrMoves; // reserve space for the boards
16982 for(i=nrMoves; i>=1; i--) { // copy boards to stack, working downwards, in case of overlap
16983 CopyBoard(boards[framePtr+i], boards[firstMove+i]);
16984 for(j=0; j<MOVE_LEN; j++)
16985 moveList[framePtr+i][j] = moveList[firstMove+i-1][j];
16986 for(j=0; j<2*MOVE_LEN; j++)
16987 parseList[framePtr+i][j] = parseList[firstMove+i-1][j];
16988 timeRemaining[0][framePtr+i] = timeRemaining[0][firstMove+i];
16989 timeRemaining[1][framePtr+i] = timeRemaining[1][firstMove+i];
16990 pvInfoList[framePtr+i] = pvInfoList[firstMove+i-1];
16991 pvInfoList[firstMove+i-1].depth = 0;
16992 commentList[framePtr+i] = commentList[firstMove+i];
16993 commentList[firstMove+i] = NULL;
16997 forwardMostMove = firstMove; // truncate game so we can start variation
17001 PushTail (int firstMove, int lastMove)
17003 if(appData.icsActive) { // only in local mode
17004 forwardMostMove = currentMove; // mimic old ICS behavior
17007 if(storedGames >= MAX_VARIATIONS-2) return; // leave one for PV-walk
17009 PushInner(firstMove, lastMove);
17010 if(storedGames == 1) GreyRevert(FALSE);
17011 if(gameMode == PlayFromGameFile) gameMode = EditGame, modeRestore = TRUE;
17015 PopInner (Boolean annotate)
17018 char buf[8000], moveBuf[20];
17020 ToNrEvent(savedFirst[storedGames-1]); // sets currentMove
17021 storedGames--; // do this after ToNrEvent, to make sure HistorySet will refresh entire game after PopInner returns
17022 nrMoves = savedLast[storedGames] - currentMove;
17025 if(!WhiteOnMove(currentMove))
17026 snprintf(buf, sizeof(buf)/sizeof(buf[0]),"(%d...", (currentMove+2)>>1);
17027 else safeStrCpy(buf, "(", sizeof(buf)/sizeof(buf[0]));
17028 for(i=currentMove; i<forwardMostMove; i++) {
17030 snprintf(moveBuf, sizeof(moveBuf)/sizeof(moveBuf[0]), " %d. %s", (i+2)>>1, SavePart(parseList[i]));
17031 else snprintf(moveBuf, sizeof(moveBuf)/sizeof(moveBuf[0])," %s", SavePart(parseList[i]));
17032 strcat(buf, moveBuf);
17033 if(commentList[i]) { strcat(buf, " "); strcat(buf, commentList[i]); }
17034 if(!--cnt) { strcat(buf, "\n"); cnt = 10; }
17038 for(i=1; i<=nrMoves; i++) { // copy last variation back
17039 CopyBoard(boards[currentMove+i], boards[framePtr+i]);
17040 for(j=0; j<MOVE_LEN; j++)
17041 moveList[currentMove+i-1][j] = moveList[framePtr+i][j];
17042 for(j=0; j<2*MOVE_LEN; j++)
17043 parseList[currentMove+i-1][j] = parseList[framePtr+i][j];
17044 timeRemaining[0][currentMove+i] = timeRemaining[0][framePtr+i];
17045 timeRemaining[1][currentMove+i] = timeRemaining[1][framePtr+i];
17046 pvInfoList[currentMove+i-1] = pvInfoList[framePtr+i];
17047 if(commentList[currentMove+i]) free(commentList[currentMove+i]);
17048 commentList[currentMove+i] = commentList[framePtr+i];
17049 commentList[framePtr+i] = NULL;
17051 if(annotate) AppendComment(currentMove+1, buf, FALSE);
17052 framePtr = savedFramePtr[storedGames];
17053 gameInfo.result = savedResult[storedGames];
17054 if(gameInfo.resultDetails != NULL) {
17055 free(gameInfo.resultDetails);
17057 gameInfo.resultDetails = savedDetails[storedGames];
17058 forwardMostMove = currentMove + nrMoves;
17062 PopTail (Boolean annotate)
17064 if(appData.icsActive) return FALSE; // only in local mode
17065 if(!storedGames) return FALSE; // sanity
17066 CommentPopDown(); // make sure no stale variation comments to the destroyed line can remain open
17068 PopInner(annotate);
17069 if(currentMove < forwardMostMove) ForwardEvent(); else
17070 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
17072 if(storedGames == 0) { GreyRevert(TRUE); if(modeRestore) modeRestore = FALSE, gameMode = PlayFromGameFile; }
17078 { // remove all shelved variations
17080 for(i=0; i<storedGames; i++) {
17081 if(savedDetails[i])
17082 free(savedDetails[i]);
17083 savedDetails[i] = NULL;
17085 for(i=framePtr; i<MAX_MOVES; i++) {
17086 if(commentList[i]) free(commentList[i]);
17087 commentList[i] = NULL;
17089 framePtr = MAX_MOVES-1;
17094 LoadVariation (int index, char *text)
17095 { // [HGM] vari: shelve previous line and load new variation, parsed from text around text[index]
17096 char *p = text, *start = NULL, *end = NULL, wait = NULLCHAR;
17097 int level = 0, move;
17099 if(gameMode != EditGame && gameMode != AnalyzeMode && gameMode != PlayFromGameFile) return;
17100 // first find outermost bracketing variation
17101 while(*p) { // hope I got this right... Non-nesting {} and [] can screen each other and nesting ()
17102 if(!wait) { // while inside [] pr {}, ignore everyting except matching closing ]}
17103 if(*p == '{') wait = '}'; else
17104 if(*p == '[') wait = ']'; else
17105 if(*p == '(' && level++ == 0 && p-text < index) start = p+1;
17106 if(*p == ')' && level > 0 && --level == 0 && p-text > index && end == NULL) end = p-1;
17108 if(*p == wait) wait = NULLCHAR; // closing ]} found
17111 if(!start || !end) return; // no variation found, or syntax error in PGN: ignore click
17112 if(appData.debugMode) fprintf(debugFP, "at move %d load variation '%s'\n", currentMove, start);
17113 end[1] = NULLCHAR; // clip off comment beyond variation
17114 ToNrEvent(currentMove-1);
17115 PushTail(currentMove, forwardMostMove); // shelve main variation. This truncates game
17116 // kludge: use ParsePV() to append variation to game
17117 move = currentMove;
17118 ParsePV(start, TRUE, TRUE);
17119 forwardMostMove = endPV; endPV = -1; currentMove = move; // cleanup what ParsePV did
17120 ClearPremoveHighlights();
17122 ToNrEvent(currentMove+1);