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 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 #define DoSleep( n ) if( (n) != 0 ) Sleep( (n) );
62 #define DoSleep( n ) if( (n) >= 0) sleep(n)
72 #include <sys/types.h>
81 #else /* not STDC_HEADERS */
84 # else /* not HAVE_STRING_H */
86 # endif /* not HAVE_STRING_H */
87 #endif /* not STDC_HEADERS */
90 # include <sys/fcntl.h>
91 #else /* not HAVE_SYS_FCNTL_H */
94 # endif /* HAVE_FCNTL_H */
95 #endif /* not HAVE_SYS_FCNTL_H */
97 #if TIME_WITH_SYS_TIME
98 # include <sys/time.h>
102 # include <sys/time.h>
108 #if defined(_amigados) && !defined(__GNUC__)
113 extern int gettimeofday(struct timeval *, struct timezone *);
121 #include "frontend.h"
128 #include "backendz.h"
132 # define _(s) gettext (s)
133 # define N_(s) gettext_noop (s)
134 # define T_(s) gettext(s)
147 /* A point in time */
149 long sec; /* Assuming this is >= 32 bits */
150 int ms; /* Assuming this is >= 16 bits */
153 int establish P((void));
154 void read_from_player P((InputSourceRef isr, VOIDSTAR closure,
155 char *buf, int count, int error));
156 void read_from_ics P((InputSourceRef isr, VOIDSTAR closure,
157 char *buf, int count, int error));
158 void ics_printf P((char *format, ...));
159 void SendToICS P((char *s));
160 void SendToICSDelayed P((char *s, long msdelay));
161 void SendMoveToICS P((ChessMove moveType, int fromX, int fromY, int toX, int toY, char promoChar));
162 void HandleMachineMove P((char *message, ChessProgramState *cps));
163 int AutoPlayOneMove P((void));
164 int LoadGameOneMove P((ChessMove readAhead));
165 int LoadGameFromFile P((char *filename, int n, char *title, int useList));
166 int LoadPositionFromFile P((char *filename, int n, char *title));
167 int SavePositionToFile P((char *filename));
168 void ApplyMove P((int fromX, int fromY, int toX, int toY, int promoChar,
170 void MakeMove P((int fromX, int fromY, int toX, int toY, int promoChar));
171 void ShowMove P((int fromX, int fromY, int toX, int toY));
172 int FinishMove P((ChessMove moveType, int fromX, int fromY, int toX, int toY,
173 /*char*/int promoChar));
174 void BackwardInner P((int target));
175 void ForwardInner P((int target));
176 int Adjudicate P((ChessProgramState *cps));
177 void GameEnds P((ChessMove result, char *resultDetails, int whosays));
178 void EditPositionDone P((Boolean fakeRights));
179 void PrintOpponents P((FILE *fp));
180 void PrintPosition P((FILE *fp, int move));
181 void StartChessProgram P((ChessProgramState *cps));
182 void SendToProgram P((char *message, ChessProgramState *cps));
183 void SendMoveToProgram P((int moveNum, ChessProgramState *cps));
184 void ReceiveFromProgram P((InputSourceRef isr, VOIDSTAR closure,
185 char *buf, int count, int error));
186 void SendTimeControl P((ChessProgramState *cps,
187 int mps, long tc, int inc, int sd, int st));
188 char *TimeControlTagValue P((void));
189 void Attention P((ChessProgramState *cps));
190 void FeedMovesToProgram P((ChessProgramState *cps, int upto));
191 void ResurrectChessProgram P((void));
192 void DisplayComment P((int moveNumber, char *text));
193 void DisplayMove P((int moveNumber));
195 void ParseGameHistory P((char *game));
196 void ParseBoard12 P((char *string));
197 void KeepAlive P((void));
198 void StartClocks P((void));
199 void SwitchClocks P((int nr));
200 void StopClocks P((void));
201 void ResetClocks P((void));
202 char *PGNDate P((void));
203 void SetGameInfo P((void));
204 Boolean ParseFEN P((Board board, int *blackPlaysFirst, char *fen));
205 int RegisterMove P((void));
206 void MakeRegisteredMove P((void));
207 void TruncateGame P((void));
208 int looking_at P((char *, int *, char *));
209 void CopyPlayerNameIntoFileName P((char **, char *));
210 char *SavePart P((char *));
211 int SaveGameOldStyle P((FILE *));
212 int SaveGamePGN P((FILE *));
213 void GetTimeMark P((TimeMark *));
214 long SubtractTimeMarks P((TimeMark *, TimeMark *));
215 int CheckFlags P((void));
216 long NextTickLength P((long));
217 void CheckTimeControl P((void));
218 void show_bytes P((FILE *, char *, int));
219 int string_to_rating P((char *str));
220 void ParseFeatures P((char* args, ChessProgramState *cps));
221 void InitBackEnd3 P((void));
222 void FeatureDone P((ChessProgramState* cps, int val));
223 void InitChessProgram P((ChessProgramState *cps, int setup));
224 void OutputKibitz(int window, char *text);
225 int PerpetualChase(int first, int last);
226 int EngineOutputIsUp();
227 void InitDrawingSizes(int x, int y);
230 extern void ConsoleCreate();
233 ChessProgramState *WhitePlayer();
234 void InsertIntoMemo P((int which, char *text)); // [HGM] kibitz: in engineo.c
235 int VerifyDisplayMode P(());
237 char *GetInfoFromComment( int, char * ); // [HGM] PV time: returns stripped comment
238 void InitEngineUCI( const char * iniDir, ChessProgramState * cps ); // [HGM] moved here from winboard.c
239 char *ProbeBook P((int moveNr, char *book)); // [HGM] book: returns a book move
240 char *SendMoveToBookUser P((int nr, ChessProgramState *cps, int initial)); // [HGM] book
241 void ics_update_width P((int new_width));
242 extern char installDir[MSG_SIZ];
243 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 */
271 /* States for ics_getting_history */
273 #define H_REQUESTED 1
274 #define H_GOT_REQ_HEADER 2
275 #define H_GOT_UNREQ_HEADER 3
276 #define H_GETTING_MOVES 4
277 #define H_GOT_UNWANTED_HEADER 5
279 /* whosays values for GameEnds */
288 /* Maximum number of games in a cmail message */
289 #define CMAIL_MAX_GAMES 20
291 /* Different types of move when calling RegisterMove */
293 #define CMAIL_RESIGN 1
295 #define CMAIL_ACCEPT 3
297 /* Different types of result to remember for each game */
298 #define CMAIL_NOT_RESULT 0
299 #define CMAIL_OLD_RESULT 1
300 #define CMAIL_NEW_RESULT 2
302 /* Telnet protocol constants */
313 safeStrCpy( char *dst, const char *src, size_t count )
315 /* see for example: https://buildsecurityin.us-cert.gov/bsi-rules/home/g1/854-BSI.html
317 * usage: safeStrCpy( stringA, stringB, sizeof(stringA)/sizeof(stringA[0]);
320 assert( dst != NULL );
321 assert( src != NULL );
324 strncpy( dst, src, count );
325 if( dst[ count-1 ] != '\0' )
327 if(appData.debugMode)
328 printf("safeStrCpy: copying %s into %s didn't work, not enough space %d\n",src,dst,count);
330 dst[ count-1 ] = '\0';
335 /* Some compiler can't cast u64 to double
336 * This function do the job for us:
338 * We use the highest bit for cast, this only
339 * works if the highest bit is not
340 * in use (This should not happen)
342 * We used this for all compiler
345 u64ToDouble(u64 value)
348 u64 tmp = value & u64Const(0x7fffffffffffffff);
349 r = (double)(s64)tmp;
350 if (value & u64Const(0x8000000000000000))
351 r += 9.2233720368547758080e18; /* 2^63 */
355 /* Fake up flags for now, as we aren't keeping track of castling
356 availability yet. [HGM] Change of logic: the flag now only
357 indicates the type of castlings allowed by the rule of the game.
358 The actual rights themselves are maintained in the array
359 castlingRights, as part of the game history, and are not probed
365 int flags = F_ALL_CASTLE_OK;
366 if ((index % 2) == 0) flags |= F_WHITE_ON_MOVE;
367 switch (gameInfo.variant) {
369 flags &= ~F_ALL_CASTLE_OK;
370 case VariantGiveaway: // [HGM] moved this case label one down: seems Giveaway does have castling on ICC!
371 flags |= F_IGNORE_CHECK;
373 flags |= F_MANDATORY_CAPTURE; //[HGM] losers: sets flag so TestLegality rejects non-capts if capts exist
376 flags |= F_IGNORE_CHECK | F_ATOMIC_CAPTURE;
378 case VariantKriegspiel:
379 flags |= F_KRIEGSPIEL_CAPTURE;
381 case VariantCapaRandom:
382 case VariantFischeRandom:
383 flags |= F_FRC_TYPE_CASTLING; /* [HGM] enable this through flag */
384 case VariantNoCastle:
385 case VariantShatranj:
388 flags &= ~F_ALL_CASTLE_OK;
396 FILE *gameFileFP, *debugFP;
399 [AS] Note: sometimes, the sscanf() function is used to parse the input
400 into a fixed-size buffer. Because of this, we must be prepared to
401 receive strings as long as the size of the input buffer, which is currently
402 set to 4K for Windows and 8K for the rest.
403 So, we must either allocate sufficiently large buffers here, or
404 reduce the size of the input buffer in the input reading part.
407 char cmailMove[CMAIL_MAX_GAMES][MOVE_LEN], cmailMsg[MSG_SIZ];
408 char bookOutput[MSG_SIZ*10], thinkOutput[MSG_SIZ*10], lastHint[MSG_SIZ];
409 char thinkOutput1[MSG_SIZ*10];
411 ChessProgramState first, second;
413 /* premove variables */
416 int premoveFromX = 0;
417 int premoveFromY = 0;
418 int premovePromoChar = 0;
420 Boolean alarmSounded;
421 /* end premove variables */
423 char *ics_prefix = "$";
424 int ics_type = ICS_GENERIC;
426 int currentMove = 0, forwardMostMove = 0, backwardMostMove = 0;
427 int pauseExamForwardMostMove = 0;
428 int nCmailGames = 0, nCmailResults = 0, nCmailMovesRegistered = 0;
429 int cmailMoveRegistered[CMAIL_MAX_GAMES], cmailResult[CMAIL_MAX_GAMES];
430 int cmailMsgLoaded = FALSE, cmailMailedMove = FALSE;
431 int cmailOldMove = -1, firstMove = TRUE, flipView = FALSE;
432 int blackPlaysFirst = FALSE, startedFromSetupPosition = FALSE;
433 int searchTime = 0, pausing = FALSE, pauseExamInvalid = FALSE;
434 int whiteFlag = FALSE, blackFlag = FALSE;
435 int userOfferedDraw = FALSE;
436 int ics_user_moved = 0, ics_gamenum = -1, ics_getting_history = H_FALSE;
437 int matchMode = FALSE, hintRequested = FALSE, bookRequested = FALSE;
438 int cmailMoveType[CMAIL_MAX_GAMES];
439 long ics_clock_paused = 0;
440 ProcRef icsPR = NoProc, cmailPR = NoProc;
441 InputSourceRef telnetISR = NULL, fromUserISR = NULL, cmailISR = NULL;
442 GameMode gameMode = BeginningOfGame;
443 char moveList[MAX_MOVES][MOVE_LEN], parseList[MAX_MOVES][MOVE_LEN * 2];
444 char *commentList[MAX_MOVES], *cmailCommentList[CMAIL_MAX_GAMES];
445 ChessProgramStats_Move pvInfoList[MAX_MOVES]; /* [AS] Info about engine thinking */
446 int hiddenThinkOutputState = 0; /* [AS] */
447 int adjudicateLossThreshold = 0; /* [AS] Automatic adjudication */
448 int adjudicateLossPlies = 6;
449 char white_holding[64], black_holding[64];
450 TimeMark lastNodeCountTime;
451 long lastNodeCount=0;
452 int have_sent_ICS_logon = 0;
454 int suddenDeath, whiteStartMove, blackStartMove; /* [HGM] for implementation of 'any per time' sessions, as in first part of byoyomi TC */
455 long whiteTimeRemaining, blackTimeRemaining, timeControl, timeIncrement, lastWhite, lastBlack;
456 long timeControl_2; /* [AS] Allow separate time controls */
457 char *fullTimeControlString = NULL, *nextSession, *whiteTC, *blackTC; /* [HGM] secondary TC: merge of MPS, TC and inc */
458 long timeRemaining[2][MAX_MOVES];
460 TimeMark programStartTime;
461 char ics_handle[MSG_SIZ];
462 int have_set_title = 0;
464 /* animateTraining preserves the state of appData.animate
465 * when Training mode is activated. This allows the
466 * response to be animated when appData.animate == TRUE and
467 * appData.animateDragging == TRUE.
469 Boolean animateTraining;
475 Board boards[MAX_MOVES];
476 /* [HGM] Following 7 needed for accurate legality tests: */
477 signed char castlingRank[BOARD_FILES]; // and corresponding ranks
478 signed char initialRights[BOARD_FILES];
479 int nrCastlingRights; // For TwoKings, or to implement castling-unknown status
480 int initialRulePlies, FENrulePlies;
481 FILE *serverMoves = NULL; // next two for broadcasting (/serverMoves option)
484 int mute; // mute all sounds
486 // [HGM] vari: next 12 to save and restore variations
487 #define MAX_VARIATIONS 10
488 int framePtr = MAX_MOVES-1; // points to free stack entry
490 int savedFirst[MAX_VARIATIONS];
491 int savedLast[MAX_VARIATIONS];
492 int savedFramePtr[MAX_VARIATIONS];
493 char *savedDetails[MAX_VARIATIONS];
494 ChessMove savedResult[MAX_VARIATIONS];
496 void PushTail P((int firstMove, int lastMove));
497 Boolean PopTail P((Boolean annotate));
498 void CleanupTail P((void));
500 ChessSquare FIDEArray[2][BOARD_FILES] = {
501 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
502 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
503 { BlackRook, BlackKnight, BlackBishop, BlackQueen,
504 BlackKing, BlackBishop, BlackKnight, BlackRook }
507 ChessSquare twoKingsArray[2][BOARD_FILES] = {
508 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
509 WhiteKing, WhiteKing, WhiteKnight, WhiteRook },
510 { BlackRook, BlackKnight, BlackBishop, BlackQueen,
511 BlackKing, BlackKing, BlackKnight, BlackRook }
514 ChessSquare KnightmateArray[2][BOARD_FILES] = {
515 { WhiteRook, WhiteMan, WhiteBishop, WhiteQueen,
516 WhiteUnicorn, WhiteBishop, WhiteMan, WhiteRook },
517 { BlackRook, BlackMan, BlackBishop, BlackQueen,
518 BlackUnicorn, BlackBishop, BlackMan, BlackRook }
521 ChessSquare fairyArray[2][BOARD_FILES] = { /* [HGM] Queen side differs from King side */
522 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
523 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
524 { BlackLance, BlackAlfil, BlackMarshall, BlackAngel,
525 BlackKing, BlackMarshall, BlackAlfil, BlackLance }
528 ChessSquare ShatranjArray[2][BOARD_FILES] = { /* [HGM] (movGen knows about Shatranj Q and P) */
529 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteKing,
530 WhiteFerz, WhiteAlfil, WhiteKnight, WhiteRook },
531 { BlackRook, BlackKnight, BlackAlfil, BlackKing,
532 BlackFerz, BlackAlfil, BlackKnight, BlackRook }
535 ChessSquare makrukArray[2][BOARD_FILES] = { /* [HGM] (movGen knows about Shatranj Q and P) */
536 { WhiteRook, WhiteKnight, WhiteMan, WhiteKing,
537 WhiteFerz, WhiteMan, WhiteKnight, WhiteRook },
538 { BlackRook, BlackKnight, BlackMan, BlackFerz,
539 BlackKing, BlackMan, BlackKnight, BlackRook }
543 #if (BOARD_FILES>=10)
544 ChessSquare ShogiArray[2][BOARD_FILES] = {
545 { WhiteQueen, WhiteKnight, WhiteFerz, WhiteWazir,
546 WhiteKing, WhiteWazir, WhiteFerz, WhiteKnight, WhiteQueen },
547 { BlackQueen, BlackKnight, BlackFerz, BlackWazir,
548 BlackKing, BlackWazir, BlackFerz, BlackKnight, BlackQueen }
551 ChessSquare XiangqiArray[2][BOARD_FILES] = {
552 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteFerz,
553 WhiteWazir, WhiteFerz, WhiteAlfil, WhiteKnight, WhiteRook },
554 { BlackRook, BlackKnight, BlackAlfil, BlackFerz,
555 BlackWazir, BlackFerz, BlackAlfil, BlackKnight, BlackRook }
558 ChessSquare CapablancaArray[2][BOARD_FILES] = {
559 { WhiteRook, WhiteKnight, WhiteAngel, WhiteBishop, WhiteQueen,
560 WhiteKing, WhiteBishop, WhiteMarshall, WhiteKnight, WhiteRook },
561 { BlackRook, BlackKnight, BlackAngel, BlackBishop, BlackQueen,
562 BlackKing, BlackBishop, BlackMarshall, BlackKnight, BlackRook }
565 ChessSquare GreatArray[2][BOARD_FILES] = {
566 { WhiteDragon, WhiteKnight, WhiteAlfil, WhiteGrasshopper, WhiteKing,
567 WhiteSilver, WhiteCardinal, WhiteAlfil, WhiteKnight, WhiteDragon },
568 { BlackDragon, BlackKnight, BlackAlfil, BlackGrasshopper, BlackKing,
569 BlackSilver, BlackCardinal, BlackAlfil, BlackKnight, BlackDragon },
572 ChessSquare JanusArray[2][BOARD_FILES] = {
573 { WhiteRook, WhiteAngel, WhiteKnight, WhiteBishop, WhiteKing,
574 WhiteQueen, WhiteBishop, WhiteKnight, WhiteAngel, WhiteRook },
575 { BlackRook, BlackAngel, BlackKnight, BlackBishop, BlackKing,
576 BlackQueen, BlackBishop, BlackKnight, BlackAngel, BlackRook }
580 ChessSquare GothicArray[2][BOARD_FILES] = {
581 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen, WhiteMarshall,
582 WhiteKing, WhiteAngel, WhiteBishop, WhiteKnight, WhiteRook },
583 { BlackRook, BlackKnight, BlackBishop, BlackQueen, BlackMarshall,
584 BlackKing, BlackAngel, BlackBishop, BlackKnight, BlackRook }
587 #define GothicArray CapablancaArray
591 ChessSquare FalconArray[2][BOARD_FILES] = {
592 { WhiteRook, WhiteKnight, WhiteBishop, WhiteLance, WhiteQueen,
593 WhiteKing, WhiteLance, WhiteBishop, WhiteKnight, WhiteRook },
594 { BlackRook, BlackKnight, BlackBishop, BlackLance, BlackQueen,
595 BlackKing, BlackLance, BlackBishop, BlackKnight, BlackRook }
598 #define FalconArray CapablancaArray
601 #else // !(BOARD_FILES>=10)
602 #define XiangqiPosition FIDEArray
603 #define CapablancaArray FIDEArray
604 #define GothicArray FIDEArray
605 #define GreatArray FIDEArray
606 #endif // !(BOARD_FILES>=10)
608 #if (BOARD_FILES>=12)
609 ChessSquare CourierArray[2][BOARD_FILES] = {
610 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteBishop, WhiteMan, WhiteKing,
611 WhiteFerz, WhiteWazir, WhiteBishop, WhiteAlfil, WhiteKnight, WhiteRook },
612 { BlackRook, BlackKnight, BlackAlfil, BlackBishop, BlackMan, BlackKing,
613 BlackFerz, BlackWazir, BlackBishop, BlackAlfil, BlackKnight, BlackRook }
615 #else // !(BOARD_FILES>=12)
616 #define CourierArray CapablancaArray
617 #endif // !(BOARD_FILES>=12)
620 Board initialPosition;
623 /* Convert str to a rating. Checks for special cases of "----",
625 "++++", etc. Also strips ()'s */
627 string_to_rating(str)
630 while(*str && !isdigit(*str)) ++str;
632 return 0; /* One of the special "no rating" cases */
640 /* Init programStats */
641 programStats.movelist[0] = 0;
642 programStats.depth = 0;
643 programStats.nr_moves = 0;
644 programStats.moves_left = 0;
645 programStats.nodes = 0;
646 programStats.time = -1; // [HGM] PGNtime: make invalid to recognize engine output
647 programStats.score = 0;
648 programStats.got_only_move = 0;
649 programStats.got_fail = 0;
650 programStats.line_is_book = 0;
656 int matched, min, sec;
658 ShowThinkingEvent(); // [HGM] thinking: make sure post/nopost state is set according to options
659 startVariant = StringToVariant(appData.variant); // [HGM] nicks: remember original variant
661 GetTimeMark(&programStartTime);
662 srandom((programStartTime.ms + 1000*programStartTime.sec)*0x1001001); // [HGM] book: makes sure random is unpredictabe to msec level
665 programStats.ok_to_send = 1;
666 programStats.seen_stat = 0;
669 * Initialize game list
675 * Internet chess server status
677 if (appData.icsActive) {
678 appData.matchMode = FALSE;
679 appData.matchGames = 0;
681 appData.noChessProgram = !appData.zippyPlay;
683 appData.zippyPlay = FALSE;
684 appData.zippyTalk = FALSE;
685 appData.noChessProgram = TRUE;
687 if (*appData.icsHelper != NULLCHAR) {
688 appData.useTelnet = TRUE;
689 appData.telnetProgram = appData.icsHelper;
692 appData.zippyTalk = appData.zippyPlay = FALSE;
695 /* [AS] Initialize pv info list [HGM] and game state */
699 for( i=0; i<=framePtr; i++ ) {
700 pvInfoList[i].depth = -1;
701 boards[i][EP_STATUS] = EP_NONE;
702 for( j=0; j<BOARD_FILES-2; j++ ) boards[i][CASTLING][j] = NoRights;
707 * Parse timeControl resource
709 if (!ParseTimeControl(appData.timeControl, appData.timeIncrement,
710 appData.movesPerSession)) {
712 snprintf(buf, sizeof(buf), _("bad timeControl option %s"), appData.timeControl);
713 DisplayFatalError(buf, 0, 2);
717 * Parse searchTime resource
719 if (*appData.searchTime != NULLCHAR) {
720 matched = sscanf(appData.searchTime, "%d:%d", &min, &sec);
722 searchTime = min * 60;
723 } else if (matched == 2) {
724 searchTime = min * 60 + sec;
727 snprintf(buf, sizeof(buf), _("bad searchTime option %s"), appData.searchTime);
728 DisplayFatalError(buf, 0, 2);
732 /* [AS] Adjudication threshold */
733 adjudicateLossThreshold = appData.adjudicateLossThreshold;
735 first.which = _("first");
736 second.which = _("second");
737 first.maybeThinking = second.maybeThinking = FALSE;
738 first.pr = second.pr = NoProc;
739 first.isr = second.isr = NULL;
740 first.sendTime = second.sendTime = 2;
741 first.sendDrawOffers = 1;
742 if (appData.firstPlaysBlack) {
743 first.twoMachinesColor = "black\n";
744 second.twoMachinesColor = "white\n";
746 first.twoMachinesColor = "white\n";
747 second.twoMachinesColor = "black\n";
749 first.program = appData.firstChessProgram;
750 second.program = appData.secondChessProgram;
751 first.host = appData.firstHost;
752 second.host = appData.secondHost;
753 first.dir = appData.firstDirectory;
754 second.dir = appData.secondDirectory;
755 first.other = &second;
756 second.other = &first;
757 first.initString = appData.initString;
758 second.initString = appData.secondInitString;
759 first.computerString = appData.firstComputerString;
760 second.computerString = appData.secondComputerString;
761 first.useSigint = second.useSigint = TRUE;
762 first.useSigterm = second.useSigterm = TRUE;
763 first.reuse = appData.reuseFirst;
764 second.reuse = appData.reuseSecond;
765 first.nps = appData.firstNPS; // [HGM] nps: copy nodes per second
766 second.nps = appData.secondNPS;
767 first.useSetboard = second.useSetboard = FALSE;
768 first.useSAN = second.useSAN = FALSE;
769 first.usePing = second.usePing = FALSE;
770 first.lastPing = second.lastPing = 0;
771 first.lastPong = second.lastPong = 0;
772 first.usePlayother = second.usePlayother = FALSE;
773 first.useColors = second.useColors = TRUE;
774 first.useUsermove = second.useUsermove = FALSE;
775 first.sendICS = second.sendICS = FALSE;
776 first.sendName = second.sendName = appData.icsActive;
777 first.sdKludge = second.sdKludge = FALSE;
778 first.stKludge = second.stKludge = FALSE;
779 TidyProgramName(first.program, first.host, first.tidy);
780 TidyProgramName(second.program, second.host, second.tidy);
781 first.matchWins = second.matchWins = 0;
782 safeStrCpy(first.variants, appData.variant, sizeof(first.variants)/sizeof(first.variants[0]));
783 safeStrCpy(second.variants, appData.variant,sizeof(second.variants)/sizeof(second.variants[0]));
784 first.analysisSupport = second.analysisSupport = 2; /* detect */
785 first.analyzing = second.analyzing = FALSE;
786 first.initDone = second.initDone = FALSE;
788 /* New features added by Tord: */
789 first.useFEN960 = FALSE; second.useFEN960 = FALSE;
790 first.useOOCastle = TRUE; second.useOOCastle = TRUE;
791 /* End of new features added by Tord. */
792 first.fenOverride = appData.fenOverride1;
793 second.fenOverride = appData.fenOverride2;
795 /* [HGM] time odds: set factor for each machine */
796 first.timeOdds = appData.firstTimeOdds;
797 second.timeOdds = appData.secondTimeOdds;
799 if(appData.timeOddsMode) {
800 norm = first.timeOdds;
801 if(norm > second.timeOdds) norm = second.timeOdds;
803 first.timeOdds /= norm;
804 second.timeOdds /= norm;
807 /* [HGM] secondary TC: how to handle sessions that do not fit in 'level'*/
808 first.accumulateTC = appData.firstAccumulateTC;
809 second.accumulateTC = appData.secondAccumulateTC;
810 first.maxNrOfSessions = second.maxNrOfSessions = 1;
813 first.debug = second.debug = FALSE;
814 first.supportsNPS = second.supportsNPS = UNKNOWN;
817 first.optionSettings = appData.firstOptions;
818 second.optionSettings = appData.secondOptions;
820 first.scoreIsAbsolute = appData.firstScoreIsAbsolute; /* [AS] */
821 second.scoreIsAbsolute = appData.secondScoreIsAbsolute; /* [AS] */
822 first.isUCI = appData.firstIsUCI; /* [AS] */
823 second.isUCI = appData.secondIsUCI; /* [AS] */
824 first.hasOwnBookUCI = appData.firstHasOwnBookUCI; /* [AS] */
825 second.hasOwnBookUCI = appData.secondHasOwnBookUCI; /* [AS] */
827 if (appData.firstProtocolVersion > PROTOVER
828 || appData.firstProtocolVersion < 1)
833 len = snprintf(buf, MSG_SIZ, _("protocol version %d not supported"),
834 appData.firstProtocolVersion);
835 if( (len > MSG_SIZ) && appData.debugMode )
836 fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
838 DisplayFatalError(buf, 0, 2);
842 first.protocolVersion = appData.firstProtocolVersion;
845 if (appData.secondProtocolVersion > PROTOVER
846 || appData.secondProtocolVersion < 1)
851 len = snprintf(buf, MSG_SIZ, _("protocol version %d not supported"),
852 appData.secondProtocolVersion);
853 if( (len > MSG_SIZ) && appData.debugMode )
854 fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
856 DisplayFatalError(buf, 0, 2);
860 second.protocolVersion = appData.secondProtocolVersion;
863 if (appData.icsActive) {
864 appData.clockMode = TRUE; /* changes dynamically in ICS mode */
865 // } else if (*appData.searchTime != NULLCHAR || appData.noChessProgram) {
866 } else if (appData.noChessProgram) { // [HGM] st: searchTime mode now also is clockMode
867 appData.clockMode = FALSE;
868 first.sendTime = second.sendTime = 0;
872 /* Override some settings from environment variables, for backward
873 compatibility. Unfortunately it's not feasible to have the env
874 vars just set defaults, at least in xboard. Ugh.
876 if (appData.icsActive && (appData.zippyPlay || appData.zippyTalk)) {
881 if (appData.noChessProgram) {
882 programVersion = (char*) malloc(5 + strlen(PACKAGE_STRING));
883 sprintf(programVersion, "%s", PACKAGE_STRING);
885 /* [HGM] tidy: use tidy name, in stead of full pathname (which was probably a bug due to / vs \ ) */
886 programVersion = (char*) malloc(8 + strlen(PACKAGE_STRING) + strlen(first.tidy));
887 sprintf(programVersion, "%s + %s", PACKAGE_STRING, first.tidy);
890 if (!appData.icsActive) {
894 /* Check for variants that are supported only in ICS mode,
895 or not at all. Some that are accepted here nevertheless
896 have bugs; see comments below.
898 VariantClass variant = StringToVariant(appData.variant);
900 case VariantBughouse: /* need four players and two boards */
901 case VariantKriegspiel: /* need to hide pieces and move details */
902 /* case VariantFischeRandom: (Fabien: moved below) */
903 len = snprintf(buf,MSG_SIZ, _("Variant %s supported only in ICS mode"), appData.variant);
904 if( (len > MSG_SIZ) && appData.debugMode )
905 fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
907 DisplayFatalError(buf, 0, 2);
911 case VariantLoadable:
921 len = snprintf(buf, MSG_SIZ, _("Unknown variant name %s"), appData.variant);
922 if( (len > MSG_SIZ) && appData.debugMode )
923 fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
925 DisplayFatalError(buf, 0, 2);
928 case VariantXiangqi: /* [HGM] repetition rules not implemented */
929 case VariantFairy: /* [HGM] TestLegality definitely off! */
930 case VariantGothic: /* [HGM] should work */
931 case VariantCapablanca: /* [HGM] should work */
932 case VariantCourier: /* [HGM] initial forced moves not implemented */
933 case VariantShogi: /* [HGM] could still mate with pawn drop */
934 case VariantKnightmate: /* [HGM] should work */
935 case VariantCylinder: /* [HGM] untested */
936 case VariantFalcon: /* [HGM] untested */
937 case VariantCrazyhouse: /* holdings not shown, ([HGM] fixed that!)
938 offboard interposition not understood */
939 case VariantNormal: /* definitely works! */
940 case VariantWildCastle: /* pieces not automatically shuffled */
941 case VariantNoCastle: /* pieces not automatically shuffled */
942 case VariantFischeRandom: /* [HGM] works and shuffles pieces */
943 case VariantLosers: /* should work except for win condition,
944 and doesn't know captures are mandatory */
945 case VariantSuicide: /* should work except for win condition,
946 and doesn't know captures are mandatory */
947 case VariantGiveaway: /* should work except for win condition,
948 and doesn't know captures are mandatory */
949 case VariantTwoKings: /* should work */
950 case VariantAtomic: /* should work except for win condition */
951 case Variant3Check: /* should work except for win condition */
952 case VariantShatranj: /* should work except for all win conditions */
953 case VariantMakruk: /* should work except for daw countdown */
954 case VariantBerolina: /* might work if TestLegality is off */
955 case VariantCapaRandom: /* should work */
956 case VariantJanus: /* should work */
957 case VariantSuper: /* experimental */
958 case VariantGreat: /* experimental, requires legality testing to be off */
963 InitEngineUCI( installDir, &first ); // [HGM] moved here from winboard.c, to make available in xboard
964 InitEngineUCI( installDir, &second );
967 int NextIntegerFromString( char ** str, long * value )
972 while( *s == ' ' || *s == '\t' ) {
978 if( *s >= '0' && *s <= '9' ) {
979 while( *s >= '0' && *s <= '9' ) {
980 *value = *value * 10 + (*s - '0');
992 int NextTimeControlFromString( char ** str, long * value )
995 int result = NextIntegerFromString( str, &temp );
998 *value = temp * 60; /* Minutes */
1001 result = NextIntegerFromString( str, &temp );
1002 *value += temp; /* Seconds */
1009 int NextSessionFromString( char ** str, int *moves, long * tc, long *inc, int *incType)
1010 { /* [HGM] routine added to read '+moves/time' for secondary time control. */
1011 int result = -1, type = 0; long temp, temp2;
1013 if(**str != ':') return -1; // old params remain in force!
1015 if(**str == '*') type = *(*str)++, temp = 0; // sandclock TC
1016 if( NextIntegerFromString( str, &temp ) ) return -1;
1017 if(type) { *moves = 0; *tc = temp * 500; *inc = temp * 1000; *incType = '*'; return 0; }
1020 /* time only: incremental or sudden-death time control */
1021 if(**str == '+') { /* increment follows; read it */
1023 if(**str == '!') type = *(*str)++; // Bronstein TC
1024 if(result = NextIntegerFromString( str, &temp2)) return -1;
1025 *inc = temp2 * 1000;
1027 *moves = 0; *tc = temp * 1000; *incType = type;
1031 (*str)++; /* classical time control */
1032 result = NextIntegerFromString( str, &temp2); // NOTE: already converted to seconds by ParseTimeControl()
1043 int GetTimeQuota(int movenr, int lastUsed, char *tcString)
1044 { /* [HGM] get time to add from the multi-session time-control string */
1045 int incType, moves=1; /* kludge to force reading of first session */
1046 long time, increment;
1049 if(!*s) return 0; // empty TC string means we ran out of the last sudden-death version
1050 if(appData.debugMode) fprintf(debugFP, "TC string = '%s'\n", tcString);
1052 if(moves) NextSessionFromString(&s, &moves, &time, &increment, &incType);
1053 nextSession = s; suddenDeath = moves == 0 && increment == 0;
1054 if(appData.debugMode) fprintf(debugFP, "mps=%d tc=%d inc=%d\n", moves, (int) time, (int) increment);
1055 if(movenr == -1) return time; /* last move before new session */
1056 if(incType == '*') increment = 0; else // for sandclock, time is added while not thinking
1057 if(incType == '!' && lastUsed < increment) increment = lastUsed;
1058 if(!moves) return increment; /* current session is incremental */
1059 if(movenr >= 0) movenr -= moves; /* we already finished this session */
1060 } while(movenr >= -1); /* try again for next session */
1062 return 0; // no new time quota on this move
1066 ParseTimeControl(tc, ti, mps)
1073 char buf[MSG_SIZ], buf2[MSG_SIZ], *mytc = tc;
1077 if(ti >= 0 && !strchr(tc, '+') && !strchr(tc, '/') ) mps = 0;
1078 if(!strchr(tc, '+') && !strchr(tc, '/') && sscanf(tc, "%d:%d", &min, &sec) >= 1)
1079 sprintf(mytc=buf2, "%d", 60*min+sec); // convert 'classical' min:sec tc string to seconds
1083 snprintf(buf, MSG_SIZ, ":%d/%s+%d", mps, mytc, ti);
1085 snprintf(buf, MSG_SIZ, ":%s+%d", mytc, ti);
1088 snprintf(buf, MSG_SIZ, ":%d/%s", mps, mytc);
1090 snprintf(buf, MSG_SIZ, ":%s", mytc);
1092 fullTimeControlString = StrSave(buf); // this should now be in PGN format
1094 if( NextTimeControlFromString( &tc, &tc1 ) != 0 ) {
1099 /* Parse second time control */
1102 if( NextTimeControlFromString( &tc, &tc2 ) != 0 ) {
1110 timeControl_2 = tc2 * 1000;
1120 timeControl = tc1 * 1000;
1123 timeIncrement = ti * 1000; /* convert to ms */
1124 movesPerSession = 0;
1127 movesPerSession = mps;
1135 if (appData.debugMode) {
1136 fprintf(debugFP, "%s\n", programVersion);
1139 set_cont_sequence(appData.wrapContSeq);
1140 if (appData.matchGames > 0) {
1141 appData.matchMode = TRUE;
1142 } else if (appData.matchMode) {
1143 appData.matchGames = 1;
1145 if(appData.matchMode && appData.sameColorGames > 0) /* [HGM] alternate: overrule matchGames */
1146 appData.matchGames = appData.sameColorGames;
1147 if(appData.rewindIndex > 1) { /* [HGM] autoinc: rewind implies auto-increment and overrules given index */
1148 if(appData.loadPositionIndex >= 0) appData.loadPositionIndex = -1;
1149 if(appData.loadGameIndex >= 0) appData.loadGameIndex = -1;
1152 if (appData.noChessProgram || first.protocolVersion == 1) {
1155 /* kludge: allow timeout for initial "feature" commands */
1157 DisplayMessage("", _("Starting chess program"));
1158 ScheduleDelayedEvent(InitBackEnd3, FEATURE_TIMEOUT);
1163 InitBackEnd3 P((void))
1165 GameMode initialMode;
1169 InitChessProgram(&first, startedFromSetupPosition);
1171 if(!appData.noChessProgram) { /* [HGM] tidy: redo program version to use name from myname feature */
1172 free(programVersion);
1173 programVersion = (char*) malloc(8 + strlen(PACKAGE_STRING) + strlen(first.tidy));
1174 sprintf(programVersion, "%s + %s", PACKAGE_STRING, first.tidy);
1177 if (appData.icsActive) {
1179 /* [DM] Make a console window if needed [HGM] merged ifs */
1185 if (*appData.icsCommPort != NULLCHAR)
1186 len = snprintf(buf, MSG_SIZ, _("Could not open comm port %s"),
1187 appData.icsCommPort);
1189 len = snprintf(buf, MSG_SIZ, _("Could not connect to host %s, port %s"),
1190 appData.icsHost, appData.icsPort);
1192 if( (len > MSG_SIZ) && appData.debugMode )
1193 fprintf(debugFP, "InitBackEnd3: buffer truncated.\n");
1195 DisplayFatalError(buf, err, 1);
1200 AddInputSource(icsPR, FALSE, read_from_ics, &telnetISR);
1202 AddInputSource(NoProc, FALSE, read_from_player, &fromUserISR);
1203 if(appData.keepAlive) // [HGM] alive: schedule sending of dummy 'date' command
1204 ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
1205 } else if (appData.noChessProgram) {
1211 if (*appData.cmailGameName != NULLCHAR) {
1213 OpenLoopback(&cmailPR);
1215 AddInputSource(cmailPR, FALSE, CmailSigHandlerCallBack, &cmailISR);
1219 DisplayMessage("", "");
1220 if (StrCaseCmp(appData.initialMode, "") == 0) {
1221 initialMode = BeginningOfGame;
1222 } else if (StrCaseCmp(appData.initialMode, "TwoMachines") == 0) {
1223 initialMode = TwoMachinesPlay;
1224 } else if (StrCaseCmp(appData.initialMode, "AnalyzeFile") == 0) {
1225 initialMode = AnalyzeFile;
1226 } else if (StrCaseCmp(appData.initialMode, "Analysis") == 0) {
1227 initialMode = AnalyzeMode;
1228 } else if (StrCaseCmp(appData.initialMode, "MachineWhite") == 0) {
1229 initialMode = MachinePlaysWhite;
1230 } else if (StrCaseCmp(appData.initialMode, "MachineBlack") == 0) {
1231 initialMode = MachinePlaysBlack;
1232 } else if (StrCaseCmp(appData.initialMode, "EditGame") == 0) {
1233 initialMode = EditGame;
1234 } else if (StrCaseCmp(appData.initialMode, "EditPosition") == 0) {
1235 initialMode = EditPosition;
1236 } else if (StrCaseCmp(appData.initialMode, "Training") == 0) {
1237 initialMode = Training;
1239 len = snprintf(buf, MSG_SIZ, _("Unknown initialMode %s"), appData.initialMode);
1240 if( (len > MSG_SIZ) && appData.debugMode )
1241 fprintf(debugFP, "InitBackEnd3: buffer truncated.\n");
1243 DisplayFatalError(buf, 0, 2);
1247 if (appData.matchMode) {
1248 /* Set up machine vs. machine match */
1249 if (appData.noChessProgram) {
1250 DisplayFatalError(_("Can't have a match with no chess programs"),
1256 if (*appData.loadGameFile != NULLCHAR) {
1257 int index = appData.loadGameIndex; // [HGM] autoinc
1258 if(index<0) lastIndex = index = 1;
1259 if (!LoadGameFromFile(appData.loadGameFile,
1261 appData.loadGameFile, FALSE)) {
1262 DisplayFatalError(_("Bad game file"), 0, 1);
1265 } else if (*appData.loadPositionFile != NULLCHAR) {
1266 int index = appData.loadPositionIndex; // [HGM] autoinc
1267 if(index<0) lastIndex = index = 1;
1268 if (!LoadPositionFromFile(appData.loadPositionFile,
1270 appData.loadPositionFile)) {
1271 DisplayFatalError(_("Bad position file"), 0, 1);
1276 } else if (*appData.cmailGameName != NULLCHAR) {
1277 /* Set up cmail mode */
1278 ReloadCmailMsgEvent(TRUE);
1280 /* Set up other modes */
1281 if (initialMode == AnalyzeFile) {
1282 if (*appData.loadGameFile == NULLCHAR) {
1283 DisplayFatalError(_("AnalyzeFile mode requires a game file"), 0, 1);
1287 if (*appData.loadGameFile != NULLCHAR) {
1288 (void) LoadGameFromFile(appData.loadGameFile,
1289 appData.loadGameIndex,
1290 appData.loadGameFile, TRUE);
1291 } else if (*appData.loadPositionFile != NULLCHAR) {
1292 (void) LoadPositionFromFile(appData.loadPositionFile,
1293 appData.loadPositionIndex,
1294 appData.loadPositionFile);
1295 /* [HGM] try to make self-starting even after FEN load */
1296 /* to allow automatic setup of fairy variants with wtm */
1297 if(initialMode == BeginningOfGame && !blackPlaysFirst) {
1298 gameMode = BeginningOfGame;
1299 setboardSpoiledMachineBlack = 1;
1301 /* [HGM] loadPos: make that every new game uses the setup */
1302 /* from file as long as we do not switch variant */
1303 if(!blackPlaysFirst) {
1304 startedFromPositionFile = TRUE;
1305 CopyBoard(filePosition, boards[0]);
1308 if (initialMode == AnalyzeMode) {
1309 if (appData.noChessProgram) {
1310 DisplayFatalError(_("Analysis mode requires a chess engine"), 0, 2);
1313 if (appData.icsActive) {
1314 DisplayFatalError(_("Analysis mode does not work with ICS mode"),0,2);
1318 } else if (initialMode == AnalyzeFile) {
1319 appData.showThinking = TRUE; // [HGM] thinking: moved out of ShowThinkingEvent
1320 ShowThinkingEvent();
1322 AnalysisPeriodicEvent(1);
1323 } else if (initialMode == MachinePlaysWhite) {
1324 if (appData.noChessProgram) {
1325 DisplayFatalError(_("MachineWhite mode requires a chess engine"),
1329 if (appData.icsActive) {
1330 DisplayFatalError(_("MachineWhite mode does not work with ICS mode"),
1334 MachineWhiteEvent();
1335 } else if (initialMode == MachinePlaysBlack) {
1336 if (appData.noChessProgram) {
1337 DisplayFatalError(_("MachineBlack mode requires a chess engine"),
1341 if (appData.icsActive) {
1342 DisplayFatalError(_("MachineBlack mode does not work with ICS mode"),
1346 MachineBlackEvent();
1347 } else if (initialMode == TwoMachinesPlay) {
1348 if (appData.noChessProgram) {
1349 DisplayFatalError(_("TwoMachines mode requires a chess engine"),
1353 if (appData.icsActive) {
1354 DisplayFatalError(_("TwoMachines mode does not work with ICS mode"),
1359 } else if (initialMode == EditGame) {
1361 } else if (initialMode == EditPosition) {
1362 EditPositionEvent();
1363 } else if (initialMode == Training) {
1364 if (*appData.loadGameFile == NULLCHAR) {
1365 DisplayFatalError(_("Training mode requires a game file"), 0, 2);
1374 * Establish will establish a contact to a remote host.port.
1375 * Sets icsPR to a ProcRef for a process (or pseudo-process)
1376 * used to talk to the host.
1377 * Returns 0 if okay, error code if not.
1384 if (*appData.icsCommPort != NULLCHAR) {
1385 /* Talk to the host through a serial comm port */
1386 return OpenCommPort(appData.icsCommPort, &icsPR);
1388 } else if (*appData.gateway != NULLCHAR) {
1389 if (*appData.remoteShell == NULLCHAR) {
1390 /* Use the rcmd protocol to run telnet program on a gateway host */
1391 snprintf(buf, sizeof(buf), "%s %s %s",
1392 appData.telnetProgram, appData.icsHost, appData.icsPort);
1393 return OpenRcmd(appData.gateway, appData.remoteUser, buf, &icsPR);
1396 /* Use the rsh program to run telnet program on a gateway host */
1397 if (*appData.remoteUser == NULLCHAR) {
1398 snprintf(buf, sizeof(buf), "%s %s %s %s %s", appData.remoteShell,
1399 appData.gateway, appData.telnetProgram,
1400 appData.icsHost, appData.icsPort);
1402 snprintf(buf, sizeof(buf), "%s %s -l %s %s %s %s",
1403 appData.remoteShell, appData.gateway,
1404 appData.remoteUser, appData.telnetProgram,
1405 appData.icsHost, appData.icsPort);
1407 return StartChildProcess(buf, "", &icsPR);
1410 } else if (appData.useTelnet) {
1411 return OpenTelnet(appData.icsHost, appData.icsPort, &icsPR);
1414 /* TCP socket interface differs somewhat between
1415 Unix and NT; handle details in the front end.
1417 return OpenTCP(appData.icsHost, appData.icsPort, &icsPR);
1421 void EscapeExpand(char *p, char *q)
1422 { // [HGM] initstring: routine to shape up string arguments
1423 while(*p++ = *q++) if(p[-1] == '\\')
1425 case 'n': p[-1] = '\n'; break;
1426 case 'r': p[-1] = '\r'; break;
1427 case 't': p[-1] = '\t'; break;
1428 case '\\': p[-1] = '\\'; break;
1429 case 0: *p = 0; return;
1430 default: p[-1] = q[-1]; break;
1435 show_bytes(fp, buf, count)
1441 if (*buf < 040 || *(unsigned char *) buf > 0177) {
1442 fprintf(fp, "\\%03o", *buf & 0xff);
1451 /* Returns an errno value */
1453 OutputMaybeTelnet(pr, message, count, outError)
1459 char buf[8192], *p, *q, *buflim;
1460 int left, newcount, outcount;
1462 if (*appData.icsCommPort != NULLCHAR || appData.useTelnet ||
1463 *appData.gateway != NULLCHAR) {
1464 if (appData.debugMode) {
1465 fprintf(debugFP, ">ICS: ");
1466 show_bytes(debugFP, message, count);
1467 fprintf(debugFP, "\n");
1469 return OutputToProcess(pr, message, count, outError);
1472 buflim = &buf[sizeof(buf)-1]; /* allow 1 byte for expanding last char */
1479 if (appData.debugMode) {
1480 fprintf(debugFP, ">ICS: ");
1481 show_bytes(debugFP, buf, newcount);
1482 fprintf(debugFP, "\n");
1484 outcount = OutputToProcess(pr, buf, newcount, outError);
1485 if (outcount < newcount) return -1; /* to be sure */
1492 } else if (((unsigned char) *p) == TN_IAC) {
1493 *q++ = (char) TN_IAC;
1500 if (appData.debugMode) {
1501 fprintf(debugFP, ">ICS: ");
1502 show_bytes(debugFP, buf, newcount);
1503 fprintf(debugFP, "\n");
1505 outcount = OutputToProcess(pr, buf, newcount, outError);
1506 if (outcount < newcount) return -1; /* to be sure */
1511 read_from_player(isr, closure, message, count, error)
1518 int outError, outCount;
1519 static int gotEof = 0;
1521 /* Pass data read from player on to ICS */
1524 outCount = OutputMaybeTelnet(icsPR, message, count, &outError);
1525 if (outCount < count) {
1526 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1528 } else if (count < 0) {
1529 RemoveInputSource(isr);
1530 DisplayFatalError(_("Error reading from keyboard"), error, 1);
1531 } else if (gotEof++ > 0) {
1532 RemoveInputSource(isr);
1533 DisplayFatalError(_("Got end of file from keyboard"), 0, 0);
1539 { // [HGM] alive: periodically send dummy (date) command to ICS to prevent time-out
1540 if(!connectionAlive) DisplayFatalError("No response from ICS", 0, 1);
1541 connectionAlive = FALSE; // only sticks if no response to 'date' command.
1542 SendToICS("date\n");
1543 if(appData.keepAlive) ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
1546 /* added routine for printf style output to ics */
1547 void ics_printf(char *format, ...)
1549 char buffer[MSG_SIZ];
1552 va_start(args, format);
1553 vsnprintf(buffer, sizeof(buffer), format, args);
1554 buffer[sizeof(buffer)-1] = '\0';
1563 int count, outCount, outError;
1565 if (icsPR == NULL) return;
1568 outCount = OutputMaybeTelnet(icsPR, s, count, &outError);
1569 if (outCount < count) {
1570 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1574 /* This is used for sending logon scripts to the ICS. Sending
1575 without a delay causes problems when using timestamp on ICC
1576 (at least on my machine). */
1578 SendToICSDelayed(s,msdelay)
1582 int count, outCount, outError;
1584 if (icsPR == NULL) return;
1587 if (appData.debugMode) {
1588 fprintf(debugFP, ">ICS: ");
1589 show_bytes(debugFP, s, count);
1590 fprintf(debugFP, "\n");
1592 outCount = OutputToProcessDelayed(icsPR, s, count, &outError,
1594 if (outCount < count) {
1595 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1600 /* Remove all highlighting escape sequences in s
1601 Also deletes any suffix starting with '('
1604 StripHighlightAndTitle(s)
1607 static char retbuf[MSG_SIZ];
1610 while (*s != NULLCHAR) {
1611 while (*s == '\033') {
1612 while (*s != NULLCHAR && !isalpha(*s)) s++;
1613 if (*s != NULLCHAR) s++;
1615 while (*s != NULLCHAR && *s != '\033') {
1616 if (*s == '(' || *s == '[') {
1627 /* Remove all highlighting escape sequences in s */
1632 static char retbuf[MSG_SIZ];
1635 while (*s != NULLCHAR) {
1636 while (*s == '\033') {
1637 while (*s != NULLCHAR && !isalpha(*s)) s++;
1638 if (*s != NULLCHAR) s++;
1640 while (*s != NULLCHAR && *s != '\033') {
1648 char *variantNames[] = VARIANT_NAMES;
1653 return variantNames[v];
1657 /* Identify a variant from the strings the chess servers use or the
1658 PGN Variant tag names we use. */
1665 VariantClass v = VariantNormal;
1666 int i, found = FALSE;
1672 /* [HGM] skip over optional board-size prefixes */
1673 if( sscanf(e, "%dx%d_", &i, &i) == 2 ||
1674 sscanf(e, "%dx%d+%d_", &i, &i, &i) == 3 ) {
1675 while( *e++ != '_');
1678 if(StrCaseStr(e, "misc/")) { // [HGM] on FICS, misc/shogi is not shogi
1682 for (i=0; i<sizeof(variantNames)/sizeof(char*); i++) {
1683 if (StrCaseStr(e, variantNames[i])) {
1684 v = (VariantClass) i;
1691 if ((StrCaseStr(e, "fischer") && StrCaseStr(e, "random"))
1692 || StrCaseStr(e, "wild/fr")
1693 || StrCaseStr(e, "frc") || StrCaseStr(e, "960")) {
1694 v = VariantFischeRandom;
1695 } else if ((i = 4, p = StrCaseStr(e, "wild")) ||
1696 (i = 1, p = StrCaseStr(e, "w"))) {
1698 while (*p && (isspace(*p) || *p == '(' || *p == '/')) p++;
1705 case 0: /* FICS only, actually */
1707 /* Castling legal even if K starts on d-file */
1708 v = VariantWildCastle;
1713 /* Castling illegal even if K & R happen to start in
1714 normal positions. */
1715 v = VariantNoCastle;
1728 /* Castling legal iff K & R start in normal positions */
1734 /* Special wilds for position setup; unclear what to do here */
1735 v = VariantLoadable;
1738 /* Bizarre ICC game */
1739 v = VariantTwoKings;
1742 v = VariantKriegspiel;
1748 v = VariantFischeRandom;
1751 v = VariantCrazyhouse;
1754 v = VariantBughouse;
1760 /* Not quite the same as FICS suicide! */
1761 v = VariantGiveaway;
1767 v = VariantShatranj;
1770 /* Temporary names for future ICC types. The name *will* change in
1771 the next xboard/WinBoard release after ICC defines it. */
1809 v = VariantCapablanca;
1812 v = VariantKnightmate;
1818 v = VariantCylinder;
1824 v = VariantCapaRandom;
1827 v = VariantBerolina;
1839 /* Found "wild" or "w" in the string but no number;
1840 must assume it's normal chess. */
1844 len = snprintf(buf, MSG_SIZ, _("Unknown wild type %d"), wnum);
1845 if( (len > MSG_SIZ) && appData.debugMode )
1846 fprintf(debugFP, "StringToVariant: buffer truncated.\n");
1848 DisplayError(buf, 0);
1854 if (appData.debugMode) {
1855 fprintf(debugFP, _("recognized '%s' (%d) as variant %s\n"),
1856 e, wnum, VariantName(v));
1861 static int leftover_start = 0, leftover_len = 0;
1862 char star_match[STAR_MATCH_N][MSG_SIZ];
1864 /* Test whether pattern is present at &buf[*index]; if so, return TRUE,
1865 advance *index beyond it, and set leftover_start to the new value of
1866 *index; else return FALSE. If pattern contains the character '*', it
1867 matches any sequence of characters not containing '\r', '\n', or the
1868 character following the '*' (if any), and the matched sequence(s) are
1869 copied into star_match.
1872 looking_at(buf, index, pattern)
1877 char *bufp = &buf[*index], *patternp = pattern;
1879 char *matchp = star_match[0];
1882 if (*patternp == NULLCHAR) {
1883 *index = leftover_start = bufp - buf;
1887 if (*bufp == NULLCHAR) return FALSE;
1888 if (*patternp == '*') {
1889 if (*bufp == *(patternp + 1)) {
1891 matchp = star_match[++star_count];
1895 } else if (*bufp == '\n' || *bufp == '\r') {
1897 if (*patternp == NULLCHAR)
1902 *matchp++ = *bufp++;
1906 if (*patternp != *bufp) return FALSE;
1913 SendToPlayer(data, length)
1917 int error, outCount;
1918 outCount = OutputToProcess(NoProc, data, length, &error);
1919 if (outCount < length) {
1920 DisplayFatalError(_("Error writing to display"), error, 1);
1925 PackHolding(packed, holding)
1937 switch (runlength) {
1948 sprintf(q, "%d", runlength);
1960 /* Telnet protocol requests from the front end */
1962 TelnetRequest(ddww, option)
1963 unsigned char ddww, option;
1965 unsigned char msg[3];
1966 int outCount, outError;
1968 if (*appData.icsCommPort != NULLCHAR || appData.useTelnet) return;
1970 if (appData.debugMode) {
1971 char buf1[8], buf2[8], *ddwwStr, *optionStr;
1987 snprintf(buf1,sizeof(buf1)/sizeof(buf1[0]), "%d", ddww);
1996 snprintf(buf2,sizeof(buf2)/sizeof(buf2[0]), "%d", option);
1999 fprintf(debugFP, ">%s %s ", ddwwStr, optionStr);
2004 outCount = OutputToProcess(icsPR, (char *)msg, 3, &outError);
2006 DisplayFatalError(_("Error writing to ICS"), outError, 1);
2013 if (!appData.icsActive) return;
2014 TelnetRequest(TN_DO, TN_ECHO);
2020 if (!appData.icsActive) return;
2021 TelnetRequest(TN_DONT, TN_ECHO);
2025 CopyHoldings(Board board, char *holdings, ChessSquare lowestPiece)
2027 /* put the holdings sent to us by the server on the board holdings area */
2028 int i, j, holdingsColumn, holdingsStartRow, direction, countsColumn;
2032 if(gameInfo.holdingsWidth < 2) return;
2033 if(gameInfo.variant != VariantBughouse && board[HOLDINGS_SET])
2034 return; // prevent overwriting by pre-board holdings
2036 if( (int)lowestPiece >= BlackPawn ) {
2039 holdingsStartRow = BOARD_HEIGHT-1;
2042 holdingsColumn = BOARD_WIDTH-1;
2043 countsColumn = BOARD_WIDTH-2;
2044 holdingsStartRow = 0;
2048 for(i=0; i<BOARD_HEIGHT; i++) { /* clear holdings */
2049 board[i][holdingsColumn] = EmptySquare;
2050 board[i][countsColumn] = (ChessSquare) 0;
2052 while( (p=*holdings++) != NULLCHAR ) {
2053 piece = CharToPiece( ToUpper(p) );
2054 if(piece == EmptySquare) continue;
2055 /*j = (int) piece - (int) WhitePawn;*/
2056 j = PieceToNumber(piece);
2057 if(j >= gameInfo.holdingsSize) continue; /* ignore pieces that do not fit */
2058 if(j < 0) continue; /* should not happen */
2059 piece = (ChessSquare) ( (int)piece + (int)lowestPiece );
2060 board[holdingsStartRow+j*direction][holdingsColumn] = piece;
2061 board[holdingsStartRow+j*direction][countsColumn]++;
2067 VariantSwitch(Board board, VariantClass newVariant)
2069 int newHoldingsWidth, newWidth = 8, newHeight = 8, i, j;
2070 static Board oldBoard;
2072 startedFromPositionFile = FALSE;
2073 if(gameInfo.variant == newVariant) return;
2075 /* [HGM] This routine is called each time an assignment is made to
2076 * gameInfo.variant during a game, to make sure the board sizes
2077 * are set to match the new variant. If that means adding or deleting
2078 * holdings, we shift the playing board accordingly
2079 * This kludge is needed because in ICS observe mode, we get boards
2080 * of an ongoing game without knowing the variant, and learn about the
2081 * latter only later. This can be because of the move list we requested,
2082 * in which case the game history is refilled from the beginning anyway,
2083 * but also when receiving holdings of a crazyhouse game. In the latter
2084 * case we want to add those holdings to the already received position.
2088 if (appData.debugMode) {
2089 fprintf(debugFP, "Switch board from %s to %s\n",
2090 VariantName(gameInfo.variant), VariantName(newVariant));
2091 setbuf(debugFP, NULL);
2093 shuffleOpenings = 0; /* [HGM] shuffle */
2094 gameInfo.holdingsSize = 5; /* [HGM] prepare holdings */
2098 newWidth = 9; newHeight = 9;
2099 gameInfo.holdingsSize = 7;
2100 case VariantBughouse:
2101 case VariantCrazyhouse:
2102 newHoldingsWidth = 2; break;
2106 newHoldingsWidth = 2;
2107 gameInfo.holdingsSize = 8;
2110 case VariantCapablanca:
2111 case VariantCapaRandom:
2114 newHoldingsWidth = gameInfo.holdingsSize = 0;
2117 if(newWidth != gameInfo.boardWidth ||
2118 newHeight != gameInfo.boardHeight ||
2119 newHoldingsWidth != gameInfo.holdingsWidth ) {
2121 /* shift position to new playing area, if needed */
2122 if(newHoldingsWidth > gameInfo.holdingsWidth) {
2123 for(i=0; i<BOARD_HEIGHT; i++)
2124 for(j=BOARD_RGHT-1; j>=BOARD_LEFT; j--)
2125 board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
2127 for(i=0; i<newHeight; i++) {
2128 board[i][0] = board[i][newWidth+2*newHoldingsWidth-1] = EmptySquare;
2129 board[i][1] = board[i][newWidth+2*newHoldingsWidth-2] = (ChessSquare) 0;
2131 } else if(newHoldingsWidth < gameInfo.holdingsWidth) {
2132 for(i=0; i<BOARD_HEIGHT; i++)
2133 for(j=BOARD_LEFT; j<BOARD_RGHT; j++)
2134 board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
2137 gameInfo.boardWidth = newWidth;
2138 gameInfo.boardHeight = newHeight;
2139 gameInfo.holdingsWidth = newHoldingsWidth;
2140 gameInfo.variant = newVariant;
2141 InitDrawingSizes(-2, 0);
2142 } else gameInfo.variant = newVariant;
2143 CopyBoard(oldBoard, board); // remember correctly formatted board
2144 InitPosition(FALSE); /* this sets up board[0], but also other stuff */
2145 DrawPosition(TRUE, currentMove ? boards[currentMove] : oldBoard);
2148 static int loggedOn = FALSE;
2150 /*-- Game start info cache: --*/
2152 char gs_kind[MSG_SIZ];
2153 static char player1Name[128] = "";
2154 static char player2Name[128] = "";
2155 static char cont_seq[] = "\n\\ ";
2156 static int player1Rating = -1;
2157 static int player2Rating = -1;
2158 /*----------------------------*/
2160 ColorClass curColor = ColorNormal;
2161 int suppressKibitz = 0;
2164 Boolean soughtPending = FALSE;
2165 Boolean seekGraphUp;
2166 #define MAX_SEEK_ADS 200
2168 char *seekAdList[MAX_SEEK_ADS];
2169 int ratingList[MAX_SEEK_ADS], xList[MAX_SEEK_ADS], yList[MAX_SEEK_ADS], seekNrList[MAX_SEEK_ADS], zList[MAX_SEEK_ADS];
2170 float tcList[MAX_SEEK_ADS];
2171 char colorList[MAX_SEEK_ADS];
2172 int nrOfSeekAds = 0;
2173 int minRating = 1010, maxRating = 2800;
2174 int hMargin = 10, vMargin = 20, h, w;
2175 extern int squareSize, lineGap;
2180 int x, y, color = 0, r = ratingList[i]; float tc = tcList[i];
2181 xList[i] = yList[i] = -100; // outside graph, so cannot be clicked
2182 if(r < minRating+100 && r >=0 ) r = minRating+100;
2183 if(r > maxRating) r = maxRating;
2184 if(tc < 1.) tc = 1.;
2185 if(tc > 95.) tc = 95.;
2186 x = (w-hMargin-squareSize/8-7)* log(tc)/log(95.) + hMargin;
2187 y = ((double)r - minRating)/(maxRating - minRating)
2188 * (h-vMargin-squareSize/8-1) + vMargin;
2189 if(ratingList[i] < 0) y = vMargin + squareSize/4;
2190 if(strstr(seekAdList[i], " u ")) color = 1;
2191 if(!strstr(seekAdList[i], "lightning") && // for now all wilds same color
2192 !strstr(seekAdList[i], "bullet") &&
2193 !strstr(seekAdList[i], "blitz") &&
2194 !strstr(seekAdList[i], "standard") ) color = 2;
2195 if(strstr(seekAdList[i], "(C) ")) color |= SQUARE; // plot computer seeks as squares
2196 DrawSeekDot(xList[i]=x+3*(color&~SQUARE), yList[i]=h-1-y, colorList[i]=color);
2200 AddAd(char *handle, char *rating, int base, int inc, char rated, char *type, int nr, Boolean plot)
2202 char buf[MSG_SIZ], *ext = "";
2203 VariantClass v = StringToVariant(type);
2204 if(strstr(type, "wild")) {
2205 ext = type + 4; // append wild number
2206 if(v == VariantFischeRandom) type = "chess960"; else
2207 if(v == VariantLoadable) type = "setup"; else
2208 type = VariantName(v);
2210 snprintf(buf, MSG_SIZ, "%s (%s) %d %d %c %s%s", handle, rating, base, inc, rated, type, ext);
2211 if(nrOfSeekAds < MAX_SEEK_ADS-1) {
2212 if(seekAdList[nrOfSeekAds]) free(seekAdList[nrOfSeekAds]);
2213 ratingList[nrOfSeekAds] = -1; // for if seeker has no rating
2214 sscanf(rating, "%d", &ratingList[nrOfSeekAds]);
2215 tcList[nrOfSeekAds] = base + (2./3.)*inc;
2216 seekNrList[nrOfSeekAds] = nr;
2217 zList[nrOfSeekAds] = 0;
2218 seekAdList[nrOfSeekAds++] = StrSave(buf);
2219 if(plot) PlotSeekAd(nrOfSeekAds-1);
2226 int x = xList[i], y = yList[i], d=squareSize/4, k;
2227 DrawSeekBackground(x-squareSize/8, y-squareSize/8, x+squareSize/8+1, y+squareSize/8+1);
2228 if(x < hMargin+d) DrawSeekAxis(hMargin, y-squareSize/8, hMargin, y+squareSize/8+1);
2229 // now replot every dot that overlapped
2230 for(k=0; k<nrOfSeekAds; k++) if(k != i) {
2231 int xx = xList[k], yy = yList[k];
2232 if(xx <= x+d && xx > x-d && yy <= y+d && yy > y-d)
2233 DrawSeekDot(xx, yy, colorList[k]);
2238 RemoveSeekAd(int nr)
2241 for(i=0; i<nrOfSeekAds; i++) if(seekNrList[i] == nr) {
2243 if(seekAdList[i]) free(seekAdList[i]);
2244 seekAdList[i] = seekAdList[--nrOfSeekAds];
2245 seekNrList[i] = seekNrList[nrOfSeekAds];
2246 ratingList[i] = ratingList[nrOfSeekAds];
2247 colorList[i] = colorList[nrOfSeekAds];
2248 tcList[i] = tcList[nrOfSeekAds];
2249 xList[i] = xList[nrOfSeekAds];
2250 yList[i] = yList[nrOfSeekAds];
2251 zList[i] = zList[nrOfSeekAds];
2252 seekAdList[nrOfSeekAds] = NULL;
2258 MatchSoughtLine(char *line)
2260 char handle[MSG_SIZ], rating[MSG_SIZ], type[MSG_SIZ];
2261 int nr, base, inc, u=0; char dummy;
2263 if(sscanf(line, "%d %s %s %d %d rated %s", &nr, rating, handle, &base, &inc, type) == 6 ||
2264 sscanf(line, "%d %s %s %s %d %d rated %c", &nr, rating, handle, type, &base, &inc, &dummy) == 7 ||
2266 (sscanf(line, "%d %s %s %d %d unrated %s", &nr, rating, handle, &base, &inc, type) == 6 ||
2267 sscanf(line, "%d %s %s %s %d %d unrated %c", &nr, rating, handle, type, &base, &inc, &dummy) == 7) ) {
2268 // match: compact and save the line
2269 AddAd(handle, rating, base, inc, u ? 'u' : 'r', type, nr, FALSE);
2279 if(!seekGraphUp) return FALSE;
2280 h = BOARD_HEIGHT * (squareSize + lineGap) + lineGap;
2281 w = BOARD_WIDTH * (squareSize + lineGap) + lineGap;
2283 DrawSeekBackground(0, 0, w, h);
2284 DrawSeekAxis(hMargin, h-1-vMargin, w-5, h-1-vMargin);
2285 DrawSeekAxis(hMargin, h-1-vMargin, hMargin, 5);
2286 for(i=0; i<4000; i+= 100) if(i>=minRating && i<maxRating) {
2287 int yy =((double)i - minRating)/(maxRating - minRating)*(h-vMargin-squareSize/8-1) + vMargin;
2289 DrawSeekAxis(hMargin+5*(i%500==0), yy, hMargin-5, yy); // rating ticks
2292 snprintf(buf, MSG_SIZ, "%d", i);
2293 DrawSeekText(buf, hMargin+squareSize/8+7, yy);
2296 DrawSeekText("unrated", hMargin+squareSize/8+7, h-1-vMargin-squareSize/4);
2297 for(i=1; i<100; i+=(i<10?1:5)) {
2298 int xx = (w-hMargin-squareSize/8-7)* log((double)i)/log(95.) + hMargin;
2299 DrawSeekAxis(xx, h-1-vMargin, xx, h-6-vMargin-3*(i%10==0)); // TC ticks
2300 if(i<=5 || (i>40 ? i%20 : i%10) == 0) {
2302 snprintf(buf, MSG_SIZ, "%d", i);
2303 DrawSeekText(buf, xx-2-3*(i>9), h-1-vMargin/2);
2306 for(i=0; i<nrOfSeekAds; i++) PlotSeekAd(i);
2310 int SeekGraphClick(ClickType click, int x, int y, int moving)
2312 static int lastDown = 0, displayed = 0, lastSecond;
2313 if(!seekGraphUp) { // initiate cration of seek graph by requesting seek-ad list
2314 if(click == Release || moving) return FALSE;
2316 soughtPending = TRUE;
2317 SendToICS(ics_prefix);
2318 SendToICS("sought\n"); // should this be "sought all"?
2319 } else { // issue challenge based on clicked ad
2320 int dist = 10000; int i, closest = 0, second = 0;
2321 for(i=0; i<nrOfSeekAds; i++) {
2322 int d = (x-xList[i])*(x-xList[i]) + (y-yList[i])*(y-yList[i]) + zList[i];
2323 if(d < dist) { dist = d; closest = i; }
2324 second += (d - zList[i] < 120); // count in-range ads
2325 if(click == Press && moving != 1 && zList[i]>0) zList[i] *= 0.8; // age priority
2329 second = (second > 1);
2330 if(displayed != closest || second != lastSecond) {
2331 DisplayMessage(second ? "!" : "", seekAdList[closest]);
2332 lastSecond = second; displayed = closest;
2334 if(click == Press) {
2335 if(moving == 2) zList[closest] = 100; // right-click; push to back on press
2338 } // on press 'hit', only show info
2339 if(moving == 2) return TRUE; // ignore right up-clicks on dot
2340 snprintf(buf, MSG_SIZ, "play %d\n", seekNrList[closest]);
2341 SendToICS(ics_prefix);
2343 return TRUE; // let incoming board of started game pop down the graph
2344 } else if(click == Release) { // release 'miss' is ignored
2345 zList[lastDown] = 100; // make future selection of the rejected ad more difficult
2346 if(moving == 2) { // right up-click
2347 nrOfSeekAds = 0; // refresh graph
2348 soughtPending = TRUE;
2349 SendToICS(ics_prefix);
2350 SendToICS("sought\n"); // should this be "sought all"?
2353 } else if(moving) { if(displayed >= 0) DisplayMessage("", ""); displayed = -1; return TRUE; }
2354 // press miss or release hit 'pop down' seek graph
2355 seekGraphUp = FALSE;
2356 DrawPosition(TRUE, NULL);
2362 read_from_ics(isr, closure, data, count, error)
2369 #define BUF_SIZE (16*1024) /* overflowed at 8K with "inchannel 1" on FICS? */
2370 #define STARTED_NONE 0
2371 #define STARTED_MOVES 1
2372 #define STARTED_BOARD 2
2373 #define STARTED_OBSERVE 3
2374 #define STARTED_HOLDINGS 4
2375 #define STARTED_CHATTER 5
2376 #define STARTED_COMMENT 6
2377 #define STARTED_MOVES_NOHIDE 7
2379 static int started = STARTED_NONE;
2380 static char parse[20000];
2381 static int parse_pos = 0;
2382 static char buf[BUF_SIZE + 1];
2383 static int firstTime = TRUE, intfSet = FALSE;
2384 static ColorClass prevColor = ColorNormal;
2385 static int savingComment = FALSE;
2386 static int cmatch = 0; // continuation sequence match
2393 int backup; /* [DM] For zippy color lines */
2395 char talker[MSG_SIZ]; // [HGM] chat
2398 connectionAlive = TRUE; // [HGM] alive: I think, therefore I am...
2400 if (appData.debugMode) {
2402 fprintf(debugFP, "<ICS: ");
2403 show_bytes(debugFP, data, count);
2404 fprintf(debugFP, "\n");
2408 if (appData.debugMode) { int f = forwardMostMove;
2409 fprintf(debugFP, "ics input %d, castling = %d %d %d %d %d %d\n", f,
2410 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
2411 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
2414 /* If last read ended with a partial line that we couldn't parse,
2415 prepend it to the new read and try again. */
2416 if (leftover_len > 0) {
2417 for (i=0; i<leftover_len; i++)
2418 buf[i] = buf[leftover_start + i];
2421 /* copy new characters into the buffer */
2422 bp = buf + leftover_len;
2423 buf_len=leftover_len;
2424 for (i=0; i<count; i++)
2427 if (data[i] == '\r')
2430 // join lines split by ICS?
2431 if (!appData.noJoin)
2434 Joining just consists of finding matches against the
2435 continuation sequence, and discarding that sequence
2436 if found instead of copying it. So, until a match
2437 fails, there's nothing to do since it might be the
2438 complete sequence, and thus, something we don't want
2441 if (data[i] == cont_seq[cmatch])
2444 if (cmatch == strlen(cont_seq))
2446 cmatch = 0; // complete match. just reset the counter
2449 it's possible for the ICS to not include the space
2450 at the end of the last word, making our [correct]
2451 join operation fuse two separate words. the server
2452 does this when the space occurs at the width setting.
2454 if (!buf_len || buf[buf_len-1] != ' ')
2465 match failed, so we have to copy what matched before
2466 falling through and copying this character. In reality,
2467 this will only ever be just the newline character, but
2468 it doesn't hurt to be precise.
2470 strncpy(bp, cont_seq, cmatch);
2482 buf[buf_len] = NULLCHAR;
2483 // next_out = leftover_len; // [HGM] should we set this to 0, and not print it in advance?
2488 while (i < buf_len) {
2489 /* Deal with part of the TELNET option negotiation
2490 protocol. We refuse to do anything beyond the
2491 defaults, except that we allow the WILL ECHO option,
2492 which ICS uses to turn off password echoing when we are
2493 directly connected to it. We reject this option
2494 if localLineEditing mode is on (always on in xboard)
2495 and we are talking to port 23, which might be a real
2496 telnet server that will try to keep WILL ECHO on permanently.
2498 if (buf_len - i >= 3 && (unsigned char) buf[i] == TN_IAC) {
2499 static int remoteEchoOption = FALSE; /* telnet ECHO option */
2500 unsigned char option;
2502 switch ((unsigned char) buf[++i]) {
2504 if (appData.debugMode)
2505 fprintf(debugFP, "\n<WILL ");
2506 switch (option = (unsigned char) buf[++i]) {
2508 if (appData.debugMode)
2509 fprintf(debugFP, "ECHO ");
2510 /* Reply only if this is a change, according
2511 to the protocol rules. */
2512 if (remoteEchoOption) break;
2513 if (appData.localLineEditing &&
2514 atoi(appData.icsPort) == TN_PORT) {
2515 TelnetRequest(TN_DONT, TN_ECHO);
2518 TelnetRequest(TN_DO, TN_ECHO);
2519 remoteEchoOption = TRUE;
2523 if (appData.debugMode)
2524 fprintf(debugFP, "%d ", option);
2525 /* Whatever this is, we don't want it. */
2526 TelnetRequest(TN_DONT, option);
2531 if (appData.debugMode)
2532 fprintf(debugFP, "\n<WONT ");
2533 switch (option = (unsigned char) buf[++i]) {
2535 if (appData.debugMode)
2536 fprintf(debugFP, "ECHO ");
2537 /* Reply only if this is a change, according
2538 to the protocol rules. */
2539 if (!remoteEchoOption) break;
2541 TelnetRequest(TN_DONT, TN_ECHO);
2542 remoteEchoOption = FALSE;
2545 if (appData.debugMode)
2546 fprintf(debugFP, "%d ", (unsigned char) option);
2547 /* Whatever this is, it must already be turned
2548 off, because we never agree to turn on
2549 anything non-default, so according to the
2550 protocol rules, we don't reply. */
2555 if (appData.debugMode)
2556 fprintf(debugFP, "\n<DO ");
2557 switch (option = (unsigned char) buf[++i]) {
2559 /* Whatever this is, we refuse to do it. */
2560 if (appData.debugMode)
2561 fprintf(debugFP, "%d ", option);
2562 TelnetRequest(TN_WONT, option);
2567 if (appData.debugMode)
2568 fprintf(debugFP, "\n<DONT ");
2569 switch (option = (unsigned char) buf[++i]) {
2571 if (appData.debugMode)
2572 fprintf(debugFP, "%d ", option);
2573 /* Whatever this is, we are already not doing
2574 it, because we never agree to do anything
2575 non-default, so according to the protocol
2576 rules, we don't reply. */
2581 if (appData.debugMode)
2582 fprintf(debugFP, "\n<IAC ");
2583 /* Doubled IAC; pass it through */
2587 if (appData.debugMode)
2588 fprintf(debugFP, "\n<%d ", (unsigned char) buf[i]);
2589 /* Drop all other telnet commands on the floor */
2592 if (oldi > next_out)
2593 SendToPlayer(&buf[next_out], oldi - next_out);
2599 /* OK, this at least will *usually* work */
2600 if (!loggedOn && looking_at(buf, &i, "ics%")) {
2604 if (loggedOn && !intfSet) {
2605 if (ics_type == ICS_ICC) {
2606 snprintf(str, MSG_SIZ,
2607 "/set-quietly interface %s\n/set-quietly style 12\n",
2609 if(appData.seekGraph && appData.autoRefresh) // [HGM] seekgraph
2610 strcat(str, "/set-2 51 1\n/set seek 1\n");
2611 } else if (ics_type == ICS_CHESSNET) {
2612 snprintf(str, MSG_SIZ, "/style 12\n");
2614 safeStrCpy(str, "alias $ @\n$set interface ", sizeof(str)/sizeof(str[0]));
2615 strcat(str, programVersion);
2616 strcat(str, "\n$iset startpos 1\n$iset ms 1\n");
2617 if(appData.seekGraph && appData.autoRefresh) // [HGM] seekgraph
2618 strcat(str, "$iset seekremove 1\n$set seek 1\n");
2620 strcat(str, "$iset nohighlight 1\n");
2622 strcat(str, "$iset lock 1\n$style 12\n");
2625 NotifyFrontendLogin();
2629 if (started == STARTED_COMMENT) {
2630 /* Accumulate characters in comment */
2631 parse[parse_pos++] = buf[i];
2632 if (buf[i] == '\n') {
2633 parse[parse_pos] = NULLCHAR;
2634 if(chattingPartner>=0) {
2636 snprintf(mess, MSG_SIZ, "%s%s", talker, parse);
2637 OutputChatMessage(chattingPartner, mess);
2638 chattingPartner = -1;
2639 next_out = i+1; // [HGM] suppress printing in ICS window
2641 if(!suppressKibitz) // [HGM] kibitz
2642 AppendComment(forwardMostMove, StripHighlight(parse), TRUE);
2643 else { // [HGM kibitz: divert memorized engine kibitz to engine-output window
2644 int nrDigit = 0, nrAlph = 0, j;
2645 if(parse_pos > MSG_SIZ - 30) // defuse unreasonably long input
2646 { parse_pos = MSG_SIZ-30; parse[parse_pos - 1] = '\n'; }
2647 parse[parse_pos] = NULLCHAR;
2648 // try to be smart: if it does not look like search info, it should go to
2649 // ICS interaction window after all, not to engine-output window.
2650 for(j=0; j<parse_pos; j++) { // count letters and digits
2651 nrDigit += (parse[j] >= '0' && parse[j] <= '9');
2652 nrAlph += (parse[j] >= 'a' && parse[j] <= 'z');
2653 nrAlph += (parse[j] >= 'A' && parse[j] <= 'Z');
2655 if(nrAlph < 9*nrDigit) { // if more than 10% digit we assume search info
2656 int depth=0; float score;
2657 if(sscanf(parse, "!!! %f/%d", &score, &depth) == 2 && depth>0) {
2658 // [HGM] kibitz: save kibitzed opponent info for PGN and eval graph
2659 pvInfoList[forwardMostMove-1].depth = depth;
2660 pvInfoList[forwardMostMove-1].score = 100*score;
2662 OutputKibitz(suppressKibitz, parse);
2665 snprintf(tmp, MSG_SIZ, _("your opponent kibitzes: %s"), parse);
2666 SendToPlayer(tmp, strlen(tmp));
2668 next_out = i+1; // [HGM] suppress printing in ICS window
2670 started = STARTED_NONE;
2672 /* Don't match patterns against characters in comment */
2677 if (started == STARTED_CHATTER) {
2678 if (buf[i] != '\n') {
2679 /* Don't match patterns against characters in chatter */
2683 started = STARTED_NONE;
2684 if(suppressKibitz) next_out = i+1;
2687 /* Kludge to deal with rcmd protocol */
2688 if (firstTime && looking_at(buf, &i, "\001*")) {
2689 DisplayFatalError(&buf[1], 0, 1);
2695 if (!loggedOn && looking_at(buf, &i, "chessclub.com")) {
2698 if (appData.debugMode)
2699 fprintf(debugFP, "ics_type %d\n", ics_type);
2702 if (!loggedOn && looking_at(buf, &i, "freechess.org")) {
2703 ics_type = ICS_FICS;
2705 if (appData.debugMode)
2706 fprintf(debugFP, "ics_type %d\n", ics_type);
2709 if (!loggedOn && looking_at(buf, &i, "chess.net")) {
2710 ics_type = ICS_CHESSNET;
2712 if (appData.debugMode)
2713 fprintf(debugFP, "ics_type %d\n", ics_type);
2718 (looking_at(buf, &i, "\"*\" is *a registered name") ||
2719 looking_at(buf, &i, "Logging you in as \"*\"") ||
2720 looking_at(buf, &i, "will be \"*\""))) {
2721 safeStrCpy(ics_handle, star_match[0], sizeof(ics_handle)/sizeof(ics_handle[0]));
2725 if (loggedOn && !have_set_title && ics_handle[0] != NULLCHAR) {
2727 snprintf(buf, sizeof(buf), "%s@%s", ics_handle, appData.icsHost);
2728 DisplayIcsInteractionTitle(buf);
2729 have_set_title = TRUE;
2732 /* skip finger notes */
2733 if (started == STARTED_NONE &&
2734 ((buf[i] == ' ' && isdigit(buf[i+1])) ||
2735 (buf[i] == '1' && buf[i+1] == '0')) &&
2736 buf[i+2] == ':' && buf[i+3] == ' ') {
2737 started = STARTED_CHATTER;
2743 // [HGM] seekgraph: recognize sought lines and end-of-sought message
2744 if(appData.seekGraph) {
2745 if(soughtPending && MatchSoughtLine(buf+i)) {
2746 i = strstr(buf+i, "rated") - buf;
2747 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2748 next_out = leftover_start = i;
2749 started = STARTED_CHATTER;
2750 suppressKibitz = TRUE;
2753 if((gameMode == IcsIdle || gameMode == BeginningOfGame)
2754 && looking_at(buf, &i, "* ads displayed")) {
2755 soughtPending = FALSE;
2760 if(appData.autoRefresh) {
2761 if(looking_at(buf, &i, "* (*) seeking * * * * *\"play *\" to respond)\n")) {
2762 int s = (ics_type == ICS_ICC); // ICC format differs
2764 AddAd(star_match[0], star_match[1], atoi(star_match[2+s]), atoi(star_match[3+s]),
2765 star_match[4+s][0], star_match[5-3*s], atoi(star_match[7]), TRUE);
2766 looking_at(buf, &i, "*% "); // eat prompt
2767 if(oldi > 0 && buf[oldi-1] == '\n') oldi--; // suppress preceding LF, if any
2768 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2769 next_out = i; // suppress
2772 if(looking_at(buf, &i, "\nAds removed: *\n") || looking_at(buf, &i, "\031(51 * *\031)")) {
2773 char *p = star_match[0];
2775 if(seekGraphUp) RemoveSeekAd(atoi(p));
2776 while(*p && *p++ != ' '); // next
2778 looking_at(buf, &i, "*% "); // eat prompt
2779 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2786 /* skip formula vars */
2787 if (started == STARTED_NONE &&
2788 buf[i] == 'f' && isdigit(buf[i+1]) && buf[i+2] == ':') {
2789 started = STARTED_CHATTER;
2794 // [HGM] kibitz: try to recognize opponent engine-score kibitzes, to divert them to engine-output window
2795 if (appData.autoKibitz && started == STARTED_NONE &&
2796 !appData.icsEngineAnalyze && // [HGM] [DM] ICS analyze
2797 (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack || gameMode == IcsObserving)) {
2798 if((looking_at(buf, &i, "* kibitzes: ") || looking_at(buf, &i, "* whispers: ")) &&
2799 (StrStr(star_match[0], gameInfo.white) == star_match[0] ||
2800 StrStr(star_match[0], gameInfo.black) == star_match[0] )) { // kibitz of self or opponent
2801 suppressKibitz = TRUE;
2802 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2804 if((StrStr(star_match[0], gameInfo.white) == star_match[0]
2805 && (gameMode == IcsPlayingWhite)) ||
2806 (StrStr(star_match[0], gameInfo.black) == star_match[0]
2807 && (gameMode == IcsPlayingBlack)) ) // opponent kibitz
2808 started = STARTED_CHATTER; // own kibitz we simply discard
2810 started = STARTED_COMMENT; // make sure it will be collected in parse[]
2811 parse_pos = 0; parse[0] = NULLCHAR;
2812 savingComment = TRUE;
2813 suppressKibitz = gameMode != IcsObserving ? 2 :
2814 (StrStr(star_match[0], gameInfo.white) == NULL) + 1;
2818 if((looking_at(buf, &i, "\nkibitzed to *\n") || looking_at(buf, &i, "kibitzed to *\n") ||
2819 looking_at(buf, &i, "\n(kibitzed to *\n") || looking_at(buf, &i, "(kibitzed to *\n"))
2820 && atoi(star_match[0])) {
2821 // suppress the acknowledgements of our own autoKibitz
2823 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2824 if(p = strchr(star_match[0], ' ')) p[1] = NULLCHAR; // clip off "players)" on FICS
2825 SendToPlayer(star_match[0], strlen(star_match[0]));
2826 if(looking_at(buf, &i, "*% ")) // eat prompt
2827 suppressKibitz = FALSE;
2831 } // [HGM] kibitz: end of patch
2833 // [HGM] chat: intercept tells by users for which we have an open chat window
2835 if(started == STARTED_NONE && (looking_at(buf, &i, "* tells you:") || looking_at(buf, &i, "* says:") ||
2836 looking_at(buf, &i, "* whispers:") ||
2837 looking_at(buf, &i, "* kibitzes:") ||
2838 looking_at(buf, &i, "* shouts:") ||
2839 looking_at(buf, &i, "* c-shouts:") ||
2840 looking_at(buf, &i, "--> * ") ||
2841 looking_at(buf, &i, "*(*):") && (sscanf(star_match[1], "%d", &channel),1) ||
2842 looking_at(buf, &i, "*(*)(*):") && (sscanf(star_match[2], "%d", &channel),1) ||
2843 looking_at(buf, &i, "*(*)(*)(*):") && (sscanf(star_match[3], "%d", &channel),1) ||
2844 looking_at(buf, &i, "*(*)(*)(*)(*):") && sscanf(star_match[4], "%d", &channel) == 1 )) {
2846 sscanf(star_match[0], "%[^(]", talker+1); // strip (C) or (U) off ICS handle
2847 chattingPartner = -1;
2849 if(channel >= 0) // channel broadcast; look if there is a chatbox for this channel
2850 for(p=0; p<MAX_CHAT; p++) {
2851 if(channel == atoi(chatPartner[p])) {
2852 talker[0] = '['; strcat(talker, "] ");
2853 Colorize(channel == 1 ? ColorChannel1 : ColorChannel, FALSE);
2854 chattingPartner = p; break;
2857 if(buf[i-3] == 'e') // kibitz; look if there is a KIBITZ chatbox
2858 for(p=0; p<MAX_CHAT; p++) {
2859 if(!strcmp("kibitzes", chatPartner[p])) {
2860 talker[0] = '['; strcat(talker, "] ");
2861 chattingPartner = p; break;
2864 if(buf[i-3] == 'r') // whisper; look if there is a WHISPER chatbox
2865 for(p=0; p<MAX_CHAT; p++) {
2866 if(!strcmp("whispers", chatPartner[p])) {
2867 talker[0] = '['; strcat(talker, "] ");
2868 chattingPartner = p; break;
2871 if(buf[i-3] == 't' || buf[oldi+2] == '>') {// shout, c-shout or it; look if there is a 'shouts' chatbox
2872 if(buf[i-8] == '-' && buf[i-3] == 't')
2873 for(p=0; p<MAX_CHAT; p++) { // c-shout; check if dedicatesd c-shout box exists
2874 if(!strcmp("c-shouts", chatPartner[p])) {
2875 talker[0] = '('; strcat(talker, ") "); Colorize(ColorSShout, FALSE);
2876 chattingPartner = p; break;
2879 if(chattingPartner < 0)
2880 for(p=0; p<MAX_CHAT; p++) {
2881 if(!strcmp("shouts", chatPartner[p])) {
2882 if(buf[oldi+2] == '>') { talker[0] = '<'; strcat(talker, "> "); Colorize(ColorShout, FALSE); }
2883 else if(buf[i-8] == '-') { talker[0] = '('; strcat(talker, ") "); Colorize(ColorSShout, FALSE); }
2884 else { talker[0] = '['; strcat(talker, "] "); Colorize(ColorShout, FALSE); }
2885 chattingPartner = p; break;
2889 if(chattingPartner<0) // if not, look if there is a chatbox for this indivdual
2890 for(p=0; p<MAX_CHAT; p++) if(!StrCaseCmp(talker+1, chatPartner[p])) {
2891 talker[0] = 0; Colorize(ColorTell, FALSE);
2892 chattingPartner = p; break;
2894 if(chattingPartner<0) i = oldi; else {
2895 Colorize(curColor, TRUE); // undo the bogus colorations we just made to trigger the souds
2896 if(oldi > 0 && buf[oldi-1] == '\n') oldi--;
2897 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2898 started = STARTED_COMMENT;
2899 parse_pos = 0; parse[0] = NULLCHAR;
2900 savingComment = 3 + chattingPartner; // counts as TRUE
2901 suppressKibitz = TRUE;
2904 } // [HGM] chat: end of patch
2906 if (appData.zippyTalk || appData.zippyPlay) {
2907 /* [DM] Backup address for color zippy lines */
2910 if (loggedOn == TRUE)
2911 if (ZippyControl(buf, &backup) || ZippyConverse(buf, &backup) ||
2912 (appData.zippyPlay && ZippyMatch(buf, &backup)));
2914 } // [DM] 'else { ' deleted
2916 /* Regular tells and says */
2917 (tkind = 1, looking_at(buf, &i, "* tells you: ")) ||
2918 looking_at(buf, &i, "* (your partner) tells you: ") ||
2919 looking_at(buf, &i, "* says: ") ||
2920 /* Don't color "message" or "messages" output */
2921 (tkind = 5, looking_at(buf, &i, "*. * (*:*): ")) ||
2922 looking_at(buf, &i, "*. * at *:*: ") ||
2923 looking_at(buf, &i, "--* (*:*): ") ||
2924 /* Message notifications (same color as tells) */
2925 looking_at(buf, &i, "* has left a message ") ||
2926 looking_at(buf, &i, "* just sent you a message:\n") ||
2927 /* Whispers and kibitzes */
2928 (tkind = 2, looking_at(buf, &i, "* whispers: ")) ||
2929 looking_at(buf, &i, "* kibitzes: ") ||
2931 (tkind = 3, looking_at(buf, &i, "*(*: "))) {
2933 if (tkind == 1 && strchr(star_match[0], ':')) {
2934 /* Avoid "tells you:" spoofs in channels */
2937 if (star_match[0][0] == NULLCHAR ||
2938 strchr(star_match[0], ' ') ||
2939 (tkind == 3 && strchr(star_match[1], ' '))) {
2940 /* Reject bogus matches */
2943 if (appData.colorize) {
2944 if (oldi > next_out) {
2945 SendToPlayer(&buf[next_out], oldi - next_out);
2950 Colorize(ColorTell, FALSE);
2951 curColor = ColorTell;
2954 Colorize(ColorKibitz, FALSE);
2955 curColor = ColorKibitz;
2958 p = strrchr(star_match[1], '(');
2965 Colorize(ColorChannel1, FALSE);
2966 curColor = ColorChannel1;
2968 Colorize(ColorChannel, FALSE);
2969 curColor = ColorChannel;
2973 curColor = ColorNormal;
2977 if (started == STARTED_NONE && appData.autoComment &&
2978 (gameMode == IcsObserving ||
2979 gameMode == IcsPlayingWhite ||
2980 gameMode == IcsPlayingBlack)) {
2981 parse_pos = i - oldi;
2982 memcpy(parse, &buf[oldi], parse_pos);
2983 parse[parse_pos] = NULLCHAR;
2984 started = STARTED_COMMENT;
2985 savingComment = TRUE;
2987 started = STARTED_CHATTER;
2988 savingComment = FALSE;
2995 if (looking_at(buf, &i, "* s-shouts: ") ||
2996 looking_at(buf, &i, "* c-shouts: ")) {
2997 if (appData.colorize) {
2998 if (oldi > next_out) {
2999 SendToPlayer(&buf[next_out], oldi - next_out);
3002 Colorize(ColorSShout, FALSE);
3003 curColor = ColorSShout;
3006 started = STARTED_CHATTER;
3010 if (looking_at(buf, &i, "--->")) {
3015 if (looking_at(buf, &i, "* shouts: ") ||
3016 looking_at(buf, &i, "--> ")) {
3017 if (appData.colorize) {
3018 if (oldi > next_out) {
3019 SendToPlayer(&buf[next_out], oldi - next_out);
3022 Colorize(ColorShout, FALSE);
3023 curColor = ColorShout;
3026 started = STARTED_CHATTER;
3030 if (looking_at( buf, &i, "Challenge:")) {
3031 if (appData.colorize) {
3032 if (oldi > next_out) {
3033 SendToPlayer(&buf[next_out], oldi - next_out);
3036 Colorize(ColorChallenge, FALSE);
3037 curColor = ColorChallenge;
3043 if (looking_at(buf, &i, "* offers you") ||
3044 looking_at(buf, &i, "* offers to be") ||
3045 looking_at(buf, &i, "* would like to") ||
3046 looking_at(buf, &i, "* requests to") ||
3047 looking_at(buf, &i, "Your opponent offers") ||
3048 looking_at(buf, &i, "Your opponent requests")) {
3050 if (appData.colorize) {
3051 if (oldi > next_out) {
3052 SendToPlayer(&buf[next_out], oldi - next_out);
3055 Colorize(ColorRequest, FALSE);
3056 curColor = ColorRequest;
3061 if (looking_at(buf, &i, "* (*) seeking")) {
3062 if (appData.colorize) {
3063 if (oldi > next_out) {
3064 SendToPlayer(&buf[next_out], oldi - next_out);
3067 Colorize(ColorSeek, FALSE);
3068 curColor = ColorSeek;
3073 if (looking_at(buf, &i, "\\ ")) {
3074 if (prevColor != ColorNormal) {
3075 if (oldi > next_out) {
3076 SendToPlayer(&buf[next_out], oldi - next_out);
3079 Colorize(prevColor, TRUE);
3080 curColor = prevColor;
3082 if (savingComment) {
3083 parse_pos = i - oldi;
3084 memcpy(parse, &buf[oldi], parse_pos);
3085 parse[parse_pos] = NULLCHAR;
3086 started = STARTED_COMMENT;
3087 if(savingComment >= 3) // [HGM] chat: continuation of line for chat box
3088 chattingPartner = savingComment - 3; // kludge to remember the box
3090 started = STARTED_CHATTER;
3095 if (looking_at(buf, &i, "Black Strength :") ||
3096 looking_at(buf, &i, "<<< style 10 board >>>") ||
3097 looking_at(buf, &i, "<10>") ||
3098 looking_at(buf, &i, "#@#")) {
3099 /* Wrong board style */
3101 SendToICS(ics_prefix);
3102 SendToICS("set style 12\n");
3103 SendToICS(ics_prefix);
3104 SendToICS("refresh\n");
3108 if (!have_sent_ICS_logon && looking_at(buf, &i, "login:")) {
3110 have_sent_ICS_logon = 1;
3114 if (ics_getting_history != H_GETTING_MOVES /*smpos kludge*/ &&
3115 (looking_at(buf, &i, "\n<12> ") ||
3116 looking_at(buf, &i, "<12> "))) {
3118 if (oldi > next_out) {
3119 SendToPlayer(&buf[next_out], oldi - next_out);
3122 started = STARTED_BOARD;
3127 if ((started == STARTED_NONE && looking_at(buf, &i, "\n<b1> ")) ||
3128 looking_at(buf, &i, "<b1> ")) {
3129 if (oldi > next_out) {
3130 SendToPlayer(&buf[next_out], oldi - next_out);
3133 started = STARTED_HOLDINGS;
3138 if (looking_at(buf, &i, "* *vs. * *--- *")) {
3140 /* Header for a move list -- first line */
3142 switch (ics_getting_history) {
3146 case BeginningOfGame:
3147 /* User typed "moves" or "oldmoves" while we
3148 were idle. Pretend we asked for these
3149 moves and soak them up so user can step
3150 through them and/or save them.
3153 gameMode = IcsObserving;
3156 ics_getting_history = H_GOT_UNREQ_HEADER;
3158 case EditGame: /*?*/
3159 case EditPosition: /*?*/
3160 /* Should above feature work in these modes too? */
3161 /* For now it doesn't */
3162 ics_getting_history = H_GOT_UNWANTED_HEADER;
3165 ics_getting_history = H_GOT_UNWANTED_HEADER;
3170 /* Is this the right one? */
3171 if (gameInfo.white && gameInfo.black &&
3172 strcmp(gameInfo.white, star_match[0]) == 0 &&
3173 strcmp(gameInfo.black, star_match[2]) == 0) {
3175 ics_getting_history = H_GOT_REQ_HEADER;
3178 case H_GOT_REQ_HEADER:
3179 case H_GOT_UNREQ_HEADER:
3180 case H_GOT_UNWANTED_HEADER:
3181 case H_GETTING_MOVES:
3182 /* Should not happen */
3183 DisplayError(_("Error gathering move list: two headers"), 0);
3184 ics_getting_history = H_FALSE;
3188 /* Save player ratings into gameInfo if needed */
3189 if ((ics_getting_history == H_GOT_REQ_HEADER ||
3190 ics_getting_history == H_GOT_UNREQ_HEADER) &&
3191 (gameInfo.whiteRating == -1 ||
3192 gameInfo.blackRating == -1)) {
3194 gameInfo.whiteRating = string_to_rating(star_match[1]);
3195 gameInfo.blackRating = string_to_rating(star_match[3]);
3196 if (appData.debugMode)
3197 fprintf(debugFP, _("Ratings from header: W %d, B %d\n"),
3198 gameInfo.whiteRating, gameInfo.blackRating);
3203 if (looking_at(buf, &i,
3204 "* * match, initial time: * minute*, increment: * second")) {
3205 /* Header for a move list -- second line */
3206 /* Initial board will follow if this is a wild game */
3207 if (gameInfo.event != NULL) free(gameInfo.event);
3208 snprintf(str, MSG_SIZ, "ICS %s %s match", star_match[0], star_match[1]);
3209 gameInfo.event = StrSave(str);
3210 /* [HGM] we switched variant. Translate boards if needed. */
3211 VariantSwitch(boards[currentMove], StringToVariant(gameInfo.event));
3215 if (looking_at(buf, &i, "Move ")) {
3216 /* Beginning of a move list */
3217 switch (ics_getting_history) {
3219 /* Normally should not happen */
3220 /* Maybe user hit reset while we were parsing */
3223 /* Happens if we are ignoring a move list that is not
3224 * the one we just requested. Common if the user
3225 * tries to observe two games without turning off
3228 case H_GETTING_MOVES:
3229 /* Should not happen */
3230 DisplayError(_("Error gathering move list: nested"), 0);
3231 ics_getting_history = H_FALSE;
3233 case H_GOT_REQ_HEADER:
3234 ics_getting_history = H_GETTING_MOVES;
3235 started = STARTED_MOVES;
3237 if (oldi > next_out) {
3238 SendToPlayer(&buf[next_out], oldi - next_out);
3241 case H_GOT_UNREQ_HEADER:
3242 ics_getting_history = H_GETTING_MOVES;
3243 started = STARTED_MOVES_NOHIDE;
3246 case H_GOT_UNWANTED_HEADER:
3247 ics_getting_history = H_FALSE;
3253 if (looking_at(buf, &i, "% ") ||
3254 ((started == STARTED_MOVES || started == STARTED_MOVES_NOHIDE)
3255 && looking_at(buf, &i, "}*"))) { char *bookHit = NULL; // [HGM] book
3256 if(ics_type == ICS_ICC && soughtPending) { // [HGM] seekgraph: on ICC sought-list has no termination line
3257 soughtPending = FALSE;
3261 if(suppressKibitz) next_out = i;
3262 savingComment = FALSE;
3266 case STARTED_MOVES_NOHIDE:
3267 memcpy(&parse[parse_pos], &buf[oldi], i - oldi);
3268 parse[parse_pos + i - oldi] = NULLCHAR;
3269 ParseGameHistory(parse);
3271 if (appData.zippyPlay && first.initDone) {
3272 FeedMovesToProgram(&first, forwardMostMove);
3273 if (gameMode == IcsPlayingWhite) {
3274 if (WhiteOnMove(forwardMostMove)) {
3275 if (first.sendTime) {
3276 if (first.useColors) {
3277 SendToProgram("black\n", &first);
3279 SendTimeRemaining(&first, TRUE);
3281 if (first.useColors) {
3282 SendToProgram("white\n", &first); // [HGM] book: made sending of "go\n" book dependent
3284 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: probe book for initial pos
3285 first.maybeThinking = TRUE;
3287 if (first.usePlayother) {
3288 if (first.sendTime) {
3289 SendTimeRemaining(&first, TRUE);
3291 SendToProgram("playother\n", &first);
3297 } else if (gameMode == IcsPlayingBlack) {
3298 if (!WhiteOnMove(forwardMostMove)) {
3299 if (first.sendTime) {
3300 if (first.useColors) {
3301 SendToProgram("white\n", &first);
3303 SendTimeRemaining(&first, FALSE);
3305 if (first.useColors) {
3306 SendToProgram("black\n", &first);
3308 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE);
3309 first.maybeThinking = TRUE;
3311 if (first.usePlayother) {
3312 if (first.sendTime) {
3313 SendTimeRemaining(&first, FALSE);
3315 SendToProgram("playother\n", &first);
3324 if (gameMode == IcsObserving && ics_gamenum == -1) {
3325 /* Moves came from oldmoves or moves command
3326 while we weren't doing anything else.
3328 currentMove = forwardMostMove;
3329 ClearHighlights();/*!!could figure this out*/
3330 flipView = appData.flipView;
3331 DrawPosition(TRUE, boards[currentMove]);
3332 DisplayBothClocks();
3333 snprintf(str, MSG_SIZ, "%s vs. %s",
3334 gameInfo.white, gameInfo.black);
3338 /* Moves were history of an active game */
3339 if (gameInfo.resultDetails != NULL) {
3340 free(gameInfo.resultDetails);
3341 gameInfo.resultDetails = NULL;
3344 HistorySet(parseList, backwardMostMove,
3345 forwardMostMove, currentMove-1);
3346 DisplayMove(currentMove - 1);
3347 if (started == STARTED_MOVES) next_out = i;
3348 started = STARTED_NONE;
3349 ics_getting_history = H_FALSE;
3352 case STARTED_OBSERVE:
3353 started = STARTED_NONE;
3354 SendToICS(ics_prefix);
3355 SendToICS("refresh\n");
3361 if(bookHit) { // [HGM] book: simulate book reply
3362 static char bookMove[MSG_SIZ]; // a bit generous?
3364 programStats.nodes = programStats.depth = programStats.time =
3365 programStats.score = programStats.got_only_move = 0;
3366 sprintf(programStats.movelist, "%s (xbook)", bookHit);
3368 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
3369 strcat(bookMove, bookHit);
3370 HandleMachineMove(bookMove, &first);
3375 if ((started == STARTED_MOVES || started == STARTED_BOARD ||
3376 started == STARTED_HOLDINGS ||
3377 started == STARTED_MOVES_NOHIDE) && i >= leftover_len) {
3378 /* Accumulate characters in move list or board */
3379 parse[parse_pos++] = buf[i];
3382 /* Start of game messages. Mostly we detect start of game
3383 when the first board image arrives. On some versions
3384 of the ICS, though, we need to do a "refresh" after starting
3385 to observe in order to get the current board right away. */
3386 if (looking_at(buf, &i, "Adding game * to observation list")) {
3387 started = STARTED_OBSERVE;
3391 /* Handle auto-observe */
3392 if (appData.autoObserve &&
3393 (gameMode == IcsIdle || gameMode == BeginningOfGame) &&
3394 looking_at(buf, &i, "Game notification: * (*) vs. * (*)")) {
3396 /* Choose the player that was highlighted, if any. */
3397 if (star_match[0][0] == '\033' ||
3398 star_match[1][0] != '\033') {
3399 player = star_match[0];
3401 player = star_match[2];
3403 snprintf(str, MSG_SIZ, "%sobserve %s\n",
3404 ics_prefix, StripHighlightAndTitle(player));
3407 /* Save ratings from notify string */
3408 safeStrCpy(player1Name, star_match[0], sizeof(player1Name)/sizeof(player1Name[0]));
3409 player1Rating = string_to_rating(star_match[1]);
3410 safeStrCpy(player2Name, star_match[2], sizeof(player2Name)/sizeof(player2Name[0]));
3411 player2Rating = string_to_rating(star_match[3]);
3413 if (appData.debugMode)
3415 "Ratings from 'Game notification:' %s %d, %s %d\n",
3416 player1Name, player1Rating,
3417 player2Name, player2Rating);
3422 /* Deal with automatic examine mode after a game,
3423 and with IcsObserving -> IcsExamining transition */
3424 if (looking_at(buf, &i, "Entering examine mode for game *") ||
3425 looking_at(buf, &i, "has made you an examiner of game *")) {
3427 int gamenum = atoi(star_match[0]);
3428 if ((gameMode == IcsIdle || gameMode == IcsObserving) &&
3429 gamenum == ics_gamenum) {
3430 /* We were already playing or observing this game;
3431 no need to refetch history */
3432 gameMode = IcsExamining;
3434 pauseExamForwardMostMove = forwardMostMove;
3435 } else if (currentMove < forwardMostMove) {
3436 ForwardInner(forwardMostMove);
3439 /* I don't think this case really can happen */
3440 SendToICS(ics_prefix);
3441 SendToICS("refresh\n");
3446 /* Error messages */
3447 // if (ics_user_moved) {
3448 if (1) { // [HGM] old way ignored error after move type in; ics_user_moved is not set then!
3449 if (looking_at(buf, &i, "Illegal move") ||
3450 looking_at(buf, &i, "Not a legal move") ||
3451 looking_at(buf, &i, "Your king is in check") ||
3452 looking_at(buf, &i, "It isn't your turn") ||
3453 looking_at(buf, &i, "It is not your move")) {
3455 if (ics_user_moved && forwardMostMove > backwardMostMove) { // only backup if we already moved
3456 currentMove = forwardMostMove-1;
3457 DisplayMove(currentMove - 1); /* before DMError */
3458 DrawPosition(FALSE, boards[currentMove]);
3459 SwitchClocks(forwardMostMove-1); // [HGM] race
3460 DisplayBothClocks();
3462 DisplayMoveError(_("Illegal move (rejected by ICS)")); // [HGM] but always relay error msg
3468 if (looking_at(buf, &i, "still have time") ||
3469 looking_at(buf, &i, "not out of time") ||
3470 looking_at(buf, &i, "either player is out of time") ||
3471 looking_at(buf, &i, "has timeseal; checking")) {
3472 /* We must have called his flag a little too soon */
3473 whiteFlag = blackFlag = FALSE;
3477 if (looking_at(buf, &i, "added * seconds to") ||
3478 looking_at(buf, &i, "seconds were added to")) {
3479 /* Update the clocks */
3480 SendToICS(ics_prefix);
3481 SendToICS("refresh\n");
3485 if (!ics_clock_paused && looking_at(buf, &i, "clock paused")) {
3486 ics_clock_paused = TRUE;
3491 if (ics_clock_paused && looking_at(buf, &i, "clock resumed")) {
3492 ics_clock_paused = FALSE;
3497 /* Grab player ratings from the Creating: message.
3498 Note we have to check for the special case when
3499 the ICS inserts things like [white] or [black]. */
3500 if (looking_at(buf, &i, "Creating: * (*)* * (*)") ||
3501 looking_at(buf, &i, "Creating: * (*) [*] * (*)")) {
3503 0 player 1 name (not necessarily white)
3505 2 empty, white, or black (IGNORED)
3506 3 player 2 name (not necessarily black)
3509 The names/ratings are sorted out when the game
3510 actually starts (below).
3512 safeStrCpy(player1Name, StripHighlightAndTitle(star_match[0]), sizeof(player1Name)/sizeof(player1Name[0]));
3513 player1Rating = string_to_rating(star_match[1]);
3514 safeStrCpy(player2Name, StripHighlightAndTitle(star_match[3]), sizeof(player2Name)/sizeof(player2Name[0]));
3515 player2Rating = string_to_rating(star_match[4]);
3517 if (appData.debugMode)
3519 "Ratings from 'Creating:' %s %d, %s %d\n",
3520 player1Name, player1Rating,
3521 player2Name, player2Rating);
3526 /* Improved generic start/end-of-game messages */
3527 if ((tkind=0, looking_at(buf, &i, "{Game * (* vs. *) *}*")) ||
3528 (tkind=1, looking_at(buf, &i, "{Game * (*(*) vs. *(*)) *}*"))){
3529 /* If tkind == 0: */
3530 /* star_match[0] is the game number */
3531 /* [1] is the white player's name */
3532 /* [2] is the black player's name */
3533 /* For end-of-game: */
3534 /* [3] is the reason for the game end */
3535 /* [4] is a PGN end game-token, preceded by " " */
3536 /* For start-of-game: */
3537 /* [3] begins with "Creating" or "Continuing" */
3538 /* [4] is " *" or empty (don't care). */
3539 int gamenum = atoi(star_match[0]);
3540 char *whitename, *blackname, *why, *endtoken;
3541 ChessMove endtype = EndOfFile;
3544 whitename = star_match[1];
3545 blackname = star_match[2];
3546 why = star_match[3];
3547 endtoken = star_match[4];
3549 whitename = star_match[1];
3550 blackname = star_match[3];
3551 why = star_match[5];
3552 endtoken = star_match[6];
3555 /* Game start messages */
3556 if (strncmp(why, "Creating ", 9) == 0 ||
3557 strncmp(why, "Continuing ", 11) == 0) {
3558 gs_gamenum = gamenum;
3559 safeStrCpy(gs_kind, strchr(why, ' ') + 1,sizeof(gs_kind)/sizeof(gs_kind[0]));
3560 VariantSwitch(boards[currentMove], StringToVariant(gs_kind)); // [HGM] variantswitch: even before we get first board
3562 if (appData.zippyPlay) {
3563 ZippyGameStart(whitename, blackname);
3566 partnerBoardValid = FALSE; // [HGM] bughouse
3570 /* Game end messages */
3571 if (gameMode == IcsIdle || gameMode == BeginningOfGame ||
3572 ics_gamenum != gamenum) {
3575 while (endtoken[0] == ' ') endtoken++;
3576 switch (endtoken[0]) {
3579 endtype = GameUnfinished;
3582 endtype = BlackWins;
3585 if (endtoken[1] == '/')
3586 endtype = GameIsDrawn;
3588 endtype = WhiteWins;
3591 GameEnds(endtype, why, GE_ICS);
3593 if (appData.zippyPlay && first.initDone) {
3594 ZippyGameEnd(endtype, why);
3595 if (first.pr == NULL) {
3596 /* Start the next process early so that we'll
3597 be ready for the next challenge */
3598 StartChessProgram(&first);
3600 /* Send "new" early, in case this command takes
3601 a long time to finish, so that we'll be ready
3602 for the next challenge. */
3603 gameInfo.variant = VariantNormal; // [HGM] variantswitch: suppress sending of 'variant'
3607 if(appData.bgObserve && partnerBoardValid) DrawPosition(TRUE, partnerBoard);
3611 if (looking_at(buf, &i, "Removing game * from observation") ||
3612 looking_at(buf, &i, "no longer observing game *") ||
3613 looking_at(buf, &i, "Game * (*) has no examiners")) {
3614 if (gameMode == IcsObserving &&
3615 atoi(star_match[0]) == ics_gamenum)
3617 /* icsEngineAnalyze */
3618 if (appData.icsEngineAnalyze) {
3625 ics_user_moved = FALSE;
3630 if (looking_at(buf, &i, "no longer examining game *")) {
3631 if (gameMode == IcsExamining &&
3632 atoi(star_match[0]) == ics_gamenum)
3636 ics_user_moved = FALSE;
3641 /* Advance leftover_start past any newlines we find,
3642 so only partial lines can get reparsed */
3643 if (looking_at(buf, &i, "\n")) {
3644 prevColor = curColor;
3645 if (curColor != ColorNormal) {
3646 if (oldi > next_out) {
3647 SendToPlayer(&buf[next_out], oldi - next_out);
3650 Colorize(ColorNormal, FALSE);
3651 curColor = ColorNormal;
3653 if (started == STARTED_BOARD) {
3654 started = STARTED_NONE;
3655 parse[parse_pos] = NULLCHAR;
3656 ParseBoard12(parse);
3659 /* Send premove here */
3660 if (appData.premove) {
3662 if (currentMove == 0 &&
3663 gameMode == IcsPlayingWhite &&
3664 appData.premoveWhite) {
3665 snprintf(str, MSG_SIZ, "%s\n", appData.premoveWhiteText);
3666 if (appData.debugMode)
3667 fprintf(debugFP, "Sending premove:\n");
3669 } else if (currentMove == 1 &&
3670 gameMode == IcsPlayingBlack &&
3671 appData.premoveBlack) {
3672 snprintf(str, MSG_SIZ, "%s\n", appData.premoveBlackText);
3673 if (appData.debugMode)
3674 fprintf(debugFP, "Sending premove:\n");
3676 } else if (gotPremove) {
3678 ClearPremoveHighlights();
3679 if (appData.debugMode)
3680 fprintf(debugFP, "Sending premove:\n");
3681 UserMoveEvent(premoveFromX, premoveFromY,
3682 premoveToX, premoveToY,
3687 /* Usually suppress following prompt */
3688 if (!(forwardMostMove == 0 && gameMode == IcsExamining)) {
3689 while(looking_at(buf, &i, "\n")); // [HGM] skip empty lines
3690 if (looking_at(buf, &i, "*% ")) {
3691 savingComment = FALSE;
3696 } else if (started == STARTED_HOLDINGS) {
3698 char new_piece[MSG_SIZ];
3699 started = STARTED_NONE;
3700 parse[parse_pos] = NULLCHAR;
3701 if (appData.debugMode)
3702 fprintf(debugFP, "Parsing holdings: %s, currentMove = %d\n",
3703 parse, currentMove);
3704 if (sscanf(parse, " game %d", &gamenum) == 1) {
3705 if(gamenum == ics_gamenum) { // [HGM] bughouse: old code if part of foreground game
3706 if (gameInfo.variant == VariantNormal) {
3707 /* [HGM] We seem to switch variant during a game!
3708 * Presumably no holdings were displayed, so we have
3709 * to move the position two files to the right to
3710 * create room for them!
3712 VariantClass newVariant;
3713 switch(gameInfo.boardWidth) { // base guess on board width
3714 case 9: newVariant = VariantShogi; break;
3715 case 10: newVariant = VariantGreat; break;
3716 default: newVariant = VariantCrazyhouse; break;
3718 VariantSwitch(boards[currentMove], newVariant); /* temp guess */
3719 /* Get a move list just to see the header, which
3720 will tell us whether this is really bug or zh */
3721 if (ics_getting_history == H_FALSE) {
3722 ics_getting_history = H_REQUESTED;
3723 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
3727 new_piece[0] = NULLCHAR;
3728 sscanf(parse, "game %d white [%s black [%s <- %s",
3729 &gamenum, white_holding, black_holding,
3731 white_holding[strlen(white_holding)-1] = NULLCHAR;
3732 black_holding[strlen(black_holding)-1] = NULLCHAR;
3733 /* [HGM] copy holdings to board holdings area */
3734 CopyHoldings(boards[forwardMostMove], white_holding, WhitePawn);
3735 CopyHoldings(boards[forwardMostMove], black_holding, BlackPawn);
3736 boards[forwardMostMove][HOLDINGS_SET] = 1; // flag holdings as set
3738 if (appData.zippyPlay && first.initDone) {
3739 ZippyHoldings(white_holding, black_holding,
3743 if (tinyLayout || smallLayout) {
3744 char wh[16], bh[16];
3745 PackHolding(wh, white_holding);
3746 PackHolding(bh, black_holding);
3747 snprintf(str, MSG_SIZ,"[%s-%s] %s-%s", wh, bh,
3748 gameInfo.white, gameInfo.black);
3750 snprintf(str, MSG_SIZ, "%s [%s] vs. %s [%s]",
3751 gameInfo.white, white_holding,
3752 gameInfo.black, black_holding);
3754 if(!partnerUp) // [HGM] bughouse: when peeking at partner game we already know what he captured...
3755 DrawPosition(FALSE, boards[currentMove]);
3757 } else if(appData.bgObserve) { // [HGM] bughouse: holdings of other game => background
3758 sscanf(parse, "game %d white [%s black [%s <- %s",
3759 &gamenum, white_holding, black_holding,
3761 white_holding[strlen(white_holding)-1] = NULLCHAR;
3762 black_holding[strlen(black_holding)-1] = NULLCHAR;
3763 /* [HGM] copy holdings to partner-board holdings area */
3764 CopyHoldings(partnerBoard, white_holding, WhitePawn);
3765 CopyHoldings(partnerBoard, black_holding, BlackPawn);
3766 if(twoBoards) { partnerUp = 1; flipView = !flipView; } // [HGM] dual: always draw
3767 if(partnerUp) DrawPosition(FALSE, partnerBoard);
3768 if(twoBoards) { partnerUp = 0; flipView = !flipView; }
3771 /* Suppress following prompt */
3772 if (looking_at(buf, &i, "*% ")) {
3773 if(strchr(star_match[0], 7)) SendToPlayer("\007", 1); // Bell(); // FICS fuses bell for next board with prompt in zh captures
3774 savingComment = FALSE;
3782 i++; /* skip unparsed character and loop back */
3785 if (started != STARTED_MOVES && started != STARTED_BOARD && !suppressKibitz && // [HGM] kibitz
3786 // started != STARTED_HOLDINGS && i > next_out) { // [HGM] should we compare to leftover_start in stead of i?
3787 // SendToPlayer(&buf[next_out], i - next_out);
3788 started != STARTED_HOLDINGS && leftover_start > next_out) {
3789 SendToPlayer(&buf[next_out], leftover_start - next_out);
3793 leftover_len = buf_len - leftover_start;
3794 /* if buffer ends with something we couldn't parse,
3795 reparse it after appending the next read */
3797 } else if (count == 0) {
3798 RemoveInputSource(isr);
3799 DisplayFatalError(_("Connection closed by ICS"), 0, 0);
3801 DisplayFatalError(_("Error reading from ICS"), error, 1);
3806 /* Board style 12 looks like this:
3808 <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
3810 * The "<12> " is stripped before it gets to this routine. The two
3811 * trailing 0's (flip state and clock ticking) are later addition, and
3812 * some chess servers may not have them, or may have only the first.
3813 * Additional trailing fields may be added in the future.
3816 #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"
3818 #define RELATION_OBSERVING_PLAYED 0
3819 #define RELATION_OBSERVING_STATIC -2 /* examined, oldmoves, or smoves */
3820 #define RELATION_PLAYING_MYMOVE 1
3821 #define RELATION_PLAYING_NOTMYMOVE -1
3822 #define RELATION_EXAMINING 2
3823 #define RELATION_ISOLATED_BOARD -3
3824 #define RELATION_STARTING_POSITION -4 /* FICS only */
3827 ParseBoard12(string)
3830 GameMode newGameMode;
3831 int gamenum, newGame, newMove, relation, basetime, increment, ics_flip = 0, i;
3832 int j, k, n, moveNum, white_stren, black_stren, white_time, black_time, takeback;
3833 int double_push, castle_ws, castle_wl, castle_bs, castle_bl, irrev_count;
3834 char to_play, board_chars[200];
3835 char move_str[500], str[500], elapsed_time[500];
3836 char black[32], white[32];
3838 int prevMove = currentMove;
3841 int fromX, fromY, toX, toY;
3843 int ranks=1, files=0; /* [HGM] ICS80: allow variable board size */
3844 char *bookHit = NULL; // [HGM] book
3845 Boolean weird = FALSE, reqFlag = FALSE;
3847 fromX = fromY = toX = toY = -1;
3851 if (appData.debugMode)
3852 fprintf(debugFP, _("Parsing board: %s\n"), string);
3854 move_str[0] = NULLCHAR;
3855 elapsed_time[0] = NULLCHAR;
3856 { /* [HGM] figure out how many ranks and files the board has, for ICS extension used by Capablanca server */
3858 while(i < 199 && (string[i] != ' ' || string[i+2] != ' ')) {
3859 if(string[i] == ' ') { ranks++; files = 0; }
3861 if(!strchr(" -pnbrqkPNBRQK" , string[i])) weird = TRUE; // test for fairies
3864 for(j = 0; j <i; j++) board_chars[j] = string[j];
3865 board_chars[i] = '\0';
3868 n = sscanf(string, PATTERN, &to_play, &double_push,
3869 &castle_ws, &castle_wl, &castle_bs, &castle_bl, &irrev_count,
3870 &gamenum, white, black, &relation, &basetime, &increment,
3871 &white_stren, &black_stren, &white_time, &black_time,
3872 &moveNum, str, elapsed_time, move_str, &ics_flip,
3876 snprintf(str, sizeof(str), _("Failed to parse board string:\n\"%s\""), string);
3877 DisplayError(str, 0);
3881 /* Convert the move number to internal form */
3882 moveNum = (moveNum - 1) * 2;
3883 if (to_play == 'B') moveNum++;
3884 if (moveNum > framePtr) { // [HGM] vari: do not run into saved variations
3885 DisplayFatalError(_("Game too long; increase MAX_MOVES and recompile"),
3891 case RELATION_OBSERVING_PLAYED:
3892 case RELATION_OBSERVING_STATIC:
3893 if (gamenum == -1) {
3894 /* Old ICC buglet */
3895 relation = RELATION_OBSERVING_STATIC;
3897 newGameMode = IcsObserving;
3899 case RELATION_PLAYING_MYMOVE:
3900 case RELATION_PLAYING_NOTMYMOVE:
3902 ((relation == RELATION_PLAYING_MYMOVE) == (to_play == 'W')) ?
3903 IcsPlayingWhite : IcsPlayingBlack;
3905 case RELATION_EXAMINING:
3906 newGameMode = IcsExamining;
3908 case RELATION_ISOLATED_BOARD:
3910 /* Just display this board. If user was doing something else,
3911 we will forget about it until the next board comes. */
3912 newGameMode = IcsIdle;
3914 case RELATION_STARTING_POSITION:
3915 newGameMode = gameMode;
3919 if((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)
3920 && newGameMode == IcsObserving && gamenum != ics_gamenum && appData.bgObserve) {
3921 // [HGM] bughouse: don't act on alien boards while we play. Just parse the board and save it */
3923 for (k = 0; k < ranks; k++) {
3924 for (j = 0; j < files; j++)
3925 board[k][j+gameInfo.holdingsWidth] = CharToPiece(board_chars[(ranks-1-k)*(files+1) + j]);
3926 if(gameInfo.holdingsWidth > 1) {
3927 board[k][0] = board[k][BOARD_WIDTH-1] = EmptySquare;
3928 board[k][1] = board[k][BOARD_WIDTH-2] = (ChessSquare) 0;;
3931 CopyBoard(partnerBoard, board);
3932 if(toSqr = strchr(str, '/')) { // extract highlights from long move
3933 partnerBoard[EP_STATUS-3] = toSqr[1] - AAA; // kludge: hide highlighting info in board
3934 partnerBoard[EP_STATUS-4] = toSqr[2] - ONE;
3935 } else partnerBoard[EP_STATUS-4] = partnerBoard[EP_STATUS-3] = -1;
3936 if(toSqr = strchr(str, '-')) {
3937 partnerBoard[EP_STATUS-1] = toSqr[1] - AAA;
3938 partnerBoard[EP_STATUS-2] = toSqr[2] - ONE;
3939 } else partnerBoard[EP_STATUS-1] = partnerBoard[EP_STATUS-2] = -1;
3940 if(appData.dualBoard && !twoBoards) { twoBoards = 1; InitDrawingSizes(-2,0); }
3941 if(twoBoards) { partnerUp = 1; flipView = !flipView; } // [HGM] dual
3942 if(partnerUp) DrawPosition(FALSE, partnerBoard);
3943 if(twoBoards) { partnerUp = 0; flipView = !flipView; } // [HGM] dual
3944 snprintf(partnerStatus, MSG_SIZ,"W: %d:%02d B: %d:%02d (%d-%d) %c", white_time/60000, (white_time%60000)/1000,
3945 (black_time/60000), (black_time%60000)/1000, white_stren, black_stren, to_play);
3946 DisplayMessage(partnerStatus, "");
3947 partnerBoardValid = TRUE;
3951 /* Modify behavior for initial board display on move listing
3954 switch (ics_getting_history) {
3958 case H_GOT_REQ_HEADER:
3959 case H_GOT_UNREQ_HEADER:
3960 /* This is the initial position of the current game */
3961 gamenum = ics_gamenum;
3962 moveNum = 0; /* old ICS bug workaround */
3963 if (to_play == 'B') {
3964 startedFromSetupPosition = TRUE;
3965 blackPlaysFirst = TRUE;
3967 if (forwardMostMove == 0) forwardMostMove = 1;
3968 if (backwardMostMove == 0) backwardMostMove = 1;
3969 if (currentMove == 0) currentMove = 1;
3971 newGameMode = gameMode;
3972 relation = RELATION_STARTING_POSITION; /* ICC needs this */
3974 case H_GOT_UNWANTED_HEADER:
3975 /* This is an initial board that we don't want */
3977 case H_GETTING_MOVES:
3978 /* Should not happen */
3979 DisplayError(_("Error gathering move list: extra board"), 0);
3980 ics_getting_history = H_FALSE;
3984 if (gameInfo.boardHeight != ranks || gameInfo.boardWidth != files ||
3985 weird && (int)gameInfo.variant <= (int)VariantShogi) {
3986 /* [HGM] We seem to have switched variant unexpectedly
3987 * Try to guess new variant from board size
3989 VariantClass newVariant = VariantFairy; // if 8x8, but fairies present
3990 if(ranks == 8 && files == 10) newVariant = VariantCapablanca; else
3991 if(ranks == 10 && files == 9) newVariant = VariantXiangqi; else
3992 if(ranks == 8 && files == 12) newVariant = VariantCourier; else
3993 if(ranks == 9 && files == 9) newVariant = VariantShogi; else
3994 if(!weird) newVariant = VariantNormal;
3995 VariantSwitch(boards[currentMove], newVariant); /* temp guess */
3996 /* Get a move list just to see the header, which
3997 will tell us whether this is really bug or zh */
3998 if (ics_getting_history == H_FALSE) {
3999 ics_getting_history = H_REQUESTED; reqFlag = TRUE;
4000 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4005 /* Take action if this is the first board of a new game, or of a
4006 different game than is currently being displayed. */
4007 if (gamenum != ics_gamenum || newGameMode != gameMode ||
4008 relation == RELATION_ISOLATED_BOARD) {
4010 /* Forget the old game and get the history (if any) of the new one */
4011 if (gameMode != BeginningOfGame) {
4015 if (appData.autoRaiseBoard) BoardToTop();
4017 if (gamenum == -1) {
4018 newGameMode = IcsIdle;
4019 } else if ((moveNum > 0 || newGameMode == IcsObserving) && newGameMode != IcsIdle &&
4020 appData.getMoveList && !reqFlag) {
4021 /* Need to get game history */
4022 ics_getting_history = H_REQUESTED;
4023 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4027 /* Initially flip the board to have black on the bottom if playing
4028 black or if the ICS flip flag is set, but let the user change
4029 it with the Flip View button. */
4030 flipView = appData.autoFlipView ?
4031 (newGameMode == IcsPlayingBlack) || ics_flip :
4034 /* Done with values from previous mode; copy in new ones */
4035 gameMode = newGameMode;
4037 ics_gamenum = gamenum;
4038 if (gamenum == gs_gamenum) {
4039 int klen = strlen(gs_kind);
4040 if (gs_kind[klen - 1] == '.') gs_kind[klen - 1] = NULLCHAR;
4041 snprintf(str, MSG_SIZ, "ICS %s", gs_kind);
4042 gameInfo.event = StrSave(str);
4044 gameInfo.event = StrSave("ICS game");
4046 gameInfo.site = StrSave(appData.icsHost);
4047 gameInfo.date = PGNDate();
4048 gameInfo.round = StrSave("-");
4049 gameInfo.white = StrSave(white);
4050 gameInfo.black = StrSave(black);
4051 timeControl = basetime * 60 * 1000;
4053 timeIncrement = increment * 1000;
4054 movesPerSession = 0;
4055 gameInfo.timeControl = TimeControlTagValue();
4056 VariantSwitch(boards[currentMove], StringToVariant(gameInfo.event) );
4057 if (appData.debugMode) {
4058 fprintf(debugFP, "ParseBoard says variant = '%s'\n", gameInfo.event);
4059 fprintf(debugFP, "recognized as %s\n", VariantName(gameInfo.variant));
4060 setbuf(debugFP, NULL);
4063 gameInfo.outOfBook = NULL;
4065 /* Do we have the ratings? */
4066 if (strcmp(player1Name, white) == 0 &&
4067 strcmp(player2Name, black) == 0) {
4068 if (appData.debugMode)
4069 fprintf(debugFP, "Remembered ratings: W %d, B %d\n",
4070 player1Rating, player2Rating);
4071 gameInfo.whiteRating = player1Rating;
4072 gameInfo.blackRating = player2Rating;
4073 } else if (strcmp(player2Name, white) == 0 &&
4074 strcmp(player1Name, black) == 0) {
4075 if (appData.debugMode)
4076 fprintf(debugFP, "Remembered ratings: W %d, B %d\n",
4077 player2Rating, player1Rating);
4078 gameInfo.whiteRating = player2Rating;
4079 gameInfo.blackRating = player1Rating;
4081 player1Name[0] = player2Name[0] = NULLCHAR;
4083 /* Silence shouts if requested */
4084 if (appData.quietPlay &&
4085 (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)) {
4086 SendToICS(ics_prefix);
4087 SendToICS("set shout 0\n");
4091 /* Deal with midgame name changes */
4093 if (!gameInfo.white || strcmp(gameInfo.white, white) != 0) {
4094 if (gameInfo.white) free(gameInfo.white);
4095 gameInfo.white = StrSave(white);
4097 if (!gameInfo.black || strcmp(gameInfo.black, black) != 0) {
4098 if (gameInfo.black) free(gameInfo.black);
4099 gameInfo.black = StrSave(black);
4103 /* Throw away game result if anything actually changes in examine mode */
4104 if (gameMode == IcsExamining && !newGame) {
4105 gameInfo.result = GameUnfinished;
4106 if (gameInfo.resultDetails != NULL) {
4107 free(gameInfo.resultDetails);
4108 gameInfo.resultDetails = NULL;
4112 /* In pausing && IcsExamining mode, we ignore boards coming
4113 in if they are in a different variation than we are. */
4114 if (pauseExamInvalid) return;
4115 if (pausing && gameMode == IcsExamining) {
4116 if (moveNum <= pauseExamForwardMostMove) {
4117 pauseExamInvalid = TRUE;
4118 forwardMostMove = pauseExamForwardMostMove;
4123 if (appData.debugMode) {
4124 fprintf(debugFP, "load %dx%d board\n", files, ranks);
4126 /* Parse the board */
4127 for (k = 0; k < ranks; k++) {
4128 for (j = 0; j < files; j++)
4129 board[k][j+gameInfo.holdingsWidth] = CharToPiece(board_chars[(ranks-1-k)*(files+1) + j]);
4130 if(gameInfo.holdingsWidth > 1) {
4131 board[k][0] = board[k][BOARD_WIDTH-1] = EmptySquare;
4132 board[k][1] = board[k][BOARD_WIDTH-2] = (ChessSquare) 0;;
4135 CopyBoard(boards[moveNum], board);
4136 boards[moveNum][HOLDINGS_SET] = 0; // [HGM] indicate holdings not set
4138 startedFromSetupPosition =
4139 !CompareBoards(board, initialPosition);
4140 if(startedFromSetupPosition)
4141 initialRulePlies = irrev_count; /* [HGM] 50-move counter offset */
4144 /* [HGM] Set castling rights. Take the outermost Rooks,
4145 to make it also work for FRC opening positions. Note that board12
4146 is really defective for later FRC positions, as it has no way to
4147 indicate which Rook can castle if they are on the same side of King.
4148 For the initial position we grant rights to the outermost Rooks,
4149 and remember thos rights, and we then copy them on positions
4150 later in an FRC game. This means WB might not recognize castlings with
4151 Rooks that have moved back to their original position as illegal,
4152 but in ICS mode that is not its job anyway.
4154 if(moveNum == 0 || gameInfo.variant != VariantFischeRandom)
4155 { int i, j; ChessSquare wKing = WhiteKing, bKing = BlackKing;
4157 for(i=BOARD_LEFT, j=NoRights; i<BOARD_RGHT; i++)
4158 if(board[0][i] == WhiteRook) j = i;
4159 initialRights[0] = boards[moveNum][CASTLING][0] = (castle_ws == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4160 for(i=BOARD_RGHT-1, j=NoRights; i>=BOARD_LEFT; i--)
4161 if(board[0][i] == WhiteRook) j = i;
4162 initialRights[1] = boards[moveNum][CASTLING][1] = (castle_wl == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4163 for(i=BOARD_LEFT, j=NoRights; i<BOARD_RGHT; i++)
4164 if(board[BOARD_HEIGHT-1][i] == BlackRook) j = i;
4165 initialRights[3] = boards[moveNum][CASTLING][3] = (castle_bs == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4166 for(i=BOARD_RGHT-1, j=NoRights; i>=BOARD_LEFT; i--)
4167 if(board[BOARD_HEIGHT-1][i] == BlackRook) j = i;
4168 initialRights[4] = boards[moveNum][CASTLING][4] = (castle_bl == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4170 if(gameInfo.variant == VariantKnightmate) { wKing = WhiteUnicorn; bKing = BlackUnicorn; }
4171 for(k=BOARD_LEFT; k<BOARD_RGHT; k++)
4172 if(board[0][k] == wKing) initialRights[2] = boards[moveNum][CASTLING][2] = k;
4173 for(k=BOARD_LEFT; k<BOARD_RGHT; k++)
4174 if(board[BOARD_HEIGHT-1][k] == bKing)
4175 initialRights[5] = boards[moveNum][CASTLING][5] = k;
4176 if(gameInfo.variant == VariantTwoKings) {
4177 // In TwoKings looking for a King does not work, so always give castling rights to a King on e1/e8
4178 if(board[0][4] == wKing) initialRights[2] = boards[moveNum][CASTLING][2] = 4;
4179 if(board[BOARD_HEIGHT-1][4] == bKing) initialRights[5] = boards[moveNum][CASTLING][5] = 4;
4182 r = boards[moveNum][CASTLING][0] = initialRights[0];
4183 if(board[0][r] != WhiteRook) boards[moveNum][CASTLING][0] = NoRights;
4184 r = boards[moveNum][CASTLING][1] = initialRights[1];
4185 if(board[0][r] != WhiteRook) boards[moveNum][CASTLING][1] = NoRights;
4186 r = boards[moveNum][CASTLING][3] = initialRights[3];
4187 if(board[BOARD_HEIGHT-1][r] != BlackRook) boards[moveNum][CASTLING][3] = NoRights;
4188 r = boards[moveNum][CASTLING][4] = initialRights[4];
4189 if(board[BOARD_HEIGHT-1][r] != BlackRook) boards[moveNum][CASTLING][4] = NoRights;
4190 /* wildcastle kludge: always assume King has rights */
4191 r = boards[moveNum][CASTLING][2] = initialRights[2];
4192 r = boards[moveNum][CASTLING][5] = initialRights[5];
4194 /* [HGM] e.p. rights. Assume that ICS sends file number here? */
4195 boards[moveNum][EP_STATUS] = double_push == -1 ? EP_NONE : double_push + BOARD_LEFT;
4198 if (ics_getting_history == H_GOT_REQ_HEADER ||
4199 ics_getting_history == H_GOT_UNREQ_HEADER) {
4200 /* This was an initial position from a move list, not
4201 the current position */
4205 /* Update currentMove and known move number limits */
4206 newMove = newGame || moveNum > forwardMostMove;
4209 forwardMostMove = backwardMostMove = currentMove = moveNum;
4210 if (gameMode == IcsExamining && moveNum == 0) {
4211 /* Workaround for ICS limitation: we are not told the wild
4212 type when starting to examine a game. But if we ask for
4213 the move list, the move list header will tell us */
4214 ics_getting_history = H_REQUESTED;
4215 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4218 } else if (moveNum == forwardMostMove + 1 || moveNum == forwardMostMove
4219 || (moveNum < forwardMostMove && moveNum >= backwardMostMove)) {
4221 /* [DM] If we found takebacks during icsEngineAnalyze try send to engine */
4222 /* [HGM] applied this also to an engine that is silently watching */
4223 if (appData.zippyPlay && moveNum < forwardMostMove && first.initDone &&
4224 (gameMode == IcsObserving || gameMode == IcsExamining) &&
4225 gameInfo.variant == currentlyInitializedVariant) {
4226 takeback = forwardMostMove - moveNum;
4227 for (i = 0; i < takeback; i++) {
4228 if (appData.debugMode) fprintf(debugFP, "take back move\n");
4229 SendToProgram("undo\n", &first);
4234 forwardMostMove = moveNum;
4235 if (!pausing || currentMove > forwardMostMove)
4236 currentMove = forwardMostMove;
4238 /* New part of history that is not contiguous with old part */
4239 if (pausing && gameMode == IcsExamining) {
4240 pauseExamInvalid = TRUE;
4241 forwardMostMove = pauseExamForwardMostMove;
4244 if (gameMode == IcsExamining && moveNum > 0 && appData.getMoveList) {
4246 if(appData.zippyPlay && forwardMostMove > 0 && first.initDone) {
4247 // [HGM] when we will receive the move list we now request, it will be
4248 // fed to the engine from the first move on. So if the engine is not
4249 // in the initial position now, bring it there.
4250 InitChessProgram(&first, 0);
4253 ics_getting_history = H_REQUESTED;
4254 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
4257 forwardMostMove = backwardMostMove = currentMove = moveNum;
4260 /* Update the clocks */
4261 if (strchr(elapsed_time, '.')) {
4263 timeRemaining[0][moveNum] = whiteTimeRemaining = white_time;
4264 timeRemaining[1][moveNum] = blackTimeRemaining = black_time;
4266 /* Time is in seconds */
4267 timeRemaining[0][moveNum] = whiteTimeRemaining = white_time * 1000;
4268 timeRemaining[1][moveNum] = blackTimeRemaining = black_time * 1000;
4273 if (appData.zippyPlay && newGame &&
4274 gameMode != IcsObserving && gameMode != IcsIdle &&
4275 gameMode != IcsExamining)
4276 ZippyFirstBoard(moveNum, basetime, increment);
4279 /* Put the move on the move list, first converting
4280 to canonical algebraic form. */
4282 if (appData.debugMode) {
4283 if (appData.debugMode) { int f = forwardMostMove;
4284 fprintf(debugFP, "parseboard %d, castling = %d %d %d %d %d %d\n", f,
4285 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
4286 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
4288 fprintf(debugFP, "accepted move %s from ICS, parse it.\n", move_str);
4289 fprintf(debugFP, "moveNum = %d\n", moveNum);
4290 fprintf(debugFP, "board = %d-%d x %d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT);
4291 setbuf(debugFP, NULL);
4293 if (moveNum <= backwardMostMove) {
4294 /* We don't know what the board looked like before
4296 safeStrCpy(parseList[moveNum - 1], move_str, sizeof(parseList[moveNum - 1])/sizeof(parseList[moveNum - 1][0]));
4297 strcat(parseList[moveNum - 1], " ");
4298 strcat(parseList[moveNum - 1], elapsed_time);
4299 moveList[moveNum - 1][0] = NULLCHAR;
4300 } else if (strcmp(move_str, "none") == 0) {
4301 // [HGM] long SAN: swapped order; test for 'none' before parsing move
4302 /* Again, we don't know what the board looked like;
4303 this is really the start of the game. */
4304 parseList[moveNum - 1][0] = NULLCHAR;
4305 moveList[moveNum - 1][0] = NULLCHAR;
4306 backwardMostMove = moveNum;
4307 startedFromSetupPosition = TRUE;
4308 fromX = fromY = toX = toY = -1;
4310 // [HGM] long SAN: if legality-testing is off, disambiguation might not work or give wrong move.
4311 // So we parse the long-algebraic move string in stead of the SAN move
4312 int valid; char buf[MSG_SIZ], *prom;
4314 // str looks something like "Q/a1-a2"; kill the slash
4316 snprintf(buf, MSG_SIZ,"%c%s", str[0], str+2);
4317 else safeStrCpy(buf, str, sizeof(buf)/sizeof(buf[0])); // might be castling
4318 if((prom = strstr(move_str, "=")) && !strstr(buf, "="))
4319 strcat(buf, prom); // long move lacks promo specification!
4320 if(!appData.testLegality && move_str[1] != '@') { // drops never ambiguous (parser chokes on long form!)
4321 if(appData.debugMode)
4322 fprintf(debugFP, "replaced ICS move '%s' by '%s'\n", move_str, buf);
4323 safeStrCpy(move_str, buf, sizeof(move_str)/sizeof(move_str[0]));
4325 valid = ParseOneMove(move_str, moveNum - 1, &moveType,
4326 &fromX, &fromY, &toX, &toY, &promoChar)
4327 || ParseOneMove(buf, moveNum - 1, &moveType,
4328 &fromX, &fromY, &toX, &toY, &promoChar);
4329 // end of long SAN patch
4331 (void) CoordsToAlgebraic(boards[moveNum - 1],
4332 PosFlags(moveNum - 1),
4333 fromY, fromX, toY, toX, promoChar,
4334 parseList[moveNum-1]);
4335 switch (MateTest(boards[moveNum], PosFlags(moveNum)) ) {
4341 if(gameInfo.variant != VariantShogi)
4342 strcat(parseList[moveNum - 1], "+");
4345 case MT_STAINMATE: // [HGM] xq: for notation stalemate that wins counts as checkmate
4346 strcat(parseList[moveNum - 1], "#");
4349 strcat(parseList[moveNum - 1], " ");
4350 strcat(parseList[moveNum - 1], elapsed_time);
4351 /* currentMoveString is set as a side-effect of ParseOneMove */
4352 safeStrCpy(moveList[moveNum - 1], currentMoveString, sizeof(moveList[moveNum - 1])/sizeof(moveList[moveNum - 1][0]));
4353 strcat(moveList[moveNum - 1], "\n");
4355 /* Move from ICS was illegal!? Punt. */
4356 if (appData.debugMode) {
4357 fprintf(debugFP, "Illegal move from ICS '%s'\n", move_str);
4358 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
4360 safeStrCpy(parseList[moveNum - 1], move_str, sizeof(parseList[moveNum - 1])/sizeof(parseList[moveNum - 1][0]));
4361 strcat(parseList[moveNum - 1], " ");
4362 strcat(parseList[moveNum - 1], elapsed_time);
4363 moveList[moveNum - 1][0] = NULLCHAR;
4364 fromX = fromY = toX = toY = -1;
4367 if (appData.debugMode) {
4368 fprintf(debugFP, "Move parsed to '%s'\n", parseList[moveNum - 1]);
4369 setbuf(debugFP, NULL);
4373 /* Send move to chess program (BEFORE animating it). */
4374 if (appData.zippyPlay && !newGame && newMove &&
4375 (!appData.getMoveList || backwardMostMove == 0) && first.initDone) {
4377 if ((gameMode == IcsPlayingWhite && WhiteOnMove(moveNum)) ||
4378 (gameMode == IcsPlayingBlack && !WhiteOnMove(moveNum))) {
4379 if (moveList[moveNum - 1][0] == NULLCHAR) {
4380 snprintf(str, MSG_SIZ, _("Couldn't parse move \"%s\" from ICS"),
4382 DisplayError(str, 0);
4384 if (first.sendTime) {
4385 SendTimeRemaining(&first, gameMode == IcsPlayingWhite);
4387 bookHit = SendMoveToBookUser(moveNum - 1, &first, FALSE); // [HGM] book
4388 if (firstMove && !bookHit) {
4390 if (first.useColors) {
4391 SendToProgram(gameMode == IcsPlayingWhite ?
4393 "black\ngo\n", &first);
4395 SendToProgram("go\n", &first);
4397 first.maybeThinking = TRUE;
4400 } else if (gameMode == IcsObserving || gameMode == IcsExamining) {
4401 if (moveList[moveNum - 1][0] == NULLCHAR) {
4402 snprintf(str, MSG_SIZ, _("Couldn't parse move \"%s\" from ICS"), move_str);
4403 DisplayError(str, 0);
4405 if(gameInfo.variant == currentlyInitializedVariant) // [HGM] refrain sending moves engine can't understand!
4406 SendMoveToProgram(moveNum - 1, &first);
4413 if (moveNum > 0 && !gotPremove && !appData.noGUI) {
4414 /* If move comes from a remote source, animate it. If it
4415 isn't remote, it will have already been animated. */
4416 if (!pausing && !ics_user_moved && prevMove == moveNum - 1) {
4417 AnimateMove(boards[moveNum - 1], fromX, fromY, toX, toY);
4419 if (!pausing && appData.highlightLastMove) {
4420 SetHighlights(fromX, fromY, toX, toY);
4424 /* Start the clocks */
4425 whiteFlag = blackFlag = FALSE;
4426 appData.clockMode = !(basetime == 0 && increment == 0);
4428 ics_clock_paused = TRUE;
4430 } else if (ticking == 1) {
4431 ics_clock_paused = FALSE;
4433 if (gameMode == IcsIdle ||
4434 relation == RELATION_OBSERVING_STATIC ||
4435 relation == RELATION_EXAMINING ||
4437 DisplayBothClocks();
4441 /* Display opponents and material strengths */
4442 if (gameInfo.variant != VariantBughouse &&
4443 gameInfo.variant != VariantCrazyhouse && !appData.noGUI) {
4444 if (tinyLayout || smallLayout) {
4445 if(gameInfo.variant == VariantNormal)
4446 snprintf(str, MSG_SIZ, "%s(%d) %s(%d) {%d %d}",
4447 gameInfo.white, white_stren, gameInfo.black, black_stren,
4448 basetime, increment);
4450 snprintf(str, MSG_SIZ, "%s(%d) %s(%d) {%d %d w%d}",
4451 gameInfo.white, white_stren, gameInfo.black, black_stren,
4452 basetime, increment, (int) gameInfo.variant);
4454 if(gameInfo.variant == VariantNormal)
4455 snprintf(str, MSG_SIZ, "%s (%d) vs. %s (%d) {%d %d}",
4456 gameInfo.white, white_stren, gameInfo.black, black_stren,
4457 basetime, increment);
4459 snprintf(str, MSG_SIZ, "%s (%d) vs. %s (%d) {%d %d %s}",
4460 gameInfo.white, white_stren, gameInfo.black, black_stren,
4461 basetime, increment, VariantName(gameInfo.variant));
4464 if (appData.debugMode) {
4465 fprintf(debugFP, "Display title '%s, gameInfo.variant = %d'\n", str, gameInfo.variant);
4470 /* Display the board */
4471 if (!pausing && !appData.noGUI) {
4473 if (appData.premove)
4475 ((gameMode == IcsPlayingWhite) && (WhiteOnMove(currentMove))) ||
4476 ((gameMode == IcsPlayingBlack) && (!WhiteOnMove(currentMove))))
4477 ClearPremoveHighlights();
4479 j = seekGraphUp; seekGraphUp = FALSE; // [HGM] seekgraph: when we draw a board, it overwrites the seek graph
4480 if(partnerUp) { flipView = originalFlip; partnerUp = FALSE; j = TRUE; } // [HGM] bughouse: restore view
4481 DrawPosition(j, boards[currentMove]);
4483 DisplayMove(moveNum - 1);
4484 if (appData.ringBellAfterMoves && /*!ics_user_moved*/ // [HGM] use absolute method to recognize own move
4485 !((gameMode == IcsPlayingWhite) && (!WhiteOnMove(moveNum)) ||
4486 (gameMode == IcsPlayingBlack) && (WhiteOnMove(moveNum)) ) ) {
4487 if(newMove) RingBell(); else PlayIcsUnfinishedSound();
4491 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
4493 if(bookHit) { // [HGM] book: simulate book reply
4494 static char bookMove[MSG_SIZ]; // a bit generous?
4496 programStats.nodes = programStats.depth = programStats.time =
4497 programStats.score = programStats.got_only_move = 0;
4498 sprintf(programStats.movelist, "%s (xbook)", bookHit);
4500 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
4501 strcat(bookMove, bookHit);
4502 HandleMachineMove(bookMove, &first);
4511 if (appData.icsActive && gameMode != IcsIdle && ics_gamenum > 0) {
4512 ics_getting_history = H_REQUESTED;
4513 snprintf(buf, MSG_SIZ, "%smoves %d\n", ics_prefix, ics_gamenum);
4519 AnalysisPeriodicEvent(force)
4522 if (((programStats.ok_to_send == 0 || programStats.line_is_book)
4523 && !force) || !appData.periodicUpdates)
4526 /* Send . command to Crafty to collect stats */
4527 SendToProgram(".\n", &first);
4529 /* Don't send another until we get a response (this makes
4530 us stop sending to old Crafty's which don't understand
4531 the "." command (sending illegal cmds resets node count & time,
4532 which looks bad)) */
4533 programStats.ok_to_send = 0;
4536 void ics_update_width(new_width)
4539 ics_printf("set width %d\n", new_width);
4543 SendMoveToProgram(moveNum, cps)
4545 ChessProgramState *cps;
4549 if (cps->useUsermove) {
4550 SendToProgram("usermove ", cps);
4554 if ((space = strchr(parseList[moveNum], ' ')) != NULL) {
4555 int len = space - parseList[moveNum];
4556 memcpy(buf, parseList[moveNum], len);
4558 buf[len] = NULLCHAR;
4560 snprintf(buf, MSG_SIZ,"%s\n", parseList[moveNum]);
4562 SendToProgram(buf, cps);
4564 if(cps->alphaRank) { /* [HGM] shogi: temporarily convert to shogi coordinates before sending */
4565 AlphaRank(moveList[moveNum], 4);
4566 SendToProgram(moveList[moveNum], cps);
4567 AlphaRank(moveList[moveNum], 4); // and back
4569 /* Added by Tord: Send castle moves in "O-O" in FRC games if required by
4570 * the engine. It would be nice to have a better way to identify castle
4572 if((gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom)
4573 && cps->useOOCastle) {
4574 int fromX = moveList[moveNum][0] - AAA;
4575 int fromY = moveList[moveNum][1] - ONE;
4576 int toX = moveList[moveNum][2] - AAA;
4577 int toY = moveList[moveNum][3] - ONE;
4578 if((boards[moveNum][fromY][fromX] == WhiteKing
4579 && boards[moveNum][toY][toX] == WhiteRook)
4580 || (boards[moveNum][fromY][fromX] == BlackKing
4581 && boards[moveNum][toY][toX] == BlackRook)) {
4582 if(toX > fromX) SendToProgram("O-O\n", cps);
4583 else SendToProgram("O-O-O\n", cps);
4585 else SendToProgram(moveList[moveNum], cps);
4587 else SendToProgram(moveList[moveNum], cps);
4588 /* End of additions by Tord */
4591 /* [HGM] setting up the opening has brought engine in force mode! */
4592 /* Send 'go' if we are in a mode where machine should play. */
4593 if( (moveNum == 0 && setboardSpoiledMachineBlack && cps == &first) &&
4594 (gameMode == TwoMachinesPlay ||
4596 gameMode == IcsPlayingBlack || gameMode == IcsPlayingWhite ||
4598 gameMode == MachinePlaysBlack || gameMode == MachinePlaysWhite) ) {
4599 SendToProgram("go\n", cps);
4600 if (appData.debugMode) {
4601 fprintf(debugFP, "(extra)\n");
4604 setboardSpoiledMachineBlack = 0;
4608 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar)
4610 int fromX, fromY, toX, toY;
4613 char user_move[MSG_SIZ];
4617 snprintf(user_move, MSG_SIZ, _("say Internal error; bad moveType %d (%d,%d-%d,%d)"),
4618 (int)moveType, fromX, fromY, toX, toY);
4619 DisplayError(user_move + strlen("say "), 0);
4621 case WhiteKingSideCastle:
4622 case BlackKingSideCastle:
4623 case WhiteQueenSideCastleWild:
4624 case BlackQueenSideCastleWild:
4626 case WhiteHSideCastleFR:
4627 case BlackHSideCastleFR:
4629 snprintf(user_move, MSG_SIZ, "o-o\n");
4631 case WhiteQueenSideCastle:
4632 case BlackQueenSideCastle:
4633 case WhiteKingSideCastleWild:
4634 case BlackKingSideCastleWild:
4636 case WhiteASideCastleFR:
4637 case BlackASideCastleFR:
4639 snprintf(user_move, MSG_SIZ, "o-o-o\n");
4641 case WhiteNonPromotion:
4642 case BlackNonPromotion:
4643 sprintf(user_move, "%c%c%c%c=\n", AAA + fromX, ONE + fromY, AAA + toX, ONE + toY);
4645 case WhitePromotion:
4646 case BlackPromotion:
4647 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
4648 snprintf(user_move, MSG_SIZ, "%c%c%c%c=%c\n",
4649 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
4650 PieceToChar(WhiteFerz));
4651 else if(gameInfo.variant == VariantGreat)
4652 snprintf(user_move, MSG_SIZ,"%c%c%c%c=%c\n",
4653 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
4654 PieceToChar(WhiteMan));
4656 snprintf(user_move, MSG_SIZ, "%c%c%c%c=%c\n",
4657 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
4662 snprintf(user_move, MSG_SIZ, "%c@%c%c\n",
4663 ToUpper(PieceToChar((ChessSquare) fromX)),
4664 AAA + toX, ONE + toY);
4667 case WhiteCapturesEnPassant:
4668 case BlackCapturesEnPassant:
4669 case IllegalMove: /* could be a variant we don't quite understand */
4670 snprintf(user_move, MSG_SIZ,"%c%c%c%c\n",
4671 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY);
4674 SendToICS(user_move);
4675 if(appData.keepAlive) // [HGM] alive: schedule sending of dummy 'date' command
4676 ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
4681 { // [HGM] upload: send entire stored game to ICS as long-algebraic moves.
4682 int i, last = forwardMostMove; // make sure ICS reply cannot pre-empt us by clearing fmm
4683 static char *castlingStrings[4] = { "none", "kside", "qside", "both" };
4684 if(gameMode == IcsObserving || gameMode == IcsPlayingBlack || gameMode == IcsPlayingWhite) {
4685 DisplayError("You cannot do this while you are playing or observing", 0);
4688 if(gameMode != IcsExamining) { // is this ever not the case?
4689 char buf[MSG_SIZ], *p, *fen, command[MSG_SIZ], bsetup = 0;
4691 if(ics_type == ICS_ICC) { // on ICC match ourselves in applicable variant
4692 snprintf(command,MSG_SIZ, "match %s", ics_handle);
4693 } else { // on FICS we must first go to general examine mode
4694 safeStrCpy(command, "examine\nbsetup", sizeof(command)/sizeof(command[0])); // and specify variant within it with bsetups
4696 if(gameInfo.variant != VariantNormal) {
4697 // try figure out wild number, as xboard names are not always valid on ICS
4698 for(i=1; i<=36; i++) {
4699 snprintf(buf, MSG_SIZ, "wild/%d", i);
4700 if(StringToVariant(buf) == gameInfo.variant) break;
4702 if(i<=36 && ics_type == ICS_ICC) snprintf(buf, MSG_SIZ,"%s w%d\n", command, i);
4703 else if(i == 22) snprintf(buf,MSG_SIZ, "%s fr\n", command);
4704 else snprintf(buf, MSG_SIZ,"%s %s\n", command, VariantName(gameInfo.variant));
4705 } else snprintf(buf, MSG_SIZ,"%s\n", ics_type == ICS_ICC ? command : "examine\n"); // match yourself or examine
4706 SendToICS(ics_prefix);
4708 if(startedFromSetupPosition || backwardMostMove != 0) {
4709 fen = PositionToFEN(backwardMostMove, NULL);
4710 if(ics_type == ICS_ICC) { // on ICC we can simply send a complete FEN to set everything
4711 snprintf(buf, MSG_SIZ,"loadfen %s\n", fen);
4713 } else { // FICS: everything has to set by separate bsetup commands
4714 p = strchr(fen, ' '); p[0] = NULLCHAR; // cut after board
4715 snprintf(buf, MSG_SIZ,"bsetup fen %s\n", fen);
4717 if(!WhiteOnMove(backwardMostMove)) {
4718 SendToICS("bsetup tomove black\n");
4720 i = (strchr(p+3, 'K') != NULL) + 2*(strchr(p+3, 'Q') != NULL);
4721 snprintf(buf, MSG_SIZ,"bsetup wcastle %s\n", castlingStrings[i]);
4723 i = (strchr(p+3, 'k') != NULL) + 2*(strchr(p+3, 'q') != NULL);
4724 snprintf(buf, MSG_SIZ, "bsetup bcastle %s\n", castlingStrings[i]);
4726 i = boards[backwardMostMove][EP_STATUS];
4727 if(i >= 0) { // set e.p.
4728 snprintf(buf, MSG_SIZ,"bsetup eppos %c\n", i+AAA);
4734 if(bsetup || ics_type != ICS_ICC && gameInfo.variant != VariantNormal)
4735 SendToICS("bsetup done\n"); // switch to normal examining.
4737 for(i = backwardMostMove; i<last; i++) {
4739 snprintf(buf, sizeof(buf)/sizeof(buf[0]),"%s\n", parseList[i]);
4742 SendToICS(ics_prefix);
4743 SendToICS(ics_type == ICS_ICC ? "tag result Game in progress\n" : "commit\n");
4747 CoordsToComputerAlgebraic(rf, ff, rt, ft, promoChar, move)
4752 if (rf == DROP_RANK) {
4753 sprintf(move, "%c@%c%c\n",
4754 ToUpper(PieceToChar((ChessSquare) ff)), AAA + ft, ONE + rt);
4756 if (promoChar == 'x' || promoChar == NULLCHAR) {
4757 sprintf(move, "%c%c%c%c\n",
4758 AAA + ff, ONE + rf, AAA + ft, ONE + rt);
4760 sprintf(move, "%c%c%c%c%c\n",
4761 AAA + ff, ONE + rf, AAA + ft, ONE + rt, promoChar);
4767 ProcessICSInitScript(f)
4772 while (fgets(buf, MSG_SIZ, f)) {
4773 SendToICSDelayed(buf,(long)appData.msLoginDelay);
4780 /* [HGM] Shogi move preprocessor: swap digits for letters, vice versa */
4782 AlphaRank(char *move, int n)
4784 // char *p = move, c; int x, y;
4786 if (appData.debugMode) {
4787 fprintf(debugFP, "alphaRank(%s,%d)\n", move, n);
4791 move[2]>='0' && move[2]<='9' &&
4792 move[3]>='a' && move[3]<='x' ) {
4794 move[2] = BOARD_RGHT -1 - (move[2]-'1') + AAA;
4795 move[3] = BOARD_HEIGHT-1 - (move[3]-'a') + ONE;
4797 if(move[0]>='0' && move[0]<='9' &&
4798 move[1]>='a' && move[1]<='x' &&
4799 move[2]>='0' && move[2]<='9' &&
4800 move[3]>='a' && move[3]<='x' ) {
4801 /* input move, Shogi -> normal */
4802 move[0] = BOARD_RGHT -1 - (move[0]-'1') + AAA;
4803 move[1] = BOARD_HEIGHT-1 - (move[1]-'a') + ONE;
4804 move[2] = BOARD_RGHT -1 - (move[2]-'1') + AAA;
4805 move[3] = BOARD_HEIGHT-1 - (move[3]-'a') + ONE;
4808 move[3]>='0' && move[3]<='9' &&
4809 move[2]>='a' && move[2]<='x' ) {
4811 move[2] = BOARD_RGHT - 1 - (move[2]-AAA) + '1';
4812 move[3] = BOARD_HEIGHT-1 - (move[3]-ONE) + 'a';
4815 move[0]>='a' && move[0]<='x' &&
4816 move[3]>='0' && move[3]<='9' &&
4817 move[2]>='a' && move[2]<='x' ) {
4818 /* output move, normal -> Shogi */
4819 move[0] = BOARD_RGHT - 1 - (move[0]-AAA) + '1';
4820 move[1] = BOARD_HEIGHT-1 - (move[1]-ONE) + 'a';
4821 move[2] = BOARD_RGHT - 1 - (move[2]-AAA) + '1';
4822 move[3] = BOARD_HEIGHT-1 - (move[3]-ONE) + 'a';
4823 if(move[4] == PieceToChar(BlackQueen)) move[4] = '+';
4825 if (appData.debugMode) {
4826 fprintf(debugFP, " out = '%s'\n", move);
4830 char yy_textstr[8000];
4832 /* Parser for moves from gnuchess, ICS, or user typein box */
4834 ParseOneMove(move, moveNum, moveType, fromX, fromY, toX, toY, promoChar)
4837 ChessMove *moveType;
4838 int *fromX, *fromY, *toX, *toY;
4841 if (appData.debugMode) {
4842 fprintf(debugFP, "move to parse: %s\n", move);
4844 *moveType = yylexstr(moveNum, move, yy_textstr, sizeof yy_textstr);
4846 switch (*moveType) {
4847 case WhitePromotion:
4848 case BlackPromotion:
4849 case WhiteNonPromotion:
4850 case BlackNonPromotion:
4852 case WhiteCapturesEnPassant:
4853 case BlackCapturesEnPassant:
4854 case WhiteKingSideCastle:
4855 case WhiteQueenSideCastle:
4856 case BlackKingSideCastle:
4857 case BlackQueenSideCastle:
4858 case WhiteKingSideCastleWild:
4859 case WhiteQueenSideCastleWild:
4860 case BlackKingSideCastleWild:
4861 case BlackQueenSideCastleWild:
4862 /* Code added by Tord: */
4863 case WhiteHSideCastleFR:
4864 case WhiteASideCastleFR:
4865 case BlackHSideCastleFR:
4866 case BlackASideCastleFR:
4867 /* End of code added by Tord */
4868 case IllegalMove: /* bug or odd chess variant */
4869 *fromX = currentMoveString[0] - AAA;
4870 *fromY = currentMoveString[1] - ONE;
4871 *toX = currentMoveString[2] - AAA;
4872 *toY = currentMoveString[3] - ONE;
4873 *promoChar = currentMoveString[4];
4874 if (*fromX < BOARD_LEFT || *fromX >= BOARD_RGHT || *fromY < 0 || *fromY >= BOARD_HEIGHT ||
4875 *toX < BOARD_LEFT || *toX >= BOARD_RGHT || *toY < 0 || *toY >= BOARD_HEIGHT) {
4876 if (appData.debugMode) {
4877 fprintf(debugFP, "Off-board move (%d,%d)-(%d,%d)%c, type = %d\n", *fromX, *fromY, *toX, *toY, *promoChar, *moveType);
4879 *fromX = *fromY = *toX = *toY = 0;
4882 if (appData.testLegality) {
4883 return (*moveType != IllegalMove);
4885 return !(*fromX == *toX && *fromY == *toY) && boards[moveNum][*fromY][*fromX] != EmptySquare &&
4886 WhiteOnMove(moveNum) == (boards[moveNum][*fromY][*fromX] < BlackPawn);
4891 *fromX = *moveType == WhiteDrop ?
4892 (int) CharToPiece(ToUpper(currentMoveString[0])) :
4893 (int) CharToPiece(ToLower(currentMoveString[0]));
4895 *toX = currentMoveString[2] - AAA;
4896 *toY = currentMoveString[3] - ONE;
4897 *promoChar = NULLCHAR;
4901 case ImpossibleMove:
4911 if (appData.debugMode) {
4912 fprintf(debugFP, "Impossible move %s, type = %d\n", currentMoveString, *moveType);
4915 *fromX = *fromY = *toX = *toY = 0;
4916 *promoChar = NULLCHAR;
4923 ParsePV(char *pv, Boolean storeComments)
4924 { // Parse a string of PV moves, and append to current game, behind forwardMostMove
4925 int fromX, fromY, toX, toY; char promoChar;
4930 endPV = forwardMostMove;
4932 while(*pv == ' ' || *pv == '\n' || *pv == '\t') pv++; // must still read away whitespace
4933 if(nr == 0 && !storeComments && *pv == '(') pv++; // first (ponder) move can be in parentheses
4934 valid = ParseOneMove(pv, endPV, &moveType, &fromX, &fromY, &toX, &toY, &promoChar);
4935 if(appData.debugMode){
4936 fprintf(debugFP,"parsePV: %d %c%c%c%c yy='%s'\nPV = '%s'\n", valid, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, yy_textstr, pv);
4938 if(!valid && nr == 0 &&
4939 ParseOneMove(pv, endPV-1, &moveType, &fromX, &fromY, &toX, &toY, &promoChar)){
4940 nr++; moveType = Comment; // First move has been played; kludge to make sure we continue
4941 // Hande case where played move is different from leading PV move
4942 CopyBoard(boards[endPV+1], boards[endPV-1]); // tentatively unplay last game move
4943 CopyBoard(boards[endPV+2], boards[endPV-1]); // and play first move of PV
4944 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[endPV+2]);
4945 if(!CompareBoards(boards[endPV], boards[endPV+2])) {
4946 endPV += 2; // if position different, keep this
4947 moveList[endPV-1][0] = fromX + AAA;
4948 moveList[endPV-1][1] = fromY + ONE;
4949 moveList[endPV-1][2] = toX + AAA;
4950 moveList[endPV-1][3] = toY + ONE;
4951 parseList[endPV-1][0] = NULLCHAR;
4952 safeStrCpy(moveList[endPV-2], "_0_0", sizeof(moveList[endPV-2])/sizeof(moveList[endPV-2][0])); // suppress premove highlight on takeback move
4955 pv = strstr(pv, yy_textstr) + strlen(yy_textstr); // skip what we parsed
4956 if(nr == 0 && !storeComments && *pv == ')') pv++; // closing parenthesis of ponder move;
4957 if(moveType == Comment && storeComments) AppendComment(endPV, yy_textstr, FALSE);
4958 if(moveType == Comment || moveType == NAG || moveType == ElapsedTime) {
4959 valid++; // allow comments in PV
4963 if(endPV+1 > framePtr) break; // no space, truncate
4966 CopyBoard(boards[endPV], boards[endPV-1]);
4967 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[endPV]);
4968 moveList[endPV-1][0] = fromX + AAA;
4969 moveList[endPV-1][1] = fromY + ONE;
4970 moveList[endPV-1][2] = toX + AAA;
4971 moveList[endPV-1][3] = toY + ONE;
4973 CoordsToAlgebraic(boards[endPV - 1],
4974 PosFlags(endPV - 1),
4975 fromY, fromX, toY, toX, promoChar,
4976 parseList[endPV - 1]);
4978 parseList[endPV-1][0] = NULLCHAR;
4980 currentMove = endPV;
4981 if(currentMove == forwardMostMove) ClearPremoveHighlights(); else
4982 SetPremoveHighlights(moveList[currentMove-1][0]-AAA, moveList[currentMove-1][1]-ONE,
4983 moveList[currentMove-1][2]-AAA, moveList[currentMove-1][3]-ONE);
4984 DrawPosition(TRUE, boards[currentMove]);
4987 static int lastX, lastY;
4990 LoadMultiPV(int x, int y, char *buf, int index, int *start, int *end)
4995 if(index < 0 || index >= strlen(buf)) return FALSE; // sanity
4996 lastX = x; lastY = y;
4997 while(index > 0 && buf[index-1] != '\n') index--; // beginning of line
4999 while(buf[index] != '\n') if(buf[index++] == '\t') startPV = index;
5000 if(index == startPV && (p = StrCaseStr(buf+index, "PV="))) startPV = p - buf + 3;
5002 do{ while(buf[index] && buf[index] != '\n') index++;
5003 } while(buf[index] == '\n' && buf[index+1] == '\\' && buf[index+2] == ' ' && index++); // join kibitzed PV continuation line
5005 ParsePV(buf+startPV, FALSE);
5006 *start = startPV; *end = index-1;
5011 LoadPV(int x, int y)
5012 { // called on right mouse click to load PV
5013 int which = gameMode == TwoMachinesPlay && (WhiteOnMove(forwardMostMove) == (second.twoMachinesColor[0] == 'w'));
5014 lastX = x; lastY = y;
5015 ParsePV(lastPV[which], FALSE); // load the PV of the thinking engine in the boards array.
5022 if(endPV < 0) return;
5024 currentMove = forwardMostMove;
5025 ClearPremoveHighlights();
5026 DrawPosition(TRUE, boards[currentMove]);
5030 MovePV(int x, int y, int h)
5031 { // step through PV based on mouse coordinates (called on mouse move)
5032 int margin = h>>3, step = 0;
5034 if(endPV < 0) return;
5035 // we must somehow check if right button is still down (might be released off board!)
5036 if(y < margin && (abs(x - lastX) > 6 || abs(y - lastY) > 6)) step = 1; else
5037 if(y > h - margin && (abs(x - lastX) > 6 || abs(y - lastY) > 6)) step = -1; else
5038 if( y > lastY + 6 ) step = -1; else if(y < lastY - 6) step = 1;
5040 lastX = x; lastY = y;
5041 if(currentMove + step > endPV || currentMove + step < forwardMostMove) step = 0;
5042 currentMove += step;
5043 if(currentMove == forwardMostMove) ClearPremoveHighlights(); else
5044 SetPremoveHighlights(moveList[currentMove-1][0]-AAA, moveList[currentMove-1][1]-ONE,
5045 moveList[currentMove-1][2]-AAA, moveList[currentMove-1][3]-ONE);
5046 DrawPosition(FALSE, boards[currentMove]);
5050 // [HGM] shuffle: a general way to suffle opening setups, applicable to arbitrary variants.
5051 // All positions will have equal probability, but the current method will not provide a unique
5052 // numbering scheme for arrays that contain 3 or more pieces of the same kind.
5058 int piecesLeft[(int)BlackPawn];
5059 int seed, nrOfShuffles;
5061 void GetPositionNumber()
5062 { // sets global variable seed
5065 seed = appData.defaultFrcPosition;
5066 if(seed < 0) { // randomize based on time for negative FRC position numbers
5067 for(i=0; i<50; i++) seed += random();
5068 seed = random() ^ random() >> 8 ^ random() << 8;
5069 if(seed<0) seed = -seed;
5073 int put(Board board, int pieceType, int rank, int n, int shade)
5074 // put the piece on the (n-1)-th empty squares of the given shade
5078 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) {
5079 if( (((i-BOARD_LEFT)&1)+1) & shade && board[rank][i] == EmptySquare && n-- == 0) {
5080 board[rank][i] = (ChessSquare) pieceType;
5081 squaresLeft[((i-BOARD_LEFT)&1) + 1]--;
5083 piecesLeft[pieceType]--;
5091 void AddOnePiece(Board board, int pieceType, int rank, int shade)
5092 // calculate where the next piece goes, (any empty square), and put it there
5096 i = seed % squaresLeft[shade];
5097 nrOfShuffles *= squaresLeft[shade];
5098 seed /= squaresLeft[shade];
5099 put(board, pieceType, rank, i, shade);
5102 void AddTwoPieces(Board board, int pieceType, int rank)
5103 // calculate where the next 2 identical pieces go, (any empty square), and put it there
5105 int i, n=squaresLeft[ANY], j=n-1, k;
5107 k = n*(n-1)/2; // nr of possibilities, not counting permutations
5108 i = seed % k; // pick one
5111 while(i >= j) i -= j--;
5112 j = n - 1 - j; i += j;
5113 put(board, pieceType, rank, j, ANY);
5114 put(board, pieceType, rank, i, ANY);
5117 void SetUpShuffle(Board board, int number)
5121 GetPositionNumber(); nrOfShuffles = 1;
5123 squaresLeft[DARK] = (BOARD_RGHT - BOARD_LEFT + 1)/2;
5124 squaresLeft[ANY] = BOARD_RGHT - BOARD_LEFT;
5125 squaresLeft[LITE] = squaresLeft[ANY] - squaresLeft[DARK];
5127 for(p = 0; p<=(int)WhiteKing; p++) piecesLeft[p] = 0;
5129 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // count pieces and clear board
5130 p = (int) board[0][i];
5131 if(p < (int) BlackPawn) piecesLeft[p] ++;
5132 board[0][i] = EmptySquare;
5135 if(PosFlags(0) & F_ALL_CASTLE_OK) {
5136 // shuffles restricted to allow normal castling put KRR first
5137 if(piecesLeft[(int)WhiteKing]) // King goes rightish of middle
5138 put(board, WhiteKing, 0, (gameInfo.boardWidth+1)/2, ANY);
5139 else if(piecesLeft[(int)WhiteUnicorn]) // in Knightmate Unicorn castles
5140 put(board, WhiteUnicorn, 0, (gameInfo.boardWidth+1)/2, ANY);
5141 if(piecesLeft[(int)WhiteRook]) // First supply a Rook for K-side castling
5142 put(board, WhiteRook, 0, gameInfo.boardWidth-2, ANY);
5143 if(piecesLeft[(int)WhiteRook]) // Then supply a Rook for Q-side castling
5144 put(board, WhiteRook, 0, 0, ANY);
5145 // in variants with super-numerary Kings and Rooks, we leave these for the shuffle
5148 if(((BOARD_RGHT-BOARD_LEFT) & 1) == 0)
5149 // only for even boards make effort to put pairs of colorbound pieces on opposite colors
5150 for(p = (int) WhiteKing; p > (int) WhitePawn; p--) {
5151 if(p != (int) WhiteBishop && p != (int) WhiteFerz && p != (int) WhiteAlfil) continue;
5152 while(piecesLeft[p] >= 2) {
5153 AddOnePiece(board, p, 0, LITE);
5154 AddOnePiece(board, p, 0, DARK);
5156 // Odd color-bound pieces are shuffled with the rest (to not run out of paired squares)
5159 for(p = (int) WhiteKing - 2; p > (int) WhitePawn; p--) {
5160 // Remaining pieces (non-colorbound, or odd color bound) can be put anywhere
5161 // but we leave King and Rooks for last, to possibly obey FRC restriction
5162 if(p == (int)WhiteRook) continue;
5163 while(piecesLeft[p] >= 2) AddTwoPieces(board, p, 0); // add in pairs, for not counting permutations
5164 if(piecesLeft[p]) AddOnePiece(board, p, 0, ANY); // add the odd piece
5167 // now everything is placed, except perhaps King (Unicorn) and Rooks
5169 if(PosFlags(0) & F_FRC_TYPE_CASTLING) {
5170 // Last King gets castling rights
5171 while(piecesLeft[(int)WhiteUnicorn]) {
5172 i = put(board, WhiteUnicorn, 0, piecesLeft[(int)WhiteRook]/2, ANY);
5173 initialRights[2] = initialRights[5] = board[CASTLING][2] = board[CASTLING][5] = i;
5176 while(piecesLeft[(int)WhiteKing]) {
5177 i = put(board, WhiteKing, 0, piecesLeft[(int)WhiteRook]/2, ANY);
5178 initialRights[2] = initialRights[5] = board[CASTLING][2] = board[CASTLING][5] = i;
5183 while(piecesLeft[(int)WhiteKing]) AddOnePiece(board, WhiteKing, 0, ANY);
5184 while(piecesLeft[(int)WhiteUnicorn]) AddOnePiece(board, WhiteUnicorn, 0, ANY);
5187 // Only Rooks can be left; simply place them all
5188 while(piecesLeft[(int)WhiteRook]) {
5189 i = put(board, WhiteRook, 0, 0, ANY);
5190 if(PosFlags(0) & F_FRC_TYPE_CASTLING) { // first and last Rook get FRC castling rights
5193 initialRights[1] = initialRights[4] = board[CASTLING][1] = board[CASTLING][4] = i;
5195 initialRights[0] = initialRights[3] = board[CASTLING][0] = board[CASTLING][3] = i;
5198 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // copy black from white
5199 board[BOARD_HEIGHT-1][i] = (int) board[0][i] < BlackPawn ? WHITE_TO_BLACK board[0][i] : EmptySquare;
5202 if(number >= 0) appData.defaultFrcPosition %= nrOfShuffles; // normalize
5205 int SetCharTable( char *table, const char * map )
5206 /* [HGM] moved here from winboard.c because of its general usefulness */
5207 /* Basically a safe strcpy that uses the last character as King */
5209 int result = FALSE; int NrPieces;
5211 if( map != NULL && (NrPieces=strlen(map)) <= (int) EmptySquare
5212 && NrPieces >= 12 && !(NrPieces&1)) {
5213 int i; /* [HGM] Accept even length from 12 to 34 */
5215 for( i=0; i<(int) EmptySquare; i++ ) table[i] = '.';
5216 for( i=0; i<NrPieces/2-1; i++ ) {
5218 table[i + (int)BlackPawn - (int) WhitePawn] = map[i+NrPieces/2];
5220 table[(int) WhiteKing] = map[NrPieces/2-1];
5221 table[(int) BlackKing] = map[NrPieces-1];
5229 void Prelude(Board board)
5230 { // [HGM] superchess: random selection of exo-pieces
5231 int i, j, k; ChessSquare p;
5232 static ChessSquare exoPieces[4] = { WhiteAngel, WhiteMarshall, WhiteSilver, WhiteLance };
5234 GetPositionNumber(); // use FRC position number
5236 if(appData.pieceToCharTable != NULL) { // select pieces to participate from given char table
5237 SetCharTable(pieceToChar, appData.pieceToCharTable);
5238 for(i=(int)WhiteQueen+1, j=0; i<(int)WhiteKing && j<4; i++)
5239 if(PieceToChar((ChessSquare)i) != '.') exoPieces[j++] = (ChessSquare) i;
5242 j = seed%4; seed /= 4;
5243 p = board[0][BOARD_LEFT+j]; board[0][BOARD_LEFT+j] = EmptySquare; k = PieceToNumber(p);
5244 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5245 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5246 j = seed%3 + (seed%3 >= j); seed /= 3;
5247 p = board[0][BOARD_LEFT+j]; board[0][BOARD_LEFT+j] = EmptySquare; k = PieceToNumber(p);
5248 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5249 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5250 j = seed%3; seed /= 3;
5251 p = board[0][BOARD_LEFT+j+5]; board[0][BOARD_LEFT+j+5] = EmptySquare; k = PieceToNumber(p);
5252 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5253 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5254 j = seed%2 + (seed%2 >= j); seed /= 2;
5255 p = board[0][BOARD_LEFT+j+5]; board[0][BOARD_LEFT+j+5] = EmptySquare; k = PieceToNumber(p);
5256 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5257 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5258 j = seed%4; seed /= 4; put(board, exoPieces[3], 0, j, ANY);
5259 j = seed%3; seed /= 3; put(board, exoPieces[2], 0, j, ANY);
5260 j = seed%2; seed /= 2; put(board, exoPieces[1], 0, j, ANY);
5261 put(board, exoPieces[0], 0, 0, ANY);
5262 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) board[BOARD_HEIGHT-1][i] = WHITE_TO_BLACK board[0][i];
5266 InitPosition(redraw)
5269 ChessSquare (* pieces)[BOARD_FILES];
5270 int i, j, pawnRow, overrule,
5271 oldx = gameInfo.boardWidth,
5272 oldy = gameInfo.boardHeight,
5273 oldh = gameInfo.holdingsWidth,
5274 oldv = gameInfo.variant;
5276 if(appData.icsActive) shuffleOpenings = FALSE; // [HGM] shuffle: in ICS mode, only shuffle on ICS request
5278 /* [AS] Initialize pv info list [HGM] and game status */
5280 for( i=0; i<=framePtr; i++ ) { // [HGM] vari: spare saved variations
5281 pvInfoList[i].depth = 0;
5282 boards[i][EP_STATUS] = EP_NONE;
5283 for( j=0; j<BOARD_FILES-2; j++ ) boards[i][CASTLING][j] = NoRights;
5286 initialRulePlies = 0; /* 50-move counter start */
5288 castlingRank[0] = castlingRank[1] = castlingRank[2] = 0;
5289 castlingRank[3] = castlingRank[4] = castlingRank[5] = BOARD_HEIGHT-1;
5293 /* [HGM] logic here is completely changed. In stead of full positions */
5294 /* the initialized data only consist of the two backranks. The switch */
5295 /* selects which one we will use, which is than copied to the Board */
5296 /* initialPosition, which for the rest is initialized by Pawns and */
5297 /* empty squares. This initial position is then copied to boards[0], */
5298 /* possibly after shuffling, so that it remains available. */
5300 gameInfo.holdingsWidth = 0; /* default board sizes */
5301 gameInfo.boardWidth = 8;
5302 gameInfo.boardHeight = 8;
5303 gameInfo.holdingsSize = 0;
5304 nrCastlingRights = -1; /* [HGM] Kludge to indicate default should be used */
5305 for(i=0; i<BOARD_FILES-2; i++)
5306 initialPosition[CASTLING][i] = initialRights[i] = NoRights; /* but no rights yet */
5307 initialPosition[EP_STATUS] = EP_NONE;
5308 SetCharTable(pieceToChar, "PNBRQ...........Kpnbrq...........k");
5309 if(startVariant == gameInfo.variant) // [HGM] nicks: enable nicknames in original variant
5310 SetCharTable(pieceNickName, appData.pieceNickNames);
5311 else SetCharTable(pieceNickName, "............");
5313 switch (gameInfo.variant) {
5314 case VariantFischeRandom:
5315 shuffleOpenings = TRUE;
5319 case VariantShatranj:
5320 pieces = ShatranjArray;
5321 nrCastlingRights = 0;
5322 SetCharTable(pieceToChar, "PN.R.QB...Kpn.r.qb...k");
5325 pieces = makrukArray;
5326 nrCastlingRights = 0;
5327 startedFromSetupPosition = TRUE;
5328 SetCharTable(pieceToChar, "PN.R.M....SKpn.r.m....sk");
5330 case VariantTwoKings:
5331 pieces = twoKingsArray;
5333 case VariantCapaRandom:
5334 shuffleOpenings = TRUE;
5335 case VariantCapablanca:
5336 pieces = CapablancaArray;
5337 gameInfo.boardWidth = 10;
5338 SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
5341 pieces = GothicArray;
5342 gameInfo.boardWidth = 10;
5343 SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
5346 pieces = JanusArray;
5347 gameInfo.boardWidth = 10;
5348 SetCharTable(pieceToChar, "PNBRQ..JKpnbrq..jk");
5349 nrCastlingRights = 6;
5350 initialPosition[CASTLING][0] = initialRights[0] = BOARD_RGHT-1;
5351 initialPosition[CASTLING][1] = initialRights[1] = BOARD_LEFT;
5352 initialPosition[CASTLING][2] = initialRights[2] =(BOARD_WIDTH-1)>>1;
5353 initialPosition[CASTLING][3] = initialRights[3] = BOARD_RGHT-1;
5354 initialPosition[CASTLING][4] = initialRights[4] = BOARD_LEFT;
5355 initialPosition[CASTLING][5] = initialRights[5] =(BOARD_WIDTH-1)>>1;
5358 pieces = FalconArray;
5359 gameInfo.boardWidth = 10;
5360 SetCharTable(pieceToChar, "PNBRQ.............FKpnbrq.............fk");
5362 case VariantXiangqi:
5363 pieces = XiangqiArray;
5364 gameInfo.boardWidth = 9;
5365 gameInfo.boardHeight = 10;
5366 nrCastlingRights = 0;
5367 SetCharTable(pieceToChar, "PH.R.AE..K.C.ph.r.ae..k.c.");
5370 pieces = ShogiArray;
5371 gameInfo.boardWidth = 9;
5372 gameInfo.boardHeight = 9;
5373 gameInfo.holdingsSize = 7;
5374 nrCastlingRights = 0;
5375 SetCharTable(pieceToChar, "PNBRLS...G.++++++Kpnbrls...g.++++++k");
5377 case VariantCourier:
5378 pieces = CourierArray;
5379 gameInfo.boardWidth = 12;
5380 nrCastlingRights = 0;
5381 SetCharTable(pieceToChar, "PNBR.FE..WMKpnbr.fe..wmk");
5383 case VariantKnightmate:
5384 pieces = KnightmateArray;
5385 SetCharTable(pieceToChar, "P.BRQ.....M.........K.p.brq.....m.........k.");
5388 pieces = fairyArray;
5389 SetCharTable(pieceToChar, "PNBRQFEACWMOHIJGDVLSUKpnbrqfeacwmohijgdvlsuk");
5392 pieces = GreatArray;
5393 gameInfo.boardWidth = 10;
5394 SetCharTable(pieceToChar, "PN....E...S..HWGMKpn....e...s..hwgmk");
5395 gameInfo.holdingsSize = 8;
5399 SetCharTable(pieceToChar, "PNBRQ..SE.......V.AKpnbrq..se.......v.ak");
5400 gameInfo.holdingsSize = 8;
5401 startedFromSetupPosition = TRUE;
5403 case VariantCrazyhouse:
5404 case VariantBughouse:
5406 SetCharTable(pieceToChar, "PNBRQ.......~~~~Kpnbrq.......~~~~k");
5407 gameInfo.holdingsSize = 5;
5409 case VariantWildCastle:
5411 /* !!?shuffle with kings guaranteed to be on d or e file */
5412 shuffleOpenings = 1;
5414 case VariantNoCastle:
5416 nrCastlingRights = 0;
5417 /* !!?unconstrained back-rank shuffle */
5418 shuffleOpenings = 1;
5423 if(appData.NrFiles >= 0) {
5424 if(gameInfo.boardWidth != appData.NrFiles) overrule++;
5425 gameInfo.boardWidth = appData.NrFiles;
5427 if(appData.NrRanks >= 0) {
5428 gameInfo.boardHeight = appData.NrRanks;
5430 if(appData.holdingsSize >= 0) {
5431 i = appData.holdingsSize;
5432 if(i > gameInfo.boardHeight) i = gameInfo.boardHeight;
5433 gameInfo.holdingsSize = i;
5435 if(gameInfo.holdingsSize) gameInfo.holdingsWidth = 2;
5436 if(BOARD_HEIGHT > BOARD_RANKS || BOARD_WIDTH > BOARD_FILES)
5437 DisplayFatalError(_("Recompile to support this BOARD_RANKS or BOARD_FILES!"), 0, 2);
5439 pawnRow = gameInfo.boardHeight - 7; /* seems to work in all common variants */
5440 if(pawnRow < 1) pawnRow = 1;
5441 if(gameInfo.variant == VariantMakruk) pawnRow = 2;
5443 /* User pieceToChar list overrules defaults */
5444 if(appData.pieceToCharTable != NULL)
5445 SetCharTable(pieceToChar, appData.pieceToCharTable);
5447 for( j=0; j<BOARD_WIDTH; j++ ) { ChessSquare s = EmptySquare;
5449 if(j==BOARD_LEFT-1 || j==BOARD_RGHT)
5450 s = (ChessSquare) 0; /* account holding counts in guard band */
5451 for( i=0; i<BOARD_HEIGHT; i++ )
5452 initialPosition[i][j] = s;
5454 if(j < BOARD_LEFT || j >= BOARD_RGHT || overrule) continue;
5455 initialPosition[0][j] = pieces[0][j-gameInfo.holdingsWidth];
5456 initialPosition[pawnRow][j] = WhitePawn;
5457 initialPosition[BOARD_HEIGHT-pawnRow-1][j] = BlackPawn;
5458 if(gameInfo.variant == VariantXiangqi) {
5460 initialPosition[pawnRow][j] =
5461 initialPosition[BOARD_HEIGHT-pawnRow-1][j] = EmptySquare;
5462 if(j==BOARD_LEFT+1 || j>=BOARD_RGHT-2) {
5463 initialPosition[2][j] = WhiteCannon;
5464 initialPosition[BOARD_HEIGHT-3][j] = BlackCannon;
5468 initialPosition[BOARD_HEIGHT-1][j] = pieces[1][j-gameInfo.holdingsWidth];
5470 if( (gameInfo.variant == VariantShogi) && !overrule ) {
5473 initialPosition[1][j] = WhiteBishop;
5474 initialPosition[BOARD_HEIGHT-2][j] = BlackRook;
5476 initialPosition[1][j] = WhiteRook;
5477 initialPosition[BOARD_HEIGHT-2][j] = BlackBishop;
5480 if( nrCastlingRights == -1) {
5481 /* [HGM] Build normal castling rights (must be done after board sizing!) */
5482 /* This sets default castling rights from none to normal corners */
5483 /* Variants with other castling rights must set them themselves above */
5484 nrCastlingRights = 6;
5486 initialPosition[CASTLING][0] = initialRights[0] = BOARD_RGHT-1;
5487 initialPosition[CASTLING][1] = initialRights[1] = BOARD_LEFT;
5488 initialPosition[CASTLING][2] = initialRights[2] = BOARD_WIDTH>>1;
5489 initialPosition[CASTLING][3] = initialRights[3] = BOARD_RGHT-1;
5490 initialPosition[CASTLING][4] = initialRights[4] = BOARD_LEFT;
5491 initialPosition[CASTLING][5] = initialRights[5] = BOARD_WIDTH>>1;
5494 if(gameInfo.variant == VariantSuper) Prelude(initialPosition);
5495 if(gameInfo.variant == VariantGreat) { // promotion commoners
5496 initialPosition[PieceToNumber(WhiteMan)][BOARD_WIDTH-1] = WhiteMan;
5497 initialPosition[PieceToNumber(WhiteMan)][BOARD_WIDTH-2] = 9;
5498 initialPosition[BOARD_HEIGHT-1-PieceToNumber(WhiteMan)][0] = BlackMan;
5499 initialPosition[BOARD_HEIGHT-1-PieceToNumber(WhiteMan)][1] = 9;
5501 if (appData.debugMode) {
5502 fprintf(debugFP, "shuffleOpenings = %d\n", shuffleOpenings);
5504 if(shuffleOpenings) {
5505 SetUpShuffle(initialPosition, appData.defaultFrcPosition);
5506 startedFromSetupPosition = TRUE;
5508 if(startedFromPositionFile) {
5509 /* [HGM] loadPos: use PositionFile for every new game */
5510 CopyBoard(initialPosition, filePosition);
5511 for(i=0; i<nrCastlingRights; i++)
5512 initialRights[i] = filePosition[CASTLING][i];
5513 startedFromSetupPosition = TRUE;
5516 CopyBoard(boards[0], initialPosition);
5518 if(oldx != gameInfo.boardWidth ||
5519 oldy != gameInfo.boardHeight ||
5520 oldh != gameInfo.holdingsWidth
5522 || oldv == VariantGothic || // For licensing popups
5523 gameInfo.variant == VariantGothic
5526 || oldv == VariantFalcon ||
5527 gameInfo.variant == VariantFalcon
5530 InitDrawingSizes(-2 ,0);
5533 DrawPosition(TRUE, boards[currentMove]);
5537 SendBoard(cps, moveNum)
5538 ChessProgramState *cps;
5541 char message[MSG_SIZ];
5543 if (cps->useSetboard) {
5544 char* fen = PositionToFEN(moveNum, cps->fenOverride);
5545 snprintf(message, MSG_SIZ,"setboard %s\n", fen);
5546 SendToProgram(message, cps);
5552 /* Kludge to set black to move, avoiding the troublesome and now
5553 * deprecated "black" command.
5555 if (!WhiteOnMove(moveNum)) SendToProgram("a2a3\n", cps);
5557 SendToProgram("edit\n", cps);
5558 SendToProgram("#\n", cps);
5559 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
5560 bp = &boards[moveNum][i][BOARD_LEFT];
5561 for (j = BOARD_LEFT; j < BOARD_RGHT; j++, bp++) {
5562 if ((int) *bp < (int) BlackPawn) {
5563 snprintf(message, MSG_SIZ, "%c%c%c\n", PieceToChar(*bp),
5565 if(message[0] == '+' || message[0] == '~') {
5566 snprintf(message, MSG_SIZ,"%c%c%c+\n",
5567 PieceToChar((ChessSquare)(DEMOTED *bp)),
5570 if(cps->alphaRank) { /* [HGM] shogi: translate coords */
5571 message[1] = BOARD_RGHT - 1 - j + '1';
5572 message[2] = BOARD_HEIGHT - 1 - i + 'a';
5574 SendToProgram(message, cps);
5579 SendToProgram("c\n", cps);
5580 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
5581 bp = &boards[moveNum][i][BOARD_LEFT];
5582 for (j = BOARD_LEFT; j < BOARD_RGHT; j++, bp++) {
5583 if (((int) *bp != (int) EmptySquare)
5584 && ((int) *bp >= (int) BlackPawn)) {
5585 snprintf(message,MSG_SIZ, "%c%c%c\n", ToUpper(PieceToChar(*bp)),
5587 if(message[0] == '+' || message[0] == '~') {
5588 snprintf(message, MSG_SIZ,"%c%c%c+\n",
5589 PieceToChar((ChessSquare)(DEMOTED *bp)),
5592 if(cps->alphaRank) { /* [HGM] shogi: translate coords */
5593 message[1] = BOARD_RGHT - 1 - j + '1';
5594 message[2] = BOARD_HEIGHT - 1 - i + 'a';
5596 SendToProgram(message, cps);
5601 SendToProgram(".\n", cps);
5603 setboardSpoiledMachineBlack = 0; /* [HGM] assume WB 4.2.7 already solves this after sending setboard */
5606 static int autoQueen; // [HGM] oneclick
5609 HasPromotionChoice(int fromX, int fromY, int toX, int toY, char *promoChoice)
5611 /* [HGM] rewritten IsPromotion to only flag promotions that offer a choice */
5612 /* [HGM] add Shogi promotions */
5613 int promotionZoneSize=1, highestPromotingPiece = (int)WhitePawn;
5618 if(fromX < BOARD_LEFT || fromX >= BOARD_RGHT) return FALSE; // drop
5619 if(toX < BOARD_LEFT || toX >= BOARD_RGHT) return FALSE; // move into holdings
5621 if(gameMode == EditPosition || gameInfo.variant == VariantXiangqi || // no promotions
5622 !(fromX >=0 && fromY >= 0 && toX >= 0 && toY >= 0) ) // invalid move
5625 piece = boards[currentMove][fromY][fromX];
5626 if(gameInfo.variant == VariantShogi) {
5627 promotionZoneSize = BOARD_HEIGHT/3;
5628 highestPromotingPiece = (int)WhiteFerz;
5629 } else if(gameInfo.variant == VariantMakruk) {
5630 promotionZoneSize = 3;
5633 // next weed out all moves that do not touch the promotion zone at all
5634 if((int)piece >= BlackPawn) {
5635 if(toY >= promotionZoneSize && fromY >= promotionZoneSize)
5637 highestPromotingPiece = WHITE_TO_BLACK highestPromotingPiece;
5639 if( toY < BOARD_HEIGHT - promotionZoneSize &&
5640 fromY < BOARD_HEIGHT - promotionZoneSize) return FALSE;
5643 if( (int)piece > highestPromotingPiece ) return FALSE; // non-promoting piece
5645 // weed out mandatory Shogi promotions
5646 if(gameInfo.variant == VariantShogi) {
5647 if(piece >= BlackPawn) {
5648 if(toY == 0 && piece == BlackPawn ||
5649 toY == 0 && piece == BlackQueen ||
5650 toY <= 1 && piece == BlackKnight) {
5655 if(toY == BOARD_HEIGHT-1 && piece == WhitePawn ||
5656 toY == BOARD_HEIGHT-1 && piece == WhiteQueen ||
5657 toY >= BOARD_HEIGHT-2 && piece == WhiteKnight) {
5664 // weed out obviously illegal Pawn moves
5665 if(appData.testLegality && (piece == WhitePawn || piece == BlackPawn) ) {
5666 if(toX > fromX+1 || toX < fromX-1) return FALSE; // wide
5667 if(piece == WhitePawn && toY != fromY+1) return FALSE; // deep
5668 if(piece == BlackPawn && toY != fromY-1) return FALSE; // deep
5669 if(fromX != toX && gameInfo.variant == VariantShogi) return FALSE;
5670 // note we are not allowed to test for valid (non-)capture, due to premove
5673 // we either have a choice what to promote to, or (in Shogi) whether to promote
5674 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk) {
5675 *promoChoice = PieceToChar(BlackFerz); // no choice
5678 if(autoQueen) { // predetermined
5679 if(gameInfo.variant == VariantSuicide || gameInfo.variant == VariantLosers)
5680 *promoChoice = PieceToChar(BlackKing); // in Suicide Q is the last thing we want
5681 else *promoChoice = PieceToChar(BlackQueen);
5685 // suppress promotion popup on illegal moves that are not premoves
5686 premove = gameMode == IcsPlayingWhite && !WhiteOnMove(currentMove) ||
5687 gameMode == IcsPlayingBlack && WhiteOnMove(currentMove);
5688 if(appData.testLegality && !premove) {
5689 moveType = LegalityTest(boards[currentMove], PosFlags(currentMove),
5690 fromY, fromX, toY, toX, NULLCHAR);
5691 if(moveType != WhitePromotion && moveType != BlackPromotion)
5699 InPalace(row, column)
5701 { /* [HGM] for Xiangqi */
5702 if( (row < 3 || row > BOARD_HEIGHT-4) &&
5703 column < (BOARD_WIDTH + 4)/2 &&
5704 column > (BOARD_WIDTH - 5)/2 ) return TRUE;
5709 PieceForSquare (x, y)
5713 if (x < 0 || x >= BOARD_WIDTH || y < 0 || y >= BOARD_HEIGHT)
5716 return boards[currentMove][y][x];
5720 OKToStartUserMove(x, y)
5723 ChessSquare from_piece;
5726 if (matchMode) return FALSE;
5727 if (gameMode == EditPosition) return TRUE;
5729 if (x >= 0 && y >= 0)
5730 from_piece = boards[currentMove][y][x];
5732 from_piece = EmptySquare;
5734 if (from_piece == EmptySquare) return FALSE;
5736 white_piece = (int)from_piece >= (int)WhitePawn &&
5737 (int)from_piece < (int)BlackPawn; /* [HGM] can be > King! */
5740 case PlayFromGameFile:
5742 case TwoMachinesPlay:
5750 case MachinePlaysWhite:
5751 case IcsPlayingBlack:
5752 if (appData.zippyPlay) return FALSE;
5754 DisplayMoveError(_("You are playing Black"));
5759 case MachinePlaysBlack:
5760 case IcsPlayingWhite:
5761 if (appData.zippyPlay) return FALSE;
5763 DisplayMoveError(_("You are playing White"));
5769 if (!white_piece && WhiteOnMove(currentMove)) {
5770 DisplayMoveError(_("It is White's turn"));
5773 if (white_piece && !WhiteOnMove(currentMove)) {
5774 DisplayMoveError(_("It is Black's turn"));
5777 if (cmailMsgLoaded && (currentMove < cmailOldMove)) {
5778 /* Editing correspondence game history */
5779 /* Could disallow this or prompt for confirmation */
5784 case BeginningOfGame:
5785 if (appData.icsActive) return FALSE;
5786 if (!appData.noChessProgram) {
5788 DisplayMoveError(_("You are playing White"));
5795 if (!white_piece && WhiteOnMove(currentMove)) {
5796 DisplayMoveError(_("It is White's turn"));
5799 if (white_piece && !WhiteOnMove(currentMove)) {
5800 DisplayMoveError(_("It is Black's turn"));
5809 if (currentMove != forwardMostMove && gameMode != AnalyzeMode
5810 && gameMode != EditGame // [HGM] vari: treat as AnalyzeMode
5811 && gameMode != AnalyzeFile && gameMode != Training) {
5812 DisplayMoveError(_("Displayed position is not current"));
5819 OnlyMove(int *x, int *y, Boolean captures) {
5820 DisambiguateClosure cl;
5821 if (appData.zippyPlay) return FALSE;
5823 case MachinePlaysBlack:
5824 case IcsPlayingWhite:
5825 case BeginningOfGame:
5826 if(!WhiteOnMove(currentMove)) return FALSE;
5828 case MachinePlaysWhite:
5829 case IcsPlayingBlack:
5830 if(WhiteOnMove(currentMove)) return FALSE;
5835 cl.pieceIn = EmptySquare;
5840 cl.promoCharIn = NULLCHAR;
5841 Disambiguate(boards[currentMove], PosFlags(currentMove), &cl);
5842 if( cl.kind == NormalMove ||
5843 cl.kind == AmbiguousMove && captures && cl.captures == 1 ||
5844 cl.kind == WhitePromotion || cl.kind == BlackPromotion ||
5845 cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) {
5852 if(cl.kind != ImpossibleMove) return FALSE;
5853 cl.pieceIn = EmptySquare;
5858 cl.promoCharIn = NULLCHAR;
5859 Disambiguate(boards[currentMove], PosFlags(currentMove), &cl);
5860 if( cl.kind == NormalMove ||
5861 cl.kind == AmbiguousMove && captures && cl.captures == 1 ||
5862 cl.kind == WhitePromotion || cl.kind == BlackPromotion ||
5863 cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) {
5868 autoQueen = TRUE; // act as if autoQueen on when we click to-square
5874 FILE *lastLoadGameFP = NULL, *lastLoadPositionFP = NULL;
5875 int lastLoadGameNumber = 0, lastLoadPositionNumber = 0;
5876 int lastLoadGameUseList = FALSE;
5877 char lastLoadGameTitle[MSG_SIZ], lastLoadPositionTitle[MSG_SIZ];
5878 ChessMove lastLoadGameStart = EndOfFile;
5881 UserMoveEvent(fromX, fromY, toX, toY, promoChar)
5882 int fromX, fromY, toX, toY;
5886 ChessSquare pdown, pup;
5888 /* Check if the user is playing in turn. This is complicated because we
5889 let the user "pick up" a piece before it is his turn. So the piece he
5890 tried to pick up may have been captured by the time he puts it down!
5891 Therefore we use the color the user is supposed to be playing in this
5892 test, not the color of the piece that is currently on the starting
5893 square---except in EditGame mode, where the user is playing both
5894 sides; fortunately there the capture race can't happen. (It can
5895 now happen in IcsExamining mode, but that's just too bad. The user
5896 will get a somewhat confusing message in that case.)
5900 case PlayFromGameFile:
5902 case TwoMachinesPlay:
5906 /* We switched into a game mode where moves are not accepted,
5907 perhaps while the mouse button was down. */
5910 case MachinePlaysWhite:
5911 /* User is moving for Black */
5912 if (WhiteOnMove(currentMove)) {
5913 DisplayMoveError(_("It is White's turn"));
5918 case MachinePlaysBlack:
5919 /* User is moving for White */
5920 if (!WhiteOnMove(currentMove)) {
5921 DisplayMoveError(_("It is Black's turn"));
5928 case BeginningOfGame:
5931 if ((int) boards[currentMove][fromY][fromX] >= (int) BlackPawn &&
5932 (int) boards[currentMove][fromY][fromX] < (int) EmptySquare) {
5933 /* User is moving for Black */
5934 if (WhiteOnMove(currentMove)) {
5935 DisplayMoveError(_("It is White's turn"));
5939 /* User is moving for White */
5940 if (!WhiteOnMove(currentMove)) {
5941 DisplayMoveError(_("It is Black's turn"));
5947 case IcsPlayingBlack:
5948 /* User is moving for Black */
5949 if (WhiteOnMove(currentMove)) {
5950 if (!appData.premove) {
5951 DisplayMoveError(_("It is White's turn"));
5952 } else if (toX >= 0 && toY >= 0) {
5955 premoveFromX = fromX;
5956 premoveFromY = fromY;
5957 premovePromoChar = promoChar;
5959 if (appData.debugMode)
5960 fprintf(debugFP, "Got premove: fromX %d,"
5961 "fromY %d, toX %d, toY %d\n",
5962 fromX, fromY, toX, toY);
5968 case IcsPlayingWhite:
5969 /* User is moving for White */
5970 if (!WhiteOnMove(currentMove)) {
5971 if (!appData.premove) {
5972 DisplayMoveError(_("It is Black's turn"));
5973 } else if (toX >= 0 && toY >= 0) {
5976 premoveFromX = fromX;
5977 premoveFromY = fromY;
5978 premovePromoChar = promoChar;
5980 if (appData.debugMode)
5981 fprintf(debugFP, "Got premove: fromX %d,"
5982 "fromY %d, toX %d, toY %d\n",
5983 fromX, fromY, toX, toY);
5993 /* EditPosition, empty square, or different color piece;
5994 click-click move is possible */
5995 if (toX == -2 || toY == -2) {
5996 boards[0][fromY][fromX] = EmptySquare;
5997 DrawPosition(FALSE, boards[currentMove]);
5999 } else if (toX >= 0 && toY >= 0) {
6000 boards[0][toY][toX] = boards[0][fromY][fromX];
6001 if(fromX == BOARD_LEFT-2) { // handle 'moves' out of holdings
6002 if(boards[0][fromY][0] != EmptySquare) {
6003 if(boards[0][fromY][1]) boards[0][fromY][1]--;
6004 if(boards[0][fromY][1] == 0) boards[0][fromY][0] = EmptySquare;
6007 if(fromX == BOARD_RGHT+1) {
6008 if(boards[0][fromY][BOARD_WIDTH-1] != EmptySquare) {
6009 if(boards[0][fromY][BOARD_WIDTH-2]) boards[0][fromY][BOARD_WIDTH-2]--;
6010 if(boards[0][fromY][BOARD_WIDTH-2] == 0) boards[0][fromY][BOARD_WIDTH-1] = EmptySquare;
6013 boards[0][fromY][fromX] = EmptySquare;
6014 DrawPosition(FALSE, boards[currentMove]);
6020 if(toX < 0 || toY < 0) return;
6021 pdown = boards[currentMove][fromY][fromX];
6022 pup = boards[currentMove][toY][toX];
6024 /* [HGM] If move started in holdings, it means a drop. Convert to standard form */
6025 if( fromX == BOARD_LEFT-2 || fromX == BOARD_RGHT+1) {
6026 if( pup != EmptySquare ) return;
6027 moveType = WhiteOnMove(currentMove) ? WhiteDrop : BlackDrop;
6028 if(appData.debugMode) fprintf(debugFP, "Drop move %d, curr=%d, x=%d,y=%d, p=%d\n",
6029 moveType, currentMove, fromX, fromY, boards[currentMove][fromY][fromX]);
6030 // holdings might not be sent yet in ICS play; we have to figure out which piece belongs here
6031 if(fromX == 0) fromY = BOARD_HEIGHT-1 - fromY; // black holdings upside-down
6032 fromX = fromX ? WhitePawn : BlackPawn; // first piece type in selected holdings
6033 while(PieceToChar(fromX) == '.' || PieceToNumber(fromX) != fromY && fromX != (int) EmptySquare) fromX++;
6037 /* [HGM] always test for legality, to get promotion info */
6038 moveType = LegalityTest(boards[currentMove], PosFlags(currentMove),
6039 fromY, fromX, toY, toX, promoChar);
6040 /* [HGM] but possibly ignore an IllegalMove result */
6041 if (appData.testLegality) {
6042 if (moveType == IllegalMove || moveType == ImpossibleMove) {
6043 DisplayMoveError(_("Illegal move"));
6048 FinishMove(moveType, fromX, fromY, toX, toY, promoChar);
6051 /* Common tail of UserMoveEvent and DropMenuEvent */
6053 FinishMove(moveType, fromX, fromY, toX, toY, promoChar)
6055 int fromX, fromY, toX, toY;
6056 /*char*/int promoChar;
6060 if((gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat) && promoChar != NULLCHAR) {
6061 // [HGM] superchess: suppress promotions to non-available piece
6062 int k = PieceToNumber(CharToPiece(ToUpper(promoChar)));
6063 if(WhiteOnMove(currentMove)) {
6064 if(!boards[currentMove][k][BOARD_WIDTH-2]) return 0;
6066 if(!boards[currentMove][BOARD_HEIGHT-1-k][1]) return 0;
6070 /* [HGM] <popupFix> kludge to avoid having to know the exact promotion
6071 move type in caller when we know the move is a legal promotion */
6072 if(moveType == NormalMove && promoChar)
6073 moveType = WhiteOnMove(currentMove) ? WhitePromotion : BlackPromotion;
6075 /* [HGM] <popupFix> The following if has been moved here from
6076 UserMoveEvent(). Because it seemed to belong here (why not allow
6077 piece drops in training games?), and because it can only be
6078 performed after it is known to what we promote. */
6079 if (gameMode == Training) {
6080 /* compare the move played on the board to the next move in the
6081 * game. If they match, display the move and the opponent's response.
6082 * If they don't match, display an error message.
6086 CopyBoard(testBoard, boards[currentMove]);
6087 ApplyMove(fromX, fromY, toX, toY, promoChar, testBoard);
6089 if (CompareBoards(testBoard, boards[currentMove+1])) {
6090 ForwardInner(currentMove+1);
6092 /* Autoplay the opponent's response.
6093 * if appData.animate was TRUE when Training mode was entered,
6094 * the response will be animated.
6096 saveAnimate = appData.animate;
6097 appData.animate = animateTraining;
6098 ForwardInner(currentMove+1);
6099 appData.animate = saveAnimate;
6101 /* check for the end of the game */
6102 if (currentMove >= forwardMostMove) {
6103 gameMode = PlayFromGameFile;
6105 SetTrainingModeOff();
6106 DisplayInformation(_("End of game"));
6109 DisplayError(_("Incorrect move"), 0);
6114 /* Ok, now we know that the move is good, so we can kill
6115 the previous line in Analysis Mode */
6116 if ((gameMode == AnalyzeMode || gameMode == EditGame)
6117 && currentMove < forwardMostMove) {
6118 PushTail(currentMove, forwardMostMove); // [HGM] vari: save tail of game
6121 /* If we need the chess program but it's dead, restart it */
6122 ResurrectChessProgram();
6124 /* A user move restarts a paused game*/
6128 thinkOutput[0] = NULLCHAR;
6130 MakeMove(fromX, fromY, toX, toY, promoChar); /*updates forwardMostMove*/
6132 if(Adjudicate(NULL)) return 1; // [HGM] adjudicate: take care of automtic game end
6134 if (gameMode == BeginningOfGame) {
6135 if (appData.noChessProgram) {
6136 gameMode = EditGame;
6140 gameMode = MachinePlaysBlack;
6143 snprintf(buf, MSG_SIZ, "%s vs. %s", gameInfo.white, gameInfo.black);
6145 if (first.sendName) {
6146 snprintf(buf, MSG_SIZ,"name %s\n", gameInfo.white);
6147 SendToProgram(buf, &first);
6154 /* Relay move to ICS or chess engine */
6155 if (appData.icsActive) {
6156 if (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
6157 gameMode == IcsExamining) {
6158 if(userOfferedDraw && (signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
6159 SendToICS(ics_prefix); // [HGM] drawclaim: send caim and move on one line for FICS
6161 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
6163 // also send plain move, in case ICS does not understand atomic claims
6164 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
6168 if (first.sendTime && (gameMode == BeginningOfGame ||
6169 gameMode == MachinePlaysWhite ||
6170 gameMode == MachinePlaysBlack)) {
6171 SendTimeRemaining(&first, gameMode != MachinePlaysBlack);
6173 if (gameMode != EditGame && gameMode != PlayFromGameFile) {
6174 // [HGM] book: if program might be playing, let it use book
6175 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, FALSE);
6176 first.maybeThinking = TRUE;
6177 } else SendMoveToProgram(forwardMostMove-1, &first);
6178 if (currentMove == cmailOldMove + 1) {
6179 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
6183 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6187 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
6193 if (WhiteOnMove(currentMove)) {
6194 GameEnds(BlackWins, "Black mates", GE_PLAYER);
6196 GameEnds(WhiteWins, "White mates", GE_PLAYER);
6200 GameEnds(GameIsDrawn, "Stalemate", GE_PLAYER);
6205 case MachinePlaysBlack:
6206 case MachinePlaysWhite:
6207 /* disable certain menu options while machine is thinking */
6208 SetMachineThinkingEnables();
6215 userOfferedDraw = FALSE; // [HGM] drawclaim: after move made, and tested for claimable draw
6217 if(bookHit) { // [HGM] book: simulate book reply
6218 static char bookMove[MSG_SIZ]; // a bit generous?
6220 programStats.nodes = programStats.depth = programStats.time =
6221 programStats.score = programStats.got_only_move = 0;
6222 sprintf(programStats.movelist, "%s (xbook)", bookHit);
6224 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
6225 strcat(bookMove, bookHit);
6226 HandleMachineMove(bookMove, &first);
6232 Mark(board, flags, kind, rf, ff, rt, ft, closure)
6239 typedef char Markers[BOARD_RANKS][BOARD_FILES];
6240 Markers *m = (Markers *) closure;
6241 if(rf == fromY && ff == fromX)
6242 (*m)[rt][ft] = 1 + (board[rt][ft] != EmptySquare
6243 || kind == WhiteCapturesEnPassant
6244 || kind == BlackCapturesEnPassant);
6245 else if(flags & F_MANDATORY_CAPTURE && board[rt][ft] != EmptySquare) (*m)[rt][ft] = 3;
6249 MarkTargetSquares(int clear)
6252 if(!appData.markers || !appData.highlightDragging ||
6253 !appData.testLegality || gameMode == EditPosition) return;
6255 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) marker[y][x] = 0;
6258 GenLegal(boards[currentMove], PosFlags(currentMove), Mark, (void*) marker);
6259 if(PosFlags(0) & F_MANDATORY_CAPTURE) {
6260 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) if(marker[y][x]>1) capt++;
6262 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) if(marker[y][x] == 1) marker[y][x] = 0;
6265 DrawPosition(TRUE, NULL);
6268 void LeftClick(ClickType clickType, int xPix, int yPix)
6271 Boolean saveAnimate;
6272 static int second = 0, promotionChoice = 0, dragging = 0;
6273 char promoChoice = NULLCHAR;
6275 if(appData.seekGraph && appData.icsActive && loggedOn &&
6276 (gameMode == BeginningOfGame || gameMode == IcsIdle)) {
6277 SeekGraphClick(clickType, xPix, yPix, 0);
6281 if (clickType == Press) ErrorPopDown();
6282 MarkTargetSquares(1);
6284 x = EventToSquare(xPix, BOARD_WIDTH);
6285 y = EventToSquare(yPix, BOARD_HEIGHT);
6286 if (!flipView && y >= 0) {
6287 y = BOARD_HEIGHT - 1 - y;
6289 if (flipView && x >= 0) {
6290 x = BOARD_WIDTH - 1 - x;
6293 if(promotionChoice) { // we are waiting for a click to indicate promotion piece
6294 if(clickType == Release) return; // ignore upclick of click-click destination
6295 promotionChoice = FALSE; // only one chance: if click not OK it is interpreted as cancel
6296 if(appData.debugMode) fprintf(debugFP, "promotion click, x=%d, y=%d\n", x, y);
6297 if(gameInfo.holdingsWidth &&
6298 (WhiteOnMove(currentMove)
6299 ? x == BOARD_WIDTH-1 && y < gameInfo.holdingsSize && y > 0
6300 : x == 0 && y >= BOARD_HEIGHT - gameInfo.holdingsSize && y < BOARD_HEIGHT-1) ) {
6301 // click in right holdings, for determining promotion piece
6302 ChessSquare p = boards[currentMove][y][x];
6303 if(appData.debugMode) fprintf(debugFP, "square contains %d\n", (int)p);
6304 if(p != EmptySquare) {
6305 FinishMove(NormalMove, fromX, fromY, toX, toY, ToLower(PieceToChar(p)));
6310 DrawPosition(FALSE, boards[currentMove]);
6314 /* [HGM] holdings: next 5 lines: ignore all clicks between board and holdings */
6315 if(clickType == Press
6316 && ( x == BOARD_LEFT-1 || x == BOARD_RGHT
6317 || x == BOARD_LEFT-2 && y < BOARD_HEIGHT-gameInfo.holdingsSize
6318 || x == BOARD_RGHT+1 && y >= gameInfo.holdingsSize) )
6321 autoQueen = appData.alwaysPromoteToQueen;
6324 if(!appData.oneClick || !OnlyMove(&x, &y, FALSE)) {
6325 if (clickType == Press) {
6327 if (OKToStartUserMove(x, y)) {
6331 MarkTargetSquares(0);
6332 DragPieceBegin(xPix, yPix); dragging = 1;
6333 if (appData.highlightDragging) {
6334 SetHighlights(x, y, -1, -1);
6337 } else if(dragging) { // [HGM] from-square must have been reset due to game end since last press
6338 DragPieceEnd(xPix, yPix); dragging = 0;
6339 DrawPosition(FALSE, NULL);
6346 if (clickType == Press && gameMode != EditPosition) {
6351 // ignore off-board to clicks
6352 if(y < 0 || x < 0) return;
6354 /* Check if clicking again on the same color piece */
6355 fromP = boards[currentMove][fromY][fromX];
6356 toP = boards[currentMove][y][x];
6357 frc = gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom;
6358 if ((WhitePawn <= fromP && fromP <= WhiteKing &&
6359 WhitePawn <= toP && toP <= WhiteKing &&
6360 !(fromP == WhiteKing && toP == WhiteRook && frc) &&
6361 !(fromP == WhiteRook && toP == WhiteKing && frc)) ||
6362 (BlackPawn <= fromP && fromP <= BlackKing &&
6363 BlackPawn <= toP && toP <= BlackKing &&
6364 !(fromP == BlackRook && toP == BlackKing && frc) && // allow also RxK as FRC castling
6365 !(fromP == BlackKing && toP == BlackRook && frc))) {
6366 /* Clicked again on same color piece -- changed his mind */
6367 second = (x == fromX && y == fromY);
6368 if(!second || appData.oneClick && !OnlyMove(&x, &y, TRUE)) {
6369 if (appData.highlightDragging) {
6370 SetHighlights(x, y, -1, -1);
6374 if (OKToStartUserMove(x, y)) {
6376 fromY = y; dragging = 1;
6377 MarkTargetSquares(0);
6378 DragPieceBegin(xPix, yPix);
6383 // ignore clicks on holdings
6384 if(x < BOARD_LEFT || x >= BOARD_RGHT) return;
6387 if (clickType == Release && x == fromX && y == fromY) {
6388 DragPieceEnd(xPix, yPix); dragging = 0;
6389 if (appData.animateDragging) {
6390 /* Undo animation damage if any */
6391 DrawPosition(FALSE, NULL);
6394 /* Second up/down in same square; just abort move */
6399 ClearPremoveHighlights();
6401 /* First upclick in same square; start click-click mode */
6402 SetHighlights(x, y, -1, -1);
6407 /* we now have a different from- and (possibly off-board) to-square */
6408 /* Completed move */
6411 saveAnimate = appData.animate;
6412 if (clickType == Press) {
6413 /* Finish clickclick move */
6414 if (appData.animate || appData.highlightLastMove) {
6415 SetHighlights(fromX, fromY, toX, toY);
6420 /* Finish drag move */
6421 if (appData.highlightLastMove) {
6422 SetHighlights(fromX, fromY, toX, toY);
6426 DragPieceEnd(xPix, yPix); dragging = 0;
6427 /* Don't animate move and drag both */
6428 appData.animate = FALSE;
6431 // moves into holding are invalid for now (except in EditPosition, adapting to-square)
6432 if(x >= 0 && x < BOARD_LEFT || x >= BOARD_RGHT) {
6433 ChessSquare piece = boards[currentMove][fromY][fromX];
6434 if(gameMode == EditPosition && piece != EmptySquare &&
6435 fromX >= BOARD_LEFT && fromX < BOARD_RGHT) {
6438 if(x == BOARD_LEFT-2 && piece >= BlackPawn) {
6439 n = PieceToNumber(piece - (int)BlackPawn);
6440 if(n >= gameInfo.holdingsSize) { n = 0; piece = BlackPawn; }
6441 boards[currentMove][BOARD_HEIGHT-1 - n][0] = piece;
6442 boards[currentMove][BOARD_HEIGHT-1 - n][1]++;
6444 if(x == BOARD_RGHT+1 && piece < BlackPawn) {
6445 n = PieceToNumber(piece);
6446 if(n >= gameInfo.holdingsSize) { n = 0; piece = WhitePawn; }
6447 boards[currentMove][n][BOARD_WIDTH-1] = piece;
6448 boards[currentMove][n][BOARD_WIDTH-2]++;
6450 boards[currentMove][fromY][fromX] = EmptySquare;
6454 DrawPosition(TRUE, boards[currentMove]);
6458 // off-board moves should not be highlighted
6459 if(x < 0 || x < 0) ClearHighlights();
6461 if (HasPromotionChoice(fromX, fromY, toX, toY, &promoChoice)) {
6462 SetHighlights(fromX, fromY, toX, toY);
6463 if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat) {
6464 // [HGM] super: promotion to captured piece selected from holdings
6465 ChessSquare p = boards[currentMove][fromY][fromX], q = boards[currentMove][toY][toX];
6466 promotionChoice = TRUE;
6467 // kludge follows to temporarily execute move on display, without promoting yet
6468 boards[currentMove][fromY][fromX] = EmptySquare; // move Pawn to 8th rank
6469 boards[currentMove][toY][toX] = p;
6470 DrawPosition(FALSE, boards[currentMove]);
6471 boards[currentMove][fromY][fromX] = p; // take back, but display stays
6472 boards[currentMove][toY][toX] = q;
6473 DisplayMessage("Click in holdings to choose piece", "");
6478 UserMoveEvent(fromX, fromY, toX, toY, promoChoice);
6479 if (!appData.highlightLastMove || gotPremove) ClearHighlights();
6480 if (gotPremove) SetPremoveHighlights(fromX, fromY, toX, toY);
6483 appData.animate = saveAnimate;
6484 if (appData.animate || appData.animateDragging) {
6485 /* Undo animation damage if needed */
6486 DrawPosition(FALSE, NULL);
6490 int RightClick(ClickType action, int x, int y, int *fromX, int *fromY)
6491 { // front-end-free part taken out of PieceMenuPopup
6492 int whichMenu; int xSqr, ySqr;
6494 if(seekGraphUp) { // [HGM] seekgraph
6495 if(action == Press) SeekGraphClick(Press, x, y, 2); // 2 indicates right-click: no pop-down on miss
6496 if(action == Release) SeekGraphClick(Release, x, y, 2); // and no challenge on hit
6500 if((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)
6501 && !appData.zippyPlay && appData.bgObserve) { // [HGM] bughouse: show background game
6502 if(!partnerBoardValid) return -2; // suppress display of uninitialized boards
6503 if( appData.dualBoard) return -2; // [HGM] dual: is already displayed
6504 if(action == Press) {
6505 originalFlip = flipView;
6506 flipView = !flipView; // temporarily flip board to see game from partners perspective
6507 DrawPosition(TRUE, partnerBoard);
6508 DisplayMessage(partnerStatus, "");
6510 } else if(action == Release) {
6511 flipView = originalFlip;
6512 DrawPosition(TRUE, boards[currentMove]);
6518 xSqr = EventToSquare(x, BOARD_WIDTH);
6519 ySqr = EventToSquare(y, BOARD_HEIGHT);
6520 if (action == Release) UnLoadPV(); // [HGM] pv
6521 if (action != Press) return -2; // return code to be ignored
6524 if(xSqr < BOARD_LEFT || xSqr >= BOARD_RGHT) return -1;
\r
6526 if (xSqr == BOARD_LEFT-1 || xSqr == BOARD_RGHT) return -1;
\r
6527 if (xSqr < 0 || ySqr < 0) return -1;
\r
6528 whichMenu = 0; // edit-position menu
6531 if(!appData.icsEngineAnalyze) return -1;
6532 case IcsPlayingWhite:
6533 case IcsPlayingBlack:
6534 if(!appData.zippyPlay) goto noZip;
6537 case MachinePlaysWhite:
6538 case MachinePlaysBlack:
6539 case TwoMachinesPlay: // [HGM] pv: use for showing PV
6540 if (!appData.dropMenu) {
6542 return 2; // flag front-end to grab mouse events
6544 if(gameMode == TwoMachinesPlay || gameMode == AnalyzeMode ||
6545 gameMode == AnalyzeFile || gameMode == IcsObserving) return -1;
6548 if (xSqr < 0 || ySqr < 0) return -1;
6549 if (!appData.dropMenu || appData.testLegality &&
6550 gameInfo.variant != VariantBughouse &&
6551 gameInfo.variant != VariantCrazyhouse) return -1;
6552 whichMenu = 1; // drop menu
6558 if (((*fromX = xSqr) < 0) ||
6559 ((*fromY = ySqr) < 0)) {
6560 *fromX = *fromY = -1;
6564 *fromX = BOARD_WIDTH - 1 - *fromX;
6566 *fromY = BOARD_HEIGHT - 1 - *fromY;
6571 void SendProgramStatsToFrontend( ChessProgramState * cps, ChessProgramStats * cpstats )
6573 // char * hint = lastHint;
6574 FrontEndProgramStats stats;
6576 stats.which = cps == &first ? 0 : 1;
6577 stats.depth = cpstats->depth;
6578 stats.nodes = cpstats->nodes;
6579 stats.score = cpstats->score;
6580 stats.time = cpstats->time;
6581 stats.pv = cpstats->movelist;
6582 stats.hint = lastHint;
6583 stats.an_move_index = 0;
6584 stats.an_move_count = 0;
6586 if( gameMode == AnalyzeMode || gameMode == AnalyzeFile ) {
6587 stats.hint = cpstats->move_name;
6588 stats.an_move_index = cpstats->nr_moves - cpstats->moves_left;
6589 stats.an_move_count = cpstats->nr_moves;
6592 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
6594 SetProgramStats( &stats );
6598 Count(Board board, int pCnt[], int *nW, int *nB, int *wStale, int *bStale, int *bishopColor)
6599 { // count all piece types
6601 *nB = *nW = *wStale = *bStale = *bishopColor = 0;
6602 for(p=WhitePawn; p<=EmptySquare; p++) pCnt[p] = 0;
6603 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
6606 if(p == WhitePawn && r == BOARD_HEIGHT-1) (*wStale)++; else
6607 if(p == BlackPawn && r == 0) (*bStale)++; // count last-Rank Pawns (XQ) separately
6608 if(p <= WhiteKing) (*nW)++; else if(p <= BlackKing) (*nB)++;
6609 if(p == WhiteBishop || p == WhiteFerz || p == WhiteAlfil ||
6610 p == BlackBishop || p == BlackFerz || p == BlackAlfil )
6611 *bishopColor |= 1 << ((f^r)&1); // track square color of color-bound pieces
6616 SufficientDefence(int pCnt[], int side, int nMine, int nHis)
6618 int myPawns = pCnt[WhitePawn+side]; // my total Pawn count;
6619 int majorDefense = pCnt[BlackRook-side] + pCnt[BlackCannon-side] + pCnt[BlackKnight-side];
6621 nMine -= pCnt[WhiteFerz+side] + pCnt[WhiteAlfil+side]; // discount defenders
6622 if(nMine - myPawns > 2) return FALSE; // no trivial draws with more than 1 major
6623 if(myPawns == 2 && nMine == 3) // KPP
6624 return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] >= 3;
6625 if(myPawns == 1 && nMine == 2) // KP
6626 return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] + pCnt[BlackPawn-side] >= 1;
6627 if(myPawns == 1 && nMine == 3 && pCnt[WhiteKnight+side]) // KHP
6628 return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side]*2 >= 5;
6629 if(myPawns) return FALSE;
6630 if(pCnt[WhiteRook+side])
6631 return pCnt[BlackRook-side] ||
6632 pCnt[BlackCannon-side] && (pCnt[BlackFerz-side] >= 2 || pCnt[BlackAlfil-side] >= 2) ||
6633 pCnt[BlackKnight-side] && pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] > 2 ||
6634 pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] >= 4;
6635 if(pCnt[WhiteCannon+side]) {
6636 if(pCnt[WhiteFerz+side] + myPawns == 0) return TRUE; // Cannon needs platform
6637 return majorDefense || pCnt[BlackAlfil-side] >= 2;
6639 if(pCnt[WhiteKnight+side])
6640 return majorDefense || pCnt[BlackFerz-side] >= 2 || pCnt[BlackAlfil-side] + pCnt[BlackPawn-side] >= 1;
6645 MatingPotential(int pCnt[], int side, int nMine, int nHis, int stale, int bisColor)
6647 VariantClass v = gameInfo.variant;
6649 if(v == VariantShogi || v == VariantCrazyhouse || v == VariantBughouse) return TRUE; // drop games always winnable
6650 if(v == VariantShatranj) return TRUE; // always winnable through baring
6651 if(v == VariantLosers || v == VariantSuicide || v == VariantGiveaway) return TRUE;
6652 if(v == Variant3Check || v == VariantAtomic) return nMine > 1; // can win through checking / exploding King
6654 if(v == VariantXiangqi) {
6655 int majors = 5*pCnt[BlackKnight-side] + 7*pCnt[BlackCannon-side] + 7*pCnt[BlackRook-side];
6657 nMine -= pCnt[WhiteFerz+side] + pCnt[WhiteAlfil+side] + stale; // discount defensive pieces and back-rank Pawns
6658 if(nMine + stale == 1) return (pCnt[BlackFerz-side] > 1 && pCnt[BlackKnight-side] > 0); // bare K can stalemate KHAA (!)
6659 if(nMine > 2) return TRUE; // if we don't have P, H or R, we must have CC
6660 if(nMine == 2 && pCnt[WhiteCannon+side] == 0) return TRUE; // We have at least one P, H or R
6661 // if we get here, we must have KC... or KP..., possibly with additional A, E or last-rank P
6662 if(stale) // we have at least one last-rank P plus perhaps C
6663 return majors // KPKX
6664 || pCnt[BlackFerz-side] && pCnt[BlackFerz-side] + pCnt[WhiteCannon+side] + stale > 2; // KPKAA, KPPKA and KCPKA
6666 return pCnt[WhiteFerz+side] // KCAK
6667 || pCnt[WhiteAlfil+side] && pCnt[BlackRook-side] + pCnt[BlackCannon-side] + pCnt[BlackFerz-side] // KCEKA, KCEKX (X!=H)
6668 || majors + (12*pCnt[BlackFerz-side] | 6*pCnt[BlackAlfil-side]) > 16; // KCKAA, KCKAX, KCKEEX, KCKEXX (XX!=HH), KCKXXX
6669 // TO DO: cases wih an unpromoted f-Pawn acting as platform for an opponent Cannon
6671 } else if(pCnt[WhiteKing] == 1 && pCnt[BlackKing] == 1) { // other variants with orthodox Kings
6672 int nBishops = pCnt[WhiteBishop+side] + pCnt[WhiteFerz+side];
6674 if(nMine == 1) return FALSE; // bare King
6675 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
6676 nMine += (nBishops > 0) - nBishops; // By now all Bishops (and Ferz) on like-colored squares, so count as one
6677 if(nMine > 2 && nMine != pCnt[WhiteAlfil+side] + 1) return TRUE; // At least two pieces, not all Alfils
6678 // by now we have King + 1 piece (or multiple Bishops on the same color)
6679 if(pCnt[WhiteKnight+side])
6680 return (pCnt[BlackKnight-side] + pCnt[BlackBishop-side] + pCnt[BlackMan-side] +
6681 pCnt[BlackWazir-side] + pCnt[BlackSilver-side] + bisColor // KNKN, KNKB, KNKF, KNKE, KNKW, KNKM, KNKS
6682 || nHis > 3); // be sure to cover suffocation mates in corner (e.g. KNKQCA)
6684 return (pCnt[BlackKnight-side]); // KBKN, KFKN
6685 if(pCnt[WhiteAlfil+side])
6686 return (nHis > 2); // Alfils can in general not reach a corner square, but there might be edge (suffocation) mates
6687 if(pCnt[WhiteWazir+side])
6688 return (pCnt[BlackKnight-side] + pCnt[BlackWazir-side] + pCnt[BlackAlfil-side]); // KWKN, KWKW, KWKE
6695 Adjudicate(ChessProgramState *cps)
6696 { // [HGM] some adjudications useful with buggy engines
6697 // [HGM] adjudicate: made into separate routine, which now can be called after every move
6698 // In any case it determnes if the game is a claimable draw (filling in EP_STATUS).
6699 // Actually ending the game is now based on the additional internal condition canAdjudicate.
6700 // Only when the game is ended, and the opponent is a computer, this opponent gets the move relayed.
6701 int k, count = 0; static int bare = 1;
6702 ChessProgramState *engineOpponent = (gameMode == TwoMachinesPlay ? cps->other : (cps ? NULL : &first));
6703 Boolean canAdjudicate = !appData.icsActive;
6705 // most tests only when we understand the game, i.e. legality-checking on
6706 if( appData.testLegality )
6707 { /* [HGM] Some more adjudications for obstinate engines */
6708 int nrW, nrB, bishopColor, staleW, staleB, nr[EmptySquare+1], i;
6709 static int moveCount = 6;
6711 char *reason = NULL;
6713 /* Count what is on board. */
6714 Count(boards[forwardMostMove], nr, &nrW, &nrB, &staleW, &staleB, &bishopColor);
6716 /* Some material-based adjudications that have to be made before stalemate test */
6717 if(gameInfo.variant == VariantAtomic && nr[WhiteKing] + nr[BlackKing] < 2) {
6718 // [HGM] atomic: stm must have lost his King on previous move, as destroying own K is illegal
6719 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE; // make claimable as if stm is checkmated
6720 if(canAdjudicate && appData.checkMates) {
6722 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets move
6723 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6724 GameEnds( WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins,
6725 "Xboard adjudication: King destroyed", GE_XBOARD );
6730 /* Bare King in Shatranj (loses) or Losers (wins) */
6731 if( nrW == 1 || nrB == 1) {
6732 if( gameInfo.variant == VariantLosers) { // [HGM] losers: bare King wins (stm must have it first)
6733 boards[forwardMostMove][EP_STATUS] = EP_WINS; // mark as win, so it becomes claimable
6734 if(canAdjudicate && appData.checkMates) {
6736 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets to see move
6737 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6738 GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
6739 "Xboard adjudication: Bare king", GE_XBOARD );
6743 if( gameInfo.variant == VariantShatranj && --bare < 0)
6745 boards[forwardMostMove][EP_STATUS] = EP_WINS; // make claimable as win for stm
6746 if(canAdjudicate && appData.checkMates) {
6747 /* but only adjudicate if adjudication enabled */
6749 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets move
6750 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6751 GameEnds( nrW > 1 ? WhiteWins : nrB > 1 ? BlackWins : GameIsDrawn,
6752 "Xboard adjudication: Bare king", GE_XBOARD );
6759 // don't wait for engine to announce game end if we can judge ourselves
6760 switch (MateTest(boards[forwardMostMove], PosFlags(forwardMostMove)) ) {
6762 if(gameInfo.variant == Variant3Check) { // [HGM] 3check: when in check, test if 3rd time
6763 int i, checkCnt = 0; // (should really be done by making nr of checks part of game state)
6764 for(i=forwardMostMove-2; i>=backwardMostMove; i-=2) {
6765 if(MateTest(boards[i], PosFlags(i)) == MT_CHECK)
6768 reason = "Xboard adjudication: 3rd check";
6769 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE;
6779 reason = "Xboard adjudication: Stalemate";
6780 if((signed char)boards[forwardMostMove][EP_STATUS] != EP_CHECKMATE) { // [HGM] don't touch win through baring or K-capt
6781 boards[forwardMostMove][EP_STATUS] = EP_STALEMATE; // default result for stalemate is draw
6782 if(gameInfo.variant == VariantLosers || gameInfo.variant == VariantGiveaway) // [HGM] losers:
6783 boards[forwardMostMove][EP_STATUS] = EP_WINS; // in these variants stalemated is always a win
6784 else if(gameInfo.variant == VariantSuicide) // in suicide it depends
6785 boards[forwardMostMove][EP_STATUS] = nrW == nrB ? EP_STALEMATE :
6786 ((nrW < nrB) != WhiteOnMove(forwardMostMove) ?
6787 EP_CHECKMATE : EP_WINS);
6788 else if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantXiangqi)
6789 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE; // and in these variants being stalemated loses
6793 reason = "Xboard adjudication: Checkmate";
6794 boards[forwardMostMove][EP_STATUS] = (gameInfo.variant == VariantLosers ? EP_WINS : EP_CHECKMATE);
6798 switch(i = (signed char)boards[forwardMostMove][EP_STATUS]) {
6800 result = GameIsDrawn; break;
6802 result = WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins; break;
6804 result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins; break;
6808 if(canAdjudicate && appData.checkMates && result) { // [HGM] mates: adjudicate finished games if requested
6810 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
6811 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6812 GameEnds( result, reason, GE_XBOARD );
6816 /* Next absolutely insufficient mating material. */
6817 if(!MatingPotential(nr, WhitePawn, nrW, nrB, staleW, bishopColor) &&
6818 !MatingPotential(nr, BlackPawn, nrB, nrW, staleB, bishopColor))
6819 { /* includes KBK, KNK, KK of KBKB with like Bishops */
6821 /* always flag draws, for judging claims */
6822 boards[forwardMostMove][EP_STATUS] = EP_INSUF_DRAW;
6824 if(canAdjudicate && appData.materialDraws) {
6825 /* but only adjudicate them if adjudication enabled */
6826 if(engineOpponent) {
6827 SendToProgram("force\n", engineOpponent); // suppress reply
6828 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see last move */
6830 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6831 GameEnds( GameIsDrawn, "Xboard adjudication: Insufficient mating material", GE_XBOARD );
6836 /* Then some trivial draws (only adjudicate, cannot be claimed) */
6837 if(gameInfo.variant == VariantXiangqi ?
6838 SufficientDefence(nr, WhitePawn, nrW, nrB) && SufficientDefence(nr, BlackPawn, nrB, nrW)
6840 ( nr[WhiteRook] == 1 && nr[BlackRook] == 1 /* KRKR */
6841 || nr[WhiteQueen] && nr[BlackQueen]==1 /* KQKQ */
6842 || nr[WhiteKnight]==2 || nr[BlackKnight]==2 /* KNNK */
6843 || nr[WhiteKnight]+nr[WhiteBishop] == 1 && nr[BlackKnight]+nr[BlackBishop] == 1 /* KBKN, KBKB, KNKN */
6845 if(--moveCount < 0 && appData.trivialDraws && canAdjudicate)
6846 { /* if the first 3 moves do not show a tactical win, declare draw */
6847 if(engineOpponent) {
6848 SendToProgram("force\n", engineOpponent); // suppress reply
6849 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
6851 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6852 GameEnds( GameIsDrawn, "Xboard adjudication: Trivial draw", GE_XBOARD );
6855 } else moveCount = 6;
6857 if (appData.debugMode) { int i;
6858 fprintf(debugFP, "repeat test fmm=%d bmm=%d ep=%d, reps=%d\n",
6859 forwardMostMove, backwardMostMove, boards[backwardMostMove][EP_STATUS],
6860 appData.drawRepeats);
6861 for( i=forwardMostMove; i>=backwardMostMove; i-- )
6862 fprintf(debugFP, "%d ep=%d\n", i, (signed char)boards[i][EP_STATUS]);
6866 // Repetition draws and 50-move rule can be applied independently of legality testing
6868 /* Check for rep-draws */
6870 for(k = forwardMostMove-2;
6871 k>=backwardMostMove && k>=forwardMostMove-100 &&
6872 (signed char)boards[k][EP_STATUS] < EP_UNKNOWN &&
6873 (signed char)boards[k+2][EP_STATUS] <= EP_NONE && (signed char)boards[k+1][EP_STATUS] <= EP_NONE;
6876 if(CompareBoards(boards[k], boards[forwardMostMove])) {
6877 /* compare castling rights */
6878 if( boards[forwardMostMove][CASTLING][2] != boards[k][CASTLING][2] &&
6879 (boards[k][CASTLING][0] != NoRights || boards[k][CASTLING][1] != NoRights) )
6880 rights++; /* King lost rights, while rook still had them */
6881 if( boards[forwardMostMove][CASTLING][2] != NoRights ) { /* king has rights */
6882 if( boards[forwardMostMove][CASTLING][0] != boards[k][CASTLING][0] ||
6883 boards[forwardMostMove][CASTLING][1] != boards[k][CASTLING][1] )
6884 rights++; /* but at least one rook lost them */
6886 if( boards[forwardMostMove][CASTLING][5] != boards[k][CASTLING][5] &&
6887 (boards[k][CASTLING][3] != NoRights || boards[k][CASTLING][4] != NoRights) )
6889 if( boards[forwardMostMove][CASTLING][5] != NoRights ) {
6890 if( boards[forwardMostMove][CASTLING][3] != boards[k][CASTLING][3] ||
6891 boards[forwardMostMove][CASTLING][4] != boards[k][CASTLING][4] )
6894 if( rights == 0 && ++count > appData.drawRepeats-2 && canAdjudicate
6895 && appData.drawRepeats > 1) {
6896 /* adjudicate after user-specified nr of repeats */
6897 int result = GameIsDrawn;
6898 char *details = "XBoard adjudication: repetition draw";
6899 if(gameInfo.variant == VariantXiangqi && appData.testLegality) {
6900 // [HGM] xiangqi: check for forbidden perpetuals
6901 int m, ourPerpetual = 1, hisPerpetual = 1;
6902 for(m=forwardMostMove; m>k; m-=2) {
6903 if(MateTest(boards[m], PosFlags(m)) != MT_CHECK)
6904 ourPerpetual = 0; // the current mover did not always check
6905 if(MateTest(boards[m-1], PosFlags(m-1)) != MT_CHECK)
6906 hisPerpetual = 0; // the opponent did not always check
6908 if(appData.debugMode) fprintf(debugFP, "XQ perpetual test, our=%d, his=%d\n",
6909 ourPerpetual, hisPerpetual);
6910 if(ourPerpetual && !hisPerpetual) { // we are actively checking him: forfeit
6911 result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
6912 details = "Xboard adjudication: perpetual checking";
6914 if(hisPerpetual && !ourPerpetual) { // he is checking us, but did not repeat yet
6915 break; // (or we would have caught him before). Abort repetition-checking loop.
6917 // Now check for perpetual chases
6918 if(!ourPerpetual && !hisPerpetual) { // no perpetual check, test for chase
6919 hisPerpetual = PerpetualChase(k, forwardMostMove);
6920 ourPerpetual = PerpetualChase(k+1, forwardMostMove);
6921 if(ourPerpetual && !hisPerpetual) { // we are actively chasing him: forfeit
6922 result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
6923 details = "Xboard adjudication: perpetual chasing";
6925 if(hisPerpetual && !ourPerpetual) // he is chasing us, but did not repeat yet
6926 break; // Abort repetition-checking loop.
6928 // if neither of us is checking or chasing all the time, or both are, it is draw
6930 if(engineOpponent) {
6931 SendToProgram("force\n", engineOpponent); // suppress reply
6932 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
6934 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6935 GameEnds( result, details, GE_XBOARD );
6938 if( rights == 0 && count > 1 ) /* occurred 2 or more times before */
6939 boards[forwardMostMove][EP_STATUS] = EP_REP_DRAW;
6943 /* Now we test for 50-move draws. Determine ply count */
6944 count = forwardMostMove;
6945 /* look for last irreversble move */
6946 while( (signed char)boards[count][EP_STATUS] <= EP_NONE && count > backwardMostMove )
6948 /* if we hit starting position, add initial plies */
6949 if( count == backwardMostMove )
6950 count -= initialRulePlies;
6951 count = forwardMostMove - count;
6952 if(gameInfo.variant == VariantXiangqi && ( count >= 100 || count >= 2*appData.ruleMoves ) ) {
6953 // adjust reversible move counter for checks in Xiangqi
6954 int i = forwardMostMove - count, inCheck = 0, lastCheck;
6955 if(i < backwardMostMove) i = backwardMostMove;
6956 while(i <= forwardMostMove) {
6957 lastCheck = inCheck; // check evasion does not count
6958 inCheck = (MateTest(boards[i], PosFlags(i)) == MT_CHECK);
6959 if(inCheck || lastCheck) count--; // check does not count
6964 boards[forwardMostMove][EP_STATUS] = EP_RULE_DRAW;
6965 /* this is used to judge if draw claims are legal */
6966 if(canAdjudicate && appData.ruleMoves > 0 && count >= 2*appData.ruleMoves) {
6967 if(engineOpponent) {
6968 SendToProgram("force\n", engineOpponent); // suppress reply
6969 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
6971 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6972 GameEnds( GameIsDrawn, "Xboard adjudication: 50-move rule", GE_XBOARD );
6976 /* if draw offer is pending, treat it as a draw claim
6977 * when draw condition present, to allow engines a way to
6978 * claim draws before making their move to avoid a race
6979 * condition occurring after their move
6981 if((gameMode == TwoMachinesPlay ? second.offeredDraw : userOfferedDraw) || first.offeredDraw ) {
6983 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_RULE_DRAW)
6984 p = "Draw claim: 50-move rule";
6985 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_REP_DRAW)
6986 p = "Draw claim: 3-fold repetition";
6987 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_INSUF_DRAW)
6988 p = "Draw claim: insufficient mating material";
6989 if( p != NULL && canAdjudicate) {
6990 if(engineOpponent) {
6991 SendToProgram("force\n", engineOpponent); // suppress reply
6992 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
6994 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6995 GameEnds( GameIsDrawn, p, GE_XBOARD );
7000 if( canAdjudicate && appData.adjudicateDrawMoves > 0 && forwardMostMove > (2*appData.adjudicateDrawMoves) ) {
7001 if(engineOpponent) {
7002 SendToProgram("force\n", engineOpponent); // suppress reply
7003 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7005 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
7006 GameEnds( GameIsDrawn, "Xboard adjudication: long game", GE_XBOARD );
7012 char *SendMoveToBookUser(int moveNr, ChessProgramState *cps, int initial)
7013 { // [HGM] book: this routine intercepts moves to simulate book replies
7014 char *bookHit = NULL;
7016 //first determine if the incoming move brings opponent into his book
7017 if(appData.usePolyglotBook && (cps == &first ? !appData.firstHasOwnBookUCI : !appData.secondHasOwnBookUCI))
7018 bookHit = ProbeBook(moveNr+1, appData.polyglotBook); // returns move
7019 if(appData.debugMode) fprintf(debugFP, "book hit = %s\n", bookHit ? bookHit : "(NULL)");
7020 if(bookHit != NULL && !cps->bookSuspend) {
7021 // make sure opponent is not going to reply after receiving move to book position
7022 SendToProgram("force\n", cps);
7023 cps->bookSuspend = TRUE; // flag indicating it has to be restarted
7025 if(!initial) SendMoveToProgram(moveNr, cps); // with hit on initial position there is no move
7026 // now arrange restart after book miss
7028 // after a book hit we never send 'go', and the code after the call to this routine
7029 // has '&& !bookHit' added to suppress potential sending there (based on 'firstMove').
7031 snprintf(buf, MSG_SIZ, "%s%s\n", (cps->useUsermove ? "usermove " : ""), bookHit); // force book move into program supposed to play it
7032 SendToProgram(buf, cps);
7033 if(!initial) firstMove = FALSE; // normally we would clear the firstMove condition after return & sending 'go'
7034 } else if(initial) { // 'go' was needed irrespective of firstMove, and it has to be done in this routine
7035 SendToProgram("go\n", cps);
7036 cps->bookSuspend = FALSE; // after a 'go' we are never suspended
7037 } else { // 'go' might be sent based on 'firstMove' after this routine returns
7038 if(cps->bookSuspend && !firstMove) // 'go' needed, and it will not be done after we return
7039 SendToProgram("go\n", cps);
7040 cps->bookSuspend = FALSE; // anyhow, we will not be suspended after a miss
7042 return bookHit; // notify caller of hit, so it can take action to send move to opponent
7046 ChessProgramState *savedState;
7047 void DeferredBookMove(void)
7049 if(savedState->lastPing != savedState->lastPong)
7050 ScheduleDelayedEvent(DeferredBookMove, 10);
7052 HandleMachineMove(savedMessage, savedState);
7056 HandleMachineMove(message, cps)
7058 ChessProgramState *cps;
7060 char machineMove[MSG_SIZ], buf1[MSG_SIZ*10], buf2[MSG_SIZ];
7061 char realname[MSG_SIZ];
7062 int fromX, fromY, toX, toY;
7071 FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book hit
7073 * Kludge to ignore BEL characters
7075 while (*message == '\007') message++;
7078 * [HGM] engine debug message: ignore lines starting with '#' character
7080 if(cps->debug && *message == '#') return;
7083 * Look for book output
7085 if (cps == &first && bookRequested) {
7086 if (message[0] == '\t' || message[0] == ' ') {
7087 /* Part of the book output is here; append it */
7088 strcat(bookOutput, message);
7089 strcat(bookOutput, " \n");
7091 } else if (bookOutput[0] != NULLCHAR) {
7092 /* All of book output has arrived; display it */
7093 char *p = bookOutput;
7094 while (*p != NULLCHAR) {
7095 if (*p == '\t') *p = ' ';
7098 DisplayInformation(bookOutput);
7099 bookRequested = FALSE;
7100 /* Fall through to parse the current output */
7105 * Look for machine move.
7107 if ((sscanf(message, "%s %s %s", buf1, buf2, machineMove) == 3 && strcmp(buf2, "...") == 0) ||
7108 (sscanf(message, "%s %s", buf1, machineMove) == 2 && strcmp(buf1, "move") == 0))
7110 /* This method is only useful on engines that support ping */
7111 if (cps->lastPing != cps->lastPong) {
7112 if (gameMode == BeginningOfGame) {
7113 /* Extra move from before last new; ignore */
7114 if (appData.debugMode) {
7115 fprintf(debugFP, "Ignoring extra move from %s\n", cps->which);
7118 if (appData.debugMode) {
7119 fprintf(debugFP, "Undoing extra move from %s, gameMode %d\n",
7120 cps->which, gameMode);
7123 SendToProgram("undo\n", cps);
7129 case BeginningOfGame:
7130 /* Extra move from before last reset; ignore */
7131 if (appData.debugMode) {
7132 fprintf(debugFP, "Ignoring extra move from %s\n", cps->which);
7139 /* Extra move after we tried to stop. The mode test is
7140 not a reliable way of detecting this problem, but it's
7141 the best we can do on engines that don't support ping.
7143 if (appData.debugMode) {
7144 fprintf(debugFP, "Undoing extra move from %s, gameMode %d\n",
7145 cps->which, gameMode);
7147 SendToProgram("undo\n", cps);
7150 case MachinePlaysWhite:
7151 case IcsPlayingWhite:
7152 machineWhite = TRUE;
7155 case MachinePlaysBlack:
7156 case IcsPlayingBlack:
7157 machineWhite = FALSE;
7160 case TwoMachinesPlay:
7161 machineWhite = (cps->twoMachinesColor[0] == 'w');
7164 if (WhiteOnMove(forwardMostMove) != machineWhite) {
7165 if (appData.debugMode) {
7167 "Ignoring move out of turn by %s, gameMode %d"
7168 ", forwardMost %d\n",
7169 cps->which, gameMode, forwardMostMove);
7174 if (appData.debugMode) { int f = forwardMostMove;
7175 fprintf(debugFP, "machine move %d, castling = %d %d %d %d %d %d\n", f,
7176 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
7177 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
7179 if(cps->alphaRank) AlphaRank(machineMove, 4);
7180 if (!ParseOneMove(machineMove, forwardMostMove, &moveType,
7181 &fromX, &fromY, &toX, &toY, &promoChar)) {
7182 /* Machine move could not be parsed; ignore it. */
7183 snprintf(buf1, MSG_SIZ*10, _("Illegal move \"%s\" from %s machine"),
7184 machineMove, cps->which);
7185 DisplayError(buf1, 0);
7186 snprintf(buf1, MSG_SIZ*10, "Xboard: Forfeit due to invalid move: %s (%c%c%c%c) res=%d",
7187 machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, moveType);
7188 if (gameMode == TwoMachinesPlay) {
7189 GameEnds(machineWhite ? BlackWins : WhiteWins,
7195 /* [HGM] Apparently legal, but so far only tested with EP_UNKOWN */
7196 /* So we have to redo legality test with true e.p. status here, */
7197 /* to make sure an illegal e.p. capture does not slip through, */
7198 /* to cause a forfeit on a justified illegal-move complaint */
7199 /* of the opponent. */
7200 if( gameMode==TwoMachinesPlay && appData.testLegality ) {
7202 moveType = LegalityTest(boards[forwardMostMove], PosFlags(forwardMostMove),
7203 fromY, fromX, toY, toX, promoChar);
7204 if (appData.debugMode) {
7206 for(i=0; i< nrCastlingRights; i++) fprintf(debugFP, "(%d,%d) ",
7207 boards[forwardMostMove][CASTLING][i], castlingRank[i]);
7208 fprintf(debugFP, "castling rights\n");
7210 if(moveType == IllegalMove) {
7211 snprintf(buf1, MSG_SIZ*10, "Xboard: Forfeit due to illegal move: %s (%c%c%c%c)%c",
7212 machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, 0);
7213 GameEnds(machineWhite ? BlackWins : WhiteWins,
7216 } else if(gameInfo.variant != VariantFischeRandom && gameInfo.variant != VariantCapaRandom)
7217 /* [HGM] Kludge to handle engines that send FRC-style castling
7218 when they shouldn't (like TSCP-Gothic) */
7220 case WhiteASideCastleFR:
7221 case BlackASideCastleFR:
7223 currentMoveString[2]++;
7225 case WhiteHSideCastleFR:
7226 case BlackHSideCastleFR:
7228 currentMoveString[2]--;
7230 default: ; // nothing to do, but suppresses warning of pedantic compilers
7233 hintRequested = FALSE;
7234 lastHint[0] = NULLCHAR;
7235 bookRequested = FALSE;
7236 /* Program may be pondering now */
7237 cps->maybeThinking = TRUE;
7238 if (cps->sendTime == 2) cps->sendTime = 1;
7239 if (cps->offeredDraw) cps->offeredDraw--;
7241 /* currentMoveString is set as a side-effect of ParseOneMove */
7242 safeStrCpy(machineMove, currentMoveString, sizeof(machineMove)/sizeof(machineMove[0]));
7243 strcat(machineMove, "\n");
7244 safeStrCpy(moveList[forwardMostMove], machineMove, sizeof(moveList[forwardMostMove])/sizeof(moveList[forwardMostMove][0]));
7246 /* [AS] Save move info*/
7247 pvInfoList[ forwardMostMove ].score = programStats.score;
7248 pvInfoList[ forwardMostMove ].depth = programStats.depth;
7249 pvInfoList[ forwardMostMove ].time = programStats.time; // [HGM] PGNtime: take time from engine stats
7251 MakeMove(fromX, fromY, toX, toY, promoChar);/*updates forwardMostMove*/
7253 /* [AS] Adjudicate game if needed (note: remember that forwardMostMove now points past the last move) */
7254 if( gameMode == TwoMachinesPlay && adjudicateLossThreshold != 0 && forwardMostMove >= adjudicateLossPlies ) {
7257 while( count < adjudicateLossPlies ) {
7258 int score = pvInfoList[ forwardMostMove - count - 1 ].score;
7261 score = -score; /* Flip score for winning side */
7264 if( score > adjudicateLossThreshold ) {
7271 if( count >= adjudicateLossPlies ) {
7272 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
7274 GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
7275 "Xboard adjudication",
7282 if(Adjudicate(cps)) return; // [HGM] adjudicate: for all automatic game ends
7285 if ((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack) &&
7287 if(cps->offeredDraw && (signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
7288 SendToICS(ics_prefix); // [HGM] drawclaim: send caim and move on one line for FICS
7290 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
7292 SendMoveToICS(moveType, fromX, fromY, toX, toY, promoChar);
7294 if(appData.autoKibitz && !appData.icsEngineAnalyze ) { /* [HGM] kibitz: send most-recent PV info to ICS */
7295 char buf[3*MSG_SIZ];
7297 snprintf(buf, 3*MSG_SIZ, "kibitz !!! %+.2f/%d (%.2f sec, %u nodes, %.0f knps) PV=%s\n",
7298 programStats.score / 100.,
7300 programStats.time / 100.,
7301 (unsigned int)programStats.nodes,
7302 (unsigned int)programStats.nodes / (10*abs(programStats.time) + 1.),
7303 programStats.movelist);
7305 if(appData.debugMode) fprintf(debugFP, "nodes = %d, %lld\n", (int) programStats.nodes, programStats.nodes);
7310 /* [AS] Clear stats for next move */
7311 ClearProgramStats();
7312 thinkOutput[0] = NULLCHAR;
7313 hiddenThinkOutputState = 0;
7316 if (gameMode == TwoMachinesPlay) {
7317 /* [HGM] relaying draw offers moved to after reception of move */
7318 /* and interpreting offer as claim if it brings draw condition */
7319 if (cps->offeredDraw == 1 && cps->other->sendDrawOffers) {
7320 SendToProgram("draw\n", cps->other);
7322 if (cps->other->sendTime) {
7323 SendTimeRemaining(cps->other,
7324 cps->other->twoMachinesColor[0] == 'w');
7326 bookHit = SendMoveToBookUser(forwardMostMove-1, cps->other, FALSE);
7327 if (firstMove && !bookHit) {
7329 if (cps->other->useColors) {
7330 SendToProgram(cps->other->twoMachinesColor, cps->other);
7332 SendToProgram("go\n", cps->other);
7334 cps->other->maybeThinking = TRUE;
7337 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
7339 if (!pausing && appData.ringBellAfterMoves) {
7344 * Reenable menu items that were disabled while
7345 * machine was thinking
7347 if (gameMode != TwoMachinesPlay)
7348 SetUserThinkingEnables();
7350 // [HGM] book: after book hit opponent has received move and is now in force mode
7351 // force the book reply into it, and then fake that it outputted this move by jumping
7352 // back to the beginning of HandleMachineMove, with cps toggled and message set to this move
7354 static char bookMove[MSG_SIZ]; // a bit generous?
7356 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
7357 strcat(bookMove, bookHit);
7360 programStats.nodes = programStats.depth = programStats.time =
7361 programStats.score = programStats.got_only_move = 0;
7362 sprintf(programStats.movelist, "%s (xbook)", bookHit);
7364 if(cps->lastPing != cps->lastPong) {
7365 savedMessage = message; // args for deferred call
7367 ScheduleDelayedEvent(DeferredBookMove, 10);
7376 /* Set special modes for chess engines. Later something general
7377 * could be added here; for now there is just one kludge feature,
7378 * needed because Crafty 15.10 and earlier don't ignore SIGINT
7379 * when "xboard" is given as an interactive command.
7381 if (strncmp(message, "kibitz Hello from Crafty", 24) == 0) {
7382 cps->useSigint = FALSE;
7383 cps->useSigterm = FALSE;
7385 if (strncmp(message, "feature ", 8) == 0) { // [HGM] moved forward to pre-empt non-compliant commands
7386 ParseFeatures(message+8, cps);
7387 return; // [HGM] This return was missing, causing option features to be recognized as non-compliant commands!
7390 /* [HGM] Allow engine to set up a position. Don't ask me why one would
7391 * want this, I was asked to put it in, and obliged.
7393 if (!strncmp(message, "setboard ", 9)) {
7394 Board initial_position;
7396 GameEnds(GameUnfinished, "Engine aborts game", GE_XBOARD);
7398 if (!ParseFEN(initial_position, &blackPlaysFirst, message + 9)) {
7399 DisplayError(_("Bad FEN received from engine"), 0);
7403 CopyBoard(boards[0], initial_position);
7404 initialRulePlies = FENrulePlies;
7405 if(blackPlaysFirst) gameMode = MachinePlaysWhite;
7406 else gameMode = MachinePlaysBlack;
7407 DrawPosition(FALSE, boards[currentMove]);
7413 * Look for communication commands
7415 if (!strncmp(message, "telluser ", 9)) {
7416 EscapeExpand(message+9, message+9); // [HGM] esc: allow escape sequences in popup box
7417 DisplayNote(message + 9);
7420 if (!strncmp(message, "tellusererror ", 14)) {
7422 EscapeExpand(message+14, message+14); // [HGM] esc: allow escape sequences in popup box
7423 DisplayError(message + 14, 0);
7426 if (!strncmp(message, "tellopponent ", 13)) {
7427 if (appData.icsActive) {
7429 snprintf(buf1, sizeof(buf1), "%ssay %s\n", ics_prefix, message + 13);
7433 DisplayNote(message + 13);
7437 if (!strncmp(message, "tellothers ", 11)) {
7438 if (appData.icsActive) {
7440 snprintf(buf1, sizeof(buf1), "%swhisper %s\n", ics_prefix, message + 11);
7446 if (!strncmp(message, "tellall ", 8)) {
7447 if (appData.icsActive) {
7449 snprintf(buf1, sizeof(buf1), "%skibitz %s\n", ics_prefix, message + 8);
7453 DisplayNote(message + 8);
7457 if (strncmp(message, "warning", 7) == 0) {
7458 /* Undocumented feature, use tellusererror in new code */
7459 DisplayError(message, 0);
7462 if (sscanf(message, "askuser %s %[^\n]", buf1, buf2) == 2) {
7463 safeStrCpy(realname, cps->tidy, sizeof(realname)/sizeof(realname[0]));
7464 strcat(realname, " query");
7465 AskQuestion(realname, buf2, buf1, cps->pr);
7468 /* Commands from the engine directly to ICS. We don't allow these to be
7469 * sent until we are logged on. Crafty kibitzes have been known to
7470 * interfere with the login process.
7473 if (!strncmp(message, "tellics ", 8)) {
7474 SendToICS(message + 8);
7478 if (!strncmp(message, "tellicsnoalias ", 15)) {
7479 SendToICS(ics_prefix);
7480 SendToICS(message + 15);
7484 /* The following are for backward compatibility only */
7485 if (!strncmp(message,"whisper",7) || !strncmp(message,"kibitz",6) ||
7486 !strncmp(message,"draw",4) || !strncmp(message,"tell",3)) {
7487 SendToICS(ics_prefix);
7493 if (sscanf(message, "pong %d", &cps->lastPong) == 1) {
7497 * If the move is illegal, cancel it and redraw the board.
7498 * Also deal with other error cases. Matching is rather loose
7499 * here to accommodate engines written before the spec.
7501 if (strncmp(message + 1, "llegal move", 11) == 0 ||
7502 strncmp(message, "Error", 5) == 0) {
7503 if (StrStr(message, "name") ||
7504 StrStr(message, "rating") || StrStr(message, "?") ||
7505 StrStr(message, "result") || StrStr(message, "board") ||
7506 StrStr(message, "bk") || StrStr(message, "computer") ||
7507 StrStr(message, "variant") || StrStr(message, "hint") ||
7508 StrStr(message, "random") || StrStr(message, "depth") ||
7509 StrStr(message, "accepted")) {
7512 if (StrStr(message, "protover")) {
7513 /* Program is responding to input, so it's apparently done
7514 initializing, and this error message indicates it is
7515 protocol version 1. So we don't need to wait any longer
7516 for it to initialize and send feature commands. */
7517 FeatureDone(cps, 1);
7518 cps->protocolVersion = 1;
7521 cps->maybeThinking = FALSE;
7523 if (StrStr(message, "draw")) {
7524 /* Program doesn't have "draw" command */
7525 cps->sendDrawOffers = 0;
7528 if (cps->sendTime != 1 &&
7529 (StrStr(message, "time") || StrStr(message, "otim"))) {
7530 /* Program apparently doesn't have "time" or "otim" command */
7534 if (StrStr(message, "analyze")) {
7535 cps->analysisSupport = FALSE;
7536 cps->analyzing = FALSE;
7538 snprintf(buf2,MSG_SIZ, _("%s does not support analysis"), cps->tidy);
7539 DisplayError(buf2, 0);
7542 if (StrStr(message, "(no matching move)st")) {
7543 /* Special kludge for GNU Chess 4 only */
7544 cps->stKludge = TRUE;
7545 SendTimeControl(cps, movesPerSession, timeControl,
7546 timeIncrement, appData.searchDepth,
7550 if (StrStr(message, "(no matching move)sd")) {
7551 /* Special kludge for GNU Chess 4 only */
7552 cps->sdKludge = TRUE;
7553 SendTimeControl(cps, movesPerSession, timeControl,
7554 timeIncrement, appData.searchDepth,
7558 if (!StrStr(message, "llegal")) {
7561 if (gameMode == BeginningOfGame || gameMode == EndOfGame ||
7562 gameMode == IcsIdle) return;
7563 if (forwardMostMove <= backwardMostMove) return;
7564 if (pausing) PauseEvent();
7565 if(appData.forceIllegal) {
7566 // [HGM] illegal: machine refused move; force position after move into it
7567 SendToProgram("force\n", cps);
7568 if(!cps->useSetboard) { // hideous kludge on kludge, because SendBoard sucks.
7569 // we have a real problem now, as SendBoard will use the a2a3 kludge
7570 // when black is to move, while there might be nothing on a2 or black
7571 // might already have the move. So send the board as if white has the move.
7572 // But first we must change the stm of the engine, as it refused the last move
7573 SendBoard(cps, 0); // always kludgeless, as white is to move on boards[0]
7574 if(WhiteOnMove(forwardMostMove)) {
7575 SendToProgram("a7a6\n", cps); // for the engine black still had the move
7576 SendBoard(cps, forwardMostMove); // kludgeless board
7578 SendToProgram("a2a3\n", cps); // for the engine white still had the move
7579 CopyBoard(boards[forwardMostMove+1], boards[forwardMostMove]);
7580 SendBoard(cps, forwardMostMove+1); // kludgeless board
7582 } else SendBoard(cps, forwardMostMove); // FEN case, also sets stm properly
7583 if(gameMode == MachinePlaysWhite || gameMode == MachinePlaysBlack ||
7584 gameMode == TwoMachinesPlay)
7585 SendToProgram("go\n", cps);
7588 if (gameMode == PlayFromGameFile) {
7589 /* Stop reading this game file */
7590 gameMode = EditGame;
7593 currentMove = forwardMostMove-1;
7594 DisplayMove(currentMove-1); /* before DisplayMoveError */
7595 SwitchClocks(forwardMostMove-1); // [HGM] race
7596 DisplayBothClocks();
7597 snprintf(buf1, 10*MSG_SIZ, _("Illegal move \"%s\" (rejected by %s chess program)"),
7598 parseList[currentMove], cps->which);
7599 DisplayMoveError(buf1);
7600 DrawPosition(FALSE, boards[currentMove]);
7602 /* [HGM] illegal-move claim should forfeit game when Xboard */
7603 /* only passes fully legal moves */
7604 if( appData.testLegality && gameMode == TwoMachinesPlay ) {
7605 GameEnds( cps->twoMachinesColor[0] == 'w' ? BlackWins : WhiteWins,
7606 "False illegal-move claim", GE_XBOARD );
7610 if (strncmp(message, "time", 4) == 0 && StrStr(message, "Illegal")) {
7611 /* Program has a broken "time" command that
7612 outputs a string not ending in newline.
7618 * If chess program startup fails, exit with an error message.
7619 * Attempts to recover here are futile.
7621 if ((StrStr(message, "unknown host") != NULL)
7622 || (StrStr(message, "No remote directory") != NULL)
7623 || (StrStr(message, "not found") != NULL)
7624 || (StrStr(message, "No such file") != NULL)
7625 || (StrStr(message, "can't alloc") != NULL)
7626 || (StrStr(message, "Permission denied") != NULL)) {
7628 cps->maybeThinking = FALSE;
7629 snprintf(buf1, sizeof(buf1), _("Failed to start %s chess program %s on %s: %s\n"),
7630 cps->which, cps->program, cps->host, message);
7631 RemoveInputSource(cps->isr);
7632 DisplayFatalError(buf1, 0, 1);
7637 * Look for hint output
7639 if (sscanf(message, "Hint: %s", buf1) == 1) {
7640 if (cps == &first && hintRequested) {
7641 hintRequested = FALSE;
7642 if (ParseOneMove(buf1, forwardMostMove, &moveType,
7643 &fromX, &fromY, &toX, &toY, &promoChar)) {
7644 (void) CoordsToAlgebraic(boards[forwardMostMove],
7645 PosFlags(forwardMostMove),
7646 fromY, fromX, toY, toX, promoChar, buf1);
7647 snprintf(buf2, sizeof(buf2), _("Hint: %s"), buf1);
7648 DisplayInformation(buf2);
7650 /* Hint move could not be parsed!? */
7651 snprintf(buf2, sizeof(buf2),
7652 _("Illegal hint move \"%s\"\nfrom %s chess program"),
7654 DisplayError(buf2, 0);
7657 safeStrCpy(lastHint, buf1, sizeof(lastHint)/sizeof(lastHint[0]));
7663 * Ignore other messages if game is not in progress
7665 if (gameMode == BeginningOfGame || gameMode == EndOfGame ||
7666 gameMode == IcsIdle || cps->lastPing != cps->lastPong) return;
7669 * look for win, lose, draw, or draw offer
7671 if (strncmp(message, "1-0", 3) == 0) {
7672 char *p, *q, *r = "";
7673 p = strchr(message, '{');
7681 GameEnds(WhiteWins, r, GE_ENGINE1 + (cps != &first)); /* [HGM] pass claimer indication for claim test */
7683 } else if (strncmp(message, "0-1", 3) == 0) {
7684 char *p, *q, *r = "";
7685 p = strchr(message, '{');
7693 /* Kludge for Arasan 4.1 bug */
7694 if (strcmp(r, "Black resigns") == 0) {
7695 GameEnds(WhiteWins, r, GE_ENGINE1 + (cps != &first));
7698 GameEnds(BlackWins, r, GE_ENGINE1 + (cps != &first));
7700 } else if (strncmp(message, "1/2", 3) == 0) {
7701 char *p, *q, *r = "";
7702 p = strchr(message, '{');
7711 GameEnds(GameIsDrawn, r, GE_ENGINE1 + (cps != &first));
7714 } else if (strncmp(message, "White resign", 12) == 0) {
7715 GameEnds(BlackWins, "White resigns", GE_ENGINE1 + (cps != &first));
7717 } else if (strncmp(message, "Black resign", 12) == 0) {
7718 GameEnds(WhiteWins, "Black resigns", GE_ENGINE1 + (cps != &first));
7720 } else if (strncmp(message, "White matches", 13) == 0 ||
7721 strncmp(message, "Black matches", 13) == 0 ) {
7722 /* [HGM] ignore GNUShogi noises */
7724 } else if (strncmp(message, "White", 5) == 0 &&
7725 message[5] != '(' &&
7726 StrStr(message, "Black") == NULL) {
7727 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
7729 } else if (strncmp(message, "Black", 5) == 0 &&
7730 message[5] != '(') {
7731 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
7733 } else if (strcmp(message, "resign") == 0 ||
7734 strcmp(message, "computer resigns") == 0) {
7736 case MachinePlaysBlack:
7737 case IcsPlayingBlack:
7738 GameEnds(WhiteWins, "Black resigns", GE_ENGINE);
7740 case MachinePlaysWhite:
7741 case IcsPlayingWhite:
7742 GameEnds(BlackWins, "White resigns", GE_ENGINE);
7744 case TwoMachinesPlay:
7745 if (cps->twoMachinesColor[0] == 'w')
7746 GameEnds(BlackWins, "White resigns", GE_ENGINE1 + (cps != &first));
7748 GameEnds(WhiteWins, "Black resigns", GE_ENGINE1 + (cps != &first));
7755 } else if (strncmp(message, "opponent mates", 14) == 0) {
7757 case MachinePlaysBlack:
7758 case IcsPlayingBlack:
7759 GameEnds(WhiteWins, "White mates", GE_ENGINE);
7761 case MachinePlaysWhite:
7762 case IcsPlayingWhite:
7763 GameEnds(BlackWins, "Black mates", GE_ENGINE);
7765 case TwoMachinesPlay:
7766 if (cps->twoMachinesColor[0] == 'w')
7767 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
7769 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
7776 } else if (strncmp(message, "computer mates", 14) == 0) {
7778 case MachinePlaysBlack:
7779 case IcsPlayingBlack:
7780 GameEnds(BlackWins, "Black mates", GE_ENGINE1);
7782 case MachinePlaysWhite:
7783 case IcsPlayingWhite:
7784 GameEnds(WhiteWins, "White mates", GE_ENGINE);
7786 case TwoMachinesPlay:
7787 if (cps->twoMachinesColor[0] == 'w')
7788 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
7790 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
7797 } else if (strncmp(message, "checkmate", 9) == 0) {
7798 if (WhiteOnMove(forwardMostMove)) {
7799 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
7801 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
7804 } else if (strstr(message, "Draw") != NULL ||
7805 strstr(message, "game is a draw") != NULL) {
7806 GameEnds(GameIsDrawn, "Draw", GE_ENGINE1 + (cps != &first));
7808 } else if (strstr(message, "offer") != NULL &&
7809 strstr(message, "draw") != NULL) {
7811 if (appData.zippyPlay && first.initDone) {
7812 /* Relay offer to ICS */
7813 SendToICS(ics_prefix);
7814 SendToICS("draw\n");
7817 cps->offeredDraw = 2; /* valid until this engine moves twice */
7818 if (gameMode == TwoMachinesPlay) {
7819 if (cps->other->offeredDraw) {
7820 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
7821 /* [HGM] in two-machine mode we delay relaying draw offer */
7822 /* until after we also have move, to see if it is really claim */
7824 } else if (gameMode == MachinePlaysWhite ||
7825 gameMode == MachinePlaysBlack) {
7826 if (userOfferedDraw) {
7827 DisplayInformation(_("Machine accepts your draw offer"));
7828 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
7830 DisplayInformation(_("Machine offers a draw\nSelect Action / Draw to agree"));
7837 * Look for thinking output
7839 if ( appData.showThinking // [HGM] thinking: test all options that cause this output
7840 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp()
7842 int plylev, mvleft, mvtot, curscore, time;
7843 char mvname[MOVE_LEN];
7847 int prefixHint = FALSE;
7848 mvname[0] = NULLCHAR;
7851 case MachinePlaysBlack:
7852 case IcsPlayingBlack:
7853 if (WhiteOnMove(forwardMostMove)) prefixHint = TRUE;
7855 case MachinePlaysWhite:
7856 case IcsPlayingWhite:
7857 if (!WhiteOnMove(forwardMostMove)) prefixHint = TRUE;
7862 case IcsObserving: /* [DM] icsEngineAnalyze */
7863 if (!appData.icsEngineAnalyze) ignore = TRUE;
7865 case TwoMachinesPlay:
7866 if ((cps->twoMachinesColor[0] == 'w') != WhiteOnMove(forwardMostMove)) {
7876 ChessProgramStats tempStats = programStats; // [HGM] info: filter out info lines
7878 if (sscanf(message, "%d%c %d %d " u64Display " %[^\n]\n",
7879 &plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5) {
7881 if (plyext != ' ' && plyext != '\t') {
7885 /* [AS] Negate score if machine is playing black and reporting absolute scores */
7886 if( cps->scoreIsAbsolute &&
7887 ( gameMode == MachinePlaysBlack ||
7888 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b' ||
7889 gameMode == IcsPlayingBlack || // [HGM] also add other situations where engine should report black POV
7890 (gameMode == AnalyzeMode || gameMode == AnalyzeFile || gameMode == IcsObserving && appData.icsEngineAnalyze) &&
7891 !WhiteOnMove(currentMove)
7894 curscore = -curscore;
7898 tempStats.depth = plylev;
7899 tempStats.nodes = nodes;
7900 tempStats.time = time;
7901 tempStats.score = curscore;
7902 tempStats.got_only_move = 0;
7904 if(cps->nps >= 0) { /* [HGM] nps: use engine nodes or time to decrement clock */
7907 if(cps->nps == 0) ticklen = 10*time; // use engine reported time
7908 else ticklen = (1000. * u64ToDouble(nodes)) / cps->nps; // convert node count to time
7909 if(WhiteOnMove(forwardMostMove) && (gameMode == MachinePlaysWhite ||
7910 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'w'))
7911 whiteTimeRemaining = timeRemaining[0][forwardMostMove] - ticklen;
7912 if(!WhiteOnMove(forwardMostMove) && (gameMode == MachinePlaysBlack ||
7913 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b'))
7914 blackTimeRemaining = timeRemaining[1][forwardMostMove] - ticklen;
7917 /* Buffer overflow protection */
7918 if (buf1[0] != NULLCHAR) {
7919 if (strlen(buf1) >= sizeof(tempStats.movelist)
7920 && appData.debugMode) {
7922 "PV is too long; using the first %u bytes.\n",
7923 (unsigned) sizeof(tempStats.movelist) - 1);
7926 safeStrCpy( tempStats.movelist, buf1, sizeof(tempStats.movelist)/sizeof(tempStats.movelist[0]) );
7928 sprintf(tempStats.movelist, " no PV\n");
7931 if (tempStats.seen_stat) {
7932 tempStats.ok_to_send = 1;
7935 if (strchr(tempStats.movelist, '(') != NULL) {
7936 tempStats.line_is_book = 1;
7937 tempStats.nr_moves = 0;
7938 tempStats.moves_left = 0;
7940 tempStats.line_is_book = 0;
7943 if(tempStats.score != 0 || tempStats.nodes != 0 || tempStats.time != 0)
7944 programStats = tempStats; // [HGM] info: only set stats if genuine PV and not an info line
7946 SendProgramStatsToFrontend( cps, &tempStats );
7949 [AS] Protect the thinkOutput buffer from overflow... this
7950 is only useful if buf1 hasn't overflowed first!
7952 snprintf(thinkOutput, sizeof(thinkOutput)/sizeof(thinkOutput[0]), "[%d]%c%+.2f %s%s",
7954 (gameMode == TwoMachinesPlay ?
7955 ToUpper(cps->twoMachinesColor[0]) : ' '),
7956 ((double) curscore) / 100.0,
7957 prefixHint ? lastHint : "",
7958 prefixHint ? " " : "" );
7960 if( buf1[0] != NULLCHAR ) {
7961 unsigned max_len = sizeof(thinkOutput) - strlen(thinkOutput) - 1;
7963 if( strlen(buf1) > max_len ) {
7964 if( appData.debugMode) {
7965 fprintf(debugFP,"PV is too long for thinkOutput, truncating.\n");
7967 buf1[max_len+1] = '\0';
7970 strcat( thinkOutput, buf1 );
7973 if (currentMove == forwardMostMove || gameMode == AnalyzeMode
7974 || gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
7975 DisplayMove(currentMove - 1);
7979 } else if ((p=StrStr(message, "(only move)")) != NULL) {
7980 /* crafty (9.25+) says "(only move) <move>"
7981 * if there is only 1 legal move
7983 sscanf(p, "(only move) %s", buf1);
7984 snprintf(thinkOutput, sizeof(thinkOutput)/sizeof(thinkOutput[0]), "%s (only move)", buf1);
7985 sprintf(programStats.movelist, "%s (only move)", buf1);
7986 programStats.depth = 1;
7987 programStats.nr_moves = 1;
7988 programStats.moves_left = 1;
7989 programStats.nodes = 1;
7990 programStats.time = 1;
7991 programStats.got_only_move = 1;
7993 /* Not really, but we also use this member to
7994 mean "line isn't going to change" (Crafty
7995 isn't searching, so stats won't change) */
7996 programStats.line_is_book = 1;
7998 SendProgramStatsToFrontend( cps, &programStats );
8000 if (currentMove == forwardMostMove || gameMode==AnalyzeMode ||
8001 gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
8002 DisplayMove(currentMove - 1);
8005 } else if (sscanf(message,"stat01: %d " u64Display " %d %d %d %s",
8006 &time, &nodes, &plylev, &mvleft,
8007 &mvtot, mvname) >= 5) {
8008 /* The stat01: line is from Crafty (9.29+) in response
8009 to the "." command */
8010 programStats.seen_stat = 1;
8011 cps->maybeThinking = TRUE;
8013 if (programStats.got_only_move || !appData.periodicUpdates)
8016 programStats.depth = plylev;
8017 programStats.time = time;
8018 programStats.nodes = nodes;
8019 programStats.moves_left = mvleft;
8020 programStats.nr_moves = mvtot;
8021 safeStrCpy(programStats.move_name, mvname, sizeof(programStats.move_name)/sizeof(programStats.move_name[0]));
8022 programStats.ok_to_send = 1;
8023 programStats.movelist[0] = '\0';
8025 SendProgramStatsToFrontend( cps, &programStats );
8029 } else if (strncmp(message,"++",2) == 0) {
8030 /* Crafty 9.29+ outputs this */
8031 programStats.got_fail = 2;
8034 } else if (strncmp(message,"--",2) == 0) {
8035 /* Crafty 9.29+ outputs this */
8036 programStats.got_fail = 1;
8039 } else if (thinkOutput[0] != NULLCHAR &&
8040 strncmp(message, " ", 4) == 0) {
8041 unsigned message_len;
8044 while (*p && *p == ' ') p++;
8046 message_len = strlen( p );
8048 /* [AS] Avoid buffer overflow */
8049 if( sizeof(thinkOutput) - strlen(thinkOutput) - 1 > message_len ) {
8050 strcat(thinkOutput, " ");
8051 strcat(thinkOutput, p);
8054 if( sizeof(programStats.movelist) - strlen(programStats.movelist) - 1 > message_len ) {
8055 strcat(programStats.movelist, " ");
8056 strcat(programStats.movelist, p);
8059 if (currentMove == forwardMostMove || gameMode==AnalyzeMode ||
8060 gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
8061 DisplayMove(currentMove - 1);
8069 if (sscanf(message, "%d%c %d %d " u64Display " %[^\n]\n",
8070 &plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5)
8072 ChessProgramStats cpstats;
8074 if (plyext != ' ' && plyext != '\t') {
8078 /* [AS] Negate score if machine is playing black and reporting absolute scores */
8079 if( cps->scoreIsAbsolute && ((gameMode == MachinePlaysBlack) || (gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b')) ) {
8080 curscore = -curscore;
8083 cpstats.depth = plylev;
8084 cpstats.nodes = nodes;
8085 cpstats.time = time;
8086 cpstats.score = curscore;
8087 cpstats.got_only_move = 0;
8088 cpstats.movelist[0] = '\0';
8090 if (buf1[0] != NULLCHAR) {
8091 safeStrCpy( cpstats.movelist, buf1, sizeof(cpstats.movelist)/sizeof(cpstats.movelist[0]) );
8094 cpstats.ok_to_send = 0;
8095 cpstats.line_is_book = 0;
8096 cpstats.nr_moves = 0;
8097 cpstats.moves_left = 0;
8099 SendProgramStatsToFrontend( cps, &cpstats );
8106 /* Parse a game score from the character string "game", and
8107 record it as the history of the current game. The game
8108 score is NOT assumed to start from the standard position.
8109 The display is not updated in any way.
8112 ParseGameHistory(game)
8116 int fromX, fromY, toX, toY, boardIndex;
8121 if (appData.debugMode)
8122 fprintf(debugFP, "Parsing game history: %s\n", game);
8124 if (gameInfo.event == NULL) gameInfo.event = StrSave("ICS game");
8125 gameInfo.site = StrSave(appData.icsHost);
8126 gameInfo.date = PGNDate();
8127 gameInfo.round = StrSave("-");
8129 /* Parse out names of players */
8130 while (*game == ' ') game++;
8132 while (*game != ' ') *p++ = *game++;
8134 gameInfo.white = StrSave(buf);
8135 while (*game == ' ') game++;
8137 while (*game != ' ' && *game != '\n') *p++ = *game++;
8139 gameInfo.black = StrSave(buf);
8142 boardIndex = blackPlaysFirst ? 1 : 0;
8145 yyboardindex = boardIndex;
8146 moveType = (ChessMove) yylex();
8148 case IllegalMove: /* maybe suicide chess, etc. */
8149 if (appData.debugMode) {
8150 fprintf(debugFP, "Illegal move from ICS: '%s'\n", yy_text);
8151 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
8152 setbuf(debugFP, NULL);
8154 case WhitePromotion:
8155 case BlackPromotion:
8156 case WhiteNonPromotion:
8157 case BlackNonPromotion:
8159 case WhiteCapturesEnPassant:
8160 case BlackCapturesEnPassant:
8161 case WhiteKingSideCastle:
8162 case WhiteQueenSideCastle:
8163 case BlackKingSideCastle:
8164 case BlackQueenSideCastle:
8165 case WhiteKingSideCastleWild:
8166 case WhiteQueenSideCastleWild:
8167 case BlackKingSideCastleWild:
8168 case BlackQueenSideCastleWild:
8170 case WhiteHSideCastleFR:
8171 case WhiteASideCastleFR:
8172 case BlackHSideCastleFR:
8173 case BlackASideCastleFR:
8175 fromX = currentMoveString[0] - AAA;
8176 fromY = currentMoveString[1] - ONE;
8177 toX = currentMoveString[2] - AAA;
8178 toY = currentMoveString[3] - ONE;
8179 promoChar = currentMoveString[4];
8183 fromX = moveType == WhiteDrop ?
8184 (int) CharToPiece(ToUpper(currentMoveString[0])) :
8185 (int) CharToPiece(ToLower(currentMoveString[0]));
8187 toX = currentMoveString[2] - AAA;
8188 toY = currentMoveString[3] - ONE;
8189 promoChar = NULLCHAR;
8193 snprintf(buf, MSG_SIZ, _("Ambiguous move in ICS output: \"%s\""), yy_text);
8194 if (appData.debugMode) {
8195 fprintf(debugFP, "Ambiguous move from ICS: '%s'\n", yy_text);
8196 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
8197 setbuf(debugFP, NULL);
8199 DisplayError(buf, 0);
8201 case ImpossibleMove:
8203 snprintf(buf, MSG_SIZ, _("Illegal move in ICS output: \"%s\""), yy_text);
8204 if (appData.debugMode) {
8205 fprintf(debugFP, "Impossible move from ICS: '%s'\n", yy_text);
8206 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
8207 setbuf(debugFP, NULL);
8209 DisplayError(buf, 0);
8212 if (boardIndex < backwardMostMove) {
8213 /* Oops, gap. How did that happen? */
8214 DisplayError(_("Gap in move list"), 0);
8217 backwardMostMove = blackPlaysFirst ? 1 : 0;
8218 if (boardIndex > forwardMostMove) {
8219 forwardMostMove = boardIndex;
8223 if (boardIndex > (blackPlaysFirst ? 1 : 0)) {
8224 strcat(parseList[boardIndex-1], " ");
8225 strcat(parseList[boardIndex-1], yy_text);
8237 case GameUnfinished:
8238 if (gameMode == IcsExamining) {
8239 if (boardIndex < backwardMostMove) {
8240 /* Oops, gap. How did that happen? */
8243 backwardMostMove = blackPlaysFirst ? 1 : 0;
8246 gameInfo.result = moveType;
8247 p = strchr(yy_text, '{');
8248 if (p == NULL) p = strchr(yy_text, '(');
8251 if (p[0] == '0' || p[0] == '1' || p[0] == '*') p = "";
8253 q = strchr(p, *p == '{' ? '}' : ')');
8254 if (q != NULL) *q = NULLCHAR;
8257 gameInfo.resultDetails = StrSave(p);
8260 if (boardIndex >= forwardMostMove &&
8261 !(gameMode == IcsObserving && ics_gamenum == -1)) {
8262 backwardMostMove = blackPlaysFirst ? 1 : 0;
8265 (void) CoordsToAlgebraic(boards[boardIndex], PosFlags(boardIndex),
8266 fromY, fromX, toY, toX, promoChar,
8267 parseList[boardIndex]);
8268 CopyBoard(boards[boardIndex + 1], boards[boardIndex]);
8269 /* currentMoveString is set as a side-effect of yylex */
8270 safeStrCpy(moveList[boardIndex], currentMoveString, sizeof(moveList[boardIndex])/sizeof(moveList[boardIndex][0]));
8271 strcat(moveList[boardIndex], "\n");
8273 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[boardIndex]);
8274 switch (MateTest(boards[boardIndex], PosFlags(boardIndex)) ) {
8280 if(gameInfo.variant != VariantShogi)
8281 strcat(parseList[boardIndex - 1], "+");
8285 strcat(parseList[boardIndex - 1], "#");
8292 /* Apply a move to the given board */
8294 ApplyMove(fromX, fromY, toX, toY, promoChar, board)
8295 int fromX, fromY, toX, toY;
8299 ChessSquare captured = board[toY][toX], piece, king; int p, oldEP = EP_NONE, berolina = 0;
8300 int promoRank = gameInfo.variant == VariantMakruk ? 3 : 1;
8302 /* [HGM] compute & store e.p. status and castling rights for new position */
8303 /* we can always do that 'in place', now pointers to these rights are passed to ApplyMove */
8305 if(gameInfo.variant == VariantBerolina) berolina = EP_BEROLIN_A;
8306 oldEP = (signed char)board[EP_STATUS];
8307 board[EP_STATUS] = EP_NONE;
8309 if( board[toY][toX] != EmptySquare )
8310 board[EP_STATUS] = EP_CAPTURE;
8312 /* [HGM] In Shatranj and Courier all promotions are to Ferz */
8313 if((gameInfo.variant==VariantShatranj || gameInfo.variant==VariantCourier || gameInfo.variant == VariantMakruk)
8314 && promoChar != 0) promoChar = PieceToChar(WhiteFerz);
8316 if (fromY == DROP_RANK) {
8318 piece = board[toY][toX] = (ChessSquare) fromX;
8322 if( board[fromY][fromX] == WhitePawn ) {
8323 if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
8324 board[EP_STATUS] = EP_PAWN_MOVE;
8326 if(toX>BOARD_LEFT && board[toY][toX-1] == BlackPawn &&
8327 gameInfo.variant != VariantBerolina || toX < fromX)
8328 board[EP_STATUS] = toX | berolina;
8329 if(toX<BOARD_RGHT-1 && board[toY][toX+1] == BlackPawn &&
8330 gameInfo.variant != VariantBerolina || toX > fromX)
8331 board[EP_STATUS] = toX;
8334 if( board[fromY][fromX] == BlackPawn ) {
8335 if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
8336 board[EP_STATUS] = EP_PAWN_MOVE;
8337 if( toY-fromY== -2) {
8338 if(toX>BOARD_LEFT && board[toY][toX-1] == WhitePawn &&
8339 gameInfo.variant != VariantBerolina || toX < fromX)
8340 board[EP_STATUS] = toX | berolina;
8341 if(toX<BOARD_RGHT-1 && board[toY][toX+1] == WhitePawn &&
8342 gameInfo.variant != VariantBerolina || toX > fromX)
8343 board[EP_STATUS] = toX;
8347 for(i=0; i<nrCastlingRights; i++) {
8348 if(board[CASTLING][i] == fromX && castlingRank[i] == fromY ||
8349 board[CASTLING][i] == toX && castlingRank[i] == toY
8350 ) board[CASTLING][i] = NoRights; // revoke for moved or captured piece
8353 if (fromX == toX && fromY == toY) return;
8355 piece = board[fromY][fromX]; /* [HGM] remember, for Shogi promotion */
8356 king = piece < (int) BlackPawn ? WhiteKing : BlackKing; /* [HGM] Knightmate simplify testing for castling */
8357 if(gameInfo.variant == VariantKnightmate)
8358 king += (int) WhiteUnicorn - (int) WhiteKing;
8360 /* Code added by Tord: */
8361 /* FRC castling assumed when king captures friendly rook. */
8362 if (board[fromY][fromX] == WhiteKing &&
8363 board[toY][toX] == WhiteRook) {
8364 board[fromY][fromX] = EmptySquare;
8365 board[toY][toX] = EmptySquare;
8367 board[0][BOARD_RGHT-2] = WhiteKing; board[0][BOARD_RGHT-3] = WhiteRook;
8369 board[0][BOARD_LEFT+2] = WhiteKing; board[0][BOARD_LEFT+3] = WhiteRook;
8371 } else if (board[fromY][fromX] == BlackKing &&
8372 board[toY][toX] == BlackRook) {
8373 board[fromY][fromX] = EmptySquare;
8374 board[toY][toX] = EmptySquare;
8376 board[BOARD_HEIGHT-1][BOARD_RGHT-2] = BlackKing; board[BOARD_HEIGHT-1][BOARD_RGHT-3] = BlackRook;
8378 board[BOARD_HEIGHT-1][BOARD_LEFT+2] = BlackKing; board[BOARD_HEIGHT-1][BOARD_LEFT+3] = BlackRook;
8380 /* End of code added by Tord */
8382 } else if (board[fromY][fromX] == king
8383 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
8384 && toY == fromY && toX > fromX+1) {
8385 board[fromY][fromX] = EmptySquare;
8386 board[toY][toX] = king;
8387 board[toY][toX-1] = board[fromY][BOARD_RGHT-1];
8388 board[fromY][BOARD_RGHT-1] = EmptySquare;
8389 } else if (board[fromY][fromX] == king
8390 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
8391 && toY == fromY && toX < fromX-1) {
8392 board[fromY][fromX] = EmptySquare;
8393 board[toY][toX] = king;
8394 board[toY][toX+1] = board[fromY][BOARD_LEFT];
8395 board[fromY][BOARD_LEFT] = EmptySquare;
8396 } else if (board[fromY][fromX] == WhitePawn
8397 && toY >= BOARD_HEIGHT-promoRank
8398 && gameInfo.variant != VariantXiangqi
8400 /* white pawn promotion */
8401 board[toY][toX] = CharToPiece(ToUpper(promoChar));
8402 if (board[toY][toX] == EmptySquare) {
8403 board[toY][toX] = WhiteQueen;
8405 if(gameInfo.variant==VariantBughouse ||
8406 gameInfo.variant==VariantCrazyhouse) /* [HGM] use shadow piece */
8407 board[toY][toX] = (ChessSquare) (PROMOTED board[toY][toX]);
8408 board[fromY][fromX] = EmptySquare;
8409 } else if ((fromY == BOARD_HEIGHT-4)
8411 && gameInfo.variant != VariantXiangqi
8412 && gameInfo.variant != VariantBerolina
8413 && (board[fromY][fromX] == WhitePawn)
8414 && (board[toY][toX] == EmptySquare)) {
8415 board[fromY][fromX] = EmptySquare;
8416 board[toY][toX] = WhitePawn;
8417 captured = board[toY - 1][toX];
8418 board[toY - 1][toX] = EmptySquare;
8419 } else if ((fromY == BOARD_HEIGHT-4)
8421 && gameInfo.variant == VariantBerolina
8422 && (board[fromY][fromX] == WhitePawn)
8423 && (board[toY][toX] == EmptySquare)) {
8424 board[fromY][fromX] = EmptySquare;
8425 board[toY][toX] = WhitePawn;
8426 if(oldEP & EP_BEROLIN_A) {
8427 captured = board[fromY][fromX-1];
8428 board[fromY][fromX-1] = EmptySquare;
8429 }else{ captured = board[fromY][fromX+1];
8430 board[fromY][fromX+1] = EmptySquare;
8432 } else if (board[fromY][fromX] == king
8433 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
8434 && toY == fromY && toX > fromX+1) {
8435 board[fromY][fromX] = EmptySquare;
8436 board[toY][toX] = king;
8437 board[toY][toX-1] = board[fromY][BOARD_RGHT-1];
8438 board[fromY][BOARD_RGHT-1] = EmptySquare;
8439 } else if (board[fromY][fromX] == king
8440 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
8441 && toY == fromY && toX < fromX-1) {
8442 board[fromY][fromX] = EmptySquare;
8443 board[toY][toX] = king;
8444 board[toY][toX+1] = board[fromY][BOARD_LEFT];
8445 board[fromY][BOARD_LEFT] = EmptySquare;
8446 } else if (fromY == 7 && fromX == 3
8447 && board[fromY][fromX] == BlackKing
8448 && toY == 7 && toX == 5) {
8449 board[fromY][fromX] = EmptySquare;
8450 board[toY][toX] = BlackKing;
8451 board[fromY][7] = EmptySquare;
8452 board[toY][4] = BlackRook;
8453 } else if (fromY == 7 && fromX == 3
8454 && board[fromY][fromX] == BlackKing
8455 && toY == 7 && toX == 1) {
8456 board[fromY][fromX] = EmptySquare;
8457 board[toY][toX] = BlackKing;
8458 board[fromY][0] = EmptySquare;
8459 board[toY][2] = BlackRook;
8460 } else if (board[fromY][fromX] == BlackPawn
8462 && gameInfo.variant != VariantXiangqi
8464 /* black pawn promotion */
8465 board[toY][toX] = CharToPiece(ToLower(promoChar));
8466 if (board[toY][toX] == EmptySquare) {
8467 board[toY][toX] = BlackQueen;
8469 if(gameInfo.variant==VariantBughouse ||
8470 gameInfo.variant==VariantCrazyhouse) /* [HGM] use shadow piece */
8471 board[toY][toX] = (ChessSquare) (PROMOTED board[toY][toX]);
8472 board[fromY][fromX] = EmptySquare;
8473 } else if ((fromY == 3)
8475 && gameInfo.variant != VariantXiangqi
8476 && gameInfo.variant != VariantBerolina
8477 && (board[fromY][fromX] == BlackPawn)
8478 && (board[toY][toX] == EmptySquare)) {
8479 board[fromY][fromX] = EmptySquare;
8480 board[toY][toX] = BlackPawn;
8481 captured = board[toY + 1][toX];
8482 board[toY + 1][toX] = EmptySquare;
8483 } else if ((fromY == 3)
8485 && gameInfo.variant == VariantBerolina
8486 && (board[fromY][fromX] == BlackPawn)
8487 && (board[toY][toX] == EmptySquare)) {
8488 board[fromY][fromX] = EmptySquare;
8489 board[toY][toX] = BlackPawn;
8490 if(oldEP & EP_BEROLIN_A) {
8491 captured = board[fromY][fromX-1];
8492 board[fromY][fromX-1] = EmptySquare;
8493 }else{ captured = board[fromY][fromX+1];
8494 board[fromY][fromX+1] = EmptySquare;
8497 board[toY][toX] = board[fromY][fromX];
8498 board[fromY][fromX] = EmptySquare;
8502 if (gameInfo.holdingsWidth != 0) {
8504 /* !!A lot more code needs to be written to support holdings */
8505 /* [HGM] OK, so I have written it. Holdings are stored in the */
8506 /* penultimate board files, so they are automaticlly stored */
8507 /* in the game history. */
8508 if (fromY == DROP_RANK) {
8509 /* Delete from holdings, by decreasing count */
8510 /* and erasing image if necessary */
8512 if(p < (int) BlackPawn) { /* white drop */
8513 p -= (int)WhitePawn;
8514 p = PieceToNumber((ChessSquare)p);
8515 if(p >= gameInfo.holdingsSize) p = 0;
8516 if(--board[p][BOARD_WIDTH-2] <= 0)
8517 board[p][BOARD_WIDTH-1] = EmptySquare;
8518 if((int)board[p][BOARD_WIDTH-2] < 0)
8519 board[p][BOARD_WIDTH-2] = 0;
8520 } else { /* black drop */
8521 p -= (int)BlackPawn;
8522 p = PieceToNumber((ChessSquare)p);
8523 if(p >= gameInfo.holdingsSize) p = 0;
8524 if(--board[BOARD_HEIGHT-1-p][1] <= 0)
8525 board[BOARD_HEIGHT-1-p][0] = EmptySquare;
8526 if((int)board[BOARD_HEIGHT-1-p][1] < 0)
8527 board[BOARD_HEIGHT-1-p][1] = 0;
8530 if (captured != EmptySquare && gameInfo.holdingsSize > 0
8531 && gameInfo.variant != VariantBughouse ) {
8532 /* [HGM] holdings: Add to holdings, if holdings exist */
8533 if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat) {
8534 // [HGM] superchess: suppress flipping color of captured pieces by reverse pre-flip
8535 captured = (int) captured >= (int) BlackPawn ? BLACK_TO_WHITE captured : WHITE_TO_BLACK captured;
8538 if (p >= (int) BlackPawn) {
8539 p -= (int)BlackPawn;
8540 if(gameInfo.variant == VariantShogi && DEMOTED p >= 0) {
8541 /* in Shogi restore piece to its original first */
8542 captured = (ChessSquare) (DEMOTED captured);
8545 p = PieceToNumber((ChessSquare)p);
8546 if(p >= gameInfo.holdingsSize) { p = 0; captured = BlackPawn; }
8547 board[p][BOARD_WIDTH-2]++;
8548 board[p][BOARD_WIDTH-1] = BLACK_TO_WHITE captured;
8550 p -= (int)WhitePawn;
8551 if(gameInfo.variant == VariantShogi && DEMOTED p >= 0) {
8552 captured = (ChessSquare) (DEMOTED captured);
8555 p = PieceToNumber((ChessSquare)p);
8556 if(p >= gameInfo.holdingsSize) { p = 0; captured = WhitePawn; }
8557 board[BOARD_HEIGHT-1-p][1]++;
8558 board[BOARD_HEIGHT-1-p][0] = WHITE_TO_BLACK captured;
8561 } else if (gameInfo.variant == VariantAtomic) {
8562 if (captured != EmptySquare) {
8564 for (y = toY-1; y <= toY+1; y++) {
8565 for (x = toX-1; x <= toX+1; x++) {
8566 if (y >= 0 && y < BOARD_HEIGHT && x >= BOARD_LEFT && x < BOARD_RGHT &&
8567 board[y][x] != WhitePawn && board[y][x] != BlackPawn) {
8568 board[y][x] = EmptySquare;
8572 board[toY][toX] = EmptySquare;
8575 if(promoChar == '+') {
8576 /* [HGM] Shogi-style promotions, to piece implied by original (Might overwrite orinary Pawn promotion) */
8577 board[toY][toX] = (ChessSquare) (PROMOTED piece);
8578 } else if(!appData.testLegality) { // without legality testing, unconditionally believe promoChar
8579 board[toY][toX] = CharToPiece(promoChar);
8581 if((gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat)
8582 && promoChar != NULLCHAR && gameInfo.holdingsSize) {
8583 // [HGM] superchess: take promotion piece out of holdings
8584 int k = PieceToNumber(CharToPiece(ToUpper(promoChar)));
8585 if((int)piece < (int)BlackPawn) { // determine stm from piece color
8586 if(!--board[k][BOARD_WIDTH-2])
8587 board[k][BOARD_WIDTH-1] = EmptySquare;
8589 if(!--board[BOARD_HEIGHT-1-k][1])
8590 board[BOARD_HEIGHT-1-k][0] = EmptySquare;
8596 /* Updates forwardMostMove */
8598 MakeMove(fromX, fromY, toX, toY, promoChar)
8599 int fromX, fromY, toX, toY;
8602 // forwardMostMove++; // [HGM] bare: moved downstream
8604 if(serverMoves != NULL) { /* [HGM] write moves on file for broadcasting (should be separate routine, really) */
8605 int timeLeft; static int lastLoadFlag=0; int king, piece;
8606 piece = boards[forwardMostMove][fromY][fromX];
8607 king = piece < (int) BlackPawn ? WhiteKing : BlackKing;
8608 if(gameInfo.variant == VariantKnightmate)
8609 king += (int) WhiteUnicorn - (int) WhiteKing;
8610 if(forwardMostMove == 0) {
8612 fprintf(serverMoves, "%s;", second.tidy);
8613 fprintf(serverMoves, "%s;", first.tidy);
8614 if(!blackPlaysFirst)
8615 fprintf(serverMoves, "%s;", second.tidy);
8616 } else fprintf(serverMoves, loadFlag|lastLoadFlag ? ":" : ";");
8617 lastLoadFlag = loadFlag;
8619 fprintf(serverMoves, "%c%c:%c%c", AAA+fromX, ONE+fromY, AAA+toX, ONE+toY);
8620 // print castling suffix
8621 if( toY == fromY && piece == king ) {
8623 fprintf(serverMoves, ":%c%c:%c%c", AAA+BOARD_RGHT-1, ONE+fromY, AAA+toX-1,ONE+toY);
8625 fprintf(serverMoves, ":%c%c:%c%c", AAA+BOARD_LEFT, ONE+fromY, AAA+toX+1,ONE+toY);
8628 if( (boards[forwardMostMove][fromY][fromX] == WhitePawn ||
8629 boards[forwardMostMove][fromY][fromX] == BlackPawn ) &&
8630 boards[forwardMostMove][toY][toX] == EmptySquare
8631 && fromX != toX && fromY != toY)
8632 fprintf(serverMoves, ":%c%c:%c%c", AAA+fromX, ONE+fromY, AAA+toX, ONE+fromY);
8634 if(promoChar != NULLCHAR)
8635 fprintf(serverMoves, ":%c:%c%c", promoChar, AAA+toX, ONE+toY);
8637 fprintf(serverMoves, "/%d/%d",
8638 pvInfoList[forwardMostMove].depth, pvInfoList[forwardMostMove].score);
8639 if(forwardMostMove+1 & 1) timeLeft = whiteTimeRemaining/1000;
8640 else timeLeft = blackTimeRemaining/1000;
8641 fprintf(serverMoves, "/%d", timeLeft);
8643 fflush(serverMoves);
8646 if (forwardMostMove+1 > framePtr) { // [HGM] vari: do not run into saved variations
8647 DisplayFatalError(_("Game too long; increase MAX_MOVES and recompile"),
8651 UnLoadPV(); // [HGM] pv: if we are looking at a PV, abort this
8652 if (commentList[forwardMostMove+1] != NULL) {
8653 free(commentList[forwardMostMove+1]);
8654 commentList[forwardMostMove+1] = NULL;
8656 CopyBoard(boards[forwardMostMove+1], boards[forwardMostMove]);
8657 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[forwardMostMove+1]);
8658 // forwardMostMove++; // [HGM] bare: moved to after ApplyMove, to make sure clock interrupt finds complete board
8659 SwitchClocks(forwardMostMove+1); // [HGM] race: incrementing move nr inside
8660 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
8661 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
8662 gameInfo.result = GameUnfinished;
8663 if (gameInfo.resultDetails != NULL) {
8664 free(gameInfo.resultDetails);
8665 gameInfo.resultDetails = NULL;
8667 CoordsToComputerAlgebraic(fromY, fromX, toY, toX, promoChar,
8668 moveList[forwardMostMove - 1]);
8669 (void) CoordsToAlgebraic(boards[forwardMostMove - 1],
8670 PosFlags(forwardMostMove - 1),
8671 fromY, fromX, toY, toX, promoChar,
8672 parseList[forwardMostMove - 1]);
8673 switch (MateTest(boards[forwardMostMove], PosFlags(forwardMostMove)) ) {
8679 if(gameInfo.variant != VariantShogi)
8680 strcat(parseList[forwardMostMove - 1], "+");
8684 strcat(parseList[forwardMostMove - 1], "#");
8687 if (appData.debugMode) {
8688 fprintf(debugFP, "move: %s, parse: %s (%c)\n", moveList[forwardMostMove-1], parseList[forwardMostMove-1], moveList[forwardMostMove-1][4]);
8693 /* Updates currentMove if not pausing */
8695 ShowMove(fromX, fromY, toX, toY)
8697 int instant = (gameMode == PlayFromGameFile) ?
8698 (matchMode || (appData.timeDelay == 0 && !pausing)) : pausing;
8699 if(appData.noGUI) return;
8700 if (!pausing || gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
8702 if (forwardMostMove == currentMove + 1) {
8703 AnimateMove(boards[forwardMostMove - 1],
8704 fromX, fromY, toX, toY);
8706 if (appData.highlightLastMove) {
8707 SetHighlights(fromX, fromY, toX, toY);
8710 currentMove = forwardMostMove;
8713 if (instant) return;
8715 DisplayMove(currentMove - 1);
8716 DrawPosition(FALSE, boards[currentMove]);
8717 DisplayBothClocks();
8718 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
8721 void SendEgtPath(ChessProgramState *cps)
8722 { /* [HGM] EGT: match formats given in feature with those given by user, and send info for each match */
8723 char buf[MSG_SIZ], name[MSG_SIZ], *p;
8725 if((p = cps->egtFormats) == NULL || appData.egtFormats == NULL) return;
8728 char c, *q = name+1, *r, *s;
8730 name[0] = ','; // extract next format name from feature and copy with prefixed ','
8731 while(*p && *p != ',') *q++ = *p++;
8733 if( appData.defaultPathEGTB && appData.defaultPathEGTB[0] &&
8734 strcmp(name, ",nalimov:") == 0 ) {
8735 // take nalimov path from the menu-changeable option first, if it is defined
8736 snprintf(buf, MSG_SIZ, "egtpath nalimov %s\n", appData.defaultPathEGTB);
8737 SendToProgram(buf,cps); // send egtbpath command for nalimov
8739 if( (s = StrStr(appData.egtFormats, name+1)) == appData.egtFormats ||
8740 (s = StrStr(appData.egtFormats, name)) != NULL) {
8741 // format name occurs amongst user-supplied formats, at beginning or immediately after comma
8742 s = r = StrStr(s, ":") + 1; // beginning of path info
8743 while(*r && *r != ',') r++; // path info is everything upto next ';' or end of string
8744 c = *r; *r = 0; // temporarily null-terminate path info
8745 *--q = 0; // strip of trailig ':' from name
8746 snprintf(buf, MSG_SIZ, "egtpath %s %s\n", name+1, s);
8748 SendToProgram(buf,cps); // send egtbpath command for this format
8750 if(*p == ',') p++; // read away comma to position for next format name
8755 InitChessProgram(cps, setup)
8756 ChessProgramState *cps;
8757 int setup; /* [HGM] needed to setup FRC opening position */
8759 char buf[MSG_SIZ], b[MSG_SIZ]; int overruled;
8760 if (appData.noChessProgram) return;
8761 hintRequested = FALSE;
8762 bookRequested = FALSE;
8764 /* [HGM] some new WB protocol commands to configure engine are sent now, if engine supports them */
8765 /* moved to before sending initstring in 4.3.15, so Polyglot can delay UCI 'isready' to recepton of 'new' */
8766 if(cps->memSize) { /* [HGM] memory */
8767 snprintf(buf, MSG_SIZ, "memory %d\n", appData.defaultHashSize + appData.defaultCacheSizeEGTB);
8768 SendToProgram(buf, cps);
8770 SendEgtPath(cps); /* [HGM] EGT */
8771 if(cps->maxCores) { /* [HGM] SMP: (protocol specified must be last settings command before new!) */
8772 snprintf(buf, MSG_SIZ, "cores %d\n", appData.smpCores);
8773 SendToProgram(buf, cps);
8776 SendToProgram(cps->initString, cps);
8777 if (gameInfo.variant != VariantNormal &&
8778 gameInfo.variant != VariantLoadable
8779 /* [HGM] also send variant if board size non-standard */
8780 || gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0
8782 char *v = VariantName(gameInfo.variant);
8783 if (cps->protocolVersion != 1 && StrStr(cps->variants, v) == NULL) {
8784 /* [HGM] in protocol 1 we have to assume all variants valid */
8785 snprintf(buf, MSG_SIZ, _("Variant %s not supported by %s"), v, cps->tidy);
8786 DisplayFatalError(buf, 0, 1);
8790 /* [HGM] make prefix for non-standard board size. Awkward testing... */
8791 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
8792 if( gameInfo.variant == VariantXiangqi )
8793 overruled = gameInfo.boardWidth != 9 || gameInfo.boardHeight != 10 || gameInfo.holdingsSize != 0;
8794 if( gameInfo.variant == VariantShogi )
8795 overruled = gameInfo.boardWidth != 9 || gameInfo.boardHeight != 9 || gameInfo.holdingsSize != 7;
8796 if( gameInfo.variant == VariantBughouse || gameInfo.variant == VariantCrazyhouse )
8797 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 5;
8798 if( gameInfo.variant == VariantCapablanca || gameInfo.variant == VariantCapaRandom ||
8799 gameInfo.variant == VariantGothic || gameInfo.variant == VariantFalcon )
8800 overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
8801 if( gameInfo.variant == VariantCourier )
8802 overruled = gameInfo.boardWidth != 12 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
8803 if( gameInfo.variant == VariantSuper )
8804 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 8;
8805 if( gameInfo.variant == VariantGreat )
8806 overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 8;
8809 snprintf(b, MSG_SIZ, "%dx%d+%d_%s", gameInfo.boardWidth, gameInfo.boardHeight,
8810 gameInfo.holdingsSize, VariantName(gameInfo.variant)); // cook up sized variant name
8811 /* [HGM] varsize: try first if this defiant size variant is specifically known */
8812 if(StrStr(cps->variants, b) == NULL) {
8813 // specific sized variant not known, check if general sizing allowed
8814 if (cps->protocolVersion != 1) { // for protocol 1 we cannot check and hope for the best
8815 if(StrStr(cps->variants, "boardsize") == NULL) {
8816 snprintf(buf, MSG_SIZ, "Board size %dx%d+%d not supported by %s",
8817 gameInfo.boardWidth, gameInfo.boardHeight, gameInfo.holdingsSize, cps->tidy);
8818 DisplayFatalError(buf, 0, 1);
8821 /* [HGM] here we really should compare with the maximum supported board size */
8824 } else snprintf(b, MSG_SIZ,"%s", VariantName(gameInfo.variant));
8825 snprintf(buf, MSG_SIZ, "variant %s\n", b);
8826 SendToProgram(buf, cps);
8828 currentlyInitializedVariant = gameInfo.variant;
8830 /* [HGM] send opening position in FRC to first engine */
8832 SendToProgram("force\n", cps);
8834 /* engine is now in force mode! Set flag to wake it up after first move. */
8835 setboardSpoiledMachineBlack = 1;
8839 snprintf(buf, sizeof(buf), "ics %s\n", appData.icsActive ? appData.icsHost : "-");
8840 SendToProgram(buf, cps);
8842 cps->maybeThinking = FALSE;
8843 cps->offeredDraw = 0;
8844 if (!appData.icsActive) {
8845 SendTimeControl(cps, movesPerSession, timeControl,
8846 timeIncrement, appData.searchDepth,
8849 if (appData.showThinking
8850 // [HGM] thinking: four options require thinking output to be sent
8851 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp()
8853 SendToProgram("post\n", cps);
8855 SendToProgram("hard\n", cps);
8856 if (!appData.ponderNextMove) {
8857 /* Warning: "easy" is a toggle in GNU Chess, so don't send
8858 it without being sure what state we are in first. "hard"
8859 is not a toggle, so that one is OK.
8861 SendToProgram("easy\n", cps);
8864 snprintf(buf, MSG_SIZ, "ping %d\n", ++cps->lastPing);
8865 SendToProgram(buf, cps);
8867 cps->initDone = TRUE;
8872 StartChessProgram(cps)
8873 ChessProgramState *cps;
8878 if (appData.noChessProgram) return;
8879 cps->initDone = FALSE;
8881 if (strcmp(cps->host, "localhost") == 0) {
8882 err = StartChildProcess(cps->program, cps->dir, &cps->pr);
8883 } else if (*appData.remoteShell == NULLCHAR) {
8884 err = OpenRcmd(cps->host, appData.remoteUser, cps->program, &cps->pr);
8886 if (*appData.remoteUser == NULLCHAR) {
8887 snprintf(buf, sizeof(buf), "%s %s %s", appData.remoteShell, cps->host,
8890 snprintf(buf, sizeof(buf), "%s %s -l %s %s", appData.remoteShell,
8891 cps->host, appData.remoteUser, cps->program);
8893 err = StartChildProcess(buf, "", &cps->pr);
8897 snprintf(buf, MSG_SIZ, _("Startup failure on '%s'"), cps->program);
8898 DisplayFatalError(buf, err, 1);
8904 cps->isr = AddInputSource(cps->pr, TRUE, ReceiveFromProgram, cps);
8905 if (cps->protocolVersion > 1) {
8906 snprintf(buf, MSG_SIZ, "xboard\nprotover %d\n", cps->protocolVersion);
8907 cps->nrOptions = 0; // [HGM] options: clear all engine-specific options
8908 cps->comboCnt = 0; // and values of combo boxes
8909 SendToProgram(buf, cps);
8911 SendToProgram("xboard\n", cps);
8917 TwoMachinesEventIfReady P((void))
8919 if (first.lastPing != first.lastPong) {
8920 DisplayMessage("", _("Waiting for first chess program"));
8921 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10); // [HGM] fast: lowered from 1000
8924 if (second.lastPing != second.lastPong) {
8925 DisplayMessage("", _("Waiting for second chess program"));
8926 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10); // [HGM] fast: lowered from 1000
8934 NextMatchGame P((void))
8936 int index; /* [HGM] autoinc: step load index during match */
8938 if (*appData.loadGameFile != NULLCHAR) {
8939 index = appData.loadGameIndex;
8940 if(index < 0) { // [HGM] autoinc
8941 lastIndex = index = (index == -2 && first.twoMachinesColor[0] == 'b') ? lastIndex : lastIndex+1;
8942 if(appData.rewindIndex > 0 && index > appData.rewindIndex) lastIndex = index = 1;
8944 LoadGameFromFile(appData.loadGameFile,
8946 appData.loadGameFile, FALSE);
8947 } else if (*appData.loadPositionFile != NULLCHAR) {
8948 index = appData.loadPositionIndex;
8949 if(index < 0) { // [HGM] autoinc
8950 lastIndex = index = (index == -2 && first.twoMachinesColor[0] == 'b') ? lastIndex : lastIndex+1;
8951 if(appData.rewindIndex > 0 && index > appData.rewindIndex) lastIndex = index = 1;
8953 LoadPositionFromFile(appData.loadPositionFile,
8955 appData.loadPositionFile);
8957 TwoMachinesEventIfReady();
8960 void UserAdjudicationEvent( int result )
8962 ChessMove gameResult = GameIsDrawn;
8965 gameResult = WhiteWins;
8967 else if( result < 0 ) {
8968 gameResult = BlackWins;
8971 if( gameMode == TwoMachinesPlay ) {
8972 GameEnds( gameResult, "User adjudication", GE_XBOARD );
8977 // [HGM] save: calculate checksum of game to make games easily identifiable
8978 int StringCheckSum(char *s)
8981 if(s==NULL) return 0;
8982 while(*s) i = i*259 + *s++;
8989 for(i=backwardMostMove; i<forwardMostMove; i++) {
8990 sum += pvInfoList[i].depth;
8991 sum += StringCheckSum(parseList[i]);
8992 sum += StringCheckSum(commentList[i]);
8995 if(i>1 && sum==0) sum++; // make sure never zero for non-empty game
8996 return sum + StringCheckSum(commentList[i]);
8997 } // end of save patch
9000 GameEnds(result, resultDetails, whosays)
9002 char *resultDetails;
9005 GameMode nextGameMode;
9007 char buf[MSG_SIZ], popupRequested = 0;
9009 if(endingGame) return; /* [HGM] crash: forbid recursion */
9011 if(twoBoards) { // [HGM] dual: switch back to one board
9012 twoBoards = partnerUp = 0; InitDrawingSizes(-2, 0);
9013 DrawPosition(TRUE, partnerBoard); // observed game becomes foreground
9015 if (appData.debugMode) {
9016 fprintf(debugFP, "GameEnds(%d, %s, %d)\n",
9017 result, resultDetails ? resultDetails : "(null)", whosays);
9020 fromX = fromY = -1; // [HGM] abort any move the user is entering.
9022 if (appData.icsActive && (whosays == GE_ENGINE || whosays >= GE_ENGINE1)) {
9023 /* If we are playing on ICS, the server decides when the
9024 game is over, but the engine can offer to draw, claim
9028 if (appData.zippyPlay && first.initDone) {
9029 if (result == GameIsDrawn) {
9030 /* In case draw still needs to be claimed */
9031 SendToICS(ics_prefix);
9032 SendToICS("draw\n");
9033 } else if (StrCaseStr(resultDetails, "resign")) {
9034 SendToICS(ics_prefix);
9035 SendToICS("resign\n");
9039 endingGame = 0; /* [HGM] crash */
9043 /* If we're loading the game from a file, stop */
9044 if (whosays == GE_FILE) {
9045 (void) StopLoadGameTimer();
9049 /* Cancel draw offers */
9050 first.offeredDraw = second.offeredDraw = 0;
9052 /* If this is an ICS game, only ICS can really say it's done;
9053 if not, anyone can. */
9054 isIcsGame = (gameMode == IcsPlayingWhite ||
9055 gameMode == IcsPlayingBlack ||
9056 gameMode == IcsObserving ||
9057 gameMode == IcsExamining);
9059 if (!isIcsGame || whosays == GE_ICS) {
9060 /* OK -- not an ICS game, or ICS said it was done */
9062 if (!isIcsGame && !appData.noChessProgram)
9063 SetUserThinkingEnables();
9065 /* [HGM] if a machine claims the game end we verify this claim */
9066 if(gameMode == TwoMachinesPlay && appData.testClaims) {
9067 if(appData.testLegality && whosays >= GE_ENGINE1 ) {
9069 ChessMove trueResult = (ChessMove) -1;
9071 claimer = whosays == GE_ENGINE1 ? /* color of claimer */
9072 first.twoMachinesColor[0] :
9073 second.twoMachinesColor[0] ;
9075 // [HGM] losers: because the logic is becoming a bit hairy, determine true result first
9076 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_CHECKMATE) {
9077 /* [HGM] verify: engine mate claims accepted if they were flagged */
9078 trueResult = WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins;
9080 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_WINS) { // added code for games where being mated is a win
9081 /* [HGM] verify: engine mate claims accepted if they were flagged */
9082 trueResult = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
9084 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_STALEMATE) { // only used to indicate draws now
9085 trueResult = GameIsDrawn; // default; in variants where stalemate loses, Status is CHECKMATE
9088 // now verify win claims, but not in drop games, as we don't understand those yet
9089 if( (gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper
9090 || gameInfo.variant == VariantGreat) &&
9091 (result == WhiteWins && claimer == 'w' ||
9092 result == BlackWins && claimer == 'b' ) ) { // case to verify: engine claims own win
9093 if (appData.debugMode) {
9094 fprintf(debugFP, "result=%d sp=%d move=%d\n",
9095 result, (signed char)boards[forwardMostMove][EP_STATUS], forwardMostMove);
9097 if(result != trueResult) {
9098 snprintf(buf, MSG_SIZ, "False win claim: '%s'", resultDetails);
9099 result = claimer == 'w' ? BlackWins : WhiteWins;
9100 resultDetails = buf;
9103 if( result == GameIsDrawn && (signed char)boards[forwardMostMove][EP_STATUS] > EP_DRAWS
9104 && (forwardMostMove <= backwardMostMove ||
9105 (signed char)boards[forwardMostMove-1][EP_STATUS] > EP_DRAWS ||
9106 (claimer=='b')==(forwardMostMove&1))
9108 /* [HGM] verify: draws that were not flagged are false claims */
9109 snprintf(buf, MSG_SIZ, "False draw claim: '%s'", resultDetails);
9110 result = claimer == 'w' ? BlackWins : WhiteWins;
9111 resultDetails = buf;
9113 /* (Claiming a loss is accepted no questions asked!) */
9115 /* [HGM] bare: don't allow bare King to win */
9116 if((gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat)
9117 && gameInfo.variant != VariantLosers && gameInfo.variant != VariantGiveaway
9118 && gameInfo.variant != VariantSuicide // [HGM] losers: except in losers, of course...
9119 && result != GameIsDrawn)
9120 { int i, j, k=0, color = (result==WhiteWins ? (int)WhitePawn : (int)BlackPawn);
9121 for(j=BOARD_LEFT; j<BOARD_RGHT; j++) for(i=0; i<BOARD_HEIGHT; i++) {
9122 int p = (signed char)boards[forwardMostMove][i][j] - color;
9123 if(p >= 0 && p <= (int)WhiteKing) k++;
9125 if (appData.debugMode) {
9126 fprintf(debugFP, "GE(%d, %s, %d) bare king k=%d color=%d\n",
9127 result, resultDetails ? resultDetails : "(null)", whosays, k, color);
9130 result = GameIsDrawn;
9131 snprintf(buf, MSG_SIZ, "%s but bare king", resultDetails);
9132 resultDetails = buf;
9138 if(serverMoves != NULL && !loadFlag) { char c = '=';
9139 if(result==WhiteWins) c = '+';
9140 if(result==BlackWins) c = '-';
9141 if(resultDetails != NULL)
9142 fprintf(serverMoves, ";%c;%s\n", c, resultDetails);
9144 if (resultDetails != NULL) {
9145 gameInfo.result = result;
9146 gameInfo.resultDetails = StrSave(resultDetails);
9148 /* display last move only if game was not loaded from file */
9149 if ((whosays != GE_FILE) && (currentMove == forwardMostMove))
9150 DisplayMove(currentMove - 1);
9152 if (forwardMostMove != 0) {
9153 if (gameMode != PlayFromGameFile && gameMode != EditGame
9154 && lastSavedGame != GameCheckSum() // [HGM] save: suppress duplicates
9156 if (*appData.saveGameFile != NULLCHAR) {
9157 SaveGameToFile(appData.saveGameFile, TRUE);
9158 } else if (appData.autoSaveGames) {
9161 if (*appData.savePositionFile != NULLCHAR) {
9162 SavePositionToFile(appData.savePositionFile);
9167 /* Tell program how game ended in case it is learning */
9168 /* [HGM] Moved this to after saving the PGN, just in case */
9169 /* engine died and we got here through time loss. In that */
9170 /* case we will get a fatal error writing the pipe, which */
9171 /* would otherwise lose us the PGN. */
9172 /* [HGM] crash: not needed anymore, but doesn't hurt; */
9173 /* output during GameEnds should never be fatal anymore */
9174 if (gameMode == MachinePlaysWhite ||
9175 gameMode == MachinePlaysBlack ||
9176 gameMode == TwoMachinesPlay ||
9177 gameMode == IcsPlayingWhite ||
9178 gameMode == IcsPlayingBlack ||
9179 gameMode == BeginningOfGame) {
9181 snprintf(buf, MSG_SIZ, "result %s {%s}\n", PGNResult(result),
9183 if (first.pr != NoProc) {
9184 SendToProgram(buf, &first);
9186 if (second.pr != NoProc &&
9187 gameMode == TwoMachinesPlay) {
9188 SendToProgram(buf, &second);
9193 if (appData.icsActive) {
9194 if (appData.quietPlay &&
9195 (gameMode == IcsPlayingWhite ||
9196 gameMode == IcsPlayingBlack)) {
9197 SendToICS(ics_prefix);
9198 SendToICS("set shout 1\n");
9200 nextGameMode = IcsIdle;
9201 ics_user_moved = FALSE;
9202 /* clean up premove. It's ugly when the game has ended and the
9203 * premove highlights are still on the board.
9207 ClearPremoveHighlights();
9208 DrawPosition(FALSE, boards[currentMove]);
9210 if (whosays == GE_ICS) {
9213 if (gameMode == IcsPlayingWhite)
9215 else if(gameMode == IcsPlayingBlack)
9219 if (gameMode == IcsPlayingBlack)
9221 else if(gameMode == IcsPlayingWhite)
9228 PlayIcsUnfinishedSound();
9231 } else if (gameMode == EditGame ||
9232 gameMode == PlayFromGameFile ||
9233 gameMode == AnalyzeMode ||
9234 gameMode == AnalyzeFile) {
9235 nextGameMode = gameMode;
9237 nextGameMode = EndOfGame;
9242 nextGameMode = gameMode;
9245 if (appData.noChessProgram) {
9246 gameMode = nextGameMode;
9248 endingGame = 0; /* [HGM] crash */
9253 /* Put first chess program into idle state */
9254 if (first.pr != NoProc &&
9255 (gameMode == MachinePlaysWhite ||
9256 gameMode == MachinePlaysBlack ||
9257 gameMode == TwoMachinesPlay ||
9258 gameMode == IcsPlayingWhite ||
9259 gameMode == IcsPlayingBlack ||
9260 gameMode == BeginningOfGame)) {
9261 SendToProgram("force\n", &first);
9262 if (first.usePing) {
9264 snprintf(buf, MSG_SIZ, "ping %d\n", ++first.lastPing);
9265 SendToProgram(buf, &first);
9268 } else if (result != GameUnfinished || nextGameMode == IcsIdle) {
9269 /* Kill off first chess program */
9270 if (first.isr != NULL)
9271 RemoveInputSource(first.isr);
9274 if (first.pr != NoProc) {
9276 DoSleep( appData.delayBeforeQuit );
9277 SendToProgram("quit\n", &first);
9278 DoSleep( appData.delayAfterQuit );
9279 DestroyChildProcess(first.pr, first.useSigterm);
9284 /* Put second chess program into idle state */
9285 if (second.pr != NoProc &&
9286 gameMode == TwoMachinesPlay) {
9287 SendToProgram("force\n", &second);
9288 if (second.usePing) {
9290 snprintf(buf, MSG_SIZ, "ping %d\n", ++second.lastPing);
9291 SendToProgram(buf, &second);
9294 } else if (result != GameUnfinished || nextGameMode == IcsIdle) {
9295 /* Kill off second chess program */
9296 if (second.isr != NULL)
9297 RemoveInputSource(second.isr);
9300 if (second.pr != NoProc) {
9301 DoSleep( appData.delayBeforeQuit );
9302 SendToProgram("quit\n", &second);
9303 DoSleep( appData.delayAfterQuit );
9304 DestroyChildProcess(second.pr, second.useSigterm);
9309 if (matchMode && gameMode == TwoMachinesPlay) {
9312 if (first.twoMachinesColor[0] == 'w') {
9319 if (first.twoMachinesColor[0] == 'b') {
9328 if (matchGame < appData.matchGames) {
9330 if(appData.sameColorGames <= 1) { /* [HGM] alternate: suppress color swap */
9331 tmp = first.twoMachinesColor;
9332 first.twoMachinesColor = second.twoMachinesColor;
9333 second.twoMachinesColor = tmp;
9335 gameMode = nextGameMode;
9337 if(appData.matchPause>10000 || appData.matchPause<10)
9338 appData.matchPause = 10000; /* [HGM] make pause adjustable */
9339 ScheduleDelayedEvent(NextMatchGame, appData.matchPause);
9340 endingGame = 0; /* [HGM] crash */
9343 gameMode = nextGameMode;
9344 snprintf(buf, MSG_SIZ, _("Match %s vs. %s: final score %d-%d-%d"),
9345 first.tidy, second.tidy,
9346 first.matchWins, second.matchWins,
9347 appData.matchGames - (first.matchWins + second.matchWins));
9348 popupRequested++; // [HGM] crash: postpone to after resetting endingGame
9351 if ((gameMode == AnalyzeMode || gameMode == AnalyzeFile) &&
9352 !(nextGameMode == AnalyzeMode || nextGameMode == AnalyzeFile))
9354 gameMode = nextGameMode;
9356 endingGame = 0; /* [HGM] crash */
9357 if(popupRequested) { // [HGM] crash: this calls GameEnds recursively through ExitEvent! Make it a harmless tail recursion.
9358 if(matchMode == TRUE) DisplayFatalError(buf, 0, 0); else {
9359 matchMode = FALSE; appData.matchGames = matchGame = 0;
9365 /* Assumes program was just initialized (initString sent).
9366 Leaves program in force mode. */
9368 FeedMovesToProgram(cps, upto)
9369 ChessProgramState *cps;
9374 if (appData.debugMode)
9375 fprintf(debugFP, "Feeding %smoves %d through %d to %s chess program\n",
9376 startedFromSetupPosition ? "position and " : "",
9377 backwardMostMove, upto, cps->which);
9378 if(currentlyInitializedVariant != gameInfo.variant) {
9380 // [HGM] variantswitch: make engine aware of new variant
9381 if(cps->protocolVersion > 1 && StrStr(cps->variants, VariantName(gameInfo.variant)) == NULL)
9382 return; // [HGM] refrain from feeding moves altogether if variant is unsupported!
9383 snprintf(buf, MSG_SIZ, "variant %s\n", VariantName(gameInfo.variant));
9384 SendToProgram(buf, cps);
9385 currentlyInitializedVariant = gameInfo.variant;
9387 SendToProgram("force\n", cps);
9388 if (startedFromSetupPosition) {
9389 SendBoard(cps, backwardMostMove);
9390 if (appData.debugMode) {
9391 fprintf(debugFP, "feedMoves\n");
9394 for (i = backwardMostMove; i < upto; i++) {
9395 SendMoveToProgram(i, cps);
9401 ResurrectChessProgram()
9403 /* The chess program may have exited.
9404 If so, restart it and feed it all the moves made so far. */
9406 if (appData.noChessProgram || first.pr != NoProc) return;
9408 StartChessProgram(&first);
9409 InitChessProgram(&first, FALSE);
9410 FeedMovesToProgram(&first, currentMove);
9412 if (!first.sendTime) {
9413 /* can't tell gnuchess what its clock should read,
9414 so we bow to its notion. */
9416 timeRemaining[0][currentMove] = whiteTimeRemaining;
9417 timeRemaining[1][currentMove] = blackTimeRemaining;
9420 if ((gameMode == AnalyzeMode || gameMode == AnalyzeFile ||
9421 appData.icsEngineAnalyze) && first.analysisSupport) {
9422 SendToProgram("analyze\n", &first);
9423 first.analyzing = TRUE;
9436 if (appData.debugMode) {
9437 fprintf(debugFP, "Reset(%d, %d) from gameMode %d\n",
9438 redraw, init, gameMode);
9440 CleanupTail(); // [HGM] vari: delete any stored variations
9441 pausing = pauseExamInvalid = FALSE;
9442 startedFromSetupPosition = blackPlaysFirst = FALSE;
9444 whiteFlag = blackFlag = FALSE;
9445 userOfferedDraw = FALSE;
9446 hintRequested = bookRequested = FALSE;
9447 first.maybeThinking = FALSE;
9448 second.maybeThinking = FALSE;
9449 first.bookSuspend = FALSE; // [HGM] book
9450 second.bookSuspend = FALSE;
9451 thinkOutput[0] = NULLCHAR;
9452 lastHint[0] = NULLCHAR;
9453 ClearGameInfo(&gameInfo);
9454 gameInfo.variant = StringToVariant(appData.variant);
9455 ics_user_moved = ics_clock_paused = FALSE;
9456 ics_getting_history = H_FALSE;
9458 white_holding[0] = black_holding[0] = NULLCHAR;
9459 ClearProgramStats();
9460 opponentKibitzes = FALSE; // [HGM] kibitz: do not reserve space in engine-output window in zippy mode
9464 flipView = appData.flipView;
9465 ClearPremoveHighlights();
9467 alarmSounded = FALSE;
9469 GameEnds(EndOfFile, NULL, GE_PLAYER);
9470 if(appData.serverMovesName != NULL) {
9471 /* [HGM] prepare to make moves file for broadcasting */
9472 clock_t t = clock();
9473 if(serverMoves != NULL) fclose(serverMoves);
9474 serverMoves = fopen(appData.serverMovesName, "r");
9475 if(serverMoves != NULL) {
9476 fclose(serverMoves);
9477 /* delay 15 sec before overwriting, so all clients can see end */
9478 while(clock()-t < appData.serverPause*CLOCKS_PER_SEC);
9480 serverMoves = fopen(appData.serverMovesName, "w");
9484 gameMode = BeginningOfGame;
9486 if(appData.icsActive) gameInfo.variant = VariantNormal;
9487 currentMove = forwardMostMove = backwardMostMove = 0;
9488 InitPosition(redraw);
9489 for (i = 0; i < MAX_MOVES; i++) {
9490 if (commentList[i] != NULL) {
9491 free(commentList[i]);
9492 commentList[i] = NULL;
9496 timeRemaining[0][0] = whiteTimeRemaining;
9497 timeRemaining[1][0] = blackTimeRemaining;
9498 if (first.pr == NULL) {
9499 StartChessProgram(&first);
9502 InitChessProgram(&first, startedFromSetupPosition);
9505 DisplayMessage("", "");
9506 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
9507 lastSavedGame = 0; // [HGM] save: make sure next game counts as unsaved
9514 if (!AutoPlayOneMove())
9516 if (matchMode || appData.timeDelay == 0)
9518 if (appData.timeDelay < 0 || gameMode == AnalyzeFile)
9520 StartLoadGameTimer((long)(1000.0 * appData.timeDelay));
9529 int fromX, fromY, toX, toY;
9531 if (appData.debugMode) {
9532 fprintf(debugFP, "AutoPlayOneMove(): current %d\n", currentMove);
9535 if (gameMode != PlayFromGameFile)
9538 if (currentMove >= forwardMostMove) {
9539 gameMode = EditGame;
9542 /* [AS] Clear current move marker at the end of a game */
9543 /* HistorySet(parseList, backwardMostMove, forwardMostMove, -1); */
9548 toX = moveList[currentMove][2] - AAA;
9549 toY = moveList[currentMove][3] - ONE;
9551 if (moveList[currentMove][1] == '@') {
9552 if (appData.highlightLastMove) {
9553 SetHighlights(-1, -1, toX, toY);
9556 fromX = moveList[currentMove][0] - AAA;
9557 fromY = moveList[currentMove][1] - ONE;
9559 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove); /* [AS] */
9561 AnimateMove(boards[currentMove], fromX, fromY, toX, toY);
9563 if (appData.highlightLastMove) {
9564 SetHighlights(fromX, fromY, toX, toY);
9567 DisplayMove(currentMove);
9568 SendMoveToProgram(currentMove++, &first);
9569 DisplayBothClocks();
9570 DrawPosition(FALSE, boards[currentMove]);
9571 // [HGM] PV info: always display, routine tests if empty
9572 DisplayComment(currentMove - 1, commentList[currentMove]);
9578 LoadGameOneMove(readAhead)
9579 ChessMove readAhead;
9581 int fromX = 0, fromY = 0, toX = 0, toY = 0, done;
9582 char promoChar = NULLCHAR;
9587 if (gameMode != PlayFromGameFile && gameMode != AnalyzeFile &&
9588 gameMode != AnalyzeMode && gameMode != Training) {
9593 yyboardindex = forwardMostMove;
9594 if (readAhead != EndOfFile) {
9595 moveType = readAhead;
9597 if (gameFileFP == NULL)
9599 moveType = (ChessMove) yylex();
9605 if (appData.debugMode)
9606 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
9609 /* append the comment but don't display it */
9610 AppendComment(currentMove, p, FALSE);
9613 case WhiteCapturesEnPassant:
9614 case BlackCapturesEnPassant:
9615 case WhitePromotion:
9616 case BlackPromotion:
9617 case WhiteNonPromotion:
9618 case BlackNonPromotion:
9620 case WhiteKingSideCastle:
9621 case WhiteQueenSideCastle:
9622 case BlackKingSideCastle:
9623 case BlackQueenSideCastle:
9624 case WhiteKingSideCastleWild:
9625 case WhiteQueenSideCastleWild:
9626 case BlackKingSideCastleWild:
9627 case BlackQueenSideCastleWild:
9629 case WhiteHSideCastleFR:
9630 case WhiteASideCastleFR:
9631 case BlackHSideCastleFR:
9632 case BlackASideCastleFR:
9634 if (appData.debugMode)
9635 fprintf(debugFP, "Parsed %s into %s\n", yy_text, currentMoveString);
9636 fromX = currentMoveString[0] - AAA;
9637 fromY = currentMoveString[1] - ONE;
9638 toX = currentMoveString[2] - AAA;
9639 toY = currentMoveString[3] - ONE;
9640 promoChar = currentMoveString[4];
9645 if (appData.debugMode)
9646 fprintf(debugFP, "Parsed %s into %s\n", yy_text, currentMoveString);
9647 fromX = moveType == WhiteDrop ?
9648 (int) CharToPiece(ToUpper(currentMoveString[0])) :
9649 (int) CharToPiece(ToLower(currentMoveString[0]));
9651 toX = currentMoveString[2] - AAA;
9652 toY = currentMoveString[3] - ONE;
9658 case GameUnfinished:
9659 if (appData.debugMode)
9660 fprintf(debugFP, "Parsed game end: %s\n", yy_text);
9661 p = strchr(yy_text, '{');
9662 if (p == NULL) p = strchr(yy_text, '(');
9665 if (p[0] == '0' || p[0] == '1' || p[0] == '*') p = "";
9667 q = strchr(p, *p == '{' ? '}' : ')');
9668 if (q != NULL) *q = NULLCHAR;
9671 GameEnds(moveType, p, GE_FILE);
9673 if (cmailMsgLoaded) {
9675 flipView = WhiteOnMove(currentMove);
9676 if (moveType == GameUnfinished) flipView = !flipView;
9677 if (appData.debugMode)
9678 fprintf(debugFP, "Setting flipView to %d\n", flipView) ;
9683 if (appData.debugMode)
9684 fprintf(debugFP, "Parser hit end of file\n");
9685 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
9691 if (WhiteOnMove(currentMove)) {
9692 GameEnds(BlackWins, "Black mates", GE_FILE);
9694 GameEnds(WhiteWins, "White mates", GE_FILE);
9698 GameEnds(GameIsDrawn, "Stalemate", GE_FILE);
9705 if (lastLoadGameStart == GNUChessGame) {
9706 /* GNUChessGames have numbers, but they aren't move numbers */
9707 if (appData.debugMode)
9708 fprintf(debugFP, "Parser ignoring: '%s' (%d)\n",
9709 yy_text, (int) moveType);
9710 return LoadGameOneMove(EndOfFile); /* tail recursion */
9712 /* else fall thru */
9717 /* Reached start of next game in file */
9718 if (appData.debugMode)
9719 fprintf(debugFP, "Parsed start of next game: %s\n", yy_text);
9720 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
9726 if (WhiteOnMove(currentMove)) {
9727 GameEnds(BlackWins, "Black mates", GE_FILE);
9729 GameEnds(WhiteWins, "White mates", GE_FILE);
9733 GameEnds(GameIsDrawn, "Stalemate", GE_FILE);
9739 case PositionDiagram: /* should not happen; ignore */
9740 case ElapsedTime: /* ignore */
9741 case NAG: /* ignore */
9742 if (appData.debugMode)
9743 fprintf(debugFP, "Parser ignoring: '%s' (%d)\n",
9744 yy_text, (int) moveType);
9745 return LoadGameOneMove(EndOfFile); /* tail recursion */
9748 if (appData.testLegality) {
9749 if (appData.debugMode)
9750 fprintf(debugFP, "Parsed IllegalMove: %s\n", yy_text);
9751 snprintf(move, MSG_SIZ, _("Illegal move: %d.%s%s"),
9752 (forwardMostMove / 2) + 1,
9753 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
9754 DisplayError(move, 0);
9757 if (appData.debugMode)
9758 fprintf(debugFP, "Parsed %s into IllegalMove %s\n",
9759 yy_text, currentMoveString);
9760 fromX = currentMoveString[0] - AAA;
9761 fromY = currentMoveString[1] - ONE;
9762 toX = currentMoveString[2] - AAA;
9763 toY = currentMoveString[3] - ONE;
9764 promoChar = currentMoveString[4];
9769 if (appData.debugMode)
9770 fprintf(debugFP, "Parsed AmbiguousMove: %s\n", yy_text);
9771 snprintf(move, MSG_SIZ, _("Ambiguous move: %d.%s%s"),
9772 (forwardMostMove / 2) + 1,
9773 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
9774 DisplayError(move, 0);
9779 case ImpossibleMove:
9780 if (appData.debugMode)
9781 fprintf(debugFP, "Parsed ImpossibleMove (type = %d): %s\n", moveType, yy_text);
9782 snprintf(move, MSG_SIZ, _("Illegal move: %d.%s%s"),
9783 (forwardMostMove / 2) + 1,
9784 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
9785 DisplayError(move, 0);
9791 if (appData.matchMode || (appData.timeDelay == 0 && !pausing)) {
9792 DrawPosition(FALSE, boards[currentMove]);
9793 DisplayBothClocks();
9794 if (!appData.matchMode) // [HGM] PV info: routine tests if empty
9795 DisplayComment(currentMove - 1, commentList[currentMove]);
9797 (void) StopLoadGameTimer();
9799 cmailOldMove = forwardMostMove;
9802 /* currentMoveString is set as a side-effect of yylex */
9803 strcat(currentMoveString, "\n");
9804 safeStrCpy(moveList[forwardMostMove], currentMoveString, sizeof(moveList[forwardMostMove])/sizeof(moveList[forwardMostMove][0]));
9806 thinkOutput[0] = NULLCHAR;
9807 MakeMove(fromX, fromY, toX, toY, promoChar);
9808 currentMove = forwardMostMove;
9813 /* Load the nth game from the given file */
9815 LoadGameFromFile(filename, n, title, useList)
9819 /*Boolean*/ int useList;
9824 if (strcmp(filename, "-") == 0) {
9828 f = fopen(filename, "rb");
9830 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
9831 DisplayError(buf, errno);
9835 if (fseek(f, 0, 0) == -1) {
9836 /* f is not seekable; probably a pipe */
9839 if (useList && n == 0) {
9840 int error = GameListBuild(f);
9842 DisplayError(_("Cannot build game list"), error);
9843 } else if (!ListEmpty(&gameList) &&
9844 ((ListGame *) gameList.tailPred)->number > 1) {
9845 GameListPopUp(f, title);
9852 return LoadGame(f, n, title, FALSE);
9857 MakeRegisteredMove()
9859 int fromX, fromY, toX, toY;
9861 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
9862 switch (cmailMoveType[lastLoadGameNumber - 1]) {
9865 if (appData.debugMode)
9866 fprintf(debugFP, "Restoring %s for game %d\n",
9867 cmailMove[lastLoadGameNumber - 1], lastLoadGameNumber);
9869 thinkOutput[0] = NULLCHAR;
9870 safeStrCpy(moveList[currentMove], cmailMove[lastLoadGameNumber - 1], sizeof(moveList[currentMove])/sizeof(moveList[currentMove][0]));
9871 fromX = cmailMove[lastLoadGameNumber - 1][0] - AAA;
9872 fromY = cmailMove[lastLoadGameNumber - 1][1] - ONE;
9873 toX = cmailMove[lastLoadGameNumber - 1][2] - AAA;
9874 toY = cmailMove[lastLoadGameNumber - 1][3] - ONE;
9875 promoChar = cmailMove[lastLoadGameNumber - 1][4];
9876 MakeMove(fromX, fromY, toX, toY, promoChar);
9877 ShowMove(fromX, fromY, toX, toY);
9879 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
9886 if (WhiteOnMove(currentMove)) {
9887 GameEnds(BlackWins, "Black mates", GE_PLAYER);
9889 GameEnds(WhiteWins, "White mates", GE_PLAYER);
9894 GameEnds(GameIsDrawn, "Stalemate", GE_PLAYER);
9901 if (WhiteOnMove(currentMove)) {
9902 GameEnds(BlackWins, "White resigns", GE_PLAYER);
9904 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
9909 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
9920 /* Wrapper around LoadGame for use when a Cmail message is loaded */
9922 CmailLoadGame(f, gameNumber, title, useList)
9930 if (gameNumber > nCmailGames) {
9931 DisplayError(_("No more games in this message"), 0);
9934 if (f == lastLoadGameFP) {
9935 int offset = gameNumber - lastLoadGameNumber;
9937 cmailMsg[0] = NULLCHAR;
9938 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
9939 cmailMoveRegistered[lastLoadGameNumber - 1] = FALSE;
9940 nCmailMovesRegistered--;
9942 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
9943 if (cmailResult[lastLoadGameNumber - 1] == CMAIL_NEW_RESULT) {
9944 cmailResult[lastLoadGameNumber - 1] = CMAIL_NOT_RESULT;
9947 if (! RegisterMove()) return FALSE;
9951 retVal = LoadGame(f, gameNumber, title, useList);
9953 /* Make move registered during previous look at this game, if any */
9954 MakeRegisteredMove();
9956 if (cmailCommentList[lastLoadGameNumber - 1] != NULL) {
9957 commentList[currentMove]
9958 = StrSave(cmailCommentList[lastLoadGameNumber - 1]);
9959 DisplayComment(currentMove - 1, commentList[currentMove]);
9965 /* Support for LoadNextGame, LoadPreviousGame, ReloadSameGame */
9970 int gameNumber = lastLoadGameNumber + offset;
9971 if (lastLoadGameFP == NULL) {
9972 DisplayError(_("No game has been loaded yet"), 0);
9975 if (gameNumber <= 0) {
9976 DisplayError(_("Can't back up any further"), 0);
9979 if (cmailMsgLoaded) {
9980 return CmailLoadGame(lastLoadGameFP, gameNumber,
9981 lastLoadGameTitle, lastLoadGameUseList);
9983 return LoadGame(lastLoadGameFP, gameNumber,
9984 lastLoadGameTitle, lastLoadGameUseList);
9990 /* Load the nth game from open file f */
9992 LoadGame(f, gameNumber, title, useList)
10000 int gn = gameNumber;
10001 ListGame *lg = NULL;
10002 int numPGNTags = 0;
10004 GameMode oldGameMode;
10005 VariantClass oldVariant = gameInfo.variant; /* [HGM] PGNvariant */
10007 if (appData.debugMode)
10008 fprintf(debugFP, "LoadGame(): on entry, gameMode %d\n", gameMode);
10010 if (gameMode == Training )
10011 SetTrainingModeOff();
10013 oldGameMode = gameMode;
10014 if (gameMode != BeginningOfGame) {
10015 Reset(FALSE, TRUE);
10019 if (lastLoadGameFP != NULL && lastLoadGameFP != f) {
10020 fclose(lastLoadGameFP);
10024 lg = (ListGame *) ListElem(&gameList, gameNumber-1);
10027 fseek(f, lg->offset, 0);
10028 GameListHighlight(gameNumber);
10032 DisplayError(_("Game number out of range"), 0);
10037 if (fseek(f, 0, 0) == -1) {
10038 if (f == lastLoadGameFP ?
10039 gameNumber == lastLoadGameNumber + 1 :
10043 DisplayError(_("Can't seek on game file"), 0);
10048 lastLoadGameFP = f;
10049 lastLoadGameNumber = gameNumber;
10050 safeStrCpy(lastLoadGameTitle, title, sizeof(lastLoadGameTitle)/sizeof(lastLoadGameTitle[0]));
10051 lastLoadGameUseList = useList;
10055 if (lg && lg->gameInfo.white && lg->gameInfo.black) {
10056 snprintf(buf, sizeof(buf), "%s vs. %s", lg->gameInfo.white,
10057 lg->gameInfo.black);
10059 } else if (*title != NULLCHAR) {
10060 if (gameNumber > 1) {
10061 snprintf(buf, MSG_SIZ, "%s %d", title, gameNumber);
10064 DisplayTitle(title);
10068 if (gameMode != AnalyzeFile && gameMode != AnalyzeMode) {
10069 gameMode = PlayFromGameFile;
10073 currentMove = forwardMostMove = backwardMostMove = 0;
10074 CopyBoard(boards[0], initialPosition);
10078 * Skip the first gn-1 games in the file.
10079 * Also skip over anything that precedes an identifiable
10080 * start of game marker, to avoid being confused by
10081 * garbage at the start of the file. Currently
10082 * recognized start of game markers are the move number "1",
10083 * the pattern "gnuchess .* game", the pattern
10084 * "^[#;%] [^ ]* game file", and a PGN tag block.
10085 * A game that starts with one of the latter two patterns
10086 * will also have a move number 1, possibly
10087 * following a position diagram.
10088 * 5-4-02: Let's try being more lenient and allowing a game to
10089 * start with an unnumbered move. Does that break anything?
10091 cm = lastLoadGameStart = EndOfFile;
10093 yyboardindex = forwardMostMove;
10094 cm = (ChessMove) yylex();
10097 if (cmailMsgLoaded) {
10098 nCmailGames = CMAIL_MAX_GAMES - gn;
10101 DisplayError(_("Game not found in file"), 0);
10108 lastLoadGameStart = cm;
10111 case MoveNumberOne:
10112 switch (lastLoadGameStart) {
10117 case MoveNumberOne:
10119 gn--; /* count this game */
10120 lastLoadGameStart = cm;
10129 switch (lastLoadGameStart) {
10132 case MoveNumberOne:
10134 gn--; /* count this game */
10135 lastLoadGameStart = cm;
10138 lastLoadGameStart = cm; /* game counted already */
10146 yyboardindex = forwardMostMove;
10147 cm = (ChessMove) yylex();
10148 } while (cm == PGNTag || cm == Comment);
10155 if (cmailMsgLoaded && (CMAIL_MAX_GAMES == lastLoadGameNumber)) {
10156 if ( cmailResult[CMAIL_MAX_GAMES - gn - 1]
10157 != CMAIL_OLD_RESULT) {
10159 cmailResult[ CMAIL_MAX_GAMES
10160 - gn - 1] = CMAIL_OLD_RESULT;
10166 /* Only a NormalMove can be at the start of a game
10167 * without a position diagram. */
10168 if (lastLoadGameStart == EndOfFile ) {
10170 lastLoadGameStart = MoveNumberOne;
10179 if (appData.debugMode)
10180 fprintf(debugFP, "Parsed game start '%s' (%d)\n", yy_text, (int) cm);
10182 if (cm == XBoardGame) {
10183 /* Skip any header junk before position diagram and/or move 1 */
10185 yyboardindex = forwardMostMove;
10186 cm = (ChessMove) yylex();
10188 if (cm == EndOfFile ||
10189 cm == GNUChessGame || cm == XBoardGame) {
10190 /* Empty game; pretend end-of-file and handle later */
10195 if (cm == MoveNumberOne || cm == PositionDiagram ||
10196 cm == PGNTag || cm == Comment)
10199 } else if (cm == GNUChessGame) {
10200 if (gameInfo.event != NULL) {
10201 free(gameInfo.event);
10203 gameInfo.event = StrSave(yy_text);
10206 startedFromSetupPosition = FALSE;
10207 while (cm == PGNTag) {
10208 if (appData.debugMode)
10209 fprintf(debugFP, "Parsed PGNTag: %s\n", yy_text);
10210 err = ParsePGNTag(yy_text, &gameInfo);
10211 if (!err) numPGNTags++;
10213 /* [HGM] PGNvariant: automatically switch to variant given in PGN tag */
10214 if(gameInfo.variant != oldVariant) {
10215 startedFromPositionFile = FALSE; /* [HGM] loadPos: variant switch likely makes position invalid */
10216 ResetFrontEnd(); // [HGM] might need other bitmaps. Cannot use Reset() because it clears gameInfo :-(
10217 InitPosition(TRUE);
10218 oldVariant = gameInfo.variant;
10219 if (appData.debugMode)
10220 fprintf(debugFP, "New variant %d\n", (int) oldVariant);
10224 if (gameInfo.fen != NULL) {
10225 Board initial_position;
10226 startedFromSetupPosition = TRUE;
10227 if (!ParseFEN(initial_position, &blackPlaysFirst, gameInfo.fen)) {
10229 DisplayError(_("Bad FEN position in file"), 0);
10232 CopyBoard(boards[0], initial_position);
10233 if (blackPlaysFirst) {
10234 currentMove = forwardMostMove = backwardMostMove = 1;
10235 CopyBoard(boards[1], initial_position);
10236 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
10237 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
10238 timeRemaining[0][1] = whiteTimeRemaining;
10239 timeRemaining[1][1] = blackTimeRemaining;
10240 if (commentList[0] != NULL) {
10241 commentList[1] = commentList[0];
10242 commentList[0] = NULL;
10245 currentMove = forwardMostMove = backwardMostMove = 0;
10247 /* [HGM] copy FEN attributes as well. Bugfix 4.3.14m and 4.3.15e: moved to after 'blackPlaysFirst' */
10249 initialRulePlies = FENrulePlies;
10250 for( i=0; i< nrCastlingRights; i++ )
10251 initialRights[i] = initial_position[CASTLING][i];
10253 yyboardindex = forwardMostMove;
10254 free(gameInfo.fen);
10255 gameInfo.fen = NULL;
10258 yyboardindex = forwardMostMove;
10259 cm = (ChessMove) yylex();
10261 /* Handle comments interspersed among the tags */
10262 while (cm == Comment) {
10264 if (appData.debugMode)
10265 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
10267 AppendComment(currentMove, p, FALSE);
10268 yyboardindex = forwardMostMove;
10269 cm = (ChessMove) yylex();
10273 /* don't rely on existence of Event tag since if game was
10274 * pasted from clipboard the Event tag may not exist
10276 if (numPGNTags > 0){
10278 if (gameInfo.variant == VariantNormal) {
10279 VariantClass v = StringToVariant(gameInfo.event);
10280 // [HGM] do not recognize variants from event tag that were introduced after supporting variant tag
10281 if(v < VariantShogi) gameInfo.variant = v;
10284 if( appData.autoDisplayTags ) {
10285 tags = PGNTags(&gameInfo);
10286 TagsPopUp(tags, CmailMsg());
10291 /* Make something up, but don't display it now */
10296 if (cm == PositionDiagram) {
10299 Board initial_position;
10301 if (appData.debugMode)
10302 fprintf(debugFP, "Parsed PositionDiagram: %s\n", yy_text);
10304 if (!startedFromSetupPosition) {
10306 for (i = BOARD_HEIGHT - 1; i >= 0; i--)
10307 for (j = BOARD_LEFT; j < BOARD_RGHT; p++)
10317 initial_position[i][j++] = CharToPiece(*p);
10320 while (*p == ' ' || *p == '\t' ||
10321 *p == '\n' || *p == '\r') p++;
10323 if (strncmp(p, "black", strlen("black"))==0)
10324 blackPlaysFirst = TRUE;
10326 blackPlaysFirst = FALSE;
10327 startedFromSetupPosition = TRUE;
10329 CopyBoard(boards[0], initial_position);
10330 if (blackPlaysFirst) {
10331 currentMove = forwardMostMove = backwardMostMove = 1;
10332 CopyBoard(boards[1], initial_position);
10333 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
10334 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
10335 timeRemaining[0][1] = whiteTimeRemaining;
10336 timeRemaining[1][1] = blackTimeRemaining;
10337 if (commentList[0] != NULL) {
10338 commentList[1] = commentList[0];
10339 commentList[0] = NULL;
10342 currentMove = forwardMostMove = backwardMostMove = 0;
10345 yyboardindex = forwardMostMove;
10346 cm = (ChessMove) yylex();
10349 if (first.pr == NoProc) {
10350 StartChessProgram(&first);
10352 InitChessProgram(&first, FALSE);
10353 SendToProgram("force\n", &first);
10354 if (startedFromSetupPosition) {
10355 SendBoard(&first, forwardMostMove);
10356 if (appData.debugMode) {
10357 fprintf(debugFP, "Load Game\n");
10359 DisplayBothClocks();
10362 /* [HGM] server: flag to write setup moves in broadcast file as one */
10363 loadFlag = appData.suppressLoadMoves;
10365 while (cm == Comment) {
10367 if (appData.debugMode)
10368 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
10370 AppendComment(currentMove, p, FALSE);
10371 yyboardindex = forwardMostMove;
10372 cm = (ChessMove) yylex();
10375 if ((cm == EndOfFile && lastLoadGameStart != EndOfFile ) ||
10376 cm == WhiteWins || cm == BlackWins ||
10377 cm == GameIsDrawn || cm == GameUnfinished) {
10378 DisplayMessage("", _("No moves in game"));
10379 if (cmailMsgLoaded) {
10380 if (appData.debugMode)
10381 fprintf(debugFP, "Setting flipView to %d.\n", FALSE);
10385 DrawPosition(FALSE, boards[currentMove]);
10386 DisplayBothClocks();
10387 gameMode = EditGame;
10394 // [HGM] PV info: routine tests if comment empty
10395 if (!matchMode && (pausing || appData.timeDelay != 0)) {
10396 DisplayComment(currentMove - 1, commentList[currentMove]);
10398 if (!matchMode && appData.timeDelay != 0)
10399 DrawPosition(FALSE, boards[currentMove]);
10401 if (gameMode == AnalyzeFile || gameMode == AnalyzeMode) {
10402 programStats.ok_to_send = 1;
10405 /* if the first token after the PGN tags is a move
10406 * and not move number 1, retrieve it from the parser
10408 if (cm != MoveNumberOne)
10409 LoadGameOneMove(cm);
10411 /* load the remaining moves from the file */
10412 while (LoadGameOneMove(EndOfFile)) {
10413 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
10414 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
10417 /* rewind to the start of the game */
10418 currentMove = backwardMostMove;
10420 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
10422 if (oldGameMode == AnalyzeFile ||
10423 oldGameMode == AnalyzeMode) {
10424 AnalyzeFileEvent();
10427 if (matchMode || appData.timeDelay == 0) {
10429 gameMode = EditGame;
10431 } else if (appData.timeDelay > 0) {
10432 AutoPlayGameLoop();
10435 if (appData.debugMode)
10436 fprintf(debugFP, "LoadGame(): on exit, gameMode %d\n", gameMode);
10438 loadFlag = 0; /* [HGM] true game starts */
10442 /* Support for LoadNextPosition, LoadPreviousPosition, ReloadSamePosition */
10444 ReloadPosition(offset)
10447 int positionNumber = lastLoadPositionNumber + offset;
10448 if (lastLoadPositionFP == NULL) {
10449 DisplayError(_("No position has been loaded yet"), 0);
10452 if (positionNumber <= 0) {
10453 DisplayError(_("Can't back up any further"), 0);
10456 return LoadPosition(lastLoadPositionFP, positionNumber,
10457 lastLoadPositionTitle);
10460 /* Load the nth position from the given file */
10462 LoadPositionFromFile(filename, n, title)
10470 if (strcmp(filename, "-") == 0) {
10471 return LoadPosition(stdin, n, "stdin");
10473 f = fopen(filename, "rb");
10475 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
10476 DisplayError(buf, errno);
10479 return LoadPosition(f, n, title);
10484 /* Load the nth position from the given open file, and close it */
10486 LoadPosition(f, positionNumber, title)
10488 int positionNumber;
10491 char *p, line[MSG_SIZ];
10492 Board initial_position;
10493 int i, j, fenMode, pn;
10495 if (gameMode == Training )
10496 SetTrainingModeOff();
10498 if (gameMode != BeginningOfGame) {
10499 Reset(FALSE, TRUE);
10501 if (lastLoadPositionFP != NULL && lastLoadPositionFP != f) {
10502 fclose(lastLoadPositionFP);
10504 if (positionNumber == 0) positionNumber = 1;
10505 lastLoadPositionFP = f;
10506 lastLoadPositionNumber = positionNumber;
10507 safeStrCpy(lastLoadPositionTitle, title, sizeof(lastLoadPositionTitle)/sizeof(lastLoadPositionTitle[0]));
10508 if (first.pr == NoProc) {
10509 StartChessProgram(&first);
10510 InitChessProgram(&first, FALSE);
10512 pn = positionNumber;
10513 if (positionNumber < 0) {
10514 /* Negative position number means to seek to that byte offset */
10515 if (fseek(f, -positionNumber, 0) == -1) {
10516 DisplayError(_("Can't seek on position file"), 0);
10521 if (fseek(f, 0, 0) == -1) {
10522 if (f == lastLoadPositionFP ?
10523 positionNumber == lastLoadPositionNumber + 1 :
10524 positionNumber == 1) {
10527 DisplayError(_("Can't seek on position file"), 0);
10532 /* See if this file is FEN or old-style xboard */
10533 if (fgets(line, MSG_SIZ, f) == NULL) {
10534 DisplayError(_("Position not found in file"), 0);
10537 // [HGM] FEN can begin with digit, any piece letter valid in this variant, or a + for Shogi promoted pieces
10538 fenMode = line[0] >= '0' && line[0] <= '9' || line[0] == '+' || CharToPiece(line[0]) != EmptySquare;
10541 if (fenMode || line[0] == '#') pn--;
10543 /* skip positions before number pn */
10544 if (fgets(line, MSG_SIZ, f) == NULL) {
10546 DisplayError(_("Position not found in file"), 0);
10549 if (fenMode || line[0] == '#') pn--;
10554 if (!ParseFEN(initial_position, &blackPlaysFirst, line)) {
10555 DisplayError(_("Bad FEN position in file"), 0);
10559 (void) fgets(line, MSG_SIZ, f);
10560 (void) fgets(line, MSG_SIZ, f);
10562 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
10563 (void) fgets(line, MSG_SIZ, f);
10564 for (p = line, j = BOARD_LEFT; j < BOARD_RGHT; p++) {
10567 initial_position[i][j++] = CharToPiece(*p);
10571 blackPlaysFirst = FALSE;
10573 (void) fgets(line, MSG_SIZ, f);
10574 if (strncmp(line, "black", strlen("black"))==0)
10575 blackPlaysFirst = TRUE;
10578 startedFromSetupPosition = TRUE;
10580 SendToProgram("force\n", &first);
10581 CopyBoard(boards[0], initial_position);
10582 if (blackPlaysFirst) {
10583 currentMove = forwardMostMove = backwardMostMove = 1;
10584 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
10585 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
10586 CopyBoard(boards[1], initial_position);
10587 DisplayMessage("", _("Black to play"));
10589 currentMove = forwardMostMove = backwardMostMove = 0;
10590 DisplayMessage("", _("White to play"));
10592 initialRulePlies = FENrulePlies; /* [HGM] copy FEN attributes as well */
10593 SendBoard(&first, forwardMostMove);
10594 if (appData.debugMode) {
10596 for(i=0;i<2;i++){for(j=0;j<6;j++)fprintf(debugFP, " %d", boards[i][CASTLING][j]);fprintf(debugFP,"\n");}
10597 for(j=0;j<6;j++)fprintf(debugFP, " %d", initialRights[j]);fprintf(debugFP,"\n");
10598 fprintf(debugFP, "Load Position\n");
10601 if (positionNumber > 1) {
10602 snprintf(line, MSG_SIZ, "%s %d", title, positionNumber);
10603 DisplayTitle(line);
10605 DisplayTitle(title);
10607 gameMode = EditGame;
10610 timeRemaining[0][1] = whiteTimeRemaining;
10611 timeRemaining[1][1] = blackTimeRemaining;
10612 DrawPosition(FALSE, boards[currentMove]);
10619 CopyPlayerNameIntoFileName(dest, src)
10622 while (*src != NULLCHAR && *src != ',') {
10627 *(*dest)++ = *src++;
10632 char *DefaultFileName(ext)
10635 static char def[MSG_SIZ];
10638 if (gameInfo.white != NULL && gameInfo.white[0] != '-') {
10640 CopyPlayerNameIntoFileName(&p, gameInfo.white);
10642 CopyPlayerNameIntoFileName(&p, gameInfo.black);
10644 safeStrCpy(p, ext, MSG_SIZ-2-strlen(gameInfo.white)-strlen(gameInfo.black));
10651 /* Save the current game to the given file */
10653 SaveGameToFile(filename, append)
10660 if (strcmp(filename, "-") == 0) {
10661 return SaveGame(stdout, 0, NULL);
10663 f = fopen(filename, append ? "a" : "w");
10665 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
10666 DisplayError(buf, errno);
10669 return SaveGame(f, 0, NULL);
10678 static char buf[MSG_SIZ];
10681 p = strchr(str, ' ');
10682 if (p == NULL) return str;
10683 strncpy(buf, str, p - str);
10684 buf[p - str] = NULLCHAR;
10688 #define PGN_MAX_LINE 75
10690 #define PGN_SIDE_WHITE 0
10691 #define PGN_SIDE_BLACK 1
10694 static int FindFirstMoveOutOfBook( int side )
10698 if( backwardMostMove == 0 && ! startedFromSetupPosition) {
10699 int index = backwardMostMove;
10700 int has_book_hit = 0;
10702 if( (index % 2) != side ) {
10706 while( index < forwardMostMove ) {
10707 /* Check to see if engine is in book */
10708 int depth = pvInfoList[index].depth;
10709 int score = pvInfoList[index].score;
10715 else if( score == 0 && depth == 63 ) {
10716 in_book = 1; /* Zappa */
10718 else if( score == 2 && depth == 99 ) {
10719 in_book = 1; /* Abrok */
10722 has_book_hit += in_book;
10738 void GetOutOfBookInfo( char * buf )
10742 int offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
10744 oob[0] = FindFirstMoveOutOfBook( PGN_SIDE_WHITE );
10745 oob[1] = FindFirstMoveOutOfBook( PGN_SIDE_BLACK );
10749 if( oob[0] >= 0 || oob[1] >= 0 ) {
10750 for( i=0; i<2; i++ ) {
10754 if( i > 0 && oob[0] >= 0 ) {
10755 strcat( buf, " " );
10758 sprintf( buf+strlen(buf), "%d%s. ", (idx - offset)/2 + 1, idx & 1 ? ".." : "" );
10759 sprintf( buf+strlen(buf), "%s%.2f",
10760 pvInfoList[idx].score >= 0 ? "+" : "",
10761 pvInfoList[idx].score / 100.0 );
10767 /* Save game in PGN style and close the file */
10772 int i, offset, linelen, newblock;
10776 int movelen, numlen, blank;
10777 char move_buffer[100]; /* [AS] Buffer for move+PV info */
10779 offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
10781 tm = time((time_t *) NULL);
10783 PrintPGNTags(f, &gameInfo);
10785 if (backwardMostMove > 0 || startedFromSetupPosition) {
10786 char *fen = PositionToFEN(backwardMostMove, NULL);
10787 fprintf(f, "[FEN \"%s\"]\n[SetUp \"1\"]\n", fen);
10788 fprintf(f, "\n{--------------\n");
10789 PrintPosition(f, backwardMostMove);
10790 fprintf(f, "--------------}\n");
10794 /* [AS] Out of book annotation */
10795 if( appData.saveOutOfBookInfo ) {
10798 GetOutOfBookInfo( buf );
10800 if( buf[0] != '\0' ) {
10801 fprintf( f, "[%s \"%s\"]\n", PGN_OUT_OF_BOOK, buf );
10808 i = backwardMostMove;
10812 while (i < forwardMostMove) {
10813 /* Print comments preceding this move */
10814 if (commentList[i] != NULL) {
10815 if (linelen > 0) fprintf(f, "\n");
10816 fprintf(f, "%s", commentList[i]);
10821 /* Format move number */
10823 snprintf(numtext, sizeof(numtext)/sizeof(numtext[0]),"%d.", (i - offset)/2 + 1);
10826 snprintf(numtext, sizeof(numtext)/sizeof(numtext[0]), "%d...", (i - offset)/2 + 1);
10828 numtext[0] = NULLCHAR;
10830 numlen = strlen(numtext);
10833 /* Print move number */
10834 blank = linelen > 0 && numlen > 0;
10835 if (linelen + (blank ? 1 : 0) + numlen > PGN_MAX_LINE) {
10844 fprintf(f, "%s", numtext);
10848 safeStrCpy(move_buffer, SavePart(parseList[i]), sizeof(move_buffer)/sizeof(move_buffer[0])); // [HGM] pgn: print move via buffer, so it can be edited
10849 movelen = strlen(move_buffer); /* [HGM] pgn: line-break point before move */
10852 blank = linelen > 0 && movelen > 0;
10853 if (linelen + (blank ? 1 : 0) + movelen > PGN_MAX_LINE) {
10862 fprintf(f, "%s", move_buffer);
10863 linelen += movelen;
10865 /* [AS] Add PV info if present */
10866 if( i >= 0 && appData.saveExtendedInfoInPGN && pvInfoList[i].depth > 0 ) {
10867 /* [HGM] add time */
10868 char buf[MSG_SIZ]; int seconds;
10870 seconds = (pvInfoList[i].time+5)/10; // deci-seconds, rounded to nearest
10876 snprintf(buf, MSG_SIZ, " %3.1f%c", seconds/10., 0);
10879 seconds = (seconds + 4)/10; // round to full seconds
10881 snprintf(buf, MSG_SIZ, " %d%c", seconds, 0);
10883 snprintf(buf, MSG_SIZ, " %d:%02d%c", seconds/60, seconds%60, 0);
10886 snprintf( move_buffer, sizeof(move_buffer)/sizeof(move_buffer[0]),"{%s%.2f/%d%s}",
10887 pvInfoList[i].score >= 0 ? "+" : "",
10888 pvInfoList[i].score / 100.0,
10889 pvInfoList[i].depth,
10892 movelen = strlen(move_buffer); /* [HGM] pgn: line-break point after move */
10894 /* Print score/depth */
10895 blank = linelen > 0 && movelen > 0;
10896 if (linelen + (blank ? 1 : 0) + movelen > PGN_MAX_LINE) {
10905 fprintf(f, "%s", move_buffer);
10906 linelen += movelen;
10912 /* Start a new line */
10913 if (linelen > 0) fprintf(f, "\n");
10915 /* Print comments after last move */
10916 if (commentList[i] != NULL) {
10917 fprintf(f, "%s\n", commentList[i]);
10921 if (gameInfo.resultDetails != NULL &&
10922 gameInfo.resultDetails[0] != NULLCHAR) {
10923 fprintf(f, "{%s} %s\n\n", gameInfo.resultDetails,
10924 PGNResult(gameInfo.result));
10926 fprintf(f, "%s\n\n", PGNResult(gameInfo.result));
10930 lastSavedGame = GameCheckSum(); // [HGM] save: remember ID of last saved game to prevent double saving
10934 /* Save game in old style and close the file */
10936 SaveGameOldStyle(f)
10942 tm = time((time_t *) NULL);
10944 fprintf(f, "# %s game file -- %s", programName, ctime(&tm));
10947 if (backwardMostMove > 0 || startedFromSetupPosition) {
10948 fprintf(f, "\n[--------------\n");
10949 PrintPosition(f, backwardMostMove);
10950 fprintf(f, "--------------]\n");
10955 i = backwardMostMove;
10956 offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
10958 while (i < forwardMostMove) {
10959 if (commentList[i] != NULL) {
10960 fprintf(f, "[%s]\n", commentList[i]);
10963 if ((i % 2) == 1) {
10964 fprintf(f, "%d. ... %s\n", (i - offset)/2 + 1, parseList[i]);
10967 fprintf(f, "%d. %s ", (i - offset)/2 + 1, parseList[i]);
10969 if (commentList[i] != NULL) {
10973 if (i >= forwardMostMove) {
10977 fprintf(f, "%s\n", parseList[i]);
10982 if (commentList[i] != NULL) {
10983 fprintf(f, "[%s]\n", commentList[i]);
10986 /* This isn't really the old style, but it's close enough */
10987 if (gameInfo.resultDetails != NULL &&
10988 gameInfo.resultDetails[0] != NULLCHAR) {
10989 fprintf(f, "%s (%s)\n\n", PGNResult(gameInfo.result),
10990 gameInfo.resultDetails);
10992 fprintf(f, "%s\n\n", PGNResult(gameInfo.result));
10999 /* Save the current game to open file f and close the file */
11001 SaveGame(f, dummy, dummy2)
11006 if (gameMode == EditPosition) EditPositionDone(TRUE);
11007 lastSavedGame = GameCheckSum(); // [HGM] save: remember ID of last saved game to prevent double saving
11008 if (appData.oldSaveStyle)
11009 return SaveGameOldStyle(f);
11011 return SaveGamePGN(f);
11014 /* Save the current position to the given file */
11016 SavePositionToFile(filename)
11022 if (strcmp(filename, "-") == 0) {
11023 return SavePosition(stdout, 0, NULL);
11025 f = fopen(filename, "a");
11027 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
11028 DisplayError(buf, errno);
11031 SavePosition(f, 0, NULL);
11037 /* Save the current position to the given open file and close the file */
11039 SavePosition(f, dummy, dummy2)
11047 if (gameMode == EditPosition) EditPositionDone(TRUE);
11048 if (appData.oldSaveStyle) {
11049 tm = time((time_t *) NULL);
11051 fprintf(f, "# %s position file -- %s", programName, ctime(&tm));
11053 fprintf(f, "[--------------\n");
11054 PrintPosition(f, currentMove);
11055 fprintf(f, "--------------]\n");
11057 fen = PositionToFEN(currentMove, NULL);
11058 fprintf(f, "%s\n", fen);
11066 ReloadCmailMsgEvent(unregister)
11070 static char *inFilename = NULL;
11071 static char *outFilename;
11073 struct stat inbuf, outbuf;
11076 /* Any registered moves are unregistered if unregister is set, */
11077 /* i.e. invoked by the signal handler */
11079 for (i = 0; i < CMAIL_MAX_GAMES; i ++) {
11080 cmailMoveRegistered[i] = FALSE;
11081 if (cmailCommentList[i] != NULL) {
11082 free(cmailCommentList[i]);
11083 cmailCommentList[i] = NULL;
11086 nCmailMovesRegistered = 0;
11089 for (i = 0; i < CMAIL_MAX_GAMES; i ++) {
11090 cmailResult[i] = CMAIL_NOT_RESULT;
11094 if (inFilename == NULL) {
11095 /* Because the filenames are static they only get malloced once */
11096 /* and they never get freed */
11097 inFilename = (char *) malloc(strlen(appData.cmailGameName) + 9);
11098 sprintf(inFilename, "%s.game.in", appData.cmailGameName);
11100 outFilename = (char *) malloc(strlen(appData.cmailGameName) + 5);
11101 sprintf(outFilename, "%s.out", appData.cmailGameName);
11104 status = stat(outFilename, &outbuf);
11106 cmailMailedMove = FALSE;
11108 status = stat(inFilename, &inbuf);
11109 cmailMailedMove = (inbuf.st_mtime < outbuf.st_mtime);
11112 /* LoadGameFromFile(CMAIL_MAX_GAMES) with cmailMsgLoaded == TRUE
11113 counts the games, notes how each one terminated, etc.
11115 It would be nice to remove this kludge and instead gather all
11116 the information while building the game list. (And to keep it
11117 in the game list nodes instead of having a bunch of fixed-size
11118 parallel arrays.) Note this will require getting each game's
11119 termination from the PGN tags, as the game list builder does
11120 not process the game moves. --mann
11122 cmailMsgLoaded = TRUE;
11123 LoadGameFromFile(inFilename, CMAIL_MAX_GAMES, "", FALSE);
11125 /* Load first game in the file or popup game menu */
11126 LoadGameFromFile(inFilename, 0, appData.cmailGameName, TRUE);
11128 #endif /* !WIN32 */
11136 char string[MSG_SIZ];
11138 if ( cmailMailedMove
11139 || (cmailResult[lastLoadGameNumber - 1] == CMAIL_OLD_RESULT)) {
11140 return TRUE; /* Allow free viewing */
11143 /* Unregister move to ensure that we don't leave RegisterMove */
11144 /* with the move registered when the conditions for registering no */
11146 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
11147 cmailMoveRegistered[lastLoadGameNumber - 1] = FALSE;
11148 nCmailMovesRegistered --;
11150 if (cmailCommentList[lastLoadGameNumber - 1] != NULL)
11152 free(cmailCommentList[lastLoadGameNumber - 1]);
11153 cmailCommentList[lastLoadGameNumber - 1] = NULL;
11157 if (cmailOldMove == -1) {
11158 DisplayError(_("You have edited the game history.\nUse Reload Same Game and make your move again."), 0);
11162 if (currentMove > cmailOldMove + 1) {
11163 DisplayError(_("You have entered too many moves.\nBack up to the correct position and try again."), 0);
11167 if (currentMove < cmailOldMove) {
11168 DisplayError(_("Displayed position is not current.\nStep forward to the correct position and try again."), 0);
11172 if (forwardMostMove > currentMove) {
11173 /* Silently truncate extra moves */
11177 if ( (currentMove == cmailOldMove + 1)
11178 || ( (currentMove == cmailOldMove)
11179 && ( (cmailMoveType[lastLoadGameNumber - 1] == CMAIL_ACCEPT)
11180 || (cmailMoveType[lastLoadGameNumber - 1] == CMAIL_RESIGN)))) {
11181 if (gameInfo.result != GameUnfinished) {
11182 cmailResult[lastLoadGameNumber - 1] = CMAIL_NEW_RESULT;
11185 if (commentList[currentMove] != NULL) {
11186 cmailCommentList[lastLoadGameNumber - 1]
11187 = StrSave(commentList[currentMove]);
11189 safeStrCpy(cmailMove[lastLoadGameNumber - 1], moveList[currentMove - 1], sizeof(cmailMove[lastLoadGameNumber - 1])/sizeof(cmailMove[lastLoadGameNumber - 1][0]));
11191 if (appData.debugMode)
11192 fprintf(debugFP, "Saving %s for game %d\n",
11193 cmailMove[lastLoadGameNumber - 1], lastLoadGameNumber);
11195 snprintf(string, MSG_SIZ, "%s.game.out.%d", appData.cmailGameName, lastLoadGameNumber);
11197 f = fopen(string, "w");
11198 if (appData.oldSaveStyle) {
11199 SaveGameOldStyle(f); /* also closes the file */
11201 snprintf(string, MSG_SIZ, "%s.pos.out", appData.cmailGameName);
11202 f = fopen(string, "w");
11203 SavePosition(f, 0, NULL); /* also closes the file */
11205 fprintf(f, "{--------------\n");
11206 PrintPosition(f, currentMove);
11207 fprintf(f, "--------------}\n\n");
11209 SaveGame(f, 0, NULL); /* also closes the file*/
11212 cmailMoveRegistered[lastLoadGameNumber - 1] = TRUE;
11213 nCmailMovesRegistered ++;
11214 } else if (nCmailGames == 1) {
11215 DisplayError(_("You have not made a move yet"), 0);
11226 static char *partCommandString = "cmail -xv%s -remail -game %s 2>&1";
11227 FILE *commandOutput;
11228 char buffer[MSG_SIZ], msg[MSG_SIZ], string[MSG_SIZ];
11229 int nBytes = 0; /* Suppress warnings on uninitialized variables */
11235 if (! cmailMsgLoaded) {
11236 DisplayError(_("The cmail message is not loaded.\nUse Reload CMail Message and make your move again."), 0);
11240 if (nCmailGames == nCmailResults) {
11241 DisplayError(_("No unfinished games"), 0);
11245 #if CMAIL_PROHIBIT_REMAIL
11246 if (cmailMailedMove) {
11247 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);
11248 DisplayError(msg, 0);
11253 if (! (cmailMailedMove || RegisterMove())) return;
11255 if ( cmailMailedMove
11256 || (nCmailMovesRegistered + nCmailResults == nCmailGames)) {
11257 snprintf(string, MSG_SIZ, partCommandString,
11258 appData.debugMode ? " -v" : "", appData.cmailGameName);
11259 commandOutput = popen(string, "r");
11261 if (commandOutput == NULL) {
11262 DisplayError(_("Failed to invoke cmail"), 0);
11264 for (nBuffers = 0; (! feof(commandOutput)); nBuffers ++) {
11265 nBytes = fread(buffer, 1, MSG_SIZ - 1, commandOutput);
11267 if (nBuffers > 1) {
11268 (void) memcpy(msg, buffer + nBytes, MSG_SIZ - nBytes - 1);
11269 (void) memcpy(msg + MSG_SIZ - nBytes - 1, buffer, nBytes);
11270 nBytes = MSG_SIZ - 1;
11272 (void) memcpy(msg, buffer, nBytes);
11274 *(msg + nBytes) = '\0'; /* \0 for end-of-string*/
11276 if(StrStr(msg, "Mailed cmail message to ") != NULL) {
11277 cmailMailedMove = TRUE; /* Prevent >1 moves */
11280 for (i = 0; i < nCmailGames; i ++) {
11281 if (cmailResult[i] == CMAIL_NOT_RESULT) {
11286 && ( (arcDir = (char *) getenv("CMAIL_ARCDIR"))
11288 snprintf(buffer, MSG_SIZ, "%s/%s.%s.archive",
11290 appData.cmailGameName,
11292 LoadGameFromFile(buffer, 1, buffer, FALSE);
11293 cmailMsgLoaded = FALSE;
11297 DisplayInformation(msg);
11298 pclose(commandOutput);
11301 if ((*cmailMsg) != '\0') {
11302 DisplayInformation(cmailMsg);
11307 #endif /* !WIN32 */
11316 int prependComma = 0;
11318 char string[MSG_SIZ]; /* Space for game-list */
11321 if (!cmailMsgLoaded) return "";
11323 if (cmailMailedMove) {
11324 snprintf(cmailMsg, MSG_SIZ, _("Waiting for reply from opponent\n"));
11326 /* Create a list of games left */
11327 snprintf(string, MSG_SIZ, "[");
11328 for (i = 0; i < nCmailGames; i ++) {
11329 if (! ( cmailMoveRegistered[i]
11330 || (cmailResult[i] == CMAIL_OLD_RESULT))) {
11331 if (prependComma) {
11332 snprintf(number, sizeof(number)/sizeof(number[0]), ",%d", i + 1);
11334 snprintf(number, sizeof(number)/sizeof(number[0]), "%d", i + 1);
11338 strcat(string, number);
11341 strcat(string, "]");
11343 if (nCmailMovesRegistered + nCmailResults == 0) {
11344 switch (nCmailGames) {
11346 snprintf(cmailMsg, MSG_SIZ, _("Still need to make move for game\n"));
11350 snprintf(cmailMsg, MSG_SIZ, _("Still need to make moves for both games\n"));
11354 snprintf(cmailMsg, MSG_SIZ, _("Still need to make moves for all %d games\n"),
11359 switch (nCmailGames - nCmailMovesRegistered - nCmailResults) {
11361 snprintf(cmailMsg, MSG_SIZ, _("Still need to make a move for game %s\n"),
11366 if (nCmailResults == nCmailGames) {
11367 snprintf(cmailMsg, MSG_SIZ, _("No unfinished games\n"));
11369 snprintf(cmailMsg, MSG_SIZ, _("Ready to send mail\n"));
11374 snprintf(cmailMsg, MSG_SIZ, _("Still need to make moves for games %s\n"),
11386 if (gameMode == Training)
11387 SetTrainingModeOff();
11390 cmailMsgLoaded = FALSE;
11391 if (appData.icsActive) {
11392 SendToICS(ics_prefix);
11393 SendToICS("refresh\n");
11403 /* Give up on clean exit */
11407 /* Keep trying for clean exit */
11411 if (appData.icsActive && appData.colorize) Colorize(ColorNone, FALSE);
11413 if (telnetISR != NULL) {
11414 RemoveInputSource(telnetISR);
11416 if (icsPR != NoProc) {
11417 DestroyChildProcess(icsPR, TRUE);
11420 /* [HGM] crash: leave writing PGN and position entirely to GameEnds() */
11421 GameEnds(gameInfo.result, gameInfo.resultDetails==NULL ? "xboard exit" : gameInfo.resultDetails, GE_PLAYER);
11423 /* [HGM] crash: the above GameEnds() is a dud if another one was running */
11424 /* make sure this other one finishes before killing it! */
11425 if(endingGame) { int count = 0;
11426 if(appData.debugMode) fprintf(debugFP, "ExitEvent() during GameEnds(), wait\n");
11427 while(endingGame && count++ < 10) DoSleep(1);
11428 if(appData.debugMode && endingGame) fprintf(debugFP, "GameEnds() seems stuck, proceed exiting\n");
11431 /* Kill off chess programs */
11432 if (first.pr != NoProc) {
11435 DoSleep( appData.delayBeforeQuit );
11436 SendToProgram("quit\n", &first);
11437 DoSleep( appData.delayAfterQuit );
11438 DestroyChildProcess(first.pr, 10 /* [AS] first.useSigterm */ );
11440 if (second.pr != NoProc) {
11441 DoSleep( appData.delayBeforeQuit );
11442 SendToProgram("quit\n", &second);
11443 DoSleep( appData.delayAfterQuit );
11444 DestroyChildProcess(second.pr, 10 /* [AS] second.useSigterm */ );
11446 if (first.isr != NULL) {
11447 RemoveInputSource(first.isr);
11449 if (second.isr != NULL) {
11450 RemoveInputSource(second.isr);
11453 ShutDownFrontEnd();
11460 if (appData.debugMode)
11461 fprintf(debugFP, "PauseEvent(): pausing %d\n", pausing);
11465 if (gameMode == MachinePlaysWhite ||
11466 gameMode == MachinePlaysBlack) {
11469 DisplayBothClocks();
11471 if (gameMode == PlayFromGameFile) {
11472 if (appData.timeDelay >= 0)
11473 AutoPlayGameLoop();
11474 } else if (gameMode == IcsExamining && pauseExamInvalid) {
11475 Reset(FALSE, TRUE);
11476 SendToICS(ics_prefix);
11477 SendToICS("refresh\n");
11478 } else if (currentMove < forwardMostMove) {
11479 ForwardInner(forwardMostMove);
11481 pauseExamInvalid = FALSE;
11483 switch (gameMode) {
11487 pauseExamForwardMostMove = forwardMostMove;
11488 pauseExamInvalid = FALSE;
11491 case IcsPlayingWhite:
11492 case IcsPlayingBlack:
11496 case PlayFromGameFile:
11497 (void) StopLoadGameTimer();
11501 case BeginningOfGame:
11502 if (appData.icsActive) return;
11503 /* else fall through */
11504 case MachinePlaysWhite:
11505 case MachinePlaysBlack:
11506 case TwoMachinesPlay:
11507 if (forwardMostMove == 0)
11508 return; /* don't pause if no one has moved */
11509 if ((gameMode == MachinePlaysWhite &&
11510 !WhiteOnMove(forwardMostMove)) ||
11511 (gameMode == MachinePlaysBlack &&
11512 WhiteOnMove(forwardMostMove))) {
11525 char title[MSG_SIZ];
11527 if (currentMove < 1 || parseList[currentMove - 1][0] == NULLCHAR) {
11528 safeStrCpy(title, _("Edit comment"), sizeof(title)/sizeof(title[0]));
11530 snprintf(title, MSG_SIZ, _("Edit comment on %d.%s%s"), (currentMove - 1) / 2 + 1,
11531 WhiteOnMove(currentMove - 1) ? " " : ".. ",
11532 parseList[currentMove - 1]);
11535 EditCommentPopUp(currentMove, title, commentList[currentMove]);
11542 char *tags = PGNTags(&gameInfo);
11543 EditTagsPopUp(tags);
11550 if (appData.noChessProgram || gameMode == AnalyzeMode)
11553 if (gameMode != AnalyzeFile) {
11554 if (!appData.icsEngineAnalyze) {
11556 if (gameMode != EditGame) return;
11558 ResurrectChessProgram();
11559 SendToProgram("analyze\n", &first);
11560 first.analyzing = TRUE;
11561 /*first.maybeThinking = TRUE;*/
11562 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
11563 EngineOutputPopUp();
11565 if (!appData.icsEngineAnalyze) gameMode = AnalyzeMode;
11570 StartAnalysisClock();
11571 GetTimeMark(&lastNodeCountTime);
11578 if (appData.noChessProgram || gameMode == AnalyzeFile)
11581 if (gameMode != AnalyzeMode) {
11583 if (gameMode != EditGame) return;
11584 ResurrectChessProgram();
11585 SendToProgram("analyze\n", &first);
11586 first.analyzing = TRUE;
11587 /*first.maybeThinking = TRUE;*/
11588 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
11589 EngineOutputPopUp();
11591 gameMode = AnalyzeFile;
11596 StartAnalysisClock();
11597 GetTimeMark(&lastNodeCountTime);
11602 MachineWhiteEvent()
11605 char *bookHit = NULL;
11607 if (appData.noChessProgram || (gameMode == MachinePlaysWhite))
11611 if (gameMode == PlayFromGameFile ||
11612 gameMode == TwoMachinesPlay ||
11613 gameMode == Training ||
11614 gameMode == AnalyzeMode ||
11615 gameMode == EndOfGame)
11618 if (gameMode == EditPosition)
11619 EditPositionDone(TRUE);
11621 if (!WhiteOnMove(currentMove)) {
11622 DisplayError(_("It is not White's turn"), 0);
11626 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile)
11629 if (gameMode == EditGame || gameMode == AnalyzeMode ||
11630 gameMode == AnalyzeFile)
11633 ResurrectChessProgram(); /* in case it isn't running */
11634 if(gameMode == BeginningOfGame) { /* [HGM] time odds: to get right odds in human mode */
11635 gameMode = MachinePlaysWhite;
11638 gameMode = MachinePlaysWhite;
11642 snprintf(buf, MSG_SIZ, "%s vs. %s", gameInfo.white, gameInfo.black);
11644 if (first.sendName) {
11645 snprintf(buf, MSG_SIZ, "name %s\n", gameInfo.black);
11646 SendToProgram(buf, &first);
11648 if (first.sendTime) {
11649 if (first.useColors) {
11650 SendToProgram("black\n", &first); /*gnu kludge*/
11652 SendTimeRemaining(&first, TRUE);
11654 if (first.useColors) {
11655 SendToProgram("white\n", &first); // [HGM] book: send 'go' separately
11657 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: send go or retrieve book move
11658 SetMachineThinkingEnables();
11659 first.maybeThinking = TRUE;
11663 if (appData.autoFlipView && !flipView) {
11664 flipView = !flipView;
11665 DrawPosition(FALSE, NULL);
11666 DisplayBothClocks(); // [HGM] logo: clocks might have to be exchanged;
11669 if(bookHit) { // [HGM] book: simulate book reply
11670 static char bookMove[MSG_SIZ]; // a bit generous?
11672 programStats.nodes = programStats.depth = programStats.time =
11673 programStats.score = programStats.got_only_move = 0;
11674 sprintf(programStats.movelist, "%s (xbook)", bookHit);
11676 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
11677 strcat(bookMove, bookHit);
11678 HandleMachineMove(bookMove, &first);
11683 MachineBlackEvent()
11686 char *bookHit = NULL;
11688 if (appData.noChessProgram || (gameMode == MachinePlaysBlack))
11692 if (gameMode == PlayFromGameFile ||
11693 gameMode == TwoMachinesPlay ||
11694 gameMode == Training ||
11695 gameMode == AnalyzeMode ||
11696 gameMode == EndOfGame)
11699 if (gameMode == EditPosition)
11700 EditPositionDone(TRUE);
11702 if (WhiteOnMove(currentMove)) {
11703 DisplayError(_("It is not Black's turn"), 0);
11707 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile)
11710 if (gameMode == EditGame || gameMode == AnalyzeMode ||
11711 gameMode == AnalyzeFile)
11714 ResurrectChessProgram(); /* in case it isn't running */
11715 gameMode = MachinePlaysBlack;
11719 snprintf(buf, MSG_SIZ, "%s vs. %s", gameInfo.white, gameInfo.black);
11721 if (first.sendName) {
11722 snprintf(buf, MSG_SIZ, "name %s\n", gameInfo.white);
11723 SendToProgram(buf, &first);
11725 if (first.sendTime) {
11726 if (first.useColors) {
11727 SendToProgram("white\n", &first); /*gnu kludge*/
11729 SendTimeRemaining(&first, FALSE);
11731 if (first.useColors) {
11732 SendToProgram("black\n", &first); // [HGM] book: 'go' sent separately
11734 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: send go or retrieve book move
11735 SetMachineThinkingEnables();
11736 first.maybeThinking = TRUE;
11739 if (appData.autoFlipView && flipView) {
11740 flipView = !flipView;
11741 DrawPosition(FALSE, NULL);
11742 DisplayBothClocks(); // [HGM] logo: clocks might have to be exchanged;
11744 if(bookHit) { // [HGM] book: simulate book reply
11745 static char bookMove[MSG_SIZ]; // a bit generous?
11747 programStats.nodes = programStats.depth = programStats.time =
11748 programStats.score = programStats.got_only_move = 0;
11749 sprintf(programStats.movelist, "%s (xbook)", bookHit);
11751 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
11752 strcat(bookMove, bookHit);
11753 HandleMachineMove(bookMove, &first);
11759 DisplayTwoMachinesTitle()
11762 if (appData.matchGames > 0) {
11763 if (first.twoMachinesColor[0] == 'w') {
11764 snprintf(buf, MSG_SIZ, "%s vs. %s (%d-%d-%d)",
11765 gameInfo.white, gameInfo.black,
11766 first.matchWins, second.matchWins,
11767 matchGame - 1 - (first.matchWins + second.matchWins));
11769 snprintf(buf, MSG_SIZ, "%s vs. %s (%d-%d-%d)",
11770 gameInfo.white, gameInfo.black,
11771 second.matchWins, first.matchWins,
11772 matchGame - 1 - (first.matchWins + second.matchWins));
11775 snprintf(buf, MSG_SIZ, "%s vs. %s", gameInfo.white, gameInfo.black);
11781 TwoMachinesEvent P((void))
11785 ChessProgramState *onmove;
11786 char *bookHit = NULL;
11788 if (appData.noChessProgram) return;
11790 switch (gameMode) {
11791 case TwoMachinesPlay:
11793 case MachinePlaysWhite:
11794 case MachinePlaysBlack:
11795 if (WhiteOnMove(forwardMostMove) == (gameMode == MachinePlaysWhite)) {
11796 DisplayError(_("Wait until your turn,\nor select Move Now"), 0);
11800 case BeginningOfGame:
11801 case PlayFromGameFile:
11804 if (gameMode != EditGame) return;
11807 EditPositionDone(TRUE);
11818 // forwardMostMove = currentMove;
11819 TruncateGame(); // [HGM] vari: MachineWhite and MachineBlack do this...
11820 ResurrectChessProgram(); /* in case first program isn't running */
11822 if (second.pr == NULL) {
11823 StartChessProgram(&second);
11824 if (second.protocolVersion == 1) {
11825 TwoMachinesEventIfReady();
11827 /* kludge: allow timeout for initial "feature" command */
11829 DisplayMessage("", _("Starting second chess program"));
11830 ScheduleDelayedEvent(TwoMachinesEventIfReady, FEATURE_TIMEOUT);
11834 DisplayMessage("", "");
11835 InitChessProgram(&second, FALSE);
11836 SendToProgram("force\n", &second);
11837 if (startedFromSetupPosition) {
11838 SendBoard(&second, backwardMostMove);
11839 if (appData.debugMode) {
11840 fprintf(debugFP, "Two Machines\n");
11843 for (i = backwardMostMove; i < forwardMostMove; i++) {
11844 SendMoveToProgram(i, &second);
11847 gameMode = TwoMachinesPlay;
11851 DisplayTwoMachinesTitle();
11853 if ((first.twoMachinesColor[0] == 'w') == WhiteOnMove(forwardMostMove)) {
11859 SendToProgram(first.computerString, &first);
11860 if (first.sendName) {
11861 snprintf(buf, MSG_SIZ, "name %s\n", second.tidy);
11862 SendToProgram(buf, &first);
11864 SendToProgram(second.computerString, &second);
11865 if (second.sendName) {
11866 snprintf(buf, MSG_SIZ, "name %s\n", first.tidy);
11867 SendToProgram(buf, &second);
11871 if (!first.sendTime || !second.sendTime) {
11872 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
11873 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
11875 if (onmove->sendTime) {
11876 if (onmove->useColors) {
11877 SendToProgram(onmove->other->twoMachinesColor, onmove); /*gnu kludge*/
11879 SendTimeRemaining(onmove, WhiteOnMove(forwardMostMove));
11881 if (onmove->useColors) {
11882 SendToProgram(onmove->twoMachinesColor, onmove);
11884 bookHit = SendMoveToBookUser(forwardMostMove-1, onmove, TRUE); // [HGM] book: send go or retrieve book move
11885 // SendToProgram("go\n", onmove);
11886 onmove->maybeThinking = TRUE;
11887 SetMachineThinkingEnables();
11891 if(bookHit) { // [HGM] book: simulate book reply
11892 static char bookMove[MSG_SIZ]; // a bit generous?
11894 programStats.nodes = programStats.depth = programStats.time =
11895 programStats.score = programStats.got_only_move = 0;
11896 sprintf(programStats.movelist, "%s (xbook)", bookHit);
11898 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
11899 strcat(bookMove, bookHit);
11900 savedMessage = bookMove; // args for deferred call
11901 savedState = onmove;
11902 ScheduleDelayedEvent(DeferredBookMove, 1);
11909 if (gameMode == Training) {
11910 SetTrainingModeOff();
11911 gameMode = PlayFromGameFile;
11912 DisplayMessage("", _("Training mode off"));
11914 gameMode = Training;
11915 animateTraining = appData.animate;
11917 /* make sure we are not already at the end of the game */
11918 if (currentMove < forwardMostMove) {
11919 SetTrainingModeOn();
11920 DisplayMessage("", _("Training mode on"));
11922 gameMode = PlayFromGameFile;
11923 DisplayError(_("Already at end of game"), 0);
11932 if (!appData.icsActive) return;
11933 switch (gameMode) {
11934 case IcsPlayingWhite:
11935 case IcsPlayingBlack:
11938 case BeginningOfGame:
11946 EditPositionDone(TRUE);
11959 gameMode = IcsIdle;
11970 switch (gameMode) {
11972 SetTrainingModeOff();
11974 case MachinePlaysWhite:
11975 case MachinePlaysBlack:
11976 case BeginningOfGame:
11977 SendToProgram("force\n", &first);
11978 SetUserThinkingEnables();
11980 case PlayFromGameFile:
11981 (void) StopLoadGameTimer();
11982 if (gameFileFP != NULL) {
11987 EditPositionDone(TRUE);
11992 SendToProgram("force\n", &first);
11994 case TwoMachinesPlay:
11995 GameEnds(EndOfFile, NULL, GE_PLAYER);
11996 ResurrectChessProgram();
11997 SetUserThinkingEnables();
12000 ResurrectChessProgram();
12002 case IcsPlayingBlack:
12003 case IcsPlayingWhite:
12004 DisplayError(_("Warning: You are still playing a game"), 0);
12007 DisplayError(_("Warning: You are still observing a game"), 0);
12010 DisplayError(_("Warning: You are still examining a game"), 0);
12021 first.offeredDraw = second.offeredDraw = 0;
12023 if (gameMode == PlayFromGameFile) {
12024 whiteTimeRemaining = timeRemaining[0][currentMove];
12025 blackTimeRemaining = timeRemaining[1][currentMove];
12029 if (gameMode == MachinePlaysWhite ||
12030 gameMode == MachinePlaysBlack ||
12031 gameMode == TwoMachinesPlay ||
12032 gameMode == EndOfGame) {
12033 i = forwardMostMove;
12034 while (i > currentMove) {
12035 SendToProgram("undo\n", &first);
12038 whiteTimeRemaining = timeRemaining[0][currentMove];
12039 blackTimeRemaining = timeRemaining[1][currentMove];
12040 DisplayBothClocks();
12041 if (whiteFlag || blackFlag) {
12042 whiteFlag = blackFlag = 0;
12047 gameMode = EditGame;
12054 EditPositionEvent()
12056 if (gameMode == EditPosition) {
12062 if (gameMode != EditGame) return;
12064 gameMode = EditPosition;
12067 if (currentMove > 0)
12068 CopyBoard(boards[0], boards[currentMove]);
12070 blackPlaysFirst = !WhiteOnMove(currentMove);
12072 currentMove = forwardMostMove = backwardMostMove = 0;
12073 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
12080 /* [DM] icsEngineAnalyze - possible call from other functions */
12081 if (appData.icsEngineAnalyze) {
12082 appData.icsEngineAnalyze = FALSE;
12084 DisplayMessage("",_("Close ICS engine analyze..."));
12086 if (first.analysisSupport && first.analyzing) {
12087 SendToProgram("exit\n", &first);
12088 first.analyzing = FALSE;
12090 thinkOutput[0] = NULLCHAR;
12094 EditPositionDone(Boolean fakeRights)
12096 int king = gameInfo.variant == VariantKnightmate ? WhiteUnicorn : WhiteKing;
12098 startedFromSetupPosition = TRUE;
12099 InitChessProgram(&first, FALSE);
12100 if(fakeRights) { // [HGM] suppress this if we just pasted a FEN.
12101 boards[0][EP_STATUS] = EP_NONE;
12102 boards[0][CASTLING][2] = boards[0][CASTLING][5] = BOARD_WIDTH>>1;
12103 if(boards[0][0][BOARD_WIDTH>>1] == king) {
12104 boards[0][CASTLING][1] = boards[0][0][BOARD_LEFT] == WhiteRook ? 0 : NoRights;
12105 boards[0][CASTLING][0] = boards[0][0][BOARD_RGHT-1] == WhiteRook ? BOARD_RGHT-1 : NoRights;
12106 } else boards[0][CASTLING][2] = NoRights;
12107 if(boards[0][BOARD_HEIGHT-1][BOARD_WIDTH>>1] == WHITE_TO_BLACK king) {
12108 boards[0][CASTLING][4] = boards[0][BOARD_HEIGHT-1][BOARD_LEFT] == BlackRook ? 0 : NoRights;
12109 boards[0][CASTLING][3] = boards[0][BOARD_HEIGHT-1][BOARD_RGHT-1] == BlackRook ? BOARD_RGHT-1 : NoRights;
12110 } else boards[0][CASTLING][5] = NoRights;
12112 SendToProgram("force\n", &first);
12113 if (blackPlaysFirst) {
12114 safeStrCpy(moveList[0], "", sizeof(moveList[0])/sizeof(moveList[0][0]));
12115 safeStrCpy(parseList[0], "", sizeof(parseList[0])/sizeof(parseList[0][0]));
12116 currentMove = forwardMostMove = backwardMostMove = 1;
12117 CopyBoard(boards[1], boards[0]);
12119 currentMove = forwardMostMove = backwardMostMove = 0;
12121 SendBoard(&first, forwardMostMove);
12122 if (appData.debugMode) {
12123 fprintf(debugFP, "EditPosDone\n");
12126 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
12127 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
12128 gameMode = EditGame;
12130 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
12131 ClearHighlights(); /* [AS] */
12134 /* Pause for `ms' milliseconds */
12135 /* !! Ugh, this is a kludge. Fix it sometime. --tpm */
12145 } while (SubtractTimeMarks(&m2, &m1) < ms);
12148 /* !! Ugh, this is a kludge. Fix it sometime. --tpm */
12150 SendMultiLineToICS(buf)
12153 char temp[MSG_SIZ+1], *p;
12160 strncpy(temp, buf, len);
12165 if (*p == '\n' || *p == '\r')
12170 strcat(temp, "\n");
12172 SendToPlayer(temp, strlen(temp));
12176 SetWhiteToPlayEvent()
12178 if (gameMode == EditPosition) {
12179 blackPlaysFirst = FALSE;
12180 DisplayBothClocks(); /* works because currentMove is 0 */
12181 } else if (gameMode == IcsExamining) {
12182 SendToICS(ics_prefix);
12183 SendToICS("tomove white\n");
12188 SetBlackToPlayEvent()
12190 if (gameMode == EditPosition) {
12191 blackPlaysFirst = TRUE;
12192 currentMove = 1; /* kludge */
12193 DisplayBothClocks();
12195 } else if (gameMode == IcsExamining) {
12196 SendToICS(ics_prefix);
12197 SendToICS("tomove black\n");
12202 EditPositionMenuEvent(selection, x, y)
12203 ChessSquare selection;
12207 ChessSquare piece = boards[0][y][x];
12209 if (gameMode != EditPosition && gameMode != IcsExamining) return;
12211 switch (selection) {
12213 if (gameMode == IcsExamining && ics_type == ICS_FICS) {
12214 SendToICS(ics_prefix);
12215 SendToICS("bsetup clear\n");
12216 } else if (gameMode == IcsExamining && ics_type == ICS_ICC) {
12217 SendToICS(ics_prefix);
12218 SendToICS("clearboard\n");
12220 for (x = 0; x < BOARD_WIDTH; x++) { ChessSquare p = EmptySquare;
12221 if(x == BOARD_LEFT-1 || x == BOARD_RGHT) p = (ChessSquare) 0; /* [HGM] holdings */
12222 for (y = 0; y < BOARD_HEIGHT; y++) {
12223 if (gameMode == IcsExamining) {
12224 if (boards[currentMove][y][x] != EmptySquare) {
12225 snprintf(buf, MSG_SIZ, "%sx@%c%c\n", ics_prefix,
12230 boards[0][y][x] = p;
12235 if (gameMode == EditPosition) {
12236 DrawPosition(FALSE, boards[0]);
12241 SetWhiteToPlayEvent();
12245 SetBlackToPlayEvent();
12249 if (gameMode == IcsExamining) {
12250 if (x < BOARD_LEFT || x >= BOARD_RGHT) break; // [HGM] holdings
12251 snprintf(buf, MSG_SIZ, "%sx@%c%c\n", ics_prefix, AAA + x, ONE + y);
12254 if(x < BOARD_LEFT || x >= BOARD_RGHT) {
12255 if(x == BOARD_LEFT-2) {
12256 if(y < BOARD_HEIGHT-1-gameInfo.holdingsSize) break;
12257 boards[0][y][1] = 0;
12259 if(x == BOARD_RGHT+1) {
12260 if(y >= gameInfo.holdingsSize) break;
12261 boards[0][y][BOARD_WIDTH-2] = 0;
12264 boards[0][y][x] = EmptySquare;
12265 DrawPosition(FALSE, boards[0]);
12270 if(piece >= (int)WhitePawn && piece < (int)WhiteMan ||
12271 piece >= (int)BlackPawn && piece < (int)BlackMan ) {
12272 selection = (ChessSquare) (PROMOTED piece);
12273 } else if(piece == EmptySquare) selection = WhiteSilver;
12274 else selection = (ChessSquare)((int)piece - 1);
12278 if(piece > (int)WhiteMan && piece <= (int)WhiteKing ||
12279 piece > (int)BlackMan && piece <= (int)BlackKing ) {
12280 selection = (ChessSquare) (DEMOTED piece);
12281 } else if(piece == EmptySquare) selection = BlackSilver;
12282 else selection = (ChessSquare)((int)piece + 1);
12287 if(gameInfo.variant == VariantShatranj ||
12288 gameInfo.variant == VariantXiangqi ||
12289 gameInfo.variant == VariantCourier ||
12290 gameInfo.variant == VariantMakruk )
12291 selection = (ChessSquare)((int)selection - (int)WhiteQueen + (int)WhiteFerz);
12296 if(gameInfo.variant == VariantXiangqi)
12297 selection = (ChessSquare)((int)selection - (int)WhiteKing + (int)WhiteWazir);
12298 if(gameInfo.variant == VariantKnightmate)
12299 selection = (ChessSquare)((int)selection - (int)WhiteKing + (int)WhiteUnicorn);
12302 if (gameMode == IcsExamining) {
12303 if (x < BOARD_LEFT || x >= BOARD_RGHT) break; // [HGM] holdings
12304 snprintf(buf, MSG_SIZ, "%s%c@%c%c\n", ics_prefix,
12305 PieceToChar(selection), AAA + x, ONE + y);
12308 if(x < BOARD_LEFT || x >= BOARD_RGHT) {
12310 if(x == BOARD_LEFT-2 && selection >= BlackPawn) {
12311 n = PieceToNumber(selection - BlackPawn);
12312 if(n >= gameInfo.holdingsSize) { n = 0; selection = BlackPawn; }
12313 boards[0][BOARD_HEIGHT-1-n][0] = selection;
12314 boards[0][BOARD_HEIGHT-1-n][1]++;
12316 if(x == BOARD_RGHT+1 && selection < BlackPawn) {
12317 n = PieceToNumber(selection);
12318 if(n >= gameInfo.holdingsSize) { n = 0; selection = WhitePawn; }
12319 boards[0][n][BOARD_WIDTH-1] = selection;
12320 boards[0][n][BOARD_WIDTH-2]++;
12323 boards[0][y][x] = selection;
12324 DrawPosition(TRUE, boards[0]);
12332 DropMenuEvent(selection, x, y)
12333 ChessSquare selection;
12336 ChessMove moveType;
12338 switch (gameMode) {
12339 case IcsPlayingWhite:
12340 case MachinePlaysBlack:
12341 if (!WhiteOnMove(currentMove)) {
12342 DisplayMoveError(_("It is Black's turn"));
12345 moveType = WhiteDrop;
12347 case IcsPlayingBlack:
12348 case MachinePlaysWhite:
12349 if (WhiteOnMove(currentMove)) {
12350 DisplayMoveError(_("It is White's turn"));
12353 moveType = BlackDrop;
12356 moveType = WhiteOnMove(currentMove) ? WhiteDrop : BlackDrop;
12362 if (moveType == BlackDrop && selection < BlackPawn) {
12363 selection = (ChessSquare) ((int) selection
12364 + (int) BlackPawn - (int) WhitePawn);
12366 if (boards[currentMove][y][x] != EmptySquare) {
12367 DisplayMoveError(_("That square is occupied"));
12371 FinishMove(moveType, (int) selection, DROP_RANK, x, y, NULLCHAR);
12377 /* Accept a pending offer of any kind from opponent */
12379 if (appData.icsActive) {
12380 SendToICS(ics_prefix);
12381 SendToICS("accept\n");
12382 } else if (cmailMsgLoaded) {
12383 if (currentMove == cmailOldMove &&
12384 commentList[cmailOldMove] != NULL &&
12385 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
12386 "Black offers a draw" : "White offers a draw")) {
12388 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
12389 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_ACCEPT;
12391 DisplayError(_("There is no pending offer on this move"), 0);
12392 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
12395 /* Not used for offers from chess program */
12402 /* Decline a pending offer of any kind from opponent */
12404 if (appData.icsActive) {
12405 SendToICS(ics_prefix);
12406 SendToICS("decline\n");
12407 } else if (cmailMsgLoaded) {
12408 if (currentMove == cmailOldMove &&
12409 commentList[cmailOldMove] != NULL &&
12410 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
12411 "Black offers a draw" : "White offers a draw")) {
12413 AppendComment(cmailOldMove, "Draw declined", TRUE);
12414 DisplayComment(cmailOldMove - 1, "Draw declined");
12417 DisplayError(_("There is no pending offer on this move"), 0);
12420 /* Not used for offers from chess program */
12427 /* Issue ICS rematch command */
12428 if (appData.icsActive) {
12429 SendToICS(ics_prefix);
12430 SendToICS("rematch\n");
12437 /* Call your opponent's flag (claim a win on time) */
12438 if (appData.icsActive) {
12439 SendToICS(ics_prefix);
12440 SendToICS("flag\n");
12442 switch (gameMode) {
12445 case MachinePlaysWhite:
12448 GameEnds(GameIsDrawn, "Both players ran out of time",
12451 GameEnds(BlackWins, "Black wins on time", GE_PLAYER);
12453 DisplayError(_("Your opponent is not out of time"), 0);
12456 case MachinePlaysBlack:
12459 GameEnds(GameIsDrawn, "Both players ran out of time",
12462 GameEnds(WhiteWins, "White wins on time", GE_PLAYER);
12464 DisplayError(_("Your opponent is not out of time"), 0);
12474 /* Offer draw or accept pending draw offer from opponent */
12476 if (appData.icsActive) {
12477 /* Note: tournament rules require draw offers to be
12478 made after you make your move but before you punch
12479 your clock. Currently ICS doesn't let you do that;
12480 instead, you immediately punch your clock after making
12481 a move, but you can offer a draw at any time. */
12483 SendToICS(ics_prefix);
12484 SendToICS("draw\n");
12485 userOfferedDraw = TRUE; // [HGM] drawclaim: also set flag in ICS play
12486 } else if (cmailMsgLoaded) {
12487 if (currentMove == cmailOldMove &&
12488 commentList[cmailOldMove] != NULL &&
12489 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
12490 "Black offers a draw" : "White offers a draw")) {
12491 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
12492 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_ACCEPT;
12493 } else if (currentMove == cmailOldMove + 1) {
12494 char *offer = WhiteOnMove(cmailOldMove) ?
12495 "White offers a draw" : "Black offers a draw";
12496 AppendComment(currentMove, offer, TRUE);
12497 DisplayComment(currentMove - 1, offer);
12498 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_DRAW;
12500 DisplayError(_("You must make your move before offering a draw"), 0);
12501 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
12503 } else if (first.offeredDraw) {
12504 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
12506 if (first.sendDrawOffers) {
12507 SendToProgram("draw\n", &first);
12508 userOfferedDraw = TRUE;
12516 /* Offer Adjourn or accept pending Adjourn offer from opponent */
12518 if (appData.icsActive) {
12519 SendToICS(ics_prefix);
12520 SendToICS("adjourn\n");
12522 /* Currently GNU Chess doesn't offer or accept Adjourns */
12530 /* Offer Abort or accept pending Abort offer from opponent */
12532 if (appData.icsActive) {
12533 SendToICS(ics_prefix);
12534 SendToICS("abort\n");
12536 GameEnds(GameUnfinished, "Game aborted", GE_PLAYER);
12543 /* Resign. You can do this even if it's not your turn. */
12545 if (appData.icsActive) {
12546 SendToICS(ics_prefix);
12547 SendToICS("resign\n");
12549 switch (gameMode) {
12550 case MachinePlaysWhite:
12551 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
12553 case MachinePlaysBlack:
12554 GameEnds(BlackWins, "White resigns", GE_PLAYER);
12557 if (cmailMsgLoaded) {
12559 if (WhiteOnMove(cmailOldMove)) {
12560 GameEnds(BlackWins, "White resigns", GE_PLAYER);
12562 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
12564 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_RESIGN;
12575 StopObservingEvent()
12577 /* Stop observing current games */
12578 SendToICS(ics_prefix);
12579 SendToICS("unobserve\n");
12583 StopExaminingEvent()
12585 /* Stop observing current game */
12586 SendToICS(ics_prefix);
12587 SendToICS("unexamine\n");
12591 ForwardInner(target)
12596 if (appData.debugMode)
12597 fprintf(debugFP, "ForwardInner(%d), current %d, forward %d\n",
12598 target, currentMove, forwardMostMove);
12600 if (gameMode == EditPosition)
12603 if (gameMode == PlayFromGameFile && !pausing)
12606 if (gameMode == IcsExamining && pausing)
12607 limit = pauseExamForwardMostMove;
12609 limit = forwardMostMove;
12611 if (target > limit) target = limit;
12613 if (target > 0 && moveList[target - 1][0]) {
12614 int fromX, fromY, toX, toY;
12615 toX = moveList[target - 1][2] - AAA;
12616 toY = moveList[target - 1][3] - ONE;
12617 if (moveList[target - 1][1] == '@') {
12618 if (appData.highlightLastMove) {
12619 SetHighlights(-1, -1, toX, toY);
12622 fromX = moveList[target - 1][0] - AAA;
12623 fromY = moveList[target - 1][1] - ONE;
12624 if (target == currentMove + 1) {
12625 AnimateMove(boards[currentMove], fromX, fromY, toX, toY);
12627 if (appData.highlightLastMove) {
12628 SetHighlights(fromX, fromY, toX, toY);
12632 if (gameMode == EditGame || gameMode == AnalyzeMode ||
12633 gameMode == Training || gameMode == PlayFromGameFile ||
12634 gameMode == AnalyzeFile) {
12635 while (currentMove < target) {
12636 SendMoveToProgram(currentMove++, &first);
12639 currentMove = target;
12642 if (gameMode == EditGame || gameMode == EndOfGame) {
12643 whiteTimeRemaining = timeRemaining[0][currentMove];
12644 blackTimeRemaining = timeRemaining[1][currentMove];
12646 DisplayBothClocks();
12647 DisplayMove(currentMove - 1);
12648 DrawPosition(FALSE, boards[currentMove]);
12649 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
12650 if ( !matchMode && gameMode != Training) { // [HGM] PV info: routine tests if empty
12651 DisplayComment(currentMove - 1, commentList[currentMove]);
12659 if (gameMode == IcsExamining && !pausing) {
12660 SendToICS(ics_prefix);
12661 SendToICS("forward\n");
12663 ForwardInner(currentMove + 1);
12670 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
12671 /* to optimze, we temporarily turn off analysis mode while we feed
12672 * the remaining moves to the engine. Otherwise we get analysis output
12675 if (first.analysisSupport) {
12676 SendToProgram("exit\nforce\n", &first);
12677 first.analyzing = FALSE;
12681 if (gameMode == IcsExamining && !pausing) {
12682 SendToICS(ics_prefix);
12683 SendToICS("forward 999999\n");
12685 ForwardInner(forwardMostMove);
12688 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
12689 /* we have fed all the moves, so reactivate analysis mode */
12690 SendToProgram("analyze\n", &first);
12691 first.analyzing = TRUE;
12692 /*first.maybeThinking = TRUE;*/
12693 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
12698 BackwardInner(target)
12701 int full_redraw = TRUE; /* [AS] Was FALSE, had to change it! */
12703 if (appData.debugMode)
12704 fprintf(debugFP, "BackwardInner(%d), current %d, forward %d\n",
12705 target, currentMove, forwardMostMove);
12707 if (gameMode == EditPosition) return;
12708 if (currentMove <= backwardMostMove) {
12710 DrawPosition(full_redraw, boards[currentMove]);
12713 if (gameMode == PlayFromGameFile && !pausing)
12716 if (moveList[target][0]) {
12717 int fromX, fromY, toX, toY;
12718 toX = moveList[target][2] - AAA;
12719 toY = moveList[target][3] - ONE;
12720 if (moveList[target][1] == '@') {
12721 if (appData.highlightLastMove) {
12722 SetHighlights(-1, -1, toX, toY);
12725 fromX = moveList[target][0] - AAA;
12726 fromY = moveList[target][1] - ONE;
12727 if (target == currentMove - 1) {
12728 AnimateMove(boards[currentMove], toX, toY, fromX, fromY);
12730 if (appData.highlightLastMove) {
12731 SetHighlights(fromX, fromY, toX, toY);
12735 if (gameMode == EditGame || gameMode==AnalyzeMode ||
12736 gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
12737 while (currentMove > target) {
12738 SendToProgram("undo\n", &first);
12742 currentMove = target;
12745 if (gameMode == EditGame || gameMode == EndOfGame) {
12746 whiteTimeRemaining = timeRemaining[0][currentMove];
12747 blackTimeRemaining = timeRemaining[1][currentMove];
12749 DisplayBothClocks();
12750 DisplayMove(currentMove - 1);
12751 DrawPosition(full_redraw, boards[currentMove]);
12752 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
12753 // [HGM] PV info: routine tests if comment empty
12754 DisplayComment(currentMove - 1, commentList[currentMove]);
12760 if (gameMode == IcsExamining && !pausing) {
12761 SendToICS(ics_prefix);
12762 SendToICS("backward\n");
12764 BackwardInner(currentMove - 1);
12771 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
12772 /* to optimize, we temporarily turn off analysis mode while we undo
12773 * all the moves. Otherwise we get analysis output after each undo.
12775 if (first.analysisSupport) {
12776 SendToProgram("exit\nforce\n", &first);
12777 first.analyzing = FALSE;
12781 if (gameMode == IcsExamining && !pausing) {
12782 SendToICS(ics_prefix);
12783 SendToICS("backward 999999\n");
12785 BackwardInner(backwardMostMove);
12788 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
12789 /* we have fed all the moves, so reactivate analysis mode */
12790 SendToProgram("analyze\n", &first);
12791 first.analyzing = TRUE;
12792 /*first.maybeThinking = TRUE;*/
12793 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
12800 if (gameMode == PlayFromGameFile && !pausing) PauseEvent();
12801 if (to >= forwardMostMove) to = forwardMostMove;
12802 if (to <= backwardMostMove) to = backwardMostMove;
12803 if (to < currentMove) {
12811 RevertEvent(Boolean annotate)
12813 if(PopTail(annotate)) { // [HGM] vari: restore old game tail
12816 if (gameMode != IcsExamining) {
12817 DisplayError(_("You are not examining a game"), 0);
12821 DisplayError(_("You can't revert while pausing"), 0);
12824 SendToICS(ics_prefix);
12825 SendToICS("revert\n");
12831 switch (gameMode) {
12832 case MachinePlaysWhite:
12833 case MachinePlaysBlack:
12834 if (WhiteOnMove(forwardMostMove) == (gameMode == MachinePlaysWhite)) {
12835 DisplayError(_("Wait until your turn,\nor select Move Now"), 0);
12838 if (forwardMostMove < 2) return;
12839 currentMove = forwardMostMove = forwardMostMove - 2;
12840 whiteTimeRemaining = timeRemaining[0][currentMove];
12841 blackTimeRemaining = timeRemaining[1][currentMove];
12842 DisplayBothClocks();
12843 DisplayMove(currentMove - 1);
12844 ClearHighlights();/*!! could figure this out*/
12845 DrawPosition(TRUE, boards[currentMove]); /* [AS] Changed to full redraw! */
12846 SendToProgram("remove\n", &first);
12847 /*first.maybeThinking = TRUE;*/ /* GNU Chess does not ponder here */
12850 case BeginningOfGame:
12854 case IcsPlayingWhite:
12855 case IcsPlayingBlack:
12856 if (WhiteOnMove(forwardMostMove) == (gameMode == IcsPlayingWhite)) {
12857 SendToICS(ics_prefix);
12858 SendToICS("takeback 2\n");
12860 SendToICS(ics_prefix);
12861 SendToICS("takeback 1\n");
12870 ChessProgramState *cps;
12872 switch (gameMode) {
12873 case MachinePlaysWhite:
12874 if (!WhiteOnMove(forwardMostMove)) {
12875 DisplayError(_("It is your turn"), 0);
12880 case MachinePlaysBlack:
12881 if (WhiteOnMove(forwardMostMove)) {
12882 DisplayError(_("It is your turn"), 0);
12887 case TwoMachinesPlay:
12888 if (WhiteOnMove(forwardMostMove) ==
12889 (first.twoMachinesColor[0] == 'w')) {
12895 case BeginningOfGame:
12899 SendToProgram("?\n", cps);
12903 TruncateGameEvent()
12906 if (gameMode != EditGame) return;
12913 CleanupTail(); // [HGM] vari: only keep current variation if we explicitly truncate
12914 if (forwardMostMove > currentMove) {
12915 if (gameInfo.resultDetails != NULL) {
12916 free(gameInfo.resultDetails);
12917 gameInfo.resultDetails = NULL;
12918 gameInfo.result = GameUnfinished;
12920 forwardMostMove = currentMove;
12921 HistorySet(parseList, backwardMostMove, forwardMostMove,
12929 if (appData.noChessProgram) return;
12930 switch (gameMode) {
12931 case MachinePlaysWhite:
12932 if (WhiteOnMove(forwardMostMove)) {
12933 DisplayError(_("Wait until your turn"), 0);
12937 case BeginningOfGame:
12938 case MachinePlaysBlack:
12939 if (!WhiteOnMove(forwardMostMove)) {
12940 DisplayError(_("Wait until your turn"), 0);
12945 DisplayError(_("No hint available"), 0);
12948 SendToProgram("hint\n", &first);
12949 hintRequested = TRUE;
12955 if (appData.noChessProgram) return;
12956 switch (gameMode) {
12957 case MachinePlaysWhite:
12958 if (WhiteOnMove(forwardMostMove)) {
12959 DisplayError(_("Wait until your turn"), 0);
12963 case BeginningOfGame:
12964 case MachinePlaysBlack:
12965 if (!WhiteOnMove(forwardMostMove)) {
12966 DisplayError(_("Wait until your turn"), 0);
12971 EditPositionDone(TRUE);
12973 case TwoMachinesPlay:
12978 SendToProgram("bk\n", &first);
12979 bookOutput[0] = NULLCHAR;
12980 bookRequested = TRUE;
12986 char *tags = PGNTags(&gameInfo);
12987 TagsPopUp(tags, CmailMsg());
12991 /* end button procedures */
12994 PrintPosition(fp, move)
13000 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
13001 for (j = BOARD_LEFT; j < BOARD_RGHT; j++) {
13002 char c = PieceToChar(boards[move][i][j]);
13003 fputc(c == 'x' ? '.' : c, fp);
13004 fputc(j == BOARD_RGHT - 1 ? '\n' : ' ', fp);
13007 if ((gameMode == EditPosition) ? !blackPlaysFirst : (move % 2 == 0))
13008 fprintf(fp, "white to play\n");
13010 fprintf(fp, "black to play\n");
13017 if (gameInfo.white != NULL) {
13018 fprintf(fp, "\t%s vs. %s\n", gameInfo.white, gameInfo.black);
13024 /* Find last component of program's own name, using some heuristics */
13026 TidyProgramName(prog, host, buf)
13027 char *prog, *host, buf[MSG_SIZ];
13030 int local = (strcmp(host, "localhost") == 0);
13031 while (!local && (p = strchr(prog, ';')) != NULL) {
13033 while (*p == ' ') p++;
13036 if (*prog == '"' || *prog == '\'') {
13037 q = strchr(prog + 1, *prog);
13039 q = strchr(prog, ' ');
13041 if (q == NULL) q = prog + strlen(prog);
13043 while (p >= prog && *p != '/' && *p != '\\') p--;
13045 if(p == prog && *p == '"') p++;
13046 if (q - p >= 4 && StrCaseCmp(q - 4, ".exe") == 0) q -= 4;
13047 memcpy(buf, p, q - p);
13048 buf[q - p] = NULLCHAR;
13056 TimeControlTagValue()
13059 if (!appData.clockMode) {
13060 safeStrCpy(buf, "-", sizeof(buf)/sizeof(buf[0]));
13061 } else if (movesPerSession > 0) {
13062 snprintf(buf, MSG_SIZ, "%d/%ld", movesPerSession, timeControl/1000);
13063 } else if (timeIncrement == 0) {
13064 snprintf(buf, MSG_SIZ, "%ld", timeControl/1000);
13066 snprintf(buf, MSG_SIZ, "%ld+%ld", timeControl/1000, timeIncrement/1000);
13068 return StrSave(buf);
13074 /* This routine is used only for certain modes */
13075 VariantClass v = gameInfo.variant;
13076 ChessMove r = GameUnfinished;
13079 if(gameMode == EditGame) { // [HGM] vari: do not erase result on EditGame
13080 r = gameInfo.result;
13081 p = gameInfo.resultDetails;
13082 gameInfo.resultDetails = NULL;
13084 ClearGameInfo(&gameInfo);
13085 gameInfo.variant = v;
13087 switch (gameMode) {
13088 case MachinePlaysWhite:
13089 gameInfo.event = StrSave( appData.pgnEventHeader );
13090 gameInfo.site = StrSave(HostName());
13091 gameInfo.date = PGNDate();
13092 gameInfo.round = StrSave("-");
13093 gameInfo.white = StrSave(first.tidy);
13094 gameInfo.black = StrSave(UserName());
13095 gameInfo.timeControl = TimeControlTagValue();
13098 case MachinePlaysBlack:
13099 gameInfo.event = StrSave( appData.pgnEventHeader );
13100 gameInfo.site = StrSave(HostName());
13101 gameInfo.date = PGNDate();
13102 gameInfo.round = StrSave("-");
13103 gameInfo.white = StrSave(UserName());
13104 gameInfo.black = StrSave(first.tidy);
13105 gameInfo.timeControl = TimeControlTagValue();
13108 case TwoMachinesPlay:
13109 gameInfo.event = StrSave( appData.pgnEventHeader );
13110 gameInfo.site = StrSave(HostName());
13111 gameInfo.date = PGNDate();
13112 if (matchGame > 0) {
13114 snprintf(buf, MSG_SIZ, "%d", matchGame);
13115 gameInfo.round = StrSave(buf);
13117 gameInfo.round = StrSave("-");
13119 if (first.twoMachinesColor[0] == 'w') {
13120 gameInfo.white = StrSave(first.tidy);
13121 gameInfo.black = StrSave(second.tidy);
13123 gameInfo.white = StrSave(second.tidy);
13124 gameInfo.black = StrSave(first.tidy);
13126 gameInfo.timeControl = TimeControlTagValue();
13130 gameInfo.event = StrSave("Edited game");
13131 gameInfo.site = StrSave(HostName());
13132 gameInfo.date = PGNDate();
13133 gameInfo.round = StrSave("-");
13134 gameInfo.white = StrSave("-");
13135 gameInfo.black = StrSave("-");
13136 gameInfo.result = r;
13137 gameInfo.resultDetails = p;
13141 gameInfo.event = StrSave("Edited position");
13142 gameInfo.site = StrSave(HostName());
13143 gameInfo.date = PGNDate();
13144 gameInfo.round = StrSave("-");
13145 gameInfo.white = StrSave("-");
13146 gameInfo.black = StrSave("-");
13149 case IcsPlayingWhite:
13150 case IcsPlayingBlack:
13155 case PlayFromGameFile:
13156 gameInfo.event = StrSave("Game from non-PGN file");
13157 gameInfo.site = StrSave(HostName());
13158 gameInfo.date = PGNDate();
13159 gameInfo.round = StrSave("-");
13160 gameInfo.white = StrSave("?");
13161 gameInfo.black = StrSave("?");
13170 ReplaceComment(index, text)
13176 while (*text == '\n') text++;
13177 len = strlen(text);
13178 while (len > 0 && text[len - 1] == '\n') len--;
13180 if (commentList[index] != NULL)
13181 free(commentList[index]);
13184 commentList[index] = NULL;
13187 if( *text == '{' && strchr(text, '}') || // [HGM] braces: if certainy malformed, put braces
13188 *text == '[' && strchr(text, ']') || // otherwise hope the user knows what he is doing
13189 *text == '(' && strchr(text, ')')) { // (perhaps check if this parses as comment-only?)
13190 commentList[index] = (char *) malloc(len + 2);
13191 strncpy(commentList[index], text, len);
13192 commentList[index][len] = '\n';
13193 commentList[index][len + 1] = NULLCHAR;
13195 // [HGM] braces: if text does not start with known OK delimiter, put braces around it.
13197 commentList[index] = (char *) malloc(len + 7);
13198 safeStrCpy(commentList[index], "{\n", 3);
13199 safeStrCpy(commentList[index]+2, text, len+1);
13200 commentList[index][len+2] = NULLCHAR;
13201 while(p = strchr(commentList[index], '}')) *p = ')'; // kill all } to make it one comment
13202 strcat(commentList[index], "\n}\n");
13216 if (ch == '\r') continue;
13218 } while (ch != '\0');
13222 AppendComment(index, text, addBraces)
13225 Boolean addBraces; // [HGM] braces: tells if we should add {}
13230 if(appData.debugMode) fprintf(debugFP, "Append: in='%s' %d\n", text, addBraces); fflush(debugFP);
13231 text = GetInfoFromComment( index, text ); /* [HGM] PV time: strip PV info from comment */
13234 while (*text == '\n') text++;
13235 len = strlen(text);
13236 while (len > 0 && text[len - 1] == '\n') len--;
13238 if (len == 0) return;
13240 if (commentList[index] != NULL) {
13241 old = commentList[index];
13242 oldlen = strlen(old);
13243 while(commentList[index][oldlen-1] == '\n')
13244 commentList[index][--oldlen] = NULLCHAR;
13245 commentList[index] = (char *) malloc(oldlen + len + 6); // might waste 4
13246 safeStrCpy(commentList[index], old, oldlen);
13248 // [HGM] braces: join "{A\n}\n" + "{\nB}" as "{A\nB\n}"
13249 if(commentList[index][oldlen-1] == '}' && (text[0] == '{' || addBraces)) {
13250 if(addBraces) addBraces = FALSE; else { text++; len--; }
13251 while (*text == '\n') { text++; len--; }
13252 commentList[index][--oldlen] = NULLCHAR;
13254 if(addBraces) strcat(commentList[index], "\n{\n");
13255 else strcat(commentList[index], "\n");
13256 strcat(commentList[index], text);
13257 if(addBraces) strcat(commentList[index], "\n}\n");
13258 else strcat(commentList[index], "\n");
13260 commentList[index] = (char *) malloc(len + 6); // perhaps wastes 4...
13262 safeStrCpy(commentList[index], "{\n", sizeof(commentList[index])/sizeof(commentList[index][0]));
13263 else commentList[index][0] = NULLCHAR;
13264 strcat(commentList[index], text);
13265 strcat(commentList[index], "\n");
13266 if(addBraces) strcat(commentList[index], "}\n");
13270 static char * FindStr( char * text, char * sub_text )
13272 char * result = strstr( text, sub_text );
13274 if( result != NULL ) {
13275 result += strlen( sub_text );
13281 /* [AS] Try to extract PV info from PGN comment */
13282 /* [HGM] PV time: and then remove it, to prevent it appearing twice */
13283 char *GetInfoFromComment( int index, char * text )
13287 if( text != NULL && index > 0 ) {
13290 int time = -1, sec = 0, deci;
13291 char * s_eval = FindStr( text, "[%eval " );
13292 char * s_emt = FindStr( text, "[%emt " );
13294 if( s_eval != NULL || s_emt != NULL ) {
13298 if( s_eval != NULL ) {
13299 if( sscanf( s_eval, "%d,%d%c", &score, &depth, &delim ) != 3 ) {
13303 if( delim != ']' ) {
13308 if( s_emt != NULL ) {
13313 /* We expect something like: [+|-]nnn.nn/dd */
13316 if(*text != '{') return text; // [HGM] braces: must be normal comment
13318 sep = strchr( text, '/' );
13319 if( sep == NULL || sep < (text+4) ) {
13323 time = -1; sec = -1; deci = -1;
13324 if( sscanf( text+1, "%d.%d/%d %d:%d", &score, &score_lo, &depth, &time, &sec ) != 5 &&
13325 sscanf( text+1, "%d.%d/%d %d.%d", &score, &score_lo, &depth, &time, &deci ) != 5 &&
13326 sscanf( text+1, "%d.%d/%d %d", &score, &score_lo, &depth, &time ) != 4 &&
13327 sscanf( text+1, "%d.%d/%d", &score, &score_lo, &depth ) != 3 ) {
13331 if( score_lo < 0 || score_lo >= 100 ) {
13335 if(sec >= 0) time = 600*time + 10*sec; else
13336 if(deci >= 0) time = 10*time + deci; else time *= 10; // deci-sec
13338 score = score >= 0 ? score*100 + score_lo : score*100 - score_lo;
13340 /* [HGM] PV time: now locate end of PV info */
13341 while( *++sep >= '0' && *sep <= '9'); // strip depth
13343 while( *++sep >= '0' && *sep <= '9'); // strip time
13345 while( *++sep >= '0' && *sep <= '9'); // strip seconds
13347 while( *++sep >= '0' && *sep <= '9'); // strip fractional seconds
13348 while(*sep == ' ') sep++;
13359 pvInfoList[index-1].depth = depth;
13360 pvInfoList[index-1].score = score;
13361 pvInfoList[index-1].time = 10*time; // centi-sec
13362 if(*sep == '}') *sep = 0; else *--sep = '{';
13368 SendToProgram(message, cps)
13370 ChessProgramState *cps;
13372 int count, outCount, error;
13375 if (cps->pr == NULL) return;
13378 if (appData.debugMode) {
13381 fprintf(debugFP, "%ld >%-6s: %s",
13382 SubtractTimeMarks(&now, &programStartTime),
13383 cps->which, message);
13386 count = strlen(message);
13387 outCount = OutputToProcess(cps->pr, message, count, &error);
13388 if (outCount < count && !exiting
13389 && !endingGame) { /* [HGM] crash: to not hang GameEnds() writing to deceased engines */
13390 snprintf(buf, MSG_SIZ, _("Error writing to %s chess program"), cps->which);
13391 if(gameInfo.resultDetails==NULL) { /* [HGM] crash: if game in progress, give reason for abort */
13392 if((signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
13393 gameInfo.result = GameIsDrawn; /* [HGM] accept exit as draw claim */
13394 snprintf(buf, MSG_SIZ, "%s program exits in draw position (%s)", cps->which, cps->program);
13396 gameInfo.result = cps->twoMachinesColor[0]=='w' ? BlackWins : WhiteWins;
13398 gameInfo.resultDetails = StrSave(buf);
13400 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, error, 1); else errorExitStatus = 1;
13405 ReceiveFromProgram(isr, closure, message, count, error)
13406 InputSourceRef isr;
13414 ChessProgramState *cps = (ChessProgramState *)closure;
13416 if (isr != cps->isr) return; /* Killed intentionally */
13419 snprintf(buf, MSG_SIZ, _("Error: %s chess program (%s) exited unexpectedly"),
13420 cps->which, cps->program);
13421 if(gameInfo.resultDetails==NULL) { /* [HGM] crash: if game in progress, give reason for abort */
13422 if((signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
13423 gameInfo.result = GameIsDrawn; /* [HGM] accept exit as draw claim */
13424 snprintf(buf, MSG_SIZ, _("%s program exits in draw position (%s)"), cps->which, cps->program);
13426 gameInfo.result = cps->twoMachinesColor[0]=='w' ? BlackWins : WhiteWins;
13428 gameInfo.resultDetails = StrSave(buf);
13430 RemoveInputSource(cps->isr);
13431 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, 0, 1); else errorExitStatus = 1;
13433 snprintf(buf, MSG_SIZ, _("Error reading from %s chess program (%s)"),
13434 cps->which, cps->program);
13435 RemoveInputSource(cps->isr);
13437 /* [AS] Program is misbehaving badly... kill it */
13438 if( count == -2 ) {
13439 DestroyChildProcess( cps->pr, 9 );
13443 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, error, 1); else errorExitStatus = 1;
13448 if ((end_str = strchr(message, '\r')) != NULL)
13449 *end_str = NULLCHAR;
13450 if ((end_str = strchr(message, '\n')) != NULL)
13451 *end_str = NULLCHAR;
13453 if (appData.debugMode) {
13454 TimeMark now; int print = 1;
13455 char *quote = ""; char c; int i;
13457 if(appData.engineComments != 1) { /* [HGM] debug: decide if protocol-violating output is written */
13458 char start = message[0];
13459 if(start >='A' && start <= 'Z') start += 'a' - 'A'; // be tolerant to capitalizing
13460 if(sscanf(message, "%d%c%d%d%d", &i, &c, &i, &i, &i) != 5 &&
13461 sscanf(message, "move %c", &c)!=1 && sscanf(message, "offer%c", &c)!=1 &&
13462 sscanf(message, "resign%c", &c)!=1 && sscanf(message, "feature %c", &c)!=1 &&
13463 sscanf(message, "error %c", &c)!=1 && sscanf(message, "illegal %c", &c)!=1 &&
13464 sscanf(message, "tell%c", &c)!=1 && sscanf(message, "0-1 %c", &c)!=1 &&
13465 sscanf(message, "1-0 %c", &c)!=1 && sscanf(message, "1/2-1/2 %c", &c)!=1 &&
13466 sscanf(message, "pong %c", &c)!=1 && start != '#') {
13467 quote = appData.engineComments == 2 ? "# " : "### NON-COMPLIANT! ### ";
13468 print = (appData.engineComments >= 2);
13470 message[0] = start; // restore original message
13474 fprintf(debugFP, "%ld <%-6s: %s%s\n",
13475 SubtractTimeMarks(&now, &programStartTime), cps->which,
13481 /* [DM] if icsEngineAnalyze is active we block all whisper and kibitz output, because nobody want to see this */
13482 if (appData.icsEngineAnalyze) {
13483 if (strstr(message, "whisper") != NULL ||
13484 strstr(message, "kibitz") != NULL ||
13485 strstr(message, "tellics") != NULL) return;
13488 HandleMachineMove(message, cps);
13493 SendTimeControl(cps, mps, tc, inc, sd, st)
13494 ChessProgramState *cps;
13495 int mps, inc, sd, st;
13501 if( timeControl_2 > 0 ) {
13502 if( (gameMode == MachinePlaysBlack) || (gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b') ) {
13503 tc = timeControl_2;
13506 tc /= cps->timeOdds; /* [HGM] time odds: apply before telling engine */
13507 inc /= cps->timeOdds;
13508 st /= cps->timeOdds;
13510 seconds = (tc / 1000) % 60; /* [HGM] displaced to after applying odds */
13513 /* Set exact time per move, normally using st command */
13514 if (cps->stKludge) {
13515 /* GNU Chess 4 has no st command; uses level in a nonstandard way */
13517 if (seconds == 0) {
13518 snprintf(buf, MSG_SIZ, "level 1 %d\n", st/60);
13520 snprintf(buf, MSG_SIZ, "level 1 %d:%02d\n", st/60, seconds);
13523 snprintf(buf, MSG_SIZ, "st %d\n", st);
13526 /* Set conventional or incremental time control, using level command */
13527 if (seconds == 0) {
13528 /* Note old gnuchess bug -- minutes:seconds used to not work.
13529 Fixed in later versions, but still avoid :seconds
13530 when seconds is 0. */
13531 snprintf(buf, MSG_SIZ, "level %d %ld %d\n", mps, tc/60000, inc/1000);
13533 snprintf(buf, MSG_SIZ, "level %d %ld:%02d %d\n", mps, tc/60000,
13534 seconds, inc/1000);
13537 SendToProgram(buf, cps);
13539 /* Orthoganally (except for GNU Chess 4), limit time to st seconds */
13540 /* Orthogonally, limit search to given depth */
13542 if (cps->sdKludge) {
13543 snprintf(buf, MSG_SIZ, "depth\n%d\n", sd);
13545 snprintf(buf, MSG_SIZ, "sd %d\n", sd);
13547 SendToProgram(buf, cps);
13550 if(cps->nps > 0) { /* [HGM] nps */
13551 if(cps->supportsNPS == FALSE)
13552 cps->nps = -1; // don't use if engine explicitly says not supported!
13554 snprintf(buf, MSG_SIZ, "nps %d\n", cps->nps);
13555 SendToProgram(buf, cps);
13560 ChessProgramState *WhitePlayer()
13561 /* [HGM] return pointer to 'first' or 'second', depending on who plays white */
13563 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b' ||
13564 gameMode == BeginningOfGame || gameMode == MachinePlaysBlack)
13570 SendTimeRemaining(cps, machineWhite)
13571 ChessProgramState *cps;
13572 int /*boolean*/ machineWhite;
13574 char message[MSG_SIZ];
13577 /* Note: this routine must be called when the clocks are stopped
13578 or when they have *just* been set or switched; otherwise
13579 it will be off by the time since the current tick started.
13581 if (machineWhite) {
13582 time = whiteTimeRemaining / 10;
13583 otime = blackTimeRemaining / 10;
13585 time = blackTimeRemaining / 10;
13586 otime = whiteTimeRemaining / 10;
13588 /* [HGM] translate opponent's time by time-odds factor */
13589 otime = (otime * cps->other->timeOdds) / cps->timeOdds;
13590 if (appData.debugMode) {
13591 fprintf(debugFP, "time odds: %f %f \n", cps->timeOdds, cps->other->timeOdds);
13594 if (time <= 0) time = 1;
13595 if (otime <= 0) otime = 1;
13597 snprintf(message, MSG_SIZ, "time %ld\n", time);
13598 SendToProgram(message, cps);
13600 snprintf(message, MSG_SIZ, "otim %ld\n", otime);
13601 SendToProgram(message, cps);
13605 BoolFeature(p, name, loc, cps)
13609 ChessProgramState *cps;
13612 int len = strlen(name);
13615 if (strncmp((*p), name, len) == 0 && (*p)[len] == '=') {
13617 sscanf(*p, "%d", &val);
13619 while (**p && **p != ' ')
13621 snprintf(buf, MSG_SIZ, "accepted %s\n", name);
13622 SendToProgram(buf, cps);
13629 IntFeature(p, name, loc, cps)
13633 ChessProgramState *cps;
13636 int len = strlen(name);
13637 if (strncmp((*p), name, len) == 0 && (*p)[len] == '=') {
13639 sscanf(*p, "%d", loc);
13640 while (**p && **p != ' ') (*p)++;
13641 snprintf(buf, MSG_SIZ, "accepted %s\n", name);
13642 SendToProgram(buf, cps);
13649 StringFeature(p, name, loc, cps)
13653 ChessProgramState *cps;
13656 int len = strlen(name);
13657 if (strncmp((*p), name, len) == 0
13658 && (*p)[len] == '=' && (*p)[len+1] == '\"') {
13660 sscanf(*p, "%[^\"]", loc);
13661 while (**p && **p != '\"') (*p)++;
13662 if (**p == '\"') (*p)++;
13663 snprintf(buf, MSG_SIZ, "accepted %s\n", name);
13664 SendToProgram(buf, cps);
13671 ParseOption(Option *opt, ChessProgramState *cps)
13672 // [HGM] options: process the string that defines an engine option, and determine
13673 // name, type, default value, and allowed value range
13675 char *p, *q, buf[MSG_SIZ];
13676 int n, min = (-1)<<31, max = 1<<31, def;
13678 if(p = strstr(opt->name, " -spin ")) {
13679 if((n = sscanf(p, " -spin %d %d %d", &def, &min, &max)) < 3 ) return FALSE;
13680 if(max < min) max = min; // enforce consistency
13681 if(def < min) def = min;
13682 if(def > max) def = max;
13687 } else if((p = strstr(opt->name, " -slider "))) {
13688 // for now -slider is a synonym for -spin, to already provide compatibility with future polyglots
13689 if((n = sscanf(p, " -slider %d %d %d", &def, &min, &max)) < 3 ) return FALSE;
13690 if(max < min) max = min; // enforce consistency
13691 if(def < min) def = min;
13692 if(def > max) def = max;
13696 opt->type = Spin; // Slider;
13697 } else if((p = strstr(opt->name, " -string "))) {
13698 opt->textValue = p+9;
13699 opt->type = TextBox;
13700 } else if((p = strstr(opt->name, " -file "))) {
13701 // for now -file is a synonym for -string, to already provide compatibility with future polyglots
13702 opt->textValue = p+7;
13703 opt->type = TextBox; // FileName;
13704 } else if((p = strstr(opt->name, " -path "))) {
13705 // for now -file is a synonym for -string, to already provide compatibility with future polyglots
13706 opt->textValue = p+7;
13707 opt->type = TextBox; // PathName;
13708 } else if(p = strstr(opt->name, " -check ")) {
13709 if(sscanf(p, " -check %d", &def) < 1) return FALSE;
13710 opt->value = (def != 0);
13711 opt->type = CheckBox;
13712 } else if(p = strstr(opt->name, " -combo ")) {
13713 opt->textValue = (char*) (&cps->comboList[cps->comboCnt]); // cheat with pointer type
13714 cps->comboList[cps->comboCnt++] = q = p+8; // holds possible choices
13715 if(*q == '*') cps->comboList[cps->comboCnt-1]++;
13716 opt->value = n = 0;
13717 while(q = StrStr(q, " /// ")) {
13718 n++; *q = 0; // count choices, and null-terminate each of them
13720 if(*q == '*') { // remember default, which is marked with * prefix
13724 cps->comboList[cps->comboCnt++] = q;
13726 cps->comboList[cps->comboCnt++] = NULL;
13728 opt->type = ComboBox;
13729 } else if(p = strstr(opt->name, " -button")) {
13730 opt->type = Button;
13731 } else if(p = strstr(opt->name, " -save")) {
13732 opt->type = SaveButton;
13733 } else return FALSE;
13734 *p = 0; // terminate option name
13735 // now look if the command-line options define a setting for this engine option.
13736 if(cps->optionSettings && cps->optionSettings[0])
13737 p = strstr(cps->optionSettings, opt->name); else p = NULL;
13738 if(p && (p == cps->optionSettings || p[-1] == ',')) {
13739 snprintf(buf, MSG_SIZ, "option %s", p);
13740 if(p = strstr(buf, ",")) *p = 0;
13742 SendToProgram(buf, cps);
13748 FeatureDone(cps, val)
13749 ChessProgramState* cps;
13752 DelayedEventCallback cb = GetDelayedEvent();
13753 if ((cb == InitBackEnd3 && cps == &first) ||
13754 (cb == TwoMachinesEventIfReady && cps == &second)) {
13755 CancelDelayedEvent();
13756 ScheduleDelayedEvent(cb, val ? 1 : 3600000);
13758 cps->initDone = val;
13761 /* Parse feature command from engine */
13763 ParseFeatures(args, cps)
13765 ChessProgramState *cps;
13773 while (*p == ' ') p++;
13774 if (*p == NULLCHAR) return;
13776 if (BoolFeature(&p, "setboard", &cps->useSetboard, cps)) continue;
13777 if (BoolFeature(&p, "time", &cps->sendTime, cps)) continue;
13778 if (BoolFeature(&p, "draw", &cps->sendDrawOffers, cps)) continue;
13779 if (BoolFeature(&p, "sigint", &cps->useSigint, cps)) continue;
13780 if (BoolFeature(&p, "sigterm", &cps->useSigterm, cps)) continue;
13781 if (BoolFeature(&p, "reuse", &val, cps)) {
13782 /* Engine can disable reuse, but can't enable it if user said no */
13783 if (!val) cps->reuse = FALSE;
13786 if (BoolFeature(&p, "analyze", &cps->analysisSupport, cps)) continue;
13787 if (StringFeature(&p, "myname", &cps->tidy, cps)) {
13788 if (gameMode == TwoMachinesPlay) {
13789 DisplayTwoMachinesTitle();
13795 if (StringFeature(&p, "variants", &cps->variants, cps)) continue;
13796 if (BoolFeature(&p, "san", &cps->useSAN, cps)) continue;
13797 if (BoolFeature(&p, "ping", &cps->usePing, cps)) continue;
13798 if (BoolFeature(&p, "playother", &cps->usePlayother, cps)) continue;
13799 if (BoolFeature(&p, "colors", &cps->useColors, cps)) continue;
13800 if (BoolFeature(&p, "usermove", &cps->useUsermove, cps)) continue;
13801 if (BoolFeature(&p, "ics", &cps->sendICS, cps)) continue;
13802 if (BoolFeature(&p, "name", &cps->sendName, cps)) continue;
13803 if (BoolFeature(&p, "pause", &val, cps)) continue; /* unused at present */
13804 if (IntFeature(&p, "done", &val, cps)) {
13805 FeatureDone(cps, val);
13808 /* Added by Tord: */
13809 if (BoolFeature(&p, "fen960", &cps->useFEN960, cps)) continue;
13810 if (BoolFeature(&p, "oocastle", &cps->useOOCastle, cps)) continue;
13811 /* End of additions by Tord */
13813 /* [HGM] added features: */
13814 if (BoolFeature(&p, "debug", &cps->debug, cps)) continue;
13815 if (BoolFeature(&p, "nps", &cps->supportsNPS, cps)) continue;
13816 if (IntFeature(&p, "level", &cps->maxNrOfSessions, cps)) continue;
13817 if (BoolFeature(&p, "memory", &cps->memSize, cps)) continue;
13818 if (BoolFeature(&p, "smp", &cps->maxCores, cps)) continue;
13819 if (StringFeature(&p, "egt", &cps->egtFormats, cps)) continue;
13820 if (StringFeature(&p, "option", &(cps->option[cps->nrOptions].name), cps)) {
13821 if(!ParseOption(&(cps->option[cps->nrOptions++]), cps)) { // [HGM] options: add option feature
13822 snprintf(buf, MSG_SIZ, "rejected option %s\n", cps->option[--cps->nrOptions].name);
13823 SendToProgram(buf, cps);
13826 if(cps->nrOptions >= MAX_OPTIONS) {
13828 snprintf(buf, MSG_SIZ, "%s engine has too many options\n", cps->which);
13829 DisplayError(buf, 0);
13833 /* End of additions by HGM */
13835 /* unknown feature: complain and skip */
13837 while (*q && *q != '=') q++;
13838 snprintf(buf, MSG_SIZ,"rejected %.*s\n", (int)(q-p), p);
13839 SendToProgram(buf, cps);
13845 while (*p && *p != '\"') p++;
13846 if (*p == '\"') p++;
13848 while (*p && *p != ' ') p++;
13856 PeriodicUpdatesEvent(newState)
13859 if (newState == appData.periodicUpdates)
13862 appData.periodicUpdates=newState;
13864 /* Display type changes, so update it now */
13865 // DisplayAnalysis();
13867 /* Get the ball rolling again... */
13869 AnalysisPeriodicEvent(1);
13870 StartAnalysisClock();
13875 PonderNextMoveEvent(newState)
13878 if (newState == appData.ponderNextMove) return;
13879 if (gameMode == EditPosition) EditPositionDone(TRUE);
13881 SendToProgram("hard\n", &first);
13882 if (gameMode == TwoMachinesPlay) {
13883 SendToProgram("hard\n", &second);
13886 SendToProgram("easy\n", &first);
13887 thinkOutput[0] = NULLCHAR;
13888 if (gameMode == TwoMachinesPlay) {
13889 SendToProgram("easy\n", &second);
13892 appData.ponderNextMove = newState;
13896 NewSettingEvent(option, feature, command, value)
13898 int option, value, *feature;
13902 if (gameMode == EditPosition) EditPositionDone(TRUE);
13903 snprintf(buf, MSG_SIZ,"%s%s %d\n", (option ? "option ": ""), command, value);
13904 if(feature == NULL || *feature) SendToProgram(buf, &first);
13905 if (gameMode == TwoMachinesPlay) {
13906 if(feature == NULL || feature[(int*)&second - (int*)&first]) SendToProgram(buf, &second);
13911 ShowThinkingEvent()
13912 // [HGM] thinking: this routine is now also called from "Options -> Engine..." popup
13914 static int oldState = 2; // kludge alert! Neither true nor fals, so first time oldState is always updated
13915 int newState = appData.showThinking
13916 // [HGM] thinking: other features now need thinking output as well
13917 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp();
13919 if (oldState == newState) return;
13920 oldState = newState;
13921 if (gameMode == EditPosition) EditPositionDone(TRUE);
13923 SendToProgram("post\n", &first);
13924 if (gameMode == TwoMachinesPlay) {
13925 SendToProgram("post\n", &second);
13928 SendToProgram("nopost\n", &first);
13929 thinkOutput[0] = NULLCHAR;
13930 if (gameMode == TwoMachinesPlay) {
13931 SendToProgram("nopost\n", &second);
13934 // appData.showThinking = newState; // [HGM] thinking: responsible option should already have be changed when calling this routine!
13938 AskQuestionEvent(title, question, replyPrefix, which)
13939 char *title; char *question; char *replyPrefix; char *which;
13941 ProcRef pr = (which[0] == '1') ? first.pr : second.pr;
13942 if (pr == NoProc) return;
13943 AskQuestion(title, question, replyPrefix, pr);
13947 DisplayMove(moveNumber)
13950 char message[MSG_SIZ];
13952 char cpThinkOutput[MSG_SIZ];
13954 if(appData.noGUI) return; // [HGM] fast: suppress display of moves
13956 if (moveNumber == forwardMostMove - 1 ||
13957 gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
13959 safeStrCpy(cpThinkOutput, thinkOutput, sizeof(cpThinkOutput)/sizeof(cpThinkOutput[0]));
13961 if (strchr(cpThinkOutput, '\n')) {
13962 *strchr(cpThinkOutput, '\n') = NULLCHAR;
13965 *cpThinkOutput = NULLCHAR;
13968 /* [AS] Hide thinking from human user */
13969 if( appData.hideThinkingFromHuman && gameMode != TwoMachinesPlay ) {
13970 *cpThinkOutput = NULLCHAR;
13971 if( thinkOutput[0] != NULLCHAR ) {
13974 for( i=0; i<=hiddenThinkOutputState; i++ ) {
13975 cpThinkOutput[i] = '.';
13977 cpThinkOutput[i] = NULLCHAR;
13978 hiddenThinkOutputState = (hiddenThinkOutputState + 1) % 3;
13982 if (moveNumber == forwardMostMove - 1 &&
13983 gameInfo.resultDetails != NULL) {
13984 if (gameInfo.resultDetails[0] == NULLCHAR) {
13985 snprintf(res, MSG_SIZ, " %s", PGNResult(gameInfo.result));
13987 snprintf(res, MSG_SIZ, " {%s} %s",
13988 T_(gameInfo.resultDetails), PGNResult(gameInfo.result));
13994 if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
13995 DisplayMessage(res, cpThinkOutput);
13997 snprintf(message, MSG_SIZ, "%d.%s%s%s", moveNumber / 2 + 1,
13998 WhiteOnMove(moveNumber) ? " " : ".. ",
13999 parseList[moveNumber], res);
14000 DisplayMessage(message, cpThinkOutput);
14005 DisplayComment(moveNumber, text)
14009 char title[MSG_SIZ];
14010 char buf[8000]; // comment can be long!
14013 if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
14014 safeStrCpy(title, "Comment", sizeof(title)/sizeof(title[0]));
14016 snprintf(title,MSG_SIZ, "Comment on %d.%s%s", moveNumber / 2 + 1,
14017 WhiteOnMove(moveNumber) ? " " : ".. ",
14018 parseList[moveNumber]);
14020 // [HGM] PV info: display PV info together with (or as) comment
14021 if(moveNumber >= 0 && (depth = pvInfoList[moveNumber].depth) > 0) {
14022 if(text == NULL) text = "";
14023 score = pvInfoList[moveNumber].score;
14024 snprintf(buf,sizeof(buf)/sizeof(buf[0]), "%s%.2f/%d %d\n%s", score>0 ? "+" : "", score/100.,
14025 depth, (pvInfoList[moveNumber].time+50)/100, text);
14028 if (text != NULL && (appData.autoDisplayComment || commentUp))
14029 CommentPopUp(title, text);
14032 /* This routine sends a ^C interrupt to gnuchess, to awaken it if it
14033 * might be busy thinking or pondering. It can be omitted if your
14034 * gnuchess is configured to stop thinking immediately on any user
14035 * input. However, that gnuchess feature depends on the FIONREAD
14036 * ioctl, which does not work properly on some flavors of Unix.
14040 ChessProgramState *cps;
14043 if (!cps->useSigint) return;
14044 if (appData.noChessProgram || (cps->pr == NoProc)) return;
14045 switch (gameMode) {
14046 case MachinePlaysWhite:
14047 case MachinePlaysBlack:
14048 case TwoMachinesPlay:
14049 case IcsPlayingWhite:
14050 case IcsPlayingBlack:
14053 /* Skip if we know it isn't thinking */
14054 if (!cps->maybeThinking) return;
14055 if (appData.debugMode)
14056 fprintf(debugFP, "Interrupting %s\n", cps->which);
14057 InterruptChildProcess(cps->pr);
14058 cps->maybeThinking = FALSE;
14063 #endif /*ATTENTION*/
14069 if (whiteTimeRemaining <= 0) {
14072 if (appData.icsActive) {
14073 if (appData.autoCallFlag &&
14074 gameMode == IcsPlayingBlack && !blackFlag) {
14075 SendToICS(ics_prefix);
14076 SendToICS("flag\n");
14080 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Both flags fell"));
14082 if(gameMode != TwoMachinesPlay) DisplayTitle(_("White's flag fell"));
14083 if (appData.autoCallFlag) {
14084 GameEnds(BlackWins, "Black wins on time", GE_XBOARD);
14091 if (blackTimeRemaining <= 0) {
14094 if (appData.icsActive) {
14095 if (appData.autoCallFlag &&
14096 gameMode == IcsPlayingWhite && !whiteFlag) {
14097 SendToICS(ics_prefix);
14098 SendToICS("flag\n");
14102 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Both flags fell"));
14104 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Black's flag fell"));
14105 if (appData.autoCallFlag) {
14106 GameEnds(WhiteWins, "White wins on time", GE_XBOARD);
14119 if (!appData.clockMode || appData.icsActive || searchTime || // [HGM] st: no inc in st mode
14120 gameMode == PlayFromGameFile || forwardMostMove == 0) return;
14123 * add time to clocks when time control is achieved ([HGM] now also used for increment)
14125 if ( !WhiteOnMove(forwardMostMove) ) {
14126 /* White made time control */
14127 lastWhite -= whiteTimeRemaining; // [HGM] contains start time, socalculate thinking time
14128 whiteTimeRemaining += GetTimeQuota((forwardMostMove-whiteStartMove-1)/2, lastWhite, whiteTC)
14129 /* [HGM] time odds: correct new time quota for time odds! */
14130 / WhitePlayer()->timeOdds;
14131 lastBlack = blackTimeRemaining; // [HGM] leave absolute time (after quota), so next switch we can us it to calculate thinking time
14133 lastBlack -= blackTimeRemaining;
14134 /* Black made time control */
14135 blackTimeRemaining += GetTimeQuota((forwardMostMove-blackStartMove-1)/2, lastBlack, blackTC)
14136 / WhitePlayer()->other->timeOdds;
14137 lastWhite = whiteTimeRemaining;
14142 DisplayBothClocks()
14144 int wom = gameMode == EditPosition ?
14145 !blackPlaysFirst : WhiteOnMove(currentMove);
14146 DisplayWhiteClock(whiteTimeRemaining, wom);
14147 DisplayBlackClock(blackTimeRemaining, !wom);
14151 /* Timekeeping seems to be a portability nightmare. I think everyone
14152 has ftime(), but I'm really not sure, so I'm including some ifdefs
14153 to use other calls if you don't. Clocks will be less accurate if
14154 you have neither ftime nor gettimeofday.
14157 /* VS 2008 requires the #include outside of the function */
14158 #if !HAVE_GETTIMEOFDAY && HAVE_FTIME
14159 #include <sys/timeb.h>
14162 /* Get the current time as a TimeMark */
14167 #if HAVE_GETTIMEOFDAY
14169 struct timeval timeVal;
14170 struct timezone timeZone;
14172 gettimeofday(&timeVal, &timeZone);
14173 tm->sec = (long) timeVal.tv_sec;
14174 tm->ms = (int) (timeVal.tv_usec / 1000L);
14176 #else /*!HAVE_GETTIMEOFDAY*/
14179 // include <sys/timeb.h> / moved to just above start of function
14180 struct timeb timeB;
14183 tm->sec = (long) timeB.time;
14184 tm->ms = (int) timeB.millitm;
14186 #else /*!HAVE_FTIME && !HAVE_GETTIMEOFDAY*/
14187 tm->sec = (long) time(NULL);
14193 /* Return the difference in milliseconds between two
14194 time marks. We assume the difference will fit in a long!
14197 SubtractTimeMarks(tm2, tm1)
14198 TimeMark *tm2, *tm1;
14200 return 1000L*(tm2->sec - tm1->sec) +
14201 (long) (tm2->ms - tm1->ms);
14206 * Code to manage the game clocks.
14208 * In tournament play, black starts the clock and then white makes a move.
14209 * We give the human user a slight advantage if he is playing white---the
14210 * clocks don't run until he makes his first move, so it takes zero time.
14211 * Also, we don't account for network lag, so we could get out of sync
14212 * with GNU Chess's clock -- but then, referees are always right.
14215 static TimeMark tickStartTM;
14216 static long intendedTickLength;
14219 NextTickLength(timeRemaining)
14220 long timeRemaining;
14222 long nominalTickLength, nextTickLength;
14224 if (timeRemaining > 0L && timeRemaining <= 10000L)
14225 nominalTickLength = 100L;
14227 nominalTickLength = 1000L;
14228 nextTickLength = timeRemaining % nominalTickLength;
14229 if (nextTickLength <= 0) nextTickLength += nominalTickLength;
14231 return nextTickLength;
14234 /* Adjust clock one minute up or down */
14236 AdjustClock(Boolean which, int dir)
14238 if(which) blackTimeRemaining += 60000*dir;
14239 else whiteTimeRemaining += 60000*dir;
14240 DisplayBothClocks();
14243 /* Stop clocks and reset to a fresh time control */
14247 (void) StopClockTimer();
14248 if (appData.icsActive) {
14249 whiteTimeRemaining = blackTimeRemaining = 0;
14250 } else if (searchTime) {
14251 whiteTimeRemaining = 1000*searchTime / WhitePlayer()->timeOdds;
14252 blackTimeRemaining = 1000*searchTime / WhitePlayer()->other->timeOdds;
14253 } else { /* [HGM] correct new time quote for time odds */
14254 whiteTC = blackTC = fullTimeControlString;
14255 whiteTimeRemaining = GetTimeQuota(-1, 0, whiteTC) / WhitePlayer()->timeOdds;
14256 blackTimeRemaining = GetTimeQuota(-1, 0, blackTC) / WhitePlayer()->other->timeOdds;
14258 if (whiteFlag || blackFlag) {
14260 whiteFlag = blackFlag = FALSE;
14262 lastWhite = lastBlack = whiteStartMove = blackStartMove = 0;
14263 DisplayBothClocks();
14266 #define FUDGE 25 /* 25ms = 1/40 sec; should be plenty even for 50 Hz clocks */
14268 /* Decrement running clock by amount of time that has passed */
14272 long timeRemaining;
14273 long lastTickLength, fudge;
14276 if (!appData.clockMode) return;
14277 if (gameMode==AnalyzeMode || gameMode == AnalyzeFile) return;
14281 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
14283 /* Fudge if we woke up a little too soon */
14284 fudge = intendedTickLength - lastTickLength;
14285 if (fudge < 0 || fudge > FUDGE) fudge = 0;
14287 if (WhiteOnMove(forwardMostMove)) {
14288 if(whiteNPS >= 0) lastTickLength = 0;
14289 timeRemaining = whiteTimeRemaining -= lastTickLength;
14290 if(timeRemaining < 0) {
14291 GetTimeQuota((forwardMostMove-whiteStartMove-1)/2, 0, whiteTC); // sets suddenDeath & nextSession;
14292 if(suddenDeath) { // [HGM] if we run out of a non-last incremental session, go to the next
14293 whiteStartMove = forwardMostMove; whiteTC = nextSession;
14294 lastWhite= timeRemaining = whiteTimeRemaining += GetTimeQuota(-1, 0, whiteTC);
14297 DisplayWhiteClock(whiteTimeRemaining - fudge,
14298 WhiteOnMove(currentMove < forwardMostMove ? currentMove : forwardMostMove));
14300 if(blackNPS >= 0) lastTickLength = 0;
14301 timeRemaining = blackTimeRemaining -= lastTickLength;
14302 if(timeRemaining < 0) { // [HGM] if we run out of a non-last incremental session, go to the next
14303 GetTimeQuota((forwardMostMove-blackStartMove-1)/2, 0, blackTC);
14305 blackStartMove = forwardMostMove;
14306 lastBlack = timeRemaining = blackTimeRemaining += GetTimeQuota(-1, 0, blackTC=nextSession);
14309 DisplayBlackClock(blackTimeRemaining - fudge,
14310 !WhiteOnMove(currentMove < forwardMostMove ? currentMove : forwardMostMove));
14312 if (CheckFlags()) return;
14315 intendedTickLength = NextTickLength(timeRemaining - fudge) + fudge;
14316 StartClockTimer(intendedTickLength);
14318 /* if the time remaining has fallen below the alarm threshold, sound the
14319 * alarm. if the alarm has sounded and (due to a takeback or time control
14320 * with increment) the time remaining has increased to a level above the
14321 * threshold, reset the alarm so it can sound again.
14324 if (appData.icsActive && appData.icsAlarm) {
14326 /* make sure we are dealing with the user's clock */
14327 if (!( ((gameMode == IcsPlayingWhite) && WhiteOnMove(currentMove)) ||
14328 ((gameMode == IcsPlayingBlack) && !WhiteOnMove(currentMove))
14331 if (alarmSounded && (timeRemaining > appData.icsAlarmTime)) {
14332 alarmSounded = FALSE;
14333 } else if (!alarmSounded && (timeRemaining <= appData.icsAlarmTime)) {
14335 alarmSounded = TRUE;
14341 /* A player has just moved, so stop the previously running
14342 clock and (if in clock mode) start the other one.
14343 We redisplay both clocks in case we're in ICS mode, because
14344 ICS gives us an update to both clocks after every move.
14345 Note that this routine is called *after* forwardMostMove
14346 is updated, so the last fractional tick must be subtracted
14347 from the color that is *not* on move now.
14350 SwitchClocks(int newMoveNr)
14352 long lastTickLength;
14354 int flagged = FALSE;
14358 if (StopClockTimer() && appData.clockMode) {
14359 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
14360 if (!WhiteOnMove(forwardMostMove)) {
14361 if(blackNPS >= 0) lastTickLength = 0;
14362 blackTimeRemaining -= lastTickLength;
14363 /* [HGM] PGNtime: save time for PGN file if engine did not give it */
14364 // if(pvInfoList[forwardMostMove-1].time == -1)
14365 pvInfoList[forwardMostMove-1].time = // use GUI time
14366 (timeRemaining[1][forwardMostMove-1] - blackTimeRemaining)/10;
14368 if(whiteNPS >= 0) lastTickLength = 0;
14369 whiteTimeRemaining -= lastTickLength;
14370 /* [HGM] PGNtime: save time for PGN file if engine did not give it */
14371 // if(pvInfoList[forwardMostMove-1].time == -1)
14372 pvInfoList[forwardMostMove-1].time =
14373 (timeRemaining[0][forwardMostMove-1] - whiteTimeRemaining)/10;
14375 flagged = CheckFlags();
14377 forwardMostMove = newMoveNr; // [HGM] race: change stm when no timer interrupt scheduled
14378 CheckTimeControl();
14380 if (flagged || !appData.clockMode) return;
14382 switch (gameMode) {
14383 case MachinePlaysBlack:
14384 case MachinePlaysWhite:
14385 case BeginningOfGame:
14386 if (pausing) return;
14390 case PlayFromGameFile:
14398 if (searchTime) { // [HGM] st: set clock of player that has to move to max time
14399 if(WhiteOnMove(forwardMostMove))
14400 whiteTimeRemaining = 1000*searchTime / WhitePlayer()->timeOdds;
14401 else blackTimeRemaining = 1000*searchTime / WhitePlayer()->other->timeOdds;
14405 intendedTickLength = NextTickLength(WhiteOnMove(forwardMostMove) ?
14406 whiteTimeRemaining : blackTimeRemaining);
14407 StartClockTimer(intendedTickLength);
14411 /* Stop both clocks */
14415 long lastTickLength;
14418 if (!StopClockTimer()) return;
14419 if (!appData.clockMode) return;
14423 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
14424 if (WhiteOnMove(forwardMostMove)) {
14425 if(whiteNPS >= 0) lastTickLength = 0;
14426 whiteTimeRemaining -= lastTickLength;
14427 DisplayWhiteClock(whiteTimeRemaining, WhiteOnMove(currentMove));
14429 if(blackNPS >= 0) lastTickLength = 0;
14430 blackTimeRemaining -= lastTickLength;
14431 DisplayBlackClock(blackTimeRemaining, !WhiteOnMove(currentMove));
14436 /* Start clock of player on move. Time may have been reset, so
14437 if clock is already running, stop and restart it. */
14441 (void) StopClockTimer(); /* in case it was running already */
14442 DisplayBothClocks();
14443 if (CheckFlags()) return;
14445 if (!appData.clockMode) return;
14446 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) return;
14448 GetTimeMark(&tickStartTM);
14449 intendedTickLength = NextTickLength(WhiteOnMove(forwardMostMove) ?
14450 whiteTimeRemaining : blackTimeRemaining);
14452 /* [HGM] nps: figure out nps factors, by determining which engine plays white and/or black once and for all */
14453 whiteNPS = blackNPS = -1;
14454 if(gameMode == MachinePlaysWhite || gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w'
14455 || appData.zippyPlay && gameMode == IcsPlayingBlack) // first (perhaps only) engine has white
14456 whiteNPS = first.nps;
14457 if(gameMode == MachinePlaysBlack || gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b'
14458 || appData.zippyPlay && gameMode == IcsPlayingWhite) // first (perhaps only) engine has black
14459 blackNPS = first.nps;
14460 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b') // second only used in Two-Machines mode
14461 whiteNPS = second.nps;
14462 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w')
14463 blackNPS = second.nps;
14464 if(appData.debugMode) fprintf(debugFP, "nps: w=%d, b=%d\n", whiteNPS, blackNPS);
14466 StartClockTimer(intendedTickLength);
14473 long second, minute, hour, day;
14475 static char buf[32];
14477 if (ms > 0 && ms <= 9900) {
14478 /* convert milliseconds to tenths, rounding up */
14479 double tenths = floor( ((double)(ms + 99L)) / 100.00 );
14481 snprintf(buf,sizeof(buf)/sizeof(buf[0]), " %03.1f ", tenths/10.0);
14485 /* convert milliseconds to seconds, rounding up */
14486 /* use floating point to avoid strangeness of integer division
14487 with negative dividends on many machines */
14488 second = (long) floor(((double) (ms + 999L)) / 1000.0);
14495 day = second / (60 * 60 * 24);
14496 second = second % (60 * 60 * 24);
14497 hour = second / (60 * 60);
14498 second = second % (60 * 60);
14499 minute = second / 60;
14500 second = second % 60;
14503 snprintf(buf, sizeof(buf)/sizeof(buf[0]), " %s%ld:%02ld:%02ld:%02ld ",
14504 sign, day, hour, minute, second);
14506 snprintf(buf, sizeof(buf)/sizeof(buf[0]), " %s%ld:%02ld:%02ld ", sign, hour, minute, second);
14508 snprintf(buf, sizeof(buf)/sizeof(buf[0]), " %s%2ld:%02ld ", sign, minute, second);
14515 * This is necessary because some C libraries aren't ANSI C compliant yet.
14518 StrStr(string, match)
14519 char *string, *match;
14523 length = strlen(match);
14525 for (i = strlen(string) - length; i >= 0; i--, string++)
14526 if (!strncmp(match, string, length))
14533 StrCaseStr(string, match)
14534 char *string, *match;
14538 length = strlen(match);
14540 for (i = strlen(string) - length; i >= 0; i--, string++) {
14541 for (j = 0; j < length; j++) {
14542 if (ToLower(match[j]) != ToLower(string[j]))
14545 if (j == length) return string;
14559 c1 = ToLower(*s1++);
14560 c2 = ToLower(*s2++);
14561 if (c1 > c2) return 1;
14562 if (c1 < c2) return -1;
14563 if (c1 == NULLCHAR) return 0;
14572 return isupper(c) ? tolower(c) : c;
14580 return islower(c) ? toupper(c) : c;
14582 #endif /* !_amigados */
14590 if ((ret = (char *) malloc(strlen(s) + 1)))
14592 safeStrCpy(ret, s, strlen(s)+1);
14598 StrSavePtr(s, savePtr)
14599 char *s, **savePtr;
14604 if ((*savePtr = (char *) malloc(strlen(s) + 1))) {
14605 safeStrCpy(*savePtr, s, strlen(s)+1);
14617 clock = time((time_t *)NULL);
14618 tm = localtime(&clock);
14619 snprintf(buf, MSG_SIZ, "%04d.%02d.%02d",
14620 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
14621 return StrSave(buf);
14626 PositionToFEN(move, overrideCastling)
14628 char *overrideCastling;
14630 int i, j, fromX, fromY, toX, toY;
14637 whiteToPlay = (gameMode == EditPosition) ?
14638 !blackPlaysFirst : (move % 2 == 0);
14641 /* Piece placement data */
14642 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
14644 for (j = BOARD_LEFT; j < BOARD_RGHT; j++) {
14645 if (boards[move][i][j] == EmptySquare) {
14647 } else { ChessSquare piece = boards[move][i][j];
14648 if (emptycount > 0) {
14649 if(emptycount<10) /* [HGM] can be >= 10 */
14650 *p++ = '0' + emptycount;
14651 else { *p++ = '0' + emptycount/10; *p++ = '0' + emptycount%10; }
14654 if(PieceToChar(piece) == '+') {
14655 /* [HGM] write promoted pieces as '+<unpromoted>' (Shogi) */
14657 piece = (ChessSquare)(DEMOTED piece);
14659 *p++ = PieceToChar(piece);
14661 /* [HGM] flag promoted pieces as '<promoted>~' (Crazyhouse) */
14662 p[-1] = PieceToChar((ChessSquare)(DEMOTED piece));
14667 if (emptycount > 0) {
14668 if(emptycount<10) /* [HGM] can be >= 10 */
14669 *p++ = '0' + emptycount;
14670 else { *p++ = '0' + emptycount/10; *p++ = '0' + emptycount%10; }
14677 /* [HGM] print Crazyhouse or Shogi holdings */
14678 if( gameInfo.holdingsWidth ) {
14679 *(p-1) = '['; /* if we wanted to support BFEN, this could be '/' */
14681 for(i=0; i<gameInfo.holdingsSize; i++) { /* white holdings */
14682 piece = boards[move][i][BOARD_WIDTH-1];
14683 if( piece != EmptySquare )
14684 for(j=0; j<(int) boards[move][i][BOARD_WIDTH-2]; j++)
14685 *p++ = PieceToChar(piece);
14687 for(i=0; i<gameInfo.holdingsSize; i++) { /* black holdings */
14688 piece = boards[move][BOARD_HEIGHT-i-1][0];
14689 if( piece != EmptySquare )
14690 for(j=0; j<(int) boards[move][BOARD_HEIGHT-i-1][1]; j++)
14691 *p++ = PieceToChar(piece);
14694 if( q == p ) *p++ = '-';
14700 *p++ = whiteToPlay ? 'w' : 'b';
14703 if(q = overrideCastling) { // [HGM] FRC: override castling & e.p fields for non-compliant engines
14704 while(*p++ = *q++); if(q != overrideCastling+1) p[-1] = ' '; else --p;
14706 if(nrCastlingRights) {
14708 if(gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom) {
14709 /* [HGM] write directly from rights */
14710 if(boards[move][CASTLING][2] != NoRights &&
14711 boards[move][CASTLING][0] != NoRights )
14712 *p++ = boards[move][CASTLING][0] + AAA + 'A' - 'a';
14713 if(boards[move][CASTLING][2] != NoRights &&
14714 boards[move][CASTLING][1] != NoRights )
14715 *p++ = boards[move][CASTLING][1] + AAA + 'A' - 'a';
14716 if(boards[move][CASTLING][5] != NoRights &&
14717 boards[move][CASTLING][3] != NoRights )
14718 *p++ = boards[move][CASTLING][3] + AAA;
14719 if(boards[move][CASTLING][5] != NoRights &&
14720 boards[move][CASTLING][4] != NoRights )
14721 *p++ = boards[move][CASTLING][4] + AAA;
14724 /* [HGM] write true castling rights */
14725 if( nrCastlingRights == 6 ) {
14726 if(boards[move][CASTLING][0] == BOARD_RGHT-1 &&
14727 boards[move][CASTLING][2] != NoRights ) *p++ = 'K';
14728 if(boards[move][CASTLING][1] == BOARD_LEFT &&
14729 boards[move][CASTLING][2] != NoRights ) *p++ = 'Q';
14730 if(boards[move][CASTLING][3] == BOARD_RGHT-1 &&
14731 boards[move][CASTLING][5] != NoRights ) *p++ = 'k';
14732 if(boards[move][CASTLING][4] == BOARD_LEFT &&
14733 boards[move][CASTLING][5] != NoRights ) *p++ = 'q';
14736 if (q == p) *p++ = '-'; /* No castling rights */
14740 if(gameInfo.variant != VariantShogi && gameInfo.variant != VariantXiangqi &&
14741 gameInfo.variant != VariantShatranj && gameInfo.variant != VariantCourier && gameInfo.variant != VariantMakruk ) {
14742 /* En passant target square */
14743 if (move > backwardMostMove) {
14744 fromX = moveList[move - 1][0] - AAA;
14745 fromY = moveList[move - 1][1] - ONE;
14746 toX = moveList[move - 1][2] - AAA;
14747 toY = moveList[move - 1][3] - ONE;
14748 if (fromY == (whiteToPlay ? BOARD_HEIGHT-2 : 1) &&
14749 toY == (whiteToPlay ? BOARD_HEIGHT-4 : 3) &&
14750 boards[move][toY][toX] == (whiteToPlay ? BlackPawn : WhitePawn) &&
14752 /* 2-square pawn move just happened */
14754 *p++ = whiteToPlay ? '6'+BOARD_HEIGHT-8 : '3';
14758 } else if(move == backwardMostMove) {
14759 // [HGM] perhaps we should always do it like this, and forget the above?
14760 if((signed char)boards[move][EP_STATUS] >= 0) {
14761 *p++ = boards[move][EP_STATUS] + AAA;
14762 *p++ = whiteToPlay ? '6'+BOARD_HEIGHT-8 : '3';
14773 /* [HGM] find reversible plies */
14774 { int i = 0, j=move;
14776 if (appData.debugMode) { int k;
14777 fprintf(debugFP, "write FEN 50-move: %d %d %d\n", initialRulePlies, forwardMostMove, backwardMostMove);
14778 for(k=backwardMostMove; k<=forwardMostMove; k++)
14779 fprintf(debugFP, "e%d. p=%d\n", k, (signed char)boards[k][EP_STATUS]);
14783 while(j > backwardMostMove && (signed char)boards[j][EP_STATUS] <= EP_NONE) j--,i++;
14784 if( j == backwardMostMove ) i += initialRulePlies;
14785 sprintf(p, "%d ", i);
14786 p += i>=100 ? 4 : i >= 10 ? 3 : 2;
14788 /* Fullmove number */
14789 sprintf(p, "%d", (move / 2) + 1);
14791 return StrSave(buf);
14795 ParseFEN(board, blackPlaysFirst, fen)
14797 int *blackPlaysFirst;
14807 /* [HGM] by default clear Crazyhouse holdings, if present */
14808 if(gameInfo.holdingsWidth) {
14809 for(i=0; i<BOARD_HEIGHT; i++) {
14810 board[i][0] = EmptySquare; /* black holdings */
14811 board[i][BOARD_WIDTH-1] = EmptySquare; /* white holdings */
14812 board[i][1] = (ChessSquare) 0; /* black counts */
14813 board[i][BOARD_WIDTH-2] = (ChessSquare) 0; /* white counts */
14817 /* Piece placement data */
14818 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
14821 if (*p == '/' || *p == ' ' || (*p == '[' && i == 0) ) {
14822 if (*p == '/') p++;
14823 emptycount = gameInfo.boardWidth - j;
14824 while (emptycount--)
14825 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
14827 #if(BOARD_FILES >= 10)
14828 } else if(*p=='x' || *p=='X') { /* [HGM] X means 10 */
14829 p++; emptycount=10;
14830 if (j + emptycount > gameInfo.boardWidth) return FALSE;
14831 while (emptycount--)
14832 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
14834 } else if (isdigit(*p)) {
14835 emptycount = *p++ - '0';
14836 while(isdigit(*p)) emptycount = 10*emptycount + *p++ - '0'; /* [HGM] allow > 9 */
14837 if (j + emptycount > gameInfo.boardWidth) return FALSE;
14838 while (emptycount--)
14839 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
14840 } else if (*p == '+' || isalpha(*p)) {
14841 if (j >= gameInfo.boardWidth) return FALSE;
14843 piece = CharToPiece(*++p);
14844 if(piece == EmptySquare) return FALSE; /* unknown piece */
14845 piece = (ChessSquare) (PROMOTED piece ); p++;
14846 if(PieceToChar(piece) != '+') return FALSE; /* unpromotable piece */
14847 } else piece = CharToPiece(*p++);
14849 if(piece==EmptySquare) return FALSE; /* unknown piece */
14850 if(*p == '~') { /* [HGM] make it a promoted piece for Crazyhouse */
14851 piece = (ChessSquare) (PROMOTED piece);
14852 if(PieceToChar(piece) != '~') return FALSE; /* cannot be a promoted piece */
14855 board[i][(j++)+gameInfo.holdingsWidth] = piece;
14861 while (*p == '/' || *p == ' ') p++;
14863 /* [HGM] look for Crazyhouse holdings here */
14864 while(*p==' ') p++;
14865 if( gameInfo.holdingsWidth && p[-1] == '/' || *p == '[') {
14867 if(*p == '-' ) p++; /* empty holdings */ else {
14868 if( !gameInfo.holdingsWidth ) return FALSE; /* no room to put holdings! */
14869 /* if we would allow FEN reading to set board size, we would */
14870 /* have to add holdings and shift the board read so far here */
14871 while( (piece = CharToPiece(*p) ) != EmptySquare ) {
14873 if((int) piece >= (int) BlackPawn ) {
14874 i = (int)piece - (int)BlackPawn;
14875 i = PieceToNumber((ChessSquare)i);
14876 if( i >= gameInfo.holdingsSize ) return FALSE;
14877 board[BOARD_HEIGHT-1-i][0] = piece; /* black holdings */
14878 board[BOARD_HEIGHT-1-i][1]++; /* black counts */
14880 i = (int)piece - (int)WhitePawn;
14881 i = PieceToNumber((ChessSquare)i);
14882 if( i >= gameInfo.holdingsSize ) return FALSE;
14883 board[i][BOARD_WIDTH-1] = piece; /* white holdings */
14884 board[i][BOARD_WIDTH-2]++; /* black holdings */
14891 while(*p == ' ') p++;
14895 if(appData.colorNickNames) {
14896 if( c == appData.colorNickNames[0] ) c = 'w'; else
14897 if( c == appData.colorNickNames[1] ) c = 'b';
14901 *blackPlaysFirst = FALSE;
14904 *blackPlaysFirst = TRUE;
14910 /* [HGM] We NO LONGER ignore the rest of the FEN notation */
14911 /* return the extra info in global variiables */
14913 /* set defaults in case FEN is incomplete */
14914 board[EP_STATUS] = EP_UNKNOWN;
14915 for(i=0; i<nrCastlingRights; i++ ) {
14916 board[CASTLING][i] =
14917 gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom ? NoRights : initialRights[i];
14918 } /* assume possible unless obviously impossible */
14919 if(initialRights[0]!=NoRights && board[castlingRank[0]][initialRights[0]] != WhiteRook) board[CASTLING][0] = NoRights;
14920 if(initialRights[1]!=NoRights && board[castlingRank[1]][initialRights[1]] != WhiteRook) board[CASTLING][1] = NoRights;
14921 if(initialRights[2]!=NoRights && board[castlingRank[2]][initialRights[2]] != WhiteUnicorn
14922 && board[castlingRank[2]][initialRights[2]] != WhiteKing) board[CASTLING][2] = NoRights;
14923 if(initialRights[3]!=NoRights && board[castlingRank[3]][initialRights[3]] != BlackRook) board[CASTLING][3] = NoRights;
14924 if(initialRights[4]!=NoRights && board[castlingRank[4]][initialRights[4]] != BlackRook) board[CASTLING][4] = NoRights;
14925 if(initialRights[5]!=NoRights && board[castlingRank[5]][initialRights[5]] != BlackUnicorn
14926 && board[castlingRank[5]][initialRights[5]] != BlackKing) board[CASTLING][5] = NoRights;
14929 while(*p==' ') p++;
14930 if(nrCastlingRights) {
14931 if(*p=='K' || *p=='Q' || *p=='k' || *p=='q' || *p=='-') {
14932 /* castling indicator present, so default becomes no castlings */
14933 for(i=0; i<nrCastlingRights; i++ ) {
14934 board[CASTLING][i] = NoRights;
14937 while(*p=='K' || *p=='Q' || *p=='k' || *p=='q' || *p=='-' ||
14938 (gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom) &&
14939 ( *p >= 'a' && *p < 'a' + gameInfo.boardWidth) ||
14940 ( *p >= 'A' && *p < 'A' + gameInfo.boardWidth) ) {
14941 char c = *p++; int whiteKingFile=NoRights, blackKingFile=NoRights;
14943 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) {
14944 if(board[BOARD_HEIGHT-1][i] == BlackKing) blackKingFile = i;
14945 if(board[0 ][i] == WhiteKing) whiteKingFile = i;
14947 if(gameInfo.variant == VariantTwoKings || gameInfo.variant == VariantKnightmate)
14948 whiteKingFile = blackKingFile = BOARD_WIDTH >> 1; // for these variant scanning fails
14949 if(whiteKingFile == NoRights || board[0][whiteKingFile] != WhiteUnicorn
14950 && board[0][whiteKingFile] != WhiteKing) whiteKingFile = NoRights;
14951 if(blackKingFile == NoRights || board[BOARD_HEIGHT-1][blackKingFile] != BlackUnicorn
14952 && board[BOARD_HEIGHT-1][blackKingFile] != BlackKing) blackKingFile = NoRights;
14955 for(i=BOARD_RGHT-1; board[0][i]!=WhiteRook && i>whiteKingFile; i--);
14956 board[CASTLING][0] = i != whiteKingFile ? i : NoRights;
14957 board[CASTLING][2] = whiteKingFile;
14960 for(i=BOARD_LEFT; i<BOARD_RGHT && board[0][i]!=WhiteRook && i<whiteKingFile; i++);
14961 board[CASTLING][1] = i != whiteKingFile ? i : NoRights;
14962 board[CASTLING][2] = whiteKingFile;
14965 for(i=BOARD_RGHT-1; board[BOARD_HEIGHT-1][i]!=BlackRook && i>blackKingFile; i--);
14966 board[CASTLING][3] = i != blackKingFile ? i : NoRights;
14967 board[CASTLING][5] = blackKingFile;
14970 for(i=BOARD_LEFT; i<BOARD_RGHT && board[BOARD_HEIGHT-1][i]!=BlackRook && i<blackKingFile; i++);
14971 board[CASTLING][4] = i != blackKingFile ? i : NoRights;
14972 board[CASTLING][5] = blackKingFile;
14975 default: /* FRC castlings */
14976 if(c >= 'a') { /* black rights */
14977 for(i=BOARD_LEFT; i<BOARD_RGHT; i++)
14978 if(board[BOARD_HEIGHT-1][i] == BlackKing) break;
14979 if(i == BOARD_RGHT) break;
14980 board[CASTLING][5] = i;
14982 if(board[BOARD_HEIGHT-1][c] < BlackPawn ||
14983 board[BOARD_HEIGHT-1][c] >= BlackKing ) break;
14985 board[CASTLING][3] = c;
14987 board[CASTLING][4] = c;
14988 } else { /* white rights */
14989 for(i=BOARD_LEFT; i<BOARD_RGHT; i++)
14990 if(board[0][i] == WhiteKing) break;
14991 if(i == BOARD_RGHT) break;
14992 board[CASTLING][2] = i;
14993 c -= AAA - 'a' + 'A';
14994 if(board[0][c] >= WhiteKing) break;
14996 board[CASTLING][0] = c;
14998 board[CASTLING][1] = c;
15002 for(i=0; i<nrCastlingRights; i++)
15003 if(board[CASTLING][i] != NoRights) initialRights[i] = board[CASTLING][i];
15004 if (appData.debugMode) {
15005 fprintf(debugFP, "FEN castling rights:");
15006 for(i=0; i<nrCastlingRights; i++)
15007 fprintf(debugFP, " %d", board[CASTLING][i]);
15008 fprintf(debugFP, "\n");
15011 while(*p==' ') p++;
15014 /* read e.p. field in games that know e.p. capture */
15015 if(gameInfo.variant != VariantShogi && gameInfo.variant != VariantXiangqi &&
15016 gameInfo.variant != VariantShatranj && gameInfo.variant != VariantCourier && gameInfo.variant != VariantMakruk ) {
15018 p++; board[EP_STATUS] = EP_NONE;
15020 char c = *p++ - AAA;
15022 if(c < BOARD_LEFT || c >= BOARD_RGHT) return TRUE;
15023 if(*p >= '0' && *p <='9') p++;
15024 board[EP_STATUS] = c;
15029 if(sscanf(p, "%d", &i) == 1) {
15030 FENrulePlies = i; /* 50-move ply counter */
15031 /* (The move number is still ignored) */
15038 EditPositionPasteFEN(char *fen)
15041 Board initial_position;
15043 if (!ParseFEN(initial_position, &blackPlaysFirst, fen)) {
15044 DisplayError(_("Bad FEN position in clipboard"), 0);
15047 int savedBlackPlaysFirst = blackPlaysFirst;
15048 EditPositionEvent();
15049 blackPlaysFirst = savedBlackPlaysFirst;
15050 CopyBoard(boards[0], initial_position);
15051 initialRulePlies = FENrulePlies; /* [HGM] copy FEN attributes as well */
15052 EditPositionDone(FALSE); // [HGM] fake: do not fake rights if we had FEN
15053 DisplayBothClocks();
15054 DrawPosition(FALSE, boards[currentMove]);
15059 static char cseq[12] = "\\ ";
15061 Boolean set_cont_sequence(char *new_seq)
15066 // handle bad attempts to set the sequence
15068 return 0; // acceptable error - no debug
15070 len = strlen(new_seq);
15071 ret = (len > 0) && (len < sizeof(cseq));
15073 safeStrCpy(cseq, new_seq, sizeof(cseq)/sizeof(cseq[0]));
15074 else if (appData.debugMode)
15075 fprintf(debugFP, "Invalid continuation sequence \"%s\" (maximum length is: %u)\n", new_seq, (unsigned) sizeof(cseq)-1);
15080 reformat a source message so words don't cross the width boundary. internal
15081 newlines are not removed. returns the wrapped size (no null character unless
15082 included in source message). If dest is NULL, only calculate the size required
15083 for the dest buffer. lp argument indicats line position upon entry, and it's
15084 passed back upon exit.
15086 int wrap(char *dest, char *src, int count, int width, int *lp)
15088 int len, i, ansi, cseq_len, line, old_line, old_i, old_len, clen;
15090 cseq_len = strlen(cseq);
15091 old_line = line = *lp;
15092 ansi = len = clen = 0;
15094 for (i=0; i < count; i++)
15096 if (src[i] == '\033')
15099 // if we hit the width, back up
15100 if (!ansi && (line >= width) && src[i] != '\n' && src[i] != ' ')
15102 // store i & len in case the word is too long
15103 old_i = i, old_len = len;
15105 // find the end of the last word
15106 while (i && src[i] != ' ' && src[i] != '\n')
15112 // word too long? restore i & len before splitting it
15113 if ((old_i-i+clen) >= width)
15120 if (i && src[i-1] == ' ')
15123 if (src[i] != ' ' && src[i] != '\n')
15130 // now append the newline and continuation sequence
15135 strncpy(dest+len, cseq, cseq_len);
15143 dest[len] = src[i];
15147 if (src[i] == '\n')
15152 if (dest && appData.debugMode)
15154 fprintf(debugFP, "wrap(count:%d,width:%d,line:%d,len:%d,*lp:%d,src: ",
15155 count, width, line, len, *lp);
15156 show_bytes(debugFP, src, count);
15157 fprintf(debugFP, "\ndest: ");
15158 show_bytes(debugFP, dest, len);
15159 fprintf(debugFP, "\n");
15161 *lp = dest ? line : old_line;
15166 // [HGM] vari: routines for shelving variations
15169 PushTail(int firstMove, int lastMove)
15171 int i, j, nrMoves = lastMove - firstMove;
15173 if(appData.icsActive) { // only in local mode
15174 forwardMostMove = currentMove; // mimic old ICS behavior
15177 if(storedGames >= MAX_VARIATIONS-1) return;
15179 // push current tail of game on stack
15180 savedResult[storedGames] = gameInfo.result;
15181 savedDetails[storedGames] = gameInfo.resultDetails;
15182 gameInfo.resultDetails = NULL;
15183 savedFirst[storedGames] = firstMove;
15184 savedLast [storedGames] = lastMove;
15185 savedFramePtr[storedGames] = framePtr;
15186 framePtr -= nrMoves; // reserve space for the boards
15187 for(i=nrMoves; i>=1; i--) { // copy boards to stack, working downwards, in case of overlap
15188 CopyBoard(boards[framePtr+i], boards[firstMove+i]);
15189 for(j=0; j<MOVE_LEN; j++)
15190 moveList[framePtr+i][j] = moveList[firstMove+i-1][j];
15191 for(j=0; j<2*MOVE_LEN; j++)
15192 parseList[framePtr+i][j] = parseList[firstMove+i-1][j];
15193 timeRemaining[0][framePtr+i] = timeRemaining[0][firstMove+i];
15194 timeRemaining[1][framePtr+i] = timeRemaining[1][firstMove+i];
15195 pvInfoList[framePtr+i] = pvInfoList[firstMove+i-1];
15196 pvInfoList[firstMove+i-1].depth = 0;
15197 commentList[framePtr+i] = commentList[firstMove+i];
15198 commentList[firstMove+i] = NULL;
15202 forwardMostMove = firstMove; // truncate game so we can start variation
15203 if(storedGames == 1) GreyRevert(FALSE);
15207 PopTail(Boolean annotate)
15210 char buf[8000], moveBuf[20];
15212 if(appData.icsActive) return FALSE; // only in local mode
15213 if(!storedGames) return FALSE; // sanity
15214 CommentPopDown(); // make sure no stale variation comments to the destroyed line can remain open
15217 ToNrEvent(savedFirst[storedGames]); // sets currentMove
15218 nrMoves = savedLast[storedGames] - currentMove;
15221 if(!WhiteOnMove(currentMove))
15222 snprintf(buf, sizeof(buf)/sizeof(buf[0]),"(%d...", (currentMove+2)>>1);
15223 else safeStrCpy(buf, "(", sizeof(buf)/sizeof(buf[0]));
15224 for(i=currentMove; i<forwardMostMove; i++) {
15226 snprintf(moveBuf, sizeof(moveBuf)/sizeof(moveBuf[0]), " %d. %s", (i+2)>>1, SavePart(parseList[i]));
15227 else snprintf(moveBuf, sizeof(moveBuf)/sizeof(moveBuf[0])," %s", SavePart(parseList[i]));
15228 strcat(buf, moveBuf);
15229 if(commentList[i]) { strcat(buf, " "); strcat(buf, commentList[i]); }
15230 if(!--cnt) { strcat(buf, "\n"); cnt = 10; }
15234 for(i=1; i<=nrMoves; i++) { // copy last variation back
15235 CopyBoard(boards[currentMove+i], boards[framePtr+i]);
15236 for(j=0; j<MOVE_LEN; j++)
15237 moveList[currentMove+i-1][j] = moveList[framePtr+i][j];
15238 for(j=0; j<2*MOVE_LEN; j++)
15239 parseList[currentMove+i-1][j] = parseList[framePtr+i][j];
15240 timeRemaining[0][currentMove+i] = timeRemaining[0][framePtr+i];
15241 timeRemaining[1][currentMove+i] = timeRemaining[1][framePtr+i];
15242 pvInfoList[currentMove+i-1] = pvInfoList[framePtr+i];
15243 if(commentList[currentMove+i]) free(commentList[currentMove+i]);
15244 commentList[currentMove+i] = commentList[framePtr+i];
15245 commentList[framePtr+i] = NULL;
15247 if(annotate) AppendComment(currentMove+1, buf, FALSE);
15248 framePtr = savedFramePtr[storedGames];
15249 gameInfo.result = savedResult[storedGames];
15250 if(gameInfo.resultDetails != NULL) {
15251 free(gameInfo.resultDetails);
15253 gameInfo.resultDetails = savedDetails[storedGames];
15254 forwardMostMove = currentMove + nrMoves;
15255 if(storedGames == 0) GreyRevert(TRUE);
15261 { // remove all shelved variations
15263 for(i=0; i<storedGames; i++) {
15264 if(savedDetails[i])
15265 free(savedDetails[i]);
15266 savedDetails[i] = NULL;
15268 for(i=framePtr; i<MAX_MOVES; i++) {
15269 if(commentList[i]) free(commentList[i]);
15270 commentList[i] = NULL;
15272 framePtr = MAX_MOVES-1;
15277 LoadVariation(int index, char *text)
15278 { // [HGM] vari: shelve previous line and load new variation, parsed from text around text[index]
15279 char *p = text, *start = NULL, *end = NULL, wait = NULLCHAR;
15280 int level = 0, move;
15282 if(gameMode != EditGame && gameMode != AnalyzeMode) return;
15283 // first find outermost bracketing variation
15284 while(*p) { // hope I got this right... Non-nesting {} and [] can screen each other and nesting ()
15285 if(!wait) { // while inside [] pr {}, ignore everyting except matching closing ]}
15286 if(*p == '{') wait = '}'; else
15287 if(*p == '[') wait = ']'; else
15288 if(*p == '(' && level++ == 0 && p-text < index) start = p+1;
15289 if(*p == ')' && level > 0 && --level == 0 && p-text > index && end == NULL) end = p-1;
15291 if(*p == wait) wait = NULLCHAR; // closing ]} found
15294 if(!start || !end) return; // no variation found, or syntax error in PGN: ignore click
15295 if(appData.debugMode) fprintf(debugFP, "at move %d load variation '%s'\n", currentMove, start);
15296 end[1] = NULLCHAR; // clip off comment beyond variation
15297 ToNrEvent(currentMove-1);
15298 PushTail(currentMove, forwardMostMove); // shelve main variation. This truncates game
15299 // kludge: use ParsePV() to append variation to game
15300 move = currentMove;
15301 ParsePV(start, TRUE);
15302 forwardMostMove = endPV; endPV = -1; currentMove = move; // cleanup what ParsePV did
15303 ClearPremoveHighlights();
15305 ToNrEvent(currentMove+1);