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 SendToICS P((char *s));
156 void SendToICSDelayed P((char *s, long msdelay));
157 void SendMoveToICS P((ChessMove moveType, int fromX, int fromY, int toX, int toY, char promoChar));
158 void HandleMachineMove P((char *message, ChessProgramState *cps));
159 int AutoPlayOneMove P((void));
160 int LoadGameOneMove P((ChessMove readAhead));
161 int LoadGameFromFile P((char *filename, int n, char *title, int useList));
162 int LoadPositionFromFile P((char *filename, int n, char *title));
163 int SavePositionToFile P((char *filename));
164 void MakeMove P((int fromX, int fromY, int toX, int toY, int promoChar));
165 void ShowMove P((int fromX, int fromY, int toX, int toY));
166 int FinishMove P((ChessMove moveType, int fromX, int fromY, int toX, int toY,
167 /*char*/int promoChar));
168 void BackwardInner P((int target));
169 void ForwardInner P((int target));
170 int Adjudicate P((ChessProgramState *cps));
171 void GameEnds P((ChessMove result, char *resultDetails, int whosays));
172 void EditPositionDone P((Boolean fakeRights));
173 void PrintOpponents P((FILE *fp));
174 void PrintPosition P((FILE *fp, int move));
175 void StartChessProgram P((ChessProgramState *cps));
176 void SendToProgram P((char *message, ChessProgramState *cps));
177 void SendMoveToProgram P((int moveNum, ChessProgramState *cps));
178 void ReceiveFromProgram P((InputSourceRef isr, VOIDSTAR closure,
179 char *buf, int count, int error));
180 void SendTimeControl P((ChessProgramState *cps,
181 int mps, long tc, int inc, int sd, int st));
182 char *TimeControlTagValue P((void));
183 void Attention P((ChessProgramState *cps));
184 void FeedMovesToProgram P((ChessProgramState *cps, int upto));
185 int ResurrectChessProgram P((void));
186 void DisplayComment P((int moveNumber, char *text));
187 void DisplayMove P((int moveNumber));
189 void ParseGameHistory P((char *game));
190 void ParseBoard12 P((char *string));
191 void KeepAlive P((void));
192 void StartClocks P((void));
193 void SwitchClocks P((int nr));
194 void StopClocks P((void));
195 void ResetClocks P((void));
196 char *PGNDate P((void));
197 void SetGameInfo P((void));
198 int RegisterMove P((void));
199 void MakeRegisteredMove P((void));
200 void TruncateGame P((void));
201 int looking_at P((char *, int *, char *));
202 void CopyPlayerNameIntoFileName P((char **, char *));
203 char *SavePart P((char *));
204 int SaveGameOldStyle P((FILE *));
205 int SaveGamePGN P((FILE *));
206 int CheckFlags P((void));
207 long NextTickLength P((long));
208 void CheckTimeControl P((void));
209 void show_bytes P((FILE *, char *, int));
210 int string_to_rating P((char *str));
211 void ParseFeatures P((char* args, ChessProgramState *cps));
212 void InitBackEnd3 P((void));
213 void FeatureDone P((ChessProgramState* cps, int val));
214 void InitChessProgram P((ChessProgramState *cps, int setup));
215 void OutputKibitz(int window, char *text);
216 int PerpetualChase(int first, int last);
217 int EngineOutputIsUp();
218 void InitDrawingSizes(int x, int y);
219 void NextMatchGame P((void));
220 int NextTourneyGame P((int nr, int *swap));
221 int Pairing P((int nr, int nPlayers, int *w, int *b, int *sync));
222 FILE *WriteTourneyFile P((char *results, FILE *f));
223 void DisplayTwoMachinesTitle P(());
224 static void ExcludeClick P((int index));
225 void ToggleSecond P((void));
228 extern void ConsoleCreate();
231 ChessProgramState *WhitePlayer();
232 void InsertIntoMemo P((int which, char *text)); // [HGM] kibitz: in engineo.c
233 int VerifyDisplayMode P(());
235 char *GetInfoFromComment( int, char * ); // [HGM] PV time: returns stripped comment
236 void InitEngineUCI( const char * iniDir, ChessProgramState * cps ); // [HGM] moved here from winboard.c
237 char *ProbeBook P((int moveNr, char *book)); // [HGM] book: returns a book move
238 char *SendMoveToBookUser P((int nr, ChessProgramState *cps, int initial)); // [HGM] book
239 void ics_update_width P((int new_width));
240 extern char installDir[MSG_SIZ];
241 VariantClass startVariant; /* [HGM] nicks: initial variant */
244 extern int tinyLayout, smallLayout;
245 ChessProgramStats programStats;
246 char lastPV[2][2*MSG_SIZ]; /* [HGM] pv: last PV in thinking output of each engine */
248 static int exiting = 0; /* [HGM] moved to top */
249 static int setboardSpoiledMachineBlack = 0 /*, errorExitFlag = 0*/;
250 int startedFromPositionFile = FALSE; Board filePosition; /* [HGM] loadPos */
251 Board partnerBoard; /* [HGM] bughouse: for peeking at partner game */
252 int partnerHighlight[2];
253 Boolean partnerBoardValid = 0;
254 char partnerStatus[MSG_SIZ];
256 Boolean originalFlip;
257 Boolean twoBoards = 0;
258 char endingGame = 0; /* [HGM] crash: flag to prevent recursion of GameEnds() */
259 int whiteNPS, blackNPS; /* [HGM] nps: for easily making clocks aware of NPS */
260 VariantClass currentlyInitializedVariant; /* [HGM] variantswitch */
261 int lastIndex = 0; /* [HGM] autoinc: last game/position used in match mode */
262 Boolean connectionAlive;/* [HGM] alive: ICS connection status from probing */
263 int opponentKibitzes;
264 int lastSavedGame; /* [HGM] save: ID of game */
265 char chatPartner[MAX_CHAT][MSG_SIZ]; /* [HGM] chat: list of chatting partners */
266 extern int chatCount;
268 char marker[BOARD_RANKS][BOARD_FILES]; /* [HGM] marks for target squares */
269 char lastMsg[MSG_SIZ];
270 ChessSquare pieceSweep = EmptySquare;
271 ChessSquare promoSweep = EmptySquare, defaultPromoChoice;
272 int promoDefaultAltered;
273 int keepInfo = 0; /* [HGM] to protect PGN tags in auto-step game analysis */
275 /* States for ics_getting_history */
277 #define H_REQUESTED 1
278 #define H_GOT_REQ_HEADER 2
279 #define H_GOT_UNREQ_HEADER 3
280 #define H_GETTING_MOVES 4
281 #define H_GOT_UNWANTED_HEADER 5
283 /* whosays values for GameEnds */
292 /* Maximum number of games in a cmail message */
293 #define CMAIL_MAX_GAMES 20
295 /* Different types of move when calling RegisterMove */
297 #define CMAIL_RESIGN 1
299 #define CMAIL_ACCEPT 3
301 /* Different types of result to remember for each game */
302 #define CMAIL_NOT_RESULT 0
303 #define CMAIL_OLD_RESULT 1
304 #define CMAIL_NEW_RESULT 2
306 /* Telnet protocol constants */
317 safeStrCpy (char *dst, const char *src, size_t count)
320 assert( dst != NULL );
321 assert( src != NULL );
324 for(i=0; i<count; i++) if((dst[i] = src[i]) == NULLCHAR) break;
325 if( i == count && dst[count-1] != NULLCHAR)
327 dst[ count-1 ] = '\0'; // make sure incomplete copy still null-terminated
328 if(appData.debugMode)
329 fprintf(debugFP, "safeStrCpy: copying %s into %s didn't work, not enough space %d\n",src,dst, (int)count);
335 /* Some compiler can't cast u64 to double
336 * This function do the job for us:
338 * We use the highest bit for cast, this only
339 * works if the highest bit is not
340 * in use (This should not happen)
342 * We used this for all compiler
345 u64ToDouble (u64 value)
348 u64 tmp = value & u64Const(0x7fffffffffffffff);
349 r = (double)(s64)tmp;
350 if (value & u64Const(0x8000000000000000))
351 r += 9.2233720368547758080e18; /* 2^63 */
355 /* Fake up flags for now, as we aren't keeping track of castling
356 availability yet. [HGM] Change of logic: the flag now only
357 indicates the type of castlings allowed by the rule of the game.
358 The actual rights themselves are maintained in the array
359 castlingRights, as part of the game history, and are not probed
365 int flags = F_ALL_CASTLE_OK;
366 if ((index % 2) == 0) flags |= F_WHITE_ON_MOVE;
367 switch (gameInfo.variant) {
369 flags &= ~F_ALL_CASTLE_OK;
370 case VariantGiveaway: // [HGM] moved this case label one down: seems Giveaway does have castling on ICC!
371 flags |= F_IGNORE_CHECK;
373 flags |= F_MANDATORY_CAPTURE; //[HGM] losers: sets flag so TestLegality rejects non-capts if capts exist
376 flags |= F_IGNORE_CHECK | F_ATOMIC_CAPTURE;
378 case VariantKriegspiel:
379 flags |= F_KRIEGSPIEL_CAPTURE;
381 case VariantCapaRandom:
382 case VariantFischeRandom:
383 flags |= F_FRC_TYPE_CASTLING; /* [HGM] enable this through flag */
384 case VariantNoCastle:
385 case VariantShatranj:
389 flags &= ~F_ALL_CASTLE_OK;
397 FILE *gameFileFP, *debugFP, *serverFP;
398 char *currentDebugFile; // [HGM] debug split: to remember name
401 [AS] Note: sometimes, the sscanf() function is used to parse the input
402 into a fixed-size buffer. Because of this, we must be prepared to
403 receive strings as long as the size of the input buffer, which is currently
404 set to 4K for Windows and 8K for the rest.
405 So, we must either allocate sufficiently large buffers here, or
406 reduce the size of the input buffer in the input reading part.
409 char cmailMove[CMAIL_MAX_GAMES][MOVE_LEN], cmailMsg[MSG_SIZ];
410 char bookOutput[MSG_SIZ*10], thinkOutput[MSG_SIZ*10], lastHint[MSG_SIZ];
411 char thinkOutput1[MSG_SIZ*10];
413 ChessProgramState first, second, pairing;
415 /* premove variables */
418 int premoveFromX = 0;
419 int premoveFromY = 0;
420 int premovePromoChar = 0;
422 Boolean alarmSounded;
423 /* end premove variables */
425 char *ics_prefix = "$";
426 enum ICS_TYPE ics_type = ICS_GENERIC;
428 int currentMove = 0, forwardMostMove = 0, backwardMostMove = 0;
429 int pauseExamForwardMostMove = 0;
430 int nCmailGames = 0, nCmailResults = 0, nCmailMovesRegistered = 0;
431 int cmailMoveRegistered[CMAIL_MAX_GAMES], cmailResult[CMAIL_MAX_GAMES];
432 int cmailMsgLoaded = FALSE, cmailMailedMove = FALSE;
433 int cmailOldMove = -1, firstMove = TRUE, flipView = FALSE;
434 int blackPlaysFirst = FALSE, startedFromSetupPosition = FALSE;
435 int searchTime = 0, pausing = FALSE, pauseExamInvalid = FALSE;
436 int whiteFlag = FALSE, blackFlag = FALSE;
437 int userOfferedDraw = FALSE;
438 int ics_user_moved = 0, ics_gamenum = -1, ics_getting_history = H_FALSE;
439 int matchMode = FALSE, hintRequested = FALSE, bookRequested = FALSE;
440 int cmailMoveType[CMAIL_MAX_GAMES];
441 long ics_clock_paused = 0;
442 ProcRef icsPR = NoProc, cmailPR = NoProc;
443 InputSourceRef telnetISR = NULL, fromUserISR = NULL, cmailISR = NULL;
444 GameMode gameMode = BeginningOfGame;
445 char moveList[MAX_MOVES][MOVE_LEN], parseList[MAX_MOVES][MOVE_LEN * 2];
446 char *commentList[MAX_MOVES], *cmailCommentList[CMAIL_MAX_GAMES];
447 ChessProgramStats_Move pvInfoList[MAX_MOVES]; /* [AS] Info about engine thinking */
448 int hiddenThinkOutputState = 0; /* [AS] */
449 int adjudicateLossThreshold = 0; /* [AS] Automatic adjudication */
450 int adjudicateLossPlies = 6;
451 char white_holding[64], black_holding[64];
452 TimeMark lastNodeCountTime;
453 long lastNodeCount=0;
454 int shiftKey, controlKey; // [HGM] set by mouse handler
456 int have_sent_ICS_logon = 0;
458 int suddenDeath, whiteStartMove, blackStartMove; /* [HGM] for implementation of 'any per time' sessions, as in first part of byoyomi TC */
459 long whiteTimeRemaining, blackTimeRemaining, timeControl, timeIncrement, lastWhite, lastBlack, activePartnerTime;
460 Boolean adjustedClock;
461 long timeControl_2; /* [AS] Allow separate time controls */
462 char *fullTimeControlString = NULL, *nextSession, *whiteTC, *blackTC, activePartner; /* [HGM] secondary TC: merge of MPS, TC and inc */
463 long timeRemaining[2][MAX_MOVES];
464 int matchGame = 0, nextGame = 0, roundNr = 0;
465 Boolean waitingForGame = FALSE;
466 TimeMark programStartTime, pauseStart;
467 char ics_handle[MSG_SIZ];
468 int have_set_title = 0;
470 /* animateTraining preserves the state of appData.animate
471 * when Training mode is activated. This allows the
472 * response to be animated when appData.animate == TRUE and
473 * appData.animateDragging == TRUE.
475 Boolean animateTraining;
481 Board boards[MAX_MOVES];
482 /* [HGM] Following 7 needed for accurate legality tests: */
483 signed char castlingRank[BOARD_FILES]; // and corresponding ranks
484 signed char initialRights[BOARD_FILES];
485 int nrCastlingRights; // For TwoKings, or to implement castling-unknown status
486 int initialRulePlies, FENrulePlies;
487 FILE *serverMoves = NULL; // next two for broadcasting (/serverMoves option)
489 Boolean shuffleOpenings;
490 int mute; // mute all sounds
492 // [HGM] vari: next 12 to save and restore variations
493 #define MAX_VARIATIONS 10
494 int framePtr = MAX_MOVES-1; // points to free stack entry
496 int savedFirst[MAX_VARIATIONS];
497 int savedLast[MAX_VARIATIONS];
498 int savedFramePtr[MAX_VARIATIONS];
499 char *savedDetails[MAX_VARIATIONS];
500 ChessMove savedResult[MAX_VARIATIONS];
502 void PushTail P((int firstMove, int lastMove));
503 Boolean PopTail P((Boolean annotate));
504 void PushInner P((int firstMove, int lastMove));
505 void PopInner P((Boolean annotate));
506 void CleanupTail P((void));
508 ChessSquare FIDEArray[2][BOARD_FILES] = {
509 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
510 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
511 { BlackRook, BlackKnight, BlackBishop, BlackQueen,
512 BlackKing, BlackBishop, BlackKnight, BlackRook }
515 ChessSquare twoKingsArray[2][BOARD_FILES] = {
516 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
517 WhiteKing, WhiteKing, WhiteKnight, WhiteRook },
518 { BlackRook, BlackKnight, BlackBishop, BlackQueen,
519 BlackKing, BlackKing, BlackKnight, BlackRook }
522 ChessSquare KnightmateArray[2][BOARD_FILES] = {
523 { WhiteRook, WhiteMan, WhiteBishop, WhiteQueen,
524 WhiteUnicorn, WhiteBishop, WhiteMan, WhiteRook },
525 { BlackRook, BlackMan, BlackBishop, BlackQueen,
526 BlackUnicorn, BlackBishop, BlackMan, BlackRook }
529 ChessSquare SpartanArray[2][BOARD_FILES] = {
530 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
531 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
532 { BlackAlfil, BlackMarshall, BlackKing, BlackDragon,
533 BlackDragon, BlackKing, BlackAngel, BlackAlfil }
536 ChessSquare fairyArray[2][BOARD_FILES] = { /* [HGM] Queen side differs from King side */
537 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
538 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
539 { BlackCardinal, BlackAlfil, BlackMarshall, BlackAngel,
540 BlackKing, BlackMarshall, BlackAlfil, BlackCardinal }
543 ChessSquare ShatranjArray[2][BOARD_FILES] = { /* [HGM] (movGen knows about Shatranj Q and P) */
544 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteKing,
545 WhiteFerz, WhiteAlfil, WhiteKnight, WhiteRook },
546 { BlackRook, BlackKnight, BlackAlfil, BlackKing,
547 BlackFerz, BlackAlfil, BlackKnight, BlackRook }
550 ChessSquare makrukArray[2][BOARD_FILES] = { /* [HGM] (movGen knows about Shatranj Q and P) */
551 { WhiteRook, WhiteKnight, WhiteMan, WhiteKing,
552 WhiteFerz, WhiteMan, WhiteKnight, WhiteRook },
553 { BlackRook, BlackKnight, BlackMan, BlackFerz,
554 BlackKing, BlackMan, BlackKnight, BlackRook }
558 #if (BOARD_FILES>=10)
559 ChessSquare ShogiArray[2][BOARD_FILES] = {
560 { WhiteQueen, WhiteKnight, WhiteFerz, WhiteWazir,
561 WhiteKing, WhiteWazir, WhiteFerz, WhiteKnight, WhiteQueen },
562 { BlackQueen, BlackKnight, BlackFerz, BlackWazir,
563 BlackKing, BlackWazir, BlackFerz, BlackKnight, BlackQueen }
566 ChessSquare XiangqiArray[2][BOARD_FILES] = {
567 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteFerz,
568 WhiteWazir, WhiteFerz, WhiteAlfil, WhiteKnight, WhiteRook },
569 { BlackRook, BlackKnight, BlackAlfil, BlackFerz,
570 BlackWazir, BlackFerz, BlackAlfil, BlackKnight, BlackRook }
573 ChessSquare CapablancaArray[2][BOARD_FILES] = {
574 { WhiteRook, WhiteKnight, WhiteAngel, WhiteBishop, WhiteQueen,
575 WhiteKing, WhiteBishop, WhiteMarshall, WhiteKnight, WhiteRook },
576 { BlackRook, BlackKnight, BlackAngel, BlackBishop, BlackQueen,
577 BlackKing, BlackBishop, BlackMarshall, BlackKnight, BlackRook }
580 ChessSquare GreatArray[2][BOARD_FILES] = {
581 { WhiteDragon, WhiteKnight, WhiteAlfil, WhiteGrasshopper, WhiteKing,
582 WhiteSilver, WhiteCardinal, WhiteAlfil, WhiteKnight, WhiteDragon },
583 { BlackDragon, BlackKnight, BlackAlfil, BlackGrasshopper, BlackKing,
584 BlackSilver, BlackCardinal, BlackAlfil, BlackKnight, BlackDragon },
587 ChessSquare JanusArray[2][BOARD_FILES] = {
588 { WhiteRook, WhiteAngel, WhiteKnight, WhiteBishop, WhiteKing,
589 WhiteQueen, WhiteBishop, WhiteKnight, WhiteAngel, WhiteRook },
590 { BlackRook, BlackAngel, BlackKnight, BlackBishop, BlackKing,
591 BlackQueen, BlackBishop, BlackKnight, BlackAngel, BlackRook }
594 ChessSquare GrandArray[2][BOARD_FILES] = {
595 { EmptySquare, WhiteKnight, WhiteBishop, WhiteQueen, WhiteKing,
596 WhiteMarshall, WhiteAngel, WhiteBishop, WhiteKnight, EmptySquare },
597 { EmptySquare, BlackKnight, BlackBishop, BlackQueen, BlackKing,
598 BlackMarshall, BlackAngel, BlackBishop, BlackKnight, EmptySquare }
602 ChessSquare GothicArray[2][BOARD_FILES] = {
603 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen, WhiteMarshall,
604 WhiteKing, WhiteAngel, WhiteBishop, WhiteKnight, WhiteRook },
605 { BlackRook, BlackKnight, BlackBishop, BlackQueen, BlackMarshall,
606 BlackKing, BlackAngel, BlackBishop, BlackKnight, BlackRook }
609 #define GothicArray CapablancaArray
613 ChessSquare FalconArray[2][BOARD_FILES] = {
614 { WhiteRook, WhiteKnight, WhiteBishop, WhiteFalcon, WhiteQueen,
615 WhiteKing, WhiteFalcon, WhiteBishop, WhiteKnight, WhiteRook },
616 { BlackRook, BlackKnight, BlackBishop, BlackFalcon, BlackQueen,
617 BlackKing, BlackFalcon, BlackBishop, BlackKnight, BlackRook }
620 #define FalconArray CapablancaArray
623 #else // !(BOARD_FILES>=10)
624 #define XiangqiPosition FIDEArray
625 #define CapablancaArray FIDEArray
626 #define GothicArray FIDEArray
627 #define GreatArray FIDEArray
628 #endif // !(BOARD_FILES>=10)
630 #if (BOARD_FILES>=12)
631 ChessSquare CourierArray[2][BOARD_FILES] = {
632 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteBishop, WhiteMan, WhiteKing,
633 WhiteFerz, WhiteWazir, WhiteBishop, WhiteAlfil, WhiteKnight, WhiteRook },
634 { BlackRook, BlackKnight, BlackAlfil, BlackBishop, BlackMan, BlackKing,
635 BlackFerz, BlackWazir, BlackBishop, BlackAlfil, BlackKnight, BlackRook }
637 #else // !(BOARD_FILES>=12)
638 #define CourierArray CapablancaArray
639 #endif // !(BOARD_FILES>=12)
642 Board initialPosition;
645 /* Convert str to a rating. Checks for special cases of "----",
647 "++++", etc. Also strips ()'s */
649 string_to_rating (char *str)
651 while(*str && !isdigit(*str)) ++str;
653 return 0; /* One of the special "no rating" cases */
661 /* Init programStats */
662 programStats.movelist[0] = 0;
663 programStats.depth = 0;
664 programStats.nr_moves = 0;
665 programStats.moves_left = 0;
666 programStats.nodes = 0;
667 programStats.time = -1; // [HGM] PGNtime: make invalid to recognize engine output
668 programStats.score = 0;
669 programStats.got_only_move = 0;
670 programStats.got_fail = 0;
671 programStats.line_is_book = 0;
676 { // [HGM] moved some code here from InitBackend1 that has to be done after both engines have contributed their settings
677 if (appData.firstPlaysBlack) {
678 first.twoMachinesColor = "black\n";
679 second.twoMachinesColor = "white\n";
681 first.twoMachinesColor = "white\n";
682 second.twoMachinesColor = "black\n";
685 first.other = &second;
686 second.other = &first;
689 if(appData.timeOddsMode) {
690 norm = appData.timeOdds[0];
691 if(norm > appData.timeOdds[1]) norm = appData.timeOdds[1];
693 first.timeOdds = appData.timeOdds[0]/norm;
694 second.timeOdds = appData.timeOdds[1]/norm;
697 if(programVersion) free(programVersion);
698 if (appData.noChessProgram) {
699 programVersion = (char*) malloc(5 + strlen(PACKAGE_STRING));
700 sprintf(programVersion, "%s", PACKAGE_STRING);
702 /* [HGM] tidy: use tidy name, in stead of full pathname (which was probably a bug due to / vs \ ) */
703 programVersion = (char*) malloc(8 + strlen(PACKAGE_STRING) + strlen(first.tidy));
704 sprintf(programVersion, "%s + %s", PACKAGE_STRING, first.tidy);
709 UnloadEngine (ChessProgramState *cps)
711 /* Kill off first chess program */
712 if (cps->isr != NULL)
713 RemoveInputSource(cps->isr);
716 if (cps->pr != NoProc) {
718 DoSleep( appData.delayBeforeQuit );
719 SendToProgram("quit\n", cps);
720 DoSleep( appData.delayAfterQuit );
721 DestroyChildProcess(cps->pr, cps->useSigterm);
724 if(appData.debugMode) fprintf(debugFP, "Unload %s\n", cps->which);
728 ClearOptions (ChessProgramState *cps)
731 cps->nrOptions = cps->comboCnt = 0;
732 for(i=0; i<MAX_OPTIONS; i++) {
733 cps->option[i].min = cps->option[i].max = cps->option[i].value = 0;
734 cps->option[i].textValue = 0;
738 char *engineNames[] = {
739 /* TRANSLATORS: "first" is the first of possible two chess engines. It is inserted into strings
740 such as "%s engine" / "%s chess program" / "%s machine" - all meaning the same thing */
742 /* TRANSLATORS: "second" is the second of possible two chess engines. It is inserted into strings
743 such as "%s engine" / "%s chess program" / "%s machine" - all meaning the same thing */
748 InitEngine (ChessProgramState *cps, int n)
749 { // [HGM] all engine initialiation put in a function that does one engine
753 cps->which = engineNames[n];
754 cps->maybeThinking = FALSE;
758 cps->sendDrawOffers = 1;
760 cps->program = appData.chessProgram[n];
761 cps->host = appData.host[n];
762 cps->dir = appData.directory[n];
763 cps->initString = appData.engInitString[n];
764 cps->computerString = appData.computerString[n];
765 cps->useSigint = TRUE;
766 cps->useSigterm = TRUE;
767 cps->reuse = appData.reuse[n];
768 cps->nps = appData.NPS[n]; // [HGM] nps: copy nodes per second
769 cps->useSetboard = FALSE;
771 cps->usePing = FALSE;
774 cps->usePlayother = FALSE;
775 cps->useColors = TRUE;
776 cps->useUsermove = FALSE;
777 cps->sendICS = FALSE;
778 cps->sendName = appData.icsActive;
779 cps->sdKludge = FALSE;
780 cps->stKludge = FALSE;
781 TidyProgramName(cps->program, cps->host, cps->tidy);
783 safeStrCpy(cps->variants, appData.variant, MSG_SIZ);
784 cps->analysisSupport = 2; /* detect */
785 cps->analyzing = FALSE;
786 cps->initDone = FALSE;
788 /* New features added by Tord: */
789 cps->useFEN960 = FALSE;
790 cps->useOOCastle = TRUE;
791 /* End of new features added by Tord. */
792 cps->fenOverride = appData.fenOverride[n];
794 /* [HGM] time odds: set factor for each machine */
795 cps->timeOdds = appData.timeOdds[n];
797 /* [HGM] secondary TC: how to handle sessions that do not fit in 'level'*/
798 cps->accumulateTC = appData.accumulateTC[n];
799 cps->maxNrOfSessions = 1;
804 cps->supportsNPS = UNKNOWN;
805 cps->memSize = FALSE;
806 cps->maxCores = FALSE;
807 cps->egtFormats[0] = NULLCHAR;
810 cps->optionSettings = appData.engOptions[n];
812 cps->scoreIsAbsolute = appData.scoreIsAbsolute[n]; /* [AS] */
813 cps->isUCI = appData.isUCI[n]; /* [AS] */
814 cps->hasOwnBookUCI = appData.hasOwnBookUCI[n]; /* [AS] */
816 if (appData.protocolVersion[n] > PROTOVER
817 || appData.protocolVersion[n] < 1)
822 len = snprintf(buf, MSG_SIZ, _("protocol version %d not supported"),
823 appData.protocolVersion[n]);
824 if( (len >= MSG_SIZ) && appData.debugMode )
825 fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
827 DisplayFatalError(buf, 0, 2);
831 cps->protocolVersion = appData.protocolVersion[n];
834 InitEngineUCI( installDir, cps ); // [HGM] moved here from winboard.c, to make available in xboard
835 ParseFeatures(appData.featureDefaults, cps);
838 ChessProgramState *savCps;
844 if(WaitForEngine(savCps, LoadEngine)) return;
845 CommonEngineInit(); // recalculate time odds
846 if(gameInfo.variant != StringToVariant(appData.variant)) {
847 // we changed variant when loading the engine; this forces us to reset
848 Reset(TRUE, savCps != &first);
849 EditGameEvent(); // for consistency with other path, as Reset changes mode
851 InitChessProgram(savCps, FALSE);
852 SendToProgram("force\n", savCps);
853 DisplayMessage("", "");
854 if (startedFromSetupPosition) SendBoard(savCps, backwardMostMove);
855 for (i = backwardMostMove; i < currentMove; i++) SendMoveToProgram(i, savCps);
861 ReplaceEngine (ChessProgramState *cps, int n)
865 appData.noChessProgram = FALSE;
866 appData.clockMode = TRUE;
869 if(n) return; // only startup first engine immediately; second can wait
870 savCps = cps; // parameter to LoadEngine passed as globals, to allow scheduled calling :-(
874 extern char *engineName, *engineDir, *engineChoice, *engineLine, *nickName, *params;
875 extern Boolean isUCI, hasBook, storeVariant, v1, addToList, useNick;
877 static char resetOptions[] =
878 "-reuse -firstIsUCI false -firstHasOwnBookUCI true -firstTimeOdds 1 "
879 "-firstInitString \"" INIT_STRING "\" -firstComputerString \"" COMPUTER_STRING "\" "
880 "-firstFeatures \"\" -firstLogo \"\" -firstAccumulateTC 1 "
881 "-firstOptions \"\" -firstNPS -1 -fn \"\" -firstScoreAbs false";
884 FloatToFront(char **list, char *engineLine)
886 char buf[MSG_SIZ], tidy[MSG_SIZ], *p = buf, *q, *r = buf;
888 if(appData.recentEngines <= 0) return;
889 TidyProgramName(engineLine, "localhost", tidy+1);
890 tidy[0] = buf[0] = '\n'; strcat(tidy, "\n");
891 strncpy(buf+1, *list, MSG_SIZ-50);
892 if(p = strstr(buf, tidy)) { // tidy name appears in list
893 q = strchr(++p, '\n'); if(q == NULL) return; // malformed, don't touch
894 while(*p++ = *++q); // squeeze out
896 strcat(tidy, buf+1); // put list behind tidy name
897 p = tidy + 1; while(q = strchr(p, '\n')) i++, r = p, p = q + 1; // count entries in new list
898 if(i > appData.recentEngines) *r = NULLCHAR; // if maximum rached, strip off last
899 ASSIGN(*list, tidy+1);
902 char *insert, *wbOptions; // point in ChessProgramNames were we should insert new engine
905 Load (ChessProgramState *cps, int i)
907 char *p, *q, buf[MSG_SIZ], command[MSG_SIZ], buf2[MSG_SIZ];
908 if(engineLine && engineLine[0]) { // an engine was selected from the combo box
909 snprintf(buf, MSG_SIZ, "-fcp %s", engineLine);
910 SwapEngines(i); // kludge to parse -f* / -first* like it is -s* / -second*
911 ParseArgsFromString(resetOptions); appData.pvSAN[0] = FALSE;
912 FREE(appData.fenOverride[0]); appData.fenOverride[0] = NULL;
913 appData.firstProtocolVersion = PROTOVER;
914 ParseArgsFromString(buf);
916 ReplaceEngine(cps, i);
917 FloatToFront(&appData.recentEngineList, engineLine);
921 while(q = strchr(p, SLASH)) p = q+1;
922 if(*p== NULLCHAR) { DisplayError(_("You did not specify the engine executable"), 0); return; }
923 if(engineDir[0] != NULLCHAR) {
924 ASSIGN(appData.directory[i], engineDir); p = engineName;
925 } else if(p != engineName) { // derive directory from engine path, when not given
927 ASSIGN(appData.directory[i], engineName);
929 if(SLASH == '/' && p - engineName > 1) *(p -= 2) = '.'; // for XBoard use ./exeName as command after split!
930 } else { ASSIGN(appData.directory[i], "."); }
932 if(strchr(p, ' ') && !strchr(p, '"')) snprintf(buf2, MSG_SIZ, "\"%s\"", p), p = buf2; // quote if it contains spaces
933 snprintf(command, MSG_SIZ, "%s %s", p, params);
936 ASSIGN(appData.chessProgram[i], p);
937 appData.isUCI[i] = isUCI;
938 appData.protocolVersion[i] = v1 ? 1 : PROTOVER;
939 appData.hasOwnBookUCI[i] = hasBook;
940 if(!nickName[0]) useNick = FALSE;
941 if(useNick) ASSIGN(appData.pgnName[i], nickName);
945 q = firstChessProgramNames;
946 if(nickName[0]) snprintf(buf, MSG_SIZ, "\"%s\" -fcp ", nickName); else buf[0] = NULLCHAR;
947 quote = strchr(p, '"') ? '\'' : '"'; // use single quotes around engine command if it contains double quotes
948 snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), "%c%s%c -fd \"%s\"%s%s%s%s%s%s%s%s\n",
949 quote, p, quote, appData.directory[i],
950 useNick ? " -fn \"" : "",
951 useNick ? nickName : "",
953 v1 ? " -firstProtocolVersion 1" : "",
954 hasBook ? "" : " -fNoOwnBookUCI",
955 isUCI ? (isUCI == TRUE ? " -fUCI" : gameInfo.variant == VariantShogi ? " -fUSI" : " -fUCCI") : "",
956 storeVariant ? " -variant " : "",
957 storeVariant ? VariantName(gameInfo.variant) : "");
958 if(wbOptions && wbOptions[0]) snprintf(buf+strlen(buf)-1, MSG_SIZ-strlen(buf), " %s\n", wbOptions);
959 firstChessProgramNames = malloc(len = strlen(q) + strlen(buf) + 1);
960 if(insert != q) insert[-1] = NULLCHAR;
961 snprintf(firstChessProgramNames, len, "%s\n%s%s", q, buf, insert);
963 FloatToFront(&appData.recentEngineList, buf);
965 ReplaceEngine(cps, i);
971 int matched, min, sec;
973 * Parse timeControl resource
975 if (!ParseTimeControl(appData.timeControl, appData.timeIncrement,
976 appData.movesPerSession)) {
978 snprintf(buf, sizeof(buf), _("bad timeControl option %s"), appData.timeControl);
979 DisplayFatalError(buf, 0, 2);
983 * Parse searchTime resource
985 if (*appData.searchTime != NULLCHAR) {
986 matched = sscanf(appData.searchTime, "%d:%d", &min, &sec);
988 searchTime = min * 60;
989 } else if (matched == 2) {
990 searchTime = min * 60 + sec;
993 snprintf(buf, sizeof(buf), _("bad searchTime option %s"), appData.searchTime);
994 DisplayFatalError(buf, 0, 2);
1003 ShowThinkingEvent(); // [HGM] thinking: make sure post/nopost state is set according to options
1004 startVariant = StringToVariant(appData.variant); // [HGM] nicks: remember original variant
1006 GetTimeMark(&programStartTime);
1007 srandom((programStartTime.ms + 1000*programStartTime.sec)*0x1001001); // [HGM] book: makes sure random is unpredictabe to msec level
1008 appData.seedBase = random() + (random()<<15);
1009 pauseStart = programStartTime; pauseStart.sec -= 100; // [HGM] matchpause: fake a pause that has long since ended
1011 ClearProgramStats();
1012 programStats.ok_to_send = 1;
1013 programStats.seen_stat = 0;
1016 * Initialize game list
1022 * Internet chess server status
1024 if (appData.icsActive) {
1025 appData.matchMode = FALSE;
1026 appData.matchGames = 0;
1028 appData.noChessProgram = !appData.zippyPlay;
1030 appData.zippyPlay = FALSE;
1031 appData.zippyTalk = FALSE;
1032 appData.noChessProgram = TRUE;
1034 if (*appData.icsHelper != NULLCHAR) {
1035 appData.useTelnet = TRUE;
1036 appData.telnetProgram = appData.icsHelper;
1039 appData.zippyTalk = appData.zippyPlay = FALSE;
1042 /* [AS] Initialize pv info list [HGM] and game state */
1046 for( i=0; i<=framePtr; i++ ) {
1047 pvInfoList[i].depth = -1;
1048 boards[i][EP_STATUS] = EP_NONE;
1049 for( j=0; j<BOARD_FILES-2; j++ ) boards[i][CASTLING][j] = NoRights;
1055 /* [AS] Adjudication threshold */
1056 adjudicateLossThreshold = appData.adjudicateLossThreshold;
1058 InitEngine(&first, 0);
1059 InitEngine(&second, 1);
1062 pairing.which = "pairing"; // pairing engine
1063 pairing.pr = NoProc;
1065 pairing.program = appData.pairingEngine;
1066 pairing.host = "localhost";
1069 if (appData.icsActive) {
1070 appData.clockMode = TRUE; /* changes dynamically in ICS mode */
1071 } else if (appData.noChessProgram) { // [HGM] st: searchTime mode now also is clockMode
1072 appData.clockMode = FALSE;
1073 first.sendTime = second.sendTime = 0;
1077 /* Override some settings from environment variables, for backward
1078 compatibility. Unfortunately it's not feasible to have the env
1079 vars just set defaults, at least in xboard. Ugh.
1081 if (appData.icsActive && (appData.zippyPlay || appData.zippyTalk)) {
1086 if (!appData.icsActive) {
1090 /* Check for variants that are supported only in ICS mode,
1091 or not at all. Some that are accepted here nevertheless
1092 have bugs; see comments below.
1094 VariantClass variant = StringToVariant(appData.variant);
1096 case VariantBughouse: /* need four players and two boards */
1097 case VariantKriegspiel: /* need to hide pieces and move details */
1098 /* case VariantFischeRandom: (Fabien: moved below) */
1099 len = snprintf(buf,MSG_SIZ, _("Variant %s supported only in ICS mode"), appData.variant);
1100 if( (len >= MSG_SIZ) && appData.debugMode )
1101 fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
1103 DisplayFatalError(buf, 0, 2);
1106 case VariantUnknown:
1107 case VariantLoadable:
1117 len = snprintf(buf, MSG_SIZ, _("Unknown variant name %s"), appData.variant);
1118 if( (len >= MSG_SIZ) && appData.debugMode )
1119 fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
1121 DisplayFatalError(buf, 0, 2);
1124 case VariantXiangqi: /* [HGM] repetition rules not implemented */
1125 case VariantFairy: /* [HGM] TestLegality definitely off! */
1126 case VariantGothic: /* [HGM] should work */
1127 case VariantCapablanca: /* [HGM] should work */
1128 case VariantCourier: /* [HGM] initial forced moves not implemented */
1129 case VariantShogi: /* [HGM] could still mate with pawn drop */
1130 case VariantKnightmate: /* [HGM] should work */
1131 case VariantCylinder: /* [HGM] untested */
1132 case VariantFalcon: /* [HGM] untested */
1133 case VariantCrazyhouse: /* holdings not shown, ([HGM] fixed that!)
1134 offboard interposition not understood */
1135 case VariantNormal: /* definitely works! */
1136 case VariantWildCastle: /* pieces not automatically shuffled */
1137 case VariantNoCastle: /* pieces not automatically shuffled */
1138 case VariantFischeRandom: /* [HGM] works and shuffles pieces */
1139 case VariantLosers: /* should work except for win condition,
1140 and doesn't know captures are mandatory */
1141 case VariantSuicide: /* should work except for win condition,
1142 and doesn't know captures are mandatory */
1143 case VariantGiveaway: /* should work except for win condition,
1144 and doesn't know captures are mandatory */
1145 case VariantTwoKings: /* should work */
1146 case VariantAtomic: /* should work except for win condition */
1147 case Variant3Check: /* should work except for win condition */
1148 case VariantShatranj: /* should work except for all win conditions */
1149 case VariantMakruk: /* should work except for draw countdown */
1150 case VariantBerolina: /* might work if TestLegality is off */
1151 case VariantCapaRandom: /* should work */
1152 case VariantJanus: /* should work */
1153 case VariantSuper: /* experimental */
1154 case VariantGreat: /* experimental, requires legality testing to be off */
1155 case VariantSChess: /* S-Chess, should work */
1156 case VariantGrand: /* should work */
1157 case VariantSpartan: /* should work */
1165 NextIntegerFromString (char ** str, long * value)
1170 while( *s == ' ' || *s == '\t' ) {
1176 if( *s >= '0' && *s <= '9' ) {
1177 while( *s >= '0' && *s <= '9' ) {
1178 *value = *value * 10 + (*s - '0');
1191 NextTimeControlFromString (char ** str, long * value)
1194 int result = NextIntegerFromString( str, &temp );
1197 *value = temp * 60; /* Minutes */
1198 if( **str == ':' ) {
1200 result = NextIntegerFromString( str, &temp );
1201 *value += temp; /* Seconds */
1209 NextSessionFromString (char ** str, int *moves, long * tc, long *inc, int *incType)
1210 { /* [HGM] routine added to read '+moves/time' for secondary time control. */
1211 int result = -1, type = 0; long temp, temp2;
1213 if(**str != ':') return -1; // old params remain in force!
1215 if(**str == '*') type = *(*str)++, temp = 0; // sandclock TC
1216 if( NextIntegerFromString( str, &temp ) ) return -1;
1217 if(type) { *moves = 0; *tc = temp * 500; *inc = temp * 1000; *incType = '*'; return 0; }
1220 /* time only: incremental or sudden-death time control */
1221 if(**str == '+') { /* increment follows; read it */
1223 if(**str == '!') type = *(*str)++; // Bronstein TC
1224 if(result = NextIntegerFromString( str, &temp2)) return -1;
1225 *inc = temp2 * 1000;
1226 if(**str == '.') { // read fraction of increment
1227 char *start = ++(*str);
1228 if(result = NextIntegerFromString( str, &temp2)) return -1;
1230 while(start++ < *str) temp2 /= 10;
1234 *moves = 0; *tc = temp * 1000; *incType = type;
1238 (*str)++; /* classical time control */
1239 result = NextIntegerFromString( str, &temp2); // NOTE: already converted to seconds by ParseTimeControl()
1251 GetTimeQuota (int movenr, int lastUsed, char *tcString)
1252 { /* [HGM] get time to add from the multi-session time-control string */
1253 int incType, moves=1; /* kludge to force reading of first session */
1254 long time, increment;
1257 if(!s || !*s) return 0; // empty TC string means we ran out of the last sudden-death version
1259 if(moves) NextSessionFromString(&s, &moves, &time, &increment, &incType);
1260 nextSession = s; suddenDeath = moves == 0 && increment == 0;
1261 if(movenr == -1) return time; /* last move before new session */
1262 if(incType == '*') increment = 0; else // for sandclock, time is added while not thinking
1263 if(incType == '!' && lastUsed < increment) increment = lastUsed;
1264 if(!moves) return increment; /* current session is incremental */
1265 if(movenr >= 0) movenr -= moves; /* we already finished this session */
1266 } while(movenr >= -1); /* try again for next session */
1268 return 0; // no new time quota on this move
1272 ParseTimeControl (char *tc, float ti, int mps)
1276 char buf[MSG_SIZ], buf2[MSG_SIZ], *mytc = tc;
1279 if(ti >= 0 && !strchr(tc, '+') && !strchr(tc, '/') ) mps = 0;
1280 if(!strchr(tc, '+') && !strchr(tc, '/') && sscanf(tc, "%d:%d", &min, &sec) >= 1)
1281 sprintf(mytc=buf2, "%d", 60*min+sec); // convert 'classical' min:sec tc string to seconds
1285 snprintf(buf, MSG_SIZ, ":%d/%s+%g", mps, mytc, ti);
1287 snprintf(buf, MSG_SIZ, ":%s+%g", mytc, ti);
1290 snprintf(buf, MSG_SIZ, ":%d/%s", mps, mytc);
1292 snprintf(buf, MSG_SIZ, ":%s", mytc);
1294 fullTimeControlString = StrSave(buf); // this should now be in PGN format
1296 if( NextTimeControlFromString( &tc, &tc1 ) != 0 ) {
1301 /* Parse second time control */
1304 if( NextTimeControlFromString( &tc, &tc2 ) != 0 ) {
1312 timeControl_2 = tc2 * 1000;
1322 timeControl = tc1 * 1000;
1325 timeIncrement = ti * 1000; /* convert to ms */
1326 movesPerSession = 0;
1329 movesPerSession = mps;
1337 if (appData.debugMode) {
1338 fprintf(debugFP, "%s\n", programVersion);
1340 ASSIGN(currentDebugFile, appData.nameOfDebugFile); // [HGM] debug split: remember initial name in use
1342 set_cont_sequence(appData.wrapContSeq);
1343 if (appData.matchGames > 0) {
1344 appData.matchMode = TRUE;
1345 } else if (appData.matchMode) {
1346 appData.matchGames = 1;
1348 if(appData.matchMode && appData.sameColorGames > 0) /* [HGM] alternate: overrule matchGames */
1349 appData.matchGames = appData.sameColorGames;
1350 if(appData.rewindIndex > 1) { /* [HGM] autoinc: rewind implies auto-increment and overrules given index */
1351 if(appData.loadPositionIndex >= 0) appData.loadPositionIndex = -1;
1352 if(appData.loadGameIndex >= 0) appData.loadGameIndex = -1;
1355 if (appData.noChessProgram || first.protocolVersion == 1) {
1358 /* kludge: allow timeout for initial "feature" commands */
1360 DisplayMessage("", _("Starting chess program"));
1361 ScheduleDelayedEvent(InitBackEnd3, FEATURE_TIMEOUT);
1366 CalculateIndex (int index, int gameNr)
1367 { // [HGM] autoinc: absolute way to determine load index from game number (taking auto-inc and rewind into account)
1369 if(index > 0) return index; // fixed nmber
1370 if(index == 0) return 1;
1371 res = (index == -1 ? gameNr : (gameNr-1)/2 + 1); // autoinc
1372 if(appData.rewindIndex > 0) res = (res-1) % appData.rewindIndex + 1; // rewind
1377 LoadGameOrPosition (int gameNr)
1378 { // [HGM] taken out of MatchEvent and NextMatchGame (to combine it)
1379 if (*appData.loadGameFile != NULLCHAR) {
1380 if (!LoadGameFromFile(appData.loadGameFile,
1381 CalculateIndex(appData.loadGameIndex, gameNr),
1382 appData.loadGameFile, FALSE)) {
1383 DisplayFatalError(_("Bad game file"), 0, 1);
1386 } else if (*appData.loadPositionFile != NULLCHAR) {
1387 if (!LoadPositionFromFile(appData.loadPositionFile,
1388 CalculateIndex(appData.loadPositionIndex, gameNr),
1389 appData.loadPositionFile)) {
1390 DisplayFatalError(_("Bad position file"), 0, 1);
1398 ReserveGame (int gameNr, char resChar)
1400 FILE *tf = fopen(appData.tourneyFile, "r+");
1401 char *p, *q, c, buf[MSG_SIZ];
1402 if(tf == NULL) { nextGame = appData.matchGames + 1; return; } // kludge to terminate match
1403 safeStrCpy(buf, lastMsg, MSG_SIZ);
1404 DisplayMessage(_("Pick new game"), "");
1405 flock(fileno(tf), LOCK_EX); // lock the tourney file while we are messing with it
1406 ParseArgsFromFile(tf);
1407 p = q = appData.results;
1408 if(appData.debugMode) {
1409 char *r = appData.participants;
1410 fprintf(debugFP, "results = '%s'\n", p);
1411 while(*r) fprintf(debugFP, *r >= ' ' ? "%c" : "\\%03o", *r), r++;
1412 fprintf(debugFP, "\n");
1414 while(*q && *q != ' ') q++; // get first un-played game (could be beyond end!)
1416 q = malloc(strlen(p) + 2); // could be arbitrary long, but allow to extend by one!
1417 safeStrCpy(q, p, strlen(p) + 2);
1418 if(gameNr >= 0) q[gameNr] = resChar; // replace '*' with result
1419 if(appData.debugMode) fprintf(debugFP, "pick next game from '%s': %d\n", q, nextGame);
1420 if(nextGame <= appData.matchGames && resChar != ' ' && !abortMatch) { // reserve next game if tourney not yet done
1421 if(q[nextGame] == NULLCHAR) q[nextGame+1] = NULLCHAR; // append one char
1424 fseek(tf, -(strlen(p)+4), SEEK_END);
1426 if(c != '"') // depending on DOS or Unix line endings we can be one off
1427 fseek(tf, -(strlen(p)+2), SEEK_END);
1428 else fseek(tf, -(strlen(p)+3), SEEK_END);
1429 fprintf(tf, "%s\"\n", q); fclose(tf); // update, and flush by closing
1430 DisplayMessage(buf, "");
1431 free(p); appData.results = q;
1432 if(nextGame <= appData.matchGames && resChar != ' ' && !abortMatch &&
1433 (gameNr < 0 || nextGame / appData.defaultMatchGames != gameNr / appData.defaultMatchGames)) {
1434 int round = appData.defaultMatchGames * appData.tourneyType;
1435 if(gameNr < 0 || appData.tourneyType < 1 || // gauntlet engine can always stay loaded as first engine
1436 appData.tourneyType > 1 && nextGame/round != gameNr/round) // in multi-gauntlet change only after round
1437 UnloadEngine(&first); // next game belongs to other pairing;
1438 UnloadEngine(&second); // already unload the engines, so TwoMachinesEvent will load new ones.
1440 if(appData.debugMode) fprintf(debugFP, "Reserved, next=%d, nr=%d\n", nextGame, gameNr);
1444 MatchEvent (int mode)
1445 { // [HGM] moved out of InitBackend3, to make it callable when match starts through menu
1447 if(matchMode) { // already in match mode: switch it off
1449 if(!appData.tourneyFile[0]) appData.matchGames = matchGame; // kludge to let match terminate after next game.
1452 // if(gameMode != BeginningOfGame) {
1453 // DisplayError(_("You can only start a match from the initial position."), 0);
1457 if(mode == 2) appData.matchGames = appData.defaultMatchGames;
1458 /* Set up machine vs. machine match */
1460 NextTourneyGame(-1, &dummy); // sets appData.matchGames if this is tourney, to make sure ReserveGame knows it
1461 if(appData.tourneyFile[0]) {
1463 if(nextGame > appData.matchGames) {
1465 if(strchr(appData.results, '*') == NULL) {
1467 appData.tourneyCycles++;
1468 if(f = WriteTourneyFile(appData.results, NULL)) { // make a tourney file with increased number of cycles
1470 NextTourneyGame(-1, &dummy);
1472 if(nextGame <= appData.matchGames) {
1473 DisplayNote(_("You restarted an already completed tourney\nOne more cycle will now be added to it\nGames commence in 10 sec"));
1475 ScheduleDelayedEvent(NextMatchGame, 10000);
1480 snprintf(buf, MSG_SIZ, _("All games in tourney '%s' are already played or playing"), appData.tourneyFile);
1481 DisplayError(buf, 0);
1482 appData.tourneyFile[0] = 0;
1486 if (appData.noChessProgram) { // [HGM] in tourney engines are loaded automatically
1487 DisplayFatalError(_("Can't have a match with no chess programs"),
1492 matchGame = roundNr = 1;
1493 first.matchWins = second.matchWins = 0; // [HGM] match: needed in later matches
1497 char *comboLine = NULL; // [HGM] recent: WinBoard's first-engine combobox line
1500 InitBackEnd3 P((void))
1502 GameMode initialMode;
1506 InitChessProgram(&first, startedFromSetupPosition);
1508 if(!appData.noChessProgram) { /* [HGM] tidy: redo program version to use name from myname feature */
1509 free(programVersion);
1510 programVersion = (char*) malloc(8 + strlen(PACKAGE_STRING) + strlen(first.tidy));
1511 sprintf(programVersion, "%s + %s", PACKAGE_STRING, first.tidy);
1512 FloatToFront(&appData.recentEngineList, comboLine ? comboLine : appData.firstChessProgram);
1515 if (appData.icsActive) {
1517 /* [DM] Make a console window if needed [HGM] merged ifs */
1523 if (*appData.icsCommPort != NULLCHAR)
1524 len = snprintf(buf, MSG_SIZ, _("Could not open comm port %s"),
1525 appData.icsCommPort);
1527 len = snprintf(buf, MSG_SIZ, _("Could not connect to host %s, port %s"),
1528 appData.icsHost, appData.icsPort);
1530 if( (len >= MSG_SIZ) && appData.debugMode )
1531 fprintf(debugFP, "InitBackEnd3: buffer truncated.\n");
1533 DisplayFatalError(buf, err, 1);
1538 AddInputSource(icsPR, FALSE, read_from_ics, &telnetISR);
1540 AddInputSource(NoProc, FALSE, read_from_player, &fromUserISR);
1541 if(appData.keepAlive) // [HGM] alive: schedule sending of dummy 'date' command
1542 ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
1543 } else if (appData.noChessProgram) {
1549 if (*appData.cmailGameName != NULLCHAR) {
1551 OpenLoopback(&cmailPR);
1553 AddInputSource(cmailPR, FALSE, CmailSigHandlerCallBack, &cmailISR);
1557 DisplayMessage("", "");
1558 if (StrCaseCmp(appData.initialMode, "") == 0) {
1559 initialMode = BeginningOfGame;
1560 if(!appData.icsActive && appData.noChessProgram) { // [HGM] could be fall-back
1561 gameMode = MachinePlaysBlack; // "Machine Black" might have been implicitly highlighted
1562 ModeHighlight(); // make sure XBoard knows it is highlighted, so it will un-highlight it
1563 gameMode = BeginningOfGame; // in case BeginningOfGame now means "Edit Position"
1566 } else if (StrCaseCmp(appData.initialMode, "TwoMachines") == 0) {
1567 initialMode = TwoMachinesPlay;
1568 } else if (StrCaseCmp(appData.initialMode, "AnalyzeFile") == 0) {
1569 initialMode = AnalyzeFile;
1570 } else if (StrCaseCmp(appData.initialMode, "Analysis") == 0) {
1571 initialMode = AnalyzeMode;
1572 } else if (StrCaseCmp(appData.initialMode, "MachineWhite") == 0) {
1573 initialMode = MachinePlaysWhite;
1574 } else if (StrCaseCmp(appData.initialMode, "MachineBlack") == 0) {
1575 initialMode = MachinePlaysBlack;
1576 } else if (StrCaseCmp(appData.initialMode, "EditGame") == 0) {
1577 initialMode = EditGame;
1578 } else if (StrCaseCmp(appData.initialMode, "EditPosition") == 0) {
1579 initialMode = EditPosition;
1580 } else if (StrCaseCmp(appData.initialMode, "Training") == 0) {
1581 initialMode = Training;
1583 len = snprintf(buf, MSG_SIZ, _("Unknown initialMode %s"), appData.initialMode);
1584 if( (len >= MSG_SIZ) && appData.debugMode )
1585 fprintf(debugFP, "InitBackEnd3: buffer truncated.\n");
1587 DisplayFatalError(buf, 0, 2);
1591 if (appData.matchMode) {
1592 if(appData.tourneyFile[0]) { // start tourney from command line
1594 if(f = fopen(appData.tourneyFile, "r")) {
1595 ParseArgsFromFile(f); // make sure tourney parmeters re known
1597 appData.clockMode = TRUE;
1599 } else appData.tourneyFile[0] = NULLCHAR; // for now ignore bad tourney file
1602 } else if (*appData.cmailGameName != NULLCHAR) {
1603 /* Set up cmail mode */
1604 ReloadCmailMsgEvent(TRUE);
1606 /* Set up other modes */
1607 if (initialMode == AnalyzeFile) {
1608 if (*appData.loadGameFile == NULLCHAR) {
1609 DisplayFatalError(_("AnalyzeFile mode requires a game file"), 0, 1);
1613 if (*appData.loadGameFile != NULLCHAR) {
1614 (void) LoadGameFromFile(appData.loadGameFile,
1615 appData.loadGameIndex,
1616 appData.loadGameFile, TRUE);
1617 } else if (*appData.loadPositionFile != NULLCHAR) {
1618 (void) LoadPositionFromFile(appData.loadPositionFile,
1619 appData.loadPositionIndex,
1620 appData.loadPositionFile);
1621 /* [HGM] try to make self-starting even after FEN load */
1622 /* to allow automatic setup of fairy variants with wtm */
1623 if(initialMode == BeginningOfGame && !blackPlaysFirst) {
1624 gameMode = BeginningOfGame;
1625 setboardSpoiledMachineBlack = 1;
1627 /* [HGM] loadPos: make that every new game uses the setup */
1628 /* from file as long as we do not switch variant */
1629 if(!blackPlaysFirst) {
1630 startedFromPositionFile = TRUE;
1631 CopyBoard(filePosition, boards[0]);
1634 if (initialMode == AnalyzeMode) {
1635 if (appData.noChessProgram) {
1636 DisplayFatalError(_("Analysis mode requires a chess engine"), 0, 2);
1639 if (appData.icsActive) {
1640 DisplayFatalError(_("Analysis mode does not work with ICS mode"),0,2);
1644 } else if (initialMode == AnalyzeFile) {
1645 appData.showThinking = TRUE; // [HGM] thinking: moved out of ShowThinkingEvent
1646 ShowThinkingEvent();
1648 AnalysisPeriodicEvent(1);
1649 } else if (initialMode == MachinePlaysWhite) {
1650 if (appData.noChessProgram) {
1651 DisplayFatalError(_("MachineWhite mode requires a chess engine"),
1655 if (appData.icsActive) {
1656 DisplayFatalError(_("MachineWhite mode does not work with ICS mode"),
1660 MachineWhiteEvent();
1661 } else if (initialMode == MachinePlaysBlack) {
1662 if (appData.noChessProgram) {
1663 DisplayFatalError(_("MachineBlack mode requires a chess engine"),
1667 if (appData.icsActive) {
1668 DisplayFatalError(_("MachineBlack mode does not work with ICS mode"),
1672 MachineBlackEvent();
1673 } else if (initialMode == TwoMachinesPlay) {
1674 if (appData.noChessProgram) {
1675 DisplayFatalError(_("TwoMachines mode requires a chess engine"),
1679 if (appData.icsActive) {
1680 DisplayFatalError(_("TwoMachines mode does not work with ICS mode"),
1685 } else if (initialMode == EditGame) {
1687 } else if (initialMode == EditPosition) {
1688 EditPositionEvent();
1689 } else if (initialMode == Training) {
1690 if (*appData.loadGameFile == NULLCHAR) {
1691 DisplayFatalError(_("Training mode requires a game file"), 0, 2);
1700 HistorySet (char movelist[][2*MOVE_LEN], int first, int last, int current)
1702 DisplayBook(current+1);
1704 MoveHistorySet( movelist, first, last, current, pvInfoList );
1706 EvalGraphSet( first, last, current, pvInfoList );
1708 MakeEngineOutputTitle();
1712 * Establish will establish a contact to a remote host.port.
1713 * Sets icsPR to a ProcRef for a process (or pseudo-process)
1714 * used to talk to the host.
1715 * Returns 0 if okay, error code if not.
1722 if (*appData.icsCommPort != NULLCHAR) {
1723 /* Talk to the host through a serial comm port */
1724 return OpenCommPort(appData.icsCommPort, &icsPR);
1726 } else if (*appData.gateway != NULLCHAR) {
1727 if (*appData.remoteShell == NULLCHAR) {
1728 /* Use the rcmd protocol to run telnet program on a gateway host */
1729 snprintf(buf, sizeof(buf), "%s %s %s",
1730 appData.telnetProgram, appData.icsHost, appData.icsPort);
1731 return OpenRcmd(appData.gateway, appData.remoteUser, buf, &icsPR);
1734 /* Use the rsh program to run telnet program on a gateway host */
1735 if (*appData.remoteUser == NULLCHAR) {
1736 snprintf(buf, sizeof(buf), "%s %s %s %s %s", appData.remoteShell,
1737 appData.gateway, appData.telnetProgram,
1738 appData.icsHost, appData.icsPort);
1740 snprintf(buf, sizeof(buf), "%s %s -l %s %s %s %s",
1741 appData.remoteShell, appData.gateway,
1742 appData.remoteUser, appData.telnetProgram,
1743 appData.icsHost, appData.icsPort);
1745 return StartChildProcess(buf, "", &icsPR);
1748 } else if (appData.useTelnet) {
1749 return OpenTelnet(appData.icsHost, appData.icsPort, &icsPR);
1752 /* TCP socket interface differs somewhat between
1753 Unix and NT; handle details in the front end.
1755 return OpenTCP(appData.icsHost, appData.icsPort, &icsPR);
1760 EscapeExpand (char *p, char *q)
1761 { // [HGM] initstring: routine to shape up string arguments
1762 while(*p++ = *q++) if(p[-1] == '\\')
1764 case 'n': p[-1] = '\n'; break;
1765 case 'r': p[-1] = '\r'; break;
1766 case 't': p[-1] = '\t'; break;
1767 case '\\': p[-1] = '\\'; break;
1768 case 0: *p = 0; return;
1769 default: p[-1] = q[-1]; break;
1774 show_bytes (FILE *fp, char *buf, int count)
1777 if (*buf < 040 || *(unsigned char *) buf > 0177) {
1778 fprintf(fp, "\\%03o", *buf & 0xff);
1787 /* Returns an errno value */
1789 OutputMaybeTelnet (ProcRef pr, char *message, int count, int *outError)
1791 char buf[8192], *p, *q, *buflim;
1792 int left, newcount, outcount;
1794 if (*appData.icsCommPort != NULLCHAR || appData.useTelnet ||
1795 *appData.gateway != NULLCHAR) {
1796 if (appData.debugMode) {
1797 fprintf(debugFP, ">ICS: ");
1798 show_bytes(debugFP, message, count);
1799 fprintf(debugFP, "\n");
1801 return OutputToProcess(pr, message, count, outError);
1804 buflim = &buf[sizeof(buf)-1]; /* allow 1 byte for expanding last char */
1811 if (appData.debugMode) {
1812 fprintf(debugFP, ">ICS: ");
1813 show_bytes(debugFP, buf, newcount);
1814 fprintf(debugFP, "\n");
1816 outcount = OutputToProcess(pr, buf, newcount, outError);
1817 if (outcount < newcount) return -1; /* to be sure */
1824 } else if (((unsigned char) *p) == TN_IAC) {
1825 *q++ = (char) TN_IAC;
1832 if (appData.debugMode) {
1833 fprintf(debugFP, ">ICS: ");
1834 show_bytes(debugFP, buf, newcount);
1835 fprintf(debugFP, "\n");
1837 outcount = OutputToProcess(pr, buf, newcount, outError);
1838 if (outcount < newcount) return -1; /* to be sure */
1843 read_from_player (InputSourceRef isr, VOIDSTAR closure, char *message, int count, int error)
1845 int outError, outCount;
1846 static int gotEof = 0;
1848 /* Pass data read from player on to ICS */
1851 outCount = OutputMaybeTelnet(icsPR, message, count, &outError);
1852 if (outCount < count) {
1853 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1855 } else if (count < 0) {
1856 RemoveInputSource(isr);
1857 DisplayFatalError(_("Error reading from keyboard"), error, 1);
1858 } else if (gotEof++ > 0) {
1859 RemoveInputSource(isr);
1860 DisplayFatalError(_("Got end of file from keyboard"), 0, 0);
1866 { // [HGM] alive: periodically send dummy (date) command to ICS to prevent time-out
1867 if(!connectionAlive) DisplayFatalError("No response from ICS", 0, 1);
1868 connectionAlive = FALSE; // only sticks if no response to 'date' command.
1869 SendToICS("date\n");
1870 if(appData.keepAlive) ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
1873 /* added routine for printf style output to ics */
1875 ics_printf (char *format, ...)
1877 char buffer[MSG_SIZ];
1880 va_start(args, format);
1881 vsnprintf(buffer, sizeof(buffer), format, args);
1882 buffer[sizeof(buffer)-1] = '\0';
1890 int count, outCount, outError;
1892 if (icsPR == NoProc) return;
1895 outCount = OutputMaybeTelnet(icsPR, s, count, &outError);
1896 if (outCount < count) {
1897 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1901 /* This is used for sending logon scripts to the ICS. Sending
1902 without a delay causes problems when using timestamp on ICC
1903 (at least on my machine). */
1905 SendToICSDelayed (char *s, long msdelay)
1907 int count, outCount, outError;
1909 if (icsPR == NoProc) return;
1912 if (appData.debugMode) {
1913 fprintf(debugFP, ">ICS: ");
1914 show_bytes(debugFP, s, count);
1915 fprintf(debugFP, "\n");
1917 outCount = OutputToProcessDelayed(icsPR, s, count, &outError,
1919 if (outCount < count) {
1920 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1925 /* Remove all highlighting escape sequences in s
1926 Also deletes any suffix starting with '('
1929 StripHighlightAndTitle (char *s)
1931 static char retbuf[MSG_SIZ];
1934 while (*s != NULLCHAR) {
1935 while (*s == '\033') {
1936 while (*s != NULLCHAR && !isalpha(*s)) s++;
1937 if (*s != NULLCHAR) s++;
1939 while (*s != NULLCHAR && *s != '\033') {
1940 if (*s == '(' || *s == '[') {
1951 /* Remove all highlighting escape sequences in s */
1953 StripHighlight (char *s)
1955 static char retbuf[MSG_SIZ];
1958 while (*s != NULLCHAR) {
1959 while (*s == '\033') {
1960 while (*s != NULLCHAR && !isalpha(*s)) s++;
1961 if (*s != NULLCHAR) s++;
1963 while (*s != NULLCHAR && *s != '\033') {
1971 char *variantNames[] = VARIANT_NAMES;
1973 VariantName (VariantClass v)
1975 return variantNames[v];
1979 /* Identify a variant from the strings the chess servers use or the
1980 PGN Variant tag names we use. */
1982 StringToVariant (char *e)
1986 VariantClass v = VariantNormal;
1987 int i, found = FALSE;
1993 /* [HGM] skip over optional board-size prefixes */
1994 if( sscanf(e, "%dx%d_", &i, &i) == 2 ||
1995 sscanf(e, "%dx%d+%d_", &i, &i, &i) == 3 ) {
1996 while( *e++ != '_');
1999 if(StrCaseStr(e, "misc/")) { // [HGM] on FICS, misc/shogi is not shogi
2003 for (i=0; i<sizeof(variantNames)/sizeof(char*); i++) {
2004 if (StrCaseStr(e, variantNames[i])) {
2005 v = (VariantClass) i;
2012 if ((StrCaseStr(e, "fischer") && StrCaseStr(e, "random"))
2013 || StrCaseStr(e, "wild/fr")
2014 || StrCaseStr(e, "frc") || StrCaseStr(e, "960")) {
2015 v = VariantFischeRandom;
2016 } else if ((i = 4, p = StrCaseStr(e, "wild")) ||
2017 (i = 1, p = StrCaseStr(e, "w"))) {
2019 while (*p && (isspace(*p) || *p == '(' || *p == '/')) p++;
2026 case 0: /* FICS only, actually */
2028 /* Castling legal even if K starts on d-file */
2029 v = VariantWildCastle;
2034 /* Castling illegal even if K & R happen to start in
2035 normal positions. */
2036 v = VariantNoCastle;
2049 /* Castling legal iff K & R start in normal positions */
2055 /* Special wilds for position setup; unclear what to do here */
2056 v = VariantLoadable;
2059 /* Bizarre ICC game */
2060 v = VariantTwoKings;
2063 v = VariantKriegspiel;
2069 v = VariantFischeRandom;
2072 v = VariantCrazyhouse;
2075 v = VariantBughouse;
2081 /* Not quite the same as FICS suicide! */
2082 v = VariantGiveaway;
2088 v = VariantShatranj;
2091 /* Temporary names for future ICC types. The name *will* change in
2092 the next xboard/WinBoard release after ICC defines it. */
2130 v = VariantCapablanca;
2133 v = VariantKnightmate;
2139 v = VariantCylinder;
2145 v = VariantCapaRandom;
2148 v = VariantBerolina;
2160 /* Found "wild" or "w" in the string but no number;
2161 must assume it's normal chess. */
2165 len = snprintf(buf, MSG_SIZ, _("Unknown wild type %d"), wnum);
2166 if( (len >= MSG_SIZ) && appData.debugMode )
2167 fprintf(debugFP, "StringToVariant: buffer truncated.\n");
2169 DisplayError(buf, 0);
2175 if (appData.debugMode) {
2176 fprintf(debugFP, _("recognized '%s' (%d) as variant %s\n"),
2177 e, wnum, VariantName(v));
2182 static int leftover_start = 0, leftover_len = 0;
2183 char star_match[STAR_MATCH_N][MSG_SIZ];
2185 /* Test whether pattern is present at &buf[*index]; if so, return TRUE,
2186 advance *index beyond it, and set leftover_start to the new value of
2187 *index; else return FALSE. If pattern contains the character '*', it
2188 matches any sequence of characters not containing '\r', '\n', or the
2189 character following the '*' (if any), and the matched sequence(s) are
2190 copied into star_match.
2193 looking_at ( char *buf, int *index, char *pattern)
2195 char *bufp = &buf[*index], *patternp = pattern;
2197 char *matchp = star_match[0];
2200 if (*patternp == NULLCHAR) {
2201 *index = leftover_start = bufp - buf;
2205 if (*bufp == NULLCHAR) return FALSE;
2206 if (*patternp == '*') {
2207 if (*bufp == *(patternp + 1)) {
2209 matchp = star_match[++star_count];
2213 } else if (*bufp == '\n' || *bufp == '\r') {
2215 if (*patternp == NULLCHAR)
2220 *matchp++ = *bufp++;
2224 if (*patternp != *bufp) return FALSE;
2231 SendToPlayer (char *data, int length)
2233 int error, outCount;
2234 outCount = OutputToProcess(NoProc, data, length, &error);
2235 if (outCount < length) {
2236 DisplayFatalError(_("Error writing to display"), error, 1);
2241 PackHolding (char packed[], char *holding)
2251 switch (runlength) {
2262 sprintf(q, "%d", runlength);
2274 /* Telnet protocol requests from the front end */
2276 TelnetRequest (unsigned char ddww, unsigned char option)
2278 unsigned char msg[3];
2279 int outCount, outError;
2281 if (*appData.icsCommPort != NULLCHAR || appData.useTelnet) return;
2283 if (appData.debugMode) {
2284 char buf1[8], buf2[8], *ddwwStr, *optionStr;
2300 snprintf(buf1,sizeof(buf1)/sizeof(buf1[0]), "%d", ddww);
2309 snprintf(buf2,sizeof(buf2)/sizeof(buf2[0]), "%d", option);
2312 fprintf(debugFP, ">%s %s ", ddwwStr, optionStr);
2317 outCount = OutputToProcess(icsPR, (char *)msg, 3, &outError);
2319 DisplayFatalError(_("Error writing to ICS"), outError, 1);
2326 if (!appData.icsActive) return;
2327 TelnetRequest(TN_DO, TN_ECHO);
2333 if (!appData.icsActive) return;
2334 TelnetRequest(TN_DONT, TN_ECHO);
2338 CopyHoldings (Board board, char *holdings, ChessSquare lowestPiece)
2340 /* put the holdings sent to us by the server on the board holdings area */
2341 int i, j, holdingsColumn, holdingsStartRow, direction, countsColumn;
2345 if(gameInfo.holdingsWidth < 2) return;
2346 if(gameInfo.variant != VariantBughouse && board[HOLDINGS_SET])
2347 return; // prevent overwriting by pre-board holdings
2349 if( (int)lowestPiece >= BlackPawn ) {
2352 holdingsStartRow = BOARD_HEIGHT-1;
2355 holdingsColumn = BOARD_WIDTH-1;
2356 countsColumn = BOARD_WIDTH-2;
2357 holdingsStartRow = 0;
2361 for(i=0; i<BOARD_HEIGHT; i++) { /* clear holdings */
2362 board[i][holdingsColumn] = EmptySquare;
2363 board[i][countsColumn] = (ChessSquare) 0;
2365 while( (p=*holdings++) != NULLCHAR ) {
2366 piece = CharToPiece( ToUpper(p) );
2367 if(piece == EmptySquare) continue;
2368 /*j = (int) piece - (int) WhitePawn;*/
2369 j = PieceToNumber(piece);
2370 if(j >= gameInfo.holdingsSize) continue; /* ignore pieces that do not fit */
2371 if(j < 0) continue; /* should not happen */
2372 piece = (ChessSquare) ( (int)piece + (int)lowestPiece );
2373 board[holdingsStartRow+j*direction][holdingsColumn] = piece;
2374 board[holdingsStartRow+j*direction][countsColumn]++;
2380 VariantSwitch (Board board, VariantClass newVariant)
2382 int newHoldingsWidth, newWidth = 8, newHeight = 8, i, j;
2383 static Board oldBoard;
2385 startedFromPositionFile = FALSE;
2386 if(gameInfo.variant == newVariant) return;
2388 /* [HGM] This routine is called each time an assignment is made to
2389 * gameInfo.variant during a game, to make sure the board sizes
2390 * are set to match the new variant. If that means adding or deleting
2391 * holdings, we shift the playing board accordingly
2392 * This kludge is needed because in ICS observe mode, we get boards
2393 * of an ongoing game without knowing the variant, and learn about the
2394 * latter only later. This can be because of the move list we requested,
2395 * in which case the game history is refilled from the beginning anyway,
2396 * but also when receiving holdings of a crazyhouse game. In the latter
2397 * case we want to add those holdings to the already received position.
2401 if (appData.debugMode) {
2402 fprintf(debugFP, "Switch board from %s to %s\n",
2403 VariantName(gameInfo.variant), VariantName(newVariant));
2404 setbuf(debugFP, NULL);
2406 shuffleOpenings = 0; /* [HGM] shuffle */
2407 gameInfo.holdingsSize = 5; /* [HGM] prepare holdings */
2411 newWidth = 9; newHeight = 9;
2412 gameInfo.holdingsSize = 7;
2413 case VariantBughouse:
2414 case VariantCrazyhouse:
2415 newHoldingsWidth = 2; break;
2419 newHoldingsWidth = 2;
2420 gameInfo.holdingsSize = 8;
2423 case VariantCapablanca:
2424 case VariantCapaRandom:
2427 newHoldingsWidth = gameInfo.holdingsSize = 0;
2430 if(newWidth != gameInfo.boardWidth ||
2431 newHeight != gameInfo.boardHeight ||
2432 newHoldingsWidth != gameInfo.holdingsWidth ) {
2434 /* shift position to new playing area, if needed */
2435 if(newHoldingsWidth > gameInfo.holdingsWidth) {
2436 for(i=0; i<BOARD_HEIGHT; i++)
2437 for(j=BOARD_RGHT-1; j>=BOARD_LEFT; j--)
2438 board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
2440 for(i=0; i<newHeight; i++) {
2441 board[i][0] = board[i][newWidth+2*newHoldingsWidth-1] = EmptySquare;
2442 board[i][1] = board[i][newWidth+2*newHoldingsWidth-2] = (ChessSquare) 0;
2444 } else if(newHoldingsWidth < gameInfo.holdingsWidth) {
2445 for(i=0; i<BOARD_HEIGHT; i++)
2446 for(j=BOARD_LEFT; j<BOARD_RGHT; j++)
2447 board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
2450 board[HOLDINGS_SET] = 0;
2451 gameInfo.boardWidth = newWidth;
2452 gameInfo.boardHeight = newHeight;
2453 gameInfo.holdingsWidth = newHoldingsWidth;
2454 gameInfo.variant = newVariant;
2455 InitDrawingSizes(-2, 0);
2456 } else gameInfo.variant = newVariant;
2457 CopyBoard(oldBoard, board); // remember correctly formatted board
2458 InitPosition(FALSE); /* this sets up board[0], but also other stuff */
2459 DrawPosition(TRUE, currentMove ? boards[currentMove] : oldBoard);
2462 static int loggedOn = FALSE;
2464 /*-- Game start info cache: --*/
2466 char gs_kind[MSG_SIZ];
2467 static char player1Name[128] = "";
2468 static char player2Name[128] = "";
2469 static char cont_seq[] = "\n\\ ";
2470 static int player1Rating = -1;
2471 static int player2Rating = -1;
2472 /*----------------------------*/
2474 ColorClass curColor = ColorNormal;
2475 int suppressKibitz = 0;
2478 Boolean soughtPending = FALSE;
2479 Boolean seekGraphUp;
2480 #define MAX_SEEK_ADS 200
2482 char *seekAdList[MAX_SEEK_ADS];
2483 int ratingList[MAX_SEEK_ADS], xList[MAX_SEEK_ADS], yList[MAX_SEEK_ADS], seekNrList[MAX_SEEK_ADS], zList[MAX_SEEK_ADS];
2484 float tcList[MAX_SEEK_ADS];
2485 char colorList[MAX_SEEK_ADS];
2486 int nrOfSeekAds = 0;
2487 int minRating = 1010, maxRating = 2800;
2488 int hMargin = 10, vMargin = 20, h, w;
2489 extern int squareSize, lineGap;
2494 int x, y, color = 0, r = ratingList[i]; float tc = tcList[i];
2495 xList[i] = yList[i] = -100; // outside graph, so cannot be clicked
2496 if(r < minRating+100 && r >=0 ) r = minRating+100;
2497 if(r > maxRating) r = maxRating;
2498 if(tc < 1.f) tc = 1.f;
2499 if(tc > 95.f) tc = 95.f;
2500 x = (w-hMargin-squareSize/8-7)* log(tc)/log(95.) + hMargin;
2501 y = ((double)r - minRating)/(maxRating - minRating)
2502 * (h-vMargin-squareSize/8-1) + vMargin;
2503 if(ratingList[i] < 0) y = vMargin + squareSize/4;
2504 if(strstr(seekAdList[i], " u ")) color = 1;
2505 if(!strstr(seekAdList[i], "lightning") && // for now all wilds same color
2506 !strstr(seekAdList[i], "bullet") &&
2507 !strstr(seekAdList[i], "blitz") &&
2508 !strstr(seekAdList[i], "standard") ) color = 2;
2509 if(strstr(seekAdList[i], "(C) ")) color |= SQUARE; // plot computer seeks as squares
2510 DrawSeekDot(xList[i]=x+3*(color&~SQUARE), yList[i]=h-1-y, colorList[i]=color);
2514 PlotSingleSeekAd (int i)
2520 AddAd (char *handle, char *rating, int base, int inc, char rated, char *type, int nr, Boolean plot)
2522 char buf[MSG_SIZ], *ext = "";
2523 VariantClass v = StringToVariant(type);
2524 if(strstr(type, "wild")) {
2525 ext = type + 4; // append wild number
2526 if(v == VariantFischeRandom) type = "chess960"; else
2527 if(v == VariantLoadable) type = "setup"; else
2528 type = VariantName(v);
2530 snprintf(buf, MSG_SIZ, "%s (%s) %d %d %c %s%s", handle, rating, base, inc, rated, type, ext);
2531 if(nrOfSeekAds < MAX_SEEK_ADS-1) {
2532 if(seekAdList[nrOfSeekAds]) free(seekAdList[nrOfSeekAds]);
2533 ratingList[nrOfSeekAds] = -1; // for if seeker has no rating
2534 sscanf(rating, "%d", &ratingList[nrOfSeekAds]);
2535 tcList[nrOfSeekAds] = base + (2./3.)*inc;
2536 seekNrList[nrOfSeekAds] = nr;
2537 zList[nrOfSeekAds] = 0;
2538 seekAdList[nrOfSeekAds++] = StrSave(buf);
2539 if(plot) PlotSingleSeekAd(nrOfSeekAds-1);
2544 EraseSeekDot (int i)
2546 int x = xList[i], y = yList[i], d=squareSize/4, k;
2547 DrawSeekBackground(x-squareSize/8, y-squareSize/8, x+squareSize/8+1, y+squareSize/8+1);
2548 if(x < hMargin+d) DrawSeekAxis(hMargin, y-squareSize/8, hMargin, y+squareSize/8+1);
2549 // now replot every dot that overlapped
2550 for(k=0; k<nrOfSeekAds; k++) if(k != i) {
2551 int xx = xList[k], yy = yList[k];
2552 if(xx <= x+d && xx > x-d && yy <= y+d && yy > y-d)
2553 DrawSeekDot(xx, yy, colorList[k]);
2558 RemoveSeekAd (int nr)
2561 for(i=0; i<nrOfSeekAds; i++) if(seekNrList[i] == nr) {
2563 if(seekAdList[i]) free(seekAdList[i]);
2564 seekAdList[i] = seekAdList[--nrOfSeekAds];
2565 seekNrList[i] = seekNrList[nrOfSeekAds];
2566 ratingList[i] = ratingList[nrOfSeekAds];
2567 colorList[i] = colorList[nrOfSeekAds];
2568 tcList[i] = tcList[nrOfSeekAds];
2569 xList[i] = xList[nrOfSeekAds];
2570 yList[i] = yList[nrOfSeekAds];
2571 zList[i] = zList[nrOfSeekAds];
2572 seekAdList[nrOfSeekAds] = NULL;
2578 MatchSoughtLine (char *line)
2580 char handle[MSG_SIZ], rating[MSG_SIZ], type[MSG_SIZ];
2581 int nr, base, inc, u=0; char dummy;
2583 if(sscanf(line, "%d %s %s %d %d rated %s", &nr, rating, handle, &base, &inc, type) == 6 ||
2584 sscanf(line, "%d %s %s %s %d %d rated %c", &nr, rating, handle, type, &base, &inc, &dummy) == 7 ||
2586 (sscanf(line, "%d %s %s %d %d unrated %s", &nr, rating, handle, &base, &inc, type) == 6 ||
2587 sscanf(line, "%d %s %s %s %d %d unrated %c", &nr, rating, handle, type, &base, &inc, &dummy) == 7) ) {
2588 // match: compact and save the line
2589 AddAd(handle, rating, base, inc, u ? 'u' : 'r', type, nr, FALSE);
2599 if(!seekGraphUp) return FALSE;
2600 h = BOARD_HEIGHT * (squareSize + lineGap) + lineGap;
2601 w = BOARD_WIDTH * (squareSize + lineGap) + lineGap;
2603 DrawSeekBackground(0, 0, w, h);
2604 DrawSeekAxis(hMargin, h-1-vMargin, w-5, h-1-vMargin);
2605 DrawSeekAxis(hMargin, h-1-vMargin, hMargin, 5);
2606 for(i=0; i<4000; i+= 100) if(i>=minRating && i<maxRating) {
2607 int yy =((double)i - minRating)/(maxRating - minRating)*(h-vMargin-squareSize/8-1) + vMargin;
2609 DrawSeekAxis(hMargin-5, yy, hMargin+5*(i%500==0), yy); // rating ticks
2612 snprintf(buf, MSG_SIZ, "%d", i);
2613 DrawSeekText(buf, hMargin+squareSize/8+7, yy);
2616 DrawSeekText("unrated", hMargin+squareSize/8+7, h-1-vMargin-squareSize/4);
2617 for(i=1; i<100; i+=(i<10?1:5)) {
2618 int xx = (w-hMargin-squareSize/8-7)* log((double)i)/log(95.) + hMargin;
2619 DrawSeekAxis(xx, h-1-vMargin, xx, h-6-vMargin-3*(i%10==0)); // TC ticks
2620 if(i<=5 || (i>40 ? i%20 : i%10) == 0) {
2622 snprintf(buf, MSG_SIZ, "%d", i);
2623 DrawSeekText(buf, xx-2-3*(i>9), h-1-vMargin/2);
2626 for(i=0; i<nrOfSeekAds; i++) PlotSeekAd(i);
2631 SeekGraphClick (ClickType click, int x, int y, int moving)
2633 static int lastDown = 0, displayed = 0, lastSecond;
2634 if(y < 0) return FALSE;
2635 if(!(appData.seekGraph && appData.icsActive && loggedOn &&
2636 (gameMode == BeginningOfGame || gameMode == IcsIdle))) {
2637 if(!seekGraphUp) return FALSE;
2638 seekGraphUp = FALSE; // seek graph is up when it shouldn't be: take it down
2639 DrawPosition(TRUE, NULL);
2642 if(!seekGraphUp) { // initiate cration of seek graph by requesting seek-ad list
2643 if(click == Release || moving) return FALSE;
2645 soughtPending = TRUE;
2646 SendToICS(ics_prefix);
2647 SendToICS("sought\n"); // should this be "sought all"?
2648 } else { // issue challenge based on clicked ad
2649 int dist = 10000; int i, closest = 0, second = 0;
2650 for(i=0; i<nrOfSeekAds; i++) {
2651 int d = (x-xList[i])*(x-xList[i]) + (y-yList[i])*(y-yList[i]) + zList[i];
2652 if(d < dist) { dist = d; closest = i; }
2653 second += (d - zList[i] < 120); // count in-range ads
2654 if(click == Press && moving != 1 && zList[i]>0) zList[i] *= 0.8; // age priority
2658 second = (second > 1);
2659 if(displayed != closest || second != lastSecond) {
2660 DisplayMessage(second ? "!" : "", seekAdList[closest]);
2661 lastSecond = second; displayed = closest;
2663 if(click == Press) {
2664 if(moving == 2) zList[closest] = 100; // right-click; push to back on press
2667 } // on press 'hit', only show info
2668 if(moving == 2) return TRUE; // ignore right up-clicks on dot
2669 snprintf(buf, MSG_SIZ, "play %d\n", seekNrList[closest]);
2670 SendToICS(ics_prefix);
2672 return TRUE; // let incoming board of started game pop down the graph
2673 } else if(click == Release) { // release 'miss' is ignored
2674 zList[lastDown] = 100; // make future selection of the rejected ad more difficult
2675 if(moving == 2) { // right up-click
2676 nrOfSeekAds = 0; // refresh graph
2677 soughtPending = TRUE;
2678 SendToICS(ics_prefix);
2679 SendToICS("sought\n"); // should this be "sought all"?
2682 } else if(moving) { if(displayed >= 0) DisplayMessage("", ""); displayed = -1; return TRUE; }
2683 // press miss or release hit 'pop down' seek graph
2684 seekGraphUp = FALSE;
2685 DrawPosition(TRUE, NULL);
2691 read_from_ics (InputSourceRef isr, VOIDSTAR closure, char *data, int count, int error)
2693 #define BUF_SIZE (16*1024) /* overflowed at 8K with "inchannel 1" on FICS? */
2694 #define STARTED_NONE 0
2695 #define STARTED_MOVES 1
2696 #define STARTED_BOARD 2
2697 #define STARTED_OBSERVE 3
2698 #define STARTED_HOLDINGS 4
2699 #define STARTED_CHATTER 5
2700 #define STARTED_COMMENT 6
2701 #define STARTED_MOVES_NOHIDE 7
2703 static int started = STARTED_NONE;
2704 static char parse[20000];
2705 static int parse_pos = 0;
2706 static char buf[BUF_SIZE + 1];
2707 static int firstTime = TRUE, intfSet = FALSE;
2708 static ColorClass prevColor = ColorNormal;
2709 static int savingComment = FALSE;
2710 static int cmatch = 0; // continuation sequence match
2717 int backup; /* [DM] For zippy color lines */
2719 char talker[MSG_SIZ]; // [HGM] chat
2722 connectionAlive = TRUE; // [HGM] alive: I think, therefore I am...
2724 if (appData.debugMode) {
2726 fprintf(debugFP, "<ICS: ");
2727 show_bytes(debugFP, data, count);
2728 fprintf(debugFP, "\n");
2732 if (appData.debugMode) { int f = forwardMostMove;
2733 fprintf(debugFP, "ics input %d, castling = %d %d %d %d %d %d\n", f,
2734 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
2735 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
2738 /* If last read ended with a partial line that we couldn't parse,
2739 prepend it to the new read and try again. */
2740 if (leftover_len > 0) {
2741 for (i=0; i<leftover_len; i++)
2742 buf[i] = buf[leftover_start + i];
2745 /* copy new characters into the buffer */
2746 bp = buf + leftover_len;
2747 buf_len=leftover_len;
2748 for (i=0; i<count; i++)
2751 if (data[i] == '\r')
2754 // join lines split by ICS?
2755 if (!appData.noJoin)
2758 Joining just consists of finding matches against the
2759 continuation sequence, and discarding that sequence
2760 if found instead of copying it. So, until a match
2761 fails, there's nothing to do since it might be the
2762 complete sequence, and thus, something we don't want
2765 if (data[i] == cont_seq[cmatch])
2768 if (cmatch == strlen(cont_seq))
2770 cmatch = 0; // complete match. just reset the counter
2773 it's possible for the ICS to not include the space
2774 at the end of the last word, making our [correct]
2775 join operation fuse two separate words. the server
2776 does this when the space occurs at the width setting.
2778 if (!buf_len || buf[buf_len-1] != ' ')
2789 match failed, so we have to copy what matched before
2790 falling through and copying this character. In reality,
2791 this will only ever be just the newline character, but
2792 it doesn't hurt to be precise.
2794 strncpy(bp, cont_seq, cmatch);
2806 buf[buf_len] = NULLCHAR;
2807 // next_out = leftover_len; // [HGM] should we set this to 0, and not print it in advance?
2812 while (i < buf_len) {
2813 /* Deal with part of the TELNET option negotiation
2814 protocol. We refuse to do anything beyond the
2815 defaults, except that we allow the WILL ECHO option,
2816 which ICS uses to turn off password echoing when we are
2817 directly connected to it. We reject this option
2818 if localLineEditing mode is on (always on in xboard)
2819 and we are talking to port 23, which might be a real
2820 telnet server that will try to keep WILL ECHO on permanently.
2822 if (buf_len - i >= 3 && (unsigned char) buf[i] == TN_IAC) {
2823 static int remoteEchoOption = FALSE; /* telnet ECHO option */
2824 unsigned char option;
2826 switch ((unsigned char) buf[++i]) {
2828 if (appData.debugMode)
2829 fprintf(debugFP, "\n<WILL ");
2830 switch (option = (unsigned char) buf[++i]) {
2832 if (appData.debugMode)
2833 fprintf(debugFP, "ECHO ");
2834 /* Reply only if this is a change, according
2835 to the protocol rules. */
2836 if (remoteEchoOption) break;
2837 if (appData.localLineEditing &&
2838 atoi(appData.icsPort) == TN_PORT) {
2839 TelnetRequest(TN_DONT, TN_ECHO);
2842 TelnetRequest(TN_DO, TN_ECHO);
2843 remoteEchoOption = TRUE;
2847 if (appData.debugMode)
2848 fprintf(debugFP, "%d ", option);
2849 /* Whatever this is, we don't want it. */
2850 TelnetRequest(TN_DONT, option);
2855 if (appData.debugMode)
2856 fprintf(debugFP, "\n<WONT ");
2857 switch (option = (unsigned char) buf[++i]) {
2859 if (appData.debugMode)
2860 fprintf(debugFP, "ECHO ");
2861 /* Reply only if this is a change, according
2862 to the protocol rules. */
2863 if (!remoteEchoOption) break;
2865 TelnetRequest(TN_DONT, TN_ECHO);
2866 remoteEchoOption = FALSE;
2869 if (appData.debugMode)
2870 fprintf(debugFP, "%d ", (unsigned char) option);
2871 /* Whatever this is, it must already be turned
2872 off, because we never agree to turn on
2873 anything non-default, so according to the
2874 protocol rules, we don't reply. */
2879 if (appData.debugMode)
2880 fprintf(debugFP, "\n<DO ");
2881 switch (option = (unsigned char) buf[++i]) {
2883 /* Whatever this is, we refuse to do it. */
2884 if (appData.debugMode)
2885 fprintf(debugFP, "%d ", option);
2886 TelnetRequest(TN_WONT, option);
2891 if (appData.debugMode)
2892 fprintf(debugFP, "\n<DONT ");
2893 switch (option = (unsigned char) buf[++i]) {
2895 if (appData.debugMode)
2896 fprintf(debugFP, "%d ", option);
2897 /* Whatever this is, we are already not doing
2898 it, because we never agree to do anything
2899 non-default, so according to the protocol
2900 rules, we don't reply. */
2905 if (appData.debugMode)
2906 fprintf(debugFP, "\n<IAC ");
2907 /* Doubled IAC; pass it through */
2911 if (appData.debugMode)
2912 fprintf(debugFP, "\n<%d ", (unsigned char) buf[i]);
2913 /* Drop all other telnet commands on the floor */
2916 if (oldi > next_out)
2917 SendToPlayer(&buf[next_out], oldi - next_out);
2923 /* OK, this at least will *usually* work */
2924 if (!loggedOn && looking_at(buf, &i, "ics%")) {
2928 if (loggedOn && !intfSet) {
2929 if (ics_type == ICS_ICC) {
2930 snprintf(str, MSG_SIZ,
2931 "/set-quietly interface %s\n/set-quietly style 12\n",
2933 if(appData.seekGraph && appData.autoRefresh) // [HGM] seekgraph
2934 strcat(str, "/set-2 51 1\n/set seek 1\n");
2935 } else if (ics_type == ICS_CHESSNET) {
2936 snprintf(str, MSG_SIZ, "/style 12\n");
2938 safeStrCpy(str, "alias $ @\n$set interface ", sizeof(str)/sizeof(str[0]));
2939 strcat(str, programVersion);
2940 strcat(str, "\n$iset startpos 1\n$iset ms 1\n");
2941 if(appData.seekGraph && appData.autoRefresh) // [HGM] seekgraph
2942 strcat(str, "$iset seekremove 1\n$set seek 1\n");
2944 strcat(str, "$iset nohighlight 1\n");
2946 strcat(str, "$iset lock 1\n$style 12\n");
2949 NotifyFrontendLogin();
2953 if (started == STARTED_COMMENT) {
2954 /* Accumulate characters in comment */
2955 parse[parse_pos++] = buf[i];
2956 if (buf[i] == '\n') {
2957 parse[parse_pos] = NULLCHAR;
2958 if(chattingPartner>=0) {
2960 snprintf(mess, MSG_SIZ, "%s%s", talker, parse);
2961 OutputChatMessage(chattingPartner, mess);
2962 chattingPartner = -1;
2963 next_out = i+1; // [HGM] suppress printing in ICS window
2965 if(!suppressKibitz) // [HGM] kibitz
2966 AppendComment(forwardMostMove, StripHighlight(parse), TRUE);
2967 else { // [HGM kibitz: divert memorized engine kibitz to engine-output window
2968 int nrDigit = 0, nrAlph = 0, j;
2969 if(parse_pos > MSG_SIZ - 30) // defuse unreasonably long input
2970 { parse_pos = MSG_SIZ-30; parse[parse_pos - 1] = '\n'; }
2971 parse[parse_pos] = NULLCHAR;
2972 // try to be smart: if it does not look like search info, it should go to
2973 // ICS interaction window after all, not to engine-output window.
2974 for(j=0; j<parse_pos; j++) { // count letters and digits
2975 nrDigit += (parse[j] >= '0' && parse[j] <= '9');
2976 nrAlph += (parse[j] >= 'a' && parse[j] <= 'z');
2977 nrAlph += (parse[j] >= 'A' && parse[j] <= 'Z');
2979 if(nrAlph < 9*nrDigit) { // if more than 10% digit we assume search info
2980 int depth=0; float score;
2981 if(sscanf(parse, "!!! %f/%d", &score, &depth) == 2 && depth>0) {
2982 // [HGM] kibitz: save kibitzed opponent info for PGN and eval graph
2983 pvInfoList[forwardMostMove-1].depth = depth;
2984 pvInfoList[forwardMostMove-1].score = 100*score;
2986 OutputKibitz(suppressKibitz, parse);
2989 if(gameMode == IcsObserving) // restore original ICS messages
2990 snprintf(tmp, MSG_SIZ, "%s kibitzes: %s", star_match[0], parse);
2992 snprintf(tmp, MSG_SIZ, _("your opponent kibitzes: %s"), parse);
2993 SendToPlayer(tmp, strlen(tmp));
2995 next_out = i+1; // [HGM] suppress printing in ICS window
2997 started = STARTED_NONE;
2999 /* Don't match patterns against characters in comment */
3004 if (started == STARTED_CHATTER) {
3005 if (buf[i] != '\n') {
3006 /* Don't match patterns against characters in chatter */
3010 started = STARTED_NONE;
3011 if(suppressKibitz) next_out = i+1;
3014 /* Kludge to deal with rcmd protocol */
3015 if (firstTime && looking_at(buf, &i, "\001*")) {
3016 DisplayFatalError(&buf[1], 0, 1);
3022 if (!loggedOn && looking_at(buf, &i, "chessclub.com")) {
3025 if (appData.debugMode)
3026 fprintf(debugFP, "ics_type %d\n", ics_type);
3029 if (!loggedOn && looking_at(buf, &i, "freechess.org")) {
3030 ics_type = ICS_FICS;
3032 if (appData.debugMode)
3033 fprintf(debugFP, "ics_type %d\n", ics_type);
3036 if (!loggedOn && looking_at(buf, &i, "chess.net")) {
3037 ics_type = ICS_CHESSNET;
3039 if (appData.debugMode)
3040 fprintf(debugFP, "ics_type %d\n", ics_type);
3045 (looking_at(buf, &i, "\"*\" is *a registered name") ||
3046 looking_at(buf, &i, "Logging you in as \"*\"") ||
3047 looking_at(buf, &i, "will be \"*\""))) {
3048 safeStrCpy(ics_handle, star_match[0], sizeof(ics_handle)/sizeof(ics_handle[0]));
3052 if (loggedOn && !have_set_title && ics_handle[0] != NULLCHAR) {
3054 snprintf(buf, sizeof(buf), "%s@%s", ics_handle, appData.icsHost);
3055 DisplayIcsInteractionTitle(buf);
3056 have_set_title = TRUE;
3059 /* skip finger notes */
3060 if (started == STARTED_NONE &&
3061 ((buf[i] == ' ' && isdigit(buf[i+1])) ||
3062 (buf[i] == '1' && buf[i+1] == '0')) &&
3063 buf[i+2] == ':' && buf[i+3] == ' ') {
3064 started = STARTED_CHATTER;
3070 // [HGM] seekgraph: recognize sought lines and end-of-sought message
3071 if(appData.seekGraph) {
3072 if(soughtPending && MatchSoughtLine(buf+i)) {
3073 i = strstr(buf+i, "rated") - buf;
3074 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3075 next_out = leftover_start = i;
3076 started = STARTED_CHATTER;
3077 suppressKibitz = TRUE;
3080 if((gameMode == IcsIdle || gameMode == BeginningOfGame)
3081 && looking_at(buf, &i, "* ads displayed")) {
3082 soughtPending = FALSE;
3087 if(appData.autoRefresh) {
3088 if(looking_at(buf, &i, "* (*) seeking * * * * *\"play *\" to respond)\n")) {
3089 int s = (ics_type == ICS_ICC); // ICC format differs
3091 AddAd(star_match[0], star_match[1], atoi(star_match[2+s]), atoi(star_match[3+s]),
3092 star_match[4+s][0], star_match[5-3*s], atoi(star_match[7]), TRUE);
3093 looking_at(buf, &i, "*% "); // eat prompt
3094 if(oldi > 0 && buf[oldi-1] == '\n') oldi--; // suppress preceding LF, if any
3095 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3096 next_out = i; // suppress
3099 if(looking_at(buf, &i, "\nAds removed: *\n") || looking_at(buf, &i, "\031(51 * *\031)")) {
3100 char *p = star_match[0];
3102 if(seekGraphUp) RemoveSeekAd(atoi(p));
3103 while(*p && *p++ != ' '); // next
3105 looking_at(buf, &i, "*% "); // eat prompt
3106 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3113 /* skip formula vars */
3114 if (started == STARTED_NONE &&
3115 buf[i] == 'f' && isdigit(buf[i+1]) && buf[i+2] == ':') {
3116 started = STARTED_CHATTER;
3121 // [HGM] kibitz: try to recognize opponent engine-score kibitzes, to divert them to engine-output window
3122 if (appData.autoKibitz && started == STARTED_NONE &&
3123 !appData.icsEngineAnalyze && // [HGM] [DM] ICS analyze
3124 (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack || gameMode == IcsObserving)) {
3125 if((looking_at(buf, &i, "\n* kibitzes: ") || looking_at(buf, &i, "\n* whispers: ") ||
3126 looking_at(buf, &i, "* kibitzes: ") || looking_at(buf, &i, "* whispers: ")) &&
3127 (StrStr(star_match[0], gameInfo.white) == star_match[0] ||
3128 StrStr(star_match[0], gameInfo.black) == star_match[0] )) { // kibitz of self or opponent
3129 suppressKibitz = TRUE;
3130 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3132 if((StrStr(star_match[0], gameInfo.white) == star_match[0]
3133 && (gameMode == IcsPlayingWhite)) ||
3134 (StrStr(star_match[0], gameInfo.black) == star_match[0]
3135 && (gameMode == IcsPlayingBlack)) ) // opponent kibitz
3136 started = STARTED_CHATTER; // own kibitz we simply discard
3138 started = STARTED_COMMENT; // make sure it will be collected in parse[]
3139 parse_pos = 0; parse[0] = NULLCHAR;
3140 savingComment = TRUE;
3141 suppressKibitz = gameMode != IcsObserving ? 2 :
3142 (StrStr(star_match[0], gameInfo.white) == NULL) + 1;
3146 if((looking_at(buf, &i, "\nkibitzed to *\n") || looking_at(buf, &i, "kibitzed to *\n") ||
3147 looking_at(buf, &i, "\n(kibitzed to *\n") || looking_at(buf, &i, "(kibitzed to *\n"))
3148 && atoi(star_match[0])) {
3149 // suppress the acknowledgements of our own autoKibitz
3151 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3152 if(p = strchr(star_match[0], ' ')) p[1] = NULLCHAR; // clip off "players)" on FICS
3153 SendToPlayer(star_match[0], strlen(star_match[0]));
3154 if(looking_at(buf, &i, "*% ")) // eat prompt
3155 suppressKibitz = FALSE;
3159 } // [HGM] kibitz: end of patch
3161 if(looking_at(buf, &i, "* rating adjustment: * --> *\n")) continue;
3163 // [HGM] chat: intercept tells by users for which we have an open chat window
3165 if(started == STARTED_NONE && (looking_at(buf, &i, "* tells you:") || looking_at(buf, &i, "* says:") ||
3166 looking_at(buf, &i, "* whispers:") ||
3167 looking_at(buf, &i, "* kibitzes:") ||
3168 looking_at(buf, &i, "* shouts:") ||
3169 looking_at(buf, &i, "* c-shouts:") ||
3170 looking_at(buf, &i, "--> * ") ||
3171 looking_at(buf, &i, "*(*):") && (sscanf(star_match[1], "%d", &channel),1) ||
3172 looking_at(buf, &i, "*(*)(*):") && (sscanf(star_match[2], "%d", &channel),1) ||
3173 looking_at(buf, &i, "*(*)(*)(*):") && (sscanf(star_match[3], "%d", &channel),1) ||
3174 looking_at(buf, &i, "*(*)(*)(*)(*):") && sscanf(star_match[4], "%d", &channel) == 1 )) {
3176 sscanf(star_match[0], "%[^(]", talker+1); // strip (C) or (U) off ICS handle
3177 chattingPartner = -1;
3179 if(channel >= 0) // channel broadcast; look if there is a chatbox for this channel
3180 for(p=0; p<MAX_CHAT; p++) {
3181 if(chatPartner[p][0] >= '0' && chatPartner[p][0] <= '9' && channel == atoi(chatPartner[p])) {
3182 talker[0] = '['; strcat(talker, "] ");
3183 Colorize(channel == 1 ? ColorChannel1 : ColorChannel, FALSE);
3184 chattingPartner = p; break;
3187 if(buf[i-3] == 'e') // kibitz; look if there is a KIBITZ chatbox
3188 for(p=0; p<MAX_CHAT; p++) {
3189 if(!strcmp("kibitzes", chatPartner[p])) {
3190 talker[0] = '['; strcat(talker, "] ");
3191 chattingPartner = p; break;
3194 if(buf[i-3] == 'r') // whisper; look if there is a WHISPER chatbox
3195 for(p=0; p<MAX_CHAT; p++) {
3196 if(!strcmp("whispers", chatPartner[p])) {
3197 talker[0] = '['; strcat(talker, "] ");
3198 chattingPartner = p; break;
3201 if(buf[i-3] == 't' || buf[oldi+2] == '>') {// shout, c-shout or it; look if there is a 'shouts' chatbox
3202 if(buf[i-8] == '-' && buf[i-3] == 't')
3203 for(p=0; p<MAX_CHAT; p++) { // c-shout; check if dedicatesd c-shout box exists
3204 if(!strcmp("c-shouts", chatPartner[p])) {
3205 talker[0] = '('; strcat(talker, ") "); Colorize(ColorSShout, FALSE);
3206 chattingPartner = p; break;
3209 if(chattingPartner < 0)
3210 for(p=0; p<MAX_CHAT; p++) {
3211 if(!strcmp("shouts", chatPartner[p])) {
3212 if(buf[oldi+2] == '>') { talker[0] = '<'; strcat(talker, "> "); Colorize(ColorShout, FALSE); }
3213 else if(buf[i-8] == '-') { talker[0] = '('; strcat(talker, ") "); Colorize(ColorSShout, FALSE); }
3214 else { talker[0] = '['; strcat(talker, "] "); Colorize(ColorShout, FALSE); }
3215 chattingPartner = p; break;
3219 if(chattingPartner<0) // if not, look if there is a chatbox for this indivdual
3220 for(p=0; p<MAX_CHAT; p++) if(!StrCaseCmp(talker+1, chatPartner[p])) {
3221 talker[0] = 0; Colorize(ColorTell, FALSE);
3222 chattingPartner = p; break;
3224 if(chattingPartner<0) i = oldi; else {
3225 Colorize(curColor, TRUE); // undo the bogus colorations we just made to trigger the souds
3226 if(oldi > 0 && buf[oldi-1] == '\n') oldi--;
3227 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3228 started = STARTED_COMMENT;
3229 parse_pos = 0; parse[0] = NULLCHAR;
3230 savingComment = 3 + chattingPartner; // counts as TRUE
3231 suppressKibitz = TRUE;
3234 } // [HGM] chat: end of patch
3237 if (appData.zippyTalk || appData.zippyPlay) {
3238 /* [DM] Backup address for color zippy lines */
3240 if (loggedOn == TRUE)
3241 if (ZippyControl(buf, &backup) || ZippyConverse(buf, &backup) ||
3242 (appData.zippyPlay && ZippyMatch(buf, &backup)));
3244 } // [DM] 'else { ' deleted
3246 /* Regular tells and says */
3247 (tkind = 1, looking_at(buf, &i, "* tells you: ")) ||
3248 looking_at(buf, &i, "* (your partner) tells you: ") ||
3249 looking_at(buf, &i, "* says: ") ||
3250 /* Don't color "message" or "messages" output */
3251 (tkind = 5, looking_at(buf, &i, "*. * (*:*): ")) ||
3252 looking_at(buf, &i, "*. * at *:*: ") ||
3253 looking_at(buf, &i, "--* (*:*): ") ||
3254 /* Message notifications (same color as tells) */
3255 looking_at(buf, &i, "* has left a message ") ||
3256 looking_at(buf, &i, "* just sent you a message:\n") ||
3257 /* Whispers and kibitzes */
3258 (tkind = 2, looking_at(buf, &i, "* whispers: ")) ||
3259 looking_at(buf, &i, "* kibitzes: ") ||
3261 (tkind = 3, looking_at(buf, &i, "*(*: "))) {
3263 if (tkind == 1 && strchr(star_match[0], ':')) {
3264 /* Avoid "tells you:" spoofs in channels */
3267 if (star_match[0][0] == NULLCHAR ||
3268 strchr(star_match[0], ' ') ||
3269 (tkind == 3 && strchr(star_match[1], ' '))) {
3270 /* Reject bogus matches */
3273 if (appData.colorize) {
3274 if (oldi > next_out) {
3275 SendToPlayer(&buf[next_out], oldi - next_out);
3280 Colorize(ColorTell, FALSE);
3281 curColor = ColorTell;
3284 Colorize(ColorKibitz, FALSE);
3285 curColor = ColorKibitz;
3288 p = strrchr(star_match[1], '(');
3295 Colorize(ColorChannel1, FALSE);
3296 curColor = ColorChannel1;
3298 Colorize(ColorChannel, FALSE);
3299 curColor = ColorChannel;
3303 curColor = ColorNormal;
3307 if (started == STARTED_NONE && appData.autoComment &&
3308 (gameMode == IcsObserving ||
3309 gameMode == IcsPlayingWhite ||
3310 gameMode == IcsPlayingBlack)) {
3311 parse_pos = i - oldi;
3312 memcpy(parse, &buf[oldi], parse_pos);
3313 parse[parse_pos] = NULLCHAR;
3314 started = STARTED_COMMENT;
3315 savingComment = TRUE;
3317 started = STARTED_CHATTER;
3318 savingComment = FALSE;
3325 if (looking_at(buf, &i, "* s-shouts: ") ||
3326 looking_at(buf, &i, "* c-shouts: ")) {
3327 if (appData.colorize) {
3328 if (oldi > next_out) {
3329 SendToPlayer(&buf[next_out], oldi - next_out);
3332 Colorize(ColorSShout, FALSE);
3333 curColor = ColorSShout;
3336 started = STARTED_CHATTER;
3340 if (looking_at(buf, &i, "--->")) {
3345 if (looking_at(buf, &i, "* shouts: ") ||
3346 looking_at(buf, &i, "--> ")) {
3347 if (appData.colorize) {
3348 if (oldi > next_out) {
3349 SendToPlayer(&buf[next_out], oldi - next_out);
3352 Colorize(ColorShout, FALSE);
3353 curColor = ColorShout;
3356 started = STARTED_CHATTER;
3360 if (looking_at( buf, &i, "Challenge:")) {
3361 if (appData.colorize) {
3362 if (oldi > next_out) {
3363 SendToPlayer(&buf[next_out], oldi - next_out);
3366 Colorize(ColorChallenge, FALSE);
3367 curColor = ColorChallenge;
3373 if (looking_at(buf, &i, "* offers you") ||
3374 looking_at(buf, &i, "* offers to be") ||
3375 looking_at(buf, &i, "* would like to") ||
3376 looking_at(buf, &i, "* requests to") ||
3377 looking_at(buf, &i, "Your opponent offers") ||
3378 looking_at(buf, &i, "Your opponent requests")) {
3380 if (appData.colorize) {
3381 if (oldi > next_out) {
3382 SendToPlayer(&buf[next_out], oldi - next_out);
3385 Colorize(ColorRequest, FALSE);
3386 curColor = ColorRequest;
3391 if (looking_at(buf, &i, "* (*) seeking")) {
3392 if (appData.colorize) {
3393 if (oldi > next_out) {
3394 SendToPlayer(&buf[next_out], oldi - next_out);
3397 Colorize(ColorSeek, FALSE);
3398 curColor = ColorSeek;
3403 if(i < backup) { i = backup; continue; } // [HGM] for if ZippyControl matches, but the colorie code doesn't
3405 if (looking_at(buf, &i, "\\ ")) {
3406 if (prevColor != ColorNormal) {
3407 if (oldi > next_out) {
3408 SendToPlayer(&buf[next_out], oldi - next_out);
3411 Colorize(prevColor, TRUE);
3412 curColor = prevColor;
3414 if (savingComment) {
3415 parse_pos = i - oldi;
3416 memcpy(parse, &buf[oldi], parse_pos);
3417 parse[parse_pos] = NULLCHAR;
3418 started = STARTED_COMMENT;
3419 if(savingComment >= 3) // [HGM] chat: continuation of line for chat box
3420 chattingPartner = savingComment - 3; // kludge to remember the box
3422 started = STARTED_CHATTER;
3427 if (looking_at(buf, &i, "Black Strength :") ||
3428 looking_at(buf, &i, "<<< style 10 board >>>") ||
3429 looking_at(buf, &i, "<10>") ||
3430 looking_at(buf, &i, "#@#")) {
3431 /* Wrong board style */
3433 SendToICS(ics_prefix);
3434 SendToICS("set style 12\n");
3435 SendToICS(ics_prefix);
3436 SendToICS("refresh\n");
3440 if (!have_sent_ICS_logon && looking_at(buf, &i, "login:")) {
3442 have_sent_ICS_logon = 1;
3446 if (ics_getting_history != H_GETTING_MOVES /*smpos kludge*/ &&
3447 (looking_at(buf, &i, "\n<12> ") ||
3448 looking_at(buf, &i, "<12> "))) {
3450 if (oldi > next_out) {
3451 SendToPlayer(&buf[next_out], oldi - next_out);
3454 started = STARTED_BOARD;
3459 if ((started == STARTED_NONE && looking_at(buf, &i, "\n<b1> ")) ||
3460 looking_at(buf, &i, "<b1> ")) {
3461 if (oldi > next_out) {
3462 SendToPlayer(&buf[next_out], oldi - next_out);
3465 started = STARTED_HOLDINGS;
3470 if (looking_at(buf, &i, "* *vs. * *--- *")) {
3472 /* Header for a move list -- first line */
3474 switch (ics_getting_history) {
3478 case BeginningOfGame:
3479 /* User typed "moves" or "oldmoves" while we
3480 were idle. Pretend we asked for these
3481 moves and soak them up so user can step
3482 through them and/or save them.
3485 gameMode = IcsObserving;
3488 ics_getting_history = H_GOT_UNREQ_HEADER;
3490 case EditGame: /*?*/
3491 case EditPosition: /*?*/
3492 /* Should above feature work in these modes too? */
3493 /* For now it doesn't */
3494 ics_getting_history = H_GOT_UNWANTED_HEADER;
3497 ics_getting_history = H_GOT_UNWANTED_HEADER;
3502 /* Is this the right one? */
3503 if (gameInfo.white && gameInfo.black &&
3504 strcmp(gameInfo.white, star_match[0]) == 0 &&
3505 strcmp(gameInfo.black, star_match[2]) == 0) {
3507 ics_getting_history = H_GOT_REQ_HEADER;
3510 case H_GOT_REQ_HEADER:
3511 case H_GOT_UNREQ_HEADER:
3512 case H_GOT_UNWANTED_HEADER:
3513 case H_GETTING_MOVES:
3514 /* Should not happen */
3515 DisplayError(_("Error gathering move list: two headers"), 0);
3516 ics_getting_history = H_FALSE;
3520 /* Save player ratings into gameInfo if needed */
3521 if ((ics_getting_history == H_GOT_REQ_HEADER ||
3522 ics_getting_history == H_GOT_UNREQ_HEADER) &&
3523 (gameInfo.whiteRating == -1 ||
3524 gameInfo.blackRating == -1)) {
3526 gameInfo.whiteRating = string_to_rating(star_match[1]);
3527 gameInfo.blackRating = string_to_rating(star_match[3]);
3528 if (appData.debugMode)
3529 fprintf(debugFP, _("Ratings from header: W %d, B %d\n"),
3530 gameInfo.whiteRating, gameInfo.blackRating);
3535 if (looking_at(buf, &i,
3536 "* * match, initial time: * minute*, increment: * second")) {
3537 /* Header for a move list -- second line */
3538 /* Initial board will follow if this is a wild game */
3539 if (gameInfo.event != NULL) free(gameInfo.event);
3540 snprintf(str, MSG_SIZ, "ICS %s %s match", star_match[0], star_match[1]);
3541 gameInfo.event = StrSave(str);
3542 /* [HGM] we switched variant. Translate boards if needed. */
3543 VariantSwitch(boards[currentMove], StringToVariant(gameInfo.event));
3547 if (looking_at(buf, &i, "Move ")) {
3548 /* Beginning of a move list */
3549 switch (ics_getting_history) {
3551 /* Normally should not happen */
3552 /* Maybe user hit reset while we were parsing */
3555 /* Happens if we are ignoring a move list that is not
3556 * the one we just requested. Common if the user
3557 * tries to observe two games without turning off
3560 case H_GETTING_MOVES:
3561 /* Should not happen */
3562 DisplayError(_("Error gathering move list: nested"), 0);
3563 ics_getting_history = H_FALSE;
3565 case H_GOT_REQ_HEADER:
3566 ics_getting_history = H_GETTING_MOVES;
3567 started = STARTED_MOVES;
3569 if (oldi > next_out) {
3570 SendToPlayer(&buf[next_out], oldi - next_out);
3573 case H_GOT_UNREQ_HEADER:
3574 ics_getting_history = H_GETTING_MOVES;
3575 started = STARTED_MOVES_NOHIDE;
3578 case H_GOT_UNWANTED_HEADER:
3579 ics_getting_history = H_FALSE;
3585 if (looking_at(buf, &i, "% ") ||
3586 ((started == STARTED_MOVES || started == STARTED_MOVES_NOHIDE)
3587 && looking_at(buf, &i, "}*"))) { char *bookHit = NULL; // [HGM] book
3588 if(soughtPending && nrOfSeekAds) { // [HGM] seekgraph: on ICC sought-list has no termination line
3589 soughtPending = FALSE;
3593 if(suppressKibitz) next_out = i;
3594 savingComment = FALSE;
3598 case STARTED_MOVES_NOHIDE:
3599 memcpy(&parse[parse_pos], &buf[oldi], i - oldi);
3600 parse[parse_pos + i - oldi] = NULLCHAR;
3601 ParseGameHistory(parse);
3603 if (appData.zippyPlay && first.initDone) {
3604 FeedMovesToProgram(&first, forwardMostMove);
3605 if (gameMode == IcsPlayingWhite) {
3606 if (WhiteOnMove(forwardMostMove)) {
3607 if (first.sendTime) {
3608 if (first.useColors) {
3609 SendToProgram("black\n", &first);
3611 SendTimeRemaining(&first, TRUE);
3613 if (first.useColors) {
3614 SendToProgram("white\n", &first); // [HGM] book: made sending of "go\n" book dependent
3616 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: probe book for initial pos
3617 first.maybeThinking = TRUE;
3619 if (first.usePlayother) {
3620 if (first.sendTime) {
3621 SendTimeRemaining(&first, TRUE);
3623 SendToProgram("playother\n", &first);
3629 } else if (gameMode == IcsPlayingBlack) {
3630 if (!WhiteOnMove(forwardMostMove)) {
3631 if (first.sendTime) {
3632 if (first.useColors) {
3633 SendToProgram("white\n", &first);
3635 SendTimeRemaining(&first, FALSE);
3637 if (first.useColors) {
3638 SendToProgram("black\n", &first);
3640 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE);
3641 first.maybeThinking = TRUE;
3643 if (first.usePlayother) {
3644 if (first.sendTime) {
3645 SendTimeRemaining(&first, FALSE);
3647 SendToProgram("playother\n", &first);
3656 if (gameMode == IcsObserving && ics_gamenum == -1) {
3657 /* Moves came from oldmoves or moves command
3658 while we weren't doing anything else.
3660 currentMove = forwardMostMove;
3661 ClearHighlights();/*!!could figure this out*/
3662 flipView = appData.flipView;
3663 DrawPosition(TRUE, boards[currentMove]);
3664 DisplayBothClocks();
3665 snprintf(str, MSG_SIZ, "%s %s %s",
3666 gameInfo.white, _("vs."), gameInfo.black);
3670 /* Moves were history of an active game */
3671 if (gameInfo.resultDetails != NULL) {
3672 free(gameInfo.resultDetails);
3673 gameInfo.resultDetails = NULL;
3676 HistorySet(parseList, backwardMostMove,
3677 forwardMostMove, currentMove-1);
3678 DisplayMove(currentMove - 1);
3679 if (started == STARTED_MOVES) next_out = i;
3680 started = STARTED_NONE;
3681 ics_getting_history = H_FALSE;
3684 case STARTED_OBSERVE:
3685 started = STARTED_NONE;
3686 SendToICS(ics_prefix);
3687 SendToICS("refresh\n");
3693 if(bookHit) { // [HGM] book: simulate book reply
3694 static char bookMove[MSG_SIZ]; // a bit generous?
3696 programStats.nodes = programStats.depth = programStats.time =
3697 programStats.score = programStats.got_only_move = 0;
3698 sprintf(programStats.movelist, "%s (xbook)", bookHit);
3700 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
3701 strcat(bookMove, bookHit);
3702 HandleMachineMove(bookMove, &first);
3707 if ((started == STARTED_MOVES || started == STARTED_BOARD ||
3708 started == STARTED_HOLDINGS ||
3709 started == STARTED_MOVES_NOHIDE) && i >= leftover_len) {
3710 /* Accumulate characters in move list or board */
3711 parse[parse_pos++] = buf[i];
3714 /* Start of game messages. Mostly we detect start of game
3715 when the first board image arrives. On some versions
3716 of the ICS, though, we need to do a "refresh" after starting
3717 to observe in order to get the current board right away. */
3718 if (looking_at(buf, &i, "Adding game * to observation list")) {
3719 started = STARTED_OBSERVE;
3723 /* Handle auto-observe */
3724 if (appData.autoObserve &&
3725 (gameMode == IcsIdle || gameMode == BeginningOfGame) &&
3726 looking_at(buf, &i, "Game notification: * (*) vs. * (*)")) {
3728 /* Choose the player that was highlighted, if any. */
3729 if (star_match[0][0] == '\033' ||
3730 star_match[1][0] != '\033') {
3731 player = star_match[0];
3733 player = star_match[2];
3735 snprintf(str, MSG_SIZ, "%sobserve %s\n",
3736 ics_prefix, StripHighlightAndTitle(player));
3739 /* Save ratings from notify string */
3740 safeStrCpy(player1Name, star_match[0], sizeof(player1Name)/sizeof(player1Name[0]));
3741 player1Rating = string_to_rating(star_match[1]);
3742 safeStrCpy(player2Name, star_match[2], sizeof(player2Name)/sizeof(player2Name[0]));
3743 player2Rating = string_to_rating(star_match[3]);
3745 if (appData.debugMode)
3747 "Ratings from 'Game notification:' %s %d, %s %d\n",
3748 player1Name, player1Rating,
3749 player2Name, player2Rating);
3754 /* Deal with automatic examine mode after a game,
3755 and with IcsObserving -> IcsExamining transition */
3756 if (looking_at(buf, &i, "Entering examine mode for game *") ||
3757 looking_at(buf, &i, "has made you an examiner of game *")) {
3759 int gamenum = atoi(star_match[0]);
3760 if ((gameMode == IcsIdle || gameMode == IcsObserving) &&
3761 gamenum == ics_gamenum) {
3762 /* We were already playing or observing this game;
3763 no need to refetch history */
3764 gameMode = IcsExamining;
3766 pauseExamForwardMostMove = forwardMostMove;
3767 } else if (currentMove < forwardMostMove) {
3768 ForwardInner(forwardMostMove);
3771 /* I don't think this case really can happen */
3772 SendToICS(ics_prefix);
3773 SendToICS("refresh\n");
3778 /* Error messages */
3779 // if (ics_user_moved) {
3780 if (1) { // [HGM] old way ignored error after move type in; ics_user_moved is not set then!
3781 if (looking_at(buf, &i, "Illegal move") ||
3782 looking_at(buf, &i, "Not a legal move") ||
3783 looking_at(buf, &i, "Your king is in check") ||
3784 looking_at(buf, &i, "It isn't your turn") ||
3785 looking_at(buf, &i, "It is not your move")) {
3787 if (ics_user_moved && forwardMostMove > backwardMostMove) { // only backup if we already moved
3788 currentMove = forwardMostMove-1;
3789 DisplayMove(currentMove - 1); /* before DMError */
3790 DrawPosition(FALSE, boards[currentMove]);
3791 SwitchClocks(forwardMostMove-1); // [HGM] race
3792 DisplayBothClocks();
3794 DisplayMoveError(_("Illegal move (rejected by ICS)")); // [HGM] but always relay error msg
3800 if (looking_at(buf, &i, "still have time") ||
3801 looking_at(buf, &i, "not out of time") ||
3802 looking_at(buf, &i, "either player is out of time") ||
3803 looking_at(buf, &i, "has timeseal; checking")) {
3804 /* We must have called his flag a little too soon */
3805 whiteFlag = blackFlag = FALSE;
3809 if (looking_at(buf, &i, "added * seconds to") ||
3810 looking_at(buf, &i, "seconds were added to")) {
3811 /* Update the clocks */
3812 SendToICS(ics_prefix);
3813 SendToICS("refresh\n");
3817 if (!ics_clock_paused && looking_at(buf, &i, "clock paused")) {
3818 ics_clock_paused = TRUE;
3823 if (ics_clock_paused && looking_at(buf, &i, "clock resumed")) {
3824 ics_clock_paused = FALSE;
3829 /* Grab player ratings from the Creating: message.
3830 Note we have to check for the special case when
3831 the ICS inserts things like [white] or [black]. */
3832 if (looking_at(buf, &i, "Creating: * (*)* * (*)") ||
3833 looking_at(buf, &i, "Creating: * (*) [*] * (*)")) {
3835 0 player 1 name (not necessarily white)
3837 2 empty, white, or black (IGNORED)
3838 3 player 2 name (not necessarily black)
3841 The names/ratings are sorted out when the game
3842 actually starts (below).
3844 safeStrCpy(player1Name, StripHighlightAndTitle(star_match[0]), sizeof(player1Name)/sizeof(player1Name[0]));
3845 player1Rating = string_to_rating(star_match[1]);
3846 safeStrCpy(player2Name, StripHighlightAndTitle(star_match[3]), sizeof(player2Name)/sizeof(player2Name[0]));
3847 player2Rating = string_to_rating(star_match[4]);
3849 if (appData.debugMode)
3851 "Ratings from 'Creating:' %s %d, %s %d\n",
3852 player1Name, player1Rating,
3853 player2Name, player2Rating);
3858 /* Improved generic start/end-of-game messages */
3859 if ((tkind=0, looking_at(buf, &i, "{Game * (* vs. *) *}*")) ||
3860 (tkind=1, looking_at(buf, &i, "{Game * (*(*) vs. *(*)) *}*"))){
3861 /* If tkind == 0: */
3862 /* star_match[0] is the game number */
3863 /* [1] is the white player's name */
3864 /* [2] is the black player's name */
3865 /* For end-of-game: */
3866 /* [3] is the reason for the game end */
3867 /* [4] is a PGN end game-token, preceded by " " */
3868 /* For start-of-game: */
3869 /* [3] begins with "Creating" or "Continuing" */
3870 /* [4] is " *" or empty (don't care). */
3871 int gamenum = atoi(star_match[0]);
3872 char *whitename, *blackname, *why, *endtoken;
3873 ChessMove endtype = EndOfFile;
3876 whitename = star_match[1];
3877 blackname = star_match[2];
3878 why = star_match[3];
3879 endtoken = star_match[4];
3881 whitename = star_match[1];
3882 blackname = star_match[3];
3883 why = star_match[5];
3884 endtoken = star_match[6];
3887 /* Game start messages */
3888 if (strncmp(why, "Creating ", 9) == 0 ||
3889 strncmp(why, "Continuing ", 11) == 0) {
3890 gs_gamenum = gamenum;
3891 safeStrCpy(gs_kind, strchr(why, ' ') + 1,sizeof(gs_kind)/sizeof(gs_kind[0]));
3892 if(ics_gamenum == -1) // [HGM] only if we are not already involved in a game (because gin=1 sends us such messages)
3893 VariantSwitch(boards[currentMove], StringToVariant(gs_kind)); // [HGM] variantswitch: even before we get first board
3895 if (appData.zippyPlay) {
3896 ZippyGameStart(whitename, blackname);
3899 partnerBoardValid = FALSE; // [HGM] bughouse
3903 /* Game end messages */
3904 if (gameMode == IcsIdle || gameMode == BeginningOfGame ||
3905 ics_gamenum != gamenum) {
3908 while (endtoken[0] == ' ') endtoken++;
3909 switch (endtoken[0]) {
3912 endtype = GameUnfinished;
3915 endtype = BlackWins;
3918 if (endtoken[1] == '/')
3919 endtype = GameIsDrawn;
3921 endtype = WhiteWins;
3924 GameEnds(endtype, why, GE_ICS);
3926 if (appData.zippyPlay && first.initDone) {
3927 ZippyGameEnd(endtype, why);
3928 if (first.pr == NoProc) {
3929 /* Start the next process early so that we'll
3930 be ready for the next challenge */
3931 StartChessProgram(&first);
3933 /* Send "new" early, in case this command takes
3934 a long time to finish, so that we'll be ready
3935 for the next challenge. */
3936 gameInfo.variant = VariantNormal; // [HGM] variantswitch: suppress sending of 'variant'
3940 if(appData.bgObserve && partnerBoardValid) DrawPosition(TRUE, partnerBoard);
3944 if (looking_at(buf, &i, "Removing game * from observation") ||
3945 looking_at(buf, &i, "no longer observing game *") ||
3946 looking_at(buf, &i, "Game * (*) has no examiners")) {
3947 if (gameMode == IcsObserving &&
3948 atoi(star_match[0]) == ics_gamenum)
3950 /* icsEngineAnalyze */
3951 if (appData.icsEngineAnalyze) {
3958 ics_user_moved = FALSE;
3963 if (looking_at(buf, &i, "no longer examining game *")) {
3964 if (gameMode == IcsExamining &&
3965 atoi(star_match[0]) == ics_gamenum)
3969 ics_user_moved = FALSE;
3974 /* Advance leftover_start past any newlines we find,
3975 so only partial lines can get reparsed */
3976 if (looking_at(buf, &i, "\n")) {
3977 prevColor = curColor;
3978 if (curColor != ColorNormal) {
3979 if (oldi > next_out) {
3980 SendToPlayer(&buf[next_out], oldi - next_out);
3983 Colorize(ColorNormal, FALSE);
3984 curColor = ColorNormal;
3986 if (started == STARTED_BOARD) {
3987 started = STARTED_NONE;
3988 parse[parse_pos] = NULLCHAR;
3989 ParseBoard12(parse);
3992 /* Send premove here */
3993 if (appData.premove) {
3995 if (currentMove == 0 &&
3996 gameMode == IcsPlayingWhite &&
3997 appData.premoveWhite) {
3998 snprintf(str, MSG_SIZ, "%s\n", appData.premoveWhiteText);
3999 if (appData.debugMode)
4000 fprintf(debugFP, "Sending premove:\n");
4002 } else if (currentMove == 1 &&
4003 gameMode == IcsPlayingBlack &&
4004 appData.premoveBlack) {
4005 snprintf(str, MSG_SIZ, "%s\n", appData.premoveBlackText);
4006 if (appData.debugMode)
4007 fprintf(debugFP, "Sending premove:\n");
4009 } else if (gotPremove) {
4011 ClearPremoveHighlights();
4012 if (appData.debugMode)
4013 fprintf(debugFP, "Sending premove:\n");
4014 UserMoveEvent(premoveFromX, premoveFromY,
4015 premoveToX, premoveToY,
4020 /* Usually suppress following prompt */
4021 if (!(forwardMostMove == 0 && gameMode == IcsExamining)) {
4022 while(looking_at(buf, &i, "\n")); // [HGM] skip empty lines
4023 if (looking_at(buf, &i, "*% ")) {
4024 savingComment = FALSE;
4029 } else if (started == STARTED_HOLDINGS) {
4031 char new_piece[MSG_SIZ];
4032 started = STARTED_NONE;
4033 parse[parse_pos] = NULLCHAR;
4034 if (appData.debugMode)
4035 fprintf(debugFP, "Parsing holdings: %s, currentMove = %d\n",
4036 parse, currentMove);
4037 if (sscanf(parse, " game %d", &gamenum) == 1) {
4038 if(gamenum == ics_gamenum) { // [HGM] bughouse: old code if part of foreground game
4039 if (gameInfo.variant == VariantNormal) {
4040 /* [HGM] We seem to switch variant during a game!
4041 * Presumably no holdings were displayed, so we have
4042 * to move the position two files to the right to
4043 * create room for them!
4045 VariantClass newVariant;
4046 switch(gameInfo.boardWidth) { // base guess on board width
4047 case 9: newVariant = VariantShogi; break;
4048 case 10: newVariant = VariantGreat; break;
4049 default: newVariant = VariantCrazyhouse; break;
4051 VariantSwitch(boards[currentMove], newVariant); /* temp guess */
4052 /* Get a move list just to see the header, which
4053 will tell us whether this is really bug or zh */
4054 if (ics_getting_history == H_FALSE) {
4055 ics_getting_history = H_REQUESTED;
4056 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4060 new_piece[0] = NULLCHAR;
4061 sscanf(parse, "game %d white [%s black [%s <- %s",
4062 &gamenum, white_holding, black_holding,
4064 white_holding[strlen(white_holding)-1] = NULLCHAR;
4065 black_holding[strlen(black_holding)-1] = NULLCHAR;
4066 /* [HGM] copy holdings to board holdings area */
4067 CopyHoldings(boards[forwardMostMove], white_holding, WhitePawn);
4068 CopyHoldings(boards[forwardMostMove], black_holding, BlackPawn);
4069 boards[forwardMostMove][HOLDINGS_SET] = 1; // flag holdings as set
4071 if (appData.zippyPlay && first.initDone) {
4072 ZippyHoldings(white_holding, black_holding,
4076 if (tinyLayout || smallLayout) {
4077 char wh[16], bh[16];
4078 PackHolding(wh, white_holding);
4079 PackHolding(bh, black_holding);
4080 snprintf(str, MSG_SIZ, "[%s-%s] %s-%s", wh, bh,
4081 gameInfo.white, gameInfo.black);
4083 snprintf(str, MSG_SIZ, "%s [%s] %s %s [%s]",
4084 gameInfo.white, white_holding, _("vs."),
4085 gameInfo.black, black_holding);
4087 if(!partnerUp) // [HGM] bughouse: when peeking at partner game we already know what he captured...
4088 DrawPosition(FALSE, boards[currentMove]);
4090 } else if(appData.bgObserve) { // [HGM] bughouse: holdings of other game => background
4091 sscanf(parse, "game %d white [%s black [%s <- %s",
4092 &gamenum, white_holding, black_holding,
4094 white_holding[strlen(white_holding)-1] = NULLCHAR;
4095 black_holding[strlen(black_holding)-1] = NULLCHAR;
4096 /* [HGM] copy holdings to partner-board holdings area */
4097 CopyHoldings(partnerBoard, white_holding, WhitePawn);
4098 CopyHoldings(partnerBoard, black_holding, BlackPawn);
4099 if(twoBoards) { partnerUp = 1; flipView = !flipView; } // [HGM] dual: always draw
4100 if(partnerUp) DrawPosition(FALSE, partnerBoard);
4101 if(twoBoards) { partnerUp = 0; flipView = !flipView; }
4104 /* Suppress following prompt */
4105 if (looking_at(buf, &i, "*% ")) {
4106 if(strchr(star_match[0], 7)) SendToPlayer("\007", 1); // Bell(); // FICS fuses bell for next board with prompt in zh captures
4107 savingComment = FALSE;
4115 i++; /* skip unparsed character and loop back */
4118 if (started != STARTED_MOVES && started != STARTED_BOARD && !suppressKibitz && // [HGM] kibitz
4119 // started != STARTED_HOLDINGS && i > next_out) { // [HGM] should we compare to leftover_start in stead of i?
4120 // SendToPlayer(&buf[next_out], i - next_out);
4121 started != STARTED_HOLDINGS && leftover_start > next_out) {
4122 SendToPlayer(&buf[next_out], leftover_start - next_out);
4126 leftover_len = buf_len - leftover_start;
4127 /* if buffer ends with something we couldn't parse,
4128 reparse it after appending the next read */
4130 } else if (count == 0) {
4131 RemoveInputSource(isr);
4132 DisplayFatalError(_("Connection closed by ICS"), 0, 0);
4134 DisplayFatalError(_("Error reading from ICS"), error, 1);
4139 /* Board style 12 looks like this:
4141 <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
4143 * The "<12> " is stripped before it gets to this routine. The two
4144 * trailing 0's (flip state and clock ticking) are later addition, and
4145 * some chess servers may not have them, or may have only the first.
4146 * Additional trailing fields may be added in the future.
4149 #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"
4151 #define RELATION_OBSERVING_PLAYED 0
4152 #define RELATION_OBSERVING_STATIC -2 /* examined, oldmoves, or smoves */
4153 #define RELATION_PLAYING_MYMOVE 1
4154 #define RELATION_PLAYING_NOTMYMOVE -1
4155 #define RELATION_EXAMINING 2
4156 #define RELATION_ISOLATED_BOARD -3
4157 #define RELATION_STARTING_POSITION -4 /* FICS only */
4160 ParseBoard12 (char *string)
4162 GameMode newGameMode;
4163 int gamenum, newGame, newMove, relation, basetime, increment, ics_flip = 0, i;
4164 int j, k, n, moveNum, white_stren, black_stren, white_time, black_time, takeback;
4165 int double_push, castle_ws, castle_wl, castle_bs, castle_bl, irrev_count;
4166 char to_play, board_chars[200];
4167 char move_str[MSG_SIZ], str[MSG_SIZ], elapsed_time[MSG_SIZ];
4168 char black[32], white[32];
4170 int prevMove = currentMove;
4173 int fromX, fromY, toX, toY;
4175 int ranks=1, files=0; /* [HGM] ICS80: allow variable board size */
4176 char *bookHit = NULL; // [HGM] book
4177 Boolean weird = FALSE, reqFlag = FALSE;
4179 fromX = fromY = toX = toY = -1;
4183 if (appData.debugMode)
4184 fprintf(debugFP, _("Parsing board: %s\n"), string);
4186 move_str[0] = NULLCHAR;
4187 elapsed_time[0] = NULLCHAR;
4188 { /* [HGM] figure out how many ranks and files the board has, for ICS extension used by Capablanca server */
4190 while(i < 199 && (string[i] != ' ' || string[i+2] != ' ')) {
4191 if(string[i] == ' ') { ranks++; files = 0; }
4193 if(!strchr(" -pnbrqkPNBRQK" , string[i])) weird = TRUE; // test for fairies
4196 for(j = 0; j <i; j++) board_chars[j] = string[j];
4197 board_chars[i] = '\0';
4200 n = sscanf(string, PATTERN, &to_play, &double_push,
4201 &castle_ws, &castle_wl, &castle_bs, &castle_bl, &irrev_count,
4202 &gamenum, white, black, &relation, &basetime, &increment,
4203 &white_stren, &black_stren, &white_time, &black_time,
4204 &moveNum, str, elapsed_time, move_str, &ics_flip,
4208 snprintf(str, MSG_SIZ, _("Failed to parse board string:\n\"%s\""), string);
4209 DisplayError(str, 0);
4213 /* Convert the move number to internal form */
4214 moveNum = (moveNum - 1) * 2;
4215 if (to_play == 'B') moveNum++;
4216 if (moveNum > framePtr) { // [HGM] vari: do not run into saved variations
4217 DisplayFatalError(_("Game too long; increase MAX_MOVES and recompile"),
4223 case RELATION_OBSERVING_PLAYED:
4224 case RELATION_OBSERVING_STATIC:
4225 if (gamenum == -1) {
4226 /* Old ICC buglet */
4227 relation = RELATION_OBSERVING_STATIC;
4229 newGameMode = IcsObserving;
4231 case RELATION_PLAYING_MYMOVE:
4232 case RELATION_PLAYING_NOTMYMOVE:
4234 ((relation == RELATION_PLAYING_MYMOVE) == (to_play == 'W')) ?
4235 IcsPlayingWhite : IcsPlayingBlack;
4236 soughtPending =FALSE; // [HGM] seekgraph: solve race condition
4238 case RELATION_EXAMINING:
4239 newGameMode = IcsExamining;
4241 case RELATION_ISOLATED_BOARD:
4243 /* Just display this board. If user was doing something else,
4244 we will forget about it until the next board comes. */
4245 newGameMode = IcsIdle;
4247 case RELATION_STARTING_POSITION:
4248 newGameMode = gameMode;
4252 if((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
4253 gameMode == IcsObserving && appData.dualBoard) // also allow use of second board for observing two games
4254 && newGameMode == IcsObserving && gamenum != ics_gamenum && appData.bgObserve) {
4255 // [HGM] bughouse: don't act on alien boards while we play. Just parse the board and save it */
4256 int fac = strchr(elapsed_time, '.') ? 1 : 1000;
4257 static int lastBgGame = -1;
4259 for (k = 0; k < ranks; k++) {
4260 for (j = 0; j < files; j++)
4261 board[k][j+gameInfo.holdingsWidth] = CharToPiece(board_chars[(ranks-1-k)*(files+1) + j]);
4262 if(gameInfo.holdingsWidth > 1) {
4263 board[k][0] = board[k][BOARD_WIDTH-1] = EmptySquare;
4264 board[k][1] = board[k][BOARD_WIDTH-2] = (ChessSquare) 0;;
4267 CopyBoard(partnerBoard, board);
4268 if(toSqr = strchr(str, '/')) { // extract highlights from long move
4269 partnerBoard[EP_STATUS-3] = toSqr[1] - AAA; // kludge: hide highlighting info in board
4270 partnerBoard[EP_STATUS-4] = toSqr[2] - ONE;
4271 } else partnerBoard[EP_STATUS-4] = partnerBoard[EP_STATUS-3] = -1;
4272 if(toSqr = strchr(str, '-')) {
4273 partnerBoard[EP_STATUS-1] = toSqr[1] - AAA;
4274 partnerBoard[EP_STATUS-2] = toSqr[2] - ONE;
4275 } else partnerBoard[EP_STATUS-1] = partnerBoard[EP_STATUS-2] = -1;
4276 if(appData.dualBoard && !twoBoards) { twoBoards = 1; InitDrawingSizes(-2,0); }
4277 if(twoBoards) { partnerUp = 1; flipView = !flipView; } // [HGM] dual
4278 if(partnerUp) DrawPosition(FALSE, partnerBoard);
4280 DisplayWhiteClock(white_time*fac, to_play == 'W');
4281 DisplayBlackClock(black_time*fac, to_play != 'W');
4282 activePartner = to_play;
4283 if(gamenum != lastBgGame) {
4285 snprintf(buf, MSG_SIZ, "%s %s %s", white, _("vs."), black);
4288 lastBgGame = gamenum;
4289 activePartnerTime = to_play == 'W' ? white_time*fac : black_time*fac;
4290 partnerUp = 0; flipView = !flipView; } // [HGM] dual
4291 snprintf(partnerStatus, MSG_SIZ,"W: %d:%02d B: %d:%02d (%d-%d) %c", white_time*fac/60000, (white_time*fac%60000)/1000,
4292 (black_time*fac/60000), (black_time*fac%60000)/1000, white_stren, black_stren, to_play);
4293 DisplayMessage(partnerStatus, "");
4294 partnerBoardValid = TRUE;
4298 if(appData.dualBoard && appData.bgObserve) {
4299 if((newGameMode == IcsPlayingWhite || newGameMode == IcsPlayingBlack) && moveNum == 1)
4300 SendToICS(ics_prefix), SendToICS("pobserve\n");
4301 else if(newGameMode == IcsObserving && (gameMode == BeginningOfGame || gameMode == IcsIdle)) {
4303 snprintf(buf, MSG_SIZ, "%spobserve %s\n", ics_prefix, white);
4308 /* Modify behavior for initial board display on move listing
4311 switch (ics_getting_history) {
4315 case H_GOT_REQ_HEADER:
4316 case H_GOT_UNREQ_HEADER:
4317 /* This is the initial position of the current game */
4318 gamenum = ics_gamenum;
4319 moveNum = 0; /* old ICS bug workaround */
4320 if (to_play == 'B') {
4321 startedFromSetupPosition = TRUE;
4322 blackPlaysFirst = TRUE;
4324 if (forwardMostMove == 0) forwardMostMove = 1;
4325 if (backwardMostMove == 0) backwardMostMove = 1;
4326 if (currentMove == 0) currentMove = 1;
4328 newGameMode = gameMode;
4329 relation = RELATION_STARTING_POSITION; /* ICC needs this */
4331 case H_GOT_UNWANTED_HEADER:
4332 /* This is an initial board that we don't want */
4334 case H_GETTING_MOVES:
4335 /* Should not happen */
4336 DisplayError(_("Error gathering move list: extra board"), 0);
4337 ics_getting_history = H_FALSE;
4341 if (gameInfo.boardHeight != ranks || gameInfo.boardWidth != files ||
4342 move_str[1] == '@' && !gameInfo.holdingsWidth ||
4343 weird && (int)gameInfo.variant < (int)VariantShogi) {
4344 /* [HGM] We seem to have switched variant unexpectedly
4345 * Try to guess new variant from board size
4347 VariantClass newVariant = VariantFairy; // if 8x8, but fairies present
4348 if(ranks == 8 && files == 10) newVariant = VariantCapablanca; else
4349 if(ranks == 10 && files == 9) newVariant = VariantXiangqi; else
4350 if(ranks == 8 && files == 12) newVariant = VariantCourier; else
4351 if(ranks == 9 && files == 9) newVariant = VariantShogi; else
4352 if(ranks == 10 && files == 10) newVariant = VariantGrand; else
4353 if(!weird) newVariant = move_str[1] == '@' ? VariantCrazyhouse : VariantNormal;
4354 VariantSwitch(boards[currentMove], newVariant); /* temp guess */
4355 /* Get a move list just to see the header, which
4356 will tell us whether this is really bug or zh */
4357 if (ics_getting_history == H_FALSE) {
4358 ics_getting_history = H_REQUESTED; reqFlag = TRUE;
4359 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4364 /* Take action if this is the first board of a new game, or of a
4365 different game than is currently being displayed. */
4366 if (gamenum != ics_gamenum || newGameMode != gameMode ||
4367 relation == RELATION_ISOLATED_BOARD) {
4369 /* Forget the old game and get the history (if any) of the new one */
4370 if (gameMode != BeginningOfGame) {
4374 if (appData.autoRaiseBoard) BoardToTop();
4376 if (gamenum == -1) {
4377 newGameMode = IcsIdle;
4378 } else if ((moveNum > 0 || newGameMode == IcsObserving) && newGameMode != IcsIdle &&
4379 appData.getMoveList && !reqFlag) {
4380 /* Need to get game history */
4381 ics_getting_history = H_REQUESTED;
4382 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4386 /* Initially flip the board to have black on the bottom if playing
4387 black or if the ICS flip flag is set, but let the user change
4388 it with the Flip View button. */
4389 flipView = appData.autoFlipView ?
4390 (newGameMode == IcsPlayingBlack) || ics_flip :
4393 /* Done with values from previous mode; copy in new ones */
4394 gameMode = newGameMode;
4396 ics_gamenum = gamenum;
4397 if (gamenum == gs_gamenum) {
4398 int klen = strlen(gs_kind);
4399 if (gs_kind[klen - 1] == '.') gs_kind[klen - 1] = NULLCHAR;
4400 snprintf(str, MSG_SIZ, "ICS %s", gs_kind);
4401 gameInfo.event = StrSave(str);
4403 gameInfo.event = StrSave("ICS game");
4405 gameInfo.site = StrSave(appData.icsHost);
4406 gameInfo.date = PGNDate();
4407 gameInfo.round = StrSave("-");
4408 gameInfo.white = StrSave(white);
4409 gameInfo.black = StrSave(black);
4410 timeControl = basetime * 60 * 1000;
4412 timeIncrement = increment * 1000;
4413 movesPerSession = 0;
4414 gameInfo.timeControl = TimeControlTagValue();
4415 VariantSwitch(boards[currentMove], StringToVariant(gameInfo.event) );
4416 if (appData.debugMode) {
4417 fprintf(debugFP, "ParseBoard says variant = '%s'\n", gameInfo.event);
4418 fprintf(debugFP, "recognized as %s\n", VariantName(gameInfo.variant));
4419 setbuf(debugFP, NULL);
4422 gameInfo.outOfBook = NULL;
4424 /* Do we have the ratings? */
4425 if (strcmp(player1Name, white) == 0 &&
4426 strcmp(player2Name, black) == 0) {
4427 if (appData.debugMode)
4428 fprintf(debugFP, "Remembered ratings: W %d, B %d\n",
4429 player1Rating, player2Rating);
4430 gameInfo.whiteRating = player1Rating;
4431 gameInfo.blackRating = player2Rating;
4432 } else if (strcmp(player2Name, white) == 0 &&
4433 strcmp(player1Name, black) == 0) {
4434 if (appData.debugMode)
4435 fprintf(debugFP, "Remembered ratings: W %d, B %d\n",
4436 player2Rating, player1Rating);
4437 gameInfo.whiteRating = player2Rating;
4438 gameInfo.blackRating = player1Rating;
4440 player1Name[0] = player2Name[0] = NULLCHAR;
4442 /* Silence shouts if requested */
4443 if (appData.quietPlay &&
4444 (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)) {
4445 SendToICS(ics_prefix);
4446 SendToICS("set shout 0\n");
4450 /* Deal with midgame name changes */
4452 if (!gameInfo.white || strcmp(gameInfo.white, white) != 0) {
4453 if (gameInfo.white) free(gameInfo.white);
4454 gameInfo.white = StrSave(white);
4456 if (!gameInfo.black || strcmp(gameInfo.black, black) != 0) {
4457 if (gameInfo.black) free(gameInfo.black);
4458 gameInfo.black = StrSave(black);
4462 /* Throw away game result if anything actually changes in examine mode */
4463 if (gameMode == IcsExamining && !newGame) {
4464 gameInfo.result = GameUnfinished;
4465 if (gameInfo.resultDetails != NULL) {
4466 free(gameInfo.resultDetails);
4467 gameInfo.resultDetails = NULL;
4471 /* In pausing && IcsExamining mode, we ignore boards coming
4472 in if they are in a different variation than we are. */
4473 if (pauseExamInvalid) return;
4474 if (pausing && gameMode == IcsExamining) {
4475 if (moveNum <= pauseExamForwardMostMove) {
4476 pauseExamInvalid = TRUE;
4477 forwardMostMove = pauseExamForwardMostMove;
4482 if (appData.debugMode) {
4483 fprintf(debugFP, "load %dx%d board\n", files, ranks);
4485 /* Parse the board */
4486 for (k = 0; k < ranks; k++) {
4487 for (j = 0; j < files; j++)
4488 board[k][j+gameInfo.holdingsWidth] = CharToPiece(board_chars[(ranks-1-k)*(files+1) + j]);
4489 if(gameInfo.holdingsWidth > 1) {
4490 board[k][0] = board[k][BOARD_WIDTH-1] = EmptySquare;
4491 board[k][1] = board[k][BOARD_WIDTH-2] = (ChessSquare) 0;;
4494 if(moveNum==0 && gameInfo.variant == VariantSChess) {
4495 board[5][BOARD_RGHT+1] = WhiteAngel;
4496 board[6][BOARD_RGHT+1] = WhiteMarshall;
4497 board[1][0] = BlackMarshall;
4498 board[2][0] = BlackAngel;
4499 board[1][1] = board[2][1] = board[5][BOARD_RGHT] = board[6][BOARD_RGHT] = 1;
4501 CopyBoard(boards[moveNum], board);
4502 boards[moveNum][HOLDINGS_SET] = 0; // [HGM] indicate holdings not set
4504 startedFromSetupPosition =
4505 !CompareBoards(board, initialPosition);
4506 if(startedFromSetupPosition)
4507 initialRulePlies = irrev_count; /* [HGM] 50-move counter offset */
4510 /* [HGM] Set castling rights. Take the outermost Rooks,
4511 to make it also work for FRC opening positions. Note that board12
4512 is really defective for later FRC positions, as it has no way to
4513 indicate which Rook can castle if they are on the same side of King.
4514 For the initial position we grant rights to the outermost Rooks,
4515 and remember thos rights, and we then copy them on positions
4516 later in an FRC game. This means WB might not recognize castlings with
4517 Rooks that have moved back to their original position as illegal,
4518 but in ICS mode that is not its job anyway.
4520 if(moveNum == 0 || gameInfo.variant != VariantFischeRandom)
4521 { int i, j; ChessSquare wKing = WhiteKing, bKing = BlackKing;
4523 for(i=BOARD_LEFT, j=NoRights; i<BOARD_RGHT; i++)
4524 if(board[0][i] == WhiteRook) j = i;
4525 initialRights[0] = boards[moveNum][CASTLING][0] = (castle_ws == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4526 for(i=BOARD_RGHT-1, j=NoRights; i>=BOARD_LEFT; i--)
4527 if(board[0][i] == WhiteRook) j = i;
4528 initialRights[1] = boards[moveNum][CASTLING][1] = (castle_wl == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4529 for(i=BOARD_LEFT, j=NoRights; i<BOARD_RGHT; i++)
4530 if(board[BOARD_HEIGHT-1][i] == BlackRook) j = i;
4531 initialRights[3] = boards[moveNum][CASTLING][3] = (castle_bs == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4532 for(i=BOARD_RGHT-1, j=NoRights; i>=BOARD_LEFT; i--)
4533 if(board[BOARD_HEIGHT-1][i] == BlackRook) j = i;
4534 initialRights[4] = boards[moveNum][CASTLING][4] = (castle_bl == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4536 boards[moveNum][CASTLING][2] = boards[moveNum][CASTLING][5] = NoRights;
4537 if(gameInfo.variant == VariantKnightmate) { wKing = WhiteUnicorn; bKing = BlackUnicorn; }
4538 for(k=BOARD_LEFT; k<BOARD_RGHT; k++)
4539 if(board[0][k] == wKing) initialRights[2] = boards[moveNum][CASTLING][2] = k;
4540 for(k=BOARD_LEFT; k<BOARD_RGHT; k++)
4541 if(board[BOARD_HEIGHT-1][k] == bKing)
4542 initialRights[5] = boards[moveNum][CASTLING][5] = k;
4543 if(gameInfo.variant == VariantTwoKings) {
4544 // In TwoKings looking for a King does not work, so always give castling rights to a King on e1/e8
4545 if(board[0][4] == wKing) initialRights[2] = boards[moveNum][CASTLING][2] = 4;
4546 if(board[BOARD_HEIGHT-1][4] == bKing) initialRights[5] = boards[moveNum][CASTLING][5] = 4;
4549 r = boards[moveNum][CASTLING][0] = initialRights[0];
4550 if(board[0][r] != WhiteRook) boards[moveNum][CASTLING][0] = NoRights;
4551 r = boards[moveNum][CASTLING][1] = initialRights[1];
4552 if(board[0][r] != WhiteRook) boards[moveNum][CASTLING][1] = NoRights;
4553 r = boards[moveNum][CASTLING][3] = initialRights[3];
4554 if(board[BOARD_HEIGHT-1][r] != BlackRook) boards[moveNum][CASTLING][3] = NoRights;
4555 r = boards[moveNum][CASTLING][4] = initialRights[4];
4556 if(board[BOARD_HEIGHT-1][r] != BlackRook) boards[moveNum][CASTLING][4] = NoRights;
4557 /* wildcastle kludge: always assume King has rights */
4558 r = boards[moveNum][CASTLING][2] = initialRights[2];
4559 r = boards[moveNum][CASTLING][5] = initialRights[5];
4561 /* [HGM] e.p. rights. Assume that ICS sends file number here? */
4562 boards[moveNum][EP_STATUS] = EP_NONE;
4563 if(str[0] == 'P') boards[moveNum][EP_STATUS] = EP_PAWN_MOVE;
4564 if(strchr(move_str, 'x')) boards[moveNum][EP_STATUS] = EP_CAPTURE;
4565 if(double_push != -1) boards[moveNum][EP_STATUS] = double_push + BOARD_LEFT;
4568 if (ics_getting_history == H_GOT_REQ_HEADER ||
4569 ics_getting_history == H_GOT_UNREQ_HEADER) {
4570 /* This was an initial position from a move list, not
4571 the current position */
4575 /* Update currentMove and known move number limits */
4576 newMove = newGame || moveNum > forwardMostMove;
4579 forwardMostMove = backwardMostMove = currentMove = moveNum;
4580 if (gameMode == IcsExamining && moveNum == 0) {
4581 /* Workaround for ICS limitation: we are not told the wild
4582 type when starting to examine a game. But if we ask for
4583 the move list, the move list header will tell us */
4584 ics_getting_history = H_REQUESTED;
4585 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4588 } else if (moveNum == forwardMostMove + 1 || moveNum == forwardMostMove
4589 || (moveNum < forwardMostMove && moveNum >= backwardMostMove)) {
4591 /* [DM] If we found takebacks during icsEngineAnalyze try send to engine */
4592 /* [HGM] applied this also to an engine that is silently watching */
4593 if (appData.zippyPlay && moveNum < forwardMostMove && first.initDone &&
4594 (gameMode == IcsObserving || gameMode == IcsExamining) &&
4595 gameInfo.variant == currentlyInitializedVariant) {
4596 takeback = forwardMostMove - moveNum;
4597 for (i = 0; i < takeback; i++) {
4598 if (appData.debugMode) fprintf(debugFP, "take back move\n");
4599 SendToProgram("undo\n", &first);
4604 forwardMostMove = moveNum;
4605 if (!pausing || currentMove > forwardMostMove)
4606 currentMove = forwardMostMove;
4608 /* New part of history that is not contiguous with old part */
4609 if (pausing && gameMode == IcsExamining) {
4610 pauseExamInvalid = TRUE;
4611 forwardMostMove = pauseExamForwardMostMove;
4614 if (gameMode == IcsExamining && moveNum > 0 && appData.getMoveList) {
4616 if(appData.zippyPlay && forwardMostMove > 0 && first.initDone) {
4617 // [HGM] when we will receive the move list we now request, it will be
4618 // fed to the engine from the first move on. So if the engine is not
4619 // in the initial position now, bring it there.
4620 InitChessProgram(&first, 0);
4623 ics_getting_history = H_REQUESTED;
4624 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4627 forwardMostMove = backwardMostMove = currentMove = moveNum;
4630 /* Update the clocks */
4631 if (strchr(elapsed_time, '.')) {
4633 timeRemaining[0][moveNum] = whiteTimeRemaining = white_time;
4634 timeRemaining[1][moveNum] = blackTimeRemaining = black_time;
4636 /* Time is in seconds */
4637 timeRemaining[0][moveNum] = whiteTimeRemaining = white_time * 1000;
4638 timeRemaining[1][moveNum] = blackTimeRemaining = black_time * 1000;
4643 if (appData.zippyPlay && newGame &&
4644 gameMode != IcsObserving && gameMode != IcsIdle &&
4645 gameMode != IcsExamining)
4646 ZippyFirstBoard(moveNum, basetime, increment);
4649 /* Put the move on the move list, first converting
4650 to canonical algebraic form. */
4652 if (appData.debugMode) {
4653 if (appData.debugMode) { int f = forwardMostMove;
4654 fprintf(debugFP, "parseboard %d, castling = %d %d %d %d %d %d\n", f,
4655 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
4656 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
4658 fprintf(debugFP, "accepted move %s from ICS, parse it.\n", move_str);
4659 fprintf(debugFP, "moveNum = %d\n", moveNum);
4660 fprintf(debugFP, "board = %d-%d x %d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT);
4661 setbuf(debugFP, NULL);
4663 if (moveNum <= backwardMostMove) {
4664 /* We don't know what the board looked like before
4666 safeStrCpy(parseList[moveNum - 1], move_str, sizeof(parseList[moveNum - 1])/sizeof(parseList[moveNum - 1][0]));
4667 strcat(parseList[moveNum - 1], " ");
4668 strcat(parseList[moveNum - 1], elapsed_time);
4669 moveList[moveNum - 1][0] = NULLCHAR;
4670 } else if (strcmp(move_str, "none") == 0) {
4671 // [HGM] long SAN: swapped order; test for 'none' before parsing move
4672 /* Again, we don't know what the board looked like;
4673 this is really the start of the game. */
4674 parseList[moveNum - 1][0] = NULLCHAR;
4675 moveList[moveNum - 1][0] = NULLCHAR;
4676 backwardMostMove = moveNum;
4677 startedFromSetupPosition = TRUE;
4678 fromX = fromY = toX = toY = -1;
4680 // [HGM] long SAN: if legality-testing is off, disambiguation might not work or give wrong move.
4681 // So we parse the long-algebraic move string in stead of the SAN move
4682 int valid; char buf[MSG_SIZ], *prom;
4684 if(gameInfo.variant == VariantShogi && !strchr(move_str, '=') && !strchr(move_str, '@'))
4685 strcat(move_str, "="); // if ICS does not say 'promote' on non-drop, we defer.
4686 // str looks something like "Q/a1-a2"; kill the slash
4688 snprintf(buf, MSG_SIZ,"%c%s", str[0], str+2);
4689 else safeStrCpy(buf, str, sizeof(buf)/sizeof(buf[0])); // might be castling
4690 if((prom = strstr(move_str, "=")) && !strstr(buf, "="))
4691 strcat(buf, prom); // long move lacks promo specification!
4692 if(!appData.testLegality && move_str[1] != '@') { // drops never ambiguous (parser chokes on long form!)
4693 if(appData.debugMode)
4694 fprintf(debugFP, "replaced ICS move '%s' by '%s'\n", move_str, buf);
4695 safeStrCpy(move_str, buf, MSG_SIZ);
4697 valid = ParseOneMove(move_str, moveNum - 1, &moveType,
4698 &fromX, &fromY, &toX, &toY, &promoChar)
4699 || ParseOneMove(buf, moveNum - 1, &moveType,
4700 &fromX, &fromY, &toX, &toY, &promoChar);
4701 // end of long SAN patch
4703 (void) CoordsToAlgebraic(boards[moveNum - 1],
4704 PosFlags(moveNum - 1),
4705 fromY, fromX, toY, toX, promoChar,
4706 parseList[moveNum-1]);
4707 switch (MateTest(boards[moveNum], PosFlags(moveNum)) ) {
4713 if(gameInfo.variant != VariantShogi)
4714 strcat(parseList[moveNum - 1], "+");
4717 case MT_STAINMATE: // [HGM] xq: for notation stalemate that wins counts as checkmate
4718 strcat(parseList[moveNum - 1], "#");
4721 strcat(parseList[moveNum - 1], " ");
4722 strcat(parseList[moveNum - 1], elapsed_time);
4723 /* currentMoveString is set as a side-effect of ParseOneMove */
4724 if(gameInfo.variant == VariantShogi && currentMoveString[4]) currentMoveString[4] = '^';
4725 safeStrCpy(moveList[moveNum - 1], currentMoveString, sizeof(moveList[moveNum - 1])/sizeof(moveList[moveNum - 1][0]));
4726 strcat(moveList[moveNum - 1], "\n");
4728 if(gameInfo.holdingsWidth && !appData.disguise && gameInfo.variant != VariantSuper && gameInfo.variant != VariantGreat
4729 && gameInfo.variant != VariantGrand&& gameInfo.variant != VariantSChess) // inherit info that ICS does not give from previous board
4730 for(k=0; k<ranks; k++) for(j=BOARD_LEFT; j<BOARD_RGHT; j++) {
4731 ChessSquare old, new = boards[moveNum][k][j];
4732 if(fromY == DROP_RANK && k==toY && j==toX) continue; // dropped pieces always stand for themselves
4733 old = (k==toY && j==toX) ? boards[moveNum-1][fromY][fromX] : boards[moveNum-1][k][j]; // trace back mover
4734 if(old == new) continue;
4735 if(old == PROMOTED new) boards[moveNum][k][j] = old; // prevent promoted pieces to revert to primordial ones
4736 else if(new == WhiteWazir || new == BlackWazir) {
4737 if(old < WhiteCannon || old >= BlackPawn && old < BlackCannon)
4738 boards[moveNum][k][j] = PROMOTED old; // choose correct type of Gold in promotion
4739 else boards[moveNum][k][j] = old; // preserve type of Gold
4740 } else if((old == WhitePawn || old == BlackPawn) && new != EmptySquare) // Pawn promotions (but not e.p.capture!)
4741 boards[moveNum][k][j] = PROMOTED new; // use non-primordial representation of chosen piece
4744 /* Move from ICS was illegal!? Punt. */
4745 if (appData.debugMode) {
4746 fprintf(debugFP, "Illegal move from ICS '%s'\n", move_str);
4747 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
4749 safeStrCpy(parseList[moveNum - 1], move_str, sizeof(parseList[moveNum - 1])/sizeof(parseList[moveNum - 1][0]));
4750 strcat(parseList[moveNum - 1], " ");
4751 strcat(parseList[moveNum - 1], elapsed_time);
4752 moveList[moveNum - 1][0] = NULLCHAR;
4753 fromX = fromY = toX = toY = -1;
4756 if (appData.debugMode) {
4757 fprintf(debugFP, "Move parsed to '%s'\n", parseList[moveNum - 1]);
4758 setbuf(debugFP, NULL);
4762 /* Send move to chess program (BEFORE animating it). */
4763 if (appData.zippyPlay && !newGame && newMove &&
4764 (!appData.getMoveList || backwardMostMove == 0) && first.initDone) {
4766 if ((gameMode == IcsPlayingWhite && WhiteOnMove(moveNum)) ||
4767 (gameMode == IcsPlayingBlack && !WhiteOnMove(moveNum))) {
4768 if (moveList[moveNum - 1][0] == NULLCHAR) {
4769 snprintf(str, MSG_SIZ, _("Couldn't parse move \"%s\" from ICS"),
4771 DisplayError(str, 0);
4773 if (first.sendTime) {
4774 SendTimeRemaining(&first, gameMode == IcsPlayingWhite);
4776 bookHit = SendMoveToBookUser(moveNum - 1, &first, FALSE); // [HGM] book
4777 if (firstMove && !bookHit) {
4779 if (first.useColors) {
4780 SendToProgram(gameMode == IcsPlayingWhite ?
4782 "black\ngo\n", &first);
4784 SendToProgram("go\n", &first);
4786 first.maybeThinking = TRUE;
4789 } else if (gameMode == IcsObserving || gameMode == IcsExamining) {
4790 if (moveList[moveNum - 1][0] == NULLCHAR) {
4791 snprintf(str, MSG_SIZ, _("Couldn't parse move \"%s\" from ICS"), move_str);
4792 DisplayError(str, 0);
4794 if(gameInfo.variant == currentlyInitializedVariant) // [HGM] refrain sending moves engine can't understand!
4795 SendMoveToProgram(moveNum - 1, &first);
4802 if (moveNum > 0 && !gotPremove && !appData.noGUI) {
4803 /* If move comes from a remote source, animate it. If it
4804 isn't remote, it will have already been animated. */
4805 if (!pausing && !ics_user_moved && prevMove == moveNum - 1) {
4806 AnimateMove(boards[moveNum - 1], fromX, fromY, toX, toY);
4808 if (!pausing && appData.highlightLastMove) {
4809 SetHighlights(fromX, fromY, toX, toY);
4813 /* Start the clocks */
4814 whiteFlag = blackFlag = FALSE;
4815 appData.clockMode = !(basetime == 0 && increment == 0);
4817 ics_clock_paused = TRUE;
4819 } else if (ticking == 1) {
4820 ics_clock_paused = FALSE;
4822 if (gameMode == IcsIdle ||
4823 relation == RELATION_OBSERVING_STATIC ||
4824 relation == RELATION_EXAMINING ||
4826 DisplayBothClocks();
4830 /* Display opponents and material strengths */
4831 if (gameInfo.variant != VariantBughouse &&
4832 gameInfo.variant != VariantCrazyhouse && !appData.noGUI) {
4833 if (tinyLayout || smallLayout) {
4834 if(gameInfo.variant == VariantNormal)
4835 snprintf(str, MSG_SIZ, "%s(%d) %s(%d) {%d %d}",
4836 gameInfo.white, white_stren, gameInfo.black, black_stren,
4837 basetime, increment);
4839 snprintf(str, MSG_SIZ, "%s(%d) %s(%d) {%d %d w%d}",
4840 gameInfo.white, white_stren, gameInfo.black, black_stren,
4841 basetime, increment, (int) gameInfo.variant);
4843 if(gameInfo.variant == VariantNormal)
4844 snprintf(str, MSG_SIZ, "%s (%d) %s %s (%d) {%d %d}",
4845 gameInfo.white, white_stren, _("vs."), gameInfo.black, black_stren,
4846 basetime, increment);
4848 snprintf(str, MSG_SIZ, "%s (%d) %s %s (%d) {%d %d %s}",
4849 gameInfo.white, white_stren, _("vs."), gameInfo.black, black_stren,
4850 basetime, increment, VariantName(gameInfo.variant));
4853 if (appData.debugMode) {
4854 fprintf(debugFP, "Display title '%s, gameInfo.variant = %d'\n", str, gameInfo.variant);
4859 /* Display the board */
4860 if (!pausing && !appData.noGUI) {
4862 if (appData.premove)
4864 ((gameMode == IcsPlayingWhite) && (WhiteOnMove(currentMove))) ||
4865 ((gameMode == IcsPlayingBlack) && (!WhiteOnMove(currentMove))))
4866 ClearPremoveHighlights();
4868 j = seekGraphUp; seekGraphUp = FALSE; // [HGM] seekgraph: when we draw a board, it overwrites the seek graph
4869 if(partnerUp) { flipView = originalFlip; partnerUp = FALSE; j = TRUE; } // [HGM] bughouse: restore view
4870 DrawPosition(j, boards[currentMove]);
4872 DisplayMove(moveNum - 1);
4873 if (appData.ringBellAfterMoves && /*!ics_user_moved*/ // [HGM] use absolute method to recognize own move
4874 !((gameMode == IcsPlayingWhite) && (!WhiteOnMove(moveNum)) ||
4875 (gameMode == IcsPlayingBlack) && (WhiteOnMove(moveNum)) ) ) {
4876 if(newMove) RingBell(); else PlayIcsUnfinishedSound();
4880 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
4882 if(bookHit) { // [HGM] book: simulate book reply
4883 static char bookMove[MSG_SIZ]; // a bit generous?
4885 programStats.nodes = programStats.depth = programStats.time =
4886 programStats.score = programStats.got_only_move = 0;
4887 sprintf(programStats.movelist, "%s (xbook)", bookHit);
4889 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
4890 strcat(bookMove, bookHit);
4891 HandleMachineMove(bookMove, &first);
4900 if (appData.icsActive && gameMode != IcsIdle && ics_gamenum > 0) {
4901 ics_getting_history = H_REQUESTED;
4902 snprintf(buf, MSG_SIZ, "%smoves %d\n", ics_prefix, ics_gamenum);
4908 SendToBoth (char *msg)
4909 { // to make it easy to keep two engines in step in dual analysis
4910 SendToProgram(msg, &first);
4911 if(second.analyzing) SendToProgram(msg, &second);
4915 AnalysisPeriodicEvent (int force)
4917 if (((programStats.ok_to_send == 0 || programStats.line_is_book)
4918 && !force) || !appData.periodicUpdates)
4921 /* Send . command to Crafty to collect stats */
4924 /* Don't send another until we get a response (this makes
4925 us stop sending to old Crafty's which don't understand
4926 the "." command (sending illegal cmds resets node count & time,
4927 which looks bad)) */
4928 programStats.ok_to_send = 0;
4932 ics_update_width (int new_width)
4934 ics_printf("set width %d\n", new_width);
4938 SendMoveToProgram (int moveNum, ChessProgramState *cps)
4942 if(moveList[moveNum][1] == '@' && moveList[moveNum][0] == '@') {
4943 // null move in variant where engine does not understand it (for analysis purposes)
4944 SendBoard(cps, moveNum + 1); // send position after move in stead.
4947 if (cps->useUsermove) {
4948 SendToProgram("usermove ", cps);
4952 if ((space = strchr(parseList[moveNum], ' ')) != NULL) {
4953 int len = space - parseList[moveNum];
4954 memcpy(buf, parseList[moveNum], len);
4956 buf[len] = NULLCHAR;
4958 snprintf(buf, MSG_SIZ,"%s\n", parseList[moveNum]);
4960 SendToProgram(buf, cps);
4962 if(cps->alphaRank) { /* [HGM] shogi: temporarily convert to shogi coordinates before sending */
4963 AlphaRank(moveList[moveNum], 4);
4964 SendToProgram(moveList[moveNum], cps);
4965 AlphaRank(moveList[moveNum], 4); // and back
4967 /* Added by Tord: Send castle moves in "O-O" in FRC games if required by
4968 * the engine. It would be nice to have a better way to identify castle
4970 if((gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom)
4971 && cps->useOOCastle) {
4972 int fromX = moveList[moveNum][0] - AAA;
4973 int fromY = moveList[moveNum][1] - ONE;
4974 int toX = moveList[moveNum][2] - AAA;
4975 int toY = moveList[moveNum][3] - ONE;
4976 if((boards[moveNum][fromY][fromX] == WhiteKing
4977 && boards[moveNum][toY][toX] == WhiteRook)
4978 || (boards[moveNum][fromY][fromX] == BlackKing
4979 && boards[moveNum][toY][toX] == BlackRook)) {
4980 if(toX > fromX) SendToProgram("O-O\n", cps);
4981 else SendToProgram("O-O-O\n", cps);
4983 else SendToProgram(moveList[moveNum], cps);
4985 if(BOARD_HEIGHT > 10) { // [HGM] big: convert ranks to double-digit where needed
4986 if(moveList[moveNum][1] == '@' && (BOARD_HEIGHT < 16 || moveList[moveNum][0] <= 'Z')) { // drop move
4987 if(moveList[moveNum][0]== '@') snprintf(buf, MSG_SIZ, "@@@@\n"); else
4988 snprintf(buf, MSG_SIZ, "%c@%c%d%s", moveList[moveNum][0],
4989 moveList[moveNum][2], moveList[moveNum][3] - '0', moveList[moveNum]+4);
4991 snprintf(buf, MSG_SIZ, "%c%d%c%d%s", moveList[moveNum][0], moveList[moveNum][1] - '0',
4992 moveList[moveNum][2], moveList[moveNum][3] - '0', moveList[moveNum]+4);
4993 SendToProgram(buf, cps);
4995 else SendToProgram(moveList[moveNum], cps);
4996 /* End of additions by Tord */
4999 /* [HGM] setting up the opening has brought engine in force mode! */
5000 /* Send 'go' if we are in a mode where machine should play. */
5001 if( (moveNum == 0 && setboardSpoiledMachineBlack && cps == &first) &&
5002 (gameMode == TwoMachinesPlay ||
5004 gameMode == IcsPlayingBlack || gameMode == IcsPlayingWhite ||
5006 gameMode == MachinePlaysBlack || gameMode == MachinePlaysWhite) ) {
5007 SendToProgram("go\n", cps);
5008 if (appData.debugMode) {
5009 fprintf(debugFP, "(extra)\n");
5012 setboardSpoiledMachineBlack = 0;
5016 SendMoveToICS (ChessMove moveType, int fromX, int fromY, int toX, int toY, char promoChar)
5018 char user_move[MSG_SIZ];
5021 if(gameInfo.variant == VariantSChess && promoChar) {
5022 snprintf(suffix, 4, "=%c", toX == BOARD_WIDTH<<1 ? ToUpper(promoChar) : ToLower(promoChar));
5023 if(moveType == NormalMove) moveType = WhitePromotion; // kludge to do gating
5024 } else suffix[0] = NULLCHAR;
5028 snprintf(user_move, MSG_SIZ, _("say Internal error; bad moveType %d (%d,%d-%d,%d)"),
5029 (int)moveType, fromX, fromY, toX, toY);
5030 DisplayError(user_move + strlen("say "), 0);
5032 case WhiteKingSideCastle:
5033 case BlackKingSideCastle:
5034 case WhiteQueenSideCastleWild:
5035 case BlackQueenSideCastleWild:
5037 case WhiteHSideCastleFR:
5038 case BlackHSideCastleFR:
5040 snprintf(user_move, MSG_SIZ, "o-o%s\n", suffix);
5042 case WhiteQueenSideCastle:
5043 case BlackQueenSideCastle:
5044 case WhiteKingSideCastleWild:
5045 case BlackKingSideCastleWild:
5047 case WhiteASideCastleFR:
5048 case BlackASideCastleFR:
5050 snprintf(user_move, MSG_SIZ, "o-o-o%s\n",suffix);
5052 case WhiteNonPromotion:
5053 case BlackNonPromotion:
5054 sprintf(user_move, "%c%c%c%c==\n", AAA + fromX, ONE + fromY, AAA + toX, ONE + toY);
5056 case WhitePromotion:
5057 case BlackPromotion:
5058 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
5059 snprintf(user_move, MSG_SIZ, "%c%c%c%c=%c\n",
5060 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
5061 PieceToChar(WhiteFerz));
5062 else if(gameInfo.variant == VariantGreat)
5063 snprintf(user_move, MSG_SIZ,"%c%c%c%c=%c\n",
5064 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
5065 PieceToChar(WhiteMan));
5067 snprintf(user_move, MSG_SIZ, "%c%c%c%c=%c\n",
5068 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
5074 snprintf(user_move, MSG_SIZ, "%c@%c%c\n",
5075 ToUpper(PieceToChar((ChessSquare) fromX)),
5076 AAA + toX, ONE + toY);
5078 case IllegalMove: /* could be a variant we don't quite understand */
5079 if(fromY == DROP_RANK) goto drop; // We need 'IllegalDrop' move type?
5081 case WhiteCapturesEnPassant:
5082 case BlackCapturesEnPassant:
5083 snprintf(user_move, MSG_SIZ,"%c%c%c%c\n",
5084 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY);
5087 SendToICS(user_move);
5088 if(appData.keepAlive) // [HGM] alive: schedule sending of dummy 'date' command
5089 ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
5094 { // [HGM] upload: send entire stored game to ICS as long-algebraic moves.
5095 int i, last = forwardMostMove; // make sure ICS reply cannot pre-empt us by clearing fmm
5096 static char *castlingStrings[4] = { "none", "kside", "qside", "both" };
5097 if(gameMode == IcsObserving || gameMode == IcsPlayingBlack || gameMode == IcsPlayingWhite) {
5098 DisplayError(_("You cannot do this while you are playing or observing"), 0);
5101 if(gameMode != IcsExamining) { // is this ever not the case?
5102 char buf[MSG_SIZ], *p, *fen, command[MSG_SIZ], bsetup = 0;
5104 if(ics_type == ICS_ICC) { // on ICC match ourselves in applicable variant
5105 snprintf(command,MSG_SIZ, "match %s", ics_handle);
5106 } else { // on FICS we must first go to general examine mode
5107 safeStrCpy(command, "examine\nbsetup", sizeof(command)/sizeof(command[0])); // and specify variant within it with bsetups
5109 if(gameInfo.variant != VariantNormal) {
5110 // try figure out wild number, as xboard names are not always valid on ICS
5111 for(i=1; i<=36; i++) {
5112 snprintf(buf, MSG_SIZ, "wild/%d", i);
5113 if(StringToVariant(buf) == gameInfo.variant) break;
5115 if(i<=36 && ics_type == ICS_ICC) snprintf(buf, MSG_SIZ,"%s w%d\n", command, i);
5116 else if(i == 22) snprintf(buf,MSG_SIZ, "%s fr\n", command);
5117 else snprintf(buf, MSG_SIZ,"%s %s\n", command, VariantName(gameInfo.variant));
5118 } else snprintf(buf, MSG_SIZ,"%s\n", ics_type == ICS_ICC ? command : "examine\n"); // match yourself or examine
5119 SendToICS(ics_prefix);
5121 if(startedFromSetupPosition || backwardMostMove != 0) {
5122 fen = PositionToFEN(backwardMostMove, NULL);
5123 if(ics_type == ICS_ICC) { // on ICC we can simply send a complete FEN to set everything
5124 snprintf(buf, MSG_SIZ,"loadfen %s\n", fen);
5126 } else { // FICS: everything has to set by separate bsetup commands
5127 p = strchr(fen, ' '); p[0] = NULLCHAR; // cut after board
5128 snprintf(buf, MSG_SIZ,"bsetup fen %s\n", fen);
5130 if(!WhiteOnMove(backwardMostMove)) {
5131 SendToICS("bsetup tomove black\n");
5133 i = (strchr(p+3, 'K') != NULL) + 2*(strchr(p+3, 'Q') != NULL);
5134 snprintf(buf, MSG_SIZ,"bsetup wcastle %s\n", castlingStrings[i]);
5136 i = (strchr(p+3, 'k') != NULL) + 2*(strchr(p+3, 'q') != NULL);
5137 snprintf(buf, MSG_SIZ, "bsetup bcastle %s\n", castlingStrings[i]);
5139 i = boards[backwardMostMove][EP_STATUS];
5140 if(i >= 0) { // set e.p.
5141 snprintf(buf, MSG_SIZ,"bsetup eppos %c\n", i+AAA);
5147 if(bsetup || ics_type != ICS_ICC && gameInfo.variant != VariantNormal)
5148 SendToICS("bsetup done\n"); // switch to normal examining.
5150 for(i = backwardMostMove; i<last; i++) {
5152 snprintf(buf, sizeof(buf)/sizeof(buf[0]),"%s\n", parseList[i]);
5153 if((*buf == 'b' || *buf == 'B') && buf[1] == 'x') { // work-around for stupid FICS bug, which thinks bxc3 can be a Bishop move
5154 int len = strlen(moveList[i]);
5155 snprintf(buf, sizeof(buf)/sizeof(buf[0]),"%s", moveList[i]); // use long algebraic
5156 if(!isdigit(buf[len-2])) snprintf(buf+len-2, 20-len, "=%c\n", ToUpper(buf[len-2])); // promotion must have '=' in ICS format
5160 SendToICS(ics_prefix);
5161 SendToICS(ics_type == ICS_ICC ? "tag result Game in progress\n" : "commit\n");
5165 CoordsToComputerAlgebraic (int rf, int ff, int rt, int ft, char promoChar, char move[7])
5167 if (rf == DROP_RANK) {
5168 if(ff == EmptySquare) sprintf(move, "@@@@\n"); else // [HGM] pass
5169 sprintf(move, "%c@%c%c\n",
5170 ToUpper(PieceToChar((ChessSquare) ff)), AAA + ft, ONE + rt);
5172 if (promoChar == 'x' || promoChar == NULLCHAR) {
5173 sprintf(move, "%c%c%c%c\n",
5174 AAA + ff, ONE + rf, AAA + ft, ONE + rt);
5176 sprintf(move, "%c%c%c%c%c\n",
5177 AAA + ff, ONE + rf, AAA + ft, ONE + rt, promoChar);
5183 ProcessICSInitScript (FILE *f)
5187 while (fgets(buf, MSG_SIZ, f)) {
5188 SendToICSDelayed(buf,(long)appData.msLoginDelay);
5195 static int lastX, lastY, selectFlag, dragging;
5200 ChessSquare king = WhiteKing, pawn = WhitePawn, last = promoSweep;
5201 if(gameInfo.variant == VariantKnightmate) king = WhiteUnicorn;
5202 if(gameInfo.variant == VariantSuicide || gameInfo.variant == VariantGiveaway) king = EmptySquare;
5203 if(promoSweep >= BlackPawn) king = WHITE_TO_BLACK king, pawn = WHITE_TO_BLACK pawn;
5204 if(gameInfo.variant == VariantSpartan && pawn == BlackPawn) pawn = BlackLance, king = EmptySquare;
5205 if(fromY != BOARD_HEIGHT-2 && fromY != 1) pawn = EmptySquare;
5208 if(promoSweep == EmptySquare) promoSweep = BlackPawn; // wrap
5209 else if((int)promoSweep == -1) promoSweep = WhiteKing;
5210 else if(promoSweep == BlackPawn && step < 0) promoSweep = WhitePawn;
5211 else if(promoSweep == WhiteKing && step > 0) promoSweep = BlackKing;
5212 if(!step) step = -1;
5213 } while(PieceToChar(promoSweep) == '.' || PieceToChar(promoSweep) == '~' || promoSweep == pawn ||
5214 appData.testLegality && (promoSweep == king ||
5215 gameInfo.variant == VariantShogi && promoSweep != PROMOTED last && last != PROMOTED promoSweep && last != promoSweep));
5217 int victim = boards[currentMove][toY][toX];
5218 boards[currentMove][toY][toX] = promoSweep;
5219 DrawPosition(FALSE, boards[currentMove]);
5220 boards[currentMove][toY][toX] = victim;
5222 ChangeDragPiece(promoSweep);
5226 PromoScroll (int x, int y)
5230 if(promoSweep == EmptySquare || !appData.sweepSelect) return FALSE;
5231 if(abs(x - lastX) < 25 && abs(y - lastY) < 25) return FALSE;
5232 if( y > lastY + 2 ) step = -1; else if(y < lastY - 2) step = 1;
5233 if(!step) return FALSE;
5234 lastX = x; lastY = y;
5235 if((promoSweep < BlackPawn) == flipView) step = -step;
5236 if(step > 0) selectFlag = 1;
5237 if(!selectFlag) Sweep(step);
5242 NextPiece (int step)
5244 ChessSquare piece = boards[currentMove][toY][toX];
5247 if(pieceSweep == EmptySquare) pieceSweep = WhitePawn; // wrap
5248 if((int)pieceSweep == -1) pieceSweep = BlackKing;
5249 if(!step) step = -1;
5250 } while(PieceToChar(pieceSweep) == '.');
5251 boards[currentMove][toY][toX] = pieceSweep;
5252 DrawPosition(FALSE, boards[currentMove]);
5253 boards[currentMove][toY][toX] = piece;
5255 /* [HGM] Shogi move preprocessor: swap digits for letters, vice versa */
5257 AlphaRank (char *move, int n)
5259 // char *p = move, c; int x, y;
5261 if (appData.debugMode) {
5262 fprintf(debugFP, "alphaRank(%s,%d)\n", move, n);
5266 move[2]>='0' && move[2]<='9' &&
5267 move[3]>='a' && move[3]<='x' ) {
5269 move[2] = BOARD_RGHT -1 - (move[2]-'1') + AAA;
5270 move[3] = BOARD_HEIGHT-1 - (move[3]-'a') + ONE;
5272 if(move[0]>='0' && move[0]<='9' &&
5273 move[1]>='a' && move[1]<='x' &&
5274 move[2]>='0' && move[2]<='9' &&
5275 move[3]>='a' && move[3]<='x' ) {
5276 /* input move, Shogi -> normal */
5277 move[0] = BOARD_RGHT -1 - (move[0]-'1') + AAA;
5278 move[1] = BOARD_HEIGHT-1 - (move[1]-'a') + ONE;
5279 move[2] = BOARD_RGHT -1 - (move[2]-'1') + AAA;
5280 move[3] = BOARD_HEIGHT-1 - (move[3]-'a') + ONE;
5283 move[3]>='0' && move[3]<='9' &&
5284 move[2]>='a' && move[2]<='x' ) {
5286 move[2] = BOARD_RGHT - 1 - (move[2]-AAA) + '1';
5287 move[3] = BOARD_HEIGHT-1 - (move[3]-ONE) + 'a';
5290 move[0]>='a' && move[0]<='x' &&
5291 move[3]>='0' && move[3]<='9' &&
5292 move[2]>='a' && move[2]<='x' ) {
5293 /* output move, normal -> Shogi */
5294 move[0] = BOARD_RGHT - 1 - (move[0]-AAA) + '1';
5295 move[1] = BOARD_HEIGHT-1 - (move[1]-ONE) + 'a';
5296 move[2] = BOARD_RGHT - 1 - (move[2]-AAA) + '1';
5297 move[3] = BOARD_HEIGHT-1 - (move[3]-ONE) + 'a';
5298 if(move[4] == PieceToChar(BlackQueen)) move[4] = '+';
5300 if (appData.debugMode) {
5301 fprintf(debugFP, " out = '%s'\n", move);
5305 char yy_textstr[8000];
5307 /* Parser for moves from gnuchess, ICS, or user typein box */
5309 ParseOneMove (char *move, int moveNum, ChessMove *moveType, int *fromX, int *fromY, int *toX, int *toY, char *promoChar)
5311 *moveType = yylexstr(moveNum, move, yy_textstr, sizeof yy_textstr);
5313 switch (*moveType) {
5314 case WhitePromotion:
5315 case BlackPromotion:
5316 case WhiteNonPromotion:
5317 case BlackNonPromotion:
5319 case WhiteCapturesEnPassant:
5320 case BlackCapturesEnPassant:
5321 case WhiteKingSideCastle:
5322 case WhiteQueenSideCastle:
5323 case BlackKingSideCastle:
5324 case BlackQueenSideCastle:
5325 case WhiteKingSideCastleWild:
5326 case WhiteQueenSideCastleWild:
5327 case BlackKingSideCastleWild:
5328 case BlackQueenSideCastleWild:
5329 /* Code added by Tord: */
5330 case WhiteHSideCastleFR:
5331 case WhiteASideCastleFR:
5332 case BlackHSideCastleFR:
5333 case BlackASideCastleFR:
5334 /* End of code added by Tord */
5335 case IllegalMove: /* bug or odd chess variant */
5336 *fromX = currentMoveString[0] - AAA;
5337 *fromY = currentMoveString[1] - ONE;
5338 *toX = currentMoveString[2] - AAA;
5339 *toY = currentMoveString[3] - ONE;
5340 *promoChar = currentMoveString[4];
5341 if (*fromX < BOARD_LEFT || *fromX >= BOARD_RGHT || *fromY < 0 || *fromY >= BOARD_HEIGHT ||
5342 *toX < BOARD_LEFT || *toX >= BOARD_RGHT || *toY < 0 || *toY >= BOARD_HEIGHT) {
5343 if (appData.debugMode) {
5344 fprintf(debugFP, "Off-board move (%d,%d)-(%d,%d)%c, type = %d\n", *fromX, *fromY, *toX, *toY, *promoChar, *moveType);
5346 *fromX = *fromY = *toX = *toY = 0;
5349 if (appData.testLegality) {
5350 return (*moveType != IllegalMove);
5352 return !(*fromX == *toX && *fromY == *toY) && boards[moveNum][*fromY][*fromX] != EmptySquare &&
5353 WhiteOnMove(moveNum) == (boards[moveNum][*fromY][*fromX] < BlackPawn);
5358 *fromX = *moveType == WhiteDrop ?
5359 (int) CharToPiece(ToUpper(currentMoveString[0])) :
5360 (int) CharToPiece(ToLower(currentMoveString[0]));
5362 *toX = currentMoveString[2] - AAA;
5363 *toY = currentMoveString[3] - ONE;
5364 *promoChar = NULLCHAR;
5368 case ImpossibleMove:
5378 if (appData.debugMode) {
5379 fprintf(debugFP, "Impossible move %s, type = %d\n", currentMoveString, *moveType);
5382 *fromX = *fromY = *toX = *toY = 0;
5383 *promoChar = NULLCHAR;
5388 Boolean pushed = FALSE;
5389 char *lastParseAttempt;
5392 ParsePV (char *pv, Boolean storeComments, Boolean atEnd)
5393 { // Parse a string of PV moves, and append to current game, behind forwardMostMove
5394 int fromX, fromY, toX, toY; char promoChar;
5399 if (gameMode == AnalyzeMode && currentMove < forwardMostMove) {
5400 PushInner(currentMove, forwardMostMove); // [HGM] engine might not be thinking on forwardMost position!
5403 endPV = forwardMostMove;
5405 while(*pv == ' ' || *pv == '\n' || *pv == '\t') pv++; // must still read away whitespace
5406 if(nr == 0 && !storeComments && *pv == '(') pv++; // first (ponder) move can be in parentheses
5407 lastParseAttempt = pv;
5408 valid = ParseOneMove(pv, endPV, &moveType, &fromX, &fromY, &toX, &toY, &promoChar);
5409 if(!valid && nr == 0 &&
5410 ParseOneMove(pv, endPV-1, &moveType, &fromX, &fromY, &toX, &toY, &promoChar)){
5411 nr++; moveType = Comment; // First move has been played; kludge to make sure we continue
5412 // Hande case where played move is different from leading PV move
5413 CopyBoard(boards[endPV+1], boards[endPV-1]); // tentatively unplay last game move
5414 CopyBoard(boards[endPV+2], boards[endPV-1]); // and play first move of PV
5415 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[endPV+2]);
5416 if(!CompareBoards(boards[endPV], boards[endPV+2])) {
5417 endPV += 2; // if position different, keep this
5418 moveList[endPV-1][0] = fromX + AAA;
5419 moveList[endPV-1][1] = fromY + ONE;
5420 moveList[endPV-1][2] = toX + AAA;
5421 moveList[endPV-1][3] = toY + ONE;
5422 parseList[endPV-1][0] = NULLCHAR;
5423 safeStrCpy(moveList[endPV-2], "_0_0", sizeof(moveList[endPV-2])/sizeof(moveList[endPV-2][0])); // suppress premove highlight on takeback move
5426 pv = strstr(pv, yy_textstr) + strlen(yy_textstr); // skip what we parsed
5427 if(nr == 0 && !storeComments && *pv == ')') pv++; // closing parenthesis of ponder move;
5428 if(moveType == Comment && storeComments) AppendComment(endPV, yy_textstr, FALSE);
5429 if(moveType == Comment || moveType == NAG || moveType == ElapsedTime) {
5430 valid++; // allow comments in PV
5434 if(endPV+1 > framePtr) break; // no space, truncate
5437 CopyBoard(boards[endPV], boards[endPV-1]);
5438 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[endPV]);
5439 CoordsToComputerAlgebraic(fromY, fromX, toY, toX, promoChar, moveList[endPV - 1]);
5440 strncat(moveList[endPV-1], "\n", MOVE_LEN);
5441 CoordsToAlgebraic(boards[endPV - 1],
5442 PosFlags(endPV - 1),
5443 fromY, fromX, toY, toX, promoChar,
5444 parseList[endPV - 1]);
5446 if(atEnd == 2) return; // used hidden, for PV conversion
5447 currentMove = (atEnd || endPV == forwardMostMove) ? endPV : forwardMostMove + 1;
5448 if(currentMove == forwardMostMove) ClearPremoveHighlights(); else
5449 SetPremoveHighlights(moveList[currentMove-1][0]-AAA, moveList[currentMove-1][1]-ONE,
5450 moveList[currentMove-1][2]-AAA, moveList[currentMove-1][3]-ONE);
5451 DrawPosition(TRUE, boards[currentMove]);
5455 MultiPV (ChessProgramState *cps)
5456 { // check if engine supports MultiPV, and if so, return the number of the option that sets it
5458 for(i=0; i<cps->nrOptions; i++)
5459 if(!strcmp(cps->option[i].name, "MultiPV") && cps->option[i].type == Spin)
5465 LoadMultiPV (int x, int y, char *buf, int index, int *start, int *end, int pane)
5467 int startPV, multi, lineStart, origIndex = index;
5468 char *p, buf2[MSG_SIZ];
5469 ChessProgramState *cps = (pane ? &second : &first);
5471 if(index < 0 || index >= strlen(buf)) return FALSE; // sanity
5472 lastX = x; lastY = y;
5473 while(index > 0 && buf[index-1] != '\n') index--; // beginning of line
5474 lineStart = startPV = index;
5475 while(buf[index] != '\n') if(buf[index++] == '\t') startPV = index;
5476 if(index == startPV && (p = StrCaseStr(buf+index, "PV="))) startPV = p - buf + 3;
5478 do{ while(buf[index] && buf[index] != '\n') index++;
5479 } while(buf[index] == '\n' && buf[index+1] == '\\' && buf[index+2] == ' ' && index++); // join kibitzed PV continuation line
5481 if(lineStart == 0 && gameMode == AnalyzeMode && (multi = MultiPV(cps)) >= 0) {
5482 int n = cps->option[multi].value;
5483 if(origIndex > 17 && origIndex < 24) { if(n>1) n--; } else if(origIndex > index - 6) n++;
5484 snprintf(buf2, MSG_SIZ, "option MultiPV=%d\n", n);
5485 if(cps->option[multi].value != n) SendToProgram(buf2, cps);
5486 cps->option[multi].value = n;
5489 } else if(strstr(buf+lineStart, "exclude:") == buf+lineStart) { // exclude moves clicked
5490 ExcludeClick(origIndex - lineStart);
5493 ParsePV(buf+startPV, FALSE, gameMode != AnalyzeMode);
5494 *start = startPV; *end = index-1;
5501 static char buf[10*MSG_SIZ];
5502 int i, k=0, savedEnd=endPV, saveFMM = forwardMostMove;
5504 if(forwardMostMove < endPV) PushInner(forwardMostMove, endPV);
5505 ParsePV(pv, FALSE, 2); // this appends PV to game, suppressing any display of it
5506 for(i = forwardMostMove; i<endPV; i++){
5507 if(i&1) snprintf(buf+k, 10*MSG_SIZ-k, "%s ", parseList[i]);
5508 else snprintf(buf+k, 10*MSG_SIZ-k, "%d. %s ", i/2 + 1, parseList[i]);
5511 snprintf(buf+k, 10*MSG_SIZ-k, "%s", lastParseAttempt); // if we ran into stuff that could not be parsed, print it verbatim
5512 if(forwardMostMove < savedEnd) { PopInner(0); forwardMostMove = saveFMM; } // PopInner would set fmm to endPV!
5518 LoadPV (int x, int y)
5519 { // called on right mouse click to load PV
5520 int which = gameMode == TwoMachinesPlay && (WhiteOnMove(forwardMostMove) == (second.twoMachinesColor[0] == 'w'));
5521 lastX = x; lastY = y;
5522 ParsePV(lastPV[which], FALSE, TRUE); // load the PV of the thinking engine in the boards array.
5529 int oldFMM = forwardMostMove; // N.B.: this was currentMove before PV was loaded!
5530 if(endPV < 0) return;
5531 if(appData.autoCopyPV) CopyFENToClipboard();
5533 if(gameMode == AnalyzeMode && currentMove > forwardMostMove) {
5534 Boolean saveAnimate = appData.animate;
5536 if(shiftKey && storedGames < MAX_VARIATIONS-2) { // wants to start variation, and there is space
5537 if(storedGames == 1) GreyRevert(FALSE); // we already pushed the tail, so just make it official
5538 } else storedGames--; // abandon shelved tail of original game
5541 forwardMostMove = currentMove;
5542 currentMove = oldFMM;
5543 appData.animate = FALSE;
5544 ToNrEvent(forwardMostMove);
5545 appData.animate = saveAnimate;
5547 currentMove = forwardMostMove;
5548 if(pushed) { PopInner(0); pushed = FALSE; } // restore shelved game continuation
5549 ClearPremoveHighlights();
5550 DrawPosition(TRUE, boards[currentMove]);
5554 MovePV (int x, int y, int h)
5555 { // step through PV based on mouse coordinates (called on mouse move)
5556 int margin = h>>3, step = 0, threshold = (pieceSweep == EmptySquare ? 10 : 15);
5558 // we must somehow check if right button is still down (might be released off board!)
5559 if(endPV < 0 && pieceSweep == EmptySquare) return; // needed in XBoard because lastX/Y is shared :-(
5560 if(abs(x - lastX) < threshold && abs(y - lastY) < threshold) return;
5561 if( y > lastY + 2 ) step = -1; else if(y < lastY - 2) step = 1;
5563 lastX = x; lastY = y;
5565 if(pieceSweep != EmptySquare) { NextPiece(step); return; }
5566 if(endPV < 0) return;
5567 if(y < margin) step = 1; else
5568 if(y > h - margin) step = -1;
5569 if(currentMove + step > endPV || currentMove + step < forwardMostMove) step = 0;
5570 currentMove += step;
5571 if(currentMove == forwardMostMove) ClearPremoveHighlights(); else
5572 SetPremoveHighlights(moveList[currentMove-1][0]-AAA, moveList[currentMove-1][1]-ONE,
5573 moveList[currentMove-1][2]-AAA, moveList[currentMove-1][3]-ONE);
5574 DrawPosition(FALSE, boards[currentMove]);
5578 // [HGM] shuffle: a general way to suffle opening setups, applicable to arbitrary variants.
5579 // All positions will have equal probability, but the current method will not provide a unique
5580 // numbering scheme for arrays that contain 3 or more pieces of the same kind.
5586 int piecesLeft[(int)BlackPawn];
5587 int seed, nrOfShuffles;
5590 GetPositionNumber ()
5591 { // sets global variable seed
5594 seed = appData.defaultFrcPosition;
5595 if(seed < 0) { // randomize based on time for negative FRC position numbers
5596 for(i=0; i<50; i++) seed += random();
5597 seed = random() ^ random() >> 8 ^ random() << 8;
5598 if(seed<0) seed = -seed;
5603 put (Board board, int pieceType, int rank, int n, int shade)
5604 // put the piece on the (n-1)-th empty squares of the given shade
5608 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) {
5609 if( (((i-BOARD_LEFT)&1)+1) & shade && board[rank][i] == EmptySquare && n-- == 0) {
5610 board[rank][i] = (ChessSquare) pieceType;
5611 squaresLeft[((i-BOARD_LEFT)&1) + 1]--;
5613 piecesLeft[pieceType]--;
5622 AddOnePiece (Board board, int pieceType, int rank, int shade)
5623 // calculate where the next piece goes, (any empty square), and put it there
5627 i = seed % squaresLeft[shade];
5628 nrOfShuffles *= squaresLeft[shade];
5629 seed /= squaresLeft[shade];
5630 put(board, pieceType, rank, i, shade);
5634 AddTwoPieces (Board board, int pieceType, int rank)
5635 // calculate where the next 2 identical pieces go, (any empty square), and put it there
5637 int i, n=squaresLeft[ANY], j=n-1, k;
5639 k = n*(n-1)/2; // nr of possibilities, not counting permutations
5640 i = seed % k; // pick one
5643 while(i >= j) i -= j--;
5644 j = n - 1 - j; i += j;
5645 put(board, pieceType, rank, j, ANY);
5646 put(board, pieceType, rank, i, ANY);
5650 SetUpShuffle (Board board, int number)
5654 GetPositionNumber(); nrOfShuffles = 1;
5656 squaresLeft[DARK] = (BOARD_RGHT - BOARD_LEFT + 1)/2;
5657 squaresLeft[ANY] = BOARD_RGHT - BOARD_LEFT;
5658 squaresLeft[LITE] = squaresLeft[ANY] - squaresLeft[DARK];
5660 for(p = 0; p<=(int)WhiteKing; p++) piecesLeft[p] = 0;
5662 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // count pieces and clear board
5663 p = (int) board[0][i];
5664 if(p < (int) BlackPawn) piecesLeft[p] ++;
5665 board[0][i] = EmptySquare;
5668 if(PosFlags(0) & F_ALL_CASTLE_OK) {
5669 // shuffles restricted to allow normal castling put KRR first
5670 if(piecesLeft[(int)WhiteKing]) // King goes rightish of middle
5671 put(board, WhiteKing, 0, (gameInfo.boardWidth+1)/2, ANY);
5672 else if(piecesLeft[(int)WhiteUnicorn]) // in Knightmate Unicorn castles
5673 put(board, WhiteUnicorn, 0, (gameInfo.boardWidth+1)/2, ANY);
5674 if(piecesLeft[(int)WhiteRook]) // First supply a Rook for K-side castling
5675 put(board, WhiteRook, 0, gameInfo.boardWidth-2, ANY);
5676 if(piecesLeft[(int)WhiteRook]) // Then supply a Rook for Q-side castling
5677 put(board, WhiteRook, 0, 0, ANY);
5678 // in variants with super-numerary Kings and Rooks, we leave these for the shuffle
5681 if(((BOARD_RGHT-BOARD_LEFT) & 1) == 0)
5682 // only for even boards make effort to put pairs of colorbound pieces on opposite colors
5683 for(p = (int) WhiteKing; p > (int) WhitePawn; p--) {
5684 if(p != (int) WhiteBishop && p != (int) WhiteFerz && p != (int) WhiteAlfil) continue;
5685 while(piecesLeft[p] >= 2) {
5686 AddOnePiece(board, p, 0, LITE);
5687 AddOnePiece(board, p, 0, DARK);
5689 // Odd color-bound pieces are shuffled with the rest (to not run out of paired squares)
5692 for(p = (int) WhiteKing - 2; p > (int) WhitePawn; p--) {
5693 // Remaining pieces (non-colorbound, or odd color bound) can be put anywhere
5694 // but we leave King and Rooks for last, to possibly obey FRC restriction
5695 if(p == (int)WhiteRook) continue;
5696 while(piecesLeft[p] >= 2) AddTwoPieces(board, p, 0); // add in pairs, for not counting permutations
5697 if(piecesLeft[p]) AddOnePiece(board, p, 0, ANY); // add the odd piece
5700 // now everything is placed, except perhaps King (Unicorn) and Rooks
5702 if(PosFlags(0) & F_FRC_TYPE_CASTLING) {
5703 // Last King gets castling rights
5704 while(piecesLeft[(int)WhiteUnicorn]) {
5705 i = put(board, WhiteUnicorn, 0, piecesLeft[(int)WhiteRook]/2, ANY);
5706 initialRights[2] = initialRights[5] = board[CASTLING][2] = board[CASTLING][5] = i;
5709 while(piecesLeft[(int)WhiteKing]) {
5710 i = put(board, WhiteKing, 0, piecesLeft[(int)WhiteRook]/2, ANY);
5711 initialRights[2] = initialRights[5] = board[CASTLING][2] = board[CASTLING][5] = i;
5716 while(piecesLeft[(int)WhiteKing]) AddOnePiece(board, WhiteKing, 0, ANY);
5717 while(piecesLeft[(int)WhiteUnicorn]) AddOnePiece(board, WhiteUnicorn, 0, ANY);
5720 // Only Rooks can be left; simply place them all
5721 while(piecesLeft[(int)WhiteRook]) {
5722 i = put(board, WhiteRook, 0, 0, ANY);
5723 if(PosFlags(0) & F_FRC_TYPE_CASTLING) { // first and last Rook get FRC castling rights
5726 initialRights[1] = initialRights[4] = board[CASTLING][1] = board[CASTLING][4] = i;
5728 initialRights[0] = initialRights[3] = board[CASTLING][0] = board[CASTLING][3] = i;
5731 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // copy black from white
5732 board[BOARD_HEIGHT-1][i] = (int) board[0][i] < BlackPawn ? WHITE_TO_BLACK board[0][i] : EmptySquare;
5735 if(number >= 0) appData.defaultFrcPosition %= nrOfShuffles; // normalize
5739 SetCharTable (char *table, const char * map)
5740 /* [HGM] moved here from winboard.c because of its general usefulness */
5741 /* Basically a safe strcpy that uses the last character as King */
5743 int result = FALSE; int NrPieces;
5745 if( map != NULL && (NrPieces=strlen(map)) <= (int) EmptySquare
5746 && NrPieces >= 12 && !(NrPieces&1)) {
5747 int i; /* [HGM] Accept even length from 12 to 34 */
5749 for( i=0; i<(int) EmptySquare; i++ ) table[i] = '.';
5750 for( i=0; i<NrPieces/2-1; i++ ) {
5752 table[i + (int)BlackPawn - (int) WhitePawn] = map[i+NrPieces/2];
5754 table[(int) WhiteKing] = map[NrPieces/2-1];
5755 table[(int) BlackKing] = map[NrPieces-1];
5764 Prelude (Board board)
5765 { // [HGM] superchess: random selection of exo-pieces
5766 int i, j, k; ChessSquare p;
5767 static ChessSquare exoPieces[4] = { WhiteAngel, WhiteMarshall, WhiteSilver, WhiteLance };
5769 GetPositionNumber(); // use FRC position number
5771 if(appData.pieceToCharTable != NULL) { // select pieces to participate from given char table
5772 SetCharTable(pieceToChar, appData.pieceToCharTable);
5773 for(i=(int)WhiteQueen+1, j=0; i<(int)WhiteKing && j<4; i++)
5774 if(PieceToChar((ChessSquare)i) != '.') exoPieces[j++] = (ChessSquare) i;
5777 j = seed%4; seed /= 4;
5778 p = board[0][BOARD_LEFT+j]; board[0][BOARD_LEFT+j] = EmptySquare; k = PieceToNumber(p);
5779 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5780 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5781 j = seed%3 + (seed%3 >= j); seed /= 3;
5782 p = board[0][BOARD_LEFT+j]; board[0][BOARD_LEFT+j] = EmptySquare; k = PieceToNumber(p);
5783 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5784 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5785 j = seed%3; seed /= 3;
5786 p = board[0][BOARD_LEFT+j+5]; board[0][BOARD_LEFT+j+5] = EmptySquare; k = PieceToNumber(p);
5787 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5788 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5789 j = seed%2 + (seed%2 >= j); seed /= 2;
5790 p = board[0][BOARD_LEFT+j+5]; board[0][BOARD_LEFT+j+5] = EmptySquare; k = PieceToNumber(p);
5791 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5792 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5793 j = seed%4; seed /= 4; put(board, exoPieces[3], 0, j, ANY);
5794 j = seed%3; seed /= 3; put(board, exoPieces[2], 0, j, ANY);
5795 j = seed%2; seed /= 2; put(board, exoPieces[1], 0, j, ANY);
5796 put(board, exoPieces[0], 0, 0, ANY);
5797 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) board[BOARD_HEIGHT-1][i] = WHITE_TO_BLACK board[0][i];
5801 InitPosition (int redraw)
5803 ChessSquare (* pieces)[BOARD_FILES];
5804 int i, j, pawnRow, overrule,
5805 oldx = gameInfo.boardWidth,
5806 oldy = gameInfo.boardHeight,
5807 oldh = gameInfo.holdingsWidth;
5810 if(appData.icsActive) shuffleOpenings = FALSE; // [HGM] shuffle: in ICS mode, only shuffle on ICS request
5812 /* [AS] Initialize pv info list [HGM] and game status */
5814 for( i=0; i<=framePtr; i++ ) { // [HGM] vari: spare saved variations
5815 pvInfoList[i].depth = 0;
5816 boards[i][EP_STATUS] = EP_NONE;
5817 for( j=0; j<BOARD_FILES-2; j++ ) boards[i][CASTLING][j] = NoRights;
5820 initialRulePlies = 0; /* 50-move counter start */
5822 castlingRank[0] = castlingRank[1] = castlingRank[2] = 0;
5823 castlingRank[3] = castlingRank[4] = castlingRank[5] = BOARD_HEIGHT-1;
5827 /* [HGM] logic here is completely changed. In stead of full positions */
5828 /* the initialized data only consist of the two backranks. The switch */
5829 /* selects which one we will use, which is than copied to the Board */
5830 /* initialPosition, which for the rest is initialized by Pawns and */
5831 /* empty squares. This initial position is then copied to boards[0], */
5832 /* possibly after shuffling, so that it remains available. */
5834 gameInfo.holdingsWidth = 0; /* default board sizes */
5835 gameInfo.boardWidth = 8;
5836 gameInfo.boardHeight = 8;
5837 gameInfo.holdingsSize = 0;
5838 nrCastlingRights = -1; /* [HGM] Kludge to indicate default should be used */
5839 for(i=0; i<BOARD_FILES-2; i++)
5840 initialPosition[CASTLING][i] = initialRights[i] = NoRights; /* but no rights yet */
5841 initialPosition[EP_STATUS] = EP_NONE;
5842 SetCharTable(pieceToChar, "PNBRQ...........Kpnbrq...........k");
5843 if(startVariant == gameInfo.variant) // [HGM] nicks: enable nicknames in original variant
5844 SetCharTable(pieceNickName, appData.pieceNickNames);
5845 else SetCharTable(pieceNickName, "............");
5848 switch (gameInfo.variant) {
5849 case VariantFischeRandom:
5850 shuffleOpenings = TRUE;
5853 case VariantShatranj:
5854 pieces = ShatranjArray;
5855 nrCastlingRights = 0;
5856 SetCharTable(pieceToChar, "PN.R.QB...Kpn.r.qb...k");
5859 pieces = makrukArray;
5860 nrCastlingRights = 0;
5861 startedFromSetupPosition = TRUE;
5862 SetCharTable(pieceToChar, "PN.R.M....SKpn.r.m....sk");
5864 case VariantTwoKings:
5865 pieces = twoKingsArray;
5868 pieces = GrandArray;
5869 nrCastlingRights = 0;
5870 SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
5871 gameInfo.boardWidth = 10;
5872 gameInfo.boardHeight = 10;
5873 gameInfo.holdingsSize = 7;
5875 case VariantCapaRandom:
5876 shuffleOpenings = TRUE;
5877 case VariantCapablanca:
5878 pieces = CapablancaArray;
5879 gameInfo.boardWidth = 10;
5880 SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
5883 pieces = GothicArray;
5884 gameInfo.boardWidth = 10;
5885 SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
5888 SetCharTable(pieceToChar, "PNBRQ..HEKpnbrq..hek");
5889 gameInfo.holdingsSize = 7;
5890 for(i=0; i<BOARD_FILES; i++) initialPosition[VIRGIN][i] = VIRGIN_W | VIRGIN_B;
5893 pieces = JanusArray;
5894 gameInfo.boardWidth = 10;
5895 SetCharTable(pieceToChar, "PNBRQ..JKpnbrq..jk");
5896 nrCastlingRights = 6;
5897 initialPosition[CASTLING][0] = initialRights[0] = BOARD_RGHT-1;
5898 initialPosition[CASTLING][1] = initialRights[1] = BOARD_LEFT;
5899 initialPosition[CASTLING][2] = initialRights[2] =(BOARD_WIDTH-1)>>1;
5900 initialPosition[CASTLING][3] = initialRights[3] = BOARD_RGHT-1;
5901 initialPosition[CASTLING][4] = initialRights[4] = BOARD_LEFT;
5902 initialPosition[CASTLING][5] = initialRights[5] =(BOARD_WIDTH-1)>>1;
5905 pieces = FalconArray;
5906 gameInfo.boardWidth = 10;
5907 SetCharTable(pieceToChar, "PNBRQ.............FKpnbrq.............fk");
5909 case VariantXiangqi:
5910 pieces = XiangqiArray;
5911 gameInfo.boardWidth = 9;
5912 gameInfo.boardHeight = 10;
5913 nrCastlingRights = 0;
5914 SetCharTable(pieceToChar, "PH.R.AE..K.C.ph.r.ae..k.c.");
5917 pieces = ShogiArray;
5918 gameInfo.boardWidth = 9;
5919 gameInfo.boardHeight = 9;
5920 gameInfo.holdingsSize = 7;
5921 nrCastlingRights = 0;
5922 SetCharTable(pieceToChar, "PNBRLS...G.++++++Kpnbrls...g.++++++k");
5924 case VariantCourier:
5925 pieces = CourierArray;
5926 gameInfo.boardWidth = 12;
5927 nrCastlingRights = 0;
5928 SetCharTable(pieceToChar, "PNBR.FE..WMKpnbr.fe..wmk");
5930 case VariantKnightmate:
5931 pieces = KnightmateArray;
5932 SetCharTable(pieceToChar, "P.BRQ.....M.........K.p.brq.....m.........k.");
5934 case VariantSpartan:
5935 pieces = SpartanArray;
5936 SetCharTable(pieceToChar, "PNBRQ................K......lwg.....c...h..k");
5939 pieces = fairyArray;
5940 SetCharTable(pieceToChar, "PNBRQFEACWMOHIJGDVLSUKpnbrqfeacwmohijgdvlsuk");
5943 pieces = GreatArray;
5944 gameInfo.boardWidth = 10;
5945 SetCharTable(pieceToChar, "PN....E...S..HWGMKpn....e...s..hwgmk");
5946 gameInfo.holdingsSize = 8;
5950 SetCharTable(pieceToChar, "PNBRQ..SE.......V.AKpnbrq..se.......v.ak");
5951 gameInfo.holdingsSize = 8;
5952 startedFromSetupPosition = TRUE;
5954 case VariantCrazyhouse:
5955 case VariantBughouse:
5957 SetCharTable(pieceToChar, "PNBRQ.......~~~~Kpnbrq.......~~~~k");
5958 gameInfo.holdingsSize = 5;
5960 case VariantWildCastle:
5962 /* !!?shuffle with kings guaranteed to be on d or e file */
5963 shuffleOpenings = 1;
5965 case VariantNoCastle:
5967 nrCastlingRights = 0;
5968 /* !!?unconstrained back-rank shuffle */
5969 shuffleOpenings = 1;
5974 if(appData.NrFiles >= 0) {
5975 if(gameInfo.boardWidth != appData.NrFiles) overrule++;
5976 gameInfo.boardWidth = appData.NrFiles;
5978 if(appData.NrRanks >= 0) {
5979 gameInfo.boardHeight = appData.NrRanks;
5981 if(appData.holdingsSize >= 0) {
5982 i = appData.holdingsSize;
5983 if(i > gameInfo.boardHeight) i = gameInfo.boardHeight;
5984 gameInfo.holdingsSize = i;
5986 if(gameInfo.holdingsSize) gameInfo.holdingsWidth = 2;
5987 if(BOARD_HEIGHT > BOARD_RANKS || BOARD_WIDTH > BOARD_FILES)
5988 DisplayFatalError(_("Recompile to support this BOARD_RANKS or BOARD_FILES!"), 0, 2);
5990 pawnRow = gameInfo.boardHeight - 7; /* seems to work in all common variants */
5991 if(pawnRow < 1) pawnRow = 1;
5992 if(gameInfo.variant == VariantMakruk || gameInfo.variant == VariantGrand) pawnRow = 2;
5994 /* User pieceToChar list overrules defaults */
5995 if(appData.pieceToCharTable != NULL)
5996 SetCharTable(pieceToChar, appData.pieceToCharTable);
5998 for( j=0; j<BOARD_WIDTH; j++ ) { ChessSquare s = EmptySquare;
6000 if(j==BOARD_LEFT-1 || j==BOARD_RGHT)
6001 s = (ChessSquare) 0; /* account holding counts in guard band */
6002 for( i=0; i<BOARD_HEIGHT; i++ )
6003 initialPosition[i][j] = s;
6005 if(j < BOARD_LEFT || j >= BOARD_RGHT || overrule) continue;
6006 initialPosition[gameInfo.variant == VariantGrand][j] = pieces[0][j-gameInfo.holdingsWidth];
6007 initialPosition[pawnRow][j] = WhitePawn;
6008 initialPosition[BOARD_HEIGHT-pawnRow-1][j] = gameInfo.variant == VariantSpartan ? BlackLance : BlackPawn;
6009 if(gameInfo.variant == VariantXiangqi) {
6011 initialPosition[pawnRow][j] =
6012 initialPosition[BOARD_HEIGHT-pawnRow-1][j] = EmptySquare;
6013 if(j==BOARD_LEFT+1 || j>=BOARD_RGHT-2) {
6014 initialPosition[2][j] = WhiteCannon;
6015 initialPosition[BOARD_HEIGHT-3][j] = BlackCannon;
6019 if(gameInfo.variant == VariantGrand) {
6020 if(j==BOARD_LEFT || j>=BOARD_RGHT-1) {
6021 initialPosition[0][j] = WhiteRook;
6022 initialPosition[BOARD_HEIGHT-1][j] = BlackRook;
6025 initialPosition[BOARD_HEIGHT-1-(gameInfo.variant == VariantGrand)][j] = pieces[1][j-gameInfo.holdingsWidth];
6027 if( (gameInfo.variant == VariantShogi) && !overrule ) {
6030 initialPosition[1][j] = WhiteBishop;
6031 initialPosition[BOARD_HEIGHT-2][j] = BlackRook;
6033 initialPosition[1][j] = WhiteRook;
6034 initialPosition[BOARD_HEIGHT-2][j] = BlackBishop;
6037 if( nrCastlingRights == -1) {
6038 /* [HGM] Build normal castling rights (must be done after board sizing!) */
6039 /* This sets default castling rights from none to normal corners */
6040 /* Variants with other castling rights must set them themselves above */
6041 nrCastlingRights = 6;
6043 initialPosition[CASTLING][0] = initialRights[0] = BOARD_RGHT-1;
6044 initialPosition[CASTLING][1] = initialRights[1] = BOARD_LEFT;
6045 initialPosition[CASTLING][2] = initialRights[2] = BOARD_WIDTH>>1;
6046 initialPosition[CASTLING][3] = initialRights[3] = BOARD_RGHT-1;
6047 initialPosition[CASTLING][4] = initialRights[4] = BOARD_LEFT;
6048 initialPosition[CASTLING][5] = initialRights[5] = BOARD_WIDTH>>1;
6051 if(gameInfo.variant == VariantSuper) Prelude(initialPosition);
6052 if(gameInfo.variant == VariantGreat) { // promotion commoners
6053 initialPosition[PieceToNumber(WhiteMan)][BOARD_WIDTH-1] = WhiteMan;
6054 initialPosition[PieceToNumber(WhiteMan)][BOARD_WIDTH-2] = 9;
6055 initialPosition[BOARD_HEIGHT-1-PieceToNumber(WhiteMan)][0] = BlackMan;
6056 initialPosition[BOARD_HEIGHT-1-PieceToNumber(WhiteMan)][1] = 9;
6058 if( gameInfo.variant == VariantSChess ) {
6059 initialPosition[1][0] = BlackMarshall;
6060 initialPosition[2][0] = BlackAngel;
6061 initialPosition[6][BOARD_WIDTH-1] = WhiteMarshall;
6062 initialPosition[5][BOARD_WIDTH-1] = WhiteAngel;
6063 initialPosition[1][1] = initialPosition[2][1] =
6064 initialPosition[6][BOARD_WIDTH-2] = initialPosition[5][BOARD_WIDTH-2] = 1;
6066 if (appData.debugMode) {
6067 fprintf(debugFP, "shuffleOpenings = %d\n", shuffleOpenings);
6069 if(shuffleOpenings) {
6070 SetUpShuffle(initialPosition, appData.defaultFrcPosition);
6071 startedFromSetupPosition = TRUE;
6073 if(startedFromPositionFile) {
6074 /* [HGM] loadPos: use PositionFile for every new game */
6075 CopyBoard(initialPosition, filePosition);
6076 for(i=0; i<nrCastlingRights; i++)
6077 initialRights[i] = filePosition[CASTLING][i];
6078 startedFromSetupPosition = TRUE;
6081 CopyBoard(boards[0], initialPosition);
6083 if(oldx != gameInfo.boardWidth ||
6084 oldy != gameInfo.boardHeight ||
6085 oldv != gameInfo.variant ||
6086 oldh != gameInfo.holdingsWidth
6088 InitDrawingSizes(-2 ,0);
6090 oldv = gameInfo.variant;
6092 DrawPosition(TRUE, boards[currentMove]);
6096 SendBoard (ChessProgramState *cps, int moveNum)
6098 char message[MSG_SIZ];
6100 if (cps->useSetboard) {
6101 char* fen = PositionToFEN(moveNum, cps->fenOverride);
6102 snprintf(message, MSG_SIZ,"setboard %s\n", fen);
6103 SendToProgram(message, cps);
6108 int i, j, left=0, right=BOARD_WIDTH;
6109 /* Kludge to set black to move, avoiding the troublesome and now
6110 * deprecated "black" command.
6112 if (!WhiteOnMove(moveNum)) // [HGM] but better a deprecated command than an illegal move...
6113 SendToProgram(boards[0][1][BOARD_LEFT] == WhitePawn ? "a2a3\n" : "black\n", cps);
6115 if(!cps->extendedEdit) left = BOARD_LEFT, right = BOARD_RGHT; // only board proper
6117 SendToProgram("edit\n", cps);
6118 SendToProgram("#\n", cps);
6119 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
6120 bp = &boards[moveNum][i][left];
6121 for (j = left; j < right; j++, bp++) {
6122 if(j == BOARD_LEFT-1 || j == BOARD_RGHT) continue;
6123 if ((int) *bp < (int) BlackPawn) {
6124 if(j == BOARD_RGHT+1)
6125 snprintf(message, MSG_SIZ, "%c@%d\n", PieceToChar(*bp), bp[-1]);
6126 else snprintf(message, MSG_SIZ, "%c%c%c\n", PieceToChar(*bp), AAA + j, ONE + i);
6127 if(message[0] == '+' || message[0] == '~') {
6128 snprintf(message, MSG_SIZ,"%c%c%c+\n",
6129 PieceToChar((ChessSquare)(DEMOTED *bp)),
6132 if(cps->alphaRank) { /* [HGM] shogi: translate coords */
6133 message[1] = BOARD_RGHT - 1 - j + '1';
6134 message[2] = BOARD_HEIGHT - 1 - i + 'a';
6136 SendToProgram(message, cps);
6141 SendToProgram("c\n", cps);
6142 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
6143 bp = &boards[moveNum][i][left];
6144 for (j = left; j < right; j++, bp++) {
6145 if(j == BOARD_LEFT-1 || j == BOARD_RGHT) continue;
6146 if (((int) *bp != (int) EmptySquare)
6147 && ((int) *bp >= (int) BlackPawn)) {
6148 if(j == BOARD_LEFT-2)
6149 snprintf(message, MSG_SIZ, "%c@%d\n", ToUpper(PieceToChar(*bp)), bp[1]);
6150 else snprintf(message,MSG_SIZ, "%c%c%c\n", ToUpper(PieceToChar(*bp)),
6152 if(message[0] == '+' || message[0] == '~') {
6153 snprintf(message, MSG_SIZ,"%c%c%c+\n",
6154 PieceToChar((ChessSquare)(DEMOTED *bp)),
6157 if(cps->alphaRank) { /* [HGM] shogi: translate coords */
6158 message[1] = BOARD_RGHT - 1 - j + '1';
6159 message[2] = BOARD_HEIGHT - 1 - i + 'a';
6161 SendToProgram(message, cps);
6166 SendToProgram(".\n", cps);
6168 setboardSpoiledMachineBlack = 0; /* [HGM] assume WB 4.2.7 already solves this after sending setboard */
6171 char exclusionHeader[MSG_SIZ];
6172 int exCnt, excludePtr;
6173 typedef struct { int ff, fr, tf, tr, pc, mark; } Exclusion;
6174 static Exclusion excluTab[200];
6175 static char excludeMap[(BOARD_RANKS*BOARD_FILES*BOARD_RANKS*BOARD_FILES+7)/8]; // [HGM] exclude: bitmap for excluced moves
6181 for(j=0; j<(BOARD_RANKS*BOARD_FILES*BOARD_RANKS*BOARD_FILES+7)/8; j++) excludeMap[j] = s;
6182 exclusionHeader[19] = s ? '-' : '+'; // update tail state
6188 safeStrCpy(exclusionHeader, "exclude: none best +tail \n", MSG_SIZ);
6189 excludePtr = 24; exCnt = 0;
6194 UpdateExcludeHeader (int fromY, int fromX, int toY, int toX, char promoChar, char state)
6195 { // search given move in table of header moves, to know where it is listed (and add if not there), and update state
6196 char buf[2*MOVE_LEN], *p;
6197 Exclusion *e = excluTab;
6199 for(i=0; i<exCnt; i++)
6200 if(e[i].ff == fromX && e[i].fr == fromY &&
6201 e[i].tf == toX && e[i].tr == toY && e[i].pc == promoChar) break;
6202 if(i == exCnt) { // was not in exclude list; add it
6203 CoordsToAlgebraic(boards[currentMove], PosFlags(currentMove), fromY, fromX, toY, toX, promoChar, buf);
6204 if(strlen(exclusionHeader + excludePtr) < strlen(buf)) { // no space to write move
6205 if(state != exclusionHeader[19]) exclusionHeader[19] = '*'; // tail is now in mixed state
6208 e[i].ff = fromX; e[i].fr = fromY; e[i].tf = toX; e[i].tr = toY; e[i].pc = promoChar;
6209 excludePtr++; e[i].mark = excludePtr++;
6210 for(p=buf; *p; p++) exclusionHeader[excludePtr++] = *p; // copy move
6213 exclusionHeader[e[i].mark] = state;
6217 ExcludeOneMove (int fromY, int fromX, int toY, int toX, char promoChar, char state)
6218 { // include or exclude the given move, as specified by state ('+' or '-'), or toggle
6222 if((signed char)promoChar == -1) { // kludge to indicate best move
6223 if(!ParseOneMove(lastPV[0], currentMove, &moveType, &fromX, &fromY, &toX, &toY, &promoChar)) // get current best move from last PV
6224 return 1; // if unparsable, abort
6226 // update exclusion map (resolving toggle by consulting existing state)
6227 k=(BOARD_FILES*fromY+fromX)*BOARD_RANKS*BOARD_FILES + (BOARD_FILES*toY+toX);
6229 if(state == '*') state = (excludeMap[k] & 1<<j ? '+' : '-'); // toggle
6230 if(state == '-' && !promoChar) // only non-promotions get marked as excluded, to allow exclusion of under-promotions
6231 excludeMap[k] |= 1<<j;
6232 else excludeMap[k] &= ~(1<<j);
6234 UpdateExcludeHeader(fromY, fromX, toY, toX, promoChar, state);
6236 snprintf(buf, MSG_SIZ, "%sclude ", state == '+' ? "in" : "ex");
6237 CoordsToComputerAlgebraic(fromY, fromX, toY, toX, promoChar, buf+8);
6239 return (state == '+');
6243 ExcludeClick (int index)
6246 Exclusion *e = excluTab;
6247 if(index < 25) { // none, best or tail clicked
6248 if(index < 13) { // none: include all
6249 WriteMap(0); // clear map
6250 for(i=0; i<exCnt; i++) exclusionHeader[excluTab[i].mark] = '+'; // and moves
6251 SendToBoth("include all\n"); // and inform engine
6252 } else if(index > 18) { // tail
6253 if(exclusionHeader[19] == '-') { // tail was excluded
6254 SendToBoth("include all\n");
6255 WriteMap(0); // clear map completely
6256 // now re-exclude selected moves
6257 for(i=0; i<exCnt; i++) if(exclusionHeader[e[i].mark] == '-')
6258 ExcludeOneMove(e[i].fr, e[i].ff, e[i].tr, e[i].tf, e[i].pc, '-');
6259 } else { // tail was included or in mixed state
6260 SendToBoth("exclude all\n");
6261 WriteMap(0xFF); // fill map completely
6262 // now re-include selected moves
6263 j = 0; // count them
6264 for(i=0; i<exCnt; i++) if(exclusionHeader[e[i].mark] == '+')
6265 ExcludeOneMove(e[i].fr, e[i].ff, e[i].tr, e[i].tf, e[i].pc, '+'), j++;
6266 if(!j) ExcludeOneMove(0, 0, 0, 0, -1, '+'); // if no moves were selected, keep best
6269 ExcludeOneMove(0, 0, 0, 0, -1, '-'); // exclude it
6272 for(i=0; i<exCnt; i++) if(i == exCnt-1 || excluTab[i+1].mark > index) {
6273 char *p=exclusionHeader + excluTab[i].mark; // do trust header more than map (promotions!)
6274 ExcludeOneMove(e[i].fr, e[i].ff, e[i].tr, e[i].tf, e[i].pc, *p == '+' ? '-' : '+');
6281 DefaultPromoChoice (int white)
6284 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
6285 result = WhiteFerz; // no choice
6286 else if(gameInfo.variant == VariantSuicide || gameInfo.variant == VariantGiveaway)
6287 result= WhiteKing; // in Suicide Q is the last thing we want
6288 else if(gameInfo.variant == VariantSpartan)
6289 result = white ? WhiteQueen : WhiteAngel;
6290 else result = WhiteQueen;
6291 if(!white) result = WHITE_TO_BLACK result;
6295 static int autoQueen; // [HGM] oneclick
6298 HasPromotionChoice (int fromX, int fromY, int toX, int toY, char *promoChoice, int sweepSelect)
6300 /* [HGM] rewritten IsPromotion to only flag promotions that offer a choice */
6301 /* [HGM] add Shogi promotions */
6302 int promotionZoneSize=1, highestPromotingPiece = (int)WhitePawn;
6307 if(fromX < BOARD_LEFT || fromX >= BOARD_RGHT) return FALSE; // drop
6308 if(toX < BOARD_LEFT || toX >= BOARD_RGHT) return FALSE; // move into holdings
6310 if(gameMode == EditPosition || gameInfo.variant == VariantXiangqi || // no promotions
6311 !(fromX >=0 && fromY >= 0 && toX >= 0 && toY >= 0) ) // invalid move
6314 piece = boards[currentMove][fromY][fromX];
6315 if(gameInfo.variant == VariantShogi) {
6316 promotionZoneSize = BOARD_HEIGHT/3;
6317 highestPromotingPiece = (int)WhiteFerz;
6318 } else if(gameInfo.variant == VariantMakruk || gameInfo.variant == VariantGrand) {
6319 promotionZoneSize = 3;
6322 // Treat Lance as Pawn when it is not representing Amazon
6323 if(gameInfo.variant != VariantSuper) {
6324 if(piece == WhiteLance) piece = WhitePawn; else
6325 if(piece == BlackLance) piece = BlackPawn;
6328 // next weed out all moves that do not touch the promotion zone at all
6329 if((int)piece >= BlackPawn) {
6330 if(toY >= promotionZoneSize && fromY >= promotionZoneSize)
6332 highestPromotingPiece = WHITE_TO_BLACK highestPromotingPiece;
6334 if( toY < BOARD_HEIGHT - promotionZoneSize &&
6335 fromY < BOARD_HEIGHT - promotionZoneSize) return FALSE;
6338 if( (int)piece > highestPromotingPiece ) return FALSE; // non-promoting piece
6340 // weed out mandatory Shogi promotions
6341 if(gameInfo.variant == VariantShogi) {
6342 if(piece >= BlackPawn) {
6343 if(toY == 0 && piece == BlackPawn ||
6344 toY == 0 && piece == BlackQueen ||
6345 toY <= 1 && piece == BlackKnight) {
6350 if(toY == BOARD_HEIGHT-1 && piece == WhitePawn ||
6351 toY == BOARD_HEIGHT-1 && piece == WhiteQueen ||
6352 toY >= BOARD_HEIGHT-2 && piece == WhiteKnight) {
6359 // weed out obviously illegal Pawn moves
6360 if(appData.testLegality && (piece == WhitePawn || piece == BlackPawn) ) {
6361 if(toX > fromX+1 || toX < fromX-1) return FALSE; // wide
6362 if(piece == WhitePawn && toY != fromY+1) return FALSE; // deep
6363 if(piece == BlackPawn && toY != fromY-1) return FALSE; // deep
6364 if(fromX != toX && gameInfo.variant == VariantShogi) return FALSE;
6365 // note we are not allowed to test for valid (non-)capture, due to premove
6368 // we either have a choice what to promote to, or (in Shogi) whether to promote
6369 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk) {
6370 *promoChoice = PieceToChar(BlackFerz); // no choice
6373 // no sense asking what we must promote to if it is going to explode...
6374 if(gameInfo.variant == VariantAtomic && boards[currentMove][toY][toX] != EmptySquare) {
6375 *promoChoice = PieceToChar(BlackQueen); // Queen as good as any
6378 // give caller the default choice even if we will not make it
6379 *promoChoice = ToLower(PieceToChar(defaultPromoChoice));
6380 if(gameInfo.variant == VariantShogi) *promoChoice = (defaultPromoChoice == piece ? '=' : '+');
6381 if( sweepSelect && gameInfo.variant != VariantGreat
6382 && gameInfo.variant != VariantGrand
6383 && gameInfo.variant != VariantSuper) return FALSE;
6384 if(autoQueen) return FALSE; // predetermined
6386 // suppress promotion popup on illegal moves that are not premoves
6387 premove = gameMode == IcsPlayingWhite && !WhiteOnMove(currentMove) ||
6388 gameMode == IcsPlayingBlack && WhiteOnMove(currentMove);
6389 if(appData.testLegality && !premove) {
6390 moveType = LegalityTest(boards[currentMove], PosFlags(currentMove),
6391 fromY, fromX, toY, toX, gameInfo.variant == VariantShogi ? '+' : NULLCHAR);
6392 if(moveType != WhitePromotion && moveType != BlackPromotion)
6400 InPalace (int row, int column)
6401 { /* [HGM] for Xiangqi */
6402 if( (row < 3 || row > BOARD_HEIGHT-4) &&
6403 column < (BOARD_WIDTH + 4)/2 &&
6404 column > (BOARD_WIDTH - 5)/2 ) return TRUE;
6409 PieceForSquare (int x, int y)
6411 if (x < 0 || x >= BOARD_WIDTH || y < 0 || y >= BOARD_HEIGHT)
6414 return boards[currentMove][y][x];
6418 OKToStartUserMove (int x, int y)
6420 ChessSquare from_piece;
6423 if (matchMode) return FALSE;
6424 if (gameMode == EditPosition) return TRUE;
6426 if (x >= 0 && y >= 0)
6427 from_piece = boards[currentMove][y][x];
6429 from_piece = EmptySquare;
6431 if (from_piece == EmptySquare) return FALSE;
6433 white_piece = (int)from_piece >= (int)WhitePawn &&
6434 (int)from_piece < (int)BlackPawn; /* [HGM] can be > King! */
6438 case TwoMachinesPlay:
6446 case MachinePlaysWhite:
6447 case IcsPlayingBlack:
6448 if (appData.zippyPlay) return FALSE;
6450 DisplayMoveError(_("You are playing Black"));
6455 case MachinePlaysBlack:
6456 case IcsPlayingWhite:
6457 if (appData.zippyPlay) return FALSE;
6459 DisplayMoveError(_("You are playing White"));
6464 case PlayFromGameFile:
6465 if(!shiftKey || !appData.variations) return FALSE; // [HGM] allow starting variation in this mode
6467 if (!white_piece && WhiteOnMove(currentMove)) {
6468 DisplayMoveError(_("It is White's turn"));
6471 if (white_piece && !WhiteOnMove(currentMove)) {
6472 DisplayMoveError(_("It is Black's turn"));
6475 if (cmailMsgLoaded && (currentMove < cmailOldMove)) {
6476 /* Editing correspondence game history */
6477 /* Could disallow this or prompt for confirmation */
6482 case BeginningOfGame:
6483 if (appData.icsActive) return FALSE;
6484 if (!appData.noChessProgram) {
6486 DisplayMoveError(_("You are playing White"));
6493 if (!white_piece && WhiteOnMove(currentMove)) {
6494 DisplayMoveError(_("It is White's turn"));
6497 if (white_piece && !WhiteOnMove(currentMove)) {
6498 DisplayMoveError(_("It is Black's turn"));
6507 if (currentMove != forwardMostMove && gameMode != AnalyzeMode
6508 && gameMode != EditGame // [HGM] vari: treat as AnalyzeMode
6509 && gameMode != PlayFromGameFile // [HGM] as EditGame, with protected main line
6510 && gameMode != AnalyzeFile && gameMode != Training) {
6511 DisplayMoveError(_("Displayed position is not current"));
6518 OnlyMove (int *x, int *y, Boolean captures)
6520 DisambiguateClosure cl;
6521 if (appData.zippyPlay || !appData.testLegality) return FALSE;
6523 case MachinePlaysBlack:
6524 case IcsPlayingWhite:
6525 case BeginningOfGame:
6526 if(!WhiteOnMove(currentMove)) return FALSE;
6528 case MachinePlaysWhite:
6529 case IcsPlayingBlack:
6530 if(WhiteOnMove(currentMove)) return FALSE;
6537 cl.pieceIn = EmptySquare;
6542 cl.promoCharIn = NULLCHAR;
6543 Disambiguate(boards[currentMove], PosFlags(currentMove), &cl);
6544 if( cl.kind == NormalMove ||
6545 cl.kind == AmbiguousMove && captures && cl.captures == 1 ||
6546 cl.kind == WhitePromotion || cl.kind == BlackPromotion ||
6547 cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) {
6554 if(cl.kind != ImpossibleMove) return FALSE;
6555 cl.pieceIn = EmptySquare;
6560 cl.promoCharIn = NULLCHAR;
6561 Disambiguate(boards[currentMove], PosFlags(currentMove), &cl);
6562 if( cl.kind == NormalMove ||
6563 cl.kind == AmbiguousMove && captures && cl.captures == 1 ||
6564 cl.kind == WhitePromotion || cl.kind == BlackPromotion ||
6565 cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) {
6570 autoQueen = TRUE; // act as if autoQueen on when we click to-square
6576 FILE *lastLoadGameFP = NULL, *lastLoadPositionFP = NULL;
6577 int lastLoadGameNumber = 0, lastLoadPositionNumber = 0;
6578 int lastLoadGameUseList = FALSE;
6579 char lastLoadGameTitle[MSG_SIZ], lastLoadPositionTitle[MSG_SIZ];
6580 ChessMove lastLoadGameStart = EndOfFile;
6584 UserMoveEvent(int fromX, int fromY, int toX, int toY, int promoChar)
6588 int ff=fromX, rf=fromY, ft=toX, rt=toY;
6590 /* Check if the user is playing in turn. This is complicated because we
6591 let the user "pick up" a piece before it is his turn. So the piece he
6592 tried to pick up may have been captured by the time he puts it down!
6593 Therefore we use the color the user is supposed to be playing in this
6594 test, not the color of the piece that is currently on the starting
6595 square---except in EditGame mode, where the user is playing both
6596 sides; fortunately there the capture race can't happen. (It can
6597 now happen in IcsExamining mode, but that's just too bad. The user
6598 will get a somewhat confusing message in that case.)
6603 case TwoMachinesPlay:
6607 /* We switched into a game mode where moves are not accepted,
6608 perhaps while the mouse button was down. */
6611 case MachinePlaysWhite:
6612 /* User is moving for Black */
6613 if (WhiteOnMove(currentMove)) {
6614 DisplayMoveError(_("It is White's turn"));
6619 case MachinePlaysBlack:
6620 /* User is moving for White */
6621 if (!WhiteOnMove(currentMove)) {
6622 DisplayMoveError(_("It is Black's turn"));
6627 case PlayFromGameFile:
6628 if(!shiftKey ||!appData.variations) return; // [HGM] only variations
6631 case BeginningOfGame:
6634 if(fromY == DROP_RANK) break; // [HGM] drop moves (entered through move type-in) are automatically assigned to side-to-move
6635 if ((int) boards[currentMove][fromY][fromX] >= (int) BlackPawn &&
6636 (int) boards[currentMove][fromY][fromX] < (int) EmptySquare) {
6637 /* User is moving for Black */
6638 if (WhiteOnMove(currentMove)) {
6639 DisplayMoveError(_("It is White's turn"));
6643 /* User is moving for White */
6644 if (!WhiteOnMove(currentMove)) {
6645 DisplayMoveError(_("It is Black's turn"));
6651 case IcsPlayingBlack:
6652 /* User is moving for Black */
6653 if (WhiteOnMove(currentMove)) {
6654 if (!appData.premove) {
6655 DisplayMoveError(_("It is White's turn"));
6656 } else if (toX >= 0 && toY >= 0) {
6659 premoveFromX = fromX;
6660 premoveFromY = fromY;
6661 premovePromoChar = promoChar;
6663 if (appData.debugMode)
6664 fprintf(debugFP, "Got premove: fromX %d,"
6665 "fromY %d, toX %d, toY %d\n",
6666 fromX, fromY, toX, toY);
6672 case IcsPlayingWhite:
6673 /* User is moving for White */
6674 if (!WhiteOnMove(currentMove)) {
6675 if (!appData.premove) {
6676 DisplayMoveError(_("It is Black's turn"));
6677 } else if (toX >= 0 && toY >= 0) {
6680 premoveFromX = fromX;
6681 premoveFromY = fromY;
6682 premovePromoChar = promoChar;
6684 if (appData.debugMode)
6685 fprintf(debugFP, "Got premove: fromX %d,"
6686 "fromY %d, toX %d, toY %d\n",
6687 fromX, fromY, toX, toY);
6697 /* EditPosition, empty square, or different color piece;
6698 click-click move is possible */
6699 if (toX == -2 || toY == -2) {
6700 boards[0][fromY][fromX] = EmptySquare;
6701 DrawPosition(FALSE, boards[currentMove]);
6703 } else if (toX >= 0 && toY >= 0) {
6704 boards[0][toY][toX] = boards[0][fromY][fromX];
6705 if(fromX == BOARD_LEFT-2) { // handle 'moves' out of holdings
6706 if(boards[0][fromY][0] != EmptySquare) {
6707 if(boards[0][fromY][1]) boards[0][fromY][1]--;
6708 if(boards[0][fromY][1] == 0) boards[0][fromY][0] = EmptySquare;
6711 if(fromX == BOARD_RGHT+1) {
6712 if(boards[0][fromY][BOARD_WIDTH-1] != EmptySquare) {
6713 if(boards[0][fromY][BOARD_WIDTH-2]) boards[0][fromY][BOARD_WIDTH-2]--;
6714 if(boards[0][fromY][BOARD_WIDTH-2] == 0) boards[0][fromY][BOARD_WIDTH-1] = EmptySquare;
6717 boards[0][fromY][fromX] = gatingPiece;
6718 DrawPosition(FALSE, boards[currentMove]);
6724 if(toX < 0 || toY < 0) return;
6725 pup = boards[currentMove][toY][toX];
6727 /* [HGM] If move started in holdings, it means a drop. Convert to standard form */
6728 if( (fromX == BOARD_LEFT-2 || fromX == BOARD_RGHT+1) && fromY != DROP_RANK ) {
6729 if( pup != EmptySquare ) return;
6730 moveType = WhiteOnMove(currentMove) ? WhiteDrop : BlackDrop;
6731 if(appData.debugMode) fprintf(debugFP, "Drop move %d, curr=%d, x=%d,y=%d, p=%d\n",
6732 moveType, currentMove, fromX, fromY, boards[currentMove][fromY][fromX]);
6733 // holdings might not be sent yet in ICS play; we have to figure out which piece belongs here
6734 if(fromX == 0) fromY = BOARD_HEIGHT-1 - fromY; // black holdings upside-down
6735 fromX = fromX ? WhitePawn : BlackPawn; // first piece type in selected holdings
6736 while(PieceToChar(fromX) == '.' || PieceToNumber(fromX) != fromY && fromX != (int) EmptySquare) fromX++;
6740 /* [HGM] always test for legality, to get promotion info */
6741 moveType = LegalityTest(boards[currentMove], PosFlags(currentMove),
6742 fromY, fromX, toY, toX, promoChar);
6744 if(fromY == DROP_RANK && fromX == EmptySquare && (gameMode == AnalyzeMode || gameMode == EditGame)) moveType = NormalMove;
6746 /* [HGM] but possibly ignore an IllegalMove result */
6747 if (appData.testLegality) {
6748 if (moveType == IllegalMove || moveType == ImpossibleMove) {
6749 DisplayMoveError(_("Illegal move"));
6754 if(doubleClick && gameMode == AnalyzeMode) { // [HGM] exclude: move entered with double-click on from square is for exclusion, not playing
6755 if(ExcludeOneMove(fromY, fromX, toY, toX, promoChar, '*')) // toggle
6756 ClearPremoveHighlights(); // was included
6757 else ClearHighlights(), SetPremoveHighlights(ff, rf, ft, rt); // exclusion indicated by premove highlights
6761 FinishMove(moveType, fromX, fromY, toX, toY, promoChar);
6764 /* Common tail of UserMoveEvent and DropMenuEvent */
6766 FinishMove (ChessMove moveType, int fromX, int fromY, int toX, int toY, int promoChar)
6770 if((gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand) && promoChar != NULLCHAR) {
6771 // [HGM] superchess: suppress promotions to non-available piece (but P always allowed)
6772 int k = PieceToNumber(CharToPiece(ToUpper(promoChar)));
6773 if(WhiteOnMove(currentMove)) {
6774 if(!boards[currentMove][k][BOARD_WIDTH-2]) return 0;
6776 if(!boards[currentMove][BOARD_HEIGHT-1-k][1]) return 0;
6780 /* [HGM] <popupFix> kludge to avoid having to know the exact promotion
6781 move type in caller when we know the move is a legal promotion */
6782 if(moveType == NormalMove && promoChar)
6783 moveType = WhiteOnMove(currentMove) ? WhitePromotion : BlackPromotion;
6785 /* [HGM] <popupFix> The following if has been moved here from
6786 UserMoveEvent(). Because it seemed to belong here (why not allow
6787 piece drops in training games?), and because it can only be
6788 performed after it is known to what we promote. */
6789 if (gameMode == Training) {
6790 /* compare the move played on the board to the next move in the
6791 * game. If they match, display the move and the opponent's response.
6792 * If they don't match, display an error message.
6796 CopyBoard(testBoard, boards[currentMove]);
6797 ApplyMove(fromX, fromY, toX, toY, promoChar, testBoard);
6799 if (CompareBoards(testBoard, boards[currentMove+1])) {
6800 ForwardInner(currentMove+1);
6802 /* Autoplay the opponent's response.
6803 * if appData.animate was TRUE when Training mode was entered,
6804 * the response will be animated.
6806 saveAnimate = appData.animate;
6807 appData.animate = animateTraining;
6808 ForwardInner(currentMove+1);
6809 appData.animate = saveAnimate;
6811 /* check for the end of the game */
6812 if (currentMove >= forwardMostMove) {
6813 gameMode = PlayFromGameFile;
6815 SetTrainingModeOff();
6816 DisplayInformation(_("End of game"));
6819 DisplayError(_("Incorrect move"), 0);
6824 /* Ok, now we know that the move is good, so we can kill
6825 the previous line in Analysis Mode */
6826 if ((gameMode == AnalyzeMode || gameMode == EditGame || gameMode == PlayFromGameFile && appData.variations && shiftKey)
6827 && currentMove < forwardMostMove) {
6828 if(appData.variations && shiftKey) PushTail(currentMove, forwardMostMove); // [HGM] vari: save tail of game
6829 else forwardMostMove = currentMove;
6834 /* If we need the chess program but it's dead, restart it */
6835 ResurrectChessProgram();
6837 /* A user move restarts a paused game*/
6841 thinkOutput[0] = NULLCHAR;
6843 MakeMove(fromX, fromY, toX, toY, promoChar); /*updates forwardMostMove*/
6845 if(Adjudicate(NULL)) { // [HGM] adjudicate: take care of automatic game end
6846 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6850 if (gameMode == BeginningOfGame) {
6851 if (appData.noChessProgram) {
6852 gameMode = EditGame;
6856 gameMode = MachinePlaysBlack;
6859 snprintf(buf, MSG_SIZ, "%s %s %s", gameInfo.white, _("vs."), gameInfo.black);
6861 if (first.sendName) {
6862 snprintf(buf, MSG_SIZ,"name %s\n", gameInfo.white);
6863 SendToProgram(buf, &first);
6870 /* Relay move to ICS or chess engine */
6871 if (appData.icsActive) {
6872 if (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
6873 gameMode == IcsExamining) {
6874 if(userOfferedDraw && (signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
6875 SendToICS(ics_prefix); // [HGM] drawclaim: send caim and move on one line for FICS
6877 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
6879 // also send plain move, in case ICS does not understand atomic claims
6880 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
6884 if (first.sendTime && (gameMode == BeginningOfGame ||
6885 gameMode == MachinePlaysWhite ||
6886 gameMode == MachinePlaysBlack)) {
6887 SendTimeRemaining(&first, gameMode != MachinePlaysBlack);
6889 if (gameMode != EditGame && gameMode != PlayFromGameFile && gameMode != AnalyzeMode) {
6890 // [HGM] book: if program might be playing, let it use book
6891 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, FALSE);
6892 first.maybeThinking = TRUE;
6893 } else if(fromY == DROP_RANK && fromX == EmptySquare) {
6894 if(!first.useSetboard) SendToProgram("undo\n", &first); // kludge to change stm in engines that do not support setboard
6895 SendBoard(&first, currentMove+1);
6896 if(second.analyzing) {
6897 if(!second.useSetboard) SendToProgram("undo\n", &second);
6898 SendBoard(&second, currentMove+1);
6901 SendMoveToProgram(forwardMostMove-1, &first);
6902 if(second.analyzing) SendMoveToProgram(forwardMostMove-1, &second);
6904 if (currentMove == cmailOldMove + 1) {
6905 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
6909 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6913 if(appData.testLegality)
6914 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
6920 if (WhiteOnMove(currentMove)) {
6921 GameEnds(BlackWins, "Black mates", GE_PLAYER);
6923 GameEnds(WhiteWins, "White mates", GE_PLAYER);
6927 GameEnds(GameIsDrawn, "Stalemate", GE_PLAYER);
6932 case MachinePlaysBlack:
6933 case MachinePlaysWhite:
6934 /* disable certain menu options while machine is thinking */
6935 SetMachineThinkingEnables();
6942 userOfferedDraw = FALSE; // [HGM] drawclaim: after move made, and tested for claimable draw
6943 promoDefaultAltered = FALSE; // [HGM] fall back on default choice
6945 if(bookHit) { // [HGM] book: simulate book reply
6946 static char bookMove[MSG_SIZ]; // a bit generous?
6948 programStats.nodes = programStats.depth = programStats.time =
6949 programStats.score = programStats.got_only_move = 0;
6950 sprintf(programStats.movelist, "%s (xbook)", bookHit);
6952 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
6953 strcat(bookMove, bookHit);
6954 HandleMachineMove(bookMove, &first);
6960 Mark (Board board, int flags, ChessMove kind, int rf, int ff, int rt, int ft, VOIDSTAR closure)
6962 typedef char Markers[BOARD_RANKS][BOARD_FILES];
6963 Markers *m = (Markers *) closure;
6964 if(rf == fromY && ff == fromX)
6965 (*m)[rt][ft] = 1 + (board[rt][ft] != EmptySquare
6966 || kind == WhiteCapturesEnPassant
6967 || kind == BlackCapturesEnPassant);
6968 else if(flags & F_MANDATORY_CAPTURE && board[rt][ft] != EmptySquare) (*m)[rt][ft] = 3;
6972 MarkTargetSquares (int clear)
6975 if(clear) // no reason to ever suppress clearing
6976 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) marker[y][x] = 0;
6977 if(!appData.markers || !appData.highlightDragging || appData.icsActive && gameInfo.variant < VariantShogi ||
6978 !appData.testLegality || gameMode == EditPosition) return;
6981 GenLegal(boards[currentMove], PosFlags(currentMove), Mark, (void*) marker, EmptySquare);
6982 if(PosFlags(0) & F_MANDATORY_CAPTURE) {
6983 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) if(marker[y][x]>1) capt++;
6985 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) if(marker[y][x] == 1) marker[y][x] = 0;
6988 DrawPosition(FALSE, NULL);
6992 Explode (Board board, int fromX, int fromY, int toX, int toY)
6994 if(gameInfo.variant == VariantAtomic &&
6995 (board[toY][toX] != EmptySquare || // capture?
6996 toX != fromX && (board[fromY][fromX] == WhitePawn || // e.p. ?
6997 board[fromY][fromX] == BlackPawn )
6999 AnimateAtomicCapture(board, fromX, fromY, toX, toY);
7005 ChessSquare gatingPiece = EmptySquare; // exported to front-end, for dragging
7008 CanPromote (ChessSquare piece, int y)
7010 if(gameMode == EditPosition) return FALSE; // no promotions when editing position
7011 // some variants have fixed promotion piece, no promotion at all, or another selection mechanism
7012 if(gameInfo.variant == VariantShogi || gameInfo.variant == VariantXiangqi ||
7013 gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat ||
7014 gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier ||
7015 gameInfo.variant == VariantMakruk) return FALSE;
7016 return (piece == BlackPawn && y == 1 ||
7017 piece == WhitePawn && y == BOARD_HEIGHT-2 ||
7018 piece == BlackLance && y == 1 ||
7019 piece == WhiteLance && y == BOARD_HEIGHT-2 );
7023 LeftClick (ClickType clickType, int xPix, int yPix)
7026 Boolean saveAnimate;
7027 static int second = 0, promotionChoice = 0, clearFlag = 0, sweepSelecting = 0;
7028 char promoChoice = NULLCHAR;
7030 static TimeMark lastClickTime, prevClickTime;
7032 if(SeekGraphClick(clickType, xPix, yPix, 0)) return;
7034 prevClickTime = lastClickTime; GetTimeMark(&lastClickTime);
7036 if (clickType == Press) ErrorPopDown();
7038 x = EventToSquare(xPix, BOARD_WIDTH);
7039 y = EventToSquare(yPix, BOARD_HEIGHT);
7040 if (!flipView && y >= 0) {
7041 y = BOARD_HEIGHT - 1 - y;
7043 if (flipView && x >= 0) {
7044 x = BOARD_WIDTH - 1 - x;
7047 if(promoSweep != EmptySquare) { // up-click during sweep-select of promo-piece
7048 defaultPromoChoice = promoSweep;
7049 promoSweep = EmptySquare; // terminate sweep
7050 promoDefaultAltered = TRUE;
7051 if(!selectFlag && !sweepSelecting && (x != toX || y != toY)) x = fromX, y = fromY; // and fake up-click on same square if we were still selecting
7054 if(promotionChoice) { // we are waiting for a click to indicate promotion piece
7055 if(clickType == Release) return; // ignore upclick of click-click destination
7056 promotionChoice = FALSE; // only one chance: if click not OK it is interpreted as cancel
7057 if(appData.debugMode) fprintf(debugFP, "promotion click, x=%d, y=%d\n", x, y);
7058 if(gameInfo.holdingsWidth &&
7059 (WhiteOnMove(currentMove)
7060 ? x == BOARD_WIDTH-1 && y < gameInfo.holdingsSize && y >= 0
7061 : x == 0 && y >= BOARD_HEIGHT - gameInfo.holdingsSize && y < BOARD_HEIGHT) ) {
7062 // click in right holdings, for determining promotion piece
7063 ChessSquare p = boards[currentMove][y][x];
7064 if(appData.debugMode) fprintf(debugFP, "square contains %d\n", (int)p);
7065 if(p == WhitePawn || p == BlackPawn) p = EmptySquare; // [HGM] Pawns could be valid as deferral
7066 if(p != EmptySquare || gameInfo.variant == VariantGrand && toY != 0 && toY != BOARD_HEIGHT-1) { // [HGM] grand: empty square means defer
7067 FinishMove(NormalMove, fromX, fromY, toX, toY, p==EmptySquare ? NULLCHAR : ToLower(PieceToChar(p)));
7072 DrawPosition(FALSE, boards[currentMove]);
7076 /* [HGM] holdings: next 5 lines: ignore all clicks between board and holdings */
7077 if(clickType == Press
7078 && ( x == BOARD_LEFT-1 || x == BOARD_RGHT
7079 || x == BOARD_LEFT-2 && y < BOARD_HEIGHT-gameInfo.holdingsSize
7080 || x == BOARD_RGHT+1 && y >= gameInfo.holdingsSize) )
7083 if(gotPremove && x == premoveFromX && y == premoveFromY && clickType == Release) {
7084 // could be static click on premove from-square: abort premove
7086 ClearPremoveHighlights();
7089 if(clickType == Press && fromX == x && fromY == y && promoDefaultAltered && SubtractTimeMarks(&lastClickTime, &prevClickTime) >= 200)
7090 fromX = fromY = -1; // second click on piece after altering default promo piece treated as first click
7092 if(!promoDefaultAltered) { // determine default promotion piece, based on the side the user is moving for
7093 int side = (gameMode == IcsPlayingWhite || gameMode == MachinePlaysBlack ||
7094 gameMode != MachinePlaysWhite && gameMode != IcsPlayingBlack && WhiteOnMove(currentMove));
7095 defaultPromoChoice = DefaultPromoChoice(side);
7098 autoQueen = appData.alwaysPromoteToQueen;
7102 gatingPiece = EmptySquare;
7103 if (clickType != Press) {
7104 if(dragging) { // [HGM] from-square must have been reset due to game end since last press
7105 DragPieceEnd(xPix, yPix); dragging = 0;
7106 DrawPosition(FALSE, NULL);
7110 doubleClick = FALSE;
7111 if(gameMode == AnalyzeMode && pausing && first.excludeMoves) { // use pause state to exclude moves
7112 doubleClick = TRUE; gatingPiece = boards[currentMove][y][x];
7114 fromX = x; fromY = y; toX = toY = -1;
7115 if(!appData.oneClick || !OnlyMove(&x, &y, FALSE) ||
7116 // even if only move, we treat as normal when this would trigger a promotion popup, to allow sweep selection
7117 appData.sweepSelect && CanPromote(boards[currentMove][fromY][fromX], fromY) && originalY != y) {
7119 if (OKToStartUserMove(fromX, fromY)) {
7121 MarkTargetSquares(0);
7122 if(gameMode == EditPosition && controlKey) gatingPiece = boards[currentMove][fromY][fromX];
7123 DragPieceBegin(xPix, yPix, FALSE); dragging = 1;
7124 if(appData.sweepSelect && CanPromote(piece = boards[currentMove][fromY][fromX], fromY)) {
7125 promoSweep = defaultPromoChoice;
7126 selectFlag = 0; lastX = xPix; lastY = yPix;
7127 Sweep(0); // Pawn that is going to promote: preview promotion piece
7128 DisplayMessage("", _("Pull pawn backwards to under-promote"));
7130 if (appData.highlightDragging) {
7131 SetHighlights(fromX, fromY, -1, -1);
7135 } else fromX = fromY = -1;
7141 if (clickType == Press && gameMode != EditPosition) {
7146 // ignore off-board to clicks
7147 if(y < 0 || x < 0) return;
7149 /* Check if clicking again on the same color piece */
7150 fromP = boards[currentMove][fromY][fromX];
7151 toP = boards[currentMove][y][x];
7152 frc = gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom || gameInfo.variant == VariantSChess;
7153 if ((WhitePawn <= fromP && fromP <= WhiteKing &&
7154 WhitePawn <= toP && toP <= WhiteKing &&
7155 !(fromP == WhiteKing && toP == WhiteRook && frc) &&
7156 !(fromP == WhiteRook && toP == WhiteKing && frc)) ||
7157 (BlackPawn <= fromP && fromP <= BlackKing &&
7158 BlackPawn <= toP && toP <= BlackKing &&
7159 !(fromP == BlackRook && toP == BlackKing && frc) && // allow also RxK as FRC castling
7160 !(fromP == BlackKing && toP == BlackRook && frc))) {
7161 /* Clicked again on same color piece -- changed his mind */
7162 second = (x == fromX && y == fromY);
7163 if(second && gameMode == AnalyzeMode && SubtractTimeMarks(&lastClickTime, &prevClickTime) < 200) {
7164 second = FALSE; // first double-click rather than scond click
7165 doubleClick = first.excludeMoves; // used by UserMoveEvent to recognize exclude moves
7167 promoDefaultAltered = FALSE;
7168 MarkTargetSquares(1);
7169 if(!second || appData.oneClick && !OnlyMove(&x, &y, TRUE)) {
7170 if (appData.highlightDragging) {
7171 SetHighlights(x, y, -1, -1);
7175 if (OKToStartUserMove(x, y)) {
7176 if(gameInfo.variant == VariantSChess && // S-Chess: back-rank piece selected after holdings means gating
7177 (fromX == BOARD_LEFT-2 || fromX == BOARD_RGHT+1) &&
7178 y == (toP < BlackPawn ? 0 : BOARD_HEIGHT-1))
7179 gatingPiece = boards[currentMove][fromY][fromX];
7180 else gatingPiece = doubleClick ? fromP : EmptySquare;
7182 fromY = y; dragging = 1;
7183 MarkTargetSquares(0);
7184 DragPieceBegin(xPix, yPix, FALSE);
7185 if(appData.sweepSelect && CanPromote(piece = boards[currentMove][y][x], y)) {
7186 promoSweep = defaultPromoChoice;
7187 selectFlag = 0; lastX = xPix; lastY = yPix;
7188 Sweep(0); // Pawn that is going to promote: preview promotion piece
7192 if(x == fromX && y == fromY) return; // if OnlyMove altered (x,y) we go on
7195 // ignore clicks on holdings
7196 if(x < BOARD_LEFT || x >= BOARD_RGHT) return;
7199 if (clickType == Release && x == fromX && y == fromY) {
7200 DragPieceEnd(xPix, yPix); dragging = 0;
7202 // a deferred attempt to click-click move an empty square on top of a piece
7203 boards[currentMove][y][x] = EmptySquare;
7205 DrawPosition(FALSE, boards[currentMove]);
7206 fromX = fromY = -1; clearFlag = 0;
7209 if (appData.animateDragging) {
7210 /* Undo animation damage if any */
7211 DrawPosition(FALSE, NULL);
7213 if (second || sweepSelecting) {
7214 /* Second up/down in same square; just abort move */
7215 if(sweepSelecting) DrawPosition(FALSE, boards[currentMove]);
7216 second = sweepSelecting = 0;
7218 gatingPiece = EmptySquare;
7221 ClearPremoveHighlights();
7223 /* First upclick in same square; start click-click mode */
7224 SetHighlights(x, y, -1, -1);
7231 /* we now have a different from- and (possibly off-board) to-square */
7232 /* Completed move */
7233 if(!sweepSelecting) {
7236 } else sweepSelecting = 0; // this must be the up-click corresponding to the down-click that started the sweep
7238 saveAnimate = appData.animate;
7239 if (clickType == Press) {
7240 if(gameMode == EditPosition && boards[currentMove][fromY][fromX] == EmptySquare) {
7241 // must be Edit Position mode with empty-square selected
7242 fromX = x; fromY = y; DragPieceBegin(xPix, yPix, FALSE); dragging = 1; // consider this a new attempt to drag
7243 if(x >= BOARD_LEFT && x < BOARD_RGHT) clearFlag = 1; // and defer click-click move of empty-square to up-click
7246 if(HasPromotionChoice(fromX, fromY, toX, toY, &promoChoice, FALSE)) {
7247 if(appData.sweepSelect) {
7248 ChessSquare piece = boards[currentMove][fromY][fromX];
7249 promoSweep = defaultPromoChoice;
7250 if(PieceToChar(PROMOTED piece) == '+') promoSweep = PROMOTED piece;
7251 selectFlag = 0; lastX = xPix; lastY = yPix;
7252 Sweep(0); // Pawn that is going to promote: preview promotion piece
7254 DisplayMessage("", _("Pull pawn backwards to under-promote"));
7255 MarkTargetSquares(1);
7257 return; // promo popup appears on up-click
7259 /* Finish clickclick move */
7260 if (appData.animate || appData.highlightLastMove) {
7261 SetHighlights(fromX, fromY, toX, toY);
7267 // [HGM] this must be done after the move is made, as with arrow it could lead to a board redraw with piece still on from square
7268 /* Finish drag move */
7269 if (appData.highlightLastMove) {
7270 SetHighlights(fromX, fromY, toX, toY);
7275 DragPieceEnd(xPix, yPix); dragging = 0;
7276 /* Don't animate move and drag both */
7277 appData.animate = FALSE;
7280 // moves into holding are invalid for now (except in EditPosition, adapting to-square)
7281 if(x >= 0 && x < BOARD_LEFT || x >= BOARD_RGHT) {
7282 ChessSquare piece = boards[currentMove][fromY][fromX];
7283 if(gameMode == EditPosition && piece != EmptySquare &&
7284 fromX >= BOARD_LEFT && fromX < BOARD_RGHT) {
7287 if(x == BOARD_LEFT-2 && piece >= BlackPawn) {
7288 n = PieceToNumber(piece - (int)BlackPawn);
7289 if(n >= gameInfo.holdingsSize) { n = 0; piece = BlackPawn; }
7290 boards[currentMove][BOARD_HEIGHT-1 - n][0] = piece;
7291 boards[currentMove][BOARD_HEIGHT-1 - n][1]++;
7293 if(x == BOARD_RGHT+1 && piece < BlackPawn) {
7294 n = PieceToNumber(piece);
7295 if(n >= gameInfo.holdingsSize) { n = 0; piece = WhitePawn; }
7296 boards[currentMove][n][BOARD_WIDTH-1] = piece;
7297 boards[currentMove][n][BOARD_WIDTH-2]++;
7299 boards[currentMove][fromY][fromX] = EmptySquare;
7303 MarkTargetSquares(1);
7304 DrawPosition(TRUE, boards[currentMove]);
7308 // off-board moves should not be highlighted
7309 if(x < 0 || y < 0) ClearHighlights();
7311 if(gatingPiece != EmptySquare && gameInfo.variant == VariantSChess) promoChoice = ToLower(PieceToChar(gatingPiece));
7313 if (HasPromotionChoice(fromX, fromY, toX, toY, &promoChoice, appData.sweepSelect)) {
7314 SetHighlights(fromX, fromY, toX, toY);
7315 MarkTargetSquares(1);
7316 if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand) {
7317 // [HGM] super: promotion to captured piece selected from holdings
7318 ChessSquare p = boards[currentMove][fromY][fromX], q = boards[currentMove][toY][toX];
7319 promotionChoice = TRUE;
7320 // kludge follows to temporarily execute move on display, without promoting yet
7321 boards[currentMove][fromY][fromX] = EmptySquare; // move Pawn to 8th rank
7322 boards[currentMove][toY][toX] = p;
7323 DrawPosition(FALSE, boards[currentMove]);
7324 boards[currentMove][fromY][fromX] = p; // take back, but display stays
7325 boards[currentMove][toY][toX] = q;
7326 DisplayMessage("Click in holdings to choose piece", "");
7331 int oldMove = currentMove;
7332 UserMoveEvent(fromX, fromY, toX, toY, promoChoice);
7333 if (!appData.highlightLastMove || gotPremove) ClearHighlights();
7334 if (gotPremove) SetPremoveHighlights(fromX, fromY, toX, toY);
7335 if(saveAnimate && !appData.animate && currentMove != oldMove && // drag-move was performed
7336 Explode(boards[currentMove-1], fromX, fromY, toX, toY))
7337 DrawPosition(TRUE, boards[currentMove]);
7338 MarkTargetSquares(1);
7341 appData.animate = saveAnimate;
7342 if (appData.animate || appData.animateDragging) {
7343 /* Undo animation damage if needed */
7344 DrawPosition(FALSE, NULL);
7349 RightClick (ClickType action, int x, int y, int *fromX, int *fromY)
7350 { // front-end-free part taken out of PieceMenuPopup
7351 int whichMenu; int xSqr, ySqr;
7353 if(seekGraphUp) { // [HGM] seekgraph
7354 if(action == Press) SeekGraphClick(Press, x, y, 2); // 2 indicates right-click: no pop-down on miss
7355 if(action == Release) SeekGraphClick(Release, x, y, 2); // and no challenge on hit
7359 if((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)
7360 && !appData.zippyPlay && appData.bgObserve) { // [HGM] bughouse: show background game
7361 if(!partnerBoardValid) return -2; // suppress display of uninitialized boards
7362 if( appData.dualBoard) return -2; // [HGM] dual: is already displayed
7363 if(action == Press) {
7364 originalFlip = flipView;
7365 flipView = !flipView; // temporarily flip board to see game from partners perspective
7366 DrawPosition(TRUE, partnerBoard);
7367 DisplayMessage(partnerStatus, "");
7369 } else if(action == Release) {
7370 flipView = originalFlip;
7371 DrawPosition(TRUE, boards[currentMove]);
7377 xSqr = EventToSquare(x, BOARD_WIDTH);
7378 ySqr = EventToSquare(y, BOARD_HEIGHT);
7379 if (action == Release) {
7380 if(pieceSweep != EmptySquare) {
7381 EditPositionMenuEvent(pieceSweep, toX, toY);
7382 pieceSweep = EmptySquare;
7383 } else UnLoadPV(); // [HGM] pv
7385 if (action != Press) return -2; // return code to be ignored
7388 if(xSqr < BOARD_LEFT || xSqr >= BOARD_RGHT) return -1;
7390 if (xSqr == BOARD_LEFT-1 || xSqr == BOARD_RGHT) return -1;
7391 if (xSqr < 0 || ySqr < 0) return -1;
7392 if(appData.pieceMenu) { whichMenu = 0; break; } // edit-position menu
7393 pieceSweep = shiftKey ? BlackPawn : WhitePawn; // [HGM] sweep: prepare selecting piece by mouse sweep
7394 toX = xSqr; toY = ySqr; lastX = x, lastY = y;
7395 if(flipView) toX = BOARD_WIDTH - 1 - toX; else toY = BOARD_HEIGHT - 1 - toY;
7399 if(!appData.icsEngineAnalyze) return -1;
7400 case IcsPlayingWhite:
7401 case IcsPlayingBlack:
7402 if(!appData.zippyPlay) goto noZip;
7405 case MachinePlaysWhite:
7406 case MachinePlaysBlack:
7407 case TwoMachinesPlay: // [HGM] pv: use for showing PV
7408 if (!appData.dropMenu) {
7410 return 2; // flag front-end to grab mouse events
7412 if(gameMode == TwoMachinesPlay || gameMode == AnalyzeMode ||
7413 gameMode == AnalyzeFile || gameMode == IcsObserving) return -1;
7416 if (xSqr < 0 || ySqr < 0) return -1;
7417 if (!appData.dropMenu || appData.testLegality &&
7418 gameInfo.variant != VariantBughouse &&
7419 gameInfo.variant != VariantCrazyhouse) return -1;
7420 whichMenu = 1; // drop menu
7426 if (((*fromX = xSqr) < 0) ||
7427 ((*fromY = ySqr) < 0)) {
7428 *fromX = *fromY = -1;
7432 *fromX = BOARD_WIDTH - 1 - *fromX;
7434 *fromY = BOARD_HEIGHT - 1 - *fromY;
7440 SendProgramStatsToFrontend (ChessProgramState * cps, ChessProgramStats * cpstats)
7442 // char * hint = lastHint;
7443 FrontEndProgramStats stats;
7445 stats.which = cps == &first ? 0 : 1;
7446 stats.depth = cpstats->depth;
7447 stats.nodes = cpstats->nodes;
7448 stats.score = cpstats->score;
7449 stats.time = cpstats->time;
7450 stats.pv = cpstats->movelist;
7451 stats.hint = lastHint;
7452 stats.an_move_index = 0;
7453 stats.an_move_count = 0;
7455 if( gameMode == AnalyzeMode || gameMode == AnalyzeFile ) {
7456 stats.hint = cpstats->move_name;
7457 stats.an_move_index = cpstats->nr_moves - cpstats->moves_left;
7458 stats.an_move_count = cpstats->nr_moves;
7461 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
7463 SetProgramStats( &stats );
7467 ClearEngineOutputPane (int which)
7469 static FrontEndProgramStats dummyStats;
7470 dummyStats.which = which;
7471 dummyStats.pv = "#";
7472 SetProgramStats( &dummyStats );
7475 #define MAXPLAYERS 500
7478 TourneyStandings (int display)
7480 int i, w, b, color, wScore, bScore, dummy, nr=0, nPlayers=0;
7481 int score[MAXPLAYERS], ranking[MAXPLAYERS], points[MAXPLAYERS], games[MAXPLAYERS];
7482 char result, *p, *names[MAXPLAYERS];
7484 if(appData.tourneyType < 0 && !strchr(appData.results, '*'))
7485 return strdup(_("Swiss tourney finished")); // standings of Swiss yet TODO
7486 names[0] = p = strdup(appData.participants);
7487 while(p = strchr(p, '\n')) *p++ = NULLCHAR, names[++nPlayers] = p; // count participants
7489 for(i=0; i<nPlayers; i++) score[i] = games[i] = 0;
7491 while(result = appData.results[nr]) {
7492 color = Pairing(nr, nPlayers, &w, &b, &dummy);
7493 if(!(color ^ matchGame & 1)) { dummy = w; w = b; b = dummy; }
7494 wScore = bScore = 0;
7496 case '+': wScore = 2; break;
7497 case '-': bScore = 2; break;
7498 case '=': wScore = bScore = 1; break;
7500 case '*': return strdup("busy"); // tourney not finished
7508 if(appData.tourneyType > 0) nPlayers = appData.tourneyType; // in gauntlet, list only gauntlet engine(s)
7509 for(w=0; w<nPlayers; w++) {
7511 for(i=0; i<nPlayers; i++) if(score[i] > bScore) bScore = score[i], b = i;
7512 ranking[w] = b; points[w] = bScore; score[b] = -2;
7514 p = malloc(nPlayers*34+1);
7515 for(w=0; w<nPlayers && w<display; w++)
7516 sprintf(p+34*w, "%2d. %5.1f/%-3d %-19.19s\n", w+1, points[w]/2., games[ranking[w]], names[ranking[w]]);
7522 Count (Board board, int pCnt[], int *nW, int *nB, int *wStale, int *bStale, int *bishopColor)
7523 { // count all piece types
7525 *nB = *nW = *wStale = *bStale = *bishopColor = 0;
7526 for(p=WhitePawn; p<=EmptySquare; p++) pCnt[p] = 0;
7527 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
7530 if(p == WhitePawn && r == BOARD_HEIGHT-1) (*wStale)++; else
7531 if(p == BlackPawn && r == 0) (*bStale)++; // count last-Rank Pawns (XQ) separately
7532 if(p <= WhiteKing) (*nW)++; else if(p <= BlackKing) (*nB)++;
7533 if(p == WhiteBishop || p == WhiteFerz || p == WhiteAlfil ||
7534 p == BlackBishop || p == BlackFerz || p == BlackAlfil )
7535 *bishopColor |= 1 << ((f^r)&1); // track square color of color-bound pieces
7540 SufficientDefence (int pCnt[], int side, int nMine, int nHis)
7542 int myPawns = pCnt[WhitePawn+side]; // my total Pawn count;
7543 int majorDefense = pCnt[BlackRook-side] + pCnt[BlackCannon-side] + pCnt[BlackKnight-side];
7545 nMine -= pCnt[WhiteFerz+side] + pCnt[WhiteAlfil+side]; // discount defenders
7546 if(nMine - myPawns > 2) return FALSE; // no trivial draws with more than 1 major
7547 if(myPawns == 2 && nMine == 3) // KPP
7548 return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] >= 3;
7549 if(myPawns == 1 && nMine == 2) // KP
7550 return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] + pCnt[BlackPawn-side] >= 1;
7551 if(myPawns == 1 && nMine == 3 && pCnt[WhiteKnight+side]) // KHP
7552 return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side]*2 >= 5;
7553 if(myPawns) return FALSE;
7554 if(pCnt[WhiteRook+side])
7555 return pCnt[BlackRook-side] ||
7556 pCnt[BlackCannon-side] && (pCnt[BlackFerz-side] >= 2 || pCnt[BlackAlfil-side] >= 2) ||
7557 pCnt[BlackKnight-side] && pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] > 2 ||
7558 pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] >= 4;
7559 if(pCnt[WhiteCannon+side]) {
7560 if(pCnt[WhiteFerz+side] + myPawns == 0) return TRUE; // Cannon needs platform
7561 return majorDefense || pCnt[BlackAlfil-side] >= 2;
7563 if(pCnt[WhiteKnight+side])
7564 return majorDefense || pCnt[BlackFerz-side] >= 2 || pCnt[BlackAlfil-side] + pCnt[BlackPawn-side] >= 1;
7569 MatingPotential (int pCnt[], int side, int nMine, int nHis, int stale, int bisColor)
7571 VariantClass v = gameInfo.variant;
7573 if(v == VariantShogi || v == VariantCrazyhouse || v == VariantBughouse) return TRUE; // drop games always winnable
7574 if(v == VariantShatranj) return TRUE; // always winnable through baring
7575 if(v == VariantLosers || v == VariantSuicide || v == VariantGiveaway) return TRUE;
7576 if(v == Variant3Check || v == VariantAtomic) return nMine > 1; // can win through checking / exploding King
7578 if(v == VariantXiangqi) {
7579 int majors = 5*pCnt[BlackKnight-side] + 7*pCnt[BlackCannon-side] + 7*pCnt[BlackRook-side];
7581 nMine -= pCnt[WhiteFerz+side] + pCnt[WhiteAlfil+side] + stale; // discount defensive pieces and back-rank Pawns
7582 if(nMine + stale == 1) return (pCnt[BlackFerz-side] > 1 && pCnt[BlackKnight-side] > 0); // bare K can stalemate KHAA (!)
7583 if(nMine > 2) return TRUE; // if we don't have P, H or R, we must have CC
7584 if(nMine == 2 && pCnt[WhiteCannon+side] == 0) return TRUE; // We have at least one P, H or R
7585 // if we get here, we must have KC... or KP..., possibly with additional A, E or last-rank P
7586 if(stale) // we have at least one last-rank P plus perhaps C
7587 return majors // KPKX
7588 || pCnt[BlackFerz-side] && pCnt[BlackFerz-side] + pCnt[WhiteCannon+side] + stale > 2; // KPKAA, KPPKA and KCPKA
7590 return pCnt[WhiteFerz+side] // KCAK
7591 || pCnt[WhiteAlfil+side] && pCnt[BlackRook-side] + pCnt[BlackCannon-side] + pCnt[BlackFerz-side] // KCEKA, KCEKX (X!=H)
7592 || majors + (12*pCnt[BlackFerz-side] | 6*pCnt[BlackAlfil-side]) > 16; // KCKAA, KCKAX, KCKEEX, KCKEXX (XX!=HH), KCKXXX
7593 // TO DO: cases wih an unpromoted f-Pawn acting as platform for an opponent Cannon
7595 } else if(pCnt[WhiteKing] == 1 && pCnt[BlackKing] == 1) { // other variants with orthodox Kings
7596 int nBishops = pCnt[WhiteBishop+side] + pCnt[WhiteFerz+side];
7598 if(nMine == 1) return FALSE; // bare King
7599 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
7600 nMine += (nBishops > 0) - nBishops; // By now all Bishops (and Ferz) on like-colored squares, so count as one
7601 if(nMine > 2 && nMine != pCnt[WhiteAlfil+side] + 1) return TRUE; // At least two pieces, not all Alfils
7602 // by now we have King + 1 piece (or multiple Bishops on the same color)
7603 if(pCnt[WhiteKnight+side])
7604 return (pCnt[BlackKnight-side] + pCnt[BlackBishop-side] + pCnt[BlackMan-side] +
7605 pCnt[BlackWazir-side] + pCnt[BlackSilver-side] + bisColor // KNKN, KNKB, KNKF, KNKE, KNKW, KNKM, KNKS
7606 || nHis > 3); // be sure to cover suffocation mates in corner (e.g. KNKQCA)
7608 return (pCnt[BlackKnight-side]); // KBKN, KFKN
7609 if(pCnt[WhiteAlfil+side])
7610 return (nHis > 2); // Alfils can in general not reach a corner square, but there might be edge (suffocation) mates
7611 if(pCnt[WhiteWazir+side])
7612 return (pCnt[BlackKnight-side] + pCnt[BlackWazir-side] + pCnt[BlackAlfil-side]); // KWKN, KWKW, KWKE
7619 CompareWithRights (Board b1, Board b2)
7622 if(!CompareBoards(b1, b2)) return FALSE;
7623 if(b1[EP_STATUS] != b2[EP_STATUS]) return FALSE;
7624 /* compare castling rights */
7625 if( b1[CASTLING][2] != b2[CASTLING][2] && (b2[CASTLING][0] != NoRights || b2[CASTLING][1] != NoRights) )
7626 rights++; /* King lost rights, while rook still had them */
7627 if( b1[CASTLING][2] != NoRights ) { /* king has rights */
7628 if( b1[CASTLING][0] != b2[CASTLING][0] || b1[CASTLING][1] != b2[CASTLING][1] )
7629 rights++; /* but at least one rook lost them */
7631 if( b1[CASTLING][5] != b1[CASTLING][5] && (b2[CASTLING][3] != NoRights || b2[CASTLING][4] != NoRights) )
7633 if( b1[CASTLING][5] != NoRights ) {
7634 if( b1[CASTLING][3] != b2[CASTLING][3] || b1[CASTLING][4] != b2[CASTLING][4] )
7641 Adjudicate (ChessProgramState *cps)
7642 { // [HGM] some adjudications useful with buggy engines
7643 // [HGM] adjudicate: made into separate routine, which now can be called after every move
7644 // In any case it determnes if the game is a claimable draw (filling in EP_STATUS).
7645 // Actually ending the game is now based on the additional internal condition canAdjudicate.
7646 // Only when the game is ended, and the opponent is a computer, this opponent gets the move relayed.
7647 int k, count = 0; static int bare = 1;
7648 ChessProgramState *engineOpponent = (gameMode == TwoMachinesPlay ? cps->other : (cps ? NULL : &first));
7649 Boolean canAdjudicate = !appData.icsActive;
7651 // most tests only when we understand the game, i.e. legality-checking on
7652 if( appData.testLegality )
7653 { /* [HGM] Some more adjudications for obstinate engines */
7654 int nrW, nrB, bishopColor, staleW, staleB, nr[EmptySquare+1], i;
7655 static int moveCount = 6;
7657 char *reason = NULL;
7659 /* Count what is on board. */
7660 Count(boards[forwardMostMove], nr, &nrW, &nrB, &staleW, &staleB, &bishopColor);
7662 /* Some material-based adjudications that have to be made before stalemate test */
7663 if(gameInfo.variant == VariantAtomic && nr[WhiteKing] + nr[BlackKing] < 2) {
7664 // [HGM] atomic: stm must have lost his King on previous move, as destroying own K is illegal
7665 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE; // make claimable as if stm is checkmated
7666 if(canAdjudicate && appData.checkMates) {
7668 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets move
7669 GameEnds( WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins,
7670 "Xboard adjudication: King destroyed", GE_XBOARD );
7675 /* Bare King in Shatranj (loses) or Losers (wins) */
7676 if( nrW == 1 || nrB == 1) {
7677 if( gameInfo.variant == VariantLosers) { // [HGM] losers: bare King wins (stm must have it first)
7678 boards[forwardMostMove][EP_STATUS] = EP_WINS; // mark as win, so it becomes claimable
7679 if(canAdjudicate && appData.checkMates) {
7681 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets to see move
7682 GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
7683 "Xboard adjudication: Bare king", GE_XBOARD );
7687 if( gameInfo.variant == VariantShatranj && --bare < 0)
7689 boards[forwardMostMove][EP_STATUS] = EP_WINS; // make claimable as win for stm
7690 if(canAdjudicate && appData.checkMates) {
7691 /* but only adjudicate if adjudication enabled */
7693 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets move
7694 GameEnds( nrW > 1 ? WhiteWins : nrB > 1 ? BlackWins : GameIsDrawn,
7695 "Xboard adjudication: Bare king", GE_XBOARD );
7702 // don't wait for engine to announce game end if we can judge ourselves
7703 switch (MateTest(boards[forwardMostMove], PosFlags(forwardMostMove)) ) {
7705 if(gameInfo.variant == Variant3Check) { // [HGM] 3check: when in check, test if 3rd time
7706 int i, checkCnt = 0; // (should really be done by making nr of checks part of game state)
7707 for(i=forwardMostMove-2; i>=backwardMostMove; i-=2) {
7708 if(MateTest(boards[i], PosFlags(i)) == MT_CHECK)
7711 reason = "Xboard adjudication: 3rd check";
7712 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE;
7722 reason = "Xboard adjudication: Stalemate";
7723 if((signed char)boards[forwardMostMove][EP_STATUS] != EP_CHECKMATE) { // [HGM] don't touch win through baring or K-capt
7724 boards[forwardMostMove][EP_STATUS] = EP_STALEMATE; // default result for stalemate is draw
7725 if(gameInfo.variant == VariantLosers || gameInfo.variant == VariantGiveaway) // [HGM] losers:
7726 boards[forwardMostMove][EP_STATUS] = EP_WINS; // in these variants stalemated is always a win
7727 else if(gameInfo.variant == VariantSuicide) // in suicide it depends
7728 boards[forwardMostMove][EP_STATUS] = nrW == nrB ? EP_STALEMATE :
7729 ((nrW < nrB) != WhiteOnMove(forwardMostMove) ?
7730 EP_CHECKMATE : EP_WINS);
7731 else if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantXiangqi)
7732 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE; // and in these variants being stalemated loses
7736 reason = "Xboard adjudication: Checkmate";
7737 boards[forwardMostMove][EP_STATUS] = (gameInfo.variant == VariantLosers ? EP_WINS : EP_CHECKMATE);
7741 switch(i = (signed char)boards[forwardMostMove][EP_STATUS]) {
7743 result = GameIsDrawn; break;
7745 result = WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins; break;
7747 result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins; break;
7751 if(canAdjudicate && appData.checkMates && result) { // [HGM] mates: adjudicate finished games if requested
7753 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7754 GameEnds( result, reason, GE_XBOARD );
7758 /* Next absolutely insufficient mating material. */
7759 if(!MatingPotential(nr, WhitePawn, nrW, nrB, staleW, bishopColor) &&
7760 !MatingPotential(nr, BlackPawn, nrB, nrW, staleB, bishopColor))
7761 { /* includes KBK, KNK, KK of KBKB with like Bishops */
7763 /* always flag draws, for judging claims */
7764 boards[forwardMostMove][EP_STATUS] = EP_INSUF_DRAW;
7766 if(canAdjudicate && appData.materialDraws) {
7767 /* but only adjudicate them if adjudication enabled */
7768 if(engineOpponent) {
7769 SendToProgram("force\n", engineOpponent); // suppress reply
7770 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see last move */
7772 GameEnds( GameIsDrawn, "Xboard adjudication: Insufficient mating material", GE_XBOARD );
7777 /* Then some trivial draws (only adjudicate, cannot be claimed) */
7778 if(gameInfo.variant == VariantXiangqi ?
7779 SufficientDefence(nr, WhitePawn, nrW, nrB) && SufficientDefence(nr, BlackPawn, nrB, nrW)
7781 ( nr[WhiteRook] == 1 && nr[BlackRook] == 1 /* KRKR */
7782 || nr[WhiteQueen] && nr[BlackQueen]==1 /* KQKQ */
7783 || nr[WhiteKnight]==2 || nr[BlackKnight]==2 /* KNNK */
7784 || nr[WhiteKnight]+nr[WhiteBishop] == 1 && nr[BlackKnight]+nr[BlackBishop] == 1 /* KBKN, KBKB, KNKN */
7786 if(--moveCount < 0 && appData.trivialDraws && canAdjudicate)
7787 { /* if the first 3 moves do not show a tactical win, declare draw */
7788 if(engineOpponent) {
7789 SendToProgram("force\n", engineOpponent); // suppress reply
7790 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7792 GameEnds( GameIsDrawn, "Xboard adjudication: Trivial draw", GE_XBOARD );
7795 } else moveCount = 6;
7798 // Repetition draws and 50-move rule can be applied independently of legality testing
7800 /* Check for rep-draws */
7802 for(k = forwardMostMove-2;
7803 k>=backwardMostMove && k>=forwardMostMove-100 &&
7804 (signed char)boards[k][EP_STATUS] < EP_UNKNOWN &&
7805 (signed char)boards[k+2][EP_STATUS] <= EP_NONE && (signed char)boards[k+1][EP_STATUS] <= EP_NONE;
7808 if(CompareBoards(boards[k], boards[forwardMostMove])) {
7809 /* compare castling rights */
7810 if( boards[forwardMostMove][CASTLING][2] != boards[k][CASTLING][2] &&
7811 (boards[k][CASTLING][0] != NoRights || boards[k][CASTLING][1] != NoRights) )
7812 rights++; /* King lost rights, while rook still had them */
7813 if( boards[forwardMostMove][CASTLING][2] != NoRights ) { /* king has rights */
7814 if( boards[forwardMostMove][CASTLING][0] != boards[k][CASTLING][0] ||
7815 boards[forwardMostMove][CASTLING][1] != boards[k][CASTLING][1] )
7816 rights++; /* but at least one rook lost them */
7818 if( boards[forwardMostMove][CASTLING][5] != boards[k][CASTLING][5] &&
7819 (boards[k][CASTLING][3] != NoRights || boards[k][CASTLING][4] != NoRights) )
7821 if( boards[forwardMostMove][CASTLING][5] != NoRights ) {
7822 if( boards[forwardMostMove][CASTLING][3] != boards[k][CASTLING][3] ||
7823 boards[forwardMostMove][CASTLING][4] != boards[k][CASTLING][4] )
7826 if( rights == 0 && ++count > appData.drawRepeats-2 && canAdjudicate
7827 && appData.drawRepeats > 1) {
7828 /* adjudicate after user-specified nr of repeats */
7829 int result = GameIsDrawn;
7830 char *details = "XBoard adjudication: repetition draw";
7831 if(gameInfo.variant == VariantXiangqi && appData.testLegality) {
7832 // [HGM] xiangqi: check for forbidden perpetuals
7833 int m, ourPerpetual = 1, hisPerpetual = 1;
7834 for(m=forwardMostMove; m>k; m-=2) {
7835 if(MateTest(boards[m], PosFlags(m)) != MT_CHECK)
7836 ourPerpetual = 0; // the current mover did not always check
7837 if(MateTest(boards[m-1], PosFlags(m-1)) != MT_CHECK)
7838 hisPerpetual = 0; // the opponent did not always check
7840 if(appData.debugMode) fprintf(debugFP, "XQ perpetual test, our=%d, his=%d\n",
7841 ourPerpetual, hisPerpetual);
7842 if(ourPerpetual && !hisPerpetual) { // we are actively checking him: forfeit
7843 result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
7844 details = "Xboard adjudication: perpetual checking";
7846 if(hisPerpetual && !ourPerpetual) { // he is checking us, but did not repeat yet
7847 break; // (or we would have caught him before). Abort repetition-checking loop.
7849 // Now check for perpetual chases
7850 if(!ourPerpetual && !hisPerpetual) { // no perpetual check, test for chase
7851 hisPerpetual = PerpetualChase(k, forwardMostMove);
7852 ourPerpetual = PerpetualChase(k+1, forwardMostMove);
7853 if(ourPerpetual && !hisPerpetual) { // we are actively chasing him: forfeit
7854 static char resdet[MSG_SIZ];
7855 result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
7857 snprintf(resdet, MSG_SIZ, "Xboard adjudication: perpetual chasing of %c%c", ourPerpetual>>8, ourPerpetual&255);
7859 if(hisPerpetual && !ourPerpetual) // he is chasing us, but did not repeat yet
7860 break; // Abort repetition-checking loop.
7862 // if neither of us is checking or chasing all the time, or both are, it is draw
7864 if(engineOpponent) {
7865 SendToProgram("force\n", engineOpponent); // suppress reply
7866 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7868 GameEnds( result, details, GE_XBOARD );
7871 if( rights == 0 && count > 1 ) /* occurred 2 or more times before */
7872 boards[forwardMostMove][EP_STATUS] = EP_REP_DRAW;
7876 /* Now we test for 50-move draws. Determine ply count */
7877 count = forwardMostMove;
7878 /* look for last irreversble move */
7879 while( (signed char)boards[count][EP_STATUS] <= EP_NONE && count > backwardMostMove )
7881 /* if we hit starting position, add initial plies */
7882 if( count == backwardMostMove )
7883 count -= initialRulePlies;
7884 count = forwardMostMove - count;
7885 if(gameInfo.variant == VariantXiangqi && ( count >= 100 || count >= 2*appData.ruleMoves ) ) {
7886 // adjust reversible move counter for checks in Xiangqi
7887 int i = forwardMostMove - count, inCheck = 0, lastCheck;
7888 if(i < backwardMostMove) i = backwardMostMove;
7889 while(i <= forwardMostMove) {
7890 lastCheck = inCheck; // check evasion does not count
7891 inCheck = (MateTest(boards[i], PosFlags(i)) == MT_CHECK);
7892 if(inCheck || lastCheck) count--; // check does not count
7897 boards[forwardMostMove][EP_STATUS] = EP_RULE_DRAW;
7898 /* this is used to judge if draw claims are legal */
7899 if(canAdjudicate && appData.ruleMoves > 0 && count >= 2*appData.ruleMoves) {
7900 if(engineOpponent) {
7901 SendToProgram("force\n", engineOpponent); // suppress reply
7902 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7904 GameEnds( GameIsDrawn, "Xboard adjudication: 50-move rule", GE_XBOARD );
7908 /* if draw offer is pending, treat it as a draw claim
7909 * when draw condition present, to allow engines a way to
7910 * claim draws before making their move to avoid a race
7911 * condition occurring after their move
7913 if((gameMode == TwoMachinesPlay ? second.offeredDraw : userOfferedDraw) || first.offeredDraw ) {
7915 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_RULE_DRAW)
7916 p = "Draw claim: 50-move rule";
7917 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_REP_DRAW)
7918 p = "Draw claim: 3-fold repetition";
7919 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_INSUF_DRAW)
7920 p = "Draw claim: insufficient mating material";
7921 if( p != NULL && canAdjudicate) {
7922 if(engineOpponent) {
7923 SendToProgram("force\n", engineOpponent); // suppress reply
7924 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7926 GameEnds( GameIsDrawn, p, GE_XBOARD );
7931 if( canAdjudicate && appData.adjudicateDrawMoves > 0 && forwardMostMove > (2*appData.adjudicateDrawMoves) ) {
7932 if(engineOpponent) {
7933 SendToProgram("force\n", engineOpponent); // suppress reply
7934 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7936 GameEnds( GameIsDrawn, "Xboard adjudication: long game", GE_XBOARD );
7943 SendMoveToBookUser (int moveNr, ChessProgramState *cps, int initial)
7944 { // [HGM] book: this routine intercepts moves to simulate book replies
7945 char *bookHit = NULL;
7947 //first determine if the incoming move brings opponent into his book
7948 if(appData.usePolyglotBook && (cps == &first ? !appData.firstHasOwnBookUCI : !appData.secondHasOwnBookUCI))
7949 bookHit = ProbeBook(moveNr+1, appData.polyglotBook); // returns move
7950 if(appData.debugMode) fprintf(debugFP, "book hit = %s\n", bookHit ? bookHit : "(NULL)");
7951 if(bookHit != NULL && !cps->bookSuspend) {
7952 // make sure opponent is not going to reply after receiving move to book position
7953 SendToProgram("force\n", cps);
7954 cps->bookSuspend = TRUE; // flag indicating it has to be restarted
7956 if(!initial) SendMoveToProgram(moveNr, cps); // with hit on initial position there is no move
7957 // now arrange restart after book miss
7959 // after a book hit we never send 'go', and the code after the call to this routine
7960 // has '&& !bookHit' added to suppress potential sending there (based on 'firstMove').
7961 char buf[MSG_SIZ], *move = bookHit;
7963 int fromX, fromY, toX, toY;
7967 if (ParseOneMove(bookHit, forwardMostMove, &moveType,
7968 &fromX, &fromY, &toX, &toY, &promoChar)) {
7969 (void) CoordsToAlgebraic(boards[forwardMostMove],
7970 PosFlags(forwardMostMove),
7971 fromY, fromX, toY, toX, promoChar, move);
7973 if(appData.debugMode) fprintf(debugFP, "Book move could not be parsed\n");
7977 snprintf(buf, MSG_SIZ, "%s%s\n", (cps->useUsermove ? "usermove " : ""), move); // force book move into program supposed to play it
7978 SendToProgram(buf, cps);
7979 if(!initial) firstMove = FALSE; // normally we would clear the firstMove condition after return & sending 'go'
7980 } else if(initial) { // 'go' was needed irrespective of firstMove, and it has to be done in this routine
7981 SendToProgram("go\n", cps);
7982 cps->bookSuspend = FALSE; // after a 'go' we are never suspended
7983 } else { // 'go' might be sent based on 'firstMove' after this routine returns
7984 if(cps->bookSuspend && !firstMove) // 'go' needed, and it will not be done after we return
7985 SendToProgram("go\n", cps);
7986 cps->bookSuspend = FALSE; // anyhow, we will not be suspended after a miss
7988 return bookHit; // notify caller of hit, so it can take action to send move to opponent
7992 LoadError (char *errmess, ChessProgramState *cps)
7993 { // unloads engine and switches back to -ncp mode if it was first
7994 if(cps->initDone) return FALSE;
7995 cps->isr = NULL; // this should suppress further error popups from breaking pipes
7996 DestroyChildProcess(cps->pr, 9 ); // just to be sure
7999 appData.noChessProgram = TRUE;
8000 gameMode = MachinePlaysBlack; ModeHighlight(); // kludge to unmark Machine Black menu
8001 gameMode = BeginningOfGame; ModeHighlight();
8004 if(GetDelayedEvent()) CancelDelayedEvent(), ThawUI(); // [HGM] cancel remaining loading effort scheduled after feature timeout
8005 DisplayMessage("", ""); // erase waiting message
8006 if(errmess) DisplayError(errmess, 0); // announce reason, if given
8011 ChessProgramState *savedState;
8013 DeferredBookMove (void)
8015 if(savedState->lastPing != savedState->lastPong)
8016 ScheduleDelayedEvent(DeferredBookMove, 10);
8018 HandleMachineMove(savedMessage, savedState);
8021 static int savedWhitePlayer, savedBlackPlayer, pairingReceived;
8024 HandleMachineMove (char *message, ChessProgramState *cps)
8026 char machineMove[MSG_SIZ], buf1[MSG_SIZ*10], buf2[MSG_SIZ];
8027 char realname[MSG_SIZ];
8028 int fromX, fromY, toX, toY;
8032 int machineWhite, oldError;
8035 if(cps == &pairing && sscanf(message, "%d-%d", &savedWhitePlayer, &savedBlackPlayer) == 2) {
8036 // [HGM] pairing: Mega-hack! Pairing engine also uses this routine (so it could give other WB commands).
8037 if(savedWhitePlayer == 0 || savedBlackPlayer == 0) {
8038 DisplayError(_("Invalid pairing from pairing engine"), 0);
8041 pairingReceived = 1;
8043 return; // Skim the pairing messages here.
8046 oldError = cps->userError; cps->userError = 0;
8048 FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book hit
8050 * Kludge to ignore BEL characters
8052 while (*message == '\007') message++;
8055 * [HGM] engine debug message: ignore lines starting with '#' character
8057 if(cps->debug && *message == '#') return;
8060 * Look for book output
8062 if (cps == &first && bookRequested) {
8063 if (message[0] == '\t' || message[0] == ' ') {
8064 /* Part of the book output is here; append it */
8065 strcat(bookOutput, message);
8066 strcat(bookOutput, " \n");
8068 } else if (bookOutput[0] != NULLCHAR) {
8069 /* All of book output has arrived; display it */
8070 char *p = bookOutput;
8071 while (*p != NULLCHAR) {
8072 if (*p == '\t') *p = ' ';
8075 DisplayInformation(bookOutput);
8076 bookRequested = FALSE;
8077 /* Fall through to parse the current output */
8082 * Look for machine move.
8084 if ((sscanf(message, "%s %s %s", buf1, buf2, machineMove) == 3 && strcmp(buf2, "...") == 0) ||
8085 (sscanf(message, "%s %s", buf1, machineMove) == 2 && strcmp(buf1, "move") == 0))
8087 /* This method is only useful on engines that support ping */
8088 if (cps->lastPing != cps->lastPong) {
8089 if (gameMode == BeginningOfGame) {
8090 /* Extra move from before last new; ignore */
8091 if (appData.debugMode) {
8092 fprintf(debugFP, "Ignoring extra move from %s\n", cps->which);
8095 if (appData.debugMode) {
8096 fprintf(debugFP, "Undoing extra move from %s, gameMode %d\n",
8097 cps->which, gameMode);
8100 SendToProgram("undo\n", cps);
8106 case BeginningOfGame:
8107 /* Extra move from before last reset; ignore */
8108 if (appData.debugMode) {
8109 fprintf(debugFP, "Ignoring extra move from %s\n", cps->which);
8116 /* Extra move after we tried to stop. The mode test is
8117 not a reliable way of detecting this problem, but it's
8118 the best we can do on engines that don't support ping.
8120 if (appData.debugMode) {
8121 fprintf(debugFP, "Undoing extra move from %s, gameMode %d\n",
8122 cps->which, gameMode);
8124 SendToProgram("undo\n", cps);
8127 case MachinePlaysWhite:
8128 case IcsPlayingWhite:
8129 machineWhite = TRUE;
8132 case MachinePlaysBlack:
8133 case IcsPlayingBlack:
8134 machineWhite = FALSE;
8137 case TwoMachinesPlay:
8138 machineWhite = (cps->twoMachinesColor[0] == 'w');
8141 if (WhiteOnMove(forwardMostMove) != machineWhite) {
8142 if (appData.debugMode) {
8144 "Ignoring move out of turn by %s, gameMode %d"
8145 ", forwardMost %d\n",
8146 cps->which, gameMode, forwardMostMove);
8151 if(cps->alphaRank) AlphaRank(machineMove, 4);
8152 if (!ParseOneMove(machineMove, forwardMostMove, &moveType,
8153 &fromX, &fromY, &toX, &toY, &promoChar)) {
8154 /* Machine move could not be parsed; ignore it. */
8155 snprintf(buf1, MSG_SIZ*10, _("Illegal move \"%s\" from %s machine"),
8156 machineMove, _(cps->which));
8157 DisplayError(buf1, 0);
8158 snprintf(buf1, MSG_SIZ*10, "Xboard: Forfeit due to invalid move: %s (%c%c%c%c) res=%d",
8159 machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, moveType);
8160 if (gameMode == TwoMachinesPlay) {
8161 GameEnds(machineWhite ? BlackWins : WhiteWins,
8167 /* [HGM] Apparently legal, but so far only tested with EP_UNKOWN */
8168 /* So we have to redo legality test with true e.p. status here, */
8169 /* to make sure an illegal e.p. capture does not slip through, */
8170 /* to cause a forfeit on a justified illegal-move complaint */
8171 /* of the opponent. */
8172 if( gameMode==TwoMachinesPlay && appData.testLegality ) {
8174 moveType = LegalityTest(boards[forwardMostMove], PosFlags(forwardMostMove),
8175 fromY, fromX, toY, toX, promoChar);
8176 if(moveType == IllegalMove) {
8177 snprintf(buf1, MSG_SIZ*10, "Xboard: Forfeit due to illegal move: %s (%c%c%c%c)%c",
8178 machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, 0);
8179 GameEnds(machineWhite ? BlackWins : WhiteWins,
8182 } else if(gameInfo.variant != VariantFischeRandom && gameInfo.variant != VariantCapaRandom)
8183 /* [HGM] Kludge to handle engines that send FRC-style castling
8184 when they shouldn't (like TSCP-Gothic) */
8186 case WhiteASideCastleFR:
8187 case BlackASideCastleFR:
8189 currentMoveString[2]++;
8191 case WhiteHSideCastleFR:
8192 case BlackHSideCastleFR:
8194 currentMoveString[2]--;
8196 default: ; // nothing to do, but suppresses warning of pedantic compilers
8199 hintRequested = FALSE;
8200 lastHint[0] = NULLCHAR;
8201 bookRequested = FALSE;
8202 /* Program may be pondering now */
8203 cps->maybeThinking = TRUE;
8204 if (cps->sendTime == 2) cps->sendTime = 1;
8205 if (cps->offeredDraw) cps->offeredDraw--;
8207 /* [AS] Save move info*/
8208 pvInfoList[ forwardMostMove ].score = programStats.score;
8209 pvInfoList[ forwardMostMove ].depth = programStats.depth;
8210 pvInfoList[ forwardMostMove ].time = programStats.time; // [HGM] PGNtime: take time from engine stats
8212 MakeMove(fromX, fromY, toX, toY, promoChar);/*updates forwardMostMove*/
8214 /* [AS] Adjudicate game if needed (note: remember that forwardMostMove now points past the last move) */
8215 if( gameMode == TwoMachinesPlay && adjudicateLossThreshold != 0 && forwardMostMove >= adjudicateLossPlies ) {
8218 while( count < adjudicateLossPlies ) {
8219 int score = pvInfoList[ forwardMostMove - count - 1 ].score;
8222 score = -score; /* Flip score for winning side */
8225 if( score > adjudicateLossThreshold ) {
8232 if( count >= adjudicateLossPlies ) {
8233 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
8235 GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
8236 "Xboard adjudication",
8243 if(Adjudicate(cps)) {
8244 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
8245 return; // [HGM] adjudicate: for all automatic game ends
8249 if ((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack) &&
8251 if(cps->offeredDraw && (signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
8252 SendToICS(ics_prefix); // [HGM] drawclaim: send caim and move on one line for FICS
8254 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
8256 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
8258 if(appData.autoKibitz && !appData.icsEngineAnalyze ) { /* [HGM] kibitz: send most-recent PV info to ICS */
8259 char buf[3*MSG_SIZ];
8261 snprintf(buf, 3*MSG_SIZ, "kibitz !!! %+.2f/%d (%.2f sec, %u nodes, %.0f knps) PV=%s\n",
8262 programStats.score / 100.,
8264 programStats.time / 100.,
8265 (unsigned int)programStats.nodes,
8266 (unsigned int)programStats.nodes / (10*abs(programStats.time) + 1.),
8267 programStats.movelist);
8269 if(appData.debugMode) fprintf(debugFP, "nodes = %d, %lld\n", (int) programStats.nodes, programStats.nodes);
8274 /* [AS] Clear stats for next move */
8275 ClearProgramStats();
8276 thinkOutput[0] = NULLCHAR;
8277 hiddenThinkOutputState = 0;
8280 if (gameMode == TwoMachinesPlay) {
8281 /* [HGM] relaying draw offers moved to after reception of move */
8282 /* and interpreting offer as claim if it brings draw condition */
8283 if (cps->offeredDraw == 1 && cps->other->sendDrawOffers) {
8284 SendToProgram("draw\n", cps->other);
8286 if (cps->other->sendTime) {
8287 SendTimeRemaining(cps->other,
8288 cps->other->twoMachinesColor[0] == 'w');
8290 bookHit = SendMoveToBookUser(forwardMostMove-1, cps->other, FALSE);
8291 if (firstMove && !bookHit) {
8293 if (cps->other->useColors) {
8294 SendToProgram(cps->other->twoMachinesColor, cps->other);
8296 SendToProgram("go\n", cps->other);
8298 cps->other->maybeThinking = TRUE;
8301 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
8303 if (!pausing && appData.ringBellAfterMoves) {
8308 * Reenable menu items that were disabled while
8309 * machine was thinking
8311 if (gameMode != TwoMachinesPlay)
8312 SetUserThinkingEnables();
8314 // [HGM] book: after book hit opponent has received move and is now in force mode
8315 // force the book reply into it, and then fake that it outputted this move by jumping
8316 // back to the beginning of HandleMachineMove, with cps toggled and message set to this move
8318 static char bookMove[MSG_SIZ]; // a bit generous?
8320 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
8321 strcat(bookMove, bookHit);
8324 programStats.nodes = programStats.depth = programStats.time =
8325 programStats.score = programStats.got_only_move = 0;
8326 sprintf(programStats.movelist, "%s (xbook)", bookHit);
8328 if(cps->lastPing != cps->lastPong) {
8329 savedMessage = message; // args for deferred call
8331 ScheduleDelayedEvent(DeferredBookMove, 10);
8340 /* Set special modes for chess engines. Later something general
8341 * could be added here; for now there is just one kludge feature,
8342 * needed because Crafty 15.10 and earlier don't ignore SIGINT
8343 * when "xboard" is given as an interactive command.
8345 if (strncmp(message, "kibitz Hello from Crafty", 24) == 0) {
8346 cps->useSigint = FALSE;
8347 cps->useSigterm = FALSE;
8349 if (strncmp(message, "feature ", 8) == 0) { // [HGM] moved forward to pre-empt non-compliant commands
8350 ParseFeatures(message+8, cps);
8351 return; // [HGM] This return was missing, causing option features to be recognized as non-compliant commands!
8354 if ((!appData.testLegality || gameInfo.variant == VariantFairy) &&
8355 !strncmp(message, "setup ", 6)) { // [HGM] allow first engine to define opening position
8356 int dummy, s=6; char buf[MSG_SIZ];
8357 if(appData.icsActive || forwardMostMove != 0 || cps != &first) return;
8358 if(sscanf(message, "setup (%s", buf) == 1) s = 8 + strlen(buf), buf[s-9] = NULLCHAR, SetCharTable(pieceToChar, buf);
8359 if(startedFromSetupPosition) return;
8360 ParseFEN(boards[0], &dummy, message+s);
8361 DrawPosition(TRUE, boards[0]);
8362 startedFromSetupPosition = TRUE;
8365 /* [HGM] Allow engine to set up a position. Don't ask me why one would
8366 * want this, I was asked to put it in, and obliged.
8368 if (!strncmp(message, "setboard ", 9)) {
8369 Board initial_position;
8371 GameEnds(GameUnfinished, "Engine aborts game", GE_XBOARD);
8373 if (!ParseFEN(initial_position, &blackPlaysFirst, message + 9)) {
8374 DisplayError(_("Bad FEN received from engine"), 0);
8378 CopyBoard(boards[0], initial_position);
8379 initialRulePlies = FENrulePlies;
8380 if(blackPlaysFirst) gameMode = MachinePlaysWhite;
8381 else gameMode = MachinePlaysBlack;
8382 DrawPosition(FALSE, boards[currentMove]);
8388 * Look for communication commands
8390 if (!strncmp(message, "telluser ", 9)) {
8391 if(message[9] == '\\' && message[10] == '\\')
8392 EscapeExpand(message+9, message+11); // [HGM] esc: allow escape sequences in popup box
8394 DisplayNote(message + 9);
8397 if (!strncmp(message, "tellusererror ", 14)) {
8399 if(message[14] == '\\' && message[15] == '\\')
8400 EscapeExpand(message+14, message+16); // [HGM] esc: allow escape sequences in popup box
8402 DisplayError(message + 14, 0);
8405 if (!strncmp(message, "tellopponent ", 13)) {
8406 if (appData.icsActive) {
8408 snprintf(buf1, sizeof(buf1), "%ssay %s\n", ics_prefix, message + 13);
8412 DisplayNote(message + 13);
8416 if (!strncmp(message, "tellothers ", 11)) {
8417 if (appData.icsActive) {
8419 snprintf(buf1, sizeof(buf1), "%swhisper %s\n", ics_prefix, message + 11);
8425 if (!strncmp(message, "tellall ", 8)) {
8426 if (appData.icsActive) {
8428 snprintf(buf1, sizeof(buf1), "%skibitz %s\n", ics_prefix, message + 8);
8432 DisplayNote(message + 8);
8436 if (strncmp(message, "warning", 7) == 0) {
8437 /* Undocumented feature, use tellusererror in new code */
8438 DisplayError(message, 0);
8441 if (sscanf(message, "askuser %s %[^\n]", buf1, buf2) == 2) {
8442 safeStrCpy(realname, cps->tidy, sizeof(realname)/sizeof(realname[0]));
8443 strcat(realname, " query");
8444 AskQuestion(realname, buf2, buf1, cps->pr);
8447 /* Commands from the engine directly to ICS. We don't allow these to be
8448 * sent until we are logged on. Crafty kibitzes have been known to
8449 * interfere with the login process.
8452 if (!strncmp(message, "tellics ", 8)) {
8453 SendToICS(message + 8);
8457 if (!strncmp(message, "tellicsnoalias ", 15)) {
8458 SendToICS(ics_prefix);
8459 SendToICS(message + 15);
8463 /* The following are for backward compatibility only */
8464 if (!strncmp(message,"whisper",7) || !strncmp(message,"kibitz",6) ||
8465 !strncmp(message,"draw",4) || !strncmp(message,"tell",3)) {
8466 SendToICS(ics_prefix);
8472 if (sscanf(message, "pong %d", &cps->lastPong) == 1) {
8476 * If the move is illegal, cancel it and redraw the board.
8477 * Also deal with other error cases. Matching is rather loose
8478 * here to accommodate engines written before the spec.
8480 if (strncmp(message + 1, "llegal move", 11) == 0 ||
8481 strncmp(message, "Error", 5) == 0) {
8482 if (StrStr(message, "name") ||
8483 StrStr(message, "rating") || StrStr(message, "?") ||
8484 StrStr(message, "result") || StrStr(message, "board") ||
8485 StrStr(message, "bk") || StrStr(message, "computer") ||
8486 StrStr(message, "variant") || StrStr(message, "hint") ||
8487 StrStr(message, "random") || StrStr(message, "depth") ||
8488 StrStr(message, "accepted")) {
8491 if (StrStr(message, "protover")) {
8492 /* Program is responding to input, so it's apparently done
8493 initializing, and this error message indicates it is
8494 protocol version 1. So we don't need to wait any longer
8495 for it to initialize and send feature commands. */
8496 FeatureDone(cps, 1);
8497 cps->protocolVersion = 1;
8500 cps->maybeThinking = FALSE;
8502 if (StrStr(message, "draw")) {
8503 /* Program doesn't have "draw" command */
8504 cps->sendDrawOffers = 0;
8507 if (cps->sendTime != 1 &&
8508 (StrStr(message, "time") || StrStr(message, "otim"))) {
8509 /* Program apparently doesn't have "time" or "otim" command */
8513 if (StrStr(message, "analyze")) {
8514 cps->analysisSupport = FALSE;
8515 cps->analyzing = FALSE;
8516 // Reset(FALSE, TRUE); // [HGM] this caused discrepancy between display and internal state!
8517 EditGameEvent(); // [HGM] try to preserve loaded game
8518 snprintf(buf2,MSG_SIZ, _("%s does not support analysis"), cps->tidy);
8519 DisplayError(buf2, 0);
8522 if (StrStr(message, "(no matching move)st")) {
8523 /* Special kludge for GNU Chess 4 only */
8524 cps->stKludge = TRUE;
8525 SendTimeControl(cps, movesPerSession, timeControl,
8526 timeIncrement, appData.searchDepth,
8530 if (StrStr(message, "(no matching move)sd")) {
8531 /* Special kludge for GNU Chess 4 only */
8532 cps->sdKludge = TRUE;
8533 SendTimeControl(cps, movesPerSession, timeControl,
8534 timeIncrement, appData.searchDepth,
8538 if (!StrStr(message, "llegal")) {
8541 if (gameMode == BeginningOfGame || gameMode == EndOfGame ||
8542 gameMode == IcsIdle) return;
8543 if (forwardMostMove <= backwardMostMove) return;
8544 if (pausing) PauseEvent();
8545 if(appData.forceIllegal) {
8546 // [HGM] illegal: machine refused move; force position after move into it
8547 SendToProgram("force\n", cps);
8548 if(!cps->useSetboard) { // hideous kludge on kludge, because SendBoard sucks.
8549 // we have a real problem now, as SendBoard will use the a2a3 kludge
8550 // when black is to move, while there might be nothing on a2 or black
8551 // might already have the move. So send the board as if white has the move.
8552 // But first we must change the stm of the engine, as it refused the last move
8553 SendBoard(cps, 0); // always kludgeless, as white is to move on boards[0]
8554 if(WhiteOnMove(forwardMostMove)) {
8555 SendToProgram("a7a6\n", cps); // for the engine black still had the move
8556 SendBoard(cps, forwardMostMove); // kludgeless board
8558 SendToProgram("a2a3\n", cps); // for the engine white still had the move
8559 CopyBoard(boards[forwardMostMove+1], boards[forwardMostMove]);
8560 SendBoard(cps, forwardMostMove+1); // kludgeless board
8562 } else SendBoard(cps, forwardMostMove); // FEN case, also sets stm properly
8563 if(gameMode == MachinePlaysWhite || gameMode == MachinePlaysBlack ||
8564 gameMode == TwoMachinesPlay)
8565 SendToProgram("go\n", cps);
8568 if (gameMode == PlayFromGameFile) {
8569 /* Stop reading this game file */
8570 gameMode = EditGame;
8573 /* [HGM] illegal-move claim should forfeit game when Xboard */
8574 /* only passes fully legal moves */
8575 if( appData.testLegality && gameMode == TwoMachinesPlay ) {
8576 GameEnds( cps->twoMachinesColor[0] == 'w' ? BlackWins : WhiteWins,
8577 "False illegal-move claim", GE_XBOARD );
8578 return; // do not take back move we tested as valid
8580 currentMove = forwardMostMove-1;
8581 DisplayMove(currentMove-1); /* before DisplayMoveError */
8582 SwitchClocks(forwardMostMove-1); // [HGM] race
8583 DisplayBothClocks();
8584 snprintf(buf1, 10*MSG_SIZ, _("Illegal move \"%s\" (rejected by %s chess program)"),
8585 parseList[currentMove], _(cps->which));
8586 DisplayMoveError(buf1);
8587 DrawPosition(FALSE, boards[currentMove]);
8589 SetUserThinkingEnables();
8592 if (strncmp(message, "time", 4) == 0 && StrStr(message, "Illegal")) {
8593 /* Program has a broken "time" command that
8594 outputs a string not ending in newline.
8600 * If chess program startup fails, exit with an error message.
8601 * Attempts to recover here are futile. [HGM] Well, we try anyway
8603 if ((StrStr(message, "unknown host") != NULL)
8604 || (StrStr(message, "No remote directory") != NULL)
8605 || (StrStr(message, "not found") != NULL)
8606 || (StrStr(message, "No such file") != NULL)
8607 || (StrStr(message, "can't alloc") != NULL)
8608 || (StrStr(message, "Permission denied") != NULL)) {
8610 cps->maybeThinking = FALSE;
8611 snprintf(buf1, sizeof(buf1), _("Failed to start %s chess program %s on %s: %s\n"),
8612 _(cps->which), cps->program, cps->host, message);
8613 RemoveInputSource(cps->isr);
8614 if(appData.icsActive) DisplayFatalError(buf1, 0, 1); else {
8615 if(LoadError(oldError ? NULL : buf1, cps)) return; // error has then been handled by LoadError
8616 if(!oldError) DisplayError(buf1, 0); // if reason neatly announced, suppress general error popup
8622 * Look for hint output
8624 if (sscanf(message, "Hint: %s", buf1) == 1) {
8625 if (cps == &first && hintRequested) {
8626 hintRequested = FALSE;
8627 if (ParseOneMove(buf1, forwardMostMove, &moveType,
8628 &fromX, &fromY, &toX, &toY, &promoChar)) {
8629 (void) CoordsToAlgebraic(boards[forwardMostMove],
8630 PosFlags(forwardMostMove),
8631 fromY, fromX, toY, toX, promoChar, buf1);
8632 snprintf(buf2, sizeof(buf2), _("Hint: %s"), buf1);
8633 DisplayInformation(buf2);
8635 /* Hint move could not be parsed!? */
8636 snprintf(buf2, sizeof(buf2),
8637 _("Illegal hint move \"%s\"\nfrom %s chess program"),
8638 buf1, _(cps->which));
8639 DisplayError(buf2, 0);
8642 safeStrCpy(lastHint, buf1, sizeof(lastHint)/sizeof(lastHint[0]));
8648 * Ignore other messages if game is not in progress
8650 if (gameMode == BeginningOfGame || gameMode == EndOfGame ||
8651 gameMode == IcsIdle || cps->lastPing != cps->lastPong) return;
8654 * look for win, lose, draw, or draw offer
8656 if (strncmp(message, "1-0", 3) == 0) {
8657 char *p, *q, *r = "";
8658 p = strchr(message, '{');
8666 GameEnds(WhiteWins, r, GE_ENGINE1 + (cps != &first)); /* [HGM] pass claimer indication for claim test */
8668 } else if (strncmp(message, "0-1", 3) == 0) {
8669 char *p, *q, *r = "";
8670 p = strchr(message, '{');
8678 /* Kludge for Arasan 4.1 bug */
8679 if (strcmp(r, "Black resigns") == 0) {
8680 GameEnds(WhiteWins, r, GE_ENGINE1 + (cps != &first));
8683 GameEnds(BlackWins, r, GE_ENGINE1 + (cps != &first));
8685 } else if (strncmp(message, "1/2", 3) == 0) {
8686 char *p, *q, *r = "";
8687 p = strchr(message, '{');
8696 GameEnds(GameIsDrawn, r, GE_ENGINE1 + (cps != &first));
8699 } else if (strncmp(message, "White resign", 12) == 0) {
8700 GameEnds(BlackWins, "White resigns", GE_ENGINE1 + (cps != &first));
8702 } else if (strncmp(message, "Black resign", 12) == 0) {
8703 GameEnds(WhiteWins, "Black resigns", GE_ENGINE1 + (cps != &first));
8705 } else if (strncmp(message, "White matches", 13) == 0 ||
8706 strncmp(message, "Black matches", 13) == 0 ) {
8707 /* [HGM] ignore GNUShogi noises */
8709 } else if (strncmp(message, "White", 5) == 0 &&
8710 message[5] != '(' &&
8711 StrStr(message, "Black") == NULL) {
8712 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
8714 } else if (strncmp(message, "Black", 5) == 0 &&
8715 message[5] != '(') {
8716 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
8718 } else if (strcmp(message, "resign") == 0 ||
8719 strcmp(message, "computer resigns") == 0) {
8721 case MachinePlaysBlack:
8722 case IcsPlayingBlack:
8723 GameEnds(WhiteWins, "Black resigns", GE_ENGINE);
8725 case MachinePlaysWhite:
8726 case IcsPlayingWhite:
8727 GameEnds(BlackWins, "White resigns", GE_ENGINE);
8729 case TwoMachinesPlay:
8730 if (cps->twoMachinesColor[0] == 'w')
8731 GameEnds(BlackWins, "White resigns", GE_ENGINE1 + (cps != &first));
8733 GameEnds(WhiteWins, "Black resigns", GE_ENGINE1 + (cps != &first));
8740 } else if (strncmp(message, "opponent mates", 14) == 0) {
8742 case MachinePlaysBlack:
8743 case IcsPlayingBlack:
8744 GameEnds(WhiteWins, "White mates", GE_ENGINE);
8746 case MachinePlaysWhite:
8747 case IcsPlayingWhite:
8748 GameEnds(BlackWins, "Black mates", GE_ENGINE);
8750 case TwoMachinesPlay:
8751 if (cps->twoMachinesColor[0] == 'w')
8752 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
8754 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
8761 } else if (strncmp(message, "computer mates", 14) == 0) {
8763 case MachinePlaysBlack:
8764 case IcsPlayingBlack:
8765 GameEnds(BlackWins, "Black mates", GE_ENGINE1);
8767 case MachinePlaysWhite:
8768 case IcsPlayingWhite:
8769 GameEnds(WhiteWins, "White mates", GE_ENGINE);
8771 case TwoMachinesPlay:
8772 if (cps->twoMachinesColor[0] == 'w')
8773 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
8775 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
8782 } else if (strncmp(message, "checkmate", 9) == 0) {
8783 if (WhiteOnMove(forwardMostMove)) {
8784 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
8786 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
8789 } else if (strstr(message, "Draw") != NULL ||
8790 strstr(message, "game is a draw") != NULL) {
8791 GameEnds(GameIsDrawn, "Draw", GE_ENGINE1 + (cps != &first));
8793 } else if (strstr(message, "offer") != NULL &&
8794 strstr(message, "draw") != NULL) {
8796 if (appData.zippyPlay && first.initDone) {
8797 /* Relay offer to ICS */
8798 SendToICS(ics_prefix);
8799 SendToICS("draw\n");
8802 cps->offeredDraw = 2; /* valid until this engine moves twice */
8803 if (gameMode == TwoMachinesPlay) {
8804 if (cps->other->offeredDraw) {
8805 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
8806 /* [HGM] in two-machine mode we delay relaying draw offer */
8807 /* until after we also have move, to see if it is really claim */
8809 } else if (gameMode == MachinePlaysWhite ||
8810 gameMode == MachinePlaysBlack) {
8811 if (userOfferedDraw) {
8812 DisplayInformation(_("Machine accepts your draw offer"));
8813 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
8815 DisplayInformation(_("Machine offers a draw\nSelect Action / Draw to agree"));
8822 * Look for thinking output
8824 if ( appData.showThinking // [HGM] thinking: test all options that cause this output
8825 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp()
8827 int plylev, mvleft, mvtot, curscore, time;
8828 char mvname[MOVE_LEN];
8832 int prefixHint = FALSE;
8833 mvname[0] = NULLCHAR;
8836 case MachinePlaysBlack:
8837 case IcsPlayingBlack:
8838 if (WhiteOnMove(forwardMostMove)) prefixHint = TRUE;
8840 case MachinePlaysWhite:
8841 case IcsPlayingWhite:
8842 if (!WhiteOnMove(forwardMostMove)) prefixHint = TRUE;
8847 case IcsObserving: /* [DM] icsEngineAnalyze */
8848 if (!appData.icsEngineAnalyze) ignore = TRUE;
8850 case TwoMachinesPlay:
8851 if ((cps->twoMachinesColor[0] == 'w') != WhiteOnMove(forwardMostMove)) {
8861 ChessProgramStats tempStats = programStats; // [HGM] info: filter out info lines
8863 if (sscanf(message, "%d%c %d %d " u64Display " %[^\n]\n",
8864 &plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5) {
8866 if (plyext != ' ' && plyext != '\t') {
8870 /* [AS] Negate score if machine is playing black and reporting absolute scores */
8871 if( cps->scoreIsAbsolute &&
8872 ( gameMode == MachinePlaysBlack ||
8873 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b' ||
8874 gameMode == IcsPlayingBlack || // [HGM] also add other situations where engine should report black POV
8875 (gameMode == AnalyzeMode || gameMode == AnalyzeFile || gameMode == IcsObserving && appData.icsEngineAnalyze) &&
8876 !WhiteOnMove(currentMove)
8879 curscore = -curscore;
8882 if(appData.pvSAN[cps==&second]) pv = PvToSAN(buf1);
8884 if(serverMoves && (time > 100 || time == 0 && plylev > 7)) {
8887 snprintf(buf, MSG_SIZ, "%s", appData.serverMovesName);
8888 buf[strlen(buf)-1] = gameMode == MachinePlaysWhite ? 'w' :
8889 gameMode == MachinePlaysBlack ? 'b' : cps->twoMachinesColor[0];
8890 if(appData.debugMode) fprintf(debugFP, "write PV on file '%s'\n", buf);
8891 if(f = fopen(buf, "w")) { // export PV to applicable PV file
8892 fprintf(f, "%5.2f/%-2d %s", curscore/100., plylev, pv);
8894 } else DisplayError(_("failed writing PV"), 0);
8897 tempStats.depth = plylev;
8898 tempStats.nodes = nodes;
8899 tempStats.time = time;
8900 tempStats.score = curscore;
8901 tempStats.got_only_move = 0;
8903 if(cps->nps >= 0) { /* [HGM] nps: use engine nodes or time to decrement clock */
8906 if(cps->nps == 0) ticklen = 10*time; // use engine reported time
8907 else ticklen = (1000. * u64ToDouble(nodes)) / cps->nps; // convert node count to time
8908 if(WhiteOnMove(forwardMostMove) && (gameMode == MachinePlaysWhite ||
8909 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'w'))
8910 whiteTimeRemaining = timeRemaining[0][forwardMostMove] - ticklen;
8911 if(!WhiteOnMove(forwardMostMove) && (gameMode == MachinePlaysBlack ||
8912 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b'))
8913 blackTimeRemaining = timeRemaining[1][forwardMostMove] - ticklen;
8916 /* Buffer overflow protection */
8917 if (pv[0] != NULLCHAR) {
8918 if (strlen(pv) >= sizeof(tempStats.movelist)
8919 && appData.debugMode) {
8921 "PV is too long; using the first %u bytes.\n",
8922 (unsigned) sizeof(tempStats.movelist) - 1);
8925 safeStrCpy( tempStats.movelist, pv, sizeof(tempStats.movelist)/sizeof(tempStats.movelist[0]) );
8927 sprintf(tempStats.movelist, " no PV\n");
8930 if (tempStats.seen_stat) {
8931 tempStats.ok_to_send = 1;
8934 if (strchr(tempStats.movelist, '(') != NULL) {
8935 tempStats.line_is_book = 1;
8936 tempStats.nr_moves = 0;
8937 tempStats.moves_left = 0;
8939 tempStats.line_is_book = 0;
8942 if(tempStats.score != 0 || tempStats.nodes != 0 || tempStats.time != 0)
8943 programStats = tempStats; // [HGM] info: only set stats if genuine PV and not an info line
8945 SendProgramStatsToFrontend( cps, &tempStats );
8948 [AS] Protect the thinkOutput buffer from overflow... this
8949 is only useful if buf1 hasn't overflowed first!
8951 snprintf(thinkOutput, sizeof(thinkOutput)/sizeof(thinkOutput[0]), "[%d]%c%+.2f %s%s",
8953 (gameMode == TwoMachinesPlay ?
8954 ToUpper(cps->twoMachinesColor[0]) : ' '),
8955 ((double) curscore) / 100.0,
8956 prefixHint ? lastHint : "",
8957 prefixHint ? " " : "" );
8959 if( buf1[0] != NULLCHAR ) {
8960 unsigned max_len = sizeof(thinkOutput) - strlen(thinkOutput) - 1;
8962 if( strlen(pv) > max_len ) {
8963 if( appData.debugMode) {
8964 fprintf(debugFP,"PV is too long for thinkOutput, truncating.\n");
8966 pv[max_len+1] = '\0';
8969 strcat( thinkOutput, pv);
8972 if (currentMove == forwardMostMove || gameMode == AnalyzeMode
8973 || gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
8974 DisplayMove(currentMove - 1);
8978 } else if ((p=StrStr(message, "(only move)")) != NULL) {
8979 /* crafty (9.25+) says "(only move) <move>"
8980 * if there is only 1 legal move
8982 sscanf(p, "(only move) %s", buf1);
8983 snprintf(thinkOutput, sizeof(thinkOutput)/sizeof(thinkOutput[0]), "%s (only move)", buf1);
8984 sprintf(programStats.movelist, "%s (only move)", buf1);
8985 programStats.depth = 1;
8986 programStats.nr_moves = 1;
8987 programStats.moves_left = 1;
8988 programStats.nodes = 1;
8989 programStats.time = 1;
8990 programStats.got_only_move = 1;
8992 /* Not really, but we also use this member to
8993 mean "line isn't going to change" (Crafty
8994 isn't searching, so stats won't change) */
8995 programStats.line_is_book = 1;
8997 SendProgramStatsToFrontend( cps, &programStats );
8999 if (currentMove == forwardMostMove || gameMode==AnalyzeMode ||
9000 gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
9001 DisplayMove(currentMove - 1);
9004 } else if (sscanf(message,"stat01: %d " u64Display " %d %d %d %s",
9005 &time, &nodes, &plylev, &mvleft,
9006 &mvtot, mvname) >= 5) {
9007 /* The stat01: line is from Crafty (9.29+) in response
9008 to the "." command */
9009 programStats.seen_stat = 1;
9010 cps->maybeThinking = TRUE;
9012 if (programStats.got_only_move || !appData.periodicUpdates)
9015 programStats.depth = plylev;
9016 programStats.time = time;
9017 programStats.nodes = nodes;
9018 programStats.moves_left = mvleft;
9019 programStats.nr_moves = mvtot;
9020 safeStrCpy(programStats.move_name, mvname, sizeof(programStats.move_name)/sizeof(programStats.move_name[0]));
9021 programStats.ok_to_send = 1;
9022 programStats.movelist[0] = '\0';
9024 SendProgramStatsToFrontend( cps, &programStats );
9028 } else if (strncmp(message,"++",2) == 0) {
9029 /* Crafty 9.29+ outputs this */
9030 programStats.got_fail = 2;
9033 } else if (strncmp(message,"--",2) == 0) {
9034 /* Crafty 9.29+ outputs this */
9035 programStats.got_fail = 1;
9038 } else if (thinkOutput[0] != NULLCHAR &&
9039 strncmp(message, " ", 4) == 0) {
9040 unsigned message_len;
9043 while (*p && *p == ' ') p++;
9045 message_len = strlen( p );
9047 /* [AS] Avoid buffer overflow */
9048 if( sizeof(thinkOutput) - strlen(thinkOutput) - 1 > message_len ) {
9049 strcat(thinkOutput, " ");
9050 strcat(thinkOutput, p);
9053 if( sizeof(programStats.movelist) - strlen(programStats.movelist) - 1 > message_len ) {
9054 strcat(programStats.movelist, " ");
9055 strcat(programStats.movelist, p);
9058 if (currentMove == forwardMostMove || gameMode==AnalyzeMode ||
9059 gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
9060 DisplayMove(currentMove - 1);
9068 if (sscanf(message, "%d%c %d %d " u64Display " %[^\n]\n",
9069 &plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5)
9071 ChessProgramStats cpstats;
9073 if (plyext != ' ' && plyext != '\t') {
9077 /* [AS] Negate score if machine is playing black and reporting absolute scores */
9078 if( cps->scoreIsAbsolute && ((gameMode == MachinePlaysBlack) || (gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b')) ) {
9079 curscore = -curscore;
9082 cpstats.depth = plylev;
9083 cpstats.nodes = nodes;
9084 cpstats.time = time;
9085 cpstats.score = curscore;
9086 cpstats.got_only_move = 0;
9087 cpstats.movelist[0] = '\0';
9089 if (buf1[0] != NULLCHAR) {
9090 safeStrCpy( cpstats.movelist, buf1, sizeof(cpstats.movelist)/sizeof(cpstats.movelist[0]) );
9093 cpstats.ok_to_send = 0;
9094 cpstats.line_is_book = 0;
9095 cpstats.nr_moves = 0;
9096 cpstats.moves_left = 0;
9098 SendProgramStatsToFrontend( cps, &cpstats );
9105 /* Parse a game score from the character string "game", and
9106 record it as the history of the current game. The game
9107 score is NOT assumed to start from the standard position.
9108 The display is not updated in any way.
9111 ParseGameHistory (char *game)
9114 int fromX, fromY, toX, toY, boardIndex;
9119 if (appData.debugMode)
9120 fprintf(debugFP, "Parsing game history: %s\n", game);
9122 if (gameInfo.event == NULL) gameInfo.event = StrSave("ICS game");
9123 gameInfo.site = StrSave(appData.icsHost);
9124 gameInfo.date = PGNDate();
9125 gameInfo.round = StrSave("-");
9127 /* Parse out names of players */
9128 while (*game == ' ') game++;
9130 while (*game != ' ') *p++ = *game++;
9132 gameInfo.white = StrSave(buf);
9133 while (*game == ' ') game++;
9135 while (*game != ' ' && *game != '\n') *p++ = *game++;
9137 gameInfo.black = StrSave(buf);
9140 boardIndex = blackPlaysFirst ? 1 : 0;
9143 yyboardindex = boardIndex;
9144 moveType = (ChessMove) Myylex();
9146 case IllegalMove: /* maybe suicide chess, etc. */
9147 if (appData.debugMode) {
9148 fprintf(debugFP, "Illegal move from ICS: '%s'\n", yy_text);
9149 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
9150 setbuf(debugFP, NULL);
9152 case WhitePromotion:
9153 case BlackPromotion:
9154 case WhiteNonPromotion:
9155 case BlackNonPromotion:
9157 case WhiteCapturesEnPassant:
9158 case BlackCapturesEnPassant:
9159 case WhiteKingSideCastle:
9160 case WhiteQueenSideCastle:
9161 case BlackKingSideCastle:
9162 case BlackQueenSideCastle:
9163 case WhiteKingSideCastleWild:
9164 case WhiteQueenSideCastleWild:
9165 case BlackKingSideCastleWild:
9166 case BlackQueenSideCastleWild:
9168 case WhiteHSideCastleFR:
9169 case WhiteASideCastleFR:
9170 case BlackHSideCastleFR:
9171 case BlackASideCastleFR:
9173 fromX = currentMoveString[0] - AAA;
9174 fromY = currentMoveString[1] - ONE;
9175 toX = currentMoveString[2] - AAA;
9176 toY = currentMoveString[3] - ONE;
9177 promoChar = currentMoveString[4];
9181 if(currentMoveString[0] == '@') continue; // no null moves in ICS mode!
9182 fromX = moveType == WhiteDrop ?
9183 (int) CharToPiece(ToUpper(currentMoveString[0])) :
9184 (int) CharToPiece(ToLower(currentMoveString[0]));
9186 toX = currentMoveString[2] - AAA;
9187 toY = currentMoveString[3] - ONE;
9188 promoChar = NULLCHAR;
9192 snprintf(buf, MSG_SIZ, _("Ambiguous move in ICS output: \"%s\""), yy_text);
9193 if (appData.debugMode) {
9194 fprintf(debugFP, "Ambiguous move from ICS: '%s'\n", yy_text);
9195 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
9196 setbuf(debugFP, NULL);
9198 DisplayError(buf, 0);
9200 case ImpossibleMove:
9202 snprintf(buf, MSG_SIZ, _("Illegal move in ICS output: \"%s\""), yy_text);
9203 if (appData.debugMode) {
9204 fprintf(debugFP, "Impossible move from ICS: '%s'\n", yy_text);
9205 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
9206 setbuf(debugFP, NULL);
9208 DisplayError(buf, 0);
9211 if (boardIndex < backwardMostMove) {
9212 /* Oops, gap. How did that happen? */
9213 DisplayError(_("Gap in move list"), 0);
9216 backwardMostMove = blackPlaysFirst ? 1 : 0;
9217 if (boardIndex > forwardMostMove) {
9218 forwardMostMove = boardIndex;
9222 if (boardIndex > (blackPlaysFirst ? 1 : 0)) {
9223 strcat(parseList[boardIndex-1], " ");
9224 strcat(parseList[boardIndex-1], yy_text);
9236 case GameUnfinished:
9237 if (gameMode == IcsExamining) {
9238 if (boardIndex < backwardMostMove) {
9239 /* Oops, gap. How did that happen? */
9242 backwardMostMove = blackPlaysFirst ? 1 : 0;
9245 gameInfo.result = moveType;
9246 p = strchr(yy_text, '{');
9247 if (p == NULL) p = strchr(yy_text, '(');
9250 if (p[0] == '0' || p[0] == '1' || p[0] == '*') p = "";
9252 q = strchr(p, *p == '{' ? '}' : ')');
9253 if (q != NULL) *q = NULLCHAR;
9256 while(q = strchr(p, '\n')) *q = ' '; // [HGM] crush linefeeds in result message
9257 gameInfo.resultDetails = StrSave(p);
9260 if (boardIndex >= forwardMostMove &&
9261 !(gameMode == IcsObserving && ics_gamenum == -1)) {
9262 backwardMostMove = blackPlaysFirst ? 1 : 0;
9265 (void) CoordsToAlgebraic(boards[boardIndex], PosFlags(boardIndex),
9266 fromY, fromX, toY, toX, promoChar,
9267 parseList[boardIndex]);
9268 CopyBoard(boards[boardIndex + 1], boards[boardIndex]);
9269 /* currentMoveString is set as a side-effect of yylex */
9270 safeStrCpy(moveList[boardIndex], currentMoveString, sizeof(moveList[boardIndex])/sizeof(moveList[boardIndex][0]));
9271 strcat(moveList[boardIndex], "\n");
9273 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[boardIndex]);
9274 switch (MateTest(boards[boardIndex], PosFlags(boardIndex)) ) {
9280 if(gameInfo.variant != VariantShogi)
9281 strcat(parseList[boardIndex - 1], "+");
9285 strcat(parseList[boardIndex - 1], "#");
9292 /* Apply a move to the given board */
9294 ApplyMove (int fromX, int fromY, int toX, int toY, int promoChar, Board board)
9296 ChessSquare captured = board[toY][toX], piece, king; int p, oldEP = EP_NONE, berolina = 0;
9297 int promoRank = gameInfo.variant == VariantMakruk || gameInfo.variant == VariantGrand ? 3 : 1;
9299 /* [HGM] compute & store e.p. status and castling rights for new position */
9300 /* we can always do that 'in place', now pointers to these rights are passed to ApplyMove */
9302 if(gameInfo.variant == VariantBerolina) berolina = EP_BEROLIN_A;
9303 oldEP = (signed char)board[EP_STATUS];
9304 board[EP_STATUS] = EP_NONE;
9306 if (fromY == DROP_RANK) {
9308 if(fromX == EmptySquare) { // [HGM] pass: empty drop encodes null move; nothing to change.
9309 board[EP_STATUS] = EP_CAPTURE; // null move considered irreversible
9312 piece = board[toY][toX] = (ChessSquare) fromX;
9316 if( board[toY][toX] != EmptySquare )
9317 board[EP_STATUS] = EP_CAPTURE;
9319 if( board[fromY][fromX] == WhiteLance || board[fromY][fromX] == BlackLance ) {
9320 if( gameInfo.variant != VariantSuper && gameInfo.variant != VariantShogi )
9321 board[EP_STATUS] = EP_PAWN_MOVE; // Lance is Pawn-like in most variants
9323 if( board[fromY][fromX] == WhitePawn ) {
9324 if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
9325 board[EP_STATUS] = EP_PAWN_MOVE;
9327 if(toX>BOARD_LEFT && board[toY][toX-1] == BlackPawn &&
9328 gameInfo.variant != VariantBerolina || toX < fromX)
9329 board[EP_STATUS] = toX | berolina;
9330 if(toX<BOARD_RGHT-1 && board[toY][toX+1] == BlackPawn &&
9331 gameInfo.variant != VariantBerolina || toX > fromX)
9332 board[EP_STATUS] = toX;
9335 if( board[fromY][fromX] == BlackPawn ) {
9336 if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
9337 board[EP_STATUS] = EP_PAWN_MOVE;
9338 if( toY-fromY== -2) {
9339 if(toX>BOARD_LEFT && board[toY][toX-1] == WhitePawn &&
9340 gameInfo.variant != VariantBerolina || toX < fromX)
9341 board[EP_STATUS] = toX | berolina;
9342 if(toX<BOARD_RGHT-1 && board[toY][toX+1] == WhitePawn &&
9343 gameInfo.variant != VariantBerolina || toX > fromX)
9344 board[EP_STATUS] = toX;
9348 for(i=0; i<nrCastlingRights; i++) {
9349 if(board[CASTLING][i] == fromX && castlingRank[i] == fromY ||
9350 board[CASTLING][i] == toX && castlingRank[i] == toY
9351 ) board[CASTLING][i] = NoRights; // revoke for moved or captured piece
9354 if(gameInfo.variant == VariantSChess) { // update virginity
9355 if(fromY == 0) board[VIRGIN][fromX] &= ~VIRGIN_W; // loss by moving
9356 if(fromY == BOARD_HEIGHT-1) board[VIRGIN][fromX] &= ~VIRGIN_B;
9357 if(toY == 0) board[VIRGIN][toX] &= ~VIRGIN_W; // loss by capture
9358 if(toY == BOARD_HEIGHT-1) board[VIRGIN][toX] &= ~VIRGIN_B;
9361 if (fromX == toX && fromY == toY) return;
9363 piece = board[fromY][fromX]; /* [HGM] remember, for Shogi promotion */
9364 king = piece < (int) BlackPawn ? WhiteKing : BlackKing; /* [HGM] Knightmate simplify testing for castling */
9365 if(gameInfo.variant == VariantKnightmate)
9366 king += (int) WhiteUnicorn - (int) WhiteKing;
9368 /* Code added by Tord: */
9369 /* FRC castling assumed when king captures friendly rook. [HGM] or RxK for S-Chess */
9370 if (board[fromY][fromX] == WhiteKing && board[toY][toX] == WhiteRook ||
9371 board[fromY][fromX] == WhiteRook && board[toY][toX] == WhiteKing) {
9372 board[fromY][fromX] = EmptySquare;
9373 board[toY][toX] = EmptySquare;
9374 if((toX > fromX) != (piece == WhiteRook)) {
9375 board[0][BOARD_RGHT-2] = WhiteKing; board[0][BOARD_RGHT-3] = WhiteRook;
9377 board[0][BOARD_LEFT+2] = WhiteKing; board[0][BOARD_LEFT+3] = WhiteRook;
9379 } else if (board[fromY][fromX] == BlackKing && board[toY][toX] == BlackRook ||
9380 board[fromY][fromX] == BlackRook && board[toY][toX] == BlackKing) {
9381 board[fromY][fromX] = EmptySquare;
9382 board[toY][toX] = EmptySquare;
9383 if((toX > fromX) != (piece == BlackRook)) {
9384 board[BOARD_HEIGHT-1][BOARD_RGHT-2] = BlackKing; board[BOARD_HEIGHT-1][BOARD_RGHT-3] = BlackRook;
9386 board[BOARD_HEIGHT-1][BOARD_LEFT+2] = BlackKing; board[BOARD_HEIGHT-1][BOARD_LEFT+3] = BlackRook;
9388 /* End of code added by Tord */
9390 } else if (board[fromY][fromX] == king
9391 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
9392 && toY == fromY && toX > fromX+1) {
9393 board[fromY][fromX] = EmptySquare;
9394 board[toY][toX] = king;
9395 board[toY][toX-1] = board[fromY][BOARD_RGHT-1];
9396 board[fromY][BOARD_RGHT-1] = EmptySquare;
9397 } else if (board[fromY][fromX] == king
9398 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
9399 && toY == fromY && toX < fromX-1) {
9400 board[fromY][fromX] = EmptySquare;
9401 board[toY][toX] = king;
9402 board[toY][toX+1] = board[fromY][BOARD_LEFT];
9403 board[fromY][BOARD_LEFT] = EmptySquare;
9404 } else if ((board[fromY][fromX] == WhitePawn && gameInfo.variant != VariantXiangqi ||
9405 board[fromY][fromX] == WhiteLance && gameInfo.variant != VariantSuper && gameInfo.variant != VariantShogi)
9406 && toY >= BOARD_HEIGHT-promoRank && promoChar // defaulting to Q is done elsewhere
9408 /* white pawn promotion */
9409 board[toY][toX] = CharToPiece(ToUpper(promoChar));
9410 if(gameInfo.variant==VariantBughouse ||
9411 gameInfo.variant==VariantCrazyhouse) /* [HGM] use shadow piece */
9412 board[toY][toX] = (ChessSquare) (PROMOTED board[toY][toX]);
9413 board[fromY][fromX] = EmptySquare;
9414 } else if ((fromY >= BOARD_HEIGHT>>1)
9416 && gameInfo.variant != VariantXiangqi
9417 && gameInfo.variant != VariantBerolina
9418 && (board[fromY][fromX] == WhitePawn)
9419 && (board[toY][toX] == EmptySquare)) {
9420 board[fromY][fromX] = EmptySquare;
9421 board[toY][toX] = WhitePawn;
9422 captured = board[toY - 1][toX];
9423 board[toY - 1][toX] = EmptySquare;
9424 } else if ((fromY == BOARD_HEIGHT-4)
9426 && gameInfo.variant == VariantBerolina
9427 && (board[fromY][fromX] == WhitePawn)
9428 && (board[toY][toX] == EmptySquare)) {
9429 board[fromY][fromX] = EmptySquare;
9430 board[toY][toX] = WhitePawn;
9431 if(oldEP & EP_BEROLIN_A) {
9432 captured = board[fromY][fromX-1];
9433 board[fromY][fromX-1] = EmptySquare;
9434 }else{ captured = board[fromY][fromX+1];
9435 board[fromY][fromX+1] = EmptySquare;
9437 } else if (board[fromY][fromX] == king
9438 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
9439 && toY == fromY && toX > fromX+1) {
9440 board[fromY][fromX] = EmptySquare;
9441 board[toY][toX] = king;
9442 board[toY][toX-1] = board[fromY][BOARD_RGHT-1];
9443 board[fromY][BOARD_RGHT-1] = EmptySquare;
9444 } else if (board[fromY][fromX] == king
9445 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
9446 && toY == fromY && toX < fromX-1) {
9447 board[fromY][fromX] = EmptySquare;
9448 board[toY][toX] = king;
9449 board[toY][toX+1] = board[fromY][BOARD_LEFT];
9450 board[fromY][BOARD_LEFT] = EmptySquare;
9451 } else if (fromY == 7 && fromX == 3
9452 && board[fromY][fromX] == BlackKing
9453 && toY == 7 && toX == 5) {
9454 board[fromY][fromX] = EmptySquare;
9455 board[toY][toX] = BlackKing;
9456 board[fromY][7] = EmptySquare;
9457 board[toY][4] = BlackRook;
9458 } else if (fromY == 7 && fromX == 3
9459 && board[fromY][fromX] == BlackKing
9460 && toY == 7 && toX == 1) {
9461 board[fromY][fromX] = EmptySquare;
9462 board[toY][toX] = BlackKing;
9463 board[fromY][0] = EmptySquare;
9464 board[toY][2] = BlackRook;
9465 } else if ((board[fromY][fromX] == BlackPawn && gameInfo.variant != VariantXiangqi ||
9466 board[fromY][fromX] == BlackLance && gameInfo.variant != VariantSuper && gameInfo.variant != VariantShogi)
9467 && toY < promoRank && promoChar
9469 /* black pawn promotion */
9470 board[toY][toX] = CharToPiece(ToLower(promoChar));
9471 if(gameInfo.variant==VariantBughouse ||
9472 gameInfo.variant==VariantCrazyhouse) /* [HGM] use shadow piece */
9473 board[toY][toX] = (ChessSquare) (PROMOTED board[toY][toX]);
9474 board[fromY][fromX] = EmptySquare;
9475 } else if ((fromY < BOARD_HEIGHT>>1)
9477 && gameInfo.variant != VariantXiangqi
9478 && gameInfo.variant != VariantBerolina
9479 && (board[fromY][fromX] == BlackPawn)
9480 && (board[toY][toX] == EmptySquare)) {
9481 board[fromY][fromX] = EmptySquare;
9482 board[toY][toX] = BlackPawn;
9483 captured = board[toY + 1][toX];
9484 board[toY + 1][toX] = EmptySquare;
9485 } else if ((fromY == 3)
9487 && gameInfo.variant == VariantBerolina
9488 && (board[fromY][fromX] == BlackPawn)
9489 && (board[toY][toX] == EmptySquare)) {
9490 board[fromY][fromX] = EmptySquare;
9491 board[toY][toX] = BlackPawn;
9492 if(oldEP & EP_BEROLIN_A) {
9493 captured = board[fromY][fromX-1];
9494 board[fromY][fromX-1] = EmptySquare;
9495 }else{ captured = board[fromY][fromX+1];
9496 board[fromY][fromX+1] = EmptySquare;
9499 board[toY][toX] = board[fromY][fromX];
9500 board[fromY][fromX] = EmptySquare;
9504 if (gameInfo.holdingsWidth != 0) {
9506 /* !!A lot more code needs to be written to support holdings */
9507 /* [HGM] OK, so I have written it. Holdings are stored in the */
9508 /* penultimate board files, so they are automaticlly stored */
9509 /* in the game history. */
9510 if (fromY == DROP_RANK || gameInfo.variant == VariantSChess
9511 && promoChar && piece != WhitePawn && piece != BlackPawn) {
9512 /* Delete from holdings, by decreasing count */
9513 /* and erasing image if necessary */
9514 p = fromY == DROP_RANK ? (int) fromX : CharToPiece(piece > BlackPawn ? ToLower(promoChar) : ToUpper(promoChar));
9515 if(p < (int) BlackPawn) { /* white drop */
9516 p -= (int)WhitePawn;
9517 p = PieceToNumber((ChessSquare)p);
9518 if(p >= gameInfo.holdingsSize) p = 0;
9519 if(--board[p][BOARD_WIDTH-2] <= 0)
9520 board[p][BOARD_WIDTH-1] = EmptySquare;
9521 if((int)board[p][BOARD_WIDTH-2] < 0)
9522 board[p][BOARD_WIDTH-2] = 0;
9523 } else { /* black drop */
9524 p -= (int)BlackPawn;
9525 p = PieceToNumber((ChessSquare)p);
9526 if(p >= gameInfo.holdingsSize) p = 0;
9527 if(--board[BOARD_HEIGHT-1-p][1] <= 0)
9528 board[BOARD_HEIGHT-1-p][0] = EmptySquare;
9529 if((int)board[BOARD_HEIGHT-1-p][1] < 0)
9530 board[BOARD_HEIGHT-1-p][1] = 0;
9533 if (captured != EmptySquare && gameInfo.holdingsSize > 0
9534 && gameInfo.variant != VariantBughouse && gameInfo.variant != VariantSChess ) {
9535 /* [HGM] holdings: Add to holdings, if holdings exist */
9536 if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand) {
9537 // [HGM] superchess: suppress flipping color of captured pieces by reverse pre-flip
9538 captured = (int) captured >= (int) BlackPawn ? BLACK_TO_WHITE captured : WHITE_TO_BLACK captured;
9541 if (p >= (int) BlackPawn) {
9542 p -= (int)BlackPawn;
9543 if(gameInfo.variant == VariantShogi && DEMOTED p >= 0) {
9544 /* in Shogi restore piece to its original first */
9545 captured = (ChessSquare) (DEMOTED captured);
9548 p = PieceToNumber((ChessSquare)p);
9549 if(p >= gameInfo.holdingsSize) { p = 0; captured = BlackPawn; }
9550 board[p][BOARD_WIDTH-2]++;
9551 board[p][BOARD_WIDTH-1] = BLACK_TO_WHITE captured;
9553 p -= (int)WhitePawn;
9554 if(gameInfo.variant == VariantShogi && DEMOTED p >= 0) {
9555 captured = (ChessSquare) (DEMOTED captured);
9558 p = PieceToNumber((ChessSquare)p);
9559 if(p >= gameInfo.holdingsSize) { p = 0; captured = WhitePawn; }
9560 board[BOARD_HEIGHT-1-p][1]++;
9561 board[BOARD_HEIGHT-1-p][0] = WHITE_TO_BLACK captured;
9564 } else if (gameInfo.variant == VariantAtomic) {
9565 if (captured != EmptySquare) {
9567 for (y = toY-1; y <= toY+1; y++) {
9568 for (x = toX-1; x <= toX+1; x++) {
9569 if (y >= 0 && y < BOARD_HEIGHT && x >= BOARD_LEFT && x < BOARD_RGHT &&
9570 board[y][x] != WhitePawn && board[y][x] != BlackPawn) {
9571 board[y][x] = EmptySquare;
9575 board[toY][toX] = EmptySquare;
9578 if(gameInfo.variant == VariantSChess && promoChar != NULLCHAR && promoChar != '=' && piece != WhitePawn && piece != BlackPawn) {
9579 board[fromY][fromX] = CharToPiece(piece < BlackPawn ? ToUpper(promoChar) : ToLower(promoChar)); // S-Chess gating
9581 if(promoChar == '+') {
9582 /* [HGM] Shogi-style promotions, to piece implied by original (Might overwrite ordinary Pawn promotion) */
9583 board[toY][toX] = (ChessSquare) (PROMOTED piece);
9584 } else if(!appData.testLegality && promoChar != NULLCHAR && promoChar != '=') { // without legality testing, unconditionally believe promoChar
9585 ChessSquare newPiece = CharToPiece(piece < BlackPawn ? ToUpper(promoChar) : ToLower(promoChar));
9586 if((newPiece <= WhiteMan || newPiece >= BlackPawn && newPiece <= BlackMan) // unpromoted piece specified
9587 && pieceToChar[PROMOTED newPiece] == '~') newPiece = PROMOTED newPiece; // but promoted version available
9588 board[toY][toX] = newPiece;
9590 if((gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand)
9591 && promoChar != NULLCHAR && gameInfo.holdingsSize) {
9592 // [HGM] superchess: take promotion piece out of holdings
9593 int k = PieceToNumber(CharToPiece(ToUpper(promoChar)));
9594 if((int)piece < (int)BlackPawn) { // determine stm from piece color
9595 if(!--board[k][BOARD_WIDTH-2])
9596 board[k][BOARD_WIDTH-1] = EmptySquare;
9598 if(!--board[BOARD_HEIGHT-1-k][1])
9599 board[BOARD_HEIGHT-1-k][0] = EmptySquare;
9605 /* Updates forwardMostMove */
9607 MakeMove (int fromX, int fromY, int toX, int toY, int promoChar)
9609 // forwardMostMove++; // [HGM] bare: moved downstream
9611 (void) CoordsToAlgebraic(boards[forwardMostMove],
9612 PosFlags(forwardMostMove),
9613 fromY, fromX, toY, toX, promoChar,
9614 parseList[forwardMostMove]);
9616 if(serverMoves != NULL) { /* [HGM] write moves on file for broadcasting (should be separate routine, really) */
9617 int timeLeft; static int lastLoadFlag=0; int king, piece;
9618 piece = boards[forwardMostMove][fromY][fromX];
9619 king = piece < (int) BlackPawn ? WhiteKing : BlackKing;
9620 if(gameInfo.variant == VariantKnightmate)
9621 king += (int) WhiteUnicorn - (int) WhiteKing;
9622 if(forwardMostMove == 0) {
9623 if(gameMode == MachinePlaysBlack || gameMode == BeginningOfGame)
9624 fprintf(serverMoves, "%s;", UserName());
9625 else if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b')
9626 fprintf(serverMoves, "%s;", second.tidy);
9627 fprintf(serverMoves, "%s;", first.tidy);
9628 if(gameMode == MachinePlaysWhite)
9629 fprintf(serverMoves, "%s;", UserName());
9630 else if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w')
9631 fprintf(serverMoves, "%s;", second.tidy);
9632 } else fprintf(serverMoves, loadFlag|lastLoadFlag ? ":" : ";");
9633 lastLoadFlag = loadFlag;
9635 fprintf(serverMoves, "%c%c:%c%c", AAA+fromX, ONE+fromY, AAA+toX, ONE+toY);
9636 // print castling suffix
9637 if( toY == fromY && piece == king ) {
9639 fprintf(serverMoves, ":%c%c:%c%c", AAA+BOARD_RGHT-1, ONE+fromY, AAA+toX-1,ONE+toY);
9641 fprintf(serverMoves, ":%c%c:%c%c", AAA+BOARD_LEFT, ONE+fromY, AAA+toX+1,ONE+toY);
9644 if( (boards[forwardMostMove][fromY][fromX] == WhitePawn ||
9645 boards[forwardMostMove][fromY][fromX] == BlackPawn ) &&
9646 boards[forwardMostMove][toY][toX] == EmptySquare
9647 && fromX != toX && fromY != toY)
9648 fprintf(serverMoves, ":%c%c:%c%c", AAA+fromX, ONE+fromY, AAA+toX, ONE+fromY);
9650 if(promoChar != NULLCHAR) {
9651 if(fromY == 0 || fromY == BOARD_HEIGHT-1)
9652 fprintf(serverMoves, ":%c%c:%c%c", WhiteOnMove(forwardMostMove) ? 'w' : 'b',
9653 ToLower(promoChar), AAA+fromX, ONE+fromY); // Seirawan gating
9654 else fprintf(serverMoves, ":%c:%c%c", ToLower(promoChar), AAA+toX, ONE+toY);
9657 char buf[MOVE_LEN*2], *p; int len;
9658 fprintf(serverMoves, "/%d/%d",
9659 pvInfoList[forwardMostMove].depth, pvInfoList[forwardMostMove].score);
9660 if(forwardMostMove+1 & 1) timeLeft = whiteTimeRemaining/1000;
9661 else timeLeft = blackTimeRemaining/1000;
9662 fprintf(serverMoves, "/%d", timeLeft);
9663 strncpy(buf, parseList[forwardMostMove], MOVE_LEN*2);
9664 if(p = strchr(buf, '/')) *p = NULLCHAR; else
9665 if(p = strchr(buf, '=')) *p = NULLCHAR;
9666 len = strlen(buf); if(len > 1 && buf[len-2] != '-') buf[len-2] = NULLCHAR; // strip to-square
9667 fprintf(serverMoves, "/%s", buf);
9669 fflush(serverMoves);
9672 if (forwardMostMove+1 > framePtr) { // [HGM] vari: do not run into saved variations..
9673 GameEnds(GameUnfinished, _("Game too long; increase MAX_MOVES and recompile"), GE_XBOARD);
9676 UnLoadPV(); // [HGM] pv: if we are looking at a PV, abort this
9677 if (commentList[forwardMostMove+1] != NULL) {
9678 free(commentList[forwardMostMove+1]);
9679 commentList[forwardMostMove+1] = NULL;
9681 CopyBoard(boards[forwardMostMove+1], boards[forwardMostMove]);
9682 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[forwardMostMove+1]);
9683 // forwardMostMove++; // [HGM] bare: moved to after ApplyMove, to make sure clock interrupt finds complete board
9684 SwitchClocks(forwardMostMove+1); // [HGM] race: incrementing move nr inside
9685 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
9686 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
9687 adjustedClock = FALSE;
9688 gameInfo.result = GameUnfinished;
9689 if (gameInfo.resultDetails != NULL) {
9690 free(gameInfo.resultDetails);
9691 gameInfo.resultDetails = NULL;
9693 CoordsToComputerAlgebraic(fromY, fromX, toY, toX, promoChar,
9694 moveList[forwardMostMove - 1]);
9695 switch (MateTest(boards[forwardMostMove], PosFlags(forwardMostMove)) ) {
9701 if(gameInfo.variant != VariantShogi)
9702 strcat(parseList[forwardMostMove - 1], "+");
9706 strcat(parseList[forwardMostMove - 1], "#");
9712 /* Updates currentMove if not pausing */
9714 ShowMove (int fromX, int fromY, int toX, int toY)
9716 int instant = (gameMode == PlayFromGameFile) ?
9717 (matchMode || (appData.timeDelay == 0 && !pausing)) : pausing;
9718 if(appData.noGUI) return;
9719 if (!pausing || gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
9721 if (forwardMostMove == currentMove + 1) {
9722 AnimateMove(boards[forwardMostMove - 1],
9723 fromX, fromY, toX, toY);
9726 currentMove = forwardMostMove;
9729 if (instant) return;
9731 DisplayMove(currentMove - 1);
9732 DrawPosition(FALSE, boards[currentMove]);
9733 if (!pausing || gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
9734 if (appData.highlightLastMove) { // [HGM] moved to after DrawPosition, as with arrow it could redraw old board
9735 SetHighlights(fromX, fromY, toX, toY);
9738 DisplayBothClocks();
9739 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
9743 SendEgtPath (ChessProgramState *cps)
9744 { /* [HGM] EGT: match formats given in feature with those given by user, and send info for each match */
9745 char buf[MSG_SIZ], name[MSG_SIZ], *p;
9747 if((p = cps->egtFormats) == NULL || appData.egtFormats == NULL) return;
9750 char c, *q = name+1, *r, *s;
9752 name[0] = ','; // extract next format name from feature and copy with prefixed ','
9753 while(*p && *p != ',') *q++ = *p++;
9755 if( appData.defaultPathEGTB && appData.defaultPathEGTB[0] &&
9756 strcmp(name, ",nalimov:") == 0 ) {
9757 // take nalimov path from the menu-changeable option first, if it is defined
9758 snprintf(buf, MSG_SIZ, "egtpath nalimov %s\n", appData.defaultPathEGTB);
9759 SendToProgram(buf,cps); // send egtbpath command for nalimov
9761 if( (s = StrStr(appData.egtFormats, name+1)) == appData.egtFormats ||
9762 (s = StrStr(appData.egtFormats, name)) != NULL) {
9763 // format name occurs amongst user-supplied formats, at beginning or immediately after comma
9764 s = r = StrStr(s, ":") + 1; // beginning of path info
9765 while(*r && *r != ',') r++; // path info is everything upto next ';' or end of string
9766 c = *r; *r = 0; // temporarily null-terminate path info
9767 *--q = 0; // strip of trailig ':' from name
9768 snprintf(buf, MSG_SIZ, "egtpath %s %s\n", name+1, s);
9770 SendToProgram(buf,cps); // send egtbpath command for this format
9772 if(*p == ',') p++; // read away comma to position for next format name
9777 InitChessProgram (ChessProgramState *cps, int setup)
9778 /* setup needed to setup FRC opening position */
9780 char buf[MSG_SIZ], b[MSG_SIZ]; int overruled;
9781 if (appData.noChessProgram) return;
9782 hintRequested = FALSE;
9783 bookRequested = FALSE;
9785 ParseFeatures(appData.features[cps == &second], cps); // [HGM] allow user to overrule features
9786 /* [HGM] some new WB protocol commands to configure engine are sent now, if engine supports them */
9787 /* moved to before sending initstring in 4.3.15, so Polyglot can delay UCI 'isready' to recepton of 'new' */
9788 if(cps->memSize) { /* [HGM] memory */
9789 snprintf(buf, MSG_SIZ, "memory %d\n", appData.defaultHashSize + appData.defaultCacheSizeEGTB);
9790 SendToProgram(buf, cps);
9792 SendEgtPath(cps); /* [HGM] EGT */
9793 if(cps->maxCores) { /* [HGM] SMP: (protocol specified must be last settings command before new!) */
9794 snprintf(buf, MSG_SIZ, "cores %d\n", appData.smpCores);
9795 SendToProgram(buf, cps);
9798 SendToProgram(cps->initString, cps);
9799 if (gameInfo.variant != VariantNormal &&
9800 gameInfo.variant != VariantLoadable
9801 /* [HGM] also send variant if board size non-standard */
9802 || gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0
9804 char *v = VariantName(gameInfo.variant);
9805 if (cps->protocolVersion != 1 && StrStr(cps->variants, v) == NULL) {
9806 /* [HGM] in protocol 1 we have to assume all variants valid */
9807 snprintf(buf, MSG_SIZ, _("Variant %s not supported by %s"), v, cps->tidy);
9808 DisplayFatalError(buf, 0, 1);
9812 /* [HGM] make prefix for non-standard board size. Awkward testing... */
9813 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
9814 if( gameInfo.variant == VariantXiangqi )
9815 overruled = gameInfo.boardWidth != 9 || gameInfo.boardHeight != 10 || gameInfo.holdingsSize != 0;
9816 if( gameInfo.variant == VariantShogi )
9817 overruled = gameInfo.boardWidth != 9 || gameInfo.boardHeight != 9 || gameInfo.holdingsSize != 7;
9818 if( gameInfo.variant == VariantBughouse || gameInfo.variant == VariantCrazyhouse )
9819 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 5;
9820 if( gameInfo.variant == VariantCapablanca || gameInfo.variant == VariantCapaRandom ||
9821 gameInfo.variant == VariantGothic || gameInfo.variant == VariantFalcon || gameInfo.variant == VariantJanus )
9822 overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
9823 if( gameInfo.variant == VariantCourier )
9824 overruled = gameInfo.boardWidth != 12 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
9825 if( gameInfo.variant == VariantSuper )
9826 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 8;
9827 if( gameInfo.variant == VariantGreat )
9828 overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 8;
9829 if( gameInfo.variant == VariantSChess )
9830 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 7;
9831 if( gameInfo.variant == VariantGrand )
9832 overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 10 || gameInfo.holdingsSize != 7;
9835 snprintf(b, MSG_SIZ, "%dx%d+%d_%s", gameInfo.boardWidth, gameInfo.boardHeight,
9836 gameInfo.holdingsSize, VariantName(gameInfo.variant)); // cook up sized variant name
9837 /* [HGM] varsize: try first if this defiant size variant is specifically known */
9838 if(StrStr(cps->variants, b) == NULL) {
9839 // specific sized variant not known, check if general sizing allowed
9840 if (cps->protocolVersion != 1) { // for protocol 1 we cannot check and hope for the best
9841 if(StrStr(cps->variants, "boardsize") == NULL) {
9842 snprintf(buf, MSG_SIZ, "Board size %dx%d+%d not supported by %s",
9843 gameInfo.boardWidth, gameInfo.boardHeight, gameInfo.holdingsSize, cps->tidy);
9844 DisplayFatalError(buf, 0, 1);
9847 /* [HGM] here we really should compare with the maximum supported board size */
9850 } else snprintf(b, MSG_SIZ,"%s", VariantName(gameInfo.variant));
9851 snprintf(buf, MSG_SIZ, "variant %s\n", b);
9852 SendToProgram(buf, cps);
9854 currentlyInitializedVariant = gameInfo.variant;
9856 /* [HGM] send opening position in FRC to first engine */
9858 SendToProgram("force\n", cps);
9860 /* engine is now in force mode! Set flag to wake it up after first move. */
9861 setboardSpoiledMachineBlack = 1;
9865 snprintf(buf, sizeof(buf), "ics %s\n", appData.icsActive ? appData.icsHost : "-");
9866 SendToProgram(buf, cps);
9868 cps->maybeThinking = FALSE;
9869 cps->offeredDraw = 0;
9870 if (!appData.icsActive) {
9871 SendTimeControl(cps, movesPerSession, timeControl,
9872 timeIncrement, appData.searchDepth,
9875 if (appData.showThinking
9876 // [HGM] thinking: four options require thinking output to be sent
9877 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp()
9879 SendToProgram("post\n", cps);
9881 SendToProgram("hard\n", cps);
9882 if (!appData.ponderNextMove) {
9883 /* Warning: "easy" is a toggle in GNU Chess, so don't send
9884 it without being sure what state we are in first. "hard"
9885 is not a toggle, so that one is OK.
9887 SendToProgram("easy\n", cps);
9890 snprintf(buf, MSG_SIZ, "ping %d\n", ++cps->lastPing);
9891 SendToProgram(buf, cps);
9893 cps->initDone = TRUE;
9894 ClearEngineOutputPane(cps == &second);
9899 StartChessProgram (ChessProgramState *cps)
9904 if (appData.noChessProgram) return;
9905 cps->initDone = FALSE;
9907 if (strcmp(cps->host, "localhost") == 0) {
9908 err = StartChildProcess(cps->program, cps->dir, &cps->pr);
9909 } else if (*appData.remoteShell == NULLCHAR) {
9910 err = OpenRcmd(cps->host, appData.remoteUser, cps->program, &cps->pr);
9912 if (*appData.remoteUser == NULLCHAR) {
9913 snprintf(buf, sizeof(buf), "%s %s %s", appData.remoteShell, cps->host,
9916 snprintf(buf, sizeof(buf), "%s %s -l %s %s", appData.remoteShell,
9917 cps->host, appData.remoteUser, cps->program);
9919 err = StartChildProcess(buf, "", &cps->pr);
9923 snprintf(buf, MSG_SIZ, _("Startup failure on '%s'"), cps->program);
9924 DisplayError(buf, err); // [HGM] bit of a rough kludge: ignore failure, (which XBoard would do anyway), and let I/O discover it
9925 if(cps != &first) return;
9926 appData.noChessProgram = TRUE;
9929 // DisplayFatalError(buf, err, 1);
9930 // cps->pr = NoProc;
9935 cps->isr = AddInputSource(cps->pr, TRUE, ReceiveFromProgram, cps);
9936 if (cps->protocolVersion > 1) {
9937 snprintf(buf, MSG_SIZ, "xboard\nprotover %d\n", cps->protocolVersion);
9938 cps->nrOptions = 0; // [HGM] options: clear all engine-specific options
9939 cps->comboCnt = 0; // and values of combo boxes
9940 SendToProgram(buf, cps);
9942 SendToProgram("xboard\n", cps);
9947 TwoMachinesEventIfReady P((void))
9949 static int curMess = 0;
9950 if (first.lastPing != first.lastPong) {
9951 if(curMess != 1) DisplayMessage("", _("Waiting for first chess program")); curMess = 1;
9952 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10); // [HGM] fast: lowered from 1000
9955 if (second.lastPing != second.lastPong) {
9956 if(curMess != 2) DisplayMessage("", _("Waiting for second chess program")); curMess = 2;
9957 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10); // [HGM] fast: lowered from 1000
9960 DisplayMessage("", ""); curMess = 0;
9966 MakeName (char *template)
9970 static char buf[MSG_SIZ];
9974 clock = time((time_t *)NULL);
9975 tm = localtime(&clock);
9977 while(*p++ = *template++) if(p[-1] == '%') {
9978 switch(*template++) {
9979 case 0: *p = 0; return buf;
9980 case 'Y': i = tm->tm_year+1900; break;
9981 case 'y': i = tm->tm_year-100; break;
9982 case 'M': i = tm->tm_mon+1; break;
9983 case 'd': i = tm->tm_mday; break;
9984 case 'h': i = tm->tm_hour; break;
9985 case 'm': i = tm->tm_min; break;
9986 case 's': i = tm->tm_sec; break;
9989 snprintf(p-1, MSG_SIZ-10 - (p - buf), "%02d", i); p += strlen(p);
9995 CountPlayers (char *p)
9998 while(p = strchr(p, '\n')) p++, n++; // count participants
10003 WriteTourneyFile (char *results, FILE *f)
10004 { // write tournament parameters on tourneyFile; on success return the stream pointer for closing
10005 if(f == NULL) f = fopen(appData.tourneyFile, "w");
10006 if(f == NULL) DisplayError(_("Could not write on tourney file"), 0); else {
10007 // create a file with tournament description
10008 fprintf(f, "-participants {%s}\n", appData.participants);
10009 fprintf(f, "-seedBase %d\n", appData.seedBase);
10010 fprintf(f, "-tourneyType %d\n", appData.tourneyType);
10011 fprintf(f, "-tourneyCycles %d\n", appData.tourneyCycles);
10012 fprintf(f, "-defaultMatchGames %d\n", appData.defaultMatchGames);
10013 fprintf(f, "-syncAfterRound %s\n", appData.roundSync ? "true" : "false");
10014 fprintf(f, "-syncAfterCycle %s\n", appData.cycleSync ? "true" : "false");
10015 fprintf(f, "-saveGameFile \"%s\"\n", appData.saveGameFile);
10016 fprintf(f, "-loadGameFile \"%s\"\n", appData.loadGameFile);
10017 fprintf(f, "-loadGameIndex %d\n", appData.loadGameIndex);
10018 fprintf(f, "-loadPositionFile \"%s\"\n", appData.loadPositionFile);
10019 fprintf(f, "-loadPositionIndex %d\n", appData.loadPositionIndex);
10020 fprintf(f, "-rewindIndex %d\n", appData.rewindIndex);
10021 fprintf(f, "-discourageOwnBooks %s\n", appData.defNoBook ? "true" : "false");
10023 fprintf(f, "-searchTime \"%d:%02d\"\n", searchTime/60, searchTime%60);
10025 fprintf(f, "-mps %d\n", appData.movesPerSession);
10026 fprintf(f, "-tc %s\n", appData.timeControl);
10027 fprintf(f, "-inc %.2f\n", appData.timeIncrement);
10029 fprintf(f, "-results \"%s\"\n", results);
10034 char *command[MAXENGINES], *mnemonic[MAXENGINES];
10037 Substitute (char *participants, int expunge)
10039 int i, changed, changes=0, nPlayers=0;
10040 char *p, *q, *r, buf[MSG_SIZ];
10041 if(participants == NULL) return;
10042 if(appData.tourneyFile[0] == NULLCHAR) { free(participants); return; }
10043 r = p = participants; q = appData.participants;
10044 while(*p && *p == *q) {
10045 if(*p == '\n') r = p+1, nPlayers++;
10048 if(*p) { // difference
10049 while(*p && *p++ != '\n');
10050 while(*q && *q++ != '\n');
10051 changed = nPlayers;
10052 changes = 1 + (strcmp(p, q) != 0);
10054 if(changes == 1) { // a single engine mnemonic was changed
10055 q = r; while(*q) nPlayers += (*q++ == '\n');
10056 p = buf; while(*r && (*p = *r++) != '\n') p++;
10058 NamesToList(firstChessProgramNames, command, mnemonic, "all");
10059 for(i=1; mnemonic[i]; i++) if(!strcmp(buf, mnemonic[i])) break;
10060 if(mnemonic[i]) { // The substitute is valid
10062 if(appData.tourneyFile[0] && (f = fopen(appData.tourneyFile, "r+")) ) {
10063 flock(fileno(f), LOCK_EX);
10064 ParseArgsFromFile(f);
10065 fseek(f, 0, SEEK_SET);
10066 FREE(appData.participants); appData.participants = participants;
10067 if(expunge) { // erase results of replaced engine
10068 int len = strlen(appData.results), w, b, dummy;
10069 for(i=0; i<len; i++) {
10070 Pairing(i, nPlayers, &w, &b, &dummy);
10071 if((w == changed || b == changed) && appData.results[i] == '*') {
10072 DisplayError(_("You cannot replace an engine while it is engaged!\nTerminate its game first."), 0);
10077 for(i=0; i<len; i++) {
10078 Pairing(i, nPlayers, &w, &b, &dummy);
10079 if(w == changed || b == changed) appData.results[i] = ' '; // mark as not played
10082 WriteTourneyFile(appData.results, f);
10083 fclose(f); // release lock
10086 } else DisplayError(_("No engine with the name you gave is installed"), 0);
10088 if(changes == 0) DisplayError(_("First change an engine by editing the participants list\nof the Tournament Options dialog"), 0);
10089 if(changes > 1) DisplayError(_("You can only change one engine at the time"), 0);
10090 free(participants);
10095 CheckPlayers (char *participants)
10098 char buf[MSG_SIZ], *p;
10099 NamesToList(firstChessProgramNames, command, mnemonic, "all");
10100 while(p = strchr(participants, '\n')) {
10102 for(i=1; mnemonic[i]; i++) if(!strcmp(participants, mnemonic[i])) break;
10104 snprintf(buf, MSG_SIZ, _("No engine %s is installed"), participants);
10106 DisplayError(buf, 0);
10110 participants = p + 1;
10116 CreateTourney (char *name)
10119 if(matchMode && strcmp(name, appData.tourneyFile)) {
10120 ASSIGN(name, appData.tourneyFile); //do not allow change of tourneyfile while playing
10122 if(name[0] == NULLCHAR) {
10123 if(appData.participants[0])
10124 DisplayError(_("You must supply a tournament file,\nfor storing the tourney progress"), 0);
10127 f = fopen(name, "r");
10128 if(f) { // file exists
10129 ASSIGN(appData.tourneyFile, name);
10130 ParseArgsFromFile(f); // parse it
10132 if(!appData.participants[0]) return 0; // ignore tourney file if non-existing & no participants
10133 if(CountPlayers(appData.participants) < (appData.tourneyType>0 ? appData.tourneyType+1 : 2)) {
10134 DisplayError(_("Not enough participants"), 0);
10137 if(CheckPlayers(appData.participants)) return 0;
10138 ASSIGN(appData.tourneyFile, name);
10139 if(appData.tourneyType < 0) appData.defaultMatchGames = 1; // Swiss forces games/pairing = 1
10140 if((f = WriteTourneyFile("", NULL)) == NULL) return 0;
10143 appData.noChessProgram = FALSE;
10144 appData.clockMode = TRUE;
10150 NamesToList (char *names, char **engineList, char **engineMnemonic, char *group)
10152 char buf[MSG_SIZ], *p, *q;
10153 int i=1, header, skip, all = !strcmp(group, "all"), depth = 0;
10154 insert = names; // afterwards, this global will point just after last retrieved engine line or group end in the 'names'
10155 skip = !all && group[0]; // if group requested, we start in skip mode
10156 for(;*names && depth >= 0 && i < MAXENGINES-1; names = p) {
10157 p = names; q = buf; header = 0;
10158 while(*p && *p != '\n') *q++ = *p++;
10160 if(*p == '\n') p++;
10161 if(buf[0] == '#') {
10162 if(strstr(buf, "# end") == buf) { if(!--depth) insert = p; continue; } // leave group, and suppress printing label
10163 depth++; // we must be entering a new group
10164 if(all) continue; // suppress printing group headers when complete list requested
10166 if(skip && !strcmp(group, buf)) { depth = 0; skip = FALSE; } // start when we reach requested group
10168 if(depth != header && !all || skip) continue; // skip contents of group (but print first-level header)
10169 if(engineList[i]) free(engineList[i]);
10170 engineList[i] = strdup(buf);
10171 if(buf[0] != '#') insert = p, TidyProgramName(engineList[i], "localhost", buf); // group headers not tidied
10172 if(engineMnemonic[i]) free(engineMnemonic[i]);
10173 if((q = strstr(engineList[i]+2, "variant")) && q[-2]== ' ' && (q[-1]=='/' || q[-1]=='-') && (q[7]==' ' || q[7]=='=')) {
10175 sscanf(q + 8, "%s", buf + strlen(buf));
10178 engineMnemonic[i] = strdup(buf);
10181 engineList[i] = engineMnemonic[i] = NULL;
10185 // following implemented as macro to avoid type limitations
10186 #define SWAP(item, temp) temp = appData.item[0]; appData.item[0] = appData.item[n]; appData.item[n] = temp;
10189 SwapEngines (int n)
10190 { // swap settings for first engine and other engine (so far only some selected options)
10195 SWAP(chessProgram, p)
10197 SWAP(hasOwnBookUCI, h)
10198 SWAP(protocolVersion, h)
10200 SWAP(scoreIsAbsolute, h)
10205 SWAP(engOptions, p)
10206 SWAP(engInitString, p)
10207 SWAP(computerString, p)
10209 SWAP(fenOverride, p)
10211 SWAP(accumulateTC, h)
10216 GetEngineLine (char *s, int n)
10220 extern char *icsNames;
10221 if(!s || !*s) return 0;
10222 NamesToList(n >= 10 ? icsNames : firstChessProgramNames, command, mnemonic, "all");
10223 for(i=1; mnemonic[i]; i++) if(!strcmp(s, mnemonic[i])) break;
10224 if(!mnemonic[i]) return 0;
10225 if(n == 11) return 1; // just testing if there was a match
10226 snprintf(buf, MSG_SIZ, "-%s %s", n == 10 ? "icshost" : "fcp", command[i]);
10227 if(n == 1) SwapEngines(n);
10228 ParseArgsFromString(buf);
10229 if(n == 1) SwapEngines(n);
10230 if(n == 0 && *appData.secondChessProgram == NULLCHAR) {
10231 SwapEngines(1); // set second same as first if not yet set (to suppress WB startup dialog)
10232 ParseArgsFromString(buf);
10238 SetPlayer (int player, char *p)
10239 { // [HGM] find the engine line of the partcipant given by number, and parse its options.
10241 char buf[MSG_SIZ], *engineName;
10242 for(i=0; i<player; i++) p = strchr(p, '\n') + 1;
10243 engineName = strdup(p); if(p = strchr(engineName, '\n')) *p = NULLCHAR;
10244 for(i=1; command[i]; i++) if(!strcmp(mnemonic[i], engineName)) break;
10246 snprintf(buf, MSG_SIZ, "-fcp %s", command[i]);
10247 ParseArgsFromString(resetOptions); appData.fenOverride[0] = NULL; appData.pvSAN[0] = FALSE;
10248 appData.firstHasOwnBookUCI = !appData.defNoBook; appData.protocolVersion[0] = PROTOVER;
10249 ParseArgsFromString(buf);
10255 char *recentEngines;
10258 RecentEngineEvent (int nr)
10261 // SwapEngines(1); // bump first to second
10262 // ReplaceEngine(&second, 1); // and load it there
10263 NamesToList(firstChessProgramNames, command, mnemonic, "all"); // get mnemonics of installed engines
10264 n = SetPlayer(nr, recentEngines); // select new (using original menu order!)
10265 if(mnemonic[n]) { // if somehow the engine with the selected nickname is no longer found in the list, we skip
10266 ReplaceEngine(&first, 0);
10267 FloatToFront(&appData.recentEngineList, command[n]);
10272 Pairing (int nr, int nPlayers, int *whitePlayer, int *blackPlayer, int *syncInterval)
10273 { // determine players from game number
10274 int curCycle, curRound, curPairing, gamesPerCycle, gamesPerRound, roundsPerCycle=1, pairingsPerRound=1;
10276 if(appData.tourneyType == 0) {
10277 roundsPerCycle = (nPlayers - 1) | 1;
10278 pairingsPerRound = nPlayers / 2;
10279 } else if(appData.tourneyType > 0) {
10280 roundsPerCycle = nPlayers - appData.tourneyType;
10281 pairingsPerRound = appData.tourneyType;
10283 gamesPerRound = pairingsPerRound * appData.defaultMatchGames;
10284 gamesPerCycle = gamesPerRound * roundsPerCycle;
10285 appData.matchGames = gamesPerCycle * appData.tourneyCycles - 1; // fake like all games are one big match
10286 curCycle = nr / gamesPerCycle; nr %= gamesPerCycle;
10287 curRound = nr / gamesPerRound; nr %= gamesPerRound;
10288 curPairing = nr / appData.defaultMatchGames; nr %= appData.defaultMatchGames;
10289 matchGame = nr + curCycle * appData.defaultMatchGames + 1; // fake game nr that loads correct game or position from file
10290 roundNr = (curCycle * roundsPerCycle + curRound) * appData.defaultMatchGames + nr + 1;
10292 if(appData.cycleSync) *syncInterval = gamesPerCycle;
10293 if(appData.roundSync) *syncInterval = gamesPerRound;
10295 if(appData.debugMode) fprintf(debugFP, "cycle=%d, round=%d, pairing=%d curGame=%d\n", curCycle, curRound, curPairing, matchGame);
10297 if(appData.tourneyType == 0) {
10298 if(curPairing == (nPlayers-1)/2 ) {
10299 *whitePlayer = curRound;
10300 *blackPlayer = nPlayers - 1; // this is the 'bye' when nPlayer is odd
10302 *whitePlayer = curRound - (nPlayers-1)/2 + curPairing;
10303 if(*whitePlayer < 0) *whitePlayer += nPlayers-1+(nPlayers&1);
10304 *blackPlayer = curRound + (nPlayers-1)/2 - curPairing;
10305 if(*blackPlayer >= nPlayers-1+(nPlayers&1)) *blackPlayer -= nPlayers-1+(nPlayers&1);
10307 } else if(appData.tourneyType > 1) {
10308 *blackPlayer = curPairing; // in multi-gauntlet, assign gauntlet engines to second, so first an be kept loaded during round
10309 *whitePlayer = curRound + appData.tourneyType;
10310 } else if(appData.tourneyType > 0) {
10311 *whitePlayer = curPairing;
10312 *blackPlayer = curRound + appData.tourneyType;
10315 // take care of white/black alternation per round.
10316 // For cycles and games this is already taken care of by default, derived from matchGame!
10317 return curRound & 1;
10321 NextTourneyGame (int nr, int *swapColors)
10322 { // !!!major kludge!!! fiddle appData settings to get everything in order for next tourney game
10324 int whitePlayer, blackPlayer, firstBusy=1000000000, syncInterval = 0, nPlayers;
10326 if(appData.tourneyFile[0] == NULLCHAR) return 1; // no tourney, always allow next game
10327 tf = fopen(appData.tourneyFile, "r");
10328 if(tf == NULL) { DisplayFatalError(_("Bad tournament file"), 0, 1); return 0; }
10329 ParseArgsFromFile(tf); fclose(tf);
10330 InitTimeControls(); // TC might be altered from tourney file
10332 nPlayers = CountPlayers(appData.participants); // count participants
10333 if(appData.tourneyType < 0) syncInterval = nPlayers/2; else
10334 *swapColors = Pairing(nr<0 ? 0 : nr, nPlayers, &whitePlayer, &blackPlayer, &syncInterval);
10337 p = q = appData.results;
10338 while(*q) if(*q++ == '*' || q[-1] == ' ') { firstBusy = q - p - 1; break; }
10339 if(firstBusy/syncInterval < (nextGame/syncInterval)) {
10340 DisplayMessage(_("Waiting for other game(s)"),"");
10341 waitingForGame = TRUE;
10342 ScheduleDelayedEvent(NextMatchGame, 1000); // wait for all games of previous round to finish
10345 waitingForGame = FALSE;
10348 if(appData.tourneyType < 0) {
10349 if(nr>=0 && !pairingReceived) {
10351 if(pairing.pr == NoProc) {
10352 if(!appData.pairingEngine[0]) {
10353 DisplayFatalError(_("No pairing engine specified"), 0, 1);
10356 StartChessProgram(&pairing); // starts the pairing engine
10358 snprintf(buf, 1<<16, "results %d %s\n", nPlayers, appData.results);
10359 SendToProgram(buf, &pairing);
10360 snprintf(buf, 1<<16, "pairing %d\n", nr+1);
10361 SendToProgram(buf, &pairing);
10362 return 0; // wait for pairing engine to answer (which causes NextTourneyGame to be called again...
10364 pairingReceived = 0; // ... so we continue here
10366 appData.matchGames = appData.tourneyCycles * syncInterval - 1;
10367 whitePlayer = savedWhitePlayer-1; blackPlayer = savedBlackPlayer-1;
10368 matchGame = 1; roundNr = nr / syncInterval + 1;
10371 if(first.pr != NoProc && second.pr != NoProc || nr<0) return 1; // engines already loaded
10373 // redefine engines, engine dir, etc.
10374 NamesToList(firstChessProgramNames, command, mnemonic, "all"); // get mnemonics of installed engines
10375 if(first.pr == NoProc) {
10376 SetPlayer(whitePlayer, appData.participants); // find white player amongst it, and parse its engine line
10377 InitEngine(&first, 0); // initialize ChessProgramStates based on new settings.
10379 if(second.pr == NoProc) {
10381 SetPlayer(blackPlayer, appData.participants); // find black player amongst it, and parse its engine line
10382 SwapEngines(1); // and make that valid for second engine by swapping
10383 InitEngine(&second, 1);
10385 CommonEngineInit(); // after this TwoMachinesEvent will create correct engine processes
10386 UpdateLogos(FALSE); // leave display to ModeHiglight()
10392 { // performs game initialization that does not invoke engines, and then tries to start the game
10393 int res, firstWhite, swapColors = 0;
10394 if(!NextTourneyGame(nextGame, &swapColors)) return; // this sets matchGame, -fcp / -scp and other options for next game, if needed
10395 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
10397 snprintf(buf, MSG_SIZ, appData.nameOfDebugFile, nextGame+1); // expand name of debug file with %d in it
10398 if(strcmp(buf, currentDebugFile)) { // name has changed
10399 FILE *f = fopen(buf, "w");
10400 if(f) { // if opening the new file failed, just keep using the old one
10401 ASSIGN(currentDebugFile, buf);
10405 if(appData.serverFileName) {
10406 if(serverFP) fclose(serverFP);
10407 serverFP = fopen(appData.serverFileName, "w");
10408 if(serverFP && first.pr != NoProc) fprintf(serverFP, "StartChildProcess (dir=\".\") .\\%s\n", first.tidy);
10409 if(serverFP && second.pr != NoProc) fprintf(serverFP, "StartChildProcess (dir=\".\") .\\%s\n", second.tidy);
10413 firstWhite = appData.firstPlaysBlack ^ (matchGame & 1 | appData.sameColorGames > 1); // non-incremental default
10414 firstWhite ^= swapColors; // reverses if NextTourneyGame says we are in an odd round
10415 first.twoMachinesColor = firstWhite ? "white\n" : "black\n"; // perform actual color assignement
10416 second.twoMachinesColor = firstWhite ? "black\n" : "white\n";
10417 appData.noChessProgram = (first.pr == NoProc); // kludge to prevent Reset from starting up chess program
10418 if(appData.loadGameIndex == -2) srandom(appData.seedBase + 68163*(nextGame & ~1)); // deterministic seed to force same opening
10419 Reset(FALSE, first.pr != NoProc);
10420 res = LoadGameOrPosition(matchGame); // setup game
10421 appData.noChessProgram = FALSE; // LoadGameOrPosition might call Reset too!
10422 if(!res) return; // abort when bad game/pos file
10423 TwoMachinesEvent();
10427 UserAdjudicationEvent (int result)
10429 ChessMove gameResult = GameIsDrawn;
10432 gameResult = WhiteWins;
10434 else if( result < 0 ) {
10435 gameResult = BlackWins;
10438 if( gameMode == TwoMachinesPlay ) {
10439 GameEnds( gameResult, "User adjudication", GE_XBOARD );
10444 // [HGM] save: calculate checksum of game to make games easily identifiable
10446 StringCheckSum (char *s)
10449 if(s==NULL) return 0;
10450 while(*s) i = i*259 + *s++;
10458 for(i=backwardMostMove; i<forwardMostMove; i++) {
10459 sum += pvInfoList[i].depth;
10460 sum += StringCheckSum(parseList[i]);
10461 sum += StringCheckSum(commentList[i]);
10464 if(i>1 && sum==0) sum++; // make sure never zero for non-empty game
10465 return sum + StringCheckSum(commentList[i]);
10466 } // end of save patch
10469 GameEnds (ChessMove result, char *resultDetails, int whosays)
10471 GameMode nextGameMode;
10473 char buf[MSG_SIZ], popupRequested = 0, *ranking = NULL;
10475 if(endingGame) return; /* [HGM] crash: forbid recursion */
10477 if(twoBoards) { // [HGM] dual: switch back to one board
10478 twoBoards = partnerUp = 0; InitDrawingSizes(-2, 0);
10479 DrawPosition(TRUE, partnerBoard); // observed game becomes foreground
10481 if (appData.debugMode) {
10482 fprintf(debugFP, "GameEnds(%d, %s, %d)\n",
10483 result, resultDetails ? resultDetails : "(null)", whosays);
10486 fromX = fromY = -1; // [HGM] abort any move the user is entering.
10488 if (appData.icsActive && (whosays == GE_ENGINE || whosays >= GE_ENGINE1)) {
10489 /* If we are playing on ICS, the server decides when the
10490 game is over, but the engine can offer to draw, claim
10494 if (appData.zippyPlay && first.initDone) {
10495 if (result == GameIsDrawn) {
10496 /* In case draw still needs to be claimed */
10497 SendToICS(ics_prefix);
10498 SendToICS("draw\n");
10499 } else if (StrCaseStr(resultDetails, "resign")) {
10500 SendToICS(ics_prefix);
10501 SendToICS("resign\n");
10505 endingGame = 0; /* [HGM] crash */
10509 /* If we're loading the game from a file, stop */
10510 if (whosays == GE_FILE) {
10511 (void) StopLoadGameTimer();
10515 /* Cancel draw offers */
10516 first.offeredDraw = second.offeredDraw = 0;
10518 /* If this is an ICS game, only ICS can really say it's done;
10519 if not, anyone can. */
10520 isIcsGame = (gameMode == IcsPlayingWhite ||
10521 gameMode == IcsPlayingBlack ||
10522 gameMode == IcsObserving ||
10523 gameMode == IcsExamining);
10525 if (!isIcsGame || whosays == GE_ICS) {
10526 /* OK -- not an ICS game, or ICS said it was done */
10528 if (!isIcsGame && !appData.noChessProgram)
10529 SetUserThinkingEnables();
10531 /* [HGM] if a machine claims the game end we verify this claim */
10532 if(gameMode == TwoMachinesPlay && appData.testClaims) {
10533 if(appData.testLegality && whosays >= GE_ENGINE1 ) {
10535 ChessMove trueResult = (ChessMove) -1;
10537 claimer = whosays == GE_ENGINE1 ? /* color of claimer */
10538 first.twoMachinesColor[0] :
10539 second.twoMachinesColor[0] ;
10541 // [HGM] losers: because the logic is becoming a bit hairy, determine true result first
10542 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_CHECKMATE) {
10543 /* [HGM] verify: engine mate claims accepted if they were flagged */
10544 trueResult = WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins;
10546 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_WINS) { // added code for games where being mated is a win
10547 /* [HGM] verify: engine mate claims accepted if they were flagged */
10548 trueResult = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
10550 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_STALEMATE) { // only used to indicate draws now
10551 trueResult = GameIsDrawn; // default; in variants where stalemate loses, Status is CHECKMATE
10554 // now verify win claims, but not in drop games, as we don't understand those yet
10555 if( (gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper
10556 || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand) &&
10557 (result == WhiteWins && claimer == 'w' ||
10558 result == BlackWins && claimer == 'b' ) ) { // case to verify: engine claims own win
10559 if (appData.debugMode) {
10560 fprintf(debugFP, "result=%d sp=%d move=%d\n",
10561 result, (signed char)boards[forwardMostMove][EP_STATUS], forwardMostMove);
10563 if(result != trueResult) {
10564 snprintf(buf, MSG_SIZ, "False win claim: '%s'", resultDetails);
10565 result = claimer == 'w' ? BlackWins : WhiteWins;
10566 resultDetails = buf;
10569 if( result == GameIsDrawn && (signed char)boards[forwardMostMove][EP_STATUS] > EP_DRAWS
10570 && (forwardMostMove <= backwardMostMove ||
10571 (signed char)boards[forwardMostMove-1][EP_STATUS] > EP_DRAWS ||
10572 (claimer=='b')==(forwardMostMove&1))
10574 /* [HGM] verify: draws that were not flagged are false claims */
10575 snprintf(buf, MSG_SIZ, "False draw claim: '%s'", resultDetails);
10576 result = claimer == 'w' ? BlackWins : WhiteWins;
10577 resultDetails = buf;
10579 /* (Claiming a loss is accepted no questions asked!) */
10581 /* [HGM] bare: don't allow bare King to win */
10582 if((gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper
10583 || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand)
10584 && gameInfo.variant != VariantLosers && gameInfo.variant != VariantGiveaway
10585 && gameInfo.variant != VariantSuicide // [HGM] losers: except in losers, of course...
10586 && result != GameIsDrawn)
10587 { int i, j, k=0, color = (result==WhiteWins ? (int)WhitePawn : (int)BlackPawn);
10588 for(j=BOARD_LEFT; j<BOARD_RGHT; j++) for(i=0; i<BOARD_HEIGHT; i++) {
10589 int p = (signed char)boards[forwardMostMove][i][j] - color;
10590 if(p >= 0 && p <= (int)WhiteKing) k++;
10592 if (appData.debugMode) {
10593 fprintf(debugFP, "GE(%d, %s, %d) bare king k=%d color=%d\n",
10594 result, resultDetails ? resultDetails : "(null)", whosays, k, color);
10597 result = GameIsDrawn;
10598 snprintf(buf, MSG_SIZ, "%s but bare king", resultDetails);
10599 resultDetails = buf;
10605 if(serverMoves != NULL && !loadFlag) { char c = '=';
10606 if(result==WhiteWins) c = '+';
10607 if(result==BlackWins) c = '-';
10608 if(resultDetails != NULL)
10609 fprintf(serverMoves, ";%c;%s\n", c, resultDetails), fflush(serverMoves);
10611 if (resultDetails != NULL) {
10612 gameInfo.result = result;
10613 gameInfo.resultDetails = StrSave(resultDetails);
10615 /* display last move only if game was not loaded from file */
10616 if ((whosays != GE_FILE) && (currentMove == forwardMostMove))
10617 DisplayMove(currentMove - 1);
10619 if (forwardMostMove != 0) {
10620 if (gameMode != PlayFromGameFile && gameMode != EditGame
10621 && lastSavedGame != GameCheckSum() // [HGM] save: suppress duplicates
10623 if (*appData.saveGameFile != NULLCHAR) {
10624 SaveGameToFile(appData.saveGameFile, TRUE);
10625 } else if (appData.autoSaveGames) {
10628 if (*appData.savePositionFile != NULLCHAR) {
10629 SavePositionToFile(appData.savePositionFile);
10634 /* Tell program how game ended in case it is learning */
10635 /* [HGM] Moved this to after saving the PGN, just in case */
10636 /* engine died and we got here through time loss. In that */
10637 /* case we will get a fatal error writing the pipe, which */
10638 /* would otherwise lose us the PGN. */
10639 /* [HGM] crash: not needed anymore, but doesn't hurt; */
10640 /* output during GameEnds should never be fatal anymore */
10641 if (gameMode == MachinePlaysWhite ||
10642 gameMode == MachinePlaysBlack ||
10643 gameMode == TwoMachinesPlay ||
10644 gameMode == IcsPlayingWhite ||
10645 gameMode == IcsPlayingBlack ||
10646 gameMode == BeginningOfGame) {
10648 snprintf(buf, MSG_SIZ, "result %s {%s}\n", PGNResult(result),
10650 if (first.pr != NoProc) {
10651 SendToProgram(buf, &first);
10653 if (second.pr != NoProc &&
10654 gameMode == TwoMachinesPlay) {
10655 SendToProgram(buf, &second);
10660 if (appData.icsActive) {
10661 if (appData.quietPlay &&
10662 (gameMode == IcsPlayingWhite ||
10663 gameMode == IcsPlayingBlack)) {
10664 SendToICS(ics_prefix);
10665 SendToICS("set shout 1\n");
10667 nextGameMode = IcsIdle;
10668 ics_user_moved = FALSE;
10669 /* clean up premove. It's ugly when the game has ended and the
10670 * premove highlights are still on the board.
10673 gotPremove = FALSE;
10674 ClearPremoveHighlights();
10675 DrawPosition(FALSE, boards[currentMove]);
10677 if (whosays == GE_ICS) {
10680 if (gameMode == IcsPlayingWhite)
10682 else if(gameMode == IcsPlayingBlack)
10683 PlayIcsLossSound();
10686 if (gameMode == IcsPlayingBlack)
10688 else if(gameMode == IcsPlayingWhite)
10689 PlayIcsLossSound();
10692 PlayIcsDrawSound();
10695 PlayIcsUnfinishedSound();
10698 } else if (gameMode == EditGame ||
10699 gameMode == PlayFromGameFile ||
10700 gameMode == AnalyzeMode ||
10701 gameMode == AnalyzeFile) {
10702 nextGameMode = gameMode;
10704 nextGameMode = EndOfGame;
10709 nextGameMode = gameMode;
10712 if (appData.noChessProgram) {
10713 gameMode = nextGameMode;
10715 endingGame = 0; /* [HGM] crash */
10720 /* Put first chess program into idle state */
10721 if (first.pr != NoProc &&
10722 (gameMode == MachinePlaysWhite ||
10723 gameMode == MachinePlaysBlack ||
10724 gameMode == TwoMachinesPlay ||
10725 gameMode == IcsPlayingWhite ||
10726 gameMode == IcsPlayingBlack ||
10727 gameMode == BeginningOfGame)) {
10728 SendToProgram("force\n", &first);
10729 if (first.usePing) {
10731 snprintf(buf, MSG_SIZ, "ping %d\n", ++first.lastPing);
10732 SendToProgram(buf, &first);
10735 } else if (result != GameUnfinished || nextGameMode == IcsIdle) {
10736 /* Kill off first chess program */
10737 if (first.isr != NULL)
10738 RemoveInputSource(first.isr);
10741 if (first.pr != NoProc) {
10743 DoSleep( appData.delayBeforeQuit );
10744 SendToProgram("quit\n", &first);
10745 DoSleep( appData.delayAfterQuit );
10746 DestroyChildProcess(first.pr, first.useSigterm);
10750 if (second.reuse) {
10751 /* Put second chess program into idle state */
10752 if (second.pr != NoProc &&
10753 gameMode == TwoMachinesPlay) {
10754 SendToProgram("force\n", &second);
10755 if (second.usePing) {
10757 snprintf(buf, MSG_SIZ, "ping %d\n", ++second.lastPing);
10758 SendToProgram(buf, &second);
10761 } else if (result != GameUnfinished || nextGameMode == IcsIdle) {
10762 /* Kill off second chess program */
10763 if (second.isr != NULL)
10764 RemoveInputSource(second.isr);
10767 if (second.pr != NoProc) {
10768 DoSleep( appData.delayBeforeQuit );
10769 SendToProgram("quit\n", &second);
10770 DoSleep( appData.delayAfterQuit );
10771 DestroyChildProcess(second.pr, second.useSigterm);
10773 second.pr = NoProc;
10776 if (matchMode && (gameMode == TwoMachinesPlay || waitingForGame && exiting)) {
10777 char resChar = '=';
10781 if (first.twoMachinesColor[0] == 'w') {
10784 second.matchWins++;
10789 if (first.twoMachinesColor[0] == 'b') {
10792 second.matchWins++;
10795 case GameUnfinished:
10801 if(waitingForGame) resChar = ' '; // quit while waiting for round sync: unreserve already reserved game
10802 if(appData.tourneyFile[0]){ // [HGM] we are in a tourney; update tourney file with game result
10803 if(appData.afterGame && appData.afterGame[0]) RunCommand(appData.afterGame);
10804 ReserveGame(nextGame, resChar); // sets nextGame
10805 if(nextGame > appData.matchGames) appData.tourneyFile[0] = 0, ranking = TourneyStandings(3); // tourney is done
10806 else ranking = strdup("busy"); //suppress popup when aborted but not finished
10807 } else roundNr = nextGame = matchGame + 1; // normal match, just increment; round equals matchGame
10809 if (nextGame <= appData.matchGames && !abortMatch) {
10810 gameMode = nextGameMode;
10811 matchGame = nextGame; // this will be overruled in tourney mode!
10812 GetTimeMark(&pauseStart); // [HGM] matchpause: stipulate a pause
10813 ScheduleDelayedEvent(NextMatchGame, 10); // but start game immediately (as it will wait out the pause itself)
10814 endingGame = 0; /* [HGM] crash */
10817 gameMode = nextGameMode;
10818 snprintf(buf, MSG_SIZ, _("Match %s vs. %s: final score %d-%d-%d"),
10819 first.tidy, second.tidy,
10820 first.matchWins, second.matchWins,
10821 appData.matchGames - (first.matchWins + second.matchWins));
10822 if(!appData.tourneyFile[0]) matchGame++, DisplayTwoMachinesTitle(); // [HGM] update result in window title
10823 if(ranking && strcmp(ranking, "busy") && appData.afterTourney && appData.afterTourney[0]) RunCommand(appData.afterTourney);
10824 popupRequested++; // [HGM] crash: postpone to after resetting endingGame
10825 if (appData.firstPlaysBlack) { // [HGM] match: back to original for next match
10826 first.twoMachinesColor = "black\n";
10827 second.twoMachinesColor = "white\n";
10829 first.twoMachinesColor = "white\n";
10830 second.twoMachinesColor = "black\n";
10834 if ((gameMode == AnalyzeMode || gameMode == AnalyzeFile) &&
10835 !(nextGameMode == AnalyzeMode || nextGameMode == AnalyzeFile))
10837 gameMode = nextGameMode;
10839 endingGame = 0; /* [HGM] crash */
10840 if(popupRequested) { // [HGM] crash: this calls GameEnds recursively through ExitEvent! Make it a harmless tail recursion.
10841 if(matchMode == TRUE) { // match through command line: exit with or without popup
10843 ToNrEvent(forwardMostMove);
10844 if(strcmp(ranking, "busy")) DisplayFatalError(ranking, 0, 0);
10846 } else DisplayFatalError(buf, 0, 0);
10847 } else { // match through menu; just stop, with or without popup
10848 matchMode = FALSE; appData.matchGames = matchGame = roundNr = 0;
10851 if(strcmp(ranking, "busy")) DisplayNote(ranking);
10852 } else DisplayNote(buf);
10854 if(ranking) free(ranking);
10858 /* Assumes program was just initialized (initString sent).
10859 Leaves program in force mode. */
10861 FeedMovesToProgram (ChessProgramState *cps, int upto)
10865 if (appData.debugMode)
10866 fprintf(debugFP, "Feeding %smoves %d through %d to %s chess program\n",
10867 startedFromSetupPosition ? "position and " : "",
10868 backwardMostMove, upto, cps->which);
10869 if(currentlyInitializedVariant != gameInfo.variant) {
10871 // [HGM] variantswitch: make engine aware of new variant
10872 if(cps->protocolVersion > 1 && StrStr(cps->variants, VariantName(gameInfo.variant)) == NULL)
10873 return; // [HGM] refrain from feeding moves altogether if variant is unsupported!
10874 snprintf(buf, MSG_SIZ, "variant %s\n", VariantName(gameInfo.variant));
10875 SendToProgram(buf, cps);
10876 currentlyInitializedVariant = gameInfo.variant;
10878 SendToProgram("force\n", cps);
10879 if (startedFromSetupPosition) {
10880 SendBoard(cps, backwardMostMove);
10881 if (appData.debugMode) {
10882 fprintf(debugFP, "feedMoves\n");
10885 for (i = backwardMostMove; i < upto; i++) {
10886 SendMoveToProgram(i, cps);
10892 ResurrectChessProgram ()
10894 /* The chess program may have exited.
10895 If so, restart it and feed it all the moves made so far. */
10896 static int doInit = 0;
10898 if (appData.noChessProgram) return 1;
10900 if(matchMode && appData.tourneyFile[0]) { // [HGM] tourney: make sure we get features after engine replacement. (Should we always do this?)
10901 if(WaitForEngine(&first, TwoMachinesEventIfReady)) { doInit = 1; return 0; } // request to do init on next visit
10902 if(!doInit) return 1; // this replaces testing first.pr != NoProc, which is true when we get here, but first time no reason to abort
10903 doInit = 0; // we fell through (first time after starting the engine); make sure it doesn't happen again
10905 if (first.pr != NoProc) return 1;
10906 StartChessProgram(&first);
10908 InitChessProgram(&first, FALSE);
10909 FeedMovesToProgram(&first, currentMove);
10911 if (!first.sendTime) {
10912 /* can't tell gnuchess what its clock should read,
10913 so we bow to its notion. */
10915 timeRemaining[0][currentMove] = whiteTimeRemaining;
10916 timeRemaining[1][currentMove] = blackTimeRemaining;
10919 if ((gameMode == AnalyzeMode || gameMode == AnalyzeFile ||
10920 appData.icsEngineAnalyze) && first.analysisSupport) {
10921 SendToProgram("analyze\n", &first);
10922 first.analyzing = TRUE;
10928 * Button procedures
10931 Reset (int redraw, int init)
10935 if (appData.debugMode) {
10936 fprintf(debugFP, "Reset(%d, %d) from gameMode %d\n",
10937 redraw, init, gameMode);
10939 CleanupTail(); // [HGM] vari: delete any stored variations
10940 CommentPopDown(); // [HGM] make sure no comments to the previous game keep hanging on
10941 pausing = pauseExamInvalid = FALSE;
10942 startedFromSetupPosition = blackPlaysFirst = FALSE;
10944 whiteFlag = blackFlag = FALSE;
10945 userOfferedDraw = FALSE;
10946 hintRequested = bookRequested = FALSE;
10947 first.maybeThinking = FALSE;
10948 second.maybeThinking = FALSE;
10949 first.bookSuspend = FALSE; // [HGM] book
10950 second.bookSuspend = FALSE;
10951 thinkOutput[0] = NULLCHAR;
10952 lastHint[0] = NULLCHAR;
10953 ClearGameInfo(&gameInfo);
10954 gameInfo.variant = StringToVariant(appData.variant);
10955 ics_user_moved = ics_clock_paused = FALSE;
10956 ics_getting_history = H_FALSE;
10958 white_holding[0] = black_holding[0] = NULLCHAR;
10959 ClearProgramStats();
10960 opponentKibitzes = FALSE; // [HGM] kibitz: do not reserve space in engine-output window in zippy mode
10964 flipView = appData.flipView;
10965 ClearPremoveHighlights();
10966 gotPremove = FALSE;
10967 alarmSounded = FALSE;
10969 GameEnds(EndOfFile, NULL, GE_PLAYER);
10970 if(appData.serverMovesName != NULL) {
10971 /* [HGM] prepare to make moves file for broadcasting */
10972 clock_t t = clock();
10973 if(serverMoves != NULL) fclose(serverMoves);
10974 serverMoves = fopen(appData.serverMovesName, "r");
10975 if(serverMoves != NULL) {
10976 fclose(serverMoves);
10977 /* delay 15 sec before overwriting, so all clients can see end */
10978 while(clock()-t < appData.serverPause*CLOCKS_PER_SEC);
10980 serverMoves = fopen(appData.serverMovesName, "w");
10984 gameMode = BeginningOfGame;
10986 if(appData.icsActive) gameInfo.variant = VariantNormal;
10987 currentMove = forwardMostMove = backwardMostMove = 0;
10988 MarkTargetSquares(1);
10989 InitPosition(redraw);
10990 for (i = 0; i < MAX_MOVES; i++) {
10991 if (commentList[i] != NULL) {
10992 free(commentList[i]);
10993 commentList[i] = NULL;
10997 timeRemaining[0][0] = whiteTimeRemaining;
10998 timeRemaining[1][0] = blackTimeRemaining;
11000 if (first.pr == NoProc) {
11001 StartChessProgram(&first);
11004 InitChessProgram(&first, startedFromSetupPosition);
11007 DisplayMessage("", "");
11008 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
11009 lastSavedGame = 0; // [HGM] save: make sure next game counts as unsaved
11010 ClearMap(); // [HGM] exclude: invalidate map
11014 AutoPlayGameLoop ()
11017 if (!AutoPlayOneMove())
11019 if (matchMode || appData.timeDelay == 0)
11021 if (appData.timeDelay < 0)
11023 StartLoadGameTimer((long)(1000.0f * appData.timeDelay));
11031 ReloadGame(1); // next game
11037 int fromX, fromY, toX, toY;
11039 if (appData.debugMode) {
11040 fprintf(debugFP, "AutoPlayOneMove(): current %d\n", currentMove);
11043 if (gameMode != PlayFromGameFile && gameMode != AnalyzeFile)
11046 if (gameMode == AnalyzeFile && currentMove > backwardMostMove) {
11047 pvInfoList[currentMove].depth = programStats.depth;
11048 pvInfoList[currentMove].score = programStats.score;
11049 pvInfoList[currentMove].time = 0;
11050 if(currentMove < forwardMostMove) AppendComment(currentMove+1, lastPV[0], 2);
11053 if (currentMove >= forwardMostMove) {
11054 if(gameMode == AnalyzeFile) {
11055 if(appData.loadGameIndex == -1) {
11056 GameEnds(EndOfFile, NULL, GE_FILE);
11057 ScheduleDelayedEvent(AnalyzeNextGame, 10);
11059 ExitAnalyzeMode(); SendToProgram("force\n", &first);
11062 // gameMode = EndOfGame;
11063 // ModeHighlight();
11065 /* [AS] Clear current move marker at the end of a game */
11066 /* HistorySet(parseList, backwardMostMove, forwardMostMove, -1); */
11071 toX = moveList[currentMove][2] - AAA;
11072 toY = moveList[currentMove][3] - ONE;
11074 if (moveList[currentMove][1] == '@') {
11075 if (appData.highlightLastMove) {
11076 SetHighlights(-1, -1, toX, toY);
11079 fromX = moveList[currentMove][0] - AAA;
11080 fromY = moveList[currentMove][1] - ONE;
11082 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove); /* [AS] */
11084 AnimateMove(boards[currentMove], fromX, fromY, toX, toY);
11086 if (appData.highlightLastMove) {
11087 SetHighlights(fromX, fromY, toX, toY);
11090 DisplayMove(currentMove);
11091 SendMoveToProgram(currentMove++, &first);
11092 DisplayBothClocks();
11093 DrawPosition(FALSE, boards[currentMove]);
11094 // [HGM] PV info: always display, routine tests if empty
11095 DisplayComment(currentMove - 1, commentList[currentMove]);
11101 LoadGameOneMove (ChessMove readAhead)
11103 int fromX = 0, fromY = 0, toX = 0, toY = 0, done;
11104 char promoChar = NULLCHAR;
11105 ChessMove moveType;
11106 char move[MSG_SIZ];
11109 if (gameMode != PlayFromGameFile && gameMode != AnalyzeFile &&
11110 gameMode != AnalyzeMode && gameMode != Training) {
11115 yyboardindex = forwardMostMove;
11116 if (readAhead != EndOfFile) {
11117 moveType = readAhead;
11119 if (gameFileFP == NULL)
11121 moveType = (ChessMove) Myylex();
11125 switch (moveType) {
11127 if (appData.debugMode)
11128 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
11131 /* append the comment but don't display it */
11132 AppendComment(currentMove, p, FALSE);
11135 case WhiteCapturesEnPassant:
11136 case BlackCapturesEnPassant:
11137 case WhitePromotion:
11138 case BlackPromotion:
11139 case WhiteNonPromotion:
11140 case BlackNonPromotion:
11142 case WhiteKingSideCastle:
11143 case WhiteQueenSideCastle:
11144 case BlackKingSideCastle:
11145 case BlackQueenSideCastle:
11146 case WhiteKingSideCastleWild:
11147 case WhiteQueenSideCastleWild:
11148 case BlackKingSideCastleWild:
11149 case BlackQueenSideCastleWild:
11151 case WhiteHSideCastleFR:
11152 case WhiteASideCastleFR:
11153 case BlackHSideCastleFR:
11154 case BlackASideCastleFR:
11156 if (appData.debugMode)
11157 fprintf(debugFP, "Parsed %s into %s\n", yy_text, currentMoveString);
11158 fromX = currentMoveString[0] - AAA;
11159 fromY = currentMoveString[1] - ONE;
11160 toX = currentMoveString[2] - AAA;
11161 toY = currentMoveString[3] - ONE;
11162 promoChar = currentMoveString[4];
11167 if (appData.debugMode)
11168 fprintf(debugFP, "Parsed %s into %s\n", yy_text, currentMoveString);
11169 fromX = moveType == WhiteDrop ?
11170 (int) CharToPiece(ToUpper(currentMoveString[0])) :
11171 (int) CharToPiece(ToLower(currentMoveString[0]));
11173 toX = currentMoveString[2] - AAA;
11174 toY = currentMoveString[3] - ONE;
11180 case GameUnfinished:
11181 if (appData.debugMode)
11182 fprintf(debugFP, "Parsed game end: %s\n", yy_text);
11183 p = strchr(yy_text, '{');
11184 if (p == NULL) p = strchr(yy_text, '(');
11187 if (p[0] == '0' || p[0] == '1' || p[0] == '*') p = "";
11189 q = strchr(p, *p == '{' ? '}' : ')');
11190 if (q != NULL) *q = NULLCHAR;
11193 while(q = strchr(p, '\n')) *q = ' '; // [HGM] crush linefeeds in result message
11194 GameEnds(moveType, p, GE_FILE);
11196 if (cmailMsgLoaded) {
11198 flipView = WhiteOnMove(currentMove);
11199 if (moveType == GameUnfinished) flipView = !flipView;
11200 if (appData.debugMode)
11201 fprintf(debugFP, "Setting flipView to %d\n", flipView) ;
11206 if (appData.debugMode)
11207 fprintf(debugFP, "Parser hit end of file\n");
11208 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
11214 if (WhiteOnMove(currentMove)) {
11215 GameEnds(BlackWins, "Black mates", GE_FILE);
11217 GameEnds(WhiteWins, "White mates", GE_FILE);
11221 GameEnds(GameIsDrawn, "Stalemate", GE_FILE);
11227 case MoveNumberOne:
11228 if (lastLoadGameStart == GNUChessGame) {
11229 /* GNUChessGames have numbers, but they aren't move numbers */
11230 if (appData.debugMode)
11231 fprintf(debugFP, "Parser ignoring: '%s' (%d)\n",
11232 yy_text, (int) moveType);
11233 return LoadGameOneMove(EndOfFile); /* tail recursion */
11235 /* else fall thru */
11240 /* Reached start of next game in file */
11241 if (appData.debugMode)
11242 fprintf(debugFP, "Parsed start of next game: %s\n", yy_text);
11243 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
11249 if (WhiteOnMove(currentMove)) {
11250 GameEnds(BlackWins, "Black mates", GE_FILE);
11252 GameEnds(WhiteWins, "White mates", GE_FILE);
11256 GameEnds(GameIsDrawn, "Stalemate", GE_FILE);
11262 case PositionDiagram: /* should not happen; ignore */
11263 case ElapsedTime: /* ignore */
11264 case NAG: /* ignore */
11265 if (appData.debugMode)
11266 fprintf(debugFP, "Parser ignoring: '%s' (%d)\n",
11267 yy_text, (int) moveType);
11268 return LoadGameOneMove(EndOfFile); /* tail recursion */
11271 if (appData.testLegality) {
11272 if (appData.debugMode)
11273 fprintf(debugFP, "Parsed IllegalMove: %s\n", yy_text);
11274 snprintf(move, MSG_SIZ, _("Illegal move: %d.%s%s"),
11275 (forwardMostMove / 2) + 1,
11276 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
11277 DisplayError(move, 0);
11280 if (appData.debugMode)
11281 fprintf(debugFP, "Parsed %s into IllegalMove %s\n",
11282 yy_text, currentMoveString);
11283 fromX = currentMoveString[0] - AAA;
11284 fromY = currentMoveString[1] - ONE;
11285 toX = currentMoveString[2] - AAA;
11286 toY = currentMoveString[3] - ONE;
11287 promoChar = currentMoveString[4];
11291 case AmbiguousMove:
11292 if (appData.debugMode)
11293 fprintf(debugFP, "Parsed AmbiguousMove: %s\n", yy_text);
11294 snprintf(move, MSG_SIZ, _("Ambiguous move: %d.%s%s"),
11295 (forwardMostMove / 2) + 1,
11296 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
11297 DisplayError(move, 0);
11302 case ImpossibleMove:
11303 if (appData.debugMode)
11304 fprintf(debugFP, "Parsed ImpossibleMove (type = %d): %s\n", moveType, yy_text);
11305 snprintf(move, MSG_SIZ, _("Illegal move: %d.%s%s"),
11306 (forwardMostMove / 2) + 1,
11307 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
11308 DisplayError(move, 0);
11314 if (appData.matchMode || (appData.timeDelay == 0 && !pausing)) {
11315 DrawPosition(FALSE, boards[currentMove]);
11316 DisplayBothClocks();
11317 if (!appData.matchMode) // [HGM] PV info: routine tests if empty
11318 DisplayComment(currentMove - 1, commentList[currentMove]);
11320 (void) StopLoadGameTimer();
11322 cmailOldMove = forwardMostMove;
11325 /* currentMoveString is set as a side-effect of yylex */
11327 thinkOutput[0] = NULLCHAR;
11328 MakeMove(fromX, fromY, toX, toY, promoChar);
11329 currentMove = forwardMostMove;
11334 /* Load the nth game from the given file */
11336 LoadGameFromFile (char *filename, int n, char *title, int useList)
11341 if (strcmp(filename, "-") == 0) {
11345 f = fopen(filename, "rb");
11347 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
11348 DisplayError(buf, errno);
11352 if (fseek(f, 0, 0) == -1) {
11353 /* f is not seekable; probably a pipe */
11356 if (useList && n == 0) {
11357 int error = GameListBuild(f);
11359 DisplayError(_("Cannot build game list"), error);
11360 } else if (!ListEmpty(&gameList) &&
11361 ((ListGame *) gameList.tailPred)->number > 1) {
11362 GameListPopUp(f, title);
11369 return LoadGame(f, n, title, FALSE);
11374 MakeRegisteredMove ()
11376 int fromX, fromY, toX, toY;
11378 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
11379 switch (cmailMoveType[lastLoadGameNumber - 1]) {
11382 if (appData.debugMode)
11383 fprintf(debugFP, "Restoring %s for game %d\n",
11384 cmailMove[lastLoadGameNumber - 1], lastLoadGameNumber);
11386 thinkOutput[0] = NULLCHAR;
11387 safeStrCpy(moveList[currentMove], cmailMove[lastLoadGameNumber - 1], sizeof(moveList[currentMove])/sizeof(moveList[currentMove][0]));
11388 fromX = cmailMove[lastLoadGameNumber - 1][0] - AAA;
11389 fromY = cmailMove[lastLoadGameNumber - 1][1] - ONE;
11390 toX = cmailMove[lastLoadGameNumber - 1][2] - AAA;
11391 toY = cmailMove[lastLoadGameNumber - 1][3] - ONE;
11392 promoChar = cmailMove[lastLoadGameNumber - 1][4];
11393 MakeMove(fromX, fromY, toX, toY, promoChar);
11394 ShowMove(fromX, fromY, toX, toY);
11396 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
11403 if (WhiteOnMove(currentMove)) {
11404 GameEnds(BlackWins, "Black mates", GE_PLAYER);
11406 GameEnds(WhiteWins, "White mates", GE_PLAYER);
11411 GameEnds(GameIsDrawn, "Stalemate", GE_PLAYER);
11418 if (WhiteOnMove(currentMove)) {
11419 GameEnds(BlackWins, "White resigns", GE_PLAYER);
11421 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
11426 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
11437 /* Wrapper around LoadGame for use when a Cmail message is loaded */
11439 CmailLoadGame (FILE *f, int gameNumber, char *title, int useList)
11443 if (gameNumber > nCmailGames) {
11444 DisplayError(_("No more games in this message"), 0);
11447 if (f == lastLoadGameFP) {
11448 int offset = gameNumber - lastLoadGameNumber;
11450 cmailMsg[0] = NULLCHAR;
11451 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
11452 cmailMoveRegistered[lastLoadGameNumber - 1] = FALSE;
11453 nCmailMovesRegistered--;
11455 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
11456 if (cmailResult[lastLoadGameNumber - 1] == CMAIL_NEW_RESULT) {
11457 cmailResult[lastLoadGameNumber - 1] = CMAIL_NOT_RESULT;
11460 if (! RegisterMove()) return FALSE;
11464 retVal = LoadGame(f, gameNumber, title, useList);
11466 /* Make move registered during previous look at this game, if any */
11467 MakeRegisteredMove();
11469 if (cmailCommentList[lastLoadGameNumber - 1] != NULL) {
11470 commentList[currentMove]
11471 = StrSave(cmailCommentList[lastLoadGameNumber - 1]);
11472 DisplayComment(currentMove - 1, commentList[currentMove]);
11478 /* Support for LoadNextGame, LoadPreviousGame, ReloadSameGame */
11480 ReloadGame (int offset)
11482 int gameNumber = lastLoadGameNumber + offset;
11483 if (lastLoadGameFP == NULL) {
11484 DisplayError(_("No game has been loaded yet"), 0);
11487 if (gameNumber <= 0) {
11488 DisplayError(_("Can't back up any further"), 0);
11491 if (cmailMsgLoaded) {
11492 return CmailLoadGame(lastLoadGameFP, gameNumber,
11493 lastLoadGameTitle, lastLoadGameUseList);
11495 return LoadGame(lastLoadGameFP, gameNumber,
11496 lastLoadGameTitle, lastLoadGameUseList);
11500 int keys[EmptySquare+1];
11503 PositionMatches (Board b1, Board b2)
11506 switch(appData.searchMode) {
11507 case 1: return CompareWithRights(b1, b2);
11509 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11510 if(b2[r][f] != EmptySquare && b1[r][f] != b2[r][f]) return FALSE;
11514 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11515 if((b2[r][f] == WhitePawn || b2[r][f] == BlackPawn) && b1[r][f] != b2[r][f]) return FALSE;
11516 sum += keys[b1[r][f]] - keys[b2[r][f]];
11520 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11521 sum += keys[b1[r][f]] - keys[b2[r][f]];
11533 int pieceList[256], quickBoard[256];
11534 ChessSquare pieceType[256] = { EmptySquare };
11535 Board soughtBoard, reverseBoard, flipBoard, rotateBoard;
11536 int counts[EmptySquare], minSought[EmptySquare], minReverse[EmptySquare], maxSought[EmptySquare], maxReverse[EmptySquare];
11537 int soughtTotal, turn;
11538 Boolean epOK, flipSearch;
11541 unsigned char piece, to;
11544 #define DSIZE (250000)
11546 Move initialSpace[DSIZE+1000]; // gamble on that game will not be more than 500 moves
11547 Move *moveDatabase = initialSpace;
11548 unsigned int movePtr, dataSize = DSIZE;
11551 MakePieceList (Board board, int *counts)
11553 int r, f, n=Q_PROMO, total=0;
11554 for(r=0;r<EmptySquare;r++) counts[r] = 0; // piece-type counts
11555 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11556 int sq = f + (r<<4);
11557 if(board[r][f] == EmptySquare) quickBoard[sq] = 0; else {
11558 quickBoard[sq] = ++n;
11560 pieceType[n] = board[r][f];
11561 counts[board[r][f]]++;
11562 if(board[r][f] == WhiteKing) pieceList[1] = n; else
11563 if(board[r][f] == BlackKing) pieceList[2] = n; // remember which are Kings, for castling
11567 epOK = gameInfo.variant != VariantXiangqi && gameInfo.variant != VariantBerolina;
11572 PackMove (int fromX, int fromY, int toX, int toY, ChessSquare promoPiece)
11574 int sq = fromX + (fromY<<4);
11575 int piece = quickBoard[sq];
11576 quickBoard[sq] = 0;
11577 moveDatabase[movePtr].to = pieceList[piece] = sq = toX + (toY<<4);
11578 if(piece == pieceList[1] && fromY == toY && (toX > fromX+1 || toX < fromX-1) && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1) {
11579 int from = toX>fromX ? BOARD_RGHT-1 : BOARD_LEFT;
11580 moveDatabase[movePtr++].piece = Q_WCASTL;
11581 quickBoard[sq] = piece;
11582 piece = quickBoard[from]; quickBoard[from] = 0;
11583 moveDatabase[movePtr].to = pieceList[piece] = sq = toX>fromX ? sq-1 : sq+1;
11585 if(piece == pieceList[2] && fromY == toY && (toX > fromX+1 || toX < fromX-1) && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1) {
11586 int from = (toX>fromX ? BOARD_RGHT-1 : BOARD_LEFT) + (BOARD_HEIGHT-1 <<4);
11587 moveDatabase[movePtr++].piece = Q_BCASTL;
11588 quickBoard[sq] = piece;
11589 piece = quickBoard[from]; quickBoard[from] = 0;
11590 moveDatabase[movePtr].to = pieceList[piece] = sq = toX>fromX ? sq-1 : sq+1;
11592 if(epOK && (pieceType[piece] == WhitePawn || pieceType[piece] == BlackPawn) && fromX != toX && quickBoard[sq] == 0) {
11593 quickBoard[(fromY<<4)+toX] = 0;
11594 moveDatabase[movePtr].piece = Q_EP;
11595 moveDatabase[movePtr++].to = (fromY<<4)+toX;
11596 moveDatabase[movePtr].to = sq;
11598 if(promoPiece != pieceType[piece]) {
11599 moveDatabase[movePtr++].piece = Q_PROMO;
11600 moveDatabase[movePtr].to = pieceType[piece] = (int) promoPiece;
11602 moveDatabase[movePtr].piece = piece;
11603 quickBoard[sq] = piece;
11608 PackGame (Board board)
11610 Move *newSpace = NULL;
11611 moveDatabase[movePtr].piece = 0; // terminate previous game
11612 if(movePtr > dataSize) {
11613 if(appData.debugMode) fprintf(debugFP, "move-cache overflow, enlarge to %d MB\n", dataSize/128);
11614 dataSize *= 8; // increase size by factor 8 (512KB -> 4MB -> 32MB -> 256MB -> 2GB)
11615 if(dataSize) newSpace = (Move*) calloc(dataSize + 1000, sizeof(Move));
11618 Move *p = moveDatabase, *q = newSpace;
11619 for(i=0; i<movePtr; i++) *q++ = *p++; // copy to newly allocated space
11620 if(dataSize > 8*DSIZE) free(moveDatabase); // and free old space (if it was allocated)
11621 moveDatabase = newSpace;
11622 } else { // calloc failed, we must be out of memory. Too bad...
11623 dataSize = 0; // prevent calloc events for all subsequent games
11624 return 0; // and signal this one isn't cached
11628 MakePieceList(board, counts);
11633 QuickCompare (Board board, int *minCounts, int *maxCounts)
11634 { // compare according to search mode
11636 switch(appData.searchMode)
11638 case 1: // exact position match
11639 if(!(turn & board[EP_STATUS-1])) return FALSE; // wrong side to move
11640 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11641 if(board[r][f] != pieceType[quickBoard[(r<<4)+f]]) return FALSE;
11644 case 2: // can have extra material on empty squares
11645 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11646 if(board[r][f] == EmptySquare) continue;
11647 if(board[r][f] != pieceType[quickBoard[(r<<4)+f]]) return FALSE;
11650 case 3: // material with exact Pawn structure
11651 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11652 if(board[r][f] != WhitePawn && board[r][f] != BlackPawn) continue;
11653 if(board[r][f] != pieceType[quickBoard[(r<<4)+f]]) return FALSE;
11654 } // fall through to material comparison
11655 case 4: // exact material
11656 for(r=0; r<EmptySquare; r++) if(counts[r] != maxCounts[r]) return FALSE;
11658 case 6: // material range with given imbalance
11659 for(r=0; r<BlackPawn; r++) if(counts[r] - minCounts[r] != counts[r+BlackPawn] - minCounts[r+BlackPawn]) return FALSE;
11660 // fall through to range comparison
11661 case 5: // material range
11662 for(r=0; r<EmptySquare; r++) if(counts[r] < minCounts[r] || counts[r] > maxCounts[r]) return FALSE;
11668 QuickScan (Board board, Move *move)
11669 { // reconstruct game,and compare all positions in it
11670 int cnt=0, stretch=0, total = MakePieceList(board, counts);
11672 int piece = move->piece;
11673 int to = move->to, from = pieceList[piece];
11674 if(piece <= Q_PROMO) { // special moves encoded by otherwise invalid piece numbers 1-4
11675 if(!piece) return -1;
11676 if(piece == Q_PROMO) { // promotion, encoded as (Q_PROMO, to) + (piece, promoType)
11677 piece = (++move)->piece;
11678 from = pieceList[piece];
11679 counts[pieceType[piece]]--;
11680 pieceType[piece] = (ChessSquare) move->to;
11681 counts[move->to]++;
11682 } else if(piece == Q_EP) { // e.p. capture, encoded as (Q_EP, ep-sqr) + (piece, to)
11683 counts[pieceType[quickBoard[to]]]--;
11684 quickBoard[to] = 0; total--;
11687 } else if(piece <= Q_BCASTL) { // castling, encoded as (Q_XCASTL, king-to) + (rook, rook-to)
11688 piece = pieceList[piece]; // first two elements of pieceList contain King numbers
11689 from = pieceList[piece]; // so this must be King
11690 quickBoard[from] = 0;
11691 pieceList[piece] = to;
11692 from = pieceList[(++move)->piece]; // for FRC this has to be done here
11693 quickBoard[from] = 0; // rook
11694 quickBoard[to] = piece;
11695 to = move->to; piece = move->piece;
11699 if(appData.searchMode > 2) counts[pieceType[quickBoard[to]]]--; // account capture
11700 if((total -= (quickBoard[to] != 0)) < soughtTotal) return -1; // piece count dropped below what we search for
11701 quickBoard[from] = 0;
11703 quickBoard[to] = piece;
11704 pieceList[piece] = to;
11706 if(QuickCompare(soughtBoard, minSought, maxSought) ||
11707 appData.ignoreColors && QuickCompare(reverseBoard, minReverse, maxReverse) ||
11708 flipSearch && (QuickCompare(flipBoard, minSought, maxSought) ||
11709 appData.ignoreColors && QuickCompare(rotateBoard, minReverse, maxReverse))
11711 static int lastCounts[EmptySquare+1];
11713 if(stretch) for(i=0; i<EmptySquare; i++) if(lastCounts[i] != counts[i]) { stretch = 0; break; } // reset if material changes
11714 if(stretch++ == 0) for(i=0; i<EmptySquare; i++) lastCounts[i] = counts[i]; // remember actual material
11715 } else stretch = 0;
11716 if(stretch && (appData.searchMode == 1 || stretch >= appData.stretch)) return cnt + 1 - stretch;
11725 flipSearch = FALSE;
11726 CopyBoard(soughtBoard, boards[currentMove]);
11727 soughtTotal = MakePieceList(soughtBoard, maxSought);
11728 soughtBoard[EP_STATUS-1] = (currentMove & 1) + 1;
11729 if(currentMove == 0 && gameMode == EditPosition) soughtBoard[EP_STATUS-1] = blackPlaysFirst + 1; // (!)
11730 CopyBoard(reverseBoard, boards[currentMove]);
11731 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11732 int piece = boards[currentMove][BOARD_HEIGHT-1-r][f];
11733 if(piece < BlackPawn) piece += BlackPawn; else if(piece < EmptySquare) piece -= BlackPawn; // color-flip
11734 reverseBoard[r][f] = piece;
11736 reverseBoard[EP_STATUS-1] = soughtBoard[EP_STATUS-1] ^ 3;
11737 for(r=0; r<6; r++) reverseBoard[CASTLING][r] = boards[currentMove][CASTLING][(r+3)%6];
11738 if(appData.findMirror && appData.searchMode <= 3 && (!nrCastlingRights
11739 || (boards[currentMove][CASTLING][2] == NoRights ||
11740 boards[currentMove][CASTLING][0] == NoRights && boards[currentMove][CASTLING][1] == NoRights )
11741 && (boards[currentMove][CASTLING][5] == NoRights ||
11742 boards[currentMove][CASTLING][3] == NoRights && boards[currentMove][CASTLING][4] == NoRights ) )
11745 CopyBoard(flipBoard, soughtBoard);
11746 CopyBoard(rotateBoard, reverseBoard);
11747 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11748 flipBoard[r][f] = soughtBoard[r][BOARD_WIDTH-1-f];
11749 rotateBoard[r][f] = reverseBoard[r][BOARD_WIDTH-1-f];
11752 for(r=0; r<BlackPawn; r++) maxReverse[r] = maxSought[r+BlackPawn], maxReverse[r+BlackPawn] = maxSought[r];
11753 if(appData.searchMode >= 5) {
11754 for(r=BOARD_HEIGHT/2; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) soughtBoard[r][f] = EmptySquare;
11755 MakePieceList(soughtBoard, minSought);
11756 for(r=0; r<BlackPawn; r++) minReverse[r] = minSought[r+BlackPawn], minReverse[r+BlackPawn] = minSought[r];
11758 if(gameInfo.variant == VariantCrazyhouse || gameInfo.variant == VariantShogi || gameInfo.variant == VariantBughouse)
11759 soughtTotal = 0; // in drop games nr of pieces does not fall monotonously
11762 GameInfo dummyInfo;
11765 GameContainsPosition (FILE *f, ListGame *lg)
11767 int next, btm=0, plyNr=0, scratch=forwardMostMove+2&~1;
11768 int fromX, fromY, toX, toY;
11770 static int initDone=FALSE;
11772 // weed out games based on numerical tag comparison
11773 if(lg->gameInfo.variant != gameInfo.variant) return -1; // wrong variant
11774 if(appData.eloThreshold1 && (lg->gameInfo.whiteRating < appData.eloThreshold1 && lg->gameInfo.blackRating < appData.eloThreshold1)) return -1;
11775 if(appData.eloThreshold2 && (lg->gameInfo.whiteRating < appData.eloThreshold2 || lg->gameInfo.blackRating < appData.eloThreshold2)) return -1;
11776 if(appData.dateThreshold && (!lg->gameInfo.date || atoi(lg->gameInfo.date) < appData.dateThreshold)) return -1;
11778 for(next = WhitePawn; next<EmptySquare; next++) keys[next] = random()>>8 ^ random()<<6 ^random()<<20;
11781 if(lg->gameInfo.fen) ParseFEN(boards[scratch], &btm, lg->gameInfo.fen);
11782 else CopyBoard(boards[scratch], initialPosition); // default start position
11785 if((next = QuickScan( boards[scratch], &moveDatabase[lg->moves] )) < 0) return -1; // quick scan rules out it is there
11786 if(appData.searchMode >= 4) return next; // for material searches, trust QuickScan.
11789 if(PositionMatches(boards[scratch], boards[currentMove])) return plyNr;
11790 fseek(f, lg->offset, 0);
11793 yyboardindex = scratch;
11794 quickFlag = plyNr+1;
11799 if(plyNr) return -1; // after we have seen moves, any tags will be start of next game
11805 if(plyNr) return -1; // after we have seen moves, this is for new game
11808 case AmbiguousMove: // we cannot reconstruct the game beyond these two
11809 case ImpossibleMove:
11810 case WhiteWins: // game ends here with these four
11813 case GameUnfinished:
11817 if(appData.testLegality) return -1;
11818 case WhiteCapturesEnPassant:
11819 case BlackCapturesEnPassant:
11820 case WhitePromotion:
11821 case BlackPromotion:
11822 case WhiteNonPromotion:
11823 case BlackNonPromotion:
11825 case WhiteKingSideCastle:
11826 case WhiteQueenSideCastle:
11827 case BlackKingSideCastle:
11828 case BlackQueenSideCastle:
11829 case WhiteKingSideCastleWild:
11830 case WhiteQueenSideCastleWild:
11831 case BlackKingSideCastleWild:
11832 case BlackQueenSideCastleWild:
11833 case WhiteHSideCastleFR:
11834 case WhiteASideCastleFR:
11835 case BlackHSideCastleFR:
11836 case BlackASideCastleFR:
11837 fromX = currentMoveString[0] - AAA;
11838 fromY = currentMoveString[1] - ONE;
11839 toX = currentMoveString[2] - AAA;
11840 toY = currentMoveString[3] - ONE;
11841 promoChar = currentMoveString[4];
11845 fromX = next == WhiteDrop ?
11846 (int) CharToPiece(ToUpper(currentMoveString[0])) :
11847 (int) CharToPiece(ToLower(currentMoveString[0]));
11849 toX = currentMoveString[2] - AAA;
11850 toY = currentMoveString[3] - ONE;
11854 // Move encountered; peform it. We need to shuttle between two boards, as even/odd index determines side to move
11856 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[scratch]);
11857 if(PositionMatches(boards[scratch], boards[currentMove])) return plyNr;
11858 if(appData.ignoreColors && PositionMatches(boards[scratch], reverseBoard)) return plyNr;
11859 if(appData.findMirror) {
11860 if(PositionMatches(boards[scratch], flipBoard)) return plyNr;
11861 if(appData.ignoreColors && PositionMatches(boards[scratch], rotateBoard)) return plyNr;
11866 /* Load the nth game from open file f */
11868 LoadGame (FILE *f, int gameNumber, char *title, int useList)
11872 int gn = gameNumber;
11873 ListGame *lg = NULL;
11874 int numPGNTags = 0;
11876 GameMode oldGameMode;
11877 VariantClass oldVariant = gameInfo.variant; /* [HGM] PGNvariant */
11879 if (appData.debugMode)
11880 fprintf(debugFP, "LoadGame(): on entry, gameMode %d\n", gameMode);
11882 if (gameMode == Training )
11883 SetTrainingModeOff();
11885 oldGameMode = gameMode;
11886 if (gameMode != BeginningOfGame) {
11887 Reset(FALSE, TRUE);
11891 if (lastLoadGameFP != NULL && lastLoadGameFP != f) {
11892 fclose(lastLoadGameFP);
11896 lg = (ListGame *) ListElem(&gameList, gameNumber-1);
11899 fseek(f, lg->offset, 0);
11900 GameListHighlight(gameNumber);
11901 pos = lg->position;
11905 if(gameMode == AnalyzeFile && appData.loadGameIndex == -1)
11906 appData.loadGameIndex = 0; // [HGM] suppress error message if we reach file end after auto-stepping analysis
11908 DisplayError(_("Game number out of range"), 0);
11913 if (fseek(f, 0, 0) == -1) {
11914 if (f == lastLoadGameFP ?
11915 gameNumber == lastLoadGameNumber + 1 :
11919 DisplayError(_("Can't seek on game file"), 0);
11924 lastLoadGameFP = f;
11925 lastLoadGameNumber = gameNumber;
11926 safeStrCpy(lastLoadGameTitle, title, sizeof(lastLoadGameTitle)/sizeof(lastLoadGameTitle[0]));
11927 lastLoadGameUseList = useList;
11931 if (lg && lg->gameInfo.white && lg->gameInfo.black) {
11932 snprintf(buf, sizeof(buf), "%s %s %s", lg->gameInfo.white, _("vs."),
11933 lg->gameInfo.black);
11935 } else if (*title != NULLCHAR) {
11936 if (gameNumber > 1) {
11937 snprintf(buf, MSG_SIZ, "%s %d", title, gameNumber);
11940 DisplayTitle(title);
11944 if (gameMode != AnalyzeFile && gameMode != AnalyzeMode) {
11945 gameMode = PlayFromGameFile;
11949 currentMove = forwardMostMove = backwardMostMove = 0;
11950 CopyBoard(boards[0], initialPosition);
11954 * Skip the first gn-1 games in the file.
11955 * Also skip over anything that precedes an identifiable
11956 * start of game marker, to avoid being confused by
11957 * garbage at the start of the file. Currently
11958 * recognized start of game markers are the move number "1",
11959 * the pattern "gnuchess .* game", the pattern
11960 * "^[#;%] [^ ]* game file", and a PGN tag block.
11961 * A game that starts with one of the latter two patterns
11962 * will also have a move number 1, possibly
11963 * following a position diagram.
11964 * 5-4-02: Let's try being more lenient and allowing a game to
11965 * start with an unnumbered move. Does that break anything?
11967 cm = lastLoadGameStart = EndOfFile;
11969 yyboardindex = forwardMostMove;
11970 cm = (ChessMove) Myylex();
11973 if (cmailMsgLoaded) {
11974 nCmailGames = CMAIL_MAX_GAMES - gn;
11977 DisplayError(_("Game not found in file"), 0);
11984 lastLoadGameStart = cm;
11987 case MoveNumberOne:
11988 switch (lastLoadGameStart) {
11993 case MoveNumberOne:
11995 gn--; /* count this game */
11996 lastLoadGameStart = cm;
12005 switch (lastLoadGameStart) {
12008 case MoveNumberOne:
12010 gn--; /* count this game */
12011 lastLoadGameStart = cm;
12014 lastLoadGameStart = cm; /* game counted already */
12022 yyboardindex = forwardMostMove;
12023 cm = (ChessMove) Myylex();
12024 } while (cm == PGNTag || cm == Comment);
12031 if (cmailMsgLoaded && (CMAIL_MAX_GAMES == lastLoadGameNumber)) {
12032 if ( cmailResult[CMAIL_MAX_GAMES - gn - 1]
12033 != CMAIL_OLD_RESULT) {
12035 cmailResult[ CMAIL_MAX_GAMES
12036 - gn - 1] = CMAIL_OLD_RESULT;
12042 /* Only a NormalMove can be at the start of a game
12043 * without a position diagram. */
12044 if (lastLoadGameStart == EndOfFile ) {
12046 lastLoadGameStart = MoveNumberOne;
12055 if (appData.debugMode)
12056 fprintf(debugFP, "Parsed game start '%s' (%d)\n", yy_text, (int) cm);
12058 if (cm == XBoardGame) {
12059 /* Skip any header junk before position diagram and/or move 1 */
12061 yyboardindex = forwardMostMove;
12062 cm = (ChessMove) Myylex();
12064 if (cm == EndOfFile ||
12065 cm == GNUChessGame || cm == XBoardGame) {
12066 /* Empty game; pretend end-of-file and handle later */
12071 if (cm == MoveNumberOne || cm == PositionDiagram ||
12072 cm == PGNTag || cm == Comment)
12075 } else if (cm == GNUChessGame) {
12076 if (gameInfo.event != NULL) {
12077 free(gameInfo.event);
12079 gameInfo.event = StrSave(yy_text);
12082 startedFromSetupPosition = FALSE;
12083 while (cm == PGNTag) {
12084 if (appData.debugMode)
12085 fprintf(debugFP, "Parsed PGNTag: %s\n", yy_text);
12086 err = ParsePGNTag(yy_text, &gameInfo);
12087 if (!err) numPGNTags++;
12089 /* [HGM] PGNvariant: automatically switch to variant given in PGN tag */
12090 if(gameInfo.variant != oldVariant) {
12091 startedFromPositionFile = FALSE; /* [HGM] loadPos: variant switch likely makes position invalid */
12092 ResetFrontEnd(); // [HGM] might need other bitmaps. Cannot use Reset() because it clears gameInfo :-(
12093 InitPosition(TRUE);
12094 oldVariant = gameInfo.variant;
12095 if (appData.debugMode)
12096 fprintf(debugFP, "New variant %d\n", (int) oldVariant);
12100 if (gameInfo.fen != NULL) {
12101 Board initial_position;
12102 startedFromSetupPosition = TRUE;
12103 if (!ParseFEN(initial_position, &blackPlaysFirst, gameInfo.fen)) {
12105 DisplayError(_("Bad FEN position in file"), 0);
12108 CopyBoard(boards[0], initial_position);
12109 if (blackPlaysFirst) {
12110 currentMove = forwardMostMove = backwardMostMove = 1;
12111 CopyBoard(boards[1], initial_position);
12112 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
12113 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
12114 timeRemaining[0][1] = whiteTimeRemaining;
12115 timeRemaining[1][1] = blackTimeRemaining;
12116 if (commentList[0] != NULL) {
12117 commentList[1] = commentList[0];
12118 commentList[0] = NULL;
12121 currentMove = forwardMostMove = backwardMostMove = 0;
12123 /* [HGM] copy FEN attributes as well. Bugfix 4.3.14m and 4.3.15e: moved to after 'blackPlaysFirst' */
12125 initialRulePlies = FENrulePlies;
12126 for( i=0; i< nrCastlingRights; i++ )
12127 initialRights[i] = initial_position[CASTLING][i];
12129 yyboardindex = forwardMostMove;
12130 free(gameInfo.fen);
12131 gameInfo.fen = NULL;
12134 yyboardindex = forwardMostMove;
12135 cm = (ChessMove) Myylex();
12137 /* Handle comments interspersed among the tags */
12138 while (cm == Comment) {
12140 if (appData.debugMode)
12141 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
12143 AppendComment(currentMove, p, FALSE);
12144 yyboardindex = forwardMostMove;
12145 cm = (ChessMove) Myylex();
12149 /* don't rely on existence of Event tag since if game was
12150 * pasted from clipboard the Event tag may not exist
12152 if (numPGNTags > 0){
12154 if (gameInfo.variant == VariantNormal) {
12155 VariantClass v = StringToVariant(gameInfo.event);
12156 // [HGM] do not recognize variants from event tag that were introduced after supporting variant tag
12157 if(v < VariantShogi) gameInfo.variant = v;
12160 if( appData.autoDisplayTags ) {
12161 tags = PGNTags(&gameInfo);
12162 TagsPopUp(tags, CmailMsg());
12167 /* Make something up, but don't display it now */
12172 if (cm == PositionDiagram) {
12175 Board initial_position;
12177 if (appData.debugMode)
12178 fprintf(debugFP, "Parsed PositionDiagram: %s\n", yy_text);
12180 if (!startedFromSetupPosition) {
12182 for (i = BOARD_HEIGHT - 1; i >= 0; i--)
12183 for (j = BOARD_LEFT; j < BOARD_RGHT; p++)
12194 initial_position[i][j++] = CharToPiece(*p);
12197 while (*p == ' ' || *p == '\t' ||
12198 *p == '\n' || *p == '\r') p++;
12200 if (strncmp(p, "black", strlen("black"))==0)
12201 blackPlaysFirst = TRUE;
12203 blackPlaysFirst = FALSE;
12204 startedFromSetupPosition = TRUE;
12206 CopyBoard(boards[0], initial_position);
12207 if (blackPlaysFirst) {
12208 currentMove = forwardMostMove = backwardMostMove = 1;
12209 CopyBoard(boards[1], initial_position);
12210 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
12211 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
12212 timeRemaining[0][1] = whiteTimeRemaining;
12213 timeRemaining[1][1] = blackTimeRemaining;
12214 if (commentList[0] != NULL) {
12215 commentList[1] = commentList[0];
12216 commentList[0] = NULL;
12219 currentMove = forwardMostMove = backwardMostMove = 0;
12222 yyboardindex = forwardMostMove;
12223 cm = (ChessMove) Myylex();
12226 if (first.pr == NoProc) {
12227 StartChessProgram(&first);
12229 InitChessProgram(&first, FALSE);
12230 SendToProgram("force\n", &first);
12231 if (startedFromSetupPosition) {
12232 SendBoard(&first, forwardMostMove);
12233 if (appData.debugMode) {
12234 fprintf(debugFP, "Load Game\n");
12236 DisplayBothClocks();
12239 /* [HGM] server: flag to write setup moves in broadcast file as one */
12240 loadFlag = appData.suppressLoadMoves;
12242 while (cm == Comment) {
12244 if (appData.debugMode)
12245 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
12247 AppendComment(currentMove, p, FALSE);
12248 yyboardindex = forwardMostMove;
12249 cm = (ChessMove) Myylex();
12252 if ((cm == EndOfFile && lastLoadGameStart != EndOfFile ) ||
12253 cm == WhiteWins || cm == BlackWins ||
12254 cm == GameIsDrawn || cm == GameUnfinished) {
12255 DisplayMessage("", _("No moves in game"));
12256 if (cmailMsgLoaded) {
12257 if (appData.debugMode)
12258 fprintf(debugFP, "Setting flipView to %d.\n", FALSE);
12262 DrawPosition(FALSE, boards[currentMove]);
12263 DisplayBothClocks();
12264 gameMode = EditGame;
12271 // [HGM] PV info: routine tests if comment empty
12272 if (!matchMode && (pausing || appData.timeDelay != 0)) {
12273 DisplayComment(currentMove - 1, commentList[currentMove]);
12275 if (!matchMode && appData.timeDelay != 0)
12276 DrawPosition(FALSE, boards[currentMove]);
12278 if (gameMode == AnalyzeFile || gameMode == AnalyzeMode) {
12279 programStats.ok_to_send = 1;
12282 /* if the first token after the PGN tags is a move
12283 * and not move number 1, retrieve it from the parser
12285 if (cm != MoveNumberOne)
12286 LoadGameOneMove(cm);
12288 /* load the remaining moves from the file */
12289 while (LoadGameOneMove(EndOfFile)) {
12290 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
12291 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
12294 /* rewind to the start of the game */
12295 currentMove = backwardMostMove;
12297 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
12299 if (oldGameMode == AnalyzeFile ||
12300 oldGameMode == AnalyzeMode) {
12301 appData.loadGameIndex = -1; // [HGM] order auto-stepping through games
12303 AnalyzeFileEvent();
12307 if (!matchMode && pos > 0) {
12308 ToNrEvent(pos); // [HGM] no autoplay if selected on position
12310 if (matchMode || appData.timeDelay == 0) {
12312 } else if (appData.timeDelay > 0) {
12313 AutoPlayGameLoop();
12316 if (appData.debugMode)
12317 fprintf(debugFP, "LoadGame(): on exit, gameMode %d\n", gameMode);
12319 loadFlag = 0; /* [HGM] true game starts */
12323 /* Support for LoadNextPosition, LoadPreviousPosition, ReloadSamePosition */
12325 ReloadPosition (int offset)
12327 int positionNumber = lastLoadPositionNumber + offset;
12328 if (lastLoadPositionFP == NULL) {
12329 DisplayError(_("No position has been loaded yet"), 0);
12332 if (positionNumber <= 0) {
12333 DisplayError(_("Can't back up any further"), 0);
12336 return LoadPosition(lastLoadPositionFP, positionNumber,
12337 lastLoadPositionTitle);
12340 /* Load the nth position from the given file */
12342 LoadPositionFromFile (char *filename, int n, char *title)
12347 if (strcmp(filename, "-") == 0) {
12348 return LoadPosition(stdin, n, "stdin");
12350 f = fopen(filename, "rb");
12352 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
12353 DisplayError(buf, errno);
12356 return LoadPosition(f, n, title);
12361 /* Load the nth position from the given open file, and close it */
12363 LoadPosition (FILE *f, int positionNumber, char *title)
12365 char *p, line[MSG_SIZ];
12366 Board initial_position;
12367 int i, j, fenMode, pn;
12369 if (gameMode == Training )
12370 SetTrainingModeOff();
12372 if (gameMode != BeginningOfGame) {
12373 Reset(FALSE, TRUE);
12375 if (lastLoadPositionFP != NULL && lastLoadPositionFP != f) {
12376 fclose(lastLoadPositionFP);
12378 if (positionNumber == 0) positionNumber = 1;
12379 lastLoadPositionFP = f;
12380 lastLoadPositionNumber = positionNumber;
12381 safeStrCpy(lastLoadPositionTitle, title, sizeof(lastLoadPositionTitle)/sizeof(lastLoadPositionTitle[0]));
12382 if (first.pr == NoProc && !appData.noChessProgram) {
12383 StartChessProgram(&first);
12384 InitChessProgram(&first, FALSE);
12386 pn = positionNumber;
12387 if (positionNumber < 0) {
12388 /* Negative position number means to seek to that byte offset */
12389 if (fseek(f, -positionNumber, 0) == -1) {
12390 DisplayError(_("Can't seek on position file"), 0);
12395 if (fseek(f, 0, 0) == -1) {
12396 if (f == lastLoadPositionFP ?
12397 positionNumber == lastLoadPositionNumber + 1 :
12398 positionNumber == 1) {
12401 DisplayError(_("Can't seek on position file"), 0);
12406 /* See if this file is FEN or old-style xboard */
12407 if (fgets(line, MSG_SIZ, f) == NULL) {
12408 DisplayError(_("Position not found in file"), 0);
12411 // [HGM] FEN can begin with digit, any piece letter valid in this variant, or a + for Shogi promoted pieces
12412 fenMode = line[0] >= '0' && line[0] <= '9' || line[0] == '+' || CharToPiece(line[0]) != EmptySquare;
12415 if (fenMode || line[0] == '#') pn--;
12417 /* skip positions before number pn */
12418 if (fgets(line, MSG_SIZ, f) == NULL) {
12420 DisplayError(_("Position not found in file"), 0);
12423 if (fenMode || line[0] == '#') pn--;
12428 if (!ParseFEN(initial_position, &blackPlaysFirst, line)) {
12429 DisplayError(_("Bad FEN position in file"), 0);
12433 (void) fgets(line, MSG_SIZ, f);
12434 (void) fgets(line, MSG_SIZ, f);
12436 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
12437 (void) fgets(line, MSG_SIZ, f);
12438 for (p = line, j = BOARD_LEFT; j < BOARD_RGHT; p++) {
12441 initial_position[i][j++] = CharToPiece(*p);
12445 blackPlaysFirst = FALSE;
12447 (void) fgets(line, MSG_SIZ, f);
12448 if (strncmp(line, "black", strlen("black"))==0)
12449 blackPlaysFirst = TRUE;
12452 startedFromSetupPosition = TRUE;
12454 CopyBoard(boards[0], initial_position);
12455 if (blackPlaysFirst) {
12456 currentMove = forwardMostMove = backwardMostMove = 1;
12457 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
12458 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
12459 CopyBoard(boards[1], initial_position);
12460 DisplayMessage("", _("Black to play"));
12462 currentMove = forwardMostMove = backwardMostMove = 0;
12463 DisplayMessage("", _("White to play"));
12465 initialRulePlies = FENrulePlies; /* [HGM] copy FEN attributes as well */
12466 if(first.pr != NoProc) { // [HGM] in tourney-mode a position can be loaded before the chess engine is installed
12467 SendToProgram("force\n", &first);
12468 SendBoard(&first, forwardMostMove);
12470 if (appData.debugMode) {
12472 for(i=0;i<2;i++){for(j=0;j<6;j++)fprintf(debugFP, " %d", boards[i][CASTLING][j]);fprintf(debugFP,"\n");}
12473 for(j=0;j<6;j++)fprintf(debugFP, " %d", initialRights[j]);fprintf(debugFP,"\n");
12474 fprintf(debugFP, "Load Position\n");
12477 if (positionNumber > 1) {
12478 snprintf(line, MSG_SIZ, "%s %d", title, positionNumber);
12479 DisplayTitle(line);
12481 DisplayTitle(title);
12483 gameMode = EditGame;
12486 timeRemaining[0][1] = whiteTimeRemaining;
12487 timeRemaining[1][1] = blackTimeRemaining;
12488 DrawPosition(FALSE, boards[currentMove]);
12495 CopyPlayerNameIntoFileName (char **dest, char *src)
12497 while (*src != NULLCHAR && *src != ',') {
12502 *(*dest)++ = *src++;
12508 DefaultFileName (char *ext)
12510 static char def[MSG_SIZ];
12513 if (gameInfo.white != NULL && gameInfo.white[0] != '-') {
12515 CopyPlayerNameIntoFileName(&p, gameInfo.white);
12517 CopyPlayerNameIntoFileName(&p, gameInfo.black);
12519 safeStrCpy(p, ext, MSG_SIZ-2-strlen(gameInfo.white)-strlen(gameInfo.black));
12526 /* Save the current game to the given file */
12528 SaveGameToFile (char *filename, int append)
12532 int result, i, t,tot=0;
12534 if (strcmp(filename, "-") == 0) {
12535 return SaveGame(stdout, 0, NULL);
12537 for(i=0; i<10; i++) { // upto 10 tries
12538 f = fopen(filename, append ? "a" : "w");
12539 if(f && i) fprintf(f, "[Delay \"%d retries, %d msec\"]\n",i,tot);
12540 if(f || errno != 13) break;
12541 DoSleep(t = 5 + random()%11); // wait 5-15 msec
12545 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
12546 DisplayError(buf, errno);
12549 safeStrCpy(buf, lastMsg, MSG_SIZ);
12550 DisplayMessage(_("Waiting for access to save file"), "");
12551 flock(fileno(f), LOCK_EX); // [HGM] lock: lock file while we are writing
12552 DisplayMessage(_("Saving game"), "");
12553 if(lseek(fileno(f), 0, SEEK_END) == -1) DisplayError(_("Bad Seek"), errno); // better safe than sorry...
12554 result = SaveGame(f, 0, NULL);
12555 DisplayMessage(buf, "");
12562 SavePart (char *str)
12564 static char buf[MSG_SIZ];
12567 p = strchr(str, ' ');
12568 if (p == NULL) return str;
12569 strncpy(buf, str, p - str);
12570 buf[p - str] = NULLCHAR;
12574 #define PGN_MAX_LINE 75
12576 #define PGN_SIDE_WHITE 0
12577 #define PGN_SIDE_BLACK 1
12580 FindFirstMoveOutOfBook (int side)
12584 if( backwardMostMove == 0 && ! startedFromSetupPosition) {
12585 int index = backwardMostMove;
12586 int has_book_hit = 0;
12588 if( (index % 2) != side ) {
12592 while( index < forwardMostMove ) {
12593 /* Check to see if engine is in book */
12594 int depth = pvInfoList[index].depth;
12595 int score = pvInfoList[index].score;
12601 else if( score == 0 && depth == 63 ) {
12602 in_book = 1; /* Zappa */
12604 else if( score == 2 && depth == 99 ) {
12605 in_book = 1; /* Abrok */
12608 has_book_hit += in_book;
12624 GetOutOfBookInfo (char * buf)
12628 int offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
12630 oob[0] = FindFirstMoveOutOfBook( PGN_SIDE_WHITE );
12631 oob[1] = FindFirstMoveOutOfBook( PGN_SIDE_BLACK );
12635 if( oob[0] >= 0 || oob[1] >= 0 ) {
12636 for( i=0; i<2; i++ ) {
12640 if( i > 0 && oob[0] >= 0 ) {
12641 strcat( buf, " " );
12644 sprintf( buf+strlen(buf), "%d%s. ", (idx - offset)/2 + 1, idx & 1 ? ".." : "" );
12645 sprintf( buf+strlen(buf), "%s%.2f",
12646 pvInfoList[idx].score >= 0 ? "+" : "",
12647 pvInfoList[idx].score / 100.0 );
12653 /* Save game in PGN style and close the file */
12655 SaveGamePGN (FILE *f)
12657 int i, offset, linelen, newblock;
12660 int movelen, numlen, blank;
12661 char move_buffer[100]; /* [AS] Buffer for move+PV info */
12663 offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
12665 PrintPGNTags(f, &gameInfo);
12667 if(appData.numberTag && matchMode) fprintf(f, "[Number \"%d\"]\n", nextGame+1); // [HGM] number tag
12669 if (backwardMostMove > 0 || startedFromSetupPosition) {
12670 char *fen = PositionToFEN(backwardMostMove, NULL);
12671 fprintf(f, "[FEN \"%s\"]\n[SetUp \"1\"]\n", fen);
12672 fprintf(f, "\n{--------------\n");
12673 PrintPosition(f, backwardMostMove);
12674 fprintf(f, "--------------}\n");
12678 /* [AS] Out of book annotation */
12679 if( appData.saveOutOfBookInfo ) {
12682 GetOutOfBookInfo( buf );
12684 if( buf[0] != '\0' ) {
12685 fprintf( f, "[%s \"%s\"]\n", PGN_OUT_OF_BOOK, buf );
12692 i = backwardMostMove;
12696 while (i < forwardMostMove) {
12697 /* Print comments preceding this move */
12698 if (commentList[i] != NULL) {
12699 if (linelen > 0) fprintf(f, "\n");
12700 fprintf(f, "%s", commentList[i]);
12705 /* Format move number */
12707 snprintf(numtext, sizeof(numtext)/sizeof(numtext[0]),"%d.", (i - offset)/2 + 1);
12710 snprintf(numtext, sizeof(numtext)/sizeof(numtext[0]), "%d...", (i - offset)/2 + 1);
12712 numtext[0] = NULLCHAR;
12714 numlen = strlen(numtext);
12717 /* Print move number */
12718 blank = linelen > 0 && numlen > 0;
12719 if (linelen + (blank ? 1 : 0) + numlen > PGN_MAX_LINE) {
12728 fprintf(f, "%s", numtext);
12732 safeStrCpy(move_buffer, SavePart(parseList[i]), sizeof(move_buffer)/sizeof(move_buffer[0])); // [HGM] pgn: print move via buffer, so it can be edited
12733 movelen = strlen(move_buffer); /* [HGM] pgn: line-break point before move */
12736 blank = linelen > 0 && movelen > 0;
12737 if (linelen + (blank ? 1 : 0) + movelen > PGN_MAX_LINE) {
12746 fprintf(f, "%s", move_buffer);
12747 linelen += movelen;
12749 /* [AS] Add PV info if present */
12750 if( i >= 0 && appData.saveExtendedInfoInPGN && pvInfoList[i].depth > 0 ) {
12751 /* [HGM] add time */
12752 char buf[MSG_SIZ]; int seconds;
12754 seconds = (pvInfoList[i].time+5)/10; // deci-seconds, rounded to nearest
12760 snprintf(buf, MSG_SIZ, " %3.1f%c", seconds/10., 0);
12763 seconds = (seconds + 4)/10; // round to full seconds
12765 snprintf(buf, MSG_SIZ, " %d%c", seconds, 0);
12767 snprintf(buf, MSG_SIZ, " %d:%02d%c", seconds/60, seconds%60, 0);
12770 snprintf( move_buffer, sizeof(move_buffer)/sizeof(move_buffer[0]),"{%s%.2f/%d%s}",
12771 pvInfoList[i].score >= 0 ? "+" : "",
12772 pvInfoList[i].score / 100.0,
12773 pvInfoList[i].depth,
12776 movelen = strlen(move_buffer); /* [HGM] pgn: line-break point after move */
12778 /* Print score/depth */
12779 blank = linelen > 0 && movelen > 0;
12780 if (linelen + (blank ? 1 : 0) + movelen > PGN_MAX_LINE) {
12789 fprintf(f, "%s", move_buffer);
12790 linelen += movelen;
12796 /* Start a new line */
12797 if (linelen > 0) fprintf(f, "\n");
12799 /* Print comments after last move */
12800 if (commentList[i] != NULL) {
12801 fprintf(f, "%s\n", commentList[i]);
12805 if (gameInfo.resultDetails != NULL &&
12806 gameInfo.resultDetails[0] != NULLCHAR) {
12807 fprintf(f, "{%s} %s\n\n", gameInfo.resultDetails,
12808 PGNResult(gameInfo.result));
12810 fprintf(f, "%s\n\n", PGNResult(gameInfo.result));
12814 lastSavedGame = GameCheckSum(); // [HGM] save: remember ID of last saved game to prevent double saving
12818 /* Save game in old style and close the file */
12820 SaveGameOldStyle (FILE *f)
12825 tm = time((time_t *) NULL);
12827 fprintf(f, "# %s game file -- %s", programName, ctime(&tm));
12830 if (backwardMostMove > 0 || startedFromSetupPosition) {
12831 fprintf(f, "\n[--------------\n");
12832 PrintPosition(f, backwardMostMove);
12833 fprintf(f, "--------------]\n");
12838 i = backwardMostMove;
12839 offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
12841 while (i < forwardMostMove) {
12842 if (commentList[i] != NULL) {
12843 fprintf(f, "[%s]\n", commentList[i]);
12846 if ((i % 2) == 1) {
12847 fprintf(f, "%d. ... %s\n", (i - offset)/2 + 1, parseList[i]);
12850 fprintf(f, "%d. %s ", (i - offset)/2 + 1, parseList[i]);
12852 if (commentList[i] != NULL) {
12856 if (i >= forwardMostMove) {
12860 fprintf(f, "%s\n", parseList[i]);
12865 if (commentList[i] != NULL) {
12866 fprintf(f, "[%s]\n", commentList[i]);
12869 /* This isn't really the old style, but it's close enough */
12870 if (gameInfo.resultDetails != NULL &&
12871 gameInfo.resultDetails[0] != NULLCHAR) {
12872 fprintf(f, "%s (%s)\n\n", PGNResult(gameInfo.result),
12873 gameInfo.resultDetails);
12875 fprintf(f, "%s\n\n", PGNResult(gameInfo.result));
12882 /* Save the current game to open file f and close the file */
12884 SaveGame (FILE *f, int dummy, char *dummy2)
12886 if (gameMode == EditPosition) EditPositionDone(TRUE);
12887 lastSavedGame = GameCheckSum(); // [HGM] save: remember ID of last saved game to prevent double saving
12888 if (appData.oldSaveStyle)
12889 return SaveGameOldStyle(f);
12891 return SaveGamePGN(f);
12894 /* Save the current position to the given file */
12896 SavePositionToFile (char *filename)
12901 if (strcmp(filename, "-") == 0) {
12902 return SavePosition(stdout, 0, NULL);
12904 f = fopen(filename, "a");
12906 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
12907 DisplayError(buf, errno);
12910 safeStrCpy(buf, lastMsg, MSG_SIZ);
12911 DisplayMessage(_("Waiting for access to save file"), "");
12912 flock(fileno(f), LOCK_EX); // [HGM] lock
12913 DisplayMessage(_("Saving position"), "");
12914 lseek(fileno(f), 0, SEEK_END); // better safe than sorry...
12915 SavePosition(f, 0, NULL);
12916 DisplayMessage(buf, "");
12922 /* Save the current position to the given open file and close the file */
12924 SavePosition (FILE *f, int dummy, char *dummy2)
12929 if (gameMode == EditPosition) EditPositionDone(TRUE);
12930 if (appData.oldSaveStyle) {
12931 tm = time((time_t *) NULL);
12933 fprintf(f, "# %s position file -- %s", programName, ctime(&tm));
12935 fprintf(f, "[--------------\n");
12936 PrintPosition(f, currentMove);
12937 fprintf(f, "--------------]\n");
12939 fen = PositionToFEN(currentMove, NULL);
12940 fprintf(f, "%s\n", fen);
12948 ReloadCmailMsgEvent (int unregister)
12951 static char *inFilename = NULL;
12952 static char *outFilename;
12954 struct stat inbuf, outbuf;
12957 /* Any registered moves are unregistered if unregister is set, */
12958 /* i.e. invoked by the signal handler */
12960 for (i = 0; i < CMAIL_MAX_GAMES; i ++) {
12961 cmailMoveRegistered[i] = FALSE;
12962 if (cmailCommentList[i] != NULL) {
12963 free(cmailCommentList[i]);
12964 cmailCommentList[i] = NULL;
12967 nCmailMovesRegistered = 0;
12970 for (i = 0; i < CMAIL_MAX_GAMES; i ++) {
12971 cmailResult[i] = CMAIL_NOT_RESULT;
12975 if (inFilename == NULL) {
12976 /* Because the filenames are static they only get malloced once */
12977 /* and they never get freed */
12978 inFilename = (char *) malloc(strlen(appData.cmailGameName) + 9);
12979 sprintf(inFilename, "%s.game.in", appData.cmailGameName);
12981 outFilename = (char *) malloc(strlen(appData.cmailGameName) + 5);
12982 sprintf(outFilename, "%s.out", appData.cmailGameName);
12985 status = stat(outFilename, &outbuf);
12987 cmailMailedMove = FALSE;
12989 status = stat(inFilename, &inbuf);
12990 cmailMailedMove = (inbuf.st_mtime < outbuf.st_mtime);
12993 /* LoadGameFromFile(CMAIL_MAX_GAMES) with cmailMsgLoaded == TRUE
12994 counts the games, notes how each one terminated, etc.
12996 It would be nice to remove this kludge and instead gather all
12997 the information while building the game list. (And to keep it
12998 in the game list nodes instead of having a bunch of fixed-size
12999 parallel arrays.) Note this will require getting each game's
13000 termination from the PGN tags, as the game list builder does
13001 not process the game moves. --mann
13003 cmailMsgLoaded = TRUE;
13004 LoadGameFromFile(inFilename, CMAIL_MAX_GAMES, "", FALSE);
13006 /* Load first game in the file or popup game menu */
13007 LoadGameFromFile(inFilename, 0, appData.cmailGameName, TRUE);
13009 #endif /* !WIN32 */
13017 char string[MSG_SIZ];
13019 if ( cmailMailedMove
13020 || (cmailResult[lastLoadGameNumber - 1] == CMAIL_OLD_RESULT)) {
13021 return TRUE; /* Allow free viewing */
13024 /* Unregister move to ensure that we don't leave RegisterMove */
13025 /* with the move registered when the conditions for registering no */
13027 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
13028 cmailMoveRegistered[lastLoadGameNumber - 1] = FALSE;
13029 nCmailMovesRegistered --;
13031 if (cmailCommentList[lastLoadGameNumber - 1] != NULL)
13033 free(cmailCommentList[lastLoadGameNumber - 1]);
13034 cmailCommentList[lastLoadGameNumber - 1] = NULL;
13038 if (cmailOldMove == -1) {
13039 DisplayError(_("You have edited the game history.\nUse Reload Same Game and make your move again."), 0);
13043 if (currentMove > cmailOldMove + 1) {
13044 DisplayError(_("You have entered too many moves.\nBack up to the correct position and try again."), 0);
13048 if (currentMove < cmailOldMove) {
13049 DisplayError(_("Displayed position is not current.\nStep forward to the correct position and try again."), 0);
13053 if (forwardMostMove > currentMove) {
13054 /* Silently truncate extra moves */
13058 if ( (currentMove == cmailOldMove + 1)
13059 || ( (currentMove == cmailOldMove)
13060 && ( (cmailMoveType[lastLoadGameNumber - 1] == CMAIL_ACCEPT)
13061 || (cmailMoveType[lastLoadGameNumber - 1] == CMAIL_RESIGN)))) {
13062 if (gameInfo.result != GameUnfinished) {
13063 cmailResult[lastLoadGameNumber - 1] = CMAIL_NEW_RESULT;
13066 if (commentList[currentMove] != NULL) {
13067 cmailCommentList[lastLoadGameNumber - 1]
13068 = StrSave(commentList[currentMove]);
13070 safeStrCpy(cmailMove[lastLoadGameNumber - 1], moveList[currentMove - 1], sizeof(cmailMove[lastLoadGameNumber - 1])/sizeof(cmailMove[lastLoadGameNumber - 1][0]));
13072 if (appData.debugMode)
13073 fprintf(debugFP, "Saving %s for game %d\n",
13074 cmailMove[lastLoadGameNumber - 1], lastLoadGameNumber);
13076 snprintf(string, MSG_SIZ, "%s.game.out.%d", appData.cmailGameName, lastLoadGameNumber);
13078 f = fopen(string, "w");
13079 if (appData.oldSaveStyle) {
13080 SaveGameOldStyle(f); /* also closes the file */
13082 snprintf(string, MSG_SIZ, "%s.pos.out", appData.cmailGameName);
13083 f = fopen(string, "w");
13084 SavePosition(f, 0, NULL); /* also closes the file */
13086 fprintf(f, "{--------------\n");
13087 PrintPosition(f, currentMove);
13088 fprintf(f, "--------------}\n\n");
13090 SaveGame(f, 0, NULL); /* also closes the file*/
13093 cmailMoveRegistered[lastLoadGameNumber - 1] = TRUE;
13094 nCmailMovesRegistered ++;
13095 } else if (nCmailGames == 1) {
13096 DisplayError(_("You have not made a move yet"), 0);
13107 static char *partCommandString = "cmail -xv%s -remail -game %s 2>&1";
13108 FILE *commandOutput;
13109 char buffer[MSG_SIZ], msg[MSG_SIZ], string[MSG_SIZ];
13110 int nBytes = 0; /* Suppress warnings on uninitialized variables */
13116 if (! cmailMsgLoaded) {
13117 DisplayError(_("The cmail message is not loaded.\nUse Reload CMail Message and make your move again."), 0);
13121 if (nCmailGames == nCmailResults) {
13122 DisplayError(_("No unfinished games"), 0);
13126 #if CMAIL_PROHIBIT_REMAIL
13127 if (cmailMailedMove) {
13128 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);
13129 DisplayError(msg, 0);
13134 if (! (cmailMailedMove || RegisterMove())) return;
13136 if ( cmailMailedMove
13137 || (nCmailMovesRegistered + nCmailResults == nCmailGames)) {
13138 snprintf(string, MSG_SIZ, partCommandString,
13139 appData.debugMode ? " -v" : "", appData.cmailGameName);
13140 commandOutput = popen(string, "r");
13142 if (commandOutput == NULL) {
13143 DisplayError(_("Failed to invoke cmail"), 0);
13145 for (nBuffers = 0; (! feof(commandOutput)); nBuffers ++) {
13146 nBytes = fread(buffer, 1, MSG_SIZ - 1, commandOutput);
13148 if (nBuffers > 1) {
13149 (void) memcpy(msg, buffer + nBytes, MSG_SIZ - nBytes - 1);
13150 (void) memcpy(msg + MSG_SIZ - nBytes - 1, buffer, nBytes);
13151 nBytes = MSG_SIZ - 1;
13153 (void) memcpy(msg, buffer, nBytes);
13155 *(msg + nBytes) = '\0'; /* \0 for end-of-string*/
13157 if(StrStr(msg, "Mailed cmail message to ") != NULL) {
13158 cmailMailedMove = TRUE; /* Prevent >1 moves */
13161 for (i = 0; i < nCmailGames; i ++) {
13162 if (cmailResult[i] == CMAIL_NOT_RESULT) {
13167 && ( (arcDir = (char *) getenv("CMAIL_ARCDIR"))
13169 snprintf(buffer, MSG_SIZ, "%s/%s.%s.archive",
13171 appData.cmailGameName,
13173 LoadGameFromFile(buffer, 1, buffer, FALSE);
13174 cmailMsgLoaded = FALSE;
13178 DisplayInformation(msg);
13179 pclose(commandOutput);
13182 if ((*cmailMsg) != '\0') {
13183 DisplayInformation(cmailMsg);
13188 #endif /* !WIN32 */
13197 int prependComma = 0;
13199 char string[MSG_SIZ]; /* Space for game-list */
13202 if (!cmailMsgLoaded) return "";
13204 if (cmailMailedMove) {
13205 snprintf(cmailMsg, MSG_SIZ, _("Waiting for reply from opponent\n"));
13207 /* Create a list of games left */
13208 snprintf(string, MSG_SIZ, "[");
13209 for (i = 0; i < nCmailGames; i ++) {
13210 if (! ( cmailMoveRegistered[i]
13211 || (cmailResult[i] == CMAIL_OLD_RESULT))) {
13212 if (prependComma) {
13213 snprintf(number, sizeof(number)/sizeof(number[0]), ",%d", i + 1);
13215 snprintf(number, sizeof(number)/sizeof(number[0]), "%d", i + 1);
13219 strcat(string, number);
13222 strcat(string, "]");
13224 if (nCmailMovesRegistered + nCmailResults == 0) {
13225 switch (nCmailGames) {
13227 snprintf(cmailMsg, MSG_SIZ, _("Still need to make move for game\n"));
13231 snprintf(cmailMsg, MSG_SIZ, _("Still need to make moves for both games\n"));
13235 snprintf(cmailMsg, MSG_SIZ, _("Still need to make moves for all %d games\n"),
13240 switch (nCmailGames - nCmailMovesRegistered - nCmailResults) {
13242 snprintf(cmailMsg, MSG_SIZ, _("Still need to make a move for game %s\n"),
13247 if (nCmailResults == nCmailGames) {
13248 snprintf(cmailMsg, MSG_SIZ, _("No unfinished games\n"));
13250 snprintf(cmailMsg, MSG_SIZ, _("Ready to send mail\n"));
13255 snprintf(cmailMsg, MSG_SIZ, _("Still need to make moves for games %s\n"),
13267 if (gameMode == Training)
13268 SetTrainingModeOff();
13271 cmailMsgLoaded = FALSE;
13272 if (appData.icsActive) {
13273 SendToICS(ics_prefix);
13274 SendToICS("refresh\n");
13279 ExitEvent (int status)
13283 /* Give up on clean exit */
13287 /* Keep trying for clean exit */
13291 if (appData.icsActive && appData.colorize) Colorize(ColorNone, FALSE);
13293 if (telnetISR != NULL) {
13294 RemoveInputSource(telnetISR);
13296 if (icsPR != NoProc) {
13297 DestroyChildProcess(icsPR, TRUE);
13300 /* [HGM] crash: leave writing PGN and position entirely to GameEnds() */
13301 GameEnds(gameInfo.result, gameInfo.resultDetails==NULL ? "xboard exit" : gameInfo.resultDetails, GE_PLAYER);
13303 /* [HGM] crash: the above GameEnds() is a dud if another one was running */
13304 /* make sure this other one finishes before killing it! */
13305 if(endingGame) { int count = 0;
13306 if(appData.debugMode) fprintf(debugFP, "ExitEvent() during GameEnds(), wait\n");
13307 while(endingGame && count++ < 10) DoSleep(1);
13308 if(appData.debugMode && endingGame) fprintf(debugFP, "GameEnds() seems stuck, proceed exiting\n");
13311 /* Kill off chess programs */
13312 if (first.pr != NoProc) {
13315 DoSleep( appData.delayBeforeQuit );
13316 SendToProgram("quit\n", &first);
13317 DoSleep( appData.delayAfterQuit );
13318 DestroyChildProcess(first.pr, 10 /* [AS] first.useSigterm */ );
13320 if (second.pr != NoProc) {
13321 DoSleep( appData.delayBeforeQuit );
13322 SendToProgram("quit\n", &second);
13323 DoSleep( appData.delayAfterQuit );
13324 DestroyChildProcess(second.pr, 10 /* [AS] second.useSigterm */ );
13326 if (first.isr != NULL) {
13327 RemoveInputSource(first.isr);
13329 if (second.isr != NULL) {
13330 RemoveInputSource(second.isr);
13333 if (pairing.pr != NoProc) SendToProgram("quit\n", &pairing);
13334 if (pairing.isr != NULL) RemoveInputSource(pairing.isr);
13336 ShutDownFrontEnd();
13343 if (appData.debugMode)
13344 fprintf(debugFP, "PauseEvent(): pausing %d\n", pausing);
13348 if (gameMode == MachinePlaysWhite ||
13349 gameMode == MachinePlaysBlack) {
13352 DisplayBothClocks();
13354 if (gameMode == PlayFromGameFile) {
13355 if (appData.timeDelay >= 0)
13356 AutoPlayGameLoop();
13357 } else if (gameMode == IcsExamining && pauseExamInvalid) {
13358 Reset(FALSE, TRUE);
13359 SendToICS(ics_prefix);
13360 SendToICS("refresh\n");
13361 } else if (currentMove < forwardMostMove) {
13362 ForwardInner(forwardMostMove);
13364 pauseExamInvalid = FALSE;
13366 switch (gameMode) {
13370 pauseExamForwardMostMove = forwardMostMove;
13371 pauseExamInvalid = FALSE;
13374 case IcsPlayingWhite:
13375 case IcsPlayingBlack:
13379 case PlayFromGameFile:
13380 (void) StopLoadGameTimer();
13384 case BeginningOfGame:
13385 if (appData.icsActive) return;
13386 /* else fall through */
13387 case MachinePlaysWhite:
13388 case MachinePlaysBlack:
13389 case TwoMachinesPlay:
13390 if (forwardMostMove == 0)
13391 return; /* don't pause if no one has moved */
13392 if ((gameMode == MachinePlaysWhite &&
13393 !WhiteOnMove(forwardMostMove)) ||
13394 (gameMode == MachinePlaysBlack &&
13395 WhiteOnMove(forwardMostMove))) {
13407 EditCommentEvent ()
13409 char title[MSG_SIZ];
13411 if (currentMove < 1 || parseList[currentMove - 1][0] == NULLCHAR) {
13412 safeStrCpy(title, _("Edit comment"), sizeof(title)/sizeof(title[0]));
13414 snprintf(title, MSG_SIZ, _("Edit comment on %d.%s%s"), (currentMove - 1) / 2 + 1,
13415 WhiteOnMove(currentMove - 1) ? " " : ".. ",
13416 parseList[currentMove - 1]);
13419 EditCommentPopUp(currentMove, title, commentList[currentMove]);
13426 char *tags = PGNTags(&gameInfo);
13428 EditTagsPopUp(tags, NULL);
13435 if(second.analyzing) {
13436 SendToProgram("exit\n", &second);
13437 second.analyzing = FALSE;
13439 if (second.pr == NoProc) StartChessProgram(&second);
13440 InitChessProgram(&second, FALSE);
13441 FeedMovesToProgram(&second, currentMove);
13443 SendToProgram("analyze\n", &second);
13444 second.analyzing = TRUE;
13449 AnalyzeModeEvent ()
13451 if (gameMode == AnalyzeMode) { ToggleSecond(); return; }
13452 if (appData.noChessProgram || gameMode == AnalyzeMode)
13455 if (gameMode != AnalyzeFile) {
13456 if (!appData.icsEngineAnalyze) {
13458 if (gameMode != EditGame) return;
13460 ResurrectChessProgram();
13461 SendToProgram("analyze\n", &first);
13462 first.analyzing = TRUE;
13463 /*first.maybeThinking = TRUE;*/
13464 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
13465 EngineOutputPopUp();
13467 if (!appData.icsEngineAnalyze) gameMode = AnalyzeMode;
13472 StartAnalysisClock();
13473 GetTimeMark(&lastNodeCountTime);
13478 AnalyzeFileEvent ()
13480 if (appData.noChessProgram || gameMode == AnalyzeFile)
13483 if (gameMode != AnalyzeMode) {
13485 if (gameMode != EditGame) return;
13486 ResurrectChessProgram();
13487 SendToProgram("analyze\n", &first);
13488 first.analyzing = TRUE;
13489 /*first.maybeThinking = TRUE;*/
13490 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
13491 EngineOutputPopUp();
13493 gameMode = AnalyzeFile;
13498 StartAnalysisClock();
13499 GetTimeMark(&lastNodeCountTime);
13501 if(appData.timeDelay > 0) StartLoadGameTimer((long)(1000.0f * appData.timeDelay));
13505 MachineWhiteEvent ()
13508 char *bookHit = NULL;
13510 if (appData.noChessProgram || (gameMode == MachinePlaysWhite))
13514 if (gameMode == PlayFromGameFile ||
13515 gameMode == TwoMachinesPlay ||
13516 gameMode == Training ||
13517 gameMode == AnalyzeMode ||
13518 gameMode == EndOfGame)
13521 if (gameMode == EditPosition)
13522 EditPositionDone(TRUE);
13524 if (!WhiteOnMove(currentMove)) {
13525 DisplayError(_("It is not White's turn"), 0);
13529 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile)
13532 if (gameMode == EditGame || gameMode == AnalyzeMode ||
13533 gameMode == AnalyzeFile)
13536 ResurrectChessProgram(); /* in case it isn't running */
13537 if(gameMode == BeginningOfGame) { /* [HGM] time odds: to get right odds in human mode */
13538 gameMode = MachinePlaysWhite;
13541 gameMode = MachinePlaysWhite;
13545 snprintf(buf, MSG_SIZ, "%s %s %s", gameInfo.white, _("vs."), gameInfo.black);
13547 if (first.sendName) {
13548 snprintf(buf, MSG_SIZ, "name %s\n", gameInfo.black);
13549 SendToProgram(buf, &first);
13551 if (first.sendTime) {
13552 if (first.useColors) {
13553 SendToProgram("black\n", &first); /*gnu kludge*/
13555 SendTimeRemaining(&first, TRUE);
13557 if (first.useColors) {
13558 SendToProgram("white\n", &first); // [HGM] book: send 'go' separately
13560 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: send go or retrieve book move
13561 SetMachineThinkingEnables();
13562 first.maybeThinking = TRUE;
13566 if (appData.autoFlipView && !flipView) {
13567 flipView = !flipView;
13568 DrawPosition(FALSE, NULL);
13569 DisplayBothClocks(); // [HGM] logo: clocks might have to be exchanged;
13572 if(bookHit) { // [HGM] book: simulate book reply
13573 static char bookMove[MSG_SIZ]; // a bit generous?
13575 programStats.nodes = programStats.depth = programStats.time =
13576 programStats.score = programStats.got_only_move = 0;
13577 sprintf(programStats.movelist, "%s (xbook)", bookHit);
13579 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
13580 strcat(bookMove, bookHit);
13581 HandleMachineMove(bookMove, &first);
13586 MachineBlackEvent ()
13589 char *bookHit = NULL;
13591 if (appData.noChessProgram || (gameMode == MachinePlaysBlack))
13595 if (gameMode == PlayFromGameFile ||
13596 gameMode == TwoMachinesPlay ||
13597 gameMode == Training ||
13598 gameMode == AnalyzeMode ||
13599 gameMode == EndOfGame)
13602 if (gameMode == EditPosition)
13603 EditPositionDone(TRUE);
13605 if (WhiteOnMove(currentMove)) {
13606 DisplayError(_("It is not Black's turn"), 0);
13610 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile)
13613 if (gameMode == EditGame || gameMode == AnalyzeMode ||
13614 gameMode == AnalyzeFile)
13617 ResurrectChessProgram(); /* in case it isn't running */
13618 gameMode = MachinePlaysBlack;
13622 snprintf(buf, MSG_SIZ, "%s %s %s", gameInfo.white, _("vs."), gameInfo.black);
13624 if (first.sendName) {
13625 snprintf(buf, MSG_SIZ, "name %s\n", gameInfo.white);
13626 SendToProgram(buf, &first);
13628 if (first.sendTime) {
13629 if (first.useColors) {
13630 SendToProgram("white\n", &first); /*gnu kludge*/
13632 SendTimeRemaining(&first, FALSE);
13634 if (first.useColors) {
13635 SendToProgram("black\n", &first); // [HGM] book: 'go' sent separately
13637 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: send go or retrieve book move
13638 SetMachineThinkingEnables();
13639 first.maybeThinking = TRUE;
13642 if (appData.autoFlipView && flipView) {
13643 flipView = !flipView;
13644 DrawPosition(FALSE, NULL);
13645 DisplayBothClocks(); // [HGM] logo: clocks might have to be exchanged;
13647 if(bookHit) { // [HGM] book: simulate book reply
13648 static char bookMove[MSG_SIZ]; // a bit generous?
13650 programStats.nodes = programStats.depth = programStats.time =
13651 programStats.score = programStats.got_only_move = 0;
13652 sprintf(programStats.movelist, "%s (xbook)", bookHit);
13654 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
13655 strcat(bookMove, bookHit);
13656 HandleMachineMove(bookMove, &first);
13662 DisplayTwoMachinesTitle ()
13665 if (appData.matchGames > 0) {
13666 if(appData.tourneyFile[0]) {
13667 snprintf(buf, MSG_SIZ, "%s %s %s (%d/%d%s)",
13668 gameInfo.white, _("vs."), gameInfo.black,
13669 nextGame+1, appData.matchGames+1,
13670 appData.tourneyType>0 ? "gt" : appData.tourneyType<0 ? "sw" : "rr");
13672 if (first.twoMachinesColor[0] == 'w') {
13673 snprintf(buf, MSG_SIZ, "%s %s %s (%d-%d-%d)",
13674 gameInfo.white, _("vs."), gameInfo.black,
13675 first.matchWins, second.matchWins,
13676 matchGame - 1 - (first.matchWins + second.matchWins));
13678 snprintf(buf, MSG_SIZ, "%s %s %s (%d-%d-%d)",
13679 gameInfo.white, _("vs."), gameInfo.black,
13680 second.matchWins, first.matchWins,
13681 matchGame - 1 - (first.matchWins + second.matchWins));
13684 snprintf(buf, MSG_SIZ, "%s %s %s", gameInfo.white, _("vs."), gameInfo.black);
13690 SettingsMenuIfReady ()
13692 if (second.lastPing != second.lastPong) {
13693 DisplayMessage("", _("Waiting for second chess program"));
13694 ScheduleDelayedEvent(SettingsMenuIfReady, 10); // [HGM] fast: lowered from 1000
13698 DisplayMessage("", "");
13699 SettingsPopUp(&second);
13703 WaitForEngine (ChessProgramState *cps, DelayedEventCallback retry)
13706 if (cps->pr == NoProc) {
13707 StartChessProgram(cps);
13708 if (cps->protocolVersion == 1) {
13711 /* kludge: allow timeout for initial "feature" command */
13713 snprintf(buf, MSG_SIZ, _("Starting %s chess program"), _(cps->which));
13714 DisplayMessage("", buf);
13715 ScheduleDelayedEvent(retry, FEATURE_TIMEOUT);
13723 TwoMachinesEvent P((void))
13727 ChessProgramState *onmove;
13728 char *bookHit = NULL;
13729 static int stalling = 0;
13733 if (appData.noChessProgram) return;
13735 switch (gameMode) {
13736 case TwoMachinesPlay:
13738 case MachinePlaysWhite:
13739 case MachinePlaysBlack:
13740 if (WhiteOnMove(forwardMostMove) == (gameMode == MachinePlaysWhite)) {
13741 DisplayError(_("Wait until your turn,\nor select Move Now"), 0);
13745 case BeginningOfGame:
13746 case PlayFromGameFile:
13749 if (gameMode != EditGame) return;
13752 EditPositionDone(TRUE);
13763 // forwardMostMove = currentMove;
13764 TruncateGame(); // [HGM] vari: MachineWhite and MachineBlack do this...
13766 if(!ResurrectChessProgram()) return; /* in case first program isn't running (unbalances its ping due to InitChessProgram!) */
13768 if(WaitForEngine(&second, TwoMachinesEventIfReady)) return; // (if needed:) started up second engine, so wait for features
13769 if(first.lastPing != first.lastPong) { // [HGM] wait till we are sure first engine has set up position
13770 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10);
13774 if(second.protocolVersion >= 2 && !strstr(second.variants, VariantName(gameInfo.variant))) {
13775 DisplayError("second engine does not play this", 0);
13780 InitChessProgram(&second, FALSE); // unbalances ping of second engine
13781 SendToProgram("force\n", &second);
13783 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10);
13786 GetTimeMark(&now); // [HGM] matchpause: implement match pause after engine load
13787 if(appData.matchPause>10000 || appData.matchPause<10)
13788 appData.matchPause = 10000; /* [HGM] make pause adjustable */
13789 wait = SubtractTimeMarks(&now, &pauseStart);
13790 if(wait < appData.matchPause) {
13791 ScheduleDelayedEvent(TwoMachinesEventIfReady, appData.matchPause - wait);
13794 // we are now committed to starting the game
13796 DisplayMessage("", "");
13797 if (startedFromSetupPosition) {
13798 SendBoard(&second, backwardMostMove);
13799 if (appData.debugMode) {
13800 fprintf(debugFP, "Two Machines\n");
13803 for (i = backwardMostMove; i < forwardMostMove; i++) {
13804 SendMoveToProgram(i, &second);
13807 gameMode = TwoMachinesPlay;
13809 ModeHighlight(); // [HGM] logo: this triggers display update of logos
13811 DisplayTwoMachinesTitle();
13813 if ((first.twoMachinesColor[0] == 'w') == WhiteOnMove(forwardMostMove)) {
13818 if(appData.debugMode) fprintf(debugFP, "New game (%d): %s-%s (%c)\n", matchGame, first.tidy, second.tidy, first.twoMachinesColor[0]);
13819 SendToProgram(first.computerString, &first);
13820 if (first.sendName) {
13821 snprintf(buf, MSG_SIZ, "name %s\n", second.tidy);
13822 SendToProgram(buf, &first);
13824 SendToProgram(second.computerString, &second);
13825 if (second.sendName) {
13826 snprintf(buf, MSG_SIZ, "name %s\n", first.tidy);
13827 SendToProgram(buf, &second);
13831 if (!first.sendTime || !second.sendTime) {
13832 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
13833 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
13835 if (onmove->sendTime) {
13836 if (onmove->useColors) {
13837 SendToProgram(onmove->other->twoMachinesColor, onmove); /*gnu kludge*/
13839 SendTimeRemaining(onmove, WhiteOnMove(forwardMostMove));
13841 if (onmove->useColors) {
13842 SendToProgram(onmove->twoMachinesColor, onmove);
13844 bookHit = SendMoveToBookUser(forwardMostMove-1, onmove, TRUE); // [HGM] book: send go or retrieve book move
13845 // SendToProgram("go\n", onmove);
13846 onmove->maybeThinking = TRUE;
13847 SetMachineThinkingEnables();
13851 if(bookHit) { // [HGM] book: simulate book reply
13852 static char bookMove[MSG_SIZ]; // a bit generous?
13854 programStats.nodes = programStats.depth = programStats.time =
13855 programStats.score = programStats.got_only_move = 0;
13856 sprintf(programStats.movelist, "%s (xbook)", bookHit);
13858 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
13859 strcat(bookMove, bookHit);
13860 savedMessage = bookMove; // args for deferred call
13861 savedState = onmove;
13862 ScheduleDelayedEvent(DeferredBookMove, 1);
13869 if (gameMode == Training) {
13870 SetTrainingModeOff();
13871 gameMode = PlayFromGameFile;
13872 DisplayMessage("", _("Training mode off"));
13874 gameMode = Training;
13875 animateTraining = appData.animate;
13877 /* make sure we are not already at the end of the game */
13878 if (currentMove < forwardMostMove) {
13879 SetTrainingModeOn();
13880 DisplayMessage("", _("Training mode on"));
13882 gameMode = PlayFromGameFile;
13883 DisplayError(_("Already at end of game"), 0);
13892 if (!appData.icsActive) return;
13893 switch (gameMode) {
13894 case IcsPlayingWhite:
13895 case IcsPlayingBlack:
13898 case BeginningOfGame:
13906 EditPositionDone(TRUE);
13919 gameMode = IcsIdle;
13929 switch (gameMode) {
13931 SetTrainingModeOff();
13933 case MachinePlaysWhite:
13934 case MachinePlaysBlack:
13935 case BeginningOfGame:
13936 SendToProgram("force\n", &first);
13937 SetUserThinkingEnables();
13939 case PlayFromGameFile:
13940 (void) StopLoadGameTimer();
13941 if (gameFileFP != NULL) {
13946 EditPositionDone(TRUE);
13951 SendToProgram("force\n", &first);
13953 case TwoMachinesPlay:
13954 GameEnds(EndOfFile, NULL, GE_PLAYER);
13955 ResurrectChessProgram();
13956 SetUserThinkingEnables();
13959 ResurrectChessProgram();
13961 case IcsPlayingBlack:
13962 case IcsPlayingWhite:
13963 DisplayError(_("Warning: You are still playing a game"), 0);
13966 DisplayError(_("Warning: You are still observing a game"), 0);
13969 DisplayError(_("Warning: You are still examining a game"), 0);
13980 first.offeredDraw = second.offeredDraw = 0;
13982 if (gameMode == PlayFromGameFile) {
13983 whiteTimeRemaining = timeRemaining[0][currentMove];
13984 blackTimeRemaining = timeRemaining[1][currentMove];
13988 if (gameMode == MachinePlaysWhite ||
13989 gameMode == MachinePlaysBlack ||
13990 gameMode == TwoMachinesPlay ||
13991 gameMode == EndOfGame) {
13992 i = forwardMostMove;
13993 while (i > currentMove) {
13994 SendToProgram("undo\n", &first);
13997 if(!adjustedClock) {
13998 whiteTimeRemaining = timeRemaining[0][currentMove];
13999 blackTimeRemaining = timeRemaining[1][currentMove];
14000 DisplayBothClocks();
14002 if (whiteFlag || blackFlag) {
14003 whiteFlag = blackFlag = 0;
14008 gameMode = EditGame;
14015 EditPositionEvent ()
14017 if (gameMode == EditPosition) {
14023 if (gameMode != EditGame) return;
14025 gameMode = EditPosition;
14028 if (currentMove > 0)
14029 CopyBoard(boards[0], boards[currentMove]);
14031 blackPlaysFirst = !WhiteOnMove(currentMove);
14033 currentMove = forwardMostMove = backwardMostMove = 0;
14034 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
14036 if(!appData.pieceMenu) DisplayMessage(_("Click clock to clear board"), "");
14042 /* [DM] icsEngineAnalyze - possible call from other functions */
14043 if (appData.icsEngineAnalyze) {
14044 appData.icsEngineAnalyze = FALSE;
14046 DisplayMessage("",_("Close ICS engine analyze..."));
14048 if (first.analysisSupport && first.analyzing) {
14049 SendToBoth("exit\n");
14050 first.analyzing = second.analyzing = FALSE;
14052 thinkOutput[0] = NULLCHAR;
14056 EditPositionDone (Boolean fakeRights)
14058 int king = gameInfo.variant == VariantKnightmate ? WhiteUnicorn : WhiteKing;
14060 startedFromSetupPosition = TRUE;
14061 InitChessProgram(&first, FALSE);
14062 if(fakeRights) { // [HGM] suppress this if we just pasted a FEN.
14063 boards[0][EP_STATUS] = EP_NONE;
14064 boards[0][CASTLING][2] = boards[0][CASTLING][5] = BOARD_WIDTH>>1;
14065 if(boards[0][0][BOARD_WIDTH>>1] == king) {
14066 boards[0][CASTLING][1] = boards[0][0][BOARD_LEFT] == WhiteRook ? BOARD_LEFT : NoRights;
14067 boards[0][CASTLING][0] = boards[0][0][BOARD_RGHT-1] == WhiteRook ? BOARD_RGHT-1 : NoRights;
14068 } else boards[0][CASTLING][2] = NoRights;
14069 if(boards[0][BOARD_HEIGHT-1][BOARD_WIDTH>>1] == WHITE_TO_BLACK king) {
14070 boards[0][CASTLING][4] = boards[0][BOARD_HEIGHT-1][BOARD_LEFT] == BlackRook ? BOARD_LEFT : NoRights;
14071 boards[0][CASTLING][3] = boards[0][BOARD_HEIGHT-1][BOARD_RGHT-1] == BlackRook ? BOARD_RGHT-1 : NoRights;
14072 } else boards[0][CASTLING][5] = NoRights;
14073 if(gameInfo.variant == VariantSChess) {
14075 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // pieces in their original position are assumed virgin
14076 boards[0][VIRGIN][i] = 0;
14077 if(boards[0][0][i] == FIDEArray[0][i-BOARD_LEFT]) boards[0][VIRGIN][i] |= VIRGIN_W;
14078 if(boards[0][BOARD_HEIGHT-1][i] == FIDEArray[1][i-BOARD_LEFT]) boards[0][VIRGIN][i] |= VIRGIN_B;
14082 SendToProgram("force\n", &first);
14083 if (blackPlaysFirst) {
14084 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
14085 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
14086 currentMove = forwardMostMove = backwardMostMove = 1;
14087 CopyBoard(boards[1], boards[0]);
14089 currentMove = forwardMostMove = backwardMostMove = 0;
14091 SendBoard(&first, forwardMostMove);
14092 if (appData.debugMode) {
14093 fprintf(debugFP, "EditPosDone\n");
14096 DisplayMessage("", "");
14097 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
14098 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
14099 gameMode = EditGame;
14101 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
14102 ClearHighlights(); /* [AS] */
14105 /* Pause for `ms' milliseconds */
14106 /* !! Ugh, this is a kludge. Fix it sometime. --tpm */
14108 TimeDelay (long ms)
14115 } while (SubtractTimeMarks(&m2, &m1) < ms);
14118 /* !! Ugh, this is a kludge. Fix it sometime. --tpm */
14120 SendMultiLineToICS (char *buf)
14122 char temp[MSG_SIZ+1], *p;
14129 strncpy(temp, buf, len);
14134 if (*p == '\n' || *p == '\r')
14139 strcat(temp, "\n");
14141 SendToPlayer(temp, strlen(temp));
14145 SetWhiteToPlayEvent ()
14147 if (gameMode == EditPosition) {
14148 blackPlaysFirst = FALSE;
14149 DisplayBothClocks(); /* works because currentMove is 0 */
14150 } else if (gameMode == IcsExamining) {
14151 SendToICS(ics_prefix);
14152 SendToICS("tomove white\n");
14157 SetBlackToPlayEvent ()
14159 if (gameMode == EditPosition) {
14160 blackPlaysFirst = TRUE;
14161 currentMove = 1; /* kludge */
14162 DisplayBothClocks();
14164 } else if (gameMode == IcsExamining) {
14165 SendToICS(ics_prefix);
14166 SendToICS("tomove black\n");
14171 EditPositionMenuEvent (ChessSquare selection, int x, int y)
14174 ChessSquare piece = boards[0][y][x];
14176 if (gameMode != EditPosition && gameMode != IcsExamining) return;
14178 switch (selection) {
14180 if (gameMode == IcsExamining && ics_type == ICS_FICS) {
14181 SendToICS(ics_prefix);
14182 SendToICS("bsetup clear\n");
14183 } else if (gameMode == IcsExamining && ics_type == ICS_ICC) {
14184 SendToICS(ics_prefix);
14185 SendToICS("clearboard\n");
14187 for (x = 0; x < BOARD_WIDTH; x++) { ChessSquare p = EmptySquare;
14188 if(x == BOARD_LEFT-1 || x == BOARD_RGHT) p = (ChessSquare) 0; /* [HGM] holdings */
14189 for (y = 0; y < BOARD_HEIGHT; y++) {
14190 if (gameMode == IcsExamining) {
14191 if (boards[currentMove][y][x] != EmptySquare) {
14192 snprintf(buf, MSG_SIZ, "%sx@%c%c\n", ics_prefix,
14197 boards[0][y][x] = p;
14202 if (gameMode == EditPosition) {
14203 DrawPosition(FALSE, boards[0]);
14208 SetWhiteToPlayEvent();
14212 SetBlackToPlayEvent();
14216 if (gameMode == IcsExamining) {
14217 if (x < BOARD_LEFT || x >= BOARD_RGHT) break; // [HGM] holdings
14218 snprintf(buf, MSG_SIZ, "%sx@%c%c\n", ics_prefix, AAA + x, ONE + y);
14221 if(x < BOARD_LEFT || x >= BOARD_RGHT) {
14222 if(x == BOARD_LEFT-2) {
14223 if(y < BOARD_HEIGHT-1-gameInfo.holdingsSize) break;
14224 boards[0][y][1] = 0;
14226 if(x == BOARD_RGHT+1) {
14227 if(y >= gameInfo.holdingsSize) break;
14228 boards[0][y][BOARD_WIDTH-2] = 0;
14231 boards[0][y][x] = EmptySquare;
14232 DrawPosition(FALSE, boards[0]);
14237 if(piece >= (int)WhitePawn && piece < (int)WhiteMan ||
14238 piece >= (int)BlackPawn && piece < (int)BlackMan ) {
14239 selection = (ChessSquare) (PROMOTED piece);
14240 } else if(piece == EmptySquare) selection = WhiteSilver;
14241 else selection = (ChessSquare)((int)piece - 1);
14245 if(piece > (int)WhiteMan && piece <= (int)WhiteKing ||
14246 piece > (int)BlackMan && piece <= (int)BlackKing ) {
14247 selection = (ChessSquare) (DEMOTED piece);
14248 } else if(piece == EmptySquare) selection = BlackSilver;
14249 else selection = (ChessSquare)((int)piece + 1);
14254 if(gameInfo.variant == VariantShatranj ||
14255 gameInfo.variant == VariantXiangqi ||
14256 gameInfo.variant == VariantCourier ||
14257 gameInfo.variant == VariantMakruk )
14258 selection = (ChessSquare)((int)selection - (int)WhiteQueen + (int)WhiteFerz);
14263 if(gameInfo.variant == VariantXiangqi)
14264 selection = (ChessSquare)((int)selection - (int)WhiteKing + (int)WhiteWazir);
14265 if(gameInfo.variant == VariantKnightmate)
14266 selection = (ChessSquare)((int)selection - (int)WhiteKing + (int)WhiteUnicorn);
14269 if (gameMode == IcsExamining) {
14270 if (x < BOARD_LEFT || x >= BOARD_RGHT) break; // [HGM] holdings
14271 snprintf(buf, MSG_SIZ, "%s%c@%c%c\n", ics_prefix,
14272 PieceToChar(selection), AAA + x, ONE + y);
14275 if(x < BOARD_LEFT || x >= BOARD_RGHT) {
14277 if(x == BOARD_LEFT-2 && selection >= BlackPawn) {
14278 n = PieceToNumber(selection - BlackPawn);
14279 if(n >= gameInfo.holdingsSize) { n = 0; selection = BlackPawn; }
14280 boards[0][BOARD_HEIGHT-1-n][0] = selection;
14281 boards[0][BOARD_HEIGHT-1-n][1]++;
14283 if(x == BOARD_RGHT+1 && selection < BlackPawn) {
14284 n = PieceToNumber(selection);
14285 if(n >= gameInfo.holdingsSize) { n = 0; selection = WhitePawn; }
14286 boards[0][n][BOARD_WIDTH-1] = selection;
14287 boards[0][n][BOARD_WIDTH-2]++;
14290 boards[0][y][x] = selection;
14291 DrawPosition(TRUE, boards[0]);
14293 fromX = fromY = -1;
14301 DropMenuEvent (ChessSquare selection, int x, int y)
14303 ChessMove moveType;
14305 switch (gameMode) {
14306 case IcsPlayingWhite:
14307 case MachinePlaysBlack:
14308 if (!WhiteOnMove(currentMove)) {
14309 DisplayMoveError(_("It is Black's turn"));
14312 moveType = WhiteDrop;
14314 case IcsPlayingBlack:
14315 case MachinePlaysWhite:
14316 if (WhiteOnMove(currentMove)) {
14317 DisplayMoveError(_("It is White's turn"));
14320 moveType = BlackDrop;
14323 moveType = WhiteOnMove(currentMove) ? WhiteDrop : BlackDrop;
14329 if (moveType == BlackDrop && selection < BlackPawn) {
14330 selection = (ChessSquare) ((int) selection
14331 + (int) BlackPawn - (int) WhitePawn);
14333 if (boards[currentMove][y][x] != EmptySquare) {
14334 DisplayMoveError(_("That square is occupied"));
14338 FinishMove(moveType, (int) selection, DROP_RANK, x, y, NULLCHAR);
14344 /* Accept a pending offer of any kind from opponent */
14346 if (appData.icsActive) {
14347 SendToICS(ics_prefix);
14348 SendToICS("accept\n");
14349 } else if (cmailMsgLoaded) {
14350 if (currentMove == cmailOldMove &&
14351 commentList[cmailOldMove] != NULL &&
14352 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
14353 "Black offers a draw" : "White offers a draw")) {
14355 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
14356 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_ACCEPT;
14358 DisplayError(_("There is no pending offer on this move"), 0);
14359 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
14362 /* Not used for offers from chess program */
14369 /* Decline a pending offer of any kind from opponent */
14371 if (appData.icsActive) {
14372 SendToICS(ics_prefix);
14373 SendToICS("decline\n");
14374 } else if (cmailMsgLoaded) {
14375 if (currentMove == cmailOldMove &&
14376 commentList[cmailOldMove] != NULL &&
14377 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
14378 "Black offers a draw" : "White offers a draw")) {
14380 AppendComment(cmailOldMove, "Draw declined", TRUE);
14381 DisplayComment(cmailOldMove - 1, "Draw declined");
14384 DisplayError(_("There is no pending offer on this move"), 0);
14387 /* Not used for offers from chess program */
14394 /* Issue ICS rematch command */
14395 if (appData.icsActive) {
14396 SendToICS(ics_prefix);
14397 SendToICS("rematch\n");
14404 /* Call your opponent's flag (claim a win on time) */
14405 if (appData.icsActive) {
14406 SendToICS(ics_prefix);
14407 SendToICS("flag\n");
14409 switch (gameMode) {
14412 case MachinePlaysWhite:
14415 GameEnds(GameIsDrawn, "Both players ran out of time",
14418 GameEnds(BlackWins, "Black wins on time", GE_PLAYER);
14420 DisplayError(_("Your opponent is not out of time"), 0);
14423 case MachinePlaysBlack:
14426 GameEnds(GameIsDrawn, "Both players ran out of time",
14429 GameEnds(WhiteWins, "White wins on time", GE_PLAYER);
14431 DisplayError(_("Your opponent is not out of time"), 0);
14439 ClockClick (int which)
14440 { // [HGM] code moved to back-end from winboard.c
14441 if(which) { // black clock
14442 if (gameMode == EditPosition || gameMode == IcsExamining) {
14443 if(!appData.pieceMenu && blackPlaysFirst) EditPositionMenuEvent(ClearBoard, 0, 0);
14444 SetBlackToPlayEvent();
14445 } else if ((gameMode == AnalyzeMode || gameMode == EditGame) && !blackFlag && WhiteOnMove(currentMove)) {
14446 UserMoveEvent((int)EmptySquare, DROP_RANK, 0, 0, 0); // [HGM] multi-move: if not out of time, enters null move
14447 } else if (shiftKey) {
14448 AdjustClock(which, -1);
14449 } else if (gameMode == IcsPlayingWhite ||
14450 gameMode == MachinePlaysBlack) {
14453 } else { // white clock
14454 if (gameMode == EditPosition || gameMode == IcsExamining) {
14455 if(!appData.pieceMenu && !blackPlaysFirst) EditPositionMenuEvent(ClearBoard, 0, 0);
14456 SetWhiteToPlayEvent();
14457 } else if ((gameMode == AnalyzeMode || gameMode == EditGame) && !whiteFlag && !WhiteOnMove(currentMove)) {
14458 UserMoveEvent((int)EmptySquare, DROP_RANK, 0, 0, 0); // [HGM] multi-move
14459 } else if (shiftKey) {
14460 AdjustClock(which, -1);
14461 } else if (gameMode == IcsPlayingBlack ||
14462 gameMode == MachinePlaysWhite) {
14471 /* Offer draw or accept pending draw offer from opponent */
14473 if (appData.icsActive) {
14474 /* Note: tournament rules require draw offers to be
14475 made after you make your move but before you punch
14476 your clock. Currently ICS doesn't let you do that;
14477 instead, you immediately punch your clock after making
14478 a move, but you can offer a draw at any time. */
14480 SendToICS(ics_prefix);
14481 SendToICS("draw\n");
14482 userOfferedDraw = TRUE; // [HGM] drawclaim: also set flag in ICS play
14483 } else if (cmailMsgLoaded) {
14484 if (currentMove == cmailOldMove &&
14485 commentList[cmailOldMove] != NULL &&
14486 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
14487 "Black offers a draw" : "White offers a draw")) {
14488 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
14489 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_ACCEPT;
14490 } else if (currentMove == cmailOldMove + 1) {
14491 char *offer = WhiteOnMove(cmailOldMove) ?
14492 "White offers a draw" : "Black offers a draw";
14493 AppendComment(currentMove, offer, TRUE);
14494 DisplayComment(currentMove - 1, offer);
14495 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_DRAW;
14497 DisplayError(_("You must make your move before offering a draw"), 0);
14498 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
14500 } else if (first.offeredDraw) {
14501 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
14503 if (first.sendDrawOffers) {
14504 SendToProgram("draw\n", &first);
14505 userOfferedDraw = TRUE;
14513 /* Offer Adjourn or accept pending Adjourn offer from opponent */
14515 if (appData.icsActive) {
14516 SendToICS(ics_prefix);
14517 SendToICS("adjourn\n");
14519 /* Currently GNU Chess doesn't offer or accept Adjourns */
14527 /* Offer Abort or accept pending Abort offer from opponent */
14529 if (appData.icsActive) {
14530 SendToICS(ics_prefix);
14531 SendToICS("abort\n");
14533 GameEnds(GameUnfinished, "Game aborted", GE_PLAYER);
14540 /* Resign. You can do this even if it's not your turn. */
14542 if (appData.icsActive) {
14543 SendToICS(ics_prefix);
14544 SendToICS("resign\n");
14546 switch (gameMode) {
14547 case MachinePlaysWhite:
14548 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
14550 case MachinePlaysBlack:
14551 GameEnds(BlackWins, "White resigns", GE_PLAYER);
14554 if (cmailMsgLoaded) {
14556 if (WhiteOnMove(cmailOldMove)) {
14557 GameEnds(BlackWins, "White resigns", GE_PLAYER);
14559 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
14561 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_RESIGN;
14572 StopObservingEvent ()
14574 /* Stop observing current games */
14575 SendToICS(ics_prefix);
14576 SendToICS("unobserve\n");
14580 StopExaminingEvent ()
14582 /* Stop observing current game */
14583 SendToICS(ics_prefix);
14584 SendToICS("unexamine\n");
14588 ForwardInner (int target)
14590 int limit; int oldSeekGraphUp = seekGraphUp;
14592 if (appData.debugMode)
14593 fprintf(debugFP, "ForwardInner(%d), current %d, forward %d\n",
14594 target, currentMove, forwardMostMove);
14596 if (gameMode == EditPosition)
14599 seekGraphUp = FALSE;
14600 MarkTargetSquares(1);
14602 if (gameMode == PlayFromGameFile && !pausing)
14605 if (gameMode == IcsExamining && pausing)
14606 limit = pauseExamForwardMostMove;
14608 limit = forwardMostMove;
14610 if (target > limit) target = limit;
14612 if (target > 0 && moveList[target - 1][0]) {
14613 int fromX, fromY, toX, toY;
14614 toX = moveList[target - 1][2] - AAA;
14615 toY = moveList[target - 1][3] - ONE;
14616 if (moveList[target - 1][1] == '@') {
14617 if (appData.highlightLastMove) {
14618 SetHighlights(-1, -1, toX, toY);
14621 fromX = moveList[target - 1][0] - AAA;
14622 fromY = moveList[target - 1][1] - ONE;
14623 if (target == currentMove + 1) {
14624 AnimateMove(boards[currentMove], fromX, fromY, toX, toY);
14626 if (appData.highlightLastMove) {
14627 SetHighlights(fromX, fromY, toX, toY);
14631 if (gameMode == EditGame || gameMode == AnalyzeMode ||
14632 gameMode == Training || gameMode == PlayFromGameFile ||
14633 gameMode == AnalyzeFile) {
14634 while (currentMove < target) {
14635 if(second.analyzing) SendMoveToProgram(currentMove, &second);
14636 SendMoveToProgram(currentMove++, &first);
14639 currentMove = target;
14642 if (gameMode == EditGame || gameMode == EndOfGame) {
14643 whiteTimeRemaining = timeRemaining[0][currentMove];
14644 blackTimeRemaining = timeRemaining[1][currentMove];
14646 DisplayBothClocks();
14647 DisplayMove(currentMove - 1);
14648 DrawPosition(oldSeekGraphUp, boards[currentMove]);
14649 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
14650 if ( !matchMode && gameMode != Training) { // [HGM] PV info: routine tests if empty
14651 DisplayComment(currentMove - 1, commentList[currentMove]);
14653 ClearMap(); // [HGM] exclude: invalidate map
14660 if (gameMode == IcsExamining && !pausing) {
14661 SendToICS(ics_prefix);
14662 SendToICS("forward\n");
14664 ForwardInner(currentMove + 1);
14671 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
14672 /* to optimze, we temporarily turn off analysis mode while we feed
14673 * the remaining moves to the engine. Otherwise we get analysis output
14676 if (first.analysisSupport) {
14677 SendToProgram("exit\nforce\n", &first);
14678 first.analyzing = FALSE;
14682 if (gameMode == IcsExamining && !pausing) {
14683 SendToICS(ics_prefix);
14684 SendToICS("forward 999999\n");
14686 ForwardInner(forwardMostMove);
14689 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
14690 /* we have fed all the moves, so reactivate analysis mode */
14691 SendToProgram("analyze\n", &first);
14692 first.analyzing = TRUE;
14693 /*first.maybeThinking = TRUE;*/
14694 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
14699 BackwardInner (int target)
14701 int full_redraw = TRUE; /* [AS] Was FALSE, had to change it! */
14703 if (appData.debugMode)
14704 fprintf(debugFP, "BackwardInner(%d), current %d, forward %d\n",
14705 target, currentMove, forwardMostMove);
14707 if (gameMode == EditPosition) return;
14708 seekGraphUp = FALSE;
14709 MarkTargetSquares(1);
14710 if (currentMove <= backwardMostMove) {
14712 DrawPosition(full_redraw, boards[currentMove]);
14715 if (gameMode == PlayFromGameFile && !pausing)
14718 if (moveList[target][0]) {
14719 int fromX, fromY, toX, toY;
14720 toX = moveList[target][2] - AAA;
14721 toY = moveList[target][3] - ONE;
14722 if (moveList[target][1] == '@') {
14723 if (appData.highlightLastMove) {
14724 SetHighlights(-1, -1, toX, toY);
14727 fromX = moveList[target][0] - AAA;
14728 fromY = moveList[target][1] - ONE;
14729 if (target == currentMove - 1) {
14730 AnimateMove(boards[currentMove], toX, toY, fromX, fromY);
14732 if (appData.highlightLastMove) {
14733 SetHighlights(fromX, fromY, toX, toY);
14737 if (gameMode == EditGame || gameMode==AnalyzeMode ||
14738 gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
14739 while (currentMove > target) {
14740 if(moveList[currentMove-1][1] == '@' && moveList[currentMove-1][0] == '@') {
14741 // null move cannot be undone. Reload program with move history before it.
14743 for(i=target; i>backwardMostMove; i--) { // seek back to start or previous null move
14744 if(moveList[i-1][1] == '@' && moveList[i-1][0] == '@') break;
14746 SendBoard(&first, i);
14747 if(second.analyzing) SendBoard(&second, i);
14748 for(currentMove=i; currentMove<target; currentMove++) {
14749 SendMoveToProgram(currentMove, &first);
14750 if(second.analyzing) SendMoveToProgram(currentMove, &second);
14754 SendToBoth("undo\n");
14758 currentMove = target;
14761 if (gameMode == EditGame || gameMode == EndOfGame) {
14762 whiteTimeRemaining = timeRemaining[0][currentMove];
14763 blackTimeRemaining = timeRemaining[1][currentMove];
14765 DisplayBothClocks();
14766 DisplayMove(currentMove - 1);
14767 DrawPosition(full_redraw, boards[currentMove]);
14768 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
14769 // [HGM] PV info: routine tests if comment empty
14770 DisplayComment(currentMove - 1, commentList[currentMove]);
14771 ClearMap(); // [HGM] exclude: invalidate map
14777 if (gameMode == IcsExamining && !pausing) {
14778 SendToICS(ics_prefix);
14779 SendToICS("backward\n");
14781 BackwardInner(currentMove - 1);
14788 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
14789 /* to optimize, we temporarily turn off analysis mode while we undo
14790 * all the moves. Otherwise we get analysis output after each undo.
14792 if (first.analysisSupport) {
14793 SendToProgram("exit\nforce\n", &first);
14794 first.analyzing = FALSE;
14798 if (gameMode == IcsExamining && !pausing) {
14799 SendToICS(ics_prefix);
14800 SendToICS("backward 999999\n");
14802 BackwardInner(backwardMostMove);
14805 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
14806 /* we have fed all the moves, so reactivate analysis mode */
14807 SendToProgram("analyze\n", &first);
14808 first.analyzing = TRUE;
14809 /*first.maybeThinking = TRUE;*/
14810 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
14817 if (gameMode == PlayFromGameFile && !pausing) PauseEvent();
14818 if (to >= forwardMostMove) to = forwardMostMove;
14819 if (to <= backwardMostMove) to = backwardMostMove;
14820 if (to < currentMove) {
14828 RevertEvent (Boolean annotate)
14830 if(PopTail(annotate)) { // [HGM] vari: restore old game tail
14833 if (gameMode != IcsExamining) {
14834 DisplayError(_("You are not examining a game"), 0);
14838 DisplayError(_("You can't revert while pausing"), 0);
14841 SendToICS(ics_prefix);
14842 SendToICS("revert\n");
14846 RetractMoveEvent ()
14848 switch (gameMode) {
14849 case MachinePlaysWhite:
14850 case MachinePlaysBlack:
14851 if (WhiteOnMove(forwardMostMove) == (gameMode == MachinePlaysWhite)) {
14852 DisplayError(_("Wait until your turn,\nor select Move Now"), 0);
14855 if (forwardMostMove < 2) return;
14856 currentMove = forwardMostMove = forwardMostMove - 2;
14857 whiteTimeRemaining = timeRemaining[0][currentMove];
14858 blackTimeRemaining = timeRemaining[1][currentMove];
14859 DisplayBothClocks();
14860 DisplayMove(currentMove - 1);
14861 ClearHighlights();/*!! could figure this out*/
14862 DrawPosition(TRUE, boards[currentMove]); /* [AS] Changed to full redraw! */
14863 SendToProgram("remove\n", &first);
14864 /*first.maybeThinking = TRUE;*/ /* GNU Chess does not ponder here */
14867 case BeginningOfGame:
14871 case IcsPlayingWhite:
14872 case IcsPlayingBlack:
14873 if (WhiteOnMove(forwardMostMove) == (gameMode == IcsPlayingWhite)) {
14874 SendToICS(ics_prefix);
14875 SendToICS("takeback 2\n");
14877 SendToICS(ics_prefix);
14878 SendToICS("takeback 1\n");
14887 ChessProgramState *cps;
14889 switch (gameMode) {
14890 case MachinePlaysWhite:
14891 if (!WhiteOnMove(forwardMostMove)) {
14892 DisplayError(_("It is your turn"), 0);
14897 case MachinePlaysBlack:
14898 if (WhiteOnMove(forwardMostMove)) {
14899 DisplayError(_("It is your turn"), 0);
14904 case TwoMachinesPlay:
14905 if (WhiteOnMove(forwardMostMove) ==
14906 (first.twoMachinesColor[0] == 'w')) {
14912 case BeginningOfGame:
14916 SendToProgram("?\n", cps);
14920 TruncateGameEvent ()
14923 if (gameMode != EditGame) return;
14930 CleanupTail(); // [HGM] vari: only keep current variation if we explicitly truncate
14931 if (forwardMostMove > currentMove) {
14932 if (gameInfo.resultDetails != NULL) {
14933 free(gameInfo.resultDetails);
14934 gameInfo.resultDetails = NULL;
14935 gameInfo.result = GameUnfinished;
14937 forwardMostMove = currentMove;
14938 HistorySet(parseList, backwardMostMove, forwardMostMove,
14946 if (appData.noChessProgram) return;
14947 switch (gameMode) {
14948 case MachinePlaysWhite:
14949 if (WhiteOnMove(forwardMostMove)) {
14950 DisplayError(_("Wait until your turn"), 0);
14954 case BeginningOfGame:
14955 case MachinePlaysBlack:
14956 if (!WhiteOnMove(forwardMostMove)) {
14957 DisplayError(_("Wait until your turn"), 0);
14962 DisplayError(_("No hint available"), 0);
14965 SendToProgram("hint\n", &first);
14966 hintRequested = TRUE;
14972 if (appData.noChessProgram) return;
14973 switch (gameMode) {
14974 case MachinePlaysWhite:
14975 if (WhiteOnMove(forwardMostMove)) {
14976 DisplayError(_("Wait until your turn"), 0);
14980 case BeginningOfGame:
14981 case MachinePlaysBlack:
14982 if (!WhiteOnMove(forwardMostMove)) {
14983 DisplayError(_("Wait until your turn"), 0);
14988 EditPositionDone(TRUE);
14990 case TwoMachinesPlay:
14995 SendToProgram("bk\n", &first);
14996 bookOutput[0] = NULLCHAR;
14997 bookRequested = TRUE;
15003 char *tags = PGNTags(&gameInfo);
15004 TagsPopUp(tags, CmailMsg());
15008 /* end button procedures */
15011 PrintPosition (FILE *fp, int move)
15015 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
15016 for (j = BOARD_LEFT; j < BOARD_RGHT; j++) {
15017 char c = PieceToChar(boards[move][i][j]);
15018 fputc(c == 'x' ? '.' : c, fp);
15019 fputc(j == BOARD_RGHT - 1 ? '\n' : ' ', fp);
15022 if ((gameMode == EditPosition) ? !blackPlaysFirst : (move % 2 == 0))
15023 fprintf(fp, "white to play\n");
15025 fprintf(fp, "black to play\n");
15029 PrintOpponents (FILE *fp)
15031 if (gameInfo.white != NULL) {
15032 fprintf(fp, "\t%s vs. %s\n", gameInfo.white, gameInfo.black);
15038 /* Find last component of program's own name, using some heuristics */
15040 TidyProgramName (char *prog, char *host, char buf[MSG_SIZ])
15043 int local = (strcmp(host, "localhost") == 0);
15044 while (!local && (p = strchr(prog, ';')) != NULL) {
15046 while (*p == ' ') p++;
15049 if (*prog == '"' || *prog == '\'') {
15050 q = strchr(prog + 1, *prog);
15052 q = strchr(prog, ' ');
15054 if (q == NULL) q = prog + strlen(prog);
15056 while (p >= prog && *p != '/' && *p != '\\') p--;
15058 if(p == prog && *p == '"') p++;
15060 if (q - p >= 4 && StrCaseCmp(q - 4, ".exe") == 0) *q = c, q -= 4; else *q = c;
15061 memcpy(buf, p, q - p);
15062 buf[q - p] = NULLCHAR;
15070 TimeControlTagValue ()
15073 if (!appData.clockMode) {
15074 safeStrCpy(buf, "-", sizeof(buf)/sizeof(buf[0]));
15075 } else if (movesPerSession > 0) {
15076 snprintf(buf, MSG_SIZ, "%d/%ld", movesPerSession, timeControl/1000);
15077 } else if (timeIncrement == 0) {
15078 snprintf(buf, MSG_SIZ, "%ld", timeControl/1000);
15080 snprintf(buf, MSG_SIZ, "%ld+%ld", timeControl/1000, timeIncrement/1000);
15082 return StrSave(buf);
15088 /* This routine is used only for certain modes */
15089 VariantClass v = gameInfo.variant;
15090 ChessMove r = GameUnfinished;
15093 if(keepInfo) return;
15095 if(gameMode == EditGame) { // [HGM] vari: do not erase result on EditGame
15096 r = gameInfo.result;
15097 p = gameInfo.resultDetails;
15098 gameInfo.resultDetails = NULL;
15100 ClearGameInfo(&gameInfo);
15101 gameInfo.variant = v;
15103 switch (gameMode) {
15104 case MachinePlaysWhite:
15105 gameInfo.event = StrSave( appData.pgnEventHeader );
15106 gameInfo.site = StrSave(HostName());
15107 gameInfo.date = PGNDate();
15108 gameInfo.round = StrSave("-");
15109 gameInfo.white = StrSave(first.tidy);
15110 gameInfo.black = StrSave(UserName());
15111 gameInfo.timeControl = TimeControlTagValue();
15114 case MachinePlaysBlack:
15115 gameInfo.event = StrSave( appData.pgnEventHeader );
15116 gameInfo.site = StrSave(HostName());
15117 gameInfo.date = PGNDate();
15118 gameInfo.round = StrSave("-");
15119 gameInfo.white = StrSave(UserName());
15120 gameInfo.black = StrSave(first.tidy);
15121 gameInfo.timeControl = TimeControlTagValue();
15124 case TwoMachinesPlay:
15125 gameInfo.event = StrSave( appData.pgnEventHeader );
15126 gameInfo.site = StrSave(HostName());
15127 gameInfo.date = PGNDate();
15130 snprintf(buf, MSG_SIZ, "%d", roundNr);
15131 gameInfo.round = StrSave(buf);
15133 gameInfo.round = StrSave("-");
15135 if (first.twoMachinesColor[0] == 'w') {
15136 gameInfo.white = StrSave(appData.pgnName[0][0] ? appData.pgnName[0] : first.tidy);
15137 gameInfo.black = StrSave(appData.pgnName[1][0] ? appData.pgnName[1] : second.tidy);
15139 gameInfo.white = StrSave(appData.pgnName[1][0] ? appData.pgnName[1] : second.tidy);
15140 gameInfo.black = StrSave(appData.pgnName[0][0] ? appData.pgnName[0] : first.tidy);
15142 gameInfo.timeControl = TimeControlTagValue();
15146 gameInfo.event = StrSave("Edited game");
15147 gameInfo.site = StrSave(HostName());
15148 gameInfo.date = PGNDate();
15149 gameInfo.round = StrSave("-");
15150 gameInfo.white = StrSave("-");
15151 gameInfo.black = StrSave("-");
15152 gameInfo.result = r;
15153 gameInfo.resultDetails = p;
15157 gameInfo.event = StrSave("Edited position");
15158 gameInfo.site = StrSave(HostName());
15159 gameInfo.date = PGNDate();
15160 gameInfo.round = StrSave("-");
15161 gameInfo.white = StrSave("-");
15162 gameInfo.black = StrSave("-");
15165 case IcsPlayingWhite:
15166 case IcsPlayingBlack:
15171 case PlayFromGameFile:
15172 gameInfo.event = StrSave("Game from non-PGN file");
15173 gameInfo.site = StrSave(HostName());
15174 gameInfo.date = PGNDate();
15175 gameInfo.round = StrSave("-");
15176 gameInfo.white = StrSave("?");
15177 gameInfo.black = StrSave("?");
15186 ReplaceComment (int index, char *text)
15192 if(index && sscanf(text, "%f/%d", &score, &len) == 2 &&
15193 pvInfoList[index-1].depth == len &&
15194 fabs(pvInfoList[index-1].score - score*100.) < 0.5 &&
15195 (p = strchr(text, '\n'))) text = p; // [HGM] strip off first line with PV info, if any
15196 while (*text == '\n') text++;
15197 len = strlen(text);
15198 while (len > 0 && text[len - 1] == '\n') len--;
15200 if (commentList[index] != NULL)
15201 free(commentList[index]);
15204 commentList[index] = NULL;
15207 if( *text == '{' && strchr(text, '}') || // [HGM] braces: if certainy malformed, put braces
15208 *text == '[' && strchr(text, ']') || // otherwise hope the user knows what he is doing
15209 *text == '(' && strchr(text, ')')) { // (perhaps check if this parses as comment-only?)
15210 commentList[index] = (char *) malloc(len + 2);
15211 strncpy(commentList[index], text, len);
15212 commentList[index][len] = '\n';
15213 commentList[index][len + 1] = NULLCHAR;
15215 // [HGM] braces: if text does not start with known OK delimiter, put braces around it.
15217 commentList[index] = (char *) malloc(len + 7);
15218 safeStrCpy(commentList[index], "{\n", 3);
15219 safeStrCpy(commentList[index]+2, text, len+1);
15220 commentList[index][len+2] = NULLCHAR;
15221 while(p = strchr(commentList[index], '}')) *p = ')'; // kill all } to make it one comment
15222 strcat(commentList[index], "\n}\n");
15227 CrushCRs (char *text)
15235 if (ch == '\r') continue;
15237 } while (ch != '\0');
15241 AppendComment (int index, char *text, Boolean addBraces)
15242 /* addBraces tells if we should add {} */
15247 if(appData.debugMode) fprintf(debugFP, "Append: in='%s' %d\n", text, addBraces); fflush(debugFP);
15248 text = GetInfoFromComment( index, text ); /* [HGM] PV time: strip PV info from comment */
15251 while (*text == '\n') text++;
15252 len = strlen(text);
15253 while (len > 0 && text[len - 1] == '\n') len--;
15254 text[len] = NULLCHAR;
15256 if (len == 0) return;
15258 if (commentList[index] != NULL) {
15259 Boolean addClosingBrace = addBraces;
15260 old = commentList[index];
15261 oldlen = strlen(old);
15262 while(commentList[index][oldlen-1] == '\n')
15263 commentList[index][--oldlen] = NULLCHAR;
15264 commentList[index] = (char *) malloc(oldlen + len + 6); // might waste 4
15265 safeStrCpy(commentList[index], old, oldlen + len + 6);
15267 // [HGM] braces: join "{A\n}\n" + "{\nB}" as "{A\nB\n}"
15268 if(commentList[index][oldlen-1] == '}' && (text[0] == '{' || addBraces == TRUE)) {
15269 if(addBraces == TRUE) addBraces = FALSE; else { text++; len--; }
15270 while (*text == '\n') { text++; len--; }
15271 commentList[index][--oldlen] = NULLCHAR;
15273 if(addBraces) strcat(commentList[index], addBraces == 2 ? "\n(" : "\n{\n");
15274 else strcat(commentList[index], "\n");
15275 strcat(commentList[index], text);
15276 if(addClosingBrace) strcat(commentList[index], addClosingBrace == 2 ? ")\n" : "\n}\n");
15277 else strcat(commentList[index], "\n");
15279 commentList[index] = (char *) malloc(len + 6); // perhaps wastes 4...
15281 safeStrCpy(commentList[index], addBraces == 2 ? "(" : "{\n", 3);
15282 else commentList[index][0] = NULLCHAR;
15283 strcat(commentList[index], text);
15284 strcat(commentList[index], addBraces == 2 ? ")\n" : "\n");
15285 if(addBraces == TRUE) strcat(commentList[index], "}\n");
15290 FindStr (char * text, char * sub_text)
15292 char * result = strstr( text, sub_text );
15294 if( result != NULL ) {
15295 result += strlen( sub_text );
15301 /* [AS] Try to extract PV info from PGN comment */
15302 /* [HGM] PV time: and then remove it, to prevent it appearing twice */
15304 GetInfoFromComment (int index, char * text)
15306 char * sep = text, *p;
15308 if( text != NULL && index > 0 ) {
15311 int time = -1, sec = 0, deci;
15312 char * s_eval = FindStr( text, "[%eval " );
15313 char * s_emt = FindStr( text, "[%emt " );
15315 if( s_eval != NULL || s_emt != NULL ) {
15319 if( s_eval != NULL ) {
15320 if( sscanf( s_eval, "%d,%d%c", &score, &depth, &delim ) != 3 ) {
15324 if( delim != ']' ) {
15329 if( s_emt != NULL ) {
15334 /* We expect something like: [+|-]nnn.nn/dd */
15337 if(*text != '{') return text; // [HGM] braces: must be normal comment
15339 sep = strchr( text, '/' );
15340 if( sep == NULL || sep < (text+4) ) {
15345 if(p[1] == '(') { // comment starts with PV
15346 p = strchr(p, ')'); // locate end of PV
15347 if(p == NULL || sep < p+5) return text;
15348 // at this point we have something like "{(.*) +0.23/6 ..."
15349 p = text; while(*++p != ')') p[-1] = *p; p[-1] = ')';
15350 *p = '\n'; while(*p == ' ' || *p == '\n') p++; *--p = '{';
15351 // we now moved the brace to behind the PV: "(.*) {+0.23/6 ..."
15353 time = -1; sec = -1; deci = -1;
15354 if( sscanf( p+1, "%d.%d/%d %d:%d", &score, &score_lo, &depth, &time, &sec ) != 5 &&
15355 sscanf( p+1, "%d.%d/%d %d.%d", &score, &score_lo, &depth, &time, &deci ) != 5 &&
15356 sscanf( p+1, "%d.%d/%d %d", &score, &score_lo, &depth, &time ) != 4 &&
15357 sscanf( p+1, "%d.%d/%d", &score, &score_lo, &depth ) != 3 ) {
15361 if( score_lo < 0 || score_lo >= 100 ) {
15365 if(sec >= 0) time = 600*time + 10*sec; else
15366 if(deci >= 0) time = 10*time + deci; else time *= 10; // deci-sec
15368 score = score >= 0 ? score*100 + score_lo : score*100 - score_lo;
15370 /* [HGM] PV time: now locate end of PV info */
15371 while( *++sep >= '0' && *sep <= '9'); // strip depth
15373 while( *++sep >= '0' && *sep <= '9' || *sep == '\n'); // strip time
15375 while( *++sep >= '0' && *sep <= '9'); // strip seconds
15377 while( *++sep >= '0' && *sep <= '9'); // strip fractional seconds
15378 while(*sep == ' ' || *sep == '\n' || *sep == '\r') sep++;
15389 pvInfoList[index-1].depth = depth;
15390 pvInfoList[index-1].score = score;
15391 pvInfoList[index-1].time = 10*time; // centi-sec
15392 if(*sep == '}') *sep = 0; else *--sep = '{';
15393 if(p != text) { while(*p++ = *sep++); sep = text; } // squeeze out space between PV and comment, and return both
15399 SendToProgram (char *message, ChessProgramState *cps)
15401 int count, outCount, error;
15404 if (cps->pr == NoProc) return;
15407 if (appData.debugMode) {
15410 fprintf(debugFP, "%ld >%-6s: %s",
15411 SubtractTimeMarks(&now, &programStartTime),
15412 cps->which, message);
15414 fprintf(serverFP, "%ld >%-6s: %s",
15415 SubtractTimeMarks(&now, &programStartTime),
15416 cps->which, message), fflush(serverFP);
15419 count = strlen(message);
15420 outCount = OutputToProcess(cps->pr, message, count, &error);
15421 if (outCount < count && !exiting
15422 && !endingGame) { /* [HGM] crash: to not hang GameEnds() writing to deceased engines */
15423 if(!cps->initDone) return; // [HGM] should not generate fatal error during engine load
15424 snprintf(buf, MSG_SIZ, _("Error writing to %s chess program"), _(cps->which));
15425 if(gameInfo.resultDetails==NULL) { /* [HGM] crash: if game in progress, give reason for abort */
15426 if((signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
15427 snprintf(buf, MSG_SIZ, _("%s program exits in draw position (%s)"), _(cps->which), cps->program);
15428 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(GameIsDrawn, buf, GE_XBOARD); return; }
15429 gameInfo.result = GameIsDrawn; /* [HGM] accept exit as draw claim */
15431 ChessMove res = cps->twoMachinesColor[0]=='w' ? BlackWins : WhiteWins;
15432 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(res, buf, GE_XBOARD); return; }
15433 gameInfo.result = res;
15435 gameInfo.resultDetails = StrSave(buf);
15437 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; return; }
15438 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, error, 1); else errorExitStatus = 1;
15443 ReceiveFromProgram (InputSourceRef isr, VOIDSTAR closure, char *message, int count, int error)
15447 ChessProgramState *cps = (ChessProgramState *)closure;
15449 if (isr != cps->isr) return; /* Killed intentionally */
15452 RemoveInputSource(cps->isr);
15453 snprintf(buf, MSG_SIZ, _("Error: %s chess program (%s) exited unexpectedly"),
15454 _(cps->which), cps->program);
15455 if(LoadError(cps->userError ? NULL : buf, cps)) return; // [HGM] should not generate fatal error during engine load
15456 if(gameInfo.resultDetails==NULL) { /* [HGM] crash: if game in progress, give reason for abort */
15457 if((signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
15458 snprintf(buf, MSG_SIZ, _("%s program exits in draw position (%s)"), _(cps->which), cps->program);
15459 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(GameIsDrawn, buf, GE_XBOARD); return; }
15460 gameInfo.result = GameIsDrawn; /* [HGM] accept exit as draw claim */
15462 ChessMove res = cps->twoMachinesColor[0]=='w' ? BlackWins : WhiteWins;
15463 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(res, buf, GE_XBOARD); return; }
15464 gameInfo.result = res;
15466 gameInfo.resultDetails = StrSave(buf);
15468 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; return; }
15469 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, 0, 1); else errorExitStatus = 1;
15471 snprintf(buf, MSG_SIZ, _("Error reading from %s chess program (%s)"),
15472 _(cps->which), cps->program);
15473 RemoveInputSource(cps->isr);
15475 /* [AS] Program is misbehaving badly... kill it */
15476 if( count == -2 ) {
15477 DestroyChildProcess( cps->pr, 9 );
15481 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, error, 1); else errorExitStatus = 1;
15486 if ((end_str = strchr(message, '\r')) != NULL)
15487 *end_str = NULLCHAR;
15488 if ((end_str = strchr(message, '\n')) != NULL)
15489 *end_str = NULLCHAR;
15491 if (appData.debugMode) {
15492 TimeMark now; int print = 1;
15493 char *quote = ""; char c; int i;
15495 if(appData.engineComments != 1) { /* [HGM] debug: decide if protocol-violating output is written */
15496 char start = message[0];
15497 if(start >='A' && start <= 'Z') start += 'a' - 'A'; // be tolerant to capitalizing
15498 if(sscanf(message, "%d%c%d%d%d", &i, &c, &i, &i, &i) != 5 &&
15499 sscanf(message, "move %c", &c)!=1 && sscanf(message, "offer%c", &c)!=1 &&
15500 sscanf(message, "resign%c", &c)!=1 && sscanf(message, "feature %c", &c)!=1 &&
15501 sscanf(message, "error %c", &c)!=1 && sscanf(message, "illegal %c", &c)!=1 &&
15502 sscanf(message, "tell%c", &c)!=1 && sscanf(message, "0-1 %c", &c)!=1 &&
15503 sscanf(message, "1-0 %c", &c)!=1 && sscanf(message, "1/2-1/2 %c", &c)!=1 &&
15504 sscanf(message, "setboard %c", &c)!=1 && sscanf(message, "setup %c", &c)!=1 &&
15505 sscanf(message, "hint: %c", &c)!=1 &&
15506 sscanf(message, "pong %c", &c)!=1 && start != '#') {
15507 quote = appData.engineComments == 2 ? "# " : "### NON-COMPLIANT! ### ";
15508 print = (appData.engineComments >= 2);
15510 message[0] = start; // restore original message
15514 fprintf(debugFP, "%ld <%-6s: %s%s\n",
15515 SubtractTimeMarks(&now, &programStartTime), cps->which,
15519 fprintf(serverFP, "%ld <%-6s: %s%s\n",
15520 SubtractTimeMarks(&now, &programStartTime), cps->which,
15522 message), fflush(serverFP);
15526 /* [DM] if icsEngineAnalyze is active we block all whisper and kibitz output, because nobody want to see this */
15527 if (appData.icsEngineAnalyze) {
15528 if (strstr(message, "whisper") != NULL ||
15529 strstr(message, "kibitz") != NULL ||
15530 strstr(message, "tellics") != NULL) return;
15533 HandleMachineMove(message, cps);
15538 SendTimeControl (ChessProgramState *cps, int mps, long tc, int inc, int sd, int st)
15543 if( timeControl_2 > 0 ) {
15544 if( (gameMode == MachinePlaysBlack) || (gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b') ) {
15545 tc = timeControl_2;
15548 tc /= cps->timeOdds; /* [HGM] time odds: apply before telling engine */
15549 inc /= cps->timeOdds;
15550 st /= cps->timeOdds;
15552 seconds = (tc / 1000) % 60; /* [HGM] displaced to after applying odds */
15555 /* Set exact time per move, normally using st command */
15556 if (cps->stKludge) {
15557 /* GNU Chess 4 has no st command; uses level in a nonstandard way */
15559 if (seconds == 0) {
15560 snprintf(buf, MSG_SIZ, "level 1 %d\n", st/60);
15562 snprintf(buf, MSG_SIZ, "level 1 %d:%02d\n", st/60, seconds);
15565 snprintf(buf, MSG_SIZ, "st %d\n", st);
15568 /* Set conventional or incremental time control, using level command */
15569 if (seconds == 0) {
15570 /* Note old gnuchess bug -- minutes:seconds used to not work.
15571 Fixed in later versions, but still avoid :seconds
15572 when seconds is 0. */
15573 snprintf(buf, MSG_SIZ, "level %d %ld %g\n", mps, tc/60000, inc/1000.);
15575 snprintf(buf, MSG_SIZ, "level %d %ld:%02d %g\n", mps, tc/60000,
15576 seconds, inc/1000.);
15579 SendToProgram(buf, cps);
15581 /* Orthoganally (except for GNU Chess 4), limit time to st seconds */
15582 /* Orthogonally, limit search to given depth */
15584 if (cps->sdKludge) {
15585 snprintf(buf, MSG_SIZ, "depth\n%d\n", sd);
15587 snprintf(buf, MSG_SIZ, "sd %d\n", sd);
15589 SendToProgram(buf, cps);
15592 if(cps->nps >= 0) { /* [HGM] nps */
15593 if(cps->supportsNPS == FALSE)
15594 cps->nps = -1; // don't use if engine explicitly says not supported!
15596 snprintf(buf, MSG_SIZ, "nps %d\n", cps->nps);
15597 SendToProgram(buf, cps);
15602 ChessProgramState *
15604 /* [HGM] return pointer to 'first' or 'second', depending on who plays white */
15606 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b' ||
15607 gameMode == BeginningOfGame || gameMode == MachinePlaysBlack)
15613 SendTimeRemaining (ChessProgramState *cps, int machineWhite)
15615 char message[MSG_SIZ];
15618 /* Note: this routine must be called when the clocks are stopped
15619 or when they have *just* been set or switched; otherwise
15620 it will be off by the time since the current tick started.
15622 if (machineWhite) {
15623 time = whiteTimeRemaining / 10;
15624 otime = blackTimeRemaining / 10;
15626 time = blackTimeRemaining / 10;
15627 otime = whiteTimeRemaining / 10;
15629 /* [HGM] translate opponent's time by time-odds factor */
15630 otime = (otime * cps->other->timeOdds) / cps->timeOdds;
15632 if (time <= 0) time = 1;
15633 if (otime <= 0) otime = 1;
15635 snprintf(message, MSG_SIZ, "time %ld\n", time);
15636 SendToProgram(message, cps);
15638 snprintf(message, MSG_SIZ, "otim %ld\n", otime);
15639 SendToProgram(message, cps);
15643 BoolFeature (char **p, char *name, int *loc, ChessProgramState *cps)
15646 int len = strlen(name);
15649 if (strncmp((*p), name, len) == 0 && (*p)[len] == '=') {
15651 sscanf(*p, "%d", &val);
15653 while (**p && **p != ' ')
15655 snprintf(buf, MSG_SIZ, "accepted %s\n", name);
15656 SendToProgram(buf, cps);
15663 IntFeature (char **p, char *name, int *loc, ChessProgramState *cps)
15666 int len = strlen(name);
15667 if (strncmp((*p), name, len) == 0 && (*p)[len] == '=') {
15669 sscanf(*p, "%d", loc);
15670 while (**p && **p != ' ') (*p)++;
15671 snprintf(buf, MSG_SIZ, "accepted %s\n", name);
15672 SendToProgram(buf, cps);
15679 StringFeature (char **p, char *name, char loc[], ChessProgramState *cps)
15682 int len = strlen(name);
15683 if (strncmp((*p), name, len) == 0
15684 && (*p)[len] == '=' && (*p)[len+1] == '\"') {
15686 sscanf(*p, "%[^\"]", loc);
15687 while (**p && **p != '\"') (*p)++;
15688 if (**p == '\"') (*p)++;
15689 snprintf(buf, MSG_SIZ, "accepted %s\n", name);
15690 SendToProgram(buf, cps);
15697 ParseOption (Option *opt, ChessProgramState *cps)
15698 // [HGM] options: process the string that defines an engine option, and determine
15699 // name, type, default value, and allowed value range
15701 char *p, *q, buf[MSG_SIZ];
15702 int n, min = (-1)<<31, max = 1<<31, def;
15704 if(p = strstr(opt->name, " -spin ")) {
15705 if((n = sscanf(p, " -spin %d %d %d", &def, &min, &max)) < 3 ) return FALSE;
15706 if(max < min) max = min; // enforce consistency
15707 if(def < min) def = min;
15708 if(def > max) def = max;
15713 } else if((p = strstr(opt->name, " -slider "))) {
15714 // for now -slider is a synonym for -spin, to already provide compatibility with future polyglots
15715 if((n = sscanf(p, " -slider %d %d %d", &def, &min, &max)) < 3 ) return FALSE;
15716 if(max < min) max = min; // enforce consistency
15717 if(def < min) def = min;
15718 if(def > max) def = max;
15722 opt->type = Spin; // Slider;
15723 } else if((p = strstr(opt->name, " -string "))) {
15724 opt->textValue = p+9;
15725 opt->type = TextBox;
15726 } else if((p = strstr(opt->name, " -file "))) {
15727 // for now -file is a synonym for -string, to already provide compatibility with future polyglots
15728 opt->textValue = p+7;
15729 opt->type = FileName; // FileName;
15730 } else if((p = strstr(opt->name, " -path "))) {
15731 // for now -file is a synonym for -string, to already provide compatibility with future polyglots
15732 opt->textValue = p+7;
15733 opt->type = PathName; // PathName;
15734 } else if(p = strstr(opt->name, " -check ")) {
15735 if(sscanf(p, " -check %d", &def) < 1) return FALSE;
15736 opt->value = (def != 0);
15737 opt->type = CheckBox;
15738 } else if(p = strstr(opt->name, " -combo ")) {
15739 opt->textValue = (char*) (opt->choice = &cps->comboList[cps->comboCnt]); // cheat with pointer type
15740 cps->comboList[cps->comboCnt++] = q = p+8; // holds possible choices
15741 if(*q == '*') cps->comboList[cps->comboCnt-1]++;
15742 opt->value = n = 0;
15743 while(q = StrStr(q, " /// ")) {
15744 n++; *q = 0; // count choices, and null-terminate each of them
15746 if(*q == '*') { // remember default, which is marked with * prefix
15750 cps->comboList[cps->comboCnt++] = q;
15752 cps->comboList[cps->comboCnt++] = NULL;
15754 opt->type = ComboBox;
15755 } else if(p = strstr(opt->name, " -button")) {
15756 opt->type = Button;
15757 } else if(p = strstr(opt->name, " -save")) {
15758 opt->type = SaveButton;
15759 } else return FALSE;
15760 *p = 0; // terminate option name
15761 // now look if the command-line options define a setting for this engine option.
15762 if(cps->optionSettings && cps->optionSettings[0])
15763 p = strstr(cps->optionSettings, opt->name); else p = NULL;
15764 if(p && (p == cps->optionSettings || p[-1] == ',')) {
15765 snprintf(buf, MSG_SIZ, "option %s", p);
15766 if(p = strstr(buf, ",")) *p = 0;
15767 if(q = strchr(buf, '=')) switch(opt->type) {
15769 for(n=0; n<opt->max; n++)
15770 if(!strcmp(((char**)opt->textValue)[n], q+1)) opt->value = n;
15773 safeStrCpy(opt->textValue, q+1, MSG_SIZ - (opt->textValue - opt->name));
15777 opt->value = atoi(q+1);
15782 SendToProgram(buf, cps);
15788 FeatureDone (ChessProgramState *cps, int val)
15790 DelayedEventCallback cb = GetDelayedEvent();
15791 if ((cb == InitBackEnd3 && cps == &first) ||
15792 (cb == SettingsMenuIfReady && cps == &second) ||
15793 (cb == LoadEngine) ||
15794 (cb == TwoMachinesEventIfReady)) {
15795 CancelDelayedEvent();
15796 ScheduleDelayedEvent(cb, val ? 1 : 3600000);
15798 cps->initDone = val;
15801 /* Parse feature command from engine */
15803 ParseFeatures (char *args, ChessProgramState *cps)
15811 while (*p == ' ') p++;
15812 if (*p == NULLCHAR) return;
15814 if (BoolFeature(&p, "setboard", &cps->useSetboard, cps)) continue;
15815 if (BoolFeature(&p, "xedit", &cps->extendedEdit, cps)) continue;
15816 if (BoolFeature(&p, "time", &cps->sendTime, cps)) continue;
15817 if (BoolFeature(&p, "draw", &cps->sendDrawOffers, cps)) continue;
15818 if (BoolFeature(&p, "sigint", &cps->useSigint, cps)) continue;
15819 if (BoolFeature(&p, "sigterm", &cps->useSigterm, cps)) continue;
15820 if (BoolFeature(&p, "reuse", &val, cps)) {
15821 /* Engine can disable reuse, but can't enable it if user said no */
15822 if (!val) cps->reuse = FALSE;
15825 if (BoolFeature(&p, "analyze", &cps->analysisSupport, cps)) continue;
15826 if (StringFeature(&p, "myname", cps->tidy, cps)) {
15827 if (gameMode == TwoMachinesPlay) {
15828 DisplayTwoMachinesTitle();
15834 if (StringFeature(&p, "variants", cps->variants, cps)) continue;
15835 if (BoolFeature(&p, "san", &cps->useSAN, cps)) continue;
15836 if (BoolFeature(&p, "ping", &cps->usePing, cps)) continue;
15837 if (BoolFeature(&p, "playother", &cps->usePlayother, cps)) continue;
15838 if (BoolFeature(&p, "colors", &cps->useColors, cps)) continue;
15839 if (BoolFeature(&p, "usermove", &cps->useUsermove, cps)) continue;
15840 if (BoolFeature(&p, "exclude", &cps->excludeMoves, cps)) continue;
15841 if (BoolFeature(&p, "ics", &cps->sendICS, cps)) continue;
15842 if (BoolFeature(&p, "name", &cps->sendName, cps)) continue;
15843 if (BoolFeature(&p, "pause", &val, cps)) continue; /* unused at present */
15844 if (IntFeature(&p, "done", &val, cps)) {
15845 FeatureDone(cps, val);
15848 /* Added by Tord: */
15849 if (BoolFeature(&p, "fen960", &cps->useFEN960, cps)) continue;
15850 if (BoolFeature(&p, "oocastle", &cps->useOOCastle, cps)) continue;
15851 /* End of additions by Tord */
15853 /* [HGM] added features: */
15854 if (BoolFeature(&p, "debug", &cps->debug, cps)) continue;
15855 if (BoolFeature(&p, "nps", &cps->supportsNPS, cps)) continue;
15856 if (IntFeature(&p, "level", &cps->maxNrOfSessions, cps)) continue;
15857 if (BoolFeature(&p, "memory", &cps->memSize, cps)) continue;
15858 if (BoolFeature(&p, "smp", &cps->maxCores, cps)) continue;
15859 if (StringFeature(&p, "egt", cps->egtFormats, cps)) continue;
15860 if (StringFeature(&p, "option", buf, cps)) {
15861 FREE(cps->option[cps->nrOptions].name);
15862 cps->option[cps->nrOptions].name = malloc(MSG_SIZ);
15863 safeStrCpy(cps->option[cps->nrOptions].name, buf, MSG_SIZ);
15864 if(!ParseOption(&(cps->option[cps->nrOptions++]), cps)) { // [HGM] options: add option feature
15865 snprintf(buf, MSG_SIZ, "rejected option %s\n", cps->option[--cps->nrOptions].name);
15866 SendToProgram(buf, cps);
15869 if(cps->nrOptions >= MAX_OPTIONS) {
15871 snprintf(buf, MSG_SIZ, _("%s engine has too many options\n"), _(cps->which));
15872 DisplayError(buf, 0);
15876 /* End of additions by HGM */
15878 /* unknown feature: complain and skip */
15880 while (*q && *q != '=') q++;
15881 snprintf(buf, MSG_SIZ,"rejected %.*s\n", (int)(q-p), p);
15882 SendToProgram(buf, cps);
15888 while (*p && *p != '\"') p++;
15889 if (*p == '\"') p++;
15891 while (*p && *p != ' ') p++;
15899 PeriodicUpdatesEvent (int newState)
15901 if (newState == appData.periodicUpdates)
15904 appData.periodicUpdates=newState;
15906 /* Display type changes, so update it now */
15907 // DisplayAnalysis();
15909 /* Get the ball rolling again... */
15911 AnalysisPeriodicEvent(1);
15912 StartAnalysisClock();
15917 PonderNextMoveEvent (int newState)
15919 if (newState == appData.ponderNextMove) return;
15920 if (gameMode == EditPosition) EditPositionDone(TRUE);
15922 SendToProgram("hard\n", &first);
15923 if (gameMode == TwoMachinesPlay) {
15924 SendToProgram("hard\n", &second);
15927 SendToProgram("easy\n", &first);
15928 thinkOutput[0] = NULLCHAR;
15929 if (gameMode == TwoMachinesPlay) {
15930 SendToProgram("easy\n", &second);
15933 appData.ponderNextMove = newState;
15937 NewSettingEvent (int option, int *feature, char *command, int value)
15941 if (gameMode == EditPosition) EditPositionDone(TRUE);
15942 snprintf(buf, MSG_SIZ,"%s%s %d\n", (option ? "option ": ""), command, value);
15943 if(feature == NULL || *feature) SendToProgram(buf, &first);
15944 if (gameMode == TwoMachinesPlay) {
15945 if(feature == NULL || feature[(int*)&second - (int*)&first]) SendToProgram(buf, &second);
15950 ShowThinkingEvent ()
15951 // [HGM] thinking: this routine is now also called from "Options -> Engine..." popup
15953 static int oldState = 2; // kludge alert! Neither true nor fals, so first time oldState is always updated
15954 int newState = appData.showThinking
15955 // [HGM] thinking: other features now need thinking output as well
15956 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp();
15958 if (oldState == newState) return;
15959 oldState = newState;
15960 if (gameMode == EditPosition) EditPositionDone(TRUE);
15962 SendToProgram("post\n", &first);
15963 if (gameMode == TwoMachinesPlay) {
15964 SendToProgram("post\n", &second);
15967 SendToProgram("nopost\n", &first);
15968 thinkOutput[0] = NULLCHAR;
15969 if (gameMode == TwoMachinesPlay) {
15970 SendToProgram("nopost\n", &second);
15973 // appData.showThinking = newState; // [HGM] thinking: responsible option should already have be changed when calling this routine!
15977 AskQuestionEvent (char *title, char *question, char *replyPrefix, char *which)
15979 ProcRef pr = (which[0] == '1') ? first.pr : second.pr;
15980 if (pr == NoProc) return;
15981 AskQuestion(title, question, replyPrefix, pr);
15985 TypeInEvent (char firstChar)
15987 if ((gameMode == BeginningOfGame && !appData.icsActive) ||
15988 gameMode == MachinePlaysWhite || gameMode == MachinePlaysBlack ||
15989 gameMode == AnalyzeMode || gameMode == EditGame ||
15990 gameMode == EditPosition || gameMode == IcsExamining ||
15991 gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
15992 isdigit(firstChar) && // [HGM] movenum: allow typing in of move nr in 'passive' modes
15993 ( gameMode == AnalyzeFile || gameMode == PlayFromGameFile ||
15994 gameMode == IcsObserving || gameMode == TwoMachinesPlay ) ||
15995 gameMode == Training) PopUpMoveDialog(firstChar);
15999 TypeInDoneEvent (char *move)
16002 int n, fromX, fromY, toX, toY;
16004 ChessMove moveType;
16007 if(gameMode == EditPosition && ParseFEN(board, &n, move) ) {
16008 EditPositionPasteFEN(move);
16011 // [HGM] movenum: allow move number to be typed in any mode
16012 if(sscanf(move, "%d", &n) == 1 && n != 0 ) {
16016 // undocumented kludge: allow command-line option to be typed in!
16017 // (potentially fatal, and does not implement the effect of the option.)
16018 // should only be used for options that are values on which future decisions will be made,
16019 // and definitely not on options that would be used during initialization.
16020 if(strstr(move, "!!! -") == move) {
16021 ParseArgsFromString(move+4);
16025 if (gameMode != EditGame && currentMove != forwardMostMove &&
16026 gameMode != Training) {
16027 DisplayMoveError(_("Displayed move is not current"));
16029 int ok = ParseOneMove(move, gameMode == EditPosition ? blackPlaysFirst : currentMove,
16030 &moveType, &fromX, &fromY, &toX, &toY, &promoChar);
16031 if(!ok && move[0] >= 'a') { move[0] += 'A' - 'a'; ok = 2; } // [HGM] try also capitalized
16032 if (ok==1 || ok && ParseOneMove(move, gameMode == EditPosition ? blackPlaysFirst : currentMove,
16033 &moveType, &fromX, &fromY, &toX, &toY, &promoChar)) {
16034 UserMoveEvent(fromX, fromY, toX, toY, promoChar);
16036 DisplayMoveError(_("Could not parse move"));
16042 DisplayMove (int moveNumber)
16044 char message[MSG_SIZ];
16046 char cpThinkOutput[MSG_SIZ];
16048 if(appData.noGUI) return; // [HGM] fast: suppress display of moves
16050 if (moveNumber == forwardMostMove - 1 ||
16051 gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
16053 safeStrCpy(cpThinkOutput, thinkOutput, sizeof(cpThinkOutput)/sizeof(cpThinkOutput[0]));
16055 if (strchr(cpThinkOutput, '\n')) {
16056 *strchr(cpThinkOutput, '\n') = NULLCHAR;
16059 *cpThinkOutput = NULLCHAR;
16062 /* [AS] Hide thinking from human user */
16063 if( appData.hideThinkingFromHuman && gameMode != TwoMachinesPlay ) {
16064 *cpThinkOutput = NULLCHAR;
16065 if( thinkOutput[0] != NULLCHAR ) {
16068 for( i=0; i<=hiddenThinkOutputState; i++ ) {
16069 cpThinkOutput[i] = '.';
16071 cpThinkOutput[i] = NULLCHAR;
16072 hiddenThinkOutputState = (hiddenThinkOutputState + 1) % 3;
16076 if (moveNumber == forwardMostMove - 1 &&
16077 gameInfo.resultDetails != NULL) {
16078 if (gameInfo.resultDetails[0] == NULLCHAR) {
16079 snprintf(res, MSG_SIZ, " %s", PGNResult(gameInfo.result));
16081 snprintf(res, MSG_SIZ, " {%s} %s",
16082 T_(gameInfo.resultDetails), PGNResult(gameInfo.result));
16088 if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
16089 DisplayMessage(res, cpThinkOutput);
16091 snprintf(message, MSG_SIZ, "%d.%s%s%s", moveNumber / 2 + 1,
16092 WhiteOnMove(moveNumber) ? " " : ".. ",
16093 parseList[moveNumber], res);
16094 DisplayMessage(message, cpThinkOutput);
16099 DisplayComment (int moveNumber, char *text)
16101 char title[MSG_SIZ];
16103 if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
16104 safeStrCpy(title, "Comment", sizeof(title)/sizeof(title[0]));
16106 snprintf(title,MSG_SIZ, "Comment on %d.%s%s", moveNumber / 2 + 1,
16107 WhiteOnMove(moveNumber) ? " " : ".. ",
16108 parseList[moveNumber]);
16110 if (text != NULL && (appData.autoDisplayComment || commentUp))
16111 CommentPopUp(title, text);
16114 /* This routine sends a ^C interrupt to gnuchess, to awaken it if it
16115 * might be busy thinking or pondering. It can be omitted if your
16116 * gnuchess is configured to stop thinking immediately on any user
16117 * input. However, that gnuchess feature depends on the FIONREAD
16118 * ioctl, which does not work properly on some flavors of Unix.
16121 Attention (ChessProgramState *cps)
16124 if (!cps->useSigint) return;
16125 if (appData.noChessProgram || (cps->pr == NoProc)) return;
16126 switch (gameMode) {
16127 case MachinePlaysWhite:
16128 case MachinePlaysBlack:
16129 case TwoMachinesPlay:
16130 case IcsPlayingWhite:
16131 case IcsPlayingBlack:
16134 /* Skip if we know it isn't thinking */
16135 if (!cps->maybeThinking) return;
16136 if (appData.debugMode)
16137 fprintf(debugFP, "Interrupting %s\n", cps->which);
16138 InterruptChildProcess(cps->pr);
16139 cps->maybeThinking = FALSE;
16144 #endif /*ATTENTION*/
16150 if (whiteTimeRemaining <= 0) {
16153 if (appData.icsActive) {
16154 if (appData.autoCallFlag &&
16155 gameMode == IcsPlayingBlack && !blackFlag) {
16156 SendToICS(ics_prefix);
16157 SendToICS("flag\n");
16161 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Both flags fell"));
16163 if(gameMode != TwoMachinesPlay) DisplayTitle(_("White's flag fell"));
16164 if (appData.autoCallFlag) {
16165 GameEnds(BlackWins, "Black wins on time", GE_XBOARD);
16172 if (blackTimeRemaining <= 0) {
16175 if (appData.icsActive) {
16176 if (appData.autoCallFlag &&
16177 gameMode == IcsPlayingWhite && !whiteFlag) {
16178 SendToICS(ics_prefix);
16179 SendToICS("flag\n");
16183 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Both flags fell"));
16185 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Black's flag fell"));
16186 if (appData.autoCallFlag) {
16187 GameEnds(WhiteWins, "White wins on time", GE_XBOARD);
16198 CheckTimeControl ()
16200 if (!appData.clockMode || appData.icsActive || searchTime || // [HGM] st: no inc in st mode
16201 gameMode == PlayFromGameFile || forwardMostMove == 0) return;
16204 * add time to clocks when time control is achieved ([HGM] now also used for increment)
16206 if ( !WhiteOnMove(forwardMostMove) ) {
16207 /* White made time control */
16208 lastWhite -= whiteTimeRemaining; // [HGM] contains start time, socalculate thinking time
16209 whiteTimeRemaining += GetTimeQuota((forwardMostMove-whiteStartMove-1)/2, lastWhite, whiteTC)
16210 /* [HGM] time odds: correct new time quota for time odds! */
16211 / WhitePlayer()->timeOdds;
16212 lastBlack = blackTimeRemaining; // [HGM] leave absolute time (after quota), so next switch we can us it to calculate thinking time
16214 lastBlack -= blackTimeRemaining;
16215 /* Black made time control */
16216 blackTimeRemaining += GetTimeQuota((forwardMostMove-blackStartMove-1)/2, lastBlack, blackTC)
16217 / WhitePlayer()->other->timeOdds;
16218 lastWhite = whiteTimeRemaining;
16223 DisplayBothClocks ()
16225 int wom = gameMode == EditPosition ?
16226 !blackPlaysFirst : WhiteOnMove(currentMove);
16227 DisplayWhiteClock(whiteTimeRemaining, wom);
16228 DisplayBlackClock(blackTimeRemaining, !wom);
16232 /* Timekeeping seems to be a portability nightmare. I think everyone
16233 has ftime(), but I'm really not sure, so I'm including some ifdefs
16234 to use other calls if you don't. Clocks will be less accurate if
16235 you have neither ftime nor gettimeofday.
16238 /* VS 2008 requires the #include outside of the function */
16239 #if !HAVE_GETTIMEOFDAY && HAVE_FTIME
16240 #include <sys/timeb.h>
16243 /* Get the current time as a TimeMark */
16245 GetTimeMark (TimeMark *tm)
16247 #if HAVE_GETTIMEOFDAY
16249 struct timeval timeVal;
16250 struct timezone timeZone;
16252 gettimeofday(&timeVal, &timeZone);
16253 tm->sec = (long) timeVal.tv_sec;
16254 tm->ms = (int) (timeVal.tv_usec / 1000L);
16256 #else /*!HAVE_GETTIMEOFDAY*/
16259 // include <sys/timeb.h> / moved to just above start of function
16260 struct timeb timeB;
16263 tm->sec = (long) timeB.time;
16264 tm->ms = (int) timeB.millitm;
16266 #else /*!HAVE_FTIME && !HAVE_GETTIMEOFDAY*/
16267 tm->sec = (long) time(NULL);
16273 /* Return the difference in milliseconds between two
16274 time marks. We assume the difference will fit in a long!
16277 SubtractTimeMarks (TimeMark *tm2, TimeMark *tm1)
16279 return 1000L*(tm2->sec - tm1->sec) +
16280 (long) (tm2->ms - tm1->ms);
16285 * Code to manage the game clocks.
16287 * In tournament play, black starts the clock and then white makes a move.
16288 * We give the human user a slight advantage if he is playing white---the
16289 * clocks don't run until he makes his first move, so it takes zero time.
16290 * Also, we don't account for network lag, so we could get out of sync
16291 * with GNU Chess's clock -- but then, referees are always right.
16294 static TimeMark tickStartTM;
16295 static long intendedTickLength;
16298 NextTickLength (long timeRemaining)
16300 long nominalTickLength, nextTickLength;
16302 if (timeRemaining > 0L && timeRemaining <= 10000L)
16303 nominalTickLength = 100L;
16305 nominalTickLength = 1000L;
16306 nextTickLength = timeRemaining % nominalTickLength;
16307 if (nextTickLength <= 0) nextTickLength += nominalTickLength;
16309 return nextTickLength;
16312 /* Adjust clock one minute up or down */
16314 AdjustClock (Boolean which, int dir)
16316 if(appData.autoCallFlag) { DisplayError(_("Clock adjustment not allowed in auto-flag mode"), 0); return; }
16317 if(which) blackTimeRemaining += 60000*dir;
16318 else whiteTimeRemaining += 60000*dir;
16319 DisplayBothClocks();
16320 adjustedClock = TRUE;
16323 /* Stop clocks and reset to a fresh time control */
16327 (void) StopClockTimer();
16328 if (appData.icsActive) {
16329 whiteTimeRemaining = blackTimeRemaining = 0;
16330 } else if (searchTime) {
16331 whiteTimeRemaining = 1000*searchTime / WhitePlayer()->timeOdds;
16332 blackTimeRemaining = 1000*searchTime / WhitePlayer()->other->timeOdds;
16333 } else { /* [HGM] correct new time quote for time odds */
16334 whiteTC = blackTC = fullTimeControlString;
16335 whiteTimeRemaining = GetTimeQuota(-1, 0, whiteTC) / WhitePlayer()->timeOdds;
16336 blackTimeRemaining = GetTimeQuota(-1, 0, blackTC) / WhitePlayer()->other->timeOdds;
16338 if (whiteFlag || blackFlag) {
16340 whiteFlag = blackFlag = FALSE;
16342 lastWhite = lastBlack = whiteStartMove = blackStartMove = 0;
16343 DisplayBothClocks();
16344 adjustedClock = FALSE;
16347 #define FUDGE 25 /* 25ms = 1/40 sec; should be plenty even for 50 Hz clocks */
16349 /* Decrement running clock by amount of time that has passed */
16353 long timeRemaining;
16354 long lastTickLength, fudge;
16357 if (!appData.clockMode) return;
16358 if (gameMode==AnalyzeMode || gameMode == AnalyzeFile) return;
16362 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
16364 /* Fudge if we woke up a little too soon */
16365 fudge = intendedTickLength - lastTickLength;
16366 if (fudge < 0 || fudge > FUDGE) fudge = 0;
16368 if (WhiteOnMove(forwardMostMove)) {
16369 if(whiteNPS >= 0) lastTickLength = 0;
16370 timeRemaining = whiteTimeRemaining -= lastTickLength;
16371 if(timeRemaining < 0 && !appData.icsActive) {
16372 GetTimeQuota((forwardMostMove-whiteStartMove-1)/2, 0, whiteTC); // sets suddenDeath & nextSession;
16373 if(suddenDeath) { // [HGM] if we run out of a non-last incremental session, go to the next
16374 whiteStartMove = forwardMostMove; whiteTC = nextSession;
16375 lastWhite= timeRemaining = whiteTimeRemaining += GetTimeQuota(-1, 0, whiteTC);
16378 DisplayWhiteClock(whiteTimeRemaining - fudge,
16379 WhiteOnMove(currentMove < forwardMostMove ? currentMove : forwardMostMove));
16381 if(blackNPS >= 0) lastTickLength = 0;
16382 timeRemaining = blackTimeRemaining -= lastTickLength;
16383 if(timeRemaining < 0 && !appData.icsActive) { // [HGM] if we run out of a non-last incremental session, go to the next
16384 GetTimeQuota((forwardMostMove-blackStartMove-1)/2, 0, blackTC);
16386 blackStartMove = forwardMostMove;
16387 lastBlack = timeRemaining = blackTimeRemaining += GetTimeQuota(-1, 0, blackTC=nextSession);
16390 DisplayBlackClock(blackTimeRemaining - fudge,
16391 !WhiteOnMove(currentMove < forwardMostMove ? currentMove : forwardMostMove));
16393 if (CheckFlags()) return;
16395 if(twoBoards) { // count down secondary board's clocks as well
16396 activePartnerTime -= lastTickLength;
16398 if(activePartner == 'W')
16399 DisplayWhiteClock(activePartnerTime, TRUE); // the counting clock is always the highlighted one!
16401 DisplayBlackClock(activePartnerTime, TRUE);
16406 intendedTickLength = NextTickLength(timeRemaining - fudge) + fudge;
16407 StartClockTimer(intendedTickLength);
16409 /* if the time remaining has fallen below the alarm threshold, sound the
16410 * alarm. if the alarm has sounded and (due to a takeback or time control
16411 * with increment) the time remaining has increased to a level above the
16412 * threshold, reset the alarm so it can sound again.
16415 if (appData.icsActive && appData.icsAlarm) {
16417 /* make sure we are dealing with the user's clock */
16418 if (!( ((gameMode == IcsPlayingWhite) && WhiteOnMove(currentMove)) ||
16419 ((gameMode == IcsPlayingBlack) && !WhiteOnMove(currentMove))
16422 if (alarmSounded && (timeRemaining > appData.icsAlarmTime)) {
16423 alarmSounded = FALSE;
16424 } else if (!alarmSounded && (timeRemaining <= appData.icsAlarmTime)) {
16426 alarmSounded = TRUE;
16432 /* A player has just moved, so stop the previously running
16433 clock and (if in clock mode) start the other one.
16434 We redisplay both clocks in case we're in ICS mode, because
16435 ICS gives us an update to both clocks after every move.
16436 Note that this routine is called *after* forwardMostMove
16437 is updated, so the last fractional tick must be subtracted
16438 from the color that is *not* on move now.
16441 SwitchClocks (int newMoveNr)
16443 long lastTickLength;
16445 int flagged = FALSE;
16449 if (StopClockTimer() && appData.clockMode) {
16450 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
16451 if (!WhiteOnMove(forwardMostMove)) {
16452 if(blackNPS >= 0) lastTickLength = 0;
16453 blackTimeRemaining -= lastTickLength;
16454 /* [HGM] PGNtime: save time for PGN file if engine did not give it */
16455 // if(pvInfoList[forwardMostMove].time == -1)
16456 pvInfoList[forwardMostMove].time = // use GUI time
16457 (timeRemaining[1][forwardMostMove-1] - blackTimeRemaining)/10;
16459 if(whiteNPS >= 0) lastTickLength = 0;
16460 whiteTimeRemaining -= lastTickLength;
16461 /* [HGM] PGNtime: save time for PGN file if engine did not give it */
16462 // if(pvInfoList[forwardMostMove].time == -1)
16463 pvInfoList[forwardMostMove].time =
16464 (timeRemaining[0][forwardMostMove-1] - whiteTimeRemaining)/10;
16466 flagged = CheckFlags();
16468 forwardMostMove = newMoveNr; // [HGM] race: change stm when no timer interrupt scheduled
16469 CheckTimeControl();
16471 if (flagged || !appData.clockMode) return;
16473 switch (gameMode) {
16474 case MachinePlaysBlack:
16475 case MachinePlaysWhite:
16476 case BeginningOfGame:
16477 if (pausing) return;
16481 case PlayFromGameFile:
16489 if (searchTime) { // [HGM] st: set clock of player that has to move to max time
16490 if(WhiteOnMove(forwardMostMove))
16491 whiteTimeRemaining = 1000*searchTime / WhitePlayer()->timeOdds;
16492 else blackTimeRemaining = 1000*searchTime / WhitePlayer()->other->timeOdds;
16496 intendedTickLength = NextTickLength(WhiteOnMove(forwardMostMove) ?
16497 whiteTimeRemaining : blackTimeRemaining);
16498 StartClockTimer(intendedTickLength);
16502 /* Stop both clocks */
16506 long lastTickLength;
16509 if (!StopClockTimer()) return;
16510 if (!appData.clockMode) return;
16514 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
16515 if (WhiteOnMove(forwardMostMove)) {
16516 if(whiteNPS >= 0) lastTickLength = 0;
16517 whiteTimeRemaining -= lastTickLength;
16518 DisplayWhiteClock(whiteTimeRemaining, WhiteOnMove(currentMove));
16520 if(blackNPS >= 0) lastTickLength = 0;
16521 blackTimeRemaining -= lastTickLength;
16522 DisplayBlackClock(blackTimeRemaining, !WhiteOnMove(currentMove));
16527 /* Start clock of player on move. Time may have been reset, so
16528 if clock is already running, stop and restart it. */
16532 (void) StopClockTimer(); /* in case it was running already */
16533 DisplayBothClocks();
16534 if (CheckFlags()) return;
16536 if (!appData.clockMode) return;
16537 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) return;
16539 GetTimeMark(&tickStartTM);
16540 intendedTickLength = NextTickLength(WhiteOnMove(forwardMostMove) ?
16541 whiteTimeRemaining : blackTimeRemaining);
16543 /* [HGM] nps: figure out nps factors, by determining which engine plays white and/or black once and for all */
16544 whiteNPS = blackNPS = -1;
16545 if(gameMode == MachinePlaysWhite || gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w'
16546 || appData.zippyPlay && gameMode == IcsPlayingBlack) // first (perhaps only) engine has white
16547 whiteNPS = first.nps;
16548 if(gameMode == MachinePlaysBlack || gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b'
16549 || appData.zippyPlay && gameMode == IcsPlayingWhite) // first (perhaps only) engine has black
16550 blackNPS = first.nps;
16551 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b') // second only used in Two-Machines mode
16552 whiteNPS = second.nps;
16553 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w')
16554 blackNPS = second.nps;
16555 if(appData.debugMode) fprintf(debugFP, "nps: w=%d, b=%d\n", whiteNPS, blackNPS);
16557 StartClockTimer(intendedTickLength);
16561 TimeString (long ms)
16563 long second, minute, hour, day;
16565 static char buf[32];
16567 if (ms > 0 && ms <= 9900) {
16568 /* convert milliseconds to tenths, rounding up */
16569 double tenths = floor( ((double)(ms + 99L)) / 100.00 );
16571 snprintf(buf,sizeof(buf)/sizeof(buf[0]), " %03.1f ", tenths/10.0);
16575 /* convert milliseconds to seconds, rounding up */
16576 /* use floating point to avoid strangeness of integer division
16577 with negative dividends on many machines */
16578 second = (long) floor(((double) (ms + 999L)) / 1000.0);
16585 day = second / (60 * 60 * 24);
16586 second = second % (60 * 60 * 24);
16587 hour = second / (60 * 60);
16588 second = second % (60 * 60);
16589 minute = second / 60;
16590 second = second % 60;
16593 snprintf(buf, sizeof(buf)/sizeof(buf[0]), " %s%ld:%02ld:%02ld:%02ld ",
16594 sign, day, hour, minute, second);
16596 snprintf(buf, sizeof(buf)/sizeof(buf[0]), " %s%ld:%02ld:%02ld ", sign, hour, minute, second);
16598 snprintf(buf, sizeof(buf)/sizeof(buf[0]), " %s%2ld:%02ld ", sign, minute, second);
16605 * This is necessary because some C libraries aren't ANSI C compliant yet.
16608 StrStr (char *string, char *match)
16612 length = strlen(match);
16614 for (i = strlen(string) - length; i >= 0; i--, string++)
16615 if (!strncmp(match, string, length))
16622 StrCaseStr (char *string, char *match)
16626 length = strlen(match);
16628 for (i = strlen(string) - length; i >= 0; i--, string++) {
16629 for (j = 0; j < length; j++) {
16630 if (ToLower(match[j]) != ToLower(string[j]))
16633 if (j == length) return string;
16641 StrCaseCmp (char *s1, char *s2)
16646 c1 = ToLower(*s1++);
16647 c2 = ToLower(*s2++);
16648 if (c1 > c2) return 1;
16649 if (c1 < c2) return -1;
16650 if (c1 == NULLCHAR) return 0;
16658 return isupper(c) ? tolower(c) : c;
16665 return islower(c) ? toupper(c) : c;
16667 #endif /* !_amigados */
16674 if ((ret = (char *) malloc(strlen(s) + 1)))
16676 safeStrCpy(ret, s, strlen(s)+1);
16682 StrSavePtr (char *s, char **savePtr)
16687 if ((*savePtr = (char *) malloc(strlen(s) + 1))) {
16688 safeStrCpy(*savePtr, s, strlen(s)+1);
16700 clock = time((time_t *)NULL);
16701 tm = localtime(&clock);
16702 snprintf(buf, MSG_SIZ, "%04d.%02d.%02d",
16703 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
16704 return StrSave(buf);
16709 PositionToFEN (int move, char *overrideCastling)
16711 int i, j, fromX, fromY, toX, toY;
16718 whiteToPlay = (gameMode == EditPosition) ?
16719 !blackPlaysFirst : (move % 2 == 0);
16722 /* Piece placement data */
16723 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
16724 if(MSG_SIZ - (p - buf) < BOARD_RGHT - BOARD_LEFT + 20) { *p = 0; return StrSave(buf); }
16726 for (j = BOARD_LEFT; j < BOARD_RGHT; j++) {
16727 if (boards[move][i][j] == EmptySquare) {
16729 } else { ChessSquare piece = boards[move][i][j];
16730 if (emptycount > 0) {
16731 if(emptycount<10) /* [HGM] can be >= 10 */
16732 *p++ = '0' + emptycount;
16733 else { *p++ = '0' + emptycount/10; *p++ = '0' + emptycount%10; }
16736 if(PieceToChar(piece) == '+') {
16737 /* [HGM] write promoted pieces as '+<unpromoted>' (Shogi) */
16739 piece = (ChessSquare)(DEMOTED piece);
16741 *p++ = PieceToChar(piece);
16743 /* [HGM] flag promoted pieces as '<promoted>~' (Crazyhouse) */
16744 p[-1] = PieceToChar((ChessSquare)(DEMOTED piece));
16749 if (emptycount > 0) {
16750 if(emptycount<10) /* [HGM] can be >= 10 */
16751 *p++ = '0' + emptycount;
16752 else { *p++ = '0' + emptycount/10; *p++ = '0' + emptycount%10; }
16759 /* [HGM] print Crazyhouse or Shogi holdings */
16760 if( gameInfo.holdingsWidth ) {
16761 *(p-1) = '['; /* if we wanted to support BFEN, this could be '/' */
16763 for(i=0; i<gameInfo.holdingsSize; i++) { /* white holdings */
16764 piece = boards[move][i][BOARD_WIDTH-1];
16765 if( piece != EmptySquare )
16766 for(j=0; j<(int) boards[move][i][BOARD_WIDTH-2]; j++)
16767 *p++ = PieceToChar(piece);
16769 for(i=0; i<gameInfo.holdingsSize; i++) { /* black holdings */
16770 piece = boards[move][BOARD_HEIGHT-i-1][0];
16771 if( piece != EmptySquare )
16772 for(j=0; j<(int) boards[move][BOARD_HEIGHT-i-1][1]; j++)
16773 *p++ = PieceToChar(piece);
16776 if( q == p ) *p++ = '-';
16782 *p++ = whiteToPlay ? 'w' : 'b';
16785 if(q = overrideCastling) { // [HGM] FRC: override castling & e.p fields for non-compliant engines
16786 while(*p++ = *q++); if(q != overrideCastling+1) p[-1] = ' '; else --p;
16788 if(nrCastlingRights) {
16790 if(gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom) {
16791 /* [HGM] write directly from rights */
16792 if(boards[move][CASTLING][2] != NoRights &&
16793 boards[move][CASTLING][0] != NoRights )
16794 *p++ = boards[move][CASTLING][0] + AAA + 'A' - 'a';
16795 if(boards[move][CASTLING][2] != NoRights &&
16796 boards[move][CASTLING][1] != NoRights )
16797 *p++ = boards[move][CASTLING][1] + AAA + 'A' - 'a';
16798 if(boards[move][CASTLING][5] != NoRights &&
16799 boards[move][CASTLING][3] != NoRights )
16800 *p++ = boards[move][CASTLING][3] + AAA;
16801 if(boards[move][CASTLING][5] != NoRights &&
16802 boards[move][CASTLING][4] != NoRights )
16803 *p++ = boards[move][CASTLING][4] + AAA;
16806 /* [HGM] write true castling rights */
16807 if( nrCastlingRights == 6 ) {
16809 if(boards[move][CASTLING][0] == BOARD_RGHT-1 &&
16810 boards[move][CASTLING][2] != NoRights ) k = 1, *p++ = 'K';
16811 q = (boards[move][CASTLING][1] == BOARD_LEFT &&
16812 boards[move][CASTLING][2] != NoRights );
16813 if(gameInfo.variant == VariantSChess) { // for S-Chess, indicate all vrgin backrank pieces
16814 for(i=j=0; i<BOARD_HEIGHT; i++) j += boards[move][i][BOARD_RGHT]; // count white held pieces
16815 for(i=BOARD_RGHT-1-k; i>=BOARD_LEFT+q && j; i--)
16816 if((boards[move][0][i] != WhiteKing || k+q == 0) &&
16817 boards[move][VIRGIN][i] & VIRGIN_W) *p++ = i + AAA + 'A' - 'a';
16821 if(boards[move][CASTLING][3] == BOARD_RGHT-1 &&
16822 boards[move][CASTLING][5] != NoRights ) k = 1, *p++ = 'k';
16823 q = (boards[move][CASTLING][4] == BOARD_LEFT &&
16824 boards[move][CASTLING][5] != NoRights );
16825 if(gameInfo.variant == VariantSChess) {
16826 for(i=j=0; i<BOARD_HEIGHT; i++) j += boards[move][i][BOARD_LEFT-1]; // count black held pieces
16827 for(i=BOARD_RGHT-1-k; i>=BOARD_LEFT+q && j; i--)
16828 if((boards[move][BOARD_HEIGHT-1][i] != BlackKing || k+q == 0) &&
16829 boards[move][VIRGIN][i] & VIRGIN_B) *p++ = i + AAA;
16834 if (q == p) *p++ = '-'; /* No castling rights */
16838 if(gameInfo.variant != VariantShogi && gameInfo.variant != VariantXiangqi &&
16839 gameInfo.variant != VariantShatranj && gameInfo.variant != VariantCourier && gameInfo.variant != VariantMakruk ) {
16840 /* En passant target square */
16841 if (move > backwardMostMove) {
16842 fromX = moveList[move - 1][0] - AAA;
16843 fromY = moveList[move - 1][1] - ONE;
16844 toX = moveList[move - 1][2] - AAA;
16845 toY = moveList[move - 1][3] - ONE;
16846 if (fromY == (whiteToPlay ? BOARD_HEIGHT-2 : 1) &&
16847 toY == (whiteToPlay ? BOARD_HEIGHT-4 : 3) &&
16848 boards[move][toY][toX] == (whiteToPlay ? BlackPawn : WhitePawn) &&
16850 /* 2-square pawn move just happened */
16852 *p++ = whiteToPlay ? '6'+BOARD_HEIGHT-8 : '3';
16856 } else if(move == backwardMostMove) {
16857 // [HGM] perhaps we should always do it like this, and forget the above?
16858 if((signed char)boards[move][EP_STATUS] >= 0) {
16859 *p++ = boards[move][EP_STATUS] + AAA;
16860 *p++ = whiteToPlay ? '6'+BOARD_HEIGHT-8 : '3';
16871 /* [HGM] find reversible plies */
16872 { int i = 0, j=move;
16874 if (appData.debugMode) { int k;
16875 fprintf(debugFP, "write FEN 50-move: %d %d %d\n", initialRulePlies, forwardMostMove, backwardMostMove);
16876 for(k=backwardMostMove; k<=forwardMostMove; k++)
16877 fprintf(debugFP, "e%d. p=%d\n", k, (signed char)boards[k][EP_STATUS]);
16881 while(j > backwardMostMove && (signed char)boards[j][EP_STATUS] <= EP_NONE) j--,i++;
16882 if( j == backwardMostMove ) i += initialRulePlies;
16883 sprintf(p, "%d ", i);
16884 p += i>=100 ? 4 : i >= 10 ? 3 : 2;
16886 /* Fullmove number */
16887 sprintf(p, "%d", (move / 2) + 1);
16889 return StrSave(buf);
16893 ParseFEN (Board board, int *blackPlaysFirst, char *fen)
16897 int emptycount, virgin[BOARD_FILES];
16902 /* [HGM] by default clear Crazyhouse holdings, if present */
16903 if(gameInfo.holdingsWidth) {
16904 for(i=0; i<BOARD_HEIGHT; i++) {
16905 board[i][0] = EmptySquare; /* black holdings */
16906 board[i][BOARD_WIDTH-1] = EmptySquare; /* white holdings */
16907 board[i][1] = (ChessSquare) 0; /* black counts */
16908 board[i][BOARD_WIDTH-2] = (ChessSquare) 0; /* white counts */
16912 /* Piece placement data */
16913 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
16916 if (*p == '/' || *p == ' ' || (*p == '[' && i == 0) ) {
16917 if (*p == '/') p++;
16918 emptycount = gameInfo.boardWidth - j;
16919 while (emptycount--)
16920 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
16922 #if(BOARD_FILES >= 10)
16923 } else if(*p=='x' || *p=='X') { /* [HGM] X means 10 */
16924 p++; emptycount=10;
16925 if (j + emptycount > gameInfo.boardWidth) return FALSE;
16926 while (emptycount--)
16927 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
16929 } else if (isdigit(*p)) {
16930 emptycount = *p++ - '0';
16931 while(isdigit(*p)) emptycount = 10*emptycount + *p++ - '0'; /* [HGM] allow > 9 */
16932 if (j + emptycount > gameInfo.boardWidth) return FALSE;
16933 while (emptycount--)
16934 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
16935 } else if (*p == '+' || isalpha(*p)) {
16936 if (j >= gameInfo.boardWidth) return FALSE;
16938 piece = CharToPiece(*++p);
16939 if(piece == EmptySquare) return FALSE; /* unknown piece */
16940 piece = (ChessSquare) (PROMOTED piece ); p++;
16941 if(PieceToChar(piece) != '+') return FALSE; /* unpromotable piece */
16942 } else piece = CharToPiece(*p++);
16944 if(piece==EmptySquare) return FALSE; /* unknown piece */
16945 if(*p == '~') { /* [HGM] make it a promoted piece for Crazyhouse */
16946 piece = (ChessSquare) (PROMOTED piece);
16947 if(PieceToChar(piece) != '~') return FALSE; /* cannot be a promoted piece */
16950 board[i][(j++)+gameInfo.holdingsWidth] = piece;
16956 while (*p == '/' || *p == ' ') p++;
16958 /* [HGM] look for Crazyhouse holdings here */
16959 while(*p==' ') p++;
16960 if( gameInfo.holdingsWidth && p[-1] == '/' || *p == '[') {
16962 if(*p == '-' ) p++; /* empty holdings */ else {
16963 if( !gameInfo.holdingsWidth ) return FALSE; /* no room to put holdings! */
16964 /* if we would allow FEN reading to set board size, we would */
16965 /* have to add holdings and shift the board read so far here */
16966 while( (piece = CharToPiece(*p) ) != EmptySquare ) {
16968 if((int) piece >= (int) BlackPawn ) {
16969 i = (int)piece - (int)BlackPawn;
16970 i = PieceToNumber((ChessSquare)i);
16971 if( i >= gameInfo.holdingsSize ) return FALSE;
16972 board[BOARD_HEIGHT-1-i][0] = piece; /* black holdings */
16973 board[BOARD_HEIGHT-1-i][1]++; /* black counts */
16975 i = (int)piece - (int)WhitePawn;
16976 i = PieceToNumber((ChessSquare)i);
16977 if( i >= gameInfo.holdingsSize ) return FALSE;
16978 board[i][BOARD_WIDTH-1] = piece; /* white holdings */
16979 board[i][BOARD_WIDTH-2]++; /* black holdings */
16986 while(*p == ' ') p++;
16990 if(appData.colorNickNames) {
16991 if( c == appData.colorNickNames[0] ) c = 'w'; else
16992 if( c == appData.colorNickNames[1] ) c = 'b';
16996 *blackPlaysFirst = FALSE;
16999 *blackPlaysFirst = TRUE;
17005 /* [HGM] We NO LONGER ignore the rest of the FEN notation */
17006 /* return the extra info in global variiables */
17008 /* set defaults in case FEN is incomplete */
17009 board[EP_STATUS] = EP_UNKNOWN;
17010 for(i=0; i<nrCastlingRights; i++ ) {
17011 board[CASTLING][i] =
17012 gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom ? NoRights : initialRights[i];
17013 } /* assume possible unless obviously impossible */
17014 if(initialRights[0]!=NoRights && board[castlingRank[0]][initialRights[0]] != WhiteRook) board[CASTLING][0] = NoRights;
17015 if(initialRights[1]!=NoRights && board[castlingRank[1]][initialRights[1]] != WhiteRook) board[CASTLING][1] = NoRights;
17016 if(initialRights[2]!=NoRights && board[castlingRank[2]][initialRights[2]] != WhiteUnicorn
17017 && board[castlingRank[2]][initialRights[2]] != WhiteKing) board[CASTLING][2] = NoRights;
17018 if(initialRights[3]!=NoRights && board[castlingRank[3]][initialRights[3]] != BlackRook) board[CASTLING][3] = NoRights;
17019 if(initialRights[4]!=NoRights && board[castlingRank[4]][initialRights[4]] != BlackRook) board[CASTLING][4] = NoRights;
17020 if(initialRights[5]!=NoRights && board[castlingRank[5]][initialRights[5]] != BlackUnicorn
17021 && board[castlingRank[5]][initialRights[5]] != BlackKing) board[CASTLING][5] = NoRights;
17024 while(*p==' ') p++;
17025 if(nrCastlingRights) {
17026 if(gameInfo.variant == VariantSChess) for(i=0; i<BOARD_FILES; i++) virgin[i] = 0;
17027 if(*p >= 'A' && *p <= 'Z' || *p >= 'a' && *p <= 'z' || *p=='-') {
17028 /* castling indicator present, so default becomes no castlings */
17029 for(i=0; i<nrCastlingRights; i++ ) {
17030 board[CASTLING][i] = NoRights;
17033 while(*p=='K' || *p=='Q' || *p=='k' || *p=='q' || *p=='-' ||
17034 (gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom || gameInfo.variant == VariantSChess) &&
17035 ( *p >= 'a' && *p < 'a' + gameInfo.boardWidth) ||
17036 ( *p >= 'A' && *p < 'A' + gameInfo.boardWidth) ) {
17037 int c = *p++, whiteKingFile=NoRights, blackKingFile=NoRights;
17039 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) {
17040 if(board[BOARD_HEIGHT-1][i] == BlackKing) blackKingFile = i;
17041 if(board[0 ][i] == WhiteKing) whiteKingFile = i;
17043 if(gameInfo.variant == VariantTwoKings || gameInfo.variant == VariantKnightmate)
17044 whiteKingFile = blackKingFile = BOARD_WIDTH >> 1; // for these variant scanning fails
17045 if(whiteKingFile == NoRights || board[0][whiteKingFile] != WhiteUnicorn
17046 && board[0][whiteKingFile] != WhiteKing) whiteKingFile = NoRights;
17047 if(blackKingFile == NoRights || board[BOARD_HEIGHT-1][blackKingFile] != BlackUnicorn
17048 && board[BOARD_HEIGHT-1][blackKingFile] != BlackKing) blackKingFile = NoRights;
17051 for(i=BOARD_RGHT-1; board[0][i]!=WhiteRook && i>whiteKingFile; i--);
17052 board[CASTLING][0] = i != whiteKingFile ? i : NoRights;
17053 board[CASTLING][2] = whiteKingFile;
17054 if(board[CASTLING][0] != NoRights) virgin[board[CASTLING][0]] |= VIRGIN_W;
17055 if(board[CASTLING][2] != NoRights) virgin[board[CASTLING][2]] |= VIRGIN_W;
17058 for(i=BOARD_LEFT; i<BOARD_RGHT && board[0][i]!=WhiteRook && i<whiteKingFile; i++);
17059 board[CASTLING][1] = i != whiteKingFile ? i : NoRights;
17060 board[CASTLING][2] = whiteKingFile;
17061 if(board[CASTLING][1] != NoRights) virgin[board[CASTLING][1]] |= VIRGIN_W;
17062 if(board[CASTLING][2] != NoRights) virgin[board[CASTLING][2]] |= VIRGIN_W;
17065 for(i=BOARD_RGHT-1; board[BOARD_HEIGHT-1][i]!=BlackRook && i>blackKingFile; i--);
17066 board[CASTLING][3] = i != blackKingFile ? i : NoRights;
17067 board[CASTLING][5] = blackKingFile;
17068 if(board[CASTLING][3] != NoRights) virgin[board[CASTLING][3]] |= VIRGIN_B;
17069 if(board[CASTLING][5] != NoRights) virgin[board[CASTLING][5]] |= VIRGIN_B;
17072 for(i=BOARD_LEFT; i<BOARD_RGHT && board[BOARD_HEIGHT-1][i]!=BlackRook && i<blackKingFile; i++);
17073 board[CASTLING][4] = i != blackKingFile ? i : NoRights;
17074 board[CASTLING][5] = blackKingFile;
17075 if(board[CASTLING][4] != NoRights) virgin[board[CASTLING][4]] |= VIRGIN_B;
17076 if(board[CASTLING][5] != NoRights) virgin[board[CASTLING][5]] |= VIRGIN_B;
17079 default: /* FRC castlings */
17080 if(c >= 'a') { /* black rights */
17081 if(gameInfo.variant == VariantSChess) { virgin[c-AAA] |= VIRGIN_B; break; } // in S-Chess castlings are always kq, so just virginity
17082 for(i=BOARD_LEFT; i<BOARD_RGHT; i++)
17083 if(board[BOARD_HEIGHT-1][i] == BlackKing) break;
17084 if(i == BOARD_RGHT) break;
17085 board[CASTLING][5] = i;
17087 if(board[BOARD_HEIGHT-1][c] < BlackPawn ||
17088 board[BOARD_HEIGHT-1][c] >= BlackKing ) break;
17090 board[CASTLING][3] = c;
17092 board[CASTLING][4] = c;
17093 } else { /* white rights */
17094 if(gameInfo.variant == VariantSChess) { virgin[c-AAA-'A'+'a'] |= VIRGIN_W; break; } // in S-Chess castlings are always KQ
17095 for(i=BOARD_LEFT; i<BOARD_RGHT; i++)
17096 if(board[0][i] == WhiteKing) break;
17097 if(i == BOARD_RGHT) break;
17098 board[CASTLING][2] = i;
17099 c -= AAA - 'a' + 'A';
17100 if(board[0][c] >= WhiteKing) break;
17102 board[CASTLING][0] = c;
17104 board[CASTLING][1] = c;
17108 for(i=0; i<nrCastlingRights; i++)
17109 if(board[CASTLING][i] != NoRights) initialRights[i] = board[CASTLING][i];
17110 if(gameInfo.variant == VariantSChess) for(i=0; i<BOARD_FILES; i++) board[VIRGIN][i] = virgin[i];
17111 if (appData.debugMode) {
17112 fprintf(debugFP, "FEN castling rights:");
17113 for(i=0; i<nrCastlingRights; i++)
17114 fprintf(debugFP, " %d", board[CASTLING][i]);
17115 fprintf(debugFP, "\n");
17118 while(*p==' ') p++;
17121 /* read e.p. field in games that know e.p. capture */
17122 if(gameInfo.variant != VariantShogi && gameInfo.variant != VariantXiangqi &&
17123 gameInfo.variant != VariantShatranj && gameInfo.variant != VariantCourier && gameInfo.variant != VariantMakruk ) {
17125 p++; board[EP_STATUS] = EP_NONE;
17127 char c = *p++ - AAA;
17129 if(c < BOARD_LEFT || c >= BOARD_RGHT) return TRUE;
17130 if(*p >= '0' && *p <='9') p++;
17131 board[EP_STATUS] = c;
17136 if(sscanf(p, "%d", &i) == 1) {
17137 FENrulePlies = i; /* 50-move ply counter */
17138 /* (The move number is still ignored) */
17145 EditPositionPasteFEN (char *fen)
17148 Board initial_position;
17150 if (!ParseFEN(initial_position, &blackPlaysFirst, fen)) {
17151 DisplayError(_("Bad FEN position in clipboard"), 0);
17154 int savedBlackPlaysFirst = blackPlaysFirst;
17155 EditPositionEvent();
17156 blackPlaysFirst = savedBlackPlaysFirst;
17157 CopyBoard(boards[0], initial_position);
17158 initialRulePlies = FENrulePlies; /* [HGM] copy FEN attributes as well */
17159 EditPositionDone(FALSE); // [HGM] fake: do not fake rights if we had FEN
17160 DisplayBothClocks();
17161 DrawPosition(FALSE, boards[currentMove]);
17166 static char cseq[12] = "\\ ";
17169 set_cont_sequence (char *new_seq)
17174 // handle bad attempts to set the sequence
17176 return 0; // acceptable error - no debug
17178 len = strlen(new_seq);
17179 ret = (len > 0) && (len < sizeof(cseq));
17181 safeStrCpy(cseq, new_seq, sizeof(cseq)/sizeof(cseq[0]));
17182 else if (appData.debugMode)
17183 fprintf(debugFP, "Invalid continuation sequence \"%s\" (maximum length is: %u)\n", new_seq, (unsigned) sizeof(cseq)-1);
17188 reformat a source message so words don't cross the width boundary. internal
17189 newlines are not removed. returns the wrapped size (no null character unless
17190 included in source message). If dest is NULL, only calculate the size required
17191 for the dest buffer. lp argument indicats line position upon entry, and it's
17192 passed back upon exit.
17195 wrap (char *dest, char *src, int count, int width, int *lp)
17197 int len, i, ansi, cseq_len, line, old_line, old_i, old_len, clen;
17199 cseq_len = strlen(cseq);
17200 old_line = line = *lp;
17201 ansi = len = clen = 0;
17203 for (i=0; i < count; i++)
17205 if (src[i] == '\033')
17208 // if we hit the width, back up
17209 if (!ansi && (line >= width) && src[i] != '\n' && src[i] != ' ')
17211 // store i & len in case the word is too long
17212 old_i = i, old_len = len;
17214 // find the end of the last word
17215 while (i && src[i] != ' ' && src[i] != '\n')
17221 // word too long? restore i & len before splitting it
17222 if ((old_i-i+clen) >= width)
17229 if (i && src[i-1] == ' ')
17232 if (src[i] != ' ' && src[i] != '\n')
17239 // now append the newline and continuation sequence
17244 strncpy(dest+len, cseq, cseq_len);
17252 dest[len] = src[i];
17256 if (src[i] == '\n')
17261 if (dest && appData.debugMode)
17263 fprintf(debugFP, "wrap(count:%d,width:%d,line:%d,len:%d,*lp:%d,src: ",
17264 count, width, line, len, *lp);
17265 show_bytes(debugFP, src, count);
17266 fprintf(debugFP, "\ndest: ");
17267 show_bytes(debugFP, dest, len);
17268 fprintf(debugFP, "\n");
17270 *lp = dest ? line : old_line;
17275 // [HGM] vari: routines for shelving variations
17276 Boolean modeRestore = FALSE;
17279 PushInner (int firstMove, int lastMove)
17281 int i, j, nrMoves = lastMove - firstMove;
17283 // push current tail of game on stack
17284 savedResult[storedGames] = gameInfo.result;
17285 savedDetails[storedGames] = gameInfo.resultDetails;
17286 gameInfo.resultDetails = NULL;
17287 savedFirst[storedGames] = firstMove;
17288 savedLast [storedGames] = lastMove;
17289 savedFramePtr[storedGames] = framePtr;
17290 framePtr -= nrMoves; // reserve space for the boards
17291 for(i=nrMoves; i>=1; i--) { // copy boards to stack, working downwards, in case of overlap
17292 CopyBoard(boards[framePtr+i], boards[firstMove+i]);
17293 for(j=0; j<MOVE_LEN; j++)
17294 moveList[framePtr+i][j] = moveList[firstMove+i-1][j];
17295 for(j=0; j<2*MOVE_LEN; j++)
17296 parseList[framePtr+i][j] = parseList[firstMove+i-1][j];
17297 timeRemaining[0][framePtr+i] = timeRemaining[0][firstMove+i];
17298 timeRemaining[1][framePtr+i] = timeRemaining[1][firstMove+i];
17299 pvInfoList[framePtr+i] = pvInfoList[firstMove+i-1];
17300 pvInfoList[firstMove+i-1].depth = 0;
17301 commentList[framePtr+i] = commentList[firstMove+i];
17302 commentList[firstMove+i] = NULL;
17306 forwardMostMove = firstMove; // truncate game so we can start variation
17310 PushTail (int firstMove, int lastMove)
17312 if(appData.icsActive) { // only in local mode
17313 forwardMostMove = currentMove; // mimic old ICS behavior
17316 if(storedGames >= MAX_VARIATIONS-2) return; // leave one for PV-walk
17318 PushInner(firstMove, lastMove);
17319 if(storedGames == 1) GreyRevert(FALSE);
17320 if(gameMode == PlayFromGameFile) gameMode = EditGame, modeRestore = TRUE;
17324 PopInner (Boolean annotate)
17327 char buf[8000], moveBuf[20];
17329 ToNrEvent(savedFirst[storedGames-1]); // sets currentMove
17330 storedGames--; // do this after ToNrEvent, to make sure HistorySet will refresh entire game after PopInner returns
17331 nrMoves = savedLast[storedGames] - currentMove;
17334 if(!WhiteOnMove(currentMove))
17335 snprintf(buf, sizeof(buf)/sizeof(buf[0]),"(%d...", (currentMove+2)>>1);
17336 else safeStrCpy(buf, "(", sizeof(buf)/sizeof(buf[0]));
17337 for(i=currentMove; i<forwardMostMove; i++) {
17339 snprintf(moveBuf, sizeof(moveBuf)/sizeof(moveBuf[0]), " %d. %s", (i+2)>>1, SavePart(parseList[i]));
17340 else snprintf(moveBuf, sizeof(moveBuf)/sizeof(moveBuf[0])," %s", SavePart(parseList[i]));
17341 strcat(buf, moveBuf);
17342 if(commentList[i]) { strcat(buf, " "); strcat(buf, commentList[i]); }
17343 if(!--cnt) { strcat(buf, "\n"); cnt = 10; }
17347 for(i=1; i<=nrMoves; i++) { // copy last variation back
17348 CopyBoard(boards[currentMove+i], boards[framePtr+i]);
17349 for(j=0; j<MOVE_LEN; j++)
17350 moveList[currentMove+i-1][j] = moveList[framePtr+i][j];
17351 for(j=0; j<2*MOVE_LEN; j++)
17352 parseList[currentMove+i-1][j] = parseList[framePtr+i][j];
17353 timeRemaining[0][currentMove+i] = timeRemaining[0][framePtr+i];
17354 timeRemaining[1][currentMove+i] = timeRemaining[1][framePtr+i];
17355 pvInfoList[currentMove+i-1] = pvInfoList[framePtr+i];
17356 if(commentList[currentMove+i]) free(commentList[currentMove+i]);
17357 commentList[currentMove+i] = commentList[framePtr+i];
17358 commentList[framePtr+i] = NULL;
17360 if(annotate) AppendComment(currentMove+1, buf, FALSE);
17361 framePtr = savedFramePtr[storedGames];
17362 gameInfo.result = savedResult[storedGames];
17363 if(gameInfo.resultDetails != NULL) {
17364 free(gameInfo.resultDetails);
17366 gameInfo.resultDetails = savedDetails[storedGames];
17367 forwardMostMove = currentMove + nrMoves;
17371 PopTail (Boolean annotate)
17373 if(appData.icsActive) return FALSE; // only in local mode
17374 if(!storedGames) return FALSE; // sanity
17375 CommentPopDown(); // make sure no stale variation comments to the destroyed line can remain open
17377 PopInner(annotate);
17378 if(currentMove < forwardMostMove) ForwardEvent(); else
17379 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
17381 if(storedGames == 0) { GreyRevert(TRUE); if(modeRestore) modeRestore = FALSE, gameMode = PlayFromGameFile; }
17387 { // remove all shelved variations
17389 for(i=0; i<storedGames; i++) {
17390 if(savedDetails[i])
17391 free(savedDetails[i]);
17392 savedDetails[i] = NULL;
17394 for(i=framePtr; i<MAX_MOVES; i++) {
17395 if(commentList[i]) free(commentList[i]);
17396 commentList[i] = NULL;
17398 framePtr = MAX_MOVES-1;
17403 LoadVariation (int index, char *text)
17404 { // [HGM] vari: shelve previous line and load new variation, parsed from text around text[index]
17405 char *p = text, *start = NULL, *end = NULL, wait = NULLCHAR;
17406 int level = 0, move;
17408 if(gameMode != EditGame && gameMode != AnalyzeMode && gameMode != PlayFromGameFile) return;
17409 // first find outermost bracketing variation
17410 while(*p) { // hope I got this right... Non-nesting {} and [] can screen each other and nesting ()
17411 if(!wait) { // while inside [] pr {}, ignore everyting except matching closing ]}
17412 if(*p == '{') wait = '}'; else
17413 if(*p == '[') wait = ']'; else
17414 if(*p == '(' && level++ == 0 && p-text < index) start = p+1;
17415 if(*p == ')' && level > 0 && --level == 0 && p-text > index && end == NULL) end = p-1;
17417 if(*p == wait) wait = NULLCHAR; // closing ]} found
17420 if(!start || !end) return; // no variation found, or syntax error in PGN: ignore click
17421 if(appData.debugMode) fprintf(debugFP, "at move %d load variation '%s'\n", currentMove, start);
17422 end[1] = NULLCHAR; // clip off comment beyond variation
17423 ToNrEvent(currentMove-1);
17424 PushTail(currentMove, forwardMostMove); // shelve main variation. This truncates game
17425 // kludge: use ParsePV() to append variation to game
17426 move = currentMove;
17427 ParsePV(start, TRUE, TRUE);
17428 forwardMostMove = endPV; endPV = -1; currentMove = move; // cleanup what ParsePV did
17429 ClearPremoveHighlights();
17431 ToNrEvent(currentMove+1);