2 * backend.c -- Common back end for X and Windows NT versions of
4 * Copyright 1991 by Digital Equipment Corporation, Maynard,
7 * Enhancements Copyright 1992-2001, 2002, 2003, 2004, 2005, 2006,
8 * 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
10 * Enhancements Copyright 2005 Alessandro Scotti
12 * The following terms apply to Digital Equipment Corporation's copyright
14 * ------------------------------------------------------------------------
17 * Permission to use, copy, modify, and distribute this software and its
18 * documentation for any purpose and without fee is hereby granted,
19 * provided that the above copyright notice appear in all copies and that
20 * both that copyright notice and this permission notice appear in
21 * supporting documentation, and that the name of Digital not be
22 * used in advertising or publicity pertaining to distribution of the
23 * software without specific, written prior permission.
25 * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
26 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
27 * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
28 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
29 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
30 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
32 * ------------------------------------------------------------------------
34 * The following terms apply to the enhanced version of XBoard
35 * distributed by the Free Software Foundation:
36 * ------------------------------------------------------------------------
38 * GNU XBoard is free software: you can redistribute it and/or modify
39 * it under the terms of the GNU General Public License as published by
40 * the Free Software Foundation, either version 3 of the License, or (at
41 * your option) any later version.
43 * GNU XBoard is distributed in the hope that it will be useful, but
44 * WITHOUT ANY WARRANTY; without even the implied warranty of
45 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
46 * General Public License for more details.
48 * You should have received a copy of the GNU General Public License
49 * along with this program. If not, see http://www.gnu.org/licenses/. *
51 *------------------------------------------------------------------------
52 ** See the file ChangeLog for a revision history. */
54 /* [AS] Also useful here for debugging */
58 int flock(int f, int code);
75 #include <sys/types.h>
84 #else /* not STDC_HEADERS */
87 # else /* not HAVE_STRING_H */
89 # endif /* not HAVE_STRING_H */
90 #endif /* not STDC_HEADERS */
93 # include <sys/fcntl.h>
94 #else /* not HAVE_SYS_FCNTL_H */
97 # endif /* HAVE_FCNTL_H */
98 #endif /* not HAVE_SYS_FCNTL_H */
100 #if TIME_WITH_SYS_TIME
101 # include <sys/time.h>
105 # include <sys/time.h>
111 #if defined(_amigados) && !defined(__GNUC__)
116 extern int gettimeofday(struct timeval *, struct timezone *);
124 #include "frontend.h"
131 #include "backendz.h"
135 # define _(s) gettext (s)
136 # define N_(s) gettext_noop (s)
137 # define T_(s) gettext(s)
150 int establish P((void));
151 void read_from_player P((InputSourceRef isr, VOIDSTAR closure,
152 char *buf, int count, int error));
153 void read_from_ics P((InputSourceRef isr, VOIDSTAR closure,
154 char *buf, int count, int error));
155 void ics_printf P((char *format, ...));
156 void SendToICS P((char *s));
157 void SendToICSDelayed P((char *s, long msdelay));
158 void SendMoveToICS P((ChessMove moveType, int fromX, int fromY, int toX, int toY, char promoChar));
159 void HandleMachineMove P((char *message, ChessProgramState *cps));
160 int AutoPlayOneMove P((void));
161 int LoadGameOneMove P((ChessMove readAhead));
162 int LoadGameFromFile P((char *filename, int n, char *title, int useList));
163 int LoadPositionFromFile P((char *filename, int n, char *title));
164 int SavePositionToFile P((char *filename));
165 void MakeMove P((int fromX, int fromY, int toX, int toY, int promoChar));
166 void ShowMove P((int fromX, int fromY, int toX, int toY));
167 int FinishMove P((ChessMove moveType, int fromX, int fromY, int toX, int toY,
168 /*char*/int promoChar));
169 void BackwardInner P((int target));
170 void ForwardInner P((int target));
171 int Adjudicate P((ChessProgramState *cps));
172 void GameEnds P((ChessMove result, char *resultDetails, int whosays));
173 void EditPositionDone P((Boolean fakeRights));
174 void PrintOpponents P((FILE *fp));
175 void PrintPosition P((FILE *fp, int move));
176 void StartChessProgram P((ChessProgramState *cps));
177 void SendToProgram P((char *message, ChessProgramState *cps));
178 void SendMoveToProgram P((int moveNum, ChessProgramState *cps));
179 void ReceiveFromProgram P((InputSourceRef isr, VOIDSTAR closure,
180 char *buf, int count, int error));
181 void SendTimeControl P((ChessProgramState *cps,
182 int mps, long tc, int inc, int sd, int st));
183 char *TimeControlTagValue P((void));
184 void Attention P((ChessProgramState *cps));
185 void FeedMovesToProgram P((ChessProgramState *cps, int upto));
186 int ResurrectChessProgram P((void));
187 void DisplayComment P((int moveNumber, char *text));
188 void DisplayMove P((int moveNumber));
190 void ParseGameHistory P((char *game));
191 void ParseBoard12 P((char *string));
192 void KeepAlive P((void));
193 void StartClocks P((void));
194 void SwitchClocks P((int nr));
195 void StopClocks P((void));
196 void ResetClocks P((void));
197 char *PGNDate P((void));
198 void SetGameInfo P((void));
199 int RegisterMove P((void));
200 void MakeRegisteredMove P((void));
201 void TruncateGame P((void));
202 int looking_at P((char *, int *, char *));
203 void CopyPlayerNameIntoFileName P((char **, char *));
204 char *SavePart P((char *));
205 int SaveGameOldStyle P((FILE *));
206 int SaveGamePGN P((FILE *));
207 int CheckFlags P((void));
208 long NextTickLength P((long));
209 void CheckTimeControl P((void));
210 void show_bytes P((FILE *, char *, int));
211 int string_to_rating P((char *str));
212 void ParseFeatures P((char* args, ChessProgramState *cps));
213 void InitBackEnd3 P((void));
214 void FeatureDone P((ChessProgramState* cps, int val));
215 void InitChessProgram P((ChessProgramState *cps, int setup));
216 void OutputKibitz(int window, char *text);
217 int PerpetualChase(int first, int last);
218 int EngineOutputIsUp();
219 void InitDrawingSizes(int x, int y);
220 void NextMatchGame P((void));
221 int NextTourneyGame P((int nr, int *swap));
222 int Pairing P((int nr, int nPlayers, int *w, int *b, int *sync));
223 FILE *WriteTourneyFile P((char *results, FILE *f));
224 void DisplayTwoMachinesTitle P(());
227 extern void ConsoleCreate();
230 ChessProgramState *WhitePlayer();
231 void InsertIntoMemo P((int which, char *text)); // [HGM] kibitz: in engineo.c
232 int VerifyDisplayMode P(());
234 char *GetInfoFromComment( int, char * ); // [HGM] PV time: returns stripped comment
235 void InitEngineUCI( const char * iniDir, ChessProgramState * cps ); // [HGM] moved here from winboard.c
236 char *ProbeBook P((int moveNr, char *book)); // [HGM] book: returns a book move
237 char *SendMoveToBookUser P((int nr, ChessProgramState *cps, int initial)); // [HGM] book
238 void ics_update_width P((int new_width));
239 extern char installDir[MSG_SIZ];
240 VariantClass startVariant; /* [HGM] nicks: initial variant */
243 extern int tinyLayout, smallLayout;
244 ChessProgramStats programStats;
245 char lastPV[2][2*MSG_SIZ]; /* [HGM] pv: last PV in thinking output of each engine */
247 static int exiting = 0; /* [HGM] moved to top */
248 static int setboardSpoiledMachineBlack = 0 /*, errorExitFlag = 0*/;
249 int startedFromPositionFile = FALSE; Board filePosition; /* [HGM] loadPos */
250 Board partnerBoard; /* [HGM] bughouse: for peeking at partner game */
251 int partnerHighlight[2];
252 Boolean partnerBoardValid = 0;
253 char partnerStatus[MSG_SIZ];
255 Boolean originalFlip;
256 Boolean twoBoards = 0;
257 char endingGame = 0; /* [HGM] crash: flag to prevent recursion of GameEnds() */
258 int whiteNPS, blackNPS; /* [HGM] nps: for easily making clocks aware of NPS */
259 VariantClass currentlyInitializedVariant; /* [HGM] variantswitch */
260 int lastIndex = 0; /* [HGM] autoinc: last game/position used in match mode */
261 Boolean connectionAlive;/* [HGM] alive: ICS connection status from probing */
262 int opponentKibitzes;
263 int lastSavedGame; /* [HGM] save: ID of game */
264 char chatPartner[MAX_CHAT][MSG_SIZ]; /* [HGM] chat: list of chatting partners */
265 extern int chatCount;
267 char marker[BOARD_RANKS][BOARD_FILES]; /* [HGM] marks for target squares */
268 char lastMsg[MSG_SIZ];
269 ChessSquare pieceSweep = EmptySquare;
270 ChessSquare promoSweep = EmptySquare, defaultPromoChoice;
271 int promoDefaultAltered;
273 /* States for ics_getting_history */
275 #define H_REQUESTED 1
276 #define H_GOT_REQ_HEADER 2
277 #define H_GOT_UNREQ_HEADER 3
278 #define H_GETTING_MOVES 4
279 #define H_GOT_UNWANTED_HEADER 5
281 /* whosays values for GameEnds */
290 /* Maximum number of games in a cmail message */
291 #define CMAIL_MAX_GAMES 20
293 /* Different types of move when calling RegisterMove */
295 #define CMAIL_RESIGN 1
297 #define CMAIL_ACCEPT 3
299 /* Different types of result to remember for each game */
300 #define CMAIL_NOT_RESULT 0
301 #define CMAIL_OLD_RESULT 1
302 #define CMAIL_NEW_RESULT 2
304 /* Telnet protocol constants */
315 safeStrCpy (char *dst, const char *src, size_t count)
318 assert( dst != NULL );
319 assert( src != NULL );
322 for(i=0; i<count; i++) if((dst[i] = src[i]) == NULLCHAR) break;
323 if( i == count && dst[count-1] != NULLCHAR)
325 dst[ count-1 ] = '\0'; // make sure incomplete copy still null-terminated
326 if(appData.debugMode)
327 fprintf(debugFP, "safeStrCpy: copying %s into %s didn't work, not enough space %d\n",src,dst, (int)count);
333 /* Some compiler can't cast u64 to double
334 * This function do the job for us:
336 * We use the highest bit for cast, this only
337 * works if the highest bit is not
338 * in use (This should not happen)
340 * We used this for all compiler
343 u64ToDouble (u64 value)
346 u64 tmp = value & u64Const(0x7fffffffffffffff);
347 r = (double)(s64)tmp;
348 if (value & u64Const(0x8000000000000000))
349 r += 9.2233720368547758080e18; /* 2^63 */
353 /* Fake up flags for now, as we aren't keeping track of castling
354 availability yet. [HGM] Change of logic: the flag now only
355 indicates the type of castlings allowed by the rule of the game.
356 The actual rights themselves are maintained in the array
357 castlingRights, as part of the game history, and are not probed
363 int flags = F_ALL_CASTLE_OK;
364 if ((index % 2) == 0) flags |= F_WHITE_ON_MOVE;
365 switch (gameInfo.variant) {
367 flags &= ~F_ALL_CASTLE_OK;
368 case VariantGiveaway: // [HGM] moved this case label one down: seems Giveaway does have castling on ICC!
369 flags |= F_IGNORE_CHECK;
371 flags |= F_MANDATORY_CAPTURE; //[HGM] losers: sets flag so TestLegality rejects non-capts if capts exist
374 flags |= F_IGNORE_CHECK | F_ATOMIC_CAPTURE;
376 case VariantKriegspiel:
377 flags |= F_KRIEGSPIEL_CAPTURE;
379 case VariantCapaRandom:
380 case VariantFischeRandom:
381 flags |= F_FRC_TYPE_CASTLING; /* [HGM] enable this through flag */
382 case VariantNoCastle:
383 case VariantShatranj:
387 flags &= ~F_ALL_CASTLE_OK;
395 FILE *gameFileFP, *debugFP, *serverFP;
396 char *currentDebugFile; // [HGM] debug split: to remember name
399 [AS] Note: sometimes, the sscanf() function is used to parse the input
400 into a fixed-size buffer. Because of this, we must be prepared to
401 receive strings as long as the size of the input buffer, which is currently
402 set to 4K for Windows and 8K for the rest.
403 So, we must either allocate sufficiently large buffers here, or
404 reduce the size of the input buffer in the input reading part.
407 char cmailMove[CMAIL_MAX_GAMES][MOVE_LEN], cmailMsg[MSG_SIZ];
408 char bookOutput[MSG_SIZ*10], thinkOutput[MSG_SIZ*10], lastHint[MSG_SIZ];
409 char thinkOutput1[MSG_SIZ*10];
411 ChessProgramState first, second, pairing;
413 /* premove variables */
416 int premoveFromX = 0;
417 int premoveFromY = 0;
418 int premovePromoChar = 0;
420 Boolean alarmSounded;
421 /* end premove variables */
423 char *ics_prefix = "$";
424 int ics_type = ICS_GENERIC;
426 int currentMove = 0, forwardMostMove = 0, backwardMostMove = 0;
427 int pauseExamForwardMostMove = 0;
428 int nCmailGames = 0, nCmailResults = 0, nCmailMovesRegistered = 0;
429 int cmailMoveRegistered[CMAIL_MAX_GAMES], cmailResult[CMAIL_MAX_GAMES];
430 int cmailMsgLoaded = FALSE, cmailMailedMove = FALSE;
431 int cmailOldMove = -1, firstMove = TRUE, flipView = FALSE;
432 int blackPlaysFirst = FALSE, startedFromSetupPosition = FALSE;
433 int searchTime = 0, pausing = FALSE, pauseExamInvalid = FALSE;
434 int whiteFlag = FALSE, blackFlag = FALSE;
435 int userOfferedDraw = FALSE;
436 int ics_user_moved = 0, ics_gamenum = -1, ics_getting_history = H_FALSE;
437 int matchMode = FALSE, hintRequested = FALSE, bookRequested = FALSE;
438 int cmailMoveType[CMAIL_MAX_GAMES];
439 long ics_clock_paused = 0;
440 ProcRef icsPR = NoProc, cmailPR = NoProc;
441 InputSourceRef telnetISR = NULL, fromUserISR = NULL, cmailISR = NULL;
442 GameMode gameMode = BeginningOfGame;
443 char moveList[MAX_MOVES][MOVE_LEN], parseList[MAX_MOVES][MOVE_LEN * 2];
444 char *commentList[MAX_MOVES], *cmailCommentList[CMAIL_MAX_GAMES];
445 ChessProgramStats_Move pvInfoList[MAX_MOVES]; /* [AS] Info about engine thinking */
446 int hiddenThinkOutputState = 0; /* [AS] */
447 int adjudicateLossThreshold = 0; /* [AS] Automatic adjudication */
448 int adjudicateLossPlies = 6;
449 char white_holding[64], black_holding[64];
450 TimeMark lastNodeCountTime;
451 long lastNodeCount=0;
452 int shiftKey; // [HGM] set by mouse handler
454 int have_sent_ICS_logon = 0;
456 int suddenDeath, whiteStartMove, blackStartMove; /* [HGM] for implementation of 'any per time' sessions, as in first part of byoyomi TC */
457 long whiteTimeRemaining, blackTimeRemaining, timeControl, timeIncrement, lastWhite, lastBlack;
458 Boolean adjustedClock;
459 long timeControl_2; /* [AS] Allow separate time controls */
460 char *fullTimeControlString = NULL, *nextSession, *whiteTC, *blackTC; /* [HGM] secondary TC: merge of MPS, TC and inc */
461 long timeRemaining[2][MAX_MOVES];
462 int matchGame = 0, nextGame = 0, roundNr = 0;
463 Boolean waitingForGame = FALSE;
464 TimeMark programStartTime, pauseStart;
465 char ics_handle[MSG_SIZ];
466 int have_set_title = 0;
468 /* animateTraining preserves the state of appData.animate
469 * when Training mode is activated. This allows the
470 * response to be animated when appData.animate == TRUE and
471 * appData.animateDragging == TRUE.
473 Boolean animateTraining;
479 Board boards[MAX_MOVES];
480 /* [HGM] Following 7 needed for accurate legality tests: */
481 signed char castlingRank[BOARD_FILES]; // and corresponding ranks
482 signed char initialRights[BOARD_FILES];
483 int nrCastlingRights; // For TwoKings, or to implement castling-unknown status
484 int initialRulePlies, FENrulePlies;
485 FILE *serverMoves = NULL; // next two for broadcasting (/serverMoves option)
487 Boolean shuffleOpenings;
488 int mute; // mute all sounds
490 // [HGM] vari: next 12 to save and restore variations
491 #define MAX_VARIATIONS 10
492 int framePtr = MAX_MOVES-1; // points to free stack entry
494 int savedFirst[MAX_VARIATIONS];
495 int savedLast[MAX_VARIATIONS];
496 int savedFramePtr[MAX_VARIATIONS];
497 char *savedDetails[MAX_VARIATIONS];
498 ChessMove savedResult[MAX_VARIATIONS];
500 void PushTail P((int firstMove, int lastMove));
501 Boolean PopTail P((Boolean annotate));
502 void PushInner P((int firstMove, int lastMove));
503 void PopInner P((Boolean annotate));
504 void CleanupTail P((void));
506 ChessSquare FIDEArray[2][BOARD_FILES] = {
507 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
508 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
509 { BlackRook, BlackKnight, BlackBishop, BlackQueen,
510 BlackKing, BlackBishop, BlackKnight, BlackRook }
513 ChessSquare twoKingsArray[2][BOARD_FILES] = {
514 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
515 WhiteKing, WhiteKing, WhiteKnight, WhiteRook },
516 { BlackRook, BlackKnight, BlackBishop, BlackQueen,
517 BlackKing, BlackKing, BlackKnight, BlackRook }
520 ChessSquare KnightmateArray[2][BOARD_FILES] = {
521 { WhiteRook, WhiteMan, WhiteBishop, WhiteQueen,
522 WhiteUnicorn, WhiteBishop, WhiteMan, WhiteRook },
523 { BlackRook, BlackMan, BlackBishop, BlackQueen,
524 BlackUnicorn, BlackBishop, BlackMan, BlackRook }
527 ChessSquare SpartanArray[2][BOARD_FILES] = {
528 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
529 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
530 { BlackAlfil, BlackMarshall, BlackKing, BlackDragon,
531 BlackDragon, BlackKing, BlackAngel, BlackAlfil }
534 ChessSquare fairyArray[2][BOARD_FILES] = { /* [HGM] Queen side differs from King side */
535 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
536 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
537 { BlackCardinal, BlackAlfil, BlackMarshall, BlackAngel,
538 BlackKing, BlackMarshall, BlackAlfil, BlackCardinal }
541 ChessSquare ShatranjArray[2][BOARD_FILES] = { /* [HGM] (movGen knows about Shatranj Q and P) */
542 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteKing,
543 WhiteFerz, WhiteAlfil, WhiteKnight, WhiteRook },
544 { BlackRook, BlackKnight, BlackAlfil, BlackKing,
545 BlackFerz, BlackAlfil, BlackKnight, BlackRook }
548 ChessSquare makrukArray[2][BOARD_FILES] = { /* [HGM] (movGen knows about Shatranj Q and P) */
549 { WhiteRook, WhiteKnight, WhiteMan, WhiteKing,
550 WhiteFerz, WhiteMan, WhiteKnight, WhiteRook },
551 { BlackRook, BlackKnight, BlackMan, BlackFerz,
552 BlackKing, BlackMan, BlackKnight, BlackRook }
556 #if (BOARD_FILES>=10)
557 ChessSquare ShogiArray[2][BOARD_FILES] = {
558 { WhiteQueen, WhiteKnight, WhiteFerz, WhiteWazir,
559 WhiteKing, WhiteWazir, WhiteFerz, WhiteKnight, WhiteQueen },
560 { BlackQueen, BlackKnight, BlackFerz, BlackWazir,
561 BlackKing, BlackWazir, BlackFerz, BlackKnight, BlackQueen }
564 ChessSquare XiangqiArray[2][BOARD_FILES] = {
565 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteFerz,
566 WhiteWazir, WhiteFerz, WhiteAlfil, WhiteKnight, WhiteRook },
567 { BlackRook, BlackKnight, BlackAlfil, BlackFerz,
568 BlackWazir, BlackFerz, BlackAlfil, BlackKnight, BlackRook }
571 ChessSquare CapablancaArray[2][BOARD_FILES] = {
572 { WhiteRook, WhiteKnight, WhiteAngel, WhiteBishop, WhiteQueen,
573 WhiteKing, WhiteBishop, WhiteMarshall, WhiteKnight, WhiteRook },
574 { BlackRook, BlackKnight, BlackAngel, BlackBishop, BlackQueen,
575 BlackKing, BlackBishop, BlackMarshall, BlackKnight, BlackRook }
578 ChessSquare GreatArray[2][BOARD_FILES] = {
579 { WhiteDragon, WhiteKnight, WhiteAlfil, WhiteGrasshopper, WhiteKing,
580 WhiteSilver, WhiteCardinal, WhiteAlfil, WhiteKnight, WhiteDragon },
581 { BlackDragon, BlackKnight, BlackAlfil, BlackGrasshopper, BlackKing,
582 BlackSilver, BlackCardinal, BlackAlfil, BlackKnight, BlackDragon },
585 ChessSquare JanusArray[2][BOARD_FILES] = {
586 { WhiteRook, WhiteAngel, WhiteKnight, WhiteBishop, WhiteKing,
587 WhiteQueen, WhiteBishop, WhiteKnight, WhiteAngel, WhiteRook },
588 { BlackRook, BlackAngel, BlackKnight, BlackBishop, BlackKing,
589 BlackQueen, BlackBishop, BlackKnight, BlackAngel, BlackRook }
592 ChessSquare GrandArray[2][BOARD_FILES] = {
593 { EmptySquare, WhiteKnight, WhiteBishop, WhiteQueen, WhiteKing,
594 WhiteMarshall, WhiteAngel, WhiteBishop, WhiteKnight, EmptySquare },
595 { EmptySquare, BlackKnight, BlackBishop, BlackQueen, BlackKing,
596 BlackMarshall, BlackAngel, BlackBishop, BlackKnight, EmptySquare }
600 ChessSquare GothicArray[2][BOARD_FILES] = {
601 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen, WhiteMarshall,
602 WhiteKing, WhiteAngel, WhiteBishop, WhiteKnight, WhiteRook },
603 { BlackRook, BlackKnight, BlackBishop, BlackQueen, BlackMarshall,
604 BlackKing, BlackAngel, BlackBishop, BlackKnight, BlackRook }
607 #define GothicArray CapablancaArray
611 ChessSquare FalconArray[2][BOARD_FILES] = {
612 { WhiteRook, WhiteKnight, WhiteBishop, WhiteFalcon, WhiteQueen,
613 WhiteKing, WhiteFalcon, WhiteBishop, WhiteKnight, WhiteRook },
614 { BlackRook, BlackKnight, BlackBishop, BlackFalcon, BlackQueen,
615 BlackKing, BlackFalcon, BlackBishop, BlackKnight, BlackRook }
618 #define FalconArray CapablancaArray
621 #else // !(BOARD_FILES>=10)
622 #define XiangqiPosition FIDEArray
623 #define CapablancaArray FIDEArray
624 #define GothicArray FIDEArray
625 #define GreatArray FIDEArray
626 #endif // !(BOARD_FILES>=10)
628 #if (BOARD_FILES>=12)
629 ChessSquare CourierArray[2][BOARD_FILES] = {
630 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteBishop, WhiteMan, WhiteKing,
631 WhiteFerz, WhiteWazir, WhiteBishop, WhiteAlfil, WhiteKnight, WhiteRook },
632 { BlackRook, BlackKnight, BlackAlfil, BlackBishop, BlackMan, BlackKing,
633 BlackFerz, BlackWazir, BlackBishop, BlackAlfil, BlackKnight, BlackRook }
635 #else // !(BOARD_FILES>=12)
636 #define CourierArray CapablancaArray
637 #endif // !(BOARD_FILES>=12)
640 Board initialPosition;
643 /* Convert str to a rating. Checks for special cases of "----",
645 "++++", etc. Also strips ()'s */
647 string_to_rating (char *str)
649 while(*str && !isdigit(*str)) ++str;
651 return 0; /* One of the special "no rating" cases */
659 /* Init programStats */
660 programStats.movelist[0] = 0;
661 programStats.depth = 0;
662 programStats.nr_moves = 0;
663 programStats.moves_left = 0;
664 programStats.nodes = 0;
665 programStats.time = -1; // [HGM] PGNtime: make invalid to recognize engine output
666 programStats.score = 0;
667 programStats.got_only_move = 0;
668 programStats.got_fail = 0;
669 programStats.line_is_book = 0;
674 { // [HGM] moved some code here from InitBackend1 that has to be done after both engines have contributed their settings
675 if (appData.firstPlaysBlack) {
676 first.twoMachinesColor = "black\n";
677 second.twoMachinesColor = "white\n";
679 first.twoMachinesColor = "white\n";
680 second.twoMachinesColor = "black\n";
683 first.other = &second;
684 second.other = &first;
687 if(appData.timeOddsMode) {
688 norm = appData.timeOdds[0];
689 if(norm > appData.timeOdds[1]) norm = appData.timeOdds[1];
691 first.timeOdds = appData.timeOdds[0]/norm;
692 second.timeOdds = appData.timeOdds[1]/norm;
695 if(programVersion) free(programVersion);
696 if (appData.noChessProgram) {
697 programVersion = (char*) malloc(5 + strlen(PACKAGE_STRING));
698 sprintf(programVersion, "%s", PACKAGE_STRING);
700 /* [HGM] tidy: use tidy name, in stead of full pathname (which was probably a bug due to / vs \ ) */
701 programVersion = (char*) malloc(8 + strlen(PACKAGE_STRING) + strlen(first.tidy));
702 sprintf(programVersion, "%s + %s", PACKAGE_STRING, first.tidy);
707 UnloadEngine (ChessProgramState *cps)
709 /* Kill off first chess program */
710 if (cps->isr != NULL)
711 RemoveInputSource(cps->isr);
714 if (cps->pr != NoProc) {
716 DoSleep( appData.delayBeforeQuit );
717 SendToProgram("quit\n", cps);
718 DoSleep( appData.delayAfterQuit );
719 DestroyChildProcess(cps->pr, cps->useSigterm);
722 if(appData.debugMode) fprintf(debugFP, "Unload %s\n", cps->which);
726 ClearOptions (ChessProgramState *cps)
729 cps->nrOptions = cps->comboCnt = 0;
730 for(i=0; i<MAX_OPTIONS; i++) {
731 cps->option[i].min = cps->option[i].max = cps->option[i].value = 0;
732 cps->option[i].textValue = 0;
736 char *engineNames[] = {
737 /* TRANSLATORS: "first" is the first of possible two chess engines. It is inserted into strings
738 such as "%s engine" / "%s chess program" / "%s machine" - all meaning the same thing */
740 /* TRANSLATORS: "second" is the second of possible two chess engines. It is inserted into strings
741 such as "%s engine" / "%s chess program" / "%s machine" - all meaning the same thing */
746 InitEngine (ChessProgramState *cps, int n)
747 { // [HGM] all engine initialiation put in a function that does one engine
751 cps->which = engineNames[n];
752 cps->maybeThinking = FALSE;
756 cps->sendDrawOffers = 1;
758 cps->program = appData.chessProgram[n];
759 cps->host = appData.host[n];
760 cps->dir = appData.directory[n];
761 cps->initString = appData.engInitString[n];
762 cps->computerString = appData.computerString[n];
763 cps->useSigint = TRUE;
764 cps->useSigterm = TRUE;
765 cps->reuse = appData.reuse[n];
766 cps->nps = appData.NPS[n]; // [HGM] nps: copy nodes per second
767 cps->useSetboard = FALSE;
769 cps->usePing = FALSE;
772 cps->usePlayother = FALSE;
773 cps->useColors = TRUE;
774 cps->useUsermove = FALSE;
775 cps->sendICS = FALSE;
776 cps->sendName = appData.icsActive;
777 cps->sdKludge = FALSE;
778 cps->stKludge = FALSE;
779 TidyProgramName(cps->program, cps->host, cps->tidy);
781 safeStrCpy(cps->variants, appData.variant, MSG_SIZ);
782 cps->analysisSupport = 2; /* detect */
783 cps->analyzing = FALSE;
784 cps->initDone = FALSE;
786 /* New features added by Tord: */
787 cps->useFEN960 = FALSE;
788 cps->useOOCastle = TRUE;
789 /* End of new features added by Tord. */
790 cps->fenOverride = appData.fenOverride[n];
792 /* [HGM] time odds: set factor for each machine */
793 cps->timeOdds = appData.timeOdds[n];
795 /* [HGM] secondary TC: how to handle sessions that do not fit in 'level'*/
796 cps->accumulateTC = appData.accumulateTC[n];
797 cps->maxNrOfSessions = 1;
802 cps->supportsNPS = UNKNOWN;
803 cps->memSize = FALSE;
804 cps->maxCores = FALSE;
805 cps->egtFormats[0] = NULLCHAR;
808 cps->optionSettings = appData.engOptions[n];
810 cps->scoreIsAbsolute = appData.scoreIsAbsolute[n]; /* [AS] */
811 cps->isUCI = appData.isUCI[n]; /* [AS] */
812 cps->hasOwnBookUCI = appData.hasOwnBookUCI[n]; /* [AS] */
814 if (appData.protocolVersion[n] > PROTOVER
815 || appData.protocolVersion[n] < 1)
820 len = snprintf(buf, MSG_SIZ, _("protocol version %d not supported"),
821 appData.protocolVersion[n]);
822 if( (len >= MSG_SIZ) && appData.debugMode )
823 fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
825 DisplayFatalError(buf, 0, 2);
829 cps->protocolVersion = appData.protocolVersion[n];
832 InitEngineUCI( installDir, cps ); // [HGM] moved here from winboard.c, to make available in xboard
833 ParseFeatures(appData.featureDefaults, cps);
836 ChessProgramState *savCps;
842 if(WaitForEngine(savCps, LoadEngine)) return;
843 CommonEngineInit(); // recalculate time odds
844 if(gameInfo.variant != StringToVariant(appData.variant)) {
845 // we changed variant when loading the engine; this forces us to reset
846 Reset(TRUE, savCps != &first);
847 EditGameEvent(); // for consistency with other path, as Reset changes mode
849 InitChessProgram(savCps, FALSE);
850 SendToProgram("force\n", savCps);
851 DisplayMessage("", "");
852 if (startedFromSetupPosition) SendBoard(savCps, backwardMostMove);
853 for (i = backwardMostMove; i < forwardMostMove; i++) SendMoveToProgram(i, savCps);
859 ReplaceEngine (ChessProgramState *cps, int n)
863 appData.noChessProgram = FALSE;
864 appData.clockMode = TRUE;
867 if(n) return; // only startup first engine immediately; second can wait
868 savCps = cps; // parameter to LoadEngine passed as globals, to allow scheduled calling :-(
872 extern char *engineName, *engineDir, *engineChoice, *engineLine, *nickName, *params;
873 extern Boolean isUCI, hasBook, storeVariant, v1, addToList, useNick;
875 static char resetOptions[] =
876 "-reuse -firstIsUCI false -firstHasOwnBookUCI true -firstTimeOdds 1 "
877 "-firstInitString \"" INIT_STRING "\" -firstComputerString \"" COMPUTER_STRING "\" "
878 "-firstOptions \"\" -firstNPS -1 -fn \"\"";
881 FloatToFront(char **list, char *engineLine)
883 char buf[MSG_SIZ], tidy[MSG_SIZ], *p = buf, *q, *r = buf;
885 if(appData.recentEngines <= 0) return;
886 TidyProgramName(engineLine, "localhost", tidy+1);
887 tidy[0] = buf[0] = '\n'; strcat(tidy, "\n");
888 strncpy(buf+1, *list, MSG_SIZ-50);
889 if(p = strstr(buf, tidy)) { // tidy name appears in list
890 q = strchr(++p, '\n'); if(q == NULL) return; // malformed, don't touch
891 while(*p++ = *++q); // squeeze out
893 strcat(tidy, buf+1); // put list behind tidy name
894 p = tidy + 1; while(q = strchr(p, '\n')) i++, r = p, p = q + 1; // count entries in new list
895 if(i > appData.recentEngines) *r = NULLCHAR; // if maximum rached, strip off last
896 ASSIGN(*list, tidy+1);
900 Load (ChessProgramState *cps, int i)
902 char *p, *q, buf[MSG_SIZ], command[MSG_SIZ], buf2[MSG_SIZ];
903 if(engineLine && engineLine[0]) { // an engine was selected from the combo box
904 snprintf(buf, MSG_SIZ, "-fcp %s", engineLine);
905 SwapEngines(i); // kludge to parse -f* / -first* like it is -s* / -second*
906 ParseArgsFromString(resetOptions); appData.fenOverride[0] = NULL; appData.pvSAN[0] = FALSE;
907 appData.firstProtocolVersion = PROTOVER;
908 ParseArgsFromString(buf);
910 ReplaceEngine(cps, i);
911 FloatToFront(&appData.recentEngineList, engineLine);
915 while(q = strchr(p, SLASH)) p = q+1;
916 if(*p== NULLCHAR) { DisplayError(_("You did not specify the engine executable"), 0); return; }
917 if(engineDir[0] != NULLCHAR)
918 appData.directory[i] = engineDir;
919 else if(p != engineName) { // derive directory from engine path, when not given
921 appData.directory[i] = strdup(engineName);
923 } else appData.directory[i] = ".";
925 if(strchr(p, ' ') && !strchr(p, '"')) snprintf(buf2, MSG_SIZ, "\"%s\"", p), p = buf2; // quote if it contains spaces
926 snprintf(command, MSG_SIZ, "%s %s", p, params);
929 appData.chessProgram[i] = strdup(p);
930 appData.isUCI[i] = isUCI;
931 appData.protocolVersion[i] = v1 ? 1 : PROTOVER;
932 appData.hasOwnBookUCI[i] = hasBook;
933 if(!nickName[0]) useNick = FALSE;
934 if(useNick) ASSIGN(appData.pgnName[i], nickName);
938 q = firstChessProgramNames;
939 if(nickName[0]) snprintf(buf, MSG_SIZ, "\"%s\" -fcp ", nickName); else buf[0] = NULLCHAR;
940 quote = strchr(p, '"') ? '\'' : '"'; // use single quotes around engine command if it contains double quotes
941 snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), "%c%s%c -fd \"%s\"%s%s%s%s%s%s%s%s\n",
942 quote, p, quote, appData.directory[i],
943 useNick ? " -fn \"" : "",
944 useNick ? nickName : "",
946 v1 ? " -firstProtocolVersion 1" : "",
947 hasBook ? "" : " -fNoOwnBookUCI",
948 isUCI ? (isUCI == TRUE ? " -fUCI" : gameInfo.variant == VariantShogi ? " -fUSI" : " -fUCCI") : "",
949 storeVariant ? " -variant " : "",
950 storeVariant ? VariantName(gameInfo.variant) : "");
951 firstChessProgramNames = malloc(len = strlen(q) + strlen(buf) + 1);
952 snprintf(firstChessProgramNames, len, "%s%s", q, buf);
954 FloatToFront(&appData.recentEngineList, buf);
956 ReplaceEngine(cps, i);
962 int matched, min, sec;
964 * Parse timeControl resource
966 if (!ParseTimeControl(appData.timeControl, appData.timeIncrement,
967 appData.movesPerSession)) {
969 snprintf(buf, sizeof(buf), _("bad timeControl option %s"), appData.timeControl);
970 DisplayFatalError(buf, 0, 2);
974 * Parse searchTime resource
976 if (*appData.searchTime != NULLCHAR) {
977 matched = sscanf(appData.searchTime, "%d:%d", &min, &sec);
979 searchTime = min * 60;
980 } else if (matched == 2) {
981 searchTime = min * 60 + sec;
984 snprintf(buf, sizeof(buf), _("bad searchTime option %s"), appData.searchTime);
985 DisplayFatalError(buf, 0, 2);
994 ShowThinkingEvent(); // [HGM] thinking: make sure post/nopost state is set according to options
995 startVariant = StringToVariant(appData.variant); // [HGM] nicks: remember original variant
997 GetTimeMark(&programStartTime);
998 srandom((programStartTime.ms + 1000*programStartTime.sec)*0x1001001); // [HGM] book: makes sure random is unpredictabe to msec level
999 appData.seedBase = random() + (random()<<15);
1000 pauseStart = programStartTime; pauseStart.sec -= 100; // [HGM] matchpause: fake a pause that has long since ended
1002 ClearProgramStats();
1003 programStats.ok_to_send = 1;
1004 programStats.seen_stat = 0;
1007 * Initialize game list
1013 * Internet chess server status
1015 if (appData.icsActive) {
1016 appData.matchMode = FALSE;
1017 appData.matchGames = 0;
1019 appData.noChessProgram = !appData.zippyPlay;
1021 appData.zippyPlay = FALSE;
1022 appData.zippyTalk = FALSE;
1023 appData.noChessProgram = TRUE;
1025 if (*appData.icsHelper != NULLCHAR) {
1026 appData.useTelnet = TRUE;
1027 appData.telnetProgram = appData.icsHelper;
1030 appData.zippyTalk = appData.zippyPlay = FALSE;
1033 /* [AS] Initialize pv info list [HGM] and game state */
1037 for( i=0; i<=framePtr; i++ ) {
1038 pvInfoList[i].depth = -1;
1039 boards[i][EP_STATUS] = EP_NONE;
1040 for( j=0; j<BOARD_FILES-2; j++ ) boards[i][CASTLING][j] = NoRights;
1046 /* [AS] Adjudication threshold */
1047 adjudicateLossThreshold = appData.adjudicateLossThreshold;
1049 InitEngine(&first, 0);
1050 InitEngine(&second, 1);
1053 pairing.which = "pairing"; // pairing engine
1054 pairing.pr = NoProc;
1056 pairing.program = appData.pairingEngine;
1057 pairing.host = "localhost";
1060 if (appData.icsActive) {
1061 appData.clockMode = TRUE; /* changes dynamically in ICS mode */
1062 } else if (appData.noChessProgram) { // [HGM] st: searchTime mode now also is clockMode
1063 appData.clockMode = FALSE;
1064 first.sendTime = second.sendTime = 0;
1068 /* Override some settings from environment variables, for backward
1069 compatibility. Unfortunately it's not feasible to have the env
1070 vars just set defaults, at least in xboard. Ugh.
1072 if (appData.icsActive && (appData.zippyPlay || appData.zippyTalk)) {
1077 if (!appData.icsActive) {
1081 /* Check for variants that are supported only in ICS mode,
1082 or not at all. Some that are accepted here nevertheless
1083 have bugs; see comments below.
1085 VariantClass variant = StringToVariant(appData.variant);
1087 case VariantBughouse: /* need four players and two boards */
1088 case VariantKriegspiel: /* need to hide pieces and move details */
1089 /* case VariantFischeRandom: (Fabien: moved below) */
1090 len = snprintf(buf,MSG_SIZ, _("Variant %s supported only in ICS mode"), appData.variant);
1091 if( (len >= MSG_SIZ) && appData.debugMode )
1092 fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
1094 DisplayFatalError(buf, 0, 2);
1097 case VariantUnknown:
1098 case VariantLoadable:
1108 len = snprintf(buf, MSG_SIZ, _("Unknown variant name %s"), appData.variant);
1109 if( (len >= MSG_SIZ) && appData.debugMode )
1110 fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
1112 DisplayFatalError(buf, 0, 2);
1115 case VariantXiangqi: /* [HGM] repetition rules not implemented */
1116 case VariantFairy: /* [HGM] TestLegality definitely off! */
1117 case VariantGothic: /* [HGM] should work */
1118 case VariantCapablanca: /* [HGM] should work */
1119 case VariantCourier: /* [HGM] initial forced moves not implemented */
1120 case VariantShogi: /* [HGM] could still mate with pawn drop */
1121 case VariantKnightmate: /* [HGM] should work */
1122 case VariantCylinder: /* [HGM] untested */
1123 case VariantFalcon: /* [HGM] untested */
1124 case VariantCrazyhouse: /* holdings not shown, ([HGM] fixed that!)
1125 offboard interposition not understood */
1126 case VariantNormal: /* definitely works! */
1127 case VariantWildCastle: /* pieces not automatically shuffled */
1128 case VariantNoCastle: /* pieces not automatically shuffled */
1129 case VariantFischeRandom: /* [HGM] works and shuffles pieces */
1130 case VariantLosers: /* should work except for win condition,
1131 and doesn't know captures are mandatory */
1132 case VariantSuicide: /* should work except for win condition,
1133 and doesn't know captures are mandatory */
1134 case VariantGiveaway: /* should work except for win condition,
1135 and doesn't know captures are mandatory */
1136 case VariantTwoKings: /* should work */
1137 case VariantAtomic: /* should work except for win condition */
1138 case Variant3Check: /* should work except for win condition */
1139 case VariantShatranj: /* should work except for all win conditions */
1140 case VariantMakruk: /* should work except for draw countdown */
1141 case VariantBerolina: /* might work if TestLegality is off */
1142 case VariantCapaRandom: /* should work */
1143 case VariantJanus: /* should work */
1144 case VariantSuper: /* experimental */
1145 case VariantGreat: /* experimental, requires legality testing to be off */
1146 case VariantSChess: /* S-Chess, should work */
1147 case VariantGrand: /* should work */
1148 case VariantSpartan: /* should work */
1156 NextIntegerFromString (char ** str, long * value)
1161 while( *s == ' ' || *s == '\t' ) {
1167 if( *s >= '0' && *s <= '9' ) {
1168 while( *s >= '0' && *s <= '9' ) {
1169 *value = *value * 10 + (*s - '0');
1182 NextTimeControlFromString (char ** str, long * value)
1185 int result = NextIntegerFromString( str, &temp );
1188 *value = temp * 60; /* Minutes */
1189 if( **str == ':' ) {
1191 result = NextIntegerFromString( str, &temp );
1192 *value += temp; /* Seconds */
1200 NextSessionFromString (char ** str, int *moves, long * tc, long *inc, int *incType)
1201 { /* [HGM] routine added to read '+moves/time' for secondary time control. */
1202 int result = -1, type = 0; long temp, temp2;
1204 if(**str != ':') return -1; // old params remain in force!
1206 if(**str == '*') type = *(*str)++, temp = 0; // sandclock TC
1207 if( NextIntegerFromString( str, &temp ) ) return -1;
1208 if(type) { *moves = 0; *tc = temp * 500; *inc = temp * 1000; *incType = '*'; return 0; }
1211 /* time only: incremental or sudden-death time control */
1212 if(**str == '+') { /* increment follows; read it */
1214 if(**str == '!') type = *(*str)++; // Bronstein TC
1215 if(result = NextIntegerFromString( str, &temp2)) return -1;
1216 *inc = temp2 * 1000;
1217 if(**str == '.') { // read fraction of increment
1218 char *start = ++(*str);
1219 if(result = NextIntegerFromString( str, &temp2)) return -1;
1221 while(start++ < *str) temp2 /= 10;
1225 *moves = 0; *tc = temp * 1000; *incType = type;
1229 (*str)++; /* classical time control */
1230 result = NextIntegerFromString( str, &temp2); // NOTE: already converted to seconds by ParseTimeControl()
1242 GetTimeQuota (int movenr, int lastUsed, char *tcString)
1243 { /* [HGM] get time to add from the multi-session time-control string */
1244 int incType, moves=1; /* kludge to force reading of first session */
1245 long time, increment;
1248 if(!*s) return 0; // empty TC string means we ran out of the last sudden-death version
1250 if(moves) NextSessionFromString(&s, &moves, &time, &increment, &incType);
1251 nextSession = s; suddenDeath = moves == 0 && increment == 0;
1252 if(movenr == -1) return time; /* last move before new session */
1253 if(incType == '*') increment = 0; else // for sandclock, time is added while not thinking
1254 if(incType == '!' && lastUsed < increment) increment = lastUsed;
1255 if(!moves) return increment; /* current session is incremental */
1256 if(movenr >= 0) movenr -= moves; /* we already finished this session */
1257 } while(movenr >= -1); /* try again for next session */
1259 return 0; // no new time quota on this move
1263 ParseTimeControl (char *tc, float ti, int mps)
1267 char buf[MSG_SIZ], buf2[MSG_SIZ], *mytc = tc;
1270 if(ti >= 0 && !strchr(tc, '+') && !strchr(tc, '/') ) mps = 0;
1271 if(!strchr(tc, '+') && !strchr(tc, '/') && sscanf(tc, "%d:%d", &min, &sec) >= 1)
1272 sprintf(mytc=buf2, "%d", 60*min+sec); // convert 'classical' min:sec tc string to seconds
1276 snprintf(buf, MSG_SIZ, ":%d/%s+%g", mps, mytc, ti);
1278 snprintf(buf, MSG_SIZ, ":%s+%g", mytc, ti);
1281 snprintf(buf, MSG_SIZ, ":%d/%s", mps, mytc);
1283 snprintf(buf, MSG_SIZ, ":%s", mytc);
1285 fullTimeControlString = StrSave(buf); // this should now be in PGN format
1287 if( NextTimeControlFromString( &tc, &tc1 ) != 0 ) {
1292 /* Parse second time control */
1295 if( NextTimeControlFromString( &tc, &tc2 ) != 0 ) {
1303 timeControl_2 = tc2 * 1000;
1313 timeControl = tc1 * 1000;
1316 timeIncrement = ti * 1000; /* convert to ms */
1317 movesPerSession = 0;
1320 movesPerSession = mps;
1328 if (appData.debugMode) {
1329 fprintf(debugFP, "%s\n", programVersion);
1331 ASSIGN(currentDebugFile, appData.nameOfDebugFile); // [HGM] debug split: remember initial name in use
1333 set_cont_sequence(appData.wrapContSeq);
1334 if (appData.matchGames > 0) {
1335 appData.matchMode = TRUE;
1336 } else if (appData.matchMode) {
1337 appData.matchGames = 1;
1339 if(appData.matchMode && appData.sameColorGames > 0) /* [HGM] alternate: overrule matchGames */
1340 appData.matchGames = appData.sameColorGames;
1341 if(appData.rewindIndex > 1) { /* [HGM] autoinc: rewind implies auto-increment and overrules given index */
1342 if(appData.loadPositionIndex >= 0) appData.loadPositionIndex = -1;
1343 if(appData.loadGameIndex >= 0) appData.loadGameIndex = -1;
1346 if (appData.noChessProgram || first.protocolVersion == 1) {
1349 /* kludge: allow timeout for initial "feature" commands */
1351 DisplayMessage("", _("Starting chess program"));
1352 ScheduleDelayedEvent(InitBackEnd3, FEATURE_TIMEOUT);
1357 CalculateIndex (int index, int gameNr)
1358 { // [HGM] autoinc: absolute way to determine load index from game number (taking auto-inc and rewind into account)
1360 if(index > 0) return index; // fixed nmber
1361 if(index == 0) return 1;
1362 res = (index == -1 ? gameNr : (gameNr-1)/2 + 1); // autoinc
1363 if(appData.rewindIndex > 0) res = (res-1) % appData.rewindIndex + 1; // rewind
1368 LoadGameOrPosition (int gameNr)
1369 { // [HGM] taken out of MatchEvent and NextMatchGame (to combine it)
1370 if (*appData.loadGameFile != NULLCHAR) {
1371 if (!LoadGameFromFile(appData.loadGameFile,
1372 CalculateIndex(appData.loadGameIndex, gameNr),
1373 appData.loadGameFile, FALSE)) {
1374 DisplayFatalError(_("Bad game file"), 0, 1);
1377 } else if (*appData.loadPositionFile != NULLCHAR) {
1378 if (!LoadPositionFromFile(appData.loadPositionFile,
1379 CalculateIndex(appData.loadPositionIndex, gameNr),
1380 appData.loadPositionFile)) {
1381 DisplayFatalError(_("Bad position file"), 0, 1);
1389 ReserveGame (int gameNr, char resChar)
1391 FILE *tf = fopen(appData.tourneyFile, "r+");
1392 char *p, *q, c, buf[MSG_SIZ];
1393 if(tf == NULL) { nextGame = appData.matchGames + 1; return; } // kludge to terminate match
1394 safeStrCpy(buf, lastMsg, MSG_SIZ);
1395 DisplayMessage(_("Pick new game"), "");
1396 flock(fileno(tf), LOCK_EX); // lock the tourney file while we are messing with it
1397 ParseArgsFromFile(tf);
1398 p = q = appData.results;
1399 if(appData.debugMode) {
1400 char *r = appData.participants;
1401 fprintf(debugFP, "results = '%s'\n", p);
1402 while(*r) fprintf(debugFP, *r >= ' ' ? "%c" : "\\%03o", *r), r++;
1403 fprintf(debugFP, "\n");
1405 while(*q && *q != ' ') q++; // get first un-played game (could be beyond end!)
1407 q = malloc(strlen(p) + 2); // could be arbitrary long, but allow to extend by one!
1408 safeStrCpy(q, p, strlen(p) + 2);
1409 if(gameNr >= 0) q[gameNr] = resChar; // replace '*' with result
1410 if(appData.debugMode) fprintf(debugFP, "pick next game from '%s': %d\n", q, nextGame);
1411 if(nextGame <= appData.matchGames && resChar != ' ' && !abortMatch) { // reserve next game if tourney not yet done
1412 if(q[nextGame] == NULLCHAR) q[nextGame+1] = NULLCHAR; // append one char
1415 fseek(tf, -(strlen(p)+4), SEEK_END);
1417 if(c != '"') // depending on DOS or Unix line endings we can be one off
1418 fseek(tf, -(strlen(p)+2), SEEK_END);
1419 else fseek(tf, -(strlen(p)+3), SEEK_END);
1420 fprintf(tf, "%s\"\n", q); fclose(tf); // update, and flush by closing
1421 DisplayMessage(buf, "");
1422 free(p); appData.results = q;
1423 if(nextGame <= appData.matchGames && resChar != ' ' && !abortMatch &&
1424 (gameNr < 0 || nextGame / appData.defaultMatchGames != gameNr / appData.defaultMatchGames)) {
1425 int round = appData.defaultMatchGames * appData.tourneyType;
1426 if(gameNr < 0 || appData.tourneyType < 1 || // gauntlet engine can always stay loaded as first engine
1427 appData.tourneyType > 1 && nextGame/round != gameNr/round) // in multi-gauntlet change only after round
1428 UnloadEngine(&first); // next game belongs to other pairing;
1429 UnloadEngine(&second); // already unload the engines, so TwoMachinesEvent will load new ones.
1431 if(appData.debugMode) fprintf(debugFP, "Reserved, next=%d, nr=%d\n", nextGame, gameNr);
1435 MatchEvent (int mode)
1436 { // [HGM] moved out of InitBackend3, to make it callable when match starts through menu
1438 if(matchMode) { // already in match mode: switch it off
1440 if(!appData.tourneyFile[0]) appData.matchGames = matchGame; // kludge to let match terminate after next game.
1443 // if(gameMode != BeginningOfGame) {
1444 // DisplayError(_("You can only start a match from the initial position."), 0);
1448 if(mode == 2) appData.matchGames = appData.defaultMatchGames;
1449 /* Set up machine vs. machine match */
1451 NextTourneyGame(-1, &dummy); // sets appData.matchGames if this is tourney, to make sure ReserveGame knows it
1452 if(appData.tourneyFile[0]) {
1454 if(nextGame > appData.matchGames) {
1456 if(strchr(appData.results, '*') == NULL) {
1458 appData.tourneyCycles++;
1459 if(f = WriteTourneyFile(appData.results, NULL)) { // make a tourney file with increased number of cycles
1461 NextTourneyGame(-1, &dummy);
1463 if(nextGame <= appData.matchGames) {
1464 DisplayNote(_("You restarted an already completed tourney\nOne more cycle will now be added to it\nGames commence in 10 sec"));
1466 ScheduleDelayedEvent(NextMatchGame, 10000);
1471 snprintf(buf, MSG_SIZ, _("All games in tourney '%s' are already played or playing"), appData.tourneyFile);
1472 DisplayError(buf, 0);
1473 appData.tourneyFile[0] = 0;
1477 if (appData.noChessProgram) { // [HGM] in tourney engines are loaded automatically
1478 DisplayFatalError(_("Can't have a match with no chess programs"),
1483 matchGame = roundNr = 1;
1484 first.matchWins = second.matchWins = 0; // [HGM] match: needed in later matches
1488 char *comboLine = NULL; // [HGM] recent: WinBoard's first-engine combobox line
1491 InitBackEnd3 P((void))
1493 GameMode initialMode;
1497 InitChessProgram(&first, startedFromSetupPosition);
1499 if(!appData.noChessProgram) { /* [HGM] tidy: redo program version to use name from myname feature */
1500 free(programVersion);
1501 programVersion = (char*) malloc(8 + strlen(PACKAGE_STRING) + strlen(first.tidy));
1502 sprintf(programVersion, "%s + %s", PACKAGE_STRING, first.tidy);
1503 FloatToFront(&appData.recentEngineList, comboLine ? comboLine : appData.firstChessProgram);
1506 if (appData.icsActive) {
1508 /* [DM] Make a console window if needed [HGM] merged ifs */
1514 if (*appData.icsCommPort != NULLCHAR)
1515 len = snprintf(buf, MSG_SIZ, _("Could not open comm port %s"),
1516 appData.icsCommPort);
1518 len = snprintf(buf, MSG_SIZ, _("Could not connect to host %s, port %s"),
1519 appData.icsHost, appData.icsPort);
1521 if( (len >= MSG_SIZ) && appData.debugMode )
1522 fprintf(debugFP, "InitBackEnd3: buffer truncated.\n");
1524 DisplayFatalError(buf, err, 1);
1529 AddInputSource(icsPR, FALSE, read_from_ics, &telnetISR);
1531 AddInputSource(NoProc, FALSE, read_from_player, &fromUserISR);
1532 if(appData.keepAlive) // [HGM] alive: schedule sending of dummy 'date' command
1533 ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
1534 } else if (appData.noChessProgram) {
1540 if (*appData.cmailGameName != NULLCHAR) {
1542 OpenLoopback(&cmailPR);
1544 AddInputSource(cmailPR, FALSE, CmailSigHandlerCallBack, &cmailISR);
1548 DisplayMessage("", "");
1549 if (StrCaseCmp(appData.initialMode, "") == 0) {
1550 initialMode = BeginningOfGame;
1551 if(!appData.icsActive && appData.noChessProgram) { // [HGM] could be fall-back
1552 gameMode = MachinePlaysBlack; // "Machine Black" might have been implicitly highlighted
1553 ModeHighlight(); // make sure XBoard knows it is highlighted, so it will un-highlight it
1554 gameMode = BeginningOfGame; // in case BeginningOfGame now means "Edit Position"
1557 } else if (StrCaseCmp(appData.initialMode, "TwoMachines") == 0) {
1558 initialMode = TwoMachinesPlay;
1559 } else if (StrCaseCmp(appData.initialMode, "AnalyzeFile") == 0) {
1560 initialMode = AnalyzeFile;
1561 } else if (StrCaseCmp(appData.initialMode, "Analysis") == 0) {
1562 initialMode = AnalyzeMode;
1563 } else if (StrCaseCmp(appData.initialMode, "MachineWhite") == 0) {
1564 initialMode = MachinePlaysWhite;
1565 } else if (StrCaseCmp(appData.initialMode, "MachineBlack") == 0) {
1566 initialMode = MachinePlaysBlack;
1567 } else if (StrCaseCmp(appData.initialMode, "EditGame") == 0) {
1568 initialMode = EditGame;
1569 } else if (StrCaseCmp(appData.initialMode, "EditPosition") == 0) {
1570 initialMode = EditPosition;
1571 } else if (StrCaseCmp(appData.initialMode, "Training") == 0) {
1572 initialMode = Training;
1574 len = snprintf(buf, MSG_SIZ, _("Unknown initialMode %s"), appData.initialMode);
1575 if( (len >= MSG_SIZ) && appData.debugMode )
1576 fprintf(debugFP, "InitBackEnd3: buffer truncated.\n");
1578 DisplayFatalError(buf, 0, 2);
1582 if (appData.matchMode) {
1583 if(appData.tourneyFile[0]) { // start tourney from command line
1585 if(f = fopen(appData.tourneyFile, "r")) {
1586 ParseArgsFromFile(f); // make sure tourney parmeters re known
1588 appData.clockMode = TRUE;
1590 } else appData.tourneyFile[0] = NULLCHAR; // for now ignore bad tourney file
1593 } else if (*appData.cmailGameName != NULLCHAR) {
1594 /* Set up cmail mode */
1595 ReloadCmailMsgEvent(TRUE);
1597 /* Set up other modes */
1598 if (initialMode == AnalyzeFile) {
1599 if (*appData.loadGameFile == NULLCHAR) {
1600 DisplayFatalError(_("AnalyzeFile mode requires a game file"), 0, 1);
1604 if (*appData.loadGameFile != NULLCHAR) {
1605 (void) LoadGameFromFile(appData.loadGameFile,
1606 appData.loadGameIndex,
1607 appData.loadGameFile, TRUE);
1608 } else if (*appData.loadPositionFile != NULLCHAR) {
1609 (void) LoadPositionFromFile(appData.loadPositionFile,
1610 appData.loadPositionIndex,
1611 appData.loadPositionFile);
1612 /* [HGM] try to make self-starting even after FEN load */
1613 /* to allow automatic setup of fairy variants with wtm */
1614 if(initialMode == BeginningOfGame && !blackPlaysFirst) {
1615 gameMode = BeginningOfGame;
1616 setboardSpoiledMachineBlack = 1;
1618 /* [HGM] loadPos: make that every new game uses the setup */
1619 /* from file as long as we do not switch variant */
1620 if(!blackPlaysFirst) {
1621 startedFromPositionFile = TRUE;
1622 CopyBoard(filePosition, boards[0]);
1625 if (initialMode == AnalyzeMode) {
1626 if (appData.noChessProgram) {
1627 DisplayFatalError(_("Analysis mode requires a chess engine"), 0, 2);
1630 if (appData.icsActive) {
1631 DisplayFatalError(_("Analysis mode does not work with ICS mode"),0,2);
1635 } else if (initialMode == AnalyzeFile) {
1636 appData.showThinking = TRUE; // [HGM] thinking: moved out of ShowThinkingEvent
1637 ShowThinkingEvent();
1639 AnalysisPeriodicEvent(1);
1640 } else if (initialMode == MachinePlaysWhite) {
1641 if (appData.noChessProgram) {
1642 DisplayFatalError(_("MachineWhite mode requires a chess engine"),
1646 if (appData.icsActive) {
1647 DisplayFatalError(_("MachineWhite mode does not work with ICS mode"),
1651 MachineWhiteEvent();
1652 } else if (initialMode == MachinePlaysBlack) {
1653 if (appData.noChessProgram) {
1654 DisplayFatalError(_("MachineBlack mode requires a chess engine"),
1658 if (appData.icsActive) {
1659 DisplayFatalError(_("MachineBlack mode does not work with ICS mode"),
1663 MachineBlackEvent();
1664 } else if (initialMode == TwoMachinesPlay) {
1665 if (appData.noChessProgram) {
1666 DisplayFatalError(_("TwoMachines mode requires a chess engine"),
1670 if (appData.icsActive) {
1671 DisplayFatalError(_("TwoMachines mode does not work with ICS mode"),
1676 } else if (initialMode == EditGame) {
1678 } else if (initialMode == EditPosition) {
1679 EditPositionEvent();
1680 } else if (initialMode == Training) {
1681 if (*appData.loadGameFile == NULLCHAR) {
1682 DisplayFatalError(_("Training mode requires a game file"), 0, 2);
1691 HistorySet (char movelist[][2*MOVE_LEN], int first, int last, int current)
1693 DisplayBook(current+1);
1695 MoveHistorySet( movelist, first, last, current, pvInfoList );
1697 EvalGraphSet( first, last, current, pvInfoList );
1699 MakeEngineOutputTitle();
1703 * Establish will establish a contact to a remote host.port.
1704 * Sets icsPR to a ProcRef for a process (or pseudo-process)
1705 * used to talk to the host.
1706 * Returns 0 if okay, error code if not.
1713 if (*appData.icsCommPort != NULLCHAR) {
1714 /* Talk to the host through a serial comm port */
1715 return OpenCommPort(appData.icsCommPort, &icsPR);
1717 } else if (*appData.gateway != NULLCHAR) {
1718 if (*appData.remoteShell == NULLCHAR) {
1719 /* Use the rcmd protocol to run telnet program on a gateway host */
1720 snprintf(buf, sizeof(buf), "%s %s %s",
1721 appData.telnetProgram, appData.icsHost, appData.icsPort);
1722 return OpenRcmd(appData.gateway, appData.remoteUser, buf, &icsPR);
1725 /* Use the rsh program to run telnet program on a gateway host */
1726 if (*appData.remoteUser == NULLCHAR) {
1727 snprintf(buf, sizeof(buf), "%s %s %s %s %s", appData.remoteShell,
1728 appData.gateway, appData.telnetProgram,
1729 appData.icsHost, appData.icsPort);
1731 snprintf(buf, sizeof(buf), "%s %s -l %s %s %s %s",
1732 appData.remoteShell, appData.gateway,
1733 appData.remoteUser, appData.telnetProgram,
1734 appData.icsHost, appData.icsPort);
1736 return StartChildProcess(buf, "", &icsPR);
1739 } else if (appData.useTelnet) {
1740 return OpenTelnet(appData.icsHost, appData.icsPort, &icsPR);
1743 /* TCP socket interface differs somewhat between
1744 Unix and NT; handle details in the front end.
1746 return OpenTCP(appData.icsHost, appData.icsPort, &icsPR);
1751 EscapeExpand (char *p, char *q)
1752 { // [HGM] initstring: routine to shape up string arguments
1753 while(*p++ = *q++) if(p[-1] == '\\')
1755 case 'n': p[-1] = '\n'; break;
1756 case 'r': p[-1] = '\r'; break;
1757 case 't': p[-1] = '\t'; break;
1758 case '\\': p[-1] = '\\'; break;
1759 case 0: *p = 0; return;
1760 default: p[-1] = q[-1]; break;
1765 show_bytes (FILE *fp, char *buf, int count)
1768 if (*buf < 040 || *(unsigned char *) buf > 0177) {
1769 fprintf(fp, "\\%03o", *buf & 0xff);
1778 /* Returns an errno value */
1780 OutputMaybeTelnet (ProcRef pr, char *message, int count, int *outError)
1782 char buf[8192], *p, *q, *buflim;
1783 int left, newcount, outcount;
1785 if (*appData.icsCommPort != NULLCHAR || appData.useTelnet ||
1786 *appData.gateway != NULLCHAR) {
1787 if (appData.debugMode) {
1788 fprintf(debugFP, ">ICS: ");
1789 show_bytes(debugFP, message, count);
1790 fprintf(debugFP, "\n");
1792 return OutputToProcess(pr, message, count, outError);
1795 buflim = &buf[sizeof(buf)-1]; /* allow 1 byte for expanding last char */
1802 if (appData.debugMode) {
1803 fprintf(debugFP, ">ICS: ");
1804 show_bytes(debugFP, buf, newcount);
1805 fprintf(debugFP, "\n");
1807 outcount = OutputToProcess(pr, buf, newcount, outError);
1808 if (outcount < newcount) return -1; /* to be sure */
1815 } else if (((unsigned char) *p) == TN_IAC) {
1816 *q++ = (char) TN_IAC;
1823 if (appData.debugMode) {
1824 fprintf(debugFP, ">ICS: ");
1825 show_bytes(debugFP, buf, newcount);
1826 fprintf(debugFP, "\n");
1828 outcount = OutputToProcess(pr, buf, newcount, outError);
1829 if (outcount < newcount) return -1; /* to be sure */
1834 read_from_player (InputSourceRef isr, VOIDSTAR closure, char *message, int count, int error)
1836 int outError, outCount;
1837 static int gotEof = 0;
1839 /* Pass data read from player on to ICS */
1842 outCount = OutputMaybeTelnet(icsPR, message, count, &outError);
1843 if (outCount < count) {
1844 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1846 } else if (count < 0) {
1847 RemoveInputSource(isr);
1848 DisplayFatalError(_("Error reading from keyboard"), error, 1);
1849 } else if (gotEof++ > 0) {
1850 RemoveInputSource(isr);
1851 DisplayFatalError(_("Got end of file from keyboard"), 0, 0);
1857 { // [HGM] alive: periodically send dummy (date) command to ICS to prevent time-out
1858 if(!connectionAlive) DisplayFatalError("No response from ICS", 0, 1);
1859 connectionAlive = FALSE; // only sticks if no response to 'date' command.
1860 SendToICS("date\n");
1861 if(appData.keepAlive) ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
1864 /* added routine for printf style output to ics */
1866 ics_printf (char *format, ...)
1868 char buffer[MSG_SIZ];
1871 va_start(args, format);
1872 vsnprintf(buffer, sizeof(buffer), format, args);
1873 buffer[sizeof(buffer)-1] = '\0';
1881 int count, outCount, outError;
1883 if (icsPR == NoProc) return;
1886 outCount = OutputMaybeTelnet(icsPR, s, count, &outError);
1887 if (outCount < count) {
1888 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1892 /* This is used for sending logon scripts to the ICS. Sending
1893 without a delay causes problems when using timestamp on ICC
1894 (at least on my machine). */
1896 SendToICSDelayed (char *s, long msdelay)
1898 int count, outCount, outError;
1900 if (icsPR == NoProc) return;
1903 if (appData.debugMode) {
1904 fprintf(debugFP, ">ICS: ");
1905 show_bytes(debugFP, s, count);
1906 fprintf(debugFP, "\n");
1908 outCount = OutputToProcessDelayed(icsPR, s, count, &outError,
1910 if (outCount < count) {
1911 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1916 /* Remove all highlighting escape sequences in s
1917 Also deletes any suffix starting with '('
1920 StripHighlightAndTitle (char *s)
1922 static char retbuf[MSG_SIZ];
1925 while (*s != NULLCHAR) {
1926 while (*s == '\033') {
1927 while (*s != NULLCHAR && !isalpha(*s)) s++;
1928 if (*s != NULLCHAR) s++;
1930 while (*s != NULLCHAR && *s != '\033') {
1931 if (*s == '(' || *s == '[') {
1942 /* Remove all highlighting escape sequences in s */
1944 StripHighlight (char *s)
1946 static char retbuf[MSG_SIZ];
1949 while (*s != NULLCHAR) {
1950 while (*s == '\033') {
1951 while (*s != NULLCHAR && !isalpha(*s)) s++;
1952 if (*s != NULLCHAR) s++;
1954 while (*s != NULLCHAR && *s != '\033') {
1962 char *variantNames[] = VARIANT_NAMES;
1964 VariantName (VariantClass v)
1966 return variantNames[v];
1970 /* Identify a variant from the strings the chess servers use or the
1971 PGN Variant tag names we use. */
1973 StringToVariant (char *e)
1977 VariantClass v = VariantNormal;
1978 int i, found = FALSE;
1984 /* [HGM] skip over optional board-size prefixes */
1985 if( sscanf(e, "%dx%d_", &i, &i) == 2 ||
1986 sscanf(e, "%dx%d+%d_", &i, &i, &i) == 3 ) {
1987 while( *e++ != '_');
1990 if(StrCaseStr(e, "misc/")) { // [HGM] on FICS, misc/shogi is not shogi
1994 for (i=0; i<sizeof(variantNames)/sizeof(char*); i++) {
1995 if (StrCaseStr(e, variantNames[i])) {
1996 v = (VariantClass) i;
2003 if ((StrCaseStr(e, "fischer") && StrCaseStr(e, "random"))
2004 || StrCaseStr(e, "wild/fr")
2005 || StrCaseStr(e, "frc") || StrCaseStr(e, "960")) {
2006 v = VariantFischeRandom;
2007 } else if ((i = 4, p = StrCaseStr(e, "wild")) ||
2008 (i = 1, p = StrCaseStr(e, "w"))) {
2010 while (*p && (isspace(*p) || *p == '(' || *p == '/')) p++;
2017 case 0: /* FICS only, actually */
2019 /* Castling legal even if K starts on d-file */
2020 v = VariantWildCastle;
2025 /* Castling illegal even if K & R happen to start in
2026 normal positions. */
2027 v = VariantNoCastle;
2040 /* Castling legal iff K & R start in normal positions */
2046 /* Special wilds for position setup; unclear what to do here */
2047 v = VariantLoadable;
2050 /* Bizarre ICC game */
2051 v = VariantTwoKings;
2054 v = VariantKriegspiel;
2060 v = VariantFischeRandom;
2063 v = VariantCrazyhouse;
2066 v = VariantBughouse;
2072 /* Not quite the same as FICS suicide! */
2073 v = VariantGiveaway;
2079 v = VariantShatranj;
2082 /* Temporary names for future ICC types. The name *will* change in
2083 the next xboard/WinBoard release after ICC defines it. */
2121 v = VariantCapablanca;
2124 v = VariantKnightmate;
2130 v = VariantCylinder;
2136 v = VariantCapaRandom;
2139 v = VariantBerolina;
2151 /* Found "wild" or "w" in the string but no number;
2152 must assume it's normal chess. */
2156 len = snprintf(buf, MSG_SIZ, _("Unknown wild type %d"), wnum);
2157 if( (len >= MSG_SIZ) && appData.debugMode )
2158 fprintf(debugFP, "StringToVariant: buffer truncated.\n");
2160 DisplayError(buf, 0);
2166 if (appData.debugMode) {
2167 fprintf(debugFP, _("recognized '%s' (%d) as variant %s\n"),
2168 e, wnum, VariantName(v));
2173 static int leftover_start = 0, leftover_len = 0;
2174 char star_match[STAR_MATCH_N][MSG_SIZ];
2176 /* Test whether pattern is present at &buf[*index]; if so, return TRUE,
2177 advance *index beyond it, and set leftover_start to the new value of
2178 *index; else return FALSE. If pattern contains the character '*', it
2179 matches any sequence of characters not containing '\r', '\n', or the
2180 character following the '*' (if any), and the matched sequence(s) are
2181 copied into star_match.
2184 looking_at ( char *buf, int *index, char *pattern)
2186 char *bufp = &buf[*index], *patternp = pattern;
2188 char *matchp = star_match[0];
2191 if (*patternp == NULLCHAR) {
2192 *index = leftover_start = bufp - buf;
2196 if (*bufp == NULLCHAR) return FALSE;
2197 if (*patternp == '*') {
2198 if (*bufp == *(patternp + 1)) {
2200 matchp = star_match[++star_count];
2204 } else if (*bufp == '\n' || *bufp == '\r') {
2206 if (*patternp == NULLCHAR)
2211 *matchp++ = *bufp++;
2215 if (*patternp != *bufp) return FALSE;
2222 SendToPlayer (char *data, int length)
2224 int error, outCount;
2225 outCount = OutputToProcess(NoProc, data, length, &error);
2226 if (outCount < length) {
2227 DisplayFatalError(_("Error writing to display"), error, 1);
2232 PackHolding (char packed[], char *holding)
2242 switch (runlength) {
2253 sprintf(q, "%d", runlength);
2265 /* Telnet protocol requests from the front end */
2267 TelnetRequest (unsigned char ddww, unsigned char option)
2269 unsigned char msg[3];
2270 int outCount, outError;
2272 if (*appData.icsCommPort != NULLCHAR || appData.useTelnet) return;
2274 if (appData.debugMode) {
2275 char buf1[8], buf2[8], *ddwwStr, *optionStr;
2291 snprintf(buf1,sizeof(buf1)/sizeof(buf1[0]), "%d", ddww);
2300 snprintf(buf2,sizeof(buf2)/sizeof(buf2[0]), "%d", option);
2303 fprintf(debugFP, ">%s %s ", ddwwStr, optionStr);
2308 outCount = OutputToProcess(icsPR, (char *)msg, 3, &outError);
2310 DisplayFatalError(_("Error writing to ICS"), outError, 1);
2317 if (!appData.icsActive) return;
2318 TelnetRequest(TN_DO, TN_ECHO);
2324 if (!appData.icsActive) return;
2325 TelnetRequest(TN_DONT, TN_ECHO);
2329 CopyHoldings (Board board, char *holdings, ChessSquare lowestPiece)
2331 /* put the holdings sent to us by the server on the board holdings area */
2332 int i, j, holdingsColumn, holdingsStartRow, direction, countsColumn;
2336 if(gameInfo.holdingsWidth < 2) return;
2337 if(gameInfo.variant != VariantBughouse && board[HOLDINGS_SET])
2338 return; // prevent overwriting by pre-board holdings
2340 if( (int)lowestPiece >= BlackPawn ) {
2343 holdingsStartRow = BOARD_HEIGHT-1;
2346 holdingsColumn = BOARD_WIDTH-1;
2347 countsColumn = BOARD_WIDTH-2;
2348 holdingsStartRow = 0;
2352 for(i=0; i<BOARD_HEIGHT; i++) { /* clear holdings */
2353 board[i][holdingsColumn] = EmptySquare;
2354 board[i][countsColumn] = (ChessSquare) 0;
2356 while( (p=*holdings++) != NULLCHAR ) {
2357 piece = CharToPiece( ToUpper(p) );
2358 if(piece == EmptySquare) continue;
2359 /*j = (int) piece - (int) WhitePawn;*/
2360 j = PieceToNumber(piece);
2361 if(j >= gameInfo.holdingsSize) continue; /* ignore pieces that do not fit */
2362 if(j < 0) continue; /* should not happen */
2363 piece = (ChessSquare) ( (int)piece + (int)lowestPiece );
2364 board[holdingsStartRow+j*direction][holdingsColumn] = piece;
2365 board[holdingsStartRow+j*direction][countsColumn]++;
2371 VariantSwitch (Board board, VariantClass newVariant)
2373 int newHoldingsWidth, newWidth = 8, newHeight = 8, i, j;
2374 static Board oldBoard;
2376 startedFromPositionFile = FALSE;
2377 if(gameInfo.variant == newVariant) return;
2379 /* [HGM] This routine is called each time an assignment is made to
2380 * gameInfo.variant during a game, to make sure the board sizes
2381 * are set to match the new variant. If that means adding or deleting
2382 * holdings, we shift the playing board accordingly
2383 * This kludge is needed because in ICS observe mode, we get boards
2384 * of an ongoing game without knowing the variant, and learn about the
2385 * latter only later. This can be because of the move list we requested,
2386 * in which case the game history is refilled from the beginning anyway,
2387 * but also when receiving holdings of a crazyhouse game. In the latter
2388 * case we want to add those holdings to the already received position.
2392 if (appData.debugMode) {
2393 fprintf(debugFP, "Switch board from %s to %s\n",
2394 VariantName(gameInfo.variant), VariantName(newVariant));
2395 setbuf(debugFP, NULL);
2397 shuffleOpenings = 0; /* [HGM] shuffle */
2398 gameInfo.holdingsSize = 5; /* [HGM] prepare holdings */
2402 newWidth = 9; newHeight = 9;
2403 gameInfo.holdingsSize = 7;
2404 case VariantBughouse:
2405 case VariantCrazyhouse:
2406 newHoldingsWidth = 2; break;
2410 newHoldingsWidth = 2;
2411 gameInfo.holdingsSize = 8;
2414 case VariantCapablanca:
2415 case VariantCapaRandom:
2418 newHoldingsWidth = gameInfo.holdingsSize = 0;
2421 if(newWidth != gameInfo.boardWidth ||
2422 newHeight != gameInfo.boardHeight ||
2423 newHoldingsWidth != gameInfo.holdingsWidth ) {
2425 /* shift position to new playing area, if needed */
2426 if(newHoldingsWidth > gameInfo.holdingsWidth) {
2427 for(i=0; i<BOARD_HEIGHT; i++)
2428 for(j=BOARD_RGHT-1; j>=BOARD_LEFT; j--)
2429 board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
2431 for(i=0; i<newHeight; i++) {
2432 board[i][0] = board[i][newWidth+2*newHoldingsWidth-1] = EmptySquare;
2433 board[i][1] = board[i][newWidth+2*newHoldingsWidth-2] = (ChessSquare) 0;
2435 } else if(newHoldingsWidth < gameInfo.holdingsWidth) {
2436 for(i=0; i<BOARD_HEIGHT; i++)
2437 for(j=BOARD_LEFT; j<BOARD_RGHT; j++)
2438 board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
2441 gameInfo.boardWidth = newWidth;
2442 gameInfo.boardHeight = newHeight;
2443 gameInfo.holdingsWidth = newHoldingsWidth;
2444 gameInfo.variant = newVariant;
2445 InitDrawingSizes(-2, 0);
2446 } else gameInfo.variant = newVariant;
2447 CopyBoard(oldBoard, board); // remember correctly formatted board
2448 InitPosition(FALSE); /* this sets up board[0], but also other stuff */
2449 DrawPosition(TRUE, currentMove ? boards[currentMove] : oldBoard);
2452 static int loggedOn = FALSE;
2454 /*-- Game start info cache: --*/
2456 char gs_kind[MSG_SIZ];
2457 static char player1Name[128] = "";
2458 static char player2Name[128] = "";
2459 static char cont_seq[] = "\n\\ ";
2460 static int player1Rating = -1;
2461 static int player2Rating = -1;
2462 /*----------------------------*/
2464 ColorClass curColor = ColorNormal;
2465 int suppressKibitz = 0;
2468 Boolean soughtPending = FALSE;
2469 Boolean seekGraphUp;
2470 #define MAX_SEEK_ADS 200
2472 char *seekAdList[MAX_SEEK_ADS];
2473 int ratingList[MAX_SEEK_ADS], xList[MAX_SEEK_ADS], yList[MAX_SEEK_ADS], seekNrList[MAX_SEEK_ADS], zList[MAX_SEEK_ADS];
2474 float tcList[MAX_SEEK_ADS];
2475 char colorList[MAX_SEEK_ADS];
2476 int nrOfSeekAds = 0;
2477 int minRating = 1010, maxRating = 2800;
2478 int hMargin = 10, vMargin = 20, h, w;
2479 extern int squareSize, lineGap;
2484 int x, y, color = 0, r = ratingList[i]; float tc = tcList[i];
2485 xList[i] = yList[i] = -100; // outside graph, so cannot be clicked
2486 if(r < minRating+100 && r >=0 ) r = minRating+100;
2487 if(r > maxRating) r = maxRating;
2488 if(tc < 1.) tc = 1.;
2489 if(tc > 95.) tc = 95.;
2490 x = (w-hMargin-squareSize/8-7)* log(tc)/log(95.) + hMargin;
2491 y = ((double)r - minRating)/(maxRating - minRating)
2492 * (h-vMargin-squareSize/8-1) + vMargin;
2493 if(ratingList[i] < 0) y = vMargin + squareSize/4;
2494 if(strstr(seekAdList[i], " u ")) color = 1;
2495 if(!strstr(seekAdList[i], "lightning") && // for now all wilds same color
2496 !strstr(seekAdList[i], "bullet") &&
2497 !strstr(seekAdList[i], "blitz") &&
2498 !strstr(seekAdList[i], "standard") ) color = 2;
2499 if(strstr(seekAdList[i], "(C) ")) color |= SQUARE; // plot computer seeks as squares
2500 DrawSeekDot(xList[i]=x+3*(color&~SQUARE), yList[i]=h-1-y, colorList[i]=color);
2504 AddAd (char *handle, char *rating, int base, int inc, char rated, char *type, int nr, Boolean plot)
2506 char buf[MSG_SIZ], *ext = "";
2507 VariantClass v = StringToVariant(type);
2508 if(strstr(type, "wild")) {
2509 ext = type + 4; // append wild number
2510 if(v == VariantFischeRandom) type = "chess960"; else
2511 if(v == VariantLoadable) type = "setup"; else
2512 type = VariantName(v);
2514 snprintf(buf, MSG_SIZ, "%s (%s) %d %d %c %s%s", handle, rating, base, inc, rated, type, ext);
2515 if(nrOfSeekAds < MAX_SEEK_ADS-1) {
2516 if(seekAdList[nrOfSeekAds]) free(seekAdList[nrOfSeekAds]);
2517 ratingList[nrOfSeekAds] = -1; // for if seeker has no rating
2518 sscanf(rating, "%d", &ratingList[nrOfSeekAds]);
2519 tcList[nrOfSeekAds] = base + (2./3.)*inc;
2520 seekNrList[nrOfSeekAds] = nr;
2521 zList[nrOfSeekAds] = 0;
2522 seekAdList[nrOfSeekAds++] = StrSave(buf);
2523 if(plot) PlotSeekAd(nrOfSeekAds-1);
2528 EraseSeekDot (int i)
2530 int x = xList[i], y = yList[i], d=squareSize/4, k;
2531 DrawSeekBackground(x-squareSize/8, y-squareSize/8, x+squareSize/8+1, y+squareSize/8+1);
2532 if(x < hMargin+d) DrawSeekAxis(hMargin, y-squareSize/8, hMargin, y+squareSize/8+1);
2533 // now replot every dot that overlapped
2534 for(k=0; k<nrOfSeekAds; k++) if(k != i) {
2535 int xx = xList[k], yy = yList[k];
2536 if(xx <= x+d && xx > x-d && yy <= y+d && yy > y-d)
2537 DrawSeekDot(xx, yy, colorList[k]);
2542 RemoveSeekAd (int nr)
2545 for(i=0; i<nrOfSeekAds; i++) if(seekNrList[i] == nr) {
2547 if(seekAdList[i]) free(seekAdList[i]);
2548 seekAdList[i] = seekAdList[--nrOfSeekAds];
2549 seekNrList[i] = seekNrList[nrOfSeekAds];
2550 ratingList[i] = ratingList[nrOfSeekAds];
2551 colorList[i] = colorList[nrOfSeekAds];
2552 tcList[i] = tcList[nrOfSeekAds];
2553 xList[i] = xList[nrOfSeekAds];
2554 yList[i] = yList[nrOfSeekAds];
2555 zList[i] = zList[nrOfSeekAds];
2556 seekAdList[nrOfSeekAds] = NULL;
2562 MatchSoughtLine (char *line)
2564 char handle[MSG_SIZ], rating[MSG_SIZ], type[MSG_SIZ];
2565 int nr, base, inc, u=0; char dummy;
2567 if(sscanf(line, "%d %s %s %d %d rated %s", &nr, rating, handle, &base, &inc, type) == 6 ||
2568 sscanf(line, "%d %s %s %s %d %d rated %c", &nr, rating, handle, type, &base, &inc, &dummy) == 7 ||
2570 (sscanf(line, "%d %s %s %d %d unrated %s", &nr, rating, handle, &base, &inc, type) == 6 ||
2571 sscanf(line, "%d %s %s %s %d %d unrated %c", &nr, rating, handle, type, &base, &inc, &dummy) == 7) ) {
2572 // match: compact and save the line
2573 AddAd(handle, rating, base, inc, u ? 'u' : 'r', type, nr, FALSE);
2583 if(!seekGraphUp) return FALSE;
2584 h = BOARD_HEIGHT * (squareSize + lineGap) + lineGap;
2585 w = BOARD_WIDTH * (squareSize + lineGap) + lineGap;
2587 DrawSeekBackground(0, 0, w, h);
2588 DrawSeekAxis(hMargin, h-1-vMargin, w-5, h-1-vMargin);
2589 DrawSeekAxis(hMargin, h-1-vMargin, hMargin, 5);
2590 for(i=0; i<4000; i+= 100) if(i>=minRating && i<maxRating) {
2591 int yy =((double)i - minRating)/(maxRating - minRating)*(h-vMargin-squareSize/8-1) + vMargin;
2593 DrawSeekAxis(hMargin+5*(i%500==0), yy, hMargin-5, yy); // rating ticks
2596 snprintf(buf, MSG_SIZ, "%d", i);
2597 DrawSeekText(buf, hMargin+squareSize/8+7, yy);
2600 DrawSeekText("unrated", hMargin+squareSize/8+7, h-1-vMargin-squareSize/4);
2601 for(i=1; i<100; i+=(i<10?1:5)) {
2602 int xx = (w-hMargin-squareSize/8-7)* log((double)i)/log(95.) + hMargin;
2603 DrawSeekAxis(xx, h-1-vMargin, xx, h-6-vMargin-3*(i%10==0)); // TC ticks
2604 if(i<=5 || (i>40 ? i%20 : i%10) == 0) {
2606 snprintf(buf, MSG_SIZ, "%d", i);
2607 DrawSeekText(buf, xx-2-3*(i>9), h-1-vMargin/2);
2610 for(i=0; i<nrOfSeekAds; i++) PlotSeekAd(i);
2615 SeekGraphClick (ClickType click, int x, int y, int moving)
2617 static int lastDown = 0, displayed = 0, lastSecond;
2618 if(y < 0) return FALSE;
2619 if(!(appData.seekGraph && appData.icsActive && loggedOn &&
2620 (gameMode == BeginningOfGame || gameMode == IcsIdle))) {
2621 if(!seekGraphUp) return FALSE;
2622 seekGraphUp = FALSE; // seek graph is up when it shouldn't be: take it down
2623 DrawPosition(TRUE, NULL);
2626 if(!seekGraphUp) { // initiate cration of seek graph by requesting seek-ad list
2627 if(click == Release || moving) return FALSE;
2629 soughtPending = TRUE;
2630 SendToICS(ics_prefix);
2631 SendToICS("sought\n"); // should this be "sought all"?
2632 } else { // issue challenge based on clicked ad
2633 int dist = 10000; int i, closest = 0, second = 0;
2634 for(i=0; i<nrOfSeekAds; i++) {
2635 int d = (x-xList[i])*(x-xList[i]) + (y-yList[i])*(y-yList[i]) + zList[i];
2636 if(d < dist) { dist = d; closest = i; }
2637 second += (d - zList[i] < 120); // count in-range ads
2638 if(click == Press && moving != 1 && zList[i]>0) zList[i] *= 0.8; // age priority
2642 second = (second > 1);
2643 if(displayed != closest || second != lastSecond) {
2644 DisplayMessage(second ? "!" : "", seekAdList[closest]);
2645 lastSecond = second; displayed = closest;
2647 if(click == Press) {
2648 if(moving == 2) zList[closest] = 100; // right-click; push to back on press
2651 } // on press 'hit', only show info
2652 if(moving == 2) return TRUE; // ignore right up-clicks on dot
2653 snprintf(buf, MSG_SIZ, "play %d\n", seekNrList[closest]);
2654 SendToICS(ics_prefix);
2656 return TRUE; // let incoming board of started game pop down the graph
2657 } else if(click == Release) { // release 'miss' is ignored
2658 zList[lastDown] = 100; // make future selection of the rejected ad more difficult
2659 if(moving == 2) { // right up-click
2660 nrOfSeekAds = 0; // refresh graph
2661 soughtPending = TRUE;
2662 SendToICS(ics_prefix);
2663 SendToICS("sought\n"); // should this be "sought all"?
2666 } else if(moving) { if(displayed >= 0) DisplayMessage("", ""); displayed = -1; return TRUE; }
2667 // press miss or release hit 'pop down' seek graph
2668 seekGraphUp = FALSE;
2669 DrawPosition(TRUE, NULL);
2675 read_from_ics (InputSourceRef isr, VOIDSTAR closure, char *data, int count, int error)
2677 #define BUF_SIZE (16*1024) /* overflowed at 8K with "inchannel 1" on FICS? */
2678 #define STARTED_NONE 0
2679 #define STARTED_MOVES 1
2680 #define STARTED_BOARD 2
2681 #define STARTED_OBSERVE 3
2682 #define STARTED_HOLDINGS 4
2683 #define STARTED_CHATTER 5
2684 #define STARTED_COMMENT 6
2685 #define STARTED_MOVES_NOHIDE 7
2687 static int started = STARTED_NONE;
2688 static char parse[20000];
2689 static int parse_pos = 0;
2690 static char buf[BUF_SIZE + 1];
2691 static int firstTime = TRUE, intfSet = FALSE;
2692 static ColorClass prevColor = ColorNormal;
2693 static int savingComment = FALSE;
2694 static int cmatch = 0; // continuation sequence match
2701 int backup; /* [DM] For zippy color lines */
2703 char talker[MSG_SIZ]; // [HGM] chat
2706 connectionAlive = TRUE; // [HGM] alive: I think, therefore I am...
2708 if (appData.debugMode) {
2710 fprintf(debugFP, "<ICS: ");
2711 show_bytes(debugFP, data, count);
2712 fprintf(debugFP, "\n");
2716 if (appData.debugMode) { int f = forwardMostMove;
2717 fprintf(debugFP, "ics input %d, castling = %d %d %d %d %d %d\n", f,
2718 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
2719 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
2722 /* If last read ended with a partial line that we couldn't parse,
2723 prepend it to the new read and try again. */
2724 if (leftover_len > 0) {
2725 for (i=0; i<leftover_len; i++)
2726 buf[i] = buf[leftover_start + i];
2729 /* copy new characters into the buffer */
2730 bp = buf + leftover_len;
2731 buf_len=leftover_len;
2732 for (i=0; i<count; i++)
2735 if (data[i] == '\r')
2738 // join lines split by ICS?
2739 if (!appData.noJoin)
2742 Joining just consists of finding matches against the
2743 continuation sequence, and discarding that sequence
2744 if found instead of copying it. So, until a match
2745 fails, there's nothing to do since it might be the
2746 complete sequence, and thus, something we don't want
2749 if (data[i] == cont_seq[cmatch])
2752 if (cmatch == strlen(cont_seq))
2754 cmatch = 0; // complete match. just reset the counter
2757 it's possible for the ICS to not include the space
2758 at the end of the last word, making our [correct]
2759 join operation fuse two separate words. the server
2760 does this when the space occurs at the width setting.
2762 if (!buf_len || buf[buf_len-1] != ' ')
2773 match failed, so we have to copy what matched before
2774 falling through and copying this character. In reality,
2775 this will only ever be just the newline character, but
2776 it doesn't hurt to be precise.
2778 strncpy(bp, cont_seq, cmatch);
2790 buf[buf_len] = NULLCHAR;
2791 // next_out = leftover_len; // [HGM] should we set this to 0, and not print it in advance?
2796 while (i < buf_len) {
2797 /* Deal with part of the TELNET option negotiation
2798 protocol. We refuse to do anything beyond the
2799 defaults, except that we allow the WILL ECHO option,
2800 which ICS uses to turn off password echoing when we are
2801 directly connected to it. We reject this option
2802 if localLineEditing mode is on (always on in xboard)
2803 and we are talking to port 23, which might be a real
2804 telnet server that will try to keep WILL ECHO on permanently.
2806 if (buf_len - i >= 3 && (unsigned char) buf[i] == TN_IAC) {
2807 static int remoteEchoOption = FALSE; /* telnet ECHO option */
2808 unsigned char option;
2810 switch ((unsigned char) buf[++i]) {
2812 if (appData.debugMode)
2813 fprintf(debugFP, "\n<WILL ");
2814 switch (option = (unsigned char) buf[++i]) {
2816 if (appData.debugMode)
2817 fprintf(debugFP, "ECHO ");
2818 /* Reply only if this is a change, according
2819 to the protocol rules. */
2820 if (remoteEchoOption) break;
2821 if (appData.localLineEditing &&
2822 atoi(appData.icsPort) == TN_PORT) {
2823 TelnetRequest(TN_DONT, TN_ECHO);
2826 TelnetRequest(TN_DO, TN_ECHO);
2827 remoteEchoOption = TRUE;
2831 if (appData.debugMode)
2832 fprintf(debugFP, "%d ", option);
2833 /* Whatever this is, we don't want it. */
2834 TelnetRequest(TN_DONT, option);
2839 if (appData.debugMode)
2840 fprintf(debugFP, "\n<WONT ");
2841 switch (option = (unsigned char) buf[++i]) {
2843 if (appData.debugMode)
2844 fprintf(debugFP, "ECHO ");
2845 /* Reply only if this is a change, according
2846 to the protocol rules. */
2847 if (!remoteEchoOption) break;
2849 TelnetRequest(TN_DONT, TN_ECHO);
2850 remoteEchoOption = FALSE;
2853 if (appData.debugMode)
2854 fprintf(debugFP, "%d ", (unsigned char) option);
2855 /* Whatever this is, it must already be turned
2856 off, because we never agree to turn on
2857 anything non-default, so according to the
2858 protocol rules, we don't reply. */
2863 if (appData.debugMode)
2864 fprintf(debugFP, "\n<DO ");
2865 switch (option = (unsigned char) buf[++i]) {
2867 /* Whatever this is, we refuse to do it. */
2868 if (appData.debugMode)
2869 fprintf(debugFP, "%d ", option);
2870 TelnetRequest(TN_WONT, option);
2875 if (appData.debugMode)
2876 fprintf(debugFP, "\n<DONT ");
2877 switch (option = (unsigned char) buf[++i]) {
2879 if (appData.debugMode)
2880 fprintf(debugFP, "%d ", option);
2881 /* Whatever this is, we are already not doing
2882 it, because we never agree to do anything
2883 non-default, so according to the protocol
2884 rules, we don't reply. */
2889 if (appData.debugMode)
2890 fprintf(debugFP, "\n<IAC ");
2891 /* Doubled IAC; pass it through */
2895 if (appData.debugMode)
2896 fprintf(debugFP, "\n<%d ", (unsigned char) buf[i]);
2897 /* Drop all other telnet commands on the floor */
2900 if (oldi > next_out)
2901 SendToPlayer(&buf[next_out], oldi - next_out);
2907 /* OK, this at least will *usually* work */
2908 if (!loggedOn && looking_at(buf, &i, "ics%")) {
2912 if (loggedOn && !intfSet) {
2913 if (ics_type == ICS_ICC) {
2914 snprintf(str, MSG_SIZ,
2915 "/set-quietly interface %s\n/set-quietly style 12\n",
2917 if(appData.seekGraph && appData.autoRefresh) // [HGM] seekgraph
2918 strcat(str, "/set-2 51 1\n/set seek 1\n");
2919 } else if (ics_type == ICS_CHESSNET) {
2920 snprintf(str, MSG_SIZ, "/style 12\n");
2922 safeStrCpy(str, "alias $ @\n$set interface ", sizeof(str)/sizeof(str[0]));
2923 strcat(str, programVersion);
2924 strcat(str, "\n$iset startpos 1\n$iset ms 1\n");
2925 if(appData.seekGraph && appData.autoRefresh) // [HGM] seekgraph
2926 strcat(str, "$iset seekremove 1\n$set seek 1\n");
2928 strcat(str, "$iset nohighlight 1\n");
2930 strcat(str, "$iset lock 1\n$style 12\n");
2933 NotifyFrontendLogin();
2937 if (started == STARTED_COMMENT) {
2938 /* Accumulate characters in comment */
2939 parse[parse_pos++] = buf[i];
2940 if (buf[i] == '\n') {
2941 parse[parse_pos] = NULLCHAR;
2942 if(chattingPartner>=0) {
2944 snprintf(mess, MSG_SIZ, "%s%s", talker, parse);
2945 OutputChatMessage(chattingPartner, mess);
2946 chattingPartner = -1;
2947 next_out = i+1; // [HGM] suppress printing in ICS window
2949 if(!suppressKibitz) // [HGM] kibitz
2950 AppendComment(forwardMostMove, StripHighlight(parse), TRUE);
2951 else { // [HGM kibitz: divert memorized engine kibitz to engine-output window
2952 int nrDigit = 0, nrAlph = 0, j;
2953 if(parse_pos > MSG_SIZ - 30) // defuse unreasonably long input
2954 { parse_pos = MSG_SIZ-30; parse[parse_pos - 1] = '\n'; }
2955 parse[parse_pos] = NULLCHAR;
2956 // try to be smart: if it does not look like search info, it should go to
2957 // ICS interaction window after all, not to engine-output window.
2958 for(j=0; j<parse_pos; j++) { // count letters and digits
2959 nrDigit += (parse[j] >= '0' && parse[j] <= '9');
2960 nrAlph += (parse[j] >= 'a' && parse[j] <= 'z');
2961 nrAlph += (parse[j] >= 'A' && parse[j] <= 'Z');
2963 if(nrAlph < 9*nrDigit) { // if more than 10% digit we assume search info
2964 int depth=0; float score;
2965 if(sscanf(parse, "!!! %f/%d", &score, &depth) == 2 && depth>0) {
2966 // [HGM] kibitz: save kibitzed opponent info for PGN and eval graph
2967 pvInfoList[forwardMostMove-1].depth = depth;
2968 pvInfoList[forwardMostMove-1].score = 100*score;
2970 OutputKibitz(suppressKibitz, parse);
2973 if(gameMode == IcsObserving) // restore original ICS messages
2974 snprintf(tmp, MSG_SIZ, "%s kibitzes: %s", star_match[0], parse);
2976 snprintf(tmp, MSG_SIZ, _("your opponent kibitzes: %s"), parse);
2977 SendToPlayer(tmp, strlen(tmp));
2979 next_out = i+1; // [HGM] suppress printing in ICS window
2981 started = STARTED_NONE;
2983 /* Don't match patterns against characters in comment */
2988 if (started == STARTED_CHATTER) {
2989 if (buf[i] != '\n') {
2990 /* Don't match patterns against characters in chatter */
2994 started = STARTED_NONE;
2995 if(suppressKibitz) next_out = i+1;
2998 /* Kludge to deal with rcmd protocol */
2999 if (firstTime && looking_at(buf, &i, "\001*")) {
3000 DisplayFatalError(&buf[1], 0, 1);
3006 if (!loggedOn && looking_at(buf, &i, "chessclub.com")) {
3009 if (appData.debugMode)
3010 fprintf(debugFP, "ics_type %d\n", ics_type);
3013 if (!loggedOn && looking_at(buf, &i, "freechess.org")) {
3014 ics_type = ICS_FICS;
3016 if (appData.debugMode)
3017 fprintf(debugFP, "ics_type %d\n", ics_type);
3020 if (!loggedOn && looking_at(buf, &i, "chess.net")) {
3021 ics_type = ICS_CHESSNET;
3023 if (appData.debugMode)
3024 fprintf(debugFP, "ics_type %d\n", ics_type);
3029 (looking_at(buf, &i, "\"*\" is *a registered name") ||
3030 looking_at(buf, &i, "Logging you in as \"*\"") ||
3031 looking_at(buf, &i, "will be \"*\""))) {
3032 safeStrCpy(ics_handle, star_match[0], sizeof(ics_handle)/sizeof(ics_handle[0]));
3036 if (loggedOn && !have_set_title && ics_handle[0] != NULLCHAR) {
3038 snprintf(buf, sizeof(buf), "%s@%s", ics_handle, appData.icsHost);
3039 DisplayIcsInteractionTitle(buf);
3040 have_set_title = TRUE;
3043 /* skip finger notes */
3044 if (started == STARTED_NONE &&
3045 ((buf[i] == ' ' && isdigit(buf[i+1])) ||
3046 (buf[i] == '1' && buf[i+1] == '0')) &&
3047 buf[i+2] == ':' && buf[i+3] == ' ') {
3048 started = STARTED_CHATTER;
3054 // [HGM] seekgraph: recognize sought lines and end-of-sought message
3055 if(appData.seekGraph) {
3056 if(soughtPending && MatchSoughtLine(buf+i)) {
3057 i = strstr(buf+i, "rated") - buf;
3058 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3059 next_out = leftover_start = i;
3060 started = STARTED_CHATTER;
3061 suppressKibitz = TRUE;
3064 if((gameMode == IcsIdle || gameMode == BeginningOfGame)
3065 && looking_at(buf, &i, "* ads displayed")) {
3066 soughtPending = FALSE;
3071 if(appData.autoRefresh) {
3072 if(looking_at(buf, &i, "* (*) seeking * * * * *\"play *\" to respond)\n")) {
3073 int s = (ics_type == ICS_ICC); // ICC format differs
3075 AddAd(star_match[0], star_match[1], atoi(star_match[2+s]), atoi(star_match[3+s]),
3076 star_match[4+s][0], star_match[5-3*s], atoi(star_match[7]), TRUE);
3077 looking_at(buf, &i, "*% "); // eat prompt
3078 if(oldi > 0 && buf[oldi-1] == '\n') oldi--; // suppress preceding LF, if any
3079 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3080 next_out = i; // suppress
3083 if(looking_at(buf, &i, "\nAds removed: *\n") || looking_at(buf, &i, "\031(51 * *\031)")) {
3084 char *p = star_match[0];
3086 if(seekGraphUp) RemoveSeekAd(atoi(p));
3087 while(*p && *p++ != ' '); // next
3089 looking_at(buf, &i, "*% "); // eat prompt
3090 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3097 /* skip formula vars */
3098 if (started == STARTED_NONE &&
3099 buf[i] == 'f' && isdigit(buf[i+1]) && buf[i+2] == ':') {
3100 started = STARTED_CHATTER;
3105 // [HGM] kibitz: try to recognize opponent engine-score kibitzes, to divert them to engine-output window
3106 if (appData.autoKibitz && started == STARTED_NONE &&
3107 !appData.icsEngineAnalyze && // [HGM] [DM] ICS analyze
3108 (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack || gameMode == IcsObserving)) {
3109 if((looking_at(buf, &i, "\n* kibitzes: ") || looking_at(buf, &i, "\n* whispers: ") ||
3110 looking_at(buf, &i, "* kibitzes: ") || looking_at(buf, &i, "* whispers: ")) &&
3111 (StrStr(star_match[0], gameInfo.white) == star_match[0] ||
3112 StrStr(star_match[0], gameInfo.black) == star_match[0] )) { // kibitz of self or opponent
3113 suppressKibitz = TRUE;
3114 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3116 if((StrStr(star_match[0], gameInfo.white) == star_match[0]
3117 && (gameMode == IcsPlayingWhite)) ||
3118 (StrStr(star_match[0], gameInfo.black) == star_match[0]
3119 && (gameMode == IcsPlayingBlack)) ) // opponent kibitz
3120 started = STARTED_CHATTER; // own kibitz we simply discard
3122 started = STARTED_COMMENT; // make sure it will be collected in parse[]
3123 parse_pos = 0; parse[0] = NULLCHAR;
3124 savingComment = TRUE;
3125 suppressKibitz = gameMode != IcsObserving ? 2 :
3126 (StrStr(star_match[0], gameInfo.white) == NULL) + 1;
3130 if((looking_at(buf, &i, "\nkibitzed to *\n") || looking_at(buf, &i, "kibitzed to *\n") ||
3131 looking_at(buf, &i, "\n(kibitzed to *\n") || looking_at(buf, &i, "(kibitzed to *\n"))
3132 && atoi(star_match[0])) {
3133 // suppress the acknowledgements of our own autoKibitz
3135 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3136 if(p = strchr(star_match[0], ' ')) p[1] = NULLCHAR; // clip off "players)" on FICS
3137 SendToPlayer(star_match[0], strlen(star_match[0]));
3138 if(looking_at(buf, &i, "*% ")) // eat prompt
3139 suppressKibitz = FALSE;
3143 } // [HGM] kibitz: end of patch
3145 if(looking_at(buf, &i, "* rating adjustment: * --> *\n")) continue;
3147 // [HGM] chat: intercept tells by users for which we have an open chat window
3149 if(started == STARTED_NONE && (looking_at(buf, &i, "* tells you:") || looking_at(buf, &i, "* says:") ||
3150 looking_at(buf, &i, "* whispers:") ||
3151 looking_at(buf, &i, "* kibitzes:") ||
3152 looking_at(buf, &i, "* shouts:") ||
3153 looking_at(buf, &i, "* c-shouts:") ||
3154 looking_at(buf, &i, "--> * ") ||
3155 looking_at(buf, &i, "*(*):") && (sscanf(star_match[1], "%d", &channel),1) ||
3156 looking_at(buf, &i, "*(*)(*):") && (sscanf(star_match[2], "%d", &channel),1) ||
3157 looking_at(buf, &i, "*(*)(*)(*):") && (sscanf(star_match[3], "%d", &channel),1) ||
3158 looking_at(buf, &i, "*(*)(*)(*)(*):") && sscanf(star_match[4], "%d", &channel) == 1 )) {
3160 sscanf(star_match[0], "%[^(]", talker+1); // strip (C) or (U) off ICS handle
3161 chattingPartner = -1;
3163 if(channel >= 0) // channel broadcast; look if there is a chatbox for this channel
3164 for(p=0; p<MAX_CHAT; p++) {
3165 if(chatPartner[p][0] >= '0' && chatPartner[p][0] <= '9' && channel == atoi(chatPartner[p])) {
3166 talker[0] = '['; strcat(talker, "] ");
3167 Colorize(channel == 1 ? ColorChannel1 : ColorChannel, FALSE);
3168 chattingPartner = p; break;
3171 if(buf[i-3] == 'e') // kibitz; look if there is a KIBITZ chatbox
3172 for(p=0; p<MAX_CHAT; p++) {
3173 if(!strcmp("kibitzes", chatPartner[p])) {
3174 talker[0] = '['; strcat(talker, "] ");
3175 chattingPartner = p; break;
3178 if(buf[i-3] == 'r') // whisper; look if there is a WHISPER chatbox
3179 for(p=0; p<MAX_CHAT; p++) {
3180 if(!strcmp("whispers", chatPartner[p])) {
3181 talker[0] = '['; strcat(talker, "] ");
3182 chattingPartner = p; break;
3185 if(buf[i-3] == 't' || buf[oldi+2] == '>') {// shout, c-shout or it; look if there is a 'shouts' chatbox
3186 if(buf[i-8] == '-' && buf[i-3] == 't')
3187 for(p=0; p<MAX_CHAT; p++) { // c-shout; check if dedicatesd c-shout box exists
3188 if(!strcmp("c-shouts", chatPartner[p])) {
3189 talker[0] = '('; strcat(talker, ") "); Colorize(ColorSShout, FALSE);
3190 chattingPartner = p; break;
3193 if(chattingPartner < 0)
3194 for(p=0; p<MAX_CHAT; p++) {
3195 if(!strcmp("shouts", chatPartner[p])) {
3196 if(buf[oldi+2] == '>') { talker[0] = '<'; strcat(talker, "> "); Colorize(ColorShout, FALSE); }
3197 else if(buf[i-8] == '-') { talker[0] = '('; strcat(talker, ") "); Colorize(ColorSShout, FALSE); }
3198 else { talker[0] = '['; strcat(talker, "] "); Colorize(ColorShout, FALSE); }
3199 chattingPartner = p; break;
3203 if(chattingPartner<0) // if not, look if there is a chatbox for this indivdual
3204 for(p=0; p<MAX_CHAT; p++) if(!StrCaseCmp(talker+1, chatPartner[p])) {
3205 talker[0] = 0; Colorize(ColorTell, FALSE);
3206 chattingPartner = p; break;
3208 if(chattingPartner<0) i = oldi; else {
3209 Colorize(curColor, TRUE); // undo the bogus colorations we just made to trigger the souds
3210 if(oldi > 0 && buf[oldi-1] == '\n') oldi--;
3211 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3212 started = STARTED_COMMENT;
3213 parse_pos = 0; parse[0] = NULLCHAR;
3214 savingComment = 3 + chattingPartner; // counts as TRUE
3215 suppressKibitz = TRUE;
3218 } // [HGM] chat: end of patch
3221 if (appData.zippyTalk || appData.zippyPlay) {
3222 /* [DM] Backup address for color zippy lines */
3224 if (loggedOn == TRUE)
3225 if (ZippyControl(buf, &backup) || ZippyConverse(buf, &backup) ||
3226 (appData.zippyPlay && ZippyMatch(buf, &backup)));
3228 } // [DM] 'else { ' deleted
3230 /* Regular tells and says */
3231 (tkind = 1, looking_at(buf, &i, "* tells you: ")) ||
3232 looking_at(buf, &i, "* (your partner) tells you: ") ||
3233 looking_at(buf, &i, "* says: ") ||
3234 /* Don't color "message" or "messages" output */
3235 (tkind = 5, looking_at(buf, &i, "*. * (*:*): ")) ||
3236 looking_at(buf, &i, "*. * at *:*: ") ||
3237 looking_at(buf, &i, "--* (*:*): ") ||
3238 /* Message notifications (same color as tells) */
3239 looking_at(buf, &i, "* has left a message ") ||
3240 looking_at(buf, &i, "* just sent you a message:\n") ||
3241 /* Whispers and kibitzes */
3242 (tkind = 2, looking_at(buf, &i, "* whispers: ")) ||
3243 looking_at(buf, &i, "* kibitzes: ") ||
3245 (tkind = 3, looking_at(buf, &i, "*(*: "))) {
3247 if (tkind == 1 && strchr(star_match[0], ':')) {
3248 /* Avoid "tells you:" spoofs in channels */
3251 if (star_match[0][0] == NULLCHAR ||
3252 strchr(star_match[0], ' ') ||
3253 (tkind == 3 && strchr(star_match[1], ' '))) {
3254 /* Reject bogus matches */
3257 if (appData.colorize) {
3258 if (oldi > next_out) {
3259 SendToPlayer(&buf[next_out], oldi - next_out);
3264 Colorize(ColorTell, FALSE);
3265 curColor = ColorTell;
3268 Colorize(ColorKibitz, FALSE);
3269 curColor = ColorKibitz;
3272 p = strrchr(star_match[1], '(');
3279 Colorize(ColorChannel1, FALSE);
3280 curColor = ColorChannel1;
3282 Colorize(ColorChannel, FALSE);
3283 curColor = ColorChannel;
3287 curColor = ColorNormal;
3291 if (started == STARTED_NONE && appData.autoComment &&
3292 (gameMode == IcsObserving ||
3293 gameMode == IcsPlayingWhite ||
3294 gameMode == IcsPlayingBlack)) {
3295 parse_pos = i - oldi;
3296 memcpy(parse, &buf[oldi], parse_pos);
3297 parse[parse_pos] = NULLCHAR;
3298 started = STARTED_COMMENT;
3299 savingComment = TRUE;
3301 started = STARTED_CHATTER;
3302 savingComment = FALSE;
3309 if (looking_at(buf, &i, "* s-shouts: ") ||
3310 looking_at(buf, &i, "* c-shouts: ")) {
3311 if (appData.colorize) {
3312 if (oldi > next_out) {
3313 SendToPlayer(&buf[next_out], oldi - next_out);
3316 Colorize(ColorSShout, FALSE);
3317 curColor = ColorSShout;
3320 started = STARTED_CHATTER;
3324 if (looking_at(buf, &i, "--->")) {
3329 if (looking_at(buf, &i, "* shouts: ") ||
3330 looking_at(buf, &i, "--> ")) {
3331 if (appData.colorize) {
3332 if (oldi > next_out) {
3333 SendToPlayer(&buf[next_out], oldi - next_out);
3336 Colorize(ColorShout, FALSE);
3337 curColor = ColorShout;
3340 started = STARTED_CHATTER;
3344 if (looking_at( buf, &i, "Challenge:")) {
3345 if (appData.colorize) {
3346 if (oldi > next_out) {
3347 SendToPlayer(&buf[next_out], oldi - next_out);
3350 Colorize(ColorChallenge, FALSE);
3351 curColor = ColorChallenge;
3357 if (looking_at(buf, &i, "* offers you") ||
3358 looking_at(buf, &i, "* offers to be") ||
3359 looking_at(buf, &i, "* would like to") ||
3360 looking_at(buf, &i, "* requests to") ||
3361 looking_at(buf, &i, "Your opponent offers") ||
3362 looking_at(buf, &i, "Your opponent requests")) {
3364 if (appData.colorize) {
3365 if (oldi > next_out) {
3366 SendToPlayer(&buf[next_out], oldi - next_out);
3369 Colorize(ColorRequest, FALSE);
3370 curColor = ColorRequest;
3375 if (looking_at(buf, &i, "* (*) seeking")) {
3376 if (appData.colorize) {
3377 if (oldi > next_out) {
3378 SendToPlayer(&buf[next_out], oldi - next_out);
3381 Colorize(ColorSeek, FALSE);
3382 curColor = ColorSeek;
3387 if(i < backup) { i = backup; continue; } // [HGM] for if ZippyControl matches, but the colorie code doesn't
3389 if (looking_at(buf, &i, "\\ ")) {
3390 if (prevColor != ColorNormal) {
3391 if (oldi > next_out) {
3392 SendToPlayer(&buf[next_out], oldi - next_out);
3395 Colorize(prevColor, TRUE);
3396 curColor = prevColor;
3398 if (savingComment) {
3399 parse_pos = i - oldi;
3400 memcpy(parse, &buf[oldi], parse_pos);
3401 parse[parse_pos] = NULLCHAR;
3402 started = STARTED_COMMENT;
3403 if(savingComment >= 3) // [HGM] chat: continuation of line for chat box
3404 chattingPartner = savingComment - 3; // kludge to remember the box
3406 started = STARTED_CHATTER;
3411 if (looking_at(buf, &i, "Black Strength :") ||
3412 looking_at(buf, &i, "<<< style 10 board >>>") ||
3413 looking_at(buf, &i, "<10>") ||
3414 looking_at(buf, &i, "#@#")) {
3415 /* Wrong board style */
3417 SendToICS(ics_prefix);
3418 SendToICS("set style 12\n");
3419 SendToICS(ics_prefix);
3420 SendToICS("refresh\n");
3424 if (!have_sent_ICS_logon && looking_at(buf, &i, "login:")) {
3426 have_sent_ICS_logon = 1;
3430 if (ics_getting_history != H_GETTING_MOVES /*smpos kludge*/ &&
3431 (looking_at(buf, &i, "\n<12> ") ||
3432 looking_at(buf, &i, "<12> "))) {
3434 if (oldi > next_out) {
3435 SendToPlayer(&buf[next_out], oldi - next_out);
3438 started = STARTED_BOARD;
3443 if ((started == STARTED_NONE && looking_at(buf, &i, "\n<b1> ")) ||
3444 looking_at(buf, &i, "<b1> ")) {
3445 if (oldi > next_out) {
3446 SendToPlayer(&buf[next_out], oldi - next_out);
3449 started = STARTED_HOLDINGS;
3454 if (looking_at(buf, &i, "* *vs. * *--- *")) {
3456 /* Header for a move list -- first line */
3458 switch (ics_getting_history) {
3462 case BeginningOfGame:
3463 /* User typed "moves" or "oldmoves" while we
3464 were idle. Pretend we asked for these
3465 moves and soak them up so user can step
3466 through them and/or save them.
3469 gameMode = IcsObserving;
3472 ics_getting_history = H_GOT_UNREQ_HEADER;
3474 case EditGame: /*?*/
3475 case EditPosition: /*?*/
3476 /* Should above feature work in these modes too? */
3477 /* For now it doesn't */
3478 ics_getting_history = H_GOT_UNWANTED_HEADER;
3481 ics_getting_history = H_GOT_UNWANTED_HEADER;
3486 /* Is this the right one? */
3487 if (gameInfo.white && gameInfo.black &&
3488 strcmp(gameInfo.white, star_match[0]) == 0 &&
3489 strcmp(gameInfo.black, star_match[2]) == 0) {
3491 ics_getting_history = H_GOT_REQ_HEADER;
3494 case H_GOT_REQ_HEADER:
3495 case H_GOT_UNREQ_HEADER:
3496 case H_GOT_UNWANTED_HEADER:
3497 case H_GETTING_MOVES:
3498 /* Should not happen */
3499 DisplayError(_("Error gathering move list: two headers"), 0);
3500 ics_getting_history = H_FALSE;
3504 /* Save player ratings into gameInfo if needed */
3505 if ((ics_getting_history == H_GOT_REQ_HEADER ||
3506 ics_getting_history == H_GOT_UNREQ_HEADER) &&
3507 (gameInfo.whiteRating == -1 ||
3508 gameInfo.blackRating == -1)) {
3510 gameInfo.whiteRating = string_to_rating(star_match[1]);
3511 gameInfo.blackRating = string_to_rating(star_match[3]);
3512 if (appData.debugMode)
3513 fprintf(debugFP, _("Ratings from header: W %d, B %d\n"),
3514 gameInfo.whiteRating, gameInfo.blackRating);
3519 if (looking_at(buf, &i,
3520 "* * match, initial time: * minute*, increment: * second")) {
3521 /* Header for a move list -- second line */
3522 /* Initial board will follow if this is a wild game */
3523 if (gameInfo.event != NULL) free(gameInfo.event);
3524 snprintf(str, MSG_SIZ, "ICS %s %s match", star_match[0], star_match[1]);
3525 gameInfo.event = StrSave(str);
3526 /* [HGM] we switched variant. Translate boards if needed. */
3527 VariantSwitch(boards[currentMove], StringToVariant(gameInfo.event));
3531 if (looking_at(buf, &i, "Move ")) {
3532 /* Beginning of a move list */
3533 switch (ics_getting_history) {
3535 /* Normally should not happen */
3536 /* Maybe user hit reset while we were parsing */
3539 /* Happens if we are ignoring a move list that is not
3540 * the one we just requested. Common if the user
3541 * tries to observe two games without turning off
3544 case H_GETTING_MOVES:
3545 /* Should not happen */
3546 DisplayError(_("Error gathering move list: nested"), 0);
3547 ics_getting_history = H_FALSE;
3549 case H_GOT_REQ_HEADER:
3550 ics_getting_history = H_GETTING_MOVES;
3551 started = STARTED_MOVES;
3553 if (oldi > next_out) {
3554 SendToPlayer(&buf[next_out], oldi - next_out);
3557 case H_GOT_UNREQ_HEADER:
3558 ics_getting_history = H_GETTING_MOVES;
3559 started = STARTED_MOVES_NOHIDE;
3562 case H_GOT_UNWANTED_HEADER:
3563 ics_getting_history = H_FALSE;
3569 if (looking_at(buf, &i, "% ") ||
3570 ((started == STARTED_MOVES || started == STARTED_MOVES_NOHIDE)
3571 && looking_at(buf, &i, "}*"))) { char *bookHit = NULL; // [HGM] book
3572 if(soughtPending && nrOfSeekAds) { // [HGM] seekgraph: on ICC sought-list has no termination line
3573 soughtPending = FALSE;
3577 if(suppressKibitz) next_out = i;
3578 savingComment = FALSE;
3582 case STARTED_MOVES_NOHIDE:
3583 memcpy(&parse[parse_pos], &buf[oldi], i - oldi);
3584 parse[parse_pos + i - oldi] = NULLCHAR;
3585 ParseGameHistory(parse);
3587 if (appData.zippyPlay && first.initDone) {
3588 FeedMovesToProgram(&first, forwardMostMove);
3589 if (gameMode == IcsPlayingWhite) {
3590 if (WhiteOnMove(forwardMostMove)) {
3591 if (first.sendTime) {
3592 if (first.useColors) {
3593 SendToProgram("black\n", &first);
3595 SendTimeRemaining(&first, TRUE);
3597 if (first.useColors) {
3598 SendToProgram("white\n", &first); // [HGM] book: made sending of "go\n" book dependent
3600 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: probe book for initial pos
3601 first.maybeThinking = TRUE;
3603 if (first.usePlayother) {
3604 if (first.sendTime) {
3605 SendTimeRemaining(&first, TRUE);
3607 SendToProgram("playother\n", &first);
3613 } else if (gameMode == IcsPlayingBlack) {
3614 if (!WhiteOnMove(forwardMostMove)) {
3615 if (first.sendTime) {
3616 if (first.useColors) {
3617 SendToProgram("white\n", &first);
3619 SendTimeRemaining(&first, FALSE);
3621 if (first.useColors) {
3622 SendToProgram("black\n", &first);
3624 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE);
3625 first.maybeThinking = TRUE;
3627 if (first.usePlayother) {
3628 if (first.sendTime) {
3629 SendTimeRemaining(&first, FALSE);
3631 SendToProgram("playother\n", &first);
3640 if (gameMode == IcsObserving && ics_gamenum == -1) {
3641 /* Moves came from oldmoves or moves command
3642 while we weren't doing anything else.
3644 currentMove = forwardMostMove;
3645 ClearHighlights();/*!!could figure this out*/
3646 flipView = appData.flipView;
3647 DrawPosition(TRUE, boards[currentMove]);
3648 DisplayBothClocks();
3649 snprintf(str, MSG_SIZ, "%s %s %s",
3650 gameInfo.white, _("vs."), gameInfo.black);
3654 /* Moves were history of an active game */
3655 if (gameInfo.resultDetails != NULL) {
3656 free(gameInfo.resultDetails);
3657 gameInfo.resultDetails = NULL;
3660 HistorySet(parseList, backwardMostMove,
3661 forwardMostMove, currentMove-1);
3662 DisplayMove(currentMove - 1);
3663 if (started == STARTED_MOVES) next_out = i;
3664 started = STARTED_NONE;
3665 ics_getting_history = H_FALSE;
3668 case STARTED_OBSERVE:
3669 started = STARTED_NONE;
3670 SendToICS(ics_prefix);
3671 SendToICS("refresh\n");
3677 if(bookHit) { // [HGM] book: simulate book reply
3678 static char bookMove[MSG_SIZ]; // a bit generous?
3680 programStats.nodes = programStats.depth = programStats.time =
3681 programStats.score = programStats.got_only_move = 0;
3682 sprintf(programStats.movelist, "%s (xbook)", bookHit);
3684 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
3685 strcat(bookMove, bookHit);
3686 HandleMachineMove(bookMove, &first);
3691 if ((started == STARTED_MOVES || started == STARTED_BOARD ||
3692 started == STARTED_HOLDINGS ||
3693 started == STARTED_MOVES_NOHIDE) && i >= leftover_len) {
3694 /* Accumulate characters in move list or board */
3695 parse[parse_pos++] = buf[i];
3698 /* Start of game messages. Mostly we detect start of game
3699 when the first board image arrives. On some versions
3700 of the ICS, though, we need to do a "refresh" after starting
3701 to observe in order to get the current board right away. */
3702 if (looking_at(buf, &i, "Adding game * to observation list")) {
3703 started = STARTED_OBSERVE;
3707 /* Handle auto-observe */
3708 if (appData.autoObserve &&
3709 (gameMode == IcsIdle || gameMode == BeginningOfGame) &&
3710 looking_at(buf, &i, "Game notification: * (*) vs. * (*)")) {
3712 /* Choose the player that was highlighted, if any. */
3713 if (star_match[0][0] == '\033' ||
3714 star_match[1][0] != '\033') {
3715 player = star_match[0];
3717 player = star_match[2];
3719 snprintf(str, MSG_SIZ, "%sobserve %s\n",
3720 ics_prefix, StripHighlightAndTitle(player));
3723 /* Save ratings from notify string */
3724 safeStrCpy(player1Name, star_match[0], sizeof(player1Name)/sizeof(player1Name[0]));
3725 player1Rating = string_to_rating(star_match[1]);
3726 safeStrCpy(player2Name, star_match[2], sizeof(player2Name)/sizeof(player2Name[0]));
3727 player2Rating = string_to_rating(star_match[3]);
3729 if (appData.debugMode)
3731 "Ratings from 'Game notification:' %s %d, %s %d\n",
3732 player1Name, player1Rating,
3733 player2Name, player2Rating);
3738 /* Deal with automatic examine mode after a game,
3739 and with IcsObserving -> IcsExamining transition */
3740 if (looking_at(buf, &i, "Entering examine mode for game *") ||
3741 looking_at(buf, &i, "has made you an examiner of game *")) {
3743 int gamenum = atoi(star_match[0]);
3744 if ((gameMode == IcsIdle || gameMode == IcsObserving) &&
3745 gamenum == ics_gamenum) {
3746 /* We were already playing or observing this game;
3747 no need to refetch history */
3748 gameMode = IcsExamining;
3750 pauseExamForwardMostMove = forwardMostMove;
3751 } else if (currentMove < forwardMostMove) {
3752 ForwardInner(forwardMostMove);
3755 /* I don't think this case really can happen */
3756 SendToICS(ics_prefix);
3757 SendToICS("refresh\n");
3762 /* Error messages */
3763 // if (ics_user_moved) {
3764 if (1) { // [HGM] old way ignored error after move type in; ics_user_moved is not set then!
3765 if (looking_at(buf, &i, "Illegal move") ||
3766 looking_at(buf, &i, "Not a legal move") ||
3767 looking_at(buf, &i, "Your king is in check") ||
3768 looking_at(buf, &i, "It isn't your turn") ||
3769 looking_at(buf, &i, "It is not your move")) {
3771 if (ics_user_moved && forwardMostMove > backwardMostMove) { // only backup if we already moved
3772 currentMove = forwardMostMove-1;
3773 DisplayMove(currentMove - 1); /* before DMError */
3774 DrawPosition(FALSE, boards[currentMove]);
3775 SwitchClocks(forwardMostMove-1); // [HGM] race
3776 DisplayBothClocks();
3778 DisplayMoveError(_("Illegal move (rejected by ICS)")); // [HGM] but always relay error msg
3784 if (looking_at(buf, &i, "still have time") ||
3785 looking_at(buf, &i, "not out of time") ||
3786 looking_at(buf, &i, "either player is out of time") ||
3787 looking_at(buf, &i, "has timeseal; checking")) {
3788 /* We must have called his flag a little too soon */
3789 whiteFlag = blackFlag = FALSE;
3793 if (looking_at(buf, &i, "added * seconds to") ||
3794 looking_at(buf, &i, "seconds were added to")) {
3795 /* Update the clocks */
3796 SendToICS(ics_prefix);
3797 SendToICS("refresh\n");
3801 if (!ics_clock_paused && looking_at(buf, &i, "clock paused")) {
3802 ics_clock_paused = TRUE;
3807 if (ics_clock_paused && looking_at(buf, &i, "clock resumed")) {
3808 ics_clock_paused = FALSE;
3813 /* Grab player ratings from the Creating: message.
3814 Note we have to check for the special case when
3815 the ICS inserts things like [white] or [black]. */
3816 if (looking_at(buf, &i, "Creating: * (*)* * (*)") ||
3817 looking_at(buf, &i, "Creating: * (*) [*] * (*)")) {
3819 0 player 1 name (not necessarily white)
3821 2 empty, white, or black (IGNORED)
3822 3 player 2 name (not necessarily black)
3825 The names/ratings are sorted out when the game
3826 actually starts (below).
3828 safeStrCpy(player1Name, StripHighlightAndTitle(star_match[0]), sizeof(player1Name)/sizeof(player1Name[0]));
3829 player1Rating = string_to_rating(star_match[1]);
3830 safeStrCpy(player2Name, StripHighlightAndTitle(star_match[3]), sizeof(player2Name)/sizeof(player2Name[0]));
3831 player2Rating = string_to_rating(star_match[4]);
3833 if (appData.debugMode)
3835 "Ratings from 'Creating:' %s %d, %s %d\n",
3836 player1Name, player1Rating,
3837 player2Name, player2Rating);
3842 /* Improved generic start/end-of-game messages */
3843 if ((tkind=0, looking_at(buf, &i, "{Game * (* vs. *) *}*")) ||
3844 (tkind=1, looking_at(buf, &i, "{Game * (*(*) vs. *(*)) *}*"))){
3845 /* If tkind == 0: */
3846 /* star_match[0] is the game number */
3847 /* [1] is the white player's name */
3848 /* [2] is the black player's name */
3849 /* For end-of-game: */
3850 /* [3] is the reason for the game end */
3851 /* [4] is a PGN end game-token, preceded by " " */
3852 /* For start-of-game: */
3853 /* [3] begins with "Creating" or "Continuing" */
3854 /* [4] is " *" or empty (don't care). */
3855 int gamenum = atoi(star_match[0]);
3856 char *whitename, *blackname, *why, *endtoken;
3857 ChessMove endtype = EndOfFile;
3860 whitename = star_match[1];
3861 blackname = star_match[2];
3862 why = star_match[3];
3863 endtoken = star_match[4];
3865 whitename = star_match[1];
3866 blackname = star_match[3];
3867 why = star_match[5];
3868 endtoken = star_match[6];
3871 /* Game start messages */
3872 if (strncmp(why, "Creating ", 9) == 0 ||
3873 strncmp(why, "Continuing ", 11) == 0) {
3874 gs_gamenum = gamenum;
3875 safeStrCpy(gs_kind, strchr(why, ' ') + 1,sizeof(gs_kind)/sizeof(gs_kind[0]));
3876 if(ics_gamenum == -1) // [HGM] only if we are not already involved in a game (because gin=1 sends us such messages)
3877 VariantSwitch(boards[currentMove], StringToVariant(gs_kind)); // [HGM] variantswitch: even before we get first board
3879 if (appData.zippyPlay) {
3880 ZippyGameStart(whitename, blackname);
3883 partnerBoardValid = FALSE; // [HGM] bughouse
3887 /* Game end messages */
3888 if (gameMode == IcsIdle || gameMode == BeginningOfGame ||
3889 ics_gamenum != gamenum) {
3892 while (endtoken[0] == ' ') endtoken++;
3893 switch (endtoken[0]) {
3896 endtype = GameUnfinished;
3899 endtype = BlackWins;
3902 if (endtoken[1] == '/')
3903 endtype = GameIsDrawn;
3905 endtype = WhiteWins;
3908 GameEnds(endtype, why, GE_ICS);
3910 if (appData.zippyPlay && first.initDone) {
3911 ZippyGameEnd(endtype, why);
3912 if (first.pr == NoProc) {
3913 /* Start the next process early so that we'll
3914 be ready for the next challenge */
3915 StartChessProgram(&first);
3917 /* Send "new" early, in case this command takes
3918 a long time to finish, so that we'll be ready
3919 for the next challenge. */
3920 gameInfo.variant = VariantNormal; // [HGM] variantswitch: suppress sending of 'variant'
3924 if(appData.bgObserve && partnerBoardValid) DrawPosition(TRUE, partnerBoard);
3928 if (looking_at(buf, &i, "Removing game * from observation") ||
3929 looking_at(buf, &i, "no longer observing game *") ||
3930 looking_at(buf, &i, "Game * (*) has no examiners")) {
3931 if (gameMode == IcsObserving &&
3932 atoi(star_match[0]) == ics_gamenum)
3934 /* icsEngineAnalyze */
3935 if (appData.icsEngineAnalyze) {
3942 ics_user_moved = FALSE;
3947 if (looking_at(buf, &i, "no longer examining game *")) {
3948 if (gameMode == IcsExamining &&
3949 atoi(star_match[0]) == ics_gamenum)
3953 ics_user_moved = FALSE;
3958 /* Advance leftover_start past any newlines we find,
3959 so only partial lines can get reparsed */
3960 if (looking_at(buf, &i, "\n")) {
3961 prevColor = curColor;
3962 if (curColor != ColorNormal) {
3963 if (oldi > next_out) {
3964 SendToPlayer(&buf[next_out], oldi - next_out);
3967 Colorize(ColorNormal, FALSE);
3968 curColor = ColorNormal;
3970 if (started == STARTED_BOARD) {
3971 started = STARTED_NONE;
3972 parse[parse_pos] = NULLCHAR;
3973 ParseBoard12(parse);
3976 /* Send premove here */
3977 if (appData.premove) {
3979 if (currentMove == 0 &&
3980 gameMode == IcsPlayingWhite &&
3981 appData.premoveWhite) {
3982 snprintf(str, MSG_SIZ, "%s\n", appData.premoveWhiteText);
3983 if (appData.debugMode)
3984 fprintf(debugFP, "Sending premove:\n");
3986 } else if (currentMove == 1 &&
3987 gameMode == IcsPlayingBlack &&
3988 appData.premoveBlack) {
3989 snprintf(str, MSG_SIZ, "%s\n", appData.premoveBlackText);
3990 if (appData.debugMode)
3991 fprintf(debugFP, "Sending premove:\n");
3993 } else if (gotPremove) {
3995 ClearPremoveHighlights();
3996 if (appData.debugMode)
3997 fprintf(debugFP, "Sending premove:\n");
3998 UserMoveEvent(premoveFromX, premoveFromY,
3999 premoveToX, premoveToY,
4004 /* Usually suppress following prompt */
4005 if (!(forwardMostMove == 0 && gameMode == IcsExamining)) {
4006 while(looking_at(buf, &i, "\n")); // [HGM] skip empty lines
4007 if (looking_at(buf, &i, "*% ")) {
4008 savingComment = FALSE;
4013 } else if (started == STARTED_HOLDINGS) {
4015 char new_piece[MSG_SIZ];
4016 started = STARTED_NONE;
4017 parse[parse_pos] = NULLCHAR;
4018 if (appData.debugMode)
4019 fprintf(debugFP, "Parsing holdings: %s, currentMove = %d\n",
4020 parse, currentMove);
4021 if (sscanf(parse, " game %d", &gamenum) == 1) {
4022 if(gamenum == ics_gamenum) { // [HGM] bughouse: old code if part of foreground game
4023 if (gameInfo.variant == VariantNormal) {
4024 /* [HGM] We seem to switch variant during a game!
4025 * Presumably no holdings were displayed, so we have
4026 * to move the position two files to the right to
4027 * create room for them!
4029 VariantClass newVariant;
4030 switch(gameInfo.boardWidth) { // base guess on board width
4031 case 9: newVariant = VariantShogi; break;
4032 case 10: newVariant = VariantGreat; break;
4033 default: newVariant = VariantCrazyhouse; break;
4035 VariantSwitch(boards[currentMove], newVariant); /* temp guess */
4036 /* Get a move list just to see the header, which
4037 will tell us whether this is really bug or zh */
4038 if (ics_getting_history == H_FALSE) {
4039 ics_getting_history = H_REQUESTED;
4040 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4044 new_piece[0] = NULLCHAR;
4045 sscanf(parse, "game %d white [%s black [%s <- %s",
4046 &gamenum, white_holding, black_holding,
4048 white_holding[strlen(white_holding)-1] = NULLCHAR;
4049 black_holding[strlen(black_holding)-1] = NULLCHAR;
4050 /* [HGM] copy holdings to board holdings area */
4051 CopyHoldings(boards[forwardMostMove], white_holding, WhitePawn);
4052 CopyHoldings(boards[forwardMostMove], black_holding, BlackPawn);
4053 boards[forwardMostMove][HOLDINGS_SET] = 1; // flag holdings as set
4055 if (appData.zippyPlay && first.initDone) {
4056 ZippyHoldings(white_holding, black_holding,
4060 if (tinyLayout || smallLayout) {
4061 char wh[16], bh[16];
4062 PackHolding(wh, white_holding);
4063 PackHolding(bh, black_holding);
4064 snprintf(str, MSG_SIZ, "[%s-%s] %s-%s", wh, bh,
4065 gameInfo.white, gameInfo.black);
4067 snprintf(str, MSG_SIZ, "%s [%s] %s %s [%s]",
4068 gameInfo.white, white_holding, _("vs."),
4069 gameInfo.black, black_holding);
4071 if(!partnerUp) // [HGM] bughouse: when peeking at partner game we already know what he captured...
4072 DrawPosition(FALSE, boards[currentMove]);
4074 } else if(appData.bgObserve) { // [HGM] bughouse: holdings of other game => background
4075 sscanf(parse, "game %d white [%s black [%s <- %s",
4076 &gamenum, white_holding, black_holding,
4078 white_holding[strlen(white_holding)-1] = NULLCHAR;
4079 black_holding[strlen(black_holding)-1] = NULLCHAR;
4080 /* [HGM] copy holdings to partner-board holdings area */
4081 CopyHoldings(partnerBoard, white_holding, WhitePawn);
4082 CopyHoldings(partnerBoard, black_holding, BlackPawn);
4083 if(twoBoards) { partnerUp = 1; flipView = !flipView; } // [HGM] dual: always draw
4084 if(partnerUp) DrawPosition(FALSE, partnerBoard);
4085 if(twoBoards) { partnerUp = 0; flipView = !flipView; }
4088 /* Suppress following prompt */
4089 if (looking_at(buf, &i, "*% ")) {
4090 if(strchr(star_match[0], 7)) SendToPlayer("\007", 1); // Bell(); // FICS fuses bell for next board with prompt in zh captures
4091 savingComment = FALSE;
4099 i++; /* skip unparsed character and loop back */
4102 if (started != STARTED_MOVES && started != STARTED_BOARD && !suppressKibitz && // [HGM] kibitz
4103 // started != STARTED_HOLDINGS && i > next_out) { // [HGM] should we compare to leftover_start in stead of i?
4104 // SendToPlayer(&buf[next_out], i - next_out);
4105 started != STARTED_HOLDINGS && leftover_start > next_out) {
4106 SendToPlayer(&buf[next_out], leftover_start - next_out);
4110 leftover_len = buf_len - leftover_start;
4111 /* if buffer ends with something we couldn't parse,
4112 reparse it after appending the next read */
4114 } else if (count == 0) {
4115 RemoveInputSource(isr);
4116 DisplayFatalError(_("Connection closed by ICS"), 0, 0);
4118 DisplayFatalError(_("Error reading from ICS"), error, 1);
4123 /* Board style 12 looks like this:
4125 <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
4127 * The "<12> " is stripped before it gets to this routine. The two
4128 * trailing 0's (flip state and clock ticking) are later addition, and
4129 * some chess servers may not have them, or may have only the first.
4130 * Additional trailing fields may be added in the future.
4133 #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"
4135 #define RELATION_OBSERVING_PLAYED 0
4136 #define RELATION_OBSERVING_STATIC -2 /* examined, oldmoves, or smoves */
4137 #define RELATION_PLAYING_MYMOVE 1
4138 #define RELATION_PLAYING_NOTMYMOVE -1
4139 #define RELATION_EXAMINING 2
4140 #define RELATION_ISOLATED_BOARD -3
4141 #define RELATION_STARTING_POSITION -4 /* FICS only */
4144 ParseBoard12 (char *string)
4146 GameMode newGameMode;
4147 int gamenum, newGame, newMove, relation, basetime, increment, ics_flip = 0, i;
4148 int j, k, n, moveNum, white_stren, black_stren, white_time, black_time, takeback;
4149 int double_push, castle_ws, castle_wl, castle_bs, castle_bl, irrev_count;
4150 char to_play, board_chars[200];
4151 char move_str[MSG_SIZ], str[MSG_SIZ], elapsed_time[MSG_SIZ];
4152 char black[32], white[32];
4154 int prevMove = currentMove;
4157 int fromX, fromY, toX, toY;
4159 int ranks=1, files=0; /* [HGM] ICS80: allow variable board size */
4160 char *bookHit = NULL; // [HGM] book
4161 Boolean weird = FALSE, reqFlag = FALSE;
4163 fromX = fromY = toX = toY = -1;
4167 if (appData.debugMode)
4168 fprintf(debugFP, _("Parsing board: %s\n"), string);
4170 move_str[0] = NULLCHAR;
4171 elapsed_time[0] = NULLCHAR;
4172 { /* [HGM] figure out how many ranks and files the board has, for ICS extension used by Capablanca server */
4174 while(i < 199 && (string[i] != ' ' || string[i+2] != ' ')) {
4175 if(string[i] == ' ') { ranks++; files = 0; }
4177 if(!strchr(" -pnbrqkPNBRQK" , string[i])) weird = TRUE; // test for fairies
4180 for(j = 0; j <i; j++) board_chars[j] = string[j];
4181 board_chars[i] = '\0';
4184 n = sscanf(string, PATTERN, &to_play, &double_push,
4185 &castle_ws, &castle_wl, &castle_bs, &castle_bl, &irrev_count,
4186 &gamenum, white, black, &relation, &basetime, &increment,
4187 &white_stren, &black_stren, &white_time, &black_time,
4188 &moveNum, str, elapsed_time, move_str, &ics_flip,
4192 snprintf(str, MSG_SIZ, _("Failed to parse board string:\n\"%s\""), string);
4193 DisplayError(str, 0);
4197 /* Convert the move number to internal form */
4198 moveNum = (moveNum - 1) * 2;
4199 if (to_play == 'B') moveNum++;
4200 if (moveNum > framePtr) { // [HGM] vari: do not run into saved variations
4201 DisplayFatalError(_("Game too long; increase MAX_MOVES and recompile"),
4207 case RELATION_OBSERVING_PLAYED:
4208 case RELATION_OBSERVING_STATIC:
4209 if (gamenum == -1) {
4210 /* Old ICC buglet */
4211 relation = RELATION_OBSERVING_STATIC;
4213 newGameMode = IcsObserving;
4215 case RELATION_PLAYING_MYMOVE:
4216 case RELATION_PLAYING_NOTMYMOVE:
4218 ((relation == RELATION_PLAYING_MYMOVE) == (to_play == 'W')) ?
4219 IcsPlayingWhite : IcsPlayingBlack;
4220 soughtPending =FALSE; // [HGM] seekgraph: solve race condition
4222 case RELATION_EXAMINING:
4223 newGameMode = IcsExamining;
4225 case RELATION_ISOLATED_BOARD:
4227 /* Just display this board. If user was doing something else,
4228 we will forget about it until the next board comes. */
4229 newGameMode = IcsIdle;
4231 case RELATION_STARTING_POSITION:
4232 newGameMode = gameMode;
4236 if((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)
4237 && newGameMode == IcsObserving && gamenum != ics_gamenum && appData.bgObserve) {
4238 // [HGM] bughouse: don't act on alien boards while we play. Just parse the board and save it */
4240 for (k = 0; k < ranks; k++) {
4241 for (j = 0; j < files; j++)
4242 board[k][j+gameInfo.holdingsWidth] = CharToPiece(board_chars[(ranks-1-k)*(files+1) + j]);
4243 if(gameInfo.holdingsWidth > 1) {
4244 board[k][0] = board[k][BOARD_WIDTH-1] = EmptySquare;
4245 board[k][1] = board[k][BOARD_WIDTH-2] = (ChessSquare) 0;;
4248 CopyBoard(partnerBoard, board);
4249 if(toSqr = strchr(str, '/')) { // extract highlights from long move
4250 partnerBoard[EP_STATUS-3] = toSqr[1] - AAA; // kludge: hide highlighting info in board
4251 partnerBoard[EP_STATUS-4] = toSqr[2] - ONE;
4252 } else partnerBoard[EP_STATUS-4] = partnerBoard[EP_STATUS-3] = -1;
4253 if(toSqr = strchr(str, '-')) {
4254 partnerBoard[EP_STATUS-1] = toSqr[1] - AAA;
4255 partnerBoard[EP_STATUS-2] = toSqr[2] - ONE;
4256 } else partnerBoard[EP_STATUS-1] = partnerBoard[EP_STATUS-2] = -1;
4257 if(appData.dualBoard && !twoBoards) { twoBoards = 1; InitDrawingSizes(-2,0); }
4258 if(twoBoards) { partnerUp = 1; flipView = !flipView; } // [HGM] dual
4259 if(partnerUp) DrawPosition(FALSE, partnerBoard);
4260 if(twoBoards) { partnerUp = 0; flipView = !flipView; } // [HGM] dual
4261 snprintf(partnerStatus, MSG_SIZ,"W: %d:%02d B: %d:%02d (%d-%d) %c", white_time/60000, (white_time%60000)/1000,
4262 (black_time/60000), (black_time%60000)/1000, white_stren, black_stren, to_play);
4263 DisplayMessage(partnerStatus, "");
4264 partnerBoardValid = TRUE;
4268 /* Modify behavior for initial board display on move listing
4271 switch (ics_getting_history) {
4275 case H_GOT_REQ_HEADER:
4276 case H_GOT_UNREQ_HEADER:
4277 /* This is the initial position of the current game */
4278 gamenum = ics_gamenum;
4279 moveNum = 0; /* old ICS bug workaround */
4280 if (to_play == 'B') {
4281 startedFromSetupPosition = TRUE;
4282 blackPlaysFirst = TRUE;
4284 if (forwardMostMove == 0) forwardMostMove = 1;
4285 if (backwardMostMove == 0) backwardMostMove = 1;
4286 if (currentMove == 0) currentMove = 1;
4288 newGameMode = gameMode;
4289 relation = RELATION_STARTING_POSITION; /* ICC needs this */
4291 case H_GOT_UNWANTED_HEADER:
4292 /* This is an initial board that we don't want */
4294 case H_GETTING_MOVES:
4295 /* Should not happen */
4296 DisplayError(_("Error gathering move list: extra board"), 0);
4297 ics_getting_history = H_FALSE;
4301 if (gameInfo.boardHeight != ranks || gameInfo.boardWidth != files ||
4302 weird && (int)gameInfo.variant < (int)VariantShogi) {
4303 /* [HGM] We seem to have switched variant unexpectedly
4304 * Try to guess new variant from board size
4306 VariantClass newVariant = VariantFairy; // if 8x8, but fairies present
4307 if(ranks == 8 && files == 10) newVariant = VariantCapablanca; else
4308 if(ranks == 10 && files == 9) newVariant = VariantXiangqi; else
4309 if(ranks == 8 && files == 12) newVariant = VariantCourier; else
4310 if(ranks == 9 && files == 9) newVariant = VariantShogi; else
4311 if(!weird) newVariant = VariantNormal;
4312 VariantSwitch(boards[currentMove], newVariant); /* temp guess */
4313 /* Get a move list just to see the header, which
4314 will tell us whether this is really bug or zh */
4315 if (ics_getting_history == H_FALSE) {
4316 ics_getting_history = H_REQUESTED; reqFlag = TRUE;
4317 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4322 /* Take action if this is the first board of a new game, or of a
4323 different game than is currently being displayed. */
4324 if (gamenum != ics_gamenum || newGameMode != gameMode ||
4325 relation == RELATION_ISOLATED_BOARD) {
4327 /* Forget the old game and get the history (if any) of the new one */
4328 if (gameMode != BeginningOfGame) {
4332 if (appData.autoRaiseBoard) BoardToTop();
4334 if (gamenum == -1) {
4335 newGameMode = IcsIdle;
4336 } else if ((moveNum > 0 || newGameMode == IcsObserving) && newGameMode != IcsIdle &&
4337 appData.getMoveList && !reqFlag) {
4338 /* Need to get game history */
4339 ics_getting_history = H_REQUESTED;
4340 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4344 /* Initially flip the board to have black on the bottom if playing
4345 black or if the ICS flip flag is set, but let the user change
4346 it with the Flip View button. */
4347 flipView = appData.autoFlipView ?
4348 (newGameMode == IcsPlayingBlack) || ics_flip :
4351 /* Done with values from previous mode; copy in new ones */
4352 gameMode = newGameMode;
4354 ics_gamenum = gamenum;
4355 if (gamenum == gs_gamenum) {
4356 int klen = strlen(gs_kind);
4357 if (gs_kind[klen - 1] == '.') gs_kind[klen - 1] = NULLCHAR;
4358 snprintf(str, MSG_SIZ, "ICS %s", gs_kind);
4359 gameInfo.event = StrSave(str);
4361 gameInfo.event = StrSave("ICS game");
4363 gameInfo.site = StrSave(appData.icsHost);
4364 gameInfo.date = PGNDate();
4365 gameInfo.round = StrSave("-");
4366 gameInfo.white = StrSave(white);
4367 gameInfo.black = StrSave(black);
4368 timeControl = basetime * 60 * 1000;
4370 timeIncrement = increment * 1000;
4371 movesPerSession = 0;
4372 gameInfo.timeControl = TimeControlTagValue();
4373 VariantSwitch(boards[currentMove], StringToVariant(gameInfo.event) );
4374 if (appData.debugMode) {
4375 fprintf(debugFP, "ParseBoard says variant = '%s'\n", gameInfo.event);
4376 fprintf(debugFP, "recognized as %s\n", VariantName(gameInfo.variant));
4377 setbuf(debugFP, NULL);
4380 gameInfo.outOfBook = NULL;
4382 /* Do we have the ratings? */
4383 if (strcmp(player1Name, white) == 0 &&
4384 strcmp(player2Name, black) == 0) {
4385 if (appData.debugMode)
4386 fprintf(debugFP, "Remembered ratings: W %d, B %d\n",
4387 player1Rating, player2Rating);
4388 gameInfo.whiteRating = player1Rating;
4389 gameInfo.blackRating = player2Rating;
4390 } else if (strcmp(player2Name, white) == 0 &&
4391 strcmp(player1Name, black) == 0) {
4392 if (appData.debugMode)
4393 fprintf(debugFP, "Remembered ratings: W %d, B %d\n",
4394 player2Rating, player1Rating);
4395 gameInfo.whiteRating = player2Rating;
4396 gameInfo.blackRating = player1Rating;
4398 player1Name[0] = player2Name[0] = NULLCHAR;
4400 /* Silence shouts if requested */
4401 if (appData.quietPlay &&
4402 (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)) {
4403 SendToICS(ics_prefix);
4404 SendToICS("set shout 0\n");
4408 /* Deal with midgame name changes */
4410 if (!gameInfo.white || strcmp(gameInfo.white, white) != 0) {
4411 if (gameInfo.white) free(gameInfo.white);
4412 gameInfo.white = StrSave(white);
4414 if (!gameInfo.black || strcmp(gameInfo.black, black) != 0) {
4415 if (gameInfo.black) free(gameInfo.black);
4416 gameInfo.black = StrSave(black);
4420 /* Throw away game result if anything actually changes in examine mode */
4421 if (gameMode == IcsExamining && !newGame) {
4422 gameInfo.result = GameUnfinished;
4423 if (gameInfo.resultDetails != NULL) {
4424 free(gameInfo.resultDetails);
4425 gameInfo.resultDetails = NULL;
4429 /* In pausing && IcsExamining mode, we ignore boards coming
4430 in if they are in a different variation than we are. */
4431 if (pauseExamInvalid) return;
4432 if (pausing && gameMode == IcsExamining) {
4433 if (moveNum <= pauseExamForwardMostMove) {
4434 pauseExamInvalid = TRUE;
4435 forwardMostMove = pauseExamForwardMostMove;
4440 if (appData.debugMode) {
4441 fprintf(debugFP, "load %dx%d board\n", files, ranks);
4443 /* Parse the board */
4444 for (k = 0; k < ranks; k++) {
4445 for (j = 0; j < files; j++)
4446 board[k][j+gameInfo.holdingsWidth] = CharToPiece(board_chars[(ranks-1-k)*(files+1) + j]);
4447 if(gameInfo.holdingsWidth > 1) {
4448 board[k][0] = board[k][BOARD_WIDTH-1] = EmptySquare;
4449 board[k][1] = board[k][BOARD_WIDTH-2] = (ChessSquare) 0;;
4452 if(moveNum==0 && gameInfo.variant == VariantSChess) {
4453 board[5][BOARD_RGHT+1] = WhiteAngel;
4454 board[6][BOARD_RGHT+1] = WhiteMarshall;
4455 board[1][0] = BlackMarshall;
4456 board[2][0] = BlackAngel;
4457 board[1][1] = board[2][1] = board[5][BOARD_RGHT] = board[6][BOARD_RGHT] = 1;
4459 CopyBoard(boards[moveNum], board);
4460 boards[moveNum][HOLDINGS_SET] = 0; // [HGM] indicate holdings not set
4462 startedFromSetupPosition =
4463 !CompareBoards(board, initialPosition);
4464 if(startedFromSetupPosition)
4465 initialRulePlies = irrev_count; /* [HGM] 50-move counter offset */
4468 /* [HGM] Set castling rights. Take the outermost Rooks,
4469 to make it also work for FRC opening positions. Note that board12
4470 is really defective for later FRC positions, as it has no way to
4471 indicate which Rook can castle if they are on the same side of King.
4472 For the initial position we grant rights to the outermost Rooks,
4473 and remember thos rights, and we then copy them on positions
4474 later in an FRC game. This means WB might not recognize castlings with
4475 Rooks that have moved back to their original position as illegal,
4476 but in ICS mode that is not its job anyway.
4478 if(moveNum == 0 || gameInfo.variant != VariantFischeRandom)
4479 { int i, j; ChessSquare wKing = WhiteKing, bKing = BlackKing;
4481 for(i=BOARD_LEFT, j=NoRights; i<BOARD_RGHT; i++)
4482 if(board[0][i] == WhiteRook) j = i;
4483 initialRights[0] = boards[moveNum][CASTLING][0] = (castle_ws == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4484 for(i=BOARD_RGHT-1, j=NoRights; i>=BOARD_LEFT; i--)
4485 if(board[0][i] == WhiteRook) j = i;
4486 initialRights[1] = boards[moveNum][CASTLING][1] = (castle_wl == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4487 for(i=BOARD_LEFT, j=NoRights; i<BOARD_RGHT; i++)
4488 if(board[BOARD_HEIGHT-1][i] == BlackRook) j = i;
4489 initialRights[3] = boards[moveNum][CASTLING][3] = (castle_bs == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4490 for(i=BOARD_RGHT-1, j=NoRights; i>=BOARD_LEFT; i--)
4491 if(board[BOARD_HEIGHT-1][i] == BlackRook) j = i;
4492 initialRights[4] = boards[moveNum][CASTLING][4] = (castle_bl == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4494 boards[moveNum][CASTLING][2] = boards[moveNum][CASTLING][5] = NoRights;
4495 if(gameInfo.variant == VariantKnightmate) { wKing = WhiteUnicorn; bKing = BlackUnicorn; }
4496 for(k=BOARD_LEFT; k<BOARD_RGHT; k++)
4497 if(board[0][k] == wKing) initialRights[2] = boards[moveNum][CASTLING][2] = k;
4498 for(k=BOARD_LEFT; k<BOARD_RGHT; k++)
4499 if(board[BOARD_HEIGHT-1][k] == bKing)
4500 initialRights[5] = boards[moveNum][CASTLING][5] = k;
4501 if(gameInfo.variant == VariantTwoKings) {
4502 // In TwoKings looking for a King does not work, so always give castling rights to a King on e1/e8
4503 if(board[0][4] == wKing) initialRights[2] = boards[moveNum][CASTLING][2] = 4;
4504 if(board[BOARD_HEIGHT-1][4] == bKing) initialRights[5] = boards[moveNum][CASTLING][5] = 4;
4507 r = boards[moveNum][CASTLING][0] = initialRights[0];
4508 if(board[0][r] != WhiteRook) boards[moveNum][CASTLING][0] = NoRights;
4509 r = boards[moveNum][CASTLING][1] = initialRights[1];
4510 if(board[0][r] != WhiteRook) boards[moveNum][CASTLING][1] = NoRights;
4511 r = boards[moveNum][CASTLING][3] = initialRights[3];
4512 if(board[BOARD_HEIGHT-1][r] != BlackRook) boards[moveNum][CASTLING][3] = NoRights;
4513 r = boards[moveNum][CASTLING][4] = initialRights[4];
4514 if(board[BOARD_HEIGHT-1][r] != BlackRook) boards[moveNum][CASTLING][4] = NoRights;
4515 /* wildcastle kludge: always assume King has rights */
4516 r = boards[moveNum][CASTLING][2] = initialRights[2];
4517 r = boards[moveNum][CASTLING][5] = initialRights[5];
4519 /* [HGM] e.p. rights. Assume that ICS sends file number here? */
4520 boards[moveNum][EP_STATUS] = EP_NONE;
4521 if(str[0] == 'P') boards[moveNum][EP_STATUS] = EP_PAWN_MOVE;
4522 if(strchr(move_str, 'x')) boards[moveNum][EP_STATUS] = EP_CAPTURE;
4523 if(double_push != -1) boards[moveNum][EP_STATUS] = double_push + BOARD_LEFT;
4526 if (ics_getting_history == H_GOT_REQ_HEADER ||
4527 ics_getting_history == H_GOT_UNREQ_HEADER) {
4528 /* This was an initial position from a move list, not
4529 the current position */
4533 /* Update currentMove and known move number limits */
4534 newMove = newGame || moveNum > forwardMostMove;
4537 forwardMostMove = backwardMostMove = currentMove = moveNum;
4538 if (gameMode == IcsExamining && moveNum == 0) {
4539 /* Workaround for ICS limitation: we are not told the wild
4540 type when starting to examine a game. But if we ask for
4541 the move list, the move list header will tell us */
4542 ics_getting_history = H_REQUESTED;
4543 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4546 } else if (moveNum == forwardMostMove + 1 || moveNum == forwardMostMove
4547 || (moveNum < forwardMostMove && moveNum >= backwardMostMove)) {
4549 /* [DM] If we found takebacks during icsEngineAnalyze try send to engine */
4550 /* [HGM] applied this also to an engine that is silently watching */
4551 if (appData.zippyPlay && moveNum < forwardMostMove && first.initDone &&
4552 (gameMode == IcsObserving || gameMode == IcsExamining) &&
4553 gameInfo.variant == currentlyInitializedVariant) {
4554 takeback = forwardMostMove - moveNum;
4555 for (i = 0; i < takeback; i++) {
4556 if (appData.debugMode) fprintf(debugFP, "take back move\n");
4557 SendToProgram("undo\n", &first);
4562 forwardMostMove = moveNum;
4563 if (!pausing || currentMove > forwardMostMove)
4564 currentMove = forwardMostMove;
4566 /* New part of history that is not contiguous with old part */
4567 if (pausing && gameMode == IcsExamining) {
4568 pauseExamInvalid = TRUE;
4569 forwardMostMove = pauseExamForwardMostMove;
4572 if (gameMode == IcsExamining && moveNum > 0 && appData.getMoveList) {
4574 if(appData.zippyPlay && forwardMostMove > 0 && first.initDone) {
4575 // [HGM] when we will receive the move list we now request, it will be
4576 // fed to the engine from the first move on. So if the engine is not
4577 // in the initial position now, bring it there.
4578 InitChessProgram(&first, 0);
4581 ics_getting_history = H_REQUESTED;
4582 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4585 forwardMostMove = backwardMostMove = currentMove = moveNum;
4588 /* Update the clocks */
4589 if (strchr(elapsed_time, '.')) {
4591 timeRemaining[0][moveNum] = whiteTimeRemaining = white_time;
4592 timeRemaining[1][moveNum] = blackTimeRemaining = black_time;
4594 /* Time is in seconds */
4595 timeRemaining[0][moveNum] = whiteTimeRemaining = white_time * 1000;
4596 timeRemaining[1][moveNum] = blackTimeRemaining = black_time * 1000;
4601 if (appData.zippyPlay && newGame &&
4602 gameMode != IcsObserving && gameMode != IcsIdle &&
4603 gameMode != IcsExamining)
4604 ZippyFirstBoard(moveNum, basetime, increment);
4607 /* Put the move on the move list, first converting
4608 to canonical algebraic form. */
4610 if (appData.debugMode) {
4611 if (appData.debugMode) { int f = forwardMostMove;
4612 fprintf(debugFP, "parseboard %d, castling = %d %d %d %d %d %d\n", f,
4613 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
4614 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
4616 fprintf(debugFP, "accepted move %s from ICS, parse it.\n", move_str);
4617 fprintf(debugFP, "moveNum = %d\n", moveNum);
4618 fprintf(debugFP, "board = %d-%d x %d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT);
4619 setbuf(debugFP, NULL);
4621 if (moveNum <= backwardMostMove) {
4622 /* We don't know what the board looked like before
4624 safeStrCpy(parseList[moveNum - 1], move_str, sizeof(parseList[moveNum - 1])/sizeof(parseList[moveNum - 1][0]));
4625 strcat(parseList[moveNum - 1], " ");
4626 strcat(parseList[moveNum - 1], elapsed_time);
4627 moveList[moveNum - 1][0] = NULLCHAR;
4628 } else if (strcmp(move_str, "none") == 0) {
4629 // [HGM] long SAN: swapped order; test for 'none' before parsing move
4630 /* Again, we don't know what the board looked like;
4631 this is really the start of the game. */
4632 parseList[moveNum - 1][0] = NULLCHAR;
4633 moveList[moveNum - 1][0] = NULLCHAR;
4634 backwardMostMove = moveNum;
4635 startedFromSetupPosition = TRUE;
4636 fromX = fromY = toX = toY = -1;
4638 // [HGM] long SAN: if legality-testing is off, disambiguation might not work or give wrong move.
4639 // So we parse the long-algebraic move string in stead of the SAN move
4640 int valid; char buf[MSG_SIZ], *prom;
4642 if(gameInfo.variant == VariantShogi && !strchr(move_str, '=') && !strchr(move_str, '@'))
4643 strcat(move_str, "="); // if ICS does not say 'promote' on non-drop, we defer.
4644 // str looks something like "Q/a1-a2"; kill the slash
4646 snprintf(buf, MSG_SIZ,"%c%s", str[0], str+2);
4647 else safeStrCpy(buf, str, sizeof(buf)/sizeof(buf[0])); // might be castling
4648 if((prom = strstr(move_str, "=")) && !strstr(buf, "="))
4649 strcat(buf, prom); // long move lacks promo specification!
4650 if(!appData.testLegality && move_str[1] != '@') { // drops never ambiguous (parser chokes on long form!)
4651 if(appData.debugMode)
4652 fprintf(debugFP, "replaced ICS move '%s' by '%s'\n", move_str, buf);
4653 safeStrCpy(move_str, buf, MSG_SIZ);
4655 valid = ParseOneMove(move_str, moveNum - 1, &moveType,
4656 &fromX, &fromY, &toX, &toY, &promoChar)
4657 || ParseOneMove(buf, moveNum - 1, &moveType,
4658 &fromX, &fromY, &toX, &toY, &promoChar);
4659 // end of long SAN patch
4661 (void) CoordsToAlgebraic(boards[moveNum - 1],
4662 PosFlags(moveNum - 1),
4663 fromY, fromX, toY, toX, promoChar,
4664 parseList[moveNum-1]);
4665 switch (MateTest(boards[moveNum], PosFlags(moveNum)) ) {
4671 if(gameInfo.variant != VariantShogi)
4672 strcat(parseList[moveNum - 1], "+");
4675 case MT_STAINMATE: // [HGM] xq: for notation stalemate that wins counts as checkmate
4676 strcat(parseList[moveNum - 1], "#");
4679 strcat(parseList[moveNum - 1], " ");
4680 strcat(parseList[moveNum - 1], elapsed_time);
4681 /* currentMoveString is set as a side-effect of ParseOneMove */
4682 if(gameInfo.variant == VariantShogi && currentMoveString[4]) currentMoveString[4] = '^';
4683 safeStrCpy(moveList[moveNum - 1], currentMoveString, sizeof(moveList[moveNum - 1])/sizeof(moveList[moveNum - 1][0]));
4684 strcat(moveList[moveNum - 1], "\n");
4686 if(gameInfo.holdingsWidth && !appData.disguise && gameInfo.variant != VariantSuper && gameInfo.variant != VariantGreat
4687 && gameInfo.variant != VariantGrand&& gameInfo.variant != VariantSChess) // inherit info that ICS does not give from previous board
4688 for(k=0; k<ranks; k++) for(j=BOARD_LEFT; j<BOARD_RGHT; j++) {
4689 ChessSquare old, new = boards[moveNum][k][j];
4690 if(fromY == DROP_RANK && k==toY && j==toX) continue; // dropped pieces always stand for themselves
4691 old = (k==toY && j==toX) ? boards[moveNum-1][fromY][fromX] : boards[moveNum-1][k][j]; // trace back mover
4692 if(old == new) continue;
4693 if(old == PROMOTED new) boards[moveNum][k][j] = old; // prevent promoted pieces to revert to primordial ones
4694 else if(new == WhiteWazir || new == BlackWazir) {
4695 if(old < WhiteCannon || old >= BlackPawn && old < BlackCannon)
4696 boards[moveNum][k][j] = PROMOTED old; // choose correct type of Gold in promotion
4697 else boards[moveNum][k][j] = old; // preserve type of Gold
4698 } else if((old == WhitePawn || old == BlackPawn) && new != EmptySquare) // Pawn promotions (but not e.p.capture!)
4699 boards[moveNum][k][j] = PROMOTED new; // use non-primordial representation of chosen piece
4702 /* Move from ICS was illegal!? Punt. */
4703 if (appData.debugMode) {
4704 fprintf(debugFP, "Illegal move from ICS '%s'\n", move_str);
4705 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
4707 safeStrCpy(parseList[moveNum - 1], move_str, sizeof(parseList[moveNum - 1])/sizeof(parseList[moveNum - 1][0]));
4708 strcat(parseList[moveNum - 1], " ");
4709 strcat(parseList[moveNum - 1], elapsed_time);
4710 moveList[moveNum - 1][0] = NULLCHAR;
4711 fromX = fromY = toX = toY = -1;
4714 if (appData.debugMode) {
4715 fprintf(debugFP, "Move parsed to '%s'\n", parseList[moveNum - 1]);
4716 setbuf(debugFP, NULL);
4720 /* Send move to chess program (BEFORE animating it). */
4721 if (appData.zippyPlay && !newGame && newMove &&
4722 (!appData.getMoveList || backwardMostMove == 0) && first.initDone) {
4724 if ((gameMode == IcsPlayingWhite && WhiteOnMove(moveNum)) ||
4725 (gameMode == IcsPlayingBlack && !WhiteOnMove(moveNum))) {
4726 if (moveList[moveNum - 1][0] == NULLCHAR) {
4727 snprintf(str, MSG_SIZ, _("Couldn't parse move \"%s\" from ICS"),
4729 DisplayError(str, 0);
4731 if (first.sendTime) {
4732 SendTimeRemaining(&first, gameMode == IcsPlayingWhite);
4734 bookHit = SendMoveToBookUser(moveNum - 1, &first, FALSE); // [HGM] book
4735 if (firstMove && !bookHit) {
4737 if (first.useColors) {
4738 SendToProgram(gameMode == IcsPlayingWhite ?
4740 "black\ngo\n", &first);
4742 SendToProgram("go\n", &first);
4744 first.maybeThinking = TRUE;
4747 } else if (gameMode == IcsObserving || gameMode == IcsExamining) {
4748 if (moveList[moveNum - 1][0] == NULLCHAR) {
4749 snprintf(str, MSG_SIZ, _("Couldn't parse move \"%s\" from ICS"), move_str);
4750 DisplayError(str, 0);
4752 if(gameInfo.variant == currentlyInitializedVariant) // [HGM] refrain sending moves engine can't understand!
4753 SendMoveToProgram(moveNum - 1, &first);
4760 if (moveNum > 0 && !gotPremove && !appData.noGUI) {
4761 /* If move comes from a remote source, animate it. If it
4762 isn't remote, it will have already been animated. */
4763 if (!pausing && !ics_user_moved && prevMove == moveNum - 1) {
4764 AnimateMove(boards[moveNum - 1], fromX, fromY, toX, toY);
4766 if (!pausing && appData.highlightLastMove) {
4767 SetHighlights(fromX, fromY, toX, toY);
4771 /* Start the clocks */
4772 whiteFlag = blackFlag = FALSE;
4773 appData.clockMode = !(basetime == 0 && increment == 0);
4775 ics_clock_paused = TRUE;
4777 } else if (ticking == 1) {
4778 ics_clock_paused = FALSE;
4780 if (gameMode == IcsIdle ||
4781 relation == RELATION_OBSERVING_STATIC ||
4782 relation == RELATION_EXAMINING ||
4784 DisplayBothClocks();
4788 /* Display opponents and material strengths */
4789 if (gameInfo.variant != VariantBughouse &&
4790 gameInfo.variant != VariantCrazyhouse && !appData.noGUI) {
4791 if (tinyLayout || smallLayout) {
4792 if(gameInfo.variant == VariantNormal)
4793 snprintf(str, MSG_SIZ, "%s(%d) %s(%d) {%d %d}",
4794 gameInfo.white, white_stren, gameInfo.black, black_stren,
4795 basetime, increment);
4797 snprintf(str, MSG_SIZ, "%s(%d) %s(%d) {%d %d w%d}",
4798 gameInfo.white, white_stren, gameInfo.black, black_stren,
4799 basetime, increment, (int) gameInfo.variant);
4801 if(gameInfo.variant == VariantNormal)
4802 snprintf(str, MSG_SIZ, "%s (%d) %s %s (%d) {%d %d}",
4803 gameInfo.white, white_stren, _("vs."), gameInfo.black, black_stren,
4804 basetime, increment);
4806 snprintf(str, MSG_SIZ, "%s (%d) %s %s (%d) {%d %d %s}",
4807 gameInfo.white, white_stren, _("vs."), gameInfo.black, black_stren,
4808 basetime, increment, VariantName(gameInfo.variant));
4811 if (appData.debugMode) {
4812 fprintf(debugFP, "Display title '%s, gameInfo.variant = %d'\n", str, gameInfo.variant);
4817 /* Display the board */
4818 if (!pausing && !appData.noGUI) {
4820 if (appData.premove)
4822 ((gameMode == IcsPlayingWhite) && (WhiteOnMove(currentMove))) ||
4823 ((gameMode == IcsPlayingBlack) && (!WhiteOnMove(currentMove))))
4824 ClearPremoveHighlights();
4826 j = seekGraphUp; seekGraphUp = FALSE; // [HGM] seekgraph: when we draw a board, it overwrites the seek graph
4827 if(partnerUp) { flipView = originalFlip; partnerUp = FALSE; j = TRUE; } // [HGM] bughouse: restore view
4828 DrawPosition(j, boards[currentMove]);
4830 DisplayMove(moveNum - 1);
4831 if (appData.ringBellAfterMoves && /*!ics_user_moved*/ // [HGM] use absolute method to recognize own move
4832 !((gameMode == IcsPlayingWhite) && (!WhiteOnMove(moveNum)) ||
4833 (gameMode == IcsPlayingBlack) && (WhiteOnMove(moveNum)) ) ) {
4834 if(newMove) RingBell(); else PlayIcsUnfinishedSound();
4838 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
4840 if(bookHit) { // [HGM] book: simulate book reply
4841 static char bookMove[MSG_SIZ]; // a bit generous?
4843 programStats.nodes = programStats.depth = programStats.time =
4844 programStats.score = programStats.got_only_move = 0;
4845 sprintf(programStats.movelist, "%s (xbook)", bookHit);
4847 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
4848 strcat(bookMove, bookHit);
4849 HandleMachineMove(bookMove, &first);
4858 if (appData.icsActive && gameMode != IcsIdle && ics_gamenum > 0) {
4859 ics_getting_history = H_REQUESTED;
4860 snprintf(buf, MSG_SIZ, "%smoves %d\n", ics_prefix, ics_gamenum);
4866 AnalysisPeriodicEvent (int force)
4868 if (((programStats.ok_to_send == 0 || programStats.line_is_book)
4869 && !force) || !appData.periodicUpdates)
4872 /* Send . command to Crafty to collect stats */
4873 SendToProgram(".\n", &first);
4875 /* Don't send another until we get a response (this makes
4876 us stop sending to old Crafty's which don't understand
4877 the "." command (sending illegal cmds resets node count & time,
4878 which looks bad)) */
4879 programStats.ok_to_send = 0;
4883 ics_update_width (int new_width)
4885 ics_printf("set width %d\n", new_width);
4889 SendMoveToProgram (int moveNum, ChessProgramState *cps)
4893 if(moveList[moveNum][1] == '@' && moveList[moveNum][0] == '@') {
4894 // null move in variant where engine does not understand it (for analysis purposes)
4895 SendBoard(cps, moveNum + 1); // send position after move in stead.
4898 if (cps->useUsermove) {
4899 SendToProgram("usermove ", cps);
4903 if ((space = strchr(parseList[moveNum], ' ')) != NULL) {
4904 int len = space - parseList[moveNum];
4905 memcpy(buf, parseList[moveNum], len);
4907 buf[len] = NULLCHAR;
4909 snprintf(buf, MSG_SIZ,"%s\n", parseList[moveNum]);
4911 SendToProgram(buf, cps);
4913 if(cps->alphaRank) { /* [HGM] shogi: temporarily convert to shogi coordinates before sending */
4914 AlphaRank(moveList[moveNum], 4);
4915 SendToProgram(moveList[moveNum], cps);
4916 AlphaRank(moveList[moveNum], 4); // and back
4918 /* Added by Tord: Send castle moves in "O-O" in FRC games if required by
4919 * the engine. It would be nice to have a better way to identify castle
4921 if((gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom)
4922 && cps->useOOCastle) {
4923 int fromX = moveList[moveNum][0] - AAA;
4924 int fromY = moveList[moveNum][1] - ONE;
4925 int toX = moveList[moveNum][2] - AAA;
4926 int toY = moveList[moveNum][3] - ONE;
4927 if((boards[moveNum][fromY][fromX] == WhiteKing
4928 && boards[moveNum][toY][toX] == WhiteRook)
4929 || (boards[moveNum][fromY][fromX] == BlackKing
4930 && boards[moveNum][toY][toX] == BlackRook)) {
4931 if(toX > fromX) SendToProgram("O-O\n", cps);
4932 else SendToProgram("O-O-O\n", cps);
4934 else SendToProgram(moveList[moveNum], cps);
4936 if(BOARD_HEIGHT > 10) { // [HGM] big: convert ranks to double-digit where needed
4937 if(moveList[moveNum][1] == '@' && (BOARD_HEIGHT < 16 || moveList[moveNum][0] <= 'Z')) { // drop move
4938 if(moveList[moveNum][0]== '@') snprintf(buf, MSG_SIZ, "@@@@\n"); else
4939 snprintf(buf, MSG_SIZ, "%c@%c%d%s", moveList[moveNum][0],
4940 moveList[moveNum][2], moveList[moveNum][3] - '0', moveList[moveNum]+4);
4942 snprintf(buf, MSG_SIZ, "%c%d%c%d%s", moveList[moveNum][0], moveList[moveNum][1] - '0',
4943 moveList[moveNum][2], moveList[moveNum][3] - '0', moveList[moveNum]+4);
4944 SendToProgram(buf, cps);
4946 else SendToProgram(moveList[moveNum], cps);
4947 /* End of additions by Tord */
4950 /* [HGM] setting up the opening has brought engine in force mode! */
4951 /* Send 'go' if we are in a mode where machine should play. */
4952 if( (moveNum == 0 && setboardSpoiledMachineBlack && cps == &first) &&
4953 (gameMode == TwoMachinesPlay ||
4955 gameMode == IcsPlayingBlack || gameMode == IcsPlayingWhite ||
4957 gameMode == MachinePlaysBlack || gameMode == MachinePlaysWhite) ) {
4958 SendToProgram("go\n", cps);
4959 if (appData.debugMode) {
4960 fprintf(debugFP, "(extra)\n");
4963 setboardSpoiledMachineBlack = 0;
4967 SendMoveToICS (ChessMove moveType, int fromX, int fromY, int toX, int toY, char promoChar)
4969 char user_move[MSG_SIZ];
4972 if(gameInfo.variant == VariantSChess && promoChar) {
4973 snprintf(suffix, 4, "=%c", toX == BOARD_WIDTH<<1 ? ToUpper(promoChar) : ToLower(promoChar));
4974 if(moveType == NormalMove) moveType = WhitePromotion; // kludge to do gating
4975 } else suffix[0] = NULLCHAR;
4979 snprintf(user_move, MSG_SIZ, _("say Internal error; bad moveType %d (%d,%d-%d,%d)"),
4980 (int)moveType, fromX, fromY, toX, toY);
4981 DisplayError(user_move + strlen("say "), 0);
4983 case WhiteKingSideCastle:
4984 case BlackKingSideCastle:
4985 case WhiteQueenSideCastleWild:
4986 case BlackQueenSideCastleWild:
4988 case WhiteHSideCastleFR:
4989 case BlackHSideCastleFR:
4991 snprintf(user_move, MSG_SIZ, "o-o%s\n", suffix);
4993 case WhiteQueenSideCastle:
4994 case BlackQueenSideCastle:
4995 case WhiteKingSideCastleWild:
4996 case BlackKingSideCastleWild:
4998 case WhiteASideCastleFR:
4999 case BlackASideCastleFR:
5001 snprintf(user_move, MSG_SIZ, "o-o-o%s\n",suffix);
5003 case WhiteNonPromotion:
5004 case BlackNonPromotion:
5005 sprintf(user_move, "%c%c%c%c==\n", AAA + fromX, ONE + fromY, AAA + toX, ONE + toY);
5007 case WhitePromotion:
5008 case BlackPromotion:
5009 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
5010 snprintf(user_move, MSG_SIZ, "%c%c%c%c=%c\n",
5011 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
5012 PieceToChar(WhiteFerz));
5013 else if(gameInfo.variant == VariantGreat)
5014 snprintf(user_move, MSG_SIZ,"%c%c%c%c=%c\n",
5015 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
5016 PieceToChar(WhiteMan));
5018 snprintf(user_move, MSG_SIZ, "%c%c%c%c=%c\n",
5019 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
5025 snprintf(user_move, MSG_SIZ, "%c@%c%c\n",
5026 ToUpper(PieceToChar((ChessSquare) fromX)),
5027 AAA + toX, ONE + toY);
5029 case IllegalMove: /* could be a variant we don't quite understand */
5030 if(fromY == DROP_RANK) goto drop; // We need 'IllegalDrop' move type?
5032 case WhiteCapturesEnPassant:
5033 case BlackCapturesEnPassant:
5034 snprintf(user_move, MSG_SIZ,"%c%c%c%c\n",
5035 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY);
5038 SendToICS(user_move);
5039 if(appData.keepAlive) // [HGM] alive: schedule sending of dummy 'date' command
5040 ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
5045 { // [HGM] upload: send entire stored game to ICS as long-algebraic moves.
5046 int i, last = forwardMostMove; // make sure ICS reply cannot pre-empt us by clearing fmm
5047 static char *castlingStrings[4] = { "none", "kside", "qside", "both" };
5048 if(gameMode == IcsObserving || gameMode == IcsPlayingBlack || gameMode == IcsPlayingWhite) {
5049 DisplayError(_("You cannot do this while you are playing or observing"), 0);
5052 if(gameMode != IcsExamining) { // is this ever not the case?
5053 char buf[MSG_SIZ], *p, *fen, command[MSG_SIZ], bsetup = 0;
5055 if(ics_type == ICS_ICC) { // on ICC match ourselves in applicable variant
5056 snprintf(command,MSG_SIZ, "match %s", ics_handle);
5057 } else { // on FICS we must first go to general examine mode
5058 safeStrCpy(command, "examine\nbsetup", sizeof(command)/sizeof(command[0])); // and specify variant within it with bsetups
5060 if(gameInfo.variant != VariantNormal) {
5061 // try figure out wild number, as xboard names are not always valid on ICS
5062 for(i=1; i<=36; i++) {
5063 snprintf(buf, MSG_SIZ, "wild/%d", i);
5064 if(StringToVariant(buf) == gameInfo.variant) break;
5066 if(i<=36 && ics_type == ICS_ICC) snprintf(buf, MSG_SIZ,"%s w%d\n", command, i);
5067 else if(i == 22) snprintf(buf,MSG_SIZ, "%s fr\n", command);
5068 else snprintf(buf, MSG_SIZ,"%s %s\n", command, VariantName(gameInfo.variant));
5069 } else snprintf(buf, MSG_SIZ,"%s\n", ics_type == ICS_ICC ? command : "examine\n"); // match yourself or examine
5070 SendToICS(ics_prefix);
5072 if(startedFromSetupPosition || backwardMostMove != 0) {
5073 fen = PositionToFEN(backwardMostMove, NULL);
5074 if(ics_type == ICS_ICC) { // on ICC we can simply send a complete FEN to set everything
5075 snprintf(buf, MSG_SIZ,"loadfen %s\n", fen);
5077 } else { // FICS: everything has to set by separate bsetup commands
5078 p = strchr(fen, ' '); p[0] = NULLCHAR; // cut after board
5079 snprintf(buf, MSG_SIZ,"bsetup fen %s\n", fen);
5081 if(!WhiteOnMove(backwardMostMove)) {
5082 SendToICS("bsetup tomove black\n");
5084 i = (strchr(p+3, 'K') != NULL) + 2*(strchr(p+3, 'Q') != NULL);
5085 snprintf(buf, MSG_SIZ,"bsetup wcastle %s\n", castlingStrings[i]);
5087 i = (strchr(p+3, 'k') != NULL) + 2*(strchr(p+3, 'q') != NULL);
5088 snprintf(buf, MSG_SIZ, "bsetup bcastle %s\n", castlingStrings[i]);
5090 i = boards[backwardMostMove][EP_STATUS];
5091 if(i >= 0) { // set e.p.
5092 snprintf(buf, MSG_SIZ,"bsetup eppos %c\n", i+AAA);
5098 if(bsetup || ics_type != ICS_ICC && gameInfo.variant != VariantNormal)
5099 SendToICS("bsetup done\n"); // switch to normal examining.
5101 for(i = backwardMostMove; i<last; i++) {
5103 snprintf(buf, sizeof(buf)/sizeof(buf[0]),"%s\n", parseList[i]);
5106 SendToICS(ics_prefix);
5107 SendToICS(ics_type == ICS_ICC ? "tag result Game in progress\n" : "commit\n");
5111 CoordsToComputerAlgebraic (int rf, int ff, int rt, int ft, char promoChar, char move[7])
5113 if (rf == DROP_RANK) {
5114 if(ff == EmptySquare) sprintf(move, "@@@@\n"); else // [HGM] pass
5115 sprintf(move, "%c@%c%c\n",
5116 ToUpper(PieceToChar((ChessSquare) ff)), AAA + ft, ONE + rt);
5118 if (promoChar == 'x' || promoChar == NULLCHAR) {
5119 sprintf(move, "%c%c%c%c\n",
5120 AAA + ff, ONE + rf, AAA + ft, ONE + rt);
5122 sprintf(move, "%c%c%c%c%c\n",
5123 AAA + ff, ONE + rf, AAA + ft, ONE + rt, promoChar);
5129 ProcessICSInitScript (FILE *f)
5133 while (fgets(buf, MSG_SIZ, f)) {
5134 SendToICSDelayed(buf,(long)appData.msLoginDelay);
5141 static int lastX, lastY, selectFlag, dragging;
5146 ChessSquare king = WhiteKing, pawn = WhitePawn, last = promoSweep;
5147 if(gameInfo.variant == VariantKnightmate) king = WhiteUnicorn;
5148 if(gameInfo.variant == VariantSuicide || gameInfo.variant == VariantGiveaway) king = EmptySquare;
5149 if(promoSweep >= BlackPawn) king = WHITE_TO_BLACK king, pawn = WHITE_TO_BLACK pawn;
5150 if(gameInfo.variant == VariantSpartan && pawn == BlackPawn) pawn = BlackLance, king = EmptySquare;
5151 if(fromY != BOARD_HEIGHT-2 && fromY != 1) pawn = EmptySquare;
5154 if(promoSweep == EmptySquare) promoSweep = BlackPawn; // wrap
5155 else if((int)promoSweep == -1) promoSweep = WhiteKing;
5156 else if(promoSweep == BlackPawn && step < 0) promoSweep = WhitePawn;
5157 else if(promoSweep == WhiteKing && step > 0) promoSweep = BlackKing;
5158 if(!step) step = -1;
5159 } while(PieceToChar(promoSweep) == '.' || PieceToChar(promoSweep) == '~' || promoSweep == pawn ||
5160 appData.testLegality && (promoSweep == king ||
5161 gameInfo.variant == VariantShogi && promoSweep != PROMOTED last && last != PROMOTED promoSweep && last != promoSweep));
5162 ChangeDragPiece(promoSweep);
5166 PromoScroll (int x, int y)
5170 if(promoSweep == EmptySquare || !appData.sweepSelect) return FALSE;
5171 if(abs(x - lastX) < 25 && abs(y - lastY) < 25) return FALSE;
5172 if( y > lastY + 2 ) step = -1; else if(y < lastY - 2) step = 1;
5173 if(!step) return FALSE;
5174 lastX = x; lastY = y;
5175 if((promoSweep < BlackPawn) == flipView) step = -step;
5176 if(step > 0) selectFlag = 1;
5177 if(!selectFlag) Sweep(step);
5182 NextPiece (int step)
5184 ChessSquare piece = boards[currentMove][toY][toX];
5187 if(pieceSweep == EmptySquare) pieceSweep = WhitePawn; // wrap
5188 if((int)pieceSweep == -1) pieceSweep = BlackKing;
5189 if(!step) step = -1;
5190 } while(PieceToChar(pieceSweep) == '.');
5191 boards[currentMove][toY][toX] = pieceSweep;
5192 DrawPosition(FALSE, boards[currentMove]);
5193 boards[currentMove][toY][toX] = piece;
5195 /* [HGM] Shogi move preprocessor: swap digits for letters, vice versa */
5197 AlphaRank (char *move, int n)
5199 // char *p = move, c; int x, y;
5201 if (appData.debugMode) {
5202 fprintf(debugFP, "alphaRank(%s,%d)\n", move, n);
5206 move[2]>='0' && move[2]<='9' &&
5207 move[3]>='a' && move[3]<='x' ) {
5209 move[2] = BOARD_RGHT -1 - (move[2]-'1') + AAA;
5210 move[3] = BOARD_HEIGHT-1 - (move[3]-'a') + ONE;
5212 if(move[0]>='0' && move[0]<='9' &&
5213 move[1]>='a' && move[1]<='x' &&
5214 move[2]>='0' && move[2]<='9' &&
5215 move[3]>='a' && move[3]<='x' ) {
5216 /* input move, Shogi -> normal */
5217 move[0] = BOARD_RGHT -1 - (move[0]-'1') + AAA;
5218 move[1] = BOARD_HEIGHT-1 - (move[1]-'a') + ONE;
5219 move[2] = BOARD_RGHT -1 - (move[2]-'1') + AAA;
5220 move[3] = BOARD_HEIGHT-1 - (move[3]-'a') + ONE;
5223 move[3]>='0' && move[3]<='9' &&
5224 move[2]>='a' && move[2]<='x' ) {
5226 move[2] = BOARD_RGHT - 1 - (move[2]-AAA) + '1';
5227 move[3] = BOARD_HEIGHT-1 - (move[3]-ONE) + 'a';
5230 move[0]>='a' && move[0]<='x' &&
5231 move[3]>='0' && move[3]<='9' &&
5232 move[2]>='a' && move[2]<='x' ) {
5233 /* output move, normal -> Shogi */
5234 move[0] = BOARD_RGHT - 1 - (move[0]-AAA) + '1';
5235 move[1] = BOARD_HEIGHT-1 - (move[1]-ONE) + 'a';
5236 move[2] = BOARD_RGHT - 1 - (move[2]-AAA) + '1';
5237 move[3] = BOARD_HEIGHT-1 - (move[3]-ONE) + 'a';
5238 if(move[4] == PieceToChar(BlackQueen)) move[4] = '+';
5240 if (appData.debugMode) {
5241 fprintf(debugFP, " out = '%s'\n", move);
5245 char yy_textstr[8000];
5247 /* Parser for moves from gnuchess, ICS, or user typein box */
5249 ParseOneMove (char *move, int moveNum, ChessMove *moveType, int *fromX, int *fromY, int *toX, int *toY, char *promoChar)
5251 *moveType = yylexstr(moveNum, move, yy_textstr, sizeof yy_textstr);
5253 switch (*moveType) {
5254 case WhitePromotion:
5255 case BlackPromotion:
5256 case WhiteNonPromotion:
5257 case BlackNonPromotion:
5259 case WhiteCapturesEnPassant:
5260 case BlackCapturesEnPassant:
5261 case WhiteKingSideCastle:
5262 case WhiteQueenSideCastle:
5263 case BlackKingSideCastle:
5264 case BlackQueenSideCastle:
5265 case WhiteKingSideCastleWild:
5266 case WhiteQueenSideCastleWild:
5267 case BlackKingSideCastleWild:
5268 case BlackQueenSideCastleWild:
5269 /* Code added by Tord: */
5270 case WhiteHSideCastleFR:
5271 case WhiteASideCastleFR:
5272 case BlackHSideCastleFR:
5273 case BlackASideCastleFR:
5274 /* End of code added by Tord */
5275 case IllegalMove: /* bug or odd chess variant */
5276 *fromX = currentMoveString[0] - AAA;
5277 *fromY = currentMoveString[1] - ONE;
5278 *toX = currentMoveString[2] - AAA;
5279 *toY = currentMoveString[3] - ONE;
5280 *promoChar = currentMoveString[4];
5281 if (*fromX < BOARD_LEFT || *fromX >= BOARD_RGHT || *fromY < 0 || *fromY >= BOARD_HEIGHT ||
5282 *toX < BOARD_LEFT || *toX >= BOARD_RGHT || *toY < 0 || *toY >= BOARD_HEIGHT) {
5283 if (appData.debugMode) {
5284 fprintf(debugFP, "Off-board move (%d,%d)-(%d,%d)%c, type = %d\n", *fromX, *fromY, *toX, *toY, *promoChar, *moveType);
5286 *fromX = *fromY = *toX = *toY = 0;
5289 if (appData.testLegality) {
5290 return (*moveType != IllegalMove);
5292 return !(*fromX == *toX && *fromY == *toY) && boards[moveNum][*fromY][*fromX] != EmptySquare &&
5293 WhiteOnMove(moveNum) == (boards[moveNum][*fromY][*fromX] < BlackPawn);
5298 *fromX = *moveType == WhiteDrop ?
5299 (int) CharToPiece(ToUpper(currentMoveString[0])) :
5300 (int) CharToPiece(ToLower(currentMoveString[0]));
5302 *toX = currentMoveString[2] - AAA;
5303 *toY = currentMoveString[3] - ONE;
5304 *promoChar = NULLCHAR;
5308 case ImpossibleMove:
5318 if (appData.debugMode) {
5319 fprintf(debugFP, "Impossible move %s, type = %d\n", currentMoveString, *moveType);
5322 *fromX = *fromY = *toX = *toY = 0;
5323 *promoChar = NULLCHAR;
5328 Boolean pushed = FALSE;
5329 char *lastParseAttempt;
5332 ParsePV (char *pv, Boolean storeComments, Boolean atEnd)
5333 { // Parse a string of PV moves, and append to current game, behind forwardMostMove
5334 int fromX, fromY, toX, toY; char promoChar;
5339 if (gameMode == AnalyzeMode && currentMove < forwardMostMove) {
5340 PushInner(currentMove, forwardMostMove); // [HGM] engine might not be thinking on forwardMost position!
5343 endPV = forwardMostMove;
5345 while(*pv == ' ' || *pv == '\n' || *pv == '\t') pv++; // must still read away whitespace
5346 if(nr == 0 && !storeComments && *pv == '(') pv++; // first (ponder) move can be in parentheses
5347 lastParseAttempt = pv;
5348 valid = ParseOneMove(pv, endPV, &moveType, &fromX, &fromY, &toX, &toY, &promoChar);
5349 if(!valid && nr == 0 &&
5350 ParseOneMove(pv, endPV-1, &moveType, &fromX, &fromY, &toX, &toY, &promoChar)){
5351 nr++; moveType = Comment; // First move has been played; kludge to make sure we continue
5352 // Hande case where played move is different from leading PV move
5353 CopyBoard(boards[endPV+1], boards[endPV-1]); // tentatively unplay last game move
5354 CopyBoard(boards[endPV+2], boards[endPV-1]); // and play first move of PV
5355 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[endPV+2]);
5356 if(!CompareBoards(boards[endPV], boards[endPV+2])) {
5357 endPV += 2; // if position different, keep this
5358 moveList[endPV-1][0] = fromX + AAA;
5359 moveList[endPV-1][1] = fromY + ONE;
5360 moveList[endPV-1][2] = toX + AAA;
5361 moveList[endPV-1][3] = toY + ONE;
5362 parseList[endPV-1][0] = NULLCHAR;
5363 safeStrCpy(moveList[endPV-2], "_0_0", sizeof(moveList[endPV-2])/sizeof(moveList[endPV-2][0])); // suppress premove highlight on takeback move
5366 pv = strstr(pv, yy_textstr) + strlen(yy_textstr); // skip what we parsed
5367 if(nr == 0 && !storeComments && *pv == ')') pv++; // closing parenthesis of ponder move;
5368 if(moveType == Comment && storeComments) AppendComment(endPV, yy_textstr, FALSE);
5369 if(moveType == Comment || moveType == NAG || moveType == ElapsedTime) {
5370 valid++; // allow comments in PV
5374 if(endPV+1 > framePtr) break; // no space, truncate
5377 CopyBoard(boards[endPV], boards[endPV-1]);
5378 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[endPV]);
5379 CoordsToComputerAlgebraic(fromY, fromX, toY, toX, promoChar, moveList[endPV - 1]);
5380 strncat(moveList[endPV-1], "\n", MOVE_LEN);
5381 CoordsToAlgebraic(boards[endPV - 1],
5382 PosFlags(endPV - 1),
5383 fromY, fromX, toY, toX, promoChar,
5384 parseList[endPV - 1]);
5386 if(atEnd == 2) return; // used hidden, for PV conversion
5387 currentMove = (atEnd || endPV == forwardMostMove) ? endPV : forwardMostMove + 1;
5388 if(currentMove == forwardMostMove) ClearPremoveHighlights(); else
5389 SetPremoveHighlights(moveList[currentMove-1][0]-AAA, moveList[currentMove-1][1]-ONE,
5390 moveList[currentMove-1][2]-AAA, moveList[currentMove-1][3]-ONE);
5391 DrawPosition(TRUE, boards[currentMove]);
5395 MultiPV (ChessProgramState *cps)
5396 { // check if engine supports MultiPV, and if so, return the number of the option that sets it
5398 for(i=0; i<cps->nrOptions; i++)
5399 if(!strcmp(cps->option[i].name, "MultiPV") && cps->option[i].type == Spin)
5405 LoadMultiPV (int x, int y, char *buf, int index, int *start, int *end)
5407 int startPV, multi, lineStart, origIndex = index;
5408 char *p, buf2[MSG_SIZ];
5410 if(index < 0 || index >= strlen(buf)) return FALSE; // sanity
5411 lastX = x; lastY = y;
5412 while(index > 0 && buf[index-1] != '\n') index--; // beginning of line
5413 lineStart = startPV = index;
5414 while(buf[index] != '\n') if(buf[index++] == '\t') startPV = index;
5415 if(index == startPV && (p = StrCaseStr(buf+index, "PV="))) startPV = p - buf + 3;
5417 do{ while(buf[index] && buf[index] != '\n') index++;
5418 } while(buf[index] == '\n' && buf[index+1] == '\\' && buf[index+2] == ' ' && index++); // join kibitzed PV continuation line
5420 if(lineStart == 0 && gameMode == AnalyzeMode && (multi = MultiPV(&first)) >= 0) {
5421 int n = first.option[multi].value;
5422 if(origIndex > 17 && origIndex < 24) { if(n>1) n--; } else if(origIndex > index - 6) n++;
5423 snprintf(buf2, MSG_SIZ, "option MultiPV=%d\n", n);
5424 if(first.option[multi].value != n) SendToProgram(buf2, &first);
5425 first.option[multi].value = n;
5429 ParsePV(buf+startPV, FALSE, gameMode != AnalyzeMode);
5430 *start = startPV; *end = index-1;
5437 static char buf[10*MSG_SIZ];
5438 int i, k=0, savedEnd=endPV, saveFMM = forwardMostMove;
5440 if(forwardMostMove < endPV) PushInner(forwardMostMove, endPV);
5441 ParsePV(pv, FALSE, 2); // this appends PV to game, suppressing any display of it
5442 for(i = forwardMostMove; i<endPV; i++){
5443 if(i&1) snprintf(buf+k, 10*MSG_SIZ-k, "%s ", parseList[i]);
5444 else snprintf(buf+k, 10*MSG_SIZ-k, "%d. %s ", i/2 + 1, parseList[i]);
5447 snprintf(buf+k, 10*MSG_SIZ-k, "%s", lastParseAttempt); // if we ran into stuff that could not be parsed, print it verbatim
5448 if(forwardMostMove < savedEnd) { PopInner(0); forwardMostMove = saveFMM; } // PopInner would set fmm to endPV!
5454 LoadPV (int x, int y)
5455 { // called on right mouse click to load PV
5456 int which = gameMode == TwoMachinesPlay && (WhiteOnMove(forwardMostMove) == (second.twoMachinesColor[0] == 'w'));
5457 lastX = x; lastY = y;
5458 ParsePV(lastPV[which], FALSE, TRUE); // load the PV of the thinking engine in the boards array.
5465 int oldFMM = forwardMostMove; // N.B.: this was currentMove before PV was loaded!
5466 if(endPV < 0) return;
5467 if(appData.autoCopyPV) CopyFENToClipboard();
5469 if(gameMode == AnalyzeMode && currentMove > forwardMostMove) {
5470 Boolean saveAnimate = appData.animate;
5472 if(shiftKey && storedGames < MAX_VARIATIONS-2) { // wants to start variation, and there is space
5473 if(storedGames == 1) GreyRevert(FALSE); // we already pushed the tail, so just make it official
5474 } else storedGames--; // abandon shelved tail of original game
5477 forwardMostMove = currentMove;
5478 currentMove = oldFMM;
5479 appData.animate = FALSE;
5480 ToNrEvent(forwardMostMove);
5481 appData.animate = saveAnimate;
5483 currentMove = forwardMostMove;
5484 if(pushed) { PopInner(0); pushed = FALSE; } // restore shelved game continuation
5485 ClearPremoveHighlights();
5486 DrawPosition(TRUE, boards[currentMove]);
5490 MovePV (int x, int y, int h)
5491 { // step through PV based on mouse coordinates (called on mouse move)
5492 int margin = h>>3, step = 0, threshold = (pieceSweep == EmptySquare ? 10 : 15);
5494 // we must somehow check if right button is still down (might be released off board!)
5495 if(endPV < 0 && pieceSweep == EmptySquare) return; // needed in XBoard because lastX/Y is shared :-(
5496 if(abs(x - lastX) < threshold && abs(y - lastY) < threshold) return;
5497 if( y > lastY + 2 ) step = -1; else if(y < lastY - 2) step = 1;
5499 lastX = x; lastY = y;
5501 if(pieceSweep != EmptySquare) { NextPiece(step); return; }
5502 if(endPV < 0) return;
5503 if(y < margin) step = 1; else
5504 if(y > h - margin) step = -1;
5505 if(currentMove + step > endPV || currentMove + step < forwardMostMove) step = 0;
5506 currentMove += step;
5507 if(currentMove == forwardMostMove) ClearPremoveHighlights(); else
5508 SetPremoveHighlights(moveList[currentMove-1][0]-AAA, moveList[currentMove-1][1]-ONE,
5509 moveList[currentMove-1][2]-AAA, moveList[currentMove-1][3]-ONE);
5510 DrawPosition(FALSE, boards[currentMove]);
5514 // [HGM] shuffle: a general way to suffle opening setups, applicable to arbitrary variants.
5515 // All positions will have equal probability, but the current method will not provide a unique
5516 // numbering scheme for arrays that contain 3 or more pieces of the same kind.
5522 int piecesLeft[(int)BlackPawn];
5523 int seed, nrOfShuffles;
5526 GetPositionNumber ()
5527 { // sets global variable seed
5530 seed = appData.defaultFrcPosition;
5531 if(seed < 0) { // randomize based on time for negative FRC position numbers
5532 for(i=0; i<50; i++) seed += random();
5533 seed = random() ^ random() >> 8 ^ random() << 8;
5534 if(seed<0) seed = -seed;
5539 put (Board board, int pieceType, int rank, int n, int shade)
5540 // put the piece on the (n-1)-th empty squares of the given shade
5544 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) {
5545 if( (((i-BOARD_LEFT)&1)+1) & shade && board[rank][i] == EmptySquare && n-- == 0) {
5546 board[rank][i] = (ChessSquare) pieceType;
5547 squaresLeft[((i-BOARD_LEFT)&1) + 1]--;
5549 piecesLeft[pieceType]--;
5558 AddOnePiece (Board board, int pieceType, int rank, int shade)
5559 // calculate where the next piece goes, (any empty square), and put it there
5563 i = seed % squaresLeft[shade];
5564 nrOfShuffles *= squaresLeft[shade];
5565 seed /= squaresLeft[shade];
5566 put(board, pieceType, rank, i, shade);
5570 AddTwoPieces (Board board, int pieceType, int rank)
5571 // calculate where the next 2 identical pieces go, (any empty square), and put it there
5573 int i, n=squaresLeft[ANY], j=n-1, k;
5575 k = n*(n-1)/2; // nr of possibilities, not counting permutations
5576 i = seed % k; // pick one
5579 while(i >= j) i -= j--;
5580 j = n - 1 - j; i += j;
5581 put(board, pieceType, rank, j, ANY);
5582 put(board, pieceType, rank, i, ANY);
5586 SetUpShuffle (Board board, int number)
5590 GetPositionNumber(); nrOfShuffles = 1;
5592 squaresLeft[DARK] = (BOARD_RGHT - BOARD_LEFT + 1)/2;
5593 squaresLeft[ANY] = BOARD_RGHT - BOARD_LEFT;
5594 squaresLeft[LITE] = squaresLeft[ANY] - squaresLeft[DARK];
5596 for(p = 0; p<=(int)WhiteKing; p++) piecesLeft[p] = 0;
5598 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // count pieces and clear board
5599 p = (int) board[0][i];
5600 if(p < (int) BlackPawn) piecesLeft[p] ++;
5601 board[0][i] = EmptySquare;
5604 if(PosFlags(0) & F_ALL_CASTLE_OK) {
5605 // shuffles restricted to allow normal castling put KRR first
5606 if(piecesLeft[(int)WhiteKing]) // King goes rightish of middle
5607 put(board, WhiteKing, 0, (gameInfo.boardWidth+1)/2, ANY);
5608 else if(piecesLeft[(int)WhiteUnicorn]) // in Knightmate Unicorn castles
5609 put(board, WhiteUnicorn, 0, (gameInfo.boardWidth+1)/2, ANY);
5610 if(piecesLeft[(int)WhiteRook]) // First supply a Rook for K-side castling
5611 put(board, WhiteRook, 0, gameInfo.boardWidth-2, ANY);
5612 if(piecesLeft[(int)WhiteRook]) // Then supply a Rook for Q-side castling
5613 put(board, WhiteRook, 0, 0, ANY);
5614 // in variants with super-numerary Kings and Rooks, we leave these for the shuffle
5617 if(((BOARD_RGHT-BOARD_LEFT) & 1) == 0)
5618 // only for even boards make effort to put pairs of colorbound pieces on opposite colors
5619 for(p = (int) WhiteKing; p > (int) WhitePawn; p--) {
5620 if(p != (int) WhiteBishop && p != (int) WhiteFerz && p != (int) WhiteAlfil) continue;
5621 while(piecesLeft[p] >= 2) {
5622 AddOnePiece(board, p, 0, LITE);
5623 AddOnePiece(board, p, 0, DARK);
5625 // Odd color-bound pieces are shuffled with the rest (to not run out of paired squares)
5628 for(p = (int) WhiteKing - 2; p > (int) WhitePawn; p--) {
5629 // Remaining pieces (non-colorbound, or odd color bound) can be put anywhere
5630 // but we leave King and Rooks for last, to possibly obey FRC restriction
5631 if(p == (int)WhiteRook) continue;
5632 while(piecesLeft[p] >= 2) AddTwoPieces(board, p, 0); // add in pairs, for not counting permutations
5633 if(piecesLeft[p]) AddOnePiece(board, p, 0, ANY); // add the odd piece
5636 // now everything is placed, except perhaps King (Unicorn) and Rooks
5638 if(PosFlags(0) & F_FRC_TYPE_CASTLING) {
5639 // Last King gets castling rights
5640 while(piecesLeft[(int)WhiteUnicorn]) {
5641 i = put(board, WhiteUnicorn, 0, piecesLeft[(int)WhiteRook]/2, ANY);
5642 initialRights[2] = initialRights[5] = board[CASTLING][2] = board[CASTLING][5] = i;
5645 while(piecesLeft[(int)WhiteKing]) {
5646 i = put(board, WhiteKing, 0, piecesLeft[(int)WhiteRook]/2, ANY);
5647 initialRights[2] = initialRights[5] = board[CASTLING][2] = board[CASTLING][5] = i;
5652 while(piecesLeft[(int)WhiteKing]) AddOnePiece(board, WhiteKing, 0, ANY);
5653 while(piecesLeft[(int)WhiteUnicorn]) AddOnePiece(board, WhiteUnicorn, 0, ANY);
5656 // Only Rooks can be left; simply place them all
5657 while(piecesLeft[(int)WhiteRook]) {
5658 i = put(board, WhiteRook, 0, 0, ANY);
5659 if(PosFlags(0) & F_FRC_TYPE_CASTLING) { // first and last Rook get FRC castling rights
5662 initialRights[1] = initialRights[4] = board[CASTLING][1] = board[CASTLING][4] = i;
5664 initialRights[0] = initialRights[3] = board[CASTLING][0] = board[CASTLING][3] = i;
5667 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // copy black from white
5668 board[BOARD_HEIGHT-1][i] = (int) board[0][i] < BlackPawn ? WHITE_TO_BLACK board[0][i] : EmptySquare;
5671 if(number >= 0) appData.defaultFrcPosition %= nrOfShuffles; // normalize
5675 SetCharTable (char *table, const char * map)
5676 /* [HGM] moved here from winboard.c because of its general usefulness */
5677 /* Basically a safe strcpy that uses the last character as King */
5679 int result = FALSE; int NrPieces;
5681 if( map != NULL && (NrPieces=strlen(map)) <= (int) EmptySquare
5682 && NrPieces >= 12 && !(NrPieces&1)) {
5683 int i; /* [HGM] Accept even length from 12 to 34 */
5685 for( i=0; i<(int) EmptySquare; i++ ) table[i] = '.';
5686 for( i=0; i<NrPieces/2-1; i++ ) {
5688 table[i + (int)BlackPawn - (int) WhitePawn] = map[i+NrPieces/2];
5690 table[(int) WhiteKing] = map[NrPieces/2-1];
5691 table[(int) BlackKing] = map[NrPieces-1];
5700 Prelude (Board board)
5701 { // [HGM] superchess: random selection of exo-pieces
5702 int i, j, k; ChessSquare p;
5703 static ChessSquare exoPieces[4] = { WhiteAngel, WhiteMarshall, WhiteSilver, WhiteLance };
5705 GetPositionNumber(); // use FRC position number
5707 if(appData.pieceToCharTable != NULL) { // select pieces to participate from given char table
5708 SetCharTable(pieceToChar, appData.pieceToCharTable);
5709 for(i=(int)WhiteQueen+1, j=0; i<(int)WhiteKing && j<4; i++)
5710 if(PieceToChar((ChessSquare)i) != '.') exoPieces[j++] = (ChessSquare) i;
5713 j = seed%4; seed /= 4;
5714 p = board[0][BOARD_LEFT+j]; board[0][BOARD_LEFT+j] = EmptySquare; k = PieceToNumber(p);
5715 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5716 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5717 j = seed%3 + (seed%3 >= j); seed /= 3;
5718 p = board[0][BOARD_LEFT+j]; board[0][BOARD_LEFT+j] = EmptySquare; k = PieceToNumber(p);
5719 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5720 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5721 j = seed%3; seed /= 3;
5722 p = board[0][BOARD_LEFT+j+5]; board[0][BOARD_LEFT+j+5] = EmptySquare; k = PieceToNumber(p);
5723 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5724 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5725 j = seed%2 + (seed%2 >= j); seed /= 2;
5726 p = board[0][BOARD_LEFT+j+5]; board[0][BOARD_LEFT+j+5] = EmptySquare; k = PieceToNumber(p);
5727 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5728 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5729 j = seed%4; seed /= 4; put(board, exoPieces[3], 0, j, ANY);
5730 j = seed%3; seed /= 3; put(board, exoPieces[2], 0, j, ANY);
5731 j = seed%2; seed /= 2; put(board, exoPieces[1], 0, j, ANY);
5732 put(board, exoPieces[0], 0, 0, ANY);
5733 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) board[BOARD_HEIGHT-1][i] = WHITE_TO_BLACK board[0][i];
5737 InitPosition (int redraw)
5739 ChessSquare (* pieces)[BOARD_FILES];
5740 int i, j, pawnRow, overrule,
5741 oldx = gameInfo.boardWidth,
5742 oldy = gameInfo.boardHeight,
5743 oldh = gameInfo.holdingsWidth;
5746 if(appData.icsActive) shuffleOpenings = FALSE; // [HGM] shuffle: in ICS mode, only shuffle on ICS request
5748 /* [AS] Initialize pv info list [HGM] and game status */
5750 for( i=0; i<=framePtr; i++ ) { // [HGM] vari: spare saved variations
5751 pvInfoList[i].depth = 0;
5752 boards[i][EP_STATUS] = EP_NONE;
5753 for( j=0; j<BOARD_FILES-2; j++ ) boards[i][CASTLING][j] = NoRights;
5756 initialRulePlies = 0; /* 50-move counter start */
5758 castlingRank[0] = castlingRank[1] = castlingRank[2] = 0;
5759 castlingRank[3] = castlingRank[4] = castlingRank[5] = BOARD_HEIGHT-1;
5763 /* [HGM] logic here is completely changed. In stead of full positions */
5764 /* the initialized data only consist of the two backranks. The switch */
5765 /* selects which one we will use, which is than copied to the Board */
5766 /* initialPosition, which for the rest is initialized by Pawns and */
5767 /* empty squares. This initial position is then copied to boards[0], */
5768 /* possibly after shuffling, so that it remains available. */
5770 gameInfo.holdingsWidth = 0; /* default board sizes */
5771 gameInfo.boardWidth = 8;
5772 gameInfo.boardHeight = 8;
5773 gameInfo.holdingsSize = 0;
5774 nrCastlingRights = -1; /* [HGM] Kludge to indicate default should be used */
5775 for(i=0; i<BOARD_FILES-2; i++)
5776 initialPosition[CASTLING][i] = initialRights[i] = NoRights; /* but no rights yet */
5777 initialPosition[EP_STATUS] = EP_NONE;
5778 SetCharTable(pieceToChar, "PNBRQ...........Kpnbrq...........k");
5779 if(startVariant == gameInfo.variant) // [HGM] nicks: enable nicknames in original variant
5780 SetCharTable(pieceNickName, appData.pieceNickNames);
5781 else SetCharTable(pieceNickName, "............");
5784 switch (gameInfo.variant) {
5785 case VariantFischeRandom:
5786 shuffleOpenings = TRUE;
5789 case VariantShatranj:
5790 pieces = ShatranjArray;
5791 nrCastlingRights = 0;
5792 SetCharTable(pieceToChar, "PN.R.QB...Kpn.r.qb...k");
5795 pieces = makrukArray;
5796 nrCastlingRights = 0;
5797 startedFromSetupPosition = TRUE;
5798 SetCharTable(pieceToChar, "PN.R.M....SKpn.r.m....sk");
5800 case VariantTwoKings:
5801 pieces = twoKingsArray;
5804 pieces = GrandArray;
5805 nrCastlingRights = 0;
5806 SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
5807 gameInfo.boardWidth = 10;
5808 gameInfo.boardHeight = 10;
5809 gameInfo.holdingsSize = 7;
5811 case VariantCapaRandom:
5812 shuffleOpenings = TRUE;
5813 case VariantCapablanca:
5814 pieces = CapablancaArray;
5815 gameInfo.boardWidth = 10;
5816 SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
5819 pieces = GothicArray;
5820 gameInfo.boardWidth = 10;
5821 SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
5824 SetCharTable(pieceToChar, "PNBRQ..HEKpnbrq..hek");
5825 gameInfo.holdingsSize = 7;
5828 pieces = JanusArray;
5829 gameInfo.boardWidth = 10;
5830 SetCharTable(pieceToChar, "PNBRQ..JKpnbrq..jk");
5831 nrCastlingRights = 6;
5832 initialPosition[CASTLING][0] = initialRights[0] = BOARD_RGHT-1;
5833 initialPosition[CASTLING][1] = initialRights[1] = BOARD_LEFT;
5834 initialPosition[CASTLING][2] = initialRights[2] =(BOARD_WIDTH-1)>>1;
5835 initialPosition[CASTLING][3] = initialRights[3] = BOARD_RGHT-1;
5836 initialPosition[CASTLING][4] = initialRights[4] = BOARD_LEFT;
5837 initialPosition[CASTLING][5] = initialRights[5] =(BOARD_WIDTH-1)>>1;
5840 pieces = FalconArray;
5841 gameInfo.boardWidth = 10;
5842 SetCharTable(pieceToChar, "PNBRQ.............FKpnbrq.............fk");
5844 case VariantXiangqi:
5845 pieces = XiangqiArray;
5846 gameInfo.boardWidth = 9;
5847 gameInfo.boardHeight = 10;
5848 nrCastlingRights = 0;
5849 SetCharTable(pieceToChar, "PH.R.AE..K.C.ph.r.ae..k.c.");
5852 pieces = ShogiArray;
5853 gameInfo.boardWidth = 9;
5854 gameInfo.boardHeight = 9;
5855 gameInfo.holdingsSize = 7;
5856 nrCastlingRights = 0;
5857 SetCharTable(pieceToChar, "PNBRLS...G.++++++Kpnbrls...g.++++++k");
5859 case VariantCourier:
5860 pieces = CourierArray;
5861 gameInfo.boardWidth = 12;
5862 nrCastlingRights = 0;
5863 SetCharTable(pieceToChar, "PNBR.FE..WMKpnbr.fe..wmk");
5865 case VariantKnightmate:
5866 pieces = KnightmateArray;
5867 SetCharTable(pieceToChar, "P.BRQ.....M.........K.p.brq.....m.........k.");
5869 case VariantSpartan:
5870 pieces = SpartanArray;
5871 SetCharTable(pieceToChar, "PNBRQ................K......lwg.....c...h..k");
5874 pieces = fairyArray;
5875 SetCharTable(pieceToChar, "PNBRQFEACWMOHIJGDVLSUKpnbrqfeacwmohijgdvlsuk");
5878 pieces = GreatArray;
5879 gameInfo.boardWidth = 10;
5880 SetCharTable(pieceToChar, "PN....E...S..HWGMKpn....e...s..hwgmk");
5881 gameInfo.holdingsSize = 8;
5885 SetCharTable(pieceToChar, "PNBRQ..SE.......V.AKpnbrq..se.......v.ak");
5886 gameInfo.holdingsSize = 8;
5887 startedFromSetupPosition = TRUE;
5889 case VariantCrazyhouse:
5890 case VariantBughouse:
5892 SetCharTable(pieceToChar, "PNBRQ.......~~~~Kpnbrq.......~~~~k");
5893 gameInfo.holdingsSize = 5;
5895 case VariantWildCastle:
5897 /* !!?shuffle with kings guaranteed to be on d or e file */
5898 shuffleOpenings = 1;
5900 case VariantNoCastle:
5902 nrCastlingRights = 0;
5903 /* !!?unconstrained back-rank shuffle */
5904 shuffleOpenings = 1;
5909 if(appData.NrFiles >= 0) {
5910 if(gameInfo.boardWidth != appData.NrFiles) overrule++;
5911 gameInfo.boardWidth = appData.NrFiles;
5913 if(appData.NrRanks >= 0) {
5914 gameInfo.boardHeight = appData.NrRanks;
5916 if(appData.holdingsSize >= 0) {
5917 i = appData.holdingsSize;
5918 if(i > gameInfo.boardHeight) i = gameInfo.boardHeight;
5919 gameInfo.holdingsSize = i;
5921 if(gameInfo.holdingsSize) gameInfo.holdingsWidth = 2;
5922 if(BOARD_HEIGHT > BOARD_RANKS || BOARD_WIDTH > BOARD_FILES)
5923 DisplayFatalError(_("Recompile to support this BOARD_RANKS or BOARD_FILES!"), 0, 2);
5925 pawnRow = gameInfo.boardHeight - 7; /* seems to work in all common variants */
5926 if(pawnRow < 1) pawnRow = 1;
5927 if(gameInfo.variant == VariantMakruk || gameInfo.variant == VariantGrand) pawnRow = 2;
5929 /* User pieceToChar list overrules defaults */
5930 if(appData.pieceToCharTable != NULL)
5931 SetCharTable(pieceToChar, appData.pieceToCharTable);
5933 for( j=0; j<BOARD_WIDTH; j++ ) { ChessSquare s = EmptySquare;
5935 if(j==BOARD_LEFT-1 || j==BOARD_RGHT)
5936 s = (ChessSquare) 0; /* account holding counts in guard band */
5937 for( i=0; i<BOARD_HEIGHT; i++ )
5938 initialPosition[i][j] = s;
5940 if(j < BOARD_LEFT || j >= BOARD_RGHT || overrule) continue;
5941 initialPosition[gameInfo.variant == VariantGrand][j] = pieces[0][j-gameInfo.holdingsWidth];
5942 initialPosition[pawnRow][j] = WhitePawn;
5943 initialPosition[BOARD_HEIGHT-pawnRow-1][j] = gameInfo.variant == VariantSpartan ? BlackLance : BlackPawn;
5944 if(gameInfo.variant == VariantXiangqi) {
5946 initialPosition[pawnRow][j] =
5947 initialPosition[BOARD_HEIGHT-pawnRow-1][j] = EmptySquare;
5948 if(j==BOARD_LEFT+1 || j>=BOARD_RGHT-2) {
5949 initialPosition[2][j] = WhiteCannon;
5950 initialPosition[BOARD_HEIGHT-3][j] = BlackCannon;
5954 if(gameInfo.variant == VariantGrand) {
5955 if(j==BOARD_LEFT || j>=BOARD_RGHT-1) {
5956 initialPosition[0][j] = WhiteRook;
5957 initialPosition[BOARD_HEIGHT-1][j] = BlackRook;
5960 initialPosition[BOARD_HEIGHT-1-(gameInfo.variant == VariantGrand)][j] = pieces[1][j-gameInfo.holdingsWidth];
5962 if( (gameInfo.variant == VariantShogi) && !overrule ) {
5965 initialPosition[1][j] = WhiteBishop;
5966 initialPosition[BOARD_HEIGHT-2][j] = BlackRook;
5968 initialPosition[1][j] = WhiteRook;
5969 initialPosition[BOARD_HEIGHT-2][j] = BlackBishop;
5972 if( nrCastlingRights == -1) {
5973 /* [HGM] Build normal castling rights (must be done after board sizing!) */
5974 /* This sets default castling rights from none to normal corners */
5975 /* Variants with other castling rights must set them themselves above */
5976 nrCastlingRights = 6;
5978 initialPosition[CASTLING][0] = initialRights[0] = BOARD_RGHT-1;
5979 initialPosition[CASTLING][1] = initialRights[1] = BOARD_LEFT;
5980 initialPosition[CASTLING][2] = initialRights[2] = BOARD_WIDTH>>1;
5981 initialPosition[CASTLING][3] = initialRights[3] = BOARD_RGHT-1;
5982 initialPosition[CASTLING][4] = initialRights[4] = BOARD_LEFT;
5983 initialPosition[CASTLING][5] = initialRights[5] = BOARD_WIDTH>>1;
5986 if(gameInfo.variant == VariantSuper) Prelude(initialPosition);
5987 if(gameInfo.variant == VariantGreat) { // promotion commoners
5988 initialPosition[PieceToNumber(WhiteMan)][BOARD_WIDTH-1] = WhiteMan;
5989 initialPosition[PieceToNumber(WhiteMan)][BOARD_WIDTH-2] = 9;
5990 initialPosition[BOARD_HEIGHT-1-PieceToNumber(WhiteMan)][0] = BlackMan;
5991 initialPosition[BOARD_HEIGHT-1-PieceToNumber(WhiteMan)][1] = 9;
5993 if( gameInfo.variant == VariantSChess ) {
5994 initialPosition[1][0] = BlackMarshall;
5995 initialPosition[2][0] = BlackAngel;
5996 initialPosition[6][BOARD_WIDTH-1] = WhiteMarshall;
5997 initialPosition[5][BOARD_WIDTH-1] = WhiteAngel;
5998 initialPosition[1][1] = initialPosition[2][1] =
5999 initialPosition[6][BOARD_WIDTH-2] = initialPosition[5][BOARD_WIDTH-2] = 1;
6001 if (appData.debugMode) {
6002 fprintf(debugFP, "shuffleOpenings = %d\n", shuffleOpenings);
6004 if(shuffleOpenings) {
6005 SetUpShuffle(initialPosition, appData.defaultFrcPosition);
6006 startedFromSetupPosition = TRUE;
6008 if(startedFromPositionFile) {
6009 /* [HGM] loadPos: use PositionFile for every new game */
6010 CopyBoard(initialPosition, filePosition);
6011 for(i=0; i<nrCastlingRights; i++)
6012 initialRights[i] = filePosition[CASTLING][i];
6013 startedFromSetupPosition = TRUE;
6016 CopyBoard(boards[0], initialPosition);
6018 if(oldx != gameInfo.boardWidth ||
6019 oldy != gameInfo.boardHeight ||
6020 oldv != gameInfo.variant ||
6021 oldh != gameInfo.holdingsWidth
6023 InitDrawingSizes(-2 ,0);
6025 oldv = gameInfo.variant;
6027 DrawPosition(TRUE, boards[currentMove]);
6031 SendBoard (ChessProgramState *cps, int moveNum)
6033 char message[MSG_SIZ];
6035 if (cps->useSetboard) {
6036 char* fen = PositionToFEN(moveNum, cps->fenOverride);
6037 snprintf(message, MSG_SIZ,"setboard %s\n", fen);
6038 SendToProgram(message, cps);
6043 int i, j, left=0, right=BOARD_WIDTH;
6044 /* Kludge to set black to move, avoiding the troublesome and now
6045 * deprecated "black" command.
6047 if (!WhiteOnMove(moveNum)) // [HGM] but better a deprecated command than an illegal move...
6048 SendToProgram(boards[0][1][BOARD_LEFT] == WhitePawn ? "a2a3\n" : "black\n", cps);
6050 if(!cps->extendedEdit) left = BOARD_LEFT, right = BOARD_RGHT; // only board proper
6052 SendToProgram("edit\n", cps);
6053 SendToProgram("#\n", cps);
6054 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
6055 bp = &boards[moveNum][i][left];
6056 for (j = left; j < right; j++, bp++) {
6057 if(j == BOARD_LEFT-1 || j == BOARD_RGHT) continue;
6058 if ((int) *bp < (int) BlackPawn) {
6059 if(j == BOARD_RGHT+1)
6060 snprintf(message, MSG_SIZ, "%c@%d\n", PieceToChar(*bp), bp[-1]);
6061 else snprintf(message, MSG_SIZ, "%c%c%c\n", PieceToChar(*bp), AAA + j, ONE + i);
6062 if(message[0] == '+' || message[0] == '~') {
6063 snprintf(message, MSG_SIZ,"%c%c%c+\n",
6064 PieceToChar((ChessSquare)(DEMOTED *bp)),
6067 if(cps->alphaRank) { /* [HGM] shogi: translate coords */
6068 message[1] = BOARD_RGHT - 1 - j + '1';
6069 message[2] = BOARD_HEIGHT - 1 - i + 'a';
6071 SendToProgram(message, cps);
6076 SendToProgram("c\n", cps);
6077 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
6078 bp = &boards[moveNum][i][left];
6079 for (j = left; j < right; j++, bp++) {
6080 if(j == BOARD_LEFT-1 || j == BOARD_RGHT) continue;
6081 if (((int) *bp != (int) EmptySquare)
6082 && ((int) *bp >= (int) BlackPawn)) {
6083 if(j == BOARD_LEFT-2)
6084 snprintf(message, MSG_SIZ, "%c@%d\n", ToUpper(PieceToChar(*bp)), bp[1]);
6085 else snprintf(message,MSG_SIZ, "%c%c%c\n", ToUpper(PieceToChar(*bp)),
6087 if(message[0] == '+' || message[0] == '~') {
6088 snprintf(message, MSG_SIZ,"%c%c%c+\n",
6089 PieceToChar((ChessSquare)(DEMOTED *bp)),
6092 if(cps->alphaRank) { /* [HGM] shogi: translate coords */
6093 message[1] = BOARD_RGHT - 1 - j + '1';
6094 message[2] = BOARD_HEIGHT - 1 - i + 'a';
6096 SendToProgram(message, cps);
6101 SendToProgram(".\n", cps);
6103 setboardSpoiledMachineBlack = 0; /* [HGM] assume WB 4.2.7 already solves this after sending setboard */
6107 DefaultPromoChoice (int white)
6110 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
6111 result = WhiteFerz; // no choice
6112 else if(gameInfo.variant == VariantSuicide || gameInfo.variant == VariantGiveaway)
6113 result= WhiteKing; // in Suicide Q is the last thing we want
6114 else if(gameInfo.variant == VariantSpartan)
6115 result = white ? WhiteQueen : WhiteAngel;
6116 else result = WhiteQueen;
6117 if(!white) result = WHITE_TO_BLACK result;
6121 static int autoQueen; // [HGM] oneclick
6124 HasPromotionChoice (int fromX, int fromY, int toX, int toY, char *promoChoice, int sweepSelect)
6126 /* [HGM] rewritten IsPromotion to only flag promotions that offer a choice */
6127 /* [HGM] add Shogi promotions */
6128 int promotionZoneSize=1, highestPromotingPiece = (int)WhitePawn;
6133 if(fromX < BOARD_LEFT || fromX >= BOARD_RGHT) return FALSE; // drop
6134 if(toX < BOARD_LEFT || toX >= BOARD_RGHT) return FALSE; // move into holdings
6136 if(gameMode == EditPosition || gameInfo.variant == VariantXiangqi || // no promotions
6137 !(fromX >=0 && fromY >= 0 && toX >= 0 && toY >= 0) ) // invalid move
6140 piece = boards[currentMove][fromY][fromX];
6141 if(gameInfo.variant == VariantShogi) {
6142 promotionZoneSize = BOARD_HEIGHT/3;
6143 highestPromotingPiece = (int)WhiteFerz;
6144 } else if(gameInfo.variant == VariantMakruk || gameInfo.variant == VariantGrand) {
6145 promotionZoneSize = 3;
6148 // Treat Lance as Pawn when it is not representing Amazon
6149 if(gameInfo.variant != VariantSuper) {
6150 if(piece == WhiteLance) piece = WhitePawn; else
6151 if(piece == BlackLance) piece = BlackPawn;
6154 // next weed out all moves that do not touch the promotion zone at all
6155 if((int)piece >= BlackPawn) {
6156 if(toY >= promotionZoneSize && fromY >= promotionZoneSize)
6158 highestPromotingPiece = WHITE_TO_BLACK highestPromotingPiece;
6160 if( toY < BOARD_HEIGHT - promotionZoneSize &&
6161 fromY < BOARD_HEIGHT - promotionZoneSize) return FALSE;
6164 if( (int)piece > highestPromotingPiece ) return FALSE; // non-promoting piece
6166 // weed out mandatory Shogi promotions
6167 if(gameInfo.variant == VariantShogi) {
6168 if(piece >= BlackPawn) {
6169 if(toY == 0 && piece == BlackPawn ||
6170 toY == 0 && piece == BlackQueen ||
6171 toY <= 1 && piece == BlackKnight) {
6176 if(toY == BOARD_HEIGHT-1 && piece == WhitePawn ||
6177 toY == BOARD_HEIGHT-1 && piece == WhiteQueen ||
6178 toY >= BOARD_HEIGHT-2 && piece == WhiteKnight) {
6185 // weed out obviously illegal Pawn moves
6186 if(appData.testLegality && (piece == WhitePawn || piece == BlackPawn) ) {
6187 if(toX > fromX+1 || toX < fromX-1) return FALSE; // wide
6188 if(piece == WhitePawn && toY != fromY+1) return FALSE; // deep
6189 if(piece == BlackPawn && toY != fromY-1) return FALSE; // deep
6190 if(fromX != toX && gameInfo.variant == VariantShogi) return FALSE;
6191 // note we are not allowed to test for valid (non-)capture, due to premove
6194 // we either have a choice what to promote to, or (in Shogi) whether to promote
6195 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk) {
6196 *promoChoice = PieceToChar(BlackFerz); // no choice
6199 // no sense asking what we must promote to if it is going to explode...
6200 if(gameInfo.variant == VariantAtomic && boards[currentMove][toY][toX] != EmptySquare) {
6201 *promoChoice = PieceToChar(BlackQueen); // Queen as good as any
6204 // give caller the default choice even if we will not make it
6205 *promoChoice = ToLower(PieceToChar(defaultPromoChoice));
6206 if(gameInfo.variant == VariantShogi) *promoChoice = (defaultPromoChoice == piece ? '=' : '+');
6207 if( sweepSelect && gameInfo.variant != VariantGreat
6208 && gameInfo.variant != VariantGrand
6209 && gameInfo.variant != VariantSuper) return FALSE;
6210 if(autoQueen) return FALSE; // predetermined
6212 // suppress promotion popup on illegal moves that are not premoves
6213 premove = gameMode == IcsPlayingWhite && !WhiteOnMove(currentMove) ||
6214 gameMode == IcsPlayingBlack && WhiteOnMove(currentMove);
6215 if(appData.testLegality && !premove) {
6216 moveType = LegalityTest(boards[currentMove], PosFlags(currentMove),
6217 fromY, fromX, toY, toX, gameInfo.variant == VariantShogi ? '+' : NULLCHAR);
6218 if(moveType != WhitePromotion && moveType != BlackPromotion)
6226 InPalace (int row, int column)
6227 { /* [HGM] for Xiangqi */
6228 if( (row < 3 || row > BOARD_HEIGHT-4) &&
6229 column < (BOARD_WIDTH + 4)/2 &&
6230 column > (BOARD_WIDTH - 5)/2 ) return TRUE;
6235 PieceForSquare (int x, int y)
6237 if (x < 0 || x >= BOARD_WIDTH || y < 0 || y >= BOARD_HEIGHT)
6240 return boards[currentMove][y][x];
6244 OKToStartUserMove (int x, int y)
6246 ChessSquare from_piece;
6249 if (matchMode) return FALSE;
6250 if (gameMode == EditPosition) return TRUE;
6252 if (x >= 0 && y >= 0)
6253 from_piece = boards[currentMove][y][x];
6255 from_piece = EmptySquare;
6257 if (from_piece == EmptySquare) return FALSE;
6259 white_piece = (int)from_piece >= (int)WhitePawn &&
6260 (int)from_piece < (int)BlackPawn; /* [HGM] can be > King! */
6264 case TwoMachinesPlay:
6272 case MachinePlaysWhite:
6273 case IcsPlayingBlack:
6274 if (appData.zippyPlay) return FALSE;
6276 DisplayMoveError(_("You are playing Black"));
6281 case MachinePlaysBlack:
6282 case IcsPlayingWhite:
6283 if (appData.zippyPlay) return FALSE;
6285 DisplayMoveError(_("You are playing White"));
6290 case PlayFromGameFile:
6291 if(!shiftKey || !appData.variations) return FALSE; // [HGM] allow starting variation in this mode
6293 if (!white_piece && WhiteOnMove(currentMove)) {
6294 DisplayMoveError(_("It is White's turn"));
6297 if (white_piece && !WhiteOnMove(currentMove)) {
6298 DisplayMoveError(_("It is Black's turn"));
6301 if (cmailMsgLoaded && (currentMove < cmailOldMove)) {
6302 /* Editing correspondence game history */
6303 /* Could disallow this or prompt for confirmation */
6308 case BeginningOfGame:
6309 if (appData.icsActive) return FALSE;
6310 if (!appData.noChessProgram) {
6312 DisplayMoveError(_("You are playing White"));
6319 if (!white_piece && WhiteOnMove(currentMove)) {
6320 DisplayMoveError(_("It is White's turn"));
6323 if (white_piece && !WhiteOnMove(currentMove)) {
6324 DisplayMoveError(_("It is Black's turn"));
6333 if (currentMove != forwardMostMove && gameMode != AnalyzeMode
6334 && gameMode != EditGame // [HGM] vari: treat as AnalyzeMode
6335 && gameMode != PlayFromGameFile // [HGM] as EditGame, with protected main line
6336 && gameMode != AnalyzeFile && gameMode != Training) {
6337 DisplayMoveError(_("Displayed position is not current"));
6344 OnlyMove (int *x, int *y, Boolean captures)
6346 DisambiguateClosure cl;
6347 if (appData.zippyPlay || !appData.testLegality) return FALSE;
6349 case MachinePlaysBlack:
6350 case IcsPlayingWhite:
6351 case BeginningOfGame:
6352 if(!WhiteOnMove(currentMove)) return FALSE;
6354 case MachinePlaysWhite:
6355 case IcsPlayingBlack:
6356 if(WhiteOnMove(currentMove)) return FALSE;
6363 cl.pieceIn = EmptySquare;
6368 cl.promoCharIn = NULLCHAR;
6369 Disambiguate(boards[currentMove], PosFlags(currentMove), &cl);
6370 if( cl.kind == NormalMove ||
6371 cl.kind == AmbiguousMove && captures && cl.captures == 1 ||
6372 cl.kind == WhitePromotion || cl.kind == BlackPromotion ||
6373 cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) {
6380 if(cl.kind != ImpossibleMove) return FALSE;
6381 cl.pieceIn = EmptySquare;
6386 cl.promoCharIn = NULLCHAR;
6387 Disambiguate(boards[currentMove], PosFlags(currentMove), &cl);
6388 if( cl.kind == NormalMove ||
6389 cl.kind == AmbiguousMove && captures && cl.captures == 1 ||
6390 cl.kind == WhitePromotion || cl.kind == BlackPromotion ||
6391 cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) {
6396 autoQueen = TRUE; // act as if autoQueen on when we click to-square
6402 FILE *lastLoadGameFP = NULL, *lastLoadPositionFP = NULL;
6403 int lastLoadGameNumber = 0, lastLoadPositionNumber = 0;
6404 int lastLoadGameUseList = FALSE;
6405 char lastLoadGameTitle[MSG_SIZ], lastLoadPositionTitle[MSG_SIZ];
6406 ChessMove lastLoadGameStart = EndOfFile;
6409 UserMoveEvent(int fromX, int fromY, int toX, int toY, int promoChar)
6412 ChessSquare pdown, pup;
6414 /* Check if the user is playing in turn. This is complicated because we
6415 let the user "pick up" a piece before it is his turn. So the piece he
6416 tried to pick up may have been captured by the time he puts it down!
6417 Therefore we use the color the user is supposed to be playing in this
6418 test, not the color of the piece that is currently on the starting
6419 square---except in EditGame mode, where the user is playing both
6420 sides; fortunately there the capture race can't happen. (It can
6421 now happen in IcsExamining mode, but that's just too bad. The user
6422 will get a somewhat confusing message in that case.)
6427 case TwoMachinesPlay:
6431 /* We switched into a game mode where moves are not accepted,
6432 perhaps while the mouse button was down. */
6435 case MachinePlaysWhite:
6436 /* User is moving for Black */
6437 if (WhiteOnMove(currentMove)) {
6438 DisplayMoveError(_("It is White's turn"));
6443 case MachinePlaysBlack:
6444 /* User is moving for White */
6445 if (!WhiteOnMove(currentMove)) {
6446 DisplayMoveError(_("It is Black's turn"));
6451 case PlayFromGameFile:
6452 if(!shiftKey ||!appData.variations) return; // [HGM] only variations
6455 case BeginningOfGame:
6458 if(fromY == DROP_RANK) break; // [HGM] drop moves (entered through move type-in) are automatically assigned to side-to-move
6459 if ((int) boards[currentMove][fromY][fromX] >= (int) BlackPawn &&
6460 (int) boards[currentMove][fromY][fromX] < (int) EmptySquare) {
6461 /* User is moving for Black */
6462 if (WhiteOnMove(currentMove)) {
6463 DisplayMoveError(_("It is White's turn"));
6467 /* User is moving for White */
6468 if (!WhiteOnMove(currentMove)) {
6469 DisplayMoveError(_("It is Black's turn"));
6475 case IcsPlayingBlack:
6476 /* User is moving for Black */
6477 if (WhiteOnMove(currentMove)) {
6478 if (!appData.premove) {
6479 DisplayMoveError(_("It is White's turn"));
6480 } else if (toX >= 0 && toY >= 0) {
6483 premoveFromX = fromX;
6484 premoveFromY = fromY;
6485 premovePromoChar = promoChar;
6487 if (appData.debugMode)
6488 fprintf(debugFP, "Got premove: fromX %d,"
6489 "fromY %d, toX %d, toY %d\n",
6490 fromX, fromY, toX, toY);
6496 case IcsPlayingWhite:
6497 /* User is moving for White */
6498 if (!WhiteOnMove(currentMove)) {
6499 if (!appData.premove) {
6500 DisplayMoveError(_("It is Black's turn"));
6501 } else if (toX >= 0 && toY >= 0) {
6504 premoveFromX = fromX;
6505 premoveFromY = fromY;
6506 premovePromoChar = promoChar;
6508 if (appData.debugMode)
6509 fprintf(debugFP, "Got premove: fromX %d,"
6510 "fromY %d, toX %d, toY %d\n",
6511 fromX, fromY, toX, toY);
6521 /* EditPosition, empty square, or different color piece;
6522 click-click move is possible */
6523 if (toX == -2 || toY == -2) {
6524 boards[0][fromY][fromX] = EmptySquare;
6525 DrawPosition(FALSE, boards[currentMove]);
6527 } else if (toX >= 0 && toY >= 0) {
6528 boards[0][toY][toX] = boards[0][fromY][fromX];
6529 if(fromX == BOARD_LEFT-2) { // handle 'moves' out of holdings
6530 if(boards[0][fromY][0] != EmptySquare) {
6531 if(boards[0][fromY][1]) boards[0][fromY][1]--;
6532 if(boards[0][fromY][1] == 0) boards[0][fromY][0] = EmptySquare;
6535 if(fromX == BOARD_RGHT+1) {
6536 if(boards[0][fromY][BOARD_WIDTH-1] != EmptySquare) {
6537 if(boards[0][fromY][BOARD_WIDTH-2]) boards[0][fromY][BOARD_WIDTH-2]--;
6538 if(boards[0][fromY][BOARD_WIDTH-2] == 0) boards[0][fromY][BOARD_WIDTH-1] = EmptySquare;
6541 boards[0][fromY][fromX] = EmptySquare;
6542 DrawPosition(FALSE, boards[currentMove]);
6548 if(toX < 0 || toY < 0) return;
6549 pdown = boards[currentMove][fromY][fromX];
6550 pup = boards[currentMove][toY][toX];
6552 /* [HGM] If move started in holdings, it means a drop. Convert to standard form */
6553 if( (fromX == BOARD_LEFT-2 || fromX == BOARD_RGHT+1) && fromY != DROP_RANK ) {
6554 if( pup != EmptySquare ) return;
6555 moveType = WhiteOnMove(currentMove) ? WhiteDrop : BlackDrop;
6556 if(appData.debugMode) fprintf(debugFP, "Drop move %d, curr=%d, x=%d,y=%d, p=%d\n",
6557 moveType, currentMove, fromX, fromY, boards[currentMove][fromY][fromX]);
6558 // holdings might not be sent yet in ICS play; we have to figure out which piece belongs here
6559 if(fromX == 0) fromY = BOARD_HEIGHT-1 - fromY; // black holdings upside-down
6560 fromX = fromX ? WhitePawn : BlackPawn; // first piece type in selected holdings
6561 while(PieceToChar(fromX) == '.' || PieceToNumber(fromX) != fromY && fromX != (int) EmptySquare) fromX++;
6565 /* [HGM] always test for legality, to get promotion info */
6566 moveType = LegalityTest(boards[currentMove], PosFlags(currentMove),
6567 fromY, fromX, toY, toX, promoChar);
6569 if(fromY == DROP_RANK && fromX == EmptySquare && (gameMode == AnalyzeMode || gameMode == EditGame)) moveType = NormalMove;
6571 /* [HGM] but possibly ignore an IllegalMove result */
6572 if (appData.testLegality) {
6573 if (moveType == IllegalMove || moveType == ImpossibleMove) {
6574 DisplayMoveError(_("Illegal move"));
6579 FinishMove(moveType, fromX, fromY, toX, toY, promoChar);
6582 /* Common tail of UserMoveEvent and DropMenuEvent */
6584 FinishMove (ChessMove moveType, int fromX, int fromY, int toX, int toY, int promoChar)
6588 if((gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand) && promoChar != NULLCHAR) {
6589 // [HGM] superchess: suppress promotions to non-available piece (but P always allowed)
6590 int k = PieceToNumber(CharToPiece(ToUpper(promoChar)));
6591 if(WhiteOnMove(currentMove)) {
6592 if(!boards[currentMove][k][BOARD_WIDTH-2]) return 0;
6594 if(!boards[currentMove][BOARD_HEIGHT-1-k][1]) return 0;
6598 /* [HGM] <popupFix> kludge to avoid having to know the exact promotion
6599 move type in caller when we know the move is a legal promotion */
6600 if(moveType == NormalMove && promoChar)
6601 moveType = WhiteOnMove(currentMove) ? WhitePromotion : BlackPromotion;
6603 /* [HGM] <popupFix> The following if has been moved here from
6604 UserMoveEvent(). Because it seemed to belong here (why not allow
6605 piece drops in training games?), and because it can only be
6606 performed after it is known to what we promote. */
6607 if (gameMode == Training) {
6608 /* compare the move played on the board to the next move in the
6609 * game. If they match, display the move and the opponent's response.
6610 * If they don't match, display an error message.
6614 CopyBoard(testBoard, boards[currentMove]);
6615 ApplyMove(fromX, fromY, toX, toY, promoChar, testBoard);
6617 if (CompareBoards(testBoard, boards[currentMove+1])) {
6618 ForwardInner(currentMove+1);
6620 /* Autoplay the opponent's response.
6621 * if appData.animate was TRUE when Training mode was entered,
6622 * the response will be animated.
6624 saveAnimate = appData.animate;
6625 appData.animate = animateTraining;
6626 ForwardInner(currentMove+1);
6627 appData.animate = saveAnimate;
6629 /* check for the end of the game */
6630 if (currentMove >= forwardMostMove) {
6631 gameMode = PlayFromGameFile;
6633 SetTrainingModeOff();
6634 DisplayInformation(_("End of game"));
6637 DisplayError(_("Incorrect move"), 0);
6642 /* Ok, now we know that the move is good, so we can kill
6643 the previous line in Analysis Mode */
6644 if ((gameMode == AnalyzeMode || gameMode == EditGame || gameMode == PlayFromGameFile && appData.variations && shiftKey)
6645 && currentMove < forwardMostMove) {
6646 if(appData.variations && shiftKey) PushTail(currentMove, forwardMostMove); // [HGM] vari: save tail of game
6647 else forwardMostMove = currentMove;
6650 /* If we need the chess program but it's dead, restart it */
6651 ResurrectChessProgram();
6653 /* A user move restarts a paused game*/
6657 thinkOutput[0] = NULLCHAR;
6659 MakeMove(fromX, fromY, toX, toY, promoChar); /*updates forwardMostMove*/
6661 if(Adjudicate(NULL)) { // [HGM] adjudicate: take care of automatic game end
6662 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6666 if (gameMode == BeginningOfGame) {
6667 if (appData.noChessProgram) {
6668 gameMode = EditGame;
6672 gameMode = MachinePlaysBlack;
6675 snprintf(buf, MSG_SIZ, "%s %s %s", gameInfo.white, _("vs."), gameInfo.black);
6677 if (first.sendName) {
6678 snprintf(buf, MSG_SIZ,"name %s\n", gameInfo.white);
6679 SendToProgram(buf, &first);
6686 /* Relay move to ICS or chess engine */
6687 if (appData.icsActive) {
6688 if (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
6689 gameMode == IcsExamining) {
6690 if(userOfferedDraw && (signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
6691 SendToICS(ics_prefix); // [HGM] drawclaim: send caim and move on one line for FICS
6693 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
6695 // also send plain move, in case ICS does not understand atomic claims
6696 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
6700 if (first.sendTime && (gameMode == BeginningOfGame ||
6701 gameMode == MachinePlaysWhite ||
6702 gameMode == MachinePlaysBlack)) {
6703 SendTimeRemaining(&first, gameMode != MachinePlaysBlack);
6705 if (gameMode != EditGame && gameMode != PlayFromGameFile) {
6706 // [HGM] book: if program might be playing, let it use book
6707 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, FALSE);
6708 first.maybeThinking = TRUE;
6709 } else if(fromY == DROP_RANK && fromX == EmptySquare) {
6710 if(!first.useSetboard) SendToProgram("undo\n", &first); // kludge to change stm in engines that do not support setboard
6711 SendBoard(&first, currentMove+1);
6712 } else SendMoveToProgram(forwardMostMove-1, &first);
6713 if (currentMove == cmailOldMove + 1) {
6714 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
6718 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6722 if(appData.testLegality)
6723 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
6729 if (WhiteOnMove(currentMove)) {
6730 GameEnds(BlackWins, "Black mates", GE_PLAYER);
6732 GameEnds(WhiteWins, "White mates", GE_PLAYER);
6736 GameEnds(GameIsDrawn, "Stalemate", GE_PLAYER);
6741 case MachinePlaysBlack:
6742 case MachinePlaysWhite:
6743 /* disable certain menu options while machine is thinking */
6744 SetMachineThinkingEnables();
6751 userOfferedDraw = FALSE; // [HGM] drawclaim: after move made, and tested for claimable draw
6752 promoDefaultAltered = FALSE; // [HGM] fall back on default choice
6754 if(bookHit) { // [HGM] book: simulate book reply
6755 static char bookMove[MSG_SIZ]; // a bit generous?
6757 programStats.nodes = programStats.depth = programStats.time =
6758 programStats.score = programStats.got_only_move = 0;
6759 sprintf(programStats.movelist, "%s (xbook)", bookHit);
6761 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
6762 strcat(bookMove, bookHit);
6763 HandleMachineMove(bookMove, &first);
6769 Mark (Board board, int flags, ChessMove kind, int rf, int ff, int rt, int ft, VOIDSTAR closure)
6771 typedef char Markers[BOARD_RANKS][BOARD_FILES];
6772 Markers *m = (Markers *) closure;
6773 if(rf == fromY && ff == fromX)
6774 (*m)[rt][ft] = 1 + (board[rt][ft] != EmptySquare
6775 || kind == WhiteCapturesEnPassant
6776 || kind == BlackCapturesEnPassant);
6777 else if(flags & F_MANDATORY_CAPTURE && board[rt][ft] != EmptySquare) (*m)[rt][ft] = 3;
6781 MarkTargetSquares (int clear)
6784 if(!appData.markers || !appData.highlightDragging || appData.icsActive && gameInfo.variant < VariantShogi ||
6785 !appData.testLegality || gameMode == EditPosition) return;
6787 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) marker[y][x] = 0;
6790 GenLegal(boards[currentMove], PosFlags(currentMove), Mark, (void*) marker, EmptySquare);
6791 if(PosFlags(0) & F_MANDATORY_CAPTURE) {
6792 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) if(marker[y][x]>1) capt++;
6794 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) if(marker[y][x] == 1) marker[y][x] = 0;
6797 DrawPosition(TRUE, NULL);
6801 Explode (Board board, int fromX, int fromY, int toX, int toY)
6803 if(gameInfo.variant == VariantAtomic &&
6804 (board[toY][toX] != EmptySquare || // capture?
6805 toX != fromX && (board[fromY][fromX] == WhitePawn || // e.p. ?
6806 board[fromY][fromX] == BlackPawn )
6808 AnimateAtomicCapture(board, fromX, fromY, toX, toY);
6814 ChessSquare gatingPiece = EmptySquare; // exported to front-end, for dragging
6817 CanPromote (ChessSquare piece, int y)
6819 if(gameMode == EditPosition) return FALSE; // no promotions when editing position
6820 // some variants have fixed promotion piece, no promotion at all, or another selection mechanism
6821 if(gameInfo.variant == VariantShogi || gameInfo.variant == VariantXiangqi ||
6822 gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat ||
6823 gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier ||
6824 gameInfo.variant == VariantMakruk) return FALSE;
6825 return (piece == BlackPawn && y == 1 ||
6826 piece == WhitePawn && y == BOARD_HEIGHT-2 ||
6827 piece == BlackLance && y == 1 ||
6828 piece == WhiteLance && y == BOARD_HEIGHT-2 );
6832 LeftClick (ClickType clickType, int xPix, int yPix)
6835 Boolean saveAnimate;
6836 static int second = 0, promotionChoice = 0, clearFlag = 0;
6837 char promoChoice = NULLCHAR;
6840 if(SeekGraphClick(clickType, xPix, yPix, 0)) return;
6842 if (clickType == Press) ErrorPopDown();
6844 x = EventToSquare(xPix, BOARD_WIDTH);
6845 y = EventToSquare(yPix, BOARD_HEIGHT);
6846 if (!flipView && y >= 0) {
6847 y = BOARD_HEIGHT - 1 - y;
6849 if (flipView && x >= 0) {
6850 x = BOARD_WIDTH - 1 - x;
6853 if(promoSweep != EmptySquare) { // up-click during sweep-select of promo-piece
6854 defaultPromoChoice = promoSweep;
6855 promoSweep = EmptySquare; // terminate sweep
6856 promoDefaultAltered = TRUE;
6857 if(!selectFlag && (x != toX || y != toY)) x = fromX, y = fromY; // and fake up-click on same square if we were still selecting
6860 if(promotionChoice) { // we are waiting for a click to indicate promotion piece
6861 if(clickType == Release) return; // ignore upclick of click-click destination
6862 promotionChoice = FALSE; // only one chance: if click not OK it is interpreted as cancel
6863 if(appData.debugMode) fprintf(debugFP, "promotion click, x=%d, y=%d\n", x, y);
6864 if(gameInfo.holdingsWidth &&
6865 (WhiteOnMove(currentMove)
6866 ? x == BOARD_WIDTH-1 && y < gameInfo.holdingsSize && y >= 0
6867 : x == 0 && y >= BOARD_HEIGHT - gameInfo.holdingsSize && y < BOARD_HEIGHT) ) {
6868 // click in right holdings, for determining promotion piece
6869 ChessSquare p = boards[currentMove][y][x];
6870 if(appData.debugMode) fprintf(debugFP, "square contains %d\n", (int)p);
6871 if(p == WhitePawn || p == BlackPawn) p = EmptySquare; // [HGM] Pawns could be valid as deferral
6872 if(p != EmptySquare || gameInfo.variant == VariantGrand && toY != 0 && toY != BOARD_HEIGHT-1) { // [HGM] grand: empty square means defer
6873 FinishMove(NormalMove, fromX, fromY, toX, toY, p==EmptySquare ? NULLCHAR : ToLower(PieceToChar(p)));
6878 DrawPosition(FALSE, boards[currentMove]);
6882 /* [HGM] holdings: next 5 lines: ignore all clicks between board and holdings */
6883 if(clickType == Press
6884 && ( x == BOARD_LEFT-1 || x == BOARD_RGHT
6885 || x == BOARD_LEFT-2 && y < BOARD_HEIGHT-gameInfo.holdingsSize
6886 || x == BOARD_RGHT+1 && y >= gameInfo.holdingsSize) )
6889 if(gotPremove && x == premoveFromX && y == premoveFromY && clickType == Release) {
6890 // could be static click on premove from-square: abort premove
6892 ClearPremoveHighlights();
6895 if(clickType == Press && fromX == x && fromY == y && promoDefaultAltered)
6896 fromX = fromY = -1; // second click on piece after altering default promo piece treated as first click
6898 if(!promoDefaultAltered) { // determine default promotion piece, based on the side the user is moving for
6899 int side = (gameMode == IcsPlayingWhite || gameMode == MachinePlaysBlack ||
6900 gameMode != MachinePlaysWhite && gameMode != IcsPlayingBlack && WhiteOnMove(currentMove));
6901 defaultPromoChoice = DefaultPromoChoice(side);
6904 autoQueen = appData.alwaysPromoteToQueen;
6908 gatingPiece = EmptySquare;
6909 if (clickType != Press) {
6910 if(dragging) { // [HGM] from-square must have been reset due to game end since last press
6911 DragPieceEnd(xPix, yPix); dragging = 0;
6912 DrawPosition(FALSE, NULL);
6916 fromX = x; fromY = y; toX = toY = -1;
6917 if(!appData.oneClick || !OnlyMove(&x, &y, FALSE) ||
6918 // even if only move, we treat as normal when this would trigger a promotion popup, to allow sweep selection
6919 appData.sweepSelect && CanPromote(boards[currentMove][fromY][fromX], fromY) && originalY != y) {
6921 if (OKToStartUserMove(fromX, fromY)) {
6923 MarkTargetSquares(0);
6924 DragPieceBegin(xPix, yPix, FALSE); dragging = 1;
6925 if(appData.sweepSelect && CanPromote(piece = boards[currentMove][fromY][fromX], fromY)) {
6926 promoSweep = defaultPromoChoice;
6927 selectFlag = 0; lastX = xPix; lastY = yPix;
6928 Sweep(0); // Pawn that is going to promote: preview promotion piece
6929 DisplayMessage("", _("Pull pawn backwards to under-promote"));
6931 if (appData.highlightDragging) {
6932 SetHighlights(fromX, fromY, -1, -1);
6934 } else fromX = fromY = -1;
6940 if (clickType == Press && gameMode != EditPosition) {
6945 // ignore off-board to clicks
6946 if(y < 0 || x < 0) return;
6948 /* Check if clicking again on the same color piece */
6949 fromP = boards[currentMove][fromY][fromX];
6950 toP = boards[currentMove][y][x];
6951 frc = gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom || gameInfo.variant == VariantSChess;
6952 if ((WhitePawn <= fromP && fromP <= WhiteKing &&
6953 WhitePawn <= toP && toP <= WhiteKing &&
6954 !(fromP == WhiteKing && toP == WhiteRook && frc) &&
6955 !(fromP == WhiteRook && toP == WhiteKing && frc)) ||
6956 (BlackPawn <= fromP && fromP <= BlackKing &&
6957 BlackPawn <= toP && toP <= BlackKing &&
6958 !(fromP == BlackRook && toP == BlackKing && frc) && // allow also RxK as FRC castling
6959 !(fromP == BlackKing && toP == BlackRook && frc))) {
6960 /* Clicked again on same color piece -- changed his mind */
6961 second = (x == fromX && y == fromY);
6962 promoDefaultAltered = FALSE;
6963 MarkTargetSquares(1);
6964 if(!second || appData.oneClick && !OnlyMove(&x, &y, TRUE)) {
6965 if (appData.highlightDragging) {
6966 SetHighlights(x, y, -1, -1);
6970 if (OKToStartUserMove(x, y)) {
6971 if(gameInfo.variant == VariantSChess && // S-Chess: back-rank piece selected after holdings means gating
6972 (fromX == BOARD_LEFT-2 || fromX == BOARD_RGHT+1) &&
6973 y == (toP < BlackPawn ? 0 : BOARD_HEIGHT-1))
6974 gatingPiece = boards[currentMove][fromY][fromX];
6975 else gatingPiece = EmptySquare;
6977 fromY = y; dragging = 1;
6978 MarkTargetSquares(0);
6979 DragPieceBegin(xPix, yPix, FALSE);
6980 if(appData.sweepSelect && CanPromote(piece = boards[currentMove][y][x], y)) {
6981 promoSweep = defaultPromoChoice;
6982 selectFlag = 0; lastX = xPix; lastY = yPix;
6983 Sweep(0); // Pawn that is going to promote: preview promotion piece
6987 if(x == fromX && y == fromY) return; // if OnlyMove altered (x,y) we go on
6990 // ignore clicks on holdings
6991 if(x < BOARD_LEFT || x >= BOARD_RGHT) return;
6994 if (clickType == Release && x == fromX && y == fromY) {
6995 DragPieceEnd(xPix, yPix); dragging = 0;
6997 // a deferred attempt to click-click move an empty square on top of a piece
6998 boards[currentMove][y][x] = EmptySquare;
7000 DrawPosition(FALSE, boards[currentMove]);
7001 fromX = fromY = -1; clearFlag = 0;
7004 if (appData.animateDragging) {
7005 /* Undo animation damage if any */
7006 DrawPosition(FALSE, NULL);
7009 /* Second up/down in same square; just abort move */
7012 gatingPiece = EmptySquare;
7015 ClearPremoveHighlights();
7017 /* First upclick in same square; start click-click mode */
7018 SetHighlights(x, y, -1, -1);
7025 /* we now have a different from- and (possibly off-board) to-square */
7026 /* Completed move */
7029 saveAnimate = appData.animate;
7030 MarkTargetSquares(1);
7031 if (clickType == Press) {
7032 if(gameMode == EditPosition && boards[currentMove][fromY][fromX] == EmptySquare) {
7033 // must be Edit Position mode with empty-square selected
7034 fromX = x; fromY = y; DragPieceBegin(xPix, yPix, FALSE); dragging = 1; // consider this a new attempt to drag
7035 if(x >= BOARD_LEFT && x < BOARD_RGHT) clearFlag = 1; // and defer click-click move of empty-square to up-click
7038 if(appData.sweepSelect && HasPromotionChoice(fromX, fromY, toX, toY, &promoChoice, FALSE)) {
7039 ChessSquare piece = boards[currentMove][fromY][fromX];
7040 DragPieceBegin(xPix, yPix, TRUE); dragging = 1;
7041 promoSweep = defaultPromoChoice;
7042 if(PieceToChar(PROMOTED piece) == '+') promoSweep = PROMOTED piece;
7043 selectFlag = 0; lastX = xPix; lastY = yPix;
7044 Sweep(0); // Pawn that is going to promote: preview promotion piece
7045 DisplayMessage("", _("Pull pawn backwards to under-promote"));
7046 DrawPosition(FALSE, boards[currentMove]);
7049 /* Finish clickclick move */
7050 if (appData.animate || appData.highlightLastMove) {
7051 SetHighlights(fromX, fromY, toX, toY);
7056 /* Finish drag move */
7057 if (appData.highlightLastMove) {
7058 SetHighlights(fromX, fromY, toX, toY);
7062 DragPieceEnd(xPix, yPix); dragging = 0;
7063 /* Don't animate move and drag both */
7064 appData.animate = FALSE;
7067 // moves into holding are invalid for now (except in EditPosition, adapting to-square)
7068 if(x >= 0 && x < BOARD_LEFT || x >= BOARD_RGHT) {
7069 ChessSquare piece = boards[currentMove][fromY][fromX];
7070 if(gameMode == EditPosition && piece != EmptySquare &&
7071 fromX >= BOARD_LEFT && fromX < BOARD_RGHT) {
7074 if(x == BOARD_LEFT-2 && piece >= BlackPawn) {
7075 n = PieceToNumber(piece - (int)BlackPawn);
7076 if(n >= gameInfo.holdingsSize) { n = 0; piece = BlackPawn; }
7077 boards[currentMove][BOARD_HEIGHT-1 - n][0] = piece;
7078 boards[currentMove][BOARD_HEIGHT-1 - n][1]++;
7080 if(x == BOARD_RGHT+1 && piece < BlackPawn) {
7081 n = PieceToNumber(piece);
7082 if(n >= gameInfo.holdingsSize) { n = 0; piece = WhitePawn; }
7083 boards[currentMove][n][BOARD_WIDTH-1] = piece;
7084 boards[currentMove][n][BOARD_WIDTH-2]++;
7086 boards[currentMove][fromY][fromX] = EmptySquare;
7090 DrawPosition(TRUE, boards[currentMove]);
7094 // off-board moves should not be highlighted
7095 if(x < 0 || y < 0) ClearHighlights();
7097 if(gatingPiece != EmptySquare) promoChoice = ToLower(PieceToChar(gatingPiece));
7099 if (HasPromotionChoice(fromX, fromY, toX, toY, &promoChoice, appData.sweepSelect)) {
7100 SetHighlights(fromX, fromY, toX, toY);
7101 if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand) {
7102 // [HGM] super: promotion to captured piece selected from holdings
7103 ChessSquare p = boards[currentMove][fromY][fromX], q = boards[currentMove][toY][toX];
7104 promotionChoice = TRUE;
7105 // kludge follows to temporarily execute move on display, without promoting yet
7106 boards[currentMove][fromY][fromX] = EmptySquare; // move Pawn to 8th rank
7107 boards[currentMove][toY][toX] = p;
7108 DrawPosition(FALSE, boards[currentMove]);
7109 boards[currentMove][fromY][fromX] = p; // take back, but display stays
7110 boards[currentMove][toY][toX] = q;
7111 DisplayMessage("Click in holdings to choose piece", "");
7116 int oldMove = currentMove;
7117 UserMoveEvent(fromX, fromY, toX, toY, promoChoice);
7118 if (!appData.highlightLastMove || gotPremove) ClearHighlights();
7119 if (gotPremove) SetPremoveHighlights(fromX, fromY, toX, toY);
7120 if(saveAnimate && !appData.animate && currentMove != oldMove && // drag-move was performed
7121 Explode(boards[currentMove-1], fromX, fromY, toX, toY))
7122 DrawPosition(TRUE, boards[currentMove]);
7125 appData.animate = saveAnimate;
7126 if (appData.animate || appData.animateDragging) {
7127 /* Undo animation damage if needed */
7128 DrawPosition(FALSE, NULL);
7133 RightClick (ClickType action, int x, int y, int *fromX, int *fromY)
7134 { // front-end-free part taken out of PieceMenuPopup
7135 int whichMenu; int xSqr, ySqr;
7137 if(seekGraphUp) { // [HGM] seekgraph
7138 if(action == Press) SeekGraphClick(Press, x, y, 2); // 2 indicates right-click: no pop-down on miss
7139 if(action == Release) SeekGraphClick(Release, x, y, 2); // and no challenge on hit
7143 if((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)
7144 && !appData.zippyPlay && appData.bgObserve) { // [HGM] bughouse: show background game
7145 if(!partnerBoardValid) return -2; // suppress display of uninitialized boards
7146 if( appData.dualBoard) return -2; // [HGM] dual: is already displayed
7147 if(action == Press) {
7148 originalFlip = flipView;
7149 flipView = !flipView; // temporarily flip board to see game from partners perspective
7150 DrawPosition(TRUE, partnerBoard);
7151 DisplayMessage(partnerStatus, "");
7153 } else if(action == Release) {
7154 flipView = originalFlip;
7155 DrawPosition(TRUE, boards[currentMove]);
7161 xSqr = EventToSquare(x, BOARD_WIDTH);
7162 ySqr = EventToSquare(y, BOARD_HEIGHT);
7163 if (action == Release) {
7164 if(pieceSweep != EmptySquare) {
7165 EditPositionMenuEvent(pieceSweep, toX, toY);
7166 pieceSweep = EmptySquare;
7167 } else UnLoadPV(); // [HGM] pv
7169 if (action != Press) return -2; // return code to be ignored
7172 if(xSqr < BOARD_LEFT || xSqr >= BOARD_RGHT) return -1;
7174 if (xSqr == BOARD_LEFT-1 || xSqr == BOARD_RGHT) return -1;
7175 if (xSqr < 0 || ySqr < 0) return -1;
7176 if(appData.pieceMenu) { whichMenu = 0; break; } // edit-position menu
7177 pieceSweep = shiftKey ? BlackPawn : WhitePawn; // [HGM] sweep: prepare selecting piece by mouse sweep
7178 toX = xSqr; toY = ySqr; lastX = x, lastY = y;
7179 if(flipView) toX = BOARD_WIDTH - 1 - toX; else toY = BOARD_HEIGHT - 1 - toY;
7183 if(!appData.icsEngineAnalyze) return -1;
7184 case IcsPlayingWhite:
7185 case IcsPlayingBlack:
7186 if(!appData.zippyPlay) goto noZip;
7189 case MachinePlaysWhite:
7190 case MachinePlaysBlack:
7191 case TwoMachinesPlay: // [HGM] pv: use for showing PV
7192 if (!appData.dropMenu) {
7194 return 2; // flag front-end to grab mouse events
7196 if(gameMode == TwoMachinesPlay || gameMode == AnalyzeMode ||
7197 gameMode == AnalyzeFile || gameMode == IcsObserving) return -1;
7200 if (xSqr < 0 || ySqr < 0) return -1;
7201 if (!appData.dropMenu || appData.testLegality &&
7202 gameInfo.variant != VariantBughouse &&
7203 gameInfo.variant != VariantCrazyhouse) return -1;
7204 whichMenu = 1; // drop menu
7210 if (((*fromX = xSqr) < 0) ||
7211 ((*fromY = ySqr) < 0)) {
7212 *fromX = *fromY = -1;
7216 *fromX = BOARD_WIDTH - 1 - *fromX;
7218 *fromY = BOARD_HEIGHT - 1 - *fromY;
7224 SendProgramStatsToFrontend (ChessProgramState * cps, ChessProgramStats * cpstats)
7226 // char * hint = lastHint;
7227 FrontEndProgramStats stats;
7229 stats.which = cps == &first ? 0 : 1;
7230 stats.depth = cpstats->depth;
7231 stats.nodes = cpstats->nodes;
7232 stats.score = cpstats->score;
7233 stats.time = cpstats->time;
7234 stats.pv = cpstats->movelist;
7235 stats.hint = lastHint;
7236 stats.an_move_index = 0;
7237 stats.an_move_count = 0;
7239 if( gameMode == AnalyzeMode || gameMode == AnalyzeFile ) {
7240 stats.hint = cpstats->move_name;
7241 stats.an_move_index = cpstats->nr_moves - cpstats->moves_left;
7242 stats.an_move_count = cpstats->nr_moves;
7245 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
7247 SetProgramStats( &stats );
7251 ClearEngineOutputPane (int which)
7253 static FrontEndProgramStats dummyStats;
7254 dummyStats.which = which;
7255 dummyStats.pv = "#";
7256 SetProgramStats( &dummyStats );
7259 #define MAXPLAYERS 500
7262 TourneyStandings (int display)
7264 int i, w, b, color, wScore, bScore, dummy, nr=0, nPlayers=0;
7265 int score[MAXPLAYERS], ranking[MAXPLAYERS], points[MAXPLAYERS], games[MAXPLAYERS];
7266 char result, *p, *names[MAXPLAYERS];
7268 if(appData.tourneyType < 0 && !strchr(appData.results, '*'))
7269 return strdup(_("Swiss tourney finished")); // standings of Swiss yet TODO
7270 names[0] = p = strdup(appData.participants);
7271 while(p = strchr(p, '\n')) *p++ = NULLCHAR, names[++nPlayers] = p; // count participants
7273 for(i=0; i<nPlayers; i++) score[i] = games[i] = 0;
7275 while(result = appData.results[nr]) {
7276 color = Pairing(nr, nPlayers, &w, &b, &dummy);
7277 if(!(color ^ matchGame & 1)) { dummy = w; w = b; b = dummy; }
7278 wScore = bScore = 0;
7280 case '+': wScore = 2; break;
7281 case '-': bScore = 2; break;
7282 case '=': wScore = bScore = 1; break;
7284 case '*': return strdup("busy"); // tourney not finished
7292 if(appData.tourneyType > 0) nPlayers = appData.tourneyType; // in gauntlet, list only gauntlet engine(s)
7293 for(w=0; w<nPlayers; w++) {
7295 for(i=0; i<nPlayers; i++) if(score[i] > bScore) bScore = score[i], b = i;
7296 ranking[w] = b; points[w] = bScore; score[b] = -2;
7298 p = malloc(nPlayers*34+1);
7299 for(w=0; w<nPlayers && w<display; w++)
7300 sprintf(p+34*w, "%2d. %5.1f/%-3d %-19.19s\n", w+1, points[w]/2., games[ranking[w]], names[ranking[w]]);
7306 Count (Board board, int pCnt[], int *nW, int *nB, int *wStale, int *bStale, int *bishopColor)
7307 { // count all piece types
7309 *nB = *nW = *wStale = *bStale = *bishopColor = 0;
7310 for(p=WhitePawn; p<=EmptySquare; p++) pCnt[p] = 0;
7311 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
7314 if(p == WhitePawn && r == BOARD_HEIGHT-1) (*wStale)++; else
7315 if(p == BlackPawn && r == 0) (*bStale)++; // count last-Rank Pawns (XQ) separately
7316 if(p <= WhiteKing) (*nW)++; else if(p <= BlackKing) (*nB)++;
7317 if(p == WhiteBishop || p == WhiteFerz || p == WhiteAlfil ||
7318 p == BlackBishop || p == BlackFerz || p == BlackAlfil )
7319 *bishopColor |= 1 << ((f^r)&1); // track square color of color-bound pieces
7324 SufficientDefence (int pCnt[], int side, int nMine, int nHis)
7326 int myPawns = pCnt[WhitePawn+side]; // my total Pawn count;
7327 int majorDefense = pCnt[BlackRook-side] + pCnt[BlackCannon-side] + pCnt[BlackKnight-side];
7329 nMine -= pCnt[WhiteFerz+side] + pCnt[WhiteAlfil+side]; // discount defenders
7330 if(nMine - myPawns > 2) return FALSE; // no trivial draws with more than 1 major
7331 if(myPawns == 2 && nMine == 3) // KPP
7332 return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] >= 3;
7333 if(myPawns == 1 && nMine == 2) // KP
7334 return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] + pCnt[BlackPawn-side] >= 1;
7335 if(myPawns == 1 && nMine == 3 && pCnt[WhiteKnight+side]) // KHP
7336 return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side]*2 >= 5;
7337 if(myPawns) return FALSE;
7338 if(pCnt[WhiteRook+side])
7339 return pCnt[BlackRook-side] ||
7340 pCnt[BlackCannon-side] && (pCnt[BlackFerz-side] >= 2 || pCnt[BlackAlfil-side] >= 2) ||
7341 pCnt[BlackKnight-side] && pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] > 2 ||
7342 pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] >= 4;
7343 if(pCnt[WhiteCannon+side]) {
7344 if(pCnt[WhiteFerz+side] + myPawns == 0) return TRUE; // Cannon needs platform
7345 return majorDefense || pCnt[BlackAlfil-side] >= 2;
7347 if(pCnt[WhiteKnight+side])
7348 return majorDefense || pCnt[BlackFerz-side] >= 2 || pCnt[BlackAlfil-side] + pCnt[BlackPawn-side] >= 1;
7353 MatingPotential (int pCnt[], int side, int nMine, int nHis, int stale, int bisColor)
7355 VariantClass v = gameInfo.variant;
7357 if(v == VariantShogi || v == VariantCrazyhouse || v == VariantBughouse) return TRUE; // drop games always winnable
7358 if(v == VariantShatranj) return TRUE; // always winnable through baring
7359 if(v == VariantLosers || v == VariantSuicide || v == VariantGiveaway) return TRUE;
7360 if(v == Variant3Check || v == VariantAtomic) return nMine > 1; // can win through checking / exploding King
7362 if(v == VariantXiangqi) {
7363 int majors = 5*pCnt[BlackKnight-side] + 7*pCnt[BlackCannon-side] + 7*pCnt[BlackRook-side];
7365 nMine -= pCnt[WhiteFerz+side] + pCnt[WhiteAlfil+side] + stale; // discount defensive pieces and back-rank Pawns
7366 if(nMine + stale == 1) return (pCnt[BlackFerz-side] > 1 && pCnt[BlackKnight-side] > 0); // bare K can stalemate KHAA (!)
7367 if(nMine > 2) return TRUE; // if we don't have P, H or R, we must have CC
7368 if(nMine == 2 && pCnt[WhiteCannon+side] == 0) return TRUE; // We have at least one P, H or R
7369 // if we get here, we must have KC... or KP..., possibly with additional A, E or last-rank P
7370 if(stale) // we have at least one last-rank P plus perhaps C
7371 return majors // KPKX
7372 || pCnt[BlackFerz-side] && pCnt[BlackFerz-side] + pCnt[WhiteCannon+side] + stale > 2; // KPKAA, KPPKA and KCPKA
7374 return pCnt[WhiteFerz+side] // KCAK
7375 || pCnt[WhiteAlfil+side] && pCnt[BlackRook-side] + pCnt[BlackCannon-side] + pCnt[BlackFerz-side] // KCEKA, KCEKX (X!=H)
7376 || majors + (12*pCnt[BlackFerz-side] | 6*pCnt[BlackAlfil-side]) > 16; // KCKAA, KCKAX, KCKEEX, KCKEXX (XX!=HH), KCKXXX
7377 // TO DO: cases wih an unpromoted f-Pawn acting as platform for an opponent Cannon
7379 } else if(pCnt[WhiteKing] == 1 && pCnt[BlackKing] == 1) { // other variants with orthodox Kings
7380 int nBishops = pCnt[WhiteBishop+side] + pCnt[WhiteFerz+side];
7382 if(nMine == 1) return FALSE; // bare King
7383 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
7384 nMine += (nBishops > 0) - nBishops; // By now all Bishops (and Ferz) on like-colored squares, so count as one
7385 if(nMine > 2 && nMine != pCnt[WhiteAlfil+side] + 1) return TRUE; // At least two pieces, not all Alfils
7386 // by now we have King + 1 piece (or multiple Bishops on the same color)
7387 if(pCnt[WhiteKnight+side])
7388 return (pCnt[BlackKnight-side] + pCnt[BlackBishop-side] + pCnt[BlackMan-side] +
7389 pCnt[BlackWazir-side] + pCnt[BlackSilver-side] + bisColor // KNKN, KNKB, KNKF, KNKE, KNKW, KNKM, KNKS
7390 || nHis > 3); // be sure to cover suffocation mates in corner (e.g. KNKQCA)
7392 return (pCnt[BlackKnight-side]); // KBKN, KFKN
7393 if(pCnt[WhiteAlfil+side])
7394 return (nHis > 2); // Alfils can in general not reach a corner square, but there might be edge (suffocation) mates
7395 if(pCnt[WhiteWazir+side])
7396 return (pCnt[BlackKnight-side] + pCnt[BlackWazir-side] + pCnt[BlackAlfil-side]); // KWKN, KWKW, KWKE
7403 CompareWithRights (Board b1, Board b2)
7406 if(!CompareBoards(b1, b2)) return FALSE;
7407 if(b1[EP_STATUS] != b2[EP_STATUS]) return FALSE;
7408 /* compare castling rights */
7409 if( b1[CASTLING][2] != b2[CASTLING][2] && (b2[CASTLING][0] != NoRights || b2[CASTLING][1] != NoRights) )
7410 rights++; /* King lost rights, while rook still had them */
7411 if( b1[CASTLING][2] != NoRights ) { /* king has rights */
7412 if( b1[CASTLING][0] != b2[CASTLING][0] || b1[CASTLING][1] != b2[CASTLING][1] )
7413 rights++; /* but at least one rook lost them */
7415 if( b1[CASTLING][5] != b1[CASTLING][5] && (b2[CASTLING][3] != NoRights || b2[CASTLING][4] != NoRights) )
7417 if( b1[CASTLING][5] != NoRights ) {
7418 if( b1[CASTLING][3] != b2[CASTLING][3] || b1[CASTLING][4] != b2[CASTLING][4] )
7425 Adjudicate (ChessProgramState *cps)
7426 { // [HGM] some adjudications useful with buggy engines
7427 // [HGM] adjudicate: made into separate routine, which now can be called after every move
7428 // In any case it determnes if the game is a claimable draw (filling in EP_STATUS).
7429 // Actually ending the game is now based on the additional internal condition canAdjudicate.
7430 // Only when the game is ended, and the opponent is a computer, this opponent gets the move relayed.
7431 int k, count = 0; static int bare = 1;
7432 ChessProgramState *engineOpponent = (gameMode == TwoMachinesPlay ? cps->other : (cps ? NULL : &first));
7433 Boolean canAdjudicate = !appData.icsActive;
7435 // most tests only when we understand the game, i.e. legality-checking on
7436 if( appData.testLegality )
7437 { /* [HGM] Some more adjudications for obstinate engines */
7438 int nrW, nrB, bishopColor, staleW, staleB, nr[EmptySquare+1], i;
7439 static int moveCount = 6;
7441 char *reason = NULL;
7443 /* Count what is on board. */
7444 Count(boards[forwardMostMove], nr, &nrW, &nrB, &staleW, &staleB, &bishopColor);
7446 /* Some material-based adjudications that have to be made before stalemate test */
7447 if(gameInfo.variant == VariantAtomic && nr[WhiteKing] + nr[BlackKing] < 2) {
7448 // [HGM] atomic: stm must have lost his King on previous move, as destroying own K is illegal
7449 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE; // make claimable as if stm is checkmated
7450 if(canAdjudicate && appData.checkMates) {
7452 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets move
7453 GameEnds( WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins,
7454 "Xboard adjudication: King destroyed", GE_XBOARD );
7459 /* Bare King in Shatranj (loses) or Losers (wins) */
7460 if( nrW == 1 || nrB == 1) {
7461 if( gameInfo.variant == VariantLosers) { // [HGM] losers: bare King wins (stm must have it first)
7462 boards[forwardMostMove][EP_STATUS] = EP_WINS; // mark as win, so it becomes claimable
7463 if(canAdjudicate && appData.checkMates) {
7465 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets to see move
7466 GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
7467 "Xboard adjudication: Bare king", GE_XBOARD );
7471 if( gameInfo.variant == VariantShatranj && --bare < 0)
7473 boards[forwardMostMove][EP_STATUS] = EP_WINS; // make claimable as win for stm
7474 if(canAdjudicate && appData.checkMates) {
7475 /* but only adjudicate if adjudication enabled */
7477 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets move
7478 GameEnds( nrW > 1 ? WhiteWins : nrB > 1 ? BlackWins : GameIsDrawn,
7479 "Xboard adjudication: Bare king", GE_XBOARD );
7486 // don't wait for engine to announce game end if we can judge ourselves
7487 switch (MateTest(boards[forwardMostMove], PosFlags(forwardMostMove)) ) {
7489 if(gameInfo.variant == Variant3Check) { // [HGM] 3check: when in check, test if 3rd time
7490 int i, checkCnt = 0; // (should really be done by making nr of checks part of game state)
7491 for(i=forwardMostMove-2; i>=backwardMostMove; i-=2) {
7492 if(MateTest(boards[i], PosFlags(i)) == MT_CHECK)
7495 reason = "Xboard adjudication: 3rd check";
7496 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE;
7506 reason = "Xboard adjudication: Stalemate";
7507 if((signed char)boards[forwardMostMove][EP_STATUS] != EP_CHECKMATE) { // [HGM] don't touch win through baring or K-capt
7508 boards[forwardMostMove][EP_STATUS] = EP_STALEMATE; // default result for stalemate is draw
7509 if(gameInfo.variant == VariantLosers || gameInfo.variant == VariantGiveaway) // [HGM] losers:
7510 boards[forwardMostMove][EP_STATUS] = EP_WINS; // in these variants stalemated is always a win
7511 else if(gameInfo.variant == VariantSuicide) // in suicide it depends
7512 boards[forwardMostMove][EP_STATUS] = nrW == nrB ? EP_STALEMATE :
7513 ((nrW < nrB) != WhiteOnMove(forwardMostMove) ?
7514 EP_CHECKMATE : EP_WINS);
7515 else if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantXiangqi)
7516 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE; // and in these variants being stalemated loses
7520 reason = "Xboard adjudication: Checkmate";
7521 boards[forwardMostMove][EP_STATUS] = (gameInfo.variant == VariantLosers ? EP_WINS : EP_CHECKMATE);
7525 switch(i = (signed char)boards[forwardMostMove][EP_STATUS]) {
7527 result = GameIsDrawn; break;
7529 result = WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins; break;
7531 result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins; break;
7535 if(canAdjudicate && appData.checkMates && result) { // [HGM] mates: adjudicate finished games if requested
7537 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7538 GameEnds( result, reason, GE_XBOARD );
7542 /* Next absolutely insufficient mating material. */
7543 if(!MatingPotential(nr, WhitePawn, nrW, nrB, staleW, bishopColor) &&
7544 !MatingPotential(nr, BlackPawn, nrB, nrW, staleB, bishopColor))
7545 { /* includes KBK, KNK, KK of KBKB with like Bishops */
7547 /* always flag draws, for judging claims */
7548 boards[forwardMostMove][EP_STATUS] = EP_INSUF_DRAW;
7550 if(canAdjudicate && appData.materialDraws) {
7551 /* but only adjudicate them if adjudication enabled */
7552 if(engineOpponent) {
7553 SendToProgram("force\n", engineOpponent); // suppress reply
7554 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see last move */
7556 GameEnds( GameIsDrawn, "Xboard adjudication: Insufficient mating material", GE_XBOARD );
7561 /* Then some trivial draws (only adjudicate, cannot be claimed) */
7562 if(gameInfo.variant == VariantXiangqi ?
7563 SufficientDefence(nr, WhitePawn, nrW, nrB) && SufficientDefence(nr, BlackPawn, nrB, nrW)
7565 ( nr[WhiteRook] == 1 && nr[BlackRook] == 1 /* KRKR */
7566 || nr[WhiteQueen] && nr[BlackQueen]==1 /* KQKQ */
7567 || nr[WhiteKnight]==2 || nr[BlackKnight]==2 /* KNNK */
7568 || nr[WhiteKnight]+nr[WhiteBishop] == 1 && nr[BlackKnight]+nr[BlackBishop] == 1 /* KBKN, KBKB, KNKN */
7570 if(--moveCount < 0 && appData.trivialDraws && canAdjudicate)
7571 { /* if the first 3 moves do not show a tactical win, declare draw */
7572 if(engineOpponent) {
7573 SendToProgram("force\n", engineOpponent); // suppress reply
7574 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7576 GameEnds( GameIsDrawn, "Xboard adjudication: Trivial draw", GE_XBOARD );
7579 } else moveCount = 6;
7582 // Repetition draws and 50-move rule can be applied independently of legality testing
7584 /* Check for rep-draws */
7586 for(k = forwardMostMove-2;
7587 k>=backwardMostMove && k>=forwardMostMove-100 &&
7588 (signed char)boards[k][EP_STATUS] < EP_UNKNOWN &&
7589 (signed char)boards[k+2][EP_STATUS] <= EP_NONE && (signed char)boards[k+1][EP_STATUS] <= EP_NONE;
7592 if(CompareBoards(boards[k], boards[forwardMostMove])) {
7593 /* compare castling rights */
7594 if( boards[forwardMostMove][CASTLING][2] != boards[k][CASTLING][2] &&
7595 (boards[k][CASTLING][0] != NoRights || boards[k][CASTLING][1] != NoRights) )
7596 rights++; /* King lost rights, while rook still had them */
7597 if( boards[forwardMostMove][CASTLING][2] != NoRights ) { /* king has rights */
7598 if( boards[forwardMostMove][CASTLING][0] != boards[k][CASTLING][0] ||
7599 boards[forwardMostMove][CASTLING][1] != boards[k][CASTLING][1] )
7600 rights++; /* but at least one rook lost them */
7602 if( boards[forwardMostMove][CASTLING][5] != boards[k][CASTLING][5] &&
7603 (boards[k][CASTLING][3] != NoRights || boards[k][CASTLING][4] != NoRights) )
7605 if( boards[forwardMostMove][CASTLING][5] != NoRights ) {
7606 if( boards[forwardMostMove][CASTLING][3] != boards[k][CASTLING][3] ||
7607 boards[forwardMostMove][CASTLING][4] != boards[k][CASTLING][4] )
7610 if( rights == 0 && ++count > appData.drawRepeats-2 && canAdjudicate
7611 && appData.drawRepeats > 1) {
7612 /* adjudicate after user-specified nr of repeats */
7613 int result = GameIsDrawn;
7614 char *details = "XBoard adjudication: repetition draw";
7615 if(gameInfo.variant == VariantXiangqi && appData.testLegality) {
7616 // [HGM] xiangqi: check for forbidden perpetuals
7617 int m, ourPerpetual = 1, hisPerpetual = 1;
7618 for(m=forwardMostMove; m>k; m-=2) {
7619 if(MateTest(boards[m], PosFlags(m)) != MT_CHECK)
7620 ourPerpetual = 0; // the current mover did not always check
7621 if(MateTest(boards[m-1], PosFlags(m-1)) != MT_CHECK)
7622 hisPerpetual = 0; // the opponent did not always check
7624 if(appData.debugMode) fprintf(debugFP, "XQ perpetual test, our=%d, his=%d\n",
7625 ourPerpetual, hisPerpetual);
7626 if(ourPerpetual && !hisPerpetual) { // we are actively checking him: forfeit
7627 result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
7628 details = "Xboard adjudication: perpetual checking";
7630 if(hisPerpetual && !ourPerpetual) { // he is checking us, but did not repeat yet
7631 break; // (or we would have caught him before). Abort repetition-checking loop.
7633 // Now check for perpetual chases
7634 if(!ourPerpetual && !hisPerpetual) { // no perpetual check, test for chase
7635 hisPerpetual = PerpetualChase(k, forwardMostMove);
7636 ourPerpetual = PerpetualChase(k+1, forwardMostMove);
7637 if(ourPerpetual && !hisPerpetual) { // we are actively chasing him: forfeit
7638 static char resdet[MSG_SIZ];
7639 result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
7641 snprintf(resdet, MSG_SIZ, "Xboard adjudication: perpetual chasing of %c%c", ourPerpetual>>8, ourPerpetual&255);
7643 if(hisPerpetual && !ourPerpetual) // he is chasing us, but did not repeat yet
7644 break; // Abort repetition-checking loop.
7646 // if neither of us is checking or chasing all the time, or both are, it is draw
7648 if(engineOpponent) {
7649 SendToProgram("force\n", engineOpponent); // suppress reply
7650 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7652 GameEnds( result, details, GE_XBOARD );
7655 if( rights == 0 && count > 1 ) /* occurred 2 or more times before */
7656 boards[forwardMostMove][EP_STATUS] = EP_REP_DRAW;
7660 /* Now we test for 50-move draws. Determine ply count */
7661 count = forwardMostMove;
7662 /* look for last irreversble move */
7663 while( (signed char)boards[count][EP_STATUS] <= EP_NONE && count > backwardMostMove )
7665 /* if we hit starting position, add initial plies */
7666 if( count == backwardMostMove )
7667 count -= initialRulePlies;
7668 count = forwardMostMove - count;
7669 if(gameInfo.variant == VariantXiangqi && ( count >= 100 || count >= 2*appData.ruleMoves ) ) {
7670 // adjust reversible move counter for checks in Xiangqi
7671 int i = forwardMostMove - count, inCheck = 0, lastCheck;
7672 if(i < backwardMostMove) i = backwardMostMove;
7673 while(i <= forwardMostMove) {
7674 lastCheck = inCheck; // check evasion does not count
7675 inCheck = (MateTest(boards[i], PosFlags(i)) == MT_CHECK);
7676 if(inCheck || lastCheck) count--; // check does not count
7681 boards[forwardMostMove][EP_STATUS] = EP_RULE_DRAW;
7682 /* this is used to judge if draw claims are legal */
7683 if(canAdjudicate && appData.ruleMoves > 0 && count >= 2*appData.ruleMoves) {
7684 if(engineOpponent) {
7685 SendToProgram("force\n", engineOpponent); // suppress reply
7686 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7688 GameEnds( GameIsDrawn, "Xboard adjudication: 50-move rule", GE_XBOARD );
7692 /* if draw offer is pending, treat it as a draw claim
7693 * when draw condition present, to allow engines a way to
7694 * claim draws before making their move to avoid a race
7695 * condition occurring after their move
7697 if((gameMode == TwoMachinesPlay ? second.offeredDraw : userOfferedDraw) || first.offeredDraw ) {
7699 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_RULE_DRAW)
7700 p = "Draw claim: 50-move rule";
7701 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_REP_DRAW)
7702 p = "Draw claim: 3-fold repetition";
7703 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_INSUF_DRAW)
7704 p = "Draw claim: insufficient mating material";
7705 if( p != NULL && canAdjudicate) {
7706 if(engineOpponent) {
7707 SendToProgram("force\n", engineOpponent); // suppress reply
7708 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7710 GameEnds( GameIsDrawn, p, GE_XBOARD );
7715 if( canAdjudicate && appData.adjudicateDrawMoves > 0 && forwardMostMove > (2*appData.adjudicateDrawMoves) ) {
7716 if(engineOpponent) {
7717 SendToProgram("force\n", engineOpponent); // suppress reply
7718 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7720 GameEnds( GameIsDrawn, "Xboard adjudication: long game", GE_XBOARD );
7727 SendMoveToBookUser (int moveNr, ChessProgramState *cps, int initial)
7728 { // [HGM] book: this routine intercepts moves to simulate book replies
7729 char *bookHit = NULL;
7731 //first determine if the incoming move brings opponent into his book
7732 if(appData.usePolyglotBook && (cps == &first ? !appData.firstHasOwnBookUCI : !appData.secondHasOwnBookUCI))
7733 bookHit = ProbeBook(moveNr+1, appData.polyglotBook); // returns move
7734 if(appData.debugMode) fprintf(debugFP, "book hit = %s\n", bookHit ? bookHit : "(NULL)");
7735 if(bookHit != NULL && !cps->bookSuspend) {
7736 // make sure opponent is not going to reply after receiving move to book position
7737 SendToProgram("force\n", cps);
7738 cps->bookSuspend = TRUE; // flag indicating it has to be restarted
7740 if(!initial) SendMoveToProgram(moveNr, cps); // with hit on initial position there is no move
7741 // now arrange restart after book miss
7743 // after a book hit we never send 'go', and the code after the call to this routine
7744 // has '&& !bookHit' added to suppress potential sending there (based on 'firstMove').
7745 char buf[MSG_SIZ], *move = bookHit;
7747 int fromX, fromY, toX, toY;
7751 if (ParseOneMove(bookHit, forwardMostMove, &moveType,
7752 &fromX, &fromY, &toX, &toY, &promoChar)) {
7753 (void) CoordsToAlgebraic(boards[forwardMostMove],
7754 PosFlags(forwardMostMove),
7755 fromY, fromX, toY, toX, promoChar, move);
7757 if(appData.debugMode) fprintf(debugFP, "Book move could not be parsed\n");
7761 snprintf(buf, MSG_SIZ, "%s%s\n", (cps->useUsermove ? "usermove " : ""), move); // force book move into program supposed to play it
7762 SendToProgram(buf, cps);
7763 if(!initial) firstMove = FALSE; // normally we would clear the firstMove condition after return & sending 'go'
7764 } else if(initial) { // 'go' was needed irrespective of firstMove, and it has to be done in this routine
7765 SendToProgram("go\n", cps);
7766 cps->bookSuspend = FALSE; // after a 'go' we are never suspended
7767 } else { // 'go' might be sent based on 'firstMove' after this routine returns
7768 if(cps->bookSuspend && !firstMove) // 'go' needed, and it will not be done after we return
7769 SendToProgram("go\n", cps);
7770 cps->bookSuspend = FALSE; // anyhow, we will not be suspended after a miss
7772 return bookHit; // notify caller of hit, so it can take action to send move to opponent
7776 ChessProgramState *savedState;
7778 DeferredBookMove (void)
7780 if(savedState->lastPing != savedState->lastPong)
7781 ScheduleDelayedEvent(DeferredBookMove, 10);
7783 HandleMachineMove(savedMessage, savedState);
7786 static int savedWhitePlayer, savedBlackPlayer, pairingReceived;
7789 HandleMachineMove (char *message, ChessProgramState *cps)
7791 char machineMove[MSG_SIZ], buf1[MSG_SIZ*10], buf2[MSG_SIZ];
7792 char realname[MSG_SIZ];
7793 int fromX, fromY, toX, toY;
7800 if(cps == &pairing && sscanf(message, "%d-%d", &savedWhitePlayer, &savedBlackPlayer) == 2) {
7801 // [HGM] pairing: Mega-hack! Pairing engine also uses this routine (so it could give other WB commands).
7802 if(savedWhitePlayer == 0 || savedBlackPlayer == 0) {
7803 DisplayError(_("Invalid pairing from pairing engine"), 0);
7806 pairingReceived = 1;
7808 return; // Skim the pairing messages here.
7813 FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book hit
7815 * Kludge to ignore BEL characters
7817 while (*message == '\007') message++;
7820 * [HGM] engine debug message: ignore lines starting with '#' character
7822 if(cps->debug && *message == '#') return;
7825 * Look for book output
7827 if (cps == &first && bookRequested) {
7828 if (message[0] == '\t' || message[0] == ' ') {
7829 /* Part of the book output is here; append it */
7830 strcat(bookOutput, message);
7831 strcat(bookOutput, " \n");
7833 } else if (bookOutput[0] != NULLCHAR) {
7834 /* All of book output has arrived; display it */
7835 char *p = bookOutput;
7836 while (*p != NULLCHAR) {
7837 if (*p == '\t') *p = ' ';
7840 DisplayInformation(bookOutput);
7841 bookRequested = FALSE;
7842 /* Fall through to parse the current output */
7847 * Look for machine move.
7849 if ((sscanf(message, "%s %s %s", buf1, buf2, machineMove) == 3 && strcmp(buf2, "...") == 0) ||
7850 (sscanf(message, "%s %s", buf1, machineMove) == 2 && strcmp(buf1, "move") == 0))
7852 /* This method is only useful on engines that support ping */
7853 if (cps->lastPing != cps->lastPong) {
7854 if (gameMode == BeginningOfGame) {
7855 /* Extra move from before last new; ignore */
7856 if (appData.debugMode) {
7857 fprintf(debugFP, "Ignoring extra move from %s\n", cps->which);
7860 if (appData.debugMode) {
7861 fprintf(debugFP, "Undoing extra move from %s, gameMode %d\n",
7862 cps->which, gameMode);
7865 SendToProgram("undo\n", cps);
7871 case BeginningOfGame:
7872 /* Extra move from before last reset; ignore */
7873 if (appData.debugMode) {
7874 fprintf(debugFP, "Ignoring extra move from %s\n", cps->which);
7881 /* Extra move after we tried to stop. The mode test is
7882 not a reliable way of detecting this problem, but it's
7883 the best we can do on engines that don't support ping.
7885 if (appData.debugMode) {
7886 fprintf(debugFP, "Undoing extra move from %s, gameMode %d\n",
7887 cps->which, gameMode);
7889 SendToProgram("undo\n", cps);
7892 case MachinePlaysWhite:
7893 case IcsPlayingWhite:
7894 machineWhite = TRUE;
7897 case MachinePlaysBlack:
7898 case IcsPlayingBlack:
7899 machineWhite = FALSE;
7902 case TwoMachinesPlay:
7903 machineWhite = (cps->twoMachinesColor[0] == 'w');
7906 if (WhiteOnMove(forwardMostMove) != machineWhite) {
7907 if (appData.debugMode) {
7909 "Ignoring move out of turn by %s, gameMode %d"
7910 ", forwardMost %d\n",
7911 cps->which, gameMode, forwardMostMove);
7916 if(cps->alphaRank) AlphaRank(machineMove, 4);
7917 if (!ParseOneMove(machineMove, forwardMostMove, &moveType,
7918 &fromX, &fromY, &toX, &toY, &promoChar)) {
7919 /* Machine move could not be parsed; ignore it. */
7920 snprintf(buf1, MSG_SIZ*10, _("Illegal move \"%s\" from %s machine"),
7921 machineMove, _(cps->which));
7922 DisplayError(buf1, 0);
7923 snprintf(buf1, MSG_SIZ*10, "Xboard: Forfeit due to invalid move: %s (%c%c%c%c) res=%d",
7924 machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, moveType);
7925 if (gameMode == TwoMachinesPlay) {
7926 GameEnds(machineWhite ? BlackWins : WhiteWins,
7932 /* [HGM] Apparently legal, but so far only tested with EP_UNKOWN */
7933 /* So we have to redo legality test with true e.p. status here, */
7934 /* to make sure an illegal e.p. capture does not slip through, */
7935 /* to cause a forfeit on a justified illegal-move complaint */
7936 /* of the opponent. */
7937 if( gameMode==TwoMachinesPlay && appData.testLegality ) {
7939 moveType = LegalityTest(boards[forwardMostMove], PosFlags(forwardMostMove),
7940 fromY, fromX, toY, toX, promoChar);
7941 if(moveType == IllegalMove) {
7942 snprintf(buf1, MSG_SIZ*10, "Xboard: Forfeit due to illegal move: %s (%c%c%c%c)%c",
7943 machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, 0);
7944 GameEnds(machineWhite ? BlackWins : WhiteWins,
7947 } else if(gameInfo.variant != VariantFischeRandom && gameInfo.variant != VariantCapaRandom)
7948 /* [HGM] Kludge to handle engines that send FRC-style castling
7949 when they shouldn't (like TSCP-Gothic) */
7951 case WhiteASideCastleFR:
7952 case BlackASideCastleFR:
7954 currentMoveString[2]++;
7956 case WhiteHSideCastleFR:
7957 case BlackHSideCastleFR:
7959 currentMoveString[2]--;
7961 default: ; // nothing to do, but suppresses warning of pedantic compilers
7964 hintRequested = FALSE;
7965 lastHint[0] = NULLCHAR;
7966 bookRequested = FALSE;
7967 /* Program may be pondering now */
7968 cps->maybeThinking = TRUE;
7969 if (cps->sendTime == 2) cps->sendTime = 1;
7970 if (cps->offeredDraw) cps->offeredDraw--;
7972 /* [AS] Save move info*/
7973 pvInfoList[ forwardMostMove ].score = programStats.score;
7974 pvInfoList[ forwardMostMove ].depth = programStats.depth;
7975 pvInfoList[ forwardMostMove ].time = programStats.time; // [HGM] PGNtime: take time from engine stats
7977 MakeMove(fromX, fromY, toX, toY, promoChar);/*updates forwardMostMove*/
7979 /* [AS] Adjudicate game if needed (note: remember that forwardMostMove now points past the last move) */
7980 if( gameMode == TwoMachinesPlay && adjudicateLossThreshold != 0 && forwardMostMove >= adjudicateLossPlies ) {
7983 while( count < adjudicateLossPlies ) {
7984 int score = pvInfoList[ forwardMostMove - count - 1 ].score;
7987 score = -score; /* Flip score for winning side */
7990 if( score > adjudicateLossThreshold ) {
7997 if( count >= adjudicateLossPlies ) {
7998 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
8000 GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
8001 "Xboard adjudication",
8008 if(Adjudicate(cps)) {
8009 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
8010 return; // [HGM] adjudicate: for all automatic game ends
8014 if ((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack) &&
8016 if(cps->offeredDraw && (signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
8017 SendToICS(ics_prefix); // [HGM] drawclaim: send caim and move on one line for FICS
8019 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
8021 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
8023 if(appData.autoKibitz && !appData.icsEngineAnalyze ) { /* [HGM] kibitz: send most-recent PV info to ICS */
8024 char buf[3*MSG_SIZ];
8026 snprintf(buf, 3*MSG_SIZ, "kibitz !!! %+.2f/%d (%.2f sec, %u nodes, %.0f knps) PV=%s\n",
8027 programStats.score / 100.,
8029 programStats.time / 100.,
8030 (unsigned int)programStats.nodes,
8031 (unsigned int)programStats.nodes / (10*abs(programStats.time) + 1.),
8032 programStats.movelist);
8034 if(appData.debugMode) fprintf(debugFP, "nodes = %d, %lld\n", (int) programStats.nodes, programStats.nodes);
8039 /* [AS] Clear stats for next move */
8040 ClearProgramStats();
8041 thinkOutput[0] = NULLCHAR;
8042 hiddenThinkOutputState = 0;
8045 if (gameMode == TwoMachinesPlay) {
8046 /* [HGM] relaying draw offers moved to after reception of move */
8047 /* and interpreting offer as claim if it brings draw condition */
8048 if (cps->offeredDraw == 1 && cps->other->sendDrawOffers) {
8049 SendToProgram("draw\n", cps->other);
8051 if (cps->other->sendTime) {
8052 SendTimeRemaining(cps->other,
8053 cps->other->twoMachinesColor[0] == 'w');
8055 bookHit = SendMoveToBookUser(forwardMostMove-1, cps->other, FALSE);
8056 if (firstMove && !bookHit) {
8058 if (cps->other->useColors) {
8059 SendToProgram(cps->other->twoMachinesColor, cps->other);
8061 SendToProgram("go\n", cps->other);
8063 cps->other->maybeThinking = TRUE;
8066 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
8068 if (!pausing && appData.ringBellAfterMoves) {
8073 * Reenable menu items that were disabled while
8074 * machine was thinking
8076 if (gameMode != TwoMachinesPlay)
8077 SetUserThinkingEnables();
8079 // [HGM] book: after book hit opponent has received move and is now in force mode
8080 // force the book reply into it, and then fake that it outputted this move by jumping
8081 // back to the beginning of HandleMachineMove, with cps toggled and message set to this move
8083 static char bookMove[MSG_SIZ]; // a bit generous?
8085 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
8086 strcat(bookMove, bookHit);
8089 programStats.nodes = programStats.depth = programStats.time =
8090 programStats.score = programStats.got_only_move = 0;
8091 sprintf(programStats.movelist, "%s (xbook)", bookHit);
8093 if(cps->lastPing != cps->lastPong) {
8094 savedMessage = message; // args for deferred call
8096 ScheduleDelayedEvent(DeferredBookMove, 10);
8105 /* Set special modes for chess engines. Later something general
8106 * could be added here; for now there is just one kludge feature,
8107 * needed because Crafty 15.10 and earlier don't ignore SIGINT
8108 * when "xboard" is given as an interactive command.
8110 if (strncmp(message, "kibitz Hello from Crafty", 24) == 0) {
8111 cps->useSigint = FALSE;
8112 cps->useSigterm = FALSE;
8114 if (strncmp(message, "feature ", 8) == 0) { // [HGM] moved forward to pre-empt non-compliant commands
8115 ParseFeatures(message+8, cps);
8116 return; // [HGM] This return was missing, causing option features to be recognized as non-compliant commands!
8119 if ((!appData.testLegality || gameInfo.variant == VariantFairy) &&
8120 !strncmp(message, "setup ", 6)) { // [HGM] allow first engine to define opening position
8121 int dummy, s=6; char buf[MSG_SIZ];
8122 if(appData.icsActive || forwardMostMove != 0 || cps != &first) return;
8123 if(sscanf(message, "setup (%s", buf) == 1) s = 8 + strlen(buf), buf[s-9] = NULLCHAR, SetCharTable(pieceToChar, buf);
8124 if(startedFromSetupPosition) return;
8125 ParseFEN(boards[0], &dummy, message+s);
8126 DrawPosition(TRUE, boards[0]);
8127 startedFromSetupPosition = TRUE;
8130 /* [HGM] Allow engine to set up a position. Don't ask me why one would
8131 * want this, I was asked to put it in, and obliged.
8133 if (!strncmp(message, "setboard ", 9)) {
8134 Board initial_position;
8136 GameEnds(GameUnfinished, "Engine aborts game", GE_XBOARD);
8138 if (!ParseFEN(initial_position, &blackPlaysFirst, message + 9)) {
8139 DisplayError(_("Bad FEN received from engine"), 0);
8143 CopyBoard(boards[0], initial_position);
8144 initialRulePlies = FENrulePlies;
8145 if(blackPlaysFirst) gameMode = MachinePlaysWhite;
8146 else gameMode = MachinePlaysBlack;
8147 DrawPosition(FALSE, boards[currentMove]);
8153 * Look for communication commands
8155 if (!strncmp(message, "telluser ", 9)) {
8156 if(message[9] == '\\' && message[10] == '\\')
8157 EscapeExpand(message+9, message+11); // [HGM] esc: allow escape sequences in popup box
8159 DisplayNote(message + 9);
8162 if (!strncmp(message, "tellusererror ", 14)) {
8164 if(message[14] == '\\' && message[15] == '\\')
8165 EscapeExpand(message+14, message+16); // [HGM] esc: allow escape sequences in popup box
8167 DisplayError(message + 14, 0);
8170 if (!strncmp(message, "tellopponent ", 13)) {
8171 if (appData.icsActive) {
8173 snprintf(buf1, sizeof(buf1), "%ssay %s\n", ics_prefix, message + 13);
8177 DisplayNote(message + 13);
8181 if (!strncmp(message, "tellothers ", 11)) {
8182 if (appData.icsActive) {
8184 snprintf(buf1, sizeof(buf1), "%swhisper %s\n", ics_prefix, message + 11);
8190 if (!strncmp(message, "tellall ", 8)) {
8191 if (appData.icsActive) {
8193 snprintf(buf1, sizeof(buf1), "%skibitz %s\n", ics_prefix, message + 8);
8197 DisplayNote(message + 8);
8201 if (strncmp(message, "warning", 7) == 0) {
8202 /* Undocumented feature, use tellusererror in new code */
8203 DisplayError(message, 0);
8206 if (sscanf(message, "askuser %s %[^\n]", buf1, buf2) == 2) {
8207 safeStrCpy(realname, cps->tidy, sizeof(realname)/sizeof(realname[0]));
8208 strcat(realname, " query");
8209 AskQuestion(realname, buf2, buf1, cps->pr);
8212 /* Commands from the engine directly to ICS. We don't allow these to be
8213 * sent until we are logged on. Crafty kibitzes have been known to
8214 * interfere with the login process.
8217 if (!strncmp(message, "tellics ", 8)) {
8218 SendToICS(message + 8);
8222 if (!strncmp(message, "tellicsnoalias ", 15)) {
8223 SendToICS(ics_prefix);
8224 SendToICS(message + 15);
8228 /* The following are for backward compatibility only */
8229 if (!strncmp(message,"whisper",7) || !strncmp(message,"kibitz",6) ||
8230 !strncmp(message,"draw",4) || !strncmp(message,"tell",3)) {
8231 SendToICS(ics_prefix);
8237 if (sscanf(message, "pong %d", &cps->lastPong) == 1) {
8241 * If the move is illegal, cancel it and redraw the board.
8242 * Also deal with other error cases. Matching is rather loose
8243 * here to accommodate engines written before the spec.
8245 if (strncmp(message + 1, "llegal move", 11) == 0 ||
8246 strncmp(message, "Error", 5) == 0) {
8247 if (StrStr(message, "name") ||
8248 StrStr(message, "rating") || StrStr(message, "?") ||
8249 StrStr(message, "result") || StrStr(message, "board") ||
8250 StrStr(message, "bk") || StrStr(message, "computer") ||
8251 StrStr(message, "variant") || StrStr(message, "hint") ||
8252 StrStr(message, "random") || StrStr(message, "depth") ||
8253 StrStr(message, "accepted")) {
8256 if (StrStr(message, "protover")) {
8257 /* Program is responding to input, so it's apparently done
8258 initializing, and this error message indicates it is
8259 protocol version 1. So we don't need to wait any longer
8260 for it to initialize and send feature commands. */
8261 FeatureDone(cps, 1);
8262 cps->protocolVersion = 1;
8265 cps->maybeThinking = FALSE;
8267 if (StrStr(message, "draw")) {
8268 /* Program doesn't have "draw" command */
8269 cps->sendDrawOffers = 0;
8272 if (cps->sendTime != 1 &&
8273 (StrStr(message, "time") || StrStr(message, "otim"))) {
8274 /* Program apparently doesn't have "time" or "otim" command */
8278 if (StrStr(message, "analyze")) {
8279 cps->analysisSupport = FALSE;
8280 cps->analyzing = FALSE;
8281 // Reset(FALSE, TRUE); // [HGM] this caused discrepancy between display and internal state!
8282 EditGameEvent(); // [HGM] try to preserve loaded game
8283 snprintf(buf2,MSG_SIZ, _("%s does not support analysis"), cps->tidy);
8284 DisplayError(buf2, 0);
8287 if (StrStr(message, "(no matching move)st")) {
8288 /* Special kludge for GNU Chess 4 only */
8289 cps->stKludge = TRUE;
8290 SendTimeControl(cps, movesPerSession, timeControl,
8291 timeIncrement, appData.searchDepth,
8295 if (StrStr(message, "(no matching move)sd")) {
8296 /* Special kludge for GNU Chess 4 only */
8297 cps->sdKludge = TRUE;
8298 SendTimeControl(cps, movesPerSession, timeControl,
8299 timeIncrement, appData.searchDepth,
8303 if (!StrStr(message, "llegal")) {
8306 if (gameMode == BeginningOfGame || gameMode == EndOfGame ||
8307 gameMode == IcsIdle) return;
8308 if (forwardMostMove <= backwardMostMove) return;
8309 if (pausing) PauseEvent();
8310 if(appData.forceIllegal) {
8311 // [HGM] illegal: machine refused move; force position after move into it
8312 SendToProgram("force\n", cps);
8313 if(!cps->useSetboard) { // hideous kludge on kludge, because SendBoard sucks.
8314 // we have a real problem now, as SendBoard will use the a2a3 kludge
8315 // when black is to move, while there might be nothing on a2 or black
8316 // might already have the move. So send the board as if white has the move.
8317 // But first we must change the stm of the engine, as it refused the last move
8318 SendBoard(cps, 0); // always kludgeless, as white is to move on boards[0]
8319 if(WhiteOnMove(forwardMostMove)) {
8320 SendToProgram("a7a6\n", cps); // for the engine black still had the move
8321 SendBoard(cps, forwardMostMove); // kludgeless board
8323 SendToProgram("a2a3\n", cps); // for the engine white still had the move
8324 CopyBoard(boards[forwardMostMove+1], boards[forwardMostMove]);
8325 SendBoard(cps, forwardMostMove+1); // kludgeless board
8327 } else SendBoard(cps, forwardMostMove); // FEN case, also sets stm properly
8328 if(gameMode == MachinePlaysWhite || gameMode == MachinePlaysBlack ||
8329 gameMode == TwoMachinesPlay)
8330 SendToProgram("go\n", cps);
8333 if (gameMode == PlayFromGameFile) {
8334 /* Stop reading this game file */
8335 gameMode = EditGame;
8338 /* [HGM] illegal-move claim should forfeit game when Xboard */
8339 /* only passes fully legal moves */
8340 if( appData.testLegality && gameMode == TwoMachinesPlay ) {
8341 GameEnds( cps->twoMachinesColor[0] == 'w' ? BlackWins : WhiteWins,
8342 "False illegal-move claim", GE_XBOARD );
8343 return; // do not take back move we tested as valid
8345 currentMove = forwardMostMove-1;
8346 DisplayMove(currentMove-1); /* before DisplayMoveError */
8347 SwitchClocks(forwardMostMove-1); // [HGM] race
8348 DisplayBothClocks();
8349 snprintf(buf1, 10*MSG_SIZ, _("Illegal move \"%s\" (rejected by %s chess program)"),
8350 parseList[currentMove], _(cps->which));
8351 DisplayMoveError(buf1);
8352 DrawPosition(FALSE, boards[currentMove]);
8354 SetUserThinkingEnables();
8357 if (strncmp(message, "time", 4) == 0 && StrStr(message, "Illegal")) {
8358 /* Program has a broken "time" command that
8359 outputs a string not ending in newline.
8365 * If chess program startup fails, exit with an error message.
8366 * Attempts to recover here are futile.
8368 if ((StrStr(message, "unknown host") != NULL)
8369 || (StrStr(message, "No remote directory") != NULL)
8370 || (StrStr(message, "not found") != NULL)
8371 || (StrStr(message, "No such file") != NULL)
8372 || (StrStr(message, "can't alloc") != NULL)
8373 || (StrStr(message, "Permission denied") != NULL)) {
8375 cps->maybeThinking = FALSE;
8376 snprintf(buf1, sizeof(buf1), _("Failed to start %s chess program %s on %s: %s\n"),
8377 _(cps->which), cps->program, cps->host, message);
8378 RemoveInputSource(cps->isr);
8379 if(appData.icsActive) DisplayFatalError(buf1, 0, 1); else {
8380 if(cps == &first) appData.noChessProgram = TRUE;
8381 DisplayError(buf1, 0);
8387 * Look for hint output
8389 if (sscanf(message, "Hint: %s", buf1) == 1) {
8390 if (cps == &first && hintRequested) {
8391 hintRequested = FALSE;
8392 if (ParseOneMove(buf1, forwardMostMove, &moveType,
8393 &fromX, &fromY, &toX, &toY, &promoChar)) {
8394 (void) CoordsToAlgebraic(boards[forwardMostMove],
8395 PosFlags(forwardMostMove),
8396 fromY, fromX, toY, toX, promoChar, buf1);
8397 snprintf(buf2, sizeof(buf2), _("Hint: %s"), buf1);
8398 DisplayInformation(buf2);
8400 /* Hint move could not be parsed!? */
8401 snprintf(buf2, sizeof(buf2),
8402 _("Illegal hint move \"%s\"\nfrom %s chess program"),
8403 buf1, _(cps->which));
8404 DisplayError(buf2, 0);
8407 safeStrCpy(lastHint, buf1, sizeof(lastHint)/sizeof(lastHint[0]));
8413 * Ignore other messages if game is not in progress
8415 if (gameMode == BeginningOfGame || gameMode == EndOfGame ||
8416 gameMode == IcsIdle || cps->lastPing != cps->lastPong) return;
8419 * look for win, lose, draw, or draw offer
8421 if (strncmp(message, "1-0", 3) == 0) {
8422 char *p, *q, *r = "";
8423 p = strchr(message, '{');
8431 GameEnds(WhiteWins, r, GE_ENGINE1 + (cps != &first)); /* [HGM] pass claimer indication for claim test */
8433 } else if (strncmp(message, "0-1", 3) == 0) {
8434 char *p, *q, *r = "";
8435 p = strchr(message, '{');
8443 /* Kludge for Arasan 4.1 bug */
8444 if (strcmp(r, "Black resigns") == 0) {
8445 GameEnds(WhiteWins, r, GE_ENGINE1 + (cps != &first));
8448 GameEnds(BlackWins, r, GE_ENGINE1 + (cps != &first));
8450 } else if (strncmp(message, "1/2", 3) == 0) {
8451 char *p, *q, *r = "";
8452 p = strchr(message, '{');
8461 GameEnds(GameIsDrawn, r, GE_ENGINE1 + (cps != &first));
8464 } else if (strncmp(message, "White resign", 12) == 0) {
8465 GameEnds(BlackWins, "White resigns", GE_ENGINE1 + (cps != &first));
8467 } else if (strncmp(message, "Black resign", 12) == 0) {
8468 GameEnds(WhiteWins, "Black resigns", GE_ENGINE1 + (cps != &first));
8470 } else if (strncmp(message, "White matches", 13) == 0 ||
8471 strncmp(message, "Black matches", 13) == 0 ) {
8472 /* [HGM] ignore GNUShogi noises */
8474 } else if (strncmp(message, "White", 5) == 0 &&
8475 message[5] != '(' &&
8476 StrStr(message, "Black") == NULL) {
8477 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
8479 } else if (strncmp(message, "Black", 5) == 0 &&
8480 message[5] != '(') {
8481 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
8483 } else if (strcmp(message, "resign") == 0 ||
8484 strcmp(message, "computer resigns") == 0) {
8486 case MachinePlaysBlack:
8487 case IcsPlayingBlack:
8488 GameEnds(WhiteWins, "Black resigns", GE_ENGINE);
8490 case MachinePlaysWhite:
8491 case IcsPlayingWhite:
8492 GameEnds(BlackWins, "White resigns", GE_ENGINE);
8494 case TwoMachinesPlay:
8495 if (cps->twoMachinesColor[0] == 'w')
8496 GameEnds(BlackWins, "White resigns", GE_ENGINE1 + (cps != &first));
8498 GameEnds(WhiteWins, "Black resigns", GE_ENGINE1 + (cps != &first));
8505 } else if (strncmp(message, "opponent mates", 14) == 0) {
8507 case MachinePlaysBlack:
8508 case IcsPlayingBlack:
8509 GameEnds(WhiteWins, "White mates", GE_ENGINE);
8511 case MachinePlaysWhite:
8512 case IcsPlayingWhite:
8513 GameEnds(BlackWins, "Black mates", GE_ENGINE);
8515 case TwoMachinesPlay:
8516 if (cps->twoMachinesColor[0] == 'w')
8517 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
8519 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
8526 } else if (strncmp(message, "computer mates", 14) == 0) {
8528 case MachinePlaysBlack:
8529 case IcsPlayingBlack:
8530 GameEnds(BlackWins, "Black mates", GE_ENGINE1);
8532 case MachinePlaysWhite:
8533 case IcsPlayingWhite:
8534 GameEnds(WhiteWins, "White mates", GE_ENGINE);
8536 case TwoMachinesPlay:
8537 if (cps->twoMachinesColor[0] == 'w')
8538 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
8540 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
8547 } else if (strncmp(message, "checkmate", 9) == 0) {
8548 if (WhiteOnMove(forwardMostMove)) {
8549 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
8551 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
8554 } else if (strstr(message, "Draw") != NULL ||
8555 strstr(message, "game is a draw") != NULL) {
8556 GameEnds(GameIsDrawn, "Draw", GE_ENGINE1 + (cps != &first));
8558 } else if (strstr(message, "offer") != NULL &&
8559 strstr(message, "draw") != NULL) {
8561 if (appData.zippyPlay && first.initDone) {
8562 /* Relay offer to ICS */
8563 SendToICS(ics_prefix);
8564 SendToICS("draw\n");
8567 cps->offeredDraw = 2; /* valid until this engine moves twice */
8568 if (gameMode == TwoMachinesPlay) {
8569 if (cps->other->offeredDraw) {
8570 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
8571 /* [HGM] in two-machine mode we delay relaying draw offer */
8572 /* until after we also have move, to see if it is really claim */
8574 } else if (gameMode == MachinePlaysWhite ||
8575 gameMode == MachinePlaysBlack) {
8576 if (userOfferedDraw) {
8577 DisplayInformation(_("Machine accepts your draw offer"));
8578 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
8580 DisplayInformation(_("Machine offers a draw\nSelect Action / Draw to agree"));
8587 * Look for thinking output
8589 if ( appData.showThinking // [HGM] thinking: test all options that cause this output
8590 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp()
8592 int plylev, mvleft, mvtot, curscore, time;
8593 char mvname[MOVE_LEN];
8597 int prefixHint = FALSE;
8598 mvname[0] = NULLCHAR;
8601 case MachinePlaysBlack:
8602 case IcsPlayingBlack:
8603 if (WhiteOnMove(forwardMostMove)) prefixHint = TRUE;
8605 case MachinePlaysWhite:
8606 case IcsPlayingWhite:
8607 if (!WhiteOnMove(forwardMostMove)) prefixHint = TRUE;
8612 case IcsObserving: /* [DM] icsEngineAnalyze */
8613 if (!appData.icsEngineAnalyze) ignore = TRUE;
8615 case TwoMachinesPlay:
8616 if ((cps->twoMachinesColor[0] == 'w') != WhiteOnMove(forwardMostMove)) {
8626 ChessProgramStats tempStats = programStats; // [HGM] info: filter out info lines
8628 if (sscanf(message, "%d%c %d %d " u64Display " %[^\n]\n",
8629 &plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5) {
8631 if (plyext != ' ' && plyext != '\t') {
8635 /* [AS] Negate score if machine is playing black and reporting absolute scores */
8636 if( cps->scoreIsAbsolute &&
8637 ( gameMode == MachinePlaysBlack ||
8638 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b' ||
8639 gameMode == IcsPlayingBlack || // [HGM] also add other situations where engine should report black POV
8640 (gameMode == AnalyzeMode || gameMode == AnalyzeFile || gameMode == IcsObserving && appData.icsEngineAnalyze) &&
8641 !WhiteOnMove(currentMove)
8644 curscore = -curscore;
8647 if(appData.pvSAN[cps==&second]) pv = PvToSAN(buf1);
8649 if(serverMoves && (time > 100 || time == 0 && plylev > 7)) {
8652 snprintf(buf, MSG_SIZ, "%s", appData.serverMovesName);
8653 buf[strlen(buf)-1] = gameMode == MachinePlaysWhite ? 'w' :
8654 gameMode == MachinePlaysBlack ? 'b' : cps->twoMachinesColor[0];
8655 if(appData.debugMode) fprintf(debugFP, "write PV on file '%s'\n", buf);
8656 if(f = fopen(buf, "w")) { // export PV to applicable PV file
8657 fprintf(f, "%5.2f/%-2d %s", curscore/100., plylev, pv);
8659 } else DisplayError(_("failed writing PV"), 0);
8662 tempStats.depth = plylev;
8663 tempStats.nodes = nodes;
8664 tempStats.time = time;
8665 tempStats.score = curscore;
8666 tempStats.got_only_move = 0;
8668 if(cps->nps >= 0) { /* [HGM] nps: use engine nodes or time to decrement clock */
8671 if(cps->nps == 0) ticklen = 10*time; // use engine reported time
8672 else ticklen = (1000. * u64ToDouble(nodes)) / cps->nps; // convert node count to time
8673 if(WhiteOnMove(forwardMostMove) && (gameMode == MachinePlaysWhite ||
8674 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'w'))
8675 whiteTimeRemaining = timeRemaining[0][forwardMostMove] - ticklen;
8676 if(!WhiteOnMove(forwardMostMove) && (gameMode == MachinePlaysBlack ||
8677 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b'))
8678 blackTimeRemaining = timeRemaining[1][forwardMostMove] - ticklen;
8681 /* Buffer overflow protection */
8682 if (pv[0] != NULLCHAR) {
8683 if (strlen(pv) >= sizeof(tempStats.movelist)
8684 && appData.debugMode) {
8686 "PV is too long; using the first %u bytes.\n",
8687 (unsigned) sizeof(tempStats.movelist) - 1);
8690 safeStrCpy( tempStats.movelist, pv, sizeof(tempStats.movelist)/sizeof(tempStats.movelist[0]) );
8692 sprintf(tempStats.movelist, " no PV\n");
8695 if (tempStats.seen_stat) {
8696 tempStats.ok_to_send = 1;
8699 if (strchr(tempStats.movelist, '(') != NULL) {
8700 tempStats.line_is_book = 1;
8701 tempStats.nr_moves = 0;
8702 tempStats.moves_left = 0;
8704 tempStats.line_is_book = 0;
8707 if(tempStats.score != 0 || tempStats.nodes != 0 || tempStats.time != 0)
8708 programStats = tempStats; // [HGM] info: only set stats if genuine PV and not an info line
8710 SendProgramStatsToFrontend( cps, &tempStats );
8713 [AS] Protect the thinkOutput buffer from overflow... this
8714 is only useful if buf1 hasn't overflowed first!
8716 snprintf(thinkOutput, sizeof(thinkOutput)/sizeof(thinkOutput[0]), "[%d]%c%+.2f %s%s",
8718 (gameMode == TwoMachinesPlay ?
8719 ToUpper(cps->twoMachinesColor[0]) : ' '),
8720 ((double) curscore) / 100.0,
8721 prefixHint ? lastHint : "",
8722 prefixHint ? " " : "" );
8724 if( buf1[0] != NULLCHAR ) {
8725 unsigned max_len = sizeof(thinkOutput) - strlen(thinkOutput) - 1;
8727 if( strlen(pv) > max_len ) {
8728 if( appData.debugMode) {
8729 fprintf(debugFP,"PV is too long for thinkOutput, truncating.\n");
8731 pv[max_len+1] = '\0';
8734 strcat( thinkOutput, pv);
8737 if (currentMove == forwardMostMove || gameMode == AnalyzeMode
8738 || gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
8739 DisplayMove(currentMove - 1);
8743 } else if ((p=StrStr(message, "(only move)")) != NULL) {
8744 /* crafty (9.25+) says "(only move) <move>"
8745 * if there is only 1 legal move
8747 sscanf(p, "(only move) %s", buf1);
8748 snprintf(thinkOutput, sizeof(thinkOutput)/sizeof(thinkOutput[0]), "%s (only move)", buf1);
8749 sprintf(programStats.movelist, "%s (only move)", buf1);
8750 programStats.depth = 1;
8751 programStats.nr_moves = 1;
8752 programStats.moves_left = 1;
8753 programStats.nodes = 1;
8754 programStats.time = 1;
8755 programStats.got_only_move = 1;
8757 /* Not really, but we also use this member to
8758 mean "line isn't going to change" (Crafty
8759 isn't searching, so stats won't change) */
8760 programStats.line_is_book = 1;
8762 SendProgramStatsToFrontend( cps, &programStats );
8764 if (currentMove == forwardMostMove || gameMode==AnalyzeMode ||
8765 gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
8766 DisplayMove(currentMove - 1);
8769 } else if (sscanf(message,"stat01: %d " u64Display " %d %d %d %s",
8770 &time, &nodes, &plylev, &mvleft,
8771 &mvtot, mvname) >= 5) {
8772 /* The stat01: line is from Crafty (9.29+) in response
8773 to the "." command */
8774 programStats.seen_stat = 1;
8775 cps->maybeThinking = TRUE;
8777 if (programStats.got_only_move || !appData.periodicUpdates)
8780 programStats.depth = plylev;
8781 programStats.time = time;
8782 programStats.nodes = nodes;
8783 programStats.moves_left = mvleft;
8784 programStats.nr_moves = mvtot;
8785 safeStrCpy(programStats.move_name, mvname, sizeof(programStats.move_name)/sizeof(programStats.move_name[0]));
8786 programStats.ok_to_send = 1;
8787 programStats.movelist[0] = '\0';
8789 SendProgramStatsToFrontend( cps, &programStats );
8793 } else if (strncmp(message,"++",2) == 0) {
8794 /* Crafty 9.29+ outputs this */
8795 programStats.got_fail = 2;
8798 } else if (strncmp(message,"--",2) == 0) {
8799 /* Crafty 9.29+ outputs this */
8800 programStats.got_fail = 1;
8803 } else if (thinkOutput[0] != NULLCHAR &&
8804 strncmp(message, " ", 4) == 0) {
8805 unsigned message_len;
8808 while (*p && *p == ' ') p++;
8810 message_len = strlen( p );
8812 /* [AS] Avoid buffer overflow */
8813 if( sizeof(thinkOutput) - strlen(thinkOutput) - 1 > message_len ) {
8814 strcat(thinkOutput, " ");
8815 strcat(thinkOutput, p);
8818 if( sizeof(programStats.movelist) - strlen(programStats.movelist) - 1 > message_len ) {
8819 strcat(programStats.movelist, " ");
8820 strcat(programStats.movelist, p);
8823 if (currentMove == forwardMostMove || gameMode==AnalyzeMode ||
8824 gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
8825 DisplayMove(currentMove - 1);
8833 if (sscanf(message, "%d%c %d %d " u64Display " %[^\n]\n",
8834 &plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5)
8836 ChessProgramStats cpstats;
8838 if (plyext != ' ' && plyext != '\t') {
8842 /* [AS] Negate score if machine is playing black and reporting absolute scores */
8843 if( cps->scoreIsAbsolute && ((gameMode == MachinePlaysBlack) || (gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b')) ) {
8844 curscore = -curscore;
8847 cpstats.depth = plylev;
8848 cpstats.nodes = nodes;
8849 cpstats.time = time;
8850 cpstats.score = curscore;
8851 cpstats.got_only_move = 0;
8852 cpstats.movelist[0] = '\0';
8854 if (buf1[0] != NULLCHAR) {
8855 safeStrCpy( cpstats.movelist, buf1, sizeof(cpstats.movelist)/sizeof(cpstats.movelist[0]) );
8858 cpstats.ok_to_send = 0;
8859 cpstats.line_is_book = 0;
8860 cpstats.nr_moves = 0;
8861 cpstats.moves_left = 0;
8863 SendProgramStatsToFrontend( cps, &cpstats );
8870 /* Parse a game score from the character string "game", and
8871 record it as the history of the current game. The game
8872 score is NOT assumed to start from the standard position.
8873 The display is not updated in any way.
8876 ParseGameHistory (char *game)
8879 int fromX, fromY, toX, toY, boardIndex;
8884 if (appData.debugMode)
8885 fprintf(debugFP, "Parsing game history: %s\n", game);
8887 if (gameInfo.event == NULL) gameInfo.event = StrSave("ICS game");
8888 gameInfo.site = StrSave(appData.icsHost);
8889 gameInfo.date = PGNDate();
8890 gameInfo.round = StrSave("-");
8892 /* Parse out names of players */
8893 while (*game == ' ') game++;
8895 while (*game != ' ') *p++ = *game++;
8897 gameInfo.white = StrSave(buf);
8898 while (*game == ' ') game++;
8900 while (*game != ' ' && *game != '\n') *p++ = *game++;
8902 gameInfo.black = StrSave(buf);
8905 boardIndex = blackPlaysFirst ? 1 : 0;
8908 yyboardindex = boardIndex;
8909 moveType = (ChessMove) Myylex();
8911 case IllegalMove: /* maybe suicide chess, etc. */
8912 if (appData.debugMode) {
8913 fprintf(debugFP, "Illegal move from ICS: '%s'\n", yy_text);
8914 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
8915 setbuf(debugFP, NULL);
8917 case WhitePromotion:
8918 case BlackPromotion:
8919 case WhiteNonPromotion:
8920 case BlackNonPromotion:
8922 case WhiteCapturesEnPassant:
8923 case BlackCapturesEnPassant:
8924 case WhiteKingSideCastle:
8925 case WhiteQueenSideCastle:
8926 case BlackKingSideCastle:
8927 case BlackQueenSideCastle:
8928 case WhiteKingSideCastleWild:
8929 case WhiteQueenSideCastleWild:
8930 case BlackKingSideCastleWild:
8931 case BlackQueenSideCastleWild:
8933 case WhiteHSideCastleFR:
8934 case WhiteASideCastleFR:
8935 case BlackHSideCastleFR:
8936 case BlackASideCastleFR:
8938 fromX = currentMoveString[0] - AAA;
8939 fromY = currentMoveString[1] - ONE;
8940 toX = currentMoveString[2] - AAA;
8941 toY = currentMoveString[3] - ONE;
8942 promoChar = currentMoveString[4];
8946 if(currentMoveString[0] == '@') continue; // no null moves in ICS mode!
8947 fromX = moveType == WhiteDrop ?
8948 (int) CharToPiece(ToUpper(currentMoveString[0])) :
8949 (int) CharToPiece(ToLower(currentMoveString[0]));
8951 toX = currentMoveString[2] - AAA;
8952 toY = currentMoveString[3] - ONE;
8953 promoChar = NULLCHAR;
8957 snprintf(buf, MSG_SIZ, _("Ambiguous move in ICS output: \"%s\""), yy_text);
8958 if (appData.debugMode) {
8959 fprintf(debugFP, "Ambiguous move from ICS: '%s'\n", yy_text);
8960 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
8961 setbuf(debugFP, NULL);
8963 DisplayError(buf, 0);
8965 case ImpossibleMove:
8967 snprintf(buf, MSG_SIZ, _("Illegal move in ICS output: \"%s\""), yy_text);
8968 if (appData.debugMode) {
8969 fprintf(debugFP, "Impossible move from ICS: '%s'\n", yy_text);
8970 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
8971 setbuf(debugFP, NULL);
8973 DisplayError(buf, 0);
8976 if (boardIndex < backwardMostMove) {
8977 /* Oops, gap. How did that happen? */
8978 DisplayError(_("Gap in move list"), 0);
8981 backwardMostMove = blackPlaysFirst ? 1 : 0;
8982 if (boardIndex > forwardMostMove) {
8983 forwardMostMove = boardIndex;
8987 if (boardIndex > (blackPlaysFirst ? 1 : 0)) {
8988 strcat(parseList[boardIndex-1], " ");
8989 strcat(parseList[boardIndex-1], yy_text);
9001 case GameUnfinished:
9002 if (gameMode == IcsExamining) {
9003 if (boardIndex < backwardMostMove) {
9004 /* Oops, gap. How did that happen? */
9007 backwardMostMove = blackPlaysFirst ? 1 : 0;
9010 gameInfo.result = moveType;
9011 p = strchr(yy_text, '{');
9012 if (p == NULL) p = strchr(yy_text, '(');
9015 if (p[0] == '0' || p[0] == '1' || p[0] == '*') p = "";
9017 q = strchr(p, *p == '{' ? '}' : ')');
9018 if (q != NULL) *q = NULLCHAR;
9021 while(q = strchr(p, '\n')) *q = ' '; // [HGM] crush linefeeds in result message
9022 gameInfo.resultDetails = StrSave(p);
9025 if (boardIndex >= forwardMostMove &&
9026 !(gameMode == IcsObserving && ics_gamenum == -1)) {
9027 backwardMostMove = blackPlaysFirst ? 1 : 0;
9030 (void) CoordsToAlgebraic(boards[boardIndex], PosFlags(boardIndex),
9031 fromY, fromX, toY, toX, promoChar,
9032 parseList[boardIndex]);
9033 CopyBoard(boards[boardIndex + 1], boards[boardIndex]);
9034 /* currentMoveString is set as a side-effect of yylex */
9035 safeStrCpy(moveList[boardIndex], currentMoveString, sizeof(moveList[boardIndex])/sizeof(moveList[boardIndex][0]));
9036 strcat(moveList[boardIndex], "\n");
9038 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[boardIndex]);
9039 switch (MateTest(boards[boardIndex], PosFlags(boardIndex)) ) {
9045 if(gameInfo.variant != VariantShogi)
9046 strcat(parseList[boardIndex - 1], "+");
9050 strcat(parseList[boardIndex - 1], "#");
9057 /* Apply a move to the given board */
9059 ApplyMove (int fromX, int fromY, int toX, int toY, int promoChar, Board board)
9061 ChessSquare captured = board[toY][toX], piece, king; int p, oldEP = EP_NONE, berolina = 0;
9062 int promoRank = gameInfo.variant == VariantMakruk || gameInfo.variant == VariantGrand ? 3 : 1;
9064 /* [HGM] compute & store e.p. status and castling rights for new position */
9065 /* we can always do that 'in place', now pointers to these rights are passed to ApplyMove */
9067 if(gameInfo.variant == VariantBerolina) berolina = EP_BEROLIN_A;
9068 oldEP = (signed char)board[EP_STATUS];
9069 board[EP_STATUS] = EP_NONE;
9071 if (fromY == DROP_RANK) {
9073 if(fromX == EmptySquare) { // [HGM] pass: empty drop encodes null move; nothing to change.
9074 board[EP_STATUS] = EP_CAPTURE; // null move considered irreversible
9077 piece = board[toY][toX] = (ChessSquare) fromX;
9081 if( board[toY][toX] != EmptySquare )
9082 board[EP_STATUS] = EP_CAPTURE;
9084 if( board[fromY][fromX] == WhiteLance || board[fromY][fromX] == BlackLance ) {
9085 if( gameInfo.variant != VariantSuper && gameInfo.variant != VariantShogi )
9086 board[EP_STATUS] = EP_PAWN_MOVE; // Lance is Pawn-like in most variants
9088 if( board[fromY][fromX] == WhitePawn ) {
9089 if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
9090 board[EP_STATUS] = EP_PAWN_MOVE;
9092 if(toX>BOARD_LEFT && board[toY][toX-1] == BlackPawn &&
9093 gameInfo.variant != VariantBerolina || toX < fromX)
9094 board[EP_STATUS] = toX | berolina;
9095 if(toX<BOARD_RGHT-1 && board[toY][toX+1] == BlackPawn &&
9096 gameInfo.variant != VariantBerolina || toX > fromX)
9097 board[EP_STATUS] = toX;
9100 if( board[fromY][fromX] == BlackPawn ) {
9101 if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
9102 board[EP_STATUS] = EP_PAWN_MOVE;
9103 if( toY-fromY== -2) {
9104 if(toX>BOARD_LEFT && board[toY][toX-1] == WhitePawn &&
9105 gameInfo.variant != VariantBerolina || toX < fromX)
9106 board[EP_STATUS] = toX | berolina;
9107 if(toX<BOARD_RGHT-1 && board[toY][toX+1] == WhitePawn &&
9108 gameInfo.variant != VariantBerolina || toX > fromX)
9109 board[EP_STATUS] = toX;
9113 for(i=0; i<nrCastlingRights; i++) {
9114 if(board[CASTLING][i] == fromX && castlingRank[i] == fromY ||
9115 board[CASTLING][i] == toX && castlingRank[i] == toY
9116 ) board[CASTLING][i] = NoRights; // revoke for moved or captured piece
9119 if (fromX == toX && fromY == toY) return;
9121 piece = board[fromY][fromX]; /* [HGM] remember, for Shogi promotion */
9122 king = piece < (int) BlackPawn ? WhiteKing : BlackKing; /* [HGM] Knightmate simplify testing for castling */
9123 if(gameInfo.variant == VariantKnightmate)
9124 king += (int) WhiteUnicorn - (int) WhiteKing;
9126 /* Code added by Tord: */
9127 /* FRC castling assumed when king captures friendly rook. [HGM] or RxK for S-Chess */
9128 if (board[fromY][fromX] == WhiteKing && board[toY][toX] == WhiteRook ||
9129 board[fromY][fromX] == WhiteRook && board[toY][toX] == WhiteKing) {
9130 board[fromY][fromX] = EmptySquare;
9131 board[toY][toX] = EmptySquare;
9132 if((toX > fromX) != (piece == WhiteRook)) {
9133 board[0][BOARD_RGHT-2] = WhiteKing; board[0][BOARD_RGHT-3] = WhiteRook;
9135 board[0][BOARD_LEFT+2] = WhiteKing; board[0][BOARD_LEFT+3] = WhiteRook;
9137 } else if (board[fromY][fromX] == BlackKing && board[toY][toX] == BlackRook ||
9138 board[fromY][fromX] == BlackRook && board[toY][toX] == BlackKing) {
9139 board[fromY][fromX] = EmptySquare;
9140 board[toY][toX] = EmptySquare;
9141 if((toX > fromX) != (piece == BlackRook)) {
9142 board[BOARD_HEIGHT-1][BOARD_RGHT-2] = BlackKing; board[BOARD_HEIGHT-1][BOARD_RGHT-3] = BlackRook;
9144 board[BOARD_HEIGHT-1][BOARD_LEFT+2] = BlackKing; board[BOARD_HEIGHT-1][BOARD_LEFT+3] = BlackRook;
9146 /* End of code added by Tord */
9148 } else if (board[fromY][fromX] == king
9149 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
9150 && toY == fromY && toX > fromX+1) {
9151 board[fromY][fromX] = EmptySquare;
9152 board[toY][toX] = king;
9153 board[toY][toX-1] = board[fromY][BOARD_RGHT-1];
9154 board[fromY][BOARD_RGHT-1] = EmptySquare;
9155 } else if (board[fromY][fromX] == king
9156 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
9157 && toY == fromY && toX < fromX-1) {
9158 board[fromY][fromX] = EmptySquare;
9159 board[toY][toX] = king;
9160 board[toY][toX+1] = board[fromY][BOARD_LEFT];
9161 board[fromY][BOARD_LEFT] = EmptySquare;
9162 } else if ((board[fromY][fromX] == WhitePawn && gameInfo.variant != VariantXiangqi ||
9163 board[fromY][fromX] == WhiteLance && gameInfo.variant != VariantSuper && gameInfo.variant != VariantShogi)
9164 && toY >= BOARD_HEIGHT-promoRank && promoChar // defaulting to Q is done elsewhere
9166 /* white pawn promotion */
9167 board[toY][toX] = CharToPiece(ToUpper(promoChar));
9168 if(gameInfo.variant==VariantBughouse ||
9169 gameInfo.variant==VariantCrazyhouse) /* [HGM] use shadow piece */
9170 board[toY][toX] = (ChessSquare) (PROMOTED board[toY][toX]);
9171 board[fromY][fromX] = EmptySquare;
9172 } else if ((fromY >= BOARD_HEIGHT>>1)
9174 && gameInfo.variant != VariantXiangqi
9175 && gameInfo.variant != VariantBerolina
9176 && (board[fromY][fromX] == WhitePawn)
9177 && (board[toY][toX] == EmptySquare)) {
9178 board[fromY][fromX] = EmptySquare;
9179 board[toY][toX] = WhitePawn;
9180 captured = board[toY - 1][toX];
9181 board[toY - 1][toX] = EmptySquare;
9182 } else if ((fromY == BOARD_HEIGHT-4)
9184 && gameInfo.variant == VariantBerolina
9185 && (board[fromY][fromX] == WhitePawn)
9186 && (board[toY][toX] == EmptySquare)) {
9187 board[fromY][fromX] = EmptySquare;
9188 board[toY][toX] = WhitePawn;
9189 if(oldEP & EP_BEROLIN_A) {
9190 captured = board[fromY][fromX-1];
9191 board[fromY][fromX-1] = EmptySquare;
9192 }else{ captured = board[fromY][fromX+1];
9193 board[fromY][fromX+1] = EmptySquare;
9195 } else if (board[fromY][fromX] == king
9196 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
9197 && toY == fromY && toX > fromX+1) {
9198 board[fromY][fromX] = EmptySquare;
9199 board[toY][toX] = king;
9200 board[toY][toX-1] = board[fromY][BOARD_RGHT-1];
9201 board[fromY][BOARD_RGHT-1] = EmptySquare;
9202 } else if (board[fromY][fromX] == king
9203 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
9204 && toY == fromY && toX < fromX-1) {
9205 board[fromY][fromX] = EmptySquare;
9206 board[toY][toX] = king;
9207 board[toY][toX+1] = board[fromY][BOARD_LEFT];
9208 board[fromY][BOARD_LEFT] = EmptySquare;
9209 } else if (fromY == 7 && fromX == 3
9210 && board[fromY][fromX] == BlackKing
9211 && toY == 7 && toX == 5) {
9212 board[fromY][fromX] = EmptySquare;
9213 board[toY][toX] = BlackKing;
9214 board[fromY][7] = EmptySquare;
9215 board[toY][4] = BlackRook;
9216 } else if (fromY == 7 && fromX == 3
9217 && board[fromY][fromX] == BlackKing
9218 && toY == 7 && toX == 1) {
9219 board[fromY][fromX] = EmptySquare;
9220 board[toY][toX] = BlackKing;
9221 board[fromY][0] = EmptySquare;
9222 board[toY][2] = BlackRook;
9223 } else if ((board[fromY][fromX] == BlackPawn && gameInfo.variant != VariantXiangqi ||
9224 board[fromY][fromX] == BlackLance && gameInfo.variant != VariantSuper && gameInfo.variant != VariantShogi)
9225 && toY < promoRank && promoChar
9227 /* black pawn promotion */
9228 board[toY][toX] = CharToPiece(ToLower(promoChar));
9229 if(gameInfo.variant==VariantBughouse ||
9230 gameInfo.variant==VariantCrazyhouse) /* [HGM] use shadow piece */
9231 board[toY][toX] = (ChessSquare) (PROMOTED board[toY][toX]);
9232 board[fromY][fromX] = EmptySquare;
9233 } else if ((fromY < BOARD_HEIGHT>>1)
9235 && gameInfo.variant != VariantXiangqi
9236 && gameInfo.variant != VariantBerolina
9237 && (board[fromY][fromX] == BlackPawn)
9238 && (board[toY][toX] == EmptySquare)) {
9239 board[fromY][fromX] = EmptySquare;
9240 board[toY][toX] = BlackPawn;
9241 captured = board[toY + 1][toX];
9242 board[toY + 1][toX] = EmptySquare;
9243 } else if ((fromY == 3)
9245 && gameInfo.variant == VariantBerolina
9246 && (board[fromY][fromX] == BlackPawn)
9247 && (board[toY][toX] == EmptySquare)) {
9248 board[fromY][fromX] = EmptySquare;
9249 board[toY][toX] = BlackPawn;
9250 if(oldEP & EP_BEROLIN_A) {
9251 captured = board[fromY][fromX-1];
9252 board[fromY][fromX-1] = EmptySquare;
9253 }else{ captured = board[fromY][fromX+1];
9254 board[fromY][fromX+1] = EmptySquare;
9257 board[toY][toX] = board[fromY][fromX];
9258 board[fromY][fromX] = EmptySquare;
9262 if (gameInfo.holdingsWidth != 0) {
9264 /* !!A lot more code needs to be written to support holdings */
9265 /* [HGM] OK, so I have written it. Holdings are stored in the */
9266 /* penultimate board files, so they are automaticlly stored */
9267 /* in the game history. */
9268 if (fromY == DROP_RANK || gameInfo.variant == VariantSChess
9269 && promoChar && piece != WhitePawn && piece != BlackPawn) {
9270 /* Delete from holdings, by decreasing count */
9271 /* and erasing image if necessary */
9272 p = fromY == DROP_RANK ? (int) fromX : CharToPiece(piece > BlackPawn ? ToLower(promoChar) : ToUpper(promoChar));
9273 if(p < (int) BlackPawn) { /* white drop */
9274 p -= (int)WhitePawn;
9275 p = PieceToNumber((ChessSquare)p);
9276 if(p >= gameInfo.holdingsSize) p = 0;
9277 if(--board[p][BOARD_WIDTH-2] <= 0)
9278 board[p][BOARD_WIDTH-1] = EmptySquare;
9279 if((int)board[p][BOARD_WIDTH-2] < 0)
9280 board[p][BOARD_WIDTH-2] = 0;
9281 } else { /* black drop */
9282 p -= (int)BlackPawn;
9283 p = PieceToNumber((ChessSquare)p);
9284 if(p >= gameInfo.holdingsSize) p = 0;
9285 if(--board[BOARD_HEIGHT-1-p][1] <= 0)
9286 board[BOARD_HEIGHT-1-p][0] = EmptySquare;
9287 if((int)board[BOARD_HEIGHT-1-p][1] < 0)
9288 board[BOARD_HEIGHT-1-p][1] = 0;
9291 if (captured != EmptySquare && gameInfo.holdingsSize > 0
9292 && gameInfo.variant != VariantBughouse && gameInfo.variant != VariantSChess ) {
9293 /* [HGM] holdings: Add to holdings, if holdings exist */
9294 if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand) {
9295 // [HGM] superchess: suppress flipping color of captured pieces by reverse pre-flip
9296 captured = (int) captured >= (int) BlackPawn ? BLACK_TO_WHITE captured : WHITE_TO_BLACK captured;
9299 if (p >= (int) BlackPawn) {
9300 p -= (int)BlackPawn;
9301 if(gameInfo.variant == VariantShogi && DEMOTED p >= 0) {
9302 /* in Shogi restore piece to its original first */
9303 captured = (ChessSquare) (DEMOTED captured);
9306 p = PieceToNumber((ChessSquare)p);
9307 if(p >= gameInfo.holdingsSize) { p = 0; captured = BlackPawn; }
9308 board[p][BOARD_WIDTH-2]++;
9309 board[p][BOARD_WIDTH-1] = BLACK_TO_WHITE captured;
9311 p -= (int)WhitePawn;
9312 if(gameInfo.variant == VariantShogi && DEMOTED p >= 0) {
9313 captured = (ChessSquare) (DEMOTED captured);
9316 p = PieceToNumber((ChessSquare)p);
9317 if(p >= gameInfo.holdingsSize) { p = 0; captured = WhitePawn; }
9318 board[BOARD_HEIGHT-1-p][1]++;
9319 board[BOARD_HEIGHT-1-p][0] = WHITE_TO_BLACK captured;
9322 } else if (gameInfo.variant == VariantAtomic) {
9323 if (captured != EmptySquare) {
9325 for (y = toY-1; y <= toY+1; y++) {
9326 for (x = toX-1; x <= toX+1; x++) {
9327 if (y >= 0 && y < BOARD_HEIGHT && x >= BOARD_LEFT && x < BOARD_RGHT &&
9328 board[y][x] != WhitePawn && board[y][x] != BlackPawn) {
9329 board[y][x] = EmptySquare;
9333 board[toY][toX] = EmptySquare;
9336 if(gameInfo.variant == VariantSChess && promoChar != NULLCHAR && promoChar != '=' && piece != WhitePawn && piece != BlackPawn) {
9337 board[fromY][fromX] = CharToPiece(piece < BlackPawn ? ToUpper(promoChar) : ToLower(promoChar)); // S-Chess gating
9339 if(promoChar == '+') {
9340 /* [HGM] Shogi-style promotions, to piece implied by original (Might overwrite orinary Pawn promotion) */
9341 board[toY][toX] = (ChessSquare) (PROMOTED piece);
9342 } else if(!appData.testLegality && promoChar != NULLCHAR && promoChar != '=') { // without legality testing, unconditionally believe promoChar
9343 board[toY][toX] = CharToPiece(piece < BlackPawn ? ToUpper(promoChar) : ToLower(promoChar));
9345 if((gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand)
9346 && promoChar != NULLCHAR && gameInfo.holdingsSize) {
9347 // [HGM] superchess: take promotion piece out of holdings
9348 int k = PieceToNumber(CharToPiece(ToUpper(promoChar)));
9349 if((int)piece < (int)BlackPawn) { // determine stm from piece color
9350 if(!--board[k][BOARD_WIDTH-2])
9351 board[k][BOARD_WIDTH-1] = EmptySquare;
9353 if(!--board[BOARD_HEIGHT-1-k][1])
9354 board[BOARD_HEIGHT-1-k][0] = EmptySquare;
9360 /* Updates forwardMostMove */
9362 MakeMove (int fromX, int fromY, int toX, int toY, int promoChar)
9364 // forwardMostMove++; // [HGM] bare: moved downstream
9366 (void) CoordsToAlgebraic(boards[forwardMostMove],
9367 PosFlags(forwardMostMove),
9368 fromY, fromX, toY, toX, promoChar,
9369 parseList[forwardMostMove]);
9371 if(serverMoves != NULL) { /* [HGM] write moves on file for broadcasting (should be separate routine, really) */
9372 int timeLeft; static int lastLoadFlag=0; int king, piece;
9373 piece = boards[forwardMostMove][fromY][fromX];
9374 king = piece < (int) BlackPawn ? WhiteKing : BlackKing;
9375 if(gameInfo.variant == VariantKnightmate)
9376 king += (int) WhiteUnicorn - (int) WhiteKing;
9377 if(forwardMostMove == 0) {
9378 if(gameMode == MachinePlaysBlack || gameMode == BeginningOfGame)
9379 fprintf(serverMoves, "%s;", UserName());
9380 else if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b')
9381 fprintf(serverMoves, "%s;", second.tidy);
9382 fprintf(serverMoves, "%s;", first.tidy);
9383 if(gameMode == MachinePlaysWhite)
9384 fprintf(serverMoves, "%s;", UserName());
9385 else if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w')
9386 fprintf(serverMoves, "%s;", second.tidy);
9387 } else fprintf(serverMoves, loadFlag|lastLoadFlag ? ":" : ";");
9388 lastLoadFlag = loadFlag;
9390 fprintf(serverMoves, "%c%c:%c%c", AAA+fromX, ONE+fromY, AAA+toX, ONE+toY);
9391 // print castling suffix
9392 if( toY == fromY && piece == king ) {
9394 fprintf(serverMoves, ":%c%c:%c%c", AAA+BOARD_RGHT-1, ONE+fromY, AAA+toX-1,ONE+toY);
9396 fprintf(serverMoves, ":%c%c:%c%c", AAA+BOARD_LEFT, ONE+fromY, AAA+toX+1,ONE+toY);
9399 if( (boards[forwardMostMove][fromY][fromX] == WhitePawn ||
9400 boards[forwardMostMove][fromY][fromX] == BlackPawn ) &&
9401 boards[forwardMostMove][toY][toX] == EmptySquare
9402 && fromX != toX && fromY != toY)
9403 fprintf(serverMoves, ":%c%c:%c%c", AAA+fromX, ONE+fromY, AAA+toX, ONE+fromY);
9405 if(promoChar != NULLCHAR)
9406 fprintf(serverMoves, ":%c:%c%c", ToLower(promoChar), AAA+toX, ONE+toY);
9408 char buf[MOVE_LEN*2], *p; int len;
9409 fprintf(serverMoves, "/%d/%d",
9410 pvInfoList[forwardMostMove].depth, pvInfoList[forwardMostMove].score);
9411 if(forwardMostMove+1 & 1) timeLeft = whiteTimeRemaining/1000;
9412 else timeLeft = blackTimeRemaining/1000;
9413 fprintf(serverMoves, "/%d", timeLeft);
9414 strncpy(buf, parseList[forwardMostMove], MOVE_LEN*2);
9415 if(p = strchr(buf, '=')) *p = NULLCHAR;
9416 len = strlen(buf); if(len > 1 && buf[len-2] != '-') buf[len-2] = NULLCHAR; // strip to-square
9417 fprintf(serverMoves, "/%s", buf);
9419 fflush(serverMoves);
9422 if (forwardMostMove+1 > framePtr) { // [HGM] vari: do not run into saved variations..
9423 GameEnds(GameUnfinished, _("Game too long; increase MAX_MOVES and recompile"), GE_XBOARD);
9426 UnLoadPV(); // [HGM] pv: if we are looking at a PV, abort this
9427 if (commentList[forwardMostMove+1] != NULL) {
9428 free(commentList[forwardMostMove+1]);
9429 commentList[forwardMostMove+1] = NULL;
9431 CopyBoard(boards[forwardMostMove+1], boards[forwardMostMove]);
9432 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[forwardMostMove+1]);
9433 // forwardMostMove++; // [HGM] bare: moved to after ApplyMove, to make sure clock interrupt finds complete board
9434 SwitchClocks(forwardMostMove+1); // [HGM] race: incrementing move nr inside
9435 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
9436 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
9437 adjustedClock = FALSE;
9438 gameInfo.result = GameUnfinished;
9439 if (gameInfo.resultDetails != NULL) {
9440 free(gameInfo.resultDetails);
9441 gameInfo.resultDetails = NULL;
9443 CoordsToComputerAlgebraic(fromY, fromX, toY, toX, promoChar,
9444 moveList[forwardMostMove - 1]);
9445 switch (MateTest(boards[forwardMostMove], PosFlags(forwardMostMove)) ) {
9451 if(gameInfo.variant != VariantShogi)
9452 strcat(parseList[forwardMostMove - 1], "+");
9456 strcat(parseList[forwardMostMove - 1], "#");
9462 /* Updates currentMove if not pausing */
9464 ShowMove (int fromX, int fromY, int toX, int toY)
9466 int instant = (gameMode == PlayFromGameFile) ?
9467 (matchMode || (appData.timeDelay == 0 && !pausing)) : pausing;
9468 if(appData.noGUI) return;
9469 if (!pausing || gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
9471 if (forwardMostMove == currentMove + 1) {
9472 AnimateMove(boards[forwardMostMove - 1],
9473 fromX, fromY, toX, toY);
9475 if (appData.highlightLastMove) {
9476 SetHighlights(fromX, fromY, toX, toY);
9479 currentMove = forwardMostMove;
9482 if (instant) return;
9484 DisplayMove(currentMove - 1);
9485 DrawPosition(FALSE, boards[currentMove]);
9486 DisplayBothClocks();
9487 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
9491 SendEgtPath (ChessProgramState *cps)
9492 { /* [HGM] EGT: match formats given in feature with those given by user, and send info for each match */
9493 char buf[MSG_SIZ], name[MSG_SIZ], *p;
9495 if((p = cps->egtFormats) == NULL || appData.egtFormats == NULL) return;
9498 char c, *q = name+1, *r, *s;
9500 name[0] = ','; // extract next format name from feature and copy with prefixed ','
9501 while(*p && *p != ',') *q++ = *p++;
9503 if( appData.defaultPathEGTB && appData.defaultPathEGTB[0] &&
9504 strcmp(name, ",nalimov:") == 0 ) {
9505 // take nalimov path from the menu-changeable option first, if it is defined
9506 snprintf(buf, MSG_SIZ, "egtpath nalimov %s\n", appData.defaultPathEGTB);
9507 SendToProgram(buf,cps); // send egtbpath command for nalimov
9509 if( (s = StrStr(appData.egtFormats, name+1)) == appData.egtFormats ||
9510 (s = StrStr(appData.egtFormats, name)) != NULL) {
9511 // format name occurs amongst user-supplied formats, at beginning or immediately after comma
9512 s = r = StrStr(s, ":") + 1; // beginning of path info
9513 while(*r && *r != ',') r++; // path info is everything upto next ';' or end of string
9514 c = *r; *r = 0; // temporarily null-terminate path info
9515 *--q = 0; // strip of trailig ':' from name
9516 snprintf(buf, MSG_SIZ, "egtpath %s %s\n", name+1, s);
9518 SendToProgram(buf,cps); // send egtbpath command for this format
9520 if(*p == ',') p++; // read away comma to position for next format name
9525 InitChessProgram (ChessProgramState *cps, int setup)
9526 /* setup needed to setup FRC opening position */
9528 char buf[MSG_SIZ], b[MSG_SIZ]; int overruled;
9529 if (appData.noChessProgram) return;
9530 hintRequested = FALSE;
9531 bookRequested = FALSE;
9533 ParseFeatures(appData.features[cps == &second], cps); // [HGM] allow user to overrule features
9534 /* [HGM] some new WB protocol commands to configure engine are sent now, if engine supports them */
9535 /* moved to before sending initstring in 4.3.15, so Polyglot can delay UCI 'isready' to recepton of 'new' */
9536 if(cps->memSize) { /* [HGM] memory */
9537 snprintf(buf, MSG_SIZ, "memory %d\n", appData.defaultHashSize + appData.defaultCacheSizeEGTB);
9538 SendToProgram(buf, cps);
9540 SendEgtPath(cps); /* [HGM] EGT */
9541 if(cps->maxCores) { /* [HGM] SMP: (protocol specified must be last settings command before new!) */
9542 snprintf(buf, MSG_SIZ, "cores %d\n", appData.smpCores);
9543 SendToProgram(buf, cps);
9546 SendToProgram(cps->initString, cps);
9547 if (gameInfo.variant != VariantNormal &&
9548 gameInfo.variant != VariantLoadable
9549 /* [HGM] also send variant if board size non-standard */
9550 || gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0
9552 char *v = VariantName(gameInfo.variant);
9553 if (cps->protocolVersion != 1 && StrStr(cps->variants, v) == NULL) {
9554 /* [HGM] in protocol 1 we have to assume all variants valid */
9555 snprintf(buf, MSG_SIZ, _("Variant %s not supported by %s"), v, cps->tidy);
9556 DisplayFatalError(buf, 0, 1);
9560 /* [HGM] make prefix for non-standard board size. Awkward testing... */
9561 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
9562 if( gameInfo.variant == VariantXiangqi )
9563 overruled = gameInfo.boardWidth != 9 || gameInfo.boardHeight != 10 || gameInfo.holdingsSize != 0;
9564 if( gameInfo.variant == VariantShogi )
9565 overruled = gameInfo.boardWidth != 9 || gameInfo.boardHeight != 9 || gameInfo.holdingsSize != 7;
9566 if( gameInfo.variant == VariantBughouse || gameInfo.variant == VariantCrazyhouse )
9567 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 5;
9568 if( gameInfo.variant == VariantCapablanca || gameInfo.variant == VariantCapaRandom ||
9569 gameInfo.variant == VariantGothic || gameInfo.variant == VariantFalcon || gameInfo.variant == VariantJanus )
9570 overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
9571 if( gameInfo.variant == VariantCourier )
9572 overruled = gameInfo.boardWidth != 12 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
9573 if( gameInfo.variant == VariantSuper )
9574 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 8;
9575 if( gameInfo.variant == VariantGreat )
9576 overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 8;
9577 if( gameInfo.variant == VariantSChess )
9578 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 7;
9579 if( gameInfo.variant == VariantGrand )
9580 overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 10 || gameInfo.holdingsSize != 7;
9583 snprintf(b, MSG_SIZ, "%dx%d+%d_%s", gameInfo.boardWidth, gameInfo.boardHeight,
9584 gameInfo.holdingsSize, VariantName(gameInfo.variant)); // cook up sized variant name
9585 /* [HGM] varsize: try first if this defiant size variant is specifically known */
9586 if(StrStr(cps->variants, b) == NULL) {
9587 // specific sized variant not known, check if general sizing allowed
9588 if (cps->protocolVersion != 1) { // for protocol 1 we cannot check and hope for the best
9589 if(StrStr(cps->variants, "boardsize") == NULL) {
9590 snprintf(buf, MSG_SIZ, "Board size %dx%d+%d not supported by %s",
9591 gameInfo.boardWidth, gameInfo.boardHeight, gameInfo.holdingsSize, cps->tidy);
9592 DisplayFatalError(buf, 0, 1);
9595 /* [HGM] here we really should compare with the maximum supported board size */
9598 } else snprintf(b, MSG_SIZ,"%s", VariantName(gameInfo.variant));
9599 snprintf(buf, MSG_SIZ, "variant %s\n", b);
9600 SendToProgram(buf, cps);
9602 currentlyInitializedVariant = gameInfo.variant;
9604 /* [HGM] send opening position in FRC to first engine */
9606 SendToProgram("force\n", cps);
9608 /* engine is now in force mode! Set flag to wake it up after first move. */
9609 setboardSpoiledMachineBlack = 1;
9613 snprintf(buf, sizeof(buf), "ics %s\n", appData.icsActive ? appData.icsHost : "-");
9614 SendToProgram(buf, cps);
9616 cps->maybeThinking = FALSE;
9617 cps->offeredDraw = 0;
9618 if (!appData.icsActive) {
9619 SendTimeControl(cps, movesPerSession, timeControl,
9620 timeIncrement, appData.searchDepth,
9623 if (appData.showThinking
9624 // [HGM] thinking: four options require thinking output to be sent
9625 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp()
9627 SendToProgram("post\n", cps);
9629 SendToProgram("hard\n", cps);
9630 if (!appData.ponderNextMove) {
9631 /* Warning: "easy" is a toggle in GNU Chess, so don't send
9632 it without being sure what state we are in first. "hard"
9633 is not a toggle, so that one is OK.
9635 SendToProgram("easy\n", cps);
9638 snprintf(buf, MSG_SIZ, "ping %d\n", ++cps->lastPing);
9639 SendToProgram(buf, cps);
9641 cps->initDone = TRUE;
9642 ClearEngineOutputPane(cps == &second);
9647 StartChessProgram (ChessProgramState *cps)
9652 if (appData.noChessProgram) return;
9653 cps->initDone = FALSE;
9655 if (strcmp(cps->host, "localhost") == 0) {
9656 err = StartChildProcess(cps->program, cps->dir, &cps->pr);
9657 } else if (*appData.remoteShell == NULLCHAR) {
9658 err = OpenRcmd(cps->host, appData.remoteUser, cps->program, &cps->pr);
9660 if (*appData.remoteUser == NULLCHAR) {
9661 snprintf(buf, sizeof(buf), "%s %s %s", appData.remoteShell, cps->host,
9664 snprintf(buf, sizeof(buf), "%s %s -l %s %s", appData.remoteShell,
9665 cps->host, appData.remoteUser, cps->program);
9667 err = StartChildProcess(buf, "", &cps->pr);
9671 snprintf(buf, MSG_SIZ, _("Startup failure on '%s'"), cps->program);
9672 DisplayError(buf, err); // [HGM] bit of a rough kludge: ignore failure, (which XBoard would do anyway), and let I/O discover it
9673 if(cps != &first) return;
9674 appData.noChessProgram = TRUE;
9677 // DisplayFatalError(buf, err, 1);
9678 // cps->pr = NoProc;
9683 cps->isr = AddInputSource(cps->pr, TRUE, ReceiveFromProgram, cps);
9684 if (cps->protocolVersion > 1) {
9685 snprintf(buf, MSG_SIZ, "xboard\nprotover %d\n", cps->protocolVersion);
9686 cps->nrOptions = 0; // [HGM] options: clear all engine-specific options
9687 cps->comboCnt = 0; // and values of combo boxes
9688 SendToProgram(buf, cps);
9690 SendToProgram("xboard\n", cps);
9695 TwoMachinesEventIfReady P((void))
9697 static int curMess = 0;
9698 if (first.lastPing != first.lastPong) {
9699 if(curMess != 1) DisplayMessage("", _("Waiting for first chess program")); curMess = 1;
9700 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10); // [HGM] fast: lowered from 1000
9703 if (second.lastPing != second.lastPong) {
9704 if(curMess != 2) DisplayMessage("", _("Waiting for second chess program")); curMess = 2;
9705 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10); // [HGM] fast: lowered from 1000
9708 DisplayMessage("", ""); curMess = 0;
9714 MakeName (char *template)
9718 static char buf[MSG_SIZ];
9722 clock = time((time_t *)NULL);
9723 tm = localtime(&clock);
9725 while(*p++ = *template++) if(p[-1] == '%') {
9726 switch(*template++) {
9727 case 0: *p = 0; return buf;
9728 case 'Y': i = tm->tm_year+1900; break;
9729 case 'y': i = tm->tm_year-100; break;
9730 case 'M': i = tm->tm_mon+1; break;
9731 case 'd': i = tm->tm_mday; break;
9732 case 'h': i = tm->tm_hour; break;
9733 case 'm': i = tm->tm_min; break;
9734 case 's': i = tm->tm_sec; break;
9737 snprintf(p-1, MSG_SIZ-10 - (p - buf), "%02d", i); p += strlen(p);
9743 CountPlayers (char *p)
9746 while(p = strchr(p, '\n')) p++, n++; // count participants
9751 WriteTourneyFile (char *results, FILE *f)
9752 { // write tournament parameters on tourneyFile; on success return the stream pointer for closing
9753 if(f == NULL) f = fopen(appData.tourneyFile, "w");
9754 if(f == NULL) DisplayError(_("Could not write on tourney file"), 0); else {
9755 // create a file with tournament description
9756 fprintf(f, "-participants {%s}\n", appData.participants);
9757 fprintf(f, "-seedBase %d\n", appData.seedBase);
9758 fprintf(f, "-tourneyType %d\n", appData.tourneyType);
9759 fprintf(f, "-tourneyCycles %d\n", appData.tourneyCycles);
9760 fprintf(f, "-defaultMatchGames %d\n", appData.defaultMatchGames);
9761 fprintf(f, "-syncAfterRound %s\n", appData.roundSync ? "true" : "false");
9762 fprintf(f, "-syncAfterCycle %s\n", appData.cycleSync ? "true" : "false");
9763 fprintf(f, "-saveGameFile \"%s\"\n", appData.saveGameFile);
9764 fprintf(f, "-loadGameFile \"%s\"\n", appData.loadGameFile);
9765 fprintf(f, "-loadGameIndex %d\n", appData.loadGameIndex);
9766 fprintf(f, "-loadPositionFile \"%s\"\n", appData.loadPositionFile);
9767 fprintf(f, "-loadPositionIndex %d\n", appData.loadPositionIndex);
9768 fprintf(f, "-rewindIndex %d\n", appData.rewindIndex);
9769 fprintf(f, "-discourageOwnBooks %s\n", appData.defNoBook ? "true" : "false");
9771 fprintf(f, "-searchTime \"%d:%02d\"\n", searchTime/60, searchTime%60);
9773 fprintf(f, "-mps %d\n", appData.movesPerSession);
9774 fprintf(f, "-tc %s\n", appData.timeControl);
9775 fprintf(f, "-inc %.2f\n", appData.timeIncrement);
9777 fprintf(f, "-results \"%s\"\n", results);
9782 char *command[MAXENGINES], *mnemonic[MAXENGINES];
9785 Substitute (char *participants, int expunge)
9787 int i, changed, changes=0, nPlayers=0;
9788 char *p, *q, *r, buf[MSG_SIZ];
9789 if(participants == NULL) return;
9790 if(appData.tourneyFile[0] == NULLCHAR) { free(participants); return; }
9791 r = p = participants; q = appData.participants;
9792 while(*p && *p == *q) {
9793 if(*p == '\n') r = p+1, nPlayers++;
9796 if(*p) { // difference
9797 while(*p && *p++ != '\n');
9798 while(*q && *q++ != '\n');
9800 changes = 1 + (strcmp(p, q) != 0);
9802 if(changes == 1) { // a single engine mnemonic was changed
9803 q = r; while(*q) nPlayers += (*q++ == '\n');
9804 p = buf; while(*r && (*p = *r++) != '\n') p++;
9806 NamesToList(firstChessProgramNames, command, mnemonic, "all");
9807 for(i=1; mnemonic[i]; i++) if(!strcmp(buf, mnemonic[i])) break;
9808 if(mnemonic[i]) { // The substitute is valid
9810 if(appData.tourneyFile[0] && (f = fopen(appData.tourneyFile, "r+")) ) {
9811 flock(fileno(f), LOCK_EX);
9812 ParseArgsFromFile(f);
9813 fseek(f, 0, SEEK_SET);
9814 FREE(appData.participants); appData.participants = participants;
9815 if(expunge) { // erase results of replaced engine
9816 int len = strlen(appData.results), w, b, dummy;
9817 for(i=0; i<len; i++) {
9818 Pairing(i, nPlayers, &w, &b, &dummy);
9819 if((w == changed || b == changed) && appData.results[i] == '*') {
9820 DisplayError(_("You cannot replace an engine while it is engaged!\nTerminate its game first."), 0);
9825 for(i=0; i<len; i++) {
9826 Pairing(i, nPlayers, &w, &b, &dummy);
9827 if(w == changed || b == changed) appData.results[i] = ' '; // mark as not played
9830 WriteTourneyFile(appData.results, f);
9831 fclose(f); // release lock
9834 } else DisplayError(_("No engine with the name you gave is installed"), 0);
9836 if(changes == 0) DisplayError(_("First change an engine by editing the participants list\nof the Tournament Options dialog"), 0);
9837 if(changes > 1) DisplayError(_("You can only change one engine at the time"), 0);
9843 CreateTourney (char *name)
9846 if(matchMode && strcmp(name, appData.tourneyFile)) {
9847 ASSIGN(name, appData.tourneyFile); //do not allow change of tourneyfile while playing
9849 if(name[0] == NULLCHAR) {
9850 if(appData.participants[0])
9851 DisplayError(_("You must supply a tournament file,\nfor storing the tourney progress"), 0);
9854 f = fopen(name, "r");
9855 if(f) { // file exists
9856 ASSIGN(appData.tourneyFile, name);
9857 ParseArgsFromFile(f); // parse it
9859 if(!appData.participants[0]) return 0; // ignore tourney file if non-existing & no participants
9860 if(CountPlayers(appData.participants) < (appData.tourneyType>0 ? appData.tourneyType+1 : 2)) {
9861 DisplayError(_("Not enough participants"), 0);
9864 ASSIGN(appData.tourneyFile, name);
9865 if(appData.tourneyType < 0) appData.defaultMatchGames = 1; // Swiss forces games/pairing = 1
9866 if((f = WriteTourneyFile("", NULL)) == NULL) return 0;
9869 appData.noChessProgram = FALSE;
9870 appData.clockMode = TRUE;
9876 NamesToList (char *names, char **engineList, char **engineMnemonic, char *group)
9878 char buf[MSG_SIZ], *p, *q;
9879 int i=1, header, skip, all = !strcmp(group, "all"), depth = 0;
9880 skip = !all && group[0]; // if group requested, we start in skip mode
9881 for(;*names && depth >= 0 && i < MAXENGINES-1; names = p) {
9882 p = names; q = buf; header = 0;
9883 while(*p && *p != '\n') *q++ = *p++;
9887 if(strstr(buf, "# end") == buf) { depth--; continue; } // leave group, and suppress printing label
9888 depth++; // we must be entering a new group
9889 if(all) continue; // suppress printing group headers when complete list requested
9891 if(skip && !strcmp(group, buf)) { depth = 0; skip = FALSE; } // start when we reach requested group
9893 if(depth != header && !all || skip) continue; // skip contents of group (but print first-level header)
9894 if(engineList[i]) free(engineList[i]);
9895 engineList[i] = strdup(buf);
9896 if(buf[0] != '#') TidyProgramName(engineList[i], "localhost", buf); // group headers not tidied
9897 if(engineMnemonic[i]) free(engineMnemonic[i]);
9898 if((q = strstr(engineList[i]+2, "variant")) && q[-2]== ' ' && (q[-1]=='/' || q[-1]=='-') && (q[7]==' ' || q[7]=='=')) {
9900 sscanf(q + 8, "%s", buf + strlen(buf));
9903 engineMnemonic[i] = strdup(buf);
9906 engineList[i] = engineMnemonic[i] = NULL;
9910 // following implemented as macro to avoid type limitations
9911 #define SWAP(item, temp) temp = appData.item[0]; appData.item[0] = appData.item[n]; appData.item[n] = temp;
9915 { // swap settings for first engine and other engine (so far only some selected options)
9920 SWAP(chessProgram, p)
9922 SWAP(hasOwnBookUCI, h)
9923 SWAP(protocolVersion, h)
9925 SWAP(scoreIsAbsolute, h)
9934 SetPlayer (int player, char *p)
9935 { // [HGM] find the engine line of the partcipant given by number, and parse its options.
9937 char buf[MSG_SIZ], *engineName;
9938 for(i=0; i<player; i++) p = strchr(p, '\n') + 1;
9939 engineName = strdup(p); if(p = strchr(engineName, '\n')) *p = NULLCHAR;
9940 for(i=1; command[i]; i++) if(!strcmp(mnemonic[i], engineName)) break;
9942 snprintf(buf, MSG_SIZ, "-fcp %s", command[i]);
9943 ParseArgsFromString(resetOptions); appData.fenOverride[0] = NULL; appData.pvSAN[0] = FALSE;
9944 appData.firstHasOwnBookUCI = !appData.defNoBook; appData.protocolVersion[0] = PROTOVER;
9945 ParseArgsFromString(buf);
9951 char *recentEngines;
9954 RecentEngineEvent (int nr)
9957 // SwapEngines(1); // bump first to second
9958 // ReplaceEngine(&second, 1); // and load it there
9959 NamesToList(firstChessProgramNames, command, mnemonic, "all"); // get mnemonics of installed engines
9960 n = SetPlayer(nr, recentEngines); // select new (using original menu order!)
9961 if(mnemonic[n]) { // if somehow the engine with the selected nickname is no longer found in the list, we skip
9962 ReplaceEngine(&first, 0);
9963 FloatToFront(&appData.recentEngineList, command[n]);
9968 Pairing (int nr, int nPlayers, int *whitePlayer, int *blackPlayer, int *syncInterval)
9969 { // determine players from game number
9970 int curCycle, curRound, curPairing, gamesPerCycle, gamesPerRound, roundsPerCycle=1, pairingsPerRound=1;
9972 if(appData.tourneyType == 0) {
9973 roundsPerCycle = (nPlayers - 1) | 1;
9974 pairingsPerRound = nPlayers / 2;
9975 } else if(appData.tourneyType > 0) {
9976 roundsPerCycle = nPlayers - appData.tourneyType;
9977 pairingsPerRound = appData.tourneyType;
9979 gamesPerRound = pairingsPerRound * appData.defaultMatchGames;
9980 gamesPerCycle = gamesPerRound * roundsPerCycle;
9981 appData.matchGames = gamesPerCycle * appData.tourneyCycles - 1; // fake like all games are one big match
9982 curCycle = nr / gamesPerCycle; nr %= gamesPerCycle;
9983 curRound = nr / gamesPerRound; nr %= gamesPerRound;
9984 curPairing = nr / appData.defaultMatchGames; nr %= appData.defaultMatchGames;
9985 matchGame = nr + curCycle * appData.defaultMatchGames + 1; // fake game nr that loads correct game or position from file
9986 roundNr = (curCycle * roundsPerCycle + curRound) * appData.defaultMatchGames + nr + 1;
9988 if(appData.cycleSync) *syncInterval = gamesPerCycle;
9989 if(appData.roundSync) *syncInterval = gamesPerRound;
9991 if(appData.debugMode) fprintf(debugFP, "cycle=%d, round=%d, pairing=%d curGame=%d\n", curCycle, curRound, curPairing, matchGame);
9993 if(appData.tourneyType == 0) {
9994 if(curPairing == (nPlayers-1)/2 ) {
9995 *whitePlayer = curRound;
9996 *blackPlayer = nPlayers - 1; // this is the 'bye' when nPlayer is odd
9998 *whitePlayer = curRound - (nPlayers-1)/2 + curPairing;
9999 if(*whitePlayer < 0) *whitePlayer += nPlayers-1+(nPlayers&1);
10000 *blackPlayer = curRound + (nPlayers-1)/2 - curPairing;
10001 if(*blackPlayer >= nPlayers-1+(nPlayers&1)) *blackPlayer -= nPlayers-1+(nPlayers&1);
10003 } else if(appData.tourneyType > 1) {
10004 *blackPlayer = curPairing; // in multi-gauntlet, assign gauntlet engines to second, so first an be kept loaded during round
10005 *whitePlayer = curRound + appData.tourneyType;
10006 } else if(appData.tourneyType > 0) {
10007 *whitePlayer = curPairing;
10008 *blackPlayer = curRound + appData.tourneyType;
10011 // take care of white/black alternation per round.
10012 // For cycles and games this is already taken care of by default, derived from matchGame!
10013 return curRound & 1;
10017 NextTourneyGame (int nr, int *swapColors)
10018 { // !!!major kludge!!! fiddle appData settings to get everything in order for next tourney game
10020 int whitePlayer, blackPlayer, firstBusy=1000000000, syncInterval = 0, nPlayers;
10022 if(appData.tourneyFile[0] == NULLCHAR) return 1; // no tourney, always allow next game
10023 tf = fopen(appData.tourneyFile, "r");
10024 if(tf == NULL) { DisplayFatalError(_("Bad tournament file"), 0, 1); return 0; }
10025 ParseArgsFromFile(tf); fclose(tf);
10026 InitTimeControls(); // TC might be altered from tourney file
10028 nPlayers = CountPlayers(appData.participants); // count participants
10029 if(appData.tourneyType < 0) syncInterval = nPlayers/2; else
10030 *swapColors = Pairing(nr<0 ? 0 : nr, nPlayers, &whitePlayer, &blackPlayer, &syncInterval);
10033 p = q = appData.results;
10034 while(*q) if(*q++ == '*' || q[-1] == ' ') { firstBusy = q - p - 1; break; }
10035 if(firstBusy/syncInterval < (nextGame/syncInterval)) {
10036 DisplayMessage(_("Waiting for other game(s)"),"");
10037 waitingForGame = TRUE;
10038 ScheduleDelayedEvent(NextMatchGame, 1000); // wait for all games of previous round to finish
10041 waitingForGame = FALSE;
10044 if(appData.tourneyType < 0) {
10045 if(nr>=0 && !pairingReceived) {
10047 if(pairing.pr == NoProc) {
10048 if(!appData.pairingEngine[0]) {
10049 DisplayFatalError(_("No pairing engine specified"), 0, 1);
10052 StartChessProgram(&pairing); // starts the pairing engine
10054 snprintf(buf, 1<<16, "results %d %s\n", nPlayers, appData.results);
10055 SendToProgram(buf, &pairing);
10056 snprintf(buf, 1<<16, "pairing %d\n", nr+1);
10057 SendToProgram(buf, &pairing);
10058 return 0; // wait for pairing engine to answer (which causes NextTourneyGame to be called again...
10060 pairingReceived = 0; // ... so we continue here
10062 appData.matchGames = appData.tourneyCycles * syncInterval - 1;
10063 whitePlayer = savedWhitePlayer-1; blackPlayer = savedBlackPlayer-1;
10064 matchGame = 1; roundNr = nr / syncInterval + 1;
10067 if(first.pr != NoProc && second.pr != NoProc || nr<0) return 1; // engines already loaded
10069 // redefine engines, engine dir, etc.
10070 NamesToList(firstChessProgramNames, command, mnemonic, "all"); // get mnemonics of installed engines
10071 if(first.pr == NoProc) {
10072 SetPlayer(whitePlayer, appData.participants); // find white player amongst it, and parse its engine line
10073 InitEngine(&first, 0); // initialize ChessProgramStates based on new settings.
10075 if(second.pr == NoProc) {
10077 SetPlayer(blackPlayer, appData.participants); // find black player amongst it, and parse its engine line
10078 SwapEngines(1); // and make that valid for second engine by swapping
10079 InitEngine(&second, 1);
10081 CommonEngineInit(); // after this TwoMachinesEvent will create correct engine processes
10082 UpdateLogos(FALSE); // leave display to ModeHiglight()
10088 { // performs game initialization that does not invoke engines, and then tries to start the game
10089 int res, firstWhite, swapColors = 0;
10090 if(!NextTourneyGame(nextGame, &swapColors)) return; // this sets matchGame, -fcp / -scp and other options for next game, if needed
10091 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
10093 snprintf(buf, MSG_SIZ, appData.nameOfDebugFile, nextGame+1); // expand name of debug file with %d in it
10094 if(strcmp(buf, currentDebugFile)) { // name has changed
10095 FILE *f = fopen(buf, "w");
10096 if(f) { // if opening the new file failed, just keep using the old one
10097 ASSIGN(currentDebugFile, buf);
10101 if(appData.serverFileName) {
10102 if(serverFP) fclose(serverFP);
10103 serverFP = fopen(appData.serverFileName, "w");
10104 if(serverFP && first.pr != NoProc) fprintf(serverFP, "StartChildProcess (dir=\".\") .\\%s\n", first.tidy);
10105 if(serverFP && second.pr != NoProc) fprintf(serverFP, "StartChildProcess (dir=\".\") .\\%s\n", second.tidy);
10109 firstWhite = appData.firstPlaysBlack ^ (matchGame & 1 | appData.sameColorGames > 1); // non-incremental default
10110 firstWhite ^= swapColors; // reverses if NextTourneyGame says we are in an odd round
10111 first.twoMachinesColor = firstWhite ? "white\n" : "black\n"; // perform actual color assignement
10112 second.twoMachinesColor = firstWhite ? "black\n" : "white\n";
10113 appData.noChessProgram = (first.pr == NoProc); // kludge to prevent Reset from starting up chess program
10114 if(appData.loadGameIndex == -2) srandom(appData.seedBase + 68163*(nextGame & ~1)); // deterministic seed to force same opening
10115 Reset(FALSE, first.pr != NoProc);
10116 res = LoadGameOrPosition(matchGame); // setup game
10117 appData.noChessProgram = FALSE; // LoadGameOrPosition might call Reset too!
10118 if(!res) return; // abort when bad game/pos file
10119 TwoMachinesEvent();
10123 UserAdjudicationEvent (int result)
10125 ChessMove gameResult = GameIsDrawn;
10128 gameResult = WhiteWins;
10130 else if( result < 0 ) {
10131 gameResult = BlackWins;
10134 if( gameMode == TwoMachinesPlay ) {
10135 GameEnds( gameResult, "User adjudication", GE_XBOARD );
10140 // [HGM] save: calculate checksum of game to make games easily identifiable
10142 StringCheckSum (char *s)
10145 if(s==NULL) return 0;
10146 while(*s) i = i*259 + *s++;
10154 for(i=backwardMostMove; i<forwardMostMove; i++) {
10155 sum += pvInfoList[i].depth;
10156 sum += StringCheckSum(parseList[i]);
10157 sum += StringCheckSum(commentList[i]);
10160 if(i>1 && sum==0) sum++; // make sure never zero for non-empty game
10161 return sum + StringCheckSum(commentList[i]);
10162 } // end of save patch
10165 GameEnds (ChessMove result, char *resultDetails, int whosays)
10167 GameMode nextGameMode;
10169 char buf[MSG_SIZ], popupRequested = 0, *ranking = NULL;
10171 if(endingGame) return; /* [HGM] crash: forbid recursion */
10173 if(twoBoards) { // [HGM] dual: switch back to one board
10174 twoBoards = partnerUp = 0; InitDrawingSizes(-2, 0);
10175 DrawPosition(TRUE, partnerBoard); // observed game becomes foreground
10177 if (appData.debugMode) {
10178 fprintf(debugFP, "GameEnds(%d, %s, %d)\n",
10179 result, resultDetails ? resultDetails : "(null)", whosays);
10182 fromX = fromY = -1; // [HGM] abort any move the user is entering.
10184 if (appData.icsActive && (whosays == GE_ENGINE || whosays >= GE_ENGINE1)) {
10185 /* If we are playing on ICS, the server decides when the
10186 game is over, but the engine can offer to draw, claim
10190 if (appData.zippyPlay && first.initDone) {
10191 if (result == GameIsDrawn) {
10192 /* In case draw still needs to be claimed */
10193 SendToICS(ics_prefix);
10194 SendToICS("draw\n");
10195 } else if (StrCaseStr(resultDetails, "resign")) {
10196 SendToICS(ics_prefix);
10197 SendToICS("resign\n");
10201 endingGame = 0; /* [HGM] crash */
10205 /* If we're loading the game from a file, stop */
10206 if (whosays == GE_FILE) {
10207 (void) StopLoadGameTimer();
10211 /* Cancel draw offers */
10212 first.offeredDraw = second.offeredDraw = 0;
10214 /* If this is an ICS game, only ICS can really say it's done;
10215 if not, anyone can. */
10216 isIcsGame = (gameMode == IcsPlayingWhite ||
10217 gameMode == IcsPlayingBlack ||
10218 gameMode == IcsObserving ||
10219 gameMode == IcsExamining);
10221 if (!isIcsGame || whosays == GE_ICS) {
10222 /* OK -- not an ICS game, or ICS said it was done */
10224 if (!isIcsGame && !appData.noChessProgram)
10225 SetUserThinkingEnables();
10227 /* [HGM] if a machine claims the game end we verify this claim */
10228 if(gameMode == TwoMachinesPlay && appData.testClaims) {
10229 if(appData.testLegality && whosays >= GE_ENGINE1 ) {
10231 ChessMove trueResult = (ChessMove) -1;
10233 claimer = whosays == GE_ENGINE1 ? /* color of claimer */
10234 first.twoMachinesColor[0] :
10235 second.twoMachinesColor[0] ;
10237 // [HGM] losers: because the logic is becoming a bit hairy, determine true result first
10238 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_CHECKMATE) {
10239 /* [HGM] verify: engine mate claims accepted if they were flagged */
10240 trueResult = WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins;
10242 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_WINS) { // added code for games where being mated is a win
10243 /* [HGM] verify: engine mate claims accepted if they were flagged */
10244 trueResult = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
10246 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_STALEMATE) { // only used to indicate draws now
10247 trueResult = GameIsDrawn; // default; in variants where stalemate loses, Status is CHECKMATE
10250 // now verify win claims, but not in drop games, as we don't understand those yet
10251 if( (gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper
10252 || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand) &&
10253 (result == WhiteWins && claimer == 'w' ||
10254 result == BlackWins && claimer == 'b' ) ) { // case to verify: engine claims own win
10255 if (appData.debugMode) {
10256 fprintf(debugFP, "result=%d sp=%d move=%d\n",
10257 result, (signed char)boards[forwardMostMove][EP_STATUS], forwardMostMove);
10259 if(result != trueResult) {
10260 snprintf(buf, MSG_SIZ, "False win claim: '%s'", resultDetails);
10261 result = claimer == 'w' ? BlackWins : WhiteWins;
10262 resultDetails = buf;
10265 if( result == GameIsDrawn && (signed char)boards[forwardMostMove][EP_STATUS] > EP_DRAWS
10266 && (forwardMostMove <= backwardMostMove ||
10267 (signed char)boards[forwardMostMove-1][EP_STATUS] > EP_DRAWS ||
10268 (claimer=='b')==(forwardMostMove&1))
10270 /* [HGM] verify: draws that were not flagged are false claims */
10271 snprintf(buf, MSG_SIZ, "False draw claim: '%s'", resultDetails);
10272 result = claimer == 'w' ? BlackWins : WhiteWins;
10273 resultDetails = buf;
10275 /* (Claiming a loss is accepted no questions asked!) */
10277 /* [HGM] bare: don't allow bare King to win */
10278 if((gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper
10279 || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand)
10280 && gameInfo.variant != VariantLosers && gameInfo.variant != VariantGiveaway
10281 && gameInfo.variant != VariantSuicide // [HGM] losers: except in losers, of course...
10282 && result != GameIsDrawn)
10283 { int i, j, k=0, color = (result==WhiteWins ? (int)WhitePawn : (int)BlackPawn);
10284 for(j=BOARD_LEFT; j<BOARD_RGHT; j++) for(i=0; i<BOARD_HEIGHT; i++) {
10285 int p = (signed char)boards[forwardMostMove][i][j] - color;
10286 if(p >= 0 && p <= (int)WhiteKing) k++;
10288 if (appData.debugMode) {
10289 fprintf(debugFP, "GE(%d, %s, %d) bare king k=%d color=%d\n",
10290 result, resultDetails ? resultDetails : "(null)", whosays, k, color);
10293 result = GameIsDrawn;
10294 snprintf(buf, MSG_SIZ, "%s but bare king", resultDetails);
10295 resultDetails = buf;
10301 if(serverMoves != NULL && !loadFlag) { char c = '=';
10302 if(result==WhiteWins) c = '+';
10303 if(result==BlackWins) c = '-';
10304 if(resultDetails != NULL)
10305 fprintf(serverMoves, ";%c;%s\n", c, resultDetails), fflush(serverMoves);
10307 if (resultDetails != NULL) {
10308 gameInfo.result = result;
10309 gameInfo.resultDetails = StrSave(resultDetails);
10311 /* display last move only if game was not loaded from file */
10312 if ((whosays != GE_FILE) && (currentMove == forwardMostMove))
10313 DisplayMove(currentMove - 1);
10315 if (forwardMostMove != 0) {
10316 if (gameMode != PlayFromGameFile && gameMode != EditGame
10317 && lastSavedGame != GameCheckSum() // [HGM] save: suppress duplicates
10319 if (*appData.saveGameFile != NULLCHAR) {
10320 SaveGameToFile(appData.saveGameFile, TRUE);
10321 } else if (appData.autoSaveGames) {
10324 if (*appData.savePositionFile != NULLCHAR) {
10325 SavePositionToFile(appData.savePositionFile);
10330 /* Tell program how game ended in case it is learning */
10331 /* [HGM] Moved this to after saving the PGN, just in case */
10332 /* engine died and we got here through time loss. In that */
10333 /* case we will get a fatal error writing the pipe, which */
10334 /* would otherwise lose us the PGN. */
10335 /* [HGM] crash: not needed anymore, but doesn't hurt; */
10336 /* output during GameEnds should never be fatal anymore */
10337 if (gameMode == MachinePlaysWhite ||
10338 gameMode == MachinePlaysBlack ||
10339 gameMode == TwoMachinesPlay ||
10340 gameMode == IcsPlayingWhite ||
10341 gameMode == IcsPlayingBlack ||
10342 gameMode == BeginningOfGame) {
10344 snprintf(buf, MSG_SIZ, "result %s {%s}\n", PGNResult(result),
10346 if (first.pr != NoProc) {
10347 SendToProgram(buf, &first);
10349 if (second.pr != NoProc &&
10350 gameMode == TwoMachinesPlay) {
10351 SendToProgram(buf, &second);
10356 if (appData.icsActive) {
10357 if (appData.quietPlay &&
10358 (gameMode == IcsPlayingWhite ||
10359 gameMode == IcsPlayingBlack)) {
10360 SendToICS(ics_prefix);
10361 SendToICS("set shout 1\n");
10363 nextGameMode = IcsIdle;
10364 ics_user_moved = FALSE;
10365 /* clean up premove. It's ugly when the game has ended and the
10366 * premove highlights are still on the board.
10369 gotPremove = FALSE;
10370 ClearPremoveHighlights();
10371 DrawPosition(FALSE, boards[currentMove]);
10373 if (whosays == GE_ICS) {
10376 if (gameMode == IcsPlayingWhite)
10378 else if(gameMode == IcsPlayingBlack)
10379 PlayIcsLossSound();
10382 if (gameMode == IcsPlayingBlack)
10384 else if(gameMode == IcsPlayingWhite)
10385 PlayIcsLossSound();
10388 PlayIcsDrawSound();
10391 PlayIcsUnfinishedSound();
10394 } else if (gameMode == EditGame ||
10395 gameMode == PlayFromGameFile ||
10396 gameMode == AnalyzeMode ||
10397 gameMode == AnalyzeFile) {
10398 nextGameMode = gameMode;
10400 nextGameMode = EndOfGame;
10405 nextGameMode = gameMode;
10408 if (appData.noChessProgram) {
10409 gameMode = nextGameMode;
10411 endingGame = 0; /* [HGM] crash */
10416 /* Put first chess program into idle state */
10417 if (first.pr != NoProc &&
10418 (gameMode == MachinePlaysWhite ||
10419 gameMode == MachinePlaysBlack ||
10420 gameMode == TwoMachinesPlay ||
10421 gameMode == IcsPlayingWhite ||
10422 gameMode == IcsPlayingBlack ||
10423 gameMode == BeginningOfGame)) {
10424 SendToProgram("force\n", &first);
10425 if (first.usePing) {
10427 snprintf(buf, MSG_SIZ, "ping %d\n", ++first.lastPing);
10428 SendToProgram(buf, &first);
10431 } else if (result != GameUnfinished || nextGameMode == IcsIdle) {
10432 /* Kill off first chess program */
10433 if (first.isr != NULL)
10434 RemoveInputSource(first.isr);
10437 if (first.pr != NoProc) {
10439 DoSleep( appData.delayBeforeQuit );
10440 SendToProgram("quit\n", &first);
10441 DoSleep( appData.delayAfterQuit );
10442 DestroyChildProcess(first.pr, first.useSigterm);
10446 if (second.reuse) {
10447 /* Put second chess program into idle state */
10448 if (second.pr != NoProc &&
10449 gameMode == TwoMachinesPlay) {
10450 SendToProgram("force\n", &second);
10451 if (second.usePing) {
10453 snprintf(buf, MSG_SIZ, "ping %d\n", ++second.lastPing);
10454 SendToProgram(buf, &second);
10457 } else if (result != GameUnfinished || nextGameMode == IcsIdle) {
10458 /* Kill off second chess program */
10459 if (second.isr != NULL)
10460 RemoveInputSource(second.isr);
10463 if (second.pr != NoProc) {
10464 DoSleep( appData.delayBeforeQuit );
10465 SendToProgram("quit\n", &second);
10466 DoSleep( appData.delayAfterQuit );
10467 DestroyChildProcess(second.pr, second.useSigterm);
10469 second.pr = NoProc;
10472 if (matchMode && (gameMode == TwoMachinesPlay || waitingForGame && exiting)) {
10473 char resChar = '=';
10477 if (first.twoMachinesColor[0] == 'w') {
10480 second.matchWins++;
10485 if (first.twoMachinesColor[0] == 'b') {
10488 second.matchWins++;
10491 case GameUnfinished:
10497 if(waitingForGame) resChar = ' '; // quit while waiting for round sync: unreserve already reserved game
10498 if(appData.tourneyFile[0]){ // [HGM] we are in a tourney; update tourney file with game result
10499 if(appData.afterGame && appData.afterGame[0]) RunCommand(appData.afterGame);
10500 ReserveGame(nextGame, resChar); // sets nextGame
10501 if(nextGame > appData.matchGames) appData.tourneyFile[0] = 0, ranking = TourneyStandings(3); // tourney is done
10502 else ranking = strdup("busy"); //suppress popup when aborted but not finished
10503 } else roundNr = nextGame = matchGame + 1; // normal match, just increment; round equals matchGame
10505 if (nextGame <= appData.matchGames && !abortMatch) {
10506 gameMode = nextGameMode;
10507 matchGame = nextGame; // this will be overruled in tourney mode!
10508 GetTimeMark(&pauseStart); // [HGM] matchpause: stipulate a pause
10509 ScheduleDelayedEvent(NextMatchGame, 10); // but start game immediately (as it will wait out the pause itself)
10510 endingGame = 0; /* [HGM] crash */
10513 gameMode = nextGameMode;
10514 snprintf(buf, MSG_SIZ, _("Match %s vs. %s: final score %d-%d-%d"),
10515 first.tidy, second.tidy,
10516 first.matchWins, second.matchWins,
10517 appData.matchGames - (first.matchWins + second.matchWins));
10518 if(!appData.tourneyFile[0]) matchGame++, DisplayTwoMachinesTitle(); // [HGM] update result in window title
10519 if(ranking && strcmp(ranking, "busy") && appData.afterTourney && appData.afterTourney[0]) RunCommand(appData.afterTourney);
10520 popupRequested++; // [HGM] crash: postpone to after resetting endingGame
10521 if (appData.firstPlaysBlack) { // [HGM] match: back to original for next match
10522 first.twoMachinesColor = "black\n";
10523 second.twoMachinesColor = "white\n";
10525 first.twoMachinesColor = "white\n";
10526 second.twoMachinesColor = "black\n";
10530 if ((gameMode == AnalyzeMode || gameMode == AnalyzeFile) &&
10531 !(nextGameMode == AnalyzeMode || nextGameMode == AnalyzeFile))
10533 gameMode = nextGameMode;
10535 endingGame = 0; /* [HGM] crash */
10536 if(popupRequested) { // [HGM] crash: this calls GameEnds recursively through ExitEvent! Make it a harmless tail recursion.
10537 if(matchMode == TRUE) { // match through command line: exit with or without popup
10539 ToNrEvent(forwardMostMove);
10540 if(strcmp(ranking, "busy")) DisplayFatalError(ranking, 0, 0);
10542 } else DisplayFatalError(buf, 0, 0);
10543 } else { // match through menu; just stop, with or without popup
10544 matchMode = FALSE; appData.matchGames = matchGame = roundNr = 0;
10547 if(strcmp(ranking, "busy")) DisplayNote(ranking);
10548 } else DisplayNote(buf);
10550 if(ranking) free(ranking);
10554 /* Assumes program was just initialized (initString sent).
10555 Leaves program in force mode. */
10557 FeedMovesToProgram (ChessProgramState *cps, int upto)
10561 if (appData.debugMode)
10562 fprintf(debugFP, "Feeding %smoves %d through %d to %s chess program\n",
10563 startedFromSetupPosition ? "position and " : "",
10564 backwardMostMove, upto, cps->which);
10565 if(currentlyInitializedVariant != gameInfo.variant) {
10567 // [HGM] variantswitch: make engine aware of new variant
10568 if(cps->protocolVersion > 1 && StrStr(cps->variants, VariantName(gameInfo.variant)) == NULL)
10569 return; // [HGM] refrain from feeding moves altogether if variant is unsupported!
10570 snprintf(buf, MSG_SIZ, "variant %s\n", VariantName(gameInfo.variant));
10571 SendToProgram(buf, cps);
10572 currentlyInitializedVariant = gameInfo.variant;
10574 SendToProgram("force\n", cps);
10575 if (startedFromSetupPosition) {
10576 SendBoard(cps, backwardMostMove);
10577 if (appData.debugMode) {
10578 fprintf(debugFP, "feedMoves\n");
10581 for (i = backwardMostMove; i < upto; i++) {
10582 SendMoveToProgram(i, cps);
10588 ResurrectChessProgram ()
10590 /* The chess program may have exited.
10591 If so, restart it and feed it all the moves made so far. */
10592 static int doInit = 0;
10594 if (appData.noChessProgram) return 1;
10596 if(matchMode && appData.tourneyFile[0]) { // [HGM] tourney: make sure we get features after engine replacement. (Should we always do this?)
10597 if(WaitForEngine(&first, TwoMachinesEventIfReady)) { doInit = 1; return 0; } // request to do init on next visit
10598 if(!doInit) return 1; // this replaces testing first.pr != NoProc, which is true when we get here, but first time no reason to abort
10599 doInit = 0; // we fell through (first time after starting the engine); make sure it doesn't happen again
10601 if (first.pr != NoProc) return 1;
10602 StartChessProgram(&first);
10604 InitChessProgram(&first, FALSE);
10605 FeedMovesToProgram(&first, currentMove);
10607 if (!first.sendTime) {
10608 /* can't tell gnuchess what its clock should read,
10609 so we bow to its notion. */
10611 timeRemaining[0][currentMove] = whiteTimeRemaining;
10612 timeRemaining[1][currentMove] = blackTimeRemaining;
10615 if ((gameMode == AnalyzeMode || gameMode == AnalyzeFile ||
10616 appData.icsEngineAnalyze) && first.analysisSupport) {
10617 SendToProgram("analyze\n", &first);
10618 first.analyzing = TRUE;
10624 * Button procedures
10627 Reset (int redraw, int init)
10631 if (appData.debugMode) {
10632 fprintf(debugFP, "Reset(%d, %d) from gameMode %d\n",
10633 redraw, init, gameMode);
10635 CleanupTail(); // [HGM] vari: delete any stored variations
10636 CommentPopDown(); // [HGM] make sure no comments to the previous game keep hanging on
10637 pausing = pauseExamInvalid = FALSE;
10638 startedFromSetupPosition = blackPlaysFirst = FALSE;
10640 whiteFlag = blackFlag = FALSE;
10641 userOfferedDraw = FALSE;
10642 hintRequested = bookRequested = FALSE;
10643 first.maybeThinking = FALSE;
10644 second.maybeThinking = FALSE;
10645 first.bookSuspend = FALSE; // [HGM] book
10646 second.bookSuspend = FALSE;
10647 thinkOutput[0] = NULLCHAR;
10648 lastHint[0] = NULLCHAR;
10649 ClearGameInfo(&gameInfo);
10650 gameInfo.variant = StringToVariant(appData.variant);
10651 ics_user_moved = ics_clock_paused = FALSE;
10652 ics_getting_history = H_FALSE;
10654 white_holding[0] = black_holding[0] = NULLCHAR;
10655 ClearProgramStats();
10656 opponentKibitzes = FALSE; // [HGM] kibitz: do not reserve space in engine-output window in zippy mode
10660 flipView = appData.flipView;
10661 ClearPremoveHighlights();
10662 gotPremove = FALSE;
10663 alarmSounded = FALSE;
10665 GameEnds(EndOfFile, NULL, GE_PLAYER);
10666 if(appData.serverMovesName != NULL) {
10667 /* [HGM] prepare to make moves file for broadcasting */
10668 clock_t t = clock();
10669 if(serverMoves != NULL) fclose(serverMoves);
10670 serverMoves = fopen(appData.serverMovesName, "r");
10671 if(serverMoves != NULL) {
10672 fclose(serverMoves);
10673 /* delay 15 sec before overwriting, so all clients can see end */
10674 while(clock()-t < appData.serverPause*CLOCKS_PER_SEC);
10676 serverMoves = fopen(appData.serverMovesName, "w");
10680 gameMode = BeginningOfGame;
10682 if(appData.icsActive) gameInfo.variant = VariantNormal;
10683 currentMove = forwardMostMove = backwardMostMove = 0;
10684 MarkTargetSquares(1);
10685 InitPosition(redraw);
10686 for (i = 0; i < MAX_MOVES; i++) {
10687 if (commentList[i] != NULL) {
10688 free(commentList[i]);
10689 commentList[i] = NULL;
10693 timeRemaining[0][0] = whiteTimeRemaining;
10694 timeRemaining[1][0] = blackTimeRemaining;
10696 if (first.pr == NoProc) {
10697 StartChessProgram(&first);
10700 InitChessProgram(&first, startedFromSetupPosition);
10703 DisplayMessage("", "");
10704 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
10705 lastSavedGame = 0; // [HGM] save: make sure next game counts as unsaved
10709 AutoPlayGameLoop ()
10712 if (!AutoPlayOneMove())
10714 if (matchMode || appData.timeDelay == 0)
10716 if (appData.timeDelay < 0)
10718 StartLoadGameTimer((long)(1000.0 * appData.timeDelay));
10727 int fromX, fromY, toX, toY;
10729 if (appData.debugMode) {
10730 fprintf(debugFP, "AutoPlayOneMove(): current %d\n", currentMove);
10733 if (gameMode != PlayFromGameFile && gameMode != AnalyzeFile)
10736 if (gameMode == AnalyzeFile && currentMove > backwardMostMove) {
10737 pvInfoList[currentMove].depth = programStats.depth;
10738 pvInfoList[currentMove].score = programStats.score;
10739 pvInfoList[currentMove].time = 0;
10740 if(currentMove < forwardMostMove) AppendComment(currentMove+1, lastPV[0], 2);
10743 if (currentMove >= forwardMostMove) {
10744 if(gameMode == AnalyzeFile) { ExitAnalyzeMode(); SendToProgram("force\n", &first); }
10745 // gameMode = EndOfGame;
10746 // ModeHighlight();
10748 /* [AS] Clear current move marker at the end of a game */
10749 /* HistorySet(parseList, backwardMostMove, forwardMostMove, -1); */
10754 toX = moveList[currentMove][2] - AAA;
10755 toY = moveList[currentMove][3] - ONE;
10757 if (moveList[currentMove][1] == '@') {
10758 if (appData.highlightLastMove) {
10759 SetHighlights(-1, -1, toX, toY);
10762 fromX = moveList[currentMove][0] - AAA;
10763 fromY = moveList[currentMove][1] - ONE;
10765 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove); /* [AS] */
10767 AnimateMove(boards[currentMove], fromX, fromY, toX, toY);
10769 if (appData.highlightLastMove) {
10770 SetHighlights(fromX, fromY, toX, toY);
10773 DisplayMove(currentMove);
10774 SendMoveToProgram(currentMove++, &first);
10775 DisplayBothClocks();
10776 DrawPosition(FALSE, boards[currentMove]);
10777 // [HGM] PV info: always display, routine tests if empty
10778 DisplayComment(currentMove - 1, commentList[currentMove]);
10784 LoadGameOneMove (ChessMove readAhead)
10786 int fromX = 0, fromY = 0, toX = 0, toY = 0, done;
10787 char promoChar = NULLCHAR;
10788 ChessMove moveType;
10789 char move[MSG_SIZ];
10792 if (gameMode != PlayFromGameFile && gameMode != AnalyzeFile &&
10793 gameMode != AnalyzeMode && gameMode != Training) {
10798 yyboardindex = forwardMostMove;
10799 if (readAhead != EndOfFile) {
10800 moveType = readAhead;
10802 if (gameFileFP == NULL)
10804 moveType = (ChessMove) Myylex();
10808 switch (moveType) {
10810 if (appData.debugMode)
10811 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
10814 /* append the comment but don't display it */
10815 AppendComment(currentMove, p, FALSE);
10818 case WhiteCapturesEnPassant:
10819 case BlackCapturesEnPassant:
10820 case WhitePromotion:
10821 case BlackPromotion:
10822 case WhiteNonPromotion:
10823 case BlackNonPromotion:
10825 case WhiteKingSideCastle:
10826 case WhiteQueenSideCastle:
10827 case BlackKingSideCastle:
10828 case BlackQueenSideCastle:
10829 case WhiteKingSideCastleWild:
10830 case WhiteQueenSideCastleWild:
10831 case BlackKingSideCastleWild:
10832 case BlackQueenSideCastleWild:
10834 case WhiteHSideCastleFR:
10835 case WhiteASideCastleFR:
10836 case BlackHSideCastleFR:
10837 case BlackASideCastleFR:
10839 if (appData.debugMode)
10840 fprintf(debugFP, "Parsed %s into %s\n", yy_text, currentMoveString);
10841 fromX = currentMoveString[0] - AAA;
10842 fromY = currentMoveString[1] - ONE;
10843 toX = currentMoveString[2] - AAA;
10844 toY = currentMoveString[3] - ONE;
10845 promoChar = currentMoveString[4];
10850 if (appData.debugMode)
10851 fprintf(debugFP, "Parsed %s into %s\n", yy_text, currentMoveString);
10852 fromX = moveType == WhiteDrop ?
10853 (int) CharToPiece(ToUpper(currentMoveString[0])) :
10854 (int) CharToPiece(ToLower(currentMoveString[0]));
10856 toX = currentMoveString[2] - AAA;
10857 toY = currentMoveString[3] - ONE;
10863 case GameUnfinished:
10864 if (appData.debugMode)
10865 fprintf(debugFP, "Parsed game end: %s\n", yy_text);
10866 p = strchr(yy_text, '{');
10867 if (p == NULL) p = strchr(yy_text, '(');
10870 if (p[0] == '0' || p[0] == '1' || p[0] == '*') p = "";
10872 q = strchr(p, *p == '{' ? '}' : ')');
10873 if (q != NULL) *q = NULLCHAR;
10876 while(q = strchr(p, '\n')) *q = ' '; // [HGM] crush linefeeds in result message
10877 GameEnds(moveType, p, GE_FILE);
10879 if (cmailMsgLoaded) {
10881 flipView = WhiteOnMove(currentMove);
10882 if (moveType == GameUnfinished) flipView = !flipView;
10883 if (appData.debugMode)
10884 fprintf(debugFP, "Setting flipView to %d\n", flipView) ;
10889 if (appData.debugMode)
10890 fprintf(debugFP, "Parser hit end of file\n");
10891 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
10897 if (WhiteOnMove(currentMove)) {
10898 GameEnds(BlackWins, "Black mates", GE_FILE);
10900 GameEnds(WhiteWins, "White mates", GE_FILE);
10904 GameEnds(GameIsDrawn, "Stalemate", GE_FILE);
10910 case MoveNumberOne:
10911 if (lastLoadGameStart == GNUChessGame) {
10912 /* GNUChessGames have numbers, but they aren't move numbers */
10913 if (appData.debugMode)
10914 fprintf(debugFP, "Parser ignoring: '%s' (%d)\n",
10915 yy_text, (int) moveType);
10916 return LoadGameOneMove(EndOfFile); /* tail recursion */
10918 /* else fall thru */
10923 /* Reached start of next game in file */
10924 if (appData.debugMode)
10925 fprintf(debugFP, "Parsed start of next game: %s\n", yy_text);
10926 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
10932 if (WhiteOnMove(currentMove)) {
10933 GameEnds(BlackWins, "Black mates", GE_FILE);
10935 GameEnds(WhiteWins, "White mates", GE_FILE);
10939 GameEnds(GameIsDrawn, "Stalemate", GE_FILE);
10945 case PositionDiagram: /* should not happen; ignore */
10946 case ElapsedTime: /* ignore */
10947 case NAG: /* ignore */
10948 if (appData.debugMode)
10949 fprintf(debugFP, "Parser ignoring: '%s' (%d)\n",
10950 yy_text, (int) moveType);
10951 return LoadGameOneMove(EndOfFile); /* tail recursion */
10954 if (appData.testLegality) {
10955 if (appData.debugMode)
10956 fprintf(debugFP, "Parsed IllegalMove: %s\n", yy_text);
10957 snprintf(move, MSG_SIZ, _("Illegal move: %d.%s%s"),
10958 (forwardMostMove / 2) + 1,
10959 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
10960 DisplayError(move, 0);
10963 if (appData.debugMode)
10964 fprintf(debugFP, "Parsed %s into IllegalMove %s\n",
10965 yy_text, currentMoveString);
10966 fromX = currentMoveString[0] - AAA;
10967 fromY = currentMoveString[1] - ONE;
10968 toX = currentMoveString[2] - AAA;
10969 toY = currentMoveString[3] - ONE;
10970 promoChar = currentMoveString[4];
10974 case AmbiguousMove:
10975 if (appData.debugMode)
10976 fprintf(debugFP, "Parsed AmbiguousMove: %s\n", yy_text);
10977 snprintf(move, MSG_SIZ, _("Ambiguous move: %d.%s%s"),
10978 (forwardMostMove / 2) + 1,
10979 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
10980 DisplayError(move, 0);
10985 case ImpossibleMove:
10986 if (appData.debugMode)
10987 fprintf(debugFP, "Parsed ImpossibleMove (type = %d): %s\n", moveType, yy_text);
10988 snprintf(move, MSG_SIZ, _("Illegal move: %d.%s%s"),
10989 (forwardMostMove / 2) + 1,
10990 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
10991 DisplayError(move, 0);
10997 if (appData.matchMode || (appData.timeDelay == 0 && !pausing)) {
10998 DrawPosition(FALSE, boards[currentMove]);
10999 DisplayBothClocks();
11000 if (!appData.matchMode) // [HGM] PV info: routine tests if empty
11001 DisplayComment(currentMove - 1, commentList[currentMove]);
11003 (void) StopLoadGameTimer();
11005 cmailOldMove = forwardMostMove;
11008 /* currentMoveString is set as a side-effect of yylex */
11010 thinkOutput[0] = NULLCHAR;
11011 MakeMove(fromX, fromY, toX, toY, promoChar);
11012 currentMove = forwardMostMove;
11017 /* Load the nth game from the given file */
11019 LoadGameFromFile (char *filename, int n, char *title, int useList)
11024 if (strcmp(filename, "-") == 0) {
11028 f = fopen(filename, "rb");
11030 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
11031 DisplayError(buf, errno);
11035 if (fseek(f, 0, 0) == -1) {
11036 /* f is not seekable; probably a pipe */
11039 if (useList && n == 0) {
11040 int error = GameListBuild(f);
11042 DisplayError(_("Cannot build game list"), error);
11043 } else if (!ListEmpty(&gameList) &&
11044 ((ListGame *) gameList.tailPred)->number > 1) {
11045 GameListPopUp(f, title);
11052 return LoadGame(f, n, title, FALSE);
11057 MakeRegisteredMove ()
11059 int fromX, fromY, toX, toY;
11061 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
11062 switch (cmailMoveType[lastLoadGameNumber - 1]) {
11065 if (appData.debugMode)
11066 fprintf(debugFP, "Restoring %s for game %d\n",
11067 cmailMove[lastLoadGameNumber - 1], lastLoadGameNumber);
11069 thinkOutput[0] = NULLCHAR;
11070 safeStrCpy(moveList[currentMove], cmailMove[lastLoadGameNumber - 1], sizeof(moveList[currentMove])/sizeof(moveList[currentMove][0]));
11071 fromX = cmailMove[lastLoadGameNumber - 1][0] - AAA;
11072 fromY = cmailMove[lastLoadGameNumber - 1][1] - ONE;
11073 toX = cmailMove[lastLoadGameNumber - 1][2] - AAA;
11074 toY = cmailMove[lastLoadGameNumber - 1][3] - ONE;
11075 promoChar = cmailMove[lastLoadGameNumber - 1][4];
11076 MakeMove(fromX, fromY, toX, toY, promoChar);
11077 ShowMove(fromX, fromY, toX, toY);
11079 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
11086 if (WhiteOnMove(currentMove)) {
11087 GameEnds(BlackWins, "Black mates", GE_PLAYER);
11089 GameEnds(WhiteWins, "White mates", GE_PLAYER);
11094 GameEnds(GameIsDrawn, "Stalemate", GE_PLAYER);
11101 if (WhiteOnMove(currentMove)) {
11102 GameEnds(BlackWins, "White resigns", GE_PLAYER);
11104 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
11109 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
11120 /* Wrapper around LoadGame for use when a Cmail message is loaded */
11122 CmailLoadGame (FILE *f, int gameNumber, char *title, int useList)
11126 if (gameNumber > nCmailGames) {
11127 DisplayError(_("No more games in this message"), 0);
11130 if (f == lastLoadGameFP) {
11131 int offset = gameNumber - lastLoadGameNumber;
11133 cmailMsg[0] = NULLCHAR;
11134 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
11135 cmailMoveRegistered[lastLoadGameNumber - 1] = FALSE;
11136 nCmailMovesRegistered--;
11138 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
11139 if (cmailResult[lastLoadGameNumber - 1] == CMAIL_NEW_RESULT) {
11140 cmailResult[lastLoadGameNumber - 1] = CMAIL_NOT_RESULT;
11143 if (! RegisterMove()) return FALSE;
11147 retVal = LoadGame(f, gameNumber, title, useList);
11149 /* Make move registered during previous look at this game, if any */
11150 MakeRegisteredMove();
11152 if (cmailCommentList[lastLoadGameNumber - 1] != NULL) {
11153 commentList[currentMove]
11154 = StrSave(cmailCommentList[lastLoadGameNumber - 1]);
11155 DisplayComment(currentMove - 1, commentList[currentMove]);
11161 /* Support for LoadNextGame, LoadPreviousGame, ReloadSameGame */
11163 ReloadGame (int offset)
11165 int gameNumber = lastLoadGameNumber + offset;
11166 if (lastLoadGameFP == NULL) {
11167 DisplayError(_("No game has been loaded yet"), 0);
11170 if (gameNumber <= 0) {
11171 DisplayError(_("Can't back up any further"), 0);
11174 if (cmailMsgLoaded) {
11175 return CmailLoadGame(lastLoadGameFP, gameNumber,
11176 lastLoadGameTitle, lastLoadGameUseList);
11178 return LoadGame(lastLoadGameFP, gameNumber,
11179 lastLoadGameTitle, lastLoadGameUseList);
11183 int keys[EmptySquare+1];
11186 PositionMatches (Board b1, Board b2)
11189 switch(appData.searchMode) {
11190 case 1: return CompareWithRights(b1, b2);
11192 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11193 if(b2[r][f] != EmptySquare && b1[r][f] != b2[r][f]) return FALSE;
11197 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11198 if((b2[r][f] == WhitePawn || b2[r][f] == BlackPawn) && b1[r][f] != b2[r][f]) return FALSE;
11199 sum += keys[b1[r][f]] - keys[b2[r][f]];
11203 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11204 sum += keys[b1[r][f]] - keys[b2[r][f]];
11216 int pieceList[256], quickBoard[256];
11217 ChessSquare pieceType[256] = { EmptySquare };
11218 Board soughtBoard, reverseBoard, flipBoard, rotateBoard;
11219 int counts[EmptySquare], minSought[EmptySquare], minReverse[EmptySquare], maxSought[EmptySquare], maxReverse[EmptySquare];
11220 int soughtTotal, turn;
11221 Boolean epOK, flipSearch;
11224 unsigned char piece, to;
11227 #define DSIZE (250000)
11229 Move initialSpace[DSIZE+1000]; // gamble on that game will not be more than 500 moves
11230 Move *moveDatabase = initialSpace;
11231 unsigned int movePtr, dataSize = DSIZE;
11234 MakePieceList (Board board, int *counts)
11236 int r, f, n=Q_PROMO, total=0;
11237 for(r=0;r<EmptySquare;r++) counts[r] = 0; // piece-type counts
11238 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11239 int sq = f + (r<<4);
11240 if(board[r][f] == EmptySquare) quickBoard[sq] = 0; else {
11241 quickBoard[sq] = ++n;
11243 pieceType[n] = board[r][f];
11244 counts[board[r][f]]++;
11245 if(board[r][f] == WhiteKing) pieceList[1] = n; else
11246 if(board[r][f] == BlackKing) pieceList[2] = n; // remember which are Kings, for castling
11250 epOK = gameInfo.variant != VariantXiangqi && gameInfo.variant != VariantBerolina;
11255 PackMove (int fromX, int fromY, int toX, int toY, ChessSquare promoPiece)
11257 int sq = fromX + (fromY<<4);
11258 int piece = quickBoard[sq];
11259 quickBoard[sq] = 0;
11260 moveDatabase[movePtr].to = pieceList[piece] = sq = toX + (toY<<4);
11261 if(piece == pieceList[1] && fromY == toY && (toX > fromX+1 || toX < fromX-1) && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1) {
11262 int from = toX>fromX ? BOARD_RGHT-1 : BOARD_LEFT;
11263 moveDatabase[movePtr++].piece = Q_WCASTL;
11264 quickBoard[sq] = piece;
11265 piece = quickBoard[from]; quickBoard[from] = 0;
11266 moveDatabase[movePtr].to = pieceList[piece] = sq = toX>fromX ? sq-1 : sq+1;
11268 if(piece == pieceList[2] && fromY == toY && (toX > fromX+1 || toX < fromX-1) && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1) {
11269 int from = (toX>fromX ? BOARD_RGHT-1 : BOARD_LEFT) + (BOARD_HEIGHT-1 <<4);
11270 moveDatabase[movePtr++].piece = Q_BCASTL;
11271 quickBoard[sq] = piece;
11272 piece = quickBoard[from]; quickBoard[from] = 0;
11273 moveDatabase[movePtr].to = pieceList[piece] = sq = toX>fromX ? sq-1 : sq+1;
11275 if(epOK && (pieceType[piece] == WhitePawn || pieceType[piece] == BlackPawn) && fromX != toX && quickBoard[sq] == 0) {
11276 quickBoard[(fromY<<4)+toX] = 0;
11277 moveDatabase[movePtr].piece = Q_EP;
11278 moveDatabase[movePtr++].to = (fromY<<4)+toX;
11279 moveDatabase[movePtr].to = sq;
11281 if(promoPiece != pieceType[piece]) {
11282 moveDatabase[movePtr++].piece = Q_PROMO;
11283 moveDatabase[movePtr].to = pieceType[piece] = (int) promoPiece;
11285 moveDatabase[movePtr].piece = piece;
11286 quickBoard[sq] = piece;
11291 PackGame (Board board)
11293 Move *newSpace = NULL;
11294 moveDatabase[movePtr].piece = 0; // terminate previous game
11295 if(movePtr > dataSize) {
11296 if(appData.debugMode) fprintf(debugFP, "move-cache overflow, enlarge to %d MB\n", dataSize/128);
11297 dataSize *= 8; // increase size by factor 8 (512KB -> 4MB -> 32MB -> 256MB -> 2GB)
11298 if(dataSize) newSpace = (Move*) calloc(dataSize + 1000, sizeof(Move));
11301 Move *p = moveDatabase, *q = newSpace;
11302 for(i=0; i<movePtr; i++) *q++ = *p++; // copy to newly allocated space
11303 if(dataSize > 8*DSIZE) free(moveDatabase); // and free old space (if it was allocated)
11304 moveDatabase = newSpace;
11305 } else { // calloc failed, we must be out of memory. Too bad...
11306 dataSize = 0; // prevent calloc events for all subsequent games
11307 return 0; // and signal this one isn't cached
11311 MakePieceList(board, counts);
11316 QuickCompare (Board board, int *minCounts, int *maxCounts)
11317 { // compare according to search mode
11319 switch(appData.searchMode)
11321 case 1: // exact position match
11322 if(!(turn & board[EP_STATUS-1])) return FALSE; // wrong side to move
11323 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11324 if(board[r][f] != pieceType[quickBoard[(r<<4)+f]]) return FALSE;
11327 case 2: // can have extra material on empty squares
11328 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11329 if(board[r][f] == EmptySquare) continue;
11330 if(board[r][f] != pieceType[quickBoard[(r<<4)+f]]) return FALSE;
11333 case 3: // material with exact Pawn structure
11334 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11335 if(board[r][f] != WhitePawn && board[r][f] != BlackPawn) continue;
11336 if(board[r][f] != pieceType[quickBoard[(r<<4)+f]]) return FALSE;
11337 } // fall through to material comparison
11338 case 4: // exact material
11339 for(r=0; r<EmptySquare; r++) if(counts[r] != maxCounts[r]) return FALSE;
11341 case 6: // material range with given imbalance
11342 for(r=0; r<BlackPawn; r++) if(counts[r] - minCounts[r] != counts[r+BlackPawn] - minCounts[r+BlackPawn]) return FALSE;
11343 // fall through to range comparison
11344 case 5: // material range
11345 for(r=0; r<EmptySquare; r++) if(counts[r] < minCounts[r] || counts[r] > maxCounts[r]) return FALSE;
11351 QuickScan (Board board, Move *move)
11352 { // reconstruct game,and compare all positions in it
11353 int cnt=0, stretch=0, total = MakePieceList(board, counts);
11355 int piece = move->piece;
11356 int to = move->to, from = pieceList[piece];
11357 if(piece <= Q_PROMO) { // special moves encoded by otherwise invalid piece numbers 1-4
11358 if(!piece) return -1;
11359 if(piece == Q_PROMO) { // promotion, encoded as (Q_PROMO, to) + (piece, promoType)
11360 piece = (++move)->piece;
11361 from = pieceList[piece];
11362 counts[pieceType[piece]]--;
11363 pieceType[piece] = (ChessSquare) move->to;
11364 counts[move->to]++;
11365 } else if(piece == Q_EP) { // e.p. capture, encoded as (Q_EP, ep-sqr) + (piece, to)
11366 counts[pieceType[quickBoard[to]]]--;
11367 quickBoard[to] = 0; total--;
11370 } else if(piece <= Q_BCASTL) { // castling, encoded as (Q_XCASTL, king-to) + (rook, rook-to)
11371 piece = pieceList[piece]; // first two elements of pieceList contain King numbers
11372 from = pieceList[piece]; // so this must be King
11373 quickBoard[from] = 0;
11374 quickBoard[to] = piece;
11375 pieceList[piece] = to;
11380 if(appData.searchMode > 2) counts[pieceType[quickBoard[to]]]--; // account capture
11381 if((total -= (quickBoard[to] != 0)) < soughtTotal) return -1; // piece count dropped below what we search for
11382 quickBoard[from] = 0;
11383 quickBoard[to] = piece;
11384 pieceList[piece] = to;
11386 if(QuickCompare(soughtBoard, minSought, maxSought) ||
11387 appData.ignoreColors && QuickCompare(reverseBoard, minReverse, maxReverse) ||
11388 flipSearch && (QuickCompare(flipBoard, minSought, maxSought) ||
11389 appData.ignoreColors && QuickCompare(rotateBoard, minReverse, maxReverse))
11391 static int lastCounts[EmptySquare+1];
11393 if(stretch) for(i=0; i<EmptySquare; i++) if(lastCounts[i] != counts[i]) { stretch = 0; break; } // reset if material changes
11394 if(stretch++ == 0) for(i=0; i<EmptySquare; i++) lastCounts[i] = counts[i]; // remember actual material
11395 } else stretch = 0;
11396 if(stretch && (appData.searchMode == 1 || stretch >= appData.stretch)) return cnt + 1 - stretch;
11405 flipSearch = FALSE;
11406 CopyBoard(soughtBoard, boards[currentMove]);
11407 soughtTotal = MakePieceList(soughtBoard, maxSought);
11408 soughtBoard[EP_STATUS-1] = (currentMove & 1) + 1;
11409 if(currentMove == 0 && gameMode == EditPosition) soughtBoard[EP_STATUS-1] = blackPlaysFirst + 1; // (!)
11410 CopyBoard(reverseBoard, boards[currentMove]);
11411 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11412 int piece = boards[currentMove][BOARD_HEIGHT-1-r][f];
11413 if(piece < BlackPawn) piece += BlackPawn; else if(piece < EmptySquare) piece -= BlackPawn; // color-flip
11414 reverseBoard[r][f] = piece;
11416 reverseBoard[EP_STATUS-1] = soughtBoard[EP_STATUS-1] ^ 3;
11417 for(r=0; r<6; r++) reverseBoard[CASTLING][r] = boards[currentMove][CASTLING][(r+3)%6];
11418 if(appData.findMirror && appData.searchMode <= 3 && (!nrCastlingRights
11419 || (boards[currentMove][CASTLING][2] == NoRights ||
11420 boards[currentMove][CASTLING][0] == NoRights && boards[currentMove][CASTLING][1] == NoRights )
11421 && (boards[currentMove][CASTLING][5] == NoRights ||
11422 boards[currentMove][CASTLING][3] == NoRights && boards[currentMove][CASTLING][4] == NoRights ) )
11425 CopyBoard(flipBoard, soughtBoard);
11426 CopyBoard(rotateBoard, reverseBoard);
11427 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11428 flipBoard[r][f] = soughtBoard[r][BOARD_WIDTH-1-f];
11429 rotateBoard[r][f] = reverseBoard[r][BOARD_WIDTH-1-f];
11432 for(r=0; r<BlackPawn; r++) maxReverse[r] = maxSought[r+BlackPawn], maxReverse[r+BlackPawn] = maxSought[r];
11433 if(appData.searchMode >= 5) {
11434 for(r=BOARD_HEIGHT/2; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) soughtBoard[r][f] = EmptySquare;
11435 MakePieceList(soughtBoard, minSought);
11436 for(r=0; r<BlackPawn; r++) minReverse[r] = minSought[r+BlackPawn], minReverse[r+BlackPawn] = minSought[r];
11438 if(gameInfo.variant == VariantCrazyhouse || gameInfo.variant == VariantShogi || gameInfo.variant == VariantBughouse)
11439 soughtTotal = 0; // in drop games nr of pieces does not fall monotonously
11442 GameInfo dummyInfo;
11445 GameContainsPosition (FILE *f, ListGame *lg)
11447 int next, btm=0, plyNr=0, scratch=forwardMostMove+2&~1;
11448 int fromX, fromY, toX, toY;
11450 static int initDone=FALSE;
11452 // weed out games based on numerical tag comparison
11453 if(lg->gameInfo.variant != gameInfo.variant) return -1; // wrong variant
11454 if(appData.eloThreshold1 && (lg->gameInfo.whiteRating < appData.eloThreshold1 && lg->gameInfo.blackRating < appData.eloThreshold1)) return -1;
11455 if(appData.eloThreshold2 && (lg->gameInfo.whiteRating < appData.eloThreshold2 || lg->gameInfo.blackRating < appData.eloThreshold2)) return -1;
11456 if(appData.dateThreshold && (!lg->gameInfo.date || atoi(lg->gameInfo.date) < appData.dateThreshold)) return -1;
11458 for(next = WhitePawn; next<EmptySquare; next++) keys[next] = random()>>8 ^ random()<<6 ^random()<<20;
11461 if(lg->gameInfo.fen) ParseFEN(boards[scratch], &btm, lg->gameInfo.fen);
11462 else CopyBoard(boards[scratch], initialPosition); // default start position
11465 if((next = QuickScan( boards[scratch], &moveDatabase[lg->moves] )) < 0) return -1; // quick scan rules out it is there
11466 if(appData.searchMode >= 4) return next; // for material searches, trust QuickScan.
11469 if(PositionMatches(boards[scratch], boards[currentMove])) return plyNr;
11470 fseek(f, lg->offset, 0);
11473 yyboardindex = scratch;
11474 quickFlag = plyNr+1;
11479 if(plyNr) return -1; // after we have seen moves, any tags will be start of next game
11485 if(plyNr) return -1; // after we have seen moves, this is for new game
11488 case AmbiguousMove: // we cannot reconstruct the game beyond these two
11489 case ImpossibleMove:
11490 case WhiteWins: // game ends here with these four
11493 case GameUnfinished:
11497 if(appData.testLegality) return -1;
11498 case WhiteCapturesEnPassant:
11499 case BlackCapturesEnPassant:
11500 case WhitePromotion:
11501 case BlackPromotion:
11502 case WhiteNonPromotion:
11503 case BlackNonPromotion:
11505 case WhiteKingSideCastle:
11506 case WhiteQueenSideCastle:
11507 case BlackKingSideCastle:
11508 case BlackQueenSideCastle:
11509 case WhiteKingSideCastleWild:
11510 case WhiteQueenSideCastleWild:
11511 case BlackKingSideCastleWild:
11512 case BlackQueenSideCastleWild:
11513 case WhiteHSideCastleFR:
11514 case WhiteASideCastleFR:
11515 case BlackHSideCastleFR:
11516 case BlackASideCastleFR:
11517 fromX = currentMoveString[0] - AAA;
11518 fromY = currentMoveString[1] - ONE;
11519 toX = currentMoveString[2] - AAA;
11520 toY = currentMoveString[3] - ONE;
11521 promoChar = currentMoveString[4];
11525 fromX = next == WhiteDrop ?
11526 (int) CharToPiece(ToUpper(currentMoveString[0])) :
11527 (int) CharToPiece(ToLower(currentMoveString[0]));
11529 toX = currentMoveString[2] - AAA;
11530 toY = currentMoveString[3] - ONE;
11534 // Move encountered; peform it. We need to shuttle between two boards, as even/odd index determines side to move
11536 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[scratch]);
11537 if(PositionMatches(boards[scratch], boards[currentMove])) return plyNr;
11538 if(appData.ignoreColors && PositionMatches(boards[scratch], reverseBoard)) return plyNr;
11539 if(appData.findMirror) {
11540 if(PositionMatches(boards[scratch], flipBoard)) return plyNr;
11541 if(appData.ignoreColors && PositionMatches(boards[scratch], rotateBoard)) return plyNr;
11546 /* Load the nth game from open file f */
11548 LoadGame (FILE *f, int gameNumber, char *title, int useList)
11552 int gn = gameNumber;
11553 ListGame *lg = NULL;
11554 int numPGNTags = 0;
11556 GameMode oldGameMode;
11557 VariantClass oldVariant = gameInfo.variant; /* [HGM] PGNvariant */
11559 if (appData.debugMode)
11560 fprintf(debugFP, "LoadGame(): on entry, gameMode %d\n", gameMode);
11562 if (gameMode == Training )
11563 SetTrainingModeOff();
11565 oldGameMode = gameMode;
11566 if (gameMode != BeginningOfGame) {
11567 Reset(FALSE, TRUE);
11571 if (lastLoadGameFP != NULL && lastLoadGameFP != f) {
11572 fclose(lastLoadGameFP);
11576 lg = (ListGame *) ListElem(&gameList, gameNumber-1);
11579 fseek(f, lg->offset, 0);
11580 GameListHighlight(gameNumber);
11581 pos = lg->position;
11585 DisplayError(_("Game number out of range"), 0);
11590 if (fseek(f, 0, 0) == -1) {
11591 if (f == lastLoadGameFP ?
11592 gameNumber == lastLoadGameNumber + 1 :
11596 DisplayError(_("Can't seek on game file"), 0);
11601 lastLoadGameFP = f;
11602 lastLoadGameNumber = gameNumber;
11603 safeStrCpy(lastLoadGameTitle, title, sizeof(lastLoadGameTitle)/sizeof(lastLoadGameTitle[0]));
11604 lastLoadGameUseList = useList;
11608 if (lg && lg->gameInfo.white && lg->gameInfo.black) {
11609 snprintf(buf, sizeof(buf), "%s %s %s", lg->gameInfo.white, _("vs."),
11610 lg->gameInfo.black);
11612 } else if (*title != NULLCHAR) {
11613 if (gameNumber > 1) {
11614 snprintf(buf, MSG_SIZ, "%s %d", title, gameNumber);
11617 DisplayTitle(title);
11621 if (gameMode != AnalyzeFile && gameMode != AnalyzeMode) {
11622 gameMode = PlayFromGameFile;
11626 currentMove = forwardMostMove = backwardMostMove = 0;
11627 CopyBoard(boards[0], initialPosition);
11631 * Skip the first gn-1 games in the file.
11632 * Also skip over anything that precedes an identifiable
11633 * start of game marker, to avoid being confused by
11634 * garbage at the start of the file. Currently
11635 * recognized start of game markers are the move number "1",
11636 * the pattern "gnuchess .* game", the pattern
11637 * "^[#;%] [^ ]* game file", and a PGN tag block.
11638 * A game that starts with one of the latter two patterns
11639 * will also have a move number 1, possibly
11640 * following a position diagram.
11641 * 5-4-02: Let's try being more lenient and allowing a game to
11642 * start with an unnumbered move. Does that break anything?
11644 cm = lastLoadGameStart = EndOfFile;
11646 yyboardindex = forwardMostMove;
11647 cm = (ChessMove) Myylex();
11650 if (cmailMsgLoaded) {
11651 nCmailGames = CMAIL_MAX_GAMES - gn;
11654 DisplayError(_("Game not found in file"), 0);
11661 lastLoadGameStart = cm;
11664 case MoveNumberOne:
11665 switch (lastLoadGameStart) {
11670 case MoveNumberOne:
11672 gn--; /* count this game */
11673 lastLoadGameStart = cm;
11682 switch (lastLoadGameStart) {
11685 case MoveNumberOne:
11687 gn--; /* count this game */
11688 lastLoadGameStart = cm;
11691 lastLoadGameStart = cm; /* game counted already */
11699 yyboardindex = forwardMostMove;
11700 cm = (ChessMove) Myylex();
11701 } while (cm == PGNTag || cm == Comment);
11708 if (cmailMsgLoaded && (CMAIL_MAX_GAMES == lastLoadGameNumber)) {
11709 if ( cmailResult[CMAIL_MAX_GAMES - gn - 1]
11710 != CMAIL_OLD_RESULT) {
11712 cmailResult[ CMAIL_MAX_GAMES
11713 - gn - 1] = CMAIL_OLD_RESULT;
11719 /* Only a NormalMove can be at the start of a game
11720 * without a position diagram. */
11721 if (lastLoadGameStart == EndOfFile ) {
11723 lastLoadGameStart = MoveNumberOne;
11732 if (appData.debugMode)
11733 fprintf(debugFP, "Parsed game start '%s' (%d)\n", yy_text, (int) cm);
11735 if (cm == XBoardGame) {
11736 /* Skip any header junk before position diagram and/or move 1 */
11738 yyboardindex = forwardMostMove;
11739 cm = (ChessMove) Myylex();
11741 if (cm == EndOfFile ||
11742 cm == GNUChessGame || cm == XBoardGame) {
11743 /* Empty game; pretend end-of-file and handle later */
11748 if (cm == MoveNumberOne || cm == PositionDiagram ||
11749 cm == PGNTag || cm == Comment)
11752 } else if (cm == GNUChessGame) {
11753 if (gameInfo.event != NULL) {
11754 free(gameInfo.event);
11756 gameInfo.event = StrSave(yy_text);
11759 startedFromSetupPosition = FALSE;
11760 while (cm == PGNTag) {
11761 if (appData.debugMode)
11762 fprintf(debugFP, "Parsed PGNTag: %s\n", yy_text);
11763 err = ParsePGNTag(yy_text, &gameInfo);
11764 if (!err) numPGNTags++;
11766 /* [HGM] PGNvariant: automatically switch to variant given in PGN tag */
11767 if(gameInfo.variant != oldVariant) {
11768 startedFromPositionFile = FALSE; /* [HGM] loadPos: variant switch likely makes position invalid */
11769 ResetFrontEnd(); // [HGM] might need other bitmaps. Cannot use Reset() because it clears gameInfo :-(
11770 InitPosition(TRUE);
11771 oldVariant = gameInfo.variant;
11772 if (appData.debugMode)
11773 fprintf(debugFP, "New variant %d\n", (int) oldVariant);
11777 if (gameInfo.fen != NULL) {
11778 Board initial_position;
11779 startedFromSetupPosition = TRUE;
11780 if (!ParseFEN(initial_position, &blackPlaysFirst, gameInfo.fen)) {
11782 DisplayError(_("Bad FEN position in file"), 0);
11785 CopyBoard(boards[0], initial_position);
11786 if (blackPlaysFirst) {
11787 currentMove = forwardMostMove = backwardMostMove = 1;
11788 CopyBoard(boards[1], initial_position);
11789 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
11790 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
11791 timeRemaining[0][1] = whiteTimeRemaining;
11792 timeRemaining[1][1] = blackTimeRemaining;
11793 if (commentList[0] != NULL) {
11794 commentList[1] = commentList[0];
11795 commentList[0] = NULL;
11798 currentMove = forwardMostMove = backwardMostMove = 0;
11800 /* [HGM] copy FEN attributes as well. Bugfix 4.3.14m and 4.3.15e: moved to after 'blackPlaysFirst' */
11802 initialRulePlies = FENrulePlies;
11803 for( i=0; i< nrCastlingRights; i++ )
11804 initialRights[i] = initial_position[CASTLING][i];
11806 yyboardindex = forwardMostMove;
11807 free(gameInfo.fen);
11808 gameInfo.fen = NULL;
11811 yyboardindex = forwardMostMove;
11812 cm = (ChessMove) Myylex();
11814 /* Handle comments interspersed among the tags */
11815 while (cm == Comment) {
11817 if (appData.debugMode)
11818 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
11820 AppendComment(currentMove, p, FALSE);
11821 yyboardindex = forwardMostMove;
11822 cm = (ChessMove) Myylex();
11826 /* don't rely on existence of Event tag since if game was
11827 * pasted from clipboard the Event tag may not exist
11829 if (numPGNTags > 0){
11831 if (gameInfo.variant == VariantNormal) {
11832 VariantClass v = StringToVariant(gameInfo.event);
11833 // [HGM] do not recognize variants from event tag that were introduced after supporting variant tag
11834 if(v < VariantShogi) gameInfo.variant = v;
11837 if( appData.autoDisplayTags ) {
11838 tags = PGNTags(&gameInfo);
11839 TagsPopUp(tags, CmailMsg());
11844 /* Make something up, but don't display it now */
11849 if (cm == PositionDiagram) {
11852 Board initial_position;
11854 if (appData.debugMode)
11855 fprintf(debugFP, "Parsed PositionDiagram: %s\n", yy_text);
11857 if (!startedFromSetupPosition) {
11859 for (i = BOARD_HEIGHT - 1; i >= 0; i--)
11860 for (j = BOARD_LEFT; j < BOARD_RGHT; p++)
11871 initial_position[i][j++] = CharToPiece(*p);
11874 while (*p == ' ' || *p == '\t' ||
11875 *p == '\n' || *p == '\r') p++;
11877 if (strncmp(p, "black", strlen("black"))==0)
11878 blackPlaysFirst = TRUE;
11880 blackPlaysFirst = FALSE;
11881 startedFromSetupPosition = TRUE;
11883 CopyBoard(boards[0], initial_position);
11884 if (blackPlaysFirst) {
11885 currentMove = forwardMostMove = backwardMostMove = 1;
11886 CopyBoard(boards[1], initial_position);
11887 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
11888 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
11889 timeRemaining[0][1] = whiteTimeRemaining;
11890 timeRemaining[1][1] = blackTimeRemaining;
11891 if (commentList[0] != NULL) {
11892 commentList[1] = commentList[0];
11893 commentList[0] = NULL;
11896 currentMove = forwardMostMove = backwardMostMove = 0;
11899 yyboardindex = forwardMostMove;
11900 cm = (ChessMove) Myylex();
11903 if (first.pr == NoProc) {
11904 StartChessProgram(&first);
11906 InitChessProgram(&first, FALSE);
11907 SendToProgram("force\n", &first);
11908 if (startedFromSetupPosition) {
11909 SendBoard(&first, forwardMostMove);
11910 if (appData.debugMode) {
11911 fprintf(debugFP, "Load Game\n");
11913 DisplayBothClocks();
11916 /* [HGM] server: flag to write setup moves in broadcast file as one */
11917 loadFlag = appData.suppressLoadMoves;
11919 while (cm == Comment) {
11921 if (appData.debugMode)
11922 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
11924 AppendComment(currentMove, p, FALSE);
11925 yyboardindex = forwardMostMove;
11926 cm = (ChessMove) Myylex();
11929 if ((cm == EndOfFile && lastLoadGameStart != EndOfFile ) ||
11930 cm == WhiteWins || cm == BlackWins ||
11931 cm == GameIsDrawn || cm == GameUnfinished) {
11932 DisplayMessage("", _("No moves in game"));
11933 if (cmailMsgLoaded) {
11934 if (appData.debugMode)
11935 fprintf(debugFP, "Setting flipView to %d.\n", FALSE);
11939 DrawPosition(FALSE, boards[currentMove]);
11940 DisplayBothClocks();
11941 gameMode = EditGame;
11948 // [HGM] PV info: routine tests if comment empty
11949 if (!matchMode && (pausing || appData.timeDelay != 0)) {
11950 DisplayComment(currentMove - 1, commentList[currentMove]);
11952 if (!matchMode && appData.timeDelay != 0)
11953 DrawPosition(FALSE, boards[currentMove]);
11955 if (gameMode == AnalyzeFile || gameMode == AnalyzeMode) {
11956 programStats.ok_to_send = 1;
11959 /* if the first token after the PGN tags is a move
11960 * and not move number 1, retrieve it from the parser
11962 if (cm != MoveNumberOne)
11963 LoadGameOneMove(cm);
11965 /* load the remaining moves from the file */
11966 while (LoadGameOneMove(EndOfFile)) {
11967 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
11968 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
11971 /* rewind to the start of the game */
11972 currentMove = backwardMostMove;
11974 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
11976 if (oldGameMode == AnalyzeFile ||
11977 oldGameMode == AnalyzeMode) {
11978 AnalyzeFileEvent();
11981 if (!matchMode && pos >= 0) {
11982 ToNrEvent(pos); // [HGM] no autoplay if selected on position
11984 if (matchMode || appData.timeDelay == 0) {
11986 } else if (appData.timeDelay > 0) {
11987 AutoPlayGameLoop();
11990 if (appData.debugMode)
11991 fprintf(debugFP, "LoadGame(): on exit, gameMode %d\n", gameMode);
11993 loadFlag = 0; /* [HGM] true game starts */
11997 /* Support for LoadNextPosition, LoadPreviousPosition, ReloadSamePosition */
11999 ReloadPosition (int offset)
12001 int positionNumber = lastLoadPositionNumber + offset;
12002 if (lastLoadPositionFP == NULL) {
12003 DisplayError(_("No position has been loaded yet"), 0);
12006 if (positionNumber <= 0) {
12007 DisplayError(_("Can't back up any further"), 0);
12010 return LoadPosition(lastLoadPositionFP, positionNumber,
12011 lastLoadPositionTitle);
12014 /* Load the nth position from the given file */
12016 LoadPositionFromFile (char *filename, int n, char *title)
12021 if (strcmp(filename, "-") == 0) {
12022 return LoadPosition(stdin, n, "stdin");
12024 f = fopen(filename, "rb");
12026 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
12027 DisplayError(buf, errno);
12030 return LoadPosition(f, n, title);
12035 /* Load the nth position from the given open file, and close it */
12037 LoadPosition (FILE *f, int positionNumber, char *title)
12039 char *p, line[MSG_SIZ];
12040 Board initial_position;
12041 int i, j, fenMode, pn;
12043 if (gameMode == Training )
12044 SetTrainingModeOff();
12046 if (gameMode != BeginningOfGame) {
12047 Reset(FALSE, TRUE);
12049 if (lastLoadPositionFP != NULL && lastLoadPositionFP != f) {
12050 fclose(lastLoadPositionFP);
12052 if (positionNumber == 0) positionNumber = 1;
12053 lastLoadPositionFP = f;
12054 lastLoadPositionNumber = positionNumber;
12055 safeStrCpy(lastLoadPositionTitle, title, sizeof(lastLoadPositionTitle)/sizeof(lastLoadPositionTitle[0]));
12056 if (first.pr == NoProc && !appData.noChessProgram) {
12057 StartChessProgram(&first);
12058 InitChessProgram(&first, FALSE);
12060 pn = positionNumber;
12061 if (positionNumber < 0) {
12062 /* Negative position number means to seek to that byte offset */
12063 if (fseek(f, -positionNumber, 0) == -1) {
12064 DisplayError(_("Can't seek on position file"), 0);
12069 if (fseek(f, 0, 0) == -1) {
12070 if (f == lastLoadPositionFP ?
12071 positionNumber == lastLoadPositionNumber + 1 :
12072 positionNumber == 1) {
12075 DisplayError(_("Can't seek on position file"), 0);
12080 /* See if this file is FEN or old-style xboard */
12081 if (fgets(line, MSG_SIZ, f) == NULL) {
12082 DisplayError(_("Position not found in file"), 0);
12085 // [HGM] FEN can begin with digit, any piece letter valid in this variant, or a + for Shogi promoted pieces
12086 fenMode = line[0] >= '0' && line[0] <= '9' || line[0] == '+' || CharToPiece(line[0]) != EmptySquare;
12089 if (fenMode || line[0] == '#') pn--;
12091 /* skip positions before number pn */
12092 if (fgets(line, MSG_SIZ, f) == NULL) {
12094 DisplayError(_("Position not found in file"), 0);
12097 if (fenMode || line[0] == '#') pn--;
12102 if (!ParseFEN(initial_position, &blackPlaysFirst, line)) {
12103 DisplayError(_("Bad FEN position in file"), 0);
12107 (void) fgets(line, MSG_SIZ, f);
12108 (void) fgets(line, MSG_SIZ, f);
12110 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
12111 (void) fgets(line, MSG_SIZ, f);
12112 for (p = line, j = BOARD_LEFT; j < BOARD_RGHT; p++) {
12115 initial_position[i][j++] = CharToPiece(*p);
12119 blackPlaysFirst = FALSE;
12121 (void) fgets(line, MSG_SIZ, f);
12122 if (strncmp(line, "black", strlen("black"))==0)
12123 blackPlaysFirst = TRUE;
12126 startedFromSetupPosition = TRUE;
12128 CopyBoard(boards[0], initial_position);
12129 if (blackPlaysFirst) {
12130 currentMove = forwardMostMove = backwardMostMove = 1;
12131 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
12132 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
12133 CopyBoard(boards[1], initial_position);
12134 DisplayMessage("", _("Black to play"));
12136 currentMove = forwardMostMove = backwardMostMove = 0;
12137 DisplayMessage("", _("White to play"));
12139 initialRulePlies = FENrulePlies; /* [HGM] copy FEN attributes as well */
12140 if(first.pr != NoProc) { // [HGM] in tourney-mode a position can be loaded before the chess engine is installed
12141 SendToProgram("force\n", &first);
12142 SendBoard(&first, forwardMostMove);
12144 if (appData.debugMode) {
12146 for(i=0;i<2;i++){for(j=0;j<6;j++)fprintf(debugFP, " %d", boards[i][CASTLING][j]);fprintf(debugFP,"\n");}
12147 for(j=0;j<6;j++)fprintf(debugFP, " %d", initialRights[j]);fprintf(debugFP,"\n");
12148 fprintf(debugFP, "Load Position\n");
12151 if (positionNumber > 1) {
12152 snprintf(line, MSG_SIZ, "%s %d", title, positionNumber);
12153 DisplayTitle(line);
12155 DisplayTitle(title);
12157 gameMode = EditGame;
12160 timeRemaining[0][1] = whiteTimeRemaining;
12161 timeRemaining[1][1] = blackTimeRemaining;
12162 DrawPosition(FALSE, boards[currentMove]);
12169 CopyPlayerNameIntoFileName (char **dest, char *src)
12171 while (*src != NULLCHAR && *src != ',') {
12176 *(*dest)++ = *src++;
12182 DefaultFileName (char *ext)
12184 static char def[MSG_SIZ];
12187 if (gameInfo.white != NULL && gameInfo.white[0] != '-') {
12189 CopyPlayerNameIntoFileName(&p, gameInfo.white);
12191 CopyPlayerNameIntoFileName(&p, gameInfo.black);
12193 safeStrCpy(p, ext, MSG_SIZ-2-strlen(gameInfo.white)-strlen(gameInfo.black));
12200 /* Save the current game to the given file */
12202 SaveGameToFile (char *filename, int append)
12206 int result, i, t,tot=0;
12208 if (strcmp(filename, "-") == 0) {
12209 return SaveGame(stdout, 0, NULL);
12211 for(i=0; i<10; i++) { // upto 10 tries
12212 f = fopen(filename, append ? "a" : "w");
12213 if(f && i) fprintf(f, "[Delay \"%d retries, %d msec\"]\n",i,tot);
12214 if(f || errno != 13) break;
12215 DoSleep(t = 5 + random()%11); // wait 5-15 msec
12219 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
12220 DisplayError(buf, errno);
12223 safeStrCpy(buf, lastMsg, MSG_SIZ);
12224 DisplayMessage(_("Waiting for access to save file"), "");
12225 flock(fileno(f), LOCK_EX); // [HGM] lock: lock file while we are writing
12226 DisplayMessage(_("Saving game"), "");
12227 if(lseek(fileno(f), 0, SEEK_END) == -1) DisplayError(_("Bad Seek"), errno); // better safe than sorry...
12228 result = SaveGame(f, 0, NULL);
12229 DisplayMessage(buf, "");
12236 SavePart (char *str)
12238 static char buf[MSG_SIZ];
12241 p = strchr(str, ' ');
12242 if (p == NULL) return str;
12243 strncpy(buf, str, p - str);
12244 buf[p - str] = NULLCHAR;
12248 #define PGN_MAX_LINE 75
12250 #define PGN_SIDE_WHITE 0
12251 #define PGN_SIDE_BLACK 1
12254 FindFirstMoveOutOfBook (int side)
12258 if( backwardMostMove == 0 && ! startedFromSetupPosition) {
12259 int index = backwardMostMove;
12260 int has_book_hit = 0;
12262 if( (index % 2) != side ) {
12266 while( index < forwardMostMove ) {
12267 /* Check to see if engine is in book */
12268 int depth = pvInfoList[index].depth;
12269 int score = pvInfoList[index].score;
12275 else if( score == 0 && depth == 63 ) {
12276 in_book = 1; /* Zappa */
12278 else if( score == 2 && depth == 99 ) {
12279 in_book = 1; /* Abrok */
12282 has_book_hit += in_book;
12298 GetOutOfBookInfo (char * buf)
12302 int offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
12304 oob[0] = FindFirstMoveOutOfBook( PGN_SIDE_WHITE );
12305 oob[1] = FindFirstMoveOutOfBook( PGN_SIDE_BLACK );
12309 if( oob[0] >= 0 || oob[1] >= 0 ) {
12310 for( i=0; i<2; i++ ) {
12314 if( i > 0 && oob[0] >= 0 ) {
12315 strcat( buf, " " );
12318 sprintf( buf+strlen(buf), "%d%s. ", (idx - offset)/2 + 1, idx & 1 ? ".." : "" );
12319 sprintf( buf+strlen(buf), "%s%.2f",
12320 pvInfoList[idx].score >= 0 ? "+" : "",
12321 pvInfoList[idx].score / 100.0 );
12327 /* Save game in PGN style and close the file */
12329 SaveGamePGN (FILE *f)
12331 int i, offset, linelen, newblock;
12335 int movelen, numlen, blank;
12336 char move_buffer[100]; /* [AS] Buffer for move+PV info */
12338 offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
12340 tm = time((time_t *) NULL);
12342 PrintPGNTags(f, &gameInfo);
12344 if(appData.numberTag && matchMode) fprintf(f, "[Number \"%d\"]\n", nextGame+1); // [HGM] number tag
12346 if (backwardMostMove > 0 || startedFromSetupPosition) {
12347 char *fen = PositionToFEN(backwardMostMove, NULL);
12348 fprintf(f, "[FEN \"%s\"]\n[SetUp \"1\"]\n", fen);
12349 fprintf(f, "\n{--------------\n");
12350 PrintPosition(f, backwardMostMove);
12351 fprintf(f, "--------------}\n");
12355 /* [AS] Out of book annotation */
12356 if( appData.saveOutOfBookInfo ) {
12359 GetOutOfBookInfo( buf );
12361 if( buf[0] != '\0' ) {
12362 fprintf( f, "[%s \"%s\"]\n", PGN_OUT_OF_BOOK, buf );
12369 i = backwardMostMove;
12373 while (i < forwardMostMove) {
12374 /* Print comments preceding this move */
12375 if (commentList[i] != NULL) {
12376 if (linelen > 0) fprintf(f, "\n");
12377 fprintf(f, "%s", commentList[i]);
12382 /* Format move number */
12384 snprintf(numtext, sizeof(numtext)/sizeof(numtext[0]),"%d.", (i - offset)/2 + 1);
12387 snprintf(numtext, sizeof(numtext)/sizeof(numtext[0]), "%d...", (i - offset)/2 + 1);
12389 numtext[0] = NULLCHAR;
12391 numlen = strlen(numtext);
12394 /* Print move number */
12395 blank = linelen > 0 && numlen > 0;
12396 if (linelen + (blank ? 1 : 0) + numlen > PGN_MAX_LINE) {
12405 fprintf(f, "%s", numtext);
12409 safeStrCpy(move_buffer, SavePart(parseList[i]), sizeof(move_buffer)/sizeof(move_buffer[0])); // [HGM] pgn: print move via buffer, so it can be edited
12410 movelen = strlen(move_buffer); /* [HGM] pgn: line-break point before move */
12413 blank = linelen > 0 && movelen > 0;
12414 if (linelen + (blank ? 1 : 0) + movelen > PGN_MAX_LINE) {
12423 fprintf(f, "%s", move_buffer);
12424 linelen += movelen;
12426 /* [AS] Add PV info if present */
12427 if( i >= 0 && appData.saveExtendedInfoInPGN && pvInfoList[i].depth > 0 ) {
12428 /* [HGM] add time */
12429 char buf[MSG_SIZ]; int seconds;
12431 seconds = (pvInfoList[i].time+5)/10; // deci-seconds, rounded to nearest
12437 snprintf(buf, MSG_SIZ, " %3.1f%c", seconds/10., 0);
12440 seconds = (seconds + 4)/10; // round to full seconds
12442 snprintf(buf, MSG_SIZ, " %d%c", seconds, 0);
12444 snprintf(buf, MSG_SIZ, " %d:%02d%c", seconds/60, seconds%60, 0);
12447 snprintf( move_buffer, sizeof(move_buffer)/sizeof(move_buffer[0]),"{%s%.2f/%d%s}",
12448 pvInfoList[i].score >= 0 ? "+" : "",
12449 pvInfoList[i].score / 100.0,
12450 pvInfoList[i].depth,
12453 movelen = strlen(move_buffer); /* [HGM] pgn: line-break point after move */
12455 /* Print score/depth */
12456 blank = linelen > 0 && movelen > 0;
12457 if (linelen + (blank ? 1 : 0) + movelen > PGN_MAX_LINE) {
12466 fprintf(f, "%s", move_buffer);
12467 linelen += movelen;
12473 /* Start a new line */
12474 if (linelen > 0) fprintf(f, "\n");
12476 /* Print comments after last move */
12477 if (commentList[i] != NULL) {
12478 fprintf(f, "%s\n", commentList[i]);
12482 if (gameInfo.resultDetails != NULL &&
12483 gameInfo.resultDetails[0] != NULLCHAR) {
12484 fprintf(f, "{%s} %s\n\n", gameInfo.resultDetails,
12485 PGNResult(gameInfo.result));
12487 fprintf(f, "%s\n\n", PGNResult(gameInfo.result));
12491 lastSavedGame = GameCheckSum(); // [HGM] save: remember ID of last saved game to prevent double saving
12495 /* Save game in old style and close the file */
12497 SaveGameOldStyle (FILE *f)
12502 tm = time((time_t *) NULL);
12504 fprintf(f, "# %s game file -- %s", programName, ctime(&tm));
12507 if (backwardMostMove > 0 || startedFromSetupPosition) {
12508 fprintf(f, "\n[--------------\n");
12509 PrintPosition(f, backwardMostMove);
12510 fprintf(f, "--------------]\n");
12515 i = backwardMostMove;
12516 offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
12518 while (i < forwardMostMove) {
12519 if (commentList[i] != NULL) {
12520 fprintf(f, "[%s]\n", commentList[i]);
12523 if ((i % 2) == 1) {
12524 fprintf(f, "%d. ... %s\n", (i - offset)/2 + 1, parseList[i]);
12527 fprintf(f, "%d. %s ", (i - offset)/2 + 1, parseList[i]);
12529 if (commentList[i] != NULL) {
12533 if (i >= forwardMostMove) {
12537 fprintf(f, "%s\n", parseList[i]);
12542 if (commentList[i] != NULL) {
12543 fprintf(f, "[%s]\n", commentList[i]);
12546 /* This isn't really the old style, but it's close enough */
12547 if (gameInfo.resultDetails != NULL &&
12548 gameInfo.resultDetails[0] != NULLCHAR) {
12549 fprintf(f, "%s (%s)\n\n", PGNResult(gameInfo.result),
12550 gameInfo.resultDetails);
12552 fprintf(f, "%s\n\n", PGNResult(gameInfo.result));
12559 /* Save the current game to open file f and close the file */
12561 SaveGame (FILE *f, int dummy, char *dummy2)
12563 if (gameMode == EditPosition) EditPositionDone(TRUE);
12564 lastSavedGame = GameCheckSum(); // [HGM] save: remember ID of last saved game to prevent double saving
12565 if (appData.oldSaveStyle)
12566 return SaveGameOldStyle(f);
12568 return SaveGamePGN(f);
12571 /* Save the current position to the given file */
12573 SavePositionToFile (char *filename)
12578 if (strcmp(filename, "-") == 0) {
12579 return SavePosition(stdout, 0, NULL);
12581 f = fopen(filename, "a");
12583 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
12584 DisplayError(buf, errno);
12587 safeStrCpy(buf, lastMsg, MSG_SIZ);
12588 DisplayMessage(_("Waiting for access to save file"), "");
12589 flock(fileno(f), LOCK_EX); // [HGM] lock
12590 DisplayMessage(_("Saving position"), "");
12591 lseek(fileno(f), 0, SEEK_END); // better safe than sorry...
12592 SavePosition(f, 0, NULL);
12593 DisplayMessage(buf, "");
12599 /* Save the current position to the given open file and close the file */
12601 SavePosition (FILE *f, int dummy, char *dummy2)
12606 if (gameMode == EditPosition) EditPositionDone(TRUE);
12607 if (appData.oldSaveStyle) {
12608 tm = time((time_t *) NULL);
12610 fprintf(f, "# %s position file -- %s", programName, ctime(&tm));
12612 fprintf(f, "[--------------\n");
12613 PrintPosition(f, currentMove);
12614 fprintf(f, "--------------]\n");
12616 fen = PositionToFEN(currentMove, NULL);
12617 fprintf(f, "%s\n", fen);
12625 ReloadCmailMsgEvent (int unregister)
12628 static char *inFilename = NULL;
12629 static char *outFilename;
12631 struct stat inbuf, outbuf;
12634 /* Any registered moves are unregistered if unregister is set, */
12635 /* i.e. invoked by the signal handler */
12637 for (i = 0; i < CMAIL_MAX_GAMES; i ++) {
12638 cmailMoveRegistered[i] = FALSE;
12639 if (cmailCommentList[i] != NULL) {
12640 free(cmailCommentList[i]);
12641 cmailCommentList[i] = NULL;
12644 nCmailMovesRegistered = 0;
12647 for (i = 0; i < CMAIL_MAX_GAMES; i ++) {
12648 cmailResult[i] = CMAIL_NOT_RESULT;
12652 if (inFilename == NULL) {
12653 /* Because the filenames are static they only get malloced once */
12654 /* and they never get freed */
12655 inFilename = (char *) malloc(strlen(appData.cmailGameName) + 9);
12656 sprintf(inFilename, "%s.game.in", appData.cmailGameName);
12658 outFilename = (char *) malloc(strlen(appData.cmailGameName) + 5);
12659 sprintf(outFilename, "%s.out", appData.cmailGameName);
12662 status = stat(outFilename, &outbuf);
12664 cmailMailedMove = FALSE;
12666 status = stat(inFilename, &inbuf);
12667 cmailMailedMove = (inbuf.st_mtime < outbuf.st_mtime);
12670 /* LoadGameFromFile(CMAIL_MAX_GAMES) with cmailMsgLoaded == TRUE
12671 counts the games, notes how each one terminated, etc.
12673 It would be nice to remove this kludge and instead gather all
12674 the information while building the game list. (And to keep it
12675 in the game list nodes instead of having a bunch of fixed-size
12676 parallel arrays.) Note this will require getting each game's
12677 termination from the PGN tags, as the game list builder does
12678 not process the game moves. --mann
12680 cmailMsgLoaded = TRUE;
12681 LoadGameFromFile(inFilename, CMAIL_MAX_GAMES, "", FALSE);
12683 /* Load first game in the file or popup game menu */
12684 LoadGameFromFile(inFilename, 0, appData.cmailGameName, TRUE);
12686 #endif /* !WIN32 */
12694 char string[MSG_SIZ];
12696 if ( cmailMailedMove
12697 || (cmailResult[lastLoadGameNumber - 1] == CMAIL_OLD_RESULT)) {
12698 return TRUE; /* Allow free viewing */
12701 /* Unregister move to ensure that we don't leave RegisterMove */
12702 /* with the move registered when the conditions for registering no */
12704 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
12705 cmailMoveRegistered[lastLoadGameNumber - 1] = FALSE;
12706 nCmailMovesRegistered --;
12708 if (cmailCommentList[lastLoadGameNumber - 1] != NULL)
12710 free(cmailCommentList[lastLoadGameNumber - 1]);
12711 cmailCommentList[lastLoadGameNumber - 1] = NULL;
12715 if (cmailOldMove == -1) {
12716 DisplayError(_("You have edited the game history.\nUse Reload Same Game and make your move again."), 0);
12720 if (currentMove > cmailOldMove + 1) {
12721 DisplayError(_("You have entered too many moves.\nBack up to the correct position and try again."), 0);
12725 if (currentMove < cmailOldMove) {
12726 DisplayError(_("Displayed position is not current.\nStep forward to the correct position and try again."), 0);
12730 if (forwardMostMove > currentMove) {
12731 /* Silently truncate extra moves */
12735 if ( (currentMove == cmailOldMove + 1)
12736 || ( (currentMove == cmailOldMove)
12737 && ( (cmailMoveType[lastLoadGameNumber - 1] == CMAIL_ACCEPT)
12738 || (cmailMoveType[lastLoadGameNumber - 1] == CMAIL_RESIGN)))) {
12739 if (gameInfo.result != GameUnfinished) {
12740 cmailResult[lastLoadGameNumber - 1] = CMAIL_NEW_RESULT;
12743 if (commentList[currentMove] != NULL) {
12744 cmailCommentList[lastLoadGameNumber - 1]
12745 = StrSave(commentList[currentMove]);
12747 safeStrCpy(cmailMove[lastLoadGameNumber - 1], moveList[currentMove - 1], sizeof(cmailMove[lastLoadGameNumber - 1])/sizeof(cmailMove[lastLoadGameNumber - 1][0]));
12749 if (appData.debugMode)
12750 fprintf(debugFP, "Saving %s for game %d\n",
12751 cmailMove[lastLoadGameNumber - 1], lastLoadGameNumber);
12753 snprintf(string, MSG_SIZ, "%s.game.out.%d", appData.cmailGameName, lastLoadGameNumber);
12755 f = fopen(string, "w");
12756 if (appData.oldSaveStyle) {
12757 SaveGameOldStyle(f); /* also closes the file */
12759 snprintf(string, MSG_SIZ, "%s.pos.out", appData.cmailGameName);
12760 f = fopen(string, "w");
12761 SavePosition(f, 0, NULL); /* also closes the file */
12763 fprintf(f, "{--------------\n");
12764 PrintPosition(f, currentMove);
12765 fprintf(f, "--------------}\n\n");
12767 SaveGame(f, 0, NULL); /* also closes the file*/
12770 cmailMoveRegistered[lastLoadGameNumber - 1] = TRUE;
12771 nCmailMovesRegistered ++;
12772 } else if (nCmailGames == 1) {
12773 DisplayError(_("You have not made a move yet"), 0);
12784 static char *partCommandString = "cmail -xv%s -remail -game %s 2>&1";
12785 FILE *commandOutput;
12786 char buffer[MSG_SIZ], msg[MSG_SIZ], string[MSG_SIZ];
12787 int nBytes = 0; /* Suppress warnings on uninitialized variables */
12793 if (! cmailMsgLoaded) {
12794 DisplayError(_("The cmail message is not loaded.\nUse Reload CMail Message and make your move again."), 0);
12798 if (nCmailGames == nCmailResults) {
12799 DisplayError(_("No unfinished games"), 0);
12803 #if CMAIL_PROHIBIT_REMAIL
12804 if (cmailMailedMove) {
12805 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);
12806 DisplayError(msg, 0);
12811 if (! (cmailMailedMove || RegisterMove())) return;
12813 if ( cmailMailedMove
12814 || (nCmailMovesRegistered + nCmailResults == nCmailGames)) {
12815 snprintf(string, MSG_SIZ, partCommandString,
12816 appData.debugMode ? " -v" : "", appData.cmailGameName);
12817 commandOutput = popen(string, "r");
12819 if (commandOutput == NULL) {
12820 DisplayError(_("Failed to invoke cmail"), 0);
12822 for (nBuffers = 0; (! feof(commandOutput)); nBuffers ++) {
12823 nBytes = fread(buffer, 1, MSG_SIZ - 1, commandOutput);
12825 if (nBuffers > 1) {
12826 (void) memcpy(msg, buffer + nBytes, MSG_SIZ - nBytes - 1);
12827 (void) memcpy(msg + MSG_SIZ - nBytes - 1, buffer, nBytes);
12828 nBytes = MSG_SIZ - 1;
12830 (void) memcpy(msg, buffer, nBytes);
12832 *(msg + nBytes) = '\0'; /* \0 for end-of-string*/
12834 if(StrStr(msg, "Mailed cmail message to ") != NULL) {
12835 cmailMailedMove = TRUE; /* Prevent >1 moves */
12838 for (i = 0; i < nCmailGames; i ++) {
12839 if (cmailResult[i] == CMAIL_NOT_RESULT) {
12844 && ( (arcDir = (char *) getenv("CMAIL_ARCDIR"))
12846 snprintf(buffer, MSG_SIZ, "%s/%s.%s.archive",
12848 appData.cmailGameName,
12850 LoadGameFromFile(buffer, 1, buffer, FALSE);
12851 cmailMsgLoaded = FALSE;
12855 DisplayInformation(msg);
12856 pclose(commandOutput);
12859 if ((*cmailMsg) != '\0') {
12860 DisplayInformation(cmailMsg);
12865 #endif /* !WIN32 */
12874 int prependComma = 0;
12876 char string[MSG_SIZ]; /* Space for game-list */
12879 if (!cmailMsgLoaded) return "";
12881 if (cmailMailedMove) {
12882 snprintf(cmailMsg, MSG_SIZ, _("Waiting for reply from opponent\n"));
12884 /* Create a list of games left */
12885 snprintf(string, MSG_SIZ, "[");
12886 for (i = 0; i < nCmailGames; i ++) {
12887 if (! ( cmailMoveRegistered[i]
12888 || (cmailResult[i] == CMAIL_OLD_RESULT))) {
12889 if (prependComma) {
12890 snprintf(number, sizeof(number)/sizeof(number[0]), ",%d", i + 1);
12892 snprintf(number, sizeof(number)/sizeof(number[0]), "%d", i + 1);
12896 strcat(string, number);
12899 strcat(string, "]");
12901 if (nCmailMovesRegistered + nCmailResults == 0) {
12902 switch (nCmailGames) {
12904 snprintf(cmailMsg, MSG_SIZ, _("Still need to make move for game\n"));
12908 snprintf(cmailMsg, MSG_SIZ, _("Still need to make moves for both games\n"));
12912 snprintf(cmailMsg, MSG_SIZ, _("Still need to make moves for all %d games\n"),
12917 switch (nCmailGames - nCmailMovesRegistered - nCmailResults) {
12919 snprintf(cmailMsg, MSG_SIZ, _("Still need to make a move for game %s\n"),
12924 if (nCmailResults == nCmailGames) {
12925 snprintf(cmailMsg, MSG_SIZ, _("No unfinished games\n"));
12927 snprintf(cmailMsg, MSG_SIZ, _("Ready to send mail\n"));
12932 snprintf(cmailMsg, MSG_SIZ, _("Still need to make moves for games %s\n"),
12944 if (gameMode == Training)
12945 SetTrainingModeOff();
12948 cmailMsgLoaded = FALSE;
12949 if (appData.icsActive) {
12950 SendToICS(ics_prefix);
12951 SendToICS("refresh\n");
12956 ExitEvent (int status)
12960 /* Give up on clean exit */
12964 /* Keep trying for clean exit */
12968 if (appData.icsActive && appData.colorize) Colorize(ColorNone, FALSE);
12970 if (telnetISR != NULL) {
12971 RemoveInputSource(telnetISR);
12973 if (icsPR != NoProc) {
12974 DestroyChildProcess(icsPR, TRUE);
12977 /* [HGM] crash: leave writing PGN and position entirely to GameEnds() */
12978 GameEnds(gameInfo.result, gameInfo.resultDetails==NULL ? "xboard exit" : gameInfo.resultDetails, GE_PLAYER);
12980 /* [HGM] crash: the above GameEnds() is a dud if another one was running */
12981 /* make sure this other one finishes before killing it! */
12982 if(endingGame) { int count = 0;
12983 if(appData.debugMode) fprintf(debugFP, "ExitEvent() during GameEnds(), wait\n");
12984 while(endingGame && count++ < 10) DoSleep(1);
12985 if(appData.debugMode && endingGame) fprintf(debugFP, "GameEnds() seems stuck, proceed exiting\n");
12988 /* Kill off chess programs */
12989 if (first.pr != NoProc) {
12992 DoSleep( appData.delayBeforeQuit );
12993 SendToProgram("quit\n", &first);
12994 DoSleep( appData.delayAfterQuit );
12995 DestroyChildProcess(first.pr, 10 /* [AS] first.useSigterm */ );
12997 if (second.pr != NoProc) {
12998 DoSleep( appData.delayBeforeQuit );
12999 SendToProgram("quit\n", &second);
13000 DoSleep( appData.delayAfterQuit );
13001 DestroyChildProcess(second.pr, 10 /* [AS] second.useSigterm */ );
13003 if (first.isr != NULL) {
13004 RemoveInputSource(first.isr);
13006 if (second.isr != NULL) {
13007 RemoveInputSource(second.isr);
13010 if (pairing.pr != NoProc) SendToProgram("quit\n", &pairing);
13011 if (pairing.isr != NULL) RemoveInputSource(pairing.isr);
13013 ShutDownFrontEnd();
13020 if (appData.debugMode)
13021 fprintf(debugFP, "PauseEvent(): pausing %d\n", pausing);
13025 if (gameMode == MachinePlaysWhite ||
13026 gameMode == MachinePlaysBlack) {
13029 DisplayBothClocks();
13031 if (gameMode == PlayFromGameFile) {
13032 if (appData.timeDelay >= 0)
13033 AutoPlayGameLoop();
13034 } else if (gameMode == IcsExamining && pauseExamInvalid) {
13035 Reset(FALSE, TRUE);
13036 SendToICS(ics_prefix);
13037 SendToICS("refresh\n");
13038 } else if (currentMove < forwardMostMove) {
13039 ForwardInner(forwardMostMove);
13041 pauseExamInvalid = FALSE;
13043 switch (gameMode) {
13047 pauseExamForwardMostMove = forwardMostMove;
13048 pauseExamInvalid = FALSE;
13051 case IcsPlayingWhite:
13052 case IcsPlayingBlack:
13056 case PlayFromGameFile:
13057 (void) StopLoadGameTimer();
13061 case BeginningOfGame:
13062 if (appData.icsActive) return;
13063 /* else fall through */
13064 case MachinePlaysWhite:
13065 case MachinePlaysBlack:
13066 case TwoMachinesPlay:
13067 if (forwardMostMove == 0)
13068 return; /* don't pause if no one has moved */
13069 if ((gameMode == MachinePlaysWhite &&
13070 !WhiteOnMove(forwardMostMove)) ||
13071 (gameMode == MachinePlaysBlack &&
13072 WhiteOnMove(forwardMostMove))) {
13083 EditCommentEvent ()
13085 char title[MSG_SIZ];
13087 if (currentMove < 1 || parseList[currentMove - 1][0] == NULLCHAR) {
13088 safeStrCpy(title, _("Edit comment"), sizeof(title)/sizeof(title[0]));
13090 snprintf(title, MSG_SIZ, _("Edit comment on %d.%s%s"), (currentMove - 1) / 2 + 1,
13091 WhiteOnMove(currentMove - 1) ? " " : ".. ",
13092 parseList[currentMove - 1]);
13095 EditCommentPopUp(currentMove, title, commentList[currentMove]);
13102 char *tags = PGNTags(&gameInfo);
13104 EditTagsPopUp(tags, NULL);
13109 AnalyzeModeEvent ()
13111 if (appData.noChessProgram || gameMode == AnalyzeMode)
13114 if (gameMode != AnalyzeFile) {
13115 if (!appData.icsEngineAnalyze) {
13117 if (gameMode != EditGame) return;
13119 ResurrectChessProgram();
13120 SendToProgram("analyze\n", &first);
13121 first.analyzing = TRUE;
13122 /*first.maybeThinking = TRUE;*/
13123 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
13124 EngineOutputPopUp();
13126 if (!appData.icsEngineAnalyze) gameMode = AnalyzeMode;
13131 StartAnalysisClock();
13132 GetTimeMark(&lastNodeCountTime);
13137 AnalyzeFileEvent ()
13139 if (appData.noChessProgram || gameMode == AnalyzeFile)
13142 if (gameMode != AnalyzeMode) {
13144 if (gameMode != EditGame) return;
13145 ResurrectChessProgram();
13146 SendToProgram("analyze\n", &first);
13147 first.analyzing = TRUE;
13148 /*first.maybeThinking = TRUE;*/
13149 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
13150 EngineOutputPopUp();
13152 gameMode = AnalyzeFile;
13157 StartAnalysisClock();
13158 GetTimeMark(&lastNodeCountTime);
13160 if(appData.timeDelay > 0) StartLoadGameTimer((long)(1000.0 * appData.timeDelay));
13164 MachineWhiteEvent ()
13167 char *bookHit = NULL;
13169 if (appData.noChessProgram || (gameMode == MachinePlaysWhite))
13173 if (gameMode == PlayFromGameFile ||
13174 gameMode == TwoMachinesPlay ||
13175 gameMode == Training ||
13176 gameMode == AnalyzeMode ||
13177 gameMode == EndOfGame)
13180 if (gameMode == EditPosition)
13181 EditPositionDone(TRUE);
13183 if (!WhiteOnMove(currentMove)) {
13184 DisplayError(_("It is not White's turn"), 0);
13188 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile)
13191 if (gameMode == EditGame || gameMode == AnalyzeMode ||
13192 gameMode == AnalyzeFile)
13195 ResurrectChessProgram(); /* in case it isn't running */
13196 if(gameMode == BeginningOfGame) { /* [HGM] time odds: to get right odds in human mode */
13197 gameMode = MachinePlaysWhite;
13200 gameMode = MachinePlaysWhite;
13204 snprintf(buf, MSG_SIZ, "%s %s %s", gameInfo.white, _("vs."), gameInfo.black);
13206 if (first.sendName) {
13207 snprintf(buf, MSG_SIZ, "name %s\n", gameInfo.black);
13208 SendToProgram(buf, &first);
13210 if (first.sendTime) {
13211 if (first.useColors) {
13212 SendToProgram("black\n", &first); /*gnu kludge*/
13214 SendTimeRemaining(&first, TRUE);
13216 if (first.useColors) {
13217 SendToProgram("white\n", &first); // [HGM] book: send 'go' separately
13219 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: send go or retrieve book move
13220 SetMachineThinkingEnables();
13221 first.maybeThinking = TRUE;
13225 if (appData.autoFlipView && !flipView) {
13226 flipView = !flipView;
13227 DrawPosition(FALSE, NULL);
13228 DisplayBothClocks(); // [HGM] logo: clocks might have to be exchanged;
13231 if(bookHit) { // [HGM] book: simulate book reply
13232 static char bookMove[MSG_SIZ]; // a bit generous?
13234 programStats.nodes = programStats.depth = programStats.time =
13235 programStats.score = programStats.got_only_move = 0;
13236 sprintf(programStats.movelist, "%s (xbook)", bookHit);
13238 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
13239 strcat(bookMove, bookHit);
13240 HandleMachineMove(bookMove, &first);
13245 MachineBlackEvent ()
13248 char *bookHit = NULL;
13250 if (appData.noChessProgram || (gameMode == MachinePlaysBlack))
13254 if (gameMode == PlayFromGameFile ||
13255 gameMode == TwoMachinesPlay ||
13256 gameMode == Training ||
13257 gameMode == AnalyzeMode ||
13258 gameMode == EndOfGame)
13261 if (gameMode == EditPosition)
13262 EditPositionDone(TRUE);
13264 if (WhiteOnMove(currentMove)) {
13265 DisplayError(_("It is not Black's turn"), 0);
13269 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile)
13272 if (gameMode == EditGame || gameMode == AnalyzeMode ||
13273 gameMode == AnalyzeFile)
13276 ResurrectChessProgram(); /* in case it isn't running */
13277 gameMode = MachinePlaysBlack;
13281 snprintf(buf, MSG_SIZ, "%s %s %s", gameInfo.white, _("vs."), gameInfo.black);
13283 if (first.sendName) {
13284 snprintf(buf, MSG_SIZ, "name %s\n", gameInfo.white);
13285 SendToProgram(buf, &first);
13287 if (first.sendTime) {
13288 if (first.useColors) {
13289 SendToProgram("white\n", &first); /*gnu kludge*/
13291 SendTimeRemaining(&first, FALSE);
13293 if (first.useColors) {
13294 SendToProgram("black\n", &first); // [HGM] book: 'go' sent separately
13296 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: send go or retrieve book move
13297 SetMachineThinkingEnables();
13298 first.maybeThinking = TRUE;
13301 if (appData.autoFlipView && flipView) {
13302 flipView = !flipView;
13303 DrawPosition(FALSE, NULL);
13304 DisplayBothClocks(); // [HGM] logo: clocks might have to be exchanged;
13306 if(bookHit) { // [HGM] book: simulate book reply
13307 static char bookMove[MSG_SIZ]; // a bit generous?
13309 programStats.nodes = programStats.depth = programStats.time =
13310 programStats.score = programStats.got_only_move = 0;
13311 sprintf(programStats.movelist, "%s (xbook)", bookHit);
13313 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
13314 strcat(bookMove, bookHit);
13315 HandleMachineMove(bookMove, &first);
13321 DisplayTwoMachinesTitle ()
13324 if (appData.matchGames > 0) {
13325 if(appData.tourneyFile[0]) {
13326 snprintf(buf, MSG_SIZ, "%s %s %s (%d/%d%s)",
13327 gameInfo.white, _("vs."), gameInfo.black,
13328 nextGame+1, appData.matchGames+1,
13329 appData.tourneyType>0 ? "gt" : appData.tourneyType<0 ? "sw" : "rr");
13331 if (first.twoMachinesColor[0] == 'w') {
13332 snprintf(buf, MSG_SIZ, "%s %s %s (%d-%d-%d)",
13333 gameInfo.white, _("vs."), gameInfo.black,
13334 first.matchWins, second.matchWins,
13335 matchGame - 1 - (first.matchWins + second.matchWins));
13337 snprintf(buf, MSG_SIZ, "%s %s %s (%d-%d-%d)",
13338 gameInfo.white, _("vs."), gameInfo.black,
13339 second.matchWins, first.matchWins,
13340 matchGame - 1 - (first.matchWins + second.matchWins));
13343 snprintf(buf, MSG_SIZ, "%s %s %s", gameInfo.white, _("vs."), gameInfo.black);
13349 SettingsMenuIfReady ()
13351 if (second.lastPing != second.lastPong) {
13352 DisplayMessage("", _("Waiting for second chess program"));
13353 ScheduleDelayedEvent(SettingsMenuIfReady, 10); // [HGM] fast: lowered from 1000
13357 DisplayMessage("", "");
13358 SettingsPopUp(&second);
13362 WaitForEngine (ChessProgramState *cps, DelayedEventCallback retry)
13365 if (cps->pr == NoProc) {
13366 StartChessProgram(cps);
13367 if (cps->protocolVersion == 1) {
13370 /* kludge: allow timeout for initial "feature" command */
13372 snprintf(buf, MSG_SIZ, _("Starting %s chess program"), _(cps->which));
13373 DisplayMessage("", buf);
13374 ScheduleDelayedEvent(retry, FEATURE_TIMEOUT);
13382 TwoMachinesEvent P((void))
13386 ChessProgramState *onmove;
13387 char *bookHit = NULL;
13388 static int stalling = 0;
13392 if (appData.noChessProgram) return;
13394 switch (gameMode) {
13395 case TwoMachinesPlay:
13397 case MachinePlaysWhite:
13398 case MachinePlaysBlack:
13399 if (WhiteOnMove(forwardMostMove) == (gameMode == MachinePlaysWhite)) {
13400 DisplayError(_("Wait until your turn,\nor select Move Now"), 0);
13404 case BeginningOfGame:
13405 case PlayFromGameFile:
13408 if (gameMode != EditGame) return;
13411 EditPositionDone(TRUE);
13422 // forwardMostMove = currentMove;
13423 TruncateGame(); // [HGM] vari: MachineWhite and MachineBlack do this...
13425 if(!ResurrectChessProgram()) return; /* in case first program isn't running (unbalances its ping due to InitChessProgram!) */
13427 if(WaitForEngine(&second, TwoMachinesEventIfReady)) return; // (if needed:) started up second engine, so wait for features
13428 if(first.lastPing != first.lastPong) { // [HGM] wait till we are sure first engine has set up position
13429 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10);
13433 InitChessProgram(&second, FALSE); // unbalances ping of second engine
13434 SendToProgram("force\n", &second);
13436 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10);
13439 GetTimeMark(&now); // [HGM] matchpause: implement match pause after engine load
13440 if(appData.matchPause>10000 || appData.matchPause<10)
13441 appData.matchPause = 10000; /* [HGM] make pause adjustable */
13442 wait = SubtractTimeMarks(&now, &pauseStart);
13443 if(wait < appData.matchPause) {
13444 ScheduleDelayedEvent(TwoMachinesEventIfReady, appData.matchPause - wait);
13447 // we are now committed to starting the game
13449 DisplayMessage("", "");
13450 if (startedFromSetupPosition) {
13451 SendBoard(&second, backwardMostMove);
13452 if (appData.debugMode) {
13453 fprintf(debugFP, "Two Machines\n");
13456 for (i = backwardMostMove; i < forwardMostMove; i++) {
13457 SendMoveToProgram(i, &second);
13460 gameMode = TwoMachinesPlay;
13462 ModeHighlight(); // [HGM] logo: this triggers display update of logos
13464 DisplayTwoMachinesTitle();
13466 if ((first.twoMachinesColor[0] == 'w') == WhiteOnMove(forwardMostMove)) {
13471 if(appData.debugMode) fprintf(debugFP, "New game (%d): %s-%s (%c)\n", matchGame, first.tidy, second.tidy, first.twoMachinesColor[0]);
13472 SendToProgram(first.computerString, &first);
13473 if (first.sendName) {
13474 snprintf(buf, MSG_SIZ, "name %s\n", second.tidy);
13475 SendToProgram(buf, &first);
13477 SendToProgram(second.computerString, &second);
13478 if (second.sendName) {
13479 snprintf(buf, MSG_SIZ, "name %s\n", first.tidy);
13480 SendToProgram(buf, &second);
13484 if (!first.sendTime || !second.sendTime) {
13485 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
13486 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
13488 if (onmove->sendTime) {
13489 if (onmove->useColors) {
13490 SendToProgram(onmove->other->twoMachinesColor, onmove); /*gnu kludge*/
13492 SendTimeRemaining(onmove, WhiteOnMove(forwardMostMove));
13494 if (onmove->useColors) {
13495 SendToProgram(onmove->twoMachinesColor, onmove);
13497 bookHit = SendMoveToBookUser(forwardMostMove-1, onmove, TRUE); // [HGM] book: send go or retrieve book move
13498 // SendToProgram("go\n", onmove);
13499 onmove->maybeThinking = TRUE;
13500 SetMachineThinkingEnables();
13504 if(bookHit) { // [HGM] book: simulate book reply
13505 static char bookMove[MSG_SIZ]; // a bit generous?
13507 programStats.nodes = programStats.depth = programStats.time =
13508 programStats.score = programStats.got_only_move = 0;
13509 sprintf(programStats.movelist, "%s (xbook)", bookHit);
13511 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
13512 strcat(bookMove, bookHit);
13513 savedMessage = bookMove; // args for deferred call
13514 savedState = onmove;
13515 ScheduleDelayedEvent(DeferredBookMove, 1);
13522 if (gameMode == Training) {
13523 SetTrainingModeOff();
13524 gameMode = PlayFromGameFile;
13525 DisplayMessage("", _("Training mode off"));
13527 gameMode = Training;
13528 animateTraining = appData.animate;
13530 /* make sure we are not already at the end of the game */
13531 if (currentMove < forwardMostMove) {
13532 SetTrainingModeOn();
13533 DisplayMessage("", _("Training mode on"));
13535 gameMode = PlayFromGameFile;
13536 DisplayError(_("Already at end of game"), 0);
13545 if (!appData.icsActive) return;
13546 switch (gameMode) {
13547 case IcsPlayingWhite:
13548 case IcsPlayingBlack:
13551 case BeginningOfGame:
13559 EditPositionDone(TRUE);
13572 gameMode = IcsIdle;
13582 switch (gameMode) {
13584 SetTrainingModeOff();
13586 case MachinePlaysWhite:
13587 case MachinePlaysBlack:
13588 case BeginningOfGame:
13589 SendToProgram("force\n", &first);
13590 SetUserThinkingEnables();
13592 case PlayFromGameFile:
13593 (void) StopLoadGameTimer();
13594 if (gameFileFP != NULL) {
13599 EditPositionDone(TRUE);
13604 SendToProgram("force\n", &first);
13606 case TwoMachinesPlay:
13607 GameEnds(EndOfFile, NULL, GE_PLAYER);
13608 ResurrectChessProgram();
13609 SetUserThinkingEnables();
13612 ResurrectChessProgram();
13614 case IcsPlayingBlack:
13615 case IcsPlayingWhite:
13616 DisplayError(_("Warning: You are still playing a game"), 0);
13619 DisplayError(_("Warning: You are still observing a game"), 0);
13622 DisplayError(_("Warning: You are still examining a game"), 0);
13633 first.offeredDraw = second.offeredDraw = 0;
13635 if (gameMode == PlayFromGameFile) {
13636 whiteTimeRemaining = timeRemaining[0][currentMove];
13637 blackTimeRemaining = timeRemaining[1][currentMove];
13641 if (gameMode == MachinePlaysWhite ||
13642 gameMode == MachinePlaysBlack ||
13643 gameMode == TwoMachinesPlay ||
13644 gameMode == EndOfGame) {
13645 i = forwardMostMove;
13646 while (i > currentMove) {
13647 SendToProgram("undo\n", &first);
13650 if(!adjustedClock) {
13651 whiteTimeRemaining = timeRemaining[0][currentMove];
13652 blackTimeRemaining = timeRemaining[1][currentMove];
13653 DisplayBothClocks();
13655 if (whiteFlag || blackFlag) {
13656 whiteFlag = blackFlag = 0;
13661 gameMode = EditGame;
13668 EditPositionEvent ()
13670 if (gameMode == EditPosition) {
13676 if (gameMode != EditGame) return;
13678 gameMode = EditPosition;
13681 if (currentMove > 0)
13682 CopyBoard(boards[0], boards[currentMove]);
13684 blackPlaysFirst = !WhiteOnMove(currentMove);
13686 currentMove = forwardMostMove = backwardMostMove = 0;
13687 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
13694 /* [DM] icsEngineAnalyze - possible call from other functions */
13695 if (appData.icsEngineAnalyze) {
13696 appData.icsEngineAnalyze = FALSE;
13698 DisplayMessage("",_("Close ICS engine analyze..."));
13700 if (first.analysisSupport && first.analyzing) {
13701 SendToProgram("exit\n", &first);
13702 first.analyzing = FALSE;
13704 thinkOutput[0] = NULLCHAR;
13708 EditPositionDone (Boolean fakeRights)
13710 int king = gameInfo.variant == VariantKnightmate ? WhiteUnicorn : WhiteKing;
13712 startedFromSetupPosition = TRUE;
13713 InitChessProgram(&first, FALSE);
13714 if(fakeRights) { // [HGM] suppress this if we just pasted a FEN.
13715 boards[0][EP_STATUS] = EP_NONE;
13716 boards[0][CASTLING][2] = boards[0][CASTLING][5] = BOARD_WIDTH>>1;
13717 if(boards[0][0][BOARD_WIDTH>>1] == king) {
13718 boards[0][CASTLING][1] = boards[0][0][BOARD_LEFT] == WhiteRook ? 0 : NoRights;
13719 boards[0][CASTLING][0] = boards[0][0][BOARD_RGHT-1] == WhiteRook ? BOARD_RGHT-1 : NoRights;
13720 } else boards[0][CASTLING][2] = NoRights;
13721 if(boards[0][BOARD_HEIGHT-1][BOARD_WIDTH>>1] == WHITE_TO_BLACK king) {
13722 boards[0][CASTLING][4] = boards[0][BOARD_HEIGHT-1][BOARD_LEFT] == BlackRook ? 0 : NoRights;
13723 boards[0][CASTLING][3] = boards[0][BOARD_HEIGHT-1][BOARD_RGHT-1] == BlackRook ? BOARD_RGHT-1 : NoRights;
13724 } else boards[0][CASTLING][5] = NoRights;
13726 SendToProgram("force\n", &first);
13727 if (blackPlaysFirst) {
13728 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
13729 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
13730 currentMove = forwardMostMove = backwardMostMove = 1;
13731 CopyBoard(boards[1], boards[0]);
13733 currentMove = forwardMostMove = backwardMostMove = 0;
13735 SendBoard(&first, forwardMostMove);
13736 if (appData.debugMode) {
13737 fprintf(debugFP, "EditPosDone\n");
13740 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
13741 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
13742 gameMode = EditGame;
13744 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
13745 ClearHighlights(); /* [AS] */
13748 /* Pause for `ms' milliseconds */
13749 /* !! Ugh, this is a kludge. Fix it sometime. --tpm */
13751 TimeDelay (long ms)
13758 } while (SubtractTimeMarks(&m2, &m1) < ms);
13761 /* !! Ugh, this is a kludge. Fix it sometime. --tpm */
13763 SendMultiLineToICS (char *buf)
13765 char temp[MSG_SIZ+1], *p;
13772 strncpy(temp, buf, len);
13777 if (*p == '\n' || *p == '\r')
13782 strcat(temp, "\n");
13784 SendToPlayer(temp, strlen(temp));
13788 SetWhiteToPlayEvent ()
13790 if (gameMode == EditPosition) {
13791 blackPlaysFirst = FALSE;
13792 DisplayBothClocks(); /* works because currentMove is 0 */
13793 } else if (gameMode == IcsExamining) {
13794 SendToICS(ics_prefix);
13795 SendToICS("tomove white\n");
13800 SetBlackToPlayEvent ()
13802 if (gameMode == EditPosition) {
13803 blackPlaysFirst = TRUE;
13804 currentMove = 1; /* kludge */
13805 DisplayBothClocks();
13807 } else if (gameMode == IcsExamining) {
13808 SendToICS(ics_prefix);
13809 SendToICS("tomove black\n");
13814 EditPositionMenuEvent (ChessSquare selection, int x, int y)
13817 ChessSquare piece = boards[0][y][x];
13819 if (gameMode != EditPosition && gameMode != IcsExamining) return;
13821 switch (selection) {
13823 if (gameMode == IcsExamining && ics_type == ICS_FICS) {
13824 SendToICS(ics_prefix);
13825 SendToICS("bsetup clear\n");
13826 } else if (gameMode == IcsExamining && ics_type == ICS_ICC) {
13827 SendToICS(ics_prefix);
13828 SendToICS("clearboard\n");
13830 for (x = 0; x < BOARD_WIDTH; x++) { ChessSquare p = EmptySquare;
13831 if(x == BOARD_LEFT-1 || x == BOARD_RGHT) p = (ChessSquare) 0; /* [HGM] holdings */
13832 for (y = 0; y < BOARD_HEIGHT; y++) {
13833 if (gameMode == IcsExamining) {
13834 if (boards[currentMove][y][x] != EmptySquare) {
13835 snprintf(buf, MSG_SIZ, "%sx@%c%c\n", ics_prefix,
13840 boards[0][y][x] = p;
13845 if (gameMode == EditPosition) {
13846 DrawPosition(FALSE, boards[0]);
13851 SetWhiteToPlayEvent();
13855 SetBlackToPlayEvent();
13859 if (gameMode == IcsExamining) {
13860 if (x < BOARD_LEFT || x >= BOARD_RGHT) break; // [HGM] holdings
13861 snprintf(buf, MSG_SIZ, "%sx@%c%c\n", ics_prefix, AAA + x, ONE + y);
13864 if(x < BOARD_LEFT || x >= BOARD_RGHT) {
13865 if(x == BOARD_LEFT-2) {
13866 if(y < BOARD_HEIGHT-1-gameInfo.holdingsSize) break;
13867 boards[0][y][1] = 0;
13869 if(x == BOARD_RGHT+1) {
13870 if(y >= gameInfo.holdingsSize) break;
13871 boards[0][y][BOARD_WIDTH-2] = 0;
13874 boards[0][y][x] = EmptySquare;
13875 DrawPosition(FALSE, boards[0]);
13880 if(piece >= (int)WhitePawn && piece < (int)WhiteMan ||
13881 piece >= (int)BlackPawn && piece < (int)BlackMan ) {
13882 selection = (ChessSquare) (PROMOTED piece);
13883 } else if(piece == EmptySquare) selection = WhiteSilver;
13884 else selection = (ChessSquare)((int)piece - 1);
13888 if(piece > (int)WhiteMan && piece <= (int)WhiteKing ||
13889 piece > (int)BlackMan && piece <= (int)BlackKing ) {
13890 selection = (ChessSquare) (DEMOTED piece);
13891 } else if(piece == EmptySquare) selection = BlackSilver;
13892 else selection = (ChessSquare)((int)piece + 1);
13897 if(gameInfo.variant == VariantShatranj ||
13898 gameInfo.variant == VariantXiangqi ||
13899 gameInfo.variant == VariantCourier ||
13900 gameInfo.variant == VariantMakruk )
13901 selection = (ChessSquare)((int)selection - (int)WhiteQueen + (int)WhiteFerz);
13906 if(gameInfo.variant == VariantXiangqi)
13907 selection = (ChessSquare)((int)selection - (int)WhiteKing + (int)WhiteWazir);
13908 if(gameInfo.variant == VariantKnightmate)
13909 selection = (ChessSquare)((int)selection - (int)WhiteKing + (int)WhiteUnicorn);
13912 if (gameMode == IcsExamining) {
13913 if (x < BOARD_LEFT || x >= BOARD_RGHT) break; // [HGM] holdings
13914 snprintf(buf, MSG_SIZ, "%s%c@%c%c\n", ics_prefix,
13915 PieceToChar(selection), AAA + x, ONE + y);
13918 if(x < BOARD_LEFT || x >= BOARD_RGHT) {
13920 if(x == BOARD_LEFT-2 && selection >= BlackPawn) {
13921 n = PieceToNumber(selection - BlackPawn);
13922 if(n >= gameInfo.holdingsSize) { n = 0; selection = BlackPawn; }
13923 boards[0][BOARD_HEIGHT-1-n][0] = selection;
13924 boards[0][BOARD_HEIGHT-1-n][1]++;
13926 if(x == BOARD_RGHT+1 && selection < BlackPawn) {
13927 n = PieceToNumber(selection);
13928 if(n >= gameInfo.holdingsSize) { n = 0; selection = WhitePawn; }
13929 boards[0][n][BOARD_WIDTH-1] = selection;
13930 boards[0][n][BOARD_WIDTH-2]++;
13933 boards[0][y][x] = selection;
13934 DrawPosition(TRUE, boards[0]);
13936 fromX = fromY = -1;
13944 DropMenuEvent (ChessSquare selection, int x, int y)
13946 ChessMove moveType;
13948 switch (gameMode) {
13949 case IcsPlayingWhite:
13950 case MachinePlaysBlack:
13951 if (!WhiteOnMove(currentMove)) {
13952 DisplayMoveError(_("It is Black's turn"));
13955 moveType = WhiteDrop;
13957 case IcsPlayingBlack:
13958 case MachinePlaysWhite:
13959 if (WhiteOnMove(currentMove)) {
13960 DisplayMoveError(_("It is White's turn"));
13963 moveType = BlackDrop;
13966 moveType = WhiteOnMove(currentMove) ? WhiteDrop : BlackDrop;
13972 if (moveType == BlackDrop && selection < BlackPawn) {
13973 selection = (ChessSquare) ((int) selection
13974 + (int) BlackPawn - (int) WhitePawn);
13976 if (boards[currentMove][y][x] != EmptySquare) {
13977 DisplayMoveError(_("That square is occupied"));
13981 FinishMove(moveType, (int) selection, DROP_RANK, x, y, NULLCHAR);
13987 /* Accept a pending offer of any kind from opponent */
13989 if (appData.icsActive) {
13990 SendToICS(ics_prefix);
13991 SendToICS("accept\n");
13992 } else if (cmailMsgLoaded) {
13993 if (currentMove == cmailOldMove &&
13994 commentList[cmailOldMove] != NULL &&
13995 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
13996 "Black offers a draw" : "White offers a draw")) {
13998 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
13999 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_ACCEPT;
14001 DisplayError(_("There is no pending offer on this move"), 0);
14002 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
14005 /* Not used for offers from chess program */
14012 /* Decline a pending offer of any kind from opponent */
14014 if (appData.icsActive) {
14015 SendToICS(ics_prefix);
14016 SendToICS("decline\n");
14017 } else if (cmailMsgLoaded) {
14018 if (currentMove == cmailOldMove &&
14019 commentList[cmailOldMove] != NULL &&
14020 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
14021 "Black offers a draw" : "White offers a draw")) {
14023 AppendComment(cmailOldMove, "Draw declined", TRUE);
14024 DisplayComment(cmailOldMove - 1, "Draw declined");
14027 DisplayError(_("There is no pending offer on this move"), 0);
14030 /* Not used for offers from chess program */
14037 /* Issue ICS rematch command */
14038 if (appData.icsActive) {
14039 SendToICS(ics_prefix);
14040 SendToICS("rematch\n");
14047 /* Call your opponent's flag (claim a win on time) */
14048 if (appData.icsActive) {
14049 SendToICS(ics_prefix);
14050 SendToICS("flag\n");
14052 switch (gameMode) {
14055 case MachinePlaysWhite:
14058 GameEnds(GameIsDrawn, "Both players ran out of time",
14061 GameEnds(BlackWins, "Black wins on time", GE_PLAYER);
14063 DisplayError(_("Your opponent is not out of time"), 0);
14066 case MachinePlaysBlack:
14069 GameEnds(GameIsDrawn, "Both players ran out of time",
14072 GameEnds(WhiteWins, "White wins on time", GE_PLAYER);
14074 DisplayError(_("Your opponent is not out of time"), 0);
14082 ClockClick (int which)
14083 { // [HGM] code moved to back-end from winboard.c
14084 if(which) { // black clock
14085 if (gameMode == EditPosition || gameMode == IcsExamining) {
14086 if(!appData.pieceMenu && blackPlaysFirst) EditPositionMenuEvent(ClearBoard, 0, 0);
14087 SetBlackToPlayEvent();
14088 } else if ((gameMode == AnalyzeMode || gameMode == EditGame) && !blackFlag && WhiteOnMove(currentMove)) {
14089 UserMoveEvent((int)EmptySquare, DROP_RANK, 0, 0, 0); // [HGM] multi-move: if not out of time, enters null move
14090 } else if (shiftKey) {
14091 AdjustClock(which, -1);
14092 } else if (gameMode == IcsPlayingWhite ||
14093 gameMode == MachinePlaysBlack) {
14096 } else { // white clock
14097 if (gameMode == EditPosition || gameMode == IcsExamining) {
14098 if(!appData.pieceMenu && !blackPlaysFirst) EditPositionMenuEvent(ClearBoard, 0, 0);
14099 SetWhiteToPlayEvent();
14100 } else if ((gameMode == AnalyzeMode || gameMode == EditGame) && !whiteFlag && !WhiteOnMove(currentMove)) {
14101 UserMoveEvent((int)EmptySquare, DROP_RANK, 0, 0, 0); // [HGM] multi-move
14102 } else if (shiftKey) {
14103 AdjustClock(which, -1);
14104 } else if (gameMode == IcsPlayingBlack ||
14105 gameMode == MachinePlaysWhite) {
14114 /* Offer draw or accept pending draw offer from opponent */
14116 if (appData.icsActive) {
14117 /* Note: tournament rules require draw offers to be
14118 made after you make your move but before you punch
14119 your clock. Currently ICS doesn't let you do that;
14120 instead, you immediately punch your clock after making
14121 a move, but you can offer a draw at any time. */
14123 SendToICS(ics_prefix);
14124 SendToICS("draw\n");
14125 userOfferedDraw = TRUE; // [HGM] drawclaim: also set flag in ICS play
14126 } else if (cmailMsgLoaded) {
14127 if (currentMove == cmailOldMove &&
14128 commentList[cmailOldMove] != NULL &&
14129 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
14130 "Black offers a draw" : "White offers a draw")) {
14131 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
14132 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_ACCEPT;
14133 } else if (currentMove == cmailOldMove + 1) {
14134 char *offer = WhiteOnMove(cmailOldMove) ?
14135 "White offers a draw" : "Black offers a draw";
14136 AppendComment(currentMove, offer, TRUE);
14137 DisplayComment(currentMove - 1, offer);
14138 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_DRAW;
14140 DisplayError(_("You must make your move before offering a draw"), 0);
14141 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
14143 } else if (first.offeredDraw) {
14144 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
14146 if (first.sendDrawOffers) {
14147 SendToProgram("draw\n", &first);
14148 userOfferedDraw = TRUE;
14156 /* Offer Adjourn or accept pending Adjourn offer from opponent */
14158 if (appData.icsActive) {
14159 SendToICS(ics_prefix);
14160 SendToICS("adjourn\n");
14162 /* Currently GNU Chess doesn't offer or accept Adjourns */
14170 /* Offer Abort or accept pending Abort offer from opponent */
14172 if (appData.icsActive) {
14173 SendToICS(ics_prefix);
14174 SendToICS("abort\n");
14176 GameEnds(GameUnfinished, "Game aborted", GE_PLAYER);
14183 /* Resign. You can do this even if it's not your turn. */
14185 if (appData.icsActive) {
14186 SendToICS(ics_prefix);
14187 SendToICS("resign\n");
14189 switch (gameMode) {
14190 case MachinePlaysWhite:
14191 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
14193 case MachinePlaysBlack:
14194 GameEnds(BlackWins, "White resigns", GE_PLAYER);
14197 if (cmailMsgLoaded) {
14199 if (WhiteOnMove(cmailOldMove)) {
14200 GameEnds(BlackWins, "White resigns", GE_PLAYER);
14202 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
14204 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_RESIGN;
14215 StopObservingEvent ()
14217 /* Stop observing current games */
14218 SendToICS(ics_prefix);
14219 SendToICS("unobserve\n");
14223 StopExaminingEvent ()
14225 /* Stop observing current game */
14226 SendToICS(ics_prefix);
14227 SendToICS("unexamine\n");
14231 ForwardInner (int target)
14233 int limit; int oldSeekGraphUp = seekGraphUp;
14235 if (appData.debugMode)
14236 fprintf(debugFP, "ForwardInner(%d), current %d, forward %d\n",
14237 target, currentMove, forwardMostMove);
14239 if (gameMode == EditPosition)
14242 seekGraphUp = FALSE;
14243 MarkTargetSquares(1);
14245 if (gameMode == PlayFromGameFile && !pausing)
14248 if (gameMode == IcsExamining && pausing)
14249 limit = pauseExamForwardMostMove;
14251 limit = forwardMostMove;
14253 if (target > limit) target = limit;
14255 if (target > 0 && moveList[target - 1][0]) {
14256 int fromX, fromY, toX, toY;
14257 toX = moveList[target - 1][2] - AAA;
14258 toY = moveList[target - 1][3] - ONE;
14259 if (moveList[target - 1][1] == '@') {
14260 if (appData.highlightLastMove) {
14261 SetHighlights(-1, -1, toX, toY);
14264 fromX = moveList[target - 1][0] - AAA;
14265 fromY = moveList[target - 1][1] - ONE;
14266 if (target == currentMove + 1) {
14267 AnimateMove(boards[currentMove], fromX, fromY, toX, toY);
14269 if (appData.highlightLastMove) {
14270 SetHighlights(fromX, fromY, toX, toY);
14274 if (gameMode == EditGame || gameMode == AnalyzeMode ||
14275 gameMode == Training || gameMode == PlayFromGameFile ||
14276 gameMode == AnalyzeFile) {
14277 while (currentMove < target) {
14278 SendMoveToProgram(currentMove++, &first);
14281 currentMove = target;
14284 if (gameMode == EditGame || gameMode == EndOfGame) {
14285 whiteTimeRemaining = timeRemaining[0][currentMove];
14286 blackTimeRemaining = timeRemaining[1][currentMove];
14288 DisplayBothClocks();
14289 DisplayMove(currentMove - 1);
14290 DrawPosition(oldSeekGraphUp, boards[currentMove]);
14291 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
14292 if ( !matchMode && gameMode != Training) { // [HGM] PV info: routine tests if empty
14293 DisplayComment(currentMove - 1, commentList[currentMove]);
14301 if (gameMode == IcsExamining && !pausing) {
14302 SendToICS(ics_prefix);
14303 SendToICS("forward\n");
14305 ForwardInner(currentMove + 1);
14312 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
14313 /* to optimze, we temporarily turn off analysis mode while we feed
14314 * the remaining moves to the engine. Otherwise we get analysis output
14317 if (first.analysisSupport) {
14318 SendToProgram("exit\nforce\n", &first);
14319 first.analyzing = FALSE;
14323 if (gameMode == IcsExamining && !pausing) {
14324 SendToICS(ics_prefix);
14325 SendToICS("forward 999999\n");
14327 ForwardInner(forwardMostMove);
14330 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
14331 /* we have fed all the moves, so reactivate analysis mode */
14332 SendToProgram("analyze\n", &first);
14333 first.analyzing = TRUE;
14334 /*first.maybeThinking = TRUE;*/
14335 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
14340 BackwardInner (int target)
14342 int full_redraw = TRUE; /* [AS] Was FALSE, had to change it! */
14344 if (appData.debugMode)
14345 fprintf(debugFP, "BackwardInner(%d), current %d, forward %d\n",
14346 target, currentMove, forwardMostMove);
14348 if (gameMode == EditPosition) return;
14349 seekGraphUp = FALSE;
14350 MarkTargetSquares(1);
14351 if (currentMove <= backwardMostMove) {
14353 DrawPosition(full_redraw, boards[currentMove]);
14356 if (gameMode == PlayFromGameFile && !pausing)
14359 if (moveList[target][0]) {
14360 int fromX, fromY, toX, toY;
14361 toX = moveList[target][2] - AAA;
14362 toY = moveList[target][3] - ONE;
14363 if (moveList[target][1] == '@') {
14364 if (appData.highlightLastMove) {
14365 SetHighlights(-1, -1, toX, toY);
14368 fromX = moveList[target][0] - AAA;
14369 fromY = moveList[target][1] - ONE;
14370 if (target == currentMove - 1) {
14371 AnimateMove(boards[currentMove], toX, toY, fromX, fromY);
14373 if (appData.highlightLastMove) {
14374 SetHighlights(fromX, fromY, toX, toY);
14378 if (gameMode == EditGame || gameMode==AnalyzeMode ||
14379 gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
14380 while (currentMove > target) {
14381 if(moveList[currentMove-1][1] == '@' && moveList[currentMove-1][0] == '@') {
14382 // null move cannot be undone. Reload program with move history before it.
14384 for(i=target; i>backwardMostMove; i--) { // seek back to start or previous null move
14385 if(moveList[i-1][1] == '@' && moveList[i-1][0] == '@') break;
14387 SendBoard(&first, i);
14388 for(currentMove=i; currentMove<target; currentMove++) SendMoveToProgram(currentMove, &first);
14391 SendToProgram("undo\n", &first);
14395 currentMove = target;
14398 if (gameMode == EditGame || gameMode == EndOfGame) {
14399 whiteTimeRemaining = timeRemaining[0][currentMove];
14400 blackTimeRemaining = timeRemaining[1][currentMove];
14402 DisplayBothClocks();
14403 DisplayMove(currentMove - 1);
14404 DrawPosition(full_redraw, boards[currentMove]);
14405 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
14406 // [HGM] PV info: routine tests if comment empty
14407 DisplayComment(currentMove - 1, commentList[currentMove]);
14413 if (gameMode == IcsExamining && !pausing) {
14414 SendToICS(ics_prefix);
14415 SendToICS("backward\n");
14417 BackwardInner(currentMove - 1);
14424 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
14425 /* to optimize, we temporarily turn off analysis mode while we undo
14426 * all the moves. Otherwise we get analysis output after each undo.
14428 if (first.analysisSupport) {
14429 SendToProgram("exit\nforce\n", &first);
14430 first.analyzing = FALSE;
14434 if (gameMode == IcsExamining && !pausing) {
14435 SendToICS(ics_prefix);
14436 SendToICS("backward 999999\n");
14438 BackwardInner(backwardMostMove);
14441 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
14442 /* we have fed all the moves, so reactivate analysis mode */
14443 SendToProgram("analyze\n", &first);
14444 first.analyzing = TRUE;
14445 /*first.maybeThinking = TRUE;*/
14446 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
14453 if (gameMode == PlayFromGameFile && !pausing) PauseEvent();
14454 if (to >= forwardMostMove) to = forwardMostMove;
14455 if (to <= backwardMostMove) to = backwardMostMove;
14456 if (to < currentMove) {
14464 RevertEvent (Boolean annotate)
14466 if(PopTail(annotate)) { // [HGM] vari: restore old game tail
14469 if (gameMode != IcsExamining) {
14470 DisplayError(_("You are not examining a game"), 0);
14474 DisplayError(_("You can't revert while pausing"), 0);
14477 SendToICS(ics_prefix);
14478 SendToICS("revert\n");
14482 RetractMoveEvent ()
14484 switch (gameMode) {
14485 case MachinePlaysWhite:
14486 case MachinePlaysBlack:
14487 if (WhiteOnMove(forwardMostMove) == (gameMode == MachinePlaysWhite)) {
14488 DisplayError(_("Wait until your turn,\nor select Move Now"), 0);
14491 if (forwardMostMove < 2) return;
14492 currentMove = forwardMostMove = forwardMostMove - 2;
14493 whiteTimeRemaining = timeRemaining[0][currentMove];
14494 blackTimeRemaining = timeRemaining[1][currentMove];
14495 DisplayBothClocks();
14496 DisplayMove(currentMove - 1);
14497 ClearHighlights();/*!! could figure this out*/
14498 DrawPosition(TRUE, boards[currentMove]); /* [AS] Changed to full redraw! */
14499 SendToProgram("remove\n", &first);
14500 /*first.maybeThinking = TRUE;*/ /* GNU Chess does not ponder here */
14503 case BeginningOfGame:
14507 case IcsPlayingWhite:
14508 case IcsPlayingBlack:
14509 if (WhiteOnMove(forwardMostMove) == (gameMode == IcsPlayingWhite)) {
14510 SendToICS(ics_prefix);
14511 SendToICS("takeback 2\n");
14513 SendToICS(ics_prefix);
14514 SendToICS("takeback 1\n");
14523 ChessProgramState *cps;
14525 switch (gameMode) {
14526 case MachinePlaysWhite:
14527 if (!WhiteOnMove(forwardMostMove)) {
14528 DisplayError(_("It is your turn"), 0);
14533 case MachinePlaysBlack:
14534 if (WhiteOnMove(forwardMostMove)) {
14535 DisplayError(_("It is your turn"), 0);
14540 case TwoMachinesPlay:
14541 if (WhiteOnMove(forwardMostMove) ==
14542 (first.twoMachinesColor[0] == 'w')) {
14548 case BeginningOfGame:
14552 SendToProgram("?\n", cps);
14556 TruncateGameEvent ()
14559 if (gameMode != EditGame) return;
14566 CleanupTail(); // [HGM] vari: only keep current variation if we explicitly truncate
14567 if (forwardMostMove > currentMove) {
14568 if (gameInfo.resultDetails != NULL) {
14569 free(gameInfo.resultDetails);
14570 gameInfo.resultDetails = NULL;
14571 gameInfo.result = GameUnfinished;
14573 forwardMostMove = currentMove;
14574 HistorySet(parseList, backwardMostMove, forwardMostMove,
14582 if (appData.noChessProgram) return;
14583 switch (gameMode) {
14584 case MachinePlaysWhite:
14585 if (WhiteOnMove(forwardMostMove)) {
14586 DisplayError(_("Wait until your turn"), 0);
14590 case BeginningOfGame:
14591 case MachinePlaysBlack:
14592 if (!WhiteOnMove(forwardMostMove)) {
14593 DisplayError(_("Wait until your turn"), 0);
14598 DisplayError(_("No hint available"), 0);
14601 SendToProgram("hint\n", &first);
14602 hintRequested = TRUE;
14608 if (appData.noChessProgram) return;
14609 switch (gameMode) {
14610 case MachinePlaysWhite:
14611 if (WhiteOnMove(forwardMostMove)) {
14612 DisplayError(_("Wait until your turn"), 0);
14616 case BeginningOfGame:
14617 case MachinePlaysBlack:
14618 if (!WhiteOnMove(forwardMostMove)) {
14619 DisplayError(_("Wait until your turn"), 0);
14624 EditPositionDone(TRUE);
14626 case TwoMachinesPlay:
14631 SendToProgram("bk\n", &first);
14632 bookOutput[0] = NULLCHAR;
14633 bookRequested = TRUE;
14639 char *tags = PGNTags(&gameInfo);
14640 TagsPopUp(tags, CmailMsg());
14644 /* end button procedures */
14647 PrintPosition (FILE *fp, int move)
14651 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
14652 for (j = BOARD_LEFT; j < BOARD_RGHT; j++) {
14653 char c = PieceToChar(boards[move][i][j]);
14654 fputc(c == 'x' ? '.' : c, fp);
14655 fputc(j == BOARD_RGHT - 1 ? '\n' : ' ', fp);
14658 if ((gameMode == EditPosition) ? !blackPlaysFirst : (move % 2 == 0))
14659 fprintf(fp, "white to play\n");
14661 fprintf(fp, "black to play\n");
14665 PrintOpponents (FILE *fp)
14667 if (gameInfo.white != NULL) {
14668 fprintf(fp, "\t%s vs. %s\n", gameInfo.white, gameInfo.black);
14674 /* Find last component of program's own name, using some heuristics */
14676 TidyProgramName (char *prog, char *host, char buf[MSG_SIZ])
14679 int local = (strcmp(host, "localhost") == 0);
14680 while (!local && (p = strchr(prog, ';')) != NULL) {
14682 while (*p == ' ') p++;
14685 if (*prog == '"' || *prog == '\'') {
14686 q = strchr(prog + 1, *prog);
14688 q = strchr(prog, ' ');
14690 if (q == NULL) q = prog + strlen(prog);
14692 while (p >= prog && *p != '/' && *p != '\\') p--;
14694 if(p == prog && *p == '"') p++;
14696 if (q - p >= 4 && StrCaseCmp(q - 4, ".exe") == 0) *q = c, q -= 4; else *q = c;
14697 memcpy(buf, p, q - p);
14698 buf[q - p] = NULLCHAR;
14706 TimeControlTagValue ()
14709 if (!appData.clockMode) {
14710 safeStrCpy(buf, "-", sizeof(buf)/sizeof(buf[0]));
14711 } else if (movesPerSession > 0) {
14712 snprintf(buf, MSG_SIZ, "%d/%ld", movesPerSession, timeControl/1000);
14713 } else if (timeIncrement == 0) {
14714 snprintf(buf, MSG_SIZ, "%ld", timeControl/1000);
14716 snprintf(buf, MSG_SIZ, "%ld+%ld", timeControl/1000, timeIncrement/1000);
14718 return StrSave(buf);
14724 /* This routine is used only for certain modes */
14725 VariantClass v = gameInfo.variant;
14726 ChessMove r = GameUnfinished;
14729 if(gameMode == EditGame) { // [HGM] vari: do not erase result on EditGame
14730 r = gameInfo.result;
14731 p = gameInfo.resultDetails;
14732 gameInfo.resultDetails = NULL;
14734 ClearGameInfo(&gameInfo);
14735 gameInfo.variant = v;
14737 switch (gameMode) {
14738 case MachinePlaysWhite:
14739 gameInfo.event = StrSave( appData.pgnEventHeader );
14740 gameInfo.site = StrSave(HostName());
14741 gameInfo.date = PGNDate();
14742 gameInfo.round = StrSave("-");
14743 gameInfo.white = StrSave(first.tidy);
14744 gameInfo.black = StrSave(UserName());
14745 gameInfo.timeControl = TimeControlTagValue();
14748 case MachinePlaysBlack:
14749 gameInfo.event = StrSave( appData.pgnEventHeader );
14750 gameInfo.site = StrSave(HostName());
14751 gameInfo.date = PGNDate();
14752 gameInfo.round = StrSave("-");
14753 gameInfo.white = StrSave(UserName());
14754 gameInfo.black = StrSave(first.tidy);
14755 gameInfo.timeControl = TimeControlTagValue();
14758 case TwoMachinesPlay:
14759 gameInfo.event = StrSave( appData.pgnEventHeader );
14760 gameInfo.site = StrSave(HostName());
14761 gameInfo.date = PGNDate();
14764 snprintf(buf, MSG_SIZ, "%d", roundNr);
14765 gameInfo.round = StrSave(buf);
14767 gameInfo.round = StrSave("-");
14769 if (first.twoMachinesColor[0] == 'w') {
14770 gameInfo.white = StrSave(appData.pgnName[0][0] ? appData.pgnName[0] : first.tidy);
14771 gameInfo.black = StrSave(appData.pgnName[1][0] ? appData.pgnName[1] : second.tidy);
14773 gameInfo.white = StrSave(appData.pgnName[1][0] ? appData.pgnName[1] : second.tidy);
14774 gameInfo.black = StrSave(appData.pgnName[0][0] ? appData.pgnName[0] : first.tidy);
14776 gameInfo.timeControl = TimeControlTagValue();
14780 gameInfo.event = StrSave("Edited game");
14781 gameInfo.site = StrSave(HostName());
14782 gameInfo.date = PGNDate();
14783 gameInfo.round = StrSave("-");
14784 gameInfo.white = StrSave("-");
14785 gameInfo.black = StrSave("-");
14786 gameInfo.result = r;
14787 gameInfo.resultDetails = p;
14791 gameInfo.event = StrSave("Edited position");
14792 gameInfo.site = StrSave(HostName());
14793 gameInfo.date = PGNDate();
14794 gameInfo.round = StrSave("-");
14795 gameInfo.white = StrSave("-");
14796 gameInfo.black = StrSave("-");
14799 case IcsPlayingWhite:
14800 case IcsPlayingBlack:
14805 case PlayFromGameFile:
14806 gameInfo.event = StrSave("Game from non-PGN file");
14807 gameInfo.site = StrSave(HostName());
14808 gameInfo.date = PGNDate();
14809 gameInfo.round = StrSave("-");
14810 gameInfo.white = StrSave("?");
14811 gameInfo.black = StrSave("?");
14820 ReplaceComment (int index, char *text)
14826 if(index && sscanf(text, "%f/%d", &score, &len) == 2 &&
14827 pvInfoList[index-1].depth == len &&
14828 fabs(pvInfoList[index-1].score - score*100.) < 0.5 &&
14829 (p = strchr(text, '\n'))) text = p; // [HGM] strip off first line with PV info, if any
14830 while (*text == '\n') text++;
14831 len = strlen(text);
14832 while (len > 0 && text[len - 1] == '\n') len--;
14834 if (commentList[index] != NULL)
14835 free(commentList[index]);
14838 commentList[index] = NULL;
14841 if( *text == '{' && strchr(text, '}') || // [HGM] braces: if certainy malformed, put braces
14842 *text == '[' && strchr(text, ']') || // otherwise hope the user knows what he is doing
14843 *text == '(' && strchr(text, ')')) { // (perhaps check if this parses as comment-only?)
14844 commentList[index] = (char *) malloc(len + 2);
14845 strncpy(commentList[index], text, len);
14846 commentList[index][len] = '\n';
14847 commentList[index][len + 1] = NULLCHAR;
14849 // [HGM] braces: if text does not start with known OK delimiter, put braces around it.
14851 commentList[index] = (char *) malloc(len + 7);
14852 safeStrCpy(commentList[index], "{\n", 3);
14853 safeStrCpy(commentList[index]+2, text, len+1);
14854 commentList[index][len+2] = NULLCHAR;
14855 while(p = strchr(commentList[index], '}')) *p = ')'; // kill all } to make it one comment
14856 strcat(commentList[index], "\n}\n");
14861 CrushCRs (char *text)
14869 if (ch == '\r') continue;
14871 } while (ch != '\0');
14875 AppendComment (int index, char *text, Boolean addBraces)
14876 /* addBraces tells if we should add {} */
14881 if(appData.debugMode) fprintf(debugFP, "Append: in='%s' %d\n", text, addBraces); fflush(debugFP);
14882 text = GetInfoFromComment( index, text ); /* [HGM] PV time: strip PV info from comment */
14885 while (*text == '\n') text++;
14886 len = strlen(text);
14887 while (len > 0 && text[len - 1] == '\n') len--;
14888 text[len] = NULLCHAR;
14890 if (len == 0) return;
14892 if (commentList[index] != NULL) {
14893 Boolean addClosingBrace = addBraces;
14894 old = commentList[index];
14895 oldlen = strlen(old);
14896 while(commentList[index][oldlen-1] == '\n')
14897 commentList[index][--oldlen] = NULLCHAR;
14898 commentList[index] = (char *) malloc(oldlen + len + 6); // might waste 4
14899 safeStrCpy(commentList[index], old, oldlen + len + 6);
14901 // [HGM] braces: join "{A\n}\n" + "{\nB}" as "{A\nB\n}"
14902 if(commentList[index][oldlen-1] == '}' && (text[0] == '{' || addBraces == TRUE)) {
14903 if(addBraces == TRUE) addBraces = FALSE; else { text++; len--; }
14904 while (*text == '\n') { text++; len--; }
14905 commentList[index][--oldlen] = NULLCHAR;
14907 if(addBraces) strcat(commentList[index], addBraces == 2 ? "\n(" : "\n{\n");
14908 else strcat(commentList[index], "\n");
14909 strcat(commentList[index], text);
14910 if(addClosingBrace) strcat(commentList[index], addClosingBrace == 2 ? ")\n" : "\n}\n");
14911 else strcat(commentList[index], "\n");
14913 commentList[index] = (char *) malloc(len + 6); // perhaps wastes 4...
14915 safeStrCpy(commentList[index], addBraces == 2 ? "(" : "{\n", 3);
14916 else commentList[index][0] = NULLCHAR;
14917 strcat(commentList[index], text);
14918 strcat(commentList[index], addBraces == 2 ? ")\n" : "\n");
14919 if(addBraces == TRUE) strcat(commentList[index], "}\n");
14924 FindStr (char * text, char * sub_text)
14926 char * result = strstr( text, sub_text );
14928 if( result != NULL ) {
14929 result += strlen( sub_text );
14935 /* [AS] Try to extract PV info from PGN comment */
14936 /* [HGM] PV time: and then remove it, to prevent it appearing twice */
14938 GetInfoFromComment (int index, char * text)
14940 char * sep = text, *p;
14942 if( text != NULL && index > 0 ) {
14945 int time = -1, sec = 0, deci;
14946 char * s_eval = FindStr( text, "[%eval " );
14947 char * s_emt = FindStr( text, "[%emt " );
14949 if( s_eval != NULL || s_emt != NULL ) {
14953 if( s_eval != NULL ) {
14954 if( sscanf( s_eval, "%d,%d%c", &score, &depth, &delim ) != 3 ) {
14958 if( delim != ']' ) {
14963 if( s_emt != NULL ) {
14968 /* We expect something like: [+|-]nnn.nn/dd */
14971 if(*text != '{') return text; // [HGM] braces: must be normal comment
14973 sep = strchr( text, '/' );
14974 if( sep == NULL || sep < (text+4) ) {
14979 if(p[1] == '(') { // comment starts with PV
14980 p = strchr(p, ')'); // locate end of PV
14981 if(p == NULL || sep < p+5) return text;
14982 // at this point we have something like "{(.*) +0.23/6 ..."
14983 p = text; while(*++p != ')') p[-1] = *p; p[-1] = ')';
14984 *p = '\n'; while(*p == ' ' || *p == '\n') p++; *--p = '{';
14985 // we now moved the brace to behind the PV: "(.*) {+0.23/6 ..."
14987 time = -1; sec = -1; deci = -1;
14988 if( sscanf( p+1, "%d.%d/%d %d:%d", &score, &score_lo, &depth, &time, &sec ) != 5 &&
14989 sscanf( p+1, "%d.%d/%d %d.%d", &score, &score_lo, &depth, &time, &deci ) != 5 &&
14990 sscanf( p+1, "%d.%d/%d %d", &score, &score_lo, &depth, &time ) != 4 &&
14991 sscanf( p+1, "%d.%d/%d", &score, &score_lo, &depth ) != 3 ) {
14995 if( score_lo < 0 || score_lo >= 100 ) {
14999 if(sec >= 0) time = 600*time + 10*sec; else
15000 if(deci >= 0) time = 10*time + deci; else time *= 10; // deci-sec
15002 score = score >= 0 ? score*100 + score_lo : score*100 - score_lo;
15004 /* [HGM] PV time: now locate end of PV info */
15005 while( *++sep >= '0' && *sep <= '9'); // strip depth
15007 while( *++sep >= '0' && *sep <= '9' || *sep == '\n'); // strip time
15009 while( *++sep >= '0' && *sep <= '9'); // strip seconds
15011 while( *++sep >= '0' && *sep <= '9'); // strip fractional seconds
15012 while(*sep == ' ' || *sep == '\n' || *sep == '\r') sep++;
15023 pvInfoList[index-1].depth = depth;
15024 pvInfoList[index-1].score = score;
15025 pvInfoList[index-1].time = 10*time; // centi-sec
15026 if(*sep == '}') *sep = 0; else *--sep = '{';
15027 if(p != text) { while(*p++ = *sep++); sep = text; } // squeeze out space between PV and comment, and return both
15033 SendToProgram (char *message, ChessProgramState *cps)
15035 int count, outCount, error;
15038 if (cps->pr == NoProc) return;
15041 if (appData.debugMode) {
15044 fprintf(debugFP, "%ld >%-6s: %s",
15045 SubtractTimeMarks(&now, &programStartTime),
15046 cps->which, message);
15048 fprintf(serverFP, "%ld >%-6s: %s",
15049 SubtractTimeMarks(&now, &programStartTime),
15050 cps->which, message), fflush(serverFP);
15053 count = strlen(message);
15054 outCount = OutputToProcess(cps->pr, message, count, &error);
15055 if (outCount < count && !exiting
15056 && !endingGame) { /* [HGM] crash: to not hang GameEnds() writing to deceased engines */
15057 if(!cps->initDone) return; // [HGM] should not generate fatal error during engine load
15058 snprintf(buf, MSG_SIZ, _("Error writing to %s chess program"), _(cps->which));
15059 if(gameInfo.resultDetails==NULL) { /* [HGM] crash: if game in progress, give reason for abort */
15060 if((signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
15061 snprintf(buf, MSG_SIZ, _("%s program exits in draw position (%s)"), _(cps->which), cps->program);
15062 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(GameIsDrawn, buf, GE_XBOARD); return; }
15063 gameInfo.result = GameIsDrawn; /* [HGM] accept exit as draw claim */
15065 ChessMove res = cps->twoMachinesColor[0]=='w' ? BlackWins : WhiteWins;
15066 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(res, buf, GE_XBOARD); return; }
15067 gameInfo.result = res;
15069 gameInfo.resultDetails = StrSave(buf);
15071 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; return; }
15072 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, error, 1); else errorExitStatus = 1;
15077 ReceiveFromProgram (InputSourceRef isr, VOIDSTAR closure, char *message, int count, int error)
15081 ChessProgramState *cps = (ChessProgramState *)closure;
15083 if (isr != cps->isr) return; /* Killed intentionally */
15086 RemoveInputSource(cps->isr);
15087 if(!cps->initDone) return; // [HGM] should not generate fatal error during engine load
15088 snprintf(buf, MSG_SIZ, _("Error: %s chess program (%s) exited unexpectedly"),
15089 _(cps->which), cps->program);
15090 if(gameInfo.resultDetails==NULL) { /* [HGM] crash: if game in progress, give reason for abort */
15091 if((signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
15092 snprintf(buf, MSG_SIZ, _("%s program exits in draw position (%s)"), _(cps->which), cps->program);
15093 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(GameIsDrawn, buf, GE_XBOARD); return; }
15094 gameInfo.result = GameIsDrawn; /* [HGM] accept exit as draw claim */
15096 ChessMove res = cps->twoMachinesColor[0]=='w' ? BlackWins : WhiteWins;
15097 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(res, buf, GE_XBOARD); return; }
15098 gameInfo.result = res;
15100 gameInfo.resultDetails = StrSave(buf);
15102 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; return; }
15103 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, 0, 1); else errorExitStatus = 1;
15105 snprintf(buf, MSG_SIZ, _("Error reading from %s chess program (%s)"),
15106 _(cps->which), cps->program);
15107 RemoveInputSource(cps->isr);
15109 /* [AS] Program is misbehaving badly... kill it */
15110 if( count == -2 ) {
15111 DestroyChildProcess( cps->pr, 9 );
15115 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, error, 1); else errorExitStatus = 1;
15120 if ((end_str = strchr(message, '\r')) != NULL)
15121 *end_str = NULLCHAR;
15122 if ((end_str = strchr(message, '\n')) != NULL)
15123 *end_str = NULLCHAR;
15125 if (appData.debugMode) {
15126 TimeMark now; int print = 1;
15127 char *quote = ""; char c; int i;
15129 if(appData.engineComments != 1) { /* [HGM] debug: decide if protocol-violating output is written */
15130 char start = message[0];
15131 if(start >='A' && start <= 'Z') start += 'a' - 'A'; // be tolerant to capitalizing
15132 if(sscanf(message, "%d%c%d%d%d", &i, &c, &i, &i, &i) != 5 &&
15133 sscanf(message, "move %c", &c)!=1 && sscanf(message, "offer%c", &c)!=1 &&
15134 sscanf(message, "resign%c", &c)!=1 && sscanf(message, "feature %c", &c)!=1 &&
15135 sscanf(message, "error %c", &c)!=1 && sscanf(message, "illegal %c", &c)!=1 &&
15136 sscanf(message, "tell%c", &c)!=1 && sscanf(message, "0-1 %c", &c)!=1 &&
15137 sscanf(message, "1-0 %c", &c)!=1 && sscanf(message, "1/2-1/2 %c", &c)!=1 &&
15138 sscanf(message, "setboard %c", &c)!=1 && sscanf(message, "setup %c", &c)!=1 &&
15139 sscanf(message, "hint: %c", &c)!=1 &&
15140 sscanf(message, "pong %c", &c)!=1 && start != '#') {
15141 quote = appData.engineComments == 2 ? "# " : "### NON-COMPLIANT! ### ";
15142 print = (appData.engineComments >= 2);
15144 message[0] = start; // restore original message
15148 fprintf(debugFP, "%ld <%-6s: %s%s\n",
15149 SubtractTimeMarks(&now, &programStartTime), cps->which,
15153 fprintf(serverFP, "%ld <%-6s: %s%s\n",
15154 SubtractTimeMarks(&now, &programStartTime), cps->which,
15156 message), fflush(serverFP);
15160 /* [DM] if icsEngineAnalyze is active we block all whisper and kibitz output, because nobody want to see this */
15161 if (appData.icsEngineAnalyze) {
15162 if (strstr(message, "whisper") != NULL ||
15163 strstr(message, "kibitz") != NULL ||
15164 strstr(message, "tellics") != NULL) return;
15167 HandleMachineMove(message, cps);
15172 SendTimeControl (ChessProgramState *cps, int mps, long tc, int inc, int sd, int st)
15177 if( timeControl_2 > 0 ) {
15178 if( (gameMode == MachinePlaysBlack) || (gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b') ) {
15179 tc = timeControl_2;
15182 tc /= cps->timeOdds; /* [HGM] time odds: apply before telling engine */
15183 inc /= cps->timeOdds;
15184 st /= cps->timeOdds;
15186 seconds = (tc / 1000) % 60; /* [HGM] displaced to after applying odds */
15189 /* Set exact time per move, normally using st command */
15190 if (cps->stKludge) {
15191 /* GNU Chess 4 has no st command; uses level in a nonstandard way */
15193 if (seconds == 0) {
15194 snprintf(buf, MSG_SIZ, "level 1 %d\n", st/60);
15196 snprintf(buf, MSG_SIZ, "level 1 %d:%02d\n", st/60, seconds);
15199 snprintf(buf, MSG_SIZ, "st %d\n", st);
15202 /* Set conventional or incremental time control, using level command */
15203 if (seconds == 0) {
15204 /* Note old gnuchess bug -- minutes:seconds used to not work.
15205 Fixed in later versions, but still avoid :seconds
15206 when seconds is 0. */
15207 snprintf(buf, MSG_SIZ, "level %d %ld %g\n", mps, tc/60000, inc/1000.);
15209 snprintf(buf, MSG_SIZ, "level %d %ld:%02d %g\n", mps, tc/60000,
15210 seconds, inc/1000.);
15213 SendToProgram(buf, cps);
15215 /* Orthoganally (except for GNU Chess 4), limit time to st seconds */
15216 /* Orthogonally, limit search to given depth */
15218 if (cps->sdKludge) {
15219 snprintf(buf, MSG_SIZ, "depth\n%d\n", sd);
15221 snprintf(buf, MSG_SIZ, "sd %d\n", sd);
15223 SendToProgram(buf, cps);
15226 if(cps->nps >= 0) { /* [HGM] nps */
15227 if(cps->supportsNPS == FALSE)
15228 cps->nps = -1; // don't use if engine explicitly says not supported!
15230 snprintf(buf, MSG_SIZ, "nps %d\n", cps->nps);
15231 SendToProgram(buf, cps);
15236 ChessProgramState *
15238 /* [HGM] return pointer to 'first' or 'second', depending on who plays white */
15240 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b' ||
15241 gameMode == BeginningOfGame || gameMode == MachinePlaysBlack)
15247 SendTimeRemaining (ChessProgramState *cps, int machineWhite)
15249 char message[MSG_SIZ];
15252 /* Note: this routine must be called when the clocks are stopped
15253 or when they have *just* been set or switched; otherwise
15254 it will be off by the time since the current tick started.
15256 if (machineWhite) {
15257 time = whiteTimeRemaining / 10;
15258 otime = blackTimeRemaining / 10;
15260 time = blackTimeRemaining / 10;
15261 otime = whiteTimeRemaining / 10;
15263 /* [HGM] translate opponent's time by time-odds factor */
15264 otime = (otime * cps->other->timeOdds) / cps->timeOdds;
15266 if (time <= 0) time = 1;
15267 if (otime <= 0) otime = 1;
15269 snprintf(message, MSG_SIZ, "time %ld\n", time);
15270 SendToProgram(message, cps);
15272 snprintf(message, MSG_SIZ, "otim %ld\n", otime);
15273 SendToProgram(message, cps);
15277 BoolFeature (char **p, char *name, int *loc, ChessProgramState *cps)
15280 int len = strlen(name);
15283 if (strncmp((*p), name, len) == 0 && (*p)[len] == '=') {
15285 sscanf(*p, "%d", &val);
15287 while (**p && **p != ' ')
15289 snprintf(buf, MSG_SIZ, "accepted %s\n", name);
15290 SendToProgram(buf, cps);
15297 IntFeature (char **p, char *name, int *loc, ChessProgramState *cps)
15300 int len = strlen(name);
15301 if (strncmp((*p), name, len) == 0 && (*p)[len] == '=') {
15303 sscanf(*p, "%d", loc);
15304 while (**p && **p != ' ') (*p)++;
15305 snprintf(buf, MSG_SIZ, "accepted %s\n", name);
15306 SendToProgram(buf, cps);
15313 StringFeature (char **p, char *name, char loc[], ChessProgramState *cps)
15316 int len = strlen(name);
15317 if (strncmp((*p), name, len) == 0
15318 && (*p)[len] == '=' && (*p)[len+1] == '\"') {
15320 sscanf(*p, "%[^\"]", loc);
15321 while (**p && **p != '\"') (*p)++;
15322 if (**p == '\"') (*p)++;
15323 snprintf(buf, MSG_SIZ, "accepted %s\n", name);
15324 SendToProgram(buf, cps);
15331 ParseOption (Option *opt, ChessProgramState *cps)
15332 // [HGM] options: process the string that defines an engine option, and determine
15333 // name, type, default value, and allowed value range
15335 char *p, *q, buf[MSG_SIZ];
15336 int n, min = (-1)<<31, max = 1<<31, def;
15338 if(p = strstr(opt->name, " -spin ")) {
15339 if((n = sscanf(p, " -spin %d %d %d", &def, &min, &max)) < 3 ) return FALSE;
15340 if(max < min) max = min; // enforce consistency
15341 if(def < min) def = min;
15342 if(def > max) def = max;
15347 } else if((p = strstr(opt->name, " -slider "))) {
15348 // for now -slider is a synonym for -spin, to already provide compatibility with future polyglots
15349 if((n = sscanf(p, " -slider %d %d %d", &def, &min, &max)) < 3 ) return FALSE;
15350 if(max < min) max = min; // enforce consistency
15351 if(def < min) def = min;
15352 if(def > max) def = max;
15356 opt->type = Spin; // Slider;
15357 } else if((p = strstr(opt->name, " -string "))) {
15358 opt->textValue = p+9;
15359 opt->type = TextBox;
15360 } else if((p = strstr(opt->name, " -file "))) {
15361 // for now -file is a synonym for -string, to already provide compatibility with future polyglots
15362 opt->textValue = p+7;
15363 opt->type = FileName; // FileName;
15364 } else if((p = strstr(opt->name, " -path "))) {
15365 // for now -file is a synonym for -string, to already provide compatibility with future polyglots
15366 opt->textValue = p+7;
15367 opt->type = PathName; // PathName;
15368 } else if(p = strstr(opt->name, " -check ")) {
15369 if(sscanf(p, " -check %d", &def) < 1) return FALSE;
15370 opt->value = (def != 0);
15371 opt->type = CheckBox;
15372 } else if(p = strstr(opt->name, " -combo ")) {
15373 opt->textValue = (char*) (&cps->comboList[cps->comboCnt]); // cheat with pointer type
15374 cps->comboList[cps->comboCnt++] = q = p+8; // holds possible choices
15375 if(*q == '*') cps->comboList[cps->comboCnt-1]++;
15376 opt->value = n = 0;
15377 while(q = StrStr(q, " /// ")) {
15378 n++; *q = 0; // count choices, and null-terminate each of them
15380 if(*q == '*') { // remember default, which is marked with * prefix
15384 cps->comboList[cps->comboCnt++] = q;
15386 cps->comboList[cps->comboCnt++] = NULL;
15388 opt->type = ComboBox;
15389 } else if(p = strstr(opt->name, " -button")) {
15390 opt->type = Button;
15391 } else if(p = strstr(opt->name, " -save")) {
15392 opt->type = SaveButton;
15393 } else return FALSE;
15394 *p = 0; // terminate option name
15395 // now look if the command-line options define a setting for this engine option.
15396 if(cps->optionSettings && cps->optionSettings[0])
15397 p = strstr(cps->optionSettings, opt->name); else p = NULL;
15398 if(p && (p == cps->optionSettings || p[-1] == ',')) {
15399 snprintf(buf, MSG_SIZ, "option %s", p);
15400 if(p = strstr(buf, ",")) *p = 0;
15401 if(q = strchr(buf, '=')) switch(opt->type) {
15403 for(n=0; n<opt->max; n++)
15404 if(!strcmp(((char**)opt->textValue)[n], q+1)) opt->value = n;
15407 safeStrCpy(opt->textValue, q+1, MSG_SIZ - (opt->textValue - opt->name));
15411 opt->value = atoi(q+1);
15416 SendToProgram(buf, cps);
15422 FeatureDone (ChessProgramState *cps, int val)
15424 DelayedEventCallback cb = GetDelayedEvent();
15425 if ((cb == InitBackEnd3 && cps == &first) ||
15426 (cb == SettingsMenuIfReady && cps == &second) ||
15427 (cb == LoadEngine) ||
15428 (cb == TwoMachinesEventIfReady)) {
15429 CancelDelayedEvent();
15430 ScheduleDelayedEvent(cb, val ? 1 : 3600000);
15432 cps->initDone = val;
15435 /* Parse feature command from engine */
15437 ParseFeatures (char *args, ChessProgramState *cps)
15445 while (*p == ' ') p++;
15446 if (*p == NULLCHAR) return;
15448 if (BoolFeature(&p, "setboard", &cps->useSetboard, cps)) continue;
15449 if (BoolFeature(&p, "xedit", &cps->extendedEdit, cps)) continue;
15450 if (BoolFeature(&p, "time", &cps->sendTime, cps)) continue;
15451 if (BoolFeature(&p, "draw", &cps->sendDrawOffers, cps)) continue;
15452 if (BoolFeature(&p, "sigint", &cps->useSigint, cps)) continue;
15453 if (BoolFeature(&p, "sigterm", &cps->useSigterm, cps)) continue;
15454 if (BoolFeature(&p, "reuse", &val, cps)) {
15455 /* Engine can disable reuse, but can't enable it if user said no */
15456 if (!val) cps->reuse = FALSE;
15459 if (BoolFeature(&p, "analyze", &cps->analysisSupport, cps)) continue;
15460 if (StringFeature(&p, "myname", cps->tidy, cps)) {
15461 if (gameMode == TwoMachinesPlay) {
15462 DisplayTwoMachinesTitle();
15468 if (StringFeature(&p, "variants", cps->variants, cps)) continue;
15469 if (BoolFeature(&p, "san", &cps->useSAN, cps)) continue;
15470 if (BoolFeature(&p, "ping", &cps->usePing, cps)) continue;
15471 if (BoolFeature(&p, "playother", &cps->usePlayother, cps)) continue;
15472 if (BoolFeature(&p, "colors", &cps->useColors, cps)) continue;
15473 if (BoolFeature(&p, "usermove", &cps->useUsermove, cps)) continue;
15474 if (BoolFeature(&p, "ics", &cps->sendICS, cps)) continue;
15475 if (BoolFeature(&p, "name", &cps->sendName, cps)) continue;
15476 if (BoolFeature(&p, "pause", &val, cps)) continue; /* unused at present */
15477 if (IntFeature(&p, "done", &val, cps)) {
15478 FeatureDone(cps, val);
15481 /* Added by Tord: */
15482 if (BoolFeature(&p, "fen960", &cps->useFEN960, cps)) continue;
15483 if (BoolFeature(&p, "oocastle", &cps->useOOCastle, cps)) continue;
15484 /* End of additions by Tord */
15486 /* [HGM] added features: */
15487 if (BoolFeature(&p, "debug", &cps->debug, cps)) continue;
15488 if (BoolFeature(&p, "nps", &cps->supportsNPS, cps)) continue;
15489 if (IntFeature(&p, "level", &cps->maxNrOfSessions, cps)) continue;
15490 if (BoolFeature(&p, "memory", &cps->memSize, cps)) continue;
15491 if (BoolFeature(&p, "smp", &cps->maxCores, cps)) continue;
15492 if (StringFeature(&p, "egt", cps->egtFormats, cps)) continue;
15493 if (StringFeature(&p, "option", cps->option[cps->nrOptions].name, cps)) {
15494 if(!ParseOption(&(cps->option[cps->nrOptions++]), cps)) { // [HGM] options: add option feature
15495 snprintf(buf, MSG_SIZ, "rejected option %s\n", cps->option[--cps->nrOptions].name);
15496 SendToProgram(buf, cps);
15499 if(cps->nrOptions >= MAX_OPTIONS) {
15501 snprintf(buf, MSG_SIZ, _("%s engine has too many options\n"), _(cps->which));
15502 DisplayError(buf, 0);
15506 /* End of additions by HGM */
15508 /* unknown feature: complain and skip */
15510 while (*q && *q != '=') q++;
15511 snprintf(buf, MSG_SIZ,"rejected %.*s\n", (int)(q-p), p);
15512 SendToProgram(buf, cps);
15518 while (*p && *p != '\"') p++;
15519 if (*p == '\"') p++;
15521 while (*p && *p != ' ') p++;
15529 PeriodicUpdatesEvent (int newState)
15531 if (newState == appData.periodicUpdates)
15534 appData.periodicUpdates=newState;
15536 /* Display type changes, so update it now */
15537 // DisplayAnalysis();
15539 /* Get the ball rolling again... */
15541 AnalysisPeriodicEvent(1);
15542 StartAnalysisClock();
15547 PonderNextMoveEvent (int newState)
15549 if (newState == appData.ponderNextMove) return;
15550 if (gameMode == EditPosition) EditPositionDone(TRUE);
15552 SendToProgram("hard\n", &first);
15553 if (gameMode == TwoMachinesPlay) {
15554 SendToProgram("hard\n", &second);
15557 SendToProgram("easy\n", &first);
15558 thinkOutput[0] = NULLCHAR;
15559 if (gameMode == TwoMachinesPlay) {
15560 SendToProgram("easy\n", &second);
15563 appData.ponderNextMove = newState;
15567 NewSettingEvent (int option, int *feature, char *command, int value)
15571 if (gameMode == EditPosition) EditPositionDone(TRUE);
15572 snprintf(buf, MSG_SIZ,"%s%s %d\n", (option ? "option ": ""), command, value);
15573 if(feature == NULL || *feature) SendToProgram(buf, &first);
15574 if (gameMode == TwoMachinesPlay) {
15575 if(feature == NULL || feature[(int*)&second - (int*)&first]) SendToProgram(buf, &second);
15580 ShowThinkingEvent ()
15581 // [HGM] thinking: this routine is now also called from "Options -> Engine..." popup
15583 static int oldState = 2; // kludge alert! Neither true nor fals, so first time oldState is always updated
15584 int newState = appData.showThinking
15585 // [HGM] thinking: other features now need thinking output as well
15586 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp();
15588 if (oldState == newState) return;
15589 oldState = newState;
15590 if (gameMode == EditPosition) EditPositionDone(TRUE);
15592 SendToProgram("post\n", &first);
15593 if (gameMode == TwoMachinesPlay) {
15594 SendToProgram("post\n", &second);
15597 SendToProgram("nopost\n", &first);
15598 thinkOutput[0] = NULLCHAR;
15599 if (gameMode == TwoMachinesPlay) {
15600 SendToProgram("nopost\n", &second);
15603 // appData.showThinking = newState; // [HGM] thinking: responsible option should already have be changed when calling this routine!
15607 AskQuestionEvent (char *title, char *question, char *replyPrefix, char *which)
15609 ProcRef pr = (which[0] == '1') ? first.pr : second.pr;
15610 if (pr == NoProc) return;
15611 AskQuestion(title, question, replyPrefix, pr);
15615 TypeInEvent (char firstChar)
15617 if ((gameMode == BeginningOfGame && !appData.icsActive) ||
15618 gameMode == MachinePlaysWhite || gameMode == MachinePlaysBlack ||
15619 gameMode == AnalyzeMode || gameMode == EditGame ||
15620 gameMode == EditPosition || gameMode == IcsExamining ||
15621 gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
15622 isdigit(firstChar) && // [HGM] movenum: allow typing in of move nr in 'passive' modes
15623 ( gameMode == AnalyzeFile || gameMode == PlayFromGameFile ||
15624 gameMode == IcsObserving || gameMode == TwoMachinesPlay ) ||
15625 gameMode == Training) PopUpMoveDialog(firstChar);
15629 TypeInDoneEvent (char *move)
15632 int n, fromX, fromY, toX, toY;
15634 ChessMove moveType;
15637 if(gameMode == EditPosition && ParseFEN(board, &n, move) ) {
15638 EditPositionPasteFEN(move);
15641 // [HGM] movenum: allow move number to be typed in any mode
15642 if(sscanf(move, "%d", &n) == 1 && n != 0 ) {
15646 // undocumented kludge: allow command-line option to be typed in!
15647 // (potentially fatal, and does not implement the effect of the option.)
15648 // should only be used for options that are values on which future decisions will be made,
15649 // and definitely not on options that would be used during initialization.
15650 if(strstr(move, "!!! -") == move) {
15651 ParseArgsFromString(move+4);
15655 if (gameMode != EditGame && currentMove != forwardMostMove &&
15656 gameMode != Training) {
15657 DisplayMoveError(_("Displayed move is not current"));
15659 int ok = ParseOneMove(move, gameMode == EditPosition ? blackPlaysFirst : currentMove,
15660 &moveType, &fromX, &fromY, &toX, &toY, &promoChar);
15661 if(!ok && move[0] >= 'a') { move[0] += 'A' - 'a'; ok = 2; } // [HGM] try also capitalized
15662 if (ok==1 || ok && ParseOneMove(move, gameMode == EditPosition ? blackPlaysFirst : currentMove,
15663 &moveType, &fromX, &fromY, &toX, &toY, &promoChar)) {
15664 UserMoveEvent(fromX, fromY, toX, toY, promoChar);
15666 DisplayMoveError(_("Could not parse move"));
15672 DisplayMove (int moveNumber)
15674 char message[MSG_SIZ];
15676 char cpThinkOutput[MSG_SIZ];
15678 if(appData.noGUI) return; // [HGM] fast: suppress display of moves
15680 if (moveNumber == forwardMostMove - 1 ||
15681 gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
15683 safeStrCpy(cpThinkOutput, thinkOutput, sizeof(cpThinkOutput)/sizeof(cpThinkOutput[0]));
15685 if (strchr(cpThinkOutput, '\n')) {
15686 *strchr(cpThinkOutput, '\n') = NULLCHAR;
15689 *cpThinkOutput = NULLCHAR;
15692 /* [AS] Hide thinking from human user */
15693 if( appData.hideThinkingFromHuman && gameMode != TwoMachinesPlay ) {
15694 *cpThinkOutput = NULLCHAR;
15695 if( thinkOutput[0] != NULLCHAR ) {
15698 for( i=0; i<=hiddenThinkOutputState; i++ ) {
15699 cpThinkOutput[i] = '.';
15701 cpThinkOutput[i] = NULLCHAR;
15702 hiddenThinkOutputState = (hiddenThinkOutputState + 1) % 3;
15706 if (moveNumber == forwardMostMove - 1 &&
15707 gameInfo.resultDetails != NULL) {
15708 if (gameInfo.resultDetails[0] == NULLCHAR) {
15709 snprintf(res, MSG_SIZ, " %s", PGNResult(gameInfo.result));
15711 snprintf(res, MSG_SIZ, " {%s} %s",
15712 T_(gameInfo.resultDetails), PGNResult(gameInfo.result));
15718 if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
15719 DisplayMessage(res, cpThinkOutput);
15721 snprintf(message, MSG_SIZ, "%d.%s%s%s", moveNumber / 2 + 1,
15722 WhiteOnMove(moveNumber) ? " " : ".. ",
15723 parseList[moveNumber], res);
15724 DisplayMessage(message, cpThinkOutput);
15729 DisplayComment (int moveNumber, char *text)
15731 char title[MSG_SIZ];
15733 if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
15734 safeStrCpy(title, "Comment", sizeof(title)/sizeof(title[0]));
15736 snprintf(title,MSG_SIZ, "Comment on %d.%s%s", moveNumber / 2 + 1,
15737 WhiteOnMove(moveNumber) ? " " : ".. ",
15738 parseList[moveNumber]);
15740 if (text != NULL && (appData.autoDisplayComment || commentUp))
15741 CommentPopUp(title, text);
15744 /* This routine sends a ^C interrupt to gnuchess, to awaken it if it
15745 * might be busy thinking or pondering. It can be omitted if your
15746 * gnuchess is configured to stop thinking immediately on any user
15747 * input. However, that gnuchess feature depends on the FIONREAD
15748 * ioctl, which does not work properly on some flavors of Unix.
15751 Attention (ChessProgramState *cps)
15754 if (!cps->useSigint) return;
15755 if (appData.noChessProgram || (cps->pr == NoProc)) return;
15756 switch (gameMode) {
15757 case MachinePlaysWhite:
15758 case MachinePlaysBlack:
15759 case TwoMachinesPlay:
15760 case IcsPlayingWhite:
15761 case IcsPlayingBlack:
15764 /* Skip if we know it isn't thinking */
15765 if (!cps->maybeThinking) return;
15766 if (appData.debugMode)
15767 fprintf(debugFP, "Interrupting %s\n", cps->which);
15768 InterruptChildProcess(cps->pr);
15769 cps->maybeThinking = FALSE;
15774 #endif /*ATTENTION*/
15780 if (whiteTimeRemaining <= 0) {
15783 if (appData.icsActive) {
15784 if (appData.autoCallFlag &&
15785 gameMode == IcsPlayingBlack && !blackFlag) {
15786 SendToICS(ics_prefix);
15787 SendToICS("flag\n");
15791 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Both flags fell"));
15793 if(gameMode != TwoMachinesPlay) DisplayTitle(_("White's flag fell"));
15794 if (appData.autoCallFlag) {
15795 GameEnds(BlackWins, "Black wins on time", GE_XBOARD);
15802 if (blackTimeRemaining <= 0) {
15805 if (appData.icsActive) {
15806 if (appData.autoCallFlag &&
15807 gameMode == IcsPlayingWhite && !whiteFlag) {
15808 SendToICS(ics_prefix);
15809 SendToICS("flag\n");
15813 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Both flags fell"));
15815 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Black's flag fell"));
15816 if (appData.autoCallFlag) {
15817 GameEnds(WhiteWins, "White wins on time", GE_XBOARD);
15828 CheckTimeControl ()
15830 if (!appData.clockMode || appData.icsActive || searchTime || // [HGM] st: no inc in st mode
15831 gameMode == PlayFromGameFile || forwardMostMove == 0) return;
15834 * add time to clocks when time control is achieved ([HGM] now also used for increment)
15836 if ( !WhiteOnMove(forwardMostMove) ) {
15837 /* White made time control */
15838 lastWhite -= whiteTimeRemaining; // [HGM] contains start time, socalculate thinking time
15839 whiteTimeRemaining += GetTimeQuota((forwardMostMove-whiteStartMove-1)/2, lastWhite, whiteTC)
15840 /* [HGM] time odds: correct new time quota for time odds! */
15841 / WhitePlayer()->timeOdds;
15842 lastBlack = blackTimeRemaining; // [HGM] leave absolute time (after quota), so next switch we can us it to calculate thinking time
15844 lastBlack -= blackTimeRemaining;
15845 /* Black made time control */
15846 blackTimeRemaining += GetTimeQuota((forwardMostMove-blackStartMove-1)/2, lastBlack, blackTC)
15847 / WhitePlayer()->other->timeOdds;
15848 lastWhite = whiteTimeRemaining;
15853 DisplayBothClocks ()
15855 int wom = gameMode == EditPosition ?
15856 !blackPlaysFirst : WhiteOnMove(currentMove);
15857 DisplayWhiteClock(whiteTimeRemaining, wom);
15858 DisplayBlackClock(blackTimeRemaining, !wom);
15862 /* Timekeeping seems to be a portability nightmare. I think everyone
15863 has ftime(), but I'm really not sure, so I'm including some ifdefs
15864 to use other calls if you don't. Clocks will be less accurate if
15865 you have neither ftime nor gettimeofday.
15868 /* VS 2008 requires the #include outside of the function */
15869 #if !HAVE_GETTIMEOFDAY && HAVE_FTIME
15870 #include <sys/timeb.h>
15873 /* Get the current time as a TimeMark */
15875 GetTimeMark (TimeMark *tm)
15877 #if HAVE_GETTIMEOFDAY
15879 struct timeval timeVal;
15880 struct timezone timeZone;
15882 gettimeofday(&timeVal, &timeZone);
15883 tm->sec = (long) timeVal.tv_sec;
15884 tm->ms = (int) (timeVal.tv_usec / 1000L);
15886 #else /*!HAVE_GETTIMEOFDAY*/
15889 // include <sys/timeb.h> / moved to just above start of function
15890 struct timeb timeB;
15893 tm->sec = (long) timeB.time;
15894 tm->ms = (int) timeB.millitm;
15896 #else /*!HAVE_FTIME && !HAVE_GETTIMEOFDAY*/
15897 tm->sec = (long) time(NULL);
15903 /* Return the difference in milliseconds between two
15904 time marks. We assume the difference will fit in a long!
15907 SubtractTimeMarks (TimeMark *tm2, TimeMark *tm1)
15909 return 1000L*(tm2->sec - tm1->sec) +
15910 (long) (tm2->ms - tm1->ms);
15915 * Code to manage the game clocks.
15917 * In tournament play, black starts the clock and then white makes a move.
15918 * We give the human user a slight advantage if he is playing white---the
15919 * clocks don't run until he makes his first move, so it takes zero time.
15920 * Also, we don't account for network lag, so we could get out of sync
15921 * with GNU Chess's clock -- but then, referees are always right.
15924 static TimeMark tickStartTM;
15925 static long intendedTickLength;
15928 NextTickLength (long timeRemaining)
15930 long nominalTickLength, nextTickLength;
15932 if (timeRemaining > 0L && timeRemaining <= 10000L)
15933 nominalTickLength = 100L;
15935 nominalTickLength = 1000L;
15936 nextTickLength = timeRemaining % nominalTickLength;
15937 if (nextTickLength <= 0) nextTickLength += nominalTickLength;
15939 return nextTickLength;
15942 /* Adjust clock one minute up or down */
15944 AdjustClock (Boolean which, int dir)
15946 if(appData.autoCallFlag) { DisplayError(_("Clock adjustment not allowed in auto-flag mode"), 0); return; }
15947 if(which) blackTimeRemaining += 60000*dir;
15948 else whiteTimeRemaining += 60000*dir;
15949 DisplayBothClocks();
15950 adjustedClock = TRUE;
15953 /* Stop clocks and reset to a fresh time control */
15957 (void) StopClockTimer();
15958 if (appData.icsActive) {
15959 whiteTimeRemaining = blackTimeRemaining = 0;
15960 } else if (searchTime) {
15961 whiteTimeRemaining = 1000*searchTime / WhitePlayer()->timeOdds;
15962 blackTimeRemaining = 1000*searchTime / WhitePlayer()->other->timeOdds;
15963 } else { /* [HGM] correct new time quote for time odds */
15964 whiteTC = blackTC = fullTimeControlString;
15965 whiteTimeRemaining = GetTimeQuota(-1, 0, whiteTC) / WhitePlayer()->timeOdds;
15966 blackTimeRemaining = GetTimeQuota(-1, 0, blackTC) / WhitePlayer()->other->timeOdds;
15968 if (whiteFlag || blackFlag) {
15970 whiteFlag = blackFlag = FALSE;
15972 lastWhite = lastBlack = whiteStartMove = blackStartMove = 0;
15973 DisplayBothClocks();
15974 adjustedClock = FALSE;
15977 #define FUDGE 25 /* 25ms = 1/40 sec; should be plenty even for 50 Hz clocks */
15979 /* Decrement running clock by amount of time that has passed */
15983 long timeRemaining;
15984 long lastTickLength, fudge;
15987 if (!appData.clockMode) return;
15988 if (gameMode==AnalyzeMode || gameMode == AnalyzeFile) return;
15992 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
15994 /* Fudge if we woke up a little too soon */
15995 fudge = intendedTickLength - lastTickLength;
15996 if (fudge < 0 || fudge > FUDGE) fudge = 0;
15998 if (WhiteOnMove(forwardMostMove)) {
15999 if(whiteNPS >= 0) lastTickLength = 0;
16000 timeRemaining = whiteTimeRemaining -= lastTickLength;
16001 if(timeRemaining < 0 && !appData.icsActive) {
16002 GetTimeQuota((forwardMostMove-whiteStartMove-1)/2, 0, whiteTC); // sets suddenDeath & nextSession;
16003 if(suddenDeath) { // [HGM] if we run out of a non-last incremental session, go to the next
16004 whiteStartMove = forwardMostMove; whiteTC = nextSession;
16005 lastWhite= timeRemaining = whiteTimeRemaining += GetTimeQuota(-1, 0, whiteTC);
16008 DisplayWhiteClock(whiteTimeRemaining - fudge,
16009 WhiteOnMove(currentMove < forwardMostMove ? currentMove : forwardMostMove));
16011 if(blackNPS >= 0) lastTickLength = 0;
16012 timeRemaining = blackTimeRemaining -= lastTickLength;
16013 if(timeRemaining < 0 && !appData.icsActive) { // [HGM] if we run out of a non-last incremental session, go to the next
16014 GetTimeQuota((forwardMostMove-blackStartMove-1)/2, 0, blackTC);
16016 blackStartMove = forwardMostMove;
16017 lastBlack = timeRemaining = blackTimeRemaining += GetTimeQuota(-1, 0, blackTC=nextSession);
16020 DisplayBlackClock(blackTimeRemaining - fudge,
16021 !WhiteOnMove(currentMove < forwardMostMove ? currentMove : forwardMostMove));
16023 if (CheckFlags()) return;
16026 intendedTickLength = NextTickLength(timeRemaining - fudge) + fudge;
16027 StartClockTimer(intendedTickLength);
16029 /* if the time remaining has fallen below the alarm threshold, sound the
16030 * alarm. if the alarm has sounded and (due to a takeback or time control
16031 * with increment) the time remaining has increased to a level above the
16032 * threshold, reset the alarm so it can sound again.
16035 if (appData.icsActive && appData.icsAlarm) {
16037 /* make sure we are dealing with the user's clock */
16038 if (!( ((gameMode == IcsPlayingWhite) && WhiteOnMove(currentMove)) ||
16039 ((gameMode == IcsPlayingBlack) && !WhiteOnMove(currentMove))
16042 if (alarmSounded && (timeRemaining > appData.icsAlarmTime)) {
16043 alarmSounded = FALSE;
16044 } else if (!alarmSounded && (timeRemaining <= appData.icsAlarmTime)) {
16046 alarmSounded = TRUE;
16052 /* A player has just moved, so stop the previously running
16053 clock and (if in clock mode) start the other one.
16054 We redisplay both clocks in case we're in ICS mode, because
16055 ICS gives us an update to both clocks after every move.
16056 Note that this routine is called *after* forwardMostMove
16057 is updated, so the last fractional tick must be subtracted
16058 from the color that is *not* on move now.
16061 SwitchClocks (int newMoveNr)
16063 long lastTickLength;
16065 int flagged = FALSE;
16069 if (StopClockTimer() && appData.clockMode) {
16070 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
16071 if (!WhiteOnMove(forwardMostMove)) {
16072 if(blackNPS >= 0) lastTickLength = 0;
16073 blackTimeRemaining -= lastTickLength;
16074 /* [HGM] PGNtime: save time for PGN file if engine did not give it */
16075 // if(pvInfoList[forwardMostMove].time == -1)
16076 pvInfoList[forwardMostMove].time = // use GUI time
16077 (timeRemaining[1][forwardMostMove-1] - blackTimeRemaining)/10;
16079 if(whiteNPS >= 0) lastTickLength = 0;
16080 whiteTimeRemaining -= lastTickLength;
16081 /* [HGM] PGNtime: save time for PGN file if engine did not give it */
16082 // if(pvInfoList[forwardMostMove].time == -1)
16083 pvInfoList[forwardMostMove].time =
16084 (timeRemaining[0][forwardMostMove-1] - whiteTimeRemaining)/10;
16086 flagged = CheckFlags();
16088 forwardMostMove = newMoveNr; // [HGM] race: change stm when no timer interrupt scheduled
16089 CheckTimeControl();
16091 if (flagged || !appData.clockMode) return;
16093 switch (gameMode) {
16094 case MachinePlaysBlack:
16095 case MachinePlaysWhite:
16096 case BeginningOfGame:
16097 if (pausing) return;
16101 case PlayFromGameFile:
16109 if (searchTime) { // [HGM] st: set clock of player that has to move to max time
16110 if(WhiteOnMove(forwardMostMove))
16111 whiteTimeRemaining = 1000*searchTime / WhitePlayer()->timeOdds;
16112 else blackTimeRemaining = 1000*searchTime / WhitePlayer()->other->timeOdds;
16116 intendedTickLength = NextTickLength(WhiteOnMove(forwardMostMove) ?
16117 whiteTimeRemaining : blackTimeRemaining);
16118 StartClockTimer(intendedTickLength);
16122 /* Stop both clocks */
16126 long lastTickLength;
16129 if (!StopClockTimer()) return;
16130 if (!appData.clockMode) return;
16134 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
16135 if (WhiteOnMove(forwardMostMove)) {
16136 if(whiteNPS >= 0) lastTickLength = 0;
16137 whiteTimeRemaining -= lastTickLength;
16138 DisplayWhiteClock(whiteTimeRemaining, WhiteOnMove(currentMove));
16140 if(blackNPS >= 0) lastTickLength = 0;
16141 blackTimeRemaining -= lastTickLength;
16142 DisplayBlackClock(blackTimeRemaining, !WhiteOnMove(currentMove));
16147 /* Start clock of player on move. Time may have been reset, so
16148 if clock is already running, stop and restart it. */
16152 (void) StopClockTimer(); /* in case it was running already */
16153 DisplayBothClocks();
16154 if (CheckFlags()) return;
16156 if (!appData.clockMode) return;
16157 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) return;
16159 GetTimeMark(&tickStartTM);
16160 intendedTickLength = NextTickLength(WhiteOnMove(forwardMostMove) ?
16161 whiteTimeRemaining : blackTimeRemaining);
16163 /* [HGM] nps: figure out nps factors, by determining which engine plays white and/or black once and for all */
16164 whiteNPS = blackNPS = -1;
16165 if(gameMode == MachinePlaysWhite || gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w'
16166 || appData.zippyPlay && gameMode == IcsPlayingBlack) // first (perhaps only) engine has white
16167 whiteNPS = first.nps;
16168 if(gameMode == MachinePlaysBlack || gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b'
16169 || appData.zippyPlay && gameMode == IcsPlayingWhite) // first (perhaps only) engine has black
16170 blackNPS = first.nps;
16171 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b') // second only used in Two-Machines mode
16172 whiteNPS = second.nps;
16173 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w')
16174 blackNPS = second.nps;
16175 if(appData.debugMode) fprintf(debugFP, "nps: w=%d, b=%d\n", whiteNPS, blackNPS);
16177 StartClockTimer(intendedTickLength);
16181 TimeString (long ms)
16183 long second, minute, hour, day;
16185 static char buf[32];
16187 if (ms > 0 && ms <= 9900) {
16188 /* convert milliseconds to tenths, rounding up */
16189 double tenths = floor( ((double)(ms + 99L)) / 100.00 );
16191 snprintf(buf,sizeof(buf)/sizeof(buf[0]), " %03.1f ", tenths/10.0);
16195 /* convert milliseconds to seconds, rounding up */
16196 /* use floating point to avoid strangeness of integer division
16197 with negative dividends on many machines */
16198 second = (long) floor(((double) (ms + 999L)) / 1000.0);
16205 day = second / (60 * 60 * 24);
16206 second = second % (60 * 60 * 24);
16207 hour = second / (60 * 60);
16208 second = second % (60 * 60);
16209 minute = second / 60;
16210 second = second % 60;
16213 snprintf(buf, sizeof(buf)/sizeof(buf[0]), " %s%ld:%02ld:%02ld:%02ld ",
16214 sign, day, hour, minute, second);
16216 snprintf(buf, sizeof(buf)/sizeof(buf[0]), " %s%ld:%02ld:%02ld ", sign, hour, minute, second);
16218 snprintf(buf, sizeof(buf)/sizeof(buf[0]), " %s%2ld:%02ld ", sign, minute, second);
16225 * This is necessary because some C libraries aren't ANSI C compliant yet.
16228 StrStr (char *string, char *match)
16232 length = strlen(match);
16234 for (i = strlen(string) - length; i >= 0; i--, string++)
16235 if (!strncmp(match, string, length))
16242 StrCaseStr (char *string, char *match)
16246 length = strlen(match);
16248 for (i = strlen(string) - length; i >= 0; i--, string++) {
16249 for (j = 0; j < length; j++) {
16250 if (ToLower(match[j]) != ToLower(string[j]))
16253 if (j == length) return string;
16261 StrCaseCmp (char *s1, char *s2)
16266 c1 = ToLower(*s1++);
16267 c2 = ToLower(*s2++);
16268 if (c1 > c2) return 1;
16269 if (c1 < c2) return -1;
16270 if (c1 == NULLCHAR) return 0;
16278 return isupper(c) ? tolower(c) : c;
16285 return islower(c) ? toupper(c) : c;
16287 #endif /* !_amigados */
16294 if ((ret = (char *) malloc(strlen(s) + 1)))
16296 safeStrCpy(ret, s, strlen(s)+1);
16302 StrSavePtr (char *s, char **savePtr)
16307 if ((*savePtr = (char *) malloc(strlen(s) + 1))) {
16308 safeStrCpy(*savePtr, s, strlen(s)+1);
16320 clock = time((time_t *)NULL);
16321 tm = localtime(&clock);
16322 snprintf(buf, MSG_SIZ, "%04d.%02d.%02d",
16323 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
16324 return StrSave(buf);
16329 PositionToFEN (int move, char *overrideCastling)
16331 int i, j, fromX, fromY, toX, toY;
16338 whiteToPlay = (gameMode == EditPosition) ?
16339 !blackPlaysFirst : (move % 2 == 0);
16342 /* Piece placement data */
16343 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
16344 if(MSG_SIZ - (p - buf) < BOARD_RGHT - BOARD_LEFT + 20) { *p = 0; return StrSave(buf); }
16346 for (j = BOARD_LEFT; j < BOARD_RGHT; j++) {
16347 if (boards[move][i][j] == EmptySquare) {
16349 } else { ChessSquare piece = boards[move][i][j];
16350 if (emptycount > 0) {
16351 if(emptycount<10) /* [HGM] can be >= 10 */
16352 *p++ = '0' + emptycount;
16353 else { *p++ = '0' + emptycount/10; *p++ = '0' + emptycount%10; }
16356 if(PieceToChar(piece) == '+') {
16357 /* [HGM] write promoted pieces as '+<unpromoted>' (Shogi) */
16359 piece = (ChessSquare)(DEMOTED piece);
16361 *p++ = PieceToChar(piece);
16363 /* [HGM] flag promoted pieces as '<promoted>~' (Crazyhouse) */
16364 p[-1] = PieceToChar((ChessSquare)(DEMOTED piece));
16369 if (emptycount > 0) {
16370 if(emptycount<10) /* [HGM] can be >= 10 */
16371 *p++ = '0' + emptycount;
16372 else { *p++ = '0' + emptycount/10; *p++ = '0' + emptycount%10; }
16379 /* [HGM] print Crazyhouse or Shogi holdings */
16380 if( gameInfo.holdingsWidth ) {
16381 *(p-1) = '['; /* if we wanted to support BFEN, this could be '/' */
16383 for(i=0; i<gameInfo.holdingsSize; i++) { /* white holdings */
16384 piece = boards[move][i][BOARD_WIDTH-1];
16385 if( piece != EmptySquare )
16386 for(j=0; j<(int) boards[move][i][BOARD_WIDTH-2]; j++)
16387 *p++ = PieceToChar(piece);
16389 for(i=0; i<gameInfo.holdingsSize; i++) { /* black holdings */
16390 piece = boards[move][BOARD_HEIGHT-i-1][0];
16391 if( piece != EmptySquare )
16392 for(j=0; j<(int) boards[move][BOARD_HEIGHT-i-1][1]; j++)
16393 *p++ = PieceToChar(piece);
16396 if( q == p ) *p++ = '-';
16402 *p++ = whiteToPlay ? 'w' : 'b';
16405 if(q = overrideCastling) { // [HGM] FRC: override castling & e.p fields for non-compliant engines
16406 while(*p++ = *q++); if(q != overrideCastling+1) p[-1] = ' '; else --p;
16408 if(nrCastlingRights) {
16410 if(gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom) {
16411 /* [HGM] write directly from rights */
16412 if(boards[move][CASTLING][2] != NoRights &&
16413 boards[move][CASTLING][0] != NoRights )
16414 *p++ = boards[move][CASTLING][0] + AAA + 'A' - 'a';
16415 if(boards[move][CASTLING][2] != NoRights &&
16416 boards[move][CASTLING][1] != NoRights )
16417 *p++ = boards[move][CASTLING][1] + AAA + 'A' - 'a';
16418 if(boards[move][CASTLING][5] != NoRights &&
16419 boards[move][CASTLING][3] != NoRights )
16420 *p++ = boards[move][CASTLING][3] + AAA;
16421 if(boards[move][CASTLING][5] != NoRights &&
16422 boards[move][CASTLING][4] != NoRights )
16423 *p++ = boards[move][CASTLING][4] + AAA;
16426 /* [HGM] write true castling rights */
16427 if( nrCastlingRights == 6 ) {
16428 if(boards[move][CASTLING][0] == BOARD_RGHT-1 &&
16429 boards[move][CASTLING][2] != NoRights ) *p++ = 'K';
16430 if(boards[move][CASTLING][1] == BOARD_LEFT &&
16431 boards[move][CASTLING][2] != NoRights ) *p++ = 'Q';
16432 if(boards[move][CASTLING][3] == BOARD_RGHT-1 &&
16433 boards[move][CASTLING][5] != NoRights ) *p++ = 'k';
16434 if(boards[move][CASTLING][4] == BOARD_LEFT &&
16435 boards[move][CASTLING][5] != NoRights ) *p++ = 'q';
16438 if (q == p) *p++ = '-'; /* No castling rights */
16442 if(gameInfo.variant != VariantShogi && gameInfo.variant != VariantXiangqi &&
16443 gameInfo.variant != VariantShatranj && gameInfo.variant != VariantCourier && gameInfo.variant != VariantMakruk ) {
16444 /* En passant target square */
16445 if (move > backwardMostMove) {
16446 fromX = moveList[move - 1][0] - AAA;
16447 fromY = moveList[move - 1][1] - ONE;
16448 toX = moveList[move - 1][2] - AAA;
16449 toY = moveList[move - 1][3] - ONE;
16450 if (fromY == (whiteToPlay ? BOARD_HEIGHT-2 : 1) &&
16451 toY == (whiteToPlay ? BOARD_HEIGHT-4 : 3) &&
16452 boards[move][toY][toX] == (whiteToPlay ? BlackPawn : WhitePawn) &&
16454 /* 2-square pawn move just happened */
16456 *p++ = whiteToPlay ? '6'+BOARD_HEIGHT-8 : '3';
16460 } else if(move == backwardMostMove) {
16461 // [HGM] perhaps we should always do it like this, and forget the above?
16462 if((signed char)boards[move][EP_STATUS] >= 0) {
16463 *p++ = boards[move][EP_STATUS] + AAA;
16464 *p++ = whiteToPlay ? '6'+BOARD_HEIGHT-8 : '3';
16475 /* [HGM] find reversible plies */
16476 { int i = 0, j=move;
16478 if (appData.debugMode) { int k;
16479 fprintf(debugFP, "write FEN 50-move: %d %d %d\n", initialRulePlies, forwardMostMove, backwardMostMove);
16480 for(k=backwardMostMove; k<=forwardMostMove; k++)
16481 fprintf(debugFP, "e%d. p=%d\n", k, (signed char)boards[k][EP_STATUS]);
16485 while(j > backwardMostMove && (signed char)boards[j][EP_STATUS] <= EP_NONE) j--,i++;
16486 if( j == backwardMostMove ) i += initialRulePlies;
16487 sprintf(p, "%d ", i);
16488 p += i>=100 ? 4 : i >= 10 ? 3 : 2;
16490 /* Fullmove number */
16491 sprintf(p, "%d", (move / 2) + 1);
16493 return StrSave(buf);
16497 ParseFEN (Board board, int *blackPlaysFirst, char *fen)
16506 /* [HGM] by default clear Crazyhouse holdings, if present */
16507 if(gameInfo.holdingsWidth) {
16508 for(i=0; i<BOARD_HEIGHT; i++) {
16509 board[i][0] = EmptySquare; /* black holdings */
16510 board[i][BOARD_WIDTH-1] = EmptySquare; /* white holdings */
16511 board[i][1] = (ChessSquare) 0; /* black counts */
16512 board[i][BOARD_WIDTH-2] = (ChessSquare) 0; /* white counts */
16516 /* Piece placement data */
16517 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
16520 if (*p == '/' || *p == ' ' || (*p == '[' && i == 0) ) {
16521 if (*p == '/') p++;
16522 emptycount = gameInfo.boardWidth - j;
16523 while (emptycount--)
16524 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
16526 #if(BOARD_FILES >= 10)
16527 } else if(*p=='x' || *p=='X') { /* [HGM] X means 10 */
16528 p++; emptycount=10;
16529 if (j + emptycount > gameInfo.boardWidth) return FALSE;
16530 while (emptycount--)
16531 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
16533 } else if (isdigit(*p)) {
16534 emptycount = *p++ - '0';
16535 while(isdigit(*p)) emptycount = 10*emptycount + *p++ - '0'; /* [HGM] allow > 9 */
16536 if (j + emptycount > gameInfo.boardWidth) return FALSE;
16537 while (emptycount--)
16538 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
16539 } else if (*p == '+' || isalpha(*p)) {
16540 if (j >= gameInfo.boardWidth) return FALSE;
16542 piece = CharToPiece(*++p);
16543 if(piece == EmptySquare) return FALSE; /* unknown piece */
16544 piece = (ChessSquare) (PROMOTED piece ); p++;
16545 if(PieceToChar(piece) != '+') return FALSE; /* unpromotable piece */
16546 } else piece = CharToPiece(*p++);
16548 if(piece==EmptySquare) return FALSE; /* unknown piece */
16549 if(*p == '~') { /* [HGM] make it a promoted piece for Crazyhouse */
16550 piece = (ChessSquare) (PROMOTED piece);
16551 if(PieceToChar(piece) != '~') return FALSE; /* cannot be a promoted piece */
16554 board[i][(j++)+gameInfo.holdingsWidth] = piece;
16560 while (*p == '/' || *p == ' ') p++;
16562 /* [HGM] look for Crazyhouse holdings here */
16563 while(*p==' ') p++;
16564 if( gameInfo.holdingsWidth && p[-1] == '/' || *p == '[') {
16566 if(*p == '-' ) p++; /* empty holdings */ else {
16567 if( !gameInfo.holdingsWidth ) return FALSE; /* no room to put holdings! */
16568 /* if we would allow FEN reading to set board size, we would */
16569 /* have to add holdings and shift the board read so far here */
16570 while( (piece = CharToPiece(*p) ) != EmptySquare ) {
16572 if((int) piece >= (int) BlackPawn ) {
16573 i = (int)piece - (int)BlackPawn;
16574 i = PieceToNumber((ChessSquare)i);
16575 if( i >= gameInfo.holdingsSize ) return FALSE;
16576 board[BOARD_HEIGHT-1-i][0] = piece; /* black holdings */
16577 board[BOARD_HEIGHT-1-i][1]++; /* black counts */
16579 i = (int)piece - (int)WhitePawn;
16580 i = PieceToNumber((ChessSquare)i);
16581 if( i >= gameInfo.holdingsSize ) return FALSE;
16582 board[i][BOARD_WIDTH-1] = piece; /* white holdings */
16583 board[i][BOARD_WIDTH-2]++; /* black holdings */
16590 while(*p == ' ') p++;
16594 if(appData.colorNickNames) {
16595 if( c == appData.colorNickNames[0] ) c = 'w'; else
16596 if( c == appData.colorNickNames[1] ) c = 'b';
16600 *blackPlaysFirst = FALSE;
16603 *blackPlaysFirst = TRUE;
16609 /* [HGM] We NO LONGER ignore the rest of the FEN notation */
16610 /* return the extra info in global variiables */
16612 /* set defaults in case FEN is incomplete */
16613 board[EP_STATUS] = EP_UNKNOWN;
16614 for(i=0; i<nrCastlingRights; i++ ) {
16615 board[CASTLING][i] =
16616 gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom ? NoRights : initialRights[i];
16617 } /* assume possible unless obviously impossible */
16618 if(initialRights[0]!=NoRights && board[castlingRank[0]][initialRights[0]] != WhiteRook) board[CASTLING][0] = NoRights;
16619 if(initialRights[1]!=NoRights && board[castlingRank[1]][initialRights[1]] != WhiteRook) board[CASTLING][1] = NoRights;
16620 if(initialRights[2]!=NoRights && board[castlingRank[2]][initialRights[2]] != WhiteUnicorn
16621 && board[castlingRank[2]][initialRights[2]] != WhiteKing) board[CASTLING][2] = NoRights;
16622 if(initialRights[3]!=NoRights && board[castlingRank[3]][initialRights[3]] != BlackRook) board[CASTLING][3] = NoRights;
16623 if(initialRights[4]!=NoRights && board[castlingRank[4]][initialRights[4]] != BlackRook) board[CASTLING][4] = NoRights;
16624 if(initialRights[5]!=NoRights && board[castlingRank[5]][initialRights[5]] != BlackUnicorn
16625 && board[castlingRank[5]][initialRights[5]] != BlackKing) board[CASTLING][5] = NoRights;
16628 while(*p==' ') p++;
16629 if(nrCastlingRights) {
16630 if(*p=='K' || *p=='Q' || *p=='k' || *p=='q' || *p=='-') {
16631 /* castling indicator present, so default becomes no castlings */
16632 for(i=0; i<nrCastlingRights; i++ ) {
16633 board[CASTLING][i] = NoRights;
16636 while(*p=='K' || *p=='Q' || *p=='k' || *p=='q' || *p=='-' ||
16637 (gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom) &&
16638 ( *p >= 'a' && *p < 'a' + gameInfo.boardWidth) ||
16639 ( *p >= 'A' && *p < 'A' + gameInfo.boardWidth) ) {
16640 char c = *p++; int whiteKingFile=NoRights, blackKingFile=NoRights;
16642 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) {
16643 if(board[BOARD_HEIGHT-1][i] == BlackKing) blackKingFile = i;
16644 if(board[0 ][i] == WhiteKing) whiteKingFile = i;
16646 if(gameInfo.variant == VariantTwoKings || gameInfo.variant == VariantKnightmate)
16647 whiteKingFile = blackKingFile = BOARD_WIDTH >> 1; // for these variant scanning fails
16648 if(whiteKingFile == NoRights || board[0][whiteKingFile] != WhiteUnicorn
16649 && board[0][whiteKingFile] != WhiteKing) whiteKingFile = NoRights;
16650 if(blackKingFile == NoRights || board[BOARD_HEIGHT-1][blackKingFile] != BlackUnicorn
16651 && board[BOARD_HEIGHT-1][blackKingFile] != BlackKing) blackKingFile = NoRights;
16654 for(i=BOARD_RGHT-1; board[0][i]!=WhiteRook && i>whiteKingFile; i--);
16655 board[CASTLING][0] = i != whiteKingFile ? i : NoRights;
16656 board[CASTLING][2] = whiteKingFile;
16659 for(i=BOARD_LEFT; i<BOARD_RGHT && board[0][i]!=WhiteRook && i<whiteKingFile; i++);
16660 board[CASTLING][1] = i != whiteKingFile ? i : NoRights;
16661 board[CASTLING][2] = whiteKingFile;
16664 for(i=BOARD_RGHT-1; board[BOARD_HEIGHT-1][i]!=BlackRook && i>blackKingFile; i--);
16665 board[CASTLING][3] = i != blackKingFile ? i : NoRights;
16666 board[CASTLING][5] = blackKingFile;
16669 for(i=BOARD_LEFT; i<BOARD_RGHT && board[BOARD_HEIGHT-1][i]!=BlackRook && i<blackKingFile; i++);
16670 board[CASTLING][4] = i != blackKingFile ? i : NoRights;
16671 board[CASTLING][5] = blackKingFile;
16674 default: /* FRC castlings */
16675 if(c >= 'a') { /* black rights */
16676 for(i=BOARD_LEFT; i<BOARD_RGHT; i++)
16677 if(board[BOARD_HEIGHT-1][i] == BlackKing) break;
16678 if(i == BOARD_RGHT) break;
16679 board[CASTLING][5] = i;
16681 if(board[BOARD_HEIGHT-1][c] < BlackPawn ||
16682 board[BOARD_HEIGHT-1][c] >= BlackKing ) break;
16684 board[CASTLING][3] = c;
16686 board[CASTLING][4] = c;
16687 } else { /* white rights */
16688 for(i=BOARD_LEFT; i<BOARD_RGHT; i++)
16689 if(board[0][i] == WhiteKing) break;
16690 if(i == BOARD_RGHT) break;
16691 board[CASTLING][2] = i;
16692 c -= AAA - 'a' + 'A';
16693 if(board[0][c] >= WhiteKing) break;
16695 board[CASTLING][0] = c;
16697 board[CASTLING][1] = c;
16701 for(i=0; i<nrCastlingRights; i++)
16702 if(board[CASTLING][i] != NoRights) initialRights[i] = board[CASTLING][i];
16703 if (appData.debugMode) {
16704 fprintf(debugFP, "FEN castling rights:");
16705 for(i=0; i<nrCastlingRights; i++)
16706 fprintf(debugFP, " %d", board[CASTLING][i]);
16707 fprintf(debugFP, "\n");
16710 while(*p==' ') p++;
16713 /* read e.p. field in games that know e.p. capture */
16714 if(gameInfo.variant != VariantShogi && gameInfo.variant != VariantXiangqi &&
16715 gameInfo.variant != VariantShatranj && gameInfo.variant != VariantCourier && gameInfo.variant != VariantMakruk ) {
16717 p++; board[EP_STATUS] = EP_NONE;
16719 char c = *p++ - AAA;
16721 if(c < BOARD_LEFT || c >= BOARD_RGHT) return TRUE;
16722 if(*p >= '0' && *p <='9') p++;
16723 board[EP_STATUS] = c;
16728 if(sscanf(p, "%d", &i) == 1) {
16729 FENrulePlies = i; /* 50-move ply counter */
16730 /* (The move number is still ignored) */
16737 EditPositionPasteFEN (char *fen)
16740 Board initial_position;
16742 if (!ParseFEN(initial_position, &blackPlaysFirst, fen)) {
16743 DisplayError(_("Bad FEN position in clipboard"), 0);
16746 int savedBlackPlaysFirst = blackPlaysFirst;
16747 EditPositionEvent();
16748 blackPlaysFirst = savedBlackPlaysFirst;
16749 CopyBoard(boards[0], initial_position);
16750 initialRulePlies = FENrulePlies; /* [HGM] copy FEN attributes as well */
16751 EditPositionDone(FALSE); // [HGM] fake: do not fake rights if we had FEN
16752 DisplayBothClocks();
16753 DrawPosition(FALSE, boards[currentMove]);
16758 static char cseq[12] = "\\ ";
16761 set_cont_sequence (char *new_seq)
16766 // handle bad attempts to set the sequence
16768 return 0; // acceptable error - no debug
16770 len = strlen(new_seq);
16771 ret = (len > 0) && (len < sizeof(cseq));
16773 safeStrCpy(cseq, new_seq, sizeof(cseq)/sizeof(cseq[0]));
16774 else if (appData.debugMode)
16775 fprintf(debugFP, "Invalid continuation sequence \"%s\" (maximum length is: %u)\n", new_seq, (unsigned) sizeof(cseq)-1);
16780 reformat a source message so words don't cross the width boundary. internal
16781 newlines are not removed. returns the wrapped size (no null character unless
16782 included in source message). If dest is NULL, only calculate the size required
16783 for the dest buffer. lp argument indicats line position upon entry, and it's
16784 passed back upon exit.
16787 wrap (char *dest, char *src, int count, int width, int *lp)
16789 int len, i, ansi, cseq_len, line, old_line, old_i, old_len, clen;
16791 cseq_len = strlen(cseq);
16792 old_line = line = *lp;
16793 ansi = len = clen = 0;
16795 for (i=0; i < count; i++)
16797 if (src[i] == '\033')
16800 // if we hit the width, back up
16801 if (!ansi && (line >= width) && src[i] != '\n' && src[i] != ' ')
16803 // store i & len in case the word is too long
16804 old_i = i, old_len = len;
16806 // find the end of the last word
16807 while (i && src[i] != ' ' && src[i] != '\n')
16813 // word too long? restore i & len before splitting it
16814 if ((old_i-i+clen) >= width)
16821 if (i && src[i-1] == ' ')
16824 if (src[i] != ' ' && src[i] != '\n')
16831 // now append the newline and continuation sequence
16836 strncpy(dest+len, cseq, cseq_len);
16844 dest[len] = src[i];
16848 if (src[i] == '\n')
16853 if (dest && appData.debugMode)
16855 fprintf(debugFP, "wrap(count:%d,width:%d,line:%d,len:%d,*lp:%d,src: ",
16856 count, width, line, len, *lp);
16857 show_bytes(debugFP, src, count);
16858 fprintf(debugFP, "\ndest: ");
16859 show_bytes(debugFP, dest, len);
16860 fprintf(debugFP, "\n");
16862 *lp = dest ? line : old_line;
16867 // [HGM] vari: routines for shelving variations
16868 Boolean modeRestore = FALSE;
16871 PushInner (int firstMove, int lastMove)
16873 int i, j, nrMoves = lastMove - firstMove;
16875 // push current tail of game on stack
16876 savedResult[storedGames] = gameInfo.result;
16877 savedDetails[storedGames] = gameInfo.resultDetails;
16878 gameInfo.resultDetails = NULL;
16879 savedFirst[storedGames] = firstMove;
16880 savedLast [storedGames] = lastMove;
16881 savedFramePtr[storedGames] = framePtr;
16882 framePtr -= nrMoves; // reserve space for the boards
16883 for(i=nrMoves; i>=1; i--) { // copy boards to stack, working downwards, in case of overlap
16884 CopyBoard(boards[framePtr+i], boards[firstMove+i]);
16885 for(j=0; j<MOVE_LEN; j++)
16886 moveList[framePtr+i][j] = moveList[firstMove+i-1][j];
16887 for(j=0; j<2*MOVE_LEN; j++)
16888 parseList[framePtr+i][j] = parseList[firstMove+i-1][j];
16889 timeRemaining[0][framePtr+i] = timeRemaining[0][firstMove+i];
16890 timeRemaining[1][framePtr+i] = timeRemaining[1][firstMove+i];
16891 pvInfoList[framePtr+i] = pvInfoList[firstMove+i-1];
16892 pvInfoList[firstMove+i-1].depth = 0;
16893 commentList[framePtr+i] = commentList[firstMove+i];
16894 commentList[firstMove+i] = NULL;
16898 forwardMostMove = firstMove; // truncate game so we can start variation
16902 PushTail (int firstMove, int lastMove)
16904 if(appData.icsActive) { // only in local mode
16905 forwardMostMove = currentMove; // mimic old ICS behavior
16908 if(storedGames >= MAX_VARIATIONS-2) return; // leave one for PV-walk
16910 PushInner(firstMove, lastMove);
16911 if(storedGames == 1) GreyRevert(FALSE);
16912 if(gameMode == PlayFromGameFile) gameMode = EditGame, modeRestore = TRUE;
16916 PopInner (Boolean annotate)
16919 char buf[8000], moveBuf[20];
16921 ToNrEvent(savedFirst[storedGames-1]); // sets currentMove
16922 storedGames--; // do this after ToNrEvent, to make sure HistorySet will refresh entire game after PopInner returns
16923 nrMoves = savedLast[storedGames] - currentMove;
16926 if(!WhiteOnMove(currentMove))
16927 snprintf(buf, sizeof(buf)/sizeof(buf[0]),"(%d...", (currentMove+2)>>1);
16928 else safeStrCpy(buf, "(", sizeof(buf)/sizeof(buf[0]));
16929 for(i=currentMove; i<forwardMostMove; i++) {
16931 snprintf(moveBuf, sizeof(moveBuf)/sizeof(moveBuf[0]), " %d. %s", (i+2)>>1, SavePart(parseList[i]));
16932 else snprintf(moveBuf, sizeof(moveBuf)/sizeof(moveBuf[0])," %s", SavePart(parseList[i]));
16933 strcat(buf, moveBuf);
16934 if(commentList[i]) { strcat(buf, " "); strcat(buf, commentList[i]); }
16935 if(!--cnt) { strcat(buf, "\n"); cnt = 10; }
16939 for(i=1; i<=nrMoves; i++) { // copy last variation back
16940 CopyBoard(boards[currentMove+i], boards[framePtr+i]);
16941 for(j=0; j<MOVE_LEN; j++)
16942 moveList[currentMove+i-1][j] = moveList[framePtr+i][j];
16943 for(j=0; j<2*MOVE_LEN; j++)
16944 parseList[currentMove+i-1][j] = parseList[framePtr+i][j];
16945 timeRemaining[0][currentMove+i] = timeRemaining[0][framePtr+i];
16946 timeRemaining[1][currentMove+i] = timeRemaining[1][framePtr+i];
16947 pvInfoList[currentMove+i-1] = pvInfoList[framePtr+i];
16948 if(commentList[currentMove+i]) free(commentList[currentMove+i]);
16949 commentList[currentMove+i] = commentList[framePtr+i];
16950 commentList[framePtr+i] = NULL;
16952 if(annotate) AppendComment(currentMove+1, buf, FALSE);
16953 framePtr = savedFramePtr[storedGames];
16954 gameInfo.result = savedResult[storedGames];
16955 if(gameInfo.resultDetails != NULL) {
16956 free(gameInfo.resultDetails);
16958 gameInfo.resultDetails = savedDetails[storedGames];
16959 forwardMostMove = currentMove + nrMoves;
16963 PopTail (Boolean annotate)
16965 if(appData.icsActive) return FALSE; // only in local mode
16966 if(!storedGames) return FALSE; // sanity
16967 CommentPopDown(); // make sure no stale variation comments to the destroyed line can remain open
16969 PopInner(annotate);
16970 if(currentMove < forwardMostMove) ForwardEvent(); else
16971 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
16973 if(storedGames == 0) { GreyRevert(TRUE); if(modeRestore) modeRestore = FALSE, gameMode = PlayFromGameFile; }
16979 { // remove all shelved variations
16981 for(i=0; i<storedGames; i++) {
16982 if(savedDetails[i])
16983 free(savedDetails[i]);
16984 savedDetails[i] = NULL;
16986 for(i=framePtr; i<MAX_MOVES; i++) {
16987 if(commentList[i]) free(commentList[i]);
16988 commentList[i] = NULL;
16990 framePtr = MAX_MOVES-1;
16995 LoadVariation (int index, char *text)
16996 { // [HGM] vari: shelve previous line and load new variation, parsed from text around text[index]
16997 char *p = text, *start = NULL, *end = NULL, wait = NULLCHAR;
16998 int level = 0, move;
17000 if(gameMode != EditGame && gameMode != AnalyzeMode && gameMode != PlayFromGameFile) return;
17001 // first find outermost bracketing variation
17002 while(*p) { // hope I got this right... Non-nesting {} and [] can screen each other and nesting ()
17003 if(!wait) { // while inside [] pr {}, ignore everyting except matching closing ]}
17004 if(*p == '{') wait = '}'; else
17005 if(*p == '[') wait = ']'; else
17006 if(*p == '(' && level++ == 0 && p-text < index) start = p+1;
17007 if(*p == ')' && level > 0 && --level == 0 && p-text > index && end == NULL) end = p-1;
17009 if(*p == wait) wait = NULLCHAR; // closing ]} found
17012 if(!start || !end) return; // no variation found, or syntax error in PGN: ignore click
17013 if(appData.debugMode) fprintf(debugFP, "at move %d load variation '%s'\n", currentMove, start);
17014 end[1] = NULLCHAR; // clip off comment beyond variation
17015 ToNrEvent(currentMove-1);
17016 PushTail(currentMove, forwardMostMove); // shelve main variation. This truncates game
17017 // kludge: use ParsePV() to append variation to game
17018 move = currentMove;
17019 ParsePV(start, TRUE, TRUE);
17020 forwardMostMove = endPV; endPV = -1; currentMove = move; // cleanup what ParsePV did
17021 ClearPremoveHighlights();
17023 ToNrEvent(currentMove+1);