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"
132 #include "evalgraph.h"
136 # define _(s) gettext (s)
137 # define N_(s) gettext_noop (s)
138 # define T_(s) gettext(s)
151 int establish P((void));
152 void read_from_player P((InputSourceRef isr, VOIDSTAR closure,
153 char *buf, int count, int error));
154 void read_from_ics P((InputSourceRef isr, VOIDSTAR closure,
155 char *buf, int count, int error));
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(());
225 static void ExcludeClick P((int index));
226 void ToggleSecond P((void));
229 extern void ConsoleCreate();
232 ChessProgramState *WhitePlayer();
233 void InsertIntoMemo P((int which, char *text)); // [HGM] kibitz: in engineo.c
234 int VerifyDisplayMode P(());
236 char *GetInfoFromComment( int, char * ); // [HGM] PV time: returns stripped comment
237 void InitEngineUCI( const char * iniDir, ChessProgramState * cps ); // [HGM] moved here from winboard.c
238 char *ProbeBook P((int moveNr, char *book)); // [HGM] book: returns a book move
239 char *SendMoveToBookUser P((int nr, ChessProgramState *cps, int initial)); // [HGM] book
240 void ics_update_width P((int new_width));
241 extern char installDir[MSG_SIZ];
242 VariantClass startVariant; /* [HGM] nicks: initial variant */
245 extern int tinyLayout, smallLayout;
246 ChessProgramStats programStats;
247 char lastPV[2][2*MSG_SIZ]; /* [HGM] pv: last PV in thinking output of each engine */
249 static int exiting = 0; /* [HGM] moved to top */
250 static int setboardSpoiledMachineBlack = 0 /*, errorExitFlag = 0*/;
251 int startedFromPositionFile = FALSE; Board filePosition; /* [HGM] loadPos */
252 Board partnerBoard; /* [HGM] bughouse: for peeking at partner game */
253 int partnerHighlight[2];
254 Boolean partnerBoardValid = 0;
255 char partnerStatus[MSG_SIZ];
257 Boolean originalFlip;
258 Boolean twoBoards = 0;
259 char endingGame = 0; /* [HGM] crash: flag to prevent recursion of GameEnds() */
260 int whiteNPS, blackNPS; /* [HGM] nps: for easily making clocks aware of NPS */
261 VariantClass currentlyInitializedVariant; /* [HGM] variantswitch */
262 int lastIndex = 0; /* [HGM] autoinc: last game/position used in match mode */
263 Boolean connectionAlive;/* [HGM] alive: ICS connection status from probing */
264 int opponentKibitzes;
265 int lastSavedGame; /* [HGM] save: ID of game */
266 char chatPartner[MAX_CHAT][MSG_SIZ]; /* [HGM] chat: list of chatting partners */
267 extern int chatCount;
269 char marker[BOARD_RANKS][BOARD_FILES]; /* [HGM] marks for target squares */
270 char lastMsg[MSG_SIZ];
271 ChessSquare pieceSweep = EmptySquare;
272 ChessSquare promoSweep = EmptySquare, defaultPromoChoice;
273 int promoDefaultAltered;
274 int keepInfo = 0; /* [HGM] to protect PGN tags in auto-step game analysis */
276 /* States for ics_getting_history */
278 #define H_REQUESTED 1
279 #define H_GOT_REQ_HEADER 2
280 #define H_GOT_UNREQ_HEADER 3
281 #define H_GETTING_MOVES 4
282 #define H_GOT_UNWANTED_HEADER 5
284 /* whosays values for GameEnds */
293 /* Maximum number of games in a cmail message */
294 #define CMAIL_MAX_GAMES 20
296 /* Different types of move when calling RegisterMove */
298 #define CMAIL_RESIGN 1
300 #define CMAIL_ACCEPT 3
302 /* Different types of result to remember for each game */
303 #define CMAIL_NOT_RESULT 0
304 #define CMAIL_OLD_RESULT 1
305 #define CMAIL_NEW_RESULT 2
307 /* Telnet protocol constants */
318 safeStrCpy (char *dst, const char *src, size_t count)
321 assert( dst != NULL );
322 assert( src != NULL );
325 for(i=0; i<count; i++) if((dst[i] = src[i]) == NULLCHAR) break;
326 if( i == count && dst[count-1] != NULLCHAR)
328 dst[ count-1 ] = '\0'; // make sure incomplete copy still null-terminated
329 if(appData.debugMode)
330 fprintf(debugFP, "safeStrCpy: copying %s into %s didn't work, not enough space %d\n",src,dst, (int)count);
336 /* Some compiler can't cast u64 to double
337 * This function do the job for us:
339 * We use the highest bit for cast, this only
340 * works if the highest bit is not
341 * in use (This should not happen)
343 * We used this for all compiler
346 u64ToDouble (u64 value)
349 u64 tmp = value & u64Const(0x7fffffffffffffff);
350 r = (double)(s64)tmp;
351 if (value & u64Const(0x8000000000000000))
352 r += 9.2233720368547758080e18; /* 2^63 */
356 /* Fake up flags for now, as we aren't keeping track of castling
357 availability yet. [HGM] Change of logic: the flag now only
358 indicates the type of castlings allowed by the rule of the game.
359 The actual rights themselves are maintained in the array
360 castlingRights, as part of the game history, and are not probed
366 int flags = F_ALL_CASTLE_OK;
367 if ((index % 2) == 0) flags |= F_WHITE_ON_MOVE;
368 switch (gameInfo.variant) {
370 flags &= ~F_ALL_CASTLE_OK;
371 case VariantGiveaway: // [HGM] moved this case label one down: seems Giveaway does have castling on ICC!
372 flags |= F_IGNORE_CHECK;
374 flags |= F_MANDATORY_CAPTURE; //[HGM] losers: sets flag so TestLegality rejects non-capts if capts exist
377 flags |= F_IGNORE_CHECK | F_ATOMIC_CAPTURE;
379 case VariantKriegspiel:
380 flags |= F_KRIEGSPIEL_CAPTURE;
382 case VariantCapaRandom:
383 case VariantFischeRandom:
384 flags |= F_FRC_TYPE_CASTLING; /* [HGM] enable this through flag */
385 case VariantNoCastle:
386 case VariantShatranj:
390 flags &= ~F_ALL_CASTLE_OK;
398 FILE *gameFileFP, *debugFP, *serverFP;
399 char *currentDebugFile; // [HGM] debug split: to remember name
402 [AS] Note: sometimes, the sscanf() function is used to parse the input
403 into a fixed-size buffer. Because of this, we must be prepared to
404 receive strings as long as the size of the input buffer, which is currently
405 set to 4K for Windows and 8K for the rest.
406 So, we must either allocate sufficiently large buffers here, or
407 reduce the size of the input buffer in the input reading part.
410 char cmailMove[CMAIL_MAX_GAMES][MOVE_LEN], cmailMsg[MSG_SIZ];
411 char bookOutput[MSG_SIZ*10], thinkOutput[MSG_SIZ*10], lastHint[MSG_SIZ];
412 char thinkOutput1[MSG_SIZ*10];
414 ChessProgramState first, second, pairing;
416 /* premove variables */
419 int premoveFromX = 0;
420 int premoveFromY = 0;
421 int premovePromoChar = 0;
423 Boolean alarmSounded;
424 /* end premove variables */
426 char *ics_prefix = "$";
427 enum ICS_TYPE ics_type = ICS_GENERIC;
429 int currentMove = 0, forwardMostMove = 0, backwardMostMove = 0;
430 int pauseExamForwardMostMove = 0;
431 int nCmailGames = 0, nCmailResults = 0, nCmailMovesRegistered = 0;
432 int cmailMoveRegistered[CMAIL_MAX_GAMES], cmailResult[CMAIL_MAX_GAMES];
433 int cmailMsgLoaded = FALSE, cmailMailedMove = FALSE;
434 int cmailOldMove = -1, firstMove = TRUE, flipView = FALSE;
435 int blackPlaysFirst = FALSE, startedFromSetupPosition = FALSE;
436 int searchTime = 0, pausing = FALSE, pauseExamInvalid = FALSE;
437 int whiteFlag = FALSE, blackFlag = FALSE;
438 int userOfferedDraw = FALSE;
439 int ics_user_moved = 0, ics_gamenum = -1, ics_getting_history = H_FALSE;
440 int matchMode = FALSE, hintRequested = FALSE, bookRequested = FALSE;
441 int cmailMoveType[CMAIL_MAX_GAMES];
442 long ics_clock_paused = 0;
443 ProcRef icsPR = NoProc, cmailPR = NoProc;
444 InputSourceRef telnetISR = NULL, fromUserISR = NULL, cmailISR = NULL;
445 GameMode gameMode = BeginningOfGame;
446 char moveList[MAX_MOVES][MOVE_LEN], parseList[MAX_MOVES][MOVE_LEN * 2];
447 char *commentList[MAX_MOVES], *cmailCommentList[CMAIL_MAX_GAMES];
448 ChessProgramStats_Move pvInfoList[MAX_MOVES]; /* [AS] Info about engine thinking */
449 int hiddenThinkOutputState = 0; /* [AS] */
450 int adjudicateLossThreshold = 0; /* [AS] Automatic adjudication */
451 int adjudicateLossPlies = 6;
452 char white_holding[64], black_holding[64];
453 TimeMark lastNodeCountTime;
454 long lastNodeCount=0;
455 int shiftKey, controlKey; // [HGM] set by mouse handler
457 int have_sent_ICS_logon = 0;
459 int suddenDeath, whiteStartMove, blackStartMove; /* [HGM] for implementation of 'any per time' sessions, as in first part of byoyomi TC */
460 long whiteTimeRemaining, blackTimeRemaining, timeControl, timeIncrement, lastWhite, lastBlack, activePartnerTime;
461 Boolean adjustedClock;
462 long timeControl_2; /* [AS] Allow separate time controls */
463 char *fullTimeControlString = NULL, *nextSession, *whiteTC, *blackTC, activePartner; /* [HGM] secondary TC: merge of MPS, TC and inc */
464 long timeRemaining[2][MAX_MOVES];
465 int matchGame = 0, nextGame = 0, roundNr = 0;
466 Boolean waitingForGame = FALSE;
467 TimeMark programStartTime, pauseStart;
468 char ics_handle[MSG_SIZ];
469 int have_set_title = 0;
471 /* animateTraining preserves the state of appData.animate
472 * when Training mode is activated. This allows the
473 * response to be animated when appData.animate == TRUE and
474 * appData.animateDragging == TRUE.
476 Boolean animateTraining;
482 Board boards[MAX_MOVES];
483 /* [HGM] Following 7 needed for accurate legality tests: */
484 signed char castlingRank[BOARD_FILES]; // and corresponding ranks
485 signed char initialRights[BOARD_FILES];
486 int nrCastlingRights; // For TwoKings, or to implement castling-unknown status
487 int initialRulePlies, FENrulePlies;
488 FILE *serverMoves = NULL; // next two for broadcasting (/serverMoves option)
490 Boolean shuffleOpenings;
491 int mute; // mute all sounds
493 // [HGM] vari: next 12 to save and restore variations
494 #define MAX_VARIATIONS 10
495 int framePtr = MAX_MOVES-1; // points to free stack entry
497 int savedFirst[MAX_VARIATIONS];
498 int savedLast[MAX_VARIATIONS];
499 int savedFramePtr[MAX_VARIATIONS];
500 char *savedDetails[MAX_VARIATIONS];
501 ChessMove savedResult[MAX_VARIATIONS];
503 void PushTail P((int firstMove, int lastMove));
504 Boolean PopTail P((Boolean annotate));
505 void PushInner P((int firstMove, int lastMove));
506 void PopInner P((Boolean annotate));
507 void CleanupTail P((void));
509 ChessSquare FIDEArray[2][BOARD_FILES] = {
510 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
511 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
512 { BlackRook, BlackKnight, BlackBishop, BlackQueen,
513 BlackKing, BlackBishop, BlackKnight, BlackRook }
516 ChessSquare twoKingsArray[2][BOARD_FILES] = {
517 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
518 WhiteKing, WhiteKing, WhiteKnight, WhiteRook },
519 { BlackRook, BlackKnight, BlackBishop, BlackQueen,
520 BlackKing, BlackKing, BlackKnight, BlackRook }
523 ChessSquare KnightmateArray[2][BOARD_FILES] = {
524 { WhiteRook, WhiteMan, WhiteBishop, WhiteQueen,
525 WhiteUnicorn, WhiteBishop, WhiteMan, WhiteRook },
526 { BlackRook, BlackMan, BlackBishop, BlackQueen,
527 BlackUnicorn, BlackBishop, BlackMan, BlackRook }
530 ChessSquare SpartanArray[2][BOARD_FILES] = {
531 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
532 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
533 { BlackAlfil, BlackMarshall, BlackKing, BlackDragon,
534 BlackDragon, BlackKing, BlackAngel, BlackAlfil }
537 ChessSquare fairyArray[2][BOARD_FILES] = { /* [HGM] Queen side differs from King side */
538 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
539 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
540 { BlackCardinal, BlackAlfil, BlackMarshall, BlackAngel,
541 BlackKing, BlackMarshall, BlackAlfil, BlackCardinal }
544 ChessSquare ShatranjArray[2][BOARD_FILES] = { /* [HGM] (movGen knows about Shatranj Q and P) */
545 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteKing,
546 WhiteFerz, WhiteAlfil, WhiteKnight, WhiteRook },
547 { BlackRook, BlackKnight, BlackAlfil, BlackKing,
548 BlackFerz, BlackAlfil, BlackKnight, BlackRook }
551 ChessSquare makrukArray[2][BOARD_FILES] = { /* [HGM] (movGen knows about Shatranj Q and P) */
552 { WhiteRook, WhiteKnight, WhiteMan, WhiteKing,
553 WhiteFerz, WhiteMan, WhiteKnight, WhiteRook },
554 { BlackRook, BlackKnight, BlackMan, BlackFerz,
555 BlackKing, BlackMan, BlackKnight, BlackRook }
559 #if (BOARD_FILES>=10)
560 ChessSquare ShogiArray[2][BOARD_FILES] = {
561 { WhiteQueen, WhiteKnight, WhiteFerz, WhiteWazir,
562 WhiteKing, WhiteWazir, WhiteFerz, WhiteKnight, WhiteQueen },
563 { BlackQueen, BlackKnight, BlackFerz, BlackWazir,
564 BlackKing, BlackWazir, BlackFerz, BlackKnight, BlackQueen }
567 ChessSquare XiangqiArray[2][BOARD_FILES] = {
568 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteFerz,
569 WhiteWazir, WhiteFerz, WhiteAlfil, WhiteKnight, WhiteRook },
570 { BlackRook, BlackKnight, BlackAlfil, BlackFerz,
571 BlackWazir, BlackFerz, BlackAlfil, BlackKnight, BlackRook }
574 ChessSquare CapablancaArray[2][BOARD_FILES] = {
575 { WhiteRook, WhiteKnight, WhiteAngel, WhiteBishop, WhiteQueen,
576 WhiteKing, WhiteBishop, WhiteMarshall, WhiteKnight, WhiteRook },
577 { BlackRook, BlackKnight, BlackAngel, BlackBishop, BlackQueen,
578 BlackKing, BlackBishop, BlackMarshall, BlackKnight, BlackRook }
581 ChessSquare GreatArray[2][BOARD_FILES] = {
582 { WhiteDragon, WhiteKnight, WhiteAlfil, WhiteGrasshopper, WhiteKing,
583 WhiteSilver, WhiteCardinal, WhiteAlfil, WhiteKnight, WhiteDragon },
584 { BlackDragon, BlackKnight, BlackAlfil, BlackGrasshopper, BlackKing,
585 BlackSilver, BlackCardinal, BlackAlfil, BlackKnight, BlackDragon },
588 ChessSquare JanusArray[2][BOARD_FILES] = {
589 { WhiteRook, WhiteAngel, WhiteKnight, WhiteBishop, WhiteKing,
590 WhiteQueen, WhiteBishop, WhiteKnight, WhiteAngel, WhiteRook },
591 { BlackRook, BlackAngel, BlackKnight, BlackBishop, BlackKing,
592 BlackQueen, BlackBishop, BlackKnight, BlackAngel, BlackRook }
595 ChessSquare GrandArray[2][BOARD_FILES] = {
596 { EmptySquare, WhiteKnight, WhiteBishop, WhiteQueen, WhiteKing,
597 WhiteMarshall, WhiteAngel, WhiteBishop, WhiteKnight, EmptySquare },
598 { EmptySquare, BlackKnight, BlackBishop, BlackQueen, BlackKing,
599 BlackMarshall, BlackAngel, BlackBishop, BlackKnight, EmptySquare }
603 ChessSquare GothicArray[2][BOARD_FILES] = {
604 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen, WhiteMarshall,
605 WhiteKing, WhiteAngel, WhiteBishop, WhiteKnight, WhiteRook },
606 { BlackRook, BlackKnight, BlackBishop, BlackQueen, BlackMarshall,
607 BlackKing, BlackAngel, BlackBishop, BlackKnight, BlackRook }
610 #define GothicArray CapablancaArray
614 ChessSquare FalconArray[2][BOARD_FILES] = {
615 { WhiteRook, WhiteKnight, WhiteBishop, WhiteFalcon, WhiteQueen,
616 WhiteKing, WhiteFalcon, WhiteBishop, WhiteKnight, WhiteRook },
617 { BlackRook, BlackKnight, BlackBishop, BlackFalcon, BlackQueen,
618 BlackKing, BlackFalcon, BlackBishop, BlackKnight, BlackRook }
621 #define FalconArray CapablancaArray
624 #else // !(BOARD_FILES>=10)
625 #define XiangqiPosition FIDEArray
626 #define CapablancaArray FIDEArray
627 #define GothicArray FIDEArray
628 #define GreatArray FIDEArray
629 #endif // !(BOARD_FILES>=10)
631 #if (BOARD_FILES>=12)
632 ChessSquare CourierArray[2][BOARD_FILES] = {
633 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteBishop, WhiteMan, WhiteKing,
634 WhiteFerz, WhiteWazir, WhiteBishop, WhiteAlfil, WhiteKnight, WhiteRook },
635 { BlackRook, BlackKnight, BlackAlfil, BlackBishop, BlackMan, BlackKing,
636 BlackFerz, BlackWazir, BlackBishop, BlackAlfil, BlackKnight, BlackRook }
638 #else // !(BOARD_FILES>=12)
639 #define CourierArray CapablancaArray
640 #endif // !(BOARD_FILES>=12)
643 Board initialPosition;
646 /* Convert str to a rating. Checks for special cases of "----",
648 "++++", etc. Also strips ()'s */
650 string_to_rating (char *str)
652 while(*str && !isdigit(*str)) ++str;
654 return 0; /* One of the special "no rating" cases */
662 /* Init programStats */
663 programStats.movelist[0] = 0;
664 programStats.depth = 0;
665 programStats.nr_moves = 0;
666 programStats.moves_left = 0;
667 programStats.nodes = 0;
668 programStats.time = -1; // [HGM] PGNtime: make invalid to recognize engine output
669 programStats.score = 0;
670 programStats.got_only_move = 0;
671 programStats.got_fail = 0;
672 programStats.line_is_book = 0;
677 { // [HGM] moved some code here from InitBackend1 that has to be done after both engines have contributed their settings
678 if (appData.firstPlaysBlack) {
679 first.twoMachinesColor = "black\n";
680 second.twoMachinesColor = "white\n";
682 first.twoMachinesColor = "white\n";
683 second.twoMachinesColor = "black\n";
686 first.other = &second;
687 second.other = &first;
690 if(appData.timeOddsMode) {
691 norm = appData.timeOdds[0];
692 if(norm > appData.timeOdds[1]) norm = appData.timeOdds[1];
694 first.timeOdds = appData.timeOdds[0]/norm;
695 second.timeOdds = appData.timeOdds[1]/norm;
698 if(programVersion) free(programVersion);
699 if (appData.noChessProgram) {
700 programVersion = (char*) malloc(5 + strlen(PACKAGE_STRING));
701 sprintf(programVersion, "%s", PACKAGE_STRING);
703 /* [HGM] tidy: use tidy name, in stead of full pathname (which was probably a bug due to / vs \ ) */
704 programVersion = (char*) malloc(8 + strlen(PACKAGE_STRING) + strlen(first.tidy));
705 sprintf(programVersion, "%s + %s", PACKAGE_STRING, first.tidy);
710 UnloadEngine (ChessProgramState *cps)
712 /* Kill off first chess program */
713 if (cps->isr != NULL)
714 RemoveInputSource(cps->isr);
717 if (cps->pr != NoProc) {
719 DoSleep( appData.delayBeforeQuit );
720 SendToProgram("quit\n", cps);
721 DoSleep( appData.delayAfterQuit );
722 DestroyChildProcess(cps->pr, cps->useSigterm);
725 if(appData.debugMode) fprintf(debugFP, "Unload %s\n", cps->which);
729 ClearOptions (ChessProgramState *cps)
732 cps->nrOptions = cps->comboCnt = 0;
733 for(i=0; i<MAX_OPTIONS; i++) {
734 cps->option[i].min = cps->option[i].max = cps->option[i].value = 0;
735 cps->option[i].textValue = 0;
739 char *engineNames[] = {
740 /* TRANSLATORS: "first" is the first 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 */
743 /* TRANSLATORS: "second" is the second of possible two chess engines. It is inserted into strings
744 such as "%s engine" / "%s chess program" / "%s machine" - all meaning the same thing */
749 InitEngine (ChessProgramState *cps, int n)
750 { // [HGM] all engine initialiation put in a function that does one engine
754 cps->which = engineNames[n];
755 cps->maybeThinking = FALSE;
759 cps->sendDrawOffers = 1;
761 cps->program = appData.chessProgram[n];
762 cps->host = appData.host[n];
763 cps->dir = appData.directory[n];
764 cps->initString = appData.engInitString[n];
765 cps->computerString = appData.computerString[n];
766 cps->useSigint = TRUE;
767 cps->useSigterm = TRUE;
768 cps->reuse = appData.reuse[n];
769 cps->nps = appData.NPS[n]; // [HGM] nps: copy nodes per second
770 cps->useSetboard = FALSE;
772 cps->usePing = FALSE;
775 cps->usePlayother = FALSE;
776 cps->useColors = TRUE;
777 cps->useUsermove = FALSE;
778 cps->sendICS = FALSE;
779 cps->sendName = appData.icsActive;
780 cps->sdKludge = FALSE;
781 cps->stKludge = FALSE;
782 TidyProgramName(cps->program, cps->host, cps->tidy);
784 safeStrCpy(cps->variants, appData.variant, MSG_SIZ);
785 cps->analysisSupport = 2; /* detect */
786 cps->analyzing = FALSE;
787 cps->initDone = FALSE;
789 /* New features added by Tord: */
790 cps->useFEN960 = FALSE;
791 cps->useOOCastle = TRUE;
792 /* End of new features added by Tord. */
793 cps->fenOverride = appData.fenOverride[n];
795 /* [HGM] time odds: set factor for each machine */
796 cps->timeOdds = appData.timeOdds[n];
798 /* [HGM] secondary TC: how to handle sessions that do not fit in 'level'*/
799 cps->accumulateTC = appData.accumulateTC[n];
800 cps->maxNrOfSessions = 1;
805 cps->supportsNPS = UNKNOWN;
806 cps->memSize = FALSE;
807 cps->maxCores = FALSE;
808 cps->egtFormats[0] = NULLCHAR;
811 cps->optionSettings = appData.engOptions[n];
813 cps->scoreIsAbsolute = appData.scoreIsAbsolute[n]; /* [AS] */
814 cps->isUCI = appData.isUCI[n]; /* [AS] */
815 cps->hasOwnBookUCI = appData.hasOwnBookUCI[n]; /* [AS] */
817 if (appData.protocolVersion[n] > PROTOVER
818 || appData.protocolVersion[n] < 1)
823 len = snprintf(buf, MSG_SIZ, _("protocol version %d not supported"),
824 appData.protocolVersion[n]);
825 if( (len >= MSG_SIZ) && appData.debugMode )
826 fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
828 DisplayFatalError(buf, 0, 2);
832 cps->protocolVersion = appData.protocolVersion[n];
835 InitEngineUCI( installDir, cps ); // [HGM] moved here from winboard.c, to make available in xboard
836 ParseFeatures(appData.featureDefaults, cps);
839 ChessProgramState *savCps;
845 if(WaitForEngine(savCps, LoadEngine)) return;
846 CommonEngineInit(); // recalculate time odds
847 if(gameInfo.variant != StringToVariant(appData.variant)) {
848 // we changed variant when loading the engine; this forces us to reset
849 Reset(TRUE, savCps != &first);
850 EditGameEvent(); // for consistency with other path, as Reset changes mode
852 InitChessProgram(savCps, FALSE);
853 SendToProgram("force\n", savCps);
854 DisplayMessage("", "");
855 if (startedFromSetupPosition) SendBoard(savCps, backwardMostMove);
856 for (i = backwardMostMove; i < currentMove; i++) SendMoveToProgram(i, savCps);
862 ReplaceEngine (ChessProgramState *cps, int n)
866 appData.noChessProgram = FALSE;
867 appData.clockMode = TRUE;
870 if(n) return; // only startup first engine immediately; second can wait
871 savCps = cps; // parameter to LoadEngine passed as globals, to allow scheduled calling :-(
875 extern char *engineName, *engineDir, *engineChoice, *engineLine, *nickName, *params;
876 extern Boolean isUCI, hasBook, storeVariant, v1, addToList, useNick;
878 static char resetOptions[] =
879 "-reuse -firstIsUCI false -firstHasOwnBookUCI true -firstTimeOdds 1 "
880 "-firstInitString \"" INIT_STRING "\" -firstComputerString \"" COMPUTER_STRING "\" "
881 "-firstFeatures \"\" -firstLogo \"\" -firstAccumulateTC 1 "
882 "-firstOptions \"\" -firstNPS -1 -fn \"\" -firstScoreAbs false";
885 FloatToFront(char **list, char *engineLine)
887 char buf[MSG_SIZ], tidy[MSG_SIZ], *p = buf, *q, *r = buf;
889 if(appData.recentEngines <= 0) return;
890 TidyProgramName(engineLine, "localhost", tidy+1);
891 tidy[0] = buf[0] = '\n'; strcat(tidy, "\n");
892 strncpy(buf+1, *list, MSG_SIZ-50);
893 if(p = strstr(buf, tidy)) { // tidy name appears in list
894 q = strchr(++p, '\n'); if(q == NULL) return; // malformed, don't touch
895 while(*p++ = *++q); // squeeze out
897 strcat(tidy, buf+1); // put list behind tidy name
898 p = tidy + 1; while(q = strchr(p, '\n')) i++, r = p, p = q + 1; // count entries in new list
899 if(i > appData.recentEngines) *r = NULLCHAR; // if maximum rached, strip off last
900 ASSIGN(*list, tidy+1);
903 char *insert, *wbOptions; // point in ChessProgramNames were we should insert new engine
906 Load (ChessProgramState *cps, int i)
908 char *p, *q, buf[MSG_SIZ], command[MSG_SIZ], buf2[MSG_SIZ];
909 if(engineLine && engineLine[0]) { // an engine was selected from the combo box
910 snprintf(buf, MSG_SIZ, "-fcp %s", engineLine);
911 SwapEngines(i); // kludge to parse -f* / -first* like it is -s* / -second*
912 ParseArgsFromString(resetOptions); appData.pvSAN[0] = FALSE;
913 FREE(appData.fenOverride[0]); appData.fenOverride[0] = NULL;
914 appData.firstProtocolVersion = PROTOVER;
915 ParseArgsFromString(buf);
917 ReplaceEngine(cps, i);
918 FloatToFront(&appData.recentEngineList, engineLine);
922 while(q = strchr(p, SLASH)) p = q+1;
923 if(*p== NULLCHAR) { DisplayError(_("You did not specify the engine executable"), 0); return; }
924 if(engineDir[0] != NULLCHAR) {
925 ASSIGN(appData.directory[i], engineDir); p = engineName;
926 } else if(p != engineName) { // derive directory from engine path, when not given
928 ASSIGN(appData.directory[i], engineName);
930 if(SLASH == '/' && p - engineName > 1) *(p -= 2) = '.'; // for XBoard use ./exeName as command after split!
931 } else { ASSIGN(appData.directory[i], "."); }
933 if(strchr(p, ' ') && !strchr(p, '"')) snprintf(buf2, MSG_SIZ, "\"%s\"", p), p = buf2; // quote if it contains spaces
934 snprintf(command, MSG_SIZ, "%s %s", p, params);
937 ASSIGN(appData.chessProgram[i], p);
938 appData.isUCI[i] = isUCI;
939 appData.protocolVersion[i] = v1 ? 1 : PROTOVER;
940 appData.hasOwnBookUCI[i] = hasBook;
941 if(!nickName[0]) useNick = FALSE;
942 if(useNick) ASSIGN(appData.pgnName[i], nickName);
946 q = firstChessProgramNames;
947 if(nickName[0]) snprintf(buf, MSG_SIZ, "\"%s\" -fcp ", nickName); else buf[0] = NULLCHAR;
948 quote = strchr(p, '"') ? '\'' : '"'; // use single quotes around engine command if it contains double quotes
949 snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), "%c%s%c -fd \"%s\"%s%s%s%s%s%s%s%s\n",
950 quote, p, quote, appData.directory[i],
951 useNick ? " -fn \"" : "",
952 useNick ? nickName : "",
954 v1 ? " -firstProtocolVersion 1" : "",
955 hasBook ? "" : " -fNoOwnBookUCI",
956 isUCI ? (isUCI == TRUE ? " -fUCI" : gameInfo.variant == VariantShogi ? " -fUSI" : " -fUCCI") : "",
957 storeVariant ? " -variant " : "",
958 storeVariant ? VariantName(gameInfo.variant) : "");
959 if(wbOptions && wbOptions[0]) snprintf(buf+strlen(buf)-1, MSG_SIZ-strlen(buf), " %s\n", wbOptions);
960 firstChessProgramNames = malloc(len = strlen(q) + strlen(buf) + 1);
961 if(insert != q) insert[-1] = NULLCHAR;
962 snprintf(firstChessProgramNames, len, "%s\n%s%s", q, buf, insert);
964 FloatToFront(&appData.recentEngineList, buf);
966 ReplaceEngine(cps, i);
972 int matched, min, sec;
974 * Parse timeControl resource
976 if (!ParseTimeControl(appData.timeControl, appData.timeIncrement,
977 appData.movesPerSession)) {
979 snprintf(buf, sizeof(buf), _("bad timeControl option %s"), appData.timeControl);
980 DisplayFatalError(buf, 0, 2);
984 * Parse searchTime resource
986 if (*appData.searchTime != NULLCHAR) {
987 matched = sscanf(appData.searchTime, "%d:%d", &min, &sec);
989 searchTime = min * 60;
990 } else if (matched == 2) {
991 searchTime = min * 60 + sec;
994 snprintf(buf, sizeof(buf), _("bad searchTime option %s"), appData.searchTime);
995 DisplayFatalError(buf, 0, 2);
1004 ShowThinkingEvent(); // [HGM] thinking: make sure post/nopost state is set according to options
1005 startVariant = StringToVariant(appData.variant); // [HGM] nicks: remember original variant
1007 GetTimeMark(&programStartTime);
1008 srandom((programStartTime.ms + 1000*programStartTime.sec)*0x1001001); // [HGM] book: makes sure random is unpredictabe to msec level
1009 appData.seedBase = random() + (random()<<15);
1010 pauseStart = programStartTime; pauseStart.sec -= 100; // [HGM] matchpause: fake a pause that has long since ended
1012 ClearProgramStats();
1013 programStats.ok_to_send = 1;
1014 programStats.seen_stat = 0;
1017 * Initialize game list
1023 * Internet chess server status
1025 if (appData.icsActive) {
1026 appData.matchMode = FALSE;
1027 appData.matchGames = 0;
1029 appData.noChessProgram = !appData.zippyPlay;
1031 appData.zippyPlay = FALSE;
1032 appData.zippyTalk = FALSE;
1033 appData.noChessProgram = TRUE;
1035 if (*appData.icsHelper != NULLCHAR) {
1036 appData.useTelnet = TRUE;
1037 appData.telnetProgram = appData.icsHelper;
1040 appData.zippyTalk = appData.zippyPlay = FALSE;
1043 /* [AS] Initialize pv info list [HGM] and game state */
1047 for( i=0; i<=framePtr; i++ ) {
1048 pvInfoList[i].depth = -1;
1049 boards[i][EP_STATUS] = EP_NONE;
1050 for( j=0; j<BOARD_FILES-2; j++ ) boards[i][CASTLING][j] = NoRights;
1056 /* [AS] Adjudication threshold */
1057 adjudicateLossThreshold = appData.adjudicateLossThreshold;
1059 InitEngine(&first, 0);
1060 InitEngine(&second, 1);
1063 pairing.which = "pairing"; // pairing engine
1064 pairing.pr = NoProc;
1066 pairing.program = appData.pairingEngine;
1067 pairing.host = "localhost";
1070 if (appData.icsActive) {
1071 appData.clockMode = TRUE; /* changes dynamically in ICS mode */
1072 } else if (appData.noChessProgram) { // [HGM] st: searchTime mode now also is clockMode
1073 appData.clockMode = FALSE;
1074 first.sendTime = second.sendTime = 0;
1078 /* Override some settings from environment variables, for backward
1079 compatibility. Unfortunately it's not feasible to have the env
1080 vars just set defaults, at least in xboard. Ugh.
1082 if (appData.icsActive && (appData.zippyPlay || appData.zippyTalk)) {
1087 if (!appData.icsActive) {
1091 /* Check for variants that are supported only in ICS mode,
1092 or not at all. Some that are accepted here nevertheless
1093 have bugs; see comments below.
1095 VariantClass variant = StringToVariant(appData.variant);
1097 case VariantBughouse: /* need four players and two boards */
1098 case VariantKriegspiel: /* need to hide pieces and move details */
1099 /* case VariantFischeRandom: (Fabien: moved below) */
1100 len = snprintf(buf,MSG_SIZ, _("Variant %s supported only in ICS mode"), appData.variant);
1101 if( (len >= MSG_SIZ) && appData.debugMode )
1102 fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
1104 DisplayFatalError(buf, 0, 2);
1107 case VariantUnknown:
1108 case VariantLoadable:
1118 len = snprintf(buf, MSG_SIZ, _("Unknown variant name %s"), appData.variant);
1119 if( (len >= MSG_SIZ) && appData.debugMode )
1120 fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
1122 DisplayFatalError(buf, 0, 2);
1125 case VariantXiangqi: /* [HGM] repetition rules not implemented */
1126 case VariantFairy: /* [HGM] TestLegality definitely off! */
1127 case VariantGothic: /* [HGM] should work */
1128 case VariantCapablanca: /* [HGM] should work */
1129 case VariantCourier: /* [HGM] initial forced moves not implemented */
1130 case VariantShogi: /* [HGM] could still mate with pawn drop */
1131 case VariantKnightmate: /* [HGM] should work */
1132 case VariantCylinder: /* [HGM] untested */
1133 case VariantFalcon: /* [HGM] untested */
1134 case VariantCrazyhouse: /* holdings not shown, ([HGM] fixed that!)
1135 offboard interposition not understood */
1136 case VariantNormal: /* definitely works! */
1137 case VariantWildCastle: /* pieces not automatically shuffled */
1138 case VariantNoCastle: /* pieces not automatically shuffled */
1139 case VariantFischeRandom: /* [HGM] works and shuffles pieces */
1140 case VariantLosers: /* should work except for win condition,
1141 and doesn't know captures are mandatory */
1142 case VariantSuicide: /* should work except for win condition,
1143 and doesn't know captures are mandatory */
1144 case VariantGiveaway: /* should work except for win condition,
1145 and doesn't know captures are mandatory */
1146 case VariantTwoKings: /* should work */
1147 case VariantAtomic: /* should work except for win condition */
1148 case Variant3Check: /* should work except for win condition */
1149 case VariantShatranj: /* should work except for all win conditions */
1150 case VariantMakruk: /* should work except for draw countdown */
1151 case VariantBerolina: /* might work if TestLegality is off */
1152 case VariantCapaRandom: /* should work */
1153 case VariantJanus: /* should work */
1154 case VariantSuper: /* experimental */
1155 case VariantGreat: /* experimental, requires legality testing to be off */
1156 case VariantSChess: /* S-Chess, should work */
1157 case VariantGrand: /* should work */
1158 case VariantSpartan: /* should work */
1166 NextIntegerFromString (char ** str, long * value)
1171 while( *s == ' ' || *s == '\t' ) {
1177 if( *s >= '0' && *s <= '9' ) {
1178 while( *s >= '0' && *s <= '9' ) {
1179 *value = *value * 10 + (*s - '0');
1192 NextTimeControlFromString (char ** str, long * value)
1195 int result = NextIntegerFromString( str, &temp );
1198 *value = temp * 60; /* Minutes */
1199 if( **str == ':' ) {
1201 result = NextIntegerFromString( str, &temp );
1202 *value += temp; /* Seconds */
1210 NextSessionFromString (char ** str, int *moves, long * tc, long *inc, int *incType)
1211 { /* [HGM] routine added to read '+moves/time' for secondary time control. */
1212 int result = -1, type = 0; long temp, temp2;
1214 if(**str != ':') return -1; // old params remain in force!
1216 if(**str == '*') type = *(*str)++, temp = 0; // sandclock TC
1217 if( NextIntegerFromString( str, &temp ) ) return -1;
1218 if(type) { *moves = 0; *tc = temp * 500; *inc = temp * 1000; *incType = '*'; return 0; }
1221 /* time only: incremental or sudden-death time control */
1222 if(**str == '+') { /* increment follows; read it */
1224 if(**str == '!') type = *(*str)++; // Bronstein TC
1225 if(result = NextIntegerFromString( str, &temp2)) return -1;
1226 *inc = temp2 * 1000;
1227 if(**str == '.') { // read fraction of increment
1228 char *start = ++(*str);
1229 if(result = NextIntegerFromString( str, &temp2)) return -1;
1231 while(start++ < *str) temp2 /= 10;
1235 *moves = 0; *tc = temp * 1000; *incType = type;
1239 (*str)++; /* classical time control */
1240 result = NextIntegerFromString( str, &temp2); // NOTE: already converted to seconds by ParseTimeControl()
1252 GetTimeQuota (int movenr, int lastUsed, char *tcString)
1253 { /* [HGM] get time to add from the multi-session time-control string */
1254 int incType, moves=1; /* kludge to force reading of first session */
1255 long time, increment;
1258 if(!s || !*s) return 0; // empty TC string means we ran out of the last sudden-death version
1260 if(moves) NextSessionFromString(&s, &moves, &time, &increment, &incType);
1261 nextSession = s; suddenDeath = moves == 0 && increment == 0;
1262 if(movenr == -1) return time; /* last move before new session */
1263 if(incType == '*') increment = 0; else // for sandclock, time is added while not thinking
1264 if(incType == '!' && lastUsed < increment) increment = lastUsed;
1265 if(!moves) return increment; /* current session is incremental */
1266 if(movenr >= 0) movenr -= moves; /* we already finished this session */
1267 } while(movenr >= -1); /* try again for next session */
1269 return 0; // no new time quota on this move
1273 ParseTimeControl (char *tc, float ti, int mps)
1277 char buf[MSG_SIZ], buf2[MSG_SIZ], *mytc = tc;
1280 if(ti >= 0 && !strchr(tc, '+') && !strchr(tc, '/') ) mps = 0;
1281 if(!strchr(tc, '+') && !strchr(tc, '/') && sscanf(tc, "%d:%d", &min, &sec) >= 1)
1282 sprintf(mytc=buf2, "%d", 60*min+sec); // convert 'classical' min:sec tc string to seconds
1286 snprintf(buf, MSG_SIZ, ":%d/%s+%g", mps, mytc, ti);
1288 snprintf(buf, MSG_SIZ, ":%s+%g", mytc, ti);
1291 snprintf(buf, MSG_SIZ, ":%d/%s", mps, mytc);
1293 snprintf(buf, MSG_SIZ, ":%s", mytc);
1295 fullTimeControlString = StrSave(buf); // this should now be in PGN format
1297 if( NextTimeControlFromString( &tc, &tc1 ) != 0 ) {
1302 /* Parse second time control */
1305 if( NextTimeControlFromString( &tc, &tc2 ) != 0 ) {
1313 timeControl_2 = tc2 * 1000;
1323 timeControl = tc1 * 1000;
1326 timeIncrement = ti * 1000; /* convert to ms */
1327 movesPerSession = 0;
1330 movesPerSession = mps;
1338 if (appData.debugMode) {
1339 fprintf(debugFP, "%s\n", programVersion);
1341 ASSIGN(currentDebugFile, appData.nameOfDebugFile); // [HGM] debug split: remember initial name in use
1343 set_cont_sequence(appData.wrapContSeq);
1344 if (appData.matchGames > 0) {
1345 appData.matchMode = TRUE;
1346 } else if (appData.matchMode) {
1347 appData.matchGames = 1;
1349 if(appData.matchMode && appData.sameColorGames > 0) /* [HGM] alternate: overrule matchGames */
1350 appData.matchGames = appData.sameColorGames;
1351 if(appData.rewindIndex > 1) { /* [HGM] autoinc: rewind implies auto-increment and overrules given index */
1352 if(appData.loadPositionIndex >= 0) appData.loadPositionIndex = -1;
1353 if(appData.loadGameIndex >= 0) appData.loadGameIndex = -1;
1356 if (appData.noChessProgram || first.protocolVersion == 1) {
1359 /* kludge: allow timeout for initial "feature" commands */
1361 DisplayMessage("", _("Starting chess program"));
1362 ScheduleDelayedEvent(InitBackEnd3, FEATURE_TIMEOUT);
1367 CalculateIndex (int index, int gameNr)
1368 { // [HGM] autoinc: absolute way to determine load index from game number (taking auto-inc and rewind into account)
1370 if(index > 0) return index; // fixed nmber
1371 if(index == 0) return 1;
1372 res = (index == -1 ? gameNr : (gameNr-1)/2 + 1); // autoinc
1373 if(appData.rewindIndex > 0) res = (res-1) % appData.rewindIndex + 1; // rewind
1378 LoadGameOrPosition (int gameNr)
1379 { // [HGM] taken out of MatchEvent and NextMatchGame (to combine it)
1380 if (*appData.loadGameFile != NULLCHAR) {
1381 if (!LoadGameFromFile(appData.loadGameFile,
1382 CalculateIndex(appData.loadGameIndex, gameNr),
1383 appData.loadGameFile, FALSE)) {
1384 DisplayFatalError(_("Bad game file"), 0, 1);
1387 } else if (*appData.loadPositionFile != NULLCHAR) {
1388 if (!LoadPositionFromFile(appData.loadPositionFile,
1389 CalculateIndex(appData.loadPositionIndex, gameNr),
1390 appData.loadPositionFile)) {
1391 DisplayFatalError(_("Bad position file"), 0, 1);
1399 ReserveGame (int gameNr, char resChar)
1401 FILE *tf = fopen(appData.tourneyFile, "r+");
1402 char *p, *q, c, buf[MSG_SIZ];
1403 if(tf == NULL) { nextGame = appData.matchGames + 1; return; } // kludge to terminate match
1404 safeStrCpy(buf, lastMsg, MSG_SIZ);
1405 DisplayMessage(_("Pick new game"), "");
1406 flock(fileno(tf), LOCK_EX); // lock the tourney file while we are messing with it
1407 ParseArgsFromFile(tf);
1408 p = q = appData.results;
1409 if(appData.debugMode) {
1410 char *r = appData.participants;
1411 fprintf(debugFP, "results = '%s'\n", p);
1412 while(*r) fprintf(debugFP, *r >= ' ' ? "%c" : "\\%03o", *r), r++;
1413 fprintf(debugFP, "\n");
1415 while(*q && *q != ' ') q++; // get first un-played game (could be beyond end!)
1417 q = malloc(strlen(p) + 2); // could be arbitrary long, but allow to extend by one!
1418 safeStrCpy(q, p, strlen(p) + 2);
1419 if(gameNr >= 0) q[gameNr] = resChar; // replace '*' with result
1420 if(appData.debugMode) fprintf(debugFP, "pick next game from '%s': %d\n", q, nextGame);
1421 if(nextGame <= appData.matchGames && resChar != ' ' && !abortMatch) { // reserve next game if tourney not yet done
1422 if(q[nextGame] == NULLCHAR) q[nextGame+1] = NULLCHAR; // append one char
1425 fseek(tf, -(strlen(p)+4), SEEK_END);
1427 if(c != '"') // depending on DOS or Unix line endings we can be one off
1428 fseek(tf, -(strlen(p)+2), SEEK_END);
1429 else fseek(tf, -(strlen(p)+3), SEEK_END);
1430 fprintf(tf, "%s\"\n", q); fclose(tf); // update, and flush by closing
1431 DisplayMessage(buf, "");
1432 free(p); appData.results = q;
1433 if(nextGame <= appData.matchGames && resChar != ' ' && !abortMatch &&
1434 (gameNr < 0 || nextGame / appData.defaultMatchGames != gameNr / appData.defaultMatchGames)) {
1435 int round = appData.defaultMatchGames * appData.tourneyType;
1436 if(gameNr < 0 || appData.tourneyType < 1 || // gauntlet engine can always stay loaded as first engine
1437 appData.tourneyType > 1 && nextGame/round != gameNr/round) // in multi-gauntlet change only after round
1438 UnloadEngine(&first); // next game belongs to other pairing;
1439 UnloadEngine(&second); // already unload the engines, so TwoMachinesEvent will load new ones.
1441 if(appData.debugMode) fprintf(debugFP, "Reserved, next=%d, nr=%d\n", nextGame, gameNr);
1445 MatchEvent (int mode)
1446 { // [HGM] moved out of InitBackend3, to make it callable when match starts through menu
1448 if(matchMode) { // already in match mode: switch it off
1450 if(!appData.tourneyFile[0]) appData.matchGames = matchGame; // kludge to let match terminate after next game.
1453 // if(gameMode != BeginningOfGame) {
1454 // DisplayError(_("You can only start a match from the initial position."), 0);
1458 if(mode == 2) appData.matchGames = appData.defaultMatchGames;
1459 /* Set up machine vs. machine match */
1461 NextTourneyGame(-1, &dummy); // sets appData.matchGames if this is tourney, to make sure ReserveGame knows it
1462 if(appData.tourneyFile[0]) {
1464 if(nextGame > appData.matchGames) {
1466 if(strchr(appData.results, '*') == NULL) {
1468 appData.tourneyCycles++;
1469 if(f = WriteTourneyFile(appData.results, NULL)) { // make a tourney file with increased number of cycles
1471 NextTourneyGame(-1, &dummy);
1473 if(nextGame <= appData.matchGames) {
1474 DisplayNote(_("You restarted an already completed tourney\nOne more cycle will now be added to it\nGames commence in 10 sec"));
1476 ScheduleDelayedEvent(NextMatchGame, 10000);
1481 snprintf(buf, MSG_SIZ, _("All games in tourney '%s' are already played or playing"), appData.tourneyFile);
1482 DisplayError(buf, 0);
1483 appData.tourneyFile[0] = 0;
1487 if (appData.noChessProgram) { // [HGM] in tourney engines are loaded automatically
1488 DisplayFatalError(_("Can't have a match with no chess programs"),
1493 matchGame = roundNr = 1;
1494 first.matchWins = second.matchWins = 0; // [HGM] match: needed in later matches
1498 char *comboLine = NULL; // [HGM] recent: WinBoard's first-engine combobox line
1501 InitBackEnd3 P((void))
1503 GameMode initialMode;
1507 InitChessProgram(&first, startedFromSetupPosition);
1509 if(!appData.noChessProgram) { /* [HGM] tidy: redo program version to use name from myname feature */
1510 free(programVersion);
1511 programVersion = (char*) malloc(8 + strlen(PACKAGE_STRING) + strlen(first.tidy));
1512 sprintf(programVersion, "%s + %s", PACKAGE_STRING, first.tidy);
1513 FloatToFront(&appData.recentEngineList, comboLine ? comboLine : appData.firstChessProgram);
1516 if (appData.icsActive) {
1518 /* [DM] Make a console window if needed [HGM] merged ifs */
1524 if (*appData.icsCommPort != NULLCHAR)
1525 len = snprintf(buf, MSG_SIZ, _("Could not open comm port %s"),
1526 appData.icsCommPort);
1528 len = snprintf(buf, MSG_SIZ, _("Could not connect to host %s, port %s"),
1529 appData.icsHost, appData.icsPort);
1531 if( (len >= MSG_SIZ) && appData.debugMode )
1532 fprintf(debugFP, "InitBackEnd3: buffer truncated.\n");
1534 DisplayFatalError(buf, err, 1);
1539 AddInputSource(icsPR, FALSE, read_from_ics, &telnetISR);
1541 AddInputSource(NoProc, FALSE, read_from_player, &fromUserISR);
1542 if(appData.keepAlive) // [HGM] alive: schedule sending of dummy 'date' command
1543 ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
1544 } else if (appData.noChessProgram) {
1550 if (*appData.cmailGameName != NULLCHAR) {
1552 OpenLoopback(&cmailPR);
1554 AddInputSource(cmailPR, FALSE, CmailSigHandlerCallBack, &cmailISR);
1558 DisplayMessage("", "");
1559 if (StrCaseCmp(appData.initialMode, "") == 0) {
1560 initialMode = BeginningOfGame;
1561 if(!appData.icsActive && appData.noChessProgram) { // [HGM] could be fall-back
1562 gameMode = MachinePlaysBlack; // "Machine Black" might have been implicitly highlighted
1563 ModeHighlight(); // make sure XBoard knows it is highlighted, so it will un-highlight it
1564 gameMode = BeginningOfGame; // in case BeginningOfGame now means "Edit Position"
1567 } else if (StrCaseCmp(appData.initialMode, "TwoMachines") == 0) {
1568 initialMode = TwoMachinesPlay;
1569 } else if (StrCaseCmp(appData.initialMode, "AnalyzeFile") == 0) {
1570 initialMode = AnalyzeFile;
1571 } else if (StrCaseCmp(appData.initialMode, "Analysis") == 0) {
1572 initialMode = AnalyzeMode;
1573 } else if (StrCaseCmp(appData.initialMode, "MachineWhite") == 0) {
1574 initialMode = MachinePlaysWhite;
1575 } else if (StrCaseCmp(appData.initialMode, "MachineBlack") == 0) {
1576 initialMode = MachinePlaysBlack;
1577 } else if (StrCaseCmp(appData.initialMode, "EditGame") == 0) {
1578 initialMode = EditGame;
1579 } else if (StrCaseCmp(appData.initialMode, "EditPosition") == 0) {
1580 initialMode = EditPosition;
1581 } else if (StrCaseCmp(appData.initialMode, "Training") == 0) {
1582 initialMode = Training;
1584 len = snprintf(buf, MSG_SIZ, _("Unknown initialMode %s"), appData.initialMode);
1585 if( (len >= MSG_SIZ) && appData.debugMode )
1586 fprintf(debugFP, "InitBackEnd3: buffer truncated.\n");
1588 DisplayFatalError(buf, 0, 2);
1592 if (appData.matchMode) {
1593 if(appData.tourneyFile[0]) { // start tourney from command line
1595 if(f = fopen(appData.tourneyFile, "r")) {
1596 ParseArgsFromFile(f); // make sure tourney parmeters re known
1598 appData.clockMode = TRUE;
1600 } else appData.tourneyFile[0] = NULLCHAR; // for now ignore bad tourney file
1603 } else if (*appData.cmailGameName != NULLCHAR) {
1604 /* Set up cmail mode */
1605 ReloadCmailMsgEvent(TRUE);
1607 /* Set up other modes */
1608 if (initialMode == AnalyzeFile) {
1609 if (*appData.loadGameFile == NULLCHAR) {
1610 DisplayFatalError(_("AnalyzeFile mode requires a game file"), 0, 1);
1614 if (*appData.loadGameFile != NULLCHAR) {
1615 (void) LoadGameFromFile(appData.loadGameFile,
1616 appData.loadGameIndex,
1617 appData.loadGameFile, TRUE);
1618 } else if (*appData.loadPositionFile != NULLCHAR) {
1619 (void) LoadPositionFromFile(appData.loadPositionFile,
1620 appData.loadPositionIndex,
1621 appData.loadPositionFile);
1622 /* [HGM] try to make self-starting even after FEN load */
1623 /* to allow automatic setup of fairy variants with wtm */
1624 if(initialMode == BeginningOfGame && !blackPlaysFirst) {
1625 gameMode = BeginningOfGame;
1626 setboardSpoiledMachineBlack = 1;
1628 /* [HGM] loadPos: make that every new game uses the setup */
1629 /* from file as long as we do not switch variant */
1630 if(!blackPlaysFirst) {
1631 startedFromPositionFile = TRUE;
1632 CopyBoard(filePosition, boards[0]);
1635 if (initialMode == AnalyzeMode) {
1636 if (appData.noChessProgram) {
1637 DisplayFatalError(_("Analysis mode requires a chess engine"), 0, 2);
1640 if (appData.icsActive) {
1641 DisplayFatalError(_("Analysis mode does not work with ICS mode"),0,2);
1645 } else if (initialMode == AnalyzeFile) {
1646 appData.showThinking = TRUE; // [HGM] thinking: moved out of ShowThinkingEvent
1647 ShowThinkingEvent();
1649 AnalysisPeriodicEvent(1);
1650 } else if (initialMode == MachinePlaysWhite) {
1651 if (appData.noChessProgram) {
1652 DisplayFatalError(_("MachineWhite mode requires a chess engine"),
1656 if (appData.icsActive) {
1657 DisplayFatalError(_("MachineWhite mode does not work with ICS mode"),
1661 MachineWhiteEvent();
1662 } else if (initialMode == MachinePlaysBlack) {
1663 if (appData.noChessProgram) {
1664 DisplayFatalError(_("MachineBlack mode requires a chess engine"),
1668 if (appData.icsActive) {
1669 DisplayFatalError(_("MachineBlack mode does not work with ICS mode"),
1673 MachineBlackEvent();
1674 } else if (initialMode == TwoMachinesPlay) {
1675 if (appData.noChessProgram) {
1676 DisplayFatalError(_("TwoMachines mode requires a chess engine"),
1680 if (appData.icsActive) {
1681 DisplayFatalError(_("TwoMachines mode does not work with ICS mode"),
1686 } else if (initialMode == EditGame) {
1688 } else if (initialMode == EditPosition) {
1689 EditPositionEvent();
1690 } else if (initialMode == Training) {
1691 if (*appData.loadGameFile == NULLCHAR) {
1692 DisplayFatalError(_("Training mode requires a game file"), 0, 2);
1701 HistorySet (char movelist[][2*MOVE_LEN], int first, int last, int current)
1703 DisplayBook(current+1);
1705 MoveHistorySet( movelist, first, last, current, pvInfoList );
1707 EvalGraphSet( first, last, current, pvInfoList );
1709 MakeEngineOutputTitle();
1713 * Establish will establish a contact to a remote host.port.
1714 * Sets icsPR to a ProcRef for a process (or pseudo-process)
1715 * used to talk to the host.
1716 * Returns 0 if okay, error code if not.
1723 if (*appData.icsCommPort != NULLCHAR) {
1724 /* Talk to the host through a serial comm port */
1725 return OpenCommPort(appData.icsCommPort, &icsPR);
1727 } else if (*appData.gateway != NULLCHAR) {
1728 if (*appData.remoteShell == NULLCHAR) {
1729 /* Use the rcmd protocol to run telnet program on a gateway host */
1730 snprintf(buf, sizeof(buf), "%s %s %s",
1731 appData.telnetProgram, appData.icsHost, appData.icsPort);
1732 return OpenRcmd(appData.gateway, appData.remoteUser, buf, &icsPR);
1735 /* Use the rsh program to run telnet program on a gateway host */
1736 if (*appData.remoteUser == NULLCHAR) {
1737 snprintf(buf, sizeof(buf), "%s %s %s %s %s", appData.remoteShell,
1738 appData.gateway, appData.telnetProgram,
1739 appData.icsHost, appData.icsPort);
1741 snprintf(buf, sizeof(buf), "%s %s -l %s %s %s %s",
1742 appData.remoteShell, appData.gateway,
1743 appData.remoteUser, appData.telnetProgram,
1744 appData.icsHost, appData.icsPort);
1746 return StartChildProcess(buf, "", &icsPR);
1749 } else if (appData.useTelnet) {
1750 return OpenTelnet(appData.icsHost, appData.icsPort, &icsPR);
1753 /* TCP socket interface differs somewhat between
1754 Unix and NT; handle details in the front end.
1756 return OpenTCP(appData.icsHost, appData.icsPort, &icsPR);
1761 EscapeExpand (char *p, char *q)
1762 { // [HGM] initstring: routine to shape up string arguments
1763 while(*p++ = *q++) if(p[-1] == '\\')
1765 case 'n': p[-1] = '\n'; break;
1766 case 'r': p[-1] = '\r'; break;
1767 case 't': p[-1] = '\t'; break;
1768 case '\\': p[-1] = '\\'; break;
1769 case 0: *p = 0; return;
1770 default: p[-1] = q[-1]; break;
1775 show_bytes (FILE *fp, char *buf, int count)
1778 if (*buf < 040 || *(unsigned char *) buf > 0177) {
1779 fprintf(fp, "\\%03o", *buf & 0xff);
1788 /* Returns an errno value */
1790 OutputMaybeTelnet (ProcRef pr, char *message, int count, int *outError)
1792 char buf[8192], *p, *q, *buflim;
1793 int left, newcount, outcount;
1795 if (*appData.icsCommPort != NULLCHAR || appData.useTelnet ||
1796 *appData.gateway != NULLCHAR) {
1797 if (appData.debugMode) {
1798 fprintf(debugFP, ">ICS: ");
1799 show_bytes(debugFP, message, count);
1800 fprintf(debugFP, "\n");
1802 return OutputToProcess(pr, message, count, outError);
1805 buflim = &buf[sizeof(buf)-1]; /* allow 1 byte for expanding last char */
1812 if (appData.debugMode) {
1813 fprintf(debugFP, ">ICS: ");
1814 show_bytes(debugFP, buf, newcount);
1815 fprintf(debugFP, "\n");
1817 outcount = OutputToProcess(pr, buf, newcount, outError);
1818 if (outcount < newcount) return -1; /* to be sure */
1825 } else if (((unsigned char) *p) == TN_IAC) {
1826 *q++ = (char) TN_IAC;
1833 if (appData.debugMode) {
1834 fprintf(debugFP, ">ICS: ");
1835 show_bytes(debugFP, buf, newcount);
1836 fprintf(debugFP, "\n");
1838 outcount = OutputToProcess(pr, buf, newcount, outError);
1839 if (outcount < newcount) return -1; /* to be sure */
1844 read_from_player (InputSourceRef isr, VOIDSTAR closure, char *message, int count, int error)
1846 int outError, outCount;
1847 static int gotEof = 0;
1850 /* Pass data read from player on to ICS */
1853 outCount = OutputMaybeTelnet(icsPR, message, count, &outError);
1854 if (outCount < count) {
1855 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1857 if(have_sent_ICS_logon == 2) {
1858 if(ini = fopen(appData.icsLogon, "w")) { // save first two lines (presumably username & password) on init script file
1859 fprintf(ini, "%s", message);
1860 have_sent_ICS_logon = 3;
1862 have_sent_ICS_logon = 1;
1863 } else if(have_sent_ICS_logon == 3) {
1864 fprintf(ini, "%s", message);
1866 have_sent_ICS_logon = 1;
1868 } else if (count < 0) {
1869 RemoveInputSource(isr);
1870 DisplayFatalError(_("Error reading from keyboard"), error, 1);
1871 } else if (gotEof++ > 0) {
1872 RemoveInputSource(isr);
1873 DisplayFatalError(_("Got end of file from keyboard"), 0, 0);
1879 { // [HGM] alive: periodically send dummy (date) command to ICS to prevent time-out
1880 if(!connectionAlive) DisplayFatalError("No response from ICS", 0, 1);
1881 connectionAlive = FALSE; // only sticks if no response to 'date' command.
1882 SendToICS("date\n");
1883 if(appData.keepAlive) ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
1886 /* added routine for printf style output to ics */
1888 ics_printf (char *format, ...)
1890 char buffer[MSG_SIZ];
1893 va_start(args, format);
1894 vsnprintf(buffer, sizeof(buffer), format, args);
1895 buffer[sizeof(buffer)-1] = '\0';
1903 int count, outCount, outError;
1905 if (icsPR == NoProc) return;
1908 outCount = OutputMaybeTelnet(icsPR, s, count, &outError);
1909 if (outCount < count) {
1910 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1914 /* This is used for sending logon scripts to the ICS. Sending
1915 without a delay causes problems when using timestamp on ICC
1916 (at least on my machine). */
1918 SendToICSDelayed (char *s, long msdelay)
1920 int count, outCount, outError;
1922 if (icsPR == NoProc) return;
1925 if (appData.debugMode) {
1926 fprintf(debugFP, ">ICS: ");
1927 show_bytes(debugFP, s, count);
1928 fprintf(debugFP, "\n");
1930 outCount = OutputToProcessDelayed(icsPR, s, count, &outError,
1932 if (outCount < count) {
1933 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1938 /* Remove all highlighting escape sequences in s
1939 Also deletes any suffix starting with '('
1942 StripHighlightAndTitle (char *s)
1944 static char retbuf[MSG_SIZ];
1947 while (*s != NULLCHAR) {
1948 while (*s == '\033') {
1949 while (*s != NULLCHAR && !isalpha(*s)) s++;
1950 if (*s != NULLCHAR) s++;
1952 while (*s != NULLCHAR && *s != '\033') {
1953 if (*s == '(' || *s == '[') {
1964 /* Remove all highlighting escape sequences in s */
1966 StripHighlight (char *s)
1968 static char retbuf[MSG_SIZ];
1971 while (*s != NULLCHAR) {
1972 while (*s == '\033') {
1973 while (*s != NULLCHAR && !isalpha(*s)) s++;
1974 if (*s != NULLCHAR) s++;
1976 while (*s != NULLCHAR && *s != '\033') {
1984 char *variantNames[] = VARIANT_NAMES;
1986 VariantName (VariantClass v)
1988 return variantNames[v];
1992 /* Identify a variant from the strings the chess servers use or the
1993 PGN Variant tag names we use. */
1995 StringToVariant (char *e)
1999 VariantClass v = VariantNormal;
2000 int i, found = FALSE;
2006 /* [HGM] skip over optional board-size prefixes */
2007 if( sscanf(e, "%dx%d_", &i, &i) == 2 ||
2008 sscanf(e, "%dx%d+%d_", &i, &i, &i) == 3 ) {
2009 while( *e++ != '_');
2012 if(StrCaseStr(e, "misc/")) { // [HGM] on FICS, misc/shogi is not shogi
2016 for (i=0; i<sizeof(variantNames)/sizeof(char*); i++) {
2017 if (StrCaseStr(e, variantNames[i])) {
2018 v = (VariantClass) i;
2025 if ((StrCaseStr(e, "fischer") && StrCaseStr(e, "random"))
2026 || StrCaseStr(e, "wild/fr")
2027 || StrCaseStr(e, "frc") || StrCaseStr(e, "960")) {
2028 v = VariantFischeRandom;
2029 } else if ((i = 4, p = StrCaseStr(e, "wild")) ||
2030 (i = 1, p = StrCaseStr(e, "w"))) {
2032 while (*p && (isspace(*p) || *p == '(' || *p == '/')) p++;
2039 case 0: /* FICS only, actually */
2041 /* Castling legal even if K starts on d-file */
2042 v = VariantWildCastle;
2047 /* Castling illegal even if K & R happen to start in
2048 normal positions. */
2049 v = VariantNoCastle;
2062 /* Castling legal iff K & R start in normal positions */
2068 /* Special wilds for position setup; unclear what to do here */
2069 v = VariantLoadable;
2072 /* Bizarre ICC game */
2073 v = VariantTwoKings;
2076 v = VariantKriegspiel;
2082 v = VariantFischeRandom;
2085 v = VariantCrazyhouse;
2088 v = VariantBughouse;
2094 /* Not quite the same as FICS suicide! */
2095 v = VariantGiveaway;
2101 v = VariantShatranj;
2104 /* Temporary names for future ICC types. The name *will* change in
2105 the next xboard/WinBoard release after ICC defines it. */
2143 v = VariantCapablanca;
2146 v = VariantKnightmate;
2152 v = VariantCylinder;
2158 v = VariantCapaRandom;
2161 v = VariantBerolina;
2173 /* Found "wild" or "w" in the string but no number;
2174 must assume it's normal chess. */
2178 len = snprintf(buf, MSG_SIZ, _("Unknown wild type %d"), wnum);
2179 if( (len >= MSG_SIZ) && appData.debugMode )
2180 fprintf(debugFP, "StringToVariant: buffer truncated.\n");
2182 DisplayError(buf, 0);
2188 if (appData.debugMode) {
2189 fprintf(debugFP, _("recognized '%s' (%d) as variant %s\n"),
2190 e, wnum, VariantName(v));
2195 static int leftover_start = 0, leftover_len = 0;
2196 char star_match[STAR_MATCH_N][MSG_SIZ];
2198 /* Test whether pattern is present at &buf[*index]; if so, return TRUE,
2199 advance *index beyond it, and set leftover_start to the new value of
2200 *index; else return FALSE. If pattern contains the character '*', it
2201 matches any sequence of characters not containing '\r', '\n', or the
2202 character following the '*' (if any), and the matched sequence(s) are
2203 copied into star_match.
2206 looking_at ( char *buf, int *index, char *pattern)
2208 char *bufp = &buf[*index], *patternp = pattern;
2210 char *matchp = star_match[0];
2213 if (*patternp == NULLCHAR) {
2214 *index = leftover_start = bufp - buf;
2218 if (*bufp == NULLCHAR) return FALSE;
2219 if (*patternp == '*') {
2220 if (*bufp == *(patternp + 1)) {
2222 matchp = star_match[++star_count];
2226 } else if (*bufp == '\n' || *bufp == '\r') {
2228 if (*patternp == NULLCHAR)
2233 *matchp++ = *bufp++;
2237 if (*patternp != *bufp) return FALSE;
2244 SendToPlayer (char *data, int length)
2246 int error, outCount;
2247 outCount = OutputToProcess(NoProc, data, length, &error);
2248 if (outCount < length) {
2249 DisplayFatalError(_("Error writing to display"), error, 1);
2254 PackHolding (char packed[], char *holding)
2264 switch (runlength) {
2275 sprintf(q, "%d", runlength);
2287 /* Telnet protocol requests from the front end */
2289 TelnetRequest (unsigned char ddww, unsigned char option)
2291 unsigned char msg[3];
2292 int outCount, outError;
2294 if (*appData.icsCommPort != NULLCHAR || appData.useTelnet) return;
2296 if (appData.debugMode) {
2297 char buf1[8], buf2[8], *ddwwStr, *optionStr;
2313 snprintf(buf1,sizeof(buf1)/sizeof(buf1[0]), "%d", ddww);
2322 snprintf(buf2,sizeof(buf2)/sizeof(buf2[0]), "%d", option);
2325 fprintf(debugFP, ">%s %s ", ddwwStr, optionStr);
2330 outCount = OutputToProcess(icsPR, (char *)msg, 3, &outError);
2332 DisplayFatalError(_("Error writing to ICS"), outError, 1);
2339 if (!appData.icsActive) return;
2340 TelnetRequest(TN_DO, TN_ECHO);
2346 if (!appData.icsActive) return;
2347 TelnetRequest(TN_DONT, TN_ECHO);
2351 CopyHoldings (Board board, char *holdings, ChessSquare lowestPiece)
2353 /* put the holdings sent to us by the server on the board holdings area */
2354 int i, j, holdingsColumn, holdingsStartRow, direction, countsColumn;
2358 if(gameInfo.holdingsWidth < 2) return;
2359 if(gameInfo.variant != VariantBughouse && board[HOLDINGS_SET])
2360 return; // prevent overwriting by pre-board holdings
2362 if( (int)lowestPiece >= BlackPawn ) {
2365 holdingsStartRow = BOARD_HEIGHT-1;
2368 holdingsColumn = BOARD_WIDTH-1;
2369 countsColumn = BOARD_WIDTH-2;
2370 holdingsStartRow = 0;
2374 for(i=0; i<BOARD_HEIGHT; i++) { /* clear holdings */
2375 board[i][holdingsColumn] = EmptySquare;
2376 board[i][countsColumn] = (ChessSquare) 0;
2378 while( (p=*holdings++) != NULLCHAR ) {
2379 piece = CharToPiece( ToUpper(p) );
2380 if(piece == EmptySquare) continue;
2381 /*j = (int) piece - (int) WhitePawn;*/
2382 j = PieceToNumber(piece);
2383 if(j >= gameInfo.holdingsSize) continue; /* ignore pieces that do not fit */
2384 if(j < 0) continue; /* should not happen */
2385 piece = (ChessSquare) ( (int)piece + (int)lowestPiece );
2386 board[holdingsStartRow+j*direction][holdingsColumn] = piece;
2387 board[holdingsStartRow+j*direction][countsColumn]++;
2393 VariantSwitch (Board board, VariantClass newVariant)
2395 int newHoldingsWidth, newWidth = 8, newHeight = 8, i, j;
2396 static Board oldBoard;
2398 startedFromPositionFile = FALSE;
2399 if(gameInfo.variant == newVariant) return;
2401 /* [HGM] This routine is called each time an assignment is made to
2402 * gameInfo.variant during a game, to make sure the board sizes
2403 * are set to match the new variant. If that means adding or deleting
2404 * holdings, we shift the playing board accordingly
2405 * This kludge is needed because in ICS observe mode, we get boards
2406 * of an ongoing game without knowing the variant, and learn about the
2407 * latter only later. This can be because of the move list we requested,
2408 * in which case the game history is refilled from the beginning anyway,
2409 * but also when receiving holdings of a crazyhouse game. In the latter
2410 * case we want to add those holdings to the already received position.
2414 if (appData.debugMode) {
2415 fprintf(debugFP, "Switch board from %s to %s\n",
2416 VariantName(gameInfo.variant), VariantName(newVariant));
2417 setbuf(debugFP, NULL);
2419 shuffleOpenings = 0; /* [HGM] shuffle */
2420 gameInfo.holdingsSize = 5; /* [HGM] prepare holdings */
2424 newWidth = 9; newHeight = 9;
2425 gameInfo.holdingsSize = 7;
2426 case VariantBughouse:
2427 case VariantCrazyhouse:
2428 newHoldingsWidth = 2; break;
2432 newHoldingsWidth = 2;
2433 gameInfo.holdingsSize = 8;
2436 case VariantCapablanca:
2437 case VariantCapaRandom:
2440 newHoldingsWidth = gameInfo.holdingsSize = 0;
2443 if(newWidth != gameInfo.boardWidth ||
2444 newHeight != gameInfo.boardHeight ||
2445 newHoldingsWidth != gameInfo.holdingsWidth ) {
2447 /* shift position to new playing area, if needed */
2448 if(newHoldingsWidth > gameInfo.holdingsWidth) {
2449 for(i=0; i<BOARD_HEIGHT; i++)
2450 for(j=BOARD_RGHT-1; j>=BOARD_LEFT; j--)
2451 board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
2453 for(i=0; i<newHeight; i++) {
2454 board[i][0] = board[i][newWidth+2*newHoldingsWidth-1] = EmptySquare;
2455 board[i][1] = board[i][newWidth+2*newHoldingsWidth-2] = (ChessSquare) 0;
2457 } else if(newHoldingsWidth < gameInfo.holdingsWidth) {
2458 for(i=0; i<BOARD_HEIGHT; i++)
2459 for(j=BOARD_LEFT; j<BOARD_RGHT; j++)
2460 board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
2463 board[HOLDINGS_SET] = 0;
2464 gameInfo.boardWidth = newWidth;
2465 gameInfo.boardHeight = newHeight;
2466 gameInfo.holdingsWidth = newHoldingsWidth;
2467 gameInfo.variant = newVariant;
2468 InitDrawingSizes(-2, 0);
2469 } else gameInfo.variant = newVariant;
2470 CopyBoard(oldBoard, board); // remember correctly formatted board
2471 InitPosition(FALSE); /* this sets up board[0], but also other stuff */
2472 DrawPosition(TRUE, currentMove ? boards[currentMove] : oldBoard);
2475 static int loggedOn = FALSE;
2477 /*-- Game start info cache: --*/
2479 char gs_kind[MSG_SIZ];
2480 static char player1Name[128] = "";
2481 static char player2Name[128] = "";
2482 static char cont_seq[] = "\n\\ ";
2483 static int player1Rating = -1;
2484 static int player2Rating = -1;
2485 /*----------------------------*/
2487 ColorClass curColor = ColorNormal;
2488 int suppressKibitz = 0;
2491 Boolean soughtPending = FALSE;
2492 Boolean seekGraphUp;
2493 #define MAX_SEEK_ADS 200
2495 char *seekAdList[MAX_SEEK_ADS];
2496 int ratingList[MAX_SEEK_ADS], xList[MAX_SEEK_ADS], yList[MAX_SEEK_ADS], seekNrList[MAX_SEEK_ADS], zList[MAX_SEEK_ADS];
2497 float tcList[MAX_SEEK_ADS];
2498 char colorList[MAX_SEEK_ADS];
2499 int nrOfSeekAds = 0;
2500 int minRating = 1010, maxRating = 2800;
2501 int hMargin = 10, vMargin = 20, h, w;
2502 extern int squareSize, lineGap;
2507 int x, y, color = 0, r = ratingList[i]; float tc = tcList[i];
2508 xList[i] = yList[i] = -100; // outside graph, so cannot be clicked
2509 if(r < minRating+100 && r >=0 ) r = minRating+100;
2510 if(r > maxRating) r = maxRating;
2511 if(tc < 1.f) tc = 1.f;
2512 if(tc > 95.f) tc = 95.f;
2513 x = (w-hMargin-squareSize/8-7)* log(tc)/log(95.) + hMargin;
2514 y = ((double)r - minRating)/(maxRating - minRating)
2515 * (h-vMargin-squareSize/8-1) + vMargin;
2516 if(ratingList[i] < 0) y = vMargin + squareSize/4;
2517 if(strstr(seekAdList[i], " u ")) color = 1;
2518 if(!strstr(seekAdList[i], "lightning") && // for now all wilds same color
2519 !strstr(seekAdList[i], "bullet") &&
2520 !strstr(seekAdList[i], "blitz") &&
2521 !strstr(seekAdList[i], "standard") ) color = 2;
2522 if(strstr(seekAdList[i], "(C) ")) color |= SQUARE; // plot computer seeks as squares
2523 DrawSeekDot(xList[i]=x+3*(color&~SQUARE), yList[i]=h-1-y, colorList[i]=color);
2527 PlotSingleSeekAd (int i)
2533 AddAd (char *handle, char *rating, int base, int inc, char rated, char *type, int nr, Boolean plot)
2535 char buf[MSG_SIZ], *ext = "";
2536 VariantClass v = StringToVariant(type);
2537 if(strstr(type, "wild")) {
2538 ext = type + 4; // append wild number
2539 if(v == VariantFischeRandom) type = "chess960"; else
2540 if(v == VariantLoadable) type = "setup"; else
2541 type = VariantName(v);
2543 snprintf(buf, MSG_SIZ, "%s (%s) %d %d %c %s%s", handle, rating, base, inc, rated, type, ext);
2544 if(nrOfSeekAds < MAX_SEEK_ADS-1) {
2545 if(seekAdList[nrOfSeekAds]) free(seekAdList[nrOfSeekAds]);
2546 ratingList[nrOfSeekAds] = -1; // for if seeker has no rating
2547 sscanf(rating, "%d", &ratingList[nrOfSeekAds]);
2548 tcList[nrOfSeekAds] = base + (2./3.)*inc;
2549 seekNrList[nrOfSeekAds] = nr;
2550 zList[nrOfSeekAds] = 0;
2551 seekAdList[nrOfSeekAds++] = StrSave(buf);
2552 if(plot) PlotSingleSeekAd(nrOfSeekAds-1);
2557 EraseSeekDot (int i)
2559 int x = xList[i], y = yList[i], d=squareSize/4, k;
2560 DrawSeekBackground(x-squareSize/8, y-squareSize/8, x+squareSize/8+1, y+squareSize/8+1);
2561 if(x < hMargin+d) DrawSeekAxis(hMargin, y-squareSize/8, hMargin, y+squareSize/8+1);
2562 // now replot every dot that overlapped
2563 for(k=0; k<nrOfSeekAds; k++) if(k != i) {
2564 int xx = xList[k], yy = yList[k];
2565 if(xx <= x+d && xx > x-d && yy <= y+d && yy > y-d)
2566 DrawSeekDot(xx, yy, colorList[k]);
2571 RemoveSeekAd (int nr)
2574 for(i=0; i<nrOfSeekAds; i++) if(seekNrList[i] == nr) {
2576 if(seekAdList[i]) free(seekAdList[i]);
2577 seekAdList[i] = seekAdList[--nrOfSeekAds];
2578 seekNrList[i] = seekNrList[nrOfSeekAds];
2579 ratingList[i] = ratingList[nrOfSeekAds];
2580 colorList[i] = colorList[nrOfSeekAds];
2581 tcList[i] = tcList[nrOfSeekAds];
2582 xList[i] = xList[nrOfSeekAds];
2583 yList[i] = yList[nrOfSeekAds];
2584 zList[i] = zList[nrOfSeekAds];
2585 seekAdList[nrOfSeekAds] = NULL;
2591 MatchSoughtLine (char *line)
2593 char handle[MSG_SIZ], rating[MSG_SIZ], type[MSG_SIZ];
2594 int nr, base, inc, u=0; char dummy;
2596 if(sscanf(line, "%d %s %s %d %d rated %s", &nr, rating, handle, &base, &inc, type) == 6 ||
2597 sscanf(line, "%d %s %s %s %d %d rated %c", &nr, rating, handle, type, &base, &inc, &dummy) == 7 ||
2599 (sscanf(line, "%d %s %s %d %d unrated %s", &nr, rating, handle, &base, &inc, type) == 6 ||
2600 sscanf(line, "%d %s %s %s %d %d unrated %c", &nr, rating, handle, type, &base, &inc, &dummy) == 7) ) {
2601 // match: compact and save the line
2602 AddAd(handle, rating, base, inc, u ? 'u' : 'r', type, nr, FALSE);
2612 if(!seekGraphUp) return FALSE;
2613 h = BOARD_HEIGHT * (squareSize + lineGap) + lineGap;
2614 w = BOARD_WIDTH * (squareSize + lineGap) + lineGap;
2616 DrawSeekBackground(0, 0, w, h);
2617 DrawSeekAxis(hMargin, h-1-vMargin, w-5, h-1-vMargin);
2618 DrawSeekAxis(hMargin, h-1-vMargin, hMargin, 5);
2619 for(i=0; i<4000; i+= 100) if(i>=minRating && i<maxRating) {
2620 int yy =((double)i - minRating)/(maxRating - minRating)*(h-vMargin-squareSize/8-1) + vMargin;
2622 DrawSeekAxis(hMargin-5, yy, hMargin+5*(i%500==0), yy); // rating ticks
2625 snprintf(buf, MSG_SIZ, "%d", i);
2626 DrawSeekText(buf, hMargin+squareSize/8+7, yy);
2629 DrawSeekText("unrated", hMargin+squareSize/8+7, h-1-vMargin-squareSize/4);
2630 for(i=1; i<100; i+=(i<10?1:5)) {
2631 int xx = (w-hMargin-squareSize/8-7)* log((double)i)/log(95.) + hMargin;
2632 DrawSeekAxis(xx, h-1-vMargin, xx, h-6-vMargin-3*(i%10==0)); // TC ticks
2633 if(i<=5 || (i>40 ? i%20 : i%10) == 0) {
2635 snprintf(buf, MSG_SIZ, "%d", i);
2636 DrawSeekText(buf, xx-2-3*(i>9), h-1-vMargin/2);
2639 for(i=0; i<nrOfSeekAds; i++) PlotSeekAd(i);
2644 SeekGraphClick (ClickType click, int x, int y, int moving)
2646 static int lastDown = 0, displayed = 0, lastSecond;
2647 if(y < 0) return FALSE;
2648 if(!(appData.seekGraph && appData.icsActive && loggedOn &&
2649 (gameMode == BeginningOfGame || gameMode == IcsIdle))) {
2650 if(!seekGraphUp) return FALSE;
2651 seekGraphUp = FALSE; // seek graph is up when it shouldn't be: take it down
2652 DrawPosition(TRUE, NULL);
2655 if(!seekGraphUp) { // initiate cration of seek graph by requesting seek-ad list
2656 if(click == Release || moving) return FALSE;
2658 soughtPending = TRUE;
2659 SendToICS(ics_prefix);
2660 SendToICS("sought\n"); // should this be "sought all"?
2661 } else { // issue challenge based on clicked ad
2662 int dist = 10000; int i, closest = 0, second = 0;
2663 for(i=0; i<nrOfSeekAds; i++) {
2664 int d = (x-xList[i])*(x-xList[i]) + (y-yList[i])*(y-yList[i]) + zList[i];
2665 if(d < dist) { dist = d; closest = i; }
2666 second += (d - zList[i] < 120); // count in-range ads
2667 if(click == Press && moving != 1 && zList[i]>0) zList[i] *= 0.8; // age priority
2671 second = (second > 1);
2672 if(displayed != closest || second != lastSecond) {
2673 DisplayMessage(second ? "!" : "", seekAdList[closest]);
2674 lastSecond = second; displayed = closest;
2676 if(click == Press) {
2677 if(moving == 2) zList[closest] = 100; // right-click; push to back on press
2680 } // on press 'hit', only show info
2681 if(moving == 2) return TRUE; // ignore right up-clicks on dot
2682 snprintf(buf, MSG_SIZ, "play %d\n", seekNrList[closest]);
2683 SendToICS(ics_prefix);
2685 return TRUE; // let incoming board of started game pop down the graph
2686 } else if(click == Release) { // release 'miss' is ignored
2687 zList[lastDown] = 100; // make future selection of the rejected ad more difficult
2688 if(moving == 2) { // right up-click
2689 nrOfSeekAds = 0; // refresh graph
2690 soughtPending = TRUE;
2691 SendToICS(ics_prefix);
2692 SendToICS("sought\n"); // should this be "sought all"?
2695 } else if(moving) { if(displayed >= 0) DisplayMessage("", ""); displayed = -1; return TRUE; }
2696 // press miss or release hit 'pop down' seek graph
2697 seekGraphUp = FALSE;
2698 DrawPosition(TRUE, NULL);
2704 read_from_ics (InputSourceRef isr, VOIDSTAR closure, char *data, int count, int error)
2706 #define BUF_SIZE (16*1024) /* overflowed at 8K with "inchannel 1" on FICS? */
2707 #define STARTED_NONE 0
2708 #define STARTED_MOVES 1
2709 #define STARTED_BOARD 2
2710 #define STARTED_OBSERVE 3
2711 #define STARTED_HOLDINGS 4
2712 #define STARTED_CHATTER 5
2713 #define STARTED_COMMENT 6
2714 #define STARTED_MOVES_NOHIDE 7
2716 static int started = STARTED_NONE;
2717 static char parse[20000];
2718 static int parse_pos = 0;
2719 static char buf[BUF_SIZE + 1];
2720 static int firstTime = TRUE, intfSet = FALSE;
2721 static ColorClass prevColor = ColorNormal;
2722 static int savingComment = FALSE;
2723 static int cmatch = 0; // continuation sequence match
2730 int backup; /* [DM] For zippy color lines */
2732 char talker[MSG_SIZ]; // [HGM] chat
2735 connectionAlive = TRUE; // [HGM] alive: I think, therefore I am...
2737 if (appData.debugMode) {
2739 fprintf(debugFP, "<ICS: ");
2740 show_bytes(debugFP, data, count);
2741 fprintf(debugFP, "\n");
2745 if (appData.debugMode) { int f = forwardMostMove;
2746 fprintf(debugFP, "ics input %d, castling = %d %d %d %d %d %d\n", f,
2747 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
2748 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
2751 /* If last read ended with a partial line that we couldn't parse,
2752 prepend it to the new read and try again. */
2753 if (leftover_len > 0) {
2754 for (i=0; i<leftover_len; i++)
2755 buf[i] = buf[leftover_start + i];
2758 /* copy new characters into the buffer */
2759 bp = buf + leftover_len;
2760 buf_len=leftover_len;
2761 for (i=0; i<count; i++)
2764 if (data[i] == '\r')
2767 // join lines split by ICS?
2768 if (!appData.noJoin)
2771 Joining just consists of finding matches against the
2772 continuation sequence, and discarding that sequence
2773 if found instead of copying it. So, until a match
2774 fails, there's nothing to do since it might be the
2775 complete sequence, and thus, something we don't want
2778 if (data[i] == cont_seq[cmatch])
2781 if (cmatch == strlen(cont_seq))
2783 cmatch = 0; // complete match. just reset the counter
2786 it's possible for the ICS to not include the space
2787 at the end of the last word, making our [correct]
2788 join operation fuse two separate words. the server
2789 does this when the space occurs at the width setting.
2791 if (!buf_len || buf[buf_len-1] != ' ')
2802 match failed, so we have to copy what matched before
2803 falling through and copying this character. In reality,
2804 this will only ever be just the newline character, but
2805 it doesn't hurt to be precise.
2807 strncpy(bp, cont_seq, cmatch);
2819 buf[buf_len] = NULLCHAR;
2820 // next_out = leftover_len; // [HGM] should we set this to 0, and not print it in advance?
2825 while (i < buf_len) {
2826 /* Deal with part of the TELNET option negotiation
2827 protocol. We refuse to do anything beyond the
2828 defaults, except that we allow the WILL ECHO option,
2829 which ICS uses to turn off password echoing when we are
2830 directly connected to it. We reject this option
2831 if localLineEditing mode is on (always on in xboard)
2832 and we are talking to port 23, which might be a real
2833 telnet server that will try to keep WILL ECHO on permanently.
2835 if (buf_len - i >= 3 && (unsigned char) buf[i] == TN_IAC) {
2836 static int remoteEchoOption = FALSE; /* telnet ECHO option */
2837 unsigned char option;
2839 switch ((unsigned char) buf[++i]) {
2841 if (appData.debugMode)
2842 fprintf(debugFP, "\n<WILL ");
2843 switch (option = (unsigned char) buf[++i]) {
2845 if (appData.debugMode)
2846 fprintf(debugFP, "ECHO ");
2847 /* Reply only if this is a change, according
2848 to the protocol rules. */
2849 if (remoteEchoOption) break;
2850 if (appData.localLineEditing &&
2851 atoi(appData.icsPort) == TN_PORT) {
2852 TelnetRequest(TN_DONT, TN_ECHO);
2855 TelnetRequest(TN_DO, TN_ECHO);
2856 remoteEchoOption = TRUE;
2860 if (appData.debugMode)
2861 fprintf(debugFP, "%d ", option);
2862 /* Whatever this is, we don't want it. */
2863 TelnetRequest(TN_DONT, option);
2868 if (appData.debugMode)
2869 fprintf(debugFP, "\n<WONT ");
2870 switch (option = (unsigned char) buf[++i]) {
2872 if (appData.debugMode)
2873 fprintf(debugFP, "ECHO ");
2874 /* Reply only if this is a change, according
2875 to the protocol rules. */
2876 if (!remoteEchoOption) break;
2878 TelnetRequest(TN_DONT, TN_ECHO);
2879 remoteEchoOption = FALSE;
2882 if (appData.debugMode)
2883 fprintf(debugFP, "%d ", (unsigned char) option);
2884 /* Whatever this is, it must already be turned
2885 off, because we never agree to turn on
2886 anything non-default, so according to the
2887 protocol rules, we don't reply. */
2892 if (appData.debugMode)
2893 fprintf(debugFP, "\n<DO ");
2894 switch (option = (unsigned char) buf[++i]) {
2896 /* Whatever this is, we refuse to do it. */
2897 if (appData.debugMode)
2898 fprintf(debugFP, "%d ", option);
2899 TelnetRequest(TN_WONT, option);
2904 if (appData.debugMode)
2905 fprintf(debugFP, "\n<DONT ");
2906 switch (option = (unsigned char) buf[++i]) {
2908 if (appData.debugMode)
2909 fprintf(debugFP, "%d ", option);
2910 /* Whatever this is, we are already not doing
2911 it, because we never agree to do anything
2912 non-default, so according to the protocol
2913 rules, we don't reply. */
2918 if (appData.debugMode)
2919 fprintf(debugFP, "\n<IAC ");
2920 /* Doubled IAC; pass it through */
2924 if (appData.debugMode)
2925 fprintf(debugFP, "\n<%d ", (unsigned char) buf[i]);
2926 /* Drop all other telnet commands on the floor */
2929 if (oldi > next_out)
2930 SendToPlayer(&buf[next_out], oldi - next_out);
2936 /* OK, this at least will *usually* work */
2937 if (!loggedOn && looking_at(buf, &i, "ics%")) {
2941 if (loggedOn && !intfSet) {
2942 if (ics_type == ICS_ICC) {
2943 snprintf(str, MSG_SIZ,
2944 "/set-quietly interface %s\n/set-quietly style 12\n",
2946 if(appData.seekGraph && appData.autoRefresh) // [HGM] seekgraph
2947 strcat(str, "/set-2 51 1\n/set seek 1\n");
2948 } else if (ics_type == ICS_CHESSNET) {
2949 snprintf(str, MSG_SIZ, "/style 12\n");
2951 safeStrCpy(str, "alias $ @\n$set interface ", sizeof(str)/sizeof(str[0]));
2952 strcat(str, programVersion);
2953 strcat(str, "\n$iset startpos 1\n$iset ms 1\n");
2954 if(appData.seekGraph && appData.autoRefresh) // [HGM] seekgraph
2955 strcat(str, "$iset seekremove 1\n$set seek 1\n");
2957 strcat(str, "$iset nohighlight 1\n");
2959 strcat(str, "$iset lock 1\n$style 12\n");
2962 NotifyFrontendLogin();
2966 if (started == STARTED_COMMENT) {
2967 /* Accumulate characters in comment */
2968 parse[parse_pos++] = buf[i];
2969 if (buf[i] == '\n') {
2970 parse[parse_pos] = NULLCHAR;
2971 if(chattingPartner>=0) {
2973 snprintf(mess, MSG_SIZ, "%s%s", talker, parse);
2974 OutputChatMessage(chattingPartner, mess);
2975 chattingPartner = -1;
2976 next_out = i+1; // [HGM] suppress printing in ICS window
2978 if(!suppressKibitz) // [HGM] kibitz
2979 AppendComment(forwardMostMove, StripHighlight(parse), TRUE);
2980 else { // [HGM kibitz: divert memorized engine kibitz to engine-output window
2981 int nrDigit = 0, nrAlph = 0, j;
2982 if(parse_pos > MSG_SIZ - 30) // defuse unreasonably long input
2983 { parse_pos = MSG_SIZ-30; parse[parse_pos - 1] = '\n'; }
2984 parse[parse_pos] = NULLCHAR;
2985 // try to be smart: if it does not look like search info, it should go to
2986 // ICS interaction window after all, not to engine-output window.
2987 for(j=0; j<parse_pos; j++) { // count letters and digits
2988 nrDigit += (parse[j] >= '0' && parse[j] <= '9');
2989 nrAlph += (parse[j] >= 'a' && parse[j] <= 'z');
2990 nrAlph += (parse[j] >= 'A' && parse[j] <= 'Z');
2992 if(nrAlph < 9*nrDigit) { // if more than 10% digit we assume search info
2993 int depth=0; float score;
2994 if(sscanf(parse, "!!! %f/%d", &score, &depth) == 2 && depth>0) {
2995 // [HGM] kibitz: save kibitzed opponent info for PGN and eval graph
2996 pvInfoList[forwardMostMove-1].depth = depth;
2997 pvInfoList[forwardMostMove-1].score = 100*score;
2999 OutputKibitz(suppressKibitz, parse);
3002 if(gameMode == IcsObserving) // restore original ICS messages
3003 snprintf(tmp, MSG_SIZ, "%s kibitzes: %s", star_match[0], parse);
3005 snprintf(tmp, MSG_SIZ, _("your opponent kibitzes: %s"), parse);
3006 SendToPlayer(tmp, strlen(tmp));
3008 next_out = i+1; // [HGM] suppress printing in ICS window
3010 started = STARTED_NONE;
3012 /* Don't match patterns against characters in comment */
3017 if (started == STARTED_CHATTER) {
3018 if (buf[i] != '\n') {
3019 /* Don't match patterns against characters in chatter */
3023 started = STARTED_NONE;
3024 if(suppressKibitz) next_out = i+1;
3027 /* Kludge to deal with rcmd protocol */
3028 if (firstTime && looking_at(buf, &i, "\001*")) {
3029 DisplayFatalError(&buf[1], 0, 1);
3035 if (!loggedOn && looking_at(buf, &i, "chessclub.com")) {
3038 if (appData.debugMode)
3039 fprintf(debugFP, "ics_type %d\n", ics_type);
3042 if (!loggedOn && looking_at(buf, &i, "freechess.org")) {
3043 ics_type = ICS_FICS;
3045 if (appData.debugMode)
3046 fprintf(debugFP, "ics_type %d\n", ics_type);
3049 if (!loggedOn && looking_at(buf, &i, "chess.net")) {
3050 ics_type = ICS_CHESSNET;
3052 if (appData.debugMode)
3053 fprintf(debugFP, "ics_type %d\n", ics_type);
3058 (looking_at(buf, &i, "\"*\" is *a registered name") ||
3059 looking_at(buf, &i, "Logging you in as \"*\"") ||
3060 looking_at(buf, &i, "will be \"*\""))) {
3061 safeStrCpy(ics_handle, star_match[0], sizeof(ics_handle)/sizeof(ics_handle[0]));
3065 if (loggedOn && !have_set_title && ics_handle[0] != NULLCHAR) {
3067 snprintf(buf, sizeof(buf), "%s@%s", ics_handle, appData.icsHost);
3068 DisplayIcsInteractionTitle(buf);
3069 have_set_title = TRUE;
3072 /* skip finger notes */
3073 if (started == STARTED_NONE &&
3074 ((buf[i] == ' ' && isdigit(buf[i+1])) ||
3075 (buf[i] == '1' && buf[i+1] == '0')) &&
3076 buf[i+2] == ':' && buf[i+3] == ' ') {
3077 started = STARTED_CHATTER;
3083 // [HGM] seekgraph: recognize sought lines and end-of-sought message
3084 if(appData.seekGraph) {
3085 if(soughtPending && MatchSoughtLine(buf+i)) {
3086 i = strstr(buf+i, "rated") - buf;
3087 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3088 next_out = leftover_start = i;
3089 started = STARTED_CHATTER;
3090 suppressKibitz = TRUE;
3093 if((gameMode == IcsIdle || gameMode == BeginningOfGame)
3094 && looking_at(buf, &i, "* ads displayed")) {
3095 soughtPending = FALSE;
3100 if(appData.autoRefresh) {
3101 if(looking_at(buf, &i, "* (*) seeking * * * * *\"play *\" to respond)\n")) {
3102 int s = (ics_type == ICS_ICC); // ICC format differs
3104 AddAd(star_match[0], star_match[1], atoi(star_match[2+s]), atoi(star_match[3+s]),
3105 star_match[4+s][0], star_match[5-3*s], atoi(star_match[7]), TRUE);
3106 looking_at(buf, &i, "*% "); // eat prompt
3107 if(oldi > 0 && buf[oldi-1] == '\n') oldi--; // suppress preceding LF, if any
3108 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3109 next_out = i; // suppress
3112 if(looking_at(buf, &i, "\nAds removed: *\n") || looking_at(buf, &i, "\031(51 * *\031)")) {
3113 char *p = star_match[0];
3115 if(seekGraphUp) RemoveSeekAd(atoi(p));
3116 while(*p && *p++ != ' '); // next
3118 looking_at(buf, &i, "*% "); // eat prompt
3119 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3126 /* skip formula vars */
3127 if (started == STARTED_NONE &&
3128 buf[i] == 'f' && isdigit(buf[i+1]) && buf[i+2] == ':') {
3129 started = STARTED_CHATTER;
3134 // [HGM] kibitz: try to recognize opponent engine-score kibitzes, to divert them to engine-output window
3135 if (appData.autoKibitz && started == STARTED_NONE &&
3136 !appData.icsEngineAnalyze && // [HGM] [DM] ICS analyze
3137 (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack || gameMode == IcsObserving)) {
3138 if((looking_at(buf, &i, "\n* kibitzes: ") || looking_at(buf, &i, "\n* whispers: ") ||
3139 looking_at(buf, &i, "* kibitzes: ") || looking_at(buf, &i, "* whispers: ")) &&
3140 (StrStr(star_match[0], gameInfo.white) == star_match[0] ||
3141 StrStr(star_match[0], gameInfo.black) == star_match[0] )) { // kibitz of self or opponent
3142 suppressKibitz = TRUE;
3143 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3145 if((StrStr(star_match[0], gameInfo.white) == star_match[0]
3146 && (gameMode == IcsPlayingWhite)) ||
3147 (StrStr(star_match[0], gameInfo.black) == star_match[0]
3148 && (gameMode == IcsPlayingBlack)) ) // opponent kibitz
3149 started = STARTED_CHATTER; // own kibitz we simply discard
3151 started = STARTED_COMMENT; // make sure it will be collected in parse[]
3152 parse_pos = 0; parse[0] = NULLCHAR;
3153 savingComment = TRUE;
3154 suppressKibitz = gameMode != IcsObserving ? 2 :
3155 (StrStr(star_match[0], gameInfo.white) == NULL) + 1;
3159 if((looking_at(buf, &i, "\nkibitzed to *\n") || looking_at(buf, &i, "kibitzed to *\n") ||
3160 looking_at(buf, &i, "\n(kibitzed to *\n") || looking_at(buf, &i, "(kibitzed to *\n"))
3161 && atoi(star_match[0])) {
3162 // suppress the acknowledgements of our own autoKibitz
3164 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3165 if(p = strchr(star_match[0], ' ')) p[1] = NULLCHAR; // clip off "players)" on FICS
3166 SendToPlayer(star_match[0], strlen(star_match[0]));
3167 if(looking_at(buf, &i, "*% ")) // eat prompt
3168 suppressKibitz = FALSE;
3172 } // [HGM] kibitz: end of patch
3174 if(looking_at(buf, &i, "* rating adjustment: * --> *\n")) continue;
3176 // [HGM] chat: intercept tells by users for which we have an open chat window
3178 if(started == STARTED_NONE && (looking_at(buf, &i, "* tells you:") || looking_at(buf, &i, "* says:") ||
3179 looking_at(buf, &i, "* whispers:") ||
3180 looking_at(buf, &i, "* kibitzes:") ||
3181 looking_at(buf, &i, "* shouts:") ||
3182 looking_at(buf, &i, "* c-shouts:") ||
3183 looking_at(buf, &i, "--> * ") ||
3184 looking_at(buf, &i, "*(*):") && (sscanf(star_match[1], "%d", &channel),1) ||
3185 looking_at(buf, &i, "*(*)(*):") && (sscanf(star_match[2], "%d", &channel),1) ||
3186 looking_at(buf, &i, "*(*)(*)(*):") && (sscanf(star_match[3], "%d", &channel),1) ||
3187 looking_at(buf, &i, "*(*)(*)(*)(*):") && sscanf(star_match[4], "%d", &channel) == 1 )) {
3189 sscanf(star_match[0], "%[^(]", talker+1); // strip (C) or (U) off ICS handle
3190 chattingPartner = -1;
3192 if(channel >= 0) // channel broadcast; look if there is a chatbox for this channel
3193 for(p=0; p<MAX_CHAT; p++) {
3194 if(chatPartner[p][0] >= '0' && chatPartner[p][0] <= '9' && channel == atoi(chatPartner[p])) {
3195 talker[0] = '['; strcat(talker, "] ");
3196 Colorize(channel == 1 ? ColorChannel1 : ColorChannel, FALSE);
3197 chattingPartner = p; break;
3200 if(buf[i-3] == 'e') // kibitz; look if there is a KIBITZ chatbox
3201 for(p=0; p<MAX_CHAT; p++) {
3202 if(!strcmp("kibitzes", chatPartner[p])) {
3203 talker[0] = '['; strcat(talker, "] ");
3204 chattingPartner = p; break;
3207 if(buf[i-3] == 'r') // whisper; look if there is a WHISPER chatbox
3208 for(p=0; p<MAX_CHAT; p++) {
3209 if(!strcmp("whispers", chatPartner[p])) {
3210 talker[0] = '['; strcat(talker, "] ");
3211 chattingPartner = p; break;
3214 if(buf[i-3] == 't' || buf[oldi+2] == '>') {// shout, c-shout or it; look if there is a 'shouts' chatbox
3215 if(buf[i-8] == '-' && buf[i-3] == 't')
3216 for(p=0; p<MAX_CHAT; p++) { // c-shout; check if dedicatesd c-shout box exists
3217 if(!strcmp("c-shouts", chatPartner[p])) {
3218 talker[0] = '('; strcat(talker, ") "); Colorize(ColorSShout, FALSE);
3219 chattingPartner = p; break;
3222 if(chattingPartner < 0)
3223 for(p=0; p<MAX_CHAT; p++) {
3224 if(!strcmp("shouts", chatPartner[p])) {
3225 if(buf[oldi+2] == '>') { talker[0] = '<'; strcat(talker, "> "); Colorize(ColorShout, FALSE); }
3226 else if(buf[i-8] == '-') { talker[0] = '('; strcat(talker, ") "); Colorize(ColorSShout, FALSE); }
3227 else { talker[0] = '['; strcat(talker, "] "); Colorize(ColorShout, FALSE); }
3228 chattingPartner = p; break;
3232 if(chattingPartner<0) // if not, look if there is a chatbox for this indivdual
3233 for(p=0; p<MAX_CHAT; p++) if(!StrCaseCmp(talker+1, chatPartner[p])) {
3234 talker[0] = 0; Colorize(ColorTell, FALSE);
3235 chattingPartner = p; break;
3237 if(chattingPartner<0) i = oldi; else {
3238 Colorize(curColor, TRUE); // undo the bogus colorations we just made to trigger the souds
3239 if(oldi > 0 && buf[oldi-1] == '\n') oldi--;
3240 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
3241 started = STARTED_COMMENT;
3242 parse_pos = 0; parse[0] = NULLCHAR;
3243 savingComment = 3 + chattingPartner; // counts as TRUE
3244 suppressKibitz = TRUE;
3247 } // [HGM] chat: end of patch
3250 if (appData.zippyTalk || appData.zippyPlay) {
3251 /* [DM] Backup address for color zippy lines */
3253 if (loggedOn == TRUE)
3254 if (ZippyControl(buf, &backup) || ZippyConverse(buf, &backup) ||
3255 (appData.zippyPlay && ZippyMatch(buf, &backup)));
3257 } // [DM] 'else { ' deleted
3259 /* Regular tells and says */
3260 (tkind = 1, looking_at(buf, &i, "* tells you: ")) ||
3261 looking_at(buf, &i, "* (your partner) tells you: ") ||
3262 looking_at(buf, &i, "* says: ") ||
3263 /* Don't color "message" or "messages" output */
3264 (tkind = 5, looking_at(buf, &i, "*. * (*:*): ")) ||
3265 looking_at(buf, &i, "*. * at *:*: ") ||
3266 looking_at(buf, &i, "--* (*:*): ") ||
3267 /* Message notifications (same color as tells) */
3268 looking_at(buf, &i, "* has left a message ") ||
3269 looking_at(buf, &i, "* just sent you a message:\n") ||
3270 /* Whispers and kibitzes */
3271 (tkind = 2, looking_at(buf, &i, "* whispers: ")) ||
3272 looking_at(buf, &i, "* kibitzes: ") ||
3274 (tkind = 3, looking_at(buf, &i, "*(*: "))) {
3276 if (tkind == 1 && strchr(star_match[0], ':')) {
3277 /* Avoid "tells you:" spoofs in channels */
3280 if (star_match[0][0] == NULLCHAR ||
3281 strchr(star_match[0], ' ') ||
3282 (tkind == 3 && strchr(star_match[1], ' '))) {
3283 /* Reject bogus matches */
3286 if (appData.colorize) {
3287 if (oldi > next_out) {
3288 SendToPlayer(&buf[next_out], oldi - next_out);
3293 Colorize(ColorTell, FALSE);
3294 curColor = ColorTell;
3297 Colorize(ColorKibitz, FALSE);
3298 curColor = ColorKibitz;
3301 p = strrchr(star_match[1], '(');
3308 Colorize(ColorChannel1, FALSE);
3309 curColor = ColorChannel1;
3311 Colorize(ColorChannel, FALSE);
3312 curColor = ColorChannel;
3316 curColor = ColorNormal;
3320 if (started == STARTED_NONE && appData.autoComment &&
3321 (gameMode == IcsObserving ||
3322 gameMode == IcsPlayingWhite ||
3323 gameMode == IcsPlayingBlack)) {
3324 parse_pos = i - oldi;
3325 memcpy(parse, &buf[oldi], parse_pos);
3326 parse[parse_pos] = NULLCHAR;
3327 started = STARTED_COMMENT;
3328 savingComment = TRUE;
3330 started = STARTED_CHATTER;
3331 savingComment = FALSE;
3338 if (looking_at(buf, &i, "* s-shouts: ") ||
3339 looking_at(buf, &i, "* c-shouts: ")) {
3340 if (appData.colorize) {
3341 if (oldi > next_out) {
3342 SendToPlayer(&buf[next_out], oldi - next_out);
3345 Colorize(ColorSShout, FALSE);
3346 curColor = ColorSShout;
3349 started = STARTED_CHATTER;
3353 if (looking_at(buf, &i, "--->")) {
3358 if (looking_at(buf, &i, "* shouts: ") ||
3359 looking_at(buf, &i, "--> ")) {
3360 if (appData.colorize) {
3361 if (oldi > next_out) {
3362 SendToPlayer(&buf[next_out], oldi - next_out);
3365 Colorize(ColorShout, FALSE);
3366 curColor = ColorShout;
3369 started = STARTED_CHATTER;
3373 if (looking_at( buf, &i, "Challenge:")) {
3374 if (appData.colorize) {
3375 if (oldi > next_out) {
3376 SendToPlayer(&buf[next_out], oldi - next_out);
3379 Colorize(ColorChallenge, FALSE);
3380 curColor = ColorChallenge;
3386 if (looking_at(buf, &i, "* offers you") ||
3387 looking_at(buf, &i, "* offers to be") ||
3388 looking_at(buf, &i, "* would like to") ||
3389 looking_at(buf, &i, "* requests to") ||
3390 looking_at(buf, &i, "Your opponent offers") ||
3391 looking_at(buf, &i, "Your opponent requests")) {
3393 if (appData.colorize) {
3394 if (oldi > next_out) {
3395 SendToPlayer(&buf[next_out], oldi - next_out);
3398 Colorize(ColorRequest, FALSE);
3399 curColor = ColorRequest;
3404 if (looking_at(buf, &i, "* (*) seeking")) {
3405 if (appData.colorize) {
3406 if (oldi > next_out) {
3407 SendToPlayer(&buf[next_out], oldi - next_out);
3410 Colorize(ColorSeek, FALSE);
3411 curColor = ColorSeek;
3416 if(i < backup) { i = backup; continue; } // [HGM] for if ZippyControl matches, but the colorie code doesn't
3418 if (looking_at(buf, &i, "\\ ")) {
3419 if (prevColor != ColorNormal) {
3420 if (oldi > next_out) {
3421 SendToPlayer(&buf[next_out], oldi - next_out);
3424 Colorize(prevColor, TRUE);
3425 curColor = prevColor;
3427 if (savingComment) {
3428 parse_pos = i - oldi;
3429 memcpy(parse, &buf[oldi], parse_pos);
3430 parse[parse_pos] = NULLCHAR;
3431 started = STARTED_COMMENT;
3432 if(savingComment >= 3) // [HGM] chat: continuation of line for chat box
3433 chattingPartner = savingComment - 3; // kludge to remember the box
3435 started = STARTED_CHATTER;
3440 if (looking_at(buf, &i, "Black Strength :") ||
3441 looking_at(buf, &i, "<<< style 10 board >>>") ||
3442 looking_at(buf, &i, "<10>") ||
3443 looking_at(buf, &i, "#@#")) {
3444 /* Wrong board style */
3446 SendToICS(ics_prefix);
3447 SendToICS("set style 12\n");
3448 SendToICS(ics_prefix);
3449 SendToICS("refresh\n");
3453 if (looking_at(buf, &i, "login:")) {
3454 if (!have_sent_ICS_logon) {
3456 have_sent_ICS_logon = 1;
3457 else // no init script was found
3458 have_sent_ICS_logon = (appData.autoCreateLogon ? 2 : 1); // flag that we should capture username + password
3459 } else { // we have sent (or created) the InitScript, but apparently the ICS rejected it
3460 have_sent_ICS_logon = (appData.autoCreateLogon ? 2 : 1); // request creation of a new script
3465 if (ics_getting_history != H_GETTING_MOVES /*smpos kludge*/ &&
3466 (looking_at(buf, &i, "\n<12> ") ||
3467 looking_at(buf, &i, "<12> "))) {
3469 if (oldi > next_out) {
3470 SendToPlayer(&buf[next_out], oldi - next_out);
3473 started = STARTED_BOARD;
3478 if ((started == STARTED_NONE && looking_at(buf, &i, "\n<b1> ")) ||
3479 looking_at(buf, &i, "<b1> ")) {
3480 if (oldi > next_out) {
3481 SendToPlayer(&buf[next_out], oldi - next_out);
3484 started = STARTED_HOLDINGS;
3489 if (looking_at(buf, &i, "* *vs. * *--- *")) {
3491 /* Header for a move list -- first line */
3493 switch (ics_getting_history) {
3497 case BeginningOfGame:
3498 /* User typed "moves" or "oldmoves" while we
3499 were idle. Pretend we asked for these
3500 moves and soak them up so user can step
3501 through them and/or save them.
3504 gameMode = IcsObserving;
3507 ics_getting_history = H_GOT_UNREQ_HEADER;
3509 case EditGame: /*?*/
3510 case EditPosition: /*?*/
3511 /* Should above feature work in these modes too? */
3512 /* For now it doesn't */
3513 ics_getting_history = H_GOT_UNWANTED_HEADER;
3516 ics_getting_history = H_GOT_UNWANTED_HEADER;
3521 /* Is this the right one? */
3522 if (gameInfo.white && gameInfo.black &&
3523 strcmp(gameInfo.white, star_match[0]) == 0 &&
3524 strcmp(gameInfo.black, star_match[2]) == 0) {
3526 ics_getting_history = H_GOT_REQ_HEADER;
3529 case H_GOT_REQ_HEADER:
3530 case H_GOT_UNREQ_HEADER:
3531 case H_GOT_UNWANTED_HEADER:
3532 case H_GETTING_MOVES:
3533 /* Should not happen */
3534 DisplayError(_("Error gathering move list: two headers"), 0);
3535 ics_getting_history = H_FALSE;
3539 /* Save player ratings into gameInfo if needed */
3540 if ((ics_getting_history == H_GOT_REQ_HEADER ||
3541 ics_getting_history == H_GOT_UNREQ_HEADER) &&
3542 (gameInfo.whiteRating == -1 ||
3543 gameInfo.blackRating == -1)) {
3545 gameInfo.whiteRating = string_to_rating(star_match[1]);
3546 gameInfo.blackRating = string_to_rating(star_match[3]);
3547 if (appData.debugMode)
3548 fprintf(debugFP, _("Ratings from header: W %d, B %d\n"),
3549 gameInfo.whiteRating, gameInfo.blackRating);
3554 if (looking_at(buf, &i,
3555 "* * match, initial time: * minute*, increment: * second")) {
3556 /* Header for a move list -- second line */
3557 /* Initial board will follow if this is a wild game */
3558 if (gameInfo.event != NULL) free(gameInfo.event);
3559 snprintf(str, MSG_SIZ, "ICS %s %s match", star_match[0], star_match[1]);
3560 gameInfo.event = StrSave(str);
3561 /* [HGM] we switched variant. Translate boards if needed. */
3562 VariantSwitch(boards[currentMove], StringToVariant(gameInfo.event));
3566 if (looking_at(buf, &i, "Move ")) {
3567 /* Beginning of a move list */
3568 switch (ics_getting_history) {
3570 /* Normally should not happen */
3571 /* Maybe user hit reset while we were parsing */
3574 /* Happens if we are ignoring a move list that is not
3575 * the one we just requested. Common if the user
3576 * tries to observe two games without turning off
3579 case H_GETTING_MOVES:
3580 /* Should not happen */
3581 DisplayError(_("Error gathering move list: nested"), 0);
3582 ics_getting_history = H_FALSE;
3584 case H_GOT_REQ_HEADER:
3585 ics_getting_history = H_GETTING_MOVES;
3586 started = STARTED_MOVES;
3588 if (oldi > next_out) {
3589 SendToPlayer(&buf[next_out], oldi - next_out);
3592 case H_GOT_UNREQ_HEADER:
3593 ics_getting_history = H_GETTING_MOVES;
3594 started = STARTED_MOVES_NOHIDE;
3597 case H_GOT_UNWANTED_HEADER:
3598 ics_getting_history = H_FALSE;
3604 if (looking_at(buf, &i, "% ") ||
3605 ((started == STARTED_MOVES || started == STARTED_MOVES_NOHIDE)
3606 && looking_at(buf, &i, "}*"))) { char *bookHit = NULL; // [HGM] book
3607 if(soughtPending && nrOfSeekAds) { // [HGM] seekgraph: on ICC sought-list has no termination line
3608 soughtPending = FALSE;
3612 if(suppressKibitz) next_out = i;
3613 savingComment = FALSE;
3617 case STARTED_MOVES_NOHIDE:
3618 memcpy(&parse[parse_pos], &buf[oldi], i - oldi);
3619 parse[parse_pos + i - oldi] = NULLCHAR;
3620 ParseGameHistory(parse);
3622 if (appData.zippyPlay && first.initDone) {
3623 FeedMovesToProgram(&first, forwardMostMove);
3624 if (gameMode == IcsPlayingWhite) {
3625 if (WhiteOnMove(forwardMostMove)) {
3626 if (first.sendTime) {
3627 if (first.useColors) {
3628 SendToProgram("black\n", &first);
3630 SendTimeRemaining(&first, TRUE);
3632 if (first.useColors) {
3633 SendToProgram("white\n", &first); // [HGM] book: made sending of "go\n" book dependent
3635 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: probe book for initial pos
3636 first.maybeThinking = TRUE;
3638 if (first.usePlayother) {
3639 if (first.sendTime) {
3640 SendTimeRemaining(&first, TRUE);
3642 SendToProgram("playother\n", &first);
3648 } else if (gameMode == IcsPlayingBlack) {
3649 if (!WhiteOnMove(forwardMostMove)) {
3650 if (first.sendTime) {
3651 if (first.useColors) {
3652 SendToProgram("white\n", &first);
3654 SendTimeRemaining(&first, FALSE);
3656 if (first.useColors) {
3657 SendToProgram("black\n", &first);
3659 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE);
3660 first.maybeThinking = TRUE;
3662 if (first.usePlayother) {
3663 if (first.sendTime) {
3664 SendTimeRemaining(&first, FALSE);
3666 SendToProgram("playother\n", &first);
3675 if (gameMode == IcsObserving && ics_gamenum == -1) {
3676 /* Moves came from oldmoves or moves command
3677 while we weren't doing anything else.
3679 currentMove = forwardMostMove;
3680 ClearHighlights();/*!!could figure this out*/
3681 flipView = appData.flipView;
3682 DrawPosition(TRUE, boards[currentMove]);
3683 DisplayBothClocks();
3684 snprintf(str, MSG_SIZ, "%s %s %s",
3685 gameInfo.white, _("vs."), gameInfo.black);
3689 /* Moves were history of an active game */
3690 if (gameInfo.resultDetails != NULL) {
3691 free(gameInfo.resultDetails);
3692 gameInfo.resultDetails = NULL;
3695 HistorySet(parseList, backwardMostMove,
3696 forwardMostMove, currentMove-1);
3697 DisplayMove(currentMove - 1);
3698 if (started == STARTED_MOVES) next_out = i;
3699 started = STARTED_NONE;
3700 ics_getting_history = H_FALSE;
3703 case STARTED_OBSERVE:
3704 started = STARTED_NONE;
3705 SendToICS(ics_prefix);
3706 SendToICS("refresh\n");
3712 if(bookHit) { // [HGM] book: simulate book reply
3713 static char bookMove[MSG_SIZ]; // a bit generous?
3715 programStats.nodes = programStats.depth = programStats.time =
3716 programStats.score = programStats.got_only_move = 0;
3717 sprintf(programStats.movelist, "%s (xbook)", bookHit);
3719 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
3720 strcat(bookMove, bookHit);
3721 HandleMachineMove(bookMove, &first);
3726 if ((started == STARTED_MOVES || started == STARTED_BOARD ||
3727 started == STARTED_HOLDINGS ||
3728 started == STARTED_MOVES_NOHIDE) && i >= leftover_len) {
3729 /* Accumulate characters in move list or board */
3730 parse[parse_pos++] = buf[i];
3733 /* Start of game messages. Mostly we detect start of game
3734 when the first board image arrives. On some versions
3735 of the ICS, though, we need to do a "refresh" after starting
3736 to observe in order to get the current board right away. */
3737 if (looking_at(buf, &i, "Adding game * to observation list")) {
3738 started = STARTED_OBSERVE;
3742 /* Handle auto-observe */
3743 if (appData.autoObserve &&
3744 (gameMode == IcsIdle || gameMode == BeginningOfGame) &&
3745 looking_at(buf, &i, "Game notification: * (*) vs. * (*)")) {
3747 /* Choose the player that was highlighted, if any. */
3748 if (star_match[0][0] == '\033' ||
3749 star_match[1][0] != '\033') {
3750 player = star_match[0];
3752 player = star_match[2];
3754 snprintf(str, MSG_SIZ, "%sobserve %s\n",
3755 ics_prefix, StripHighlightAndTitle(player));
3758 /* Save ratings from notify string */
3759 safeStrCpy(player1Name, star_match[0], sizeof(player1Name)/sizeof(player1Name[0]));
3760 player1Rating = string_to_rating(star_match[1]);
3761 safeStrCpy(player2Name, star_match[2], sizeof(player2Name)/sizeof(player2Name[0]));
3762 player2Rating = string_to_rating(star_match[3]);
3764 if (appData.debugMode)
3766 "Ratings from 'Game notification:' %s %d, %s %d\n",
3767 player1Name, player1Rating,
3768 player2Name, player2Rating);
3773 /* Deal with automatic examine mode after a game,
3774 and with IcsObserving -> IcsExamining transition */
3775 if (looking_at(buf, &i, "Entering examine mode for game *") ||
3776 looking_at(buf, &i, "has made you an examiner of game *")) {
3778 int gamenum = atoi(star_match[0]);
3779 if ((gameMode == IcsIdle || gameMode == IcsObserving) &&
3780 gamenum == ics_gamenum) {
3781 /* We were already playing or observing this game;
3782 no need to refetch history */
3783 gameMode = IcsExamining;
3785 pauseExamForwardMostMove = forwardMostMove;
3786 } else if (currentMove < forwardMostMove) {
3787 ForwardInner(forwardMostMove);
3790 /* I don't think this case really can happen */
3791 SendToICS(ics_prefix);
3792 SendToICS("refresh\n");
3797 /* Error messages */
3798 // if (ics_user_moved) {
3799 if (1) { // [HGM] old way ignored error after move type in; ics_user_moved is not set then!
3800 if (looking_at(buf, &i, "Illegal move") ||
3801 looking_at(buf, &i, "Not a legal move") ||
3802 looking_at(buf, &i, "Your king is in check") ||
3803 looking_at(buf, &i, "It isn't your turn") ||
3804 looking_at(buf, &i, "It is not your move")) {
3806 if (ics_user_moved && forwardMostMove > backwardMostMove) { // only backup if we already moved
3807 currentMove = forwardMostMove-1;
3808 DisplayMove(currentMove - 1); /* before DMError */
3809 DrawPosition(FALSE, boards[currentMove]);
3810 SwitchClocks(forwardMostMove-1); // [HGM] race
3811 DisplayBothClocks();
3813 DisplayMoveError(_("Illegal move (rejected by ICS)")); // [HGM] but always relay error msg
3819 if (looking_at(buf, &i, "still have time") ||
3820 looking_at(buf, &i, "not out of time") ||
3821 looking_at(buf, &i, "either player is out of time") ||
3822 looking_at(buf, &i, "has timeseal; checking")) {
3823 /* We must have called his flag a little too soon */
3824 whiteFlag = blackFlag = FALSE;
3828 if (looking_at(buf, &i, "added * seconds to") ||
3829 looking_at(buf, &i, "seconds were added to")) {
3830 /* Update the clocks */
3831 SendToICS(ics_prefix);
3832 SendToICS("refresh\n");
3836 if (!ics_clock_paused && looking_at(buf, &i, "clock paused")) {
3837 ics_clock_paused = TRUE;
3842 if (ics_clock_paused && looking_at(buf, &i, "clock resumed")) {
3843 ics_clock_paused = FALSE;
3848 /* Grab player ratings from the Creating: message.
3849 Note we have to check for the special case when
3850 the ICS inserts things like [white] or [black]. */
3851 if (looking_at(buf, &i, "Creating: * (*)* * (*)") ||
3852 looking_at(buf, &i, "Creating: * (*) [*] * (*)")) {
3854 0 player 1 name (not necessarily white)
3856 2 empty, white, or black (IGNORED)
3857 3 player 2 name (not necessarily black)
3860 The names/ratings are sorted out when the game
3861 actually starts (below).
3863 safeStrCpy(player1Name, StripHighlightAndTitle(star_match[0]), sizeof(player1Name)/sizeof(player1Name[0]));
3864 player1Rating = string_to_rating(star_match[1]);
3865 safeStrCpy(player2Name, StripHighlightAndTitle(star_match[3]), sizeof(player2Name)/sizeof(player2Name[0]));
3866 player2Rating = string_to_rating(star_match[4]);
3868 if (appData.debugMode)
3870 "Ratings from 'Creating:' %s %d, %s %d\n",
3871 player1Name, player1Rating,
3872 player2Name, player2Rating);
3877 /* Improved generic start/end-of-game messages */
3878 if ((tkind=0, looking_at(buf, &i, "{Game * (* vs. *) *}*")) ||
3879 (tkind=1, looking_at(buf, &i, "{Game * (*(*) vs. *(*)) *}*"))){
3880 /* If tkind == 0: */
3881 /* star_match[0] is the game number */
3882 /* [1] is the white player's name */
3883 /* [2] is the black player's name */
3884 /* For end-of-game: */
3885 /* [3] is the reason for the game end */
3886 /* [4] is a PGN end game-token, preceded by " " */
3887 /* For start-of-game: */
3888 /* [3] begins with "Creating" or "Continuing" */
3889 /* [4] is " *" or empty (don't care). */
3890 int gamenum = atoi(star_match[0]);
3891 char *whitename, *blackname, *why, *endtoken;
3892 ChessMove endtype = EndOfFile;
3895 whitename = star_match[1];
3896 blackname = star_match[2];
3897 why = star_match[3];
3898 endtoken = star_match[4];
3900 whitename = star_match[1];
3901 blackname = star_match[3];
3902 why = star_match[5];
3903 endtoken = star_match[6];
3906 /* Game start messages */
3907 if (strncmp(why, "Creating ", 9) == 0 ||
3908 strncmp(why, "Continuing ", 11) == 0) {
3909 gs_gamenum = gamenum;
3910 safeStrCpy(gs_kind, strchr(why, ' ') + 1,sizeof(gs_kind)/sizeof(gs_kind[0]));
3911 if(ics_gamenum == -1) // [HGM] only if we are not already involved in a game (because gin=1 sends us such messages)
3912 VariantSwitch(boards[currentMove], StringToVariant(gs_kind)); // [HGM] variantswitch: even before we get first board
3914 if (appData.zippyPlay) {
3915 ZippyGameStart(whitename, blackname);
3918 partnerBoardValid = FALSE; // [HGM] bughouse
3922 /* Game end messages */
3923 if (gameMode == IcsIdle || gameMode == BeginningOfGame ||
3924 ics_gamenum != gamenum) {
3927 while (endtoken[0] == ' ') endtoken++;
3928 switch (endtoken[0]) {
3931 endtype = GameUnfinished;
3934 endtype = BlackWins;
3937 if (endtoken[1] == '/')
3938 endtype = GameIsDrawn;
3940 endtype = WhiteWins;
3943 GameEnds(endtype, why, GE_ICS);
3945 if (appData.zippyPlay && first.initDone) {
3946 ZippyGameEnd(endtype, why);
3947 if (first.pr == NoProc) {
3948 /* Start the next process early so that we'll
3949 be ready for the next challenge */
3950 StartChessProgram(&first);
3952 /* Send "new" early, in case this command takes
3953 a long time to finish, so that we'll be ready
3954 for the next challenge. */
3955 gameInfo.variant = VariantNormal; // [HGM] variantswitch: suppress sending of 'variant'
3959 if(appData.bgObserve && partnerBoardValid) DrawPosition(TRUE, partnerBoard);
3963 if (looking_at(buf, &i, "Removing game * from observation") ||
3964 looking_at(buf, &i, "no longer observing game *") ||
3965 looking_at(buf, &i, "Game * (*) has no examiners")) {
3966 if (gameMode == IcsObserving &&
3967 atoi(star_match[0]) == ics_gamenum)
3969 /* icsEngineAnalyze */
3970 if (appData.icsEngineAnalyze) {
3977 ics_user_moved = FALSE;
3982 if (looking_at(buf, &i, "no longer examining game *")) {
3983 if (gameMode == IcsExamining &&
3984 atoi(star_match[0]) == ics_gamenum)
3988 ics_user_moved = FALSE;
3993 /* Advance leftover_start past any newlines we find,
3994 so only partial lines can get reparsed */
3995 if (looking_at(buf, &i, "\n")) {
3996 prevColor = curColor;
3997 if (curColor != ColorNormal) {
3998 if (oldi > next_out) {
3999 SendToPlayer(&buf[next_out], oldi - next_out);
4002 Colorize(ColorNormal, FALSE);
4003 curColor = ColorNormal;
4005 if (started == STARTED_BOARD) {
4006 started = STARTED_NONE;
4007 parse[parse_pos] = NULLCHAR;
4008 ParseBoard12(parse);
4011 /* Send premove here */
4012 if (appData.premove) {
4014 if (currentMove == 0 &&
4015 gameMode == IcsPlayingWhite &&
4016 appData.premoveWhite) {
4017 snprintf(str, MSG_SIZ, "%s\n", appData.premoveWhiteText);
4018 if (appData.debugMode)
4019 fprintf(debugFP, "Sending premove:\n");
4021 } else if (currentMove == 1 &&
4022 gameMode == IcsPlayingBlack &&
4023 appData.premoveBlack) {
4024 snprintf(str, MSG_SIZ, "%s\n", appData.premoveBlackText);
4025 if (appData.debugMode)
4026 fprintf(debugFP, "Sending premove:\n");
4028 } else if (gotPremove) {
4030 ClearPremoveHighlights();
4031 if (appData.debugMode)
4032 fprintf(debugFP, "Sending premove:\n");
4033 UserMoveEvent(premoveFromX, premoveFromY,
4034 premoveToX, premoveToY,
4039 /* Usually suppress following prompt */
4040 if (!(forwardMostMove == 0 && gameMode == IcsExamining)) {
4041 while(looking_at(buf, &i, "\n")); // [HGM] skip empty lines
4042 if (looking_at(buf, &i, "*% ")) {
4043 savingComment = FALSE;
4048 } else if (started == STARTED_HOLDINGS) {
4050 char new_piece[MSG_SIZ];
4051 started = STARTED_NONE;
4052 parse[parse_pos] = NULLCHAR;
4053 if (appData.debugMode)
4054 fprintf(debugFP, "Parsing holdings: %s, currentMove = %d\n",
4055 parse, currentMove);
4056 if (sscanf(parse, " game %d", &gamenum) == 1) {
4057 if(gamenum == ics_gamenum) { // [HGM] bughouse: old code if part of foreground game
4058 if (gameInfo.variant == VariantNormal) {
4059 /* [HGM] We seem to switch variant during a game!
4060 * Presumably no holdings were displayed, so we have
4061 * to move the position two files to the right to
4062 * create room for them!
4064 VariantClass newVariant;
4065 switch(gameInfo.boardWidth) { // base guess on board width
4066 case 9: newVariant = VariantShogi; break;
4067 case 10: newVariant = VariantGreat; break;
4068 default: newVariant = VariantCrazyhouse; break;
4070 VariantSwitch(boards[currentMove], newVariant); /* temp guess */
4071 /* Get a move list just to see the header, which
4072 will tell us whether this is really bug or zh */
4073 if (ics_getting_history == H_FALSE) {
4074 ics_getting_history = H_REQUESTED;
4075 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4079 new_piece[0] = NULLCHAR;
4080 sscanf(parse, "game %d white [%s black [%s <- %s",
4081 &gamenum, white_holding, black_holding,
4083 white_holding[strlen(white_holding)-1] = NULLCHAR;
4084 black_holding[strlen(black_holding)-1] = NULLCHAR;
4085 /* [HGM] copy holdings to board holdings area */
4086 CopyHoldings(boards[forwardMostMove], white_holding, WhitePawn);
4087 CopyHoldings(boards[forwardMostMove], black_holding, BlackPawn);
4088 boards[forwardMostMove][HOLDINGS_SET] = 1; // flag holdings as set
4090 if (appData.zippyPlay && first.initDone) {
4091 ZippyHoldings(white_holding, black_holding,
4095 if (tinyLayout || smallLayout) {
4096 char wh[16], bh[16];
4097 PackHolding(wh, white_holding);
4098 PackHolding(bh, black_holding);
4099 snprintf(str, MSG_SIZ, "[%s-%s] %s-%s", wh, bh,
4100 gameInfo.white, gameInfo.black);
4102 snprintf(str, MSG_SIZ, "%s [%s] %s %s [%s]",
4103 gameInfo.white, white_holding, _("vs."),
4104 gameInfo.black, black_holding);
4106 if(!partnerUp) // [HGM] bughouse: when peeking at partner game we already know what he captured...
4107 DrawPosition(FALSE, boards[currentMove]);
4109 } else if(appData.bgObserve) { // [HGM] bughouse: holdings of other game => background
4110 sscanf(parse, "game %d white [%s black [%s <- %s",
4111 &gamenum, white_holding, black_holding,
4113 white_holding[strlen(white_holding)-1] = NULLCHAR;
4114 black_holding[strlen(black_holding)-1] = NULLCHAR;
4115 /* [HGM] copy holdings to partner-board holdings area */
4116 CopyHoldings(partnerBoard, white_holding, WhitePawn);
4117 CopyHoldings(partnerBoard, black_holding, BlackPawn);
4118 if(twoBoards) { partnerUp = 1; flipView = !flipView; } // [HGM] dual: always draw
4119 if(partnerUp) DrawPosition(FALSE, partnerBoard);
4120 if(twoBoards) { partnerUp = 0; flipView = !flipView; }
4123 /* Suppress following prompt */
4124 if (looking_at(buf, &i, "*% ")) {
4125 if(strchr(star_match[0], 7)) SendToPlayer("\007", 1); // Bell(); // FICS fuses bell for next board with prompt in zh captures
4126 savingComment = FALSE;
4134 i++; /* skip unparsed character and loop back */
4137 if (started != STARTED_MOVES && started != STARTED_BOARD && !suppressKibitz && // [HGM] kibitz
4138 // started != STARTED_HOLDINGS && i > next_out) { // [HGM] should we compare to leftover_start in stead of i?
4139 // SendToPlayer(&buf[next_out], i - next_out);
4140 started != STARTED_HOLDINGS && leftover_start > next_out) {
4141 SendToPlayer(&buf[next_out], leftover_start - next_out);
4145 leftover_len = buf_len - leftover_start;
4146 /* if buffer ends with something we couldn't parse,
4147 reparse it after appending the next read */
4149 } else if (count == 0) {
4150 RemoveInputSource(isr);
4151 DisplayFatalError(_("Connection closed by ICS"), 0, 0);
4153 DisplayFatalError(_("Error reading from ICS"), error, 1);
4158 /* Board style 12 looks like this:
4160 <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
4162 * The "<12> " is stripped before it gets to this routine. The two
4163 * trailing 0's (flip state and clock ticking) are later addition, and
4164 * some chess servers may not have them, or may have only the first.
4165 * Additional trailing fields may be added in the future.
4168 #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"
4170 #define RELATION_OBSERVING_PLAYED 0
4171 #define RELATION_OBSERVING_STATIC -2 /* examined, oldmoves, or smoves */
4172 #define RELATION_PLAYING_MYMOVE 1
4173 #define RELATION_PLAYING_NOTMYMOVE -1
4174 #define RELATION_EXAMINING 2
4175 #define RELATION_ISOLATED_BOARD -3
4176 #define RELATION_STARTING_POSITION -4 /* FICS only */
4179 ParseBoard12 (char *string)
4181 GameMode newGameMode;
4182 int gamenum, newGame, newMove, relation, basetime, increment, ics_flip = 0, i;
4183 int j, k, n, moveNum, white_stren, black_stren, white_time, black_time, takeback;
4184 int double_push, castle_ws, castle_wl, castle_bs, castle_bl, irrev_count;
4185 char to_play, board_chars[200];
4186 char move_str[MSG_SIZ], str[MSG_SIZ], elapsed_time[MSG_SIZ];
4187 char black[32], white[32];
4189 int prevMove = currentMove;
4192 int fromX, fromY, toX, toY;
4194 int ranks=1, files=0; /* [HGM] ICS80: allow variable board size */
4195 char *bookHit = NULL; // [HGM] book
4196 Boolean weird = FALSE, reqFlag = FALSE;
4198 fromX = fromY = toX = toY = -1;
4202 if (appData.debugMode)
4203 fprintf(debugFP, _("Parsing board: %s\n"), string);
4205 move_str[0] = NULLCHAR;
4206 elapsed_time[0] = NULLCHAR;
4207 { /* [HGM] figure out how many ranks and files the board has, for ICS extension used by Capablanca server */
4209 while(i < 199 && (string[i] != ' ' || string[i+2] != ' ')) {
4210 if(string[i] == ' ') { ranks++; files = 0; }
4212 if(!strchr(" -pnbrqkPNBRQK" , string[i])) weird = TRUE; // test for fairies
4215 for(j = 0; j <i; j++) board_chars[j] = string[j];
4216 board_chars[i] = '\0';
4219 n = sscanf(string, PATTERN, &to_play, &double_push,
4220 &castle_ws, &castle_wl, &castle_bs, &castle_bl, &irrev_count,
4221 &gamenum, white, black, &relation, &basetime, &increment,
4222 &white_stren, &black_stren, &white_time, &black_time,
4223 &moveNum, str, elapsed_time, move_str, &ics_flip,
4227 snprintf(str, MSG_SIZ, _("Failed to parse board string:\n\"%s\""), string);
4228 DisplayError(str, 0);
4232 /* Convert the move number to internal form */
4233 moveNum = (moveNum - 1) * 2;
4234 if (to_play == 'B') moveNum++;
4235 if (moveNum > framePtr) { // [HGM] vari: do not run into saved variations
4236 DisplayFatalError(_("Game too long; increase MAX_MOVES and recompile"),
4242 case RELATION_OBSERVING_PLAYED:
4243 case RELATION_OBSERVING_STATIC:
4244 if (gamenum == -1) {
4245 /* Old ICC buglet */
4246 relation = RELATION_OBSERVING_STATIC;
4248 newGameMode = IcsObserving;
4250 case RELATION_PLAYING_MYMOVE:
4251 case RELATION_PLAYING_NOTMYMOVE:
4253 ((relation == RELATION_PLAYING_MYMOVE) == (to_play == 'W')) ?
4254 IcsPlayingWhite : IcsPlayingBlack;
4255 soughtPending =FALSE; // [HGM] seekgraph: solve race condition
4257 case RELATION_EXAMINING:
4258 newGameMode = IcsExamining;
4260 case RELATION_ISOLATED_BOARD:
4262 /* Just display this board. If user was doing something else,
4263 we will forget about it until the next board comes. */
4264 newGameMode = IcsIdle;
4266 case RELATION_STARTING_POSITION:
4267 newGameMode = gameMode;
4271 if((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
4272 gameMode == IcsObserving && appData.dualBoard) // also allow use of second board for observing two games
4273 && newGameMode == IcsObserving && gamenum != ics_gamenum && appData.bgObserve) {
4274 // [HGM] bughouse: don't act on alien boards while we play. Just parse the board and save it */
4275 int fac = strchr(elapsed_time, '.') ? 1 : 1000;
4276 static int lastBgGame = -1;
4278 for (k = 0; k < ranks; k++) {
4279 for (j = 0; j < files; j++)
4280 board[k][j+gameInfo.holdingsWidth] = CharToPiece(board_chars[(ranks-1-k)*(files+1) + j]);
4281 if(gameInfo.holdingsWidth > 1) {
4282 board[k][0] = board[k][BOARD_WIDTH-1] = EmptySquare;
4283 board[k][1] = board[k][BOARD_WIDTH-2] = (ChessSquare) 0;;
4286 CopyBoard(partnerBoard, board);
4287 if(toSqr = strchr(str, '/')) { // extract highlights from long move
4288 partnerBoard[EP_STATUS-3] = toSqr[1] - AAA; // kludge: hide highlighting info in board
4289 partnerBoard[EP_STATUS-4] = toSqr[2] - ONE;
4290 } else partnerBoard[EP_STATUS-4] = partnerBoard[EP_STATUS-3] = -1;
4291 if(toSqr = strchr(str, '-')) {
4292 partnerBoard[EP_STATUS-1] = toSqr[1] - AAA;
4293 partnerBoard[EP_STATUS-2] = toSqr[2] - ONE;
4294 } else partnerBoard[EP_STATUS-1] = partnerBoard[EP_STATUS-2] = -1;
4295 if(appData.dualBoard && !twoBoards) { twoBoards = 1; InitDrawingSizes(-2,0); }
4296 if(twoBoards) { partnerUp = 1; flipView = !flipView; } // [HGM] dual
4297 if(partnerUp) DrawPosition(FALSE, partnerBoard);
4299 DisplayWhiteClock(white_time*fac, to_play == 'W');
4300 DisplayBlackClock(black_time*fac, to_play != 'W');
4301 activePartner = to_play;
4302 if(gamenum != lastBgGame) {
4304 snprintf(buf, MSG_SIZ, "%s %s %s", white, _("vs."), black);
4307 lastBgGame = gamenum;
4308 activePartnerTime = to_play == 'W' ? white_time*fac : black_time*fac;
4309 partnerUp = 0; flipView = !flipView; } // [HGM] dual
4310 snprintf(partnerStatus, MSG_SIZ,"W: %d:%02d B: %d:%02d (%d-%d) %c", white_time*fac/60000, (white_time*fac%60000)/1000,
4311 (black_time*fac/60000), (black_time*fac%60000)/1000, white_stren, black_stren, to_play);
4312 DisplayMessage(partnerStatus, "");
4313 partnerBoardValid = TRUE;
4317 if(appData.dualBoard && appData.bgObserve) {
4318 if((newGameMode == IcsPlayingWhite || newGameMode == IcsPlayingBlack) && moveNum == 1)
4319 SendToICS(ics_prefix), SendToICS("pobserve\n");
4320 else if(newGameMode == IcsObserving && (gameMode == BeginningOfGame || gameMode == IcsIdle)) {
4322 snprintf(buf, MSG_SIZ, "%spobserve %s\n", ics_prefix, white);
4327 /* Modify behavior for initial board display on move listing
4330 switch (ics_getting_history) {
4334 case H_GOT_REQ_HEADER:
4335 case H_GOT_UNREQ_HEADER:
4336 /* This is the initial position of the current game */
4337 gamenum = ics_gamenum;
4338 moveNum = 0; /* old ICS bug workaround */
4339 if (to_play == 'B') {
4340 startedFromSetupPosition = TRUE;
4341 blackPlaysFirst = TRUE;
4343 if (forwardMostMove == 0) forwardMostMove = 1;
4344 if (backwardMostMove == 0) backwardMostMove = 1;
4345 if (currentMove == 0) currentMove = 1;
4347 newGameMode = gameMode;
4348 relation = RELATION_STARTING_POSITION; /* ICC needs this */
4350 case H_GOT_UNWANTED_HEADER:
4351 /* This is an initial board that we don't want */
4353 case H_GETTING_MOVES:
4354 /* Should not happen */
4355 DisplayError(_("Error gathering move list: extra board"), 0);
4356 ics_getting_history = H_FALSE;
4360 if (gameInfo.boardHeight != ranks || gameInfo.boardWidth != files ||
4361 move_str[1] == '@' && !gameInfo.holdingsWidth ||
4362 weird && (int)gameInfo.variant < (int)VariantShogi) {
4363 /* [HGM] We seem to have switched variant unexpectedly
4364 * Try to guess new variant from board size
4366 VariantClass newVariant = VariantFairy; // if 8x8, but fairies present
4367 if(ranks == 8 && files == 10) newVariant = VariantCapablanca; else
4368 if(ranks == 10 && files == 9) newVariant = VariantXiangqi; else
4369 if(ranks == 8 && files == 12) newVariant = VariantCourier; else
4370 if(ranks == 9 && files == 9) newVariant = VariantShogi; else
4371 if(ranks == 10 && files == 10) newVariant = VariantGrand; else
4372 if(!weird) newVariant = move_str[1] == '@' ? VariantCrazyhouse : VariantNormal;
4373 VariantSwitch(boards[currentMove], newVariant); /* temp guess */
4374 /* Get a move list just to see the header, which
4375 will tell us whether this is really bug or zh */
4376 if (ics_getting_history == H_FALSE) {
4377 ics_getting_history = H_REQUESTED; reqFlag = TRUE;
4378 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4383 /* Take action if this is the first board of a new game, or of a
4384 different game than is currently being displayed. */
4385 if (gamenum != ics_gamenum || newGameMode != gameMode ||
4386 relation == RELATION_ISOLATED_BOARD) {
4388 /* Forget the old game and get the history (if any) of the new one */
4389 if (gameMode != BeginningOfGame) {
4393 if (appData.autoRaiseBoard) BoardToTop();
4395 if (gamenum == -1) {
4396 newGameMode = IcsIdle;
4397 } else if ((moveNum > 0 || newGameMode == IcsObserving) && newGameMode != IcsIdle &&
4398 appData.getMoveList && !reqFlag) {
4399 /* Need to get game history */
4400 ics_getting_history = H_REQUESTED;
4401 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4405 /* Initially flip the board to have black on the bottom if playing
4406 black or if the ICS flip flag is set, but let the user change
4407 it with the Flip View button. */
4408 flipView = appData.autoFlipView ?
4409 (newGameMode == IcsPlayingBlack) || ics_flip :
4412 /* Done with values from previous mode; copy in new ones */
4413 gameMode = newGameMode;
4415 ics_gamenum = gamenum;
4416 if (gamenum == gs_gamenum) {
4417 int klen = strlen(gs_kind);
4418 if (gs_kind[klen - 1] == '.') gs_kind[klen - 1] = NULLCHAR;
4419 snprintf(str, MSG_SIZ, "ICS %s", gs_kind);
4420 gameInfo.event = StrSave(str);
4422 gameInfo.event = StrSave("ICS game");
4424 gameInfo.site = StrSave(appData.icsHost);
4425 gameInfo.date = PGNDate();
4426 gameInfo.round = StrSave("-");
4427 gameInfo.white = StrSave(white);
4428 gameInfo.black = StrSave(black);
4429 timeControl = basetime * 60 * 1000;
4431 timeIncrement = increment * 1000;
4432 movesPerSession = 0;
4433 gameInfo.timeControl = TimeControlTagValue();
4434 VariantSwitch(boards[currentMove], StringToVariant(gameInfo.event) );
4435 if (appData.debugMode) {
4436 fprintf(debugFP, "ParseBoard says variant = '%s'\n", gameInfo.event);
4437 fprintf(debugFP, "recognized as %s\n", VariantName(gameInfo.variant));
4438 setbuf(debugFP, NULL);
4441 gameInfo.outOfBook = NULL;
4443 /* Do we have the ratings? */
4444 if (strcmp(player1Name, white) == 0 &&
4445 strcmp(player2Name, black) == 0) {
4446 if (appData.debugMode)
4447 fprintf(debugFP, "Remembered ratings: W %d, B %d\n",
4448 player1Rating, player2Rating);
4449 gameInfo.whiteRating = player1Rating;
4450 gameInfo.blackRating = player2Rating;
4451 } else if (strcmp(player2Name, white) == 0 &&
4452 strcmp(player1Name, black) == 0) {
4453 if (appData.debugMode)
4454 fprintf(debugFP, "Remembered ratings: W %d, B %d\n",
4455 player2Rating, player1Rating);
4456 gameInfo.whiteRating = player2Rating;
4457 gameInfo.blackRating = player1Rating;
4459 player1Name[0] = player2Name[0] = NULLCHAR;
4461 /* Silence shouts if requested */
4462 if (appData.quietPlay &&
4463 (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)) {
4464 SendToICS(ics_prefix);
4465 SendToICS("set shout 0\n");
4469 /* Deal with midgame name changes */
4471 if (!gameInfo.white || strcmp(gameInfo.white, white) != 0) {
4472 if (gameInfo.white) free(gameInfo.white);
4473 gameInfo.white = StrSave(white);
4475 if (!gameInfo.black || strcmp(gameInfo.black, black) != 0) {
4476 if (gameInfo.black) free(gameInfo.black);
4477 gameInfo.black = StrSave(black);
4481 /* Throw away game result if anything actually changes in examine mode */
4482 if (gameMode == IcsExamining && !newGame) {
4483 gameInfo.result = GameUnfinished;
4484 if (gameInfo.resultDetails != NULL) {
4485 free(gameInfo.resultDetails);
4486 gameInfo.resultDetails = NULL;
4490 /* In pausing && IcsExamining mode, we ignore boards coming
4491 in if they are in a different variation than we are. */
4492 if (pauseExamInvalid) return;
4493 if (pausing && gameMode == IcsExamining) {
4494 if (moveNum <= pauseExamForwardMostMove) {
4495 pauseExamInvalid = TRUE;
4496 forwardMostMove = pauseExamForwardMostMove;
4501 if (appData.debugMode) {
4502 fprintf(debugFP, "load %dx%d board\n", files, ranks);
4504 /* Parse the board */
4505 for (k = 0; k < ranks; k++) {
4506 for (j = 0; j < files; j++)
4507 board[k][j+gameInfo.holdingsWidth] = CharToPiece(board_chars[(ranks-1-k)*(files+1) + j]);
4508 if(gameInfo.holdingsWidth > 1) {
4509 board[k][0] = board[k][BOARD_WIDTH-1] = EmptySquare;
4510 board[k][1] = board[k][BOARD_WIDTH-2] = (ChessSquare) 0;;
4513 if(moveNum==0 && gameInfo.variant == VariantSChess) {
4514 board[5][BOARD_RGHT+1] = WhiteAngel;
4515 board[6][BOARD_RGHT+1] = WhiteMarshall;
4516 board[1][0] = BlackMarshall;
4517 board[2][0] = BlackAngel;
4518 board[1][1] = board[2][1] = board[5][BOARD_RGHT] = board[6][BOARD_RGHT] = 1;
4520 CopyBoard(boards[moveNum], board);
4521 boards[moveNum][HOLDINGS_SET] = 0; // [HGM] indicate holdings not set
4523 startedFromSetupPosition =
4524 !CompareBoards(board, initialPosition);
4525 if(startedFromSetupPosition)
4526 initialRulePlies = irrev_count; /* [HGM] 50-move counter offset */
4529 /* [HGM] Set castling rights. Take the outermost Rooks,
4530 to make it also work for FRC opening positions. Note that board12
4531 is really defective for later FRC positions, as it has no way to
4532 indicate which Rook can castle if they are on the same side of King.
4533 For the initial position we grant rights to the outermost Rooks,
4534 and remember thos rights, and we then copy them on positions
4535 later in an FRC game. This means WB might not recognize castlings with
4536 Rooks that have moved back to their original position as illegal,
4537 but in ICS mode that is not its job anyway.
4539 if(moveNum == 0 || gameInfo.variant != VariantFischeRandom)
4540 { int i, j; ChessSquare wKing = WhiteKing, bKing = BlackKing;
4542 for(i=BOARD_LEFT, j=NoRights; i<BOARD_RGHT; i++)
4543 if(board[0][i] == WhiteRook) j = i;
4544 initialRights[0] = boards[moveNum][CASTLING][0] = (castle_ws == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4545 for(i=BOARD_RGHT-1, j=NoRights; i>=BOARD_LEFT; i--)
4546 if(board[0][i] == WhiteRook) j = i;
4547 initialRights[1] = boards[moveNum][CASTLING][1] = (castle_wl == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4548 for(i=BOARD_LEFT, j=NoRights; i<BOARD_RGHT; i++)
4549 if(board[BOARD_HEIGHT-1][i] == BlackRook) j = i;
4550 initialRights[3] = boards[moveNum][CASTLING][3] = (castle_bs == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4551 for(i=BOARD_RGHT-1, j=NoRights; i>=BOARD_LEFT; i--)
4552 if(board[BOARD_HEIGHT-1][i] == BlackRook) j = i;
4553 initialRights[4] = boards[moveNum][CASTLING][4] = (castle_bl == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4555 boards[moveNum][CASTLING][2] = boards[moveNum][CASTLING][5] = NoRights;
4556 if(gameInfo.variant == VariantKnightmate) { wKing = WhiteUnicorn; bKing = BlackUnicorn; }
4557 for(k=BOARD_LEFT; k<BOARD_RGHT; k++)
4558 if(board[0][k] == wKing) initialRights[2] = boards[moveNum][CASTLING][2] = k;
4559 for(k=BOARD_LEFT; k<BOARD_RGHT; k++)
4560 if(board[BOARD_HEIGHT-1][k] == bKing)
4561 initialRights[5] = boards[moveNum][CASTLING][5] = k;
4562 if(gameInfo.variant == VariantTwoKings) {
4563 // In TwoKings looking for a King does not work, so always give castling rights to a King on e1/e8
4564 if(board[0][4] == wKing) initialRights[2] = boards[moveNum][CASTLING][2] = 4;
4565 if(board[BOARD_HEIGHT-1][4] == bKing) initialRights[5] = boards[moveNum][CASTLING][5] = 4;
4568 r = boards[moveNum][CASTLING][0] = initialRights[0];
4569 if(board[0][r] != WhiteRook) boards[moveNum][CASTLING][0] = NoRights;
4570 r = boards[moveNum][CASTLING][1] = initialRights[1];
4571 if(board[0][r] != WhiteRook) boards[moveNum][CASTLING][1] = NoRights;
4572 r = boards[moveNum][CASTLING][3] = initialRights[3];
4573 if(board[BOARD_HEIGHT-1][r] != BlackRook) boards[moveNum][CASTLING][3] = NoRights;
4574 r = boards[moveNum][CASTLING][4] = initialRights[4];
4575 if(board[BOARD_HEIGHT-1][r] != BlackRook) boards[moveNum][CASTLING][4] = NoRights;
4576 /* wildcastle kludge: always assume King has rights */
4577 r = boards[moveNum][CASTLING][2] = initialRights[2];
4578 r = boards[moveNum][CASTLING][5] = initialRights[5];
4580 /* [HGM] e.p. rights. Assume that ICS sends file number here? */
4581 boards[moveNum][EP_STATUS] = EP_NONE;
4582 if(str[0] == 'P') boards[moveNum][EP_STATUS] = EP_PAWN_MOVE;
4583 if(strchr(move_str, 'x')) boards[moveNum][EP_STATUS] = EP_CAPTURE;
4584 if(double_push != -1) boards[moveNum][EP_STATUS] = double_push + BOARD_LEFT;
4587 if (ics_getting_history == H_GOT_REQ_HEADER ||
4588 ics_getting_history == H_GOT_UNREQ_HEADER) {
4589 /* This was an initial position from a move list, not
4590 the current position */
4594 /* Update currentMove and known move number limits */
4595 newMove = newGame || moveNum > forwardMostMove;
4598 forwardMostMove = backwardMostMove = currentMove = moveNum;
4599 if (gameMode == IcsExamining && moveNum == 0) {
4600 /* Workaround for ICS limitation: we are not told the wild
4601 type when starting to examine a game. But if we ask for
4602 the move list, the move list header will tell us */
4603 ics_getting_history = H_REQUESTED;
4604 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4607 } else if (moveNum == forwardMostMove + 1 || moveNum == forwardMostMove
4608 || (moveNum < forwardMostMove && moveNum >= backwardMostMove)) {
4610 /* [DM] If we found takebacks during icsEngineAnalyze try send to engine */
4611 /* [HGM] applied this also to an engine that is silently watching */
4612 if (appData.zippyPlay && moveNum < forwardMostMove && first.initDone &&
4613 (gameMode == IcsObserving || gameMode == IcsExamining) &&
4614 gameInfo.variant == currentlyInitializedVariant) {
4615 takeback = forwardMostMove - moveNum;
4616 for (i = 0; i < takeback; i++) {
4617 if (appData.debugMode) fprintf(debugFP, "take back move\n");
4618 SendToProgram("undo\n", &first);
4623 forwardMostMove = moveNum;
4624 if (!pausing || currentMove > forwardMostMove)
4625 currentMove = forwardMostMove;
4627 /* New part of history that is not contiguous with old part */
4628 if (pausing && gameMode == IcsExamining) {
4629 pauseExamInvalid = TRUE;
4630 forwardMostMove = pauseExamForwardMostMove;
4633 if (gameMode == IcsExamining && moveNum > 0 && appData.getMoveList) {
4635 if(appData.zippyPlay && forwardMostMove > 0 && first.initDone) {
4636 // [HGM] when we will receive the move list we now request, it will be
4637 // fed to the engine from the first move on. So if the engine is not
4638 // in the initial position now, bring it there.
4639 InitChessProgram(&first, 0);
4642 ics_getting_history = H_REQUESTED;
4643 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4646 forwardMostMove = backwardMostMove = currentMove = moveNum;
4649 /* Update the clocks */
4650 if (strchr(elapsed_time, '.')) {
4652 timeRemaining[0][moveNum] = whiteTimeRemaining = white_time;
4653 timeRemaining[1][moveNum] = blackTimeRemaining = black_time;
4655 /* Time is in seconds */
4656 timeRemaining[0][moveNum] = whiteTimeRemaining = white_time * 1000;
4657 timeRemaining[1][moveNum] = blackTimeRemaining = black_time * 1000;
4662 if (appData.zippyPlay && newGame &&
4663 gameMode != IcsObserving && gameMode != IcsIdle &&
4664 gameMode != IcsExamining)
4665 ZippyFirstBoard(moveNum, basetime, increment);
4668 /* Put the move on the move list, first converting
4669 to canonical algebraic form. */
4671 if (appData.debugMode) {
4672 if (appData.debugMode) { int f = forwardMostMove;
4673 fprintf(debugFP, "parseboard %d, castling = %d %d %d %d %d %d\n", f,
4674 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
4675 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
4677 fprintf(debugFP, "accepted move %s from ICS, parse it.\n", move_str);
4678 fprintf(debugFP, "moveNum = %d\n", moveNum);
4679 fprintf(debugFP, "board = %d-%d x %d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT);
4680 setbuf(debugFP, NULL);
4682 if (moveNum <= backwardMostMove) {
4683 /* We don't know what the board looked like before
4685 safeStrCpy(parseList[moveNum - 1], move_str, sizeof(parseList[moveNum - 1])/sizeof(parseList[moveNum - 1][0]));
4686 strcat(parseList[moveNum - 1], " ");
4687 strcat(parseList[moveNum - 1], elapsed_time);
4688 moveList[moveNum - 1][0] = NULLCHAR;
4689 } else if (strcmp(move_str, "none") == 0) {
4690 // [HGM] long SAN: swapped order; test for 'none' before parsing move
4691 /* Again, we don't know what the board looked like;
4692 this is really the start of the game. */
4693 parseList[moveNum - 1][0] = NULLCHAR;
4694 moveList[moveNum - 1][0] = NULLCHAR;
4695 backwardMostMove = moveNum;
4696 startedFromSetupPosition = TRUE;
4697 fromX = fromY = toX = toY = -1;
4699 // [HGM] long SAN: if legality-testing is off, disambiguation might not work or give wrong move.
4700 // So we parse the long-algebraic move string in stead of the SAN move
4701 int valid; char buf[MSG_SIZ], *prom;
4703 if(gameInfo.variant == VariantShogi && !strchr(move_str, '=') && !strchr(move_str, '@'))
4704 strcat(move_str, "="); // if ICS does not say 'promote' on non-drop, we defer.
4705 // str looks something like "Q/a1-a2"; kill the slash
4707 snprintf(buf, MSG_SIZ,"%c%s", str[0], str+2);
4708 else safeStrCpy(buf, str, sizeof(buf)/sizeof(buf[0])); // might be castling
4709 if((prom = strstr(move_str, "=")) && !strstr(buf, "="))
4710 strcat(buf, prom); // long move lacks promo specification!
4711 if(!appData.testLegality && move_str[1] != '@') { // drops never ambiguous (parser chokes on long form!)
4712 if(appData.debugMode)
4713 fprintf(debugFP, "replaced ICS move '%s' by '%s'\n", move_str, buf);
4714 safeStrCpy(move_str, buf, MSG_SIZ);
4716 valid = ParseOneMove(move_str, moveNum - 1, &moveType,
4717 &fromX, &fromY, &toX, &toY, &promoChar)
4718 || ParseOneMove(buf, moveNum - 1, &moveType,
4719 &fromX, &fromY, &toX, &toY, &promoChar);
4720 // end of long SAN patch
4722 (void) CoordsToAlgebraic(boards[moveNum - 1],
4723 PosFlags(moveNum - 1),
4724 fromY, fromX, toY, toX, promoChar,
4725 parseList[moveNum-1]);
4726 switch (MateTest(boards[moveNum], PosFlags(moveNum)) ) {
4732 if(gameInfo.variant != VariantShogi)
4733 strcat(parseList[moveNum - 1], "+");
4736 case MT_STAINMATE: // [HGM] xq: for notation stalemate that wins counts as checkmate
4737 strcat(parseList[moveNum - 1], "#");
4740 strcat(parseList[moveNum - 1], " ");
4741 strcat(parseList[moveNum - 1], elapsed_time);
4742 /* currentMoveString is set as a side-effect of ParseOneMove */
4743 if(gameInfo.variant == VariantShogi && currentMoveString[4]) currentMoveString[4] = '^';
4744 safeStrCpy(moveList[moveNum - 1], currentMoveString, sizeof(moveList[moveNum - 1])/sizeof(moveList[moveNum - 1][0]));
4745 strcat(moveList[moveNum - 1], "\n");
4747 if(gameInfo.holdingsWidth && !appData.disguise && gameInfo.variant != VariantSuper && gameInfo.variant != VariantGreat
4748 && gameInfo.variant != VariantGrand&& gameInfo.variant != VariantSChess) // inherit info that ICS does not give from previous board
4749 for(k=0; k<ranks; k++) for(j=BOARD_LEFT; j<BOARD_RGHT; j++) {
4750 ChessSquare old, new = boards[moveNum][k][j];
4751 if(fromY == DROP_RANK && k==toY && j==toX) continue; // dropped pieces always stand for themselves
4752 old = (k==toY && j==toX) ? boards[moveNum-1][fromY][fromX] : boards[moveNum-1][k][j]; // trace back mover
4753 if(old == new) continue;
4754 if(old == PROMOTED new) boards[moveNum][k][j] = old; // prevent promoted pieces to revert to primordial ones
4755 else if(new == WhiteWazir || new == BlackWazir) {
4756 if(old < WhiteCannon || old >= BlackPawn && old < BlackCannon)
4757 boards[moveNum][k][j] = PROMOTED old; // choose correct type of Gold in promotion
4758 else boards[moveNum][k][j] = old; // preserve type of Gold
4759 } else if((old == WhitePawn || old == BlackPawn) && new != EmptySquare) // Pawn promotions (but not e.p.capture!)
4760 boards[moveNum][k][j] = PROMOTED new; // use non-primordial representation of chosen piece
4763 /* Move from ICS was illegal!? Punt. */
4764 if (appData.debugMode) {
4765 fprintf(debugFP, "Illegal move from ICS '%s'\n", move_str);
4766 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
4768 safeStrCpy(parseList[moveNum - 1], move_str, sizeof(parseList[moveNum - 1])/sizeof(parseList[moveNum - 1][0]));
4769 strcat(parseList[moveNum - 1], " ");
4770 strcat(parseList[moveNum - 1], elapsed_time);
4771 moveList[moveNum - 1][0] = NULLCHAR;
4772 fromX = fromY = toX = toY = -1;
4775 if (appData.debugMode) {
4776 fprintf(debugFP, "Move parsed to '%s'\n", parseList[moveNum - 1]);
4777 setbuf(debugFP, NULL);
4781 /* Send move to chess program (BEFORE animating it). */
4782 if (appData.zippyPlay && !newGame && newMove &&
4783 (!appData.getMoveList || backwardMostMove == 0) && first.initDone) {
4785 if ((gameMode == IcsPlayingWhite && WhiteOnMove(moveNum)) ||
4786 (gameMode == IcsPlayingBlack && !WhiteOnMove(moveNum))) {
4787 if (moveList[moveNum - 1][0] == NULLCHAR) {
4788 snprintf(str, MSG_SIZ, _("Couldn't parse move \"%s\" from ICS"),
4790 DisplayError(str, 0);
4792 if (first.sendTime) {
4793 SendTimeRemaining(&first, gameMode == IcsPlayingWhite);
4795 bookHit = SendMoveToBookUser(moveNum - 1, &first, FALSE); // [HGM] book
4796 if (firstMove && !bookHit) {
4798 if (first.useColors) {
4799 SendToProgram(gameMode == IcsPlayingWhite ?
4801 "black\ngo\n", &first);
4803 SendToProgram("go\n", &first);
4805 first.maybeThinking = TRUE;
4808 } else if (gameMode == IcsObserving || gameMode == IcsExamining) {
4809 if (moveList[moveNum - 1][0] == NULLCHAR) {
4810 snprintf(str, MSG_SIZ, _("Couldn't parse move \"%s\" from ICS"), move_str);
4811 DisplayError(str, 0);
4813 if(gameInfo.variant == currentlyInitializedVariant) // [HGM] refrain sending moves engine can't understand!
4814 SendMoveToProgram(moveNum - 1, &first);
4821 if (moveNum > 0 && !gotPremove && !appData.noGUI) {
4822 /* If move comes from a remote source, animate it. If it
4823 isn't remote, it will have already been animated. */
4824 if (!pausing && !ics_user_moved && prevMove == moveNum - 1) {
4825 AnimateMove(boards[moveNum - 1], fromX, fromY, toX, toY);
4827 if (!pausing && appData.highlightLastMove) {
4828 SetHighlights(fromX, fromY, toX, toY);
4832 /* Start the clocks */
4833 whiteFlag = blackFlag = FALSE;
4834 appData.clockMode = !(basetime == 0 && increment == 0);
4836 ics_clock_paused = TRUE;
4838 } else if (ticking == 1) {
4839 ics_clock_paused = FALSE;
4841 if (gameMode == IcsIdle ||
4842 relation == RELATION_OBSERVING_STATIC ||
4843 relation == RELATION_EXAMINING ||
4845 DisplayBothClocks();
4849 /* Display opponents and material strengths */
4850 if (gameInfo.variant != VariantBughouse &&
4851 gameInfo.variant != VariantCrazyhouse && !appData.noGUI) {
4852 if (tinyLayout || smallLayout) {
4853 if(gameInfo.variant == VariantNormal)
4854 snprintf(str, MSG_SIZ, "%s(%d) %s(%d) {%d %d}",
4855 gameInfo.white, white_stren, gameInfo.black, black_stren,
4856 basetime, increment);
4858 snprintf(str, MSG_SIZ, "%s(%d) %s(%d) {%d %d w%d}",
4859 gameInfo.white, white_stren, gameInfo.black, black_stren,
4860 basetime, increment, (int) gameInfo.variant);
4862 if(gameInfo.variant == VariantNormal)
4863 snprintf(str, MSG_SIZ, "%s (%d) %s %s (%d) {%d %d}",
4864 gameInfo.white, white_stren, _("vs."), gameInfo.black, black_stren,
4865 basetime, increment);
4867 snprintf(str, MSG_SIZ, "%s (%d) %s %s (%d) {%d %d %s}",
4868 gameInfo.white, white_stren, _("vs."), gameInfo.black, black_stren,
4869 basetime, increment, VariantName(gameInfo.variant));
4872 if (appData.debugMode) {
4873 fprintf(debugFP, "Display title '%s, gameInfo.variant = %d'\n", str, gameInfo.variant);
4878 /* Display the board */
4879 if (!pausing && !appData.noGUI) {
4881 if (appData.premove)
4883 ((gameMode == IcsPlayingWhite) && (WhiteOnMove(currentMove))) ||
4884 ((gameMode == IcsPlayingBlack) && (!WhiteOnMove(currentMove))))
4885 ClearPremoveHighlights();
4887 j = seekGraphUp; seekGraphUp = FALSE; // [HGM] seekgraph: when we draw a board, it overwrites the seek graph
4888 if(partnerUp) { flipView = originalFlip; partnerUp = FALSE; j = TRUE; } // [HGM] bughouse: restore view
4889 DrawPosition(j, boards[currentMove]);
4891 DisplayMove(moveNum - 1);
4892 if (appData.ringBellAfterMoves && /*!ics_user_moved*/ // [HGM] use absolute method to recognize own move
4893 !((gameMode == IcsPlayingWhite) && (!WhiteOnMove(moveNum)) ||
4894 (gameMode == IcsPlayingBlack) && (WhiteOnMove(moveNum)) ) ) {
4895 if(newMove) RingBell(); else PlayIcsUnfinishedSound();
4899 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
4901 if(bookHit) { // [HGM] book: simulate book reply
4902 static char bookMove[MSG_SIZ]; // a bit generous?
4904 programStats.nodes = programStats.depth = programStats.time =
4905 programStats.score = programStats.got_only_move = 0;
4906 sprintf(programStats.movelist, "%s (xbook)", bookHit);
4908 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
4909 strcat(bookMove, bookHit);
4910 HandleMachineMove(bookMove, &first);
4919 if (appData.icsActive && gameMode != IcsIdle && ics_gamenum > 0) {
4920 ics_getting_history = H_REQUESTED;
4921 snprintf(buf, MSG_SIZ, "%smoves %d\n", ics_prefix, ics_gamenum);
4927 SendToBoth (char *msg)
4928 { // to make it easy to keep two engines in step in dual analysis
4929 SendToProgram(msg, &first);
4930 if(second.analyzing) SendToProgram(msg, &second);
4934 AnalysisPeriodicEvent (int force)
4936 if (((programStats.ok_to_send == 0 || programStats.line_is_book)
4937 && !force) || !appData.periodicUpdates)
4940 /* Send . command to Crafty to collect stats */
4943 /* Don't send another until we get a response (this makes
4944 us stop sending to old Crafty's which don't understand
4945 the "." command (sending illegal cmds resets node count & time,
4946 which looks bad)) */
4947 programStats.ok_to_send = 0;
4951 ics_update_width (int new_width)
4953 ics_printf("set width %d\n", new_width);
4957 SendMoveToProgram (int moveNum, ChessProgramState *cps)
4961 if(moveList[moveNum][1] == '@' && moveList[moveNum][0] == '@') {
4962 // null move in variant where engine does not understand it (for analysis purposes)
4963 SendBoard(cps, moveNum + 1); // send position after move in stead.
4966 if (cps->useUsermove) {
4967 SendToProgram("usermove ", cps);
4971 if ((space = strchr(parseList[moveNum], ' ')) != NULL) {
4972 int len = space - parseList[moveNum];
4973 memcpy(buf, parseList[moveNum], len);
4975 buf[len] = NULLCHAR;
4977 snprintf(buf, MSG_SIZ,"%s\n", parseList[moveNum]);
4979 SendToProgram(buf, cps);
4981 if(cps->alphaRank) { /* [HGM] shogi: temporarily convert to shogi coordinates before sending */
4982 AlphaRank(moveList[moveNum], 4);
4983 SendToProgram(moveList[moveNum], cps);
4984 AlphaRank(moveList[moveNum], 4); // and back
4986 /* Added by Tord: Send castle moves in "O-O" in FRC games if required by
4987 * the engine. It would be nice to have a better way to identify castle
4989 if((gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom)
4990 && cps->useOOCastle) {
4991 int fromX = moveList[moveNum][0] - AAA;
4992 int fromY = moveList[moveNum][1] - ONE;
4993 int toX = moveList[moveNum][2] - AAA;
4994 int toY = moveList[moveNum][3] - ONE;
4995 if((boards[moveNum][fromY][fromX] == WhiteKing
4996 && boards[moveNum][toY][toX] == WhiteRook)
4997 || (boards[moveNum][fromY][fromX] == BlackKing
4998 && boards[moveNum][toY][toX] == BlackRook)) {
4999 if(toX > fromX) SendToProgram("O-O\n", cps);
5000 else SendToProgram("O-O-O\n", cps);
5002 else SendToProgram(moveList[moveNum], cps);
5004 if(BOARD_HEIGHT > 10) { // [HGM] big: convert ranks to double-digit where needed
5005 if(moveList[moveNum][1] == '@' && (BOARD_HEIGHT < 16 || moveList[moveNum][0] <= 'Z')) { // drop move
5006 if(moveList[moveNum][0]== '@') snprintf(buf, MSG_SIZ, "@@@@\n"); else
5007 snprintf(buf, MSG_SIZ, "%c@%c%d%s", moveList[moveNum][0],
5008 moveList[moveNum][2], moveList[moveNum][3] - '0', moveList[moveNum]+4);
5010 snprintf(buf, MSG_SIZ, "%c%d%c%d%s", moveList[moveNum][0], moveList[moveNum][1] - '0',
5011 moveList[moveNum][2], moveList[moveNum][3] - '0', moveList[moveNum]+4);
5012 SendToProgram(buf, cps);
5014 else SendToProgram(moveList[moveNum], cps);
5015 /* End of additions by Tord */
5018 /* [HGM] setting up the opening has brought engine in force mode! */
5019 /* Send 'go' if we are in a mode where machine should play. */
5020 if( (moveNum == 0 && setboardSpoiledMachineBlack && cps == &first) &&
5021 (gameMode == TwoMachinesPlay ||
5023 gameMode == IcsPlayingBlack || gameMode == IcsPlayingWhite ||
5025 gameMode == MachinePlaysBlack || gameMode == MachinePlaysWhite) ) {
5026 SendToProgram("go\n", cps);
5027 if (appData.debugMode) {
5028 fprintf(debugFP, "(extra)\n");
5031 setboardSpoiledMachineBlack = 0;
5035 SendMoveToICS (ChessMove moveType, int fromX, int fromY, int toX, int toY, char promoChar)
5037 char user_move[MSG_SIZ];
5040 if(gameInfo.variant == VariantSChess && promoChar) {
5041 snprintf(suffix, 4, "=%c", toX == BOARD_WIDTH<<1 ? ToUpper(promoChar) : ToLower(promoChar));
5042 if(moveType == NormalMove) moveType = WhitePromotion; // kludge to do gating
5043 } else suffix[0] = NULLCHAR;
5047 snprintf(user_move, MSG_SIZ, _("say Internal error; bad moveType %d (%d,%d-%d,%d)"),
5048 (int)moveType, fromX, fromY, toX, toY);
5049 DisplayError(user_move + strlen("say "), 0);
5051 case WhiteKingSideCastle:
5052 case BlackKingSideCastle:
5053 case WhiteQueenSideCastleWild:
5054 case BlackQueenSideCastleWild:
5056 case WhiteHSideCastleFR:
5057 case BlackHSideCastleFR:
5059 snprintf(user_move, MSG_SIZ, "o-o%s\n", suffix);
5061 case WhiteQueenSideCastle:
5062 case BlackQueenSideCastle:
5063 case WhiteKingSideCastleWild:
5064 case BlackKingSideCastleWild:
5066 case WhiteASideCastleFR:
5067 case BlackASideCastleFR:
5069 snprintf(user_move, MSG_SIZ, "o-o-o%s\n",suffix);
5071 case WhiteNonPromotion:
5072 case BlackNonPromotion:
5073 sprintf(user_move, "%c%c%c%c==\n", AAA + fromX, ONE + fromY, AAA + toX, ONE + toY);
5075 case WhitePromotion:
5076 case BlackPromotion:
5077 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
5078 snprintf(user_move, MSG_SIZ, "%c%c%c%c=%c\n",
5079 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
5080 PieceToChar(WhiteFerz));
5081 else if(gameInfo.variant == VariantGreat)
5082 snprintf(user_move, MSG_SIZ,"%c%c%c%c=%c\n",
5083 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
5084 PieceToChar(WhiteMan));
5086 snprintf(user_move, MSG_SIZ, "%c%c%c%c=%c\n",
5087 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
5093 snprintf(user_move, MSG_SIZ, "%c@%c%c\n",
5094 ToUpper(PieceToChar((ChessSquare) fromX)),
5095 AAA + toX, ONE + toY);
5097 case IllegalMove: /* could be a variant we don't quite understand */
5098 if(fromY == DROP_RANK) goto drop; // We need 'IllegalDrop' move type?
5100 case WhiteCapturesEnPassant:
5101 case BlackCapturesEnPassant:
5102 snprintf(user_move, MSG_SIZ,"%c%c%c%c\n",
5103 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY);
5106 SendToICS(user_move);
5107 if(appData.keepAlive) // [HGM] alive: schedule sending of dummy 'date' command
5108 ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
5113 { // [HGM] upload: send entire stored game to ICS as long-algebraic moves.
5114 int i, last = forwardMostMove; // make sure ICS reply cannot pre-empt us by clearing fmm
5115 static char *castlingStrings[4] = { "none", "kside", "qside", "both" };
5116 if(gameMode == IcsObserving || gameMode == IcsPlayingBlack || gameMode == IcsPlayingWhite) {
5117 DisplayError(_("You cannot do this while you are playing or observing"), 0);
5120 if(gameMode != IcsExamining) { // is this ever not the case?
5121 char buf[MSG_SIZ], *p, *fen, command[MSG_SIZ], bsetup = 0;
5123 if(ics_type == ICS_ICC) { // on ICC match ourselves in applicable variant
5124 snprintf(command,MSG_SIZ, "match %s", ics_handle);
5125 } else { // on FICS we must first go to general examine mode
5126 safeStrCpy(command, "examine\nbsetup", sizeof(command)/sizeof(command[0])); // and specify variant within it with bsetups
5128 if(gameInfo.variant != VariantNormal) {
5129 // try figure out wild number, as xboard names are not always valid on ICS
5130 for(i=1; i<=36; i++) {
5131 snprintf(buf, MSG_SIZ, "wild/%d", i);
5132 if(StringToVariant(buf) == gameInfo.variant) break;
5134 if(i<=36 && ics_type == ICS_ICC) snprintf(buf, MSG_SIZ,"%s w%d\n", command, i);
5135 else if(i == 22) snprintf(buf,MSG_SIZ, "%s fr\n", command);
5136 else snprintf(buf, MSG_SIZ,"%s %s\n", command, VariantName(gameInfo.variant));
5137 } else snprintf(buf, MSG_SIZ,"%s\n", ics_type == ICS_ICC ? command : "examine\n"); // match yourself or examine
5138 SendToICS(ics_prefix);
5140 if(startedFromSetupPosition || backwardMostMove != 0) {
5141 fen = PositionToFEN(backwardMostMove, NULL);
5142 if(ics_type == ICS_ICC) { // on ICC we can simply send a complete FEN to set everything
5143 snprintf(buf, MSG_SIZ,"loadfen %s\n", fen);
5145 } else { // FICS: everything has to set by separate bsetup commands
5146 p = strchr(fen, ' '); p[0] = NULLCHAR; // cut after board
5147 snprintf(buf, MSG_SIZ,"bsetup fen %s\n", fen);
5149 if(!WhiteOnMove(backwardMostMove)) {
5150 SendToICS("bsetup tomove black\n");
5152 i = (strchr(p+3, 'K') != NULL) + 2*(strchr(p+3, 'Q') != NULL);
5153 snprintf(buf, MSG_SIZ,"bsetup wcastle %s\n", castlingStrings[i]);
5155 i = (strchr(p+3, 'k') != NULL) + 2*(strchr(p+3, 'q') != NULL);
5156 snprintf(buf, MSG_SIZ, "bsetup bcastle %s\n", castlingStrings[i]);
5158 i = boards[backwardMostMove][EP_STATUS];
5159 if(i >= 0) { // set e.p.
5160 snprintf(buf, MSG_SIZ,"bsetup eppos %c\n", i+AAA);
5166 if(bsetup || ics_type != ICS_ICC && gameInfo.variant != VariantNormal)
5167 SendToICS("bsetup done\n"); // switch to normal examining.
5169 for(i = backwardMostMove; i<last; i++) {
5171 snprintf(buf, sizeof(buf)/sizeof(buf[0]),"%s\n", parseList[i]);
5172 if((*buf == 'b' || *buf == 'B') && buf[1] == 'x') { // work-around for stupid FICS bug, which thinks bxc3 can be a Bishop move
5173 int len = strlen(moveList[i]);
5174 snprintf(buf, sizeof(buf)/sizeof(buf[0]),"%s", moveList[i]); // use long algebraic
5175 if(!isdigit(buf[len-2])) snprintf(buf+len-2, 20-len, "=%c\n", ToUpper(buf[len-2])); // promotion must have '=' in ICS format
5179 SendToICS(ics_prefix);
5180 SendToICS(ics_type == ICS_ICC ? "tag result Game in progress\n" : "commit\n");
5184 CoordsToComputerAlgebraic (int rf, int ff, int rt, int ft, char promoChar, char move[7])
5186 if (rf == DROP_RANK) {
5187 if(ff == EmptySquare) sprintf(move, "@@@@\n"); else // [HGM] pass
5188 sprintf(move, "%c@%c%c\n",
5189 ToUpper(PieceToChar((ChessSquare) ff)), AAA + ft, ONE + rt);
5191 if (promoChar == 'x' || promoChar == NULLCHAR) {
5192 sprintf(move, "%c%c%c%c\n",
5193 AAA + ff, ONE + rf, AAA + ft, ONE + rt);
5195 sprintf(move, "%c%c%c%c%c\n",
5196 AAA + ff, ONE + rf, AAA + ft, ONE + rt, promoChar);
5202 ProcessICSInitScript (FILE *f)
5206 while (fgets(buf, MSG_SIZ, f)) {
5207 SendToICSDelayed(buf,(long)appData.msLoginDelay);
5214 static int lastX, lastY, selectFlag, dragging;
5219 ChessSquare king = WhiteKing, pawn = WhitePawn, last = promoSweep;
5220 if(gameInfo.variant == VariantKnightmate) king = WhiteUnicorn;
5221 if(gameInfo.variant == VariantSuicide || gameInfo.variant == VariantGiveaway) king = EmptySquare;
5222 if(promoSweep >= BlackPawn) king = WHITE_TO_BLACK king, pawn = WHITE_TO_BLACK pawn;
5223 if(gameInfo.variant == VariantSpartan && pawn == BlackPawn) pawn = BlackLance, king = EmptySquare;
5224 if(fromY != BOARD_HEIGHT-2 && fromY != 1) pawn = EmptySquare;
5227 if(promoSweep == EmptySquare) promoSweep = BlackPawn; // wrap
5228 else if((int)promoSweep == -1) promoSweep = WhiteKing;
5229 else if(promoSweep == BlackPawn && step < 0) promoSweep = WhitePawn;
5230 else if(promoSweep == WhiteKing && step > 0) promoSweep = BlackKing;
5231 if(!step) step = -1;
5232 } while(PieceToChar(promoSweep) == '.' || PieceToChar(promoSweep) == '~' || promoSweep == pawn ||
5233 appData.testLegality && (promoSweep == king ||
5234 gameInfo.variant == VariantShogi && promoSweep != PROMOTED last && last != PROMOTED promoSweep && last != promoSweep));
5236 int victim = boards[currentMove][toY][toX];
5237 boards[currentMove][toY][toX] = promoSweep;
5238 DrawPosition(FALSE, boards[currentMove]);
5239 boards[currentMove][toY][toX] = victim;
5241 ChangeDragPiece(promoSweep);
5245 PromoScroll (int x, int y)
5249 if(promoSweep == EmptySquare || !appData.sweepSelect) return FALSE;
5250 if(abs(x - lastX) < 25 && abs(y - lastY) < 25) return FALSE;
5251 if( y > lastY + 2 ) step = -1; else if(y < lastY - 2) step = 1;
5252 if(!step) return FALSE;
5253 lastX = x; lastY = y;
5254 if((promoSweep < BlackPawn) == flipView) step = -step;
5255 if(step > 0) selectFlag = 1;
5256 if(!selectFlag) Sweep(step);
5261 NextPiece (int step)
5263 ChessSquare piece = boards[currentMove][toY][toX];
5266 if(pieceSweep == EmptySquare) pieceSweep = WhitePawn; // wrap
5267 if((int)pieceSweep == -1) pieceSweep = BlackKing;
5268 if(!step) step = -1;
5269 } while(PieceToChar(pieceSweep) == '.');
5270 boards[currentMove][toY][toX] = pieceSweep;
5271 DrawPosition(FALSE, boards[currentMove]);
5272 boards[currentMove][toY][toX] = piece;
5274 /* [HGM] Shogi move preprocessor: swap digits for letters, vice versa */
5276 AlphaRank (char *move, int n)
5278 // char *p = move, c; int x, y;
5280 if (appData.debugMode) {
5281 fprintf(debugFP, "alphaRank(%s,%d)\n", move, n);
5285 move[2]>='0' && move[2]<='9' &&
5286 move[3]>='a' && move[3]<='x' ) {
5288 move[2] = BOARD_RGHT -1 - (move[2]-'1') + AAA;
5289 move[3] = BOARD_HEIGHT-1 - (move[3]-'a') + ONE;
5291 if(move[0]>='0' && move[0]<='9' &&
5292 move[1]>='a' && move[1]<='x' &&
5293 move[2]>='0' && move[2]<='9' &&
5294 move[3]>='a' && move[3]<='x' ) {
5295 /* input move, Shogi -> normal */
5296 move[0] = BOARD_RGHT -1 - (move[0]-'1') + AAA;
5297 move[1] = BOARD_HEIGHT-1 - (move[1]-'a') + ONE;
5298 move[2] = BOARD_RGHT -1 - (move[2]-'1') + AAA;
5299 move[3] = BOARD_HEIGHT-1 - (move[3]-'a') + ONE;
5302 move[3]>='0' && move[3]<='9' &&
5303 move[2]>='a' && move[2]<='x' ) {
5305 move[2] = BOARD_RGHT - 1 - (move[2]-AAA) + '1';
5306 move[3] = BOARD_HEIGHT-1 - (move[3]-ONE) + 'a';
5309 move[0]>='a' && move[0]<='x' &&
5310 move[3]>='0' && move[3]<='9' &&
5311 move[2]>='a' && move[2]<='x' ) {
5312 /* output move, normal -> Shogi */
5313 move[0] = BOARD_RGHT - 1 - (move[0]-AAA) + '1';
5314 move[1] = BOARD_HEIGHT-1 - (move[1]-ONE) + 'a';
5315 move[2] = BOARD_RGHT - 1 - (move[2]-AAA) + '1';
5316 move[3] = BOARD_HEIGHT-1 - (move[3]-ONE) + 'a';
5317 if(move[4] == PieceToChar(BlackQueen)) move[4] = '+';
5319 if (appData.debugMode) {
5320 fprintf(debugFP, " out = '%s'\n", move);
5324 char yy_textstr[8000];
5326 /* Parser for moves from gnuchess, ICS, or user typein box */
5328 ParseOneMove (char *move, int moveNum, ChessMove *moveType, int *fromX, int *fromY, int *toX, int *toY, char *promoChar)
5330 *moveType = yylexstr(moveNum, move, yy_textstr, sizeof yy_textstr);
5332 switch (*moveType) {
5333 case WhitePromotion:
5334 case BlackPromotion:
5335 case WhiteNonPromotion:
5336 case BlackNonPromotion:
5338 case WhiteCapturesEnPassant:
5339 case BlackCapturesEnPassant:
5340 case WhiteKingSideCastle:
5341 case WhiteQueenSideCastle:
5342 case BlackKingSideCastle:
5343 case BlackQueenSideCastle:
5344 case WhiteKingSideCastleWild:
5345 case WhiteQueenSideCastleWild:
5346 case BlackKingSideCastleWild:
5347 case BlackQueenSideCastleWild:
5348 /* Code added by Tord: */
5349 case WhiteHSideCastleFR:
5350 case WhiteASideCastleFR:
5351 case BlackHSideCastleFR:
5352 case BlackASideCastleFR:
5353 /* End of code added by Tord */
5354 case IllegalMove: /* bug or odd chess variant */
5355 *fromX = currentMoveString[0] - AAA;
5356 *fromY = currentMoveString[1] - ONE;
5357 *toX = currentMoveString[2] - AAA;
5358 *toY = currentMoveString[3] - ONE;
5359 *promoChar = currentMoveString[4];
5360 if (*fromX < BOARD_LEFT || *fromX >= BOARD_RGHT || *fromY < 0 || *fromY >= BOARD_HEIGHT ||
5361 *toX < BOARD_LEFT || *toX >= BOARD_RGHT || *toY < 0 || *toY >= BOARD_HEIGHT) {
5362 if (appData.debugMode) {
5363 fprintf(debugFP, "Off-board move (%d,%d)-(%d,%d)%c, type = %d\n", *fromX, *fromY, *toX, *toY, *promoChar, *moveType);
5365 *fromX = *fromY = *toX = *toY = 0;
5368 if (appData.testLegality) {
5369 return (*moveType != IllegalMove);
5371 return !(*fromX == *toX && *fromY == *toY) && boards[moveNum][*fromY][*fromX] != EmptySquare &&
5372 WhiteOnMove(moveNum) == (boards[moveNum][*fromY][*fromX] < BlackPawn);
5377 *fromX = *moveType == WhiteDrop ?
5378 (int) CharToPiece(ToUpper(currentMoveString[0])) :
5379 (int) CharToPiece(ToLower(currentMoveString[0]));
5381 *toX = currentMoveString[2] - AAA;
5382 *toY = currentMoveString[3] - ONE;
5383 *promoChar = NULLCHAR;
5387 case ImpossibleMove:
5397 if (appData.debugMode) {
5398 fprintf(debugFP, "Impossible move %s, type = %d\n", currentMoveString, *moveType);
5401 *fromX = *fromY = *toX = *toY = 0;
5402 *promoChar = NULLCHAR;
5407 Boolean pushed = FALSE;
5408 char *lastParseAttempt;
5411 ParsePV (char *pv, Boolean storeComments, Boolean atEnd)
5412 { // Parse a string of PV moves, and append to current game, behind forwardMostMove
5413 int fromX, fromY, toX, toY; char promoChar;
5418 if (gameMode == AnalyzeMode && currentMove < forwardMostMove) {
5419 PushInner(currentMove, forwardMostMove); // [HGM] engine might not be thinking on forwardMost position!
5422 endPV = forwardMostMove;
5424 while(*pv == ' ' || *pv == '\n' || *pv == '\t') pv++; // must still read away whitespace
5425 if(nr == 0 && !storeComments && *pv == '(') pv++; // first (ponder) move can be in parentheses
5426 lastParseAttempt = pv;
5427 valid = ParseOneMove(pv, endPV, &moveType, &fromX, &fromY, &toX, &toY, &promoChar);
5428 if(!valid && nr == 0 &&
5429 ParseOneMove(pv, endPV-1, &moveType, &fromX, &fromY, &toX, &toY, &promoChar)){
5430 nr++; moveType = Comment; // First move has been played; kludge to make sure we continue
5431 // Hande case where played move is different from leading PV move
5432 CopyBoard(boards[endPV+1], boards[endPV-1]); // tentatively unplay last game move
5433 CopyBoard(boards[endPV+2], boards[endPV-1]); // and play first move of PV
5434 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[endPV+2]);
5435 if(!CompareBoards(boards[endPV], boards[endPV+2])) {
5436 endPV += 2; // if position different, keep this
5437 moveList[endPV-1][0] = fromX + AAA;
5438 moveList[endPV-1][1] = fromY + ONE;
5439 moveList[endPV-1][2] = toX + AAA;
5440 moveList[endPV-1][3] = toY + ONE;
5441 parseList[endPV-1][0] = NULLCHAR;
5442 safeStrCpy(moveList[endPV-2], "_0_0", sizeof(moveList[endPV-2])/sizeof(moveList[endPV-2][0])); // suppress premove highlight on takeback move
5445 pv = strstr(pv, yy_textstr) + strlen(yy_textstr); // skip what we parsed
5446 if(nr == 0 && !storeComments && *pv == ')') pv++; // closing parenthesis of ponder move;
5447 if(moveType == Comment && storeComments) AppendComment(endPV, yy_textstr, FALSE);
5448 if(moveType == Comment || moveType == NAG || moveType == ElapsedTime) {
5449 valid++; // allow comments in PV
5453 if(endPV+1 > framePtr) break; // no space, truncate
5456 CopyBoard(boards[endPV], boards[endPV-1]);
5457 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[endPV]);
5458 CoordsToComputerAlgebraic(fromY, fromX, toY, toX, promoChar, moveList[endPV - 1]);
5459 strncat(moveList[endPV-1], "\n", MOVE_LEN);
5460 CoordsToAlgebraic(boards[endPV - 1],
5461 PosFlags(endPV - 1),
5462 fromY, fromX, toY, toX, promoChar,
5463 parseList[endPV - 1]);
5465 if(atEnd == 2) return; // used hidden, for PV conversion
5466 currentMove = (atEnd || endPV == forwardMostMove) ? endPV : forwardMostMove + 1;
5467 if(currentMove == forwardMostMove) ClearPremoveHighlights(); else
5468 SetPremoveHighlights(moveList[currentMove-1][0]-AAA, moveList[currentMove-1][1]-ONE,
5469 moveList[currentMove-1][2]-AAA, moveList[currentMove-1][3]-ONE);
5470 DrawPosition(TRUE, boards[currentMove]);
5474 MultiPV (ChessProgramState *cps)
5475 { // check if engine supports MultiPV, and if so, return the number of the option that sets it
5477 for(i=0; i<cps->nrOptions; i++)
5478 if(!strcmp(cps->option[i].name, "MultiPV") && cps->option[i].type == Spin)
5484 LoadMultiPV (int x, int y, char *buf, int index, int *start, int *end, int pane)
5486 int startPV, multi, lineStart, origIndex = index;
5487 char *p, buf2[MSG_SIZ];
5488 ChessProgramState *cps = (pane ? &second : &first);
5490 if(index < 0 || index >= strlen(buf)) return FALSE; // sanity
5491 lastX = x; lastY = y;
5492 while(index > 0 && buf[index-1] != '\n') index--; // beginning of line
5493 lineStart = startPV = index;
5494 while(buf[index] != '\n') if(buf[index++] == '\t') startPV = index;
5495 if(index == startPV && (p = StrCaseStr(buf+index, "PV="))) startPV = p - buf + 3;
5497 do{ while(buf[index] && buf[index] != '\n') index++;
5498 } while(buf[index] == '\n' && buf[index+1] == '\\' && buf[index+2] == ' ' && index++); // join kibitzed PV continuation line
5500 if(lineStart == 0 && gameMode == AnalyzeMode && (multi = MultiPV(cps)) >= 0) {
5501 int n = cps->option[multi].value;
5502 if(origIndex > 17 && origIndex < 24) { if(n>1) n--; } else if(origIndex > index - 6) n++;
5503 snprintf(buf2, MSG_SIZ, "option MultiPV=%d\n", n);
5504 if(cps->option[multi].value != n) SendToProgram(buf2, cps);
5505 cps->option[multi].value = n;
5508 } else if(strstr(buf+lineStart, "exclude:") == buf+lineStart) { // exclude moves clicked
5509 ExcludeClick(origIndex - lineStart);
5512 ParsePV(buf+startPV, FALSE, gameMode != AnalyzeMode);
5513 *start = startPV; *end = index-1;
5520 static char buf[10*MSG_SIZ];
5521 int i, k=0, savedEnd=endPV, saveFMM = forwardMostMove;
5523 if(forwardMostMove < endPV) PushInner(forwardMostMove, endPV);
5524 ParsePV(pv, FALSE, 2); // this appends PV to game, suppressing any display of it
5525 for(i = forwardMostMove; i<endPV; i++){
5526 if(i&1) snprintf(buf+k, 10*MSG_SIZ-k, "%s ", parseList[i]);
5527 else snprintf(buf+k, 10*MSG_SIZ-k, "%d. %s ", i/2 + 1, parseList[i]);
5530 snprintf(buf+k, 10*MSG_SIZ-k, "%s", lastParseAttempt); // if we ran into stuff that could not be parsed, print it verbatim
5531 if(forwardMostMove < savedEnd) { PopInner(0); forwardMostMove = saveFMM; } // PopInner would set fmm to endPV!
5537 LoadPV (int x, int y)
5538 { // called on right mouse click to load PV
5539 int which = gameMode == TwoMachinesPlay && (WhiteOnMove(forwardMostMove) == (second.twoMachinesColor[0] == 'w'));
5540 lastX = x; lastY = y;
5541 ParsePV(lastPV[which], FALSE, TRUE); // load the PV of the thinking engine in the boards array.
5548 int oldFMM = forwardMostMove; // N.B.: this was currentMove before PV was loaded!
5549 if(endPV < 0) return;
5550 if(appData.autoCopyPV) CopyFENToClipboard();
5552 if(gameMode == AnalyzeMode && currentMove > forwardMostMove) {
5553 Boolean saveAnimate = appData.animate;
5555 if(shiftKey && storedGames < MAX_VARIATIONS-2) { // wants to start variation, and there is space
5556 if(storedGames == 1) GreyRevert(FALSE); // we already pushed the tail, so just make it official
5557 } else storedGames--; // abandon shelved tail of original game
5560 forwardMostMove = currentMove;
5561 currentMove = oldFMM;
5562 appData.animate = FALSE;
5563 ToNrEvent(forwardMostMove);
5564 appData.animate = saveAnimate;
5566 currentMove = forwardMostMove;
5567 if(pushed) { PopInner(0); pushed = FALSE; } // restore shelved game continuation
5568 ClearPremoveHighlights();
5569 DrawPosition(TRUE, boards[currentMove]);
5573 MovePV (int x, int y, int h)
5574 { // step through PV based on mouse coordinates (called on mouse move)
5575 int margin = h>>3, step = 0, threshold = (pieceSweep == EmptySquare ? 10 : 15);
5577 // we must somehow check if right button is still down (might be released off board!)
5578 if(endPV < 0 && pieceSweep == EmptySquare) return; // needed in XBoard because lastX/Y is shared :-(
5579 if(abs(x - lastX) < threshold && abs(y - lastY) < threshold) return;
5580 if( y > lastY + 2 ) step = -1; else if(y < lastY - 2) step = 1;
5582 lastX = x; lastY = y;
5584 if(pieceSweep != EmptySquare) { NextPiece(step); return; }
5585 if(endPV < 0) return;
5586 if(y < margin) step = 1; else
5587 if(y > h - margin) step = -1;
5588 if(currentMove + step > endPV || currentMove + step < forwardMostMove) step = 0;
5589 currentMove += step;
5590 if(currentMove == forwardMostMove) ClearPremoveHighlights(); else
5591 SetPremoveHighlights(moveList[currentMove-1][0]-AAA, moveList[currentMove-1][1]-ONE,
5592 moveList[currentMove-1][2]-AAA, moveList[currentMove-1][3]-ONE);
5593 DrawPosition(FALSE, boards[currentMove]);
5597 // [HGM] shuffle: a general way to suffle opening setups, applicable to arbitrary variants.
5598 // All positions will have equal probability, but the current method will not provide a unique
5599 // numbering scheme for arrays that contain 3 or more pieces of the same kind.
5605 int piecesLeft[(int)BlackPawn];
5606 int seed, nrOfShuffles;
5609 GetPositionNumber ()
5610 { // sets global variable seed
5613 seed = appData.defaultFrcPosition;
5614 if(seed < 0) { // randomize based on time for negative FRC position numbers
5615 for(i=0; i<50; i++) seed += random();
5616 seed = random() ^ random() >> 8 ^ random() << 8;
5617 if(seed<0) seed = -seed;
5622 put (Board board, int pieceType, int rank, int n, int shade)
5623 // put the piece on the (n-1)-th empty squares of the given shade
5627 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) {
5628 if( (((i-BOARD_LEFT)&1)+1) & shade && board[rank][i] == EmptySquare && n-- == 0) {
5629 board[rank][i] = (ChessSquare) pieceType;
5630 squaresLeft[((i-BOARD_LEFT)&1) + 1]--;
5632 piecesLeft[pieceType]--;
5641 AddOnePiece (Board board, int pieceType, int rank, int shade)
5642 // calculate where the next piece goes, (any empty square), and put it there
5646 i = seed % squaresLeft[shade];
5647 nrOfShuffles *= squaresLeft[shade];
5648 seed /= squaresLeft[shade];
5649 put(board, pieceType, rank, i, shade);
5653 AddTwoPieces (Board board, int pieceType, int rank)
5654 // calculate where the next 2 identical pieces go, (any empty square), and put it there
5656 int i, n=squaresLeft[ANY], j=n-1, k;
5658 k = n*(n-1)/2; // nr of possibilities, not counting permutations
5659 i = seed % k; // pick one
5662 while(i >= j) i -= j--;
5663 j = n - 1 - j; i += j;
5664 put(board, pieceType, rank, j, ANY);
5665 put(board, pieceType, rank, i, ANY);
5669 SetUpShuffle (Board board, int number)
5673 GetPositionNumber(); nrOfShuffles = 1;
5675 squaresLeft[DARK] = (BOARD_RGHT - BOARD_LEFT + 1)/2;
5676 squaresLeft[ANY] = BOARD_RGHT - BOARD_LEFT;
5677 squaresLeft[LITE] = squaresLeft[ANY] - squaresLeft[DARK];
5679 for(p = 0; p<=(int)WhiteKing; p++) piecesLeft[p] = 0;
5681 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // count pieces and clear board
5682 p = (int) board[0][i];
5683 if(p < (int) BlackPawn) piecesLeft[p] ++;
5684 board[0][i] = EmptySquare;
5687 if(PosFlags(0) & F_ALL_CASTLE_OK) {
5688 // shuffles restricted to allow normal castling put KRR first
5689 if(piecesLeft[(int)WhiteKing]) // King goes rightish of middle
5690 put(board, WhiteKing, 0, (gameInfo.boardWidth+1)/2, ANY);
5691 else if(piecesLeft[(int)WhiteUnicorn]) // in Knightmate Unicorn castles
5692 put(board, WhiteUnicorn, 0, (gameInfo.boardWidth+1)/2, ANY);
5693 if(piecesLeft[(int)WhiteRook]) // First supply a Rook for K-side castling
5694 put(board, WhiteRook, 0, gameInfo.boardWidth-2, ANY);
5695 if(piecesLeft[(int)WhiteRook]) // Then supply a Rook for Q-side castling
5696 put(board, WhiteRook, 0, 0, ANY);
5697 // in variants with super-numerary Kings and Rooks, we leave these for the shuffle
5700 if(((BOARD_RGHT-BOARD_LEFT) & 1) == 0)
5701 // only for even boards make effort to put pairs of colorbound pieces on opposite colors
5702 for(p = (int) WhiteKing; p > (int) WhitePawn; p--) {
5703 if(p != (int) WhiteBishop && p != (int) WhiteFerz && p != (int) WhiteAlfil) continue;
5704 while(piecesLeft[p] >= 2) {
5705 AddOnePiece(board, p, 0, LITE);
5706 AddOnePiece(board, p, 0, DARK);
5708 // Odd color-bound pieces are shuffled with the rest (to not run out of paired squares)
5711 for(p = (int) WhiteKing - 2; p > (int) WhitePawn; p--) {
5712 // Remaining pieces (non-colorbound, or odd color bound) can be put anywhere
5713 // but we leave King and Rooks for last, to possibly obey FRC restriction
5714 if(p == (int)WhiteRook) continue;
5715 while(piecesLeft[p] >= 2) AddTwoPieces(board, p, 0); // add in pairs, for not counting permutations
5716 if(piecesLeft[p]) AddOnePiece(board, p, 0, ANY); // add the odd piece
5719 // now everything is placed, except perhaps King (Unicorn) and Rooks
5721 if(PosFlags(0) & F_FRC_TYPE_CASTLING) {
5722 // Last King gets castling rights
5723 while(piecesLeft[(int)WhiteUnicorn]) {
5724 i = put(board, WhiteUnicorn, 0, piecesLeft[(int)WhiteRook]/2, ANY);
5725 initialRights[2] = initialRights[5] = board[CASTLING][2] = board[CASTLING][5] = i;
5728 while(piecesLeft[(int)WhiteKing]) {
5729 i = put(board, WhiteKing, 0, piecesLeft[(int)WhiteRook]/2, ANY);
5730 initialRights[2] = initialRights[5] = board[CASTLING][2] = board[CASTLING][5] = i;
5735 while(piecesLeft[(int)WhiteKing]) AddOnePiece(board, WhiteKing, 0, ANY);
5736 while(piecesLeft[(int)WhiteUnicorn]) AddOnePiece(board, WhiteUnicorn, 0, ANY);
5739 // Only Rooks can be left; simply place them all
5740 while(piecesLeft[(int)WhiteRook]) {
5741 i = put(board, WhiteRook, 0, 0, ANY);
5742 if(PosFlags(0) & F_FRC_TYPE_CASTLING) { // first and last Rook get FRC castling rights
5745 initialRights[1] = initialRights[4] = board[CASTLING][1] = board[CASTLING][4] = i;
5747 initialRights[0] = initialRights[3] = board[CASTLING][0] = board[CASTLING][3] = i;
5750 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // copy black from white
5751 board[BOARD_HEIGHT-1][i] = (int) board[0][i] < BlackPawn ? WHITE_TO_BLACK board[0][i] : EmptySquare;
5754 if(number >= 0) appData.defaultFrcPosition %= nrOfShuffles; // normalize
5758 SetCharTable (char *table, const char * map)
5759 /* [HGM] moved here from winboard.c because of its general usefulness */
5760 /* Basically a safe strcpy that uses the last character as King */
5762 int result = FALSE; int NrPieces;
5764 if( map != NULL && (NrPieces=strlen(map)) <= (int) EmptySquare
5765 && NrPieces >= 12 && !(NrPieces&1)) {
5766 int i; /* [HGM] Accept even length from 12 to 34 */
5768 for( i=0; i<(int) EmptySquare; i++ ) table[i] = '.';
5769 for( i=0; i<NrPieces/2-1; i++ ) {
5771 table[i + (int)BlackPawn - (int) WhitePawn] = map[i+NrPieces/2];
5773 table[(int) WhiteKing] = map[NrPieces/2-1];
5774 table[(int) BlackKing] = map[NrPieces-1];
5783 Prelude (Board board)
5784 { // [HGM] superchess: random selection of exo-pieces
5785 int i, j, k; ChessSquare p;
5786 static ChessSquare exoPieces[4] = { WhiteAngel, WhiteMarshall, WhiteSilver, WhiteLance };
5788 GetPositionNumber(); // use FRC position number
5790 if(appData.pieceToCharTable != NULL) { // select pieces to participate from given char table
5791 SetCharTable(pieceToChar, appData.pieceToCharTable);
5792 for(i=(int)WhiteQueen+1, j=0; i<(int)WhiteKing && j<4; i++)
5793 if(PieceToChar((ChessSquare)i) != '.') exoPieces[j++] = (ChessSquare) i;
5796 j = seed%4; seed /= 4;
5797 p = board[0][BOARD_LEFT+j]; board[0][BOARD_LEFT+j] = EmptySquare; k = PieceToNumber(p);
5798 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5799 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5800 j = seed%3 + (seed%3 >= j); seed /= 3;
5801 p = board[0][BOARD_LEFT+j]; board[0][BOARD_LEFT+j] = EmptySquare; k = PieceToNumber(p);
5802 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5803 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5804 j = seed%3; seed /= 3;
5805 p = board[0][BOARD_LEFT+j+5]; board[0][BOARD_LEFT+j+5] = EmptySquare; k = PieceToNumber(p);
5806 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5807 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5808 j = seed%2 + (seed%2 >= j); seed /= 2;
5809 p = board[0][BOARD_LEFT+j+5]; board[0][BOARD_LEFT+j+5] = EmptySquare; k = PieceToNumber(p);
5810 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5811 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5812 j = seed%4; seed /= 4; put(board, exoPieces[3], 0, j, ANY);
5813 j = seed%3; seed /= 3; put(board, exoPieces[2], 0, j, ANY);
5814 j = seed%2; seed /= 2; put(board, exoPieces[1], 0, j, ANY);
5815 put(board, exoPieces[0], 0, 0, ANY);
5816 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) board[BOARD_HEIGHT-1][i] = WHITE_TO_BLACK board[0][i];
5820 InitPosition (int redraw)
5822 ChessSquare (* pieces)[BOARD_FILES];
5823 int i, j, pawnRow, overrule,
5824 oldx = gameInfo.boardWidth,
5825 oldy = gameInfo.boardHeight,
5826 oldh = gameInfo.holdingsWidth;
5829 if(appData.icsActive) shuffleOpenings = FALSE; // [HGM] shuffle: in ICS mode, only shuffle on ICS request
5831 /* [AS] Initialize pv info list [HGM] and game status */
5833 for( i=0; i<=framePtr; i++ ) { // [HGM] vari: spare saved variations
5834 pvInfoList[i].depth = 0;
5835 boards[i][EP_STATUS] = EP_NONE;
5836 for( j=0; j<BOARD_FILES-2; j++ ) boards[i][CASTLING][j] = NoRights;
5839 initialRulePlies = 0; /* 50-move counter start */
5841 castlingRank[0] = castlingRank[1] = castlingRank[2] = 0;
5842 castlingRank[3] = castlingRank[4] = castlingRank[5] = BOARD_HEIGHT-1;
5846 /* [HGM] logic here is completely changed. In stead of full positions */
5847 /* the initialized data only consist of the two backranks. The switch */
5848 /* selects which one we will use, which is than copied to the Board */
5849 /* initialPosition, which for the rest is initialized by Pawns and */
5850 /* empty squares. This initial position is then copied to boards[0], */
5851 /* possibly after shuffling, so that it remains available. */
5853 gameInfo.holdingsWidth = 0; /* default board sizes */
5854 gameInfo.boardWidth = 8;
5855 gameInfo.boardHeight = 8;
5856 gameInfo.holdingsSize = 0;
5857 nrCastlingRights = -1; /* [HGM] Kludge to indicate default should be used */
5858 for(i=0; i<BOARD_FILES-2; i++)
5859 initialPosition[CASTLING][i] = initialRights[i] = NoRights; /* but no rights yet */
5860 initialPosition[EP_STATUS] = EP_NONE;
5861 SetCharTable(pieceToChar, "PNBRQ...........Kpnbrq...........k");
5862 if(startVariant == gameInfo.variant) // [HGM] nicks: enable nicknames in original variant
5863 SetCharTable(pieceNickName, appData.pieceNickNames);
5864 else SetCharTable(pieceNickName, "............");
5867 switch (gameInfo.variant) {
5868 case VariantFischeRandom:
5869 shuffleOpenings = TRUE;
5872 case VariantShatranj:
5873 pieces = ShatranjArray;
5874 nrCastlingRights = 0;
5875 SetCharTable(pieceToChar, "PN.R.QB...Kpn.r.qb...k");
5878 pieces = makrukArray;
5879 nrCastlingRights = 0;
5880 startedFromSetupPosition = TRUE;
5881 SetCharTable(pieceToChar, "PN.R.M....SKpn.r.m....sk");
5883 case VariantTwoKings:
5884 pieces = twoKingsArray;
5887 pieces = GrandArray;
5888 nrCastlingRights = 0;
5889 SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
5890 gameInfo.boardWidth = 10;
5891 gameInfo.boardHeight = 10;
5892 gameInfo.holdingsSize = 7;
5894 case VariantCapaRandom:
5895 shuffleOpenings = TRUE;
5896 case VariantCapablanca:
5897 pieces = CapablancaArray;
5898 gameInfo.boardWidth = 10;
5899 SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
5902 pieces = GothicArray;
5903 gameInfo.boardWidth = 10;
5904 SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
5907 SetCharTable(pieceToChar, "PNBRQ..HEKpnbrq..hek");
5908 gameInfo.holdingsSize = 7;
5909 for(i=0; i<BOARD_FILES; i++) initialPosition[VIRGIN][i] = VIRGIN_W | VIRGIN_B;
5912 pieces = JanusArray;
5913 gameInfo.boardWidth = 10;
5914 SetCharTable(pieceToChar, "PNBRQ..JKpnbrq..jk");
5915 nrCastlingRights = 6;
5916 initialPosition[CASTLING][0] = initialRights[0] = BOARD_RGHT-1;
5917 initialPosition[CASTLING][1] = initialRights[1] = BOARD_LEFT;
5918 initialPosition[CASTLING][2] = initialRights[2] =(BOARD_WIDTH-1)>>1;
5919 initialPosition[CASTLING][3] = initialRights[3] = BOARD_RGHT-1;
5920 initialPosition[CASTLING][4] = initialRights[4] = BOARD_LEFT;
5921 initialPosition[CASTLING][5] = initialRights[5] =(BOARD_WIDTH-1)>>1;
5924 pieces = FalconArray;
5925 gameInfo.boardWidth = 10;
5926 SetCharTable(pieceToChar, "PNBRQ.............FKpnbrq.............fk");
5928 case VariantXiangqi:
5929 pieces = XiangqiArray;
5930 gameInfo.boardWidth = 9;
5931 gameInfo.boardHeight = 10;
5932 nrCastlingRights = 0;
5933 SetCharTable(pieceToChar, "PH.R.AE..K.C.ph.r.ae..k.c.");
5936 pieces = ShogiArray;
5937 gameInfo.boardWidth = 9;
5938 gameInfo.boardHeight = 9;
5939 gameInfo.holdingsSize = 7;
5940 nrCastlingRights = 0;
5941 SetCharTable(pieceToChar, "PNBRLS...G.++++++Kpnbrls...g.++++++k");
5943 case VariantCourier:
5944 pieces = CourierArray;
5945 gameInfo.boardWidth = 12;
5946 nrCastlingRights = 0;
5947 SetCharTable(pieceToChar, "PNBR.FE..WMKpnbr.fe..wmk");
5949 case VariantKnightmate:
5950 pieces = KnightmateArray;
5951 SetCharTable(pieceToChar, "P.BRQ.....M.........K.p.brq.....m.........k.");
5953 case VariantSpartan:
5954 pieces = SpartanArray;
5955 SetCharTable(pieceToChar, "PNBRQ................K......lwg.....c...h..k");
5958 pieces = fairyArray;
5959 SetCharTable(pieceToChar, "PNBRQFEACWMOHIJGDVLSUKpnbrqfeacwmohijgdvlsuk");
5962 pieces = GreatArray;
5963 gameInfo.boardWidth = 10;
5964 SetCharTable(pieceToChar, "PN....E...S..HWGMKpn....e...s..hwgmk");
5965 gameInfo.holdingsSize = 8;
5969 SetCharTable(pieceToChar, "PNBRQ..SE.......V.AKpnbrq..se.......v.ak");
5970 gameInfo.holdingsSize = 8;
5971 startedFromSetupPosition = TRUE;
5973 case VariantCrazyhouse:
5974 case VariantBughouse:
5976 SetCharTable(pieceToChar, "PNBRQ.......~~~~Kpnbrq.......~~~~k");
5977 gameInfo.holdingsSize = 5;
5979 case VariantWildCastle:
5981 /* !!?shuffle with kings guaranteed to be on d or e file */
5982 shuffleOpenings = 1;
5984 case VariantNoCastle:
5986 nrCastlingRights = 0;
5987 /* !!?unconstrained back-rank shuffle */
5988 shuffleOpenings = 1;
5993 if(appData.NrFiles >= 0) {
5994 if(gameInfo.boardWidth != appData.NrFiles) overrule++;
5995 gameInfo.boardWidth = appData.NrFiles;
5997 if(appData.NrRanks >= 0) {
5998 gameInfo.boardHeight = appData.NrRanks;
6000 if(appData.holdingsSize >= 0) {
6001 i = appData.holdingsSize;
6002 if(i > gameInfo.boardHeight) i = gameInfo.boardHeight;
6003 gameInfo.holdingsSize = i;
6005 if(gameInfo.holdingsSize) gameInfo.holdingsWidth = 2;
6006 if(BOARD_HEIGHT > BOARD_RANKS || BOARD_WIDTH > BOARD_FILES)
6007 DisplayFatalError(_("Recompile to support this BOARD_RANKS or BOARD_FILES!"), 0, 2);
6009 pawnRow = gameInfo.boardHeight - 7; /* seems to work in all common variants */
6010 if(pawnRow < 1) pawnRow = 1;
6011 if(gameInfo.variant == VariantMakruk || gameInfo.variant == VariantGrand) pawnRow = 2;
6013 /* User pieceToChar list overrules defaults */
6014 if(appData.pieceToCharTable != NULL)
6015 SetCharTable(pieceToChar, appData.pieceToCharTable);
6017 for( j=0; j<BOARD_WIDTH; j++ ) { ChessSquare s = EmptySquare;
6019 if(j==BOARD_LEFT-1 || j==BOARD_RGHT)
6020 s = (ChessSquare) 0; /* account holding counts in guard band */
6021 for( i=0; i<BOARD_HEIGHT; i++ )
6022 initialPosition[i][j] = s;
6024 if(j < BOARD_LEFT || j >= BOARD_RGHT || overrule) continue;
6025 initialPosition[gameInfo.variant == VariantGrand][j] = pieces[0][j-gameInfo.holdingsWidth];
6026 initialPosition[pawnRow][j] = WhitePawn;
6027 initialPosition[BOARD_HEIGHT-pawnRow-1][j] = gameInfo.variant == VariantSpartan ? BlackLance : BlackPawn;
6028 if(gameInfo.variant == VariantXiangqi) {
6030 initialPosition[pawnRow][j] =
6031 initialPosition[BOARD_HEIGHT-pawnRow-1][j] = EmptySquare;
6032 if(j==BOARD_LEFT+1 || j>=BOARD_RGHT-2) {
6033 initialPosition[2][j] = WhiteCannon;
6034 initialPosition[BOARD_HEIGHT-3][j] = BlackCannon;
6038 if(gameInfo.variant == VariantGrand) {
6039 if(j==BOARD_LEFT || j>=BOARD_RGHT-1) {
6040 initialPosition[0][j] = WhiteRook;
6041 initialPosition[BOARD_HEIGHT-1][j] = BlackRook;
6044 initialPosition[BOARD_HEIGHT-1-(gameInfo.variant == VariantGrand)][j] = pieces[1][j-gameInfo.holdingsWidth];
6046 if( (gameInfo.variant == VariantShogi) && !overrule ) {
6049 initialPosition[1][j] = WhiteBishop;
6050 initialPosition[BOARD_HEIGHT-2][j] = BlackRook;
6052 initialPosition[1][j] = WhiteRook;
6053 initialPosition[BOARD_HEIGHT-2][j] = BlackBishop;
6056 if( nrCastlingRights == -1) {
6057 /* [HGM] Build normal castling rights (must be done after board sizing!) */
6058 /* This sets default castling rights from none to normal corners */
6059 /* Variants with other castling rights must set them themselves above */
6060 nrCastlingRights = 6;
6062 initialPosition[CASTLING][0] = initialRights[0] = BOARD_RGHT-1;
6063 initialPosition[CASTLING][1] = initialRights[1] = BOARD_LEFT;
6064 initialPosition[CASTLING][2] = initialRights[2] = BOARD_WIDTH>>1;
6065 initialPosition[CASTLING][3] = initialRights[3] = BOARD_RGHT-1;
6066 initialPosition[CASTLING][4] = initialRights[4] = BOARD_LEFT;
6067 initialPosition[CASTLING][5] = initialRights[5] = BOARD_WIDTH>>1;
6070 if(gameInfo.variant == VariantSuper) Prelude(initialPosition);
6071 if(gameInfo.variant == VariantGreat) { // promotion commoners
6072 initialPosition[PieceToNumber(WhiteMan)][BOARD_WIDTH-1] = WhiteMan;
6073 initialPosition[PieceToNumber(WhiteMan)][BOARD_WIDTH-2] = 9;
6074 initialPosition[BOARD_HEIGHT-1-PieceToNumber(WhiteMan)][0] = BlackMan;
6075 initialPosition[BOARD_HEIGHT-1-PieceToNumber(WhiteMan)][1] = 9;
6077 if( gameInfo.variant == VariantSChess ) {
6078 initialPosition[1][0] = BlackMarshall;
6079 initialPosition[2][0] = BlackAngel;
6080 initialPosition[6][BOARD_WIDTH-1] = WhiteMarshall;
6081 initialPosition[5][BOARD_WIDTH-1] = WhiteAngel;
6082 initialPosition[1][1] = initialPosition[2][1] =
6083 initialPosition[6][BOARD_WIDTH-2] = initialPosition[5][BOARD_WIDTH-2] = 1;
6085 if (appData.debugMode) {
6086 fprintf(debugFP, "shuffleOpenings = %d\n", shuffleOpenings);
6088 if(shuffleOpenings) {
6089 SetUpShuffle(initialPosition, appData.defaultFrcPosition);
6090 startedFromSetupPosition = TRUE;
6092 if(startedFromPositionFile) {
6093 /* [HGM] loadPos: use PositionFile for every new game */
6094 CopyBoard(initialPosition, filePosition);
6095 for(i=0; i<nrCastlingRights; i++)
6096 initialRights[i] = filePosition[CASTLING][i];
6097 startedFromSetupPosition = TRUE;
6100 CopyBoard(boards[0], initialPosition);
6102 if(oldx != gameInfo.boardWidth ||
6103 oldy != gameInfo.boardHeight ||
6104 oldv != gameInfo.variant ||
6105 oldh != gameInfo.holdingsWidth
6107 InitDrawingSizes(-2 ,0);
6109 oldv = gameInfo.variant;
6111 DrawPosition(TRUE, boards[currentMove]);
6115 SendBoard (ChessProgramState *cps, int moveNum)
6117 char message[MSG_SIZ];
6119 if (cps->useSetboard) {
6120 char* fen = PositionToFEN(moveNum, cps->fenOverride);
6121 snprintf(message, MSG_SIZ,"setboard %s\n", fen);
6122 SendToProgram(message, cps);
6127 int i, j, left=0, right=BOARD_WIDTH;
6128 /* Kludge to set black to move, avoiding the troublesome and now
6129 * deprecated "black" command.
6131 if (!WhiteOnMove(moveNum)) // [HGM] but better a deprecated command than an illegal move...
6132 SendToProgram(boards[0][1][BOARD_LEFT] == WhitePawn ? "a2a3\n" : "black\n", cps);
6134 if(!cps->extendedEdit) left = BOARD_LEFT, right = BOARD_RGHT; // only board proper
6136 SendToProgram("edit\n", cps);
6137 SendToProgram("#\n", cps);
6138 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
6139 bp = &boards[moveNum][i][left];
6140 for (j = left; j < right; j++, bp++) {
6141 if(j == BOARD_LEFT-1 || j == BOARD_RGHT) continue;
6142 if ((int) *bp < (int) BlackPawn) {
6143 if(j == BOARD_RGHT+1)
6144 snprintf(message, MSG_SIZ, "%c@%d\n", PieceToChar(*bp), bp[-1]);
6145 else snprintf(message, MSG_SIZ, "%c%c%c\n", PieceToChar(*bp), AAA + j, ONE + i);
6146 if(message[0] == '+' || message[0] == '~') {
6147 snprintf(message, MSG_SIZ,"%c%c%c+\n",
6148 PieceToChar((ChessSquare)(DEMOTED *bp)),
6151 if(cps->alphaRank) { /* [HGM] shogi: translate coords */
6152 message[1] = BOARD_RGHT - 1 - j + '1';
6153 message[2] = BOARD_HEIGHT - 1 - i + 'a';
6155 SendToProgram(message, cps);
6160 SendToProgram("c\n", cps);
6161 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
6162 bp = &boards[moveNum][i][left];
6163 for (j = left; j < right; j++, bp++) {
6164 if(j == BOARD_LEFT-1 || j == BOARD_RGHT) continue;
6165 if (((int) *bp != (int) EmptySquare)
6166 && ((int) *bp >= (int) BlackPawn)) {
6167 if(j == BOARD_LEFT-2)
6168 snprintf(message, MSG_SIZ, "%c@%d\n", ToUpper(PieceToChar(*bp)), bp[1]);
6169 else snprintf(message,MSG_SIZ, "%c%c%c\n", ToUpper(PieceToChar(*bp)),
6171 if(message[0] == '+' || message[0] == '~') {
6172 snprintf(message, MSG_SIZ,"%c%c%c+\n",
6173 PieceToChar((ChessSquare)(DEMOTED *bp)),
6176 if(cps->alphaRank) { /* [HGM] shogi: translate coords */
6177 message[1] = BOARD_RGHT - 1 - j + '1';
6178 message[2] = BOARD_HEIGHT - 1 - i + 'a';
6180 SendToProgram(message, cps);
6185 SendToProgram(".\n", cps);
6187 setboardSpoiledMachineBlack = 0; /* [HGM] assume WB 4.2.7 already solves this after sending setboard */
6190 char exclusionHeader[MSG_SIZ];
6191 int exCnt, excludePtr;
6192 typedef struct { int ff, fr, tf, tr, pc, mark; } Exclusion;
6193 static Exclusion excluTab[200];
6194 static char excludeMap[(BOARD_RANKS*BOARD_FILES*BOARD_RANKS*BOARD_FILES+7)/8]; // [HGM] exclude: bitmap for excluced moves
6200 for(j=0; j<(BOARD_RANKS*BOARD_FILES*BOARD_RANKS*BOARD_FILES+7)/8; j++) excludeMap[j] = s;
6201 exclusionHeader[19] = s ? '-' : '+'; // update tail state
6207 safeStrCpy(exclusionHeader, "exclude: none best +tail \n", MSG_SIZ);
6208 excludePtr = 24; exCnt = 0;
6213 UpdateExcludeHeader (int fromY, int fromX, int toY, int toX, char promoChar, char state)
6214 { // search given move in table of header moves, to know where it is listed (and add if not there), and update state
6215 char buf[2*MOVE_LEN], *p;
6216 Exclusion *e = excluTab;
6218 for(i=0; i<exCnt; i++)
6219 if(e[i].ff == fromX && e[i].fr == fromY &&
6220 e[i].tf == toX && e[i].tr == toY && e[i].pc == promoChar) break;
6221 if(i == exCnt) { // was not in exclude list; add it
6222 CoordsToAlgebraic(boards[currentMove], PosFlags(currentMove), fromY, fromX, toY, toX, promoChar, buf);
6223 if(strlen(exclusionHeader + excludePtr) < strlen(buf)) { // no space to write move
6224 if(state != exclusionHeader[19]) exclusionHeader[19] = '*'; // tail is now in mixed state
6227 e[i].ff = fromX; e[i].fr = fromY; e[i].tf = toX; e[i].tr = toY; e[i].pc = promoChar;
6228 excludePtr++; e[i].mark = excludePtr++;
6229 for(p=buf; *p; p++) exclusionHeader[excludePtr++] = *p; // copy move
6232 exclusionHeader[e[i].mark] = state;
6236 ExcludeOneMove (int fromY, int fromX, int toY, int toX, char promoChar, char state)
6237 { // include or exclude the given move, as specified by state ('+' or '-'), or toggle
6241 if((signed char)promoChar == -1) { // kludge to indicate best move
6242 if(!ParseOneMove(lastPV[0], currentMove, &moveType, &fromX, &fromY, &toX, &toY, &promoChar)) // get current best move from last PV
6243 return 1; // if unparsable, abort
6245 // update exclusion map (resolving toggle by consulting existing state)
6246 k=(BOARD_FILES*fromY+fromX)*BOARD_RANKS*BOARD_FILES + (BOARD_FILES*toY+toX);
6248 if(state == '*') state = (excludeMap[k] & 1<<j ? '+' : '-'); // toggle
6249 if(state == '-' && !promoChar) // only non-promotions get marked as excluded, to allow exclusion of under-promotions
6250 excludeMap[k] |= 1<<j;
6251 else excludeMap[k] &= ~(1<<j);
6253 UpdateExcludeHeader(fromY, fromX, toY, toX, promoChar, state);
6255 snprintf(buf, MSG_SIZ, "%sclude ", state == '+' ? "in" : "ex");
6256 CoordsToComputerAlgebraic(fromY, fromX, toY, toX, promoChar, buf+8);
6258 return (state == '+');
6262 ExcludeClick (int index)
6265 Exclusion *e = excluTab;
6266 if(index < 25) { // none, best or tail clicked
6267 if(index < 13) { // none: include all
6268 WriteMap(0); // clear map
6269 for(i=0; i<exCnt; i++) exclusionHeader[excluTab[i].mark] = '+'; // and moves
6270 SendToBoth("include all\n"); // and inform engine
6271 } else if(index > 18) { // tail
6272 if(exclusionHeader[19] == '-') { // tail was excluded
6273 SendToBoth("include all\n");
6274 WriteMap(0); // clear map completely
6275 // now re-exclude selected moves
6276 for(i=0; i<exCnt; i++) if(exclusionHeader[e[i].mark] == '-')
6277 ExcludeOneMove(e[i].fr, e[i].ff, e[i].tr, e[i].tf, e[i].pc, '-');
6278 } else { // tail was included or in mixed state
6279 SendToBoth("exclude all\n");
6280 WriteMap(0xFF); // fill map completely
6281 // now re-include selected moves
6282 j = 0; // count them
6283 for(i=0; i<exCnt; i++) if(exclusionHeader[e[i].mark] == '+')
6284 ExcludeOneMove(e[i].fr, e[i].ff, e[i].tr, e[i].tf, e[i].pc, '+'), j++;
6285 if(!j) ExcludeOneMove(0, 0, 0, 0, -1, '+'); // if no moves were selected, keep best
6288 ExcludeOneMove(0, 0, 0, 0, -1, '-'); // exclude it
6291 for(i=0; i<exCnt; i++) if(i == exCnt-1 || excluTab[i+1].mark > index) {
6292 char *p=exclusionHeader + excluTab[i].mark; // do trust header more than map (promotions!)
6293 ExcludeOneMove(e[i].fr, e[i].ff, e[i].tr, e[i].tf, e[i].pc, *p == '+' ? '-' : '+');
6300 DefaultPromoChoice (int white)
6303 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
6304 result = WhiteFerz; // no choice
6305 else if(gameInfo.variant == VariantSuicide || gameInfo.variant == VariantGiveaway)
6306 result= WhiteKing; // in Suicide Q is the last thing we want
6307 else if(gameInfo.variant == VariantSpartan)
6308 result = white ? WhiteQueen : WhiteAngel;
6309 else result = WhiteQueen;
6310 if(!white) result = WHITE_TO_BLACK result;
6314 static int autoQueen; // [HGM] oneclick
6317 HasPromotionChoice (int fromX, int fromY, int toX, int toY, char *promoChoice, int sweepSelect)
6319 /* [HGM] rewritten IsPromotion to only flag promotions that offer a choice */
6320 /* [HGM] add Shogi promotions */
6321 int promotionZoneSize=1, highestPromotingPiece = (int)WhitePawn;
6326 if(fromX < BOARD_LEFT || fromX >= BOARD_RGHT) return FALSE; // drop
6327 if(toX < BOARD_LEFT || toX >= BOARD_RGHT) return FALSE; // move into holdings
6329 if(gameMode == EditPosition || gameInfo.variant == VariantXiangqi || // no promotions
6330 !(fromX >=0 && fromY >= 0 && toX >= 0 && toY >= 0) ) // invalid move
6333 piece = boards[currentMove][fromY][fromX];
6334 if(gameInfo.variant == VariantShogi) {
6335 promotionZoneSize = BOARD_HEIGHT/3;
6336 highestPromotingPiece = (int)WhiteFerz;
6337 } else if(gameInfo.variant == VariantMakruk || gameInfo.variant == VariantGrand) {
6338 promotionZoneSize = 3;
6341 // Treat Lance as Pawn when it is not representing Amazon
6342 if(gameInfo.variant != VariantSuper) {
6343 if(piece == WhiteLance) piece = WhitePawn; else
6344 if(piece == BlackLance) piece = BlackPawn;
6347 // next weed out all moves that do not touch the promotion zone at all
6348 if((int)piece >= BlackPawn) {
6349 if(toY >= promotionZoneSize && fromY >= promotionZoneSize)
6351 highestPromotingPiece = WHITE_TO_BLACK highestPromotingPiece;
6353 if( toY < BOARD_HEIGHT - promotionZoneSize &&
6354 fromY < BOARD_HEIGHT - promotionZoneSize) return FALSE;
6357 if( (int)piece > highestPromotingPiece ) return FALSE; // non-promoting piece
6359 // weed out mandatory Shogi promotions
6360 if(gameInfo.variant == VariantShogi) {
6361 if(piece >= BlackPawn) {
6362 if(toY == 0 && piece == BlackPawn ||
6363 toY == 0 && piece == BlackQueen ||
6364 toY <= 1 && piece == BlackKnight) {
6369 if(toY == BOARD_HEIGHT-1 && piece == WhitePawn ||
6370 toY == BOARD_HEIGHT-1 && piece == WhiteQueen ||
6371 toY >= BOARD_HEIGHT-2 && piece == WhiteKnight) {
6378 // weed out obviously illegal Pawn moves
6379 if(appData.testLegality && (piece == WhitePawn || piece == BlackPawn) ) {
6380 if(toX > fromX+1 || toX < fromX-1) return FALSE; // wide
6381 if(piece == WhitePawn && toY != fromY+1) return FALSE; // deep
6382 if(piece == BlackPawn && toY != fromY-1) return FALSE; // deep
6383 if(fromX != toX && gameInfo.variant == VariantShogi) return FALSE;
6384 // note we are not allowed to test for valid (non-)capture, due to premove
6387 // we either have a choice what to promote to, or (in Shogi) whether to promote
6388 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk) {
6389 *promoChoice = PieceToChar(BlackFerz); // no choice
6392 // no sense asking what we must promote to if it is going to explode...
6393 if(gameInfo.variant == VariantAtomic && boards[currentMove][toY][toX] != EmptySquare) {
6394 *promoChoice = PieceToChar(BlackQueen); // Queen as good as any
6397 // give caller the default choice even if we will not make it
6398 *promoChoice = ToLower(PieceToChar(defaultPromoChoice));
6399 if(gameInfo.variant == VariantShogi) *promoChoice = (defaultPromoChoice == piece ? '=' : '+');
6400 if( sweepSelect && gameInfo.variant != VariantGreat
6401 && gameInfo.variant != VariantGrand
6402 && gameInfo.variant != VariantSuper) return FALSE;
6403 if(autoQueen) return FALSE; // predetermined
6405 // suppress promotion popup on illegal moves that are not premoves
6406 premove = gameMode == IcsPlayingWhite && !WhiteOnMove(currentMove) ||
6407 gameMode == IcsPlayingBlack && WhiteOnMove(currentMove);
6408 if(appData.testLegality && !premove) {
6409 moveType = LegalityTest(boards[currentMove], PosFlags(currentMove),
6410 fromY, fromX, toY, toX, gameInfo.variant == VariantShogi ? '+' : NULLCHAR);
6411 if(moveType != WhitePromotion && moveType != BlackPromotion)
6419 InPalace (int row, int column)
6420 { /* [HGM] for Xiangqi */
6421 if( (row < 3 || row > BOARD_HEIGHT-4) &&
6422 column < (BOARD_WIDTH + 4)/2 &&
6423 column > (BOARD_WIDTH - 5)/2 ) return TRUE;
6428 PieceForSquare (int x, int y)
6430 if (x < 0 || x >= BOARD_WIDTH || y < 0 || y >= BOARD_HEIGHT)
6433 return boards[currentMove][y][x];
6437 OKToStartUserMove (int x, int y)
6439 ChessSquare from_piece;
6442 if (matchMode) return FALSE;
6443 if (gameMode == EditPosition) return TRUE;
6445 if (x >= 0 && y >= 0)
6446 from_piece = boards[currentMove][y][x];
6448 from_piece = EmptySquare;
6450 if (from_piece == EmptySquare) return FALSE;
6452 white_piece = (int)from_piece >= (int)WhitePawn &&
6453 (int)from_piece < (int)BlackPawn; /* [HGM] can be > King! */
6457 case TwoMachinesPlay:
6465 case MachinePlaysWhite:
6466 case IcsPlayingBlack:
6467 if (appData.zippyPlay) return FALSE;
6469 DisplayMoveError(_("You are playing Black"));
6474 case MachinePlaysBlack:
6475 case IcsPlayingWhite:
6476 if (appData.zippyPlay) return FALSE;
6478 DisplayMoveError(_("You are playing White"));
6483 case PlayFromGameFile:
6484 if(!shiftKey || !appData.variations) return FALSE; // [HGM] allow starting variation in this mode
6486 if (!white_piece && WhiteOnMove(currentMove)) {
6487 DisplayMoveError(_("It is White's turn"));
6490 if (white_piece && !WhiteOnMove(currentMove)) {
6491 DisplayMoveError(_("It is Black's turn"));
6494 if (cmailMsgLoaded && (currentMove < cmailOldMove)) {
6495 /* Editing correspondence game history */
6496 /* Could disallow this or prompt for confirmation */
6501 case BeginningOfGame:
6502 if (appData.icsActive) return FALSE;
6503 if (!appData.noChessProgram) {
6505 DisplayMoveError(_("You are playing White"));
6512 if (!white_piece && WhiteOnMove(currentMove)) {
6513 DisplayMoveError(_("It is White's turn"));
6516 if (white_piece && !WhiteOnMove(currentMove)) {
6517 DisplayMoveError(_("It is Black's turn"));
6526 if (currentMove != forwardMostMove && gameMode != AnalyzeMode
6527 && gameMode != EditGame // [HGM] vari: treat as AnalyzeMode
6528 && gameMode != PlayFromGameFile // [HGM] as EditGame, with protected main line
6529 && gameMode != AnalyzeFile && gameMode != Training) {
6530 DisplayMoveError(_("Displayed position is not current"));
6537 OnlyMove (int *x, int *y, Boolean captures)
6539 DisambiguateClosure cl;
6540 if (appData.zippyPlay || !appData.testLegality) return FALSE;
6542 case MachinePlaysBlack:
6543 case IcsPlayingWhite:
6544 case BeginningOfGame:
6545 if(!WhiteOnMove(currentMove)) return FALSE;
6547 case MachinePlaysWhite:
6548 case IcsPlayingBlack:
6549 if(WhiteOnMove(currentMove)) return FALSE;
6556 cl.pieceIn = EmptySquare;
6561 cl.promoCharIn = NULLCHAR;
6562 Disambiguate(boards[currentMove], PosFlags(currentMove), &cl);
6563 if( cl.kind == NormalMove ||
6564 cl.kind == AmbiguousMove && captures && cl.captures == 1 ||
6565 cl.kind == WhitePromotion || cl.kind == BlackPromotion ||
6566 cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) {
6573 if(cl.kind != ImpossibleMove) return FALSE;
6574 cl.pieceIn = EmptySquare;
6579 cl.promoCharIn = NULLCHAR;
6580 Disambiguate(boards[currentMove], PosFlags(currentMove), &cl);
6581 if( cl.kind == NormalMove ||
6582 cl.kind == AmbiguousMove && captures && cl.captures == 1 ||
6583 cl.kind == WhitePromotion || cl.kind == BlackPromotion ||
6584 cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) {
6589 autoQueen = TRUE; // act as if autoQueen on when we click to-square
6595 FILE *lastLoadGameFP = NULL, *lastLoadPositionFP = NULL;
6596 int lastLoadGameNumber = 0, lastLoadPositionNumber = 0;
6597 int lastLoadGameUseList = FALSE;
6598 char lastLoadGameTitle[MSG_SIZ], lastLoadPositionTitle[MSG_SIZ];
6599 ChessMove lastLoadGameStart = EndOfFile;
6603 UserMoveEvent(int fromX, int fromY, int toX, int toY, int promoChar)
6607 int ff=fromX, rf=fromY, ft=toX, rt=toY;
6609 /* Check if the user is playing in turn. This is complicated because we
6610 let the user "pick up" a piece before it is his turn. So the piece he
6611 tried to pick up may have been captured by the time he puts it down!
6612 Therefore we use the color the user is supposed to be playing in this
6613 test, not the color of the piece that is currently on the starting
6614 square---except in EditGame mode, where the user is playing both
6615 sides; fortunately there the capture race can't happen. (It can
6616 now happen in IcsExamining mode, but that's just too bad. The user
6617 will get a somewhat confusing message in that case.)
6622 case TwoMachinesPlay:
6626 /* We switched into a game mode where moves are not accepted,
6627 perhaps while the mouse button was down. */
6630 case MachinePlaysWhite:
6631 /* User is moving for Black */
6632 if (WhiteOnMove(currentMove)) {
6633 DisplayMoveError(_("It is White's turn"));
6638 case MachinePlaysBlack:
6639 /* User is moving for White */
6640 if (!WhiteOnMove(currentMove)) {
6641 DisplayMoveError(_("It is Black's turn"));
6646 case PlayFromGameFile:
6647 if(!shiftKey ||!appData.variations) return; // [HGM] only variations
6650 case BeginningOfGame:
6653 if(fromY == DROP_RANK) break; // [HGM] drop moves (entered through move type-in) are automatically assigned to side-to-move
6654 if ((int) boards[currentMove][fromY][fromX] >= (int) BlackPawn &&
6655 (int) boards[currentMove][fromY][fromX] < (int) EmptySquare) {
6656 /* User is moving for Black */
6657 if (WhiteOnMove(currentMove)) {
6658 DisplayMoveError(_("It is White's turn"));
6662 /* User is moving for White */
6663 if (!WhiteOnMove(currentMove)) {
6664 DisplayMoveError(_("It is Black's turn"));
6670 case IcsPlayingBlack:
6671 /* User is moving for Black */
6672 if (WhiteOnMove(currentMove)) {
6673 if (!appData.premove) {
6674 DisplayMoveError(_("It is White's turn"));
6675 } else if (toX >= 0 && toY >= 0) {
6678 premoveFromX = fromX;
6679 premoveFromY = fromY;
6680 premovePromoChar = promoChar;
6682 if (appData.debugMode)
6683 fprintf(debugFP, "Got premove: fromX %d,"
6684 "fromY %d, toX %d, toY %d\n",
6685 fromX, fromY, toX, toY);
6691 case IcsPlayingWhite:
6692 /* User is moving for White */
6693 if (!WhiteOnMove(currentMove)) {
6694 if (!appData.premove) {
6695 DisplayMoveError(_("It is Black's turn"));
6696 } else if (toX >= 0 && toY >= 0) {
6699 premoveFromX = fromX;
6700 premoveFromY = fromY;
6701 premovePromoChar = promoChar;
6703 if (appData.debugMode)
6704 fprintf(debugFP, "Got premove: fromX %d,"
6705 "fromY %d, toX %d, toY %d\n",
6706 fromX, fromY, toX, toY);
6716 /* EditPosition, empty square, or different color piece;
6717 click-click move is possible */
6718 if (toX == -2 || toY == -2) {
6719 boards[0][fromY][fromX] = EmptySquare;
6720 DrawPosition(FALSE, boards[currentMove]);
6722 } else if (toX >= 0 && toY >= 0) {
6723 boards[0][toY][toX] = boards[0][fromY][fromX];
6724 if(fromX == BOARD_LEFT-2) { // handle 'moves' out of holdings
6725 if(boards[0][fromY][0] != EmptySquare) {
6726 if(boards[0][fromY][1]) boards[0][fromY][1]--;
6727 if(boards[0][fromY][1] == 0) boards[0][fromY][0] = EmptySquare;
6730 if(fromX == BOARD_RGHT+1) {
6731 if(boards[0][fromY][BOARD_WIDTH-1] != EmptySquare) {
6732 if(boards[0][fromY][BOARD_WIDTH-2]) boards[0][fromY][BOARD_WIDTH-2]--;
6733 if(boards[0][fromY][BOARD_WIDTH-2] == 0) boards[0][fromY][BOARD_WIDTH-1] = EmptySquare;
6736 boards[0][fromY][fromX] = gatingPiece;
6737 DrawPosition(FALSE, boards[currentMove]);
6743 if(toX < 0 || toY < 0) return;
6744 pup = boards[currentMove][toY][toX];
6746 /* [HGM] If move started in holdings, it means a drop. Convert to standard form */
6747 if( (fromX == BOARD_LEFT-2 || fromX == BOARD_RGHT+1) && fromY != DROP_RANK ) {
6748 if( pup != EmptySquare ) return;
6749 moveType = WhiteOnMove(currentMove) ? WhiteDrop : BlackDrop;
6750 if(appData.debugMode) fprintf(debugFP, "Drop move %d, curr=%d, x=%d,y=%d, p=%d\n",
6751 moveType, currentMove, fromX, fromY, boards[currentMove][fromY][fromX]);
6752 // holdings might not be sent yet in ICS play; we have to figure out which piece belongs here
6753 if(fromX == 0) fromY = BOARD_HEIGHT-1 - fromY; // black holdings upside-down
6754 fromX = fromX ? WhitePawn : BlackPawn; // first piece type in selected holdings
6755 while(PieceToChar(fromX) == '.' || PieceToNumber(fromX) != fromY && fromX != (int) EmptySquare) fromX++;
6759 /* [HGM] always test for legality, to get promotion info */
6760 moveType = LegalityTest(boards[currentMove], PosFlags(currentMove),
6761 fromY, fromX, toY, toX, promoChar);
6763 if(fromY == DROP_RANK && fromX == EmptySquare && (gameMode == AnalyzeMode || gameMode == EditGame)) moveType = NormalMove;
6765 /* [HGM] but possibly ignore an IllegalMove result */
6766 if (appData.testLegality) {
6767 if (moveType == IllegalMove || moveType == ImpossibleMove) {
6768 DisplayMoveError(_("Illegal move"));
6773 if(doubleClick && gameMode == AnalyzeMode) { // [HGM] exclude: move entered with double-click on from square is for exclusion, not playing
6774 if(ExcludeOneMove(fromY, fromX, toY, toX, promoChar, '*')) // toggle
6775 ClearPremoveHighlights(); // was included
6776 else ClearHighlights(), SetPremoveHighlights(ff, rf, ft, rt); // exclusion indicated by premove highlights
6780 FinishMove(moveType, fromX, fromY, toX, toY, promoChar);
6783 /* Common tail of UserMoveEvent and DropMenuEvent */
6785 FinishMove (ChessMove moveType, int fromX, int fromY, int toX, int toY, int promoChar)
6789 if((gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand) && promoChar != NULLCHAR) {
6790 // [HGM] superchess: suppress promotions to non-available piece (but P always allowed)
6791 int k = PieceToNumber(CharToPiece(ToUpper(promoChar)));
6792 if(WhiteOnMove(currentMove)) {
6793 if(!boards[currentMove][k][BOARD_WIDTH-2]) return 0;
6795 if(!boards[currentMove][BOARD_HEIGHT-1-k][1]) return 0;
6799 /* [HGM] <popupFix> kludge to avoid having to know the exact promotion
6800 move type in caller when we know the move is a legal promotion */
6801 if(moveType == NormalMove && promoChar)
6802 moveType = WhiteOnMove(currentMove) ? WhitePromotion : BlackPromotion;
6804 /* [HGM] <popupFix> The following if has been moved here from
6805 UserMoveEvent(). Because it seemed to belong here (why not allow
6806 piece drops in training games?), and because it can only be
6807 performed after it is known to what we promote. */
6808 if (gameMode == Training) {
6809 /* compare the move played on the board to the next move in the
6810 * game. If they match, display the move and the opponent's response.
6811 * If they don't match, display an error message.
6815 CopyBoard(testBoard, boards[currentMove]);
6816 ApplyMove(fromX, fromY, toX, toY, promoChar, testBoard);
6818 if (CompareBoards(testBoard, boards[currentMove+1])) {
6819 ForwardInner(currentMove+1);
6821 /* Autoplay the opponent's response.
6822 * if appData.animate was TRUE when Training mode was entered,
6823 * the response will be animated.
6825 saveAnimate = appData.animate;
6826 appData.animate = animateTraining;
6827 ForwardInner(currentMove+1);
6828 appData.animate = saveAnimate;
6830 /* check for the end of the game */
6831 if (currentMove >= forwardMostMove) {
6832 gameMode = PlayFromGameFile;
6834 SetTrainingModeOff();
6835 DisplayInformation(_("End of game"));
6838 DisplayError(_("Incorrect move"), 0);
6843 /* Ok, now we know that the move is good, so we can kill
6844 the previous line in Analysis Mode */
6845 if ((gameMode == AnalyzeMode || gameMode == EditGame || gameMode == PlayFromGameFile && appData.variations && shiftKey)
6846 && currentMove < forwardMostMove) {
6847 if(appData.variations && shiftKey) PushTail(currentMove, forwardMostMove); // [HGM] vari: save tail of game
6848 else forwardMostMove = currentMove;
6853 /* If we need the chess program but it's dead, restart it */
6854 ResurrectChessProgram();
6856 /* A user move restarts a paused game*/
6860 thinkOutput[0] = NULLCHAR;
6862 MakeMove(fromX, fromY, toX, toY, promoChar); /*updates forwardMostMove*/
6864 if(Adjudicate(NULL)) { // [HGM] adjudicate: take care of automatic game end
6865 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6869 if (gameMode == BeginningOfGame) {
6870 if (appData.noChessProgram) {
6871 gameMode = EditGame;
6875 gameMode = MachinePlaysBlack;
6878 snprintf(buf, MSG_SIZ, "%s %s %s", gameInfo.white, _("vs."), gameInfo.black);
6880 if (first.sendName) {
6881 snprintf(buf, MSG_SIZ,"name %s\n", gameInfo.white);
6882 SendToProgram(buf, &first);
6889 /* Relay move to ICS or chess engine */
6890 if (appData.icsActive) {
6891 if (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
6892 gameMode == IcsExamining) {
6893 if(userOfferedDraw && (signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
6894 SendToICS(ics_prefix); // [HGM] drawclaim: send caim and move on one line for FICS
6896 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
6898 // also send plain move, in case ICS does not understand atomic claims
6899 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
6903 if (first.sendTime && (gameMode == BeginningOfGame ||
6904 gameMode == MachinePlaysWhite ||
6905 gameMode == MachinePlaysBlack)) {
6906 SendTimeRemaining(&first, gameMode != MachinePlaysBlack);
6908 if (gameMode != EditGame && gameMode != PlayFromGameFile && gameMode != AnalyzeMode) {
6909 // [HGM] book: if program might be playing, let it use book
6910 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, FALSE);
6911 first.maybeThinking = TRUE;
6912 } else if(fromY == DROP_RANK && fromX == EmptySquare) {
6913 if(!first.useSetboard) SendToProgram("undo\n", &first); // kludge to change stm in engines that do not support setboard
6914 SendBoard(&first, currentMove+1);
6915 if(second.analyzing) {
6916 if(!second.useSetboard) SendToProgram("undo\n", &second);
6917 SendBoard(&second, currentMove+1);
6920 SendMoveToProgram(forwardMostMove-1, &first);
6921 if(second.analyzing) SendMoveToProgram(forwardMostMove-1, &second);
6923 if (currentMove == cmailOldMove + 1) {
6924 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
6928 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6932 if(appData.testLegality)
6933 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
6939 if (WhiteOnMove(currentMove)) {
6940 GameEnds(BlackWins, "Black mates", GE_PLAYER);
6942 GameEnds(WhiteWins, "White mates", GE_PLAYER);
6946 GameEnds(GameIsDrawn, "Stalemate", GE_PLAYER);
6951 case MachinePlaysBlack:
6952 case MachinePlaysWhite:
6953 /* disable certain menu options while machine is thinking */
6954 SetMachineThinkingEnables();
6961 userOfferedDraw = FALSE; // [HGM] drawclaim: after move made, and tested for claimable draw
6962 promoDefaultAltered = FALSE; // [HGM] fall back on default choice
6964 if(bookHit) { // [HGM] book: simulate book reply
6965 static char bookMove[MSG_SIZ]; // a bit generous?
6967 programStats.nodes = programStats.depth = programStats.time =
6968 programStats.score = programStats.got_only_move = 0;
6969 sprintf(programStats.movelist, "%s (xbook)", bookHit);
6971 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
6972 strcat(bookMove, bookHit);
6973 HandleMachineMove(bookMove, &first);
6979 Mark (Board board, int flags, ChessMove kind, int rf, int ff, int rt, int ft, VOIDSTAR closure)
6981 typedef char Markers[BOARD_RANKS][BOARD_FILES];
6982 Markers *m = (Markers *) closure;
6983 if(rf == fromY && ff == fromX)
6984 (*m)[rt][ft] = 1 + (board[rt][ft] != EmptySquare
6985 || kind == WhiteCapturesEnPassant
6986 || kind == BlackCapturesEnPassant);
6987 else if(flags & F_MANDATORY_CAPTURE && board[rt][ft] != EmptySquare) (*m)[rt][ft] = 3;
6991 MarkTargetSquares (int clear)
6994 if(clear) // no reason to ever suppress clearing
6995 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) marker[y][x] = 0;
6996 if(!appData.markers || !appData.highlightDragging || appData.icsActive && gameInfo.variant < VariantShogi ||
6997 !appData.testLegality || gameMode == EditPosition) return;
7000 GenLegal(boards[currentMove], PosFlags(currentMove), Mark, (void*) marker, EmptySquare);
7001 if(PosFlags(0) & F_MANDATORY_CAPTURE) {
7002 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) if(marker[y][x]>1) capt++;
7004 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) if(marker[y][x] == 1) marker[y][x] = 0;
7007 DrawPosition(FALSE, NULL);
7011 Explode (Board board, int fromX, int fromY, int toX, int toY)
7013 if(gameInfo.variant == VariantAtomic &&
7014 (board[toY][toX] != EmptySquare || // capture?
7015 toX != fromX && (board[fromY][fromX] == WhitePawn || // e.p. ?
7016 board[fromY][fromX] == BlackPawn )
7018 AnimateAtomicCapture(board, fromX, fromY, toX, toY);
7024 ChessSquare gatingPiece = EmptySquare; // exported to front-end, for dragging
7027 CanPromote (ChessSquare piece, int y)
7029 if(gameMode == EditPosition) return FALSE; // no promotions when editing position
7030 // some variants have fixed promotion piece, no promotion at all, or another selection mechanism
7031 if(gameInfo.variant == VariantShogi || gameInfo.variant == VariantXiangqi ||
7032 gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat ||
7033 gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier ||
7034 gameInfo.variant == VariantMakruk) return FALSE;
7035 return (piece == BlackPawn && y == 1 ||
7036 piece == WhitePawn && y == BOARD_HEIGHT-2 ||
7037 piece == BlackLance && y == 1 ||
7038 piece == WhiteLance && y == BOARD_HEIGHT-2 );
7042 LeftClick (ClickType clickType, int xPix, int yPix)
7045 Boolean saveAnimate;
7046 static int second = 0, promotionChoice = 0, clearFlag = 0, sweepSelecting = 0;
7047 char promoChoice = NULLCHAR;
7049 static TimeMark lastClickTime, prevClickTime;
7051 if(SeekGraphClick(clickType, xPix, yPix, 0)) return;
7053 prevClickTime = lastClickTime; GetTimeMark(&lastClickTime);
7055 if (clickType == Press) ErrorPopDown();
7057 x = EventToSquare(xPix, BOARD_WIDTH);
7058 y = EventToSquare(yPix, BOARD_HEIGHT);
7059 if (!flipView && y >= 0) {
7060 y = BOARD_HEIGHT - 1 - y;
7062 if (flipView && x >= 0) {
7063 x = BOARD_WIDTH - 1 - x;
7066 if(promoSweep != EmptySquare) { // up-click during sweep-select of promo-piece
7067 defaultPromoChoice = promoSweep;
7068 promoSweep = EmptySquare; // terminate sweep
7069 promoDefaultAltered = TRUE;
7070 if(!selectFlag && !sweepSelecting && (x != toX || y != toY)) x = fromX, y = fromY; // and fake up-click on same square if we were still selecting
7073 if(promotionChoice) { // we are waiting for a click to indicate promotion piece
7074 if(clickType == Release) return; // ignore upclick of click-click destination
7075 promotionChoice = FALSE; // only one chance: if click not OK it is interpreted as cancel
7076 if(appData.debugMode) fprintf(debugFP, "promotion click, x=%d, y=%d\n", x, y);
7077 if(gameInfo.holdingsWidth &&
7078 (WhiteOnMove(currentMove)
7079 ? x == BOARD_WIDTH-1 && y < gameInfo.holdingsSize && y >= 0
7080 : x == 0 && y >= BOARD_HEIGHT - gameInfo.holdingsSize && y < BOARD_HEIGHT) ) {
7081 // click in right holdings, for determining promotion piece
7082 ChessSquare p = boards[currentMove][y][x];
7083 if(appData.debugMode) fprintf(debugFP, "square contains %d\n", (int)p);
7084 if(p == WhitePawn || p == BlackPawn) p = EmptySquare; // [HGM] Pawns could be valid as deferral
7085 if(p != EmptySquare || gameInfo.variant == VariantGrand && toY != 0 && toY != BOARD_HEIGHT-1) { // [HGM] grand: empty square means defer
7086 FinishMove(NormalMove, fromX, fromY, toX, toY, p==EmptySquare ? NULLCHAR : ToLower(PieceToChar(p)));
7091 DrawPosition(FALSE, boards[currentMove]);
7095 /* [HGM] holdings: next 5 lines: ignore all clicks between board and holdings */
7096 if(clickType == Press
7097 && ( x == BOARD_LEFT-1 || x == BOARD_RGHT
7098 || x == BOARD_LEFT-2 && y < BOARD_HEIGHT-gameInfo.holdingsSize
7099 || x == BOARD_RGHT+1 && y >= gameInfo.holdingsSize) )
7102 if(gotPremove && x == premoveFromX && y == premoveFromY && clickType == Release) {
7103 // could be static click on premove from-square: abort premove
7105 ClearPremoveHighlights();
7108 if(clickType == Press && fromX == x && fromY == y && promoDefaultAltered && SubtractTimeMarks(&lastClickTime, &prevClickTime) >= 200)
7109 fromX = fromY = -1; // second click on piece after altering default promo piece treated as first click
7111 if(!promoDefaultAltered) { // determine default promotion piece, based on the side the user is moving for
7112 int side = (gameMode == IcsPlayingWhite || gameMode == MachinePlaysBlack ||
7113 gameMode != MachinePlaysWhite && gameMode != IcsPlayingBlack && WhiteOnMove(currentMove));
7114 defaultPromoChoice = DefaultPromoChoice(side);
7117 autoQueen = appData.alwaysPromoteToQueen;
7121 gatingPiece = EmptySquare;
7122 if (clickType != Press) {
7123 if(dragging) { // [HGM] from-square must have been reset due to game end since last press
7124 DragPieceEnd(xPix, yPix); dragging = 0;
7125 DrawPosition(FALSE, NULL);
7129 doubleClick = FALSE;
7130 if(gameMode == AnalyzeMode && pausing && first.excludeMoves) { // use pause state to exclude moves
7131 doubleClick = TRUE; gatingPiece = boards[currentMove][y][x];
7133 fromX = x; fromY = y; toX = toY = -1;
7134 if(!appData.oneClick || !OnlyMove(&x, &y, FALSE) ||
7135 // even if only move, we treat as normal when this would trigger a promotion popup, to allow sweep selection
7136 appData.sweepSelect && CanPromote(boards[currentMove][fromY][fromX], fromY) && originalY != y) {
7138 if (OKToStartUserMove(fromX, fromY)) {
7140 MarkTargetSquares(0);
7141 if(gameMode == EditPosition && controlKey) gatingPiece = boards[currentMove][fromY][fromX];
7142 DragPieceBegin(xPix, yPix, FALSE); dragging = 1;
7143 if(appData.sweepSelect && CanPromote(piece = boards[currentMove][fromY][fromX], fromY)) {
7144 promoSweep = defaultPromoChoice;
7145 selectFlag = 0; lastX = xPix; lastY = yPix;
7146 Sweep(0); // Pawn that is going to promote: preview promotion piece
7147 DisplayMessage("", _("Pull pawn backwards to under-promote"));
7149 if (appData.highlightDragging) {
7150 SetHighlights(fromX, fromY, -1, -1);
7154 } else fromX = fromY = -1;
7160 if (clickType == Press && gameMode != EditPosition) {
7165 // ignore off-board to clicks
7166 if(y < 0 || x < 0) return;
7168 /* Check if clicking again on the same color piece */
7169 fromP = boards[currentMove][fromY][fromX];
7170 toP = boards[currentMove][y][x];
7171 frc = gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom || gameInfo.variant == VariantSChess;
7172 if ((WhitePawn <= fromP && fromP <= WhiteKing &&
7173 WhitePawn <= toP && toP <= WhiteKing &&
7174 !(fromP == WhiteKing && toP == WhiteRook && frc) &&
7175 !(fromP == WhiteRook && toP == WhiteKing && frc)) ||
7176 (BlackPawn <= fromP && fromP <= BlackKing &&
7177 BlackPawn <= toP && toP <= BlackKing &&
7178 !(fromP == BlackRook && toP == BlackKing && frc) && // allow also RxK as FRC castling
7179 !(fromP == BlackKing && toP == BlackRook && frc))) {
7180 /* Clicked again on same color piece -- changed his mind */
7181 second = (x == fromX && y == fromY);
7182 if(second && gameMode == AnalyzeMode && SubtractTimeMarks(&lastClickTime, &prevClickTime) < 200) {
7183 second = FALSE; // first double-click rather than scond click
7184 doubleClick = first.excludeMoves; // used by UserMoveEvent to recognize exclude moves
7186 promoDefaultAltered = FALSE;
7187 MarkTargetSquares(1);
7188 if(!second || appData.oneClick && !OnlyMove(&x, &y, TRUE)) {
7189 if (appData.highlightDragging) {
7190 SetHighlights(x, y, -1, -1);
7194 if (OKToStartUserMove(x, y)) {
7195 if(gameInfo.variant == VariantSChess && // S-Chess: back-rank piece selected after holdings means gating
7196 (fromX == BOARD_LEFT-2 || fromX == BOARD_RGHT+1) &&
7197 y == (toP < BlackPawn ? 0 : BOARD_HEIGHT-1))
7198 gatingPiece = boards[currentMove][fromY][fromX];
7199 else gatingPiece = doubleClick ? fromP : EmptySquare;
7201 fromY = y; dragging = 1;
7202 MarkTargetSquares(0);
7203 DragPieceBegin(xPix, yPix, FALSE);
7204 if(appData.sweepSelect && CanPromote(piece = boards[currentMove][y][x], y)) {
7205 promoSweep = defaultPromoChoice;
7206 selectFlag = 0; lastX = xPix; lastY = yPix;
7207 Sweep(0); // Pawn that is going to promote: preview promotion piece
7211 if(x == fromX && y == fromY) return; // if OnlyMove altered (x,y) we go on
7214 // ignore clicks on holdings
7215 if(x < BOARD_LEFT || x >= BOARD_RGHT) return;
7218 if (clickType == Release && x == fromX && y == fromY) {
7219 DragPieceEnd(xPix, yPix); dragging = 0;
7221 // a deferred attempt to click-click move an empty square on top of a piece
7222 boards[currentMove][y][x] = EmptySquare;
7224 DrawPosition(FALSE, boards[currentMove]);
7225 fromX = fromY = -1; clearFlag = 0;
7228 if (appData.animateDragging) {
7229 /* Undo animation damage if any */
7230 DrawPosition(FALSE, NULL);
7232 if (second || sweepSelecting) {
7233 /* Second up/down in same square; just abort move */
7234 if(sweepSelecting) DrawPosition(FALSE, boards[currentMove]);
7235 second = sweepSelecting = 0;
7237 gatingPiece = EmptySquare;
7240 ClearPremoveHighlights();
7242 /* First upclick in same square; start click-click mode */
7243 SetHighlights(x, y, -1, -1);
7250 /* we now have a different from- and (possibly off-board) to-square */
7251 /* Completed move */
7252 if(!sweepSelecting) {
7255 } else sweepSelecting = 0; // this must be the up-click corresponding to the down-click that started the sweep
7257 saveAnimate = appData.animate;
7258 if (clickType == Press) {
7259 if(gameMode == EditPosition && boards[currentMove][fromY][fromX] == EmptySquare) {
7260 // must be Edit Position mode with empty-square selected
7261 fromX = x; fromY = y; DragPieceBegin(xPix, yPix, FALSE); dragging = 1; // consider this a new attempt to drag
7262 if(x >= BOARD_LEFT && x < BOARD_RGHT) clearFlag = 1; // and defer click-click move of empty-square to up-click
7265 if(HasPromotionChoice(fromX, fromY, toX, toY, &promoChoice, FALSE)) {
7266 if(appData.sweepSelect) {
7267 ChessSquare piece = boards[currentMove][fromY][fromX];
7268 promoSweep = defaultPromoChoice;
7269 if(PieceToChar(PROMOTED piece) == '+') promoSweep = PROMOTED piece;
7270 selectFlag = 0; lastX = xPix; lastY = yPix;
7271 Sweep(0); // Pawn that is going to promote: preview promotion piece
7273 DisplayMessage("", _("Pull pawn backwards to under-promote"));
7274 MarkTargetSquares(1);
7276 return; // promo popup appears on up-click
7278 /* Finish clickclick move */
7279 if (appData.animate || appData.highlightLastMove) {
7280 SetHighlights(fromX, fromY, toX, toY);
7286 // [HGM] this must be done after the move is made, as with arrow it could lead to a board redraw with piece still on from square
7287 /* Finish drag move */
7288 if (appData.highlightLastMove) {
7289 SetHighlights(fromX, fromY, toX, toY);
7294 DragPieceEnd(xPix, yPix); dragging = 0;
7295 /* Don't animate move and drag both */
7296 appData.animate = FALSE;
7299 // moves into holding are invalid for now (except in EditPosition, adapting to-square)
7300 if(x >= 0 && x < BOARD_LEFT || x >= BOARD_RGHT) {
7301 ChessSquare piece = boards[currentMove][fromY][fromX];
7302 if(gameMode == EditPosition && piece != EmptySquare &&
7303 fromX >= BOARD_LEFT && fromX < BOARD_RGHT) {
7306 if(x == BOARD_LEFT-2 && piece >= BlackPawn) {
7307 n = PieceToNumber(piece - (int)BlackPawn);
7308 if(n >= gameInfo.holdingsSize) { n = 0; piece = BlackPawn; }
7309 boards[currentMove][BOARD_HEIGHT-1 - n][0] = piece;
7310 boards[currentMove][BOARD_HEIGHT-1 - n][1]++;
7312 if(x == BOARD_RGHT+1 && piece < BlackPawn) {
7313 n = PieceToNumber(piece);
7314 if(n >= gameInfo.holdingsSize) { n = 0; piece = WhitePawn; }
7315 boards[currentMove][n][BOARD_WIDTH-1] = piece;
7316 boards[currentMove][n][BOARD_WIDTH-2]++;
7318 boards[currentMove][fromY][fromX] = EmptySquare;
7322 MarkTargetSquares(1);
7323 DrawPosition(TRUE, boards[currentMove]);
7327 // off-board moves should not be highlighted
7328 if(x < 0 || y < 0) ClearHighlights();
7330 if(gatingPiece != EmptySquare && gameInfo.variant == VariantSChess) promoChoice = ToLower(PieceToChar(gatingPiece));
7332 if (HasPromotionChoice(fromX, fromY, toX, toY, &promoChoice, appData.sweepSelect)) {
7333 SetHighlights(fromX, fromY, toX, toY);
7334 MarkTargetSquares(1);
7335 if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand) {
7336 // [HGM] super: promotion to captured piece selected from holdings
7337 ChessSquare p = boards[currentMove][fromY][fromX], q = boards[currentMove][toY][toX];
7338 promotionChoice = TRUE;
7339 // kludge follows to temporarily execute move on display, without promoting yet
7340 boards[currentMove][fromY][fromX] = EmptySquare; // move Pawn to 8th rank
7341 boards[currentMove][toY][toX] = p;
7342 DrawPosition(FALSE, boards[currentMove]);
7343 boards[currentMove][fromY][fromX] = p; // take back, but display stays
7344 boards[currentMove][toY][toX] = q;
7345 DisplayMessage("Click in holdings to choose piece", "");
7350 int oldMove = currentMove;
7351 UserMoveEvent(fromX, fromY, toX, toY, promoChoice);
7352 if (!appData.highlightLastMove || gotPremove) ClearHighlights();
7353 if (gotPremove) SetPremoveHighlights(fromX, fromY, toX, toY);
7354 if(saveAnimate && !appData.animate && currentMove != oldMove && // drag-move was performed
7355 Explode(boards[currentMove-1], fromX, fromY, toX, toY))
7356 DrawPosition(TRUE, boards[currentMove]);
7357 MarkTargetSquares(1);
7360 appData.animate = saveAnimate;
7361 if (appData.animate || appData.animateDragging) {
7362 /* Undo animation damage if needed */
7363 DrawPosition(FALSE, NULL);
7368 RightClick (ClickType action, int x, int y, int *fromX, int *fromY)
7369 { // front-end-free part taken out of PieceMenuPopup
7370 int whichMenu; int xSqr, ySqr;
7372 if(seekGraphUp) { // [HGM] seekgraph
7373 if(action == Press) SeekGraphClick(Press, x, y, 2); // 2 indicates right-click: no pop-down on miss
7374 if(action == Release) SeekGraphClick(Release, x, y, 2); // and no challenge on hit
7378 if((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)
7379 && !appData.zippyPlay && appData.bgObserve) { // [HGM] bughouse: show background game
7380 if(!partnerBoardValid) return -2; // suppress display of uninitialized boards
7381 if( appData.dualBoard) return -2; // [HGM] dual: is already displayed
7382 if(action == Press) {
7383 originalFlip = flipView;
7384 flipView = !flipView; // temporarily flip board to see game from partners perspective
7385 DrawPosition(TRUE, partnerBoard);
7386 DisplayMessage(partnerStatus, "");
7388 } else if(action == Release) {
7389 flipView = originalFlip;
7390 DrawPosition(TRUE, boards[currentMove]);
7396 xSqr = EventToSquare(x, BOARD_WIDTH);
7397 ySqr = EventToSquare(y, BOARD_HEIGHT);
7398 if (action == Release) {
7399 if(pieceSweep != EmptySquare) {
7400 EditPositionMenuEvent(pieceSweep, toX, toY);
7401 pieceSweep = EmptySquare;
7402 } else UnLoadPV(); // [HGM] pv
7404 if (action != Press) return -2; // return code to be ignored
7407 if(xSqr < BOARD_LEFT || xSqr >= BOARD_RGHT) return -1;
7409 if (xSqr == BOARD_LEFT-1 || xSqr == BOARD_RGHT) return -1;
7410 if (xSqr < 0 || ySqr < 0) return -1;
7411 if(appData.pieceMenu) { whichMenu = 0; break; } // edit-position menu
7412 pieceSweep = shiftKey ? BlackPawn : WhitePawn; // [HGM] sweep: prepare selecting piece by mouse sweep
7413 toX = xSqr; toY = ySqr; lastX = x, lastY = y;
7414 if(flipView) toX = BOARD_WIDTH - 1 - toX; else toY = BOARD_HEIGHT - 1 - toY;
7418 if(!appData.icsEngineAnalyze) return -1;
7419 case IcsPlayingWhite:
7420 case IcsPlayingBlack:
7421 if(!appData.zippyPlay) goto noZip;
7424 case MachinePlaysWhite:
7425 case MachinePlaysBlack:
7426 case TwoMachinesPlay: // [HGM] pv: use for showing PV
7427 if (!appData.dropMenu) {
7429 return 2; // flag front-end to grab mouse events
7431 if(gameMode == TwoMachinesPlay || gameMode == AnalyzeMode ||
7432 gameMode == AnalyzeFile || gameMode == IcsObserving) return -1;
7435 if (xSqr < 0 || ySqr < 0) return -1;
7436 if (!appData.dropMenu || appData.testLegality &&
7437 gameInfo.variant != VariantBughouse &&
7438 gameInfo.variant != VariantCrazyhouse) return -1;
7439 whichMenu = 1; // drop menu
7445 if (((*fromX = xSqr) < 0) ||
7446 ((*fromY = ySqr) < 0)) {
7447 *fromX = *fromY = -1;
7451 *fromX = BOARD_WIDTH - 1 - *fromX;
7453 *fromY = BOARD_HEIGHT - 1 - *fromY;
7459 SendProgramStatsToFrontend (ChessProgramState * cps, ChessProgramStats * cpstats)
7461 // char * hint = lastHint;
7462 FrontEndProgramStats stats;
7464 stats.which = cps == &first ? 0 : 1;
7465 stats.depth = cpstats->depth;
7466 stats.nodes = cpstats->nodes;
7467 stats.score = cpstats->score;
7468 stats.time = cpstats->time;
7469 stats.pv = cpstats->movelist;
7470 stats.hint = lastHint;
7471 stats.an_move_index = 0;
7472 stats.an_move_count = 0;
7474 if( gameMode == AnalyzeMode || gameMode == AnalyzeFile ) {
7475 stats.hint = cpstats->move_name;
7476 stats.an_move_index = cpstats->nr_moves - cpstats->moves_left;
7477 stats.an_move_count = cpstats->nr_moves;
7480 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
7482 SetProgramStats( &stats );
7486 ClearEngineOutputPane (int which)
7488 static FrontEndProgramStats dummyStats;
7489 dummyStats.which = which;
7490 dummyStats.pv = "#";
7491 SetProgramStats( &dummyStats );
7494 #define MAXPLAYERS 500
7497 TourneyStandings (int display)
7499 int i, w, b, color, wScore, bScore, dummy, nr=0, nPlayers=0;
7500 int score[MAXPLAYERS], ranking[MAXPLAYERS], points[MAXPLAYERS], games[MAXPLAYERS];
7501 char result, *p, *names[MAXPLAYERS];
7503 if(appData.tourneyType < 0 && !strchr(appData.results, '*'))
7504 return strdup(_("Swiss tourney finished")); // standings of Swiss yet TODO
7505 names[0] = p = strdup(appData.participants);
7506 while(p = strchr(p, '\n')) *p++ = NULLCHAR, names[++nPlayers] = p; // count participants
7508 for(i=0; i<nPlayers; i++) score[i] = games[i] = 0;
7510 while(result = appData.results[nr]) {
7511 color = Pairing(nr, nPlayers, &w, &b, &dummy);
7512 if(!(color ^ matchGame & 1)) { dummy = w; w = b; b = dummy; }
7513 wScore = bScore = 0;
7515 case '+': wScore = 2; break;
7516 case '-': bScore = 2; break;
7517 case '=': wScore = bScore = 1; break;
7519 case '*': return strdup("busy"); // tourney not finished
7527 if(appData.tourneyType > 0) nPlayers = appData.tourneyType; // in gauntlet, list only gauntlet engine(s)
7528 for(w=0; w<nPlayers; w++) {
7530 for(i=0; i<nPlayers; i++) if(score[i] > bScore) bScore = score[i], b = i;
7531 ranking[w] = b; points[w] = bScore; score[b] = -2;
7533 p = malloc(nPlayers*34+1);
7534 for(w=0; w<nPlayers && w<display; w++)
7535 sprintf(p+34*w, "%2d. %5.1f/%-3d %-19.19s\n", w+1, points[w]/2., games[ranking[w]], names[ranking[w]]);
7541 Count (Board board, int pCnt[], int *nW, int *nB, int *wStale, int *bStale, int *bishopColor)
7542 { // count all piece types
7544 *nB = *nW = *wStale = *bStale = *bishopColor = 0;
7545 for(p=WhitePawn; p<=EmptySquare; p++) pCnt[p] = 0;
7546 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
7549 if(p == WhitePawn && r == BOARD_HEIGHT-1) (*wStale)++; else
7550 if(p == BlackPawn && r == 0) (*bStale)++; // count last-Rank Pawns (XQ) separately
7551 if(p <= WhiteKing) (*nW)++; else if(p <= BlackKing) (*nB)++;
7552 if(p == WhiteBishop || p == WhiteFerz || p == WhiteAlfil ||
7553 p == BlackBishop || p == BlackFerz || p == BlackAlfil )
7554 *bishopColor |= 1 << ((f^r)&1); // track square color of color-bound pieces
7559 SufficientDefence (int pCnt[], int side, int nMine, int nHis)
7561 int myPawns = pCnt[WhitePawn+side]; // my total Pawn count;
7562 int majorDefense = pCnt[BlackRook-side] + pCnt[BlackCannon-side] + pCnt[BlackKnight-side];
7564 nMine -= pCnt[WhiteFerz+side] + pCnt[WhiteAlfil+side]; // discount defenders
7565 if(nMine - myPawns > 2) return FALSE; // no trivial draws with more than 1 major
7566 if(myPawns == 2 && nMine == 3) // KPP
7567 return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] >= 3;
7568 if(myPawns == 1 && nMine == 2) // KP
7569 return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] + pCnt[BlackPawn-side] >= 1;
7570 if(myPawns == 1 && nMine == 3 && pCnt[WhiteKnight+side]) // KHP
7571 return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side]*2 >= 5;
7572 if(myPawns) return FALSE;
7573 if(pCnt[WhiteRook+side])
7574 return pCnt[BlackRook-side] ||
7575 pCnt[BlackCannon-side] && (pCnt[BlackFerz-side] >= 2 || pCnt[BlackAlfil-side] >= 2) ||
7576 pCnt[BlackKnight-side] && pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] > 2 ||
7577 pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] >= 4;
7578 if(pCnt[WhiteCannon+side]) {
7579 if(pCnt[WhiteFerz+side] + myPawns == 0) return TRUE; // Cannon needs platform
7580 return majorDefense || pCnt[BlackAlfil-side] >= 2;
7582 if(pCnt[WhiteKnight+side])
7583 return majorDefense || pCnt[BlackFerz-side] >= 2 || pCnt[BlackAlfil-side] + pCnt[BlackPawn-side] >= 1;
7588 MatingPotential (int pCnt[], int side, int nMine, int nHis, int stale, int bisColor)
7590 VariantClass v = gameInfo.variant;
7592 if(v == VariantShogi || v == VariantCrazyhouse || v == VariantBughouse) return TRUE; // drop games always winnable
7593 if(v == VariantShatranj) return TRUE; // always winnable through baring
7594 if(v == VariantLosers || v == VariantSuicide || v == VariantGiveaway) return TRUE;
7595 if(v == Variant3Check || v == VariantAtomic) return nMine > 1; // can win through checking / exploding King
7597 if(v == VariantXiangqi) {
7598 int majors = 5*pCnt[BlackKnight-side] + 7*pCnt[BlackCannon-side] + 7*pCnt[BlackRook-side];
7600 nMine -= pCnt[WhiteFerz+side] + pCnt[WhiteAlfil+side] + stale; // discount defensive pieces and back-rank Pawns
7601 if(nMine + stale == 1) return (pCnt[BlackFerz-side] > 1 && pCnt[BlackKnight-side] > 0); // bare K can stalemate KHAA (!)
7602 if(nMine > 2) return TRUE; // if we don't have P, H or R, we must have CC
7603 if(nMine == 2 && pCnt[WhiteCannon+side] == 0) return TRUE; // We have at least one P, H or R
7604 // if we get here, we must have KC... or KP..., possibly with additional A, E or last-rank P
7605 if(stale) // we have at least one last-rank P plus perhaps C
7606 return majors // KPKX
7607 || pCnt[BlackFerz-side] && pCnt[BlackFerz-side] + pCnt[WhiteCannon+side] + stale > 2; // KPKAA, KPPKA and KCPKA
7609 return pCnt[WhiteFerz+side] // KCAK
7610 || pCnt[WhiteAlfil+side] && pCnt[BlackRook-side] + pCnt[BlackCannon-side] + pCnt[BlackFerz-side] // KCEKA, KCEKX (X!=H)
7611 || majors + (12*pCnt[BlackFerz-side] | 6*pCnt[BlackAlfil-side]) > 16; // KCKAA, KCKAX, KCKEEX, KCKEXX (XX!=HH), KCKXXX
7612 // TO DO: cases wih an unpromoted f-Pawn acting as platform for an opponent Cannon
7614 } else if(pCnt[WhiteKing] == 1 && pCnt[BlackKing] == 1) { // other variants with orthodox Kings
7615 int nBishops = pCnt[WhiteBishop+side] + pCnt[WhiteFerz+side];
7617 if(nMine == 1) return FALSE; // bare King
7618 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
7619 nMine += (nBishops > 0) - nBishops; // By now all Bishops (and Ferz) on like-colored squares, so count as one
7620 if(nMine > 2 && nMine != pCnt[WhiteAlfil+side] + 1) return TRUE; // At least two pieces, not all Alfils
7621 // by now we have King + 1 piece (or multiple Bishops on the same color)
7622 if(pCnt[WhiteKnight+side])
7623 return (pCnt[BlackKnight-side] + pCnt[BlackBishop-side] + pCnt[BlackMan-side] +
7624 pCnt[BlackWazir-side] + pCnt[BlackSilver-side] + bisColor // KNKN, KNKB, KNKF, KNKE, KNKW, KNKM, KNKS
7625 || nHis > 3); // be sure to cover suffocation mates in corner (e.g. KNKQCA)
7627 return (pCnt[BlackKnight-side]); // KBKN, KFKN
7628 if(pCnt[WhiteAlfil+side])
7629 return (nHis > 2); // Alfils can in general not reach a corner square, but there might be edge (suffocation) mates
7630 if(pCnt[WhiteWazir+side])
7631 return (pCnt[BlackKnight-side] + pCnt[BlackWazir-side] + pCnt[BlackAlfil-side]); // KWKN, KWKW, KWKE
7638 CompareWithRights (Board b1, Board b2)
7641 if(!CompareBoards(b1, b2)) return FALSE;
7642 if(b1[EP_STATUS] != b2[EP_STATUS]) return FALSE;
7643 /* compare castling rights */
7644 if( b1[CASTLING][2] != b2[CASTLING][2] && (b2[CASTLING][0] != NoRights || b2[CASTLING][1] != NoRights) )
7645 rights++; /* King lost rights, while rook still had them */
7646 if( b1[CASTLING][2] != NoRights ) { /* king has rights */
7647 if( b1[CASTLING][0] != b2[CASTLING][0] || b1[CASTLING][1] != b2[CASTLING][1] )
7648 rights++; /* but at least one rook lost them */
7650 if( b1[CASTLING][5] != b1[CASTLING][5] && (b2[CASTLING][3] != NoRights || b2[CASTLING][4] != NoRights) )
7652 if( b1[CASTLING][5] != NoRights ) {
7653 if( b1[CASTLING][3] != b2[CASTLING][3] || b1[CASTLING][4] != b2[CASTLING][4] )
7660 Adjudicate (ChessProgramState *cps)
7661 { // [HGM] some adjudications useful with buggy engines
7662 // [HGM] adjudicate: made into separate routine, which now can be called after every move
7663 // In any case it determnes if the game is a claimable draw (filling in EP_STATUS).
7664 // Actually ending the game is now based on the additional internal condition canAdjudicate.
7665 // Only when the game is ended, and the opponent is a computer, this opponent gets the move relayed.
7666 int k, count = 0; static int bare = 1;
7667 ChessProgramState *engineOpponent = (gameMode == TwoMachinesPlay ? cps->other : (cps ? NULL : &first));
7668 Boolean canAdjudicate = !appData.icsActive;
7670 // most tests only when we understand the game, i.e. legality-checking on
7671 if( appData.testLegality )
7672 { /* [HGM] Some more adjudications for obstinate engines */
7673 int nrW, nrB, bishopColor, staleW, staleB, nr[EmptySquare+1], i;
7674 static int moveCount = 6;
7676 char *reason = NULL;
7678 /* Count what is on board. */
7679 Count(boards[forwardMostMove], nr, &nrW, &nrB, &staleW, &staleB, &bishopColor);
7681 /* Some material-based adjudications that have to be made before stalemate test */
7682 if(gameInfo.variant == VariantAtomic && nr[WhiteKing] + nr[BlackKing] < 2) {
7683 // [HGM] atomic: stm must have lost his King on previous move, as destroying own K is illegal
7684 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE; // make claimable as if stm is checkmated
7685 if(canAdjudicate && appData.checkMates) {
7687 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets move
7688 GameEnds( WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins,
7689 "Xboard adjudication: King destroyed", GE_XBOARD );
7694 /* Bare King in Shatranj (loses) or Losers (wins) */
7695 if( nrW == 1 || nrB == 1) {
7696 if( gameInfo.variant == VariantLosers) { // [HGM] losers: bare King wins (stm must have it first)
7697 boards[forwardMostMove][EP_STATUS] = EP_WINS; // mark as win, so it becomes claimable
7698 if(canAdjudicate && appData.checkMates) {
7700 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets to see move
7701 GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
7702 "Xboard adjudication: Bare king", GE_XBOARD );
7706 if( gameInfo.variant == VariantShatranj && --bare < 0)
7708 boards[forwardMostMove][EP_STATUS] = EP_WINS; // make claimable as win for stm
7709 if(canAdjudicate && appData.checkMates) {
7710 /* but only adjudicate if adjudication enabled */
7712 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets move
7713 GameEnds( nrW > 1 ? WhiteWins : nrB > 1 ? BlackWins : GameIsDrawn,
7714 "Xboard adjudication: Bare king", GE_XBOARD );
7721 // don't wait for engine to announce game end if we can judge ourselves
7722 switch (MateTest(boards[forwardMostMove], PosFlags(forwardMostMove)) ) {
7724 if(gameInfo.variant == Variant3Check) { // [HGM] 3check: when in check, test if 3rd time
7725 int i, checkCnt = 0; // (should really be done by making nr of checks part of game state)
7726 for(i=forwardMostMove-2; i>=backwardMostMove; i-=2) {
7727 if(MateTest(boards[i], PosFlags(i)) == MT_CHECK)
7730 reason = "Xboard adjudication: 3rd check";
7731 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE;
7741 reason = "Xboard adjudication: Stalemate";
7742 if((signed char)boards[forwardMostMove][EP_STATUS] != EP_CHECKMATE) { // [HGM] don't touch win through baring or K-capt
7743 boards[forwardMostMove][EP_STATUS] = EP_STALEMATE; // default result for stalemate is draw
7744 if(gameInfo.variant == VariantLosers || gameInfo.variant == VariantGiveaway) // [HGM] losers:
7745 boards[forwardMostMove][EP_STATUS] = EP_WINS; // in these variants stalemated is always a win
7746 else if(gameInfo.variant == VariantSuicide) // in suicide it depends
7747 boards[forwardMostMove][EP_STATUS] = nrW == nrB ? EP_STALEMATE :
7748 ((nrW < nrB) != WhiteOnMove(forwardMostMove) ?
7749 EP_CHECKMATE : EP_WINS);
7750 else if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantXiangqi)
7751 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE; // and in these variants being stalemated loses
7755 reason = "Xboard adjudication: Checkmate";
7756 boards[forwardMostMove][EP_STATUS] = (gameInfo.variant == VariantLosers ? EP_WINS : EP_CHECKMATE);
7760 switch(i = (signed char)boards[forwardMostMove][EP_STATUS]) {
7762 result = GameIsDrawn; break;
7764 result = WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins; break;
7766 result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins; break;
7770 if(canAdjudicate && appData.checkMates && result) { // [HGM] mates: adjudicate finished games if requested
7772 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7773 GameEnds( result, reason, GE_XBOARD );
7777 /* Next absolutely insufficient mating material. */
7778 if(!MatingPotential(nr, WhitePawn, nrW, nrB, staleW, bishopColor) &&
7779 !MatingPotential(nr, BlackPawn, nrB, nrW, staleB, bishopColor))
7780 { /* includes KBK, KNK, KK of KBKB with like Bishops */
7782 /* always flag draws, for judging claims */
7783 boards[forwardMostMove][EP_STATUS] = EP_INSUF_DRAW;
7785 if(canAdjudicate && appData.materialDraws) {
7786 /* but only adjudicate them if adjudication enabled */
7787 if(engineOpponent) {
7788 SendToProgram("force\n", engineOpponent); // suppress reply
7789 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see last move */
7791 GameEnds( GameIsDrawn, "Xboard adjudication: Insufficient mating material", GE_XBOARD );
7796 /* Then some trivial draws (only adjudicate, cannot be claimed) */
7797 if(gameInfo.variant == VariantXiangqi ?
7798 SufficientDefence(nr, WhitePawn, nrW, nrB) && SufficientDefence(nr, BlackPawn, nrB, nrW)
7800 ( nr[WhiteRook] == 1 && nr[BlackRook] == 1 /* KRKR */
7801 || nr[WhiteQueen] && nr[BlackQueen]==1 /* KQKQ */
7802 || nr[WhiteKnight]==2 || nr[BlackKnight]==2 /* KNNK */
7803 || nr[WhiteKnight]+nr[WhiteBishop] == 1 && nr[BlackKnight]+nr[BlackBishop] == 1 /* KBKN, KBKB, KNKN */
7805 if(--moveCount < 0 && appData.trivialDraws && canAdjudicate)
7806 { /* if the first 3 moves do not show a tactical win, declare draw */
7807 if(engineOpponent) {
7808 SendToProgram("force\n", engineOpponent); // suppress reply
7809 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7811 GameEnds( GameIsDrawn, "Xboard adjudication: Trivial draw", GE_XBOARD );
7814 } else moveCount = 6;
7817 // Repetition draws and 50-move rule can be applied independently of legality testing
7819 /* Check for rep-draws */
7821 for(k = forwardMostMove-2;
7822 k>=backwardMostMove && k>=forwardMostMove-100 &&
7823 (signed char)boards[k][EP_STATUS] < EP_UNKNOWN &&
7824 (signed char)boards[k+2][EP_STATUS] <= EP_NONE && (signed char)boards[k+1][EP_STATUS] <= EP_NONE;
7827 if(CompareBoards(boards[k], boards[forwardMostMove])) {
7828 /* compare castling rights */
7829 if( boards[forwardMostMove][CASTLING][2] != boards[k][CASTLING][2] &&
7830 (boards[k][CASTLING][0] != NoRights || boards[k][CASTLING][1] != NoRights) )
7831 rights++; /* King lost rights, while rook still had them */
7832 if( boards[forwardMostMove][CASTLING][2] != NoRights ) { /* king has rights */
7833 if( boards[forwardMostMove][CASTLING][0] != boards[k][CASTLING][0] ||
7834 boards[forwardMostMove][CASTLING][1] != boards[k][CASTLING][1] )
7835 rights++; /* but at least one rook lost them */
7837 if( boards[forwardMostMove][CASTLING][5] != boards[k][CASTLING][5] &&
7838 (boards[k][CASTLING][3] != NoRights || boards[k][CASTLING][4] != NoRights) )
7840 if( boards[forwardMostMove][CASTLING][5] != NoRights ) {
7841 if( boards[forwardMostMove][CASTLING][3] != boards[k][CASTLING][3] ||
7842 boards[forwardMostMove][CASTLING][4] != boards[k][CASTLING][4] )
7845 if( rights == 0 && ++count > appData.drawRepeats-2 && canAdjudicate
7846 && appData.drawRepeats > 1) {
7847 /* adjudicate after user-specified nr of repeats */
7848 int result = GameIsDrawn;
7849 char *details = "XBoard adjudication: repetition draw";
7850 if(gameInfo.variant == VariantXiangqi && appData.testLegality) {
7851 // [HGM] xiangqi: check for forbidden perpetuals
7852 int m, ourPerpetual = 1, hisPerpetual = 1;
7853 for(m=forwardMostMove; m>k; m-=2) {
7854 if(MateTest(boards[m], PosFlags(m)) != MT_CHECK)
7855 ourPerpetual = 0; // the current mover did not always check
7856 if(MateTest(boards[m-1], PosFlags(m-1)) != MT_CHECK)
7857 hisPerpetual = 0; // the opponent did not always check
7859 if(appData.debugMode) fprintf(debugFP, "XQ perpetual test, our=%d, his=%d\n",
7860 ourPerpetual, hisPerpetual);
7861 if(ourPerpetual && !hisPerpetual) { // we are actively checking him: forfeit
7862 result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
7863 details = "Xboard adjudication: perpetual checking";
7865 if(hisPerpetual && !ourPerpetual) { // he is checking us, but did not repeat yet
7866 break; // (or we would have caught him before). Abort repetition-checking loop.
7868 // Now check for perpetual chases
7869 if(!ourPerpetual && !hisPerpetual) { // no perpetual check, test for chase
7870 hisPerpetual = PerpetualChase(k, forwardMostMove);
7871 ourPerpetual = PerpetualChase(k+1, forwardMostMove);
7872 if(ourPerpetual && !hisPerpetual) { // we are actively chasing him: forfeit
7873 static char resdet[MSG_SIZ];
7874 result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
7876 snprintf(resdet, MSG_SIZ, "Xboard adjudication: perpetual chasing of %c%c", ourPerpetual>>8, ourPerpetual&255);
7878 if(hisPerpetual && !ourPerpetual) // he is chasing us, but did not repeat yet
7879 break; // Abort repetition-checking loop.
7881 // if neither of us is checking or chasing all the time, or both are, it is draw
7883 if(engineOpponent) {
7884 SendToProgram("force\n", engineOpponent); // suppress reply
7885 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7887 GameEnds( result, details, GE_XBOARD );
7890 if( rights == 0 && count > 1 ) /* occurred 2 or more times before */
7891 boards[forwardMostMove][EP_STATUS] = EP_REP_DRAW;
7895 /* Now we test for 50-move draws. Determine ply count */
7896 count = forwardMostMove;
7897 /* look for last irreversble move */
7898 while( (signed char)boards[count][EP_STATUS] <= EP_NONE && count > backwardMostMove )
7900 /* if we hit starting position, add initial plies */
7901 if( count == backwardMostMove )
7902 count -= initialRulePlies;
7903 count = forwardMostMove - count;
7904 if(gameInfo.variant == VariantXiangqi && ( count >= 100 || count >= 2*appData.ruleMoves ) ) {
7905 // adjust reversible move counter for checks in Xiangqi
7906 int i = forwardMostMove - count, inCheck = 0, lastCheck;
7907 if(i < backwardMostMove) i = backwardMostMove;
7908 while(i <= forwardMostMove) {
7909 lastCheck = inCheck; // check evasion does not count
7910 inCheck = (MateTest(boards[i], PosFlags(i)) == MT_CHECK);
7911 if(inCheck || lastCheck) count--; // check does not count
7916 boards[forwardMostMove][EP_STATUS] = EP_RULE_DRAW;
7917 /* this is used to judge if draw claims are legal */
7918 if(canAdjudicate && appData.ruleMoves > 0 && count >= 2*appData.ruleMoves) {
7919 if(engineOpponent) {
7920 SendToProgram("force\n", engineOpponent); // suppress reply
7921 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7923 GameEnds( GameIsDrawn, "Xboard adjudication: 50-move rule", GE_XBOARD );
7927 /* if draw offer is pending, treat it as a draw claim
7928 * when draw condition present, to allow engines a way to
7929 * claim draws before making their move to avoid a race
7930 * condition occurring after their move
7932 if((gameMode == TwoMachinesPlay ? second.offeredDraw : userOfferedDraw) || first.offeredDraw ) {
7934 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_RULE_DRAW)
7935 p = "Draw claim: 50-move rule";
7936 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_REP_DRAW)
7937 p = "Draw claim: 3-fold repetition";
7938 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_INSUF_DRAW)
7939 p = "Draw claim: insufficient mating material";
7940 if( p != NULL && canAdjudicate) {
7941 if(engineOpponent) {
7942 SendToProgram("force\n", engineOpponent); // suppress reply
7943 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7945 GameEnds( GameIsDrawn, p, GE_XBOARD );
7950 if( canAdjudicate && appData.adjudicateDrawMoves > 0 && forwardMostMove > (2*appData.adjudicateDrawMoves) ) {
7951 if(engineOpponent) {
7952 SendToProgram("force\n", engineOpponent); // suppress reply
7953 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7955 GameEnds( GameIsDrawn, "Xboard adjudication: long game", GE_XBOARD );
7962 SendMoveToBookUser (int moveNr, ChessProgramState *cps, int initial)
7963 { // [HGM] book: this routine intercepts moves to simulate book replies
7964 char *bookHit = NULL;
7966 //first determine if the incoming move brings opponent into his book
7967 if(appData.usePolyglotBook && (cps == &first ? !appData.firstHasOwnBookUCI : !appData.secondHasOwnBookUCI))
7968 bookHit = ProbeBook(moveNr+1, appData.polyglotBook); // returns move
7969 if(appData.debugMode) fprintf(debugFP, "book hit = %s\n", bookHit ? bookHit : "(NULL)");
7970 if(bookHit != NULL && !cps->bookSuspend) {
7971 // make sure opponent is not going to reply after receiving move to book position
7972 SendToProgram("force\n", cps);
7973 cps->bookSuspend = TRUE; // flag indicating it has to be restarted
7975 if(!initial) SendMoveToProgram(moveNr, cps); // with hit on initial position there is no move
7976 // now arrange restart after book miss
7978 // after a book hit we never send 'go', and the code after the call to this routine
7979 // has '&& !bookHit' added to suppress potential sending there (based on 'firstMove').
7980 char buf[MSG_SIZ], *move = bookHit;
7982 int fromX, fromY, toX, toY;
7986 if (ParseOneMove(bookHit, forwardMostMove, &moveType,
7987 &fromX, &fromY, &toX, &toY, &promoChar)) {
7988 (void) CoordsToAlgebraic(boards[forwardMostMove],
7989 PosFlags(forwardMostMove),
7990 fromY, fromX, toY, toX, promoChar, move);
7992 if(appData.debugMode) fprintf(debugFP, "Book move could not be parsed\n");
7996 snprintf(buf, MSG_SIZ, "%s%s\n", (cps->useUsermove ? "usermove " : ""), move); // force book move into program supposed to play it
7997 SendToProgram(buf, cps);
7998 if(!initial) firstMove = FALSE; // normally we would clear the firstMove condition after return & sending 'go'
7999 } else if(initial) { // 'go' was needed irrespective of firstMove, and it has to be done in this routine
8000 SendToProgram("go\n", cps);
8001 cps->bookSuspend = FALSE; // after a 'go' we are never suspended
8002 } else { // 'go' might be sent based on 'firstMove' after this routine returns
8003 if(cps->bookSuspend && !firstMove) // 'go' needed, and it will not be done after we return
8004 SendToProgram("go\n", cps);
8005 cps->bookSuspend = FALSE; // anyhow, we will not be suspended after a miss
8007 return bookHit; // notify caller of hit, so it can take action to send move to opponent
8011 LoadError (char *errmess, ChessProgramState *cps)
8012 { // unloads engine and switches back to -ncp mode if it was first
8013 if(cps->initDone) return FALSE;
8014 cps->isr = NULL; // this should suppress further error popups from breaking pipes
8015 DestroyChildProcess(cps->pr, 9 ); // just to be sure
8018 appData.noChessProgram = TRUE;
8019 gameMode = MachinePlaysBlack; ModeHighlight(); // kludge to unmark Machine Black menu
8020 gameMode = BeginningOfGame; ModeHighlight();
8023 if(GetDelayedEvent()) CancelDelayedEvent(), ThawUI(); // [HGM] cancel remaining loading effort scheduled after feature timeout
8024 DisplayMessage("", ""); // erase waiting message
8025 if(errmess) DisplayError(errmess, 0); // announce reason, if given
8030 ChessProgramState *savedState;
8032 DeferredBookMove (void)
8034 if(savedState->lastPing != savedState->lastPong)
8035 ScheduleDelayedEvent(DeferredBookMove, 10);
8037 HandleMachineMove(savedMessage, savedState);
8040 static int savedWhitePlayer, savedBlackPlayer, pairingReceived;
8041 static ChessProgramState *stalledEngine;
8042 static char stashedInputMove[MSG_SIZ];
8045 HandleMachineMove (char *message, ChessProgramState *cps)
8047 char machineMove[MSG_SIZ], buf1[MSG_SIZ*10], buf2[MSG_SIZ];
8048 char realname[MSG_SIZ];
8049 int fromX, fromY, toX, toY;
8053 int machineWhite, oldError;
8056 if(cps == &pairing && sscanf(message, "%d-%d", &savedWhitePlayer, &savedBlackPlayer) == 2) {
8057 // [HGM] pairing: Mega-hack! Pairing engine also uses this routine (so it could give other WB commands).
8058 if(savedWhitePlayer == 0 || savedBlackPlayer == 0) {
8059 DisplayError(_("Invalid pairing from pairing engine"), 0);
8062 pairingReceived = 1;
8064 return; // Skim the pairing messages here.
8067 oldError = cps->userError; cps->userError = 0;
8069 FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book hit
8071 * Kludge to ignore BEL characters
8073 while (*message == '\007') message++;
8076 * [HGM] engine debug message: ignore lines starting with '#' character
8078 if(cps->debug && *message == '#') return;
8081 * Look for book output
8083 if (cps == &first && bookRequested) {
8084 if (message[0] == '\t' || message[0] == ' ') {
8085 /* Part of the book output is here; append it */
8086 strcat(bookOutput, message);
8087 strcat(bookOutput, " \n");
8089 } else if (bookOutput[0] != NULLCHAR) {
8090 /* All of book output has arrived; display it */
8091 char *p = bookOutput;
8092 while (*p != NULLCHAR) {
8093 if (*p == '\t') *p = ' ';
8096 DisplayInformation(bookOutput);
8097 bookRequested = FALSE;
8098 /* Fall through to parse the current output */
8103 * Look for machine move.
8105 if ((sscanf(message, "%s %s %s", buf1, buf2, machineMove) == 3 && strcmp(buf2, "...") == 0) ||
8106 (sscanf(message, "%s %s", buf1, machineMove) == 2 && strcmp(buf1, "move") == 0))
8108 if(pausing && !cps->pause) { // for pausing engine that does not support 'pause', we stash its move for processing when we resume.
8109 if(appData.debugMode) fprintf(debugFP, "pause %s engine after move\n", cps->which);
8110 safeStrCpy(stashedInputMove, message, MSG_SIZ);
8111 stalledEngine = cps;
8112 if(appData.ponderNextMove) { // bring both engines out of ponder
8113 SendToProgram("easy\n", &first);
8114 if(gameMode == TwoMachinesPlay) SendToProgram("easy\n", &second);
8120 /* This method is only useful on engines that support ping */
8121 if (cps->lastPing != cps->lastPong) {
8122 if (gameMode == BeginningOfGame) {
8123 /* Extra move from before last new; ignore */
8124 if (appData.debugMode) {
8125 fprintf(debugFP, "Ignoring extra move from %s\n", cps->which);
8128 if (appData.debugMode) {
8129 fprintf(debugFP, "Undoing extra move from %s, gameMode %d\n",
8130 cps->which, gameMode);
8133 SendToProgram("undo\n", cps);
8139 case BeginningOfGame:
8140 /* Extra move from before last reset; ignore */
8141 if (appData.debugMode) {
8142 fprintf(debugFP, "Ignoring extra move from %s\n", cps->which);
8149 /* Extra move after we tried to stop. The mode test is
8150 not a reliable way of detecting this problem, but it's
8151 the best we can do on engines that don't support ping.
8153 if (appData.debugMode) {
8154 fprintf(debugFP, "Undoing extra move from %s, gameMode %d\n",
8155 cps->which, gameMode);
8157 SendToProgram("undo\n", cps);
8160 case MachinePlaysWhite:
8161 case IcsPlayingWhite:
8162 machineWhite = TRUE;
8165 case MachinePlaysBlack:
8166 case IcsPlayingBlack:
8167 machineWhite = FALSE;
8170 case TwoMachinesPlay:
8171 machineWhite = (cps->twoMachinesColor[0] == 'w');
8174 if (WhiteOnMove(forwardMostMove) != machineWhite) {
8175 if (appData.debugMode) {
8177 "Ignoring move out of turn by %s, gameMode %d"
8178 ", forwardMost %d\n",
8179 cps->which, gameMode, forwardMostMove);
8184 if(cps->alphaRank) AlphaRank(machineMove, 4);
8185 if (!ParseOneMove(machineMove, forwardMostMove, &moveType,
8186 &fromX, &fromY, &toX, &toY, &promoChar)) {
8187 /* Machine move could not be parsed; ignore it. */
8188 snprintf(buf1, MSG_SIZ*10, _("Illegal move \"%s\" from %s machine"),
8189 machineMove, _(cps->which));
8190 DisplayError(buf1, 0);
8191 snprintf(buf1, MSG_SIZ*10, "Xboard: Forfeit due to invalid move: %s (%c%c%c%c) res=%d",
8192 machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, moveType);
8193 if (gameMode == TwoMachinesPlay) {
8194 GameEnds(machineWhite ? BlackWins : WhiteWins,
8200 /* [HGM] Apparently legal, but so far only tested with EP_UNKOWN */
8201 /* So we have to redo legality test with true e.p. status here, */
8202 /* to make sure an illegal e.p. capture does not slip through, */
8203 /* to cause a forfeit on a justified illegal-move complaint */
8204 /* of the opponent. */
8205 if( gameMode==TwoMachinesPlay && appData.testLegality ) {
8207 moveType = LegalityTest(boards[forwardMostMove], PosFlags(forwardMostMove),
8208 fromY, fromX, toY, toX, promoChar);
8209 if(moveType == IllegalMove) {
8210 snprintf(buf1, MSG_SIZ*10, "Xboard: Forfeit due to illegal move: %s (%c%c%c%c)%c",
8211 machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, 0);
8212 GameEnds(machineWhite ? BlackWins : WhiteWins,
8215 } else if(gameInfo.variant != VariantFischeRandom && gameInfo.variant != VariantCapaRandom)
8216 /* [HGM] Kludge to handle engines that send FRC-style castling
8217 when they shouldn't (like TSCP-Gothic) */
8219 case WhiteASideCastleFR:
8220 case BlackASideCastleFR:
8222 currentMoveString[2]++;
8224 case WhiteHSideCastleFR:
8225 case BlackHSideCastleFR:
8227 currentMoveString[2]--;
8229 default: ; // nothing to do, but suppresses warning of pedantic compilers
8232 hintRequested = FALSE;
8233 lastHint[0] = NULLCHAR;
8234 bookRequested = FALSE;
8235 /* Program may be pondering now */
8236 cps->maybeThinking = TRUE;
8237 if (cps->sendTime == 2) cps->sendTime = 1;
8238 if (cps->offeredDraw) cps->offeredDraw--;
8240 /* [AS] Save move info*/
8241 pvInfoList[ forwardMostMove ].score = programStats.score;
8242 pvInfoList[ forwardMostMove ].depth = programStats.depth;
8243 pvInfoList[ forwardMostMove ].time = programStats.time; // [HGM] PGNtime: take time from engine stats
8245 MakeMove(fromX, fromY, toX, toY, promoChar);/*updates forwardMostMove*/
8247 /* [AS] Adjudicate game if needed (note: remember that forwardMostMove now points past the last move) */
8248 if( gameMode == TwoMachinesPlay && adjudicateLossThreshold != 0 && forwardMostMove >= adjudicateLossPlies ) {
8251 while( count < adjudicateLossPlies ) {
8252 int score = pvInfoList[ forwardMostMove - count - 1 ].score;
8255 score = -score; /* Flip score for winning side */
8258 if( score > adjudicateLossThreshold ) {
8265 if( count >= adjudicateLossPlies ) {
8266 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
8268 GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
8269 "Xboard adjudication",
8276 if(Adjudicate(cps)) {
8277 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
8278 return; // [HGM] adjudicate: for all automatic game ends
8282 if ((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack) &&
8284 if(cps->offeredDraw && (signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
8285 SendToICS(ics_prefix); // [HGM] drawclaim: send caim and move on one line for FICS
8287 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
8289 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
8291 if(appData.autoKibitz && !appData.icsEngineAnalyze ) { /* [HGM] kibitz: send most-recent PV info to ICS */
8292 char buf[3*MSG_SIZ];
8294 snprintf(buf, 3*MSG_SIZ, "kibitz !!! %+.2f/%d (%.2f sec, %u nodes, %.0f knps) PV=%s\n",
8295 programStats.score / 100.,
8297 programStats.time / 100.,
8298 (unsigned int)programStats.nodes,
8299 (unsigned int)programStats.nodes / (10*abs(programStats.time) + 1.),
8300 programStats.movelist);
8302 if(appData.debugMode) fprintf(debugFP, "nodes = %d, %lld\n", (int) programStats.nodes, programStats.nodes);
8307 /* [AS] Clear stats for next move */
8308 ClearProgramStats();
8309 thinkOutput[0] = NULLCHAR;
8310 hiddenThinkOutputState = 0;
8313 if (gameMode == TwoMachinesPlay) {
8314 /* [HGM] relaying draw offers moved to after reception of move */
8315 /* and interpreting offer as claim if it brings draw condition */
8316 if (cps->offeredDraw == 1 && cps->other->sendDrawOffers) {
8317 SendToProgram("draw\n", cps->other);
8319 if (cps->other->sendTime) {
8320 SendTimeRemaining(cps->other,
8321 cps->other->twoMachinesColor[0] == 'w');
8323 bookHit = SendMoveToBookUser(forwardMostMove-1, cps->other, FALSE);
8324 if (firstMove && !bookHit) {
8326 if (cps->other->useColors) {
8327 SendToProgram(cps->other->twoMachinesColor, cps->other);
8329 SendToProgram("go\n", cps->other);
8331 cps->other->maybeThinking = TRUE;
8334 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
8336 if (!pausing && appData.ringBellAfterMoves) {
8341 * Reenable menu items that were disabled while
8342 * machine was thinking
8344 if (gameMode != TwoMachinesPlay)
8345 SetUserThinkingEnables();
8347 // [HGM] book: after book hit opponent has received move and is now in force mode
8348 // force the book reply into it, and then fake that it outputted this move by jumping
8349 // back to the beginning of HandleMachineMove, with cps toggled and message set to this move
8351 static char bookMove[MSG_SIZ]; // a bit generous?
8353 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
8354 strcat(bookMove, bookHit);
8357 programStats.nodes = programStats.depth = programStats.time =
8358 programStats.score = programStats.got_only_move = 0;
8359 sprintf(programStats.movelist, "%s (xbook)", bookHit);
8361 if(cps->lastPing != cps->lastPong) {
8362 savedMessage = message; // args for deferred call
8364 ScheduleDelayedEvent(DeferredBookMove, 10);
8373 /* Set special modes for chess engines. Later something general
8374 * could be added here; for now there is just one kludge feature,
8375 * needed because Crafty 15.10 and earlier don't ignore SIGINT
8376 * when "xboard" is given as an interactive command.
8378 if (strncmp(message, "kibitz Hello from Crafty", 24) == 0) {
8379 cps->useSigint = FALSE;
8380 cps->useSigterm = FALSE;
8382 if (strncmp(message, "feature ", 8) == 0) { // [HGM] moved forward to pre-empt non-compliant commands
8383 ParseFeatures(message+8, cps);
8384 return; // [HGM] This return was missing, causing option features to be recognized as non-compliant commands!
8387 if ((!appData.testLegality || gameInfo.variant == VariantFairy) &&
8388 !strncmp(message, "setup ", 6)) { // [HGM] allow first engine to define opening position
8389 int dummy, s=6; char buf[MSG_SIZ];
8390 if(appData.icsActive || forwardMostMove != 0 || cps != &first) return;
8391 if(sscanf(message, "setup (%s", buf) == 1) s = 8 + strlen(buf), buf[s-9] = NULLCHAR, SetCharTable(pieceToChar, buf);
8392 if(startedFromSetupPosition) return;
8393 ParseFEN(boards[0], &dummy, message+s);
8394 DrawPosition(TRUE, boards[0]);
8395 startedFromSetupPosition = TRUE;
8398 /* [HGM] Allow engine to set up a position. Don't ask me why one would
8399 * want this, I was asked to put it in, and obliged.
8401 if (!strncmp(message, "setboard ", 9)) {
8402 Board initial_position;
8404 GameEnds(GameUnfinished, "Engine aborts game", GE_XBOARD);
8406 if (!ParseFEN(initial_position, &blackPlaysFirst, message + 9)) {
8407 DisplayError(_("Bad FEN received from engine"), 0);
8411 CopyBoard(boards[0], initial_position);
8412 initialRulePlies = FENrulePlies;
8413 if(blackPlaysFirst) gameMode = MachinePlaysWhite;
8414 else gameMode = MachinePlaysBlack;
8415 DrawPosition(FALSE, boards[currentMove]);
8421 * Look for communication commands
8423 if (!strncmp(message, "telluser ", 9)) {
8424 if(message[9] == '\\' && message[10] == '\\')
8425 EscapeExpand(message+9, message+11); // [HGM] esc: allow escape sequences in popup box
8427 DisplayNote(message + 9);
8430 if (!strncmp(message, "tellusererror ", 14)) {
8432 if(message[14] == '\\' && message[15] == '\\')
8433 EscapeExpand(message+14, message+16); // [HGM] esc: allow escape sequences in popup box
8435 DisplayError(message + 14, 0);
8438 if (!strncmp(message, "tellopponent ", 13)) {
8439 if (appData.icsActive) {
8441 snprintf(buf1, sizeof(buf1), "%ssay %s\n", ics_prefix, message + 13);
8445 DisplayNote(message + 13);
8449 if (!strncmp(message, "tellothers ", 11)) {
8450 if (appData.icsActive) {
8452 snprintf(buf1, sizeof(buf1), "%swhisper %s\n", ics_prefix, message + 11);
8458 if (!strncmp(message, "tellall ", 8)) {
8459 if (appData.icsActive) {
8461 snprintf(buf1, sizeof(buf1), "%skibitz %s\n", ics_prefix, message + 8);
8465 DisplayNote(message + 8);
8469 if (strncmp(message, "warning", 7) == 0) {
8470 /* Undocumented feature, use tellusererror in new code */
8471 DisplayError(message, 0);
8474 if (sscanf(message, "askuser %s %[^\n]", buf1, buf2) == 2) {
8475 safeStrCpy(realname, cps->tidy, sizeof(realname)/sizeof(realname[0]));
8476 strcat(realname, " query");
8477 AskQuestion(realname, buf2, buf1, cps->pr);
8480 /* Commands from the engine directly to ICS. We don't allow these to be
8481 * sent until we are logged on. Crafty kibitzes have been known to
8482 * interfere with the login process.
8485 if (!strncmp(message, "tellics ", 8)) {
8486 SendToICS(message + 8);
8490 if (!strncmp(message, "tellicsnoalias ", 15)) {
8491 SendToICS(ics_prefix);
8492 SendToICS(message + 15);
8496 /* The following are for backward compatibility only */
8497 if (!strncmp(message,"whisper",7) || !strncmp(message,"kibitz",6) ||
8498 !strncmp(message,"draw",4) || !strncmp(message,"tell",3)) {
8499 SendToICS(ics_prefix);
8505 if (sscanf(message, "pong %d", &cps->lastPong) == 1) {
8509 * If the move is illegal, cancel it and redraw the board.
8510 * Also deal with other error cases. Matching is rather loose
8511 * here to accommodate engines written before the spec.
8513 if (strncmp(message + 1, "llegal move", 11) == 0 ||
8514 strncmp(message, "Error", 5) == 0) {
8515 if (StrStr(message, "name") ||
8516 StrStr(message, "rating") || StrStr(message, "?") ||
8517 StrStr(message, "result") || StrStr(message, "board") ||
8518 StrStr(message, "bk") || StrStr(message, "computer") ||
8519 StrStr(message, "variant") || StrStr(message, "hint") ||
8520 StrStr(message, "random") || StrStr(message, "depth") ||
8521 StrStr(message, "accepted")) {
8524 if (StrStr(message, "protover")) {
8525 /* Program is responding to input, so it's apparently done
8526 initializing, and this error message indicates it is
8527 protocol version 1. So we don't need to wait any longer
8528 for it to initialize and send feature commands. */
8529 FeatureDone(cps, 1);
8530 cps->protocolVersion = 1;
8533 cps->maybeThinking = FALSE;
8535 if (StrStr(message, "draw")) {
8536 /* Program doesn't have "draw" command */
8537 cps->sendDrawOffers = 0;
8540 if (cps->sendTime != 1 &&
8541 (StrStr(message, "time") || StrStr(message, "otim"))) {
8542 /* Program apparently doesn't have "time" or "otim" command */
8546 if (StrStr(message, "analyze")) {
8547 cps->analysisSupport = FALSE;
8548 cps->analyzing = FALSE;
8549 // Reset(FALSE, TRUE); // [HGM] this caused discrepancy between display and internal state!
8550 EditGameEvent(); // [HGM] try to preserve loaded game
8551 snprintf(buf2,MSG_SIZ, _("%s does not support analysis"), cps->tidy);
8552 DisplayError(buf2, 0);
8555 if (StrStr(message, "(no matching move)st")) {
8556 /* Special kludge for GNU Chess 4 only */
8557 cps->stKludge = TRUE;
8558 SendTimeControl(cps, movesPerSession, timeControl,
8559 timeIncrement, appData.searchDepth,
8563 if (StrStr(message, "(no matching move)sd")) {
8564 /* Special kludge for GNU Chess 4 only */
8565 cps->sdKludge = TRUE;
8566 SendTimeControl(cps, movesPerSession, timeControl,
8567 timeIncrement, appData.searchDepth,
8571 if (!StrStr(message, "llegal")) {
8574 if (gameMode == BeginningOfGame || gameMode == EndOfGame ||
8575 gameMode == IcsIdle) return;
8576 if (forwardMostMove <= backwardMostMove) return;
8577 if (pausing) PauseEvent();
8578 if(appData.forceIllegal) {
8579 // [HGM] illegal: machine refused move; force position after move into it
8580 SendToProgram("force\n", cps);
8581 if(!cps->useSetboard) { // hideous kludge on kludge, because SendBoard sucks.
8582 // we have a real problem now, as SendBoard will use the a2a3 kludge
8583 // when black is to move, while there might be nothing on a2 or black
8584 // might already have the move. So send the board as if white has the move.
8585 // But first we must change the stm of the engine, as it refused the last move
8586 SendBoard(cps, 0); // always kludgeless, as white is to move on boards[0]
8587 if(WhiteOnMove(forwardMostMove)) {
8588 SendToProgram("a7a6\n", cps); // for the engine black still had the move
8589 SendBoard(cps, forwardMostMove); // kludgeless board
8591 SendToProgram("a2a3\n", cps); // for the engine white still had the move
8592 CopyBoard(boards[forwardMostMove+1], boards[forwardMostMove]);
8593 SendBoard(cps, forwardMostMove+1); // kludgeless board
8595 } else SendBoard(cps, forwardMostMove); // FEN case, also sets stm properly
8596 if(gameMode == MachinePlaysWhite || gameMode == MachinePlaysBlack ||
8597 gameMode == TwoMachinesPlay)
8598 SendToProgram("go\n", cps);
8601 if (gameMode == PlayFromGameFile) {
8602 /* Stop reading this game file */
8603 gameMode = EditGame;
8606 /* [HGM] illegal-move claim should forfeit game when Xboard */
8607 /* only passes fully legal moves */
8608 if( appData.testLegality && gameMode == TwoMachinesPlay ) {
8609 GameEnds( cps->twoMachinesColor[0] == 'w' ? BlackWins : WhiteWins,
8610 "False illegal-move claim", GE_XBOARD );
8611 return; // do not take back move we tested as valid
8613 currentMove = forwardMostMove-1;
8614 DisplayMove(currentMove-1); /* before DisplayMoveError */
8615 SwitchClocks(forwardMostMove-1); // [HGM] race
8616 DisplayBothClocks();
8617 snprintf(buf1, 10*MSG_SIZ, _("Illegal move \"%s\" (rejected by %s chess program)"),
8618 parseList[currentMove], _(cps->which));
8619 DisplayMoveError(buf1);
8620 DrawPosition(FALSE, boards[currentMove]);
8622 SetUserThinkingEnables();
8625 if (strncmp(message, "time", 4) == 0 && StrStr(message, "Illegal")) {
8626 /* Program has a broken "time" command that
8627 outputs a string not ending in newline.
8633 * If chess program startup fails, exit with an error message.
8634 * Attempts to recover here are futile. [HGM] Well, we try anyway
8636 if ((StrStr(message, "unknown host") != NULL)
8637 || (StrStr(message, "No remote directory") != NULL)
8638 || (StrStr(message, "not found") != NULL)
8639 || (StrStr(message, "No such file") != NULL)
8640 || (StrStr(message, "can't alloc") != NULL)
8641 || (StrStr(message, "Permission denied") != NULL)) {
8643 cps->maybeThinking = FALSE;
8644 snprintf(buf1, sizeof(buf1), _("Failed to start %s chess program %s on %s: %s\n"),
8645 _(cps->which), cps->program, cps->host, message);
8646 RemoveInputSource(cps->isr);
8647 if(appData.icsActive) DisplayFatalError(buf1, 0, 1); else {
8648 if(LoadError(oldError ? NULL : buf1, cps)) return; // error has then been handled by LoadError
8649 if(!oldError) DisplayError(buf1, 0); // if reason neatly announced, suppress general error popup
8655 * Look for hint output
8657 if (sscanf(message, "Hint: %s", buf1) == 1) {
8658 if (cps == &first && hintRequested) {
8659 hintRequested = FALSE;
8660 if (ParseOneMove(buf1, forwardMostMove, &moveType,
8661 &fromX, &fromY, &toX, &toY, &promoChar)) {
8662 (void) CoordsToAlgebraic(boards[forwardMostMove],
8663 PosFlags(forwardMostMove),
8664 fromY, fromX, toY, toX, promoChar, buf1);
8665 snprintf(buf2, sizeof(buf2), _("Hint: %s"), buf1);
8666 DisplayInformation(buf2);
8668 /* Hint move could not be parsed!? */
8669 snprintf(buf2, sizeof(buf2),
8670 _("Illegal hint move \"%s\"\nfrom %s chess program"),
8671 buf1, _(cps->which));
8672 DisplayError(buf2, 0);
8675 safeStrCpy(lastHint, buf1, sizeof(lastHint)/sizeof(lastHint[0]));
8681 * Ignore other messages if game is not in progress
8683 if (gameMode == BeginningOfGame || gameMode == EndOfGame ||
8684 gameMode == IcsIdle || cps->lastPing != cps->lastPong) return;
8687 * look for win, lose, draw, or draw offer
8689 if (strncmp(message, "1-0", 3) == 0) {
8690 char *p, *q, *r = "";
8691 p = strchr(message, '{');
8699 GameEnds(WhiteWins, r, GE_ENGINE1 + (cps != &first)); /* [HGM] pass claimer indication for claim test */
8701 } else if (strncmp(message, "0-1", 3) == 0) {
8702 char *p, *q, *r = "";
8703 p = strchr(message, '{');
8711 /* Kludge for Arasan 4.1 bug */
8712 if (strcmp(r, "Black resigns") == 0) {
8713 GameEnds(WhiteWins, r, GE_ENGINE1 + (cps != &first));
8716 GameEnds(BlackWins, r, GE_ENGINE1 + (cps != &first));
8718 } else if (strncmp(message, "1/2", 3) == 0) {
8719 char *p, *q, *r = "";
8720 p = strchr(message, '{');
8729 GameEnds(GameIsDrawn, r, GE_ENGINE1 + (cps != &first));
8732 } else if (strncmp(message, "White resign", 12) == 0) {
8733 GameEnds(BlackWins, "White resigns", GE_ENGINE1 + (cps != &first));
8735 } else if (strncmp(message, "Black resign", 12) == 0) {
8736 GameEnds(WhiteWins, "Black resigns", GE_ENGINE1 + (cps != &first));
8738 } else if (strncmp(message, "White matches", 13) == 0 ||
8739 strncmp(message, "Black matches", 13) == 0 ) {
8740 /* [HGM] ignore GNUShogi noises */
8742 } else if (strncmp(message, "White", 5) == 0 &&
8743 message[5] != '(' &&
8744 StrStr(message, "Black") == NULL) {
8745 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
8747 } else if (strncmp(message, "Black", 5) == 0 &&
8748 message[5] != '(') {
8749 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
8751 } else if (strcmp(message, "resign") == 0 ||
8752 strcmp(message, "computer resigns") == 0) {
8754 case MachinePlaysBlack:
8755 case IcsPlayingBlack:
8756 GameEnds(WhiteWins, "Black resigns", GE_ENGINE);
8758 case MachinePlaysWhite:
8759 case IcsPlayingWhite:
8760 GameEnds(BlackWins, "White resigns", GE_ENGINE);
8762 case TwoMachinesPlay:
8763 if (cps->twoMachinesColor[0] == 'w')
8764 GameEnds(BlackWins, "White resigns", GE_ENGINE1 + (cps != &first));
8766 GameEnds(WhiteWins, "Black resigns", GE_ENGINE1 + (cps != &first));
8773 } else if (strncmp(message, "opponent mates", 14) == 0) {
8775 case MachinePlaysBlack:
8776 case IcsPlayingBlack:
8777 GameEnds(WhiteWins, "White mates", GE_ENGINE);
8779 case MachinePlaysWhite:
8780 case IcsPlayingWhite:
8781 GameEnds(BlackWins, "Black mates", GE_ENGINE);
8783 case TwoMachinesPlay:
8784 if (cps->twoMachinesColor[0] == 'w')
8785 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
8787 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
8794 } else if (strncmp(message, "computer mates", 14) == 0) {
8796 case MachinePlaysBlack:
8797 case IcsPlayingBlack:
8798 GameEnds(BlackWins, "Black mates", GE_ENGINE1);
8800 case MachinePlaysWhite:
8801 case IcsPlayingWhite:
8802 GameEnds(WhiteWins, "White mates", GE_ENGINE);
8804 case TwoMachinesPlay:
8805 if (cps->twoMachinesColor[0] == 'w')
8806 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
8808 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
8815 } else if (strncmp(message, "checkmate", 9) == 0) {
8816 if (WhiteOnMove(forwardMostMove)) {
8817 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
8819 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
8822 } else if (strstr(message, "Draw") != NULL ||
8823 strstr(message, "game is a draw") != NULL) {
8824 GameEnds(GameIsDrawn, "Draw", GE_ENGINE1 + (cps != &first));
8826 } else if (strstr(message, "offer") != NULL &&
8827 strstr(message, "draw") != NULL) {
8829 if (appData.zippyPlay && first.initDone) {
8830 /* Relay offer to ICS */
8831 SendToICS(ics_prefix);
8832 SendToICS("draw\n");
8835 cps->offeredDraw = 2; /* valid until this engine moves twice */
8836 if (gameMode == TwoMachinesPlay) {
8837 if (cps->other->offeredDraw) {
8838 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
8839 /* [HGM] in two-machine mode we delay relaying draw offer */
8840 /* until after we also have move, to see if it is really claim */
8842 } else if (gameMode == MachinePlaysWhite ||
8843 gameMode == MachinePlaysBlack) {
8844 if (userOfferedDraw) {
8845 DisplayInformation(_("Machine accepts your draw offer"));
8846 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
8848 DisplayInformation(_("Machine offers a draw\nSelect Action / Draw to agree"));
8855 * Look for thinking output
8857 if ( appData.showThinking // [HGM] thinking: test all options that cause this output
8858 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp()
8860 int plylev, mvleft, mvtot, curscore, time;
8861 char mvname[MOVE_LEN];
8865 int prefixHint = FALSE;
8866 mvname[0] = NULLCHAR;
8869 case MachinePlaysBlack:
8870 case IcsPlayingBlack:
8871 if (WhiteOnMove(forwardMostMove)) prefixHint = TRUE;
8873 case MachinePlaysWhite:
8874 case IcsPlayingWhite:
8875 if (!WhiteOnMove(forwardMostMove)) prefixHint = TRUE;
8880 case IcsObserving: /* [DM] icsEngineAnalyze */
8881 if (!appData.icsEngineAnalyze) ignore = TRUE;
8883 case TwoMachinesPlay:
8884 if ((cps->twoMachinesColor[0] == 'w') != WhiteOnMove(forwardMostMove)) {
8894 ChessProgramStats tempStats = programStats; // [HGM] info: filter out info lines
8896 if (sscanf(message, "%d%c %d %d " u64Display " %[^\n]\n",
8897 &plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5) {
8899 if (plyext != ' ' && plyext != '\t') {
8903 /* [AS] Negate score if machine is playing black and reporting absolute scores */
8904 if( cps->scoreIsAbsolute &&
8905 ( gameMode == MachinePlaysBlack ||
8906 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b' ||
8907 gameMode == IcsPlayingBlack || // [HGM] also add other situations where engine should report black POV
8908 (gameMode == AnalyzeMode || gameMode == AnalyzeFile || gameMode == IcsObserving && appData.icsEngineAnalyze) &&
8909 !WhiteOnMove(currentMove)
8912 curscore = -curscore;
8915 if(appData.pvSAN[cps==&second]) pv = PvToSAN(buf1);
8917 if(serverMoves && (time > 100 || time == 0 && plylev > 7)) {
8920 snprintf(buf, MSG_SIZ, "%s", appData.serverMovesName);
8921 buf[strlen(buf)-1] = gameMode == MachinePlaysWhite ? 'w' :
8922 gameMode == MachinePlaysBlack ? 'b' : cps->twoMachinesColor[0];
8923 if(appData.debugMode) fprintf(debugFP, "write PV on file '%s'\n", buf);
8924 if(f = fopen(buf, "w")) { // export PV to applicable PV file
8925 fprintf(f, "%5.2f/%-2d %s", curscore/100., plylev, pv);
8927 } else DisplayError(_("failed writing PV"), 0);
8930 tempStats.depth = plylev;
8931 tempStats.nodes = nodes;
8932 tempStats.time = time;
8933 tempStats.score = curscore;
8934 tempStats.got_only_move = 0;
8936 if(cps->nps >= 0) { /* [HGM] nps: use engine nodes or time to decrement clock */
8939 if(cps->nps == 0) ticklen = 10*time; // use engine reported time
8940 else ticklen = (1000. * u64ToDouble(nodes)) / cps->nps; // convert node count to time
8941 if(WhiteOnMove(forwardMostMove) && (gameMode == MachinePlaysWhite ||
8942 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'w'))
8943 whiteTimeRemaining = timeRemaining[0][forwardMostMove] - ticklen;
8944 if(!WhiteOnMove(forwardMostMove) && (gameMode == MachinePlaysBlack ||
8945 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b'))
8946 blackTimeRemaining = timeRemaining[1][forwardMostMove] - ticklen;
8949 /* Buffer overflow protection */
8950 if (pv[0] != NULLCHAR) {
8951 if (strlen(pv) >= sizeof(tempStats.movelist)
8952 && appData.debugMode) {
8954 "PV is too long; using the first %u bytes.\n",
8955 (unsigned) sizeof(tempStats.movelist) - 1);
8958 safeStrCpy( tempStats.movelist, pv, sizeof(tempStats.movelist)/sizeof(tempStats.movelist[0]) );
8960 sprintf(tempStats.movelist, " no PV\n");
8963 if (tempStats.seen_stat) {
8964 tempStats.ok_to_send = 1;
8967 if (strchr(tempStats.movelist, '(') != NULL) {
8968 tempStats.line_is_book = 1;
8969 tempStats.nr_moves = 0;
8970 tempStats.moves_left = 0;
8972 tempStats.line_is_book = 0;
8975 if(tempStats.score != 0 || tempStats.nodes != 0 || tempStats.time != 0)
8976 programStats = tempStats; // [HGM] info: only set stats if genuine PV and not an info line
8978 SendProgramStatsToFrontend( cps, &tempStats );
8981 [AS] Protect the thinkOutput buffer from overflow... this
8982 is only useful if buf1 hasn't overflowed first!
8984 snprintf(thinkOutput, sizeof(thinkOutput)/sizeof(thinkOutput[0]), "[%d]%c%+.2f %s%s",
8986 (gameMode == TwoMachinesPlay ?
8987 ToUpper(cps->twoMachinesColor[0]) : ' '),
8988 ((double) curscore) / 100.0,
8989 prefixHint ? lastHint : "",
8990 prefixHint ? " " : "" );
8992 if( buf1[0] != NULLCHAR ) {
8993 unsigned max_len = sizeof(thinkOutput) - strlen(thinkOutput) - 1;
8995 if( strlen(pv) > max_len ) {
8996 if( appData.debugMode) {
8997 fprintf(debugFP,"PV is too long for thinkOutput, truncating.\n");
8999 pv[max_len+1] = '\0';
9002 strcat( thinkOutput, pv);
9005 if (currentMove == forwardMostMove || gameMode == AnalyzeMode
9006 || gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
9007 DisplayMove(currentMove - 1);
9011 } else if ((p=StrStr(message, "(only move)")) != NULL) {
9012 /* crafty (9.25+) says "(only move) <move>"
9013 * if there is only 1 legal move
9015 sscanf(p, "(only move) %s", buf1);
9016 snprintf(thinkOutput, sizeof(thinkOutput)/sizeof(thinkOutput[0]), "%s (only move)", buf1);
9017 sprintf(programStats.movelist, "%s (only move)", buf1);
9018 programStats.depth = 1;
9019 programStats.nr_moves = 1;
9020 programStats.moves_left = 1;
9021 programStats.nodes = 1;
9022 programStats.time = 1;
9023 programStats.got_only_move = 1;
9025 /* Not really, but we also use this member to
9026 mean "line isn't going to change" (Crafty
9027 isn't searching, so stats won't change) */
9028 programStats.line_is_book = 1;
9030 SendProgramStatsToFrontend( cps, &programStats );
9032 if (currentMove == forwardMostMove || gameMode==AnalyzeMode ||
9033 gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
9034 DisplayMove(currentMove - 1);
9037 } else if (sscanf(message,"stat01: %d " u64Display " %d %d %d %s",
9038 &time, &nodes, &plylev, &mvleft,
9039 &mvtot, mvname) >= 5) {
9040 /* The stat01: line is from Crafty (9.29+) in response
9041 to the "." command */
9042 programStats.seen_stat = 1;
9043 cps->maybeThinking = TRUE;
9045 if (programStats.got_only_move || !appData.periodicUpdates)
9048 programStats.depth = plylev;
9049 programStats.time = time;
9050 programStats.nodes = nodes;
9051 programStats.moves_left = mvleft;
9052 programStats.nr_moves = mvtot;
9053 safeStrCpy(programStats.move_name, mvname, sizeof(programStats.move_name)/sizeof(programStats.move_name[0]));
9054 programStats.ok_to_send = 1;
9055 programStats.movelist[0] = '\0';
9057 SendProgramStatsToFrontend( cps, &programStats );
9061 } else if (strncmp(message,"++",2) == 0) {
9062 /* Crafty 9.29+ outputs this */
9063 programStats.got_fail = 2;
9066 } else if (strncmp(message,"--",2) == 0) {
9067 /* Crafty 9.29+ outputs this */
9068 programStats.got_fail = 1;
9071 } else if (thinkOutput[0] != NULLCHAR &&
9072 strncmp(message, " ", 4) == 0) {
9073 unsigned message_len;
9076 while (*p && *p == ' ') p++;
9078 message_len = strlen( p );
9080 /* [AS] Avoid buffer overflow */
9081 if( sizeof(thinkOutput) - strlen(thinkOutput) - 1 > message_len ) {
9082 strcat(thinkOutput, " ");
9083 strcat(thinkOutput, p);
9086 if( sizeof(programStats.movelist) - strlen(programStats.movelist) - 1 > message_len ) {
9087 strcat(programStats.movelist, " ");
9088 strcat(programStats.movelist, p);
9091 if (currentMove == forwardMostMove || gameMode==AnalyzeMode ||
9092 gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
9093 DisplayMove(currentMove - 1);
9101 if (sscanf(message, "%d%c %d %d " u64Display " %[^\n]\n",
9102 &plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5)
9104 ChessProgramStats cpstats;
9106 if (plyext != ' ' && plyext != '\t') {
9110 /* [AS] Negate score if machine is playing black and reporting absolute scores */
9111 if( cps->scoreIsAbsolute && ((gameMode == MachinePlaysBlack) || (gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b')) ) {
9112 curscore = -curscore;
9115 cpstats.depth = plylev;
9116 cpstats.nodes = nodes;
9117 cpstats.time = time;
9118 cpstats.score = curscore;
9119 cpstats.got_only_move = 0;
9120 cpstats.movelist[0] = '\0';
9122 if (buf1[0] != NULLCHAR) {
9123 safeStrCpy( cpstats.movelist, buf1, sizeof(cpstats.movelist)/sizeof(cpstats.movelist[0]) );
9126 cpstats.ok_to_send = 0;
9127 cpstats.line_is_book = 0;
9128 cpstats.nr_moves = 0;
9129 cpstats.moves_left = 0;
9131 SendProgramStatsToFrontend( cps, &cpstats );
9138 /* Parse a game score from the character string "game", and
9139 record it as the history of the current game. The game
9140 score is NOT assumed to start from the standard position.
9141 The display is not updated in any way.
9144 ParseGameHistory (char *game)
9147 int fromX, fromY, toX, toY, boardIndex;
9152 if (appData.debugMode)
9153 fprintf(debugFP, "Parsing game history: %s\n", game);
9155 if (gameInfo.event == NULL) gameInfo.event = StrSave("ICS game");
9156 gameInfo.site = StrSave(appData.icsHost);
9157 gameInfo.date = PGNDate();
9158 gameInfo.round = StrSave("-");
9160 /* Parse out names of players */
9161 while (*game == ' ') game++;
9163 while (*game != ' ') *p++ = *game++;
9165 gameInfo.white = StrSave(buf);
9166 while (*game == ' ') game++;
9168 while (*game != ' ' && *game != '\n') *p++ = *game++;
9170 gameInfo.black = StrSave(buf);
9173 boardIndex = blackPlaysFirst ? 1 : 0;
9176 yyboardindex = boardIndex;
9177 moveType = (ChessMove) Myylex();
9179 case IllegalMove: /* maybe suicide chess, etc. */
9180 if (appData.debugMode) {
9181 fprintf(debugFP, "Illegal move from ICS: '%s'\n", yy_text);
9182 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
9183 setbuf(debugFP, NULL);
9185 case WhitePromotion:
9186 case BlackPromotion:
9187 case WhiteNonPromotion:
9188 case BlackNonPromotion:
9190 case WhiteCapturesEnPassant:
9191 case BlackCapturesEnPassant:
9192 case WhiteKingSideCastle:
9193 case WhiteQueenSideCastle:
9194 case BlackKingSideCastle:
9195 case BlackQueenSideCastle:
9196 case WhiteKingSideCastleWild:
9197 case WhiteQueenSideCastleWild:
9198 case BlackKingSideCastleWild:
9199 case BlackQueenSideCastleWild:
9201 case WhiteHSideCastleFR:
9202 case WhiteASideCastleFR:
9203 case BlackHSideCastleFR:
9204 case BlackASideCastleFR:
9206 fromX = currentMoveString[0] - AAA;
9207 fromY = currentMoveString[1] - ONE;
9208 toX = currentMoveString[2] - AAA;
9209 toY = currentMoveString[3] - ONE;
9210 promoChar = currentMoveString[4];
9214 if(currentMoveString[0] == '@') continue; // no null moves in ICS mode!
9215 fromX = moveType == WhiteDrop ?
9216 (int) CharToPiece(ToUpper(currentMoveString[0])) :
9217 (int) CharToPiece(ToLower(currentMoveString[0]));
9219 toX = currentMoveString[2] - AAA;
9220 toY = currentMoveString[3] - ONE;
9221 promoChar = NULLCHAR;
9225 snprintf(buf, MSG_SIZ, _("Ambiguous move in ICS output: \"%s\""), yy_text);
9226 if (appData.debugMode) {
9227 fprintf(debugFP, "Ambiguous move from ICS: '%s'\n", yy_text);
9228 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
9229 setbuf(debugFP, NULL);
9231 DisplayError(buf, 0);
9233 case ImpossibleMove:
9235 snprintf(buf, MSG_SIZ, _("Illegal move in ICS output: \"%s\""), yy_text);
9236 if (appData.debugMode) {
9237 fprintf(debugFP, "Impossible move from ICS: '%s'\n", yy_text);
9238 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
9239 setbuf(debugFP, NULL);
9241 DisplayError(buf, 0);
9244 if (boardIndex < backwardMostMove) {
9245 /* Oops, gap. How did that happen? */
9246 DisplayError(_("Gap in move list"), 0);
9249 backwardMostMove = blackPlaysFirst ? 1 : 0;
9250 if (boardIndex > forwardMostMove) {
9251 forwardMostMove = boardIndex;
9255 if (boardIndex > (blackPlaysFirst ? 1 : 0)) {
9256 strcat(parseList[boardIndex-1], " ");
9257 strcat(parseList[boardIndex-1], yy_text);
9269 case GameUnfinished:
9270 if (gameMode == IcsExamining) {
9271 if (boardIndex < backwardMostMove) {
9272 /* Oops, gap. How did that happen? */
9275 backwardMostMove = blackPlaysFirst ? 1 : 0;
9278 gameInfo.result = moveType;
9279 p = strchr(yy_text, '{');
9280 if (p == NULL) p = strchr(yy_text, '(');
9283 if (p[0] == '0' || p[0] == '1' || p[0] == '*') p = "";
9285 q = strchr(p, *p == '{' ? '}' : ')');
9286 if (q != NULL) *q = NULLCHAR;
9289 while(q = strchr(p, '\n')) *q = ' '; // [HGM] crush linefeeds in result message
9290 gameInfo.resultDetails = StrSave(p);
9293 if (boardIndex >= forwardMostMove &&
9294 !(gameMode == IcsObserving && ics_gamenum == -1)) {
9295 backwardMostMove = blackPlaysFirst ? 1 : 0;
9298 (void) CoordsToAlgebraic(boards[boardIndex], PosFlags(boardIndex),
9299 fromY, fromX, toY, toX, promoChar,
9300 parseList[boardIndex]);
9301 CopyBoard(boards[boardIndex + 1], boards[boardIndex]);
9302 /* currentMoveString is set as a side-effect of yylex */
9303 safeStrCpy(moveList[boardIndex], currentMoveString, sizeof(moveList[boardIndex])/sizeof(moveList[boardIndex][0]));
9304 strcat(moveList[boardIndex], "\n");
9306 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[boardIndex]);
9307 switch (MateTest(boards[boardIndex], PosFlags(boardIndex)) ) {
9313 if(gameInfo.variant != VariantShogi)
9314 strcat(parseList[boardIndex - 1], "+");
9318 strcat(parseList[boardIndex - 1], "#");
9325 /* Apply a move to the given board */
9327 ApplyMove (int fromX, int fromY, int toX, int toY, int promoChar, Board board)
9329 ChessSquare captured = board[toY][toX], piece, king; int p, oldEP = EP_NONE, berolina = 0;
9330 int promoRank = gameInfo.variant == VariantMakruk || gameInfo.variant == VariantGrand ? 3 : 1;
9332 /* [HGM] compute & store e.p. status and castling rights for new position */
9333 /* we can always do that 'in place', now pointers to these rights are passed to ApplyMove */
9335 if(gameInfo.variant == VariantBerolina) berolina = EP_BEROLIN_A;
9336 oldEP = (signed char)board[EP_STATUS];
9337 board[EP_STATUS] = EP_NONE;
9339 if (fromY == DROP_RANK) {
9341 if(fromX == EmptySquare) { // [HGM] pass: empty drop encodes null move; nothing to change.
9342 board[EP_STATUS] = EP_CAPTURE; // null move considered irreversible
9345 piece = board[toY][toX] = (ChessSquare) fromX;
9349 if( board[toY][toX] != EmptySquare )
9350 board[EP_STATUS] = EP_CAPTURE;
9352 if( board[fromY][fromX] == WhiteLance || board[fromY][fromX] == BlackLance ) {
9353 if( gameInfo.variant != VariantSuper && gameInfo.variant != VariantShogi )
9354 board[EP_STATUS] = EP_PAWN_MOVE; // Lance is Pawn-like in most variants
9356 if( board[fromY][fromX] == WhitePawn ) {
9357 if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
9358 board[EP_STATUS] = EP_PAWN_MOVE;
9360 if(toX>BOARD_LEFT && board[toY][toX-1] == BlackPawn &&
9361 gameInfo.variant != VariantBerolina || toX < fromX)
9362 board[EP_STATUS] = toX | berolina;
9363 if(toX<BOARD_RGHT-1 && board[toY][toX+1] == BlackPawn &&
9364 gameInfo.variant != VariantBerolina || toX > fromX)
9365 board[EP_STATUS] = toX;
9368 if( board[fromY][fromX] == BlackPawn ) {
9369 if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
9370 board[EP_STATUS] = EP_PAWN_MOVE;
9371 if( toY-fromY== -2) {
9372 if(toX>BOARD_LEFT && board[toY][toX-1] == WhitePawn &&
9373 gameInfo.variant != VariantBerolina || toX < fromX)
9374 board[EP_STATUS] = toX | berolina;
9375 if(toX<BOARD_RGHT-1 && board[toY][toX+1] == WhitePawn &&
9376 gameInfo.variant != VariantBerolina || toX > fromX)
9377 board[EP_STATUS] = toX;
9381 for(i=0; i<nrCastlingRights; i++) {
9382 if(board[CASTLING][i] == fromX && castlingRank[i] == fromY ||
9383 board[CASTLING][i] == toX && castlingRank[i] == toY
9384 ) board[CASTLING][i] = NoRights; // revoke for moved or captured piece
9387 if(gameInfo.variant == VariantSChess) { // update virginity
9388 if(fromY == 0) board[VIRGIN][fromX] &= ~VIRGIN_W; // loss by moving
9389 if(fromY == BOARD_HEIGHT-1) board[VIRGIN][fromX] &= ~VIRGIN_B;
9390 if(toY == 0) board[VIRGIN][toX] &= ~VIRGIN_W; // loss by capture
9391 if(toY == BOARD_HEIGHT-1) board[VIRGIN][toX] &= ~VIRGIN_B;
9394 if (fromX == toX && fromY == toY) return;
9396 piece = board[fromY][fromX]; /* [HGM] remember, for Shogi promotion */
9397 king = piece < (int) BlackPawn ? WhiteKing : BlackKing; /* [HGM] Knightmate simplify testing for castling */
9398 if(gameInfo.variant == VariantKnightmate)
9399 king += (int) WhiteUnicorn - (int) WhiteKing;
9401 /* Code added by Tord: */
9402 /* FRC castling assumed when king captures friendly rook. [HGM] or RxK for S-Chess */
9403 if (board[fromY][fromX] == WhiteKing && board[toY][toX] == WhiteRook ||
9404 board[fromY][fromX] == WhiteRook && board[toY][toX] == WhiteKing) {
9405 board[fromY][fromX] = EmptySquare;
9406 board[toY][toX] = EmptySquare;
9407 if((toX > fromX) != (piece == WhiteRook)) {
9408 board[0][BOARD_RGHT-2] = WhiteKing; board[0][BOARD_RGHT-3] = WhiteRook;
9410 board[0][BOARD_LEFT+2] = WhiteKing; board[0][BOARD_LEFT+3] = WhiteRook;
9412 } else if (board[fromY][fromX] == BlackKing && board[toY][toX] == BlackRook ||
9413 board[fromY][fromX] == BlackRook && board[toY][toX] == BlackKing) {
9414 board[fromY][fromX] = EmptySquare;
9415 board[toY][toX] = EmptySquare;
9416 if((toX > fromX) != (piece == BlackRook)) {
9417 board[BOARD_HEIGHT-1][BOARD_RGHT-2] = BlackKing; board[BOARD_HEIGHT-1][BOARD_RGHT-3] = BlackRook;
9419 board[BOARD_HEIGHT-1][BOARD_LEFT+2] = BlackKing; board[BOARD_HEIGHT-1][BOARD_LEFT+3] = BlackRook;
9421 /* End of code added by Tord */
9423 } else if (board[fromY][fromX] == king
9424 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
9425 && toY == fromY && toX > fromX+1) {
9426 board[fromY][fromX] = EmptySquare;
9427 board[toY][toX] = king;
9428 board[toY][toX-1] = board[fromY][BOARD_RGHT-1];
9429 board[fromY][BOARD_RGHT-1] = EmptySquare;
9430 } else if (board[fromY][fromX] == king
9431 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
9432 && toY == fromY && toX < fromX-1) {
9433 board[fromY][fromX] = EmptySquare;
9434 board[toY][toX] = king;
9435 board[toY][toX+1] = board[fromY][BOARD_LEFT];
9436 board[fromY][BOARD_LEFT] = EmptySquare;
9437 } else if ((board[fromY][fromX] == WhitePawn && gameInfo.variant != VariantXiangqi ||
9438 board[fromY][fromX] == WhiteLance && gameInfo.variant != VariantSuper && gameInfo.variant != VariantShogi)
9439 && toY >= BOARD_HEIGHT-promoRank && promoChar // defaulting to Q is done elsewhere
9441 /* white pawn promotion */
9442 board[toY][toX] = CharToPiece(ToUpper(promoChar));
9443 if((gameInfo.variant==VariantBughouse || gameInfo.variant==VariantCrazyhouse)
9444 && PieceToChar(PROMOTED board[toY][toX]) == '~') /* [HGM] use shadow piece (if available) */
9445 board[toY][toX] = (ChessSquare) (PROMOTED board[toY][toX]);
9446 board[fromY][fromX] = EmptySquare;
9447 } else if ((fromY >= BOARD_HEIGHT>>1)
9449 && gameInfo.variant != VariantXiangqi
9450 && gameInfo.variant != VariantBerolina
9451 && (board[fromY][fromX] == WhitePawn)
9452 && (board[toY][toX] == EmptySquare)) {
9453 board[fromY][fromX] = EmptySquare;
9454 board[toY][toX] = WhitePawn;
9455 captured = board[toY - 1][toX];
9456 board[toY - 1][toX] = EmptySquare;
9457 } else if ((fromY == BOARD_HEIGHT-4)
9459 && gameInfo.variant == VariantBerolina
9460 && (board[fromY][fromX] == WhitePawn)
9461 && (board[toY][toX] == EmptySquare)) {
9462 board[fromY][fromX] = EmptySquare;
9463 board[toY][toX] = WhitePawn;
9464 if(oldEP & EP_BEROLIN_A) {
9465 captured = board[fromY][fromX-1];
9466 board[fromY][fromX-1] = EmptySquare;
9467 }else{ captured = board[fromY][fromX+1];
9468 board[fromY][fromX+1] = EmptySquare;
9470 } else if (board[fromY][fromX] == king
9471 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
9472 && toY == fromY && toX > fromX+1) {
9473 board[fromY][fromX] = EmptySquare;
9474 board[toY][toX] = king;
9475 board[toY][toX-1] = board[fromY][BOARD_RGHT-1];
9476 board[fromY][BOARD_RGHT-1] = EmptySquare;
9477 } else if (board[fromY][fromX] == king
9478 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
9479 && toY == fromY && toX < fromX-1) {
9480 board[fromY][fromX] = EmptySquare;
9481 board[toY][toX] = king;
9482 board[toY][toX+1] = board[fromY][BOARD_LEFT];
9483 board[fromY][BOARD_LEFT] = EmptySquare;
9484 } else if (fromY == 7 && fromX == 3
9485 && board[fromY][fromX] == BlackKing
9486 && toY == 7 && toX == 5) {
9487 board[fromY][fromX] = EmptySquare;
9488 board[toY][toX] = BlackKing;
9489 board[fromY][7] = EmptySquare;
9490 board[toY][4] = BlackRook;
9491 } else if (fromY == 7 && fromX == 3
9492 && board[fromY][fromX] == BlackKing
9493 && toY == 7 && toX == 1) {
9494 board[fromY][fromX] = EmptySquare;
9495 board[toY][toX] = BlackKing;
9496 board[fromY][0] = EmptySquare;
9497 board[toY][2] = BlackRook;
9498 } else if ((board[fromY][fromX] == BlackPawn && gameInfo.variant != VariantXiangqi ||
9499 board[fromY][fromX] == BlackLance && gameInfo.variant != VariantSuper && gameInfo.variant != VariantShogi)
9500 && toY < promoRank && promoChar
9502 /* black pawn promotion */
9503 board[toY][toX] = CharToPiece(ToLower(promoChar));
9504 if((gameInfo.variant==VariantBughouse || gameInfo.variant==VariantCrazyhouse)
9505 && PieceToChar(PROMOTED board[toY][toX]) == '~') /* [HGM] use shadow piece (if available) */
9506 board[toY][toX] = (ChessSquare) (PROMOTED board[toY][toX]);
9507 board[fromY][fromX] = EmptySquare;
9508 } else if ((fromY < BOARD_HEIGHT>>1)
9510 && gameInfo.variant != VariantXiangqi
9511 && gameInfo.variant != VariantBerolina
9512 && (board[fromY][fromX] == BlackPawn)
9513 && (board[toY][toX] == EmptySquare)) {
9514 board[fromY][fromX] = EmptySquare;
9515 board[toY][toX] = BlackPawn;
9516 captured = board[toY + 1][toX];
9517 board[toY + 1][toX] = EmptySquare;
9518 } else if ((fromY == 3)
9520 && gameInfo.variant == VariantBerolina
9521 && (board[fromY][fromX] == BlackPawn)
9522 && (board[toY][toX] == EmptySquare)) {
9523 board[fromY][fromX] = EmptySquare;
9524 board[toY][toX] = BlackPawn;
9525 if(oldEP & EP_BEROLIN_A) {
9526 captured = board[fromY][fromX-1];
9527 board[fromY][fromX-1] = EmptySquare;
9528 }else{ captured = board[fromY][fromX+1];
9529 board[fromY][fromX+1] = EmptySquare;
9532 board[toY][toX] = board[fromY][fromX];
9533 board[fromY][fromX] = EmptySquare;
9537 if (gameInfo.holdingsWidth != 0) {
9539 /* !!A lot more code needs to be written to support holdings */
9540 /* [HGM] OK, so I have written it. Holdings are stored in the */
9541 /* penultimate board files, so they are automaticlly stored */
9542 /* in the game history. */
9543 if (fromY == DROP_RANK || gameInfo.variant == VariantSChess
9544 && promoChar && piece != WhitePawn && piece != BlackPawn) {
9545 /* Delete from holdings, by decreasing count */
9546 /* and erasing image if necessary */
9547 p = fromY == DROP_RANK ? (int) fromX : CharToPiece(piece > BlackPawn ? ToLower(promoChar) : ToUpper(promoChar));
9548 if(p < (int) BlackPawn) { /* white drop */
9549 p -= (int)WhitePawn;
9550 p = PieceToNumber((ChessSquare)p);
9551 if(p >= gameInfo.holdingsSize) p = 0;
9552 if(--board[p][BOARD_WIDTH-2] <= 0)
9553 board[p][BOARD_WIDTH-1] = EmptySquare;
9554 if((int)board[p][BOARD_WIDTH-2] < 0)
9555 board[p][BOARD_WIDTH-2] = 0;
9556 } else { /* black drop */
9557 p -= (int)BlackPawn;
9558 p = PieceToNumber((ChessSquare)p);
9559 if(p >= gameInfo.holdingsSize) p = 0;
9560 if(--board[BOARD_HEIGHT-1-p][1] <= 0)
9561 board[BOARD_HEIGHT-1-p][0] = EmptySquare;
9562 if((int)board[BOARD_HEIGHT-1-p][1] < 0)
9563 board[BOARD_HEIGHT-1-p][1] = 0;
9566 if (captured != EmptySquare && gameInfo.holdingsSize > 0
9567 && gameInfo.variant != VariantBughouse && gameInfo.variant != VariantSChess ) {
9568 /* [HGM] holdings: Add to holdings, if holdings exist */
9569 if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand) {
9570 // [HGM] superchess: suppress flipping color of captured pieces by reverse pre-flip
9571 captured = (int) captured >= (int) BlackPawn ? BLACK_TO_WHITE captured : WHITE_TO_BLACK captured;
9574 if (p >= (int) BlackPawn) {
9575 p -= (int)BlackPawn;
9576 if(gameInfo.variant == VariantShogi && DEMOTED p >= 0) {
9577 /* in Shogi restore piece to its original first */
9578 captured = (ChessSquare) (DEMOTED captured);
9581 p = PieceToNumber((ChessSquare)p);
9582 if(p >= gameInfo.holdingsSize) { p = 0; captured = BlackPawn; }
9583 board[p][BOARD_WIDTH-2]++;
9584 board[p][BOARD_WIDTH-1] = BLACK_TO_WHITE captured;
9586 p -= (int)WhitePawn;
9587 if(gameInfo.variant == VariantShogi && DEMOTED p >= 0) {
9588 captured = (ChessSquare) (DEMOTED captured);
9591 p = PieceToNumber((ChessSquare)p);
9592 if(p >= gameInfo.holdingsSize) { p = 0; captured = WhitePawn; }
9593 board[BOARD_HEIGHT-1-p][1]++;
9594 board[BOARD_HEIGHT-1-p][0] = WHITE_TO_BLACK captured;
9597 } else if (gameInfo.variant == VariantAtomic) {
9598 if (captured != EmptySquare) {
9600 for (y = toY-1; y <= toY+1; y++) {
9601 for (x = toX-1; x <= toX+1; x++) {
9602 if (y >= 0 && y < BOARD_HEIGHT && x >= BOARD_LEFT && x < BOARD_RGHT &&
9603 board[y][x] != WhitePawn && board[y][x] != BlackPawn) {
9604 board[y][x] = EmptySquare;
9608 board[toY][toX] = EmptySquare;
9611 if(gameInfo.variant == VariantSChess && promoChar != NULLCHAR && promoChar != '=' && piece != WhitePawn && piece != BlackPawn) {
9612 board[fromY][fromX] = CharToPiece(piece < BlackPawn ? ToUpper(promoChar) : ToLower(promoChar)); // S-Chess gating
9614 if(promoChar == '+') {
9615 /* [HGM] Shogi-style promotions, to piece implied by original (Might overwrite ordinary Pawn promotion) */
9616 board[toY][toX] = (ChessSquare) (PROMOTED piece);
9617 } else if(!appData.testLegality && promoChar != NULLCHAR && promoChar != '=') { // without legality testing, unconditionally believe promoChar
9618 ChessSquare newPiece = CharToPiece(piece < BlackPawn ? ToUpper(promoChar) : ToLower(promoChar));
9619 if((newPiece <= WhiteMan || newPiece >= BlackPawn && newPiece <= BlackMan) // unpromoted piece specified
9620 && pieceToChar[PROMOTED newPiece] == '~') newPiece = PROMOTED newPiece; // but promoted version available
9621 board[toY][toX] = newPiece;
9623 if((gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand)
9624 && promoChar != NULLCHAR && gameInfo.holdingsSize) {
9625 // [HGM] superchess: take promotion piece out of holdings
9626 int k = PieceToNumber(CharToPiece(ToUpper(promoChar)));
9627 if((int)piece < (int)BlackPawn) { // determine stm from piece color
9628 if(!--board[k][BOARD_WIDTH-2])
9629 board[k][BOARD_WIDTH-1] = EmptySquare;
9631 if(!--board[BOARD_HEIGHT-1-k][1])
9632 board[BOARD_HEIGHT-1-k][0] = EmptySquare;
9638 /* Updates forwardMostMove */
9640 MakeMove (int fromX, int fromY, int toX, int toY, int promoChar)
9642 // forwardMostMove++; // [HGM] bare: moved downstream
9644 (void) CoordsToAlgebraic(boards[forwardMostMove],
9645 PosFlags(forwardMostMove),
9646 fromY, fromX, toY, toX, promoChar,
9647 parseList[forwardMostMove]);
9649 if(serverMoves != NULL) { /* [HGM] write moves on file for broadcasting (should be separate routine, really) */
9650 int timeLeft; static int lastLoadFlag=0; int king, piece;
9651 piece = boards[forwardMostMove][fromY][fromX];
9652 king = piece < (int) BlackPawn ? WhiteKing : BlackKing;
9653 if(gameInfo.variant == VariantKnightmate)
9654 king += (int) WhiteUnicorn - (int) WhiteKing;
9655 if(forwardMostMove == 0) {
9656 if(gameMode == MachinePlaysBlack || gameMode == BeginningOfGame)
9657 fprintf(serverMoves, "%s;", UserName());
9658 else if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b')
9659 fprintf(serverMoves, "%s;", second.tidy);
9660 fprintf(serverMoves, "%s;", first.tidy);
9661 if(gameMode == MachinePlaysWhite)
9662 fprintf(serverMoves, "%s;", UserName());
9663 else if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w')
9664 fprintf(serverMoves, "%s;", second.tidy);
9665 } else fprintf(serverMoves, loadFlag|lastLoadFlag ? ":" : ";");
9666 lastLoadFlag = loadFlag;
9668 fprintf(serverMoves, "%c%c:%c%c", AAA+fromX, ONE+fromY, AAA+toX, ONE+toY);
9669 // print castling suffix
9670 if( toY == fromY && piece == king ) {
9672 fprintf(serverMoves, ":%c%c:%c%c", AAA+BOARD_RGHT-1, ONE+fromY, AAA+toX-1,ONE+toY);
9674 fprintf(serverMoves, ":%c%c:%c%c", AAA+BOARD_LEFT, ONE+fromY, AAA+toX+1,ONE+toY);
9677 if( (boards[forwardMostMove][fromY][fromX] == WhitePawn ||
9678 boards[forwardMostMove][fromY][fromX] == BlackPawn ) &&
9679 boards[forwardMostMove][toY][toX] == EmptySquare
9680 && fromX != toX && fromY != toY)
9681 fprintf(serverMoves, ":%c%c:%c%c", AAA+fromX, ONE+fromY, AAA+toX, ONE+fromY);
9683 if(promoChar != NULLCHAR) {
9684 if(fromY == 0 || fromY == BOARD_HEIGHT-1)
9685 fprintf(serverMoves, ":%c%c:%c%c", WhiteOnMove(forwardMostMove) ? 'w' : 'b',
9686 ToLower(promoChar), AAA+fromX, ONE+fromY); // Seirawan gating
9687 else fprintf(serverMoves, ":%c:%c%c", ToLower(promoChar), AAA+toX, ONE+toY);
9690 char buf[MOVE_LEN*2], *p; int len;
9691 fprintf(serverMoves, "/%d/%d",
9692 pvInfoList[forwardMostMove].depth, pvInfoList[forwardMostMove].score);
9693 if(forwardMostMove+1 & 1) timeLeft = whiteTimeRemaining/1000;
9694 else timeLeft = blackTimeRemaining/1000;
9695 fprintf(serverMoves, "/%d", timeLeft);
9696 strncpy(buf, parseList[forwardMostMove], MOVE_LEN*2);
9697 if(p = strchr(buf, '/')) *p = NULLCHAR; else
9698 if(p = strchr(buf, '=')) *p = NULLCHAR;
9699 len = strlen(buf); if(len > 1 && buf[len-2] != '-') buf[len-2] = NULLCHAR; // strip to-square
9700 fprintf(serverMoves, "/%s", buf);
9702 fflush(serverMoves);
9705 if (forwardMostMove+1 > framePtr) { // [HGM] vari: do not run into saved variations..
9706 GameEnds(GameUnfinished, _("Game too long; increase MAX_MOVES and recompile"), GE_XBOARD);
9709 UnLoadPV(); // [HGM] pv: if we are looking at a PV, abort this
9710 if (commentList[forwardMostMove+1] != NULL) {
9711 free(commentList[forwardMostMove+1]);
9712 commentList[forwardMostMove+1] = NULL;
9714 CopyBoard(boards[forwardMostMove+1], boards[forwardMostMove]);
9715 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[forwardMostMove+1]);
9716 // forwardMostMove++; // [HGM] bare: moved to after ApplyMove, to make sure clock interrupt finds complete board
9717 SwitchClocks(forwardMostMove+1); // [HGM] race: incrementing move nr inside
9718 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
9719 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
9720 adjustedClock = FALSE;
9721 gameInfo.result = GameUnfinished;
9722 if (gameInfo.resultDetails != NULL) {
9723 free(gameInfo.resultDetails);
9724 gameInfo.resultDetails = NULL;
9726 CoordsToComputerAlgebraic(fromY, fromX, toY, toX, promoChar,
9727 moveList[forwardMostMove - 1]);
9728 switch (MateTest(boards[forwardMostMove], PosFlags(forwardMostMove)) ) {
9734 if(gameInfo.variant != VariantShogi)
9735 strcat(parseList[forwardMostMove - 1], "+");
9739 strcat(parseList[forwardMostMove - 1], "#");
9745 /* Updates currentMove if not pausing */
9747 ShowMove (int fromX, int fromY, int toX, int toY)
9749 int instant = (gameMode == PlayFromGameFile) ?
9750 (matchMode || (appData.timeDelay == 0 && !pausing)) : pausing;
9751 if(appData.noGUI) return;
9752 if (!pausing || gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
9754 if (forwardMostMove == currentMove + 1) {
9755 AnimateMove(boards[forwardMostMove - 1],
9756 fromX, fromY, toX, toY);
9759 currentMove = forwardMostMove;
9762 if (instant) return;
9764 DisplayMove(currentMove - 1);
9765 if (!pausing || gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
9766 if (appData.highlightLastMove) { // [HGM] moved to after DrawPosition, as with arrow it could redraw old board
9767 SetHighlights(fromX, fromY, toX, toY);
9770 DrawPosition(FALSE, boards[currentMove]);
9771 DisplayBothClocks();
9772 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
9776 SendEgtPath (ChessProgramState *cps)
9777 { /* [HGM] EGT: match formats given in feature with those given by user, and send info for each match */
9778 char buf[MSG_SIZ], name[MSG_SIZ], *p;
9780 if((p = cps->egtFormats) == NULL || appData.egtFormats == NULL) return;
9783 char c, *q = name+1, *r, *s;
9785 name[0] = ','; // extract next format name from feature and copy with prefixed ','
9786 while(*p && *p != ',') *q++ = *p++;
9788 if( appData.defaultPathEGTB && appData.defaultPathEGTB[0] &&
9789 strcmp(name, ",nalimov:") == 0 ) {
9790 // take nalimov path from the menu-changeable option first, if it is defined
9791 snprintf(buf, MSG_SIZ, "egtpath nalimov %s\n", appData.defaultPathEGTB);
9792 SendToProgram(buf,cps); // send egtbpath command for nalimov
9794 if( (s = StrStr(appData.egtFormats, name+1)) == appData.egtFormats ||
9795 (s = StrStr(appData.egtFormats, name)) != NULL) {
9796 // format name occurs amongst user-supplied formats, at beginning or immediately after comma
9797 s = r = StrStr(s, ":") + 1; // beginning of path info
9798 while(*r && *r != ',') r++; // path info is everything upto next ';' or end of string
9799 c = *r; *r = 0; // temporarily null-terminate path info
9800 *--q = 0; // strip of trailig ':' from name
9801 snprintf(buf, MSG_SIZ, "egtpath %s %s\n", name+1, s);
9803 SendToProgram(buf,cps); // send egtbpath command for this format
9805 if(*p == ',') p++; // read away comma to position for next format name
9810 InitChessProgram (ChessProgramState *cps, int setup)
9811 /* setup needed to setup FRC opening position */
9813 char buf[MSG_SIZ], b[MSG_SIZ]; int overruled;
9814 if (appData.noChessProgram) return;
9815 hintRequested = FALSE;
9816 bookRequested = FALSE;
9818 ParseFeatures(appData.features[cps == &second], cps); // [HGM] allow user to overrule features
9819 /* [HGM] some new WB protocol commands to configure engine are sent now, if engine supports them */
9820 /* moved to before sending initstring in 4.3.15, so Polyglot can delay UCI 'isready' to recepton of 'new' */
9821 if(cps->memSize) { /* [HGM] memory */
9822 snprintf(buf, MSG_SIZ, "memory %d\n", appData.defaultHashSize + appData.defaultCacheSizeEGTB);
9823 SendToProgram(buf, cps);
9825 SendEgtPath(cps); /* [HGM] EGT */
9826 if(cps->maxCores) { /* [HGM] SMP: (protocol specified must be last settings command before new!) */
9827 snprintf(buf, MSG_SIZ, "cores %d\n", appData.smpCores);
9828 SendToProgram(buf, cps);
9831 SendToProgram(cps->initString, cps);
9832 if (gameInfo.variant != VariantNormal &&
9833 gameInfo.variant != VariantLoadable
9834 /* [HGM] also send variant if board size non-standard */
9835 || gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0
9837 char *v = VariantName(gameInfo.variant);
9838 if (cps->protocolVersion != 1 && StrStr(cps->variants, v) == NULL) {
9839 /* [HGM] in protocol 1 we have to assume all variants valid */
9840 snprintf(buf, MSG_SIZ, _("Variant %s not supported by %s"), v, cps->tidy);
9841 DisplayFatalError(buf, 0, 1);
9845 /* [HGM] make prefix for non-standard board size. Awkward testing... */
9846 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
9847 if( gameInfo.variant == VariantXiangqi )
9848 overruled = gameInfo.boardWidth != 9 || gameInfo.boardHeight != 10 || gameInfo.holdingsSize != 0;
9849 if( gameInfo.variant == VariantShogi )
9850 overruled = gameInfo.boardWidth != 9 || gameInfo.boardHeight != 9 || gameInfo.holdingsSize != 7;
9851 if( gameInfo.variant == VariantBughouse || gameInfo.variant == VariantCrazyhouse )
9852 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 5;
9853 if( gameInfo.variant == VariantCapablanca || gameInfo.variant == VariantCapaRandom ||
9854 gameInfo.variant == VariantGothic || gameInfo.variant == VariantFalcon || gameInfo.variant == VariantJanus )
9855 overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
9856 if( gameInfo.variant == VariantCourier )
9857 overruled = gameInfo.boardWidth != 12 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
9858 if( gameInfo.variant == VariantSuper )
9859 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 8;
9860 if( gameInfo.variant == VariantGreat )
9861 overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 8;
9862 if( gameInfo.variant == VariantSChess )
9863 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 7;
9864 if( gameInfo.variant == VariantGrand )
9865 overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 10 || gameInfo.holdingsSize != 7;
9868 snprintf(b, MSG_SIZ, "%dx%d+%d_%s", gameInfo.boardWidth, gameInfo.boardHeight,
9869 gameInfo.holdingsSize, VariantName(gameInfo.variant)); // cook up sized variant name
9870 /* [HGM] varsize: try first if this defiant size variant is specifically known */
9871 if(StrStr(cps->variants, b) == NULL) {
9872 // specific sized variant not known, check if general sizing allowed
9873 if (cps->protocolVersion != 1) { // for protocol 1 we cannot check and hope for the best
9874 if(StrStr(cps->variants, "boardsize") == NULL) {
9875 snprintf(buf, MSG_SIZ, "Board size %dx%d+%d not supported by %s",
9876 gameInfo.boardWidth, gameInfo.boardHeight, gameInfo.holdingsSize, cps->tidy);
9877 DisplayFatalError(buf, 0, 1);
9880 /* [HGM] here we really should compare with the maximum supported board size */
9883 } else snprintf(b, MSG_SIZ,"%s", VariantName(gameInfo.variant));
9884 snprintf(buf, MSG_SIZ, "variant %s\n", b);
9885 SendToProgram(buf, cps);
9887 currentlyInitializedVariant = gameInfo.variant;
9889 /* [HGM] send opening position in FRC to first engine */
9891 SendToProgram("force\n", cps);
9893 /* engine is now in force mode! Set flag to wake it up after first move. */
9894 setboardSpoiledMachineBlack = 1;
9898 snprintf(buf, sizeof(buf), "ics %s\n", appData.icsActive ? appData.icsHost : "-");
9899 SendToProgram(buf, cps);
9901 cps->maybeThinking = FALSE;
9902 cps->offeredDraw = 0;
9903 if (!appData.icsActive) {
9904 SendTimeControl(cps, movesPerSession, timeControl,
9905 timeIncrement, appData.searchDepth,
9908 if (appData.showThinking
9909 // [HGM] thinking: four options require thinking output to be sent
9910 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp()
9912 SendToProgram("post\n", cps);
9914 SendToProgram("hard\n", cps);
9915 if (!appData.ponderNextMove) {
9916 /* Warning: "easy" is a toggle in GNU Chess, so don't send
9917 it without being sure what state we are in first. "hard"
9918 is not a toggle, so that one is OK.
9920 SendToProgram("easy\n", cps);
9923 snprintf(buf, MSG_SIZ, "ping %d\n", ++cps->lastPing);
9924 SendToProgram(buf, cps);
9926 cps->initDone = TRUE;
9927 ClearEngineOutputPane(cps == &second);
9932 StartChessProgram (ChessProgramState *cps)
9937 if (appData.noChessProgram) return;
9938 cps->initDone = FALSE;
9940 if (strcmp(cps->host, "localhost") == 0) {
9941 err = StartChildProcess(cps->program, cps->dir, &cps->pr);
9942 } else if (*appData.remoteShell == NULLCHAR) {
9943 err = OpenRcmd(cps->host, appData.remoteUser, cps->program, &cps->pr);
9945 if (*appData.remoteUser == NULLCHAR) {
9946 snprintf(buf, sizeof(buf), "%s %s %s", appData.remoteShell, cps->host,
9949 snprintf(buf, sizeof(buf), "%s %s -l %s %s", appData.remoteShell,
9950 cps->host, appData.remoteUser, cps->program);
9952 err = StartChildProcess(buf, "", &cps->pr);
9956 snprintf(buf, MSG_SIZ, _("Startup failure on '%s'"), cps->program);
9957 DisplayError(buf, err); // [HGM] bit of a rough kludge: ignore failure, (which XBoard would do anyway), and let I/O discover it
9958 if(cps != &first) return;
9959 appData.noChessProgram = TRUE;
9962 // DisplayFatalError(buf, err, 1);
9963 // cps->pr = NoProc;
9968 cps->isr = AddInputSource(cps->pr, TRUE, ReceiveFromProgram, cps);
9969 if (cps->protocolVersion > 1) {
9970 snprintf(buf, MSG_SIZ, "xboard\nprotover %d\n", cps->protocolVersion);
9971 cps->nrOptions = 0; // [HGM] options: clear all engine-specific options
9972 cps->comboCnt = 0; // and values of combo boxes
9973 SendToProgram(buf, cps);
9975 SendToProgram("xboard\n", cps);
9980 TwoMachinesEventIfReady P((void))
9982 static int curMess = 0;
9983 if (first.lastPing != first.lastPong) {
9984 if(curMess != 1) DisplayMessage("", _("Waiting for first chess program")); curMess = 1;
9985 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10); // [HGM] fast: lowered from 1000
9988 if (second.lastPing != second.lastPong) {
9989 if(curMess != 2) DisplayMessage("", _("Waiting for second chess program")); curMess = 2;
9990 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10); // [HGM] fast: lowered from 1000
9993 DisplayMessage("", ""); curMess = 0;
9999 MakeName (char *template)
10003 static char buf[MSG_SIZ];
10007 clock = time((time_t *)NULL);
10008 tm = localtime(&clock);
10010 while(*p++ = *template++) if(p[-1] == '%') {
10011 switch(*template++) {
10012 case 0: *p = 0; return buf;
10013 case 'Y': i = tm->tm_year+1900; break;
10014 case 'y': i = tm->tm_year-100; break;
10015 case 'M': i = tm->tm_mon+1; break;
10016 case 'd': i = tm->tm_mday; break;
10017 case 'h': i = tm->tm_hour; break;
10018 case 'm': i = tm->tm_min; break;
10019 case 's': i = tm->tm_sec; break;
10022 snprintf(p-1, MSG_SIZ-10 - (p - buf), "%02d", i); p += strlen(p);
10028 CountPlayers (char *p)
10031 while(p = strchr(p, '\n')) p++, n++; // count participants
10036 WriteTourneyFile (char *results, FILE *f)
10037 { // write tournament parameters on tourneyFile; on success return the stream pointer for closing
10038 if(f == NULL) f = fopen(appData.tourneyFile, "w");
10039 if(f == NULL) DisplayError(_("Could not write on tourney file"), 0); else {
10040 // create a file with tournament description
10041 fprintf(f, "-participants {%s}\n", appData.participants);
10042 fprintf(f, "-seedBase %d\n", appData.seedBase);
10043 fprintf(f, "-tourneyType %d\n", appData.tourneyType);
10044 fprintf(f, "-tourneyCycles %d\n", appData.tourneyCycles);
10045 fprintf(f, "-defaultMatchGames %d\n", appData.defaultMatchGames);
10046 fprintf(f, "-syncAfterRound %s\n", appData.roundSync ? "true" : "false");
10047 fprintf(f, "-syncAfterCycle %s\n", appData.cycleSync ? "true" : "false");
10048 fprintf(f, "-saveGameFile \"%s\"\n", appData.saveGameFile);
10049 fprintf(f, "-loadGameFile \"%s\"\n", appData.loadGameFile);
10050 fprintf(f, "-loadGameIndex %d\n", appData.loadGameIndex);
10051 fprintf(f, "-loadPositionFile \"%s\"\n", appData.loadPositionFile);
10052 fprintf(f, "-loadPositionIndex %d\n", appData.loadPositionIndex);
10053 fprintf(f, "-rewindIndex %d\n", appData.rewindIndex);
10054 fprintf(f, "-discourageOwnBooks %s\n", appData.defNoBook ? "true" : "false");
10056 fprintf(f, "-searchTime \"%d:%02d\"\n", searchTime/60, searchTime%60);
10058 fprintf(f, "-mps %d\n", appData.movesPerSession);
10059 fprintf(f, "-tc %s\n", appData.timeControl);
10060 fprintf(f, "-inc %.2f\n", appData.timeIncrement);
10062 fprintf(f, "-results \"%s\"\n", results);
10067 char *command[MAXENGINES], *mnemonic[MAXENGINES];
10070 Substitute (char *participants, int expunge)
10072 int i, changed, changes=0, nPlayers=0;
10073 char *p, *q, *r, buf[MSG_SIZ];
10074 if(participants == NULL) return;
10075 if(appData.tourneyFile[0] == NULLCHAR) { free(participants); return; }
10076 r = p = participants; q = appData.participants;
10077 while(*p && *p == *q) {
10078 if(*p == '\n') r = p+1, nPlayers++;
10081 if(*p) { // difference
10082 while(*p && *p++ != '\n');
10083 while(*q && *q++ != '\n');
10084 changed = nPlayers;
10085 changes = 1 + (strcmp(p, q) != 0);
10087 if(changes == 1) { // a single engine mnemonic was changed
10088 q = r; while(*q) nPlayers += (*q++ == '\n');
10089 p = buf; while(*r && (*p = *r++) != '\n') p++;
10091 NamesToList(firstChessProgramNames, command, mnemonic, "all");
10092 for(i=1; mnemonic[i]; i++) if(!strcmp(buf, mnemonic[i])) break;
10093 if(mnemonic[i]) { // The substitute is valid
10095 if(appData.tourneyFile[0] && (f = fopen(appData.tourneyFile, "r+")) ) {
10096 flock(fileno(f), LOCK_EX);
10097 ParseArgsFromFile(f);
10098 fseek(f, 0, SEEK_SET);
10099 FREE(appData.participants); appData.participants = participants;
10100 if(expunge) { // erase results of replaced engine
10101 int len = strlen(appData.results), w, b, dummy;
10102 for(i=0; i<len; i++) {
10103 Pairing(i, nPlayers, &w, &b, &dummy);
10104 if((w == changed || b == changed) && appData.results[i] == '*') {
10105 DisplayError(_("You cannot replace an engine while it is engaged!\nTerminate its game first."), 0);
10110 for(i=0; i<len; i++) {
10111 Pairing(i, nPlayers, &w, &b, &dummy);
10112 if(w == changed || b == changed) appData.results[i] = ' '; // mark as not played
10115 WriteTourneyFile(appData.results, f);
10116 fclose(f); // release lock
10119 } else DisplayError(_("No engine with the name you gave is installed"), 0);
10121 if(changes == 0) DisplayError(_("First change an engine by editing the participants list\nof the Tournament Options dialog"), 0);
10122 if(changes > 1) DisplayError(_("You can only change one engine at the time"), 0);
10123 free(participants);
10128 CheckPlayers (char *participants)
10131 char buf[MSG_SIZ], *p;
10132 NamesToList(firstChessProgramNames, command, mnemonic, "all");
10133 while(p = strchr(participants, '\n')) {
10135 for(i=1; mnemonic[i]; i++) if(!strcmp(participants, mnemonic[i])) break;
10137 snprintf(buf, MSG_SIZ, _("No engine %s is installed"), participants);
10139 DisplayError(buf, 0);
10143 participants = p + 1;
10149 CreateTourney (char *name)
10152 if(matchMode && strcmp(name, appData.tourneyFile)) {
10153 ASSIGN(name, appData.tourneyFile); //do not allow change of tourneyfile while playing
10155 if(name[0] == NULLCHAR) {
10156 if(appData.participants[0])
10157 DisplayError(_("You must supply a tournament file,\nfor storing the tourney progress"), 0);
10160 f = fopen(name, "r");
10161 if(f) { // file exists
10162 ASSIGN(appData.tourneyFile, name);
10163 ParseArgsFromFile(f); // parse it
10165 if(!appData.participants[0]) return 0; // ignore tourney file if non-existing & no participants
10166 if(CountPlayers(appData.participants) < (appData.tourneyType>0 ? appData.tourneyType+1 : 2)) {
10167 DisplayError(_("Not enough participants"), 0);
10170 if(CheckPlayers(appData.participants)) return 0;
10171 ASSIGN(appData.tourneyFile, name);
10172 if(appData.tourneyType < 0) appData.defaultMatchGames = 1; // Swiss forces games/pairing = 1
10173 if((f = WriteTourneyFile("", NULL)) == NULL) return 0;
10176 appData.noChessProgram = FALSE;
10177 appData.clockMode = TRUE;
10183 NamesToList (char *names, char **engineList, char **engineMnemonic, char *group)
10185 char buf[MSG_SIZ], *p, *q;
10186 int i=1, header, skip, all = !strcmp(group, "all"), depth = 0;
10187 insert = names; // afterwards, this global will point just after last retrieved engine line or group end in the 'names'
10188 skip = !all && group[0]; // if group requested, we start in skip mode
10189 for(;*names && depth >= 0 && i < MAXENGINES-1; names = p) {
10190 p = names; q = buf; header = 0;
10191 while(*p && *p != '\n') *q++ = *p++;
10193 if(*p == '\n') p++;
10194 if(buf[0] == '#') {
10195 if(strstr(buf, "# end") == buf) { if(!--depth) insert = p; continue; } // leave group, and suppress printing label
10196 depth++; // we must be entering a new group
10197 if(all) continue; // suppress printing group headers when complete list requested
10199 if(skip && !strcmp(group, buf)) { depth = 0; skip = FALSE; } // start when we reach requested group
10201 if(depth != header && !all || skip) continue; // skip contents of group (but print first-level header)
10202 if(engineList[i]) free(engineList[i]);
10203 engineList[i] = strdup(buf);
10204 if(buf[0] != '#') insert = p, TidyProgramName(engineList[i], "localhost", buf); // group headers not tidied
10205 if(engineMnemonic[i]) free(engineMnemonic[i]);
10206 if((q = strstr(engineList[i]+2, "variant")) && q[-2]== ' ' && (q[-1]=='/' || q[-1]=='-') && (q[7]==' ' || q[7]=='=')) {
10208 sscanf(q + 8, "%s", buf + strlen(buf));
10211 engineMnemonic[i] = strdup(buf);
10214 engineList[i] = engineMnemonic[i] = NULL;
10218 // following implemented as macro to avoid type limitations
10219 #define SWAP(item, temp) temp = appData.item[0]; appData.item[0] = appData.item[n]; appData.item[n] = temp;
10222 SwapEngines (int n)
10223 { // swap settings for first engine and other engine (so far only some selected options)
10228 SWAP(chessProgram, p)
10230 SWAP(hasOwnBookUCI, h)
10231 SWAP(protocolVersion, h)
10233 SWAP(scoreIsAbsolute, h)
10238 SWAP(engOptions, p)
10239 SWAP(engInitString, p)
10240 SWAP(computerString, p)
10242 SWAP(fenOverride, p)
10244 SWAP(accumulateTC, h)
10249 GetEngineLine (char *s, int n)
10253 extern char *icsNames;
10254 if(!s || !*s) return 0;
10255 NamesToList(n >= 10 ? icsNames : firstChessProgramNames, command, mnemonic, "all");
10256 for(i=1; mnemonic[i]; i++) if(!strcmp(s, mnemonic[i])) break;
10257 if(!mnemonic[i]) return 0;
10258 if(n == 11) return 1; // just testing if there was a match
10259 snprintf(buf, MSG_SIZ, "-%s %s", n == 10 ? "icshost" : "fcp", command[i]);
10260 if(n == 1) SwapEngines(n);
10261 ParseArgsFromString(buf);
10262 if(n == 1) SwapEngines(n);
10263 if(n == 0 && *appData.secondChessProgram == NULLCHAR) {
10264 SwapEngines(1); // set second same as first if not yet set (to suppress WB startup dialog)
10265 ParseArgsFromString(buf);
10271 SetPlayer (int player, char *p)
10272 { // [HGM] find the engine line of the partcipant given by number, and parse its options.
10274 char buf[MSG_SIZ], *engineName;
10275 for(i=0; i<player; i++) p = strchr(p, '\n') + 1;
10276 engineName = strdup(p); if(p = strchr(engineName, '\n')) *p = NULLCHAR;
10277 for(i=1; command[i]; i++) if(!strcmp(mnemonic[i], engineName)) break;
10279 snprintf(buf, MSG_SIZ, "-fcp %s", command[i]);
10280 ParseArgsFromString(resetOptions); appData.fenOverride[0] = NULL; appData.pvSAN[0] = FALSE;
10281 appData.firstHasOwnBookUCI = !appData.defNoBook; appData.protocolVersion[0] = PROTOVER;
10282 ParseArgsFromString(buf);
10288 char *recentEngines;
10291 RecentEngineEvent (int nr)
10294 // SwapEngines(1); // bump first to second
10295 // ReplaceEngine(&second, 1); // and load it there
10296 NamesToList(firstChessProgramNames, command, mnemonic, "all"); // get mnemonics of installed engines
10297 n = SetPlayer(nr, recentEngines); // select new (using original menu order!)
10298 if(mnemonic[n]) { // if somehow the engine with the selected nickname is no longer found in the list, we skip
10299 ReplaceEngine(&first, 0);
10300 FloatToFront(&appData.recentEngineList, command[n]);
10305 Pairing (int nr, int nPlayers, int *whitePlayer, int *blackPlayer, int *syncInterval)
10306 { // determine players from game number
10307 int curCycle, curRound, curPairing, gamesPerCycle, gamesPerRound, roundsPerCycle=1, pairingsPerRound=1;
10309 if(appData.tourneyType == 0) {
10310 roundsPerCycle = (nPlayers - 1) | 1;
10311 pairingsPerRound = nPlayers / 2;
10312 } else if(appData.tourneyType > 0) {
10313 roundsPerCycle = nPlayers - appData.tourneyType;
10314 pairingsPerRound = appData.tourneyType;
10316 gamesPerRound = pairingsPerRound * appData.defaultMatchGames;
10317 gamesPerCycle = gamesPerRound * roundsPerCycle;
10318 appData.matchGames = gamesPerCycle * appData.tourneyCycles - 1; // fake like all games are one big match
10319 curCycle = nr / gamesPerCycle; nr %= gamesPerCycle;
10320 curRound = nr / gamesPerRound; nr %= gamesPerRound;
10321 curPairing = nr / appData.defaultMatchGames; nr %= appData.defaultMatchGames;
10322 matchGame = nr + curCycle * appData.defaultMatchGames + 1; // fake game nr that loads correct game or position from file
10323 roundNr = (curCycle * roundsPerCycle + curRound) * appData.defaultMatchGames + nr + 1;
10325 if(appData.cycleSync) *syncInterval = gamesPerCycle;
10326 if(appData.roundSync) *syncInterval = gamesPerRound;
10328 if(appData.debugMode) fprintf(debugFP, "cycle=%d, round=%d, pairing=%d curGame=%d\n", curCycle, curRound, curPairing, matchGame);
10330 if(appData.tourneyType == 0) {
10331 if(curPairing == (nPlayers-1)/2 ) {
10332 *whitePlayer = curRound;
10333 *blackPlayer = nPlayers - 1; // this is the 'bye' when nPlayer is odd
10335 *whitePlayer = curRound - (nPlayers-1)/2 + curPairing;
10336 if(*whitePlayer < 0) *whitePlayer += nPlayers-1+(nPlayers&1);
10337 *blackPlayer = curRound + (nPlayers-1)/2 - curPairing;
10338 if(*blackPlayer >= nPlayers-1+(nPlayers&1)) *blackPlayer -= nPlayers-1+(nPlayers&1);
10340 } else if(appData.tourneyType > 1) {
10341 *blackPlayer = curPairing; // in multi-gauntlet, assign gauntlet engines to second, so first an be kept loaded during round
10342 *whitePlayer = curRound + appData.tourneyType;
10343 } else if(appData.tourneyType > 0) {
10344 *whitePlayer = curPairing;
10345 *blackPlayer = curRound + appData.tourneyType;
10348 // take care of white/black alternation per round.
10349 // For cycles and games this is already taken care of by default, derived from matchGame!
10350 return curRound & 1;
10354 NextTourneyGame (int nr, int *swapColors)
10355 { // !!!major kludge!!! fiddle appData settings to get everything in order for next tourney game
10357 int whitePlayer, blackPlayer, firstBusy=1000000000, syncInterval = 0, nPlayers;
10359 if(appData.tourneyFile[0] == NULLCHAR) return 1; // no tourney, always allow next game
10360 tf = fopen(appData.tourneyFile, "r");
10361 if(tf == NULL) { DisplayFatalError(_("Bad tournament file"), 0, 1); return 0; }
10362 ParseArgsFromFile(tf); fclose(tf);
10363 InitTimeControls(); // TC might be altered from tourney file
10365 nPlayers = CountPlayers(appData.participants); // count participants
10366 if(appData.tourneyType < 0) syncInterval = nPlayers/2; else
10367 *swapColors = Pairing(nr<0 ? 0 : nr, nPlayers, &whitePlayer, &blackPlayer, &syncInterval);
10370 p = q = appData.results;
10371 while(*q) if(*q++ == '*' || q[-1] == ' ') { firstBusy = q - p - 1; break; }
10372 if(firstBusy/syncInterval < (nextGame/syncInterval)) {
10373 DisplayMessage(_("Waiting for other game(s)"),"");
10374 waitingForGame = TRUE;
10375 ScheduleDelayedEvent(NextMatchGame, 1000); // wait for all games of previous round to finish
10378 waitingForGame = FALSE;
10381 if(appData.tourneyType < 0) {
10382 if(nr>=0 && !pairingReceived) {
10384 if(pairing.pr == NoProc) {
10385 if(!appData.pairingEngine[0]) {
10386 DisplayFatalError(_("No pairing engine specified"), 0, 1);
10389 StartChessProgram(&pairing); // starts the pairing engine
10391 snprintf(buf, 1<<16, "results %d %s\n", nPlayers, appData.results);
10392 SendToProgram(buf, &pairing);
10393 snprintf(buf, 1<<16, "pairing %d\n", nr+1);
10394 SendToProgram(buf, &pairing);
10395 return 0; // wait for pairing engine to answer (which causes NextTourneyGame to be called again...
10397 pairingReceived = 0; // ... so we continue here
10399 appData.matchGames = appData.tourneyCycles * syncInterval - 1;
10400 whitePlayer = savedWhitePlayer-1; blackPlayer = savedBlackPlayer-1;
10401 matchGame = 1; roundNr = nr / syncInterval + 1;
10404 if(first.pr != NoProc && second.pr != NoProc || nr<0) return 1; // engines already loaded
10406 // redefine engines, engine dir, etc.
10407 NamesToList(firstChessProgramNames, command, mnemonic, "all"); // get mnemonics of installed engines
10408 if(first.pr == NoProc) {
10409 SetPlayer(whitePlayer, appData.participants); // find white player amongst it, and parse its engine line
10410 InitEngine(&first, 0); // initialize ChessProgramStates based on new settings.
10412 if(second.pr == NoProc) {
10414 SetPlayer(blackPlayer, appData.participants); // find black player amongst it, and parse its engine line
10415 SwapEngines(1); // and make that valid for second engine by swapping
10416 InitEngine(&second, 1);
10418 CommonEngineInit(); // after this TwoMachinesEvent will create correct engine processes
10419 UpdateLogos(FALSE); // leave display to ModeHiglight()
10425 { // performs game initialization that does not invoke engines, and then tries to start the game
10426 int res, firstWhite, swapColors = 0;
10427 if(!NextTourneyGame(nextGame, &swapColors)) return; // this sets matchGame, -fcp / -scp and other options for next game, if needed
10428 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
10430 snprintf(buf, MSG_SIZ, appData.nameOfDebugFile, nextGame+1); // expand name of debug file with %d in it
10431 if(strcmp(buf, currentDebugFile)) { // name has changed
10432 FILE *f = fopen(buf, "w");
10433 if(f) { // if opening the new file failed, just keep using the old one
10434 ASSIGN(currentDebugFile, buf);
10438 if(appData.serverFileName) {
10439 if(serverFP) fclose(serverFP);
10440 serverFP = fopen(appData.serverFileName, "w");
10441 if(serverFP && first.pr != NoProc) fprintf(serverFP, "StartChildProcess (dir=\".\") .\\%s\n", first.tidy);
10442 if(serverFP && second.pr != NoProc) fprintf(serverFP, "StartChildProcess (dir=\".\") .\\%s\n", second.tidy);
10446 firstWhite = appData.firstPlaysBlack ^ (matchGame & 1 | appData.sameColorGames > 1); // non-incremental default
10447 firstWhite ^= swapColors; // reverses if NextTourneyGame says we are in an odd round
10448 first.twoMachinesColor = firstWhite ? "white\n" : "black\n"; // perform actual color assignement
10449 second.twoMachinesColor = firstWhite ? "black\n" : "white\n";
10450 appData.noChessProgram = (first.pr == NoProc); // kludge to prevent Reset from starting up chess program
10451 if(appData.loadGameIndex == -2) srandom(appData.seedBase + 68163*(nextGame & ~1)); // deterministic seed to force same opening
10452 Reset(FALSE, first.pr != NoProc);
10453 res = LoadGameOrPosition(matchGame); // setup game
10454 appData.noChessProgram = FALSE; // LoadGameOrPosition might call Reset too!
10455 if(!res) return; // abort when bad game/pos file
10456 TwoMachinesEvent();
10460 UserAdjudicationEvent (int result)
10462 ChessMove gameResult = GameIsDrawn;
10465 gameResult = WhiteWins;
10467 else if( result < 0 ) {
10468 gameResult = BlackWins;
10471 if( gameMode == TwoMachinesPlay ) {
10472 GameEnds( gameResult, "User adjudication", GE_XBOARD );
10477 // [HGM] save: calculate checksum of game to make games easily identifiable
10479 StringCheckSum (char *s)
10482 if(s==NULL) return 0;
10483 while(*s) i = i*259 + *s++;
10491 for(i=backwardMostMove; i<forwardMostMove; i++) {
10492 sum += pvInfoList[i].depth;
10493 sum += StringCheckSum(parseList[i]);
10494 sum += StringCheckSum(commentList[i]);
10497 if(i>1 && sum==0) sum++; // make sure never zero for non-empty game
10498 return sum + StringCheckSum(commentList[i]);
10499 } // end of save patch
10502 GameEnds (ChessMove result, char *resultDetails, int whosays)
10504 GameMode nextGameMode;
10506 char buf[MSG_SIZ], popupRequested = 0, *ranking = NULL;
10508 if(endingGame) return; /* [HGM] crash: forbid recursion */
10510 if(twoBoards) { // [HGM] dual: switch back to one board
10511 twoBoards = partnerUp = 0; InitDrawingSizes(-2, 0);
10512 DrawPosition(TRUE, partnerBoard); // observed game becomes foreground
10514 if (appData.debugMode) {
10515 fprintf(debugFP, "GameEnds(%d, %s, %d)\n",
10516 result, resultDetails ? resultDetails : "(null)", whosays);
10519 fromX = fromY = -1; // [HGM] abort any move the user is entering.
10521 if(pausing) PauseEvent(); // can happen when we abort a paused game (New Game or Quit)
10523 if (appData.icsActive && (whosays == GE_ENGINE || whosays >= GE_ENGINE1)) {
10524 /* If we are playing on ICS, the server decides when the
10525 game is over, but the engine can offer to draw, claim
10529 if (appData.zippyPlay && first.initDone) {
10530 if (result == GameIsDrawn) {
10531 /* In case draw still needs to be claimed */
10532 SendToICS(ics_prefix);
10533 SendToICS("draw\n");
10534 } else if (StrCaseStr(resultDetails, "resign")) {
10535 SendToICS(ics_prefix);
10536 SendToICS("resign\n");
10540 endingGame = 0; /* [HGM] crash */
10544 /* If we're loading the game from a file, stop */
10545 if (whosays == GE_FILE) {
10546 (void) StopLoadGameTimer();
10550 /* Cancel draw offers */
10551 first.offeredDraw = second.offeredDraw = 0;
10553 /* If this is an ICS game, only ICS can really say it's done;
10554 if not, anyone can. */
10555 isIcsGame = (gameMode == IcsPlayingWhite ||
10556 gameMode == IcsPlayingBlack ||
10557 gameMode == IcsObserving ||
10558 gameMode == IcsExamining);
10560 if (!isIcsGame || whosays == GE_ICS) {
10561 /* OK -- not an ICS game, or ICS said it was done */
10563 if (!isIcsGame && !appData.noChessProgram)
10564 SetUserThinkingEnables();
10566 /* [HGM] if a machine claims the game end we verify this claim */
10567 if(gameMode == TwoMachinesPlay && appData.testClaims) {
10568 if(appData.testLegality && whosays >= GE_ENGINE1 ) {
10570 ChessMove trueResult = (ChessMove) -1;
10572 claimer = whosays == GE_ENGINE1 ? /* color of claimer */
10573 first.twoMachinesColor[0] :
10574 second.twoMachinesColor[0] ;
10576 // [HGM] losers: because the logic is becoming a bit hairy, determine true result first
10577 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_CHECKMATE) {
10578 /* [HGM] verify: engine mate claims accepted if they were flagged */
10579 trueResult = WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins;
10581 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_WINS) { // added code for games where being mated is a win
10582 /* [HGM] verify: engine mate claims accepted if they were flagged */
10583 trueResult = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
10585 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_STALEMATE) { // only used to indicate draws now
10586 trueResult = GameIsDrawn; // default; in variants where stalemate loses, Status is CHECKMATE
10589 // now verify win claims, but not in drop games, as we don't understand those yet
10590 if( (gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper
10591 || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand) &&
10592 (result == WhiteWins && claimer == 'w' ||
10593 result == BlackWins && claimer == 'b' ) ) { // case to verify: engine claims own win
10594 if (appData.debugMode) {
10595 fprintf(debugFP, "result=%d sp=%d move=%d\n",
10596 result, (signed char)boards[forwardMostMove][EP_STATUS], forwardMostMove);
10598 if(result != trueResult) {
10599 snprintf(buf, MSG_SIZ, "False win claim: '%s'", resultDetails);
10600 result = claimer == 'w' ? BlackWins : WhiteWins;
10601 resultDetails = buf;
10604 if( result == GameIsDrawn && (signed char)boards[forwardMostMove][EP_STATUS] > EP_DRAWS
10605 && (forwardMostMove <= backwardMostMove ||
10606 (signed char)boards[forwardMostMove-1][EP_STATUS] > EP_DRAWS ||
10607 (claimer=='b')==(forwardMostMove&1))
10609 /* [HGM] verify: draws that were not flagged are false claims */
10610 snprintf(buf, MSG_SIZ, "False draw claim: '%s'", resultDetails);
10611 result = claimer == 'w' ? BlackWins : WhiteWins;
10612 resultDetails = buf;
10614 /* (Claiming a loss is accepted no questions asked!) */
10616 /* [HGM] bare: don't allow bare King to win */
10617 if((gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper
10618 || gameInfo.variant == VariantGreat || gameInfo.variant == VariantGrand)
10619 && gameInfo.variant != VariantLosers && gameInfo.variant != VariantGiveaway
10620 && gameInfo.variant != VariantSuicide // [HGM] losers: except in losers, of course...
10621 && result != GameIsDrawn)
10622 { int i, j, k=0, color = (result==WhiteWins ? (int)WhitePawn : (int)BlackPawn);
10623 for(j=BOARD_LEFT; j<BOARD_RGHT; j++) for(i=0; i<BOARD_HEIGHT; i++) {
10624 int p = (signed char)boards[forwardMostMove][i][j] - color;
10625 if(p >= 0 && p <= (int)WhiteKing) k++;
10627 if (appData.debugMode) {
10628 fprintf(debugFP, "GE(%d, %s, %d) bare king k=%d color=%d\n",
10629 result, resultDetails ? resultDetails : "(null)", whosays, k, color);
10632 result = GameIsDrawn;
10633 snprintf(buf, MSG_SIZ, "%s but bare king", resultDetails);
10634 resultDetails = buf;
10640 if(serverMoves != NULL && !loadFlag) { char c = '=';
10641 if(result==WhiteWins) c = '+';
10642 if(result==BlackWins) c = '-';
10643 if(resultDetails != NULL)
10644 fprintf(serverMoves, ";%c;%s\n", c, resultDetails), fflush(serverMoves);
10646 if (resultDetails != NULL) {
10647 gameInfo.result = result;
10648 gameInfo.resultDetails = StrSave(resultDetails);
10650 /* display last move only if game was not loaded from file */
10651 if ((whosays != GE_FILE) && (currentMove == forwardMostMove))
10652 DisplayMove(currentMove - 1);
10654 if (forwardMostMove != 0) {
10655 if (gameMode != PlayFromGameFile && gameMode != EditGame
10656 && lastSavedGame != GameCheckSum() // [HGM] save: suppress duplicates
10658 if (*appData.saveGameFile != NULLCHAR) {
10659 SaveGameToFile(appData.saveGameFile, TRUE);
10660 } else if (appData.autoSaveGames) {
10663 if (*appData.savePositionFile != NULLCHAR) {
10664 SavePositionToFile(appData.savePositionFile);
10666 AddGameToBook(FALSE); // Only does something during Monte-Carlo book building
10670 /* Tell program how game ended in case it is learning */
10671 /* [HGM] Moved this to after saving the PGN, just in case */
10672 /* engine died and we got here through time loss. In that */
10673 /* case we will get a fatal error writing the pipe, which */
10674 /* would otherwise lose us the PGN. */
10675 /* [HGM] crash: not needed anymore, but doesn't hurt; */
10676 /* output during GameEnds should never be fatal anymore */
10677 if (gameMode == MachinePlaysWhite ||
10678 gameMode == MachinePlaysBlack ||
10679 gameMode == TwoMachinesPlay ||
10680 gameMode == IcsPlayingWhite ||
10681 gameMode == IcsPlayingBlack ||
10682 gameMode == BeginningOfGame) {
10684 snprintf(buf, MSG_SIZ, "result %s {%s}\n", PGNResult(result),
10686 if (first.pr != NoProc) {
10687 SendToProgram(buf, &first);
10689 if (second.pr != NoProc &&
10690 gameMode == TwoMachinesPlay) {
10691 SendToProgram(buf, &second);
10696 if (appData.icsActive) {
10697 if (appData.quietPlay &&
10698 (gameMode == IcsPlayingWhite ||
10699 gameMode == IcsPlayingBlack)) {
10700 SendToICS(ics_prefix);
10701 SendToICS("set shout 1\n");
10703 nextGameMode = IcsIdle;
10704 ics_user_moved = FALSE;
10705 /* clean up premove. It's ugly when the game has ended and the
10706 * premove highlights are still on the board.
10709 gotPremove = FALSE;
10710 ClearPremoveHighlights();
10711 DrawPosition(FALSE, boards[currentMove]);
10713 if (whosays == GE_ICS) {
10716 if (gameMode == IcsPlayingWhite)
10718 else if(gameMode == IcsPlayingBlack)
10719 PlayIcsLossSound();
10722 if (gameMode == IcsPlayingBlack)
10724 else if(gameMode == IcsPlayingWhite)
10725 PlayIcsLossSound();
10728 PlayIcsDrawSound();
10731 PlayIcsUnfinishedSound();
10734 } else if (gameMode == EditGame ||
10735 gameMode == PlayFromGameFile ||
10736 gameMode == AnalyzeMode ||
10737 gameMode == AnalyzeFile) {
10738 nextGameMode = gameMode;
10740 nextGameMode = EndOfGame;
10745 nextGameMode = gameMode;
10748 if (appData.noChessProgram) {
10749 gameMode = nextGameMode;
10751 endingGame = 0; /* [HGM] crash */
10756 /* Put first chess program into idle state */
10757 if (first.pr != NoProc &&
10758 (gameMode == MachinePlaysWhite ||
10759 gameMode == MachinePlaysBlack ||
10760 gameMode == TwoMachinesPlay ||
10761 gameMode == IcsPlayingWhite ||
10762 gameMode == IcsPlayingBlack ||
10763 gameMode == BeginningOfGame)) {
10764 SendToProgram("force\n", &first);
10765 if (first.usePing) {
10767 snprintf(buf, MSG_SIZ, "ping %d\n", ++first.lastPing);
10768 SendToProgram(buf, &first);
10771 } else if (result != GameUnfinished || nextGameMode == IcsIdle) {
10772 /* Kill off first chess program */
10773 if (first.isr != NULL)
10774 RemoveInputSource(first.isr);
10777 if (first.pr != NoProc) {
10779 DoSleep( appData.delayBeforeQuit );
10780 SendToProgram("quit\n", &first);
10781 DoSleep( appData.delayAfterQuit );
10782 DestroyChildProcess(first.pr, first.useSigterm);
10786 if (second.reuse) {
10787 /* Put second chess program into idle state */
10788 if (second.pr != NoProc &&
10789 gameMode == TwoMachinesPlay) {
10790 SendToProgram("force\n", &second);
10791 if (second.usePing) {
10793 snprintf(buf, MSG_SIZ, "ping %d\n", ++second.lastPing);
10794 SendToProgram(buf, &second);
10797 } else if (result != GameUnfinished || nextGameMode == IcsIdle) {
10798 /* Kill off second chess program */
10799 if (second.isr != NULL)
10800 RemoveInputSource(second.isr);
10803 if (second.pr != NoProc) {
10804 DoSleep( appData.delayBeforeQuit );
10805 SendToProgram("quit\n", &second);
10806 DoSleep( appData.delayAfterQuit );
10807 DestroyChildProcess(second.pr, second.useSigterm);
10809 second.pr = NoProc;
10812 if (matchMode && (gameMode == TwoMachinesPlay || waitingForGame && exiting)) {
10813 char resChar = '=';
10817 if (first.twoMachinesColor[0] == 'w') {
10820 second.matchWins++;
10825 if (first.twoMachinesColor[0] == 'b') {
10828 second.matchWins++;
10831 case GameUnfinished:
10837 if(waitingForGame) resChar = ' '; // quit while waiting for round sync: unreserve already reserved game
10838 if(appData.tourneyFile[0]){ // [HGM] we are in a tourney; update tourney file with game result
10839 if(appData.afterGame && appData.afterGame[0]) RunCommand(appData.afterGame);
10840 ReserveGame(nextGame, resChar); // sets nextGame
10841 if(nextGame > appData.matchGames) appData.tourneyFile[0] = 0, ranking = TourneyStandings(3); // tourney is done
10842 else ranking = strdup("busy"); //suppress popup when aborted but not finished
10843 } else roundNr = nextGame = matchGame + 1; // normal match, just increment; round equals matchGame
10845 if (nextGame <= appData.matchGames && !abortMatch) {
10846 gameMode = nextGameMode;
10847 matchGame = nextGame; // this will be overruled in tourney mode!
10848 GetTimeMark(&pauseStart); // [HGM] matchpause: stipulate a pause
10849 ScheduleDelayedEvent(NextMatchGame, 10); // but start game immediately (as it will wait out the pause itself)
10850 endingGame = 0; /* [HGM] crash */
10853 gameMode = nextGameMode;
10854 snprintf(buf, MSG_SIZ, _("Match %s vs. %s: final score %d-%d-%d"),
10855 first.tidy, second.tidy,
10856 first.matchWins, second.matchWins,
10857 appData.matchGames - (first.matchWins + second.matchWins));
10858 if(!appData.tourneyFile[0]) matchGame++, DisplayTwoMachinesTitle(); // [HGM] update result in window title
10859 if(ranking && strcmp(ranking, "busy") && appData.afterTourney && appData.afterTourney[0]) RunCommand(appData.afterTourney);
10860 popupRequested++; // [HGM] crash: postpone to after resetting endingGame
10861 if (appData.firstPlaysBlack) { // [HGM] match: back to original for next match
10862 first.twoMachinesColor = "black\n";
10863 second.twoMachinesColor = "white\n";
10865 first.twoMachinesColor = "white\n";
10866 second.twoMachinesColor = "black\n";
10870 if ((gameMode == AnalyzeMode || gameMode == AnalyzeFile) &&
10871 !(nextGameMode == AnalyzeMode || nextGameMode == AnalyzeFile))
10873 gameMode = nextGameMode;
10875 endingGame = 0; /* [HGM] crash */
10876 if(popupRequested) { // [HGM] crash: this calls GameEnds recursively through ExitEvent! Make it a harmless tail recursion.
10877 if(matchMode == TRUE) { // match through command line: exit with or without popup
10879 ToNrEvent(forwardMostMove);
10880 if(strcmp(ranking, "busy")) DisplayFatalError(ranking, 0, 0);
10882 } else DisplayFatalError(buf, 0, 0);
10883 } else { // match through menu; just stop, with or without popup
10884 matchMode = FALSE; appData.matchGames = matchGame = roundNr = 0;
10887 if(strcmp(ranking, "busy")) DisplayNote(ranking);
10888 } else DisplayNote(buf);
10890 if(ranking) free(ranking);
10894 /* Assumes program was just initialized (initString sent).
10895 Leaves program in force mode. */
10897 FeedMovesToProgram (ChessProgramState *cps, int upto)
10901 if (appData.debugMode)
10902 fprintf(debugFP, "Feeding %smoves %d through %d to %s chess program\n",
10903 startedFromSetupPosition ? "position and " : "",
10904 backwardMostMove, upto, cps->which);
10905 if(currentlyInitializedVariant != gameInfo.variant) {
10907 // [HGM] variantswitch: make engine aware of new variant
10908 if(cps->protocolVersion > 1 && StrStr(cps->variants, VariantName(gameInfo.variant)) == NULL)
10909 return; // [HGM] refrain from feeding moves altogether if variant is unsupported!
10910 snprintf(buf, MSG_SIZ, "variant %s\n", VariantName(gameInfo.variant));
10911 SendToProgram(buf, cps);
10912 currentlyInitializedVariant = gameInfo.variant;
10914 SendToProgram("force\n", cps);
10915 if (startedFromSetupPosition) {
10916 SendBoard(cps, backwardMostMove);
10917 if (appData.debugMode) {
10918 fprintf(debugFP, "feedMoves\n");
10921 for (i = backwardMostMove; i < upto; i++) {
10922 SendMoveToProgram(i, cps);
10928 ResurrectChessProgram ()
10930 /* The chess program may have exited.
10931 If so, restart it and feed it all the moves made so far. */
10932 static int doInit = 0;
10934 if (appData.noChessProgram) return 1;
10936 if(matchMode && appData.tourneyFile[0]) { // [HGM] tourney: make sure we get features after engine replacement. (Should we always do this?)
10937 if(WaitForEngine(&first, TwoMachinesEventIfReady)) { doInit = 1; return 0; } // request to do init on next visit
10938 if(!doInit) return 1; // this replaces testing first.pr != NoProc, which is true when we get here, but first time no reason to abort
10939 doInit = 0; // we fell through (first time after starting the engine); make sure it doesn't happen again
10941 if (first.pr != NoProc) return 1;
10942 StartChessProgram(&first);
10944 InitChessProgram(&first, FALSE);
10945 FeedMovesToProgram(&first, currentMove);
10947 if (!first.sendTime) {
10948 /* can't tell gnuchess what its clock should read,
10949 so we bow to its notion. */
10951 timeRemaining[0][currentMove] = whiteTimeRemaining;
10952 timeRemaining[1][currentMove] = blackTimeRemaining;
10955 if ((gameMode == AnalyzeMode || gameMode == AnalyzeFile ||
10956 appData.icsEngineAnalyze) && first.analysisSupport) {
10957 SendToProgram("analyze\n", &first);
10958 first.analyzing = TRUE;
10964 * Button procedures
10967 Reset (int redraw, int init)
10971 if (appData.debugMode) {
10972 fprintf(debugFP, "Reset(%d, %d) from gameMode %d\n",
10973 redraw, init, gameMode);
10975 CleanupTail(); // [HGM] vari: delete any stored variations
10976 CommentPopDown(); // [HGM] make sure no comments to the previous game keep hanging on
10977 pausing = pauseExamInvalid = FALSE;
10978 startedFromSetupPosition = blackPlaysFirst = FALSE;
10980 whiteFlag = blackFlag = FALSE;
10981 userOfferedDraw = FALSE;
10982 hintRequested = bookRequested = FALSE;
10983 first.maybeThinking = FALSE;
10984 second.maybeThinking = FALSE;
10985 first.bookSuspend = FALSE; // [HGM] book
10986 second.bookSuspend = FALSE;
10987 thinkOutput[0] = NULLCHAR;
10988 lastHint[0] = NULLCHAR;
10989 ClearGameInfo(&gameInfo);
10990 gameInfo.variant = StringToVariant(appData.variant);
10991 ics_user_moved = ics_clock_paused = FALSE;
10992 ics_getting_history = H_FALSE;
10994 white_holding[0] = black_holding[0] = NULLCHAR;
10995 ClearProgramStats();
10996 opponentKibitzes = FALSE; // [HGM] kibitz: do not reserve space in engine-output window in zippy mode
11000 flipView = appData.flipView;
11001 ClearPremoveHighlights();
11002 gotPremove = FALSE;
11003 alarmSounded = FALSE;
11005 GameEnds(EndOfFile, NULL, GE_PLAYER);
11006 if(appData.serverMovesName != NULL) {
11007 /* [HGM] prepare to make moves file for broadcasting */
11008 clock_t t = clock();
11009 if(serverMoves != NULL) fclose(serverMoves);
11010 serverMoves = fopen(appData.serverMovesName, "r");
11011 if(serverMoves != NULL) {
11012 fclose(serverMoves);
11013 /* delay 15 sec before overwriting, so all clients can see end */
11014 while(clock()-t < appData.serverPause*CLOCKS_PER_SEC);
11016 serverMoves = fopen(appData.serverMovesName, "w");
11020 gameMode = BeginningOfGame;
11022 if(appData.icsActive) gameInfo.variant = VariantNormal;
11023 currentMove = forwardMostMove = backwardMostMove = 0;
11024 MarkTargetSquares(1);
11025 InitPosition(redraw);
11026 for (i = 0; i < MAX_MOVES; i++) {
11027 if (commentList[i] != NULL) {
11028 free(commentList[i]);
11029 commentList[i] = NULL;
11033 timeRemaining[0][0] = whiteTimeRemaining;
11034 timeRemaining[1][0] = blackTimeRemaining;
11036 if (first.pr == NoProc) {
11037 StartChessProgram(&first);
11040 InitChessProgram(&first, startedFromSetupPosition);
11043 DisplayMessage("", "");
11044 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
11045 lastSavedGame = 0; // [HGM] save: make sure next game counts as unsaved
11046 ClearMap(); // [HGM] exclude: invalidate map
11050 AutoPlayGameLoop ()
11053 if (!AutoPlayOneMove())
11055 if (matchMode || appData.timeDelay == 0)
11057 if (appData.timeDelay < 0)
11059 StartLoadGameTimer((long)(1000.0f * appData.timeDelay));
11067 ReloadGame(1); // next game
11073 int fromX, fromY, toX, toY;
11075 if (appData.debugMode) {
11076 fprintf(debugFP, "AutoPlayOneMove(): current %d\n", currentMove);
11079 if (gameMode != PlayFromGameFile && gameMode != AnalyzeFile)
11082 if (gameMode == AnalyzeFile && currentMove > backwardMostMove) {
11083 pvInfoList[currentMove].depth = programStats.depth;
11084 pvInfoList[currentMove].score = programStats.score;
11085 pvInfoList[currentMove].time = 0;
11086 if(currentMove < forwardMostMove) AppendComment(currentMove+1, lastPV[0], 2);
11089 if (currentMove >= forwardMostMove) {
11090 if(gameMode == AnalyzeFile) {
11091 if(appData.loadGameIndex == -1) {
11092 GameEnds(EndOfFile, NULL, GE_FILE);
11093 ScheduleDelayedEvent(AnalyzeNextGame, 10);
11095 ExitAnalyzeMode(); SendToProgram("force\n", &first);
11098 // gameMode = EndOfGame;
11099 // ModeHighlight();
11101 /* [AS] Clear current move marker at the end of a game */
11102 /* HistorySet(parseList, backwardMostMove, forwardMostMove, -1); */
11107 toX = moveList[currentMove][2] - AAA;
11108 toY = moveList[currentMove][3] - ONE;
11110 if (moveList[currentMove][1] == '@') {
11111 if (appData.highlightLastMove) {
11112 SetHighlights(-1, -1, toX, toY);
11115 fromX = moveList[currentMove][0] - AAA;
11116 fromY = moveList[currentMove][1] - ONE;
11118 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove); /* [AS] */
11120 AnimateMove(boards[currentMove], fromX, fromY, toX, toY);
11122 if (appData.highlightLastMove) {
11123 SetHighlights(fromX, fromY, toX, toY);
11126 DisplayMove(currentMove);
11127 SendMoveToProgram(currentMove++, &first);
11128 DisplayBothClocks();
11129 DrawPosition(FALSE, boards[currentMove]);
11130 // [HGM] PV info: always display, routine tests if empty
11131 DisplayComment(currentMove - 1, commentList[currentMove]);
11137 LoadGameOneMove (ChessMove readAhead)
11139 int fromX = 0, fromY = 0, toX = 0, toY = 0, done;
11140 char promoChar = NULLCHAR;
11141 ChessMove moveType;
11142 char move[MSG_SIZ];
11145 if (gameMode != PlayFromGameFile && gameMode != AnalyzeFile &&
11146 gameMode != AnalyzeMode && gameMode != Training) {
11151 yyboardindex = forwardMostMove;
11152 if (readAhead != EndOfFile) {
11153 moveType = readAhead;
11155 if (gameFileFP == NULL)
11157 moveType = (ChessMove) Myylex();
11161 switch (moveType) {
11163 if (appData.debugMode)
11164 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
11167 /* append the comment but don't display it */
11168 AppendComment(currentMove, p, FALSE);
11171 case WhiteCapturesEnPassant:
11172 case BlackCapturesEnPassant:
11173 case WhitePromotion:
11174 case BlackPromotion:
11175 case WhiteNonPromotion:
11176 case BlackNonPromotion:
11178 case WhiteKingSideCastle:
11179 case WhiteQueenSideCastle:
11180 case BlackKingSideCastle:
11181 case BlackQueenSideCastle:
11182 case WhiteKingSideCastleWild:
11183 case WhiteQueenSideCastleWild:
11184 case BlackKingSideCastleWild:
11185 case BlackQueenSideCastleWild:
11187 case WhiteHSideCastleFR:
11188 case WhiteASideCastleFR:
11189 case BlackHSideCastleFR:
11190 case BlackASideCastleFR:
11192 if (appData.debugMode)
11193 fprintf(debugFP, "Parsed %s into %s\n", yy_text, currentMoveString);
11194 fromX = currentMoveString[0] - AAA;
11195 fromY = currentMoveString[1] - ONE;
11196 toX = currentMoveString[2] - AAA;
11197 toY = currentMoveString[3] - ONE;
11198 promoChar = currentMoveString[4];
11203 if (appData.debugMode)
11204 fprintf(debugFP, "Parsed %s into %s\n", yy_text, currentMoveString);
11205 fromX = moveType == WhiteDrop ?
11206 (int) CharToPiece(ToUpper(currentMoveString[0])) :
11207 (int) CharToPiece(ToLower(currentMoveString[0]));
11209 toX = currentMoveString[2] - AAA;
11210 toY = currentMoveString[3] - ONE;
11216 case GameUnfinished:
11217 if (appData.debugMode)
11218 fprintf(debugFP, "Parsed game end: %s\n", yy_text);
11219 p = strchr(yy_text, '{');
11220 if (p == NULL) p = strchr(yy_text, '(');
11223 if (p[0] == '0' || p[0] == '1' || p[0] == '*') p = "";
11225 q = strchr(p, *p == '{' ? '}' : ')');
11226 if (q != NULL) *q = NULLCHAR;
11229 while(q = strchr(p, '\n')) *q = ' '; // [HGM] crush linefeeds in result message
11230 GameEnds(moveType, p, GE_FILE);
11232 if (cmailMsgLoaded) {
11234 flipView = WhiteOnMove(currentMove);
11235 if (moveType == GameUnfinished) flipView = !flipView;
11236 if (appData.debugMode)
11237 fprintf(debugFP, "Setting flipView to %d\n", flipView) ;
11242 if (appData.debugMode)
11243 fprintf(debugFP, "Parser hit end of file\n");
11244 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
11250 if (WhiteOnMove(currentMove)) {
11251 GameEnds(BlackWins, "Black mates", GE_FILE);
11253 GameEnds(WhiteWins, "White mates", GE_FILE);
11257 GameEnds(GameIsDrawn, "Stalemate", GE_FILE);
11263 case MoveNumberOne:
11264 if (lastLoadGameStart == GNUChessGame) {
11265 /* GNUChessGames have numbers, but they aren't move numbers */
11266 if (appData.debugMode)
11267 fprintf(debugFP, "Parser ignoring: '%s' (%d)\n",
11268 yy_text, (int) moveType);
11269 return LoadGameOneMove(EndOfFile); /* tail recursion */
11271 /* else fall thru */
11276 /* Reached start of next game in file */
11277 if (appData.debugMode)
11278 fprintf(debugFP, "Parsed start of next game: %s\n", yy_text);
11279 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
11285 if (WhiteOnMove(currentMove)) {
11286 GameEnds(BlackWins, "Black mates", GE_FILE);
11288 GameEnds(WhiteWins, "White mates", GE_FILE);
11292 GameEnds(GameIsDrawn, "Stalemate", GE_FILE);
11298 case PositionDiagram: /* should not happen; ignore */
11299 case ElapsedTime: /* ignore */
11300 case NAG: /* ignore */
11301 if (appData.debugMode)
11302 fprintf(debugFP, "Parser ignoring: '%s' (%d)\n",
11303 yy_text, (int) moveType);
11304 return LoadGameOneMove(EndOfFile); /* tail recursion */
11307 if (appData.testLegality) {
11308 if (appData.debugMode)
11309 fprintf(debugFP, "Parsed IllegalMove: %s\n", yy_text);
11310 snprintf(move, MSG_SIZ, _("Illegal move: %d.%s%s"),
11311 (forwardMostMove / 2) + 1,
11312 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
11313 DisplayError(move, 0);
11316 if (appData.debugMode)
11317 fprintf(debugFP, "Parsed %s into IllegalMove %s\n",
11318 yy_text, currentMoveString);
11319 fromX = currentMoveString[0] - AAA;
11320 fromY = currentMoveString[1] - ONE;
11321 toX = currentMoveString[2] - AAA;
11322 toY = currentMoveString[3] - ONE;
11323 promoChar = currentMoveString[4];
11327 case AmbiguousMove:
11328 if (appData.debugMode)
11329 fprintf(debugFP, "Parsed AmbiguousMove: %s\n", yy_text);
11330 snprintf(move, MSG_SIZ, _("Ambiguous move: %d.%s%s"),
11331 (forwardMostMove / 2) + 1,
11332 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
11333 DisplayError(move, 0);
11338 case ImpossibleMove:
11339 if (appData.debugMode)
11340 fprintf(debugFP, "Parsed ImpossibleMove (type = %d): %s\n", moveType, yy_text);
11341 snprintf(move, MSG_SIZ, _("Illegal move: %d.%s%s"),
11342 (forwardMostMove / 2) + 1,
11343 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
11344 DisplayError(move, 0);
11350 if (appData.matchMode || (appData.timeDelay == 0 && !pausing)) {
11351 DrawPosition(FALSE, boards[currentMove]);
11352 DisplayBothClocks();
11353 if (!appData.matchMode) // [HGM] PV info: routine tests if empty
11354 DisplayComment(currentMove - 1, commentList[currentMove]);
11356 (void) StopLoadGameTimer();
11358 cmailOldMove = forwardMostMove;
11361 /* currentMoveString is set as a side-effect of yylex */
11363 thinkOutput[0] = NULLCHAR;
11364 MakeMove(fromX, fromY, toX, toY, promoChar);
11365 currentMove = forwardMostMove;
11370 /* Load the nth game from the given file */
11372 LoadGameFromFile (char *filename, int n, char *title, int useList)
11377 if (strcmp(filename, "-") == 0) {
11381 f = fopen(filename, "rb");
11383 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
11384 DisplayError(buf, errno);
11388 if (fseek(f, 0, 0) == -1) {
11389 /* f is not seekable; probably a pipe */
11392 if (useList && n == 0) {
11393 int error = GameListBuild(f);
11395 DisplayError(_("Cannot build game list"), error);
11396 } else if (!ListEmpty(&gameList) &&
11397 ((ListGame *) gameList.tailPred)->number > 1) {
11398 GameListPopUp(f, title);
11405 return LoadGame(f, n, title, FALSE);
11410 MakeRegisteredMove ()
11412 int fromX, fromY, toX, toY;
11414 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
11415 switch (cmailMoveType[lastLoadGameNumber - 1]) {
11418 if (appData.debugMode)
11419 fprintf(debugFP, "Restoring %s for game %d\n",
11420 cmailMove[lastLoadGameNumber - 1], lastLoadGameNumber);
11422 thinkOutput[0] = NULLCHAR;
11423 safeStrCpy(moveList[currentMove], cmailMove[lastLoadGameNumber - 1], sizeof(moveList[currentMove])/sizeof(moveList[currentMove][0]));
11424 fromX = cmailMove[lastLoadGameNumber - 1][0] - AAA;
11425 fromY = cmailMove[lastLoadGameNumber - 1][1] - ONE;
11426 toX = cmailMove[lastLoadGameNumber - 1][2] - AAA;
11427 toY = cmailMove[lastLoadGameNumber - 1][3] - ONE;
11428 promoChar = cmailMove[lastLoadGameNumber - 1][4];
11429 MakeMove(fromX, fromY, toX, toY, promoChar);
11430 ShowMove(fromX, fromY, toX, toY);
11432 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
11439 if (WhiteOnMove(currentMove)) {
11440 GameEnds(BlackWins, "Black mates", GE_PLAYER);
11442 GameEnds(WhiteWins, "White mates", GE_PLAYER);
11447 GameEnds(GameIsDrawn, "Stalemate", GE_PLAYER);
11454 if (WhiteOnMove(currentMove)) {
11455 GameEnds(BlackWins, "White resigns", GE_PLAYER);
11457 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
11462 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
11473 /* Wrapper around LoadGame for use when a Cmail message is loaded */
11475 CmailLoadGame (FILE *f, int gameNumber, char *title, int useList)
11479 if (gameNumber > nCmailGames) {
11480 DisplayError(_("No more games in this message"), 0);
11483 if (f == lastLoadGameFP) {
11484 int offset = gameNumber - lastLoadGameNumber;
11486 cmailMsg[0] = NULLCHAR;
11487 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
11488 cmailMoveRegistered[lastLoadGameNumber - 1] = FALSE;
11489 nCmailMovesRegistered--;
11491 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
11492 if (cmailResult[lastLoadGameNumber - 1] == CMAIL_NEW_RESULT) {
11493 cmailResult[lastLoadGameNumber - 1] = CMAIL_NOT_RESULT;
11496 if (! RegisterMove()) return FALSE;
11500 retVal = LoadGame(f, gameNumber, title, useList);
11502 /* Make move registered during previous look at this game, if any */
11503 MakeRegisteredMove();
11505 if (cmailCommentList[lastLoadGameNumber - 1] != NULL) {
11506 commentList[currentMove]
11507 = StrSave(cmailCommentList[lastLoadGameNumber - 1]);
11508 DisplayComment(currentMove - 1, commentList[currentMove]);
11514 /* Support for LoadNextGame, LoadPreviousGame, ReloadSameGame */
11516 ReloadGame (int offset)
11518 int gameNumber = lastLoadGameNumber + offset;
11519 if (lastLoadGameFP == NULL) {
11520 DisplayError(_("No game has been loaded yet"), 0);
11523 if (gameNumber <= 0) {
11524 DisplayError(_("Can't back up any further"), 0);
11527 if (cmailMsgLoaded) {
11528 return CmailLoadGame(lastLoadGameFP, gameNumber,
11529 lastLoadGameTitle, lastLoadGameUseList);
11531 return LoadGame(lastLoadGameFP, gameNumber,
11532 lastLoadGameTitle, lastLoadGameUseList);
11536 int keys[EmptySquare+1];
11539 PositionMatches (Board b1, Board b2)
11542 switch(appData.searchMode) {
11543 case 1: return CompareWithRights(b1, b2);
11545 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11546 if(b2[r][f] != EmptySquare && b1[r][f] != b2[r][f]) return FALSE;
11550 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11551 if((b2[r][f] == WhitePawn || b2[r][f] == BlackPawn) && b1[r][f] != b2[r][f]) return FALSE;
11552 sum += keys[b1[r][f]] - keys[b2[r][f]];
11556 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11557 sum += keys[b1[r][f]] - keys[b2[r][f]];
11569 int pieceList[256], quickBoard[256];
11570 ChessSquare pieceType[256] = { EmptySquare };
11571 Board soughtBoard, reverseBoard, flipBoard, rotateBoard;
11572 int counts[EmptySquare], minSought[EmptySquare], minReverse[EmptySquare], maxSought[EmptySquare], maxReverse[EmptySquare];
11573 int soughtTotal, turn;
11574 Boolean epOK, flipSearch;
11577 unsigned char piece, to;
11580 #define DSIZE (250000)
11582 Move initialSpace[DSIZE+1000]; // gamble on that game will not be more than 500 moves
11583 Move *moveDatabase = initialSpace;
11584 unsigned int movePtr, dataSize = DSIZE;
11587 MakePieceList (Board board, int *counts)
11589 int r, f, n=Q_PROMO, total=0;
11590 for(r=0;r<EmptySquare;r++) counts[r] = 0; // piece-type counts
11591 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11592 int sq = f + (r<<4);
11593 if(board[r][f] == EmptySquare) quickBoard[sq] = 0; else {
11594 quickBoard[sq] = ++n;
11596 pieceType[n] = board[r][f];
11597 counts[board[r][f]]++;
11598 if(board[r][f] == WhiteKing) pieceList[1] = n; else
11599 if(board[r][f] == BlackKing) pieceList[2] = n; // remember which are Kings, for castling
11603 epOK = gameInfo.variant != VariantXiangqi && gameInfo.variant != VariantBerolina;
11608 PackMove (int fromX, int fromY, int toX, int toY, ChessSquare promoPiece)
11610 int sq = fromX + (fromY<<4);
11611 int piece = quickBoard[sq];
11612 quickBoard[sq] = 0;
11613 moveDatabase[movePtr].to = pieceList[piece] = sq = toX + (toY<<4);
11614 if(piece == pieceList[1] && fromY == toY && (toX > fromX+1 || toX < fromX-1) && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1) {
11615 int from = toX>fromX ? BOARD_RGHT-1 : BOARD_LEFT;
11616 moveDatabase[movePtr++].piece = Q_WCASTL;
11617 quickBoard[sq] = piece;
11618 piece = quickBoard[from]; quickBoard[from] = 0;
11619 moveDatabase[movePtr].to = pieceList[piece] = sq = toX>fromX ? sq-1 : sq+1;
11621 if(piece == pieceList[2] && fromY == toY && (toX > fromX+1 || toX < fromX-1) && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1) {
11622 int from = (toX>fromX ? BOARD_RGHT-1 : BOARD_LEFT) + (BOARD_HEIGHT-1 <<4);
11623 moveDatabase[movePtr++].piece = Q_BCASTL;
11624 quickBoard[sq] = piece;
11625 piece = quickBoard[from]; quickBoard[from] = 0;
11626 moveDatabase[movePtr].to = pieceList[piece] = sq = toX>fromX ? sq-1 : sq+1;
11628 if(epOK && (pieceType[piece] == WhitePawn || pieceType[piece] == BlackPawn) && fromX != toX && quickBoard[sq] == 0) {
11629 quickBoard[(fromY<<4)+toX] = 0;
11630 moveDatabase[movePtr].piece = Q_EP;
11631 moveDatabase[movePtr++].to = (fromY<<4)+toX;
11632 moveDatabase[movePtr].to = sq;
11634 if(promoPiece != pieceType[piece]) {
11635 moveDatabase[movePtr++].piece = Q_PROMO;
11636 moveDatabase[movePtr].to = pieceType[piece] = (int) promoPiece;
11638 moveDatabase[movePtr].piece = piece;
11639 quickBoard[sq] = piece;
11644 PackGame (Board board)
11646 Move *newSpace = NULL;
11647 moveDatabase[movePtr].piece = 0; // terminate previous game
11648 if(movePtr > dataSize) {
11649 if(appData.debugMode) fprintf(debugFP, "move-cache overflow, enlarge to %d MB\n", dataSize/128);
11650 dataSize *= 8; // increase size by factor 8 (512KB -> 4MB -> 32MB -> 256MB -> 2GB)
11651 if(dataSize) newSpace = (Move*) calloc(dataSize + 1000, sizeof(Move));
11654 Move *p = moveDatabase, *q = newSpace;
11655 for(i=0; i<movePtr; i++) *q++ = *p++; // copy to newly allocated space
11656 if(dataSize > 8*DSIZE) free(moveDatabase); // and free old space (if it was allocated)
11657 moveDatabase = newSpace;
11658 } else { // calloc failed, we must be out of memory. Too bad...
11659 dataSize = 0; // prevent calloc events for all subsequent games
11660 return 0; // and signal this one isn't cached
11664 MakePieceList(board, counts);
11669 QuickCompare (Board board, int *minCounts, int *maxCounts)
11670 { // compare according to search mode
11672 switch(appData.searchMode)
11674 case 1: // exact position match
11675 if(!(turn & board[EP_STATUS-1])) return FALSE; // wrong side to move
11676 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11677 if(board[r][f] != pieceType[quickBoard[(r<<4)+f]]) return FALSE;
11680 case 2: // can have extra material on empty squares
11681 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11682 if(board[r][f] == EmptySquare) continue;
11683 if(board[r][f] != pieceType[quickBoard[(r<<4)+f]]) return FALSE;
11686 case 3: // material with exact Pawn structure
11687 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11688 if(board[r][f] != WhitePawn && board[r][f] != BlackPawn) continue;
11689 if(board[r][f] != pieceType[quickBoard[(r<<4)+f]]) return FALSE;
11690 } // fall through to material comparison
11691 case 4: // exact material
11692 for(r=0; r<EmptySquare; r++) if(counts[r] != maxCounts[r]) return FALSE;
11694 case 6: // material range with given imbalance
11695 for(r=0; r<BlackPawn; r++) if(counts[r] - minCounts[r] != counts[r+BlackPawn] - minCounts[r+BlackPawn]) return FALSE;
11696 // fall through to range comparison
11697 case 5: // material range
11698 for(r=0; r<EmptySquare; r++) if(counts[r] < minCounts[r] || counts[r] > maxCounts[r]) return FALSE;
11704 QuickScan (Board board, Move *move)
11705 { // reconstruct game,and compare all positions in it
11706 int cnt=0, stretch=0, total = MakePieceList(board, counts);
11708 int piece = move->piece;
11709 int to = move->to, from = pieceList[piece];
11710 if(piece <= Q_PROMO) { // special moves encoded by otherwise invalid piece numbers 1-4
11711 if(!piece) return -1;
11712 if(piece == Q_PROMO) { // promotion, encoded as (Q_PROMO, to) + (piece, promoType)
11713 piece = (++move)->piece;
11714 from = pieceList[piece];
11715 counts[pieceType[piece]]--;
11716 pieceType[piece] = (ChessSquare) move->to;
11717 counts[move->to]++;
11718 } else if(piece == Q_EP) { // e.p. capture, encoded as (Q_EP, ep-sqr) + (piece, to)
11719 counts[pieceType[quickBoard[to]]]--;
11720 quickBoard[to] = 0; total--;
11723 } else if(piece <= Q_BCASTL) { // castling, encoded as (Q_XCASTL, king-to) + (rook, rook-to)
11724 piece = pieceList[piece]; // first two elements of pieceList contain King numbers
11725 from = pieceList[piece]; // so this must be King
11726 quickBoard[from] = 0;
11727 pieceList[piece] = to;
11728 from = pieceList[(++move)->piece]; // for FRC this has to be done here
11729 quickBoard[from] = 0; // rook
11730 quickBoard[to] = piece;
11731 to = move->to; piece = move->piece;
11735 if(appData.searchMode > 2) counts[pieceType[quickBoard[to]]]--; // account capture
11736 if((total -= (quickBoard[to] != 0)) < soughtTotal) return -1; // piece count dropped below what we search for
11737 quickBoard[from] = 0;
11739 quickBoard[to] = piece;
11740 pieceList[piece] = to;
11742 if(QuickCompare(soughtBoard, minSought, maxSought) ||
11743 appData.ignoreColors && QuickCompare(reverseBoard, minReverse, maxReverse) ||
11744 flipSearch && (QuickCompare(flipBoard, minSought, maxSought) ||
11745 appData.ignoreColors && QuickCompare(rotateBoard, minReverse, maxReverse))
11747 static int lastCounts[EmptySquare+1];
11749 if(stretch) for(i=0; i<EmptySquare; i++) if(lastCounts[i] != counts[i]) { stretch = 0; break; } // reset if material changes
11750 if(stretch++ == 0) for(i=0; i<EmptySquare; i++) lastCounts[i] = counts[i]; // remember actual material
11751 } else stretch = 0;
11752 if(stretch && (appData.searchMode == 1 || stretch >= appData.stretch)) return cnt + 1 - stretch;
11761 flipSearch = FALSE;
11762 CopyBoard(soughtBoard, boards[currentMove]);
11763 soughtTotal = MakePieceList(soughtBoard, maxSought);
11764 soughtBoard[EP_STATUS-1] = (currentMove & 1) + 1;
11765 if(currentMove == 0 && gameMode == EditPosition) soughtBoard[EP_STATUS-1] = blackPlaysFirst + 1; // (!)
11766 CopyBoard(reverseBoard, boards[currentMove]);
11767 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11768 int piece = boards[currentMove][BOARD_HEIGHT-1-r][f];
11769 if(piece < BlackPawn) piece += BlackPawn; else if(piece < EmptySquare) piece -= BlackPawn; // color-flip
11770 reverseBoard[r][f] = piece;
11772 reverseBoard[EP_STATUS-1] = soughtBoard[EP_STATUS-1] ^ 3;
11773 for(r=0; r<6; r++) reverseBoard[CASTLING][r] = boards[currentMove][CASTLING][(r+3)%6];
11774 if(appData.findMirror && appData.searchMode <= 3 && (!nrCastlingRights
11775 || (boards[currentMove][CASTLING][2] == NoRights ||
11776 boards[currentMove][CASTLING][0] == NoRights && boards[currentMove][CASTLING][1] == NoRights )
11777 && (boards[currentMove][CASTLING][5] == NoRights ||
11778 boards[currentMove][CASTLING][3] == NoRights && boards[currentMove][CASTLING][4] == NoRights ) )
11781 CopyBoard(flipBoard, soughtBoard);
11782 CopyBoard(rotateBoard, reverseBoard);
11783 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
11784 flipBoard[r][f] = soughtBoard[r][BOARD_WIDTH-1-f];
11785 rotateBoard[r][f] = reverseBoard[r][BOARD_WIDTH-1-f];
11788 for(r=0; r<BlackPawn; r++) maxReverse[r] = maxSought[r+BlackPawn], maxReverse[r+BlackPawn] = maxSought[r];
11789 if(appData.searchMode >= 5) {
11790 for(r=BOARD_HEIGHT/2; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) soughtBoard[r][f] = EmptySquare;
11791 MakePieceList(soughtBoard, minSought);
11792 for(r=0; r<BlackPawn; r++) minReverse[r] = minSought[r+BlackPawn], minReverse[r+BlackPawn] = minSought[r];
11794 if(gameInfo.variant == VariantCrazyhouse || gameInfo.variant == VariantShogi || gameInfo.variant == VariantBughouse)
11795 soughtTotal = 0; // in drop games nr of pieces does not fall monotonously
11798 GameInfo dummyInfo;
11799 static int creatingBook;
11802 GameContainsPosition (FILE *f, ListGame *lg)
11804 int next, btm=0, plyNr=0, scratch=forwardMostMove+2&~1;
11805 int fromX, fromY, toX, toY;
11807 static int initDone=FALSE;
11809 // weed out games based on numerical tag comparison
11810 if(lg->gameInfo.variant != gameInfo.variant) return -1; // wrong variant
11811 if(appData.eloThreshold1 && (lg->gameInfo.whiteRating < appData.eloThreshold1 && lg->gameInfo.blackRating < appData.eloThreshold1)) return -1;
11812 if(appData.eloThreshold2 && (lg->gameInfo.whiteRating < appData.eloThreshold2 || lg->gameInfo.blackRating < appData.eloThreshold2)) return -1;
11813 if(appData.dateThreshold && (!lg->gameInfo.date || atoi(lg->gameInfo.date) < appData.dateThreshold)) return -1;
11815 for(next = WhitePawn; next<EmptySquare; next++) keys[next] = random()>>8 ^ random()<<6 ^random()<<20;
11818 if(lg->gameInfo.fen) ParseFEN(boards[scratch], &btm, lg->gameInfo.fen);
11819 else CopyBoard(boards[scratch], initialPosition); // default start position
11822 if((next = QuickScan( boards[scratch], &moveDatabase[lg->moves] )) < 0) return -1; // quick scan rules out it is there
11823 if(appData.searchMode >= 4) return next; // for material searches, trust QuickScan.
11826 if(PositionMatches(boards[scratch], boards[currentMove])) return plyNr;
11827 fseek(f, lg->offset, 0);
11830 yyboardindex = scratch;
11831 quickFlag = plyNr+1;
11836 if(plyNr) return -1; // after we have seen moves, any tags will be start of next game
11842 if(plyNr) return -1; // after we have seen moves, this is for new game
11845 case AmbiguousMove: // we cannot reconstruct the game beyond these two
11846 case ImpossibleMove:
11847 case WhiteWins: // game ends here with these four
11850 case GameUnfinished:
11854 if(appData.testLegality) return -1;
11855 case WhiteCapturesEnPassant:
11856 case BlackCapturesEnPassant:
11857 case WhitePromotion:
11858 case BlackPromotion:
11859 case WhiteNonPromotion:
11860 case BlackNonPromotion:
11862 case WhiteKingSideCastle:
11863 case WhiteQueenSideCastle:
11864 case BlackKingSideCastle:
11865 case BlackQueenSideCastle:
11866 case WhiteKingSideCastleWild:
11867 case WhiteQueenSideCastleWild:
11868 case BlackKingSideCastleWild:
11869 case BlackQueenSideCastleWild:
11870 case WhiteHSideCastleFR:
11871 case WhiteASideCastleFR:
11872 case BlackHSideCastleFR:
11873 case BlackASideCastleFR:
11874 fromX = currentMoveString[0] - AAA;
11875 fromY = currentMoveString[1] - ONE;
11876 toX = currentMoveString[2] - AAA;
11877 toY = currentMoveString[3] - ONE;
11878 promoChar = currentMoveString[4];
11882 fromX = next == WhiteDrop ?
11883 (int) CharToPiece(ToUpper(currentMoveString[0])) :
11884 (int) CharToPiece(ToLower(currentMoveString[0]));
11886 toX = currentMoveString[2] - AAA;
11887 toY = currentMoveString[3] - ONE;
11891 // Move encountered; peform it. We need to shuttle between two boards, as even/odd index determines side to move
11893 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[scratch]);
11894 if(PositionMatches(boards[scratch], boards[currentMove])) return plyNr;
11895 if(appData.ignoreColors && PositionMatches(boards[scratch], reverseBoard)) return plyNr;
11896 if(appData.findMirror) {
11897 if(PositionMatches(boards[scratch], flipBoard)) return plyNr;
11898 if(appData.ignoreColors && PositionMatches(boards[scratch], rotateBoard)) return plyNr;
11903 /* Load the nth game from open file f */
11905 LoadGame (FILE *f, int gameNumber, char *title, int useList)
11909 int gn = gameNumber;
11910 ListGame *lg = NULL;
11911 int numPGNTags = 0;
11913 GameMode oldGameMode;
11914 VariantClass oldVariant = gameInfo.variant; /* [HGM] PGNvariant */
11916 if (appData.debugMode)
11917 fprintf(debugFP, "LoadGame(): on entry, gameMode %d\n", gameMode);
11919 if (gameMode == Training )
11920 SetTrainingModeOff();
11922 oldGameMode = gameMode;
11923 if (gameMode != BeginningOfGame) {
11924 Reset(FALSE, TRUE);
11928 if (lastLoadGameFP != NULL && lastLoadGameFP != f) {
11929 fclose(lastLoadGameFP);
11933 lg = (ListGame *) ListElem(&gameList, gameNumber-1);
11936 fseek(f, lg->offset, 0);
11937 GameListHighlight(gameNumber);
11938 pos = lg->position;
11942 if(gameMode == AnalyzeFile && appData.loadGameIndex == -1)
11943 appData.loadGameIndex = 0; // [HGM] suppress error message if we reach file end after auto-stepping analysis
11945 DisplayError(_("Game number out of range"), 0);
11950 if (fseek(f, 0, 0) == -1) {
11951 if (f == lastLoadGameFP ?
11952 gameNumber == lastLoadGameNumber + 1 :
11956 DisplayError(_("Can't seek on game file"), 0);
11961 lastLoadGameFP = f;
11962 lastLoadGameNumber = gameNumber;
11963 safeStrCpy(lastLoadGameTitle, title, sizeof(lastLoadGameTitle)/sizeof(lastLoadGameTitle[0]));
11964 lastLoadGameUseList = useList;
11968 if (lg && lg->gameInfo.white && lg->gameInfo.black) {
11969 snprintf(buf, sizeof(buf), "%s %s %s", lg->gameInfo.white, _("vs."),
11970 lg->gameInfo.black);
11972 } else if (*title != NULLCHAR) {
11973 if (gameNumber > 1) {
11974 snprintf(buf, MSG_SIZ, "%s %d", title, gameNumber);
11977 DisplayTitle(title);
11981 if (gameMode != AnalyzeFile && gameMode != AnalyzeMode) {
11982 gameMode = PlayFromGameFile;
11986 currentMove = forwardMostMove = backwardMostMove = 0;
11987 CopyBoard(boards[0], initialPosition);
11991 * Skip the first gn-1 games in the file.
11992 * Also skip over anything that precedes an identifiable
11993 * start of game marker, to avoid being confused by
11994 * garbage at the start of the file. Currently
11995 * recognized start of game markers are the move number "1",
11996 * the pattern "gnuchess .* game", the pattern
11997 * "^[#;%] [^ ]* game file", and a PGN tag block.
11998 * A game that starts with one of the latter two patterns
11999 * will also have a move number 1, possibly
12000 * following a position diagram.
12001 * 5-4-02: Let's try being more lenient and allowing a game to
12002 * start with an unnumbered move. Does that break anything?
12004 cm = lastLoadGameStart = EndOfFile;
12006 yyboardindex = forwardMostMove;
12007 cm = (ChessMove) Myylex();
12010 if (cmailMsgLoaded) {
12011 nCmailGames = CMAIL_MAX_GAMES - gn;
12014 DisplayError(_("Game not found in file"), 0);
12021 lastLoadGameStart = cm;
12024 case MoveNumberOne:
12025 switch (lastLoadGameStart) {
12030 case MoveNumberOne:
12032 gn--; /* count this game */
12033 lastLoadGameStart = cm;
12042 switch (lastLoadGameStart) {
12045 case MoveNumberOne:
12047 gn--; /* count this game */
12048 lastLoadGameStart = cm;
12051 lastLoadGameStart = cm; /* game counted already */
12059 yyboardindex = forwardMostMove;
12060 cm = (ChessMove) Myylex();
12061 } while (cm == PGNTag || cm == Comment);
12068 if (cmailMsgLoaded && (CMAIL_MAX_GAMES == lastLoadGameNumber)) {
12069 if ( cmailResult[CMAIL_MAX_GAMES - gn - 1]
12070 != CMAIL_OLD_RESULT) {
12072 cmailResult[ CMAIL_MAX_GAMES
12073 - gn - 1] = CMAIL_OLD_RESULT;
12079 /* Only a NormalMove can be at the start of a game
12080 * without a position diagram. */
12081 if (lastLoadGameStart == EndOfFile ) {
12083 lastLoadGameStart = MoveNumberOne;
12092 if (appData.debugMode)
12093 fprintf(debugFP, "Parsed game start '%s' (%d)\n", yy_text, (int) cm);
12095 if (cm == XBoardGame) {
12096 /* Skip any header junk before position diagram and/or move 1 */
12098 yyboardindex = forwardMostMove;
12099 cm = (ChessMove) Myylex();
12101 if (cm == EndOfFile ||
12102 cm == GNUChessGame || cm == XBoardGame) {
12103 /* Empty game; pretend end-of-file and handle later */
12108 if (cm == MoveNumberOne || cm == PositionDiagram ||
12109 cm == PGNTag || cm == Comment)
12112 } else if (cm == GNUChessGame) {
12113 if (gameInfo.event != NULL) {
12114 free(gameInfo.event);
12116 gameInfo.event = StrSave(yy_text);
12119 startedFromSetupPosition = FALSE;
12120 while (cm == PGNTag) {
12121 if (appData.debugMode)
12122 fprintf(debugFP, "Parsed PGNTag: %s\n", yy_text);
12123 err = ParsePGNTag(yy_text, &gameInfo);
12124 if (!err) numPGNTags++;
12126 /* [HGM] PGNvariant: automatically switch to variant given in PGN tag */
12127 if(gameInfo.variant != oldVariant) {
12128 startedFromPositionFile = FALSE; /* [HGM] loadPos: variant switch likely makes position invalid */
12129 ResetFrontEnd(); // [HGM] might need other bitmaps. Cannot use Reset() because it clears gameInfo :-(
12130 InitPosition(TRUE);
12131 oldVariant = gameInfo.variant;
12132 if (appData.debugMode)
12133 fprintf(debugFP, "New variant %d\n", (int) oldVariant);
12137 if (gameInfo.fen != NULL) {
12138 Board initial_position;
12139 startedFromSetupPosition = TRUE;
12140 if (!ParseFEN(initial_position, &blackPlaysFirst, gameInfo.fen)) {
12142 DisplayError(_("Bad FEN position in file"), 0);
12145 CopyBoard(boards[0], initial_position);
12146 if (blackPlaysFirst) {
12147 currentMove = forwardMostMove = backwardMostMove = 1;
12148 CopyBoard(boards[1], initial_position);
12149 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
12150 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
12151 timeRemaining[0][1] = whiteTimeRemaining;
12152 timeRemaining[1][1] = blackTimeRemaining;
12153 if (commentList[0] != NULL) {
12154 commentList[1] = commentList[0];
12155 commentList[0] = NULL;
12158 currentMove = forwardMostMove = backwardMostMove = 0;
12160 /* [HGM] copy FEN attributes as well. Bugfix 4.3.14m and 4.3.15e: moved to after 'blackPlaysFirst' */
12162 initialRulePlies = FENrulePlies;
12163 for( i=0; i< nrCastlingRights; i++ )
12164 initialRights[i] = initial_position[CASTLING][i];
12166 yyboardindex = forwardMostMove;
12167 free(gameInfo.fen);
12168 gameInfo.fen = NULL;
12171 yyboardindex = forwardMostMove;
12172 cm = (ChessMove) Myylex();
12174 /* Handle comments interspersed among the tags */
12175 while (cm == Comment) {
12177 if (appData.debugMode)
12178 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
12180 AppendComment(currentMove, p, FALSE);
12181 yyboardindex = forwardMostMove;
12182 cm = (ChessMove) Myylex();
12186 /* don't rely on existence of Event tag since if game was
12187 * pasted from clipboard the Event tag may not exist
12189 if (numPGNTags > 0){
12191 if (gameInfo.variant == VariantNormal) {
12192 VariantClass v = StringToVariant(gameInfo.event);
12193 // [HGM] do not recognize variants from event tag that were introduced after supporting variant tag
12194 if(v < VariantShogi) gameInfo.variant = v;
12197 if( appData.autoDisplayTags ) {
12198 tags = PGNTags(&gameInfo);
12199 TagsPopUp(tags, CmailMsg());
12204 /* Make something up, but don't display it now */
12209 if (cm == PositionDiagram) {
12212 Board initial_position;
12214 if (appData.debugMode)
12215 fprintf(debugFP, "Parsed PositionDiagram: %s\n", yy_text);
12217 if (!startedFromSetupPosition) {
12219 for (i = BOARD_HEIGHT - 1; i >= 0; i--)
12220 for (j = BOARD_LEFT; j < BOARD_RGHT; p++)
12231 initial_position[i][j++] = CharToPiece(*p);
12234 while (*p == ' ' || *p == '\t' ||
12235 *p == '\n' || *p == '\r') p++;
12237 if (strncmp(p, "black", strlen("black"))==0)
12238 blackPlaysFirst = TRUE;
12240 blackPlaysFirst = FALSE;
12241 startedFromSetupPosition = TRUE;
12243 CopyBoard(boards[0], initial_position);
12244 if (blackPlaysFirst) {
12245 currentMove = forwardMostMove = backwardMostMove = 1;
12246 CopyBoard(boards[1], initial_position);
12247 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
12248 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
12249 timeRemaining[0][1] = whiteTimeRemaining;
12250 timeRemaining[1][1] = blackTimeRemaining;
12251 if (commentList[0] != NULL) {
12252 commentList[1] = commentList[0];
12253 commentList[0] = NULL;
12256 currentMove = forwardMostMove = backwardMostMove = 0;
12259 yyboardindex = forwardMostMove;
12260 cm = (ChessMove) Myylex();
12263 if(!creatingBook) {
12264 if (first.pr == NoProc) {
12265 StartChessProgram(&first);
12267 InitChessProgram(&first, FALSE);
12268 SendToProgram("force\n", &first);
12269 if (startedFromSetupPosition) {
12270 SendBoard(&first, forwardMostMove);
12271 if (appData.debugMode) {
12272 fprintf(debugFP, "Load Game\n");
12274 DisplayBothClocks();
12278 /* [HGM] server: flag to write setup moves in broadcast file as one */
12279 loadFlag = appData.suppressLoadMoves;
12281 while (cm == Comment) {
12283 if (appData.debugMode)
12284 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
12286 AppendComment(currentMove, p, FALSE);
12287 yyboardindex = forwardMostMove;
12288 cm = (ChessMove) Myylex();
12291 if ((cm == EndOfFile && lastLoadGameStart != EndOfFile ) ||
12292 cm == WhiteWins || cm == BlackWins ||
12293 cm == GameIsDrawn || cm == GameUnfinished) {
12294 DisplayMessage("", _("No moves in game"));
12295 if (cmailMsgLoaded) {
12296 if (appData.debugMode)
12297 fprintf(debugFP, "Setting flipView to %d.\n", FALSE);
12301 DrawPosition(FALSE, boards[currentMove]);
12302 DisplayBothClocks();
12303 gameMode = EditGame;
12310 // [HGM] PV info: routine tests if comment empty
12311 if (!matchMode && (pausing || appData.timeDelay != 0)) {
12312 DisplayComment(currentMove - 1, commentList[currentMove]);
12314 if (!matchMode && appData.timeDelay != 0)
12315 DrawPosition(FALSE, boards[currentMove]);
12317 if (gameMode == AnalyzeFile || gameMode == AnalyzeMode) {
12318 programStats.ok_to_send = 1;
12321 /* if the first token after the PGN tags is a move
12322 * and not move number 1, retrieve it from the parser
12324 if (cm != MoveNumberOne)
12325 LoadGameOneMove(cm);
12327 /* load the remaining moves from the file */
12328 while (LoadGameOneMove(EndOfFile)) {
12329 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
12330 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
12333 /* rewind to the start of the game */
12334 currentMove = backwardMostMove;
12336 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
12338 if (oldGameMode == AnalyzeFile ||
12339 oldGameMode == AnalyzeMode) {
12340 appData.loadGameIndex = -1; // [HGM] order auto-stepping through games
12342 AnalyzeFileEvent();
12346 if(creatingBook) return TRUE;
12347 if (!matchMode && pos > 0) {
12348 ToNrEvent(pos); // [HGM] no autoplay if selected on position
12350 if (matchMode || appData.timeDelay == 0) {
12352 } else if (appData.timeDelay > 0) {
12353 AutoPlayGameLoop();
12356 if (appData.debugMode)
12357 fprintf(debugFP, "LoadGame(): on exit, gameMode %d\n", gameMode);
12359 loadFlag = 0; /* [HGM] true game starts */
12363 /* Support for LoadNextPosition, LoadPreviousPosition, ReloadSamePosition */
12365 ReloadPosition (int offset)
12367 int positionNumber = lastLoadPositionNumber + offset;
12368 if (lastLoadPositionFP == NULL) {
12369 DisplayError(_("No position has been loaded yet"), 0);
12372 if (positionNumber <= 0) {
12373 DisplayError(_("Can't back up any further"), 0);
12376 return LoadPosition(lastLoadPositionFP, positionNumber,
12377 lastLoadPositionTitle);
12380 /* Load the nth position from the given file */
12382 LoadPositionFromFile (char *filename, int n, char *title)
12387 if (strcmp(filename, "-") == 0) {
12388 return LoadPosition(stdin, n, "stdin");
12390 f = fopen(filename, "rb");
12392 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
12393 DisplayError(buf, errno);
12396 return LoadPosition(f, n, title);
12401 /* Load the nth position from the given open file, and close it */
12403 LoadPosition (FILE *f, int positionNumber, char *title)
12405 char *p, line[MSG_SIZ];
12406 Board initial_position;
12407 int i, j, fenMode, pn;
12409 if (gameMode == Training )
12410 SetTrainingModeOff();
12412 if (gameMode != BeginningOfGame) {
12413 Reset(FALSE, TRUE);
12415 if (lastLoadPositionFP != NULL && lastLoadPositionFP != f) {
12416 fclose(lastLoadPositionFP);
12418 if (positionNumber == 0) positionNumber = 1;
12419 lastLoadPositionFP = f;
12420 lastLoadPositionNumber = positionNumber;
12421 safeStrCpy(lastLoadPositionTitle, title, sizeof(lastLoadPositionTitle)/sizeof(lastLoadPositionTitle[0]));
12422 if (first.pr == NoProc && !appData.noChessProgram) {
12423 StartChessProgram(&first);
12424 InitChessProgram(&first, FALSE);
12426 pn = positionNumber;
12427 if (positionNumber < 0) {
12428 /* Negative position number means to seek to that byte offset */
12429 if (fseek(f, -positionNumber, 0) == -1) {
12430 DisplayError(_("Can't seek on position file"), 0);
12435 if (fseek(f, 0, 0) == -1) {
12436 if (f == lastLoadPositionFP ?
12437 positionNumber == lastLoadPositionNumber + 1 :
12438 positionNumber == 1) {
12441 DisplayError(_("Can't seek on position file"), 0);
12446 /* See if this file is FEN or old-style xboard */
12447 if (fgets(line, MSG_SIZ, f) == NULL) {
12448 DisplayError(_("Position not found in file"), 0);
12451 // [HGM] FEN can begin with digit, any piece letter valid in this variant, or a + for Shogi promoted pieces
12452 fenMode = line[0] >= '0' && line[0] <= '9' || line[0] == '+' || CharToPiece(line[0]) != EmptySquare;
12455 if (fenMode || line[0] == '#') pn--;
12457 /* skip positions before number pn */
12458 if (fgets(line, MSG_SIZ, f) == NULL) {
12460 DisplayError(_("Position not found in file"), 0);
12463 if (fenMode || line[0] == '#') pn--;
12468 if (!ParseFEN(initial_position, &blackPlaysFirst, line)) {
12469 DisplayError(_("Bad FEN position in file"), 0);
12473 (void) fgets(line, MSG_SIZ, f);
12474 (void) fgets(line, MSG_SIZ, f);
12476 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
12477 (void) fgets(line, MSG_SIZ, f);
12478 for (p = line, j = BOARD_LEFT; j < BOARD_RGHT; p++) {
12481 initial_position[i][j++] = CharToPiece(*p);
12485 blackPlaysFirst = FALSE;
12487 (void) fgets(line, MSG_SIZ, f);
12488 if (strncmp(line, "black", strlen("black"))==0)
12489 blackPlaysFirst = TRUE;
12492 startedFromSetupPosition = TRUE;
12494 CopyBoard(boards[0], initial_position);
12495 if (blackPlaysFirst) {
12496 currentMove = forwardMostMove = backwardMostMove = 1;
12497 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
12498 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
12499 CopyBoard(boards[1], initial_position);
12500 DisplayMessage("", _("Black to play"));
12502 currentMove = forwardMostMove = backwardMostMove = 0;
12503 DisplayMessage("", _("White to play"));
12505 initialRulePlies = FENrulePlies; /* [HGM] copy FEN attributes as well */
12506 if(first.pr != NoProc) { // [HGM] in tourney-mode a position can be loaded before the chess engine is installed
12507 SendToProgram("force\n", &first);
12508 SendBoard(&first, forwardMostMove);
12510 if (appData.debugMode) {
12512 for(i=0;i<2;i++){for(j=0;j<6;j++)fprintf(debugFP, " %d", boards[i][CASTLING][j]);fprintf(debugFP,"\n");}
12513 for(j=0;j<6;j++)fprintf(debugFP, " %d", initialRights[j]);fprintf(debugFP,"\n");
12514 fprintf(debugFP, "Load Position\n");
12517 if (positionNumber > 1) {
12518 snprintf(line, MSG_SIZ, "%s %d", title, positionNumber);
12519 DisplayTitle(line);
12521 DisplayTitle(title);
12523 gameMode = EditGame;
12526 timeRemaining[0][1] = whiteTimeRemaining;
12527 timeRemaining[1][1] = blackTimeRemaining;
12528 DrawPosition(FALSE, boards[currentMove]);
12535 CopyPlayerNameIntoFileName (char **dest, char *src)
12537 while (*src != NULLCHAR && *src != ',') {
12542 *(*dest)++ = *src++;
12548 DefaultFileName (char *ext)
12550 static char def[MSG_SIZ];
12553 if (gameInfo.white != NULL && gameInfo.white[0] != '-') {
12555 CopyPlayerNameIntoFileName(&p, gameInfo.white);
12557 CopyPlayerNameIntoFileName(&p, gameInfo.black);
12559 safeStrCpy(p, ext, MSG_SIZ-2-strlen(gameInfo.white)-strlen(gameInfo.black));
12566 /* Save the current game to the given file */
12568 SaveGameToFile (char *filename, int append)
12572 int result, i, t,tot=0;
12574 if (strcmp(filename, "-") == 0) {
12575 return SaveGame(stdout, 0, NULL);
12577 for(i=0; i<10; i++) { // upto 10 tries
12578 f = fopen(filename, append ? "a" : "w");
12579 if(f && i) fprintf(f, "[Delay \"%d retries, %d msec\"]\n",i,tot);
12580 if(f || errno != 13) break;
12581 DoSleep(t = 5 + random()%11); // wait 5-15 msec
12585 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
12586 DisplayError(buf, errno);
12589 safeStrCpy(buf, lastMsg, MSG_SIZ);
12590 DisplayMessage(_("Waiting for access to save file"), "");
12591 flock(fileno(f), LOCK_EX); // [HGM] lock: lock file while we are writing
12592 DisplayMessage(_("Saving game"), "");
12593 if(lseek(fileno(f), 0, SEEK_END) == -1) DisplayError(_("Bad Seek"), errno); // better safe than sorry...
12594 result = SaveGame(f, 0, NULL);
12595 DisplayMessage(buf, "");
12602 SavePart (char *str)
12604 static char buf[MSG_SIZ];
12607 p = strchr(str, ' ');
12608 if (p == NULL) return str;
12609 strncpy(buf, str, p - str);
12610 buf[p - str] = NULLCHAR;
12614 #define PGN_MAX_LINE 75
12616 #define PGN_SIDE_WHITE 0
12617 #define PGN_SIDE_BLACK 1
12620 FindFirstMoveOutOfBook (int side)
12624 if( backwardMostMove == 0 && ! startedFromSetupPosition) {
12625 int index = backwardMostMove;
12626 int has_book_hit = 0;
12628 if( (index % 2) != side ) {
12632 while( index < forwardMostMove ) {
12633 /* Check to see if engine is in book */
12634 int depth = pvInfoList[index].depth;
12635 int score = pvInfoList[index].score;
12641 else if( score == 0 && depth == 63 ) {
12642 in_book = 1; /* Zappa */
12644 else if( score == 2 && depth == 99 ) {
12645 in_book = 1; /* Abrok */
12648 has_book_hit += in_book;
12664 GetOutOfBookInfo (char * buf)
12668 int offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
12670 oob[0] = FindFirstMoveOutOfBook( PGN_SIDE_WHITE );
12671 oob[1] = FindFirstMoveOutOfBook( PGN_SIDE_BLACK );
12675 if( oob[0] >= 0 || oob[1] >= 0 ) {
12676 for( i=0; i<2; i++ ) {
12680 if( i > 0 && oob[0] >= 0 ) {
12681 strcat( buf, " " );
12684 sprintf( buf+strlen(buf), "%d%s. ", (idx - offset)/2 + 1, idx & 1 ? ".." : "" );
12685 sprintf( buf+strlen(buf), "%s%.2f",
12686 pvInfoList[idx].score >= 0 ? "+" : "",
12687 pvInfoList[idx].score / 100.0 );
12693 /* Save game in PGN style and close the file */
12695 SaveGamePGN (FILE *f)
12697 int i, offset, linelen, newblock;
12700 int movelen, numlen, blank;
12701 char move_buffer[100]; /* [AS] Buffer for move+PV info */
12703 offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
12705 PrintPGNTags(f, &gameInfo);
12707 if(appData.numberTag && matchMode) fprintf(f, "[Number \"%d\"]\n", nextGame+1); // [HGM] number tag
12709 if (backwardMostMove > 0 || startedFromSetupPosition) {
12710 char *fen = PositionToFEN(backwardMostMove, NULL);
12711 fprintf(f, "[FEN \"%s\"]\n[SetUp \"1\"]\n", fen);
12712 fprintf(f, "\n{--------------\n");
12713 PrintPosition(f, backwardMostMove);
12714 fprintf(f, "--------------}\n");
12718 /* [AS] Out of book annotation */
12719 if( appData.saveOutOfBookInfo ) {
12722 GetOutOfBookInfo( buf );
12724 if( buf[0] != '\0' ) {
12725 fprintf( f, "[%s \"%s\"]\n", PGN_OUT_OF_BOOK, buf );
12732 i = backwardMostMove;
12736 while (i < forwardMostMove) {
12737 /* Print comments preceding this move */
12738 if (commentList[i] != NULL) {
12739 if (linelen > 0) fprintf(f, "\n");
12740 fprintf(f, "%s", commentList[i]);
12745 /* Format move number */
12747 snprintf(numtext, sizeof(numtext)/sizeof(numtext[0]),"%d.", (i - offset)/2 + 1);
12750 snprintf(numtext, sizeof(numtext)/sizeof(numtext[0]), "%d...", (i - offset)/2 + 1);
12752 numtext[0] = NULLCHAR;
12754 numlen = strlen(numtext);
12757 /* Print move number */
12758 blank = linelen > 0 && numlen > 0;
12759 if (linelen + (blank ? 1 : 0) + numlen > PGN_MAX_LINE) {
12768 fprintf(f, "%s", numtext);
12772 safeStrCpy(move_buffer, SavePart(parseList[i]), sizeof(move_buffer)/sizeof(move_buffer[0])); // [HGM] pgn: print move via buffer, so it can be edited
12773 movelen = strlen(move_buffer); /* [HGM] pgn: line-break point before move */
12776 blank = linelen > 0 && movelen > 0;
12777 if (linelen + (blank ? 1 : 0) + movelen > PGN_MAX_LINE) {
12786 fprintf(f, "%s", move_buffer);
12787 linelen += movelen;
12789 /* [AS] Add PV info if present */
12790 if( i >= 0 && appData.saveExtendedInfoInPGN && pvInfoList[i].depth > 0 ) {
12791 /* [HGM] add time */
12792 char buf[MSG_SIZ]; int seconds;
12794 seconds = (pvInfoList[i].time+5)/10; // deci-seconds, rounded to nearest
12800 snprintf(buf, MSG_SIZ, " %3.1f%c", seconds/10., 0);
12803 seconds = (seconds + 4)/10; // round to full seconds
12805 snprintf(buf, MSG_SIZ, " %d%c", seconds, 0);
12807 snprintf(buf, MSG_SIZ, " %d:%02d%c", seconds/60, seconds%60, 0);
12810 snprintf( move_buffer, sizeof(move_buffer)/sizeof(move_buffer[0]),"{%s%.2f/%d%s}",
12811 pvInfoList[i].score >= 0 ? "+" : "",
12812 pvInfoList[i].score / 100.0,
12813 pvInfoList[i].depth,
12816 movelen = strlen(move_buffer); /* [HGM] pgn: line-break point after move */
12818 /* Print score/depth */
12819 blank = linelen > 0 && movelen > 0;
12820 if (linelen + (blank ? 1 : 0) + movelen > PGN_MAX_LINE) {
12829 fprintf(f, "%s", move_buffer);
12830 linelen += movelen;
12836 /* Start a new line */
12837 if (linelen > 0) fprintf(f, "\n");
12839 /* Print comments after last move */
12840 if (commentList[i] != NULL) {
12841 fprintf(f, "%s\n", commentList[i]);
12845 if (gameInfo.resultDetails != NULL &&
12846 gameInfo.resultDetails[0] != NULLCHAR) {
12847 fprintf(f, "{%s} %s\n\n", gameInfo.resultDetails,
12848 PGNResult(gameInfo.result));
12850 fprintf(f, "%s\n\n", PGNResult(gameInfo.result));
12854 lastSavedGame = GameCheckSum(); // [HGM] save: remember ID of last saved game to prevent double saving
12858 /* Save game in old style and close the file */
12860 SaveGameOldStyle (FILE *f)
12865 tm = time((time_t *) NULL);
12867 fprintf(f, "# %s game file -- %s", programName, ctime(&tm));
12870 if (backwardMostMove > 0 || startedFromSetupPosition) {
12871 fprintf(f, "\n[--------------\n");
12872 PrintPosition(f, backwardMostMove);
12873 fprintf(f, "--------------]\n");
12878 i = backwardMostMove;
12879 offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
12881 while (i < forwardMostMove) {
12882 if (commentList[i] != NULL) {
12883 fprintf(f, "[%s]\n", commentList[i]);
12886 if ((i % 2) == 1) {
12887 fprintf(f, "%d. ... %s\n", (i - offset)/2 + 1, parseList[i]);
12890 fprintf(f, "%d. %s ", (i - offset)/2 + 1, parseList[i]);
12892 if (commentList[i] != NULL) {
12896 if (i >= forwardMostMove) {
12900 fprintf(f, "%s\n", parseList[i]);
12905 if (commentList[i] != NULL) {
12906 fprintf(f, "[%s]\n", commentList[i]);
12909 /* This isn't really the old style, but it's close enough */
12910 if (gameInfo.resultDetails != NULL &&
12911 gameInfo.resultDetails[0] != NULLCHAR) {
12912 fprintf(f, "%s (%s)\n\n", PGNResult(gameInfo.result),
12913 gameInfo.resultDetails);
12915 fprintf(f, "%s\n\n", PGNResult(gameInfo.result));
12922 /* Save the current game to open file f and close the file */
12924 SaveGame (FILE *f, int dummy, char *dummy2)
12926 if (gameMode == EditPosition) EditPositionDone(TRUE);
12927 lastSavedGame = GameCheckSum(); // [HGM] save: remember ID of last saved game to prevent double saving
12928 if (appData.oldSaveStyle)
12929 return SaveGameOldStyle(f);
12931 return SaveGamePGN(f);
12934 /* Save the current position to the given file */
12936 SavePositionToFile (char *filename)
12941 if (strcmp(filename, "-") == 0) {
12942 return SavePosition(stdout, 0, NULL);
12944 f = fopen(filename, "a");
12946 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
12947 DisplayError(buf, errno);
12950 safeStrCpy(buf, lastMsg, MSG_SIZ);
12951 DisplayMessage(_("Waiting for access to save file"), "");
12952 flock(fileno(f), LOCK_EX); // [HGM] lock
12953 DisplayMessage(_("Saving position"), "");
12954 lseek(fileno(f), 0, SEEK_END); // better safe than sorry...
12955 SavePosition(f, 0, NULL);
12956 DisplayMessage(buf, "");
12962 /* Save the current position to the given open file and close the file */
12964 SavePosition (FILE *f, int dummy, char *dummy2)
12969 if (gameMode == EditPosition) EditPositionDone(TRUE);
12970 if (appData.oldSaveStyle) {
12971 tm = time((time_t *) NULL);
12973 fprintf(f, "# %s position file -- %s", programName, ctime(&tm));
12975 fprintf(f, "[--------------\n");
12976 PrintPosition(f, currentMove);
12977 fprintf(f, "--------------]\n");
12979 fen = PositionToFEN(currentMove, NULL);
12980 fprintf(f, "%s\n", fen);
12988 ReloadCmailMsgEvent (int unregister)
12991 static char *inFilename = NULL;
12992 static char *outFilename;
12994 struct stat inbuf, outbuf;
12997 /* Any registered moves are unregistered if unregister is set, */
12998 /* i.e. invoked by the signal handler */
13000 for (i = 0; i < CMAIL_MAX_GAMES; i ++) {
13001 cmailMoveRegistered[i] = FALSE;
13002 if (cmailCommentList[i] != NULL) {
13003 free(cmailCommentList[i]);
13004 cmailCommentList[i] = NULL;
13007 nCmailMovesRegistered = 0;
13010 for (i = 0; i < CMAIL_MAX_GAMES; i ++) {
13011 cmailResult[i] = CMAIL_NOT_RESULT;
13015 if (inFilename == NULL) {
13016 /* Because the filenames are static they only get malloced once */
13017 /* and they never get freed */
13018 inFilename = (char *) malloc(strlen(appData.cmailGameName) + 9);
13019 sprintf(inFilename, "%s.game.in", appData.cmailGameName);
13021 outFilename = (char *) malloc(strlen(appData.cmailGameName) + 5);
13022 sprintf(outFilename, "%s.out", appData.cmailGameName);
13025 status = stat(outFilename, &outbuf);
13027 cmailMailedMove = FALSE;
13029 status = stat(inFilename, &inbuf);
13030 cmailMailedMove = (inbuf.st_mtime < outbuf.st_mtime);
13033 /* LoadGameFromFile(CMAIL_MAX_GAMES) with cmailMsgLoaded == TRUE
13034 counts the games, notes how each one terminated, etc.
13036 It would be nice to remove this kludge and instead gather all
13037 the information while building the game list. (And to keep it
13038 in the game list nodes instead of having a bunch of fixed-size
13039 parallel arrays.) Note this will require getting each game's
13040 termination from the PGN tags, as the game list builder does
13041 not process the game moves. --mann
13043 cmailMsgLoaded = TRUE;
13044 LoadGameFromFile(inFilename, CMAIL_MAX_GAMES, "", FALSE);
13046 /* Load first game in the file or popup game menu */
13047 LoadGameFromFile(inFilename, 0, appData.cmailGameName, TRUE);
13049 #endif /* !WIN32 */
13057 char string[MSG_SIZ];
13059 if ( cmailMailedMove
13060 || (cmailResult[lastLoadGameNumber - 1] == CMAIL_OLD_RESULT)) {
13061 return TRUE; /* Allow free viewing */
13064 /* Unregister move to ensure that we don't leave RegisterMove */
13065 /* with the move registered when the conditions for registering no */
13067 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
13068 cmailMoveRegistered[lastLoadGameNumber - 1] = FALSE;
13069 nCmailMovesRegistered --;
13071 if (cmailCommentList[lastLoadGameNumber - 1] != NULL)
13073 free(cmailCommentList[lastLoadGameNumber - 1]);
13074 cmailCommentList[lastLoadGameNumber - 1] = NULL;
13078 if (cmailOldMove == -1) {
13079 DisplayError(_("You have edited the game history.\nUse Reload Same Game and make your move again."), 0);
13083 if (currentMove > cmailOldMove + 1) {
13084 DisplayError(_("You have entered too many moves.\nBack up to the correct position and try again."), 0);
13088 if (currentMove < cmailOldMove) {
13089 DisplayError(_("Displayed position is not current.\nStep forward to the correct position and try again."), 0);
13093 if (forwardMostMove > currentMove) {
13094 /* Silently truncate extra moves */
13098 if ( (currentMove == cmailOldMove + 1)
13099 || ( (currentMove == cmailOldMove)
13100 && ( (cmailMoveType[lastLoadGameNumber - 1] == CMAIL_ACCEPT)
13101 || (cmailMoveType[lastLoadGameNumber - 1] == CMAIL_RESIGN)))) {
13102 if (gameInfo.result != GameUnfinished) {
13103 cmailResult[lastLoadGameNumber - 1] = CMAIL_NEW_RESULT;
13106 if (commentList[currentMove] != NULL) {
13107 cmailCommentList[lastLoadGameNumber - 1]
13108 = StrSave(commentList[currentMove]);
13110 safeStrCpy(cmailMove[lastLoadGameNumber - 1], moveList[currentMove - 1], sizeof(cmailMove[lastLoadGameNumber - 1])/sizeof(cmailMove[lastLoadGameNumber - 1][0]));
13112 if (appData.debugMode)
13113 fprintf(debugFP, "Saving %s for game %d\n",
13114 cmailMove[lastLoadGameNumber - 1], lastLoadGameNumber);
13116 snprintf(string, MSG_SIZ, "%s.game.out.%d", appData.cmailGameName, lastLoadGameNumber);
13118 f = fopen(string, "w");
13119 if (appData.oldSaveStyle) {
13120 SaveGameOldStyle(f); /* also closes the file */
13122 snprintf(string, MSG_SIZ, "%s.pos.out", appData.cmailGameName);
13123 f = fopen(string, "w");
13124 SavePosition(f, 0, NULL); /* also closes the file */
13126 fprintf(f, "{--------------\n");
13127 PrintPosition(f, currentMove);
13128 fprintf(f, "--------------}\n\n");
13130 SaveGame(f, 0, NULL); /* also closes the file*/
13133 cmailMoveRegistered[lastLoadGameNumber - 1] = TRUE;
13134 nCmailMovesRegistered ++;
13135 } else if (nCmailGames == 1) {
13136 DisplayError(_("You have not made a move yet"), 0);
13147 static char *partCommandString = "cmail -xv%s -remail -game %s 2>&1";
13148 FILE *commandOutput;
13149 char buffer[MSG_SIZ], msg[MSG_SIZ], string[MSG_SIZ];
13150 int nBytes = 0; /* Suppress warnings on uninitialized variables */
13156 if (! cmailMsgLoaded) {
13157 DisplayError(_("The cmail message is not loaded.\nUse Reload CMail Message and make your move again."), 0);
13161 if (nCmailGames == nCmailResults) {
13162 DisplayError(_("No unfinished games"), 0);
13166 #if CMAIL_PROHIBIT_REMAIL
13167 if (cmailMailedMove) {
13168 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);
13169 DisplayError(msg, 0);
13174 if (! (cmailMailedMove || RegisterMove())) return;
13176 if ( cmailMailedMove
13177 || (nCmailMovesRegistered + nCmailResults == nCmailGames)) {
13178 snprintf(string, MSG_SIZ, partCommandString,
13179 appData.debugMode ? " -v" : "", appData.cmailGameName);
13180 commandOutput = popen(string, "r");
13182 if (commandOutput == NULL) {
13183 DisplayError(_("Failed to invoke cmail"), 0);
13185 for (nBuffers = 0; (! feof(commandOutput)); nBuffers ++) {
13186 nBytes = fread(buffer, 1, MSG_SIZ - 1, commandOutput);
13188 if (nBuffers > 1) {
13189 (void) memcpy(msg, buffer + nBytes, MSG_SIZ - nBytes - 1);
13190 (void) memcpy(msg + MSG_SIZ - nBytes - 1, buffer, nBytes);
13191 nBytes = MSG_SIZ - 1;
13193 (void) memcpy(msg, buffer, nBytes);
13195 *(msg + nBytes) = '\0'; /* \0 for end-of-string*/
13197 if(StrStr(msg, "Mailed cmail message to ") != NULL) {
13198 cmailMailedMove = TRUE; /* Prevent >1 moves */
13201 for (i = 0; i < nCmailGames; i ++) {
13202 if (cmailResult[i] == CMAIL_NOT_RESULT) {
13207 && ( (arcDir = (char *) getenv("CMAIL_ARCDIR"))
13209 snprintf(buffer, MSG_SIZ, "%s/%s.%s.archive",
13211 appData.cmailGameName,
13213 LoadGameFromFile(buffer, 1, buffer, FALSE);
13214 cmailMsgLoaded = FALSE;
13218 DisplayInformation(msg);
13219 pclose(commandOutput);
13222 if ((*cmailMsg) != '\0') {
13223 DisplayInformation(cmailMsg);
13228 #endif /* !WIN32 */
13237 int prependComma = 0;
13239 char string[MSG_SIZ]; /* Space for game-list */
13242 if (!cmailMsgLoaded) return "";
13244 if (cmailMailedMove) {
13245 snprintf(cmailMsg, MSG_SIZ, _("Waiting for reply from opponent\n"));
13247 /* Create a list of games left */
13248 snprintf(string, MSG_SIZ, "[");
13249 for (i = 0; i < nCmailGames; i ++) {
13250 if (! ( cmailMoveRegistered[i]
13251 || (cmailResult[i] == CMAIL_OLD_RESULT))) {
13252 if (prependComma) {
13253 snprintf(number, sizeof(number)/sizeof(number[0]), ",%d", i + 1);
13255 snprintf(number, sizeof(number)/sizeof(number[0]), "%d", i + 1);
13259 strcat(string, number);
13262 strcat(string, "]");
13264 if (nCmailMovesRegistered + nCmailResults == 0) {
13265 switch (nCmailGames) {
13267 snprintf(cmailMsg, MSG_SIZ, _("Still need to make move for game\n"));
13271 snprintf(cmailMsg, MSG_SIZ, _("Still need to make moves for both games\n"));
13275 snprintf(cmailMsg, MSG_SIZ, _("Still need to make moves for all %d games\n"),
13280 switch (nCmailGames - nCmailMovesRegistered - nCmailResults) {
13282 snprintf(cmailMsg, MSG_SIZ, _("Still need to make a move for game %s\n"),
13287 if (nCmailResults == nCmailGames) {
13288 snprintf(cmailMsg, MSG_SIZ, _("No unfinished games\n"));
13290 snprintf(cmailMsg, MSG_SIZ, _("Ready to send mail\n"));
13295 snprintf(cmailMsg, MSG_SIZ, _("Still need to make moves for games %s\n"),
13307 if (gameMode == Training)
13308 SetTrainingModeOff();
13311 cmailMsgLoaded = FALSE;
13312 if (appData.icsActive) {
13313 SendToICS(ics_prefix);
13314 SendToICS("refresh\n");
13319 ExitEvent (int status)
13323 /* Give up on clean exit */
13327 /* Keep trying for clean exit */
13331 if (appData.icsActive && appData.colorize) Colorize(ColorNone, FALSE);
13333 if (telnetISR != NULL) {
13334 RemoveInputSource(telnetISR);
13336 if (icsPR != NoProc) {
13337 DestroyChildProcess(icsPR, TRUE);
13340 /* [HGM] crash: leave writing PGN and position entirely to GameEnds() */
13341 GameEnds(gameInfo.result, gameInfo.resultDetails==NULL ? "xboard exit" : gameInfo.resultDetails, GE_PLAYER);
13343 /* [HGM] crash: the above GameEnds() is a dud if another one was running */
13344 /* make sure this other one finishes before killing it! */
13345 if(endingGame) { int count = 0;
13346 if(appData.debugMode) fprintf(debugFP, "ExitEvent() during GameEnds(), wait\n");
13347 while(endingGame && count++ < 10) DoSleep(1);
13348 if(appData.debugMode && endingGame) fprintf(debugFP, "GameEnds() seems stuck, proceed exiting\n");
13351 /* Kill off chess programs */
13352 if (first.pr != NoProc) {
13355 DoSleep( appData.delayBeforeQuit );
13356 SendToProgram("quit\n", &first);
13357 DoSleep( appData.delayAfterQuit );
13358 DestroyChildProcess(first.pr, 10 /* [AS] first.useSigterm */ );
13360 if (second.pr != NoProc) {
13361 DoSleep( appData.delayBeforeQuit );
13362 SendToProgram("quit\n", &second);
13363 DoSleep( appData.delayAfterQuit );
13364 DestroyChildProcess(second.pr, 10 /* [AS] second.useSigterm */ );
13366 if (first.isr != NULL) {
13367 RemoveInputSource(first.isr);
13369 if (second.isr != NULL) {
13370 RemoveInputSource(second.isr);
13373 if (pairing.pr != NoProc) SendToProgram("quit\n", &pairing);
13374 if (pairing.isr != NULL) RemoveInputSource(pairing.isr);
13376 ShutDownFrontEnd();
13381 PauseEngine (ChessProgramState *cps)
13383 SendToProgram("pause\n", cps);
13388 UnPauseEngine (ChessProgramState *cps)
13390 SendToProgram("resume\n", cps);
13397 if (appData.debugMode)
13398 fprintf(debugFP, "PauseEvent(): pausing %d\n", pausing);
13402 if(stalledEngine) { // [HGM] pause: resume game by releasing withheld move
13404 if(gameMode == TwoMachinesPlay) { // we might have to make the opponent resume pondering
13405 if(stalledEngine->other->pause) UnPauseEngine(stalledEngine->other);
13406 else if(appData.ponderNextMove) SendToProgram("hard\n", stalledEngine->other);
13408 if(appData.ponderNextMove) SendToProgram("hard\n", stalledEngine);
13409 HandleMachineMove(stashedInputMove, stalledEngine);
13410 stalledEngine = NULL;
13413 if (gameMode == MachinePlaysWhite ||
13414 gameMode == TwoMachinesPlay ||
13415 gameMode == MachinePlaysBlack) { // the thinking engine must have used pause mode, or it would have been stalledEngine
13416 if(first.pause) UnPauseEngine(&first);
13417 else if(appData.ponderNextMove) SendToProgram("hard\n", &first);
13418 if(second.pause) UnPauseEngine(&second);
13419 else if(gameMode == TwoMachinesPlay && appData.ponderNextMove) SendToProgram("hard\n", &second);
13422 DisplayBothClocks();
13424 if (gameMode == PlayFromGameFile) {
13425 if (appData.timeDelay >= 0)
13426 AutoPlayGameLoop();
13427 } else if (gameMode == IcsExamining && pauseExamInvalid) {
13428 Reset(FALSE, TRUE);
13429 SendToICS(ics_prefix);
13430 SendToICS("refresh\n");
13431 } else if (currentMove < forwardMostMove) {
13432 ForwardInner(forwardMostMove);
13434 pauseExamInvalid = FALSE;
13436 switch (gameMode) {
13440 pauseExamForwardMostMove = forwardMostMove;
13441 pauseExamInvalid = FALSE;
13444 case IcsPlayingWhite:
13445 case IcsPlayingBlack:
13449 case PlayFromGameFile:
13450 (void) StopLoadGameTimer();
13454 case BeginningOfGame:
13455 if (appData.icsActive) return;
13456 /* else fall through */
13457 case MachinePlaysWhite:
13458 case MachinePlaysBlack:
13459 case TwoMachinesPlay:
13460 if (forwardMostMove == 0)
13461 return; /* don't pause if no one has moved */
13462 if(gameMode == TwoMachinesPlay) { // [HGM] pause: stop clocks if engine can be paused immediately
13463 ChessProgramState *onMove = (WhiteOnMove(forwardMostMove) == (first.twoMachinesColor[0] == 'w') ? &first : &second);
13464 if(onMove->pause) { // thinking engine can be paused
13465 PauseEngine(onMove); // do it
13466 if(onMove->other->pause) // pondering opponent can always be paused immediately
13467 PauseEngine(onMove->other);
13469 SendToProgram("easy\n", onMove->other);
13472 } else if(gameMode == (WhiteOnMove(forwardMostMove) ? MachinePlaysWhite : MachinePlaysBlack)) { // engine on move
13474 PauseEngine(&first);
13477 } else { // human on move, pause pondering by either method
13479 PauseEngine(&first);
13481 SendToProgram("easy\n", &first);
13484 // if no immediate pausing is possible, wait for engine to move, and stop clocks then
13494 EditCommentEvent ()
13496 char title[MSG_SIZ];
13498 if (currentMove < 1 || parseList[currentMove - 1][0] == NULLCHAR) {
13499 safeStrCpy(title, _("Edit comment"), sizeof(title)/sizeof(title[0]));
13501 snprintf(title, MSG_SIZ, _("Edit comment on %d.%s%s"), (currentMove - 1) / 2 + 1,
13502 WhiteOnMove(currentMove - 1) ? " " : ".. ",
13503 parseList[currentMove - 1]);
13506 EditCommentPopUp(currentMove, title, commentList[currentMove]);
13513 char *tags = PGNTags(&gameInfo);
13515 EditTagsPopUp(tags, NULL);
13522 if(second.analyzing) {
13523 SendToProgram("exit\n", &second);
13524 second.analyzing = FALSE;
13526 if (second.pr == NoProc) StartChessProgram(&second);
13527 InitChessProgram(&second, FALSE);
13528 FeedMovesToProgram(&second, currentMove);
13530 SendToProgram("analyze\n", &second);
13531 second.analyzing = TRUE;
13535 /* Toggle ShowThinking */
13537 ToggleShowThinking()
13539 appData.showThinking = !appData.showThinking;
13540 ShowThinkingEvent();
13544 AnalyzeModeEvent ()
13548 if (!first.analysisSupport) {
13549 snprintf(buf, sizeof(buf), _("%s does not support analysis"), first.tidy);
13550 DisplayError(buf, 0);
13553 /* [DM] icsEngineAnalyze [HGM] This is horrible code; reverse the gameMode and isEngineAnalyze tests! */
13554 if (appData.icsActive) {
13555 if (gameMode != IcsObserving) {
13556 snprintf(buf, MSG_SIZ, _("You are not observing a game"));
13557 DisplayError(buf, 0);
13559 if (appData.icsEngineAnalyze) {
13560 if (appData.debugMode)
13561 fprintf(debugFP, _("Found unexpected active ICS engine analyze \n"));
13567 /* if enable, user wants to disable icsEngineAnalyze */
13568 if (appData.icsEngineAnalyze) {
13573 appData.icsEngineAnalyze = TRUE;
13574 if (appData.debugMode)
13575 fprintf(debugFP, _("ICS engine analyze starting... \n"));
13578 if (gameMode == AnalyzeMode) { ToggleSecond(); return 0; }
13579 if (appData.noChessProgram || gameMode == AnalyzeMode)
13582 if (gameMode != AnalyzeFile) {
13583 if (!appData.icsEngineAnalyze) {
13585 if (gameMode != EditGame) return 0;
13587 if (!appData.showThinking) ToggleShowThinking();
13588 ResurrectChessProgram();
13589 SendToProgram("analyze\n", &first);
13590 first.analyzing = TRUE;
13591 /*first.maybeThinking = TRUE;*/
13592 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
13593 EngineOutputPopUp();
13595 if (!appData.icsEngineAnalyze) gameMode = AnalyzeMode;
13600 StartAnalysisClock();
13601 GetTimeMark(&lastNodeCountTime);
13607 AnalyzeFileEvent ()
13609 if (appData.noChessProgram || gameMode == AnalyzeFile)
13612 if (!first.analysisSupport) {
13614 snprintf(buf, sizeof(buf), _("%s does not support analysis"), first.tidy);
13615 DisplayError(buf, 0);
13619 if (gameMode != AnalyzeMode) {
13621 if (gameMode != EditGame) return;
13622 if (!appData.showThinking) ToggleShowThinking();
13623 ResurrectChessProgram();
13624 SendToProgram("analyze\n", &first);
13625 first.analyzing = TRUE;
13626 /*first.maybeThinking = TRUE;*/
13627 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
13628 EngineOutputPopUp();
13630 gameMode = AnalyzeFile;
13635 StartAnalysisClock();
13636 GetTimeMark(&lastNodeCountTime);
13638 if(appData.timeDelay > 0) StartLoadGameTimer((long)(1000.0f * appData.timeDelay));
13639 AnalysisPeriodicEvent(1);
13643 MachineWhiteEvent ()
13646 char *bookHit = NULL;
13648 if (appData.noChessProgram || (gameMode == MachinePlaysWhite))
13652 if (gameMode == PlayFromGameFile ||
13653 gameMode == TwoMachinesPlay ||
13654 gameMode == Training ||
13655 gameMode == AnalyzeMode ||
13656 gameMode == EndOfGame)
13659 if (gameMode == EditPosition)
13660 EditPositionDone(TRUE);
13662 if (!WhiteOnMove(currentMove)) {
13663 DisplayError(_("It is not White's turn"), 0);
13667 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile)
13670 if (gameMode == EditGame || gameMode == AnalyzeMode ||
13671 gameMode == AnalyzeFile)
13674 ResurrectChessProgram(); /* in case it isn't running */
13675 if(gameMode == BeginningOfGame) { /* [HGM] time odds: to get right odds in human mode */
13676 gameMode = MachinePlaysWhite;
13679 gameMode = MachinePlaysWhite;
13683 snprintf(buf, MSG_SIZ, "%s %s %s", gameInfo.white, _("vs."), gameInfo.black);
13685 if (first.sendName) {
13686 snprintf(buf, MSG_SIZ, "name %s\n", gameInfo.black);
13687 SendToProgram(buf, &first);
13689 if (first.sendTime) {
13690 if (first.useColors) {
13691 SendToProgram("black\n", &first); /*gnu kludge*/
13693 SendTimeRemaining(&first, TRUE);
13695 if (first.useColors) {
13696 SendToProgram("white\n", &first); // [HGM] book: send 'go' separately
13698 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: send go or retrieve book move
13699 SetMachineThinkingEnables();
13700 first.maybeThinking = TRUE;
13704 if (appData.autoFlipView && !flipView) {
13705 flipView = !flipView;
13706 DrawPosition(FALSE, NULL);
13707 DisplayBothClocks(); // [HGM] logo: clocks might have to be exchanged;
13710 if(bookHit) { // [HGM] book: simulate book reply
13711 static char bookMove[MSG_SIZ]; // a bit generous?
13713 programStats.nodes = programStats.depth = programStats.time =
13714 programStats.score = programStats.got_only_move = 0;
13715 sprintf(programStats.movelist, "%s (xbook)", bookHit);
13717 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
13718 strcat(bookMove, bookHit);
13719 HandleMachineMove(bookMove, &first);
13724 MachineBlackEvent ()
13727 char *bookHit = NULL;
13729 if (appData.noChessProgram || (gameMode == MachinePlaysBlack))
13733 if (gameMode == PlayFromGameFile ||
13734 gameMode == TwoMachinesPlay ||
13735 gameMode == Training ||
13736 gameMode == AnalyzeMode ||
13737 gameMode == EndOfGame)
13740 if (gameMode == EditPosition)
13741 EditPositionDone(TRUE);
13743 if (WhiteOnMove(currentMove)) {
13744 DisplayError(_("It is not Black's turn"), 0);
13748 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile)
13751 if (gameMode == EditGame || gameMode == AnalyzeMode ||
13752 gameMode == AnalyzeFile)
13755 ResurrectChessProgram(); /* in case it isn't running */
13756 gameMode = MachinePlaysBlack;
13760 snprintf(buf, MSG_SIZ, "%s %s %s", gameInfo.white, _("vs."), gameInfo.black);
13762 if (first.sendName) {
13763 snprintf(buf, MSG_SIZ, "name %s\n", gameInfo.white);
13764 SendToProgram(buf, &first);
13766 if (first.sendTime) {
13767 if (first.useColors) {
13768 SendToProgram("white\n", &first); /*gnu kludge*/
13770 SendTimeRemaining(&first, FALSE);
13772 if (first.useColors) {
13773 SendToProgram("black\n", &first); // [HGM] book: 'go' sent separately
13775 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: send go or retrieve book move
13776 SetMachineThinkingEnables();
13777 first.maybeThinking = TRUE;
13780 if (appData.autoFlipView && flipView) {
13781 flipView = !flipView;
13782 DrawPosition(FALSE, NULL);
13783 DisplayBothClocks(); // [HGM] logo: clocks might have to be exchanged;
13785 if(bookHit) { // [HGM] book: simulate book reply
13786 static char bookMove[MSG_SIZ]; // a bit generous?
13788 programStats.nodes = programStats.depth = programStats.time =
13789 programStats.score = programStats.got_only_move = 0;
13790 sprintf(programStats.movelist, "%s (xbook)", bookHit);
13792 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
13793 strcat(bookMove, bookHit);
13794 HandleMachineMove(bookMove, &first);
13800 DisplayTwoMachinesTitle ()
13803 if (appData.matchGames > 0) {
13804 if(appData.tourneyFile[0]) {
13805 snprintf(buf, MSG_SIZ, "%s %s %s (%d/%d%s)",
13806 gameInfo.white, _("vs."), gameInfo.black,
13807 nextGame+1, appData.matchGames+1,
13808 appData.tourneyType>0 ? "gt" : appData.tourneyType<0 ? "sw" : "rr");
13810 if (first.twoMachinesColor[0] == 'w') {
13811 snprintf(buf, MSG_SIZ, "%s %s %s (%d-%d-%d)",
13812 gameInfo.white, _("vs."), gameInfo.black,
13813 first.matchWins, second.matchWins,
13814 matchGame - 1 - (first.matchWins + second.matchWins));
13816 snprintf(buf, MSG_SIZ, "%s %s %s (%d-%d-%d)",
13817 gameInfo.white, _("vs."), gameInfo.black,
13818 second.matchWins, first.matchWins,
13819 matchGame - 1 - (first.matchWins + second.matchWins));
13822 snprintf(buf, MSG_SIZ, "%s %s %s", gameInfo.white, _("vs."), gameInfo.black);
13828 SettingsMenuIfReady ()
13830 if (second.lastPing != second.lastPong) {
13831 DisplayMessage("", _("Waiting for second chess program"));
13832 ScheduleDelayedEvent(SettingsMenuIfReady, 10); // [HGM] fast: lowered from 1000
13836 DisplayMessage("", "");
13837 SettingsPopUp(&second);
13841 WaitForEngine (ChessProgramState *cps, DelayedEventCallback retry)
13844 if (cps->pr == NoProc) {
13845 StartChessProgram(cps);
13846 if (cps->protocolVersion == 1) {
13849 /* kludge: allow timeout for initial "feature" command */
13851 snprintf(buf, MSG_SIZ, _("Starting %s chess program"), _(cps->which));
13852 DisplayMessage("", buf);
13853 ScheduleDelayedEvent(retry, FEATURE_TIMEOUT);
13861 TwoMachinesEvent P((void))
13865 ChessProgramState *onmove;
13866 char *bookHit = NULL;
13867 static int stalling = 0;
13871 if (appData.noChessProgram) return;
13873 switch (gameMode) {
13874 case TwoMachinesPlay:
13876 case MachinePlaysWhite:
13877 case MachinePlaysBlack:
13878 if (WhiteOnMove(forwardMostMove) == (gameMode == MachinePlaysWhite)) {
13879 DisplayError(_("Wait until your turn,\nor select Move Now"), 0);
13883 case BeginningOfGame:
13884 case PlayFromGameFile:
13887 if (gameMode != EditGame) return;
13890 EditPositionDone(TRUE);
13901 // forwardMostMove = currentMove;
13902 TruncateGame(); // [HGM] vari: MachineWhite and MachineBlack do this...
13904 if(!ResurrectChessProgram()) return; /* in case first program isn't running (unbalances its ping due to InitChessProgram!) */
13906 if(WaitForEngine(&second, TwoMachinesEventIfReady)) return; // (if needed:) started up second engine, so wait for features
13907 if(first.lastPing != first.lastPong) { // [HGM] wait till we are sure first engine has set up position
13908 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10);
13912 if(second.protocolVersion >= 2 && !strstr(second.variants, VariantName(gameInfo.variant))) {
13913 DisplayError("second engine does not play this", 0);
13918 InitChessProgram(&second, FALSE); // unbalances ping of second engine
13919 SendToProgram("force\n", &second);
13921 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10);
13924 GetTimeMark(&now); // [HGM] matchpause: implement match pause after engine load
13925 if(appData.matchPause>10000 || appData.matchPause<10)
13926 appData.matchPause = 10000; /* [HGM] make pause adjustable */
13927 wait = SubtractTimeMarks(&now, &pauseStart);
13928 if(wait < appData.matchPause) {
13929 ScheduleDelayedEvent(TwoMachinesEventIfReady, appData.matchPause - wait);
13932 // we are now committed to starting the game
13934 DisplayMessage("", "");
13935 if (startedFromSetupPosition) {
13936 SendBoard(&second, backwardMostMove);
13937 if (appData.debugMode) {
13938 fprintf(debugFP, "Two Machines\n");
13941 for (i = backwardMostMove; i < forwardMostMove; i++) {
13942 SendMoveToProgram(i, &second);
13945 gameMode = TwoMachinesPlay;
13947 ModeHighlight(); // [HGM] logo: this triggers display update of logos
13949 DisplayTwoMachinesTitle();
13951 if ((first.twoMachinesColor[0] == 'w') == WhiteOnMove(forwardMostMove)) {
13956 if(appData.debugMode) fprintf(debugFP, "New game (%d): %s-%s (%c)\n", matchGame, first.tidy, second.tidy, first.twoMachinesColor[0]);
13957 SendToProgram(first.computerString, &first);
13958 if (first.sendName) {
13959 snprintf(buf, MSG_SIZ, "name %s\n", second.tidy);
13960 SendToProgram(buf, &first);
13962 SendToProgram(second.computerString, &second);
13963 if (second.sendName) {
13964 snprintf(buf, MSG_SIZ, "name %s\n", first.tidy);
13965 SendToProgram(buf, &second);
13969 if (!first.sendTime || !second.sendTime) {
13970 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
13971 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
13973 if (onmove->sendTime) {
13974 if (onmove->useColors) {
13975 SendToProgram(onmove->other->twoMachinesColor, onmove); /*gnu kludge*/
13977 SendTimeRemaining(onmove, WhiteOnMove(forwardMostMove));
13979 if (onmove->useColors) {
13980 SendToProgram(onmove->twoMachinesColor, onmove);
13982 bookHit = SendMoveToBookUser(forwardMostMove-1, onmove, TRUE); // [HGM] book: send go or retrieve book move
13983 // SendToProgram("go\n", onmove);
13984 onmove->maybeThinking = TRUE;
13985 SetMachineThinkingEnables();
13989 if(bookHit) { // [HGM] book: simulate book reply
13990 static char bookMove[MSG_SIZ]; // a bit generous?
13992 programStats.nodes = programStats.depth = programStats.time =
13993 programStats.score = programStats.got_only_move = 0;
13994 sprintf(programStats.movelist, "%s (xbook)", bookHit);
13996 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
13997 strcat(bookMove, bookHit);
13998 savedMessage = bookMove; // args for deferred call
13999 savedState = onmove;
14000 ScheduleDelayedEvent(DeferredBookMove, 1);
14007 if (gameMode == Training) {
14008 SetTrainingModeOff();
14009 gameMode = PlayFromGameFile;
14010 DisplayMessage("", _("Training mode off"));
14012 gameMode = Training;
14013 animateTraining = appData.animate;
14015 /* make sure we are not already at the end of the game */
14016 if (currentMove < forwardMostMove) {
14017 SetTrainingModeOn();
14018 DisplayMessage("", _("Training mode on"));
14020 gameMode = PlayFromGameFile;
14021 DisplayError(_("Already at end of game"), 0);
14030 if (!appData.icsActive) return;
14031 switch (gameMode) {
14032 case IcsPlayingWhite:
14033 case IcsPlayingBlack:
14036 case BeginningOfGame:
14044 EditPositionDone(TRUE);
14057 gameMode = IcsIdle;
14067 switch (gameMode) {
14069 SetTrainingModeOff();
14071 case MachinePlaysWhite:
14072 case MachinePlaysBlack:
14073 case BeginningOfGame:
14074 SendToProgram("force\n", &first);
14075 SetUserThinkingEnables();
14077 case PlayFromGameFile:
14078 (void) StopLoadGameTimer();
14079 if (gameFileFP != NULL) {
14084 EditPositionDone(TRUE);
14089 SendToProgram("force\n", &first);
14091 case TwoMachinesPlay:
14092 GameEnds(EndOfFile, NULL, GE_PLAYER);
14093 ResurrectChessProgram();
14094 SetUserThinkingEnables();
14097 ResurrectChessProgram();
14099 case IcsPlayingBlack:
14100 case IcsPlayingWhite:
14101 DisplayError(_("Warning: You are still playing a game"), 0);
14104 DisplayError(_("Warning: You are still observing a game"), 0);
14107 DisplayError(_("Warning: You are still examining a game"), 0);
14118 first.offeredDraw = second.offeredDraw = 0;
14120 if (gameMode == PlayFromGameFile) {
14121 whiteTimeRemaining = timeRemaining[0][currentMove];
14122 blackTimeRemaining = timeRemaining[1][currentMove];
14126 if (gameMode == MachinePlaysWhite ||
14127 gameMode == MachinePlaysBlack ||
14128 gameMode == TwoMachinesPlay ||
14129 gameMode == EndOfGame) {
14130 i = forwardMostMove;
14131 while (i > currentMove) {
14132 SendToProgram("undo\n", &first);
14135 if(!adjustedClock) {
14136 whiteTimeRemaining = timeRemaining[0][currentMove];
14137 blackTimeRemaining = timeRemaining[1][currentMove];
14138 DisplayBothClocks();
14140 if (whiteFlag || blackFlag) {
14141 whiteFlag = blackFlag = 0;
14146 gameMode = EditGame;
14153 EditPositionEvent ()
14155 if (gameMode == EditPosition) {
14161 if (gameMode != EditGame) return;
14163 gameMode = EditPosition;
14166 if (currentMove > 0)
14167 CopyBoard(boards[0], boards[currentMove]);
14169 blackPlaysFirst = !WhiteOnMove(currentMove);
14171 currentMove = forwardMostMove = backwardMostMove = 0;
14172 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
14174 if(!appData.pieceMenu) DisplayMessage(_("Click clock to clear board"), "");
14180 /* [DM] icsEngineAnalyze - possible call from other functions */
14181 if (appData.icsEngineAnalyze) {
14182 appData.icsEngineAnalyze = FALSE;
14184 DisplayMessage("",_("Close ICS engine analyze..."));
14186 if (first.analysisSupport && first.analyzing) {
14187 SendToBoth("exit\n");
14188 first.analyzing = second.analyzing = FALSE;
14190 thinkOutput[0] = NULLCHAR;
14194 EditPositionDone (Boolean fakeRights)
14196 int king = gameInfo.variant == VariantKnightmate ? WhiteUnicorn : WhiteKing;
14198 startedFromSetupPosition = TRUE;
14199 InitChessProgram(&first, FALSE);
14200 if(fakeRights) { // [HGM] suppress this if we just pasted a FEN.
14201 boards[0][EP_STATUS] = EP_NONE;
14202 boards[0][CASTLING][2] = boards[0][CASTLING][5] = BOARD_WIDTH>>1;
14203 if(boards[0][0][BOARD_WIDTH>>1] == king) {
14204 boards[0][CASTLING][1] = boards[0][0][BOARD_LEFT] == WhiteRook ? BOARD_LEFT : NoRights;
14205 boards[0][CASTLING][0] = boards[0][0][BOARD_RGHT-1] == WhiteRook ? BOARD_RGHT-1 : NoRights;
14206 } else boards[0][CASTLING][2] = NoRights;
14207 if(boards[0][BOARD_HEIGHT-1][BOARD_WIDTH>>1] == WHITE_TO_BLACK king) {
14208 boards[0][CASTLING][4] = boards[0][BOARD_HEIGHT-1][BOARD_LEFT] == BlackRook ? BOARD_LEFT : NoRights;
14209 boards[0][CASTLING][3] = boards[0][BOARD_HEIGHT-1][BOARD_RGHT-1] == BlackRook ? BOARD_RGHT-1 : NoRights;
14210 } else boards[0][CASTLING][5] = NoRights;
14211 if(gameInfo.variant == VariantSChess) {
14213 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // pieces in their original position are assumed virgin
14214 boards[0][VIRGIN][i] = 0;
14215 if(boards[0][0][i] == FIDEArray[0][i-BOARD_LEFT]) boards[0][VIRGIN][i] |= VIRGIN_W;
14216 if(boards[0][BOARD_HEIGHT-1][i] == FIDEArray[1][i-BOARD_LEFT]) boards[0][VIRGIN][i] |= VIRGIN_B;
14220 SendToProgram("force\n", &first);
14221 if (blackPlaysFirst) {
14222 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
14223 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
14224 currentMove = forwardMostMove = backwardMostMove = 1;
14225 CopyBoard(boards[1], boards[0]);
14227 currentMove = forwardMostMove = backwardMostMove = 0;
14229 SendBoard(&first, forwardMostMove);
14230 if (appData.debugMode) {
14231 fprintf(debugFP, "EditPosDone\n");
14234 DisplayMessage("", "");
14235 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
14236 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
14237 gameMode = EditGame;
14239 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
14240 ClearHighlights(); /* [AS] */
14243 /* Pause for `ms' milliseconds */
14244 /* !! Ugh, this is a kludge. Fix it sometime. --tpm */
14246 TimeDelay (long ms)
14253 } while (SubtractTimeMarks(&m2, &m1) < ms);
14256 /* !! Ugh, this is a kludge. Fix it sometime. --tpm */
14258 SendMultiLineToICS (char *buf)
14260 char temp[MSG_SIZ+1], *p;
14267 strncpy(temp, buf, len);
14272 if (*p == '\n' || *p == '\r')
14277 strcat(temp, "\n");
14279 SendToPlayer(temp, strlen(temp));
14283 SetWhiteToPlayEvent ()
14285 if (gameMode == EditPosition) {
14286 blackPlaysFirst = FALSE;
14287 DisplayBothClocks(); /* works because currentMove is 0 */
14288 } else if (gameMode == IcsExamining) {
14289 SendToICS(ics_prefix);
14290 SendToICS("tomove white\n");
14295 SetBlackToPlayEvent ()
14297 if (gameMode == EditPosition) {
14298 blackPlaysFirst = TRUE;
14299 currentMove = 1; /* kludge */
14300 DisplayBothClocks();
14302 } else if (gameMode == IcsExamining) {
14303 SendToICS(ics_prefix);
14304 SendToICS("tomove black\n");
14309 EditPositionMenuEvent (ChessSquare selection, int x, int y)
14312 ChessSquare piece = boards[0][y][x];
14314 if (gameMode != EditPosition && gameMode != IcsExamining) return;
14316 switch (selection) {
14318 if (gameMode == IcsExamining && ics_type == ICS_FICS) {
14319 SendToICS(ics_prefix);
14320 SendToICS("bsetup clear\n");
14321 } else if (gameMode == IcsExamining && ics_type == ICS_ICC) {
14322 SendToICS(ics_prefix);
14323 SendToICS("clearboard\n");
14325 for (x = 0; x < BOARD_WIDTH; x++) { ChessSquare p = EmptySquare;
14326 if(x == BOARD_LEFT-1 || x == BOARD_RGHT) p = (ChessSquare) 0; /* [HGM] holdings */
14327 for (y = 0; y < BOARD_HEIGHT; y++) {
14328 if (gameMode == IcsExamining) {
14329 if (boards[currentMove][y][x] != EmptySquare) {
14330 snprintf(buf, MSG_SIZ, "%sx@%c%c\n", ics_prefix,
14335 boards[0][y][x] = p;
14340 if (gameMode == EditPosition) {
14341 DrawPosition(FALSE, boards[0]);
14346 SetWhiteToPlayEvent();
14350 SetBlackToPlayEvent();
14354 if (gameMode == IcsExamining) {
14355 if (x < BOARD_LEFT || x >= BOARD_RGHT) break; // [HGM] holdings
14356 snprintf(buf, MSG_SIZ, "%sx@%c%c\n", ics_prefix, AAA + x, ONE + y);
14359 if(x < BOARD_LEFT || x >= BOARD_RGHT) {
14360 if(x == BOARD_LEFT-2) {
14361 if(y < BOARD_HEIGHT-1-gameInfo.holdingsSize) break;
14362 boards[0][y][1] = 0;
14364 if(x == BOARD_RGHT+1) {
14365 if(y >= gameInfo.holdingsSize) break;
14366 boards[0][y][BOARD_WIDTH-2] = 0;
14369 boards[0][y][x] = EmptySquare;
14370 DrawPosition(FALSE, boards[0]);
14375 if(piece >= (int)WhitePawn && piece < (int)WhiteMan ||
14376 piece >= (int)BlackPawn && piece < (int)BlackMan ) {
14377 selection = (ChessSquare) (PROMOTED piece);
14378 } else if(piece == EmptySquare) selection = WhiteSilver;
14379 else selection = (ChessSquare)((int)piece - 1);
14383 if(piece > (int)WhiteMan && piece <= (int)WhiteKing ||
14384 piece > (int)BlackMan && piece <= (int)BlackKing ) {
14385 selection = (ChessSquare) (DEMOTED piece);
14386 } else if(piece == EmptySquare) selection = BlackSilver;
14387 else selection = (ChessSquare)((int)piece + 1);
14392 if(gameInfo.variant == VariantShatranj ||
14393 gameInfo.variant == VariantXiangqi ||
14394 gameInfo.variant == VariantCourier ||
14395 gameInfo.variant == VariantMakruk )
14396 selection = (ChessSquare)((int)selection - (int)WhiteQueen + (int)WhiteFerz);
14401 if(gameInfo.variant == VariantXiangqi)
14402 selection = (ChessSquare)((int)selection - (int)WhiteKing + (int)WhiteWazir);
14403 if(gameInfo.variant == VariantKnightmate)
14404 selection = (ChessSquare)((int)selection - (int)WhiteKing + (int)WhiteUnicorn);
14407 if (gameMode == IcsExamining) {
14408 if (x < BOARD_LEFT || x >= BOARD_RGHT) break; // [HGM] holdings
14409 snprintf(buf, MSG_SIZ, "%s%c@%c%c\n", ics_prefix,
14410 PieceToChar(selection), AAA + x, ONE + y);
14413 if(x < BOARD_LEFT || x >= BOARD_RGHT) {
14415 if(x == BOARD_LEFT-2 && selection >= BlackPawn) {
14416 n = PieceToNumber(selection - BlackPawn);
14417 if(n >= gameInfo.holdingsSize) { n = 0; selection = BlackPawn; }
14418 boards[0][BOARD_HEIGHT-1-n][0] = selection;
14419 boards[0][BOARD_HEIGHT-1-n][1]++;
14421 if(x == BOARD_RGHT+1 && selection < BlackPawn) {
14422 n = PieceToNumber(selection);
14423 if(n >= gameInfo.holdingsSize) { n = 0; selection = WhitePawn; }
14424 boards[0][n][BOARD_WIDTH-1] = selection;
14425 boards[0][n][BOARD_WIDTH-2]++;
14428 boards[0][y][x] = selection;
14429 DrawPosition(TRUE, boards[0]);
14431 fromX = fromY = -1;
14439 DropMenuEvent (ChessSquare selection, int x, int y)
14441 ChessMove moveType;
14443 switch (gameMode) {
14444 case IcsPlayingWhite:
14445 case MachinePlaysBlack:
14446 if (!WhiteOnMove(currentMove)) {
14447 DisplayMoveError(_("It is Black's turn"));
14450 moveType = WhiteDrop;
14452 case IcsPlayingBlack:
14453 case MachinePlaysWhite:
14454 if (WhiteOnMove(currentMove)) {
14455 DisplayMoveError(_("It is White's turn"));
14458 moveType = BlackDrop;
14461 moveType = WhiteOnMove(currentMove) ? WhiteDrop : BlackDrop;
14467 if (moveType == BlackDrop && selection < BlackPawn) {
14468 selection = (ChessSquare) ((int) selection
14469 + (int) BlackPawn - (int) WhitePawn);
14471 if (boards[currentMove][y][x] != EmptySquare) {
14472 DisplayMoveError(_("That square is occupied"));
14476 FinishMove(moveType, (int) selection, DROP_RANK, x, y, NULLCHAR);
14482 /* Accept a pending offer of any kind from opponent */
14484 if (appData.icsActive) {
14485 SendToICS(ics_prefix);
14486 SendToICS("accept\n");
14487 } else if (cmailMsgLoaded) {
14488 if (currentMove == cmailOldMove &&
14489 commentList[cmailOldMove] != NULL &&
14490 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
14491 "Black offers a draw" : "White offers a draw")) {
14493 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
14494 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_ACCEPT;
14496 DisplayError(_("There is no pending offer on this move"), 0);
14497 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
14500 /* Not used for offers from chess program */
14507 /* Decline a pending offer of any kind from opponent */
14509 if (appData.icsActive) {
14510 SendToICS(ics_prefix);
14511 SendToICS("decline\n");
14512 } else if (cmailMsgLoaded) {
14513 if (currentMove == cmailOldMove &&
14514 commentList[cmailOldMove] != NULL &&
14515 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
14516 "Black offers a draw" : "White offers a draw")) {
14518 AppendComment(cmailOldMove, "Draw declined", TRUE);
14519 DisplayComment(cmailOldMove - 1, "Draw declined");
14522 DisplayError(_("There is no pending offer on this move"), 0);
14525 /* Not used for offers from chess program */
14532 /* Issue ICS rematch command */
14533 if (appData.icsActive) {
14534 SendToICS(ics_prefix);
14535 SendToICS("rematch\n");
14542 /* Call your opponent's flag (claim a win on time) */
14543 if (appData.icsActive) {
14544 SendToICS(ics_prefix);
14545 SendToICS("flag\n");
14547 switch (gameMode) {
14550 case MachinePlaysWhite:
14553 GameEnds(GameIsDrawn, "Both players ran out of time",
14556 GameEnds(BlackWins, "Black wins on time", GE_PLAYER);
14558 DisplayError(_("Your opponent is not out of time"), 0);
14561 case MachinePlaysBlack:
14564 GameEnds(GameIsDrawn, "Both players ran out of time",
14567 GameEnds(WhiteWins, "White wins on time", GE_PLAYER);
14569 DisplayError(_("Your opponent is not out of time"), 0);
14577 ClockClick (int which)
14578 { // [HGM] code moved to back-end from winboard.c
14579 if(which) { // black clock
14580 if (gameMode == EditPosition || gameMode == IcsExamining) {
14581 if(!appData.pieceMenu && blackPlaysFirst) EditPositionMenuEvent(ClearBoard, 0, 0);
14582 SetBlackToPlayEvent();
14583 } else if ((gameMode == AnalyzeMode || gameMode == EditGame) && !blackFlag && WhiteOnMove(currentMove)) {
14584 UserMoveEvent((int)EmptySquare, DROP_RANK, 0, 0, 0); // [HGM] multi-move: if not out of time, enters null move
14585 } else if (shiftKey) {
14586 AdjustClock(which, -1);
14587 } else if (gameMode == IcsPlayingWhite ||
14588 gameMode == MachinePlaysBlack) {
14591 } else { // white clock
14592 if (gameMode == EditPosition || gameMode == IcsExamining) {
14593 if(!appData.pieceMenu && !blackPlaysFirst) EditPositionMenuEvent(ClearBoard, 0, 0);
14594 SetWhiteToPlayEvent();
14595 } else if ((gameMode == AnalyzeMode || gameMode == EditGame) && !whiteFlag && !WhiteOnMove(currentMove)) {
14596 UserMoveEvent((int)EmptySquare, DROP_RANK, 0, 0, 0); // [HGM] multi-move
14597 } else if (shiftKey) {
14598 AdjustClock(which, -1);
14599 } else if (gameMode == IcsPlayingBlack ||
14600 gameMode == MachinePlaysWhite) {
14609 /* Offer draw or accept pending draw offer from opponent */
14611 if (appData.icsActive) {
14612 /* Note: tournament rules require draw offers to be
14613 made after you make your move but before you punch
14614 your clock. Currently ICS doesn't let you do that;
14615 instead, you immediately punch your clock after making
14616 a move, but you can offer a draw at any time. */
14618 SendToICS(ics_prefix);
14619 SendToICS("draw\n");
14620 userOfferedDraw = TRUE; // [HGM] drawclaim: also set flag in ICS play
14621 } else if (cmailMsgLoaded) {
14622 if (currentMove == cmailOldMove &&
14623 commentList[cmailOldMove] != NULL &&
14624 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
14625 "Black offers a draw" : "White offers a draw")) {
14626 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
14627 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_ACCEPT;
14628 } else if (currentMove == cmailOldMove + 1) {
14629 char *offer = WhiteOnMove(cmailOldMove) ?
14630 "White offers a draw" : "Black offers a draw";
14631 AppendComment(currentMove, offer, TRUE);
14632 DisplayComment(currentMove - 1, offer);
14633 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_DRAW;
14635 DisplayError(_("You must make your move before offering a draw"), 0);
14636 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
14638 } else if (first.offeredDraw) {
14639 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
14641 if (first.sendDrawOffers) {
14642 SendToProgram("draw\n", &first);
14643 userOfferedDraw = TRUE;
14651 /* Offer Adjourn or accept pending Adjourn offer from opponent */
14653 if (appData.icsActive) {
14654 SendToICS(ics_prefix);
14655 SendToICS("adjourn\n");
14657 /* Currently GNU Chess doesn't offer or accept Adjourns */
14665 /* Offer Abort or accept pending Abort offer from opponent */
14667 if (appData.icsActive) {
14668 SendToICS(ics_prefix);
14669 SendToICS("abort\n");
14671 GameEnds(GameUnfinished, "Game aborted", GE_PLAYER);
14678 /* Resign. You can do this even if it's not your turn. */
14680 if (appData.icsActive) {
14681 SendToICS(ics_prefix);
14682 SendToICS("resign\n");
14684 switch (gameMode) {
14685 case MachinePlaysWhite:
14686 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
14688 case MachinePlaysBlack:
14689 GameEnds(BlackWins, "White resigns", GE_PLAYER);
14692 if (cmailMsgLoaded) {
14694 if (WhiteOnMove(cmailOldMove)) {
14695 GameEnds(BlackWins, "White resigns", GE_PLAYER);
14697 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
14699 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_RESIGN;
14710 StopObservingEvent ()
14712 /* Stop observing current games */
14713 SendToICS(ics_prefix);
14714 SendToICS("unobserve\n");
14718 StopExaminingEvent ()
14720 /* Stop observing current game */
14721 SendToICS(ics_prefix);
14722 SendToICS("unexamine\n");
14726 ForwardInner (int target)
14728 int limit; int oldSeekGraphUp = seekGraphUp;
14730 if (appData.debugMode)
14731 fprintf(debugFP, "ForwardInner(%d), current %d, forward %d\n",
14732 target, currentMove, forwardMostMove);
14734 if (gameMode == EditPosition)
14737 seekGraphUp = FALSE;
14738 MarkTargetSquares(1);
14740 if (gameMode == PlayFromGameFile && !pausing)
14743 if (gameMode == IcsExamining && pausing)
14744 limit = pauseExamForwardMostMove;
14746 limit = forwardMostMove;
14748 if (target > limit) target = limit;
14750 if (target > 0 && moveList[target - 1][0]) {
14751 int fromX, fromY, toX, toY;
14752 toX = moveList[target - 1][2] - AAA;
14753 toY = moveList[target - 1][3] - ONE;
14754 if (moveList[target - 1][1] == '@') {
14755 if (appData.highlightLastMove) {
14756 SetHighlights(-1, -1, toX, toY);
14759 fromX = moveList[target - 1][0] - AAA;
14760 fromY = moveList[target - 1][1] - ONE;
14761 if (target == currentMove + 1) {
14762 AnimateMove(boards[currentMove], fromX, fromY, toX, toY);
14764 if (appData.highlightLastMove) {
14765 SetHighlights(fromX, fromY, toX, toY);
14769 if (gameMode == EditGame || gameMode == AnalyzeMode ||
14770 gameMode == Training || gameMode == PlayFromGameFile ||
14771 gameMode == AnalyzeFile) {
14772 while (currentMove < target) {
14773 if(second.analyzing) SendMoveToProgram(currentMove, &second);
14774 SendMoveToProgram(currentMove++, &first);
14777 currentMove = target;
14780 if (gameMode == EditGame || gameMode == EndOfGame) {
14781 whiteTimeRemaining = timeRemaining[0][currentMove];
14782 blackTimeRemaining = timeRemaining[1][currentMove];
14784 DisplayBothClocks();
14785 DisplayMove(currentMove - 1);
14786 DrawPosition(oldSeekGraphUp, boards[currentMove]);
14787 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
14788 if ( !matchMode && gameMode != Training) { // [HGM] PV info: routine tests if empty
14789 DisplayComment(currentMove - 1, commentList[currentMove]);
14791 ClearMap(); // [HGM] exclude: invalidate map
14798 if (gameMode == IcsExamining && !pausing) {
14799 SendToICS(ics_prefix);
14800 SendToICS("forward\n");
14802 ForwardInner(currentMove + 1);
14809 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
14810 /* to optimze, we temporarily turn off analysis mode while we feed
14811 * the remaining moves to the engine. Otherwise we get analysis output
14814 if (first.analysisSupport) {
14815 SendToProgram("exit\nforce\n", &first);
14816 first.analyzing = FALSE;
14820 if (gameMode == IcsExamining && !pausing) {
14821 SendToICS(ics_prefix);
14822 SendToICS("forward 999999\n");
14824 ForwardInner(forwardMostMove);
14827 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
14828 /* we have fed all the moves, so reactivate analysis mode */
14829 SendToProgram("analyze\n", &first);
14830 first.analyzing = TRUE;
14831 /*first.maybeThinking = TRUE;*/
14832 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
14837 BackwardInner (int target)
14839 int full_redraw = TRUE; /* [AS] Was FALSE, had to change it! */
14841 if (appData.debugMode)
14842 fprintf(debugFP, "BackwardInner(%d), current %d, forward %d\n",
14843 target, currentMove, forwardMostMove);
14845 if (gameMode == EditPosition) return;
14846 seekGraphUp = FALSE;
14847 MarkTargetSquares(1);
14848 if (currentMove <= backwardMostMove) {
14850 DrawPosition(full_redraw, boards[currentMove]);
14853 if (gameMode == PlayFromGameFile && !pausing)
14856 if (moveList[target][0]) {
14857 int fromX, fromY, toX, toY;
14858 toX = moveList[target][2] - AAA;
14859 toY = moveList[target][3] - ONE;
14860 if (moveList[target][1] == '@') {
14861 if (appData.highlightLastMove) {
14862 SetHighlights(-1, -1, toX, toY);
14865 fromX = moveList[target][0] - AAA;
14866 fromY = moveList[target][1] - ONE;
14867 if (target == currentMove - 1) {
14868 AnimateMove(boards[currentMove], toX, toY, fromX, fromY);
14870 if (appData.highlightLastMove) {
14871 SetHighlights(fromX, fromY, toX, toY);
14875 if (gameMode == EditGame || gameMode==AnalyzeMode ||
14876 gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
14877 while (currentMove > target) {
14878 if(moveList[currentMove-1][1] == '@' && moveList[currentMove-1][0] == '@') {
14879 // null move cannot be undone. Reload program with move history before it.
14881 for(i=target; i>backwardMostMove; i--) { // seek back to start or previous null move
14882 if(moveList[i-1][1] == '@' && moveList[i-1][0] == '@') break;
14884 SendBoard(&first, i);
14885 if(second.analyzing) SendBoard(&second, i);
14886 for(currentMove=i; currentMove<target; currentMove++) {
14887 SendMoveToProgram(currentMove, &first);
14888 if(second.analyzing) SendMoveToProgram(currentMove, &second);
14892 SendToBoth("undo\n");
14896 currentMove = target;
14899 if (gameMode == EditGame || gameMode == EndOfGame) {
14900 whiteTimeRemaining = timeRemaining[0][currentMove];
14901 blackTimeRemaining = timeRemaining[1][currentMove];
14903 DisplayBothClocks();
14904 DisplayMove(currentMove - 1);
14905 DrawPosition(full_redraw, boards[currentMove]);
14906 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
14907 // [HGM] PV info: routine tests if comment empty
14908 DisplayComment(currentMove - 1, commentList[currentMove]);
14909 ClearMap(); // [HGM] exclude: invalidate map
14915 if (gameMode == IcsExamining && !pausing) {
14916 SendToICS(ics_prefix);
14917 SendToICS("backward\n");
14919 BackwardInner(currentMove - 1);
14926 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
14927 /* to optimize, we temporarily turn off analysis mode while we undo
14928 * all the moves. Otherwise we get analysis output after each undo.
14930 if (first.analysisSupport) {
14931 SendToProgram("exit\nforce\n", &first);
14932 first.analyzing = FALSE;
14936 if (gameMode == IcsExamining && !pausing) {
14937 SendToICS(ics_prefix);
14938 SendToICS("backward 999999\n");
14940 BackwardInner(backwardMostMove);
14943 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
14944 /* we have fed all the moves, so reactivate analysis mode */
14945 SendToProgram("analyze\n", &first);
14946 first.analyzing = TRUE;
14947 /*first.maybeThinking = TRUE;*/
14948 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
14955 if (gameMode == PlayFromGameFile && !pausing) PauseEvent();
14956 if (to >= forwardMostMove) to = forwardMostMove;
14957 if (to <= backwardMostMove) to = backwardMostMove;
14958 if (to < currentMove) {
14966 RevertEvent (Boolean annotate)
14968 if(PopTail(annotate)) { // [HGM] vari: restore old game tail
14971 if (gameMode != IcsExamining) {
14972 DisplayError(_("You are not examining a game"), 0);
14976 DisplayError(_("You can't revert while pausing"), 0);
14979 SendToICS(ics_prefix);
14980 SendToICS("revert\n");
14984 RetractMoveEvent ()
14986 switch (gameMode) {
14987 case MachinePlaysWhite:
14988 case MachinePlaysBlack:
14989 if (WhiteOnMove(forwardMostMove) == (gameMode == MachinePlaysWhite)) {
14990 DisplayError(_("Wait until your turn,\nor select Move Now"), 0);
14993 if (forwardMostMove < 2) return;
14994 currentMove = forwardMostMove = forwardMostMove - 2;
14995 whiteTimeRemaining = timeRemaining[0][currentMove];
14996 blackTimeRemaining = timeRemaining[1][currentMove];
14997 DisplayBothClocks();
14998 DisplayMove(currentMove - 1);
14999 ClearHighlights();/*!! could figure this out*/
15000 DrawPosition(TRUE, boards[currentMove]); /* [AS] Changed to full redraw! */
15001 SendToProgram("remove\n", &first);
15002 /*first.maybeThinking = TRUE;*/ /* GNU Chess does not ponder here */
15005 case BeginningOfGame:
15009 case IcsPlayingWhite:
15010 case IcsPlayingBlack:
15011 if (WhiteOnMove(forwardMostMove) == (gameMode == IcsPlayingWhite)) {
15012 SendToICS(ics_prefix);
15013 SendToICS("takeback 2\n");
15015 SendToICS(ics_prefix);
15016 SendToICS("takeback 1\n");
15025 ChessProgramState *cps;
15027 switch (gameMode) {
15028 case MachinePlaysWhite:
15029 if (!WhiteOnMove(forwardMostMove)) {
15030 DisplayError(_("It is your turn"), 0);
15035 case MachinePlaysBlack:
15036 if (WhiteOnMove(forwardMostMove)) {
15037 DisplayError(_("It is your turn"), 0);
15042 case TwoMachinesPlay:
15043 if (WhiteOnMove(forwardMostMove) ==
15044 (first.twoMachinesColor[0] == 'w')) {
15050 case BeginningOfGame:
15054 SendToProgram("?\n", cps);
15058 TruncateGameEvent ()
15061 if (gameMode != EditGame) return;
15068 CleanupTail(); // [HGM] vari: only keep current variation if we explicitly truncate
15069 if (forwardMostMove > currentMove) {
15070 if (gameInfo.resultDetails != NULL) {
15071 free(gameInfo.resultDetails);
15072 gameInfo.resultDetails = NULL;
15073 gameInfo.result = GameUnfinished;
15075 forwardMostMove = currentMove;
15076 HistorySet(parseList, backwardMostMove, forwardMostMove,
15084 if (appData.noChessProgram) return;
15085 switch (gameMode) {
15086 case MachinePlaysWhite:
15087 if (WhiteOnMove(forwardMostMove)) {
15088 DisplayError(_("Wait until your turn"), 0);
15092 case BeginningOfGame:
15093 case MachinePlaysBlack:
15094 if (!WhiteOnMove(forwardMostMove)) {
15095 DisplayError(_("Wait until your turn"), 0);
15100 DisplayError(_("No hint available"), 0);
15103 SendToProgram("hint\n", &first);
15104 hintRequested = TRUE;
15110 ListGame * lg = (ListGame *) gameList.head;
15113 static int secondTime = FALSE;
15115 if( !(f = GameFile()) || ((ListGame *) gameList.tailPred)->number <= 0 ) {
15116 DisplayError(_("Game list not loaded or empty"), 0);
15120 if(!secondTime && (f = fopen(appData.polyglotBook, "r"))) {
15123 DisplayNote(_("Book file exists! Try again for overwrite."));
15127 creatingBook = TRUE;
15128 secondTime = FALSE;
15130 /* Get list size */
15131 for (nItem = 1; nItem <= ((ListGame *) gameList.tailPred)->number; nItem++){
15132 LoadGame(f, nItem, "", TRUE);
15133 AddGameToBook(TRUE);
15134 lg = (ListGame *) lg->node.succ;
15137 creatingBook = FALSE;
15144 if (appData.noChessProgram) return;
15145 switch (gameMode) {
15146 case MachinePlaysWhite:
15147 if (WhiteOnMove(forwardMostMove)) {
15148 DisplayError(_("Wait until your turn"), 0);
15152 case BeginningOfGame:
15153 case MachinePlaysBlack:
15154 if (!WhiteOnMove(forwardMostMove)) {
15155 DisplayError(_("Wait until your turn"), 0);
15160 EditPositionDone(TRUE);
15162 case TwoMachinesPlay:
15167 SendToProgram("bk\n", &first);
15168 bookOutput[0] = NULLCHAR;
15169 bookRequested = TRUE;
15175 char *tags = PGNTags(&gameInfo);
15176 TagsPopUp(tags, CmailMsg());
15180 /* end button procedures */
15183 PrintPosition (FILE *fp, int move)
15187 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
15188 for (j = BOARD_LEFT; j < BOARD_RGHT; j++) {
15189 char c = PieceToChar(boards[move][i][j]);
15190 fputc(c == 'x' ? '.' : c, fp);
15191 fputc(j == BOARD_RGHT - 1 ? '\n' : ' ', fp);
15194 if ((gameMode == EditPosition) ? !blackPlaysFirst : (move % 2 == 0))
15195 fprintf(fp, "white to play\n");
15197 fprintf(fp, "black to play\n");
15201 PrintOpponents (FILE *fp)
15203 if (gameInfo.white != NULL) {
15204 fprintf(fp, "\t%s vs. %s\n", gameInfo.white, gameInfo.black);
15210 /* Find last component of program's own name, using some heuristics */
15212 TidyProgramName (char *prog, char *host, char buf[MSG_SIZ])
15215 int local = (strcmp(host, "localhost") == 0);
15216 while (!local && (p = strchr(prog, ';')) != NULL) {
15218 while (*p == ' ') p++;
15221 if (*prog == '"' || *prog == '\'') {
15222 q = strchr(prog + 1, *prog);
15224 q = strchr(prog, ' ');
15226 if (q == NULL) q = prog + strlen(prog);
15228 while (p >= prog && *p != '/' && *p != '\\') p--;
15230 if(p == prog && *p == '"') p++;
15232 if (q - p >= 4 && StrCaseCmp(q - 4, ".exe") == 0) *q = c, q -= 4; else *q = c;
15233 memcpy(buf, p, q - p);
15234 buf[q - p] = NULLCHAR;
15242 TimeControlTagValue ()
15245 if (!appData.clockMode) {
15246 safeStrCpy(buf, "-", sizeof(buf)/sizeof(buf[0]));
15247 } else if (movesPerSession > 0) {
15248 snprintf(buf, MSG_SIZ, "%d/%ld", movesPerSession, timeControl/1000);
15249 } else if (timeIncrement == 0) {
15250 snprintf(buf, MSG_SIZ, "%ld", timeControl/1000);
15252 snprintf(buf, MSG_SIZ, "%ld+%ld", timeControl/1000, timeIncrement/1000);
15254 return StrSave(buf);
15260 /* This routine is used only for certain modes */
15261 VariantClass v = gameInfo.variant;
15262 ChessMove r = GameUnfinished;
15265 if(keepInfo) return;
15267 if(gameMode == EditGame) { // [HGM] vari: do not erase result on EditGame
15268 r = gameInfo.result;
15269 p = gameInfo.resultDetails;
15270 gameInfo.resultDetails = NULL;
15272 ClearGameInfo(&gameInfo);
15273 gameInfo.variant = v;
15275 switch (gameMode) {
15276 case MachinePlaysWhite:
15277 gameInfo.event = StrSave( appData.pgnEventHeader );
15278 gameInfo.site = StrSave(HostName());
15279 gameInfo.date = PGNDate();
15280 gameInfo.round = StrSave("-");
15281 gameInfo.white = StrSave(first.tidy);
15282 gameInfo.black = StrSave(UserName());
15283 gameInfo.timeControl = TimeControlTagValue();
15286 case MachinePlaysBlack:
15287 gameInfo.event = StrSave( appData.pgnEventHeader );
15288 gameInfo.site = StrSave(HostName());
15289 gameInfo.date = PGNDate();
15290 gameInfo.round = StrSave("-");
15291 gameInfo.white = StrSave(UserName());
15292 gameInfo.black = StrSave(first.tidy);
15293 gameInfo.timeControl = TimeControlTagValue();
15296 case TwoMachinesPlay:
15297 gameInfo.event = StrSave( appData.pgnEventHeader );
15298 gameInfo.site = StrSave(HostName());
15299 gameInfo.date = PGNDate();
15302 snprintf(buf, MSG_SIZ, "%d", roundNr);
15303 gameInfo.round = StrSave(buf);
15305 gameInfo.round = StrSave("-");
15307 if (first.twoMachinesColor[0] == 'w') {
15308 gameInfo.white = StrSave(appData.pgnName[0][0] ? appData.pgnName[0] : first.tidy);
15309 gameInfo.black = StrSave(appData.pgnName[1][0] ? appData.pgnName[1] : second.tidy);
15311 gameInfo.white = StrSave(appData.pgnName[1][0] ? appData.pgnName[1] : second.tidy);
15312 gameInfo.black = StrSave(appData.pgnName[0][0] ? appData.pgnName[0] : first.tidy);
15314 gameInfo.timeControl = TimeControlTagValue();
15318 gameInfo.event = StrSave("Edited game");
15319 gameInfo.site = StrSave(HostName());
15320 gameInfo.date = PGNDate();
15321 gameInfo.round = StrSave("-");
15322 gameInfo.white = StrSave("-");
15323 gameInfo.black = StrSave("-");
15324 gameInfo.result = r;
15325 gameInfo.resultDetails = p;
15329 gameInfo.event = StrSave("Edited position");
15330 gameInfo.site = StrSave(HostName());
15331 gameInfo.date = PGNDate();
15332 gameInfo.round = StrSave("-");
15333 gameInfo.white = StrSave("-");
15334 gameInfo.black = StrSave("-");
15337 case IcsPlayingWhite:
15338 case IcsPlayingBlack:
15343 case PlayFromGameFile:
15344 gameInfo.event = StrSave("Game from non-PGN file");
15345 gameInfo.site = StrSave(HostName());
15346 gameInfo.date = PGNDate();
15347 gameInfo.round = StrSave("-");
15348 gameInfo.white = StrSave("?");
15349 gameInfo.black = StrSave("?");
15358 ReplaceComment (int index, char *text)
15364 if(index && sscanf(text, "%f/%d", &score, &len) == 2 &&
15365 pvInfoList[index-1].depth == len &&
15366 fabs(pvInfoList[index-1].score - score*100.) < 0.5 &&
15367 (p = strchr(text, '\n'))) text = p; // [HGM] strip off first line with PV info, if any
15368 while (*text == '\n') text++;
15369 len = strlen(text);
15370 while (len > 0 && text[len - 1] == '\n') len--;
15372 if (commentList[index] != NULL)
15373 free(commentList[index]);
15376 commentList[index] = NULL;
15379 if( *text == '{' && strchr(text, '}') || // [HGM] braces: if certainy malformed, put braces
15380 *text == '[' && strchr(text, ']') || // otherwise hope the user knows what he is doing
15381 *text == '(' && strchr(text, ')')) { // (perhaps check if this parses as comment-only?)
15382 commentList[index] = (char *) malloc(len + 2);
15383 strncpy(commentList[index], text, len);
15384 commentList[index][len] = '\n';
15385 commentList[index][len + 1] = NULLCHAR;
15387 // [HGM] braces: if text does not start with known OK delimiter, put braces around it.
15389 commentList[index] = (char *) malloc(len + 7);
15390 safeStrCpy(commentList[index], "{\n", 3);
15391 safeStrCpy(commentList[index]+2, text, len+1);
15392 commentList[index][len+2] = NULLCHAR;
15393 while(p = strchr(commentList[index], '}')) *p = ')'; // kill all } to make it one comment
15394 strcat(commentList[index], "\n}\n");
15399 CrushCRs (char *text)
15407 if (ch == '\r') continue;
15409 } while (ch != '\0');
15413 AppendComment (int index, char *text, Boolean addBraces)
15414 /* addBraces tells if we should add {} */
15419 if(appData.debugMode) fprintf(debugFP, "Append: in='%s' %d\n", text, addBraces); fflush(debugFP);
15420 text = GetInfoFromComment( index, text ); /* [HGM] PV time: strip PV info from comment */
15423 while (*text == '\n') text++;
15424 len = strlen(text);
15425 while (len > 0 && text[len - 1] == '\n') len--;
15426 text[len] = NULLCHAR;
15428 if (len == 0) return;
15430 if (commentList[index] != NULL) {
15431 Boolean addClosingBrace = addBraces;
15432 old = commentList[index];
15433 oldlen = strlen(old);
15434 while(commentList[index][oldlen-1] == '\n')
15435 commentList[index][--oldlen] = NULLCHAR;
15436 commentList[index] = (char *) malloc(oldlen + len + 6); // might waste 4
15437 safeStrCpy(commentList[index], old, oldlen + len + 6);
15439 // [HGM] braces: join "{A\n}\n" + "{\nB}" as "{A\nB\n}"
15440 if(commentList[index][oldlen-1] == '}' && (text[0] == '{' || addBraces == TRUE)) {
15441 if(addBraces == TRUE) addBraces = FALSE; else { text++; len--; }
15442 while (*text == '\n') { text++; len--; }
15443 commentList[index][--oldlen] = NULLCHAR;
15445 if(addBraces) strcat(commentList[index], addBraces == 2 ? "\n(" : "\n{\n");
15446 else strcat(commentList[index], "\n");
15447 strcat(commentList[index], text);
15448 if(addClosingBrace) strcat(commentList[index], addClosingBrace == 2 ? ")\n" : "\n}\n");
15449 else strcat(commentList[index], "\n");
15451 commentList[index] = (char *) malloc(len + 6); // perhaps wastes 4...
15453 safeStrCpy(commentList[index], addBraces == 2 ? "(" : "{\n", 3);
15454 else commentList[index][0] = NULLCHAR;
15455 strcat(commentList[index], text);
15456 strcat(commentList[index], addBraces == 2 ? ")\n" : "\n");
15457 if(addBraces == TRUE) strcat(commentList[index], "}\n");
15462 FindStr (char * text, char * sub_text)
15464 char * result = strstr( text, sub_text );
15466 if( result != NULL ) {
15467 result += strlen( sub_text );
15473 /* [AS] Try to extract PV info from PGN comment */
15474 /* [HGM] PV time: and then remove it, to prevent it appearing twice */
15476 GetInfoFromComment (int index, char * text)
15478 char * sep = text, *p;
15480 if( text != NULL && index > 0 ) {
15483 int time = -1, sec = 0, deci;
15484 char * s_eval = FindStr( text, "[%eval " );
15485 char * s_emt = FindStr( text, "[%emt " );
15487 if( s_eval != NULL || s_emt != NULL ) {
15491 if( s_eval != NULL ) {
15492 if( sscanf( s_eval, "%d,%d%c", &score, &depth, &delim ) != 3 ) {
15496 if( delim != ']' ) {
15501 if( s_emt != NULL ) {
15506 /* We expect something like: [+|-]nnn.nn/dd */
15509 if(*text != '{') return text; // [HGM] braces: must be normal comment
15511 sep = strchr( text, '/' );
15512 if( sep == NULL || sep < (text+4) ) {
15517 if(p[1] == '(') { // comment starts with PV
15518 p = strchr(p, ')'); // locate end of PV
15519 if(p == NULL || sep < p+5) return text;
15520 // at this point we have something like "{(.*) +0.23/6 ..."
15521 p = text; while(*++p != ')') p[-1] = *p; p[-1] = ')';
15522 *p = '\n'; while(*p == ' ' || *p == '\n') p++; *--p = '{';
15523 // we now moved the brace to behind the PV: "(.*) {+0.23/6 ..."
15525 time = -1; sec = -1; deci = -1;
15526 if( sscanf( p+1, "%d.%d/%d %d:%d", &score, &score_lo, &depth, &time, &sec ) != 5 &&
15527 sscanf( p+1, "%d.%d/%d %d.%d", &score, &score_lo, &depth, &time, &deci ) != 5 &&
15528 sscanf( p+1, "%d.%d/%d %d", &score, &score_lo, &depth, &time ) != 4 &&
15529 sscanf( p+1, "%d.%d/%d", &score, &score_lo, &depth ) != 3 ) {
15533 if( score_lo < 0 || score_lo >= 100 ) {
15537 if(sec >= 0) time = 600*time + 10*sec; else
15538 if(deci >= 0) time = 10*time + deci; else time *= 10; // deci-sec
15540 score = score >= 0 ? score*100 + score_lo : score*100 - score_lo;
15542 /* [HGM] PV time: now locate end of PV info */
15543 while( *++sep >= '0' && *sep <= '9'); // strip depth
15545 while( *++sep >= '0' && *sep <= '9' || *sep == '\n'); // strip time
15547 while( *++sep >= '0' && *sep <= '9'); // strip seconds
15549 while( *++sep >= '0' && *sep <= '9'); // strip fractional seconds
15550 while(*sep == ' ' || *sep == '\n' || *sep == '\r') sep++;
15561 pvInfoList[index-1].depth = depth;
15562 pvInfoList[index-1].score = score;
15563 pvInfoList[index-1].time = 10*time; // centi-sec
15564 if(*sep == '}') *sep = 0; else *--sep = '{';
15565 if(p != text) { while(*p++ = *sep++); sep = text; } // squeeze out space between PV and comment, and return both
15571 SendToProgram (char *message, ChessProgramState *cps)
15573 int count, outCount, error;
15576 if (cps->pr == NoProc) return;
15579 if (appData.debugMode) {
15582 fprintf(debugFP, "%ld >%-6s: %s",
15583 SubtractTimeMarks(&now, &programStartTime),
15584 cps->which, message);
15586 fprintf(serverFP, "%ld >%-6s: %s",
15587 SubtractTimeMarks(&now, &programStartTime),
15588 cps->which, message), fflush(serverFP);
15591 count = strlen(message);
15592 outCount = OutputToProcess(cps->pr, message, count, &error);
15593 if (outCount < count && !exiting
15594 && !endingGame) { /* [HGM] crash: to not hang GameEnds() writing to deceased engines */
15595 if(!cps->initDone) return; // [HGM] should not generate fatal error during engine load
15596 snprintf(buf, MSG_SIZ, _("Error writing to %s chess program"), _(cps->which));
15597 if(gameInfo.resultDetails==NULL) { /* [HGM] crash: if game in progress, give reason for abort */
15598 if((signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
15599 snprintf(buf, MSG_SIZ, _("%s program exits in draw position (%s)"), _(cps->which), cps->program);
15600 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(GameIsDrawn, buf, GE_XBOARD); return; }
15601 gameInfo.result = GameIsDrawn; /* [HGM] accept exit as draw claim */
15603 ChessMove res = cps->twoMachinesColor[0]=='w' ? BlackWins : WhiteWins;
15604 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(res, buf, GE_XBOARD); return; }
15605 gameInfo.result = res;
15607 gameInfo.resultDetails = StrSave(buf);
15609 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; return; }
15610 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, error, 1); else errorExitStatus = 1;
15615 ReceiveFromProgram (InputSourceRef isr, VOIDSTAR closure, char *message, int count, int error)
15619 ChessProgramState *cps = (ChessProgramState *)closure;
15621 if (isr != cps->isr) return; /* Killed intentionally */
15624 RemoveInputSource(cps->isr);
15625 snprintf(buf, MSG_SIZ, _("Error: %s chess program (%s) exited unexpectedly"),
15626 _(cps->which), cps->program);
15627 if(LoadError(cps->userError ? NULL : buf, cps)) return; // [HGM] should not generate fatal error during engine load
15628 if(gameInfo.resultDetails==NULL) { /* [HGM] crash: if game in progress, give reason for abort */
15629 if((signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
15630 snprintf(buf, MSG_SIZ, _("%s program exits in draw position (%s)"), _(cps->which), cps->program);
15631 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(GameIsDrawn, buf, GE_XBOARD); return; }
15632 gameInfo.result = GameIsDrawn; /* [HGM] accept exit as draw claim */
15634 ChessMove res = cps->twoMachinesColor[0]=='w' ? BlackWins : WhiteWins;
15635 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; GameEnds(res, buf, GE_XBOARD); return; }
15636 gameInfo.result = res;
15638 gameInfo.resultDetails = StrSave(buf);
15640 if(matchMode && appData.tourneyFile[0]) { cps->pr = NoProc; return; }
15641 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, 0, 1); else errorExitStatus = 1;
15643 snprintf(buf, MSG_SIZ, _("Error reading from %s chess program (%s)"),
15644 _(cps->which), cps->program);
15645 RemoveInputSource(cps->isr);
15647 /* [AS] Program is misbehaving badly... kill it */
15648 if( count == -2 ) {
15649 DestroyChildProcess( cps->pr, 9 );
15653 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, error, 1); else errorExitStatus = 1;
15658 if ((end_str = strchr(message, '\r')) != NULL)
15659 *end_str = NULLCHAR;
15660 if ((end_str = strchr(message, '\n')) != NULL)
15661 *end_str = NULLCHAR;
15663 if (appData.debugMode) {
15664 TimeMark now; int print = 1;
15665 char *quote = ""; char c; int i;
15667 if(appData.engineComments != 1) { /* [HGM] debug: decide if protocol-violating output is written */
15668 char start = message[0];
15669 if(start >='A' && start <= 'Z') start += 'a' - 'A'; // be tolerant to capitalizing
15670 if(sscanf(message, "%d%c%d%d%d", &i, &c, &i, &i, &i) != 5 &&
15671 sscanf(message, "move %c", &c)!=1 && sscanf(message, "offer%c", &c)!=1 &&
15672 sscanf(message, "resign%c", &c)!=1 && sscanf(message, "feature %c", &c)!=1 &&
15673 sscanf(message, "error %c", &c)!=1 && sscanf(message, "illegal %c", &c)!=1 &&
15674 sscanf(message, "tell%c", &c)!=1 && sscanf(message, "0-1 %c", &c)!=1 &&
15675 sscanf(message, "1-0 %c", &c)!=1 && sscanf(message, "1/2-1/2 %c", &c)!=1 &&
15676 sscanf(message, "setboard %c", &c)!=1 && sscanf(message, "setup %c", &c)!=1 &&
15677 sscanf(message, "hint: %c", &c)!=1 &&
15678 sscanf(message, "pong %c", &c)!=1 && start != '#') {
15679 quote = appData.engineComments == 2 ? "# " : "### NON-COMPLIANT! ### ";
15680 print = (appData.engineComments >= 2);
15682 message[0] = start; // restore original message
15686 fprintf(debugFP, "%ld <%-6s: %s%s\n",
15687 SubtractTimeMarks(&now, &programStartTime), cps->which,
15691 fprintf(serverFP, "%ld <%-6s: %s%s\n",
15692 SubtractTimeMarks(&now, &programStartTime), cps->which,
15694 message), fflush(serverFP);
15698 /* [DM] if icsEngineAnalyze is active we block all whisper and kibitz output, because nobody want to see this */
15699 if (appData.icsEngineAnalyze) {
15700 if (strstr(message, "whisper") != NULL ||
15701 strstr(message, "kibitz") != NULL ||
15702 strstr(message, "tellics") != NULL) return;
15705 HandleMachineMove(message, cps);
15710 SendTimeControl (ChessProgramState *cps, int mps, long tc, int inc, int sd, int st)
15715 if( timeControl_2 > 0 ) {
15716 if( (gameMode == MachinePlaysBlack) || (gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b') ) {
15717 tc = timeControl_2;
15720 tc /= cps->timeOdds; /* [HGM] time odds: apply before telling engine */
15721 inc /= cps->timeOdds;
15722 st /= cps->timeOdds;
15724 seconds = (tc / 1000) % 60; /* [HGM] displaced to after applying odds */
15727 /* Set exact time per move, normally using st command */
15728 if (cps->stKludge) {
15729 /* GNU Chess 4 has no st command; uses level in a nonstandard way */
15731 if (seconds == 0) {
15732 snprintf(buf, MSG_SIZ, "level 1 %d\n", st/60);
15734 snprintf(buf, MSG_SIZ, "level 1 %d:%02d\n", st/60, seconds);
15737 snprintf(buf, MSG_SIZ, "st %d\n", st);
15740 /* Set conventional or incremental time control, using level command */
15741 if (seconds == 0) {
15742 /* Note old gnuchess bug -- minutes:seconds used to not work.
15743 Fixed in later versions, but still avoid :seconds
15744 when seconds is 0. */
15745 snprintf(buf, MSG_SIZ, "level %d %ld %g\n", mps, tc/60000, inc/1000.);
15747 snprintf(buf, MSG_SIZ, "level %d %ld:%02d %g\n", mps, tc/60000,
15748 seconds, inc/1000.);
15751 SendToProgram(buf, cps);
15753 /* Orthoganally (except for GNU Chess 4), limit time to st seconds */
15754 /* Orthogonally, limit search to given depth */
15756 if (cps->sdKludge) {
15757 snprintf(buf, MSG_SIZ, "depth\n%d\n", sd);
15759 snprintf(buf, MSG_SIZ, "sd %d\n", sd);
15761 SendToProgram(buf, cps);
15764 if(cps->nps >= 0) { /* [HGM] nps */
15765 if(cps->supportsNPS == FALSE)
15766 cps->nps = -1; // don't use if engine explicitly says not supported!
15768 snprintf(buf, MSG_SIZ, "nps %d\n", cps->nps);
15769 SendToProgram(buf, cps);
15774 ChessProgramState *
15776 /* [HGM] return pointer to 'first' or 'second', depending on who plays white */
15778 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b' ||
15779 gameMode == BeginningOfGame || gameMode == MachinePlaysBlack)
15785 SendTimeRemaining (ChessProgramState *cps, int machineWhite)
15787 char message[MSG_SIZ];
15790 /* Note: this routine must be called when the clocks are stopped
15791 or when they have *just* been set or switched; otherwise
15792 it will be off by the time since the current tick started.
15794 if (machineWhite) {
15795 time = whiteTimeRemaining / 10;
15796 otime = blackTimeRemaining / 10;
15798 time = blackTimeRemaining / 10;
15799 otime = whiteTimeRemaining / 10;
15801 /* [HGM] translate opponent's time by time-odds factor */
15802 otime = (otime * cps->other->timeOdds) / cps->timeOdds;
15804 if (time <= 0) time = 1;
15805 if (otime <= 0) otime = 1;
15807 snprintf(message, MSG_SIZ, "time %ld\n", time);
15808 SendToProgram(message, cps);
15810 snprintf(message, MSG_SIZ, "otim %ld\n", otime);
15811 SendToProgram(message, cps);
15815 BoolFeature (char **p, char *name, int *loc, ChessProgramState *cps)
15818 int len = strlen(name);
15821 if (strncmp((*p), name, len) == 0 && (*p)[len] == '=') {
15823 sscanf(*p, "%d", &val);
15825 while (**p && **p != ' ')
15827 snprintf(buf, MSG_SIZ, "accepted %s\n", name);
15828 SendToProgram(buf, cps);
15835 IntFeature (char **p, char *name, int *loc, ChessProgramState *cps)
15838 int len = strlen(name);
15839 if (strncmp((*p), name, len) == 0 && (*p)[len] == '=') {
15841 sscanf(*p, "%d", loc);
15842 while (**p && **p != ' ') (*p)++;
15843 snprintf(buf, MSG_SIZ, "accepted %s\n", name);
15844 SendToProgram(buf, cps);
15851 StringFeature (char **p, char *name, char loc[], ChessProgramState *cps)
15854 int len = strlen(name);
15855 if (strncmp((*p), name, len) == 0
15856 && (*p)[len] == '=' && (*p)[len+1] == '\"') {
15858 sscanf(*p, "%[^\"]", loc);
15859 while (**p && **p != '\"') (*p)++;
15860 if (**p == '\"') (*p)++;
15861 snprintf(buf, MSG_SIZ, "accepted %s\n", name);
15862 SendToProgram(buf, cps);
15869 ParseOption (Option *opt, ChessProgramState *cps)
15870 // [HGM] options: process the string that defines an engine option, and determine
15871 // name, type, default value, and allowed value range
15873 char *p, *q, buf[MSG_SIZ];
15874 int n, min = (-1)<<31, max = 1<<31, def;
15876 if(p = strstr(opt->name, " -spin ")) {
15877 if((n = sscanf(p, " -spin %d %d %d", &def, &min, &max)) < 3 ) return FALSE;
15878 if(max < min) max = min; // enforce consistency
15879 if(def < min) def = min;
15880 if(def > max) def = max;
15885 } else if((p = strstr(opt->name, " -slider "))) {
15886 // for now -slider is a synonym for -spin, to already provide compatibility with future polyglots
15887 if((n = sscanf(p, " -slider %d %d %d", &def, &min, &max)) < 3 ) return FALSE;
15888 if(max < min) max = min; // enforce consistency
15889 if(def < min) def = min;
15890 if(def > max) def = max;
15894 opt->type = Spin; // Slider;
15895 } else if((p = strstr(opt->name, " -string "))) {
15896 opt->textValue = p+9;
15897 opt->type = TextBox;
15898 } else if((p = strstr(opt->name, " -file "))) {
15899 // for now -file is a synonym for -string, to already provide compatibility with future polyglots
15900 opt->textValue = p+7;
15901 opt->type = FileName; // FileName;
15902 } else if((p = strstr(opt->name, " -path "))) {
15903 // for now -file is a synonym for -string, to already provide compatibility with future polyglots
15904 opt->textValue = p+7;
15905 opt->type = PathName; // PathName;
15906 } else if(p = strstr(opt->name, " -check ")) {
15907 if(sscanf(p, " -check %d", &def) < 1) return FALSE;
15908 opt->value = (def != 0);
15909 opt->type = CheckBox;
15910 } else if(p = strstr(opt->name, " -combo ")) {
15911 opt->textValue = (char*) (opt->choice = &cps->comboList[cps->comboCnt]); // cheat with pointer type
15912 cps->comboList[cps->comboCnt++] = q = p+8; // holds possible choices
15913 if(*q == '*') cps->comboList[cps->comboCnt-1]++;
15914 opt->value = n = 0;
15915 while(q = StrStr(q, " /// ")) {
15916 n++; *q = 0; // count choices, and null-terminate each of them
15918 if(*q == '*') { // remember default, which is marked with * prefix
15922 cps->comboList[cps->comboCnt++] = q;
15924 cps->comboList[cps->comboCnt++] = NULL;
15926 opt->type = ComboBox;
15927 } else if(p = strstr(opt->name, " -button")) {
15928 opt->type = Button;
15929 } else if(p = strstr(opt->name, " -save")) {
15930 opt->type = SaveButton;
15931 } else return FALSE;
15932 *p = 0; // terminate option name
15933 // now look if the command-line options define a setting for this engine option.
15934 if(cps->optionSettings && cps->optionSettings[0])
15935 p = strstr(cps->optionSettings, opt->name); else p = NULL;
15936 if(p && (p == cps->optionSettings || p[-1] == ',')) {
15937 snprintf(buf, MSG_SIZ, "option %s", p);
15938 if(p = strstr(buf, ",")) *p = 0;
15939 if(q = strchr(buf, '=')) switch(opt->type) {
15941 for(n=0; n<opt->max; n++)
15942 if(!strcmp(((char**)opt->textValue)[n], q+1)) opt->value = n;
15945 safeStrCpy(opt->textValue, q+1, MSG_SIZ - (opt->textValue - opt->name));
15949 opt->value = atoi(q+1);
15954 SendToProgram(buf, cps);
15960 FeatureDone (ChessProgramState *cps, int val)
15962 DelayedEventCallback cb = GetDelayedEvent();
15963 if ((cb == InitBackEnd3 && cps == &first) ||
15964 (cb == SettingsMenuIfReady && cps == &second) ||
15965 (cb == LoadEngine) ||
15966 (cb == TwoMachinesEventIfReady)) {
15967 CancelDelayedEvent();
15968 ScheduleDelayedEvent(cb, val ? 1 : 3600000);
15970 cps->initDone = val;
15973 /* Parse feature command from engine */
15975 ParseFeatures (char *args, ChessProgramState *cps)
15983 while (*p == ' ') p++;
15984 if (*p == NULLCHAR) return;
15986 if (BoolFeature(&p, "setboard", &cps->useSetboard, cps)) continue;
15987 if (BoolFeature(&p, "xedit", &cps->extendedEdit, cps)) continue;
15988 if (BoolFeature(&p, "time", &cps->sendTime, cps)) continue;
15989 if (BoolFeature(&p, "draw", &cps->sendDrawOffers, cps)) continue;
15990 if (BoolFeature(&p, "sigint", &cps->useSigint, cps)) continue;
15991 if (BoolFeature(&p, "sigterm", &cps->useSigterm, cps)) continue;
15992 if (BoolFeature(&p, "reuse", &val, cps)) {
15993 /* Engine can disable reuse, but can't enable it if user said no */
15994 if (!val) cps->reuse = FALSE;
15997 if (BoolFeature(&p, "analyze", &cps->analysisSupport, cps)) continue;
15998 if (StringFeature(&p, "myname", cps->tidy, cps)) {
15999 if (gameMode == TwoMachinesPlay) {
16000 DisplayTwoMachinesTitle();
16006 if (StringFeature(&p, "variants", cps->variants, cps)) continue;
16007 if (BoolFeature(&p, "san", &cps->useSAN, cps)) continue;
16008 if (BoolFeature(&p, "ping", &cps->usePing, cps)) continue;
16009 if (BoolFeature(&p, "playother", &cps->usePlayother, cps)) continue;
16010 if (BoolFeature(&p, "colors", &cps->useColors, cps)) continue;
16011 if (BoolFeature(&p, "usermove", &cps->useUsermove, cps)) continue;
16012 if (BoolFeature(&p, "exclude", &cps->excludeMoves, cps)) continue;
16013 if (BoolFeature(&p, "ics", &cps->sendICS, cps)) continue;
16014 if (BoolFeature(&p, "name", &cps->sendName, cps)) continue;
16015 if (BoolFeature(&p, "pause", &cps->pause, cps)) continue; // [HGM] pause
16016 if (IntFeature(&p, "done", &val, cps)) {
16017 FeatureDone(cps, val);
16020 /* Added by Tord: */
16021 if (BoolFeature(&p, "fen960", &cps->useFEN960, cps)) continue;
16022 if (BoolFeature(&p, "oocastle", &cps->useOOCastle, cps)) continue;
16023 /* End of additions by Tord */
16025 /* [HGM] added features: */
16026 if (BoolFeature(&p, "debug", &cps->debug, cps)) continue;
16027 if (BoolFeature(&p, "nps", &cps->supportsNPS, cps)) continue;
16028 if (IntFeature(&p, "level", &cps->maxNrOfSessions, cps)) continue;
16029 if (BoolFeature(&p, "memory", &cps->memSize, cps)) continue;
16030 if (BoolFeature(&p, "smp", &cps->maxCores, cps)) continue;
16031 if (StringFeature(&p, "egt", cps->egtFormats, cps)) continue;
16032 if (StringFeature(&p, "option", buf, cps)) {
16033 FREE(cps->option[cps->nrOptions].name);
16034 cps->option[cps->nrOptions].name = malloc(MSG_SIZ);
16035 safeStrCpy(cps->option[cps->nrOptions].name, buf, MSG_SIZ);
16036 if(!ParseOption(&(cps->option[cps->nrOptions++]), cps)) { // [HGM] options: add option feature
16037 snprintf(buf, MSG_SIZ, "rejected option %s\n", cps->option[--cps->nrOptions].name);
16038 SendToProgram(buf, cps);
16041 if(cps->nrOptions >= MAX_OPTIONS) {
16043 snprintf(buf, MSG_SIZ, _("%s engine has too many options\n"), _(cps->which));
16044 DisplayError(buf, 0);
16048 /* End of additions by HGM */
16050 /* unknown feature: complain and skip */
16052 while (*q && *q != '=') q++;
16053 snprintf(buf, MSG_SIZ,"rejected %.*s\n", (int)(q-p), p);
16054 SendToProgram(buf, cps);
16060 while (*p && *p != '\"') p++;
16061 if (*p == '\"') p++;
16063 while (*p && *p != ' ') p++;
16071 PeriodicUpdatesEvent (int newState)
16073 if (newState == appData.periodicUpdates)
16076 appData.periodicUpdates=newState;
16078 /* Display type changes, so update it now */
16079 // DisplayAnalysis();
16081 /* Get the ball rolling again... */
16083 AnalysisPeriodicEvent(1);
16084 StartAnalysisClock();
16089 PonderNextMoveEvent (int newState)
16091 if (newState == appData.ponderNextMove) return;
16092 if (gameMode == EditPosition) EditPositionDone(TRUE);
16094 SendToProgram("hard\n", &first);
16095 if (gameMode == TwoMachinesPlay) {
16096 SendToProgram("hard\n", &second);
16099 SendToProgram("easy\n", &first);
16100 thinkOutput[0] = NULLCHAR;
16101 if (gameMode == TwoMachinesPlay) {
16102 SendToProgram("easy\n", &second);
16105 appData.ponderNextMove = newState;
16109 NewSettingEvent (int option, int *feature, char *command, int value)
16113 if (gameMode == EditPosition) EditPositionDone(TRUE);
16114 snprintf(buf, MSG_SIZ,"%s%s %d\n", (option ? "option ": ""), command, value);
16115 if(feature == NULL || *feature) SendToProgram(buf, &first);
16116 if (gameMode == TwoMachinesPlay) {
16117 if(feature == NULL || feature[(int*)&second - (int*)&first]) SendToProgram(buf, &second);
16122 ShowThinkingEvent ()
16123 // [HGM] thinking: this routine is now also called from "Options -> Engine..." popup
16125 static int oldState = 2; // kludge alert! Neither true nor fals, so first time oldState is always updated
16126 int newState = appData.showThinking
16127 // [HGM] thinking: other features now need thinking output as well
16128 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp();
16130 if (oldState == newState) return;
16131 oldState = newState;
16132 if (gameMode == EditPosition) EditPositionDone(TRUE);
16134 SendToProgram("post\n", &first);
16135 if (gameMode == TwoMachinesPlay) {
16136 SendToProgram("post\n", &second);
16139 SendToProgram("nopost\n", &first);
16140 thinkOutput[0] = NULLCHAR;
16141 if (gameMode == TwoMachinesPlay) {
16142 SendToProgram("nopost\n", &second);
16145 // appData.showThinking = newState; // [HGM] thinking: responsible option should already have be changed when calling this routine!
16149 AskQuestionEvent (char *title, char *question, char *replyPrefix, char *which)
16151 ProcRef pr = (which[0] == '1') ? first.pr : second.pr;
16152 if (pr == NoProc) return;
16153 AskQuestion(title, question, replyPrefix, pr);
16157 TypeInEvent (char firstChar)
16159 if ((gameMode == BeginningOfGame && !appData.icsActive) ||
16160 gameMode == MachinePlaysWhite || gameMode == MachinePlaysBlack ||
16161 gameMode == AnalyzeMode || gameMode == EditGame ||
16162 gameMode == EditPosition || gameMode == IcsExamining ||
16163 gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
16164 isdigit(firstChar) && // [HGM] movenum: allow typing in of move nr in 'passive' modes
16165 ( gameMode == AnalyzeFile || gameMode == PlayFromGameFile ||
16166 gameMode == IcsObserving || gameMode == TwoMachinesPlay ) ||
16167 gameMode == Training) PopUpMoveDialog(firstChar);
16171 TypeInDoneEvent (char *move)
16174 int n, fromX, fromY, toX, toY;
16176 ChessMove moveType;
16179 if(gameMode == EditPosition && ParseFEN(board, &n, move) ) {
16180 EditPositionPasteFEN(move);
16183 // [HGM] movenum: allow move number to be typed in any mode
16184 if(sscanf(move, "%d", &n) == 1 && n != 0 ) {
16188 // undocumented kludge: allow command-line option to be typed in!
16189 // (potentially fatal, and does not implement the effect of the option.)
16190 // should only be used for options that are values on which future decisions will be made,
16191 // and definitely not on options that would be used during initialization.
16192 if(strstr(move, "!!! -") == move) {
16193 ParseArgsFromString(move+4);
16197 if (gameMode != EditGame && currentMove != forwardMostMove &&
16198 gameMode != Training) {
16199 DisplayMoveError(_("Displayed move is not current"));
16201 int ok = ParseOneMove(move, gameMode == EditPosition ? blackPlaysFirst : currentMove,
16202 &moveType, &fromX, &fromY, &toX, &toY, &promoChar);
16203 if(!ok && move[0] >= 'a') { move[0] += 'A' - 'a'; ok = 2; } // [HGM] try also capitalized
16204 if (ok==1 || ok && ParseOneMove(move, gameMode == EditPosition ? blackPlaysFirst : currentMove,
16205 &moveType, &fromX, &fromY, &toX, &toY, &promoChar)) {
16206 UserMoveEvent(fromX, fromY, toX, toY, promoChar);
16208 DisplayMoveError(_("Could not parse move"));
16214 DisplayMove (int moveNumber)
16216 char message[MSG_SIZ];
16218 char cpThinkOutput[MSG_SIZ];
16220 if(appData.noGUI) return; // [HGM] fast: suppress display of moves
16222 if (moveNumber == forwardMostMove - 1 ||
16223 gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
16225 safeStrCpy(cpThinkOutput, thinkOutput, sizeof(cpThinkOutput)/sizeof(cpThinkOutput[0]));
16227 if (strchr(cpThinkOutput, '\n')) {
16228 *strchr(cpThinkOutput, '\n') = NULLCHAR;
16231 *cpThinkOutput = NULLCHAR;
16234 /* [AS] Hide thinking from human user */
16235 if( appData.hideThinkingFromHuman && gameMode != TwoMachinesPlay ) {
16236 *cpThinkOutput = NULLCHAR;
16237 if( thinkOutput[0] != NULLCHAR ) {
16240 for( i=0; i<=hiddenThinkOutputState; i++ ) {
16241 cpThinkOutput[i] = '.';
16243 cpThinkOutput[i] = NULLCHAR;
16244 hiddenThinkOutputState = (hiddenThinkOutputState + 1) % 3;
16248 if (moveNumber == forwardMostMove - 1 &&
16249 gameInfo.resultDetails != NULL) {
16250 if (gameInfo.resultDetails[0] == NULLCHAR) {
16251 snprintf(res, MSG_SIZ, " %s", PGNResult(gameInfo.result));
16253 snprintf(res, MSG_SIZ, " {%s} %s",
16254 T_(gameInfo.resultDetails), PGNResult(gameInfo.result));
16260 if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
16261 DisplayMessage(res, cpThinkOutput);
16263 snprintf(message, MSG_SIZ, "%d.%s%s%s", moveNumber / 2 + 1,
16264 WhiteOnMove(moveNumber) ? " " : ".. ",
16265 parseList[moveNumber], res);
16266 DisplayMessage(message, cpThinkOutput);
16271 DisplayComment (int moveNumber, char *text)
16273 char title[MSG_SIZ];
16275 if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
16276 safeStrCpy(title, "Comment", sizeof(title)/sizeof(title[0]));
16278 snprintf(title,MSG_SIZ, "Comment on %d.%s%s", moveNumber / 2 + 1,
16279 WhiteOnMove(moveNumber) ? " " : ".. ",
16280 parseList[moveNumber]);
16282 if (text != NULL && (appData.autoDisplayComment || commentUp))
16283 CommentPopUp(title, text);
16286 /* This routine sends a ^C interrupt to gnuchess, to awaken it if it
16287 * might be busy thinking or pondering. It can be omitted if your
16288 * gnuchess is configured to stop thinking immediately on any user
16289 * input. However, that gnuchess feature depends on the FIONREAD
16290 * ioctl, which does not work properly on some flavors of Unix.
16293 Attention (ChessProgramState *cps)
16296 if (!cps->useSigint) return;
16297 if (appData.noChessProgram || (cps->pr == NoProc)) return;
16298 switch (gameMode) {
16299 case MachinePlaysWhite:
16300 case MachinePlaysBlack:
16301 case TwoMachinesPlay:
16302 case IcsPlayingWhite:
16303 case IcsPlayingBlack:
16306 /* Skip if we know it isn't thinking */
16307 if (!cps->maybeThinking) return;
16308 if (appData.debugMode)
16309 fprintf(debugFP, "Interrupting %s\n", cps->which);
16310 InterruptChildProcess(cps->pr);
16311 cps->maybeThinking = FALSE;
16316 #endif /*ATTENTION*/
16322 if (whiteTimeRemaining <= 0) {
16325 if (appData.icsActive) {
16326 if (appData.autoCallFlag &&
16327 gameMode == IcsPlayingBlack && !blackFlag) {
16328 SendToICS(ics_prefix);
16329 SendToICS("flag\n");
16333 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Both flags fell"));
16335 if(gameMode != TwoMachinesPlay) DisplayTitle(_("White's flag fell"));
16336 if (appData.autoCallFlag) {
16337 GameEnds(BlackWins, "Black wins on time", GE_XBOARD);
16344 if (blackTimeRemaining <= 0) {
16347 if (appData.icsActive) {
16348 if (appData.autoCallFlag &&
16349 gameMode == IcsPlayingWhite && !whiteFlag) {
16350 SendToICS(ics_prefix);
16351 SendToICS("flag\n");
16355 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Both flags fell"));
16357 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Black's flag fell"));
16358 if (appData.autoCallFlag) {
16359 GameEnds(WhiteWins, "White wins on time", GE_XBOARD);
16370 CheckTimeControl ()
16372 if (!appData.clockMode || appData.icsActive || searchTime || // [HGM] st: no inc in st mode
16373 gameMode == PlayFromGameFile || forwardMostMove == 0) return;
16376 * add time to clocks when time control is achieved ([HGM] now also used for increment)
16378 if ( !WhiteOnMove(forwardMostMove) ) {
16379 /* White made time control */
16380 lastWhite -= whiteTimeRemaining; // [HGM] contains start time, socalculate thinking time
16381 whiteTimeRemaining += GetTimeQuota((forwardMostMove-whiteStartMove-1)/2, lastWhite, whiteTC)
16382 /* [HGM] time odds: correct new time quota for time odds! */
16383 / WhitePlayer()->timeOdds;
16384 lastBlack = blackTimeRemaining; // [HGM] leave absolute time (after quota), so next switch we can us it to calculate thinking time
16386 lastBlack -= blackTimeRemaining;
16387 /* Black made time control */
16388 blackTimeRemaining += GetTimeQuota((forwardMostMove-blackStartMove-1)/2, lastBlack, blackTC)
16389 / WhitePlayer()->other->timeOdds;
16390 lastWhite = whiteTimeRemaining;
16395 DisplayBothClocks ()
16397 int wom = gameMode == EditPosition ?
16398 !blackPlaysFirst : WhiteOnMove(currentMove);
16399 DisplayWhiteClock(whiteTimeRemaining, wom);
16400 DisplayBlackClock(blackTimeRemaining, !wom);
16404 /* Timekeeping seems to be a portability nightmare. I think everyone
16405 has ftime(), but I'm really not sure, so I'm including some ifdefs
16406 to use other calls if you don't. Clocks will be less accurate if
16407 you have neither ftime nor gettimeofday.
16410 /* VS 2008 requires the #include outside of the function */
16411 #if !HAVE_GETTIMEOFDAY && HAVE_FTIME
16412 #include <sys/timeb.h>
16415 /* Get the current time as a TimeMark */
16417 GetTimeMark (TimeMark *tm)
16419 #if HAVE_GETTIMEOFDAY
16421 struct timeval timeVal;
16422 struct timezone timeZone;
16424 gettimeofday(&timeVal, &timeZone);
16425 tm->sec = (long) timeVal.tv_sec;
16426 tm->ms = (int) (timeVal.tv_usec / 1000L);
16428 #else /*!HAVE_GETTIMEOFDAY*/
16431 // include <sys/timeb.h> / moved to just above start of function
16432 struct timeb timeB;
16435 tm->sec = (long) timeB.time;
16436 tm->ms = (int) timeB.millitm;
16438 #else /*!HAVE_FTIME && !HAVE_GETTIMEOFDAY*/
16439 tm->sec = (long) time(NULL);
16445 /* Return the difference in milliseconds between two
16446 time marks. We assume the difference will fit in a long!
16449 SubtractTimeMarks (TimeMark *tm2, TimeMark *tm1)
16451 return 1000L*(tm2->sec - tm1->sec) +
16452 (long) (tm2->ms - tm1->ms);
16457 * Code to manage the game clocks.
16459 * In tournament play, black starts the clock and then white makes a move.
16460 * We give the human user a slight advantage if he is playing white---the
16461 * clocks don't run until he makes his first move, so it takes zero time.
16462 * Also, we don't account for network lag, so we could get out of sync
16463 * with GNU Chess's clock -- but then, referees are always right.
16466 static TimeMark tickStartTM;
16467 static long intendedTickLength;
16470 NextTickLength (long timeRemaining)
16472 long nominalTickLength, nextTickLength;
16474 if (timeRemaining > 0L && timeRemaining <= 10000L)
16475 nominalTickLength = 100L;
16477 nominalTickLength = 1000L;
16478 nextTickLength = timeRemaining % nominalTickLength;
16479 if (nextTickLength <= 0) nextTickLength += nominalTickLength;
16481 return nextTickLength;
16484 /* Adjust clock one minute up or down */
16486 AdjustClock (Boolean which, int dir)
16488 if(appData.autoCallFlag) { DisplayError(_("Clock adjustment not allowed in auto-flag mode"), 0); return; }
16489 if(which) blackTimeRemaining += 60000*dir;
16490 else whiteTimeRemaining += 60000*dir;
16491 DisplayBothClocks();
16492 adjustedClock = TRUE;
16495 /* Stop clocks and reset to a fresh time control */
16499 (void) StopClockTimer();
16500 if (appData.icsActive) {
16501 whiteTimeRemaining = blackTimeRemaining = 0;
16502 } else if (searchTime) {
16503 whiteTimeRemaining = 1000*searchTime / WhitePlayer()->timeOdds;
16504 blackTimeRemaining = 1000*searchTime / WhitePlayer()->other->timeOdds;
16505 } else { /* [HGM] correct new time quote for time odds */
16506 whiteTC = blackTC = fullTimeControlString;
16507 whiteTimeRemaining = GetTimeQuota(-1, 0, whiteTC) / WhitePlayer()->timeOdds;
16508 blackTimeRemaining = GetTimeQuota(-1, 0, blackTC) / WhitePlayer()->other->timeOdds;
16510 if (whiteFlag || blackFlag) {
16512 whiteFlag = blackFlag = FALSE;
16514 lastWhite = lastBlack = whiteStartMove = blackStartMove = 0;
16515 DisplayBothClocks();
16516 adjustedClock = FALSE;
16519 #define FUDGE 25 /* 25ms = 1/40 sec; should be plenty even for 50 Hz clocks */
16521 /* Decrement running clock by amount of time that has passed */
16525 long timeRemaining;
16526 long lastTickLength, fudge;
16529 if (!appData.clockMode) return;
16530 if (gameMode==AnalyzeMode || gameMode == AnalyzeFile) return;
16534 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
16536 /* Fudge if we woke up a little too soon */
16537 fudge = intendedTickLength - lastTickLength;
16538 if (fudge < 0 || fudge > FUDGE) fudge = 0;
16540 if (WhiteOnMove(forwardMostMove)) {
16541 if(whiteNPS >= 0) lastTickLength = 0;
16542 timeRemaining = whiteTimeRemaining -= lastTickLength;
16543 if(timeRemaining < 0 && !appData.icsActive) {
16544 GetTimeQuota((forwardMostMove-whiteStartMove-1)/2, 0, whiteTC); // sets suddenDeath & nextSession;
16545 if(suddenDeath) { // [HGM] if we run out of a non-last incremental session, go to the next
16546 whiteStartMove = forwardMostMove; whiteTC = nextSession;
16547 lastWhite= timeRemaining = whiteTimeRemaining += GetTimeQuota(-1, 0, whiteTC);
16550 DisplayWhiteClock(whiteTimeRemaining - fudge,
16551 WhiteOnMove(currentMove < forwardMostMove ? currentMove : forwardMostMove));
16553 if(blackNPS >= 0) lastTickLength = 0;
16554 timeRemaining = blackTimeRemaining -= lastTickLength;
16555 if(timeRemaining < 0 && !appData.icsActive) { // [HGM] if we run out of a non-last incremental session, go to the next
16556 GetTimeQuota((forwardMostMove-blackStartMove-1)/2, 0, blackTC);
16558 blackStartMove = forwardMostMove;
16559 lastBlack = timeRemaining = blackTimeRemaining += GetTimeQuota(-1, 0, blackTC=nextSession);
16562 DisplayBlackClock(blackTimeRemaining - fudge,
16563 !WhiteOnMove(currentMove < forwardMostMove ? currentMove : forwardMostMove));
16565 if (CheckFlags()) return;
16567 if(twoBoards) { // count down secondary board's clocks as well
16568 activePartnerTime -= lastTickLength;
16570 if(activePartner == 'W')
16571 DisplayWhiteClock(activePartnerTime, TRUE); // the counting clock is always the highlighted one!
16573 DisplayBlackClock(activePartnerTime, TRUE);
16578 intendedTickLength = NextTickLength(timeRemaining - fudge) + fudge;
16579 StartClockTimer(intendedTickLength);
16581 /* if the time remaining has fallen below the alarm threshold, sound the
16582 * alarm. if the alarm has sounded and (due to a takeback or time control
16583 * with increment) the time remaining has increased to a level above the
16584 * threshold, reset the alarm so it can sound again.
16587 if (appData.icsActive && appData.icsAlarm) {
16589 /* make sure we are dealing with the user's clock */
16590 if (!( ((gameMode == IcsPlayingWhite) && WhiteOnMove(currentMove)) ||
16591 ((gameMode == IcsPlayingBlack) && !WhiteOnMove(currentMove))
16594 if (alarmSounded && (timeRemaining > appData.icsAlarmTime)) {
16595 alarmSounded = FALSE;
16596 } else if (!alarmSounded && (timeRemaining <= appData.icsAlarmTime)) {
16598 alarmSounded = TRUE;
16604 /* A player has just moved, so stop the previously running
16605 clock and (if in clock mode) start the other one.
16606 We redisplay both clocks in case we're in ICS mode, because
16607 ICS gives us an update to both clocks after every move.
16608 Note that this routine is called *after* forwardMostMove
16609 is updated, so the last fractional tick must be subtracted
16610 from the color that is *not* on move now.
16613 SwitchClocks (int newMoveNr)
16615 long lastTickLength;
16617 int flagged = FALSE;
16621 if (StopClockTimer() && appData.clockMode) {
16622 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
16623 if (!WhiteOnMove(forwardMostMove)) {
16624 if(blackNPS >= 0) lastTickLength = 0;
16625 blackTimeRemaining -= lastTickLength;
16626 /* [HGM] PGNtime: save time for PGN file if engine did not give it */
16627 // if(pvInfoList[forwardMostMove].time == -1)
16628 pvInfoList[forwardMostMove].time = // use GUI time
16629 (timeRemaining[1][forwardMostMove-1] - blackTimeRemaining)/10;
16631 if(whiteNPS >= 0) lastTickLength = 0;
16632 whiteTimeRemaining -= lastTickLength;
16633 /* [HGM] PGNtime: save time for PGN file if engine did not give it */
16634 // if(pvInfoList[forwardMostMove].time == -1)
16635 pvInfoList[forwardMostMove].time =
16636 (timeRemaining[0][forwardMostMove-1] - whiteTimeRemaining)/10;
16638 flagged = CheckFlags();
16640 forwardMostMove = newMoveNr; // [HGM] race: change stm when no timer interrupt scheduled
16641 CheckTimeControl();
16643 if (flagged || !appData.clockMode) return;
16645 switch (gameMode) {
16646 case MachinePlaysBlack:
16647 case MachinePlaysWhite:
16648 case BeginningOfGame:
16649 if (pausing) return;
16653 case PlayFromGameFile:
16661 if (searchTime) { // [HGM] st: set clock of player that has to move to max time
16662 if(WhiteOnMove(forwardMostMove))
16663 whiteTimeRemaining = 1000*searchTime / WhitePlayer()->timeOdds;
16664 else blackTimeRemaining = 1000*searchTime / WhitePlayer()->other->timeOdds;
16668 intendedTickLength = NextTickLength(WhiteOnMove(forwardMostMove) ?
16669 whiteTimeRemaining : blackTimeRemaining);
16670 StartClockTimer(intendedTickLength);
16674 /* Stop both clocks */
16678 long lastTickLength;
16681 if (!StopClockTimer()) return;
16682 if (!appData.clockMode) return;
16686 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
16687 if (WhiteOnMove(forwardMostMove)) {
16688 if(whiteNPS >= 0) lastTickLength = 0;
16689 whiteTimeRemaining -= lastTickLength;
16690 DisplayWhiteClock(whiteTimeRemaining, WhiteOnMove(currentMove));
16692 if(blackNPS >= 0) lastTickLength = 0;
16693 blackTimeRemaining -= lastTickLength;
16694 DisplayBlackClock(blackTimeRemaining, !WhiteOnMove(currentMove));
16699 /* Start clock of player on move. Time may have been reset, so
16700 if clock is already running, stop and restart it. */
16704 (void) StopClockTimer(); /* in case it was running already */
16705 DisplayBothClocks();
16706 if (CheckFlags()) return;
16708 if (!appData.clockMode) return;
16709 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) return;
16711 GetTimeMark(&tickStartTM);
16712 intendedTickLength = NextTickLength(WhiteOnMove(forwardMostMove) ?
16713 whiteTimeRemaining : blackTimeRemaining);
16715 /* [HGM] nps: figure out nps factors, by determining which engine plays white and/or black once and for all */
16716 whiteNPS = blackNPS = -1;
16717 if(gameMode == MachinePlaysWhite || gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w'
16718 || appData.zippyPlay && gameMode == IcsPlayingBlack) // first (perhaps only) engine has white
16719 whiteNPS = first.nps;
16720 if(gameMode == MachinePlaysBlack || gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b'
16721 || appData.zippyPlay && gameMode == IcsPlayingWhite) // first (perhaps only) engine has black
16722 blackNPS = first.nps;
16723 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b') // second only used in Two-Machines mode
16724 whiteNPS = second.nps;
16725 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w')
16726 blackNPS = second.nps;
16727 if(appData.debugMode) fprintf(debugFP, "nps: w=%d, b=%d\n", whiteNPS, blackNPS);
16729 StartClockTimer(intendedTickLength);
16733 TimeString (long ms)
16735 long second, minute, hour, day;
16737 static char buf[32];
16739 if (ms > 0 && ms <= 9900) {
16740 /* convert milliseconds to tenths, rounding up */
16741 double tenths = floor( ((double)(ms + 99L)) / 100.00 );
16743 snprintf(buf,sizeof(buf)/sizeof(buf[0]), " %03.1f ", tenths/10.0);
16747 /* convert milliseconds to seconds, rounding up */
16748 /* use floating point to avoid strangeness of integer division
16749 with negative dividends on many machines */
16750 second = (long) floor(((double) (ms + 999L)) / 1000.0);
16757 day = second / (60 * 60 * 24);
16758 second = second % (60 * 60 * 24);
16759 hour = second / (60 * 60);
16760 second = second % (60 * 60);
16761 minute = second / 60;
16762 second = second % 60;
16765 snprintf(buf, sizeof(buf)/sizeof(buf[0]), " %s%ld:%02ld:%02ld:%02ld ",
16766 sign, day, hour, minute, second);
16768 snprintf(buf, sizeof(buf)/sizeof(buf[0]), " %s%ld:%02ld:%02ld ", sign, hour, minute, second);
16770 snprintf(buf, sizeof(buf)/sizeof(buf[0]), " %s%2ld:%02ld ", sign, minute, second);
16777 * This is necessary because some C libraries aren't ANSI C compliant yet.
16780 StrStr (char *string, char *match)
16784 length = strlen(match);
16786 for (i = strlen(string) - length; i >= 0; i--, string++)
16787 if (!strncmp(match, string, length))
16794 StrCaseStr (char *string, char *match)
16798 length = strlen(match);
16800 for (i = strlen(string) - length; i >= 0; i--, string++) {
16801 for (j = 0; j < length; j++) {
16802 if (ToLower(match[j]) != ToLower(string[j]))
16805 if (j == length) return string;
16813 StrCaseCmp (char *s1, char *s2)
16818 c1 = ToLower(*s1++);
16819 c2 = ToLower(*s2++);
16820 if (c1 > c2) return 1;
16821 if (c1 < c2) return -1;
16822 if (c1 == NULLCHAR) return 0;
16830 return isupper(c) ? tolower(c) : c;
16837 return islower(c) ? toupper(c) : c;
16839 #endif /* !_amigados */
16846 if ((ret = (char *) malloc(strlen(s) + 1)))
16848 safeStrCpy(ret, s, strlen(s)+1);
16854 StrSavePtr (char *s, char **savePtr)
16859 if ((*savePtr = (char *) malloc(strlen(s) + 1))) {
16860 safeStrCpy(*savePtr, s, strlen(s)+1);
16872 clock = time((time_t *)NULL);
16873 tm = localtime(&clock);
16874 snprintf(buf, MSG_SIZ, "%04d.%02d.%02d",
16875 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
16876 return StrSave(buf);
16881 PositionToFEN (int move, char *overrideCastling)
16883 int i, j, fromX, fromY, toX, toY;
16890 whiteToPlay = (gameMode == EditPosition) ?
16891 !blackPlaysFirst : (move % 2 == 0);
16894 /* Piece placement data */
16895 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
16896 if(MSG_SIZ - (p - buf) < BOARD_RGHT - BOARD_LEFT + 20) { *p = 0; return StrSave(buf); }
16898 for (j = BOARD_LEFT; j < BOARD_RGHT; j++) {
16899 if (boards[move][i][j] == EmptySquare) {
16901 } else { ChessSquare piece = boards[move][i][j];
16902 if (emptycount > 0) {
16903 if(emptycount<10) /* [HGM] can be >= 10 */
16904 *p++ = '0' + emptycount;
16905 else { *p++ = '0' + emptycount/10; *p++ = '0' + emptycount%10; }
16908 if(PieceToChar(piece) == '+') {
16909 /* [HGM] write promoted pieces as '+<unpromoted>' (Shogi) */
16911 piece = (ChessSquare)(DEMOTED piece);
16913 *p++ = PieceToChar(piece);
16915 /* [HGM] flag promoted pieces as '<promoted>~' (Crazyhouse) */
16916 p[-1] = PieceToChar((ChessSquare)(DEMOTED piece));
16921 if (emptycount > 0) {
16922 if(emptycount<10) /* [HGM] can be >= 10 */
16923 *p++ = '0' + emptycount;
16924 else { *p++ = '0' + emptycount/10; *p++ = '0' + emptycount%10; }
16931 /* [HGM] print Crazyhouse or Shogi holdings */
16932 if( gameInfo.holdingsWidth ) {
16933 *(p-1) = '['; /* if we wanted to support BFEN, this could be '/' */
16935 for(i=0; i<gameInfo.holdingsSize; i++) { /* white holdings */
16936 piece = boards[move][i][BOARD_WIDTH-1];
16937 if( piece != EmptySquare )
16938 for(j=0; j<(int) boards[move][i][BOARD_WIDTH-2]; j++)
16939 *p++ = PieceToChar(piece);
16941 for(i=0; i<gameInfo.holdingsSize; i++) { /* black holdings */
16942 piece = boards[move][BOARD_HEIGHT-i-1][0];
16943 if( piece != EmptySquare )
16944 for(j=0; j<(int) boards[move][BOARD_HEIGHT-i-1][1]; j++)
16945 *p++ = PieceToChar(piece);
16948 if( q == p ) *p++ = '-';
16954 *p++ = whiteToPlay ? 'w' : 'b';
16957 if(q = overrideCastling) { // [HGM] FRC: override castling & e.p fields for non-compliant engines
16958 while(*p++ = *q++); if(q != overrideCastling+1) p[-1] = ' '; else --p;
16960 if(nrCastlingRights) {
16962 if(gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom) {
16963 /* [HGM] write directly from rights */
16964 if(boards[move][CASTLING][2] != NoRights &&
16965 boards[move][CASTLING][0] != NoRights )
16966 *p++ = boards[move][CASTLING][0] + AAA + 'A' - 'a';
16967 if(boards[move][CASTLING][2] != NoRights &&
16968 boards[move][CASTLING][1] != NoRights )
16969 *p++ = boards[move][CASTLING][1] + AAA + 'A' - 'a';
16970 if(boards[move][CASTLING][5] != NoRights &&
16971 boards[move][CASTLING][3] != NoRights )
16972 *p++ = boards[move][CASTLING][3] + AAA;
16973 if(boards[move][CASTLING][5] != NoRights &&
16974 boards[move][CASTLING][4] != NoRights )
16975 *p++ = boards[move][CASTLING][4] + AAA;
16978 /* [HGM] write true castling rights */
16979 if( nrCastlingRights == 6 ) {
16981 if(boards[move][CASTLING][0] == BOARD_RGHT-1 &&
16982 boards[move][CASTLING][2] != NoRights ) k = 1, *p++ = 'K';
16983 q = (boards[move][CASTLING][1] == BOARD_LEFT &&
16984 boards[move][CASTLING][2] != NoRights );
16985 if(gameInfo.variant == VariantSChess) { // for S-Chess, indicate all vrgin backrank pieces
16986 for(i=j=0; i<BOARD_HEIGHT; i++) j += boards[move][i][BOARD_RGHT]; // count white held pieces
16987 for(i=BOARD_RGHT-1-k; i>=BOARD_LEFT+q && j; i--)
16988 if((boards[move][0][i] != WhiteKing || k+q == 0) &&
16989 boards[move][VIRGIN][i] & VIRGIN_W) *p++ = i + AAA + 'A' - 'a';
16993 if(boards[move][CASTLING][3] == BOARD_RGHT-1 &&
16994 boards[move][CASTLING][5] != NoRights ) k = 1, *p++ = 'k';
16995 q = (boards[move][CASTLING][4] == BOARD_LEFT &&
16996 boards[move][CASTLING][5] != NoRights );
16997 if(gameInfo.variant == VariantSChess) {
16998 for(i=j=0; i<BOARD_HEIGHT; i++) j += boards[move][i][BOARD_LEFT-1]; // count black held pieces
16999 for(i=BOARD_RGHT-1-k; i>=BOARD_LEFT+q && j; i--)
17000 if((boards[move][BOARD_HEIGHT-1][i] != BlackKing || k+q == 0) &&
17001 boards[move][VIRGIN][i] & VIRGIN_B) *p++ = i + AAA;
17006 if (q == p) *p++ = '-'; /* No castling rights */
17010 if(gameInfo.variant != VariantShogi && gameInfo.variant != VariantXiangqi &&
17011 gameInfo.variant != VariantShatranj && gameInfo.variant != VariantCourier && gameInfo.variant != VariantMakruk ) {
17012 /* En passant target square */
17013 if (move > backwardMostMove) {
17014 fromX = moveList[move - 1][0] - AAA;
17015 fromY = moveList[move - 1][1] - ONE;
17016 toX = moveList[move - 1][2] - AAA;
17017 toY = moveList[move - 1][3] - ONE;
17018 if (fromY == (whiteToPlay ? BOARD_HEIGHT-2 : 1) &&
17019 toY == (whiteToPlay ? BOARD_HEIGHT-4 : 3) &&
17020 boards[move][toY][toX] == (whiteToPlay ? BlackPawn : WhitePawn) &&
17022 /* 2-square pawn move just happened */
17024 *p++ = whiteToPlay ? '6'+BOARD_HEIGHT-8 : '3';
17028 } else if(move == backwardMostMove) {
17029 // [HGM] perhaps we should always do it like this, and forget the above?
17030 if((signed char)boards[move][EP_STATUS] >= 0) {
17031 *p++ = boards[move][EP_STATUS] + AAA;
17032 *p++ = whiteToPlay ? '6'+BOARD_HEIGHT-8 : '3';
17043 /* [HGM] find reversible plies */
17044 { int i = 0, j=move;
17046 if (appData.debugMode) { int k;
17047 fprintf(debugFP, "write FEN 50-move: %d %d %d\n", initialRulePlies, forwardMostMove, backwardMostMove);
17048 for(k=backwardMostMove; k<=forwardMostMove; k++)
17049 fprintf(debugFP, "e%d. p=%d\n", k, (signed char)boards[k][EP_STATUS]);
17053 while(j > backwardMostMove && (signed char)boards[j][EP_STATUS] <= EP_NONE) j--,i++;
17054 if( j == backwardMostMove ) i += initialRulePlies;
17055 sprintf(p, "%d ", i);
17056 p += i>=100 ? 4 : i >= 10 ? 3 : 2;
17058 /* Fullmove number */
17059 sprintf(p, "%d", (move / 2) + 1);
17061 return StrSave(buf);
17065 ParseFEN (Board board, int *blackPlaysFirst, char *fen)
17069 int emptycount, virgin[BOARD_FILES];
17074 /* [HGM] by default clear Crazyhouse holdings, if present */
17075 if(gameInfo.holdingsWidth) {
17076 for(i=0; i<BOARD_HEIGHT; i++) {
17077 board[i][0] = EmptySquare; /* black holdings */
17078 board[i][BOARD_WIDTH-1] = EmptySquare; /* white holdings */
17079 board[i][1] = (ChessSquare) 0; /* black counts */
17080 board[i][BOARD_WIDTH-2] = (ChessSquare) 0; /* white counts */
17084 /* Piece placement data */
17085 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
17088 if (*p == '/' || *p == ' ' || (*p == '[' && i == 0) ) {
17089 if (*p == '/') p++;
17090 emptycount = gameInfo.boardWidth - j;
17091 while (emptycount--)
17092 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
17094 #if(BOARD_FILES >= 10)
17095 } else if(*p=='x' || *p=='X') { /* [HGM] X means 10 */
17096 p++; emptycount=10;
17097 if (j + emptycount > gameInfo.boardWidth) return FALSE;
17098 while (emptycount--)
17099 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
17101 } else if (isdigit(*p)) {
17102 emptycount = *p++ - '0';
17103 while(isdigit(*p)) emptycount = 10*emptycount + *p++ - '0'; /* [HGM] allow > 9 */
17104 if (j + emptycount > gameInfo.boardWidth) return FALSE;
17105 while (emptycount--)
17106 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
17107 } else if (*p == '+' || isalpha(*p)) {
17108 if (j >= gameInfo.boardWidth) return FALSE;
17110 piece = CharToPiece(*++p);
17111 if(piece == EmptySquare) return FALSE; /* unknown piece */
17112 piece = (ChessSquare) (PROMOTED piece ); p++;
17113 if(PieceToChar(piece) != '+') return FALSE; /* unpromotable piece */
17114 } else piece = CharToPiece(*p++);
17116 if(piece==EmptySquare) return FALSE; /* unknown piece */
17117 if(*p == '~') { /* [HGM] make it a promoted piece for Crazyhouse */
17118 piece = (ChessSquare) (PROMOTED piece);
17119 if(PieceToChar(piece) != '~') return FALSE; /* cannot be a promoted piece */
17122 board[i][(j++)+gameInfo.holdingsWidth] = piece;
17128 while (*p == '/' || *p == ' ') p++;
17130 /* [HGM] look for Crazyhouse holdings here */
17131 while(*p==' ') p++;
17132 if( gameInfo.holdingsWidth && p[-1] == '/' || *p == '[') {
17134 if(*p == '-' ) p++; /* empty holdings */ else {
17135 if( !gameInfo.holdingsWidth ) return FALSE; /* no room to put holdings! */
17136 /* if we would allow FEN reading to set board size, we would */
17137 /* have to add holdings and shift the board read so far here */
17138 while( (piece = CharToPiece(*p) ) != EmptySquare ) {
17140 if((int) piece >= (int) BlackPawn ) {
17141 i = (int)piece - (int)BlackPawn;
17142 i = PieceToNumber((ChessSquare)i);
17143 if( i >= gameInfo.holdingsSize ) return FALSE;
17144 board[BOARD_HEIGHT-1-i][0] = piece; /* black holdings */
17145 board[BOARD_HEIGHT-1-i][1]++; /* black counts */
17147 i = (int)piece - (int)WhitePawn;
17148 i = PieceToNumber((ChessSquare)i);
17149 if( i >= gameInfo.holdingsSize ) return FALSE;
17150 board[i][BOARD_WIDTH-1] = piece; /* white holdings */
17151 board[i][BOARD_WIDTH-2]++; /* black holdings */
17158 while(*p == ' ') p++;
17162 if(appData.colorNickNames) {
17163 if( c == appData.colorNickNames[0] ) c = 'w'; else
17164 if( c == appData.colorNickNames[1] ) c = 'b';
17168 *blackPlaysFirst = FALSE;
17171 *blackPlaysFirst = TRUE;
17177 /* [HGM] We NO LONGER ignore the rest of the FEN notation */
17178 /* return the extra info in global variiables */
17180 /* set defaults in case FEN is incomplete */
17181 board[EP_STATUS] = EP_UNKNOWN;
17182 for(i=0; i<nrCastlingRights; i++ ) {
17183 board[CASTLING][i] =
17184 gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom ? NoRights : initialRights[i];
17185 } /* assume possible unless obviously impossible */
17186 if(initialRights[0]!=NoRights && board[castlingRank[0]][initialRights[0]] != WhiteRook) board[CASTLING][0] = NoRights;
17187 if(initialRights[1]!=NoRights && board[castlingRank[1]][initialRights[1]] != WhiteRook) board[CASTLING][1] = NoRights;
17188 if(initialRights[2]!=NoRights && board[castlingRank[2]][initialRights[2]] != WhiteUnicorn
17189 && board[castlingRank[2]][initialRights[2]] != WhiteKing) board[CASTLING][2] = NoRights;
17190 if(initialRights[3]!=NoRights && board[castlingRank[3]][initialRights[3]] != BlackRook) board[CASTLING][3] = NoRights;
17191 if(initialRights[4]!=NoRights && board[castlingRank[4]][initialRights[4]] != BlackRook) board[CASTLING][4] = NoRights;
17192 if(initialRights[5]!=NoRights && board[castlingRank[5]][initialRights[5]] != BlackUnicorn
17193 && board[castlingRank[5]][initialRights[5]] != BlackKing) board[CASTLING][5] = NoRights;
17196 while(*p==' ') p++;
17197 if(nrCastlingRights) {
17198 if(gameInfo.variant == VariantSChess) for(i=0; i<BOARD_FILES; i++) virgin[i] = 0;
17199 if(*p >= 'A' && *p <= 'Z' || *p >= 'a' && *p <= 'z' || *p=='-') {
17200 /* castling indicator present, so default becomes no castlings */
17201 for(i=0; i<nrCastlingRights; i++ ) {
17202 board[CASTLING][i] = NoRights;
17205 while(*p=='K' || *p=='Q' || *p=='k' || *p=='q' || *p=='-' ||
17206 (gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom || gameInfo.variant == VariantSChess) &&
17207 ( *p >= 'a' && *p < 'a' + gameInfo.boardWidth) ||
17208 ( *p >= 'A' && *p < 'A' + gameInfo.boardWidth) ) {
17209 int c = *p++, whiteKingFile=NoRights, blackKingFile=NoRights;
17211 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) {
17212 if(board[BOARD_HEIGHT-1][i] == BlackKing) blackKingFile = i;
17213 if(board[0 ][i] == WhiteKing) whiteKingFile = i;
17215 if(gameInfo.variant == VariantTwoKings || gameInfo.variant == VariantKnightmate)
17216 whiteKingFile = blackKingFile = BOARD_WIDTH >> 1; // for these variant scanning fails
17217 if(whiteKingFile == NoRights || board[0][whiteKingFile] != WhiteUnicorn
17218 && board[0][whiteKingFile] != WhiteKing) whiteKingFile = NoRights;
17219 if(blackKingFile == NoRights || board[BOARD_HEIGHT-1][blackKingFile] != BlackUnicorn
17220 && board[BOARD_HEIGHT-1][blackKingFile] != BlackKing) blackKingFile = NoRights;
17223 for(i=BOARD_RGHT-1; board[0][i]!=WhiteRook && i>whiteKingFile; i--);
17224 board[CASTLING][0] = i != whiteKingFile ? i : NoRights;
17225 board[CASTLING][2] = whiteKingFile;
17226 if(board[CASTLING][0] != NoRights) virgin[board[CASTLING][0]] |= VIRGIN_W;
17227 if(board[CASTLING][2] != NoRights) virgin[board[CASTLING][2]] |= VIRGIN_W;
17230 for(i=BOARD_LEFT; i<BOARD_RGHT && board[0][i]!=WhiteRook && i<whiteKingFile; i++);
17231 board[CASTLING][1] = i != whiteKingFile ? i : NoRights;
17232 board[CASTLING][2] = whiteKingFile;
17233 if(board[CASTLING][1] != NoRights) virgin[board[CASTLING][1]] |= VIRGIN_W;
17234 if(board[CASTLING][2] != NoRights) virgin[board[CASTLING][2]] |= VIRGIN_W;
17237 for(i=BOARD_RGHT-1; board[BOARD_HEIGHT-1][i]!=BlackRook && i>blackKingFile; i--);
17238 board[CASTLING][3] = i != blackKingFile ? i : NoRights;
17239 board[CASTLING][5] = blackKingFile;
17240 if(board[CASTLING][3] != NoRights) virgin[board[CASTLING][3]] |= VIRGIN_B;
17241 if(board[CASTLING][5] != NoRights) virgin[board[CASTLING][5]] |= VIRGIN_B;
17244 for(i=BOARD_LEFT; i<BOARD_RGHT && board[BOARD_HEIGHT-1][i]!=BlackRook && i<blackKingFile; i++);
17245 board[CASTLING][4] = i != blackKingFile ? i : NoRights;
17246 board[CASTLING][5] = blackKingFile;
17247 if(board[CASTLING][4] != NoRights) virgin[board[CASTLING][4]] |= VIRGIN_B;
17248 if(board[CASTLING][5] != NoRights) virgin[board[CASTLING][5]] |= VIRGIN_B;
17251 default: /* FRC castlings */
17252 if(c >= 'a') { /* black rights */
17253 if(gameInfo.variant == VariantSChess) { virgin[c-AAA] |= VIRGIN_B; break; } // in S-Chess castlings are always kq, so just virginity
17254 for(i=BOARD_LEFT; i<BOARD_RGHT; i++)
17255 if(board[BOARD_HEIGHT-1][i] == BlackKing) break;
17256 if(i == BOARD_RGHT) break;
17257 board[CASTLING][5] = i;
17259 if(board[BOARD_HEIGHT-1][c] < BlackPawn ||
17260 board[BOARD_HEIGHT-1][c] >= BlackKing ) break;
17262 board[CASTLING][3] = c;
17264 board[CASTLING][4] = c;
17265 } else { /* white rights */
17266 if(gameInfo.variant == VariantSChess) { virgin[c-AAA-'A'+'a'] |= VIRGIN_W; break; } // in S-Chess castlings are always KQ
17267 for(i=BOARD_LEFT; i<BOARD_RGHT; i++)
17268 if(board[0][i] == WhiteKing) break;
17269 if(i == BOARD_RGHT) break;
17270 board[CASTLING][2] = i;
17271 c -= AAA - 'a' + 'A';
17272 if(board[0][c] >= WhiteKing) break;
17274 board[CASTLING][0] = c;
17276 board[CASTLING][1] = c;
17280 for(i=0; i<nrCastlingRights; i++)
17281 if(board[CASTLING][i] != NoRights) initialRights[i] = board[CASTLING][i];
17282 if(gameInfo.variant == VariantSChess) for(i=0; i<BOARD_FILES; i++) board[VIRGIN][i] = virgin[i];
17283 if (appData.debugMode) {
17284 fprintf(debugFP, "FEN castling rights:");
17285 for(i=0; i<nrCastlingRights; i++)
17286 fprintf(debugFP, " %d", board[CASTLING][i]);
17287 fprintf(debugFP, "\n");
17290 while(*p==' ') p++;
17293 /* read e.p. field in games that know e.p. capture */
17294 if(gameInfo.variant != VariantShogi && gameInfo.variant != VariantXiangqi &&
17295 gameInfo.variant != VariantShatranj && gameInfo.variant != VariantCourier && gameInfo.variant != VariantMakruk ) {
17297 p++; board[EP_STATUS] = EP_NONE;
17299 char c = *p++ - AAA;
17301 if(c < BOARD_LEFT || c >= BOARD_RGHT) return TRUE;
17302 if(*p >= '0' && *p <='9') p++;
17303 board[EP_STATUS] = c;
17308 if(sscanf(p, "%d", &i) == 1) {
17309 FENrulePlies = i; /* 50-move ply counter */
17310 /* (The move number is still ignored) */
17317 EditPositionPasteFEN (char *fen)
17320 Board initial_position;
17322 if (!ParseFEN(initial_position, &blackPlaysFirst, fen)) {
17323 DisplayError(_("Bad FEN position in clipboard"), 0);
17326 int savedBlackPlaysFirst = blackPlaysFirst;
17327 EditPositionEvent();
17328 blackPlaysFirst = savedBlackPlaysFirst;
17329 CopyBoard(boards[0], initial_position);
17330 initialRulePlies = FENrulePlies; /* [HGM] copy FEN attributes as well */
17331 EditPositionDone(FALSE); // [HGM] fake: do not fake rights if we had FEN
17332 DisplayBothClocks();
17333 DrawPosition(FALSE, boards[currentMove]);
17338 static char cseq[12] = "\\ ";
17341 set_cont_sequence (char *new_seq)
17346 // handle bad attempts to set the sequence
17348 return 0; // acceptable error - no debug
17350 len = strlen(new_seq);
17351 ret = (len > 0) && (len < sizeof(cseq));
17353 safeStrCpy(cseq, new_seq, sizeof(cseq)/sizeof(cseq[0]));
17354 else if (appData.debugMode)
17355 fprintf(debugFP, "Invalid continuation sequence \"%s\" (maximum length is: %u)\n", new_seq, (unsigned) sizeof(cseq)-1);
17360 reformat a source message so words don't cross the width boundary. internal
17361 newlines are not removed. returns the wrapped size (no null character unless
17362 included in source message). If dest is NULL, only calculate the size required
17363 for the dest buffer. lp argument indicats line position upon entry, and it's
17364 passed back upon exit.
17367 wrap (char *dest, char *src, int count, int width, int *lp)
17369 int len, i, ansi, cseq_len, line, old_line, old_i, old_len, clen;
17371 cseq_len = strlen(cseq);
17372 old_line = line = *lp;
17373 ansi = len = clen = 0;
17375 for (i=0; i < count; i++)
17377 if (src[i] == '\033')
17380 // if we hit the width, back up
17381 if (!ansi && (line >= width) && src[i] != '\n' && src[i] != ' ')
17383 // store i & len in case the word is too long
17384 old_i = i, old_len = len;
17386 // find the end of the last word
17387 while (i && src[i] != ' ' && src[i] != '\n')
17393 // word too long? restore i & len before splitting it
17394 if ((old_i-i+clen) >= width)
17401 if (i && src[i-1] == ' ')
17404 if (src[i] != ' ' && src[i] != '\n')
17411 // now append the newline and continuation sequence
17416 strncpy(dest+len, cseq, cseq_len);
17424 dest[len] = src[i];
17428 if (src[i] == '\n')
17433 if (dest && appData.debugMode)
17435 fprintf(debugFP, "wrap(count:%d,width:%d,line:%d,len:%d,*lp:%d,src: ",
17436 count, width, line, len, *lp);
17437 show_bytes(debugFP, src, count);
17438 fprintf(debugFP, "\ndest: ");
17439 show_bytes(debugFP, dest, len);
17440 fprintf(debugFP, "\n");
17442 *lp = dest ? line : old_line;
17447 // [HGM] vari: routines for shelving variations
17448 Boolean modeRestore = FALSE;
17451 PushInner (int firstMove, int lastMove)
17453 int i, j, nrMoves = lastMove - firstMove;
17455 // push current tail of game on stack
17456 savedResult[storedGames] = gameInfo.result;
17457 savedDetails[storedGames] = gameInfo.resultDetails;
17458 gameInfo.resultDetails = NULL;
17459 savedFirst[storedGames] = firstMove;
17460 savedLast [storedGames] = lastMove;
17461 savedFramePtr[storedGames] = framePtr;
17462 framePtr -= nrMoves; // reserve space for the boards
17463 for(i=nrMoves; i>=1; i--) { // copy boards to stack, working downwards, in case of overlap
17464 CopyBoard(boards[framePtr+i], boards[firstMove+i]);
17465 for(j=0; j<MOVE_LEN; j++)
17466 moveList[framePtr+i][j] = moveList[firstMove+i-1][j];
17467 for(j=0; j<2*MOVE_LEN; j++)
17468 parseList[framePtr+i][j] = parseList[firstMove+i-1][j];
17469 timeRemaining[0][framePtr+i] = timeRemaining[0][firstMove+i];
17470 timeRemaining[1][framePtr+i] = timeRemaining[1][firstMove+i];
17471 pvInfoList[framePtr+i] = pvInfoList[firstMove+i-1];
17472 pvInfoList[firstMove+i-1].depth = 0;
17473 commentList[framePtr+i] = commentList[firstMove+i];
17474 commentList[firstMove+i] = NULL;
17478 forwardMostMove = firstMove; // truncate game so we can start variation
17482 PushTail (int firstMove, int lastMove)
17484 if(appData.icsActive) { // only in local mode
17485 forwardMostMove = currentMove; // mimic old ICS behavior
17488 if(storedGames >= MAX_VARIATIONS-2) return; // leave one for PV-walk
17490 PushInner(firstMove, lastMove);
17491 if(storedGames == 1) GreyRevert(FALSE);
17492 if(gameMode == PlayFromGameFile) gameMode = EditGame, modeRestore = TRUE;
17496 PopInner (Boolean annotate)
17499 char buf[8000], moveBuf[20];
17501 ToNrEvent(savedFirst[storedGames-1]); // sets currentMove
17502 storedGames--; // do this after ToNrEvent, to make sure HistorySet will refresh entire game after PopInner returns
17503 nrMoves = savedLast[storedGames] - currentMove;
17506 if(!WhiteOnMove(currentMove))
17507 snprintf(buf, sizeof(buf)/sizeof(buf[0]),"(%d...", (currentMove+2)>>1);
17508 else safeStrCpy(buf, "(", sizeof(buf)/sizeof(buf[0]));
17509 for(i=currentMove; i<forwardMostMove; i++) {
17511 snprintf(moveBuf, sizeof(moveBuf)/sizeof(moveBuf[0]), " %d. %s", (i+2)>>1, SavePart(parseList[i]));
17512 else snprintf(moveBuf, sizeof(moveBuf)/sizeof(moveBuf[0])," %s", SavePart(parseList[i]));
17513 strcat(buf, moveBuf);
17514 if(commentList[i]) { strcat(buf, " "); strcat(buf, commentList[i]); }
17515 if(!--cnt) { strcat(buf, "\n"); cnt = 10; }
17519 for(i=1; i<=nrMoves; i++) { // copy last variation back
17520 CopyBoard(boards[currentMove+i], boards[framePtr+i]);
17521 for(j=0; j<MOVE_LEN; j++)
17522 moveList[currentMove+i-1][j] = moveList[framePtr+i][j];
17523 for(j=0; j<2*MOVE_LEN; j++)
17524 parseList[currentMove+i-1][j] = parseList[framePtr+i][j];
17525 timeRemaining[0][currentMove+i] = timeRemaining[0][framePtr+i];
17526 timeRemaining[1][currentMove+i] = timeRemaining[1][framePtr+i];
17527 pvInfoList[currentMove+i-1] = pvInfoList[framePtr+i];
17528 if(commentList[currentMove+i]) free(commentList[currentMove+i]);
17529 commentList[currentMove+i] = commentList[framePtr+i];
17530 commentList[framePtr+i] = NULL;
17532 if(annotate) AppendComment(currentMove+1, buf, FALSE);
17533 framePtr = savedFramePtr[storedGames];
17534 gameInfo.result = savedResult[storedGames];
17535 if(gameInfo.resultDetails != NULL) {
17536 free(gameInfo.resultDetails);
17538 gameInfo.resultDetails = savedDetails[storedGames];
17539 forwardMostMove = currentMove + nrMoves;
17543 PopTail (Boolean annotate)
17545 if(appData.icsActive) return FALSE; // only in local mode
17546 if(!storedGames) return FALSE; // sanity
17547 CommentPopDown(); // make sure no stale variation comments to the destroyed line can remain open
17549 PopInner(annotate);
17550 if(currentMove < forwardMostMove) ForwardEvent(); else
17551 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
17553 if(storedGames == 0) { GreyRevert(TRUE); if(modeRestore) modeRestore = FALSE, gameMode = PlayFromGameFile; }
17559 { // remove all shelved variations
17561 for(i=0; i<storedGames; i++) {
17562 if(savedDetails[i])
17563 free(savedDetails[i]);
17564 savedDetails[i] = NULL;
17566 for(i=framePtr; i<MAX_MOVES; i++) {
17567 if(commentList[i]) free(commentList[i]);
17568 commentList[i] = NULL;
17570 framePtr = MAX_MOVES-1;
17575 LoadVariation (int index, char *text)
17576 { // [HGM] vari: shelve previous line and load new variation, parsed from text around text[index]
17577 char *p = text, *start = NULL, *end = NULL, wait = NULLCHAR;
17578 int level = 0, move;
17580 if(gameMode != EditGame && gameMode != AnalyzeMode && gameMode != PlayFromGameFile) return;
17581 // first find outermost bracketing variation
17582 while(*p) { // hope I got this right... Non-nesting {} and [] can screen each other and nesting ()
17583 if(!wait) { // while inside [] pr {}, ignore everyting except matching closing ]}
17584 if(*p == '{') wait = '}'; else
17585 if(*p == '[') wait = ']'; else
17586 if(*p == '(' && level++ == 0 && p-text < index) start = p+1;
17587 if(*p == ')' && level > 0 && --level == 0 && p-text > index && end == NULL) end = p-1;
17589 if(*p == wait) wait = NULLCHAR; // closing ]} found
17592 if(!start || !end) return; // no variation found, or syntax error in PGN: ignore click
17593 if(appData.debugMode) fprintf(debugFP, "at move %d load variation '%s'\n", currentMove, start);
17594 end[1] = NULLCHAR; // clip off comment beyond variation
17595 ToNrEvent(currentMove-1);
17596 PushTail(currentMove, forwardMostMove); // shelve main variation. This truncates game
17597 // kludge: use ParsePV() to append variation to game
17598 move = currentMove;
17599 ParsePV(start, TRUE, TRUE);
17600 forwardMostMove = endPV; endPV = -1; currentMove = move; // cleanup what ParsePV did
17601 ClearPremoveHighlights();
17603 ToNrEvent(currentMove+1);
17609 char *p, *q, buf[MSG_SIZ];
17610 if(engineLine && engineLine[0]) { // a theme was selected from the listbox
17611 snprintf(buf, MSG_SIZ, "-theme %s", engineLine);
17612 ParseArgsFromString(buf);
17613 ActivateTheme(TRUE); // also redo colors
17617 if(*p && !strchr(p, '"')) // theme name specified and well-formed; add settings to theme list
17620 q = appData.themeNames;
17621 snprintf(buf, MSG_SIZ, "\"%s\"", nickName);
17622 if(appData.useBitmaps) {
17623 snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), " -ubt true -lbtf \"%s\" -dbtf \"%s\" -lbtm %d -dbtm %d",
17624 appData.liteBackTextureFile, appData.darkBackTextureFile,
17625 appData.liteBackTextureMode,
17626 appData.darkBackTextureMode );
17628 snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), " -ubt false -lsc %s -dsc %s",
17629 Col2Text(2), // lightSquareColor
17630 Col2Text(3) ); // darkSquareColor
17632 if(appData.useBorder) {
17633 snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), " -ub true -border \"%s\"",
17636 snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), " -ub false");
17638 if(appData.useFont) {
17639 snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), " -upf true -pf \"%s\" -fptc \"%s\" -fpfcw %s -fpbcb %s",
17640 appData.renderPiecesWithFont,
17641 appData.fontToPieceTable,
17642 Col2Text(9), // appData.fontBackColorWhite
17643 Col2Text(10) ); // appData.fontForeColorBlack
17645 snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), " -upf false -pid \"%s\"",
17646 appData.pieceDirectory);
17647 if(!appData.pieceDirectory[0])
17648 snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), " -wpc %s -bpc %s",
17649 Col2Text(0), // whitePieceColor
17650 Col2Text(1) ); // blackPieceColor
17652 snprintf(buf+strlen(buf), MSG_SIZ-strlen(buf), " -hsc %s -phc %s\n",
17653 Col2Text(4), // highlightSquareColor
17654 Col2Text(5) ); // premoveHighlightColor
17655 appData.themeNames = malloc(len = strlen(q) + strlen(buf) + 1);
17656 if(insert != q) insert[-1] = NULLCHAR;
17657 snprintf(appData.themeNames, len, "%s\n%s%s", q, buf, insert);
17660 ActivateTheme(FALSE);