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)
140 /* A point in time */
142 long sec; /* Assuming this is >= 32 bits */
143 int ms; /* Assuming this is >= 16 bits */
146 int establish P((void));
147 void read_from_player P((InputSourceRef isr, VOIDSTAR closure,
148 char *buf, int count, int error));
149 void read_from_ics P((InputSourceRef isr, VOIDSTAR closure,
150 char *buf, int count, int error));
151 void ics_printf P((char *format, ...));
152 void SendToICS P((char *s));
153 void SendToICSDelayed P((char *s, long msdelay));
154 void SendMoveToICS P((ChessMove moveType, int fromX, int fromY,
156 void HandleMachineMove P((char *message, ChessProgramState *cps));
157 int AutoPlayOneMove P((void));
158 int LoadGameOneMove P((ChessMove readAhead));
159 int LoadGameFromFile P((char *filename, int n, char *title, int useList));
160 int LoadPositionFromFile P((char *filename, int n, char *title));
161 int SavePositionToFile P((char *filename));
162 void ApplyMove P((int fromX, int fromY, int toX, int toY, int promoChar,
164 void MakeMove P((int fromX, int fromY, int toX, int toY, int promoChar));
165 void ShowMove P((int fromX, int fromY, int toX, int toY));
166 int FinishMove P((ChessMove moveType, int fromX, int fromY, int toX, int toY,
167 /*char*/int promoChar));
168 void BackwardInner P((int target));
169 void ForwardInner P((int target));
170 int Adjudicate P((ChessProgramState *cps));
171 void GameEnds P((ChessMove result, char *resultDetails, int whosays));
172 void EditPositionDone P((Boolean fakeRights));
173 void PrintOpponents P((FILE *fp));
174 void PrintPosition P((FILE *fp, int move));
175 void StartChessProgram P((ChessProgramState *cps));
176 void SendToProgram P((char *message, ChessProgramState *cps));
177 void SendMoveToProgram P((int moveNum, ChessProgramState *cps));
178 void ReceiveFromProgram P((InputSourceRef isr, VOIDSTAR closure,
179 char *buf, int count, int error));
180 void SendTimeControl P((ChessProgramState *cps,
181 int mps, long tc, int inc, int sd, int st));
182 char *TimeControlTagValue P((void));
183 void Attention P((ChessProgramState *cps));
184 void FeedMovesToProgram P((ChessProgramState *cps, int upto));
185 void ResurrectChessProgram P((void));
186 void DisplayComment P((int moveNumber, char *text));
187 void DisplayMove P((int moveNumber));
189 void ParseGameHistory P((char *game));
190 void ParseBoard12 P((char *string));
191 void KeepAlive P((void));
192 void StartClocks P((void));
193 void SwitchClocks P((int nr));
194 void StopClocks P((void));
195 void ResetClocks P((void));
196 char *PGNDate P((void));
197 void SetGameInfo P((void));
198 Boolean ParseFEN P((Board board, int *blackPlaysFirst, char *fen));
199 int RegisterMove P((void));
200 void MakeRegisteredMove P((void));
201 void TruncateGame P((void));
202 int looking_at P((char *, int *, char *));
203 void CopyPlayerNameIntoFileName P((char **, char *));
204 char *SavePart P((char *));
205 int SaveGameOldStyle P((FILE *));
206 int SaveGamePGN P((FILE *));
207 void GetTimeMark P((TimeMark *));
208 long SubtractTimeMarks P((TimeMark *, TimeMark *));
209 int CheckFlags P((void));
210 long NextTickLength P((long));
211 void CheckTimeControl P((void));
212 void show_bytes P((FILE *, char *, int));
213 int string_to_rating P((char *str));
214 void ParseFeatures P((char* args, ChessProgramState *cps));
215 void InitBackEnd3 P((void));
216 void FeatureDone P((ChessProgramState* cps, int val));
217 void InitChessProgram P((ChessProgramState *cps, int setup));
218 void OutputKibitz(int window, char *text);
219 int PerpetualChase(int first, int last);
220 int EngineOutputIsUp();
221 void InitDrawingSizes(int x, int y);
224 extern void ConsoleCreate();
227 ChessProgramState *WhitePlayer();
228 void InsertIntoMemo P((int which, char *text)); // [HGM] kibitz: in engineo.c
229 int VerifyDisplayMode P(());
231 char *GetInfoFromComment( int, char * ); // [HGM] PV time: returns stripped comment
232 void InitEngineUCI( const char * iniDir, ChessProgramState * cps ); // [HGM] moved here from winboard.c
233 char *ProbeBook P((int moveNr, char *book)); // [HGM] book: returns a book move
234 char *SendMoveToBookUser P((int nr, ChessProgramState *cps, int initial)); // [HGM] book
235 void ics_update_width P((int new_width));
236 extern char installDir[MSG_SIZ];
238 extern int tinyLayout, smallLayout;
239 ChessProgramStats programStats;
240 char lastPV[2][2*MSG_SIZ]; /* [HGM] pv: last PV in thinking output of each engine */
242 static int exiting = 0; /* [HGM] moved to top */
243 static int setboardSpoiledMachineBlack = 0 /*, errorExitFlag = 0*/;
244 int startedFromPositionFile = FALSE; Board filePosition; /* [HGM] loadPos */
245 Board partnerBoard; /* [HGM] bughouse: for peeking at partner game */
247 char endingGame = 0; /* [HGM] crash: flag to prevent recursion of GameEnds() */
248 int whiteNPS, blackNPS; /* [HGM] nps: for easily making clocks aware of NPS */
249 VariantClass currentlyInitializedVariant; /* [HGM] variantswitch */
250 int lastIndex = 0; /* [HGM] autoinc: last game/position used in match mode */
251 Boolean connectionAlive;/* [HGM] alive: ICS connection status from probing */
252 int opponentKibitzes;
253 int lastSavedGame; /* [HGM] save: ID of game */
254 char chatPartner[MAX_CHAT][MSG_SIZ]; /* [HGM] chat: list of chatting partners */
255 extern int chatCount;
257 char marker[BOARD_RANKS][BOARD_FILES]; /* [HGM] marks for target squares */
259 /* States for ics_getting_history */
261 #define H_REQUESTED 1
262 #define H_GOT_REQ_HEADER 2
263 #define H_GOT_UNREQ_HEADER 3
264 #define H_GETTING_MOVES 4
265 #define H_GOT_UNWANTED_HEADER 5
267 /* whosays values for GameEnds */
276 /* Maximum number of games in a cmail message */
277 #define CMAIL_MAX_GAMES 20
279 /* Different types of move when calling RegisterMove */
281 #define CMAIL_RESIGN 1
283 #define CMAIL_ACCEPT 3
285 /* Different types of result to remember for each game */
286 #define CMAIL_NOT_RESULT 0
287 #define CMAIL_OLD_RESULT 1
288 #define CMAIL_NEW_RESULT 2
290 /* Telnet protocol constants */
301 static char * safeStrCpy( char * dst, const char * src, size_t count )
303 assert( dst != NULL );
304 assert( src != NULL );
307 strncpy( dst, src, count );
308 dst[ count-1 ] = '\0';
312 /* Some compiler can't cast u64 to double
313 * This function do the job for us:
315 * We use the highest bit for cast, this only
316 * works if the highest bit is not
317 * in use (This should not happen)
319 * We used this for all compiler
322 u64ToDouble(u64 value)
325 u64 tmp = value & u64Const(0x7fffffffffffffff);
326 r = (double)(s64)tmp;
327 if (value & u64Const(0x8000000000000000))
328 r += 9.2233720368547758080e18; /* 2^63 */
332 /* Fake up flags for now, as we aren't keeping track of castling
333 availability yet. [HGM] Change of logic: the flag now only
334 indicates the type of castlings allowed by the rule of the game.
335 The actual rights themselves are maintained in the array
336 castlingRights, as part of the game history, and are not probed
342 int flags = F_ALL_CASTLE_OK;
343 if ((index % 2) == 0) flags |= F_WHITE_ON_MOVE;
344 switch (gameInfo.variant) {
346 flags &= ~F_ALL_CASTLE_OK;
347 case VariantGiveaway: // [HGM] moved this case label one down: seems Giveaway does have castling on ICC!
348 flags |= F_IGNORE_CHECK;
350 flags |= F_MANDATORY_CAPTURE; //[HGM] losers: sets flag so TestLegality rejects non-capts if capts exist
353 flags |= F_IGNORE_CHECK | F_ATOMIC_CAPTURE;
355 case VariantKriegspiel:
356 flags |= F_KRIEGSPIEL_CAPTURE;
358 case VariantCapaRandom:
359 case VariantFischeRandom:
360 flags |= F_FRC_TYPE_CASTLING; /* [HGM] enable this through flag */
361 case VariantNoCastle:
362 case VariantShatranj:
365 flags &= ~F_ALL_CASTLE_OK;
373 FILE *gameFileFP, *debugFP;
376 [AS] Note: sometimes, the sscanf() function is used to parse the input
377 into a fixed-size buffer. Because of this, we must be prepared to
378 receive strings as long as the size of the input buffer, which is currently
379 set to 4K for Windows and 8K for the rest.
380 So, we must either allocate sufficiently large buffers here, or
381 reduce the size of the input buffer in the input reading part.
384 char cmailMove[CMAIL_MAX_GAMES][MOVE_LEN], cmailMsg[MSG_SIZ];
385 char bookOutput[MSG_SIZ*10], thinkOutput[MSG_SIZ*10], lastHint[MSG_SIZ];
386 char thinkOutput1[MSG_SIZ*10];
388 ChessProgramState first, second;
390 /* premove variables */
393 int premoveFromX = 0;
394 int premoveFromY = 0;
395 int premovePromoChar = 0;
397 Boolean alarmSounded;
398 /* end premove variables */
400 char *ics_prefix = "$";
401 int ics_type = ICS_GENERIC;
403 int currentMove = 0, forwardMostMove = 0, backwardMostMove = 0;
404 int pauseExamForwardMostMove = 0;
405 int nCmailGames = 0, nCmailResults = 0, nCmailMovesRegistered = 0;
406 int cmailMoveRegistered[CMAIL_MAX_GAMES], cmailResult[CMAIL_MAX_GAMES];
407 int cmailMsgLoaded = FALSE, cmailMailedMove = FALSE;
408 int cmailOldMove = -1, firstMove = TRUE, flipView = FALSE;
409 int blackPlaysFirst = FALSE, startedFromSetupPosition = FALSE;
410 int searchTime = 0, pausing = FALSE, pauseExamInvalid = FALSE;
411 int whiteFlag = FALSE, blackFlag = FALSE;
412 int userOfferedDraw = FALSE;
413 int ics_user_moved = 0, ics_gamenum = -1, ics_getting_history = H_FALSE;
414 int matchMode = FALSE, hintRequested = FALSE, bookRequested = FALSE;
415 int cmailMoveType[CMAIL_MAX_GAMES];
416 long ics_clock_paused = 0;
417 ProcRef icsPR = NoProc, cmailPR = NoProc;
418 InputSourceRef telnetISR = NULL, fromUserISR = NULL, cmailISR = NULL;
419 GameMode gameMode = BeginningOfGame;
420 char moveList[MAX_MOVES][MOVE_LEN], parseList[MAX_MOVES][MOVE_LEN * 2];
421 char *commentList[MAX_MOVES], *cmailCommentList[CMAIL_MAX_GAMES];
422 ChessProgramStats_Move pvInfoList[MAX_MOVES]; /* [AS] Info about engine thinking */
423 int hiddenThinkOutputState = 0; /* [AS] */
424 int adjudicateLossThreshold = 0; /* [AS] Automatic adjudication */
425 int adjudicateLossPlies = 6;
426 char white_holding[64], black_holding[64];
427 TimeMark lastNodeCountTime;
428 long lastNodeCount=0;
429 int have_sent_ICS_logon = 0;
431 long whiteTimeRemaining, blackTimeRemaining, timeControl, timeIncrement;
432 long timeControl_2; /* [AS] Allow separate time controls */
433 char *fullTimeControlString = NULL; /* [HGM] secondary TC: merge of MPS, TC and inc */
434 long timeRemaining[2][MAX_MOVES];
436 TimeMark programStartTime;
437 char ics_handle[MSG_SIZ];
438 int have_set_title = 0;
440 /* animateTraining preserves the state of appData.animate
441 * when Training mode is activated. This allows the
442 * response to be animated when appData.animate == TRUE and
443 * appData.animateDragging == TRUE.
445 Boolean animateTraining;
451 Board boards[MAX_MOVES];
452 /* [HGM] Following 7 needed for accurate legality tests: */
453 signed char castlingRank[BOARD_FILES]; // and corresponding ranks
454 signed char initialRights[BOARD_FILES];
455 int nrCastlingRights; // For TwoKings, or to implement castling-unknown status
456 int initialRulePlies, FENrulePlies;
457 FILE *serverMoves = NULL; // next two for broadcasting (/serverMoves option)
460 int mute; // mute all sounds
462 // [HGM] vari: next 12 to save and restore variations
463 #define MAX_VARIATIONS 10
464 int framePtr = MAX_MOVES-1; // points to free stack entry
466 int savedFirst[MAX_VARIATIONS];
467 int savedLast[MAX_VARIATIONS];
468 int savedFramePtr[MAX_VARIATIONS];
469 char *savedDetails[MAX_VARIATIONS];
470 ChessMove savedResult[MAX_VARIATIONS];
472 void PushTail P((int firstMove, int lastMove));
473 Boolean PopTail P((Boolean annotate));
474 void CleanupTail P((void));
476 ChessSquare FIDEArray[2][BOARD_FILES] = {
477 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
478 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
479 { BlackRook, BlackKnight, BlackBishop, BlackQueen,
480 BlackKing, BlackBishop, BlackKnight, BlackRook }
483 ChessSquare twoKingsArray[2][BOARD_FILES] = {
484 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
485 WhiteKing, WhiteKing, WhiteKnight, WhiteRook },
486 { BlackRook, BlackKnight, BlackBishop, BlackQueen,
487 BlackKing, BlackKing, BlackKnight, BlackRook }
490 ChessSquare KnightmateArray[2][BOARD_FILES] = {
491 { WhiteRook, WhiteMan, WhiteBishop, WhiteQueen,
492 WhiteUnicorn, WhiteBishop, WhiteMan, WhiteRook },
493 { BlackRook, BlackMan, BlackBishop, BlackQueen,
494 BlackUnicorn, BlackBishop, BlackMan, BlackRook }
497 ChessSquare fairyArray[2][BOARD_FILES] = { /* [HGM] Queen side differs from King side */
498 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
499 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
500 { BlackLance, BlackAlfil, BlackMarshall, BlackAngel,
501 BlackKing, BlackMarshall, BlackAlfil, BlackLance }
504 ChessSquare ShatranjArray[2][BOARD_FILES] = { /* [HGM] (movGen knows about Shatranj Q and P) */
505 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteKing,
506 WhiteFerz, WhiteAlfil, WhiteKnight, WhiteRook },
507 { BlackRook, BlackKnight, BlackAlfil, BlackKing,
508 BlackFerz, BlackAlfil, BlackKnight, BlackRook }
511 ChessSquare makrukArray[2][BOARD_FILES] = { /* [HGM] (movGen knows about Shatranj Q and P) */
512 { WhiteRook, WhiteKnight, WhiteMan, WhiteKing,
513 WhiteFerz, WhiteMan, WhiteKnight, WhiteRook },
514 { BlackRook, BlackKnight, BlackMan, BlackFerz,
515 BlackKing, BlackMan, BlackKnight, BlackRook }
519 #if (BOARD_FILES>=10)
520 ChessSquare ShogiArray[2][BOARD_FILES] = {
521 { WhiteQueen, WhiteKnight, WhiteFerz, WhiteWazir,
522 WhiteKing, WhiteWazir, WhiteFerz, WhiteKnight, WhiteQueen },
523 { BlackQueen, BlackKnight, BlackFerz, BlackWazir,
524 BlackKing, BlackWazir, BlackFerz, BlackKnight, BlackQueen }
527 ChessSquare XiangqiArray[2][BOARD_FILES] = {
528 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteFerz,
529 WhiteWazir, WhiteFerz, WhiteAlfil, WhiteKnight, WhiteRook },
530 { BlackRook, BlackKnight, BlackAlfil, BlackFerz,
531 BlackWazir, BlackFerz, BlackAlfil, BlackKnight, BlackRook }
534 ChessSquare CapablancaArray[2][BOARD_FILES] = {
535 { WhiteRook, WhiteKnight, WhiteAngel, WhiteBishop, WhiteQueen,
536 WhiteKing, WhiteBishop, WhiteMarshall, WhiteKnight, WhiteRook },
537 { BlackRook, BlackKnight, BlackAngel, BlackBishop, BlackQueen,
538 BlackKing, BlackBishop, BlackMarshall, BlackKnight, BlackRook }
541 ChessSquare GreatArray[2][BOARD_FILES] = {
542 { WhiteDragon, WhiteKnight, WhiteAlfil, WhiteGrasshopper, WhiteKing,
543 WhiteSilver, WhiteCardinal, WhiteAlfil, WhiteKnight, WhiteDragon },
544 { BlackDragon, BlackKnight, BlackAlfil, BlackGrasshopper, BlackKing,
545 BlackSilver, BlackCardinal, BlackAlfil, BlackKnight, BlackDragon },
548 ChessSquare JanusArray[2][BOARD_FILES] = {
549 { WhiteRook, WhiteAngel, WhiteKnight, WhiteBishop, WhiteKing,
550 WhiteQueen, WhiteBishop, WhiteKnight, WhiteAngel, WhiteRook },
551 { BlackRook, BlackAngel, BlackKnight, BlackBishop, BlackKing,
552 BlackQueen, BlackBishop, BlackKnight, BlackAngel, BlackRook }
556 ChessSquare GothicArray[2][BOARD_FILES] = {
557 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen, WhiteMarshall,
558 WhiteKing, WhiteAngel, WhiteBishop, WhiteKnight, WhiteRook },
559 { BlackRook, BlackKnight, BlackBishop, BlackQueen, BlackMarshall,
560 BlackKing, BlackAngel, BlackBishop, BlackKnight, BlackRook }
563 #define GothicArray CapablancaArray
567 ChessSquare FalconArray[2][BOARD_FILES] = {
568 { WhiteRook, WhiteKnight, WhiteBishop, WhiteLance, WhiteQueen,
569 WhiteKing, WhiteLance, WhiteBishop, WhiteKnight, WhiteRook },
570 { BlackRook, BlackKnight, BlackBishop, BlackLance, BlackQueen,
571 BlackKing, BlackLance, BlackBishop, BlackKnight, BlackRook }
574 #define FalconArray CapablancaArray
577 #else // !(BOARD_FILES>=10)
578 #define XiangqiPosition FIDEArray
579 #define CapablancaArray FIDEArray
580 #define GothicArray FIDEArray
581 #define GreatArray FIDEArray
582 #endif // !(BOARD_FILES>=10)
584 #if (BOARD_FILES>=12)
585 ChessSquare CourierArray[2][BOARD_FILES] = {
586 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteBishop, WhiteMan, WhiteKing,
587 WhiteFerz, WhiteWazir, WhiteBishop, WhiteAlfil, WhiteKnight, WhiteRook },
588 { BlackRook, BlackKnight, BlackAlfil, BlackBishop, BlackMan, BlackKing,
589 BlackFerz, BlackWazir, BlackBishop, BlackAlfil, BlackKnight, BlackRook }
591 #else // !(BOARD_FILES>=12)
592 #define CourierArray CapablancaArray
593 #endif // !(BOARD_FILES>=12)
596 Board initialPosition;
599 /* Convert str to a rating. Checks for special cases of "----",
601 "++++", etc. Also strips ()'s */
603 string_to_rating(str)
606 while(*str && !isdigit(*str)) ++str;
608 return 0; /* One of the special "no rating" cases */
616 /* Init programStats */
617 programStats.movelist[0] = 0;
618 programStats.depth = 0;
619 programStats.nr_moves = 0;
620 programStats.moves_left = 0;
621 programStats.nodes = 0;
622 programStats.time = -1; // [HGM] PGNtime: make invalid to recognize engine output
623 programStats.score = 0;
624 programStats.got_only_move = 0;
625 programStats.got_fail = 0;
626 programStats.line_is_book = 0;
632 int matched, min, sec;
634 ShowThinkingEvent(); // [HGM] thinking: make sure post/nopost state is set according to options
636 GetTimeMark(&programStartTime);
637 srandom((programStartTime.ms + 1000*programStartTime.sec)*0x1001001); // [HGM] book: makes sure random is unpredictabe to msec level
640 programStats.ok_to_send = 1;
641 programStats.seen_stat = 0;
644 * Initialize game list
650 * Internet chess server status
652 if (appData.icsActive) {
653 appData.matchMode = FALSE;
654 appData.matchGames = 0;
656 appData.noChessProgram = !appData.zippyPlay;
658 appData.zippyPlay = FALSE;
659 appData.zippyTalk = FALSE;
660 appData.noChessProgram = TRUE;
662 if (*appData.icsHelper != NULLCHAR) {
663 appData.useTelnet = TRUE;
664 appData.telnetProgram = appData.icsHelper;
667 appData.zippyTalk = appData.zippyPlay = FALSE;
670 /* [AS] Initialize pv info list [HGM] and game state */
674 for( i=0; i<=framePtr; i++ ) {
675 pvInfoList[i].depth = -1;
676 boards[i][EP_STATUS] = EP_NONE;
677 for( j=0; j<BOARD_FILES-2; j++ ) boards[i][CASTLING][j] = NoRights;
682 * Parse timeControl resource
684 if (!ParseTimeControl(appData.timeControl, appData.timeIncrement,
685 appData.movesPerSession)) {
687 snprintf(buf, sizeof(buf), _("bad timeControl option %s"), appData.timeControl);
688 DisplayFatalError(buf, 0, 2);
692 * Parse searchTime resource
694 if (*appData.searchTime != NULLCHAR) {
695 matched = sscanf(appData.searchTime, "%d:%d", &min, &sec);
697 searchTime = min * 60;
698 } else if (matched == 2) {
699 searchTime = min * 60 + sec;
702 snprintf(buf, sizeof(buf), _("bad searchTime option %s"), appData.searchTime);
703 DisplayFatalError(buf, 0, 2);
707 /* [AS] Adjudication threshold */
708 adjudicateLossThreshold = appData.adjudicateLossThreshold;
710 first.which = "first";
711 second.which = "second";
712 first.maybeThinking = second.maybeThinking = FALSE;
713 first.pr = second.pr = NoProc;
714 first.isr = second.isr = NULL;
715 first.sendTime = second.sendTime = 2;
716 first.sendDrawOffers = 1;
717 if (appData.firstPlaysBlack) {
718 first.twoMachinesColor = "black\n";
719 second.twoMachinesColor = "white\n";
721 first.twoMachinesColor = "white\n";
722 second.twoMachinesColor = "black\n";
724 first.program = appData.firstChessProgram;
725 second.program = appData.secondChessProgram;
726 first.host = appData.firstHost;
727 second.host = appData.secondHost;
728 first.dir = appData.firstDirectory;
729 second.dir = appData.secondDirectory;
730 first.other = &second;
731 second.other = &first;
732 first.initString = appData.initString;
733 second.initString = appData.secondInitString;
734 first.computerString = appData.firstComputerString;
735 second.computerString = appData.secondComputerString;
736 first.useSigint = second.useSigint = TRUE;
737 first.useSigterm = second.useSigterm = TRUE;
738 first.reuse = appData.reuseFirst;
739 second.reuse = appData.reuseSecond;
740 first.nps = appData.firstNPS; // [HGM] nps: copy nodes per second
741 second.nps = appData.secondNPS;
742 first.useSetboard = second.useSetboard = FALSE;
743 first.useSAN = second.useSAN = FALSE;
744 first.usePing = second.usePing = FALSE;
745 first.lastPing = second.lastPing = 0;
746 first.lastPong = second.lastPong = 0;
747 first.usePlayother = second.usePlayother = FALSE;
748 first.useColors = second.useColors = TRUE;
749 first.useUsermove = second.useUsermove = FALSE;
750 first.sendICS = second.sendICS = FALSE;
751 first.sendName = second.sendName = appData.icsActive;
752 first.sdKludge = second.sdKludge = FALSE;
753 first.stKludge = second.stKludge = FALSE;
754 TidyProgramName(first.program, first.host, first.tidy);
755 TidyProgramName(second.program, second.host, second.tidy);
756 first.matchWins = second.matchWins = 0;
757 strcpy(first.variants, appData.variant);
758 strcpy(second.variants, appData.variant);
759 first.analysisSupport = second.analysisSupport = 2; /* detect */
760 first.analyzing = second.analyzing = FALSE;
761 first.initDone = second.initDone = FALSE;
763 /* New features added by Tord: */
764 first.useFEN960 = FALSE; second.useFEN960 = FALSE;
765 first.useOOCastle = TRUE; second.useOOCastle = TRUE;
766 /* End of new features added by Tord. */
767 first.fenOverride = appData.fenOverride1;
768 second.fenOverride = appData.fenOverride2;
770 /* [HGM] time odds: set factor for each machine */
771 first.timeOdds = appData.firstTimeOdds;
772 second.timeOdds = appData.secondTimeOdds;
774 if(appData.timeOddsMode) {
775 norm = first.timeOdds;
776 if(norm > second.timeOdds) norm = second.timeOdds;
778 first.timeOdds /= norm;
779 second.timeOdds /= norm;
782 /* [HGM] secondary TC: how to handle sessions that do not fit in 'level'*/
783 first.accumulateTC = appData.firstAccumulateTC;
784 second.accumulateTC = appData.secondAccumulateTC;
785 first.maxNrOfSessions = second.maxNrOfSessions = 1;
788 first.debug = second.debug = FALSE;
789 first.supportsNPS = second.supportsNPS = UNKNOWN;
792 first.optionSettings = appData.firstOptions;
793 second.optionSettings = appData.secondOptions;
795 first.scoreIsAbsolute = appData.firstScoreIsAbsolute; /* [AS] */
796 second.scoreIsAbsolute = appData.secondScoreIsAbsolute; /* [AS] */
797 first.isUCI = appData.firstIsUCI; /* [AS] */
798 second.isUCI = appData.secondIsUCI; /* [AS] */
799 first.hasOwnBookUCI = appData.firstHasOwnBookUCI; /* [AS] */
800 second.hasOwnBookUCI = appData.secondHasOwnBookUCI; /* [AS] */
802 if (appData.firstProtocolVersion > PROTOVER ||
803 appData.firstProtocolVersion < 1) {
805 sprintf(buf, _("protocol version %d not supported"),
806 appData.firstProtocolVersion);
807 DisplayFatalError(buf, 0, 2);
809 first.protocolVersion = appData.firstProtocolVersion;
812 if (appData.secondProtocolVersion > PROTOVER ||
813 appData.secondProtocolVersion < 1) {
815 sprintf(buf, _("protocol version %d not supported"),
816 appData.secondProtocolVersion);
817 DisplayFatalError(buf, 0, 2);
819 second.protocolVersion = appData.secondProtocolVersion;
822 if (appData.icsActive) {
823 appData.clockMode = TRUE; /* changes dynamically in ICS mode */
824 // } else if (*appData.searchTime != NULLCHAR || appData.noChessProgram) {
825 } else if (appData.noChessProgram) { // [HGM] st: searchTime mode now also is clockMode
826 appData.clockMode = FALSE;
827 first.sendTime = second.sendTime = 0;
831 /* Override some settings from environment variables, for backward
832 compatibility. Unfortunately it's not feasible to have the env
833 vars just set defaults, at least in xboard. Ugh.
835 if (appData.icsActive && (appData.zippyPlay || appData.zippyTalk)) {
840 if (appData.noChessProgram) {
841 programVersion = (char*) malloc(5 + strlen(PACKAGE_STRING));
842 sprintf(programVersion, "%s", PACKAGE_STRING);
844 /* [HGM] tidy: use tidy name, in stead of full pathname (which was probably a bug due to / vs \ ) */
845 programVersion = (char*) malloc(8 + strlen(PACKAGE_STRING) + strlen(first.tidy));
846 sprintf(programVersion, "%s + %s", PACKAGE_STRING, first.tidy);
849 if (!appData.icsActive) {
851 /* Check for variants that are supported only in ICS mode,
852 or not at all. Some that are accepted here nevertheless
853 have bugs; see comments below.
855 VariantClass variant = StringToVariant(appData.variant);
857 case VariantBughouse: /* need four players and two boards */
858 case VariantKriegspiel: /* need to hide pieces and move details */
859 /* case VariantFischeRandom: (Fabien: moved below) */
860 sprintf(buf, _("Variant %s supported only in ICS mode"), appData.variant);
861 DisplayFatalError(buf, 0, 2);
865 case VariantLoadable:
875 sprintf(buf, _("Unknown variant name %s"), appData.variant);
876 DisplayFatalError(buf, 0, 2);
879 case VariantXiangqi: /* [HGM] repetition rules not implemented */
880 case VariantFairy: /* [HGM] TestLegality definitely off! */
881 case VariantGothic: /* [HGM] should work */
882 case VariantCapablanca: /* [HGM] should work */
883 case VariantCourier: /* [HGM] initial forced moves not implemented */
884 case VariantShogi: /* [HGM] drops not tested for legality */
885 case VariantKnightmate: /* [HGM] should work */
886 case VariantCylinder: /* [HGM] untested */
887 case VariantFalcon: /* [HGM] untested */
888 case VariantCrazyhouse: /* holdings not shown, ([HGM] fixed that!)
889 offboard interposition not understood */
890 case VariantNormal: /* definitely works! */
891 case VariantWildCastle: /* pieces not automatically shuffled */
892 case VariantNoCastle: /* pieces not automatically shuffled */
893 case VariantFischeRandom: /* [HGM] works and shuffles pieces */
894 case VariantLosers: /* should work except for win condition,
895 and doesn't know captures are mandatory */
896 case VariantSuicide: /* should work except for win condition,
897 and doesn't know captures are mandatory */
898 case VariantGiveaway: /* should work except for win condition,
899 and doesn't know captures are mandatory */
900 case VariantTwoKings: /* should work */
901 case VariantAtomic: /* should work except for win condition */
902 case Variant3Check: /* should work except for win condition */
903 case VariantShatranj: /* should work except for all win conditions */
904 case VariantMakruk: /* should work except for daw countdown */
905 case VariantBerolina: /* might work if TestLegality is off */
906 case VariantCapaRandom: /* should work */
907 case VariantJanus: /* should work */
908 case VariantSuper: /* experimental */
909 case VariantGreat: /* experimental, requires legality testing to be off */
914 InitEngineUCI( installDir, &first ); // [HGM] moved here from winboard.c, to make available in xboard
915 InitEngineUCI( installDir, &second );
918 int NextIntegerFromString( char ** str, long * value )
923 while( *s == ' ' || *s == '\t' ) {
929 if( *s >= '0' && *s <= '9' ) {
930 while( *s >= '0' && *s <= '9' ) {
931 *value = *value * 10 + (*s - '0');
943 int NextTimeControlFromString( char ** str, long * value )
946 int result = NextIntegerFromString( str, &temp );
949 *value = temp * 60; /* Minutes */
952 result = NextIntegerFromString( str, &temp );
953 *value += temp; /* Seconds */
960 int NextSessionFromString( char ** str, int *moves, long * tc, long *inc)
961 { /* [HGM] routine added to read '+moves/time' for secondary time control */
962 int result = -1; long temp, temp2;
964 if(**str != '+') return -1; // old params remain in force!
966 if( NextTimeControlFromString( str, &temp ) ) return -1;
969 /* time only: incremental or sudden-death time control */
970 if(**str == '+') { /* increment follows; read it */
972 if(result = NextIntegerFromString( str, &temp2)) return -1;
975 *moves = 0; *tc = temp * 1000;
977 } else if(temp % 60 != 0) return -1; /* moves was given as min:sec */
979 (*str)++; /* classical time control */
980 result = NextTimeControlFromString( str, &temp2);
989 int GetTimeQuota(int movenr)
990 { /* [HGM] get time to add from the multi-session time-control string */
991 int moves=1; /* kludge to force reading of first session */
992 long time, increment;
993 char *s = fullTimeControlString;
995 if(appData.debugMode) fprintf(debugFP, "TC string = '%s'\n", fullTimeControlString);
997 if(moves) NextSessionFromString(&s, &moves, &time, &increment);
998 if(appData.debugMode) fprintf(debugFP, "mps=%d tc=%d inc=%d\n", moves, (int) time, (int) increment);
999 if(movenr == -1) return time; /* last move before new session */
1000 if(!moves) return increment; /* current session is incremental */
1001 if(movenr >= 0) movenr -= moves; /* we already finished this session */
1002 } while(movenr >= -1); /* try again for next session */
1004 return 0; // no new time quota on this move
1008 ParseTimeControl(tc, ti, mps)
1017 if(ti >= 0 && !strchr(tc, '+') && !strchr(tc, '/') ) mps = 0;
1020 sprintf(buf, "+%d/%s+%d", mps, tc, ti);
1021 else sprintf(buf, "+%s+%d", tc, ti);
1024 sprintf(buf, "+%d/%s", mps, tc);
1025 else sprintf(buf, "+%s", tc);
1027 fullTimeControlString = StrSave(buf);
1029 if( NextTimeControlFromString( &tc, &tc1 ) != 0 ) {
1034 /* Parse second time control */
1037 if( NextTimeControlFromString( &tc, &tc2 ) != 0 ) {
1045 timeControl_2 = tc2 * 1000;
1055 timeControl = tc1 * 1000;
1058 timeIncrement = ti * 1000; /* convert to ms */
1059 movesPerSession = 0;
1062 movesPerSession = mps;
1070 if (appData.debugMode) {
1071 fprintf(debugFP, "%s\n", programVersion);
1074 set_cont_sequence(appData.wrapContSeq);
1075 if (appData.matchGames > 0) {
1076 appData.matchMode = TRUE;
1077 } else if (appData.matchMode) {
1078 appData.matchGames = 1;
1080 if(appData.matchMode && appData.sameColorGames > 0) /* [HGM] alternate: overrule matchGames */
1081 appData.matchGames = appData.sameColorGames;
1082 if(appData.rewindIndex > 1) { /* [HGM] autoinc: rewind implies auto-increment and overrules given index */
1083 if(appData.loadPositionIndex >= 0) appData.loadPositionIndex = -1;
1084 if(appData.loadGameIndex >= 0) appData.loadGameIndex = -1;
1087 if (appData.noChessProgram || first.protocolVersion == 1) {
1090 /* kludge: allow timeout for initial "feature" commands */
1092 DisplayMessage("", _("Starting chess program"));
1093 ScheduleDelayedEvent(InitBackEnd3, FEATURE_TIMEOUT);
1098 InitBackEnd3 P((void))
1100 GameMode initialMode;
1104 InitChessProgram(&first, startedFromSetupPosition);
1107 if (appData.icsActive) {
1109 /* [DM] Make a console window if needed [HGM] merged ifs */
1114 if (*appData.icsCommPort != NULLCHAR) {
1115 sprintf(buf, _("Could not open comm port %s"),
1116 appData.icsCommPort);
1118 snprintf(buf, sizeof(buf), _("Could not connect to host %s, port %s"),
1119 appData.icsHost, appData.icsPort);
1121 DisplayFatalError(buf, err, 1);
1126 AddInputSource(icsPR, FALSE, read_from_ics, &telnetISR);
1128 AddInputSource(NoProc, FALSE, read_from_player, &fromUserISR);
1129 if(appData.keepAlive) // [HGM] alive: schedule sending of dummy 'date' command
1130 ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
1131 } else if (appData.noChessProgram) {
1137 if (*appData.cmailGameName != NULLCHAR) {
1139 OpenLoopback(&cmailPR);
1141 AddInputSource(cmailPR, FALSE, CmailSigHandlerCallBack, &cmailISR);
1145 DisplayMessage("", "");
1146 if (StrCaseCmp(appData.initialMode, "") == 0) {
1147 initialMode = BeginningOfGame;
1148 } else if (StrCaseCmp(appData.initialMode, "TwoMachines") == 0) {
1149 initialMode = TwoMachinesPlay;
1150 } else if (StrCaseCmp(appData.initialMode, "AnalyzeFile") == 0) {
1151 initialMode = AnalyzeFile;
1152 } else if (StrCaseCmp(appData.initialMode, "Analysis") == 0) {
1153 initialMode = AnalyzeMode;
1154 } else if (StrCaseCmp(appData.initialMode, "MachineWhite") == 0) {
1155 initialMode = MachinePlaysWhite;
1156 } else if (StrCaseCmp(appData.initialMode, "MachineBlack") == 0) {
1157 initialMode = MachinePlaysBlack;
1158 } else if (StrCaseCmp(appData.initialMode, "EditGame") == 0) {
1159 initialMode = EditGame;
1160 } else if (StrCaseCmp(appData.initialMode, "EditPosition") == 0) {
1161 initialMode = EditPosition;
1162 } else if (StrCaseCmp(appData.initialMode, "Training") == 0) {
1163 initialMode = Training;
1165 sprintf(buf, _("Unknown initialMode %s"), appData.initialMode);
1166 DisplayFatalError(buf, 0, 2);
1170 if (appData.matchMode) {
1171 /* Set up machine vs. machine match */
1172 if (appData.noChessProgram) {
1173 DisplayFatalError(_("Can't have a match with no chess programs"),
1179 if (*appData.loadGameFile != NULLCHAR) {
1180 int index = appData.loadGameIndex; // [HGM] autoinc
1181 if(index<0) lastIndex = index = 1;
1182 if (!LoadGameFromFile(appData.loadGameFile,
1184 appData.loadGameFile, FALSE)) {
1185 DisplayFatalError(_("Bad game file"), 0, 1);
1188 } else if (*appData.loadPositionFile != NULLCHAR) {
1189 int index = appData.loadPositionIndex; // [HGM] autoinc
1190 if(index<0) lastIndex = index = 1;
1191 if (!LoadPositionFromFile(appData.loadPositionFile,
1193 appData.loadPositionFile)) {
1194 DisplayFatalError(_("Bad position file"), 0, 1);
1199 } else if (*appData.cmailGameName != NULLCHAR) {
1200 /* Set up cmail mode */
1201 ReloadCmailMsgEvent(TRUE);
1203 /* Set up other modes */
1204 if (initialMode == AnalyzeFile) {
1205 if (*appData.loadGameFile == NULLCHAR) {
1206 DisplayFatalError(_("AnalyzeFile mode requires a game file"), 0, 1);
1210 if (*appData.loadGameFile != NULLCHAR) {
1211 (void) LoadGameFromFile(appData.loadGameFile,
1212 appData.loadGameIndex,
1213 appData.loadGameFile, TRUE);
1214 } else if (*appData.loadPositionFile != NULLCHAR) {
1215 (void) LoadPositionFromFile(appData.loadPositionFile,
1216 appData.loadPositionIndex,
1217 appData.loadPositionFile);
1218 /* [HGM] try to make self-starting even after FEN load */
1219 /* to allow automatic setup of fairy variants with wtm */
1220 if(initialMode == BeginningOfGame && !blackPlaysFirst) {
1221 gameMode = BeginningOfGame;
1222 setboardSpoiledMachineBlack = 1;
1224 /* [HGM] loadPos: make that every new game uses the setup */
1225 /* from file as long as we do not switch variant */
1226 if(!blackPlaysFirst) {
1227 startedFromPositionFile = TRUE;
1228 CopyBoard(filePosition, boards[0]);
1231 if (initialMode == AnalyzeMode) {
1232 if (appData.noChessProgram) {
1233 DisplayFatalError(_("Analysis mode requires a chess engine"), 0, 2);
1236 if (appData.icsActive) {
1237 DisplayFatalError(_("Analysis mode does not work with ICS mode"),0,2);
1241 } else if (initialMode == AnalyzeFile) {
1242 appData.showThinking = TRUE; // [HGM] thinking: moved out of ShowThinkingEvent
1243 ShowThinkingEvent();
1245 AnalysisPeriodicEvent(1);
1246 } else if (initialMode == MachinePlaysWhite) {
1247 if (appData.noChessProgram) {
1248 DisplayFatalError(_("MachineWhite mode requires a chess engine"),
1252 if (appData.icsActive) {
1253 DisplayFatalError(_("MachineWhite mode does not work with ICS mode"),
1257 MachineWhiteEvent();
1258 } else if (initialMode == MachinePlaysBlack) {
1259 if (appData.noChessProgram) {
1260 DisplayFatalError(_("MachineBlack mode requires a chess engine"),
1264 if (appData.icsActive) {
1265 DisplayFatalError(_("MachineBlack mode does not work with ICS mode"),
1269 MachineBlackEvent();
1270 } else if (initialMode == TwoMachinesPlay) {
1271 if (appData.noChessProgram) {
1272 DisplayFatalError(_("TwoMachines mode requires a chess engine"),
1276 if (appData.icsActive) {
1277 DisplayFatalError(_("TwoMachines mode does not work with ICS mode"),
1282 } else if (initialMode == EditGame) {
1284 } else if (initialMode == EditPosition) {
1285 EditPositionEvent();
1286 } else if (initialMode == Training) {
1287 if (*appData.loadGameFile == NULLCHAR) {
1288 DisplayFatalError(_("Training mode requires a game file"), 0, 2);
1297 * Establish will establish a contact to a remote host.port.
1298 * Sets icsPR to a ProcRef for a process (or pseudo-process)
1299 * used to talk to the host.
1300 * Returns 0 if okay, error code if not.
1307 if (*appData.icsCommPort != NULLCHAR) {
1308 /* Talk to the host through a serial comm port */
1309 return OpenCommPort(appData.icsCommPort, &icsPR);
1311 } else if (*appData.gateway != NULLCHAR) {
1312 if (*appData.remoteShell == NULLCHAR) {
1313 /* Use the rcmd protocol to run telnet program on a gateway host */
1314 snprintf(buf, sizeof(buf), "%s %s %s",
1315 appData.telnetProgram, appData.icsHost, appData.icsPort);
1316 return OpenRcmd(appData.gateway, appData.remoteUser, buf, &icsPR);
1319 /* Use the rsh program to run telnet program on a gateway host */
1320 if (*appData.remoteUser == NULLCHAR) {
1321 snprintf(buf, sizeof(buf), "%s %s %s %s %s", appData.remoteShell,
1322 appData.gateway, appData.telnetProgram,
1323 appData.icsHost, appData.icsPort);
1325 snprintf(buf, sizeof(buf), "%s %s -l %s %s %s %s",
1326 appData.remoteShell, appData.gateway,
1327 appData.remoteUser, appData.telnetProgram,
1328 appData.icsHost, appData.icsPort);
1330 return StartChildProcess(buf, "", &icsPR);
1333 } else if (appData.useTelnet) {
1334 return OpenTelnet(appData.icsHost, appData.icsPort, &icsPR);
1337 /* TCP socket interface differs somewhat between
1338 Unix and NT; handle details in the front end.
1340 return OpenTCP(appData.icsHost, appData.icsPort, &icsPR);
1345 show_bytes(fp, buf, count)
1351 if (*buf < 040 || *(unsigned char *) buf > 0177) {
1352 fprintf(fp, "\\%03o", *buf & 0xff);
1361 /* Returns an errno value */
1363 OutputMaybeTelnet(pr, message, count, outError)
1369 char buf[8192], *p, *q, *buflim;
1370 int left, newcount, outcount;
1372 if (*appData.icsCommPort != NULLCHAR || appData.useTelnet ||
1373 *appData.gateway != NULLCHAR) {
1374 if (appData.debugMode) {
1375 fprintf(debugFP, ">ICS: ");
1376 show_bytes(debugFP, message, count);
1377 fprintf(debugFP, "\n");
1379 return OutputToProcess(pr, message, count, outError);
1382 buflim = &buf[sizeof(buf)-1]; /* allow 1 byte for expanding last char */
1389 if (appData.debugMode) {
1390 fprintf(debugFP, ">ICS: ");
1391 show_bytes(debugFP, buf, newcount);
1392 fprintf(debugFP, "\n");
1394 outcount = OutputToProcess(pr, buf, newcount, outError);
1395 if (outcount < newcount) return -1; /* to be sure */
1402 } else if (((unsigned char) *p) == TN_IAC) {
1403 *q++ = (char) TN_IAC;
1410 if (appData.debugMode) {
1411 fprintf(debugFP, ">ICS: ");
1412 show_bytes(debugFP, buf, newcount);
1413 fprintf(debugFP, "\n");
1415 outcount = OutputToProcess(pr, buf, newcount, outError);
1416 if (outcount < newcount) return -1; /* to be sure */
1421 read_from_player(isr, closure, message, count, error)
1428 int outError, outCount;
1429 static int gotEof = 0;
1431 /* Pass data read from player on to ICS */
1434 outCount = OutputMaybeTelnet(icsPR, message, count, &outError);
1435 if (outCount < count) {
1436 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1438 } else if (count < 0) {
1439 RemoveInputSource(isr);
1440 DisplayFatalError(_("Error reading from keyboard"), error, 1);
1441 } else if (gotEof++ > 0) {
1442 RemoveInputSource(isr);
1443 DisplayFatalError(_("Got end of file from keyboard"), 0, 0);
1449 { // [HGM] alive: periodically send dummy (date) command to ICS to prevent time-out
1450 if(!connectionAlive) DisplayFatalError("No response from ICS", 0, 1);
1451 connectionAlive = FALSE; // only sticks if no response to 'date' command.
1452 SendToICS("date\n");
1453 if(appData.keepAlive) ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
1456 /* added routine for printf style output to ics */
1457 void ics_printf(char *format, ...)
1459 char buffer[MSG_SIZ];
1462 va_start(args, format);
1463 vsnprintf(buffer, sizeof(buffer), format, args);
1464 buffer[sizeof(buffer)-1] = '\0';
1473 int count, outCount, outError;
1475 if (icsPR == NULL) return;
1478 outCount = OutputMaybeTelnet(icsPR, s, count, &outError);
1479 if (outCount < count) {
1480 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1484 /* This is used for sending logon scripts to the ICS. Sending
1485 without a delay causes problems when using timestamp on ICC
1486 (at least on my machine). */
1488 SendToICSDelayed(s,msdelay)
1492 int count, outCount, outError;
1494 if (icsPR == NULL) return;
1497 if (appData.debugMode) {
1498 fprintf(debugFP, ">ICS: ");
1499 show_bytes(debugFP, s, count);
1500 fprintf(debugFP, "\n");
1502 outCount = OutputToProcessDelayed(icsPR, s, count, &outError,
1504 if (outCount < count) {
1505 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1510 /* Remove all highlighting escape sequences in s
1511 Also deletes any suffix starting with '('
1514 StripHighlightAndTitle(s)
1517 static char retbuf[MSG_SIZ];
1520 while (*s != NULLCHAR) {
1521 while (*s == '\033') {
1522 while (*s != NULLCHAR && !isalpha(*s)) s++;
1523 if (*s != NULLCHAR) s++;
1525 while (*s != NULLCHAR && *s != '\033') {
1526 if (*s == '(' || *s == '[') {
1537 /* Remove all highlighting escape sequences in s */
1542 static char retbuf[MSG_SIZ];
1545 while (*s != NULLCHAR) {
1546 while (*s == '\033') {
1547 while (*s != NULLCHAR && !isalpha(*s)) s++;
1548 if (*s != NULLCHAR) s++;
1550 while (*s != NULLCHAR && *s != '\033') {
1558 char *variantNames[] = VARIANT_NAMES;
1563 return variantNames[v];
1567 /* Identify a variant from the strings the chess servers use or the
1568 PGN Variant tag names we use. */
1575 VariantClass v = VariantNormal;
1576 int i, found = FALSE;
1581 /* [HGM] skip over optional board-size prefixes */
1582 if( sscanf(e, "%dx%d_", &i, &i) == 2 ||
1583 sscanf(e, "%dx%d+%d_", &i, &i, &i) == 3 ) {
1584 while( *e++ != '_');
1587 if(StrCaseStr(e, "misc/")) { // [HGM] on FICS, misc/shogi is not shogi
1591 for (i=0; i<sizeof(variantNames)/sizeof(char*); i++) {
1592 if (StrCaseStr(e, variantNames[i])) {
1593 v = (VariantClass) i;
1600 if ((StrCaseStr(e, "fischer") && StrCaseStr(e, "random"))
1601 || StrCaseStr(e, "wild/fr")
1602 || StrCaseStr(e, "frc") || StrCaseStr(e, "960")) {
1603 v = VariantFischeRandom;
1604 } else if ((i = 4, p = StrCaseStr(e, "wild")) ||
1605 (i = 1, p = StrCaseStr(e, "w"))) {
1607 while (*p && (isspace(*p) || *p == '(' || *p == '/')) p++;
1614 case 0: /* FICS only, actually */
1616 /* Castling legal even if K starts on d-file */
1617 v = VariantWildCastle;
1622 /* Castling illegal even if K & R happen to start in
1623 normal positions. */
1624 v = VariantNoCastle;
1637 /* Castling legal iff K & R start in normal positions */
1643 /* Special wilds for position setup; unclear what to do here */
1644 v = VariantLoadable;
1647 /* Bizarre ICC game */
1648 v = VariantTwoKings;
1651 v = VariantKriegspiel;
1657 v = VariantFischeRandom;
1660 v = VariantCrazyhouse;
1663 v = VariantBughouse;
1669 /* Not quite the same as FICS suicide! */
1670 v = VariantGiveaway;
1676 v = VariantShatranj;
1679 /* Temporary names for future ICC types. The name *will* change in
1680 the next xboard/WinBoard release after ICC defines it. */
1718 v = VariantCapablanca;
1721 v = VariantKnightmate;
1727 v = VariantCylinder;
1733 v = VariantCapaRandom;
1736 v = VariantBerolina;
1748 /* Found "wild" or "w" in the string but no number;
1749 must assume it's normal chess. */
1753 sprintf(buf, _("Unknown wild type %d"), wnum);
1754 DisplayError(buf, 0);
1760 if (appData.debugMode) {
1761 fprintf(debugFP, _("recognized '%s' (%d) as variant %s\n"),
1762 e, wnum, VariantName(v));
1767 static int leftover_start = 0, leftover_len = 0;
1768 char star_match[STAR_MATCH_N][MSG_SIZ];
1770 /* Test whether pattern is present at &buf[*index]; if so, return TRUE,
1771 advance *index beyond it, and set leftover_start to the new value of
1772 *index; else return FALSE. If pattern contains the character '*', it
1773 matches any sequence of characters not containing '\r', '\n', or the
1774 character following the '*' (if any), and the matched sequence(s) are
1775 copied into star_match.
1778 looking_at(buf, index, pattern)
1783 char *bufp = &buf[*index], *patternp = pattern;
1785 char *matchp = star_match[0];
1788 if (*patternp == NULLCHAR) {
1789 *index = leftover_start = bufp - buf;
1793 if (*bufp == NULLCHAR) return FALSE;
1794 if (*patternp == '*') {
1795 if (*bufp == *(patternp + 1)) {
1797 matchp = star_match[++star_count];
1801 } else if (*bufp == '\n' || *bufp == '\r') {
1803 if (*patternp == NULLCHAR)
1808 *matchp++ = *bufp++;
1812 if (*patternp != *bufp) return FALSE;
1819 SendToPlayer(data, length)
1823 int error, outCount;
1824 outCount = OutputToProcess(NoProc, data, length, &error);
1825 if (outCount < length) {
1826 DisplayFatalError(_("Error writing to display"), error, 1);
1831 PackHolding(packed, holding)
1843 switch (runlength) {
1854 sprintf(q, "%d", runlength);
1866 /* Telnet protocol requests from the front end */
1868 TelnetRequest(ddww, option)
1869 unsigned char ddww, option;
1871 unsigned char msg[3];
1872 int outCount, outError;
1874 if (*appData.icsCommPort != NULLCHAR || appData.useTelnet) return;
1876 if (appData.debugMode) {
1877 char buf1[8], buf2[8], *ddwwStr, *optionStr;
1893 sprintf(buf1, "%d", ddww);
1902 sprintf(buf2, "%d", option);
1905 fprintf(debugFP, ">%s %s ", ddwwStr, optionStr);
1910 outCount = OutputToProcess(icsPR, (char *)msg, 3, &outError);
1912 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1919 if (!appData.icsActive) return;
1920 TelnetRequest(TN_DO, TN_ECHO);
1926 if (!appData.icsActive) return;
1927 TelnetRequest(TN_DONT, TN_ECHO);
1931 CopyHoldings(Board board, char *holdings, ChessSquare lowestPiece)
1933 /* put the holdings sent to us by the server on the board holdings area */
1934 int i, j, holdingsColumn, holdingsStartRow, direction, countsColumn;
1938 if(gameInfo.holdingsWidth < 2) return;
1939 if(gameInfo.variant != VariantBughouse && board[HOLDINGS_SET])
1940 return; // prevent overwriting by pre-board holdings
1942 if( (int)lowestPiece >= BlackPawn ) {
1945 holdingsStartRow = BOARD_HEIGHT-1;
1948 holdingsColumn = BOARD_WIDTH-1;
1949 countsColumn = BOARD_WIDTH-2;
1950 holdingsStartRow = 0;
1954 for(i=0; i<BOARD_HEIGHT; i++) { /* clear holdings */
1955 board[i][holdingsColumn] = EmptySquare;
1956 board[i][countsColumn] = (ChessSquare) 0;
1958 while( (p=*holdings++) != NULLCHAR ) {
1959 piece = CharToPiece( ToUpper(p) );
1960 if(piece == EmptySquare) continue;
1961 /*j = (int) piece - (int) WhitePawn;*/
1962 j = PieceToNumber(piece);
1963 if(j >= gameInfo.holdingsSize) continue; /* ignore pieces that do not fit */
1964 if(j < 0) continue; /* should not happen */
1965 piece = (ChessSquare) ( (int)piece + (int)lowestPiece );
1966 board[holdingsStartRow+j*direction][holdingsColumn] = piece;
1967 board[holdingsStartRow+j*direction][countsColumn]++;
1973 VariantSwitch(Board board, VariantClass newVariant)
1975 int newHoldingsWidth, newWidth = 8, newHeight = 8, i, j;
1978 startedFromPositionFile = FALSE;
1979 if(gameInfo.variant == newVariant) return;
1981 /* [HGM] This routine is called each time an assignment is made to
1982 * gameInfo.variant during a game, to make sure the board sizes
1983 * are set to match the new variant. If that means adding or deleting
1984 * holdings, we shift the playing board accordingly
1985 * This kludge is needed because in ICS observe mode, we get boards
1986 * of an ongoing game without knowing the variant, and learn about the
1987 * latter only later. This can be because of the move list we requested,
1988 * in which case the game history is refilled from the beginning anyway,
1989 * but also when receiving holdings of a crazyhouse game. In the latter
1990 * case we want to add those holdings to the already received position.
1994 if (appData.debugMode) {
1995 fprintf(debugFP, "Switch board from %s to %s\n",
1996 VariantName(gameInfo.variant), VariantName(newVariant));
1997 setbuf(debugFP, NULL);
1999 shuffleOpenings = 0; /* [HGM] shuffle */
2000 gameInfo.holdingsSize = 5; /* [HGM] prepare holdings */
2004 newWidth = 9; newHeight = 9;
2005 gameInfo.holdingsSize = 7;
2006 case VariantBughouse:
2007 case VariantCrazyhouse:
2008 newHoldingsWidth = 2; break;
2012 newHoldingsWidth = 2;
2013 gameInfo.holdingsSize = 8;
2016 case VariantCapablanca:
2017 case VariantCapaRandom:
2020 newHoldingsWidth = gameInfo.holdingsSize = 0;
2023 if(newWidth != gameInfo.boardWidth ||
2024 newHeight != gameInfo.boardHeight ||
2025 newHoldingsWidth != gameInfo.holdingsWidth ) {
2027 /* shift position to new playing area, if needed */
2028 if(newHoldingsWidth > gameInfo.holdingsWidth) {
2029 for(i=0; i<BOARD_HEIGHT; i++)
2030 for(j=BOARD_RGHT-1; j>=BOARD_LEFT; j--)
2031 board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
2033 for(i=0; i<newHeight; i++) {
2034 board[i][0] = board[i][newWidth+2*newHoldingsWidth-1] = EmptySquare;
2035 board[i][1] = board[i][newWidth+2*newHoldingsWidth-2] = (ChessSquare) 0;
2037 } else if(newHoldingsWidth < gameInfo.holdingsWidth) {
2038 for(i=0; i<BOARD_HEIGHT; i++)
2039 for(j=BOARD_LEFT; j<BOARD_RGHT; j++)
2040 board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
2043 gameInfo.boardWidth = newWidth;
2044 gameInfo.boardHeight = newHeight;
2045 gameInfo.holdingsWidth = newHoldingsWidth;
2046 gameInfo.variant = newVariant;
2047 InitDrawingSizes(-2, 0);
2048 } else gameInfo.variant = newVariant;
2049 CopyBoard(oldBoard, board); // remember correctly formatted board
2050 InitPosition(FALSE); /* this sets up board[0], but also other stuff */
2051 DrawPosition(TRUE, currentMove ? boards[currentMove] : oldBoard);
2054 static int loggedOn = FALSE;
2056 /*-- Game start info cache: --*/
2058 char gs_kind[MSG_SIZ];
2059 static char player1Name[128] = "";
2060 static char player2Name[128] = "";
2061 static char cont_seq[] = "\n\\ ";
2062 static int player1Rating = -1;
2063 static int player2Rating = -1;
2064 /*----------------------------*/
2066 ColorClass curColor = ColorNormal;
2067 int suppressKibitz = 0;
2070 Boolean soughtPending = FALSE;
2071 Boolean seekGraphUp;
2072 #define MAX_SEEK_ADS 200
2074 char *seekAdList[MAX_SEEK_ADS];
2075 int ratingList[MAX_SEEK_ADS], xList[MAX_SEEK_ADS], yList[MAX_SEEK_ADS], seekNrList[MAX_SEEK_ADS], zList[MAX_SEEK_ADS];
2076 float tcList[MAX_SEEK_ADS];
2077 char colorList[MAX_SEEK_ADS];
2078 int nrOfSeekAds = 0;
2079 int minRating = 1010, maxRating = 2800;
2080 int hMargin = 10, vMargin = 20, h, w;
2081 extern int squareSize, lineGap;
2086 int x, y, color = 0, r = ratingList[i]; float tc = tcList[i];
2087 xList[i] = yList[i] = -100; // outside graph, so cannot be clicked
2088 if(r < minRating+100 && r >=0 ) r = minRating+100;
2089 if(r > maxRating) r = maxRating;
2090 if(tc < 1.) tc = 1.;
2091 if(tc > 95.) tc = 95.;
2092 x = (w-hMargin-squareSize/8-7)* log(tc)/log(95.) + hMargin;
2093 y = ((double)r - minRating)/(maxRating - minRating)
2094 * (h-vMargin-squareSize/8-1) + vMargin;
2095 if(ratingList[i] < 0) y = vMargin + squareSize/4;
2096 if(strstr(seekAdList[i], " u ")) color = 1;
2097 if(!strstr(seekAdList[i], "lightning") && // for now all wilds same color
2098 !strstr(seekAdList[i], "bullet") &&
2099 !strstr(seekAdList[i], "blitz") &&
2100 !strstr(seekAdList[i], "standard") ) color = 2;
2101 if(strstr(seekAdList[i], "(C) ")) color |= SQUARE; // plot computer seeks as squares
2102 DrawSeekDot(xList[i]=x+3*(color&~SQUARE), yList[i]=h-1-y, colorList[i]=color);
2106 AddAd(char *handle, char *rating, int base, int inc, char rated, char *type, int nr, Boolean plot)
2108 char buf[MSG_SIZ], *ext = "";
2109 VariantClass v = StringToVariant(type);
2110 if(strstr(type, "wild")) {
2111 ext = type + 4; // append wild number
2112 if(v == VariantFischeRandom) type = "chess960"; else
2113 if(v == VariantLoadable) type = "setup"; else
2114 type = VariantName(v);
2116 sprintf(buf, "%s (%s) %d %d %c %s%s", handle, rating, base, inc, rated, type, ext);
2117 if(nrOfSeekAds < MAX_SEEK_ADS-1) {
2118 if(seekAdList[nrOfSeekAds]) free(seekAdList[nrOfSeekAds]);
2119 ratingList[nrOfSeekAds] = -1; // for if seeker has no rating
2120 sscanf(rating, "%d", &ratingList[nrOfSeekAds]);
2121 tcList[nrOfSeekAds] = base + (2./3.)*inc;
2122 seekNrList[nrOfSeekAds] = nr;
2123 zList[nrOfSeekAds] = 0;
2124 seekAdList[nrOfSeekAds++] = StrSave(buf);
2125 if(plot) PlotSeekAd(nrOfSeekAds-1);
2132 int x = xList[i], y = yList[i], d=squareSize/4, k;
2133 DrawSeekBackground(x-squareSize/8, y-squareSize/8, x+squareSize/8+1, y+squareSize/8+1);
2134 if(x < hMargin+d) DrawSeekAxis(hMargin, y-squareSize/8, hMargin, y+squareSize/8+1);
2135 // now replot every dot that overlapped
2136 for(k=0; k<nrOfSeekAds; k++) if(k != i) {
2137 int xx = xList[k], yy = yList[k];
2138 if(xx <= x+d && xx > x-d && yy <= y+d && yy > y-d)
2139 DrawSeekDot(xx, yy, colorList[k]);
2144 RemoveSeekAd(int nr)
2147 for(i=0; i<nrOfSeekAds; i++) if(seekNrList[i] == nr) {
2149 if(seekAdList[i]) free(seekAdList[i]);
2150 seekAdList[i] = seekAdList[--nrOfSeekAds];
2151 seekNrList[i] = seekNrList[nrOfSeekAds];
2152 ratingList[i] = ratingList[nrOfSeekAds];
2153 colorList[i] = colorList[nrOfSeekAds];
2154 tcList[i] = tcList[nrOfSeekAds];
2155 xList[i] = xList[nrOfSeekAds];
2156 yList[i] = yList[nrOfSeekAds];
2157 zList[i] = zList[nrOfSeekAds];
2158 seekAdList[nrOfSeekAds] = NULL;
2164 MatchSoughtLine(char *line)
2166 char handle[MSG_SIZ], rating[MSG_SIZ], type[MSG_SIZ];
2167 int nr, base, inc, u=0; char dummy;
2169 if(sscanf(line, "%d %s %s %d %d rated %s", &nr, rating, handle, &base, &inc, type) == 6 ||
2170 sscanf(line, "%d %s %s %s %d %d rated %c", &nr, rating, handle, type, &base, &inc, &dummy) == 7 ||
2172 (sscanf(line, "%d %s %s %d %d unrated %s", &nr, rating, handle, &base, &inc, type) == 6 ||
2173 sscanf(line, "%d %s %s %s %d %d unrated %c", &nr, rating, handle, type, &base, &inc, &dummy) == 7) ) {
2174 // match: compact and save the line
2175 AddAd(handle, rating, base, inc, u ? 'u' : 'r', type, nr, FALSE);
2184 if(!seekGraphUp) return FALSE;
2186 h = BOARD_HEIGHT * (squareSize + lineGap) + lineGap;
2187 w = BOARD_WIDTH * (squareSize + lineGap) + lineGap;
2189 DrawSeekBackground(0, 0, w, h);
2190 DrawSeekAxis(hMargin, h-1-vMargin, w-5, h-1-vMargin);
2191 DrawSeekAxis(hMargin, h-1-vMargin, hMargin, 5);
2192 for(i=0; i<4000; i+= 100) if(i>=minRating && i<maxRating) {
2193 int yy =((double)i - minRating)/(maxRating - minRating)*(h-vMargin-squareSize/8-1) + vMargin;
2195 DrawSeekAxis(hMargin+5*(i%500==0), yy, hMargin-5, yy); // rating ticks
2198 sprintf(buf, "%d", i);
2199 DrawSeekText(buf, hMargin+squareSize/8+7, yy);
2202 DrawSeekText("unrated", hMargin+squareSize/8+7, h-1-vMargin-squareSize/4);
2203 for(i=1; i<100; i+=(i<10?1:5)) {
2204 int xx = (w-hMargin-squareSize/8-7)* log((double)i)/log(95.) + hMargin;
2205 DrawSeekAxis(xx, h-1-vMargin, xx, h-6-vMargin-3*(i%10==0)); // TC ticks
2206 if(i<=5 || (i>40 ? i%20 : i%10) == 0) {
2208 sprintf(buf, "%d", i);
2209 DrawSeekText(buf, xx-2-3*(i>9), h-1-vMargin/2);
2212 for(i=0; i<nrOfSeekAds; i++) PlotSeekAd(i);
2216 int SeekGraphClick(ClickType click, int x, int y, int moving)
2218 static int lastDown = 0, displayed = 0, lastSecond;
2219 if(!seekGraphUp) { // initiate cration of seek graph by requesting seek-ad list
2220 if(click == Release || moving) return FALSE;
2222 soughtPending = TRUE;
2223 SendToICS(ics_prefix);
2224 SendToICS("sought\n"); // should this be "sought all"?
2225 } else { // issue challenge based on clicked ad
2226 int dist = 10000; int i, closest = 0, second = 0;
2227 for(i=0; i<nrOfSeekAds; i++) {
2228 int d = (x-xList[i])*(x-xList[i]) + (y-yList[i])*(y-yList[i]) + zList[i];
2229 if(d < dist) { dist = d; closest = i; }
2230 second += (d - zList[i] < 120); // count in-range ads
2231 if(click == Press && moving != 1 && zList[i]>0) zList[i] *= 0.8; // age priority
2235 second = (second > 1);
2236 if(displayed != closest || second != lastSecond) {
2237 DisplayMessage(second ? "!" : "", seekAdList[closest]);
2238 lastSecond = second; displayed = closest;
2240 if(click == Press) {
2241 if(moving == 2) zList[closest] = 100; // right-click; push to back on press
2244 } // on press 'hit', only show info
2245 if(moving == 2) return TRUE; // ignore right up-clicks on dot
2246 sprintf(buf, "play %d\n", seekNrList[closest]);
2247 SendToICS(ics_prefix);
2249 return TRUE; // let incoming board of started game pop down the graph
2250 } else if(click == Release) { // release 'miss' is ignored
2251 zList[lastDown] = 100; // make future selection of the rejected ad more difficult
2252 if(moving == 2) { // right up-click
2253 nrOfSeekAds = 0; // refresh graph
2254 soughtPending = TRUE;
2255 SendToICS(ics_prefix);
2256 SendToICS("sought\n"); // should this be "sought all"?
2259 } else if(moving) { if(displayed >= 0) DisplayMessage("", ""); displayed = -1; return TRUE; }
2260 // press miss or release hit 'pop down' seek graph
2261 seekGraphUp = FALSE;
2262 DrawPosition(TRUE, NULL);
2268 read_from_ics(isr, closure, data, count, error)
2275 #define BUF_SIZE (16*1024) /* overflowed at 8K with "inchannel 1" on FICS? */
2276 #define STARTED_NONE 0
2277 #define STARTED_MOVES 1
2278 #define STARTED_BOARD 2
2279 #define STARTED_OBSERVE 3
2280 #define STARTED_HOLDINGS 4
2281 #define STARTED_CHATTER 5
2282 #define STARTED_COMMENT 6
2283 #define STARTED_MOVES_NOHIDE 7
2285 static int started = STARTED_NONE;
2286 static char parse[20000];
2287 static int parse_pos = 0;
2288 static char buf[BUF_SIZE + 1];
2289 static int firstTime = TRUE, intfSet = FALSE;
2290 static ColorClass prevColor = ColorNormal;
2291 static int savingComment = FALSE;
2292 static int cmatch = 0; // continuation sequence match
2299 int backup; /* [DM] For zippy color lines */
2301 char talker[MSG_SIZ]; // [HGM] chat
2304 connectionAlive = TRUE; // [HGM] alive: I think, therefore I am...
2306 if (appData.debugMode) {
2308 fprintf(debugFP, "<ICS: ");
2309 show_bytes(debugFP, data, count);
2310 fprintf(debugFP, "\n");
2314 if (appData.debugMode) { int f = forwardMostMove;
2315 fprintf(debugFP, "ics input %d, castling = %d %d %d %d %d %d\n", f,
2316 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
2317 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
2320 /* If last read ended with a partial line that we couldn't parse,
2321 prepend it to the new read and try again. */
2322 if (leftover_len > 0) {
2323 for (i=0; i<leftover_len; i++)
2324 buf[i] = buf[leftover_start + i];
2327 /* copy new characters into the buffer */
2328 bp = buf + leftover_len;
2329 buf_len=leftover_len;
2330 for (i=0; i<count; i++)
2333 if (data[i] == '\r')
2336 // join lines split by ICS?
2337 if (!appData.noJoin)
2340 Joining just consists of finding matches against the
2341 continuation sequence, and discarding that sequence
2342 if found instead of copying it. So, until a match
2343 fails, there's nothing to do since it might be the
2344 complete sequence, and thus, something we don't want
2347 if (data[i] == cont_seq[cmatch])
2350 if (cmatch == strlen(cont_seq))
2352 cmatch = 0; // complete match. just reset the counter
2355 it's possible for the ICS to not include the space
2356 at the end of the last word, making our [correct]
2357 join operation fuse two separate words. the server
2358 does this when the space occurs at the width setting.
2360 if (!buf_len || buf[buf_len-1] != ' ')
2371 match failed, so we have to copy what matched before
2372 falling through and copying this character. In reality,
2373 this will only ever be just the newline character, but
2374 it doesn't hurt to be precise.
2376 strncpy(bp, cont_seq, cmatch);
2388 buf[buf_len] = NULLCHAR;
2389 // next_out = leftover_len; // [HGM] should we set this to 0, and not print it in advance?
2394 while (i < buf_len) {
2395 /* Deal with part of the TELNET option negotiation
2396 protocol. We refuse to do anything beyond the
2397 defaults, except that we allow the WILL ECHO option,
2398 which ICS uses to turn off password echoing when we are
2399 directly connected to it. We reject this option
2400 if localLineEditing mode is on (always on in xboard)
2401 and we are talking to port 23, which might be a real
2402 telnet server that will try to keep WILL ECHO on permanently.
2404 if (buf_len - i >= 3 && (unsigned char) buf[i] == TN_IAC) {
2405 static int remoteEchoOption = FALSE; /* telnet ECHO option */
2406 unsigned char option;
2408 switch ((unsigned char) buf[++i]) {
2410 if (appData.debugMode)
2411 fprintf(debugFP, "\n<WILL ");
2412 switch (option = (unsigned char) buf[++i]) {
2414 if (appData.debugMode)
2415 fprintf(debugFP, "ECHO ");
2416 /* Reply only if this is a change, according
2417 to the protocol rules. */
2418 if (remoteEchoOption) break;
2419 if (appData.localLineEditing &&
2420 atoi(appData.icsPort) == TN_PORT) {
2421 TelnetRequest(TN_DONT, TN_ECHO);
2424 TelnetRequest(TN_DO, TN_ECHO);
2425 remoteEchoOption = TRUE;
2429 if (appData.debugMode)
2430 fprintf(debugFP, "%d ", option);
2431 /* Whatever this is, we don't want it. */
2432 TelnetRequest(TN_DONT, option);
2437 if (appData.debugMode)
2438 fprintf(debugFP, "\n<WONT ");
2439 switch (option = (unsigned char) buf[++i]) {
2441 if (appData.debugMode)
2442 fprintf(debugFP, "ECHO ");
2443 /* Reply only if this is a change, according
2444 to the protocol rules. */
2445 if (!remoteEchoOption) break;
2447 TelnetRequest(TN_DONT, TN_ECHO);
2448 remoteEchoOption = FALSE;
2451 if (appData.debugMode)
2452 fprintf(debugFP, "%d ", (unsigned char) option);
2453 /* Whatever this is, it must already be turned
2454 off, because we never agree to turn on
2455 anything non-default, so according to the
2456 protocol rules, we don't reply. */
2461 if (appData.debugMode)
2462 fprintf(debugFP, "\n<DO ");
2463 switch (option = (unsigned char) buf[++i]) {
2465 /* Whatever this is, we refuse to do it. */
2466 if (appData.debugMode)
2467 fprintf(debugFP, "%d ", option);
2468 TelnetRequest(TN_WONT, option);
2473 if (appData.debugMode)
2474 fprintf(debugFP, "\n<DONT ");
2475 switch (option = (unsigned char) buf[++i]) {
2477 if (appData.debugMode)
2478 fprintf(debugFP, "%d ", option);
2479 /* Whatever this is, we are already not doing
2480 it, because we never agree to do anything
2481 non-default, so according to the protocol
2482 rules, we don't reply. */
2487 if (appData.debugMode)
2488 fprintf(debugFP, "\n<IAC ");
2489 /* Doubled IAC; pass it through */
2493 if (appData.debugMode)
2494 fprintf(debugFP, "\n<%d ", (unsigned char) buf[i]);
2495 /* Drop all other telnet commands on the floor */
2498 if (oldi > next_out)
2499 SendToPlayer(&buf[next_out], oldi - next_out);
2505 /* OK, this at least will *usually* work */
2506 if (!loggedOn && looking_at(buf, &i, "ics%")) {
2510 if (loggedOn && !intfSet) {
2511 if (ics_type == ICS_ICC) {
2513 "/set-quietly interface %s\n/set-quietly style 12\n",
2515 if(appData.seekGraph && appData.autoRefresh) // [HGM] seekgraph
2516 strcat(str, "/set-2 51 1\n/set seek 1\n");
2517 } else if (ics_type == ICS_CHESSNET) {
2518 sprintf(str, "/style 12\n");
2520 strcpy(str, "alias $ @\n$set interface ");
2521 strcat(str, programVersion);
2522 strcat(str, "\n$iset startpos 1\n$iset ms 1\n");
2523 if(appData.seekGraph && appData.autoRefresh) // [HGM] seekgraph
2524 strcat(str, "$iset seekremove 1\n$set seek 1\n");
2526 strcat(str, "$iset nohighlight 1\n");
2528 strcat(str, "$iset lock 1\n$style 12\n");
2531 NotifyFrontendLogin();
2535 if (started == STARTED_COMMENT) {
2536 /* Accumulate characters in comment */
2537 parse[parse_pos++] = buf[i];
2538 if (buf[i] == '\n') {
2539 parse[parse_pos] = NULLCHAR;
2540 if(chattingPartner>=0) {
2542 sprintf(mess, "%s%s", talker, parse);
2543 OutputChatMessage(chattingPartner, mess);
2544 chattingPartner = -1;
2545 next_out = i+1; // [HGM] suppress printing in ICS window
2547 if(!suppressKibitz) // [HGM] kibitz
2548 AppendComment(forwardMostMove, StripHighlight(parse), TRUE);
2549 else { // [HGM kibitz: divert memorized engine kibitz to engine-output window
2550 int nrDigit = 0, nrAlph = 0, j;
2551 if(parse_pos > MSG_SIZ - 30) // defuse unreasonably long input
2552 { parse_pos = MSG_SIZ-30; parse[parse_pos - 1] = '\n'; }
2553 parse[parse_pos] = NULLCHAR;
2554 // try to be smart: if it does not look like search info, it should go to
2555 // ICS interaction window after all, not to engine-output window.
2556 for(j=0; j<parse_pos; j++) { // count letters and digits
2557 nrDigit += (parse[j] >= '0' && parse[j] <= '9');
2558 nrAlph += (parse[j] >= 'a' && parse[j] <= 'z');
2559 nrAlph += (parse[j] >= 'A' && parse[j] <= 'Z');
2561 if(nrAlph < 9*nrDigit) { // if more than 10% digit we assume search info
2562 int depth=0; float score;
2563 if(sscanf(parse, "!!! %f/%d", &score, &depth) == 2 && depth>0) {
2564 // [HGM] kibitz: save kibitzed opponent info for PGN and eval graph
2565 pvInfoList[forwardMostMove-1].depth = depth;
2566 pvInfoList[forwardMostMove-1].score = 100*score;
2568 OutputKibitz(suppressKibitz, parse);
2571 sprintf(tmp, _("your opponent kibitzes: %s"), parse);
2572 SendToPlayer(tmp, strlen(tmp));
2574 next_out = i+1; // [HGM] suppress printing in ICS window
2576 started = STARTED_NONE;
2578 /* Don't match patterns against characters in comment */
2583 if (started == STARTED_CHATTER) {
2584 if (buf[i] != '\n') {
2585 /* Don't match patterns against characters in chatter */
2589 started = STARTED_NONE;
2590 if(suppressKibitz) next_out = i+1;
2593 /* Kludge to deal with rcmd protocol */
2594 if (firstTime && looking_at(buf, &i, "\001*")) {
2595 DisplayFatalError(&buf[1], 0, 1);
2601 if (!loggedOn && looking_at(buf, &i, "chessclub.com")) {
2604 if (appData.debugMode)
2605 fprintf(debugFP, "ics_type %d\n", ics_type);
2608 if (!loggedOn && looking_at(buf, &i, "freechess.org")) {
2609 ics_type = ICS_FICS;
2611 if (appData.debugMode)
2612 fprintf(debugFP, "ics_type %d\n", ics_type);
2615 if (!loggedOn && looking_at(buf, &i, "chess.net")) {
2616 ics_type = ICS_CHESSNET;
2618 if (appData.debugMode)
2619 fprintf(debugFP, "ics_type %d\n", ics_type);
2624 (looking_at(buf, &i, "\"*\" is *a registered name") ||
2625 looking_at(buf, &i, "Logging you in as \"*\"") ||
2626 looking_at(buf, &i, "will be \"*\""))) {
2627 strcpy(ics_handle, star_match[0]);
2631 if (loggedOn && !have_set_title && ics_handle[0] != NULLCHAR) {
2633 snprintf(buf, sizeof(buf), "%s@%s", ics_handle, appData.icsHost);
2634 DisplayIcsInteractionTitle(buf);
2635 have_set_title = TRUE;
2638 /* skip finger notes */
2639 if (started == STARTED_NONE &&
2640 ((buf[i] == ' ' && isdigit(buf[i+1])) ||
2641 (buf[i] == '1' && buf[i+1] == '0')) &&
2642 buf[i+2] == ':' && buf[i+3] == ' ') {
2643 started = STARTED_CHATTER;
2649 // [HGM] seekgraph: recognize sought lines and end-of-sought message
2650 if(appData.seekGraph) {
2651 if(soughtPending && MatchSoughtLine(buf+i)) {
2652 i = strstr(buf+i, "rated") - buf;
2653 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2654 next_out = leftover_start = i;
2655 started = STARTED_CHATTER;
2656 suppressKibitz = TRUE;
2659 if((gameMode == IcsIdle || gameMode == BeginningOfGame)
2660 && looking_at(buf, &i, "* ads displayed")) {
2661 soughtPending = FALSE;
2666 if(appData.autoRefresh) {
2667 if(looking_at(buf, &i, "* (*) seeking * * * * *\"play *\" to respond)\n")) {
2668 int s = (ics_type == ICS_ICC); // ICC format differs
2670 AddAd(star_match[0], star_match[1], atoi(star_match[2+s]), atoi(star_match[3+s]),
2671 star_match[4+s][0], star_match[5-3*s], atoi(star_match[7]), TRUE);
2672 looking_at(buf, &i, "*% "); // eat prompt
2673 if(oldi > 0 && buf[oldi-1] == '\n') oldi--; // suppress preceding LF, if any
2674 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2675 next_out = i; // suppress
2678 if(looking_at(buf, &i, "\nAds removed: *\n") || looking_at(buf, &i, "\031(51 * *\031)")) {
2679 char *p = star_match[0];
2681 if(seekGraphUp) RemoveSeekAd(atoi(p));
2682 while(*p && *p++ != ' '); // next
2684 looking_at(buf, &i, "*% "); // eat prompt
2685 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2692 /* skip formula vars */
2693 if (started == STARTED_NONE &&
2694 buf[i] == 'f' && isdigit(buf[i+1]) && buf[i+2] == ':') {
2695 started = STARTED_CHATTER;
2700 // [HGM] kibitz: try to recognize opponent engine-score kibitzes, to divert them to engine-output window
2701 if (appData.autoKibitz && started == STARTED_NONE &&
2702 !appData.icsEngineAnalyze && // [HGM] [DM] ICS analyze
2703 (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack || gameMode == IcsObserving)) {
2704 if(looking_at(buf, &i, "* kibitzes: ") &&
2705 (StrStr(star_match[0], gameInfo.white) == star_match[0] ||
2706 StrStr(star_match[0], gameInfo.black) == star_match[0] )) { // kibitz of self or opponent
2707 suppressKibitz = TRUE;
2708 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2710 if((StrStr(star_match[0], gameInfo.white) == star_match[0]
2711 && (gameMode == IcsPlayingWhite)) ||
2712 (StrStr(star_match[0], gameInfo.black) == star_match[0]
2713 && (gameMode == IcsPlayingBlack)) ) // opponent kibitz
2714 started = STARTED_CHATTER; // own kibitz we simply discard
2716 started = STARTED_COMMENT; // make sure it will be collected in parse[]
2717 parse_pos = 0; parse[0] = NULLCHAR;
2718 savingComment = TRUE;
2719 suppressKibitz = gameMode != IcsObserving ? 2 :
2720 (StrStr(star_match[0], gameInfo.white) == NULL) + 1;
2724 if((looking_at(buf, &i, "\nkibitzed to *\n") || looking_at(buf, &i, "kibitzed to *\n") ||
2725 looking_at(buf, &i, "\n(kibitzed to *\n") || looking_at(buf, &i, "(kibitzed to *\n"))
2726 && atoi(star_match[0])) {
2727 // suppress the acknowledgements of our own autoKibitz
2729 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2730 if(p = strchr(star_match[0], ' ')) p[1] = NULLCHAR; // clip off "players)" on FICS
2731 SendToPlayer(star_match[0], strlen(star_match[0]));
2732 if(looking_at(buf, &i, "*% ")) // eat prompt
2733 suppressKibitz = FALSE;
2737 } // [HGM] kibitz: end of patch
2739 // [HGM] chat: intercept tells by users for which we have an open chat window
2741 if(started == STARTED_NONE && (looking_at(buf, &i, "* tells you:") || looking_at(buf, &i, "* says:") ||
2742 looking_at(buf, &i, "* whispers:") ||
2743 looking_at(buf, &i, "* shouts:") ||
2744 looking_at(buf, &i, "* c-shouts:") ||
2745 looking_at(buf, &i, "--> * ") ||
2746 looking_at(buf, &i, "*(*):") && (sscanf(star_match[1], "%d", &channel),1) ||
2747 looking_at(buf, &i, "*(*)(*):") && (sscanf(star_match[2], "%d", &channel),1) ||
2748 looking_at(buf, &i, "*(*)(*)(*):") && (sscanf(star_match[3], "%d", &channel),1) ||
2749 looking_at(buf, &i, "*(*)(*)(*)(*):") && sscanf(star_match[4], "%d", &channel) == 1 )) {
2751 sscanf(star_match[0], "%[^(]", talker+1); // strip (C) or (U) off ICS handle
2752 chattingPartner = -1;
2754 if(channel >= 0) // channel broadcast; look if there is a chatbox for this channel
2755 for(p=0; p<MAX_CHAT; p++) {
2756 if(channel == atoi(chatPartner[p])) {
2757 talker[0] = '['; strcat(talker, "] ");
2758 Colorize(channel == 1 ? ColorChannel1 : ColorChannel, FALSE);
2759 chattingPartner = p; break;
2762 if(buf[i-3] == 'r') // whisper; look if there is a WHISPER chatbox
2763 for(p=0; p<MAX_CHAT; p++) {
2764 if(!strcmp("whispers", chatPartner[p])) {
2765 talker[0] = '['; strcat(talker, "] ");
2766 chattingPartner = p; break;
2769 if(buf[i-3] == 't' || buf[oldi+2] == '>') // shout, c-shout or it; look if there is a 'shouts' chatbox
2770 for(p=0; p<MAX_CHAT; p++) {
2771 if(!strcmp("shouts", chatPartner[p])) {
2772 if(buf[oldi+2] == '>') { talker[0] = '<'; strcat(talker, "> "); Colorize(ColorShout, FALSE); }
2773 else if(buf[i-8] == '-') { talker[0] = '('; strcat(talker, ") "); Colorize(ColorSShout, FALSE); }
2774 else { talker[0] = '['; strcat(talker, "] "); Colorize(ColorShout, FALSE); }
2775 chattingPartner = p; break;
2778 if(chattingPartner<0) // if not, look if there is a chatbox for this indivdual
2779 for(p=0; p<MAX_CHAT; p++) if(!StrCaseCmp(talker+1, chatPartner[p])) {
2780 talker[0] = 0; Colorize(ColorTell, FALSE);
2781 chattingPartner = p; break;
2783 if(chattingPartner<0) i = oldi; else {
2784 Colorize(curColor, TRUE); // undo the bogus colorations we just made to trigger the souds
2785 if(oldi > 0 && buf[oldi-1] == '\n') oldi--;
2786 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2787 started = STARTED_COMMENT;
2788 parse_pos = 0; parse[0] = NULLCHAR;
2789 savingComment = 3 + chattingPartner; // counts as TRUE
2790 suppressKibitz = TRUE;
2793 } // [HGM] chat: end of patch
2795 if (appData.zippyTalk || appData.zippyPlay) {
2796 /* [DM] Backup address for color zippy lines */
2800 if (loggedOn == TRUE)
2801 if (ZippyControl(buf, &backup) || ZippyConverse(buf, &backup) ||
2802 (appData.zippyPlay && ZippyMatch(buf, &backup)));
2804 if (ZippyControl(buf, &i) ||
2805 ZippyConverse(buf, &i) ||
2806 (appData.zippyPlay && ZippyMatch(buf, &i))) {
2808 if (!appData.colorize) continue;
2812 } // [DM] 'else { ' deleted
2814 /* Regular tells and says */
2815 (tkind = 1, looking_at(buf, &i, "* tells you: ")) ||
2816 looking_at(buf, &i, "* (your partner) tells you: ") ||
2817 looking_at(buf, &i, "* says: ") ||
2818 /* Don't color "message" or "messages" output */
2819 (tkind = 5, looking_at(buf, &i, "*. * (*:*): ")) ||
2820 looking_at(buf, &i, "*. * at *:*: ") ||
2821 looking_at(buf, &i, "--* (*:*): ") ||
2822 /* Message notifications (same color as tells) */
2823 looking_at(buf, &i, "* has left a message ") ||
2824 looking_at(buf, &i, "* just sent you a message:\n") ||
2825 /* Whispers and kibitzes */
2826 (tkind = 2, looking_at(buf, &i, "* whispers: ")) ||
2827 looking_at(buf, &i, "* kibitzes: ") ||
2829 (tkind = 3, looking_at(buf, &i, "*(*: "))) {
2831 if (tkind == 1 && strchr(star_match[0], ':')) {
2832 /* Avoid "tells you:" spoofs in channels */
2835 if (star_match[0][0] == NULLCHAR ||
2836 strchr(star_match[0], ' ') ||
2837 (tkind == 3 && strchr(star_match[1], ' '))) {
2838 /* Reject bogus matches */
2841 if (appData.colorize) {
2842 if (oldi > next_out) {
2843 SendToPlayer(&buf[next_out], oldi - next_out);
2848 Colorize(ColorTell, FALSE);
2849 curColor = ColorTell;
2852 Colorize(ColorKibitz, FALSE);
2853 curColor = ColorKibitz;
2856 p = strrchr(star_match[1], '(');
2863 Colorize(ColorChannel1, FALSE);
2864 curColor = ColorChannel1;
2866 Colorize(ColorChannel, FALSE);
2867 curColor = ColorChannel;
2871 curColor = ColorNormal;
2875 if (started == STARTED_NONE && appData.autoComment &&
2876 (gameMode == IcsObserving ||
2877 gameMode == IcsPlayingWhite ||
2878 gameMode == IcsPlayingBlack)) {
2879 parse_pos = i - oldi;
2880 memcpy(parse, &buf[oldi], parse_pos);
2881 parse[parse_pos] = NULLCHAR;
2882 started = STARTED_COMMENT;
2883 savingComment = TRUE;
2885 started = STARTED_CHATTER;
2886 savingComment = FALSE;
2893 if (looking_at(buf, &i, "* s-shouts: ") ||
2894 looking_at(buf, &i, "* c-shouts: ")) {
2895 if (appData.colorize) {
2896 if (oldi > next_out) {
2897 SendToPlayer(&buf[next_out], oldi - next_out);
2900 Colorize(ColorSShout, FALSE);
2901 curColor = ColorSShout;
2904 started = STARTED_CHATTER;
2908 if (looking_at(buf, &i, "--->")) {
2913 if (looking_at(buf, &i, "* shouts: ") ||
2914 looking_at(buf, &i, "--> ")) {
2915 if (appData.colorize) {
2916 if (oldi > next_out) {
2917 SendToPlayer(&buf[next_out], oldi - next_out);
2920 Colorize(ColorShout, FALSE);
2921 curColor = ColorShout;
2924 started = STARTED_CHATTER;
2928 if (looking_at( buf, &i, "Challenge:")) {
2929 if (appData.colorize) {
2930 if (oldi > next_out) {
2931 SendToPlayer(&buf[next_out], oldi - next_out);
2934 Colorize(ColorChallenge, FALSE);
2935 curColor = ColorChallenge;
2941 if (looking_at(buf, &i, "* offers you") ||
2942 looking_at(buf, &i, "* offers to be") ||
2943 looking_at(buf, &i, "* would like to") ||
2944 looking_at(buf, &i, "* requests to") ||
2945 looking_at(buf, &i, "Your opponent offers") ||
2946 looking_at(buf, &i, "Your opponent requests")) {
2948 if (appData.colorize) {
2949 if (oldi > next_out) {
2950 SendToPlayer(&buf[next_out], oldi - next_out);
2953 Colorize(ColorRequest, FALSE);
2954 curColor = ColorRequest;
2959 if (looking_at(buf, &i, "* (*) seeking")) {
2960 if (appData.colorize) {
2961 if (oldi > next_out) {
2962 SendToPlayer(&buf[next_out], oldi - next_out);
2965 Colorize(ColorSeek, FALSE);
2966 curColor = ColorSeek;
2971 if (looking_at(buf, &i, "\\ ")) {
2972 if (prevColor != ColorNormal) {
2973 if (oldi > next_out) {
2974 SendToPlayer(&buf[next_out], oldi - next_out);
2977 Colorize(prevColor, TRUE);
2978 curColor = prevColor;
2980 if (savingComment) {
2981 parse_pos = i - oldi;
2982 memcpy(parse, &buf[oldi], parse_pos);
2983 parse[parse_pos] = NULLCHAR;
2984 started = STARTED_COMMENT;
2985 if(savingComment >= 3) // [HGM] chat: continuation of line for chat box
2986 chattingPartner = savingComment - 3; // kludge to remember the box
2988 started = STARTED_CHATTER;
2993 if (looking_at(buf, &i, "Black Strength :") ||
2994 looking_at(buf, &i, "<<< style 10 board >>>") ||
2995 looking_at(buf, &i, "<10>") ||
2996 looking_at(buf, &i, "#@#")) {
2997 /* Wrong board style */
2999 SendToICS(ics_prefix);
3000 SendToICS("set style 12\n");
3001 SendToICS(ics_prefix);
3002 SendToICS("refresh\n");
3006 if (!have_sent_ICS_logon && looking_at(buf, &i, "login:")) {
3008 have_sent_ICS_logon = 1;
3012 if (ics_getting_history != H_GETTING_MOVES /*smpos kludge*/ &&
3013 (looking_at(buf, &i, "\n<12> ") ||
3014 looking_at(buf, &i, "<12> "))) {
3016 if (oldi > next_out) {
3017 SendToPlayer(&buf[next_out], oldi - next_out);
3020 started = STARTED_BOARD;
3025 if ((started == STARTED_NONE && looking_at(buf, &i, "\n<b1> ")) ||
3026 looking_at(buf, &i, "<b1> ")) {
3027 if (oldi > next_out) {
3028 SendToPlayer(&buf[next_out], oldi - next_out);
3031 started = STARTED_HOLDINGS;
3036 if (looking_at(buf, &i, "* *vs. * *--- *")) {
3038 /* Header for a move list -- first line */
3040 switch (ics_getting_history) {
3044 case BeginningOfGame:
3045 /* User typed "moves" or "oldmoves" while we
3046 were idle. Pretend we asked for these
3047 moves and soak them up so user can step
3048 through them and/or save them.
3051 gameMode = IcsObserving;
3054 ics_getting_history = H_GOT_UNREQ_HEADER;
3056 case EditGame: /*?*/
3057 case EditPosition: /*?*/
3058 /* Should above feature work in these modes too? */
3059 /* For now it doesn't */
3060 ics_getting_history = H_GOT_UNWANTED_HEADER;
3063 ics_getting_history = H_GOT_UNWANTED_HEADER;
3068 /* Is this the right one? */
3069 if (gameInfo.white && gameInfo.black &&
3070 strcmp(gameInfo.white, star_match[0]) == 0 &&
3071 strcmp(gameInfo.black, star_match[2]) == 0) {
3073 ics_getting_history = H_GOT_REQ_HEADER;
3076 case H_GOT_REQ_HEADER:
3077 case H_GOT_UNREQ_HEADER:
3078 case H_GOT_UNWANTED_HEADER:
3079 case H_GETTING_MOVES:
3080 /* Should not happen */
3081 DisplayError(_("Error gathering move list: two headers"), 0);
3082 ics_getting_history = H_FALSE;
3086 /* Save player ratings into gameInfo if needed */
3087 if ((ics_getting_history == H_GOT_REQ_HEADER ||
3088 ics_getting_history == H_GOT_UNREQ_HEADER) &&
3089 (gameInfo.whiteRating == -1 ||
3090 gameInfo.blackRating == -1)) {
3092 gameInfo.whiteRating = string_to_rating(star_match[1]);
3093 gameInfo.blackRating = string_to_rating(star_match[3]);
3094 if (appData.debugMode)
3095 fprintf(debugFP, _("Ratings from header: W %d, B %d\n"),
3096 gameInfo.whiteRating, gameInfo.blackRating);
3101 if (looking_at(buf, &i,
3102 "* * match, initial time: * minute*, increment: * second")) {
3103 /* Header for a move list -- second line */
3104 /* Initial board will follow if this is a wild game */
3105 if (gameInfo.event != NULL) free(gameInfo.event);
3106 sprintf(str, "ICS %s %s match", star_match[0], star_match[1]);
3107 gameInfo.event = StrSave(str);
3108 /* [HGM] we switched variant. Translate boards if needed. */
3109 VariantSwitch(boards[currentMove], StringToVariant(gameInfo.event));
3113 if (looking_at(buf, &i, "Move ")) {
3114 /* Beginning of a move list */
3115 switch (ics_getting_history) {
3117 /* Normally should not happen */
3118 /* Maybe user hit reset while we were parsing */
3121 /* Happens if we are ignoring a move list that is not
3122 * the one we just requested. Common if the user
3123 * tries to observe two games without turning off
3126 case H_GETTING_MOVES:
3127 /* Should not happen */
3128 DisplayError(_("Error gathering move list: nested"), 0);
3129 ics_getting_history = H_FALSE;
3131 case H_GOT_REQ_HEADER:
3132 ics_getting_history = H_GETTING_MOVES;
3133 started = STARTED_MOVES;
3135 if (oldi > next_out) {
3136 SendToPlayer(&buf[next_out], oldi - next_out);
3139 case H_GOT_UNREQ_HEADER:
3140 ics_getting_history = H_GETTING_MOVES;
3141 started = STARTED_MOVES_NOHIDE;
3144 case H_GOT_UNWANTED_HEADER:
3145 ics_getting_history = H_FALSE;
3151 if (looking_at(buf, &i, "% ") ||
3152 ((started == STARTED_MOVES || started == STARTED_MOVES_NOHIDE)
3153 && looking_at(buf, &i, "}*"))) { char *bookHit = NULL; // [HGM] book
3154 if(ics_type == ICS_ICC && soughtPending) { // [HGM] seekgraph: on ICC sought-list has no termination line
3155 soughtPending = FALSE;
3159 if(suppressKibitz) next_out = i;
3160 savingComment = FALSE;
3164 case STARTED_MOVES_NOHIDE:
3165 memcpy(&parse[parse_pos], &buf[oldi], i - oldi);
3166 parse[parse_pos + i - oldi] = NULLCHAR;
3167 ParseGameHistory(parse);
3169 if (appData.zippyPlay && first.initDone) {
3170 FeedMovesToProgram(&first, forwardMostMove);
3171 if (gameMode == IcsPlayingWhite) {
3172 if (WhiteOnMove(forwardMostMove)) {
3173 if (first.sendTime) {
3174 if (first.useColors) {
3175 SendToProgram("black\n", &first);
3177 SendTimeRemaining(&first, TRUE);
3179 if (first.useColors) {
3180 SendToProgram("white\n", &first); // [HGM] book: made sending of "go\n" book dependent
3182 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: probe book for initial pos
3183 first.maybeThinking = TRUE;
3185 if (first.usePlayother) {
3186 if (first.sendTime) {
3187 SendTimeRemaining(&first, TRUE);
3189 SendToProgram("playother\n", &first);
3195 } else if (gameMode == IcsPlayingBlack) {
3196 if (!WhiteOnMove(forwardMostMove)) {
3197 if (first.sendTime) {
3198 if (first.useColors) {
3199 SendToProgram("white\n", &first);
3201 SendTimeRemaining(&first, FALSE);
3203 if (first.useColors) {
3204 SendToProgram("black\n", &first);
3206 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE);
3207 first.maybeThinking = TRUE;
3209 if (first.usePlayother) {
3210 if (first.sendTime) {
3211 SendTimeRemaining(&first, FALSE);
3213 SendToProgram("playother\n", &first);
3222 if (gameMode == IcsObserving && ics_gamenum == -1) {
3223 /* Moves came from oldmoves or moves command
3224 while we weren't doing anything else.
3226 currentMove = forwardMostMove;
3227 ClearHighlights();/*!!could figure this out*/
3228 flipView = appData.flipView;
3229 DrawPosition(TRUE, boards[currentMove]);
3230 DisplayBothClocks();
3231 sprintf(str, "%s vs. %s",
3232 gameInfo.white, gameInfo.black);
3236 /* Moves were history of an active game */
3237 if (gameInfo.resultDetails != NULL) {
3238 free(gameInfo.resultDetails);
3239 gameInfo.resultDetails = NULL;
3242 HistorySet(parseList, backwardMostMove,
3243 forwardMostMove, currentMove-1);
3244 DisplayMove(currentMove - 1);
3245 if (started == STARTED_MOVES) next_out = i;
3246 started = STARTED_NONE;
3247 ics_getting_history = H_FALSE;
3250 case STARTED_OBSERVE:
3251 started = STARTED_NONE;
3252 SendToICS(ics_prefix);
3253 SendToICS("refresh\n");
3259 if(bookHit) { // [HGM] book: simulate book reply
3260 static char bookMove[MSG_SIZ]; // a bit generous?
3262 programStats.nodes = programStats.depth = programStats.time =
3263 programStats.score = programStats.got_only_move = 0;
3264 sprintf(programStats.movelist, "%s (xbook)", bookHit);
3266 strcpy(bookMove, "move ");
3267 strcat(bookMove, bookHit);
3268 HandleMachineMove(bookMove, &first);
3273 if ((started == STARTED_MOVES || started == STARTED_BOARD ||
3274 started == STARTED_HOLDINGS ||
3275 started == STARTED_MOVES_NOHIDE) && i >= leftover_len) {
3276 /* Accumulate characters in move list or board */
3277 parse[parse_pos++] = buf[i];
3280 /* Start of game messages. Mostly we detect start of game
3281 when the first board image arrives. On some versions
3282 of the ICS, though, we need to do a "refresh" after starting
3283 to observe in order to get the current board right away. */
3284 if (looking_at(buf, &i, "Adding game * to observation list")) {
3285 started = STARTED_OBSERVE;
3289 /* Handle auto-observe */
3290 if (appData.autoObserve &&
3291 (gameMode == IcsIdle || gameMode == BeginningOfGame) &&
3292 looking_at(buf, &i, "Game notification: * (*) vs. * (*)")) {
3294 /* Choose the player that was highlighted, if any. */
3295 if (star_match[0][0] == '\033' ||
3296 star_match[1][0] != '\033') {
3297 player = star_match[0];
3299 player = star_match[2];
3301 sprintf(str, "%sobserve %s\n",
3302 ics_prefix, StripHighlightAndTitle(player));
3305 /* Save ratings from notify string */
3306 strcpy(player1Name, star_match[0]);
3307 player1Rating = string_to_rating(star_match[1]);
3308 strcpy(player2Name, star_match[2]);
3309 player2Rating = string_to_rating(star_match[3]);
3311 if (appData.debugMode)
3313 "Ratings from 'Game notification:' %s %d, %s %d\n",
3314 player1Name, player1Rating,
3315 player2Name, player2Rating);
3320 /* Deal with automatic examine mode after a game,
3321 and with IcsObserving -> IcsExamining transition */
3322 if (looking_at(buf, &i, "Entering examine mode for game *") ||
3323 looking_at(buf, &i, "has made you an examiner of game *")) {
3325 int gamenum = atoi(star_match[0]);
3326 if ((gameMode == IcsIdle || gameMode == IcsObserving) &&
3327 gamenum == ics_gamenum) {
3328 /* We were already playing or observing this game;
3329 no need to refetch history */
3330 gameMode = IcsExamining;
3332 pauseExamForwardMostMove = forwardMostMove;
3333 } else if (currentMove < forwardMostMove) {
3334 ForwardInner(forwardMostMove);
3337 /* I don't think this case really can happen */
3338 SendToICS(ics_prefix);
3339 SendToICS("refresh\n");
3344 /* Error messages */
3345 // if (ics_user_moved) {
3346 if (1) { // [HGM] old way ignored error after move type in; ics_user_moved is not set then!
3347 if (looking_at(buf, &i, "Illegal move") ||
3348 looking_at(buf, &i, "Not a legal move") ||
3349 looking_at(buf, &i, "Your king is in check") ||
3350 looking_at(buf, &i, "It isn't your turn") ||
3351 looking_at(buf, &i, "It is not your move")) {
3353 if (ics_user_moved && forwardMostMove > backwardMostMove) { // only backup if we already moved
3354 currentMove = forwardMostMove-1;
3355 DisplayMove(currentMove - 1); /* before DMError */
3356 DrawPosition(FALSE, boards[currentMove]);
3357 SwitchClocks(forwardMostMove-1); // [HGM] race
3358 DisplayBothClocks();
3360 DisplayMoveError(_("Illegal move (rejected by ICS)")); // [HGM] but always relay error msg
3366 if (looking_at(buf, &i, "still have time") ||
3367 looking_at(buf, &i, "not out of time") ||
3368 looking_at(buf, &i, "either player is out of time") ||
3369 looking_at(buf, &i, "has timeseal; checking")) {
3370 /* We must have called his flag a little too soon */
3371 whiteFlag = blackFlag = FALSE;
3375 if (looking_at(buf, &i, "added * seconds to") ||
3376 looking_at(buf, &i, "seconds were added to")) {
3377 /* Update the clocks */
3378 SendToICS(ics_prefix);
3379 SendToICS("refresh\n");
3383 if (!ics_clock_paused && looking_at(buf, &i, "clock paused")) {
3384 ics_clock_paused = TRUE;
3389 if (ics_clock_paused && looking_at(buf, &i, "clock resumed")) {
3390 ics_clock_paused = FALSE;
3395 /* Grab player ratings from the Creating: message.
3396 Note we have to check for the special case when
3397 the ICS inserts things like [white] or [black]. */
3398 if (looking_at(buf, &i, "Creating: * (*)* * (*)") ||
3399 looking_at(buf, &i, "Creating: * (*) [*] * (*)")) {
3401 0 player 1 name (not necessarily white)
3403 2 empty, white, or black (IGNORED)
3404 3 player 2 name (not necessarily black)
3407 The names/ratings are sorted out when the game
3408 actually starts (below).
3410 strcpy(player1Name, StripHighlightAndTitle(star_match[0]));
3411 player1Rating = string_to_rating(star_match[1]);
3412 strcpy(player2Name, StripHighlightAndTitle(star_match[3]));
3413 player2Rating = string_to_rating(star_match[4]);
3415 if (appData.debugMode)
3417 "Ratings from 'Creating:' %s %d, %s %d\n",
3418 player1Name, player1Rating,
3419 player2Name, player2Rating);
3424 /* Improved generic start/end-of-game messages */
3425 if ((tkind=0, looking_at(buf, &i, "{Game * (* vs. *) *}*")) ||
3426 (tkind=1, looking_at(buf, &i, "{Game * (*(*) vs. *(*)) *}*"))){
3427 /* If tkind == 0: */
3428 /* star_match[0] is the game number */
3429 /* [1] is the white player's name */
3430 /* [2] is the black player's name */
3431 /* For end-of-game: */
3432 /* [3] is the reason for the game end */
3433 /* [4] is a PGN end game-token, preceded by " " */
3434 /* For start-of-game: */
3435 /* [3] begins with "Creating" or "Continuing" */
3436 /* [4] is " *" or empty (don't care). */
3437 int gamenum = atoi(star_match[0]);
3438 char *whitename, *blackname, *why, *endtoken;
3439 ChessMove endtype = (ChessMove) 0;
3442 whitename = star_match[1];
3443 blackname = star_match[2];
3444 why = star_match[3];
3445 endtoken = star_match[4];
3447 whitename = star_match[1];
3448 blackname = star_match[3];
3449 why = star_match[5];
3450 endtoken = star_match[6];
3453 /* Game start messages */
3454 if (strncmp(why, "Creating ", 9) == 0 ||
3455 strncmp(why, "Continuing ", 11) == 0) {
3456 gs_gamenum = gamenum;
3457 strcpy(gs_kind, strchr(why, ' ') + 1);
3458 VariantSwitch(boards[currentMove], StringToVariant(gs_kind)); // [HGM] variantswitch: even before we get first board
3460 if (appData.zippyPlay) {
3461 ZippyGameStart(whitename, blackname);
3467 /* Game end messages */
3468 if (gameMode == IcsIdle || gameMode == BeginningOfGame ||
3469 ics_gamenum != gamenum) {
3472 while (endtoken[0] == ' ') endtoken++;
3473 switch (endtoken[0]) {
3476 endtype = GameUnfinished;
3479 endtype = BlackWins;
3482 if (endtoken[1] == '/')
3483 endtype = GameIsDrawn;
3485 endtype = WhiteWins;
3488 GameEnds(endtype, why, GE_ICS);
3490 if (appData.zippyPlay && first.initDone) {
3491 ZippyGameEnd(endtype, why);
3492 if (first.pr == NULL) {
3493 /* Start the next process early so that we'll
3494 be ready for the next challenge */
3495 StartChessProgram(&first);
3497 /* Send "new" early, in case this command takes
3498 a long time to finish, so that we'll be ready
3499 for the next challenge. */
3500 gameInfo.variant = VariantNormal; // [HGM] variantswitch: suppress sending of 'variant'
3507 if (looking_at(buf, &i, "Removing game * from observation") ||
3508 looking_at(buf, &i, "no longer observing game *") ||
3509 looking_at(buf, &i, "Game * (*) has no examiners")) {
3510 if (gameMode == IcsObserving &&
3511 atoi(star_match[0]) == ics_gamenum)
3513 /* icsEngineAnalyze */
3514 if (appData.icsEngineAnalyze) {
3521 ics_user_moved = FALSE;
3526 if (looking_at(buf, &i, "no longer examining game *")) {
3527 if (gameMode == IcsExamining &&
3528 atoi(star_match[0]) == ics_gamenum)
3532 ics_user_moved = FALSE;
3537 /* Advance leftover_start past any newlines we find,
3538 so only partial lines can get reparsed */
3539 if (looking_at(buf, &i, "\n")) {
3540 prevColor = curColor;
3541 if (curColor != ColorNormal) {
3542 if (oldi > next_out) {
3543 SendToPlayer(&buf[next_out], oldi - next_out);
3546 Colorize(ColorNormal, FALSE);
3547 curColor = ColorNormal;
3549 if (started == STARTED_BOARD) {
3550 started = STARTED_NONE;
3551 parse[parse_pos] = NULLCHAR;
3552 ParseBoard12(parse);
3555 /* Send premove here */
3556 if (appData.premove) {
3558 if (currentMove == 0 &&
3559 gameMode == IcsPlayingWhite &&
3560 appData.premoveWhite) {
3561 sprintf(str, "%s\n", appData.premoveWhiteText);
3562 if (appData.debugMode)
3563 fprintf(debugFP, "Sending premove:\n");
3565 } else if (currentMove == 1 &&
3566 gameMode == IcsPlayingBlack &&
3567 appData.premoveBlack) {
3568 sprintf(str, "%s\n", appData.premoveBlackText);
3569 if (appData.debugMode)
3570 fprintf(debugFP, "Sending premove:\n");
3572 } else if (gotPremove) {
3574 ClearPremoveHighlights();
3575 if (appData.debugMode)
3576 fprintf(debugFP, "Sending premove:\n");
3577 UserMoveEvent(premoveFromX, premoveFromY,
3578 premoveToX, premoveToY,
3583 /* Usually suppress following prompt */
3584 if (!(forwardMostMove == 0 && gameMode == IcsExamining)) {
3585 while(looking_at(buf, &i, "\n")); // [HGM] skip empty lines
3586 if (looking_at(buf, &i, "*% ")) {
3587 savingComment = FALSE;
3592 } else if (started == STARTED_HOLDINGS) {
3594 char new_piece[MSG_SIZ];
3595 started = STARTED_NONE;
3596 parse[parse_pos] = NULLCHAR;
3597 if (appData.debugMode)
3598 fprintf(debugFP, "Parsing holdings: %s, currentMove = %d\n",
3599 parse, currentMove);
3600 if (sscanf(parse, " game %d", &gamenum) == 1) {
3601 if(gamenum == ics_gamenum) { // [HGM] bughouse: old code if part of foreground game
3602 if (gameInfo.variant == VariantNormal) {
3603 /* [HGM] We seem to switch variant during a game!
3604 * Presumably no holdings were displayed, so we have
3605 * to move the position two files to the right to
3606 * create room for them!
3608 VariantClass newVariant;
3609 switch(gameInfo.boardWidth) { // base guess on board width
3610 case 9: newVariant = VariantShogi; break;
3611 case 10: newVariant = VariantGreat; break;
3612 default: newVariant = VariantCrazyhouse; break;
3614 VariantSwitch(boards[currentMove], newVariant); /* temp guess */
3615 /* Get a move list just to see the header, which
3616 will tell us whether this is really bug or zh */
3617 if (ics_getting_history == H_FALSE) {
3618 ics_getting_history = H_REQUESTED;
3619 sprintf(str, "%smoves %d\n", ics_prefix, gamenum);
3623 new_piece[0] = NULLCHAR;
3624 sscanf(parse, "game %d white [%s black [%s <- %s",
3625 &gamenum, white_holding, black_holding,
3627 white_holding[strlen(white_holding)-1] = NULLCHAR;
3628 black_holding[strlen(black_holding)-1] = NULLCHAR;
3629 /* [HGM] copy holdings to board holdings area */
3630 CopyHoldings(boards[forwardMostMove], white_holding, WhitePawn);
3631 CopyHoldings(boards[forwardMostMove], black_holding, BlackPawn);
3632 boards[forwardMostMove][HOLDINGS_SET] = 1; // flag holdings as set
3634 if (appData.zippyPlay && first.initDone) {
3635 ZippyHoldings(white_holding, black_holding,
3639 if (tinyLayout || smallLayout) {
3640 char wh[16], bh[16];
3641 PackHolding(wh, white_holding);
3642 PackHolding(bh, black_holding);
3643 sprintf(str, "[%s-%s] %s-%s", wh, bh,
3644 gameInfo.white, gameInfo.black);
3646 sprintf(str, "%s [%s] vs. %s [%s]",
3647 gameInfo.white, white_holding,
3648 gameInfo.black, black_holding);
3650 if(!partnerUp) // [HGM] bughouse: when peeking at partner game we already know what he captured...
3651 DrawPosition(FALSE, boards[currentMove]);
3653 } else if(appData.bgObserve) { // [HGM] bughouse: holdings of other game => background
3654 sscanf(parse, "game %d white [%s black [%s <- %s",
3655 &gamenum, white_holding, black_holding,
3657 white_holding[strlen(white_holding)-1] = NULLCHAR;
3658 black_holding[strlen(black_holding)-1] = NULLCHAR;
3659 /* [HGM] copy holdings to partner-board holdings area */
3660 CopyHoldings(partnerBoard, white_holding, WhitePawn);
3661 CopyHoldings(partnerBoard, black_holding, BlackPawn);
3662 if(partnerUp) DrawPosition(FALSE, partnerBoard);
3665 /* Suppress following prompt */
3666 if (looking_at(buf, &i, "*% ")) {
3667 if(strchr(star_match[0], 7)) SendToPlayer("\007", 1); // Bell(); // FICS fuses bell for next board with prompt in zh captures
3668 savingComment = FALSE;
3676 i++; /* skip unparsed character and loop back */
3679 if (started != STARTED_MOVES && started != STARTED_BOARD && !suppressKibitz && // [HGM] kibitz
3680 // started != STARTED_HOLDINGS && i > next_out) { // [HGM] should we compare to leftover_start in stead of i?
3681 // SendToPlayer(&buf[next_out], i - next_out);
3682 started != STARTED_HOLDINGS && leftover_start > next_out) {
3683 SendToPlayer(&buf[next_out], leftover_start - next_out);
3687 leftover_len = buf_len - leftover_start;
3688 /* if buffer ends with something we couldn't parse,
3689 reparse it after appending the next read */
3691 } else if (count == 0) {
3692 RemoveInputSource(isr);
3693 DisplayFatalError(_("Connection closed by ICS"), 0, 0);
3695 DisplayFatalError(_("Error reading from ICS"), error, 1);
3700 /* Board style 12 looks like this:
3702 <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
3704 * The "<12> " is stripped before it gets to this routine. The two
3705 * trailing 0's (flip state and clock ticking) are later addition, and
3706 * some chess servers may not have them, or may have only the first.
3707 * Additional trailing fields may be added in the future.
3710 #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"
3712 #define RELATION_OBSERVING_PLAYED 0
3713 #define RELATION_OBSERVING_STATIC -2 /* examined, oldmoves, or smoves */
3714 #define RELATION_PLAYING_MYMOVE 1
3715 #define RELATION_PLAYING_NOTMYMOVE -1
3716 #define RELATION_EXAMINING 2
3717 #define RELATION_ISOLATED_BOARD -3
3718 #define RELATION_STARTING_POSITION -4 /* FICS only */
3721 ParseBoard12(string)
3724 GameMode newGameMode;
3725 int gamenum, newGame, newMove, relation, basetime, increment, ics_flip = 0, i;
3726 int j, k, n, moveNum, white_stren, black_stren, white_time, black_time, takeback;
3727 int double_push, castle_ws, castle_wl, castle_bs, castle_bl, irrev_count;
3728 char to_play, board_chars[200];
3729 char move_str[500], str[500], elapsed_time[500];
3730 char black[32], white[32];
3732 int prevMove = currentMove;
3735 int fromX, fromY, toX, toY;
3737 int ranks=1, files=0; /* [HGM] ICS80: allow variable board size */
3738 char *bookHit = NULL; // [HGM] book
3739 Boolean weird = FALSE, reqFlag = FALSE;
3741 fromX = fromY = toX = toY = -1;
3745 if (appData.debugMode)
3746 fprintf(debugFP, _("Parsing board: %s\n"), string);
3748 move_str[0] = NULLCHAR;
3749 elapsed_time[0] = NULLCHAR;
3750 { /* [HGM] figure out how many ranks and files the board has, for ICS extension used by Capablanca server */
3752 while(i < 199 && (string[i] != ' ' || string[i+2] != ' ')) {
3753 if(string[i] == ' ') { ranks++; files = 0; }
3755 if(!strchr(" -pnbrqkPNBRQK" , string[i])) weird = TRUE; // test for fairies
3758 for(j = 0; j <i; j++) board_chars[j] = string[j];
3759 board_chars[i] = '\0';
3762 n = sscanf(string, PATTERN, &to_play, &double_push,
3763 &castle_ws, &castle_wl, &castle_bs, &castle_bl, &irrev_count,
3764 &gamenum, white, black, &relation, &basetime, &increment,
3765 &white_stren, &black_stren, &white_time, &black_time,
3766 &moveNum, str, elapsed_time, move_str, &ics_flip,
3770 snprintf(str, sizeof(str), _("Failed to parse board string:\n\"%s\""), string);
3771 DisplayError(str, 0);
3775 /* Convert the move number to internal form */
3776 moveNum = (moveNum - 1) * 2;
3777 if (to_play == 'B') moveNum++;
3778 if (moveNum > framePtr) { // [HGM] vari: do not run into saved variations
3779 DisplayFatalError(_("Game too long; increase MAX_MOVES and recompile"),
3785 case RELATION_OBSERVING_PLAYED:
3786 case RELATION_OBSERVING_STATIC:
3787 if (gamenum == -1) {
3788 /* Old ICC buglet */
3789 relation = RELATION_OBSERVING_STATIC;
3791 newGameMode = IcsObserving;
3793 case RELATION_PLAYING_MYMOVE:
3794 case RELATION_PLAYING_NOTMYMOVE:
3796 ((relation == RELATION_PLAYING_MYMOVE) == (to_play == 'W')) ?
3797 IcsPlayingWhite : IcsPlayingBlack;
3799 case RELATION_EXAMINING:
3800 newGameMode = IcsExamining;
3802 case RELATION_ISOLATED_BOARD:
3804 /* Just display this board. If user was doing something else,
3805 we will forget about it until the next board comes. */
3806 newGameMode = IcsIdle;
3808 case RELATION_STARTING_POSITION:
3809 newGameMode = gameMode;
3813 if((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)
3814 && newGameMode == IcsObserving && appData.bgObserve) {
3815 // [HGM] bughouse: don't act on alien boards while we play. Just parse the board and save it */
3817 for (k = 0; k < ranks; k++) {
3818 for (j = 0; j < files; j++)
3819 board[k][j+gameInfo.holdingsWidth] = CharToPiece(board_chars[(ranks-1-k)*(files+1) + j]);
3820 if(gameInfo.holdingsWidth > 1) {
3821 board[k][0] = board[k][BOARD_WIDTH-1] = EmptySquare;
3822 board[k][1] = board[k][BOARD_WIDTH-2] = (ChessSquare) 0;;
3825 CopyBoard(partnerBoard, board);
3826 if(partnerUp) DrawPosition(FALSE, partnerBoard);
3827 sprintf(buf, "W: %d:%d B: %d:%d (%d-%d) %c", white_time/60000, (white_time%60000)/1000,
3828 (black_time/60000), (black_time%60000)/1000, white_stren, black_stren, to_play);
3829 DisplayMessage(buf, "");
3833 /* Modify behavior for initial board display on move listing
3836 switch (ics_getting_history) {
3840 case H_GOT_REQ_HEADER:
3841 case H_GOT_UNREQ_HEADER:
3842 /* This is the initial position of the current game */
3843 gamenum = ics_gamenum;
3844 moveNum = 0; /* old ICS bug workaround */
3845 if (to_play == 'B') {
3846 startedFromSetupPosition = TRUE;
3847 blackPlaysFirst = TRUE;
3849 if (forwardMostMove == 0) forwardMostMove = 1;
3850 if (backwardMostMove == 0) backwardMostMove = 1;
3851 if (currentMove == 0) currentMove = 1;
3853 newGameMode = gameMode;
3854 relation = RELATION_STARTING_POSITION; /* ICC needs this */
3856 case H_GOT_UNWANTED_HEADER:
3857 /* This is an initial board that we don't want */
3859 case H_GETTING_MOVES:
3860 /* Should not happen */
3861 DisplayError(_("Error gathering move list: extra board"), 0);
3862 ics_getting_history = H_FALSE;
3866 if (gameInfo.boardHeight != ranks || gameInfo.boardWidth != files ||
3867 weird && (int)gameInfo.variant <= (int)VariantShogi) {
3868 /* [HGM] We seem to have switched variant unexpectedly
3869 * Try to guess new variant from board size
3871 VariantClass newVariant = VariantFairy; // if 8x8, but fairies present
3872 if(ranks == 8 && files == 10) newVariant = VariantCapablanca; else
3873 if(ranks == 10 && files == 9) newVariant = VariantXiangqi; else
3874 if(ranks == 8 && files == 12) newVariant = VariantCourier; else
3875 if(ranks == 9 && files == 9) newVariant = VariantShogi; else
3876 if(!weird) newVariant = VariantNormal;
3877 VariantSwitch(boards[currentMove], newVariant); /* temp guess */
3878 /* Get a move list just to see the header, which
3879 will tell us whether this is really bug or zh */
3880 if (ics_getting_history == H_FALSE) {
3881 ics_getting_history = H_REQUESTED; reqFlag = TRUE;
3882 sprintf(str, "%smoves %d\n", ics_prefix, gamenum);
3887 /* Take action if this is the first board of a new game, or of a
3888 different game than is currently being displayed. */
3889 if (gamenum != ics_gamenum || newGameMode != gameMode ||
3890 relation == RELATION_ISOLATED_BOARD) {
3892 /* Forget the old game and get the history (if any) of the new one */
3893 if (gameMode != BeginningOfGame) {
3897 if (appData.autoRaiseBoard) BoardToTop();
3899 if (gamenum == -1) {
3900 newGameMode = IcsIdle;
3901 } else if ((moveNum > 0 || newGameMode == IcsObserving) && newGameMode != IcsIdle &&
3902 appData.getMoveList && !reqFlag) {
3903 /* Need to get game history */
3904 ics_getting_history = H_REQUESTED;
3905 sprintf(str, "%smoves %d\n", ics_prefix, gamenum);
3909 /* Initially flip the board to have black on the bottom if playing
3910 black or if the ICS flip flag is set, but let the user change
3911 it with the Flip View button. */
3912 flipView = appData.autoFlipView ?
3913 (newGameMode == IcsPlayingBlack) || ics_flip :
3916 /* Done with values from previous mode; copy in new ones */
3917 gameMode = newGameMode;
3919 ics_gamenum = gamenum;
3920 if (gamenum == gs_gamenum) {
3921 int klen = strlen(gs_kind);
3922 if (gs_kind[klen - 1] == '.') gs_kind[klen - 1] = NULLCHAR;
3923 sprintf(str, "ICS %s", gs_kind);
3924 gameInfo.event = StrSave(str);
3926 gameInfo.event = StrSave("ICS game");
3928 gameInfo.site = StrSave(appData.icsHost);
3929 gameInfo.date = PGNDate();
3930 gameInfo.round = StrSave("-");
3931 gameInfo.white = StrSave(white);
3932 gameInfo.black = StrSave(black);
3933 timeControl = basetime * 60 * 1000;
3935 timeIncrement = increment * 1000;
3936 movesPerSession = 0;
3937 gameInfo.timeControl = TimeControlTagValue();
3938 VariantSwitch(boards[currentMove], StringToVariant(gameInfo.event) );
3939 if (appData.debugMode) {
3940 fprintf(debugFP, "ParseBoard says variant = '%s'\n", gameInfo.event);
3941 fprintf(debugFP, "recognized as %s\n", VariantName(gameInfo.variant));
3942 setbuf(debugFP, NULL);
3945 gameInfo.outOfBook = NULL;
3947 /* Do we have the ratings? */
3948 if (strcmp(player1Name, white) == 0 &&
3949 strcmp(player2Name, black) == 0) {
3950 if (appData.debugMode)
3951 fprintf(debugFP, "Remembered ratings: W %d, B %d\n",
3952 player1Rating, player2Rating);
3953 gameInfo.whiteRating = player1Rating;
3954 gameInfo.blackRating = player2Rating;
3955 } else if (strcmp(player2Name, white) == 0 &&
3956 strcmp(player1Name, black) == 0) {
3957 if (appData.debugMode)
3958 fprintf(debugFP, "Remembered ratings: W %d, B %d\n",
3959 player2Rating, player1Rating);
3960 gameInfo.whiteRating = player2Rating;
3961 gameInfo.blackRating = player1Rating;
3963 player1Name[0] = player2Name[0] = NULLCHAR;
3965 /* Silence shouts if requested */
3966 if (appData.quietPlay &&
3967 (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)) {
3968 SendToICS(ics_prefix);
3969 SendToICS("set shout 0\n");
3973 /* Deal with midgame name changes */
3975 if (!gameInfo.white || strcmp(gameInfo.white, white) != 0) {
3976 if (gameInfo.white) free(gameInfo.white);
3977 gameInfo.white = StrSave(white);
3979 if (!gameInfo.black || strcmp(gameInfo.black, black) != 0) {
3980 if (gameInfo.black) free(gameInfo.black);
3981 gameInfo.black = StrSave(black);
3985 /* Throw away game result if anything actually changes in examine mode */
3986 if (gameMode == IcsExamining && !newGame) {
3987 gameInfo.result = GameUnfinished;
3988 if (gameInfo.resultDetails != NULL) {
3989 free(gameInfo.resultDetails);
3990 gameInfo.resultDetails = NULL;
3994 /* In pausing && IcsExamining mode, we ignore boards coming
3995 in if they are in a different variation than we are. */
3996 if (pauseExamInvalid) return;
3997 if (pausing && gameMode == IcsExamining) {
3998 if (moveNum <= pauseExamForwardMostMove) {
3999 pauseExamInvalid = TRUE;
4000 forwardMostMove = pauseExamForwardMostMove;
4005 if (appData.debugMode) {
4006 fprintf(debugFP, "load %dx%d board\n", files, ranks);
4008 /* Parse the board */
4009 for (k = 0; k < ranks; k++) {
4010 for (j = 0; j < files; j++)
4011 board[k][j+gameInfo.holdingsWidth] = CharToPiece(board_chars[(ranks-1-k)*(files+1) + j]);
4012 if(gameInfo.holdingsWidth > 1) {
4013 board[k][0] = board[k][BOARD_WIDTH-1] = EmptySquare;
4014 board[k][1] = board[k][BOARD_WIDTH-2] = (ChessSquare) 0;;
4017 CopyBoard(boards[moveNum], board);
4018 boards[moveNum][HOLDINGS_SET] = 0; // [HGM] indicate holdings not set
4020 startedFromSetupPosition =
4021 !CompareBoards(board, initialPosition);
4022 if(startedFromSetupPosition)
4023 initialRulePlies = irrev_count; /* [HGM] 50-move counter offset */
4026 /* [HGM] Set castling rights. Take the outermost Rooks,
4027 to make it also work for FRC opening positions. Note that board12
4028 is really defective for later FRC positions, as it has no way to
4029 indicate which Rook can castle if they are on the same side of King.
4030 For the initial position we grant rights to the outermost Rooks,
4031 and remember thos rights, and we then copy them on positions
4032 later in an FRC game. This means WB might not recognize castlings with
4033 Rooks that have moved back to their original position as illegal,
4034 but in ICS mode that is not its job anyway.
4036 if(moveNum == 0 || gameInfo.variant != VariantFischeRandom)
4037 { int i, j; ChessSquare wKing = WhiteKing, bKing = BlackKing;
4039 for(i=BOARD_LEFT, j=NoRights; i<BOARD_RGHT; i++)
4040 if(board[0][i] == WhiteRook) j = i;
4041 initialRights[0] = boards[moveNum][CASTLING][0] = (castle_ws == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4042 for(i=BOARD_RGHT-1, j=NoRights; i>=BOARD_LEFT; i--)
4043 if(board[0][i] == WhiteRook) j = i;
4044 initialRights[1] = boards[moveNum][CASTLING][1] = (castle_wl == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4045 for(i=BOARD_LEFT, j=NoRights; i<BOARD_RGHT; i++)
4046 if(board[BOARD_HEIGHT-1][i] == BlackRook) j = i;
4047 initialRights[3] = boards[moveNum][CASTLING][3] = (castle_bs == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4048 for(i=BOARD_RGHT-1, j=NoRights; i>=BOARD_LEFT; i--)
4049 if(board[BOARD_HEIGHT-1][i] == BlackRook) j = i;
4050 initialRights[4] = boards[moveNum][CASTLING][4] = (castle_bl == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4052 if(gameInfo.variant == VariantKnightmate) { wKing = WhiteUnicorn; bKing = BlackUnicorn; }
4053 for(k=BOARD_LEFT; k<BOARD_RGHT; k++)
4054 if(board[0][k] == wKing) initialRights[2] = boards[moveNum][CASTLING][2] = k;
4055 for(k=BOARD_LEFT; k<BOARD_RGHT; k++)
4056 if(board[BOARD_HEIGHT-1][k] == bKing)
4057 initialRights[5] = boards[moveNum][CASTLING][5] = k;
4058 if(gameInfo.variant == VariantTwoKings) {
4059 // In TwoKings looking for a King does not work, so always give castling rights to a King on e1/e8
4060 if(board[0][4] == wKing) initialRights[2] = boards[moveNum][CASTLING][2] = 4;
4061 if(board[BOARD_HEIGHT-1][4] == bKing) initialRights[5] = boards[moveNum][CASTLING][5] = 4;
4064 r = boards[moveNum][CASTLING][0] = initialRights[0];
4065 if(board[0][r] != WhiteRook) boards[moveNum][CASTLING][0] = NoRights;
4066 r = boards[moveNum][CASTLING][1] = initialRights[1];
4067 if(board[0][r] != WhiteRook) boards[moveNum][CASTLING][1] = NoRights;
4068 r = boards[moveNum][CASTLING][3] = initialRights[3];
4069 if(board[BOARD_HEIGHT-1][r] != BlackRook) boards[moveNum][CASTLING][3] = NoRights;
4070 r = boards[moveNum][CASTLING][4] = initialRights[4];
4071 if(board[BOARD_HEIGHT-1][r] != BlackRook) boards[moveNum][CASTLING][4] = NoRights;
4072 /* wildcastle kludge: always assume King has rights */
4073 r = boards[moveNum][CASTLING][2] = initialRights[2];
4074 r = boards[moveNum][CASTLING][5] = initialRights[5];
4076 /* [HGM] e.p. rights. Assume that ICS sends file number here? */
4077 boards[moveNum][EP_STATUS] = double_push == -1 ? EP_NONE : double_push + BOARD_LEFT;
4080 if (ics_getting_history == H_GOT_REQ_HEADER ||
4081 ics_getting_history == H_GOT_UNREQ_HEADER) {
4082 /* This was an initial position from a move list, not
4083 the current position */
4087 /* Update currentMove and known move number limits */
4088 newMove = newGame || moveNum > forwardMostMove;
4091 forwardMostMove = backwardMostMove = currentMove = moveNum;
4092 if (gameMode == IcsExamining && moveNum == 0) {
4093 /* Workaround for ICS limitation: we are not told the wild
4094 type when starting to examine a game. But if we ask for
4095 the move list, the move list header will tell us */
4096 ics_getting_history = H_REQUESTED;
4097 sprintf(str, "%smoves %d\n", ics_prefix, gamenum);
4100 } else if (moveNum == forwardMostMove + 1 || moveNum == forwardMostMove
4101 || (moveNum < forwardMostMove && moveNum >= backwardMostMove)) {
4103 /* [DM] If we found takebacks during icsEngineAnalyze try send to engine */
4104 /* [HGM] applied this also to an engine that is silently watching */
4105 if (appData.zippyPlay && moveNum < forwardMostMove && first.initDone &&
4106 (gameMode == IcsObserving || gameMode == IcsExamining) &&
4107 gameInfo.variant == currentlyInitializedVariant) {
4108 takeback = forwardMostMove - moveNum;
4109 for (i = 0; i < takeback; i++) {
4110 if (appData.debugMode) fprintf(debugFP, "take back move\n");
4111 SendToProgram("undo\n", &first);
4116 forwardMostMove = moveNum;
4117 if (!pausing || currentMove > forwardMostMove)
4118 currentMove = forwardMostMove;
4120 /* New part of history that is not contiguous with old part */
4121 if (pausing && gameMode == IcsExamining) {
4122 pauseExamInvalid = TRUE;
4123 forwardMostMove = pauseExamForwardMostMove;
4126 if (gameMode == IcsExamining && moveNum > 0 && appData.getMoveList) {
4128 if(appData.zippyPlay && forwardMostMove > 0 && first.initDone) {
4129 // [HGM] when we will receive the move list we now request, it will be
4130 // fed to the engine from the first move on. So if the engine is not
4131 // in the initial position now, bring it there.
4132 InitChessProgram(&first, 0);
4135 ics_getting_history = H_REQUESTED;
4136 sprintf(str, "%smoves %d\n", ics_prefix, gamenum);
4139 forwardMostMove = backwardMostMove = currentMove = moveNum;
4142 /* Update the clocks */
4143 if (strchr(elapsed_time, '.')) {
4145 timeRemaining[0][moveNum] = whiteTimeRemaining = white_time;
4146 timeRemaining[1][moveNum] = blackTimeRemaining = black_time;
4148 /* Time is in seconds */
4149 timeRemaining[0][moveNum] = whiteTimeRemaining = white_time * 1000;
4150 timeRemaining[1][moveNum] = blackTimeRemaining = black_time * 1000;
4155 if (appData.zippyPlay && newGame &&
4156 gameMode != IcsObserving && gameMode != IcsIdle &&
4157 gameMode != IcsExamining)
4158 ZippyFirstBoard(moveNum, basetime, increment);
4161 /* Put the move on the move list, first converting
4162 to canonical algebraic form. */
4164 if (appData.debugMode) {
4165 if (appData.debugMode) { int f = forwardMostMove;
4166 fprintf(debugFP, "parseboard %d, castling = %d %d %d %d %d %d\n", f,
4167 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
4168 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
4170 fprintf(debugFP, "accepted move %s from ICS, parse it.\n", move_str);
4171 fprintf(debugFP, "moveNum = %d\n", moveNum);
4172 fprintf(debugFP, "board = %d-%d x %d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT);
4173 setbuf(debugFP, NULL);
4175 if (moveNum <= backwardMostMove) {
4176 /* We don't know what the board looked like before
4178 strcpy(parseList[moveNum - 1], move_str);
4179 strcat(parseList[moveNum - 1], " ");
4180 strcat(parseList[moveNum - 1], elapsed_time);
4181 moveList[moveNum - 1][0] = NULLCHAR;
4182 } else if (strcmp(move_str, "none") == 0) {
4183 // [HGM] long SAN: swapped order; test for 'none' before parsing move
4184 /* Again, we don't know what the board looked like;
4185 this is really the start of the game. */
4186 parseList[moveNum - 1][0] = NULLCHAR;
4187 moveList[moveNum - 1][0] = NULLCHAR;
4188 backwardMostMove = moveNum;
4189 startedFromSetupPosition = TRUE;
4190 fromX = fromY = toX = toY = -1;
4192 // [HGM] long SAN: if legality-testing is off, disambiguation might not work or give wrong move.
4193 // So we parse the long-algebraic move string in stead of the SAN move
4194 int valid; char buf[MSG_SIZ], *prom;
4196 // str looks something like "Q/a1-a2"; kill the slash
4198 sprintf(buf, "%c%s", str[0], str+2);
4199 else strcpy(buf, str); // might be castling
4200 if((prom = strstr(move_str, "=")) && !strstr(buf, "="))
4201 strcat(buf, prom); // long move lacks promo specification!
4202 if(!appData.testLegality && move_str[1] != '@') { // drops never ambiguous (parser chokes on long form!)
4203 if(appData.debugMode)
4204 fprintf(debugFP, "replaced ICS move '%s' by '%s'\n", move_str, buf);
4205 strcpy(move_str, buf);
4207 valid = ParseOneMove(move_str, moveNum - 1, &moveType,
4208 &fromX, &fromY, &toX, &toY, &promoChar)
4209 || ParseOneMove(buf, moveNum - 1, &moveType,
4210 &fromX, &fromY, &toX, &toY, &promoChar);
4211 // end of long SAN patch
4213 (void) CoordsToAlgebraic(boards[moveNum - 1],
4214 PosFlags(moveNum - 1),
4215 fromY, fromX, toY, toX, promoChar,
4216 parseList[moveNum-1]);
4217 switch (MateTest(boards[moveNum], PosFlags(moveNum)) ) {
4223 if(gameInfo.variant != VariantShogi)
4224 strcat(parseList[moveNum - 1], "+");
4227 case MT_STAINMATE: // [HGM] xq: for notation stalemate that wins counts as checkmate
4228 strcat(parseList[moveNum - 1], "#");
4231 strcat(parseList[moveNum - 1], " ");
4232 strcat(parseList[moveNum - 1], elapsed_time);
4233 /* currentMoveString is set as a side-effect of ParseOneMove */
4234 strcpy(moveList[moveNum - 1], currentMoveString);
4235 strcat(moveList[moveNum - 1], "\n");
4237 /* Move from ICS was illegal!? Punt. */
4238 if (appData.debugMode) {
4239 fprintf(debugFP, "Illegal move from ICS '%s'\n", move_str);
4240 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
4242 strcpy(parseList[moveNum - 1], move_str);
4243 strcat(parseList[moveNum - 1], " ");
4244 strcat(parseList[moveNum - 1], elapsed_time);
4245 moveList[moveNum - 1][0] = NULLCHAR;
4246 fromX = fromY = toX = toY = -1;
4249 if (appData.debugMode) {
4250 fprintf(debugFP, "Move parsed to '%s'\n", parseList[moveNum - 1]);
4251 setbuf(debugFP, NULL);
4255 /* Send move to chess program (BEFORE animating it). */
4256 if (appData.zippyPlay && !newGame && newMove &&
4257 (!appData.getMoveList || backwardMostMove == 0) && first.initDone) {
4259 if ((gameMode == IcsPlayingWhite && WhiteOnMove(moveNum)) ||
4260 (gameMode == IcsPlayingBlack && !WhiteOnMove(moveNum))) {
4261 if (moveList[moveNum - 1][0] == NULLCHAR) {
4262 sprintf(str, _("Couldn't parse move \"%s\" from ICS"),
4264 DisplayError(str, 0);
4266 if (first.sendTime) {
4267 SendTimeRemaining(&first, gameMode == IcsPlayingWhite);
4269 bookHit = SendMoveToBookUser(moveNum - 1, &first, FALSE); // [HGM] book
4270 if (firstMove && !bookHit) {
4272 if (first.useColors) {
4273 SendToProgram(gameMode == IcsPlayingWhite ?
4275 "black\ngo\n", &first);
4277 SendToProgram("go\n", &first);
4279 first.maybeThinking = TRUE;
4282 } else if (gameMode == IcsObserving || gameMode == IcsExamining) {
4283 if (moveList[moveNum - 1][0] == NULLCHAR) {
4284 sprintf(str, _("Couldn't parse move \"%s\" from ICS"), move_str);
4285 DisplayError(str, 0);
4287 if(gameInfo.variant == currentlyInitializedVariant) // [HGM] refrain sending moves engine can't understand!
4288 SendMoveToProgram(moveNum - 1, &first);
4295 if (moveNum > 0 && !gotPremove && !appData.noGUI) {
4296 /* If move comes from a remote source, animate it. If it
4297 isn't remote, it will have already been animated. */
4298 if (!pausing && !ics_user_moved && prevMove == moveNum - 1) {
4299 AnimateMove(boards[moveNum - 1], fromX, fromY, toX, toY);
4301 if (!pausing && appData.highlightLastMove) {
4302 SetHighlights(fromX, fromY, toX, toY);
4306 /* Start the clocks */
4307 whiteFlag = blackFlag = FALSE;
4308 appData.clockMode = !(basetime == 0 && increment == 0);
4310 ics_clock_paused = TRUE;
4312 } else if (ticking == 1) {
4313 ics_clock_paused = FALSE;
4315 if (gameMode == IcsIdle ||
4316 relation == RELATION_OBSERVING_STATIC ||
4317 relation == RELATION_EXAMINING ||
4319 DisplayBothClocks();
4323 /* Display opponents and material strengths */
4324 if (gameInfo.variant != VariantBughouse &&
4325 gameInfo.variant != VariantCrazyhouse && !appData.noGUI) {
4326 if (tinyLayout || smallLayout) {
4327 if(gameInfo.variant == VariantNormal)
4328 sprintf(str, "%s(%d) %s(%d) {%d %d}",
4329 gameInfo.white, white_stren, gameInfo.black, black_stren,
4330 basetime, increment);
4332 sprintf(str, "%s(%d) %s(%d) {%d %d w%d}",
4333 gameInfo.white, white_stren, gameInfo.black, black_stren,
4334 basetime, increment, (int) gameInfo.variant);
4336 if(gameInfo.variant == VariantNormal)
4337 sprintf(str, "%s (%d) vs. %s (%d) {%d %d}",
4338 gameInfo.white, white_stren, gameInfo.black, black_stren,
4339 basetime, increment);
4341 sprintf(str, "%s (%d) vs. %s (%d) {%d %d %s}",
4342 gameInfo.white, white_stren, gameInfo.black, black_stren,
4343 basetime, increment, VariantName(gameInfo.variant));
4346 if (appData.debugMode) {
4347 fprintf(debugFP, "Display title '%s, gameInfo.variant = %d'\n", str, gameInfo.variant);
4352 /* Display the board */
4353 if (!pausing && !appData.noGUI) {
4355 if (appData.premove)
4357 ((gameMode == IcsPlayingWhite) && (WhiteOnMove(currentMove))) ||
4358 ((gameMode == IcsPlayingBlack) && (!WhiteOnMove(currentMove))))
4359 ClearPremoveHighlights();
4361 j = seekGraphUp; seekGraphUp = FALSE; // [HGM] seekgraph: when we draw a board, it overwrites the seek graph
4362 DrawPosition(j, boards[currentMove]);
4364 DisplayMove(moveNum - 1);
4365 if (appData.ringBellAfterMoves && /*!ics_user_moved*/ // [HGM] use absolute method to recognize own move
4366 !((gameMode == IcsPlayingWhite) && (!WhiteOnMove(moveNum)) ||
4367 (gameMode == IcsPlayingBlack) && (WhiteOnMove(moveNum)) ) ) {
4368 if(newMove) RingBell(); else PlayIcsUnfinishedSound();
4372 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
4374 if(bookHit) { // [HGM] book: simulate book reply
4375 static char bookMove[MSG_SIZ]; // a bit generous?
4377 programStats.nodes = programStats.depth = programStats.time =
4378 programStats.score = programStats.got_only_move = 0;
4379 sprintf(programStats.movelist, "%s (xbook)", bookHit);
4381 strcpy(bookMove, "move ");
4382 strcat(bookMove, bookHit);
4383 HandleMachineMove(bookMove, &first);
4392 if (appData.icsActive && gameMode != IcsIdle && ics_gamenum > 0) {
4393 ics_getting_history = H_REQUESTED;
4394 sprintf(buf, "%smoves %d\n", ics_prefix, ics_gamenum);
4400 AnalysisPeriodicEvent(force)
4403 if (((programStats.ok_to_send == 0 || programStats.line_is_book)
4404 && !force) || !appData.periodicUpdates)
4407 /* Send . command to Crafty to collect stats */
4408 SendToProgram(".\n", &first);
4410 /* Don't send another until we get a response (this makes
4411 us stop sending to old Crafty's which don't understand
4412 the "." command (sending illegal cmds resets node count & time,
4413 which looks bad)) */
4414 programStats.ok_to_send = 0;
4417 void ics_update_width(new_width)
4420 ics_printf("set width %d\n", new_width);
4424 SendMoveToProgram(moveNum, cps)
4426 ChessProgramState *cps;
4430 if (cps->useUsermove) {
4431 SendToProgram("usermove ", cps);
4435 if ((space = strchr(parseList[moveNum], ' ')) != NULL) {
4436 int len = space - parseList[moveNum];
4437 memcpy(buf, parseList[moveNum], len);
4439 buf[len] = NULLCHAR;
4441 sprintf(buf, "%s\n", parseList[moveNum]);
4443 SendToProgram(buf, cps);
4445 if(cps->alphaRank) { /* [HGM] shogi: temporarily convert to shogi coordinates before sending */
4446 AlphaRank(moveList[moveNum], 4);
4447 SendToProgram(moveList[moveNum], cps);
4448 AlphaRank(moveList[moveNum], 4); // and back
4450 /* Added by Tord: Send castle moves in "O-O" in FRC games if required by
4451 * the engine. It would be nice to have a better way to identify castle
4453 if((gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom)
4454 && cps->useOOCastle) {
4455 int fromX = moveList[moveNum][0] - AAA;
4456 int fromY = moveList[moveNum][1] - ONE;
4457 int toX = moveList[moveNum][2] - AAA;
4458 int toY = moveList[moveNum][3] - ONE;
4459 if((boards[moveNum][fromY][fromX] == WhiteKing
4460 && boards[moveNum][toY][toX] == WhiteRook)
4461 || (boards[moveNum][fromY][fromX] == BlackKing
4462 && boards[moveNum][toY][toX] == BlackRook)) {
4463 if(toX > fromX) SendToProgram("O-O\n", cps);
4464 else SendToProgram("O-O-O\n", cps);
4466 else SendToProgram(moveList[moveNum], cps);
4468 else SendToProgram(moveList[moveNum], cps);
4469 /* End of additions by Tord */
4472 /* [HGM] setting up the opening has brought engine in force mode! */
4473 /* Send 'go' if we are in a mode where machine should play. */
4474 if( (moveNum == 0 && setboardSpoiledMachineBlack && cps == &first) &&
4475 (gameMode == TwoMachinesPlay ||
4477 gameMode == IcsPlayingBlack || gameMode == IcsPlayingWhite ||
4479 gameMode == MachinePlaysBlack || gameMode == MachinePlaysWhite) ) {
4480 SendToProgram("go\n", cps);
4481 if (appData.debugMode) {
4482 fprintf(debugFP, "(extra)\n");
4485 setboardSpoiledMachineBlack = 0;
4489 SendMoveToICS(moveType, fromX, fromY, toX, toY)
4491 int fromX, fromY, toX, toY;
4493 char user_move[MSG_SIZ];
4497 sprintf(user_move, _("say Internal error; bad moveType %d (%d,%d-%d,%d)"),
4498 (int)moveType, fromX, fromY, toX, toY);
4499 DisplayError(user_move + strlen("say "), 0);
4501 case WhiteKingSideCastle:
4502 case BlackKingSideCastle:
4503 case WhiteQueenSideCastleWild:
4504 case BlackQueenSideCastleWild:
4506 case WhiteHSideCastleFR:
4507 case BlackHSideCastleFR:
4509 sprintf(user_move, "o-o\n");
4511 case WhiteQueenSideCastle:
4512 case BlackQueenSideCastle:
4513 case WhiteKingSideCastleWild:
4514 case BlackKingSideCastleWild:
4516 case WhiteASideCastleFR:
4517 case BlackASideCastleFR:
4519 sprintf(user_move, "o-o-o\n");
4521 case WhitePromotionQueen:
4522 case BlackPromotionQueen:
4523 case WhitePromotionRook:
4524 case BlackPromotionRook:
4525 case WhitePromotionBishop:
4526 case BlackPromotionBishop:
4527 case WhitePromotionKnight:
4528 case BlackPromotionKnight:
4529 case WhitePromotionKing:
4530 case BlackPromotionKing:
4531 case WhitePromotionChancellor:
4532 case BlackPromotionChancellor:
4533 case WhitePromotionArchbishop:
4534 case BlackPromotionArchbishop:
4535 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
4536 sprintf(user_move, "%c%c%c%c=%c\n",
4537 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
4538 PieceToChar(WhiteFerz));
4539 else if(gameInfo.variant == VariantGreat)
4540 sprintf(user_move, "%c%c%c%c=%c\n",
4541 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
4542 PieceToChar(WhiteMan));
4544 sprintf(user_move, "%c%c%c%c=%c\n",
4545 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
4546 PieceToChar(PromoPiece(moveType)));
4550 sprintf(user_move, "%c@%c%c\n",
4551 ToUpper(PieceToChar((ChessSquare) fromX)),
4552 AAA + toX, ONE + toY);
4555 case WhiteCapturesEnPassant:
4556 case BlackCapturesEnPassant:
4557 case IllegalMove: /* could be a variant we don't quite understand */
4558 sprintf(user_move, "%c%c%c%c\n",
4559 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY);
4562 SendToICS(user_move);
4563 if(appData.keepAlive) // [HGM] alive: schedule sending of dummy 'date' command
4564 ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
4569 { // [HGM] upload: send entire stored game to ICS as long-algebraic moves.
4570 int i, last = forwardMostMove; // make sure ICS reply cannot pre-empt us by clearing fmm
4571 static char *castlingStrings[4] = { "none", "kside", "qside", "both" };
4572 if(gameMode == IcsObserving || gameMode == IcsPlayingBlack || gameMode == IcsPlayingWhite) {
4573 DisplayError("You cannot do this while you are playing or observing", 0);
4576 if(gameMode != IcsExamining) { // is this ever not the case?
4577 char buf[MSG_SIZ], *p, *fen, command[MSG_SIZ], bsetup = 0;
4579 if(ics_type == ICS_ICC) { // on ICC match ourselves in applicable variant
4580 sprintf(command, "match %s", ics_handle);
4581 } else { // on FICS we must first go to general examine mode
4582 strcpy(command, "examine\nbsetup"); // and specify variant within it with bsetups
4584 if(gameInfo.variant != VariantNormal) {
4585 // try figure out wild number, as xboard names are not always valid on ICS
4586 for(i=1; i<=36; i++) {
4587 sprintf(buf, "wild/%d", i);
4588 if(StringToVariant(buf) == gameInfo.variant) break;
4590 if(i<=36 && ics_type == ICS_ICC) sprintf(buf, "%s w%d\n", command, i);
4591 else if(i == 22) sprintf(buf, "%s fr\n", command);
4592 else sprintf(buf, "%s %s\n", command, VariantName(gameInfo.variant));
4593 } else sprintf(buf, "%s\n", ics_type == ICS_ICC ? command : "examine\n"); // match yourself or examine
4594 SendToICS(ics_prefix);
4596 if(startedFromSetupPosition || backwardMostMove != 0) {
4597 fen = PositionToFEN(backwardMostMove, NULL);
4598 if(ics_type == ICS_ICC) { // on ICC we can simply send a complete FEN to set everything
4599 sprintf(buf, "loadfen %s\n", fen);
4601 } else { // FICS: everything has to set by separate bsetup commands
4602 p = strchr(fen, ' '); p[0] = NULLCHAR; // cut after board
4603 sprintf(buf, "bsetup fen %s\n", fen);
4605 if(!WhiteOnMove(backwardMostMove)) {
4606 SendToICS("bsetup tomove black\n");
4608 i = (strchr(p+3, 'K') != NULL) + 2*(strchr(p+3, 'Q') != NULL);
4609 sprintf(buf, "bsetup wcastle %s\n", castlingStrings[i]);
4611 i = (strchr(p+3, 'k') != NULL) + 2*(strchr(p+3, 'q') != NULL);
4612 sprintf(buf, "bsetup bcastle %s\n", castlingStrings[i]);
4614 i = boards[backwardMostMove][EP_STATUS];
4615 if(i >= 0) { // set e.p.
4616 sprintf(buf, "bsetup eppos %c\n", i+AAA);
4622 if(bsetup || ics_type != ICS_ICC && gameInfo.variant != VariantNormal)
4623 SendToICS("bsetup done\n"); // switch to normal examining.
4625 for(i = backwardMostMove; i<last; i++) {
4627 sprintf(buf, "%s\n", parseList[i]);
4630 SendToICS(ics_prefix);
4631 SendToICS(ics_type == ICS_ICC ? "tag result Game in progress\n" : "commit\n");
4635 CoordsToComputerAlgebraic(rf, ff, rt, ft, promoChar, move)
4640 if (rf == DROP_RANK) {
4641 sprintf(move, "%c@%c%c\n",
4642 ToUpper(PieceToChar((ChessSquare) ff)), AAA + ft, ONE + rt);
4644 if (promoChar == 'x' || promoChar == NULLCHAR) {
4645 sprintf(move, "%c%c%c%c\n",
4646 AAA + ff, ONE + rf, AAA + ft, ONE + rt);
4648 sprintf(move, "%c%c%c%c%c\n",
4649 AAA + ff, ONE + rf, AAA + ft, ONE + rt, promoChar);
4655 ProcessICSInitScript(f)
4660 while (fgets(buf, MSG_SIZ, f)) {
4661 SendToICSDelayed(buf,(long)appData.msLoginDelay);
4668 /* [HGM] Shogi move preprocessor: swap digits for letters, vice versa */
4670 AlphaRank(char *move, int n)
4672 // char *p = move, c; int x, y;
4674 if (appData.debugMode) {
4675 fprintf(debugFP, "alphaRank(%s,%d)\n", move, n);
4679 move[2]>='0' && move[2]<='9' &&
4680 move[3]>='a' && move[3]<='x' ) {
4682 move[2] = BOARD_RGHT -1 - (move[2]-'1') + AAA;
4683 move[3] = BOARD_HEIGHT-1 - (move[3]-'a') + ONE;
4685 if(move[0]>='0' && move[0]<='9' &&
4686 move[1]>='a' && move[1]<='x' &&
4687 move[2]>='0' && move[2]<='9' &&
4688 move[3]>='a' && move[3]<='x' ) {
4689 /* input move, Shogi -> normal */
4690 move[0] = BOARD_RGHT -1 - (move[0]-'1') + AAA;
4691 move[1] = BOARD_HEIGHT-1 - (move[1]-'a') + ONE;
4692 move[2] = BOARD_RGHT -1 - (move[2]-'1') + AAA;
4693 move[3] = BOARD_HEIGHT-1 - (move[3]-'a') + ONE;
4696 move[3]>='0' && move[3]<='9' &&
4697 move[2]>='a' && move[2]<='x' ) {
4699 move[2] = BOARD_RGHT - 1 - (move[2]-AAA) + '1';
4700 move[3] = BOARD_HEIGHT-1 - (move[3]-ONE) + 'a';
4703 move[0]>='a' && move[0]<='x' &&
4704 move[3]>='0' && move[3]<='9' &&
4705 move[2]>='a' && move[2]<='x' ) {
4706 /* output move, normal -> Shogi */
4707 move[0] = BOARD_RGHT - 1 - (move[0]-AAA) + '1';
4708 move[1] = BOARD_HEIGHT-1 - (move[1]-ONE) + 'a';
4709 move[2] = BOARD_RGHT - 1 - (move[2]-AAA) + '1';
4710 move[3] = BOARD_HEIGHT-1 - (move[3]-ONE) + 'a';
4711 if(move[4] == PieceToChar(BlackQueen)) move[4] = '+';
4713 if (appData.debugMode) {
4714 fprintf(debugFP, " out = '%s'\n", move);
4718 char yy_textstr[8000];
4720 /* Parser for moves from gnuchess, ICS, or user typein box */
4722 ParseOneMove(move, moveNum, moveType, fromX, fromY, toX, toY, promoChar)
4725 ChessMove *moveType;
4726 int *fromX, *fromY, *toX, *toY;
4729 if (appData.debugMode) {
4730 fprintf(debugFP, "move to parse: %s\n", move);
4732 *moveType = yylexstr(moveNum, move, yy_textstr, sizeof yy_textstr);
4734 switch (*moveType) {
4735 case WhitePromotionChancellor:
4736 case BlackPromotionChancellor:
4737 case WhitePromotionArchbishop:
4738 case BlackPromotionArchbishop:
4739 case WhitePromotionQueen:
4740 case BlackPromotionQueen:
4741 case WhitePromotionRook:
4742 case BlackPromotionRook:
4743 case WhitePromotionBishop:
4744 case BlackPromotionBishop:
4745 case WhitePromotionKnight:
4746 case BlackPromotionKnight:
4747 case WhitePromotionKing:
4748 case BlackPromotionKing:
4750 case WhiteCapturesEnPassant:
4751 case BlackCapturesEnPassant:
4752 case WhiteKingSideCastle:
4753 case WhiteQueenSideCastle:
4754 case BlackKingSideCastle:
4755 case BlackQueenSideCastle:
4756 case WhiteKingSideCastleWild:
4757 case WhiteQueenSideCastleWild:
4758 case BlackKingSideCastleWild:
4759 case BlackQueenSideCastleWild:
4760 /* Code added by Tord: */
4761 case WhiteHSideCastleFR:
4762 case WhiteASideCastleFR:
4763 case BlackHSideCastleFR:
4764 case BlackASideCastleFR:
4765 /* End of code added by Tord */
4766 case IllegalMove: /* bug or odd chess variant */
4767 *fromX = currentMoveString[0] - AAA;
4768 *fromY = currentMoveString[1] - ONE;
4769 *toX = currentMoveString[2] - AAA;
4770 *toY = currentMoveString[3] - ONE;
4771 *promoChar = currentMoveString[4];
4772 if (*fromX < BOARD_LEFT || *fromX >= BOARD_RGHT || *fromY < 0 || *fromY >= BOARD_HEIGHT ||
4773 *toX < BOARD_LEFT || *toX >= BOARD_RGHT || *toY < 0 || *toY >= BOARD_HEIGHT) {
4774 if (appData.debugMode) {
4775 fprintf(debugFP, "Off-board move (%d,%d)-(%d,%d)%c, type = %d\n", *fromX, *fromY, *toX, *toY, *promoChar, *moveType);
4777 *fromX = *fromY = *toX = *toY = 0;
4780 if (appData.testLegality) {
4781 return (*moveType != IllegalMove);
4783 return !(*fromX == *toX && *fromY == *toY) && boards[moveNum][*fromY][*fromX] != EmptySquare &&
4784 WhiteOnMove(moveNum) == (boards[moveNum][*fromY][*fromX] < BlackPawn);
4789 *fromX = *moveType == WhiteDrop ?
4790 (int) CharToPiece(ToUpper(currentMoveString[0])) :
4791 (int) CharToPiece(ToLower(currentMoveString[0]));
4793 *toX = currentMoveString[2] - AAA;
4794 *toY = currentMoveString[3] - ONE;
4795 *promoChar = NULLCHAR;
4799 case ImpossibleMove:
4800 case (ChessMove) 0: /* end of file */
4809 if (appData.debugMode) {
4810 fprintf(debugFP, "Impossible move %s, type = %d\n", currentMoveString, *moveType);
4813 *fromX = *fromY = *toX = *toY = 0;
4814 *promoChar = NULLCHAR;
4821 ParsePV(char *pv, Boolean storeComments)
4822 { // Parse a string of PV moves, and append to current game, behind forwardMostMove
4823 int fromX, fromY, toX, toY; char promoChar;
4828 endPV = forwardMostMove;
4830 while(*pv == ' ' || *pv == '\n' || *pv == '\t') pv++; // must still read away whitespace
4831 if(nr == 0 && !storeComments && *pv == '(') pv++; // first (ponder) move can be in parentheses
4832 valid = ParseOneMove(pv, endPV, &moveType, &fromX, &fromY, &toX, &toY, &promoChar);
4833 if(appData.debugMode){
4834 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);
4836 if(!valid && nr == 0 &&
4837 ParseOneMove(pv, endPV-1, &moveType, &fromX, &fromY, &toX, &toY, &promoChar)){
4838 nr++; moveType = Comment; // First move has been played; kludge to make sure we continue
4839 // Hande case where played move is different from leading PV move
4840 CopyBoard(boards[endPV+1], boards[endPV-1]); // tentatively unplay last game move
4841 CopyBoard(boards[endPV+2], boards[endPV-1]); // and play first move of PV
4842 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[endPV+2]);
4843 if(!CompareBoards(boards[endPV], boards[endPV+2])) {
4844 endPV += 2; // if position different, keep this
4845 moveList[endPV-1][0] = fromX + AAA;
4846 moveList[endPV-1][1] = fromY + ONE;
4847 moveList[endPV-1][2] = toX + AAA;
4848 moveList[endPV-1][3] = toY + ONE;
4849 parseList[endPV-1][0] = NULLCHAR;
4850 strcpy(moveList[endPV-2], "_0_0"); // suppress premove highlight on takeback move
4853 pv = strstr(pv, yy_textstr) + strlen(yy_textstr); // skip what we parsed
4854 if(nr == 0 && !storeComments && *pv == ')') pv++; // closing parenthesis of ponder move;
4855 if(moveType == Comment && storeComments) AppendComment(endPV, yy_textstr, FALSE);
4856 if(moveType == Comment || moveType == NAG || moveType == ElapsedTime) {
4857 valid++; // allow comments in PV
4861 if(endPV+1 > framePtr) break; // no space, truncate
4864 CopyBoard(boards[endPV], boards[endPV-1]);
4865 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[endPV]);
4866 moveList[endPV-1][0] = fromX + AAA;
4867 moveList[endPV-1][1] = fromY + ONE;
4868 moveList[endPV-1][2] = toX + AAA;
4869 moveList[endPV-1][3] = toY + ONE;
4871 CoordsToAlgebraic(boards[endPV - 1],
4872 PosFlags(endPV - 1),
4873 fromY, fromX, toY, toX, promoChar,
4874 parseList[endPV - 1]);
4876 parseList[endPV-1][0] = NULLCHAR;
4878 currentMove = endPV;
4879 if(currentMove == forwardMostMove) ClearPremoveHighlights(); else
4880 SetPremoveHighlights(moveList[currentMove-1][0]-AAA, moveList[currentMove-1][1]-ONE,
4881 moveList[currentMove-1][2]-AAA, moveList[currentMove-1][3]-ONE);
4882 DrawPosition(TRUE, boards[currentMove]);
4885 static int lastX, lastY;
4888 LoadMultiPV(int x, int y, char *buf, int index, int *start, int *end)
4893 if(index < 0 || index >= strlen(buf)) return FALSE; // sanity
4894 lastX = x; lastY = y;
4895 while(index > 0 && buf[index-1] != '\n') index--; // beginning of line
4897 while(buf[index] != '\n') if(buf[index++] == '\t') startPV = index;
4898 if(index == startPV && (p = StrCaseStr(buf+index, "PV="))) startPV = p - buf + 3;
4900 do{ while(buf[index] && buf[index] != '\n') index++;
4901 } while(buf[index] == '\n' && buf[index+1] == '\\' && buf[index+2] == ' ' && index++); // join kibitzed PV continuation line
4903 ParsePV(buf+startPV, FALSE);
4904 *start = startPV; *end = index-1;
4909 LoadPV(int x, int y)
4910 { // called on right mouse click to load PV
4911 int which = gameMode == TwoMachinesPlay && (WhiteOnMove(forwardMostMove) == (second.twoMachinesColor[0] == 'w'));
4912 lastX = x; lastY = y;
4913 ParsePV(lastPV[which], FALSE); // load the PV of the thinking engine in the boards array.
4920 if(endPV < 0) return;
4922 currentMove = forwardMostMove;
4923 ClearPremoveHighlights();
4924 DrawPosition(TRUE, boards[currentMove]);
4928 MovePV(int x, int y, int h)
4929 { // step through PV based on mouse coordinates (called on mouse move)
4930 int margin = h>>3, step = 0;
4932 if(endPV < 0) return;
4933 // we must somehow check if right button is still down (might be released off board!)
4934 if(y < margin && (abs(x - lastX) > 6 || abs(y - lastY) > 6)) step = 1; else
4935 if(y > h - margin && (abs(x - lastX) > 6 || abs(y - lastY) > 6)) step = -1; else
4936 if( y > lastY + 6 ) step = -1; else if(y < lastY - 6) step = 1;
4938 lastX = x; lastY = y;
4939 if(currentMove + step > endPV || currentMove + step < forwardMostMove) step = 0;
4940 currentMove += step;
4941 if(currentMove == forwardMostMove) ClearPremoveHighlights(); else
4942 SetPremoveHighlights(moveList[currentMove-1][0]-AAA, moveList[currentMove-1][1]-ONE,
4943 moveList[currentMove-1][2]-AAA, moveList[currentMove-1][3]-ONE);
4944 DrawPosition(FALSE, boards[currentMove]);
4948 // [HGM] shuffle: a general way to suffle opening setups, applicable to arbitrary variants.
4949 // All positions will have equal probability, but the current method will not provide a unique
4950 // numbering scheme for arrays that contain 3 or more pieces of the same kind.
4956 int piecesLeft[(int)BlackPawn];
4957 int seed, nrOfShuffles;
4959 void GetPositionNumber()
4960 { // sets global variable seed
4963 seed = appData.defaultFrcPosition;
4964 if(seed < 0) { // randomize based on time for negative FRC position numbers
4965 for(i=0; i<50; i++) seed += random();
4966 seed = random() ^ random() >> 8 ^ random() << 8;
4967 if(seed<0) seed = -seed;
4971 int put(Board board, int pieceType, int rank, int n, int shade)
4972 // put the piece on the (n-1)-th empty squares of the given shade
4976 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) {
4977 if( (((i-BOARD_LEFT)&1)+1) & shade && board[rank][i] == EmptySquare && n-- == 0) {
4978 board[rank][i] = (ChessSquare) pieceType;
4979 squaresLeft[((i-BOARD_LEFT)&1) + 1]--;
4981 piecesLeft[pieceType]--;
4989 void AddOnePiece(Board board, int pieceType, int rank, int shade)
4990 // calculate where the next piece goes, (any empty square), and put it there
4994 i = seed % squaresLeft[shade];
4995 nrOfShuffles *= squaresLeft[shade];
4996 seed /= squaresLeft[shade];
4997 put(board, pieceType, rank, i, shade);
5000 void AddTwoPieces(Board board, int pieceType, int rank)
5001 // calculate where the next 2 identical pieces go, (any empty square), and put it there
5003 int i, n=squaresLeft[ANY], j=n-1, k;
5005 k = n*(n-1)/2; // nr of possibilities, not counting permutations
5006 i = seed % k; // pick one
5009 while(i >= j) i -= j--;
5010 j = n - 1 - j; i += j;
5011 put(board, pieceType, rank, j, ANY);
5012 put(board, pieceType, rank, i, ANY);
5015 void SetUpShuffle(Board board, int number)
5019 GetPositionNumber(); nrOfShuffles = 1;
5021 squaresLeft[DARK] = (BOARD_RGHT - BOARD_LEFT + 1)/2;
5022 squaresLeft[ANY] = BOARD_RGHT - BOARD_LEFT;
5023 squaresLeft[LITE] = squaresLeft[ANY] - squaresLeft[DARK];
5025 for(p = 0; p<=(int)WhiteKing; p++) piecesLeft[p] = 0;
5027 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // count pieces and clear board
5028 p = (int) board[0][i];
5029 if(p < (int) BlackPawn) piecesLeft[p] ++;
5030 board[0][i] = EmptySquare;
5033 if(PosFlags(0) & F_ALL_CASTLE_OK) {
5034 // shuffles restricted to allow normal castling put KRR first
5035 if(piecesLeft[(int)WhiteKing]) // King goes rightish of middle
5036 put(board, WhiteKing, 0, (gameInfo.boardWidth+1)/2, ANY);
5037 else if(piecesLeft[(int)WhiteUnicorn]) // in Knightmate Unicorn castles
5038 put(board, WhiteUnicorn, 0, (gameInfo.boardWidth+1)/2, ANY);
5039 if(piecesLeft[(int)WhiteRook]) // First supply a Rook for K-side castling
5040 put(board, WhiteRook, 0, gameInfo.boardWidth-2, ANY);
5041 if(piecesLeft[(int)WhiteRook]) // Then supply a Rook for Q-side castling
5042 put(board, WhiteRook, 0, 0, ANY);
5043 // in variants with super-numerary Kings and Rooks, we leave these for the shuffle
5046 if(((BOARD_RGHT-BOARD_LEFT) & 1) == 0)
5047 // only for even boards make effort to put pairs of colorbound pieces on opposite colors
5048 for(p = (int) WhiteKing; p > (int) WhitePawn; p--) {
5049 if(p != (int) WhiteBishop && p != (int) WhiteFerz && p != (int) WhiteAlfil) continue;
5050 while(piecesLeft[p] >= 2) {
5051 AddOnePiece(board, p, 0, LITE);
5052 AddOnePiece(board, p, 0, DARK);
5054 // Odd color-bound pieces are shuffled with the rest (to not run out of paired squares)
5057 for(p = (int) WhiteKing - 2; p > (int) WhitePawn; p--) {
5058 // Remaining pieces (non-colorbound, or odd color bound) can be put anywhere
5059 // but we leave King and Rooks for last, to possibly obey FRC restriction
5060 if(p == (int)WhiteRook) continue;
5061 while(piecesLeft[p] >= 2) AddTwoPieces(board, p, 0); // add in pairs, for not counting permutations
5062 if(piecesLeft[p]) AddOnePiece(board, p, 0, ANY); // add the odd piece
5065 // now everything is placed, except perhaps King (Unicorn) and Rooks
5067 if(PosFlags(0) & F_FRC_TYPE_CASTLING) {
5068 // Last King gets castling rights
5069 while(piecesLeft[(int)WhiteUnicorn]) {
5070 i = put(board, WhiteUnicorn, 0, piecesLeft[(int)WhiteRook]/2, ANY);
5071 initialRights[2] = initialRights[5] = board[CASTLING][2] = board[CASTLING][5] = i;
5074 while(piecesLeft[(int)WhiteKing]) {
5075 i = put(board, WhiteKing, 0, piecesLeft[(int)WhiteRook]/2, ANY);
5076 initialRights[2] = initialRights[5] = board[CASTLING][2] = board[CASTLING][5] = i;
5081 while(piecesLeft[(int)WhiteKing]) AddOnePiece(board, WhiteKing, 0, ANY);
5082 while(piecesLeft[(int)WhiteUnicorn]) AddOnePiece(board, WhiteUnicorn, 0, ANY);
5085 // Only Rooks can be left; simply place them all
5086 while(piecesLeft[(int)WhiteRook]) {
5087 i = put(board, WhiteRook, 0, 0, ANY);
5088 if(PosFlags(0) & F_FRC_TYPE_CASTLING) { // first and last Rook get FRC castling rights
5091 initialRights[1] = initialRights[4] = board[CASTLING][1] = board[CASTLING][4] = i;
5093 initialRights[0] = initialRights[3] = board[CASTLING][0] = board[CASTLING][3] = i;
5096 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // copy black from white
5097 board[BOARD_HEIGHT-1][i] = (int) board[0][i] < BlackPawn ? WHITE_TO_BLACK board[0][i] : EmptySquare;
5100 if(number >= 0) appData.defaultFrcPosition %= nrOfShuffles; // normalize
5103 int SetCharTable( char *table, const char * map )
5104 /* [HGM] moved here from winboard.c because of its general usefulness */
5105 /* Basically a safe strcpy that uses the last character as King */
5107 int result = FALSE; int NrPieces;
5109 if( map != NULL && (NrPieces=strlen(map)) <= (int) EmptySquare
5110 && NrPieces >= 12 && !(NrPieces&1)) {
5111 int i; /* [HGM] Accept even length from 12 to 34 */
5113 for( i=0; i<(int) EmptySquare; i++ ) table[i] = '.';
5114 for( i=0; i<NrPieces/2-1; i++ ) {
5116 table[i + (int)BlackPawn - (int) WhitePawn] = map[i+NrPieces/2];
5118 table[(int) WhiteKing] = map[NrPieces/2-1];
5119 table[(int) BlackKing] = map[NrPieces-1];
5127 void Prelude(Board board)
5128 { // [HGM] superchess: random selection of exo-pieces
5129 int i, j, k; ChessSquare p;
5130 static ChessSquare exoPieces[4] = { WhiteAngel, WhiteMarshall, WhiteSilver, WhiteLance };
5132 GetPositionNumber(); // use FRC position number
5134 if(appData.pieceToCharTable != NULL) { // select pieces to participate from given char table
5135 SetCharTable(pieceToChar, appData.pieceToCharTable);
5136 for(i=(int)WhiteQueen+1, j=0; i<(int)WhiteKing && j<4; i++)
5137 if(PieceToChar((ChessSquare)i) != '.') exoPieces[j++] = (ChessSquare) i;
5140 j = seed%4; seed /= 4;
5141 p = board[0][BOARD_LEFT+j]; board[0][BOARD_LEFT+j] = EmptySquare; k = PieceToNumber(p);
5142 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5143 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5144 j = seed%3 + (seed%3 >= j); seed /= 3;
5145 p = board[0][BOARD_LEFT+j]; board[0][BOARD_LEFT+j] = EmptySquare; k = PieceToNumber(p);
5146 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5147 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5148 j = seed%3; seed /= 3;
5149 p = board[0][BOARD_LEFT+j+5]; board[0][BOARD_LEFT+j+5] = EmptySquare; k = PieceToNumber(p);
5150 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5151 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5152 j = seed%2 + (seed%2 >= j); seed /= 2;
5153 p = board[0][BOARD_LEFT+j+5]; board[0][BOARD_LEFT+j+5] = EmptySquare; k = PieceToNumber(p);
5154 board[k][BOARD_WIDTH-1] = p; board[k][BOARD_WIDTH-2]++;
5155 board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p; board[BOARD_HEIGHT-1-k][1]++;
5156 j = seed%4; seed /= 4; put(board, exoPieces[3], 0, j, ANY);
5157 j = seed%3; seed /= 3; put(board, exoPieces[2], 0, j, ANY);
5158 j = seed%2; seed /= 2; put(board, exoPieces[1], 0, j, ANY);
5159 put(board, exoPieces[0], 0, 0, ANY);
5160 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) board[BOARD_HEIGHT-1][i] = WHITE_TO_BLACK board[0][i];
5164 InitPosition(redraw)
5167 ChessSquare (* pieces)[BOARD_FILES];
5168 int i, j, pawnRow, overrule,
5169 oldx = gameInfo.boardWidth,
5170 oldy = gameInfo.boardHeight,
5171 oldh = gameInfo.holdingsWidth,
5172 oldv = gameInfo.variant;
5174 if(appData.icsActive) shuffleOpenings = FALSE; // [HGM] shuffle: in ICS mode, only shuffle on ICS request
5176 /* [AS] Initialize pv info list [HGM] and game status */
5178 for( i=0; i<=framePtr; i++ ) { // [HGM] vari: spare saved variations
5179 pvInfoList[i].depth = 0;
5180 boards[i][EP_STATUS] = EP_NONE;
5181 for( j=0; j<BOARD_FILES-2; j++ ) boards[i][CASTLING][j] = NoRights;
5184 initialRulePlies = 0; /* 50-move counter start */
5186 castlingRank[0] = castlingRank[1] = castlingRank[2] = 0;
5187 castlingRank[3] = castlingRank[4] = castlingRank[5] = BOARD_HEIGHT-1;
5191 /* [HGM] logic here is completely changed. In stead of full positions */
5192 /* the initialized data only consist of the two backranks. The switch */
5193 /* selects which one we will use, which is than copied to the Board */
5194 /* initialPosition, which for the rest is initialized by Pawns and */
5195 /* empty squares. This initial position is then copied to boards[0], */
5196 /* possibly after shuffling, so that it remains available. */
5198 gameInfo.holdingsWidth = 0; /* default board sizes */
5199 gameInfo.boardWidth = 8;
5200 gameInfo.boardHeight = 8;
5201 gameInfo.holdingsSize = 0;
5202 nrCastlingRights = -1; /* [HGM] Kludge to indicate default should be used */
5203 for(i=0; i<BOARD_FILES-2; i++)
5204 initialPosition[CASTLING][i] = initialRights[i] = NoRights; /* but no rights yet */
5205 initialPosition[EP_STATUS] = EP_NONE;
5206 SetCharTable(pieceToChar, "PNBRQ...........Kpnbrq...........k");
5208 switch (gameInfo.variant) {
5209 case VariantFischeRandom:
5210 shuffleOpenings = TRUE;
5214 case VariantShatranj:
5215 pieces = ShatranjArray;
5216 nrCastlingRights = 0;
5217 SetCharTable(pieceToChar, "PN.R.QB...Kpn.r.qb...k");
5220 pieces = makrukArray;
5221 nrCastlingRights = 0;
5222 startedFromSetupPosition = TRUE;
5223 SetCharTable(pieceToChar, "PN.R.M....SKpn.r.m....sk");
5225 case VariantTwoKings:
5226 pieces = twoKingsArray;
5228 case VariantCapaRandom:
5229 shuffleOpenings = TRUE;
5230 case VariantCapablanca:
5231 pieces = CapablancaArray;
5232 gameInfo.boardWidth = 10;
5233 SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
5236 pieces = GothicArray;
5237 gameInfo.boardWidth = 10;
5238 SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack");
5241 pieces = JanusArray;
5242 gameInfo.boardWidth = 10;
5243 SetCharTable(pieceToChar, "PNBRQ..JKpnbrq..jk");
5244 nrCastlingRights = 6;
5245 initialPosition[CASTLING][0] = initialRights[0] = BOARD_RGHT-1;
5246 initialPosition[CASTLING][1] = initialRights[1] = BOARD_LEFT;
5247 initialPosition[CASTLING][2] = initialRights[2] =(BOARD_WIDTH-1)>>1;
5248 initialPosition[CASTLING][3] = initialRights[3] = BOARD_RGHT-1;
5249 initialPosition[CASTLING][4] = initialRights[4] = BOARD_LEFT;
5250 initialPosition[CASTLING][5] = initialRights[5] =(BOARD_WIDTH-1)>>1;
5253 pieces = FalconArray;
5254 gameInfo.boardWidth = 10;
5255 SetCharTable(pieceToChar, "PNBRQ.............FKpnbrq.............fk");
5257 case VariantXiangqi:
5258 pieces = XiangqiArray;
5259 gameInfo.boardWidth = 9;
5260 gameInfo.boardHeight = 10;
5261 nrCastlingRights = 0;
5262 SetCharTable(pieceToChar, "PH.R.AE..K.C.ph.r.ae..k.c.");
5265 pieces = ShogiArray;
5266 gameInfo.boardWidth = 9;
5267 gameInfo.boardHeight = 9;
5268 gameInfo.holdingsSize = 7;
5269 nrCastlingRights = 0;
5270 SetCharTable(pieceToChar, "PNBRLS...G.++++++Kpnbrls...g.++++++k");
5272 case VariantCourier:
5273 pieces = CourierArray;
5274 gameInfo.boardWidth = 12;
5275 nrCastlingRights = 0;
5276 SetCharTable(pieceToChar, "PNBR.FE..WMKpnbr.fe..wmk");
5278 case VariantKnightmate:
5279 pieces = KnightmateArray;
5280 SetCharTable(pieceToChar, "P.BRQ.....M.........K.p.brq.....m.........k.");
5283 pieces = fairyArray;
5284 SetCharTable(pieceToChar, "PNBRQFEACWMOHIJGDVLSUKpnbrqfeacwmohijgdvlsuk");
5287 pieces = GreatArray;
5288 gameInfo.boardWidth = 10;
5289 SetCharTable(pieceToChar, "PN....E...S..HWGMKpn....e...s..hwgmk");
5290 gameInfo.holdingsSize = 8;
5294 SetCharTable(pieceToChar, "PNBRQ..SE.......V.AKpnbrq..se.......v.ak");
5295 gameInfo.holdingsSize = 8;
5296 startedFromSetupPosition = TRUE;
5298 case VariantCrazyhouse:
5299 case VariantBughouse:
5301 SetCharTable(pieceToChar, "PNBRQ.......~~~~Kpnbrq.......~~~~k");
5302 gameInfo.holdingsSize = 5;
5304 case VariantWildCastle:
5306 /* !!?shuffle with kings guaranteed to be on d or e file */
5307 shuffleOpenings = 1;
5309 case VariantNoCastle:
5311 nrCastlingRights = 0;
5312 /* !!?unconstrained back-rank shuffle */
5313 shuffleOpenings = 1;
5318 if(appData.NrFiles >= 0) {
5319 if(gameInfo.boardWidth != appData.NrFiles) overrule++;
5320 gameInfo.boardWidth = appData.NrFiles;
5322 if(appData.NrRanks >= 0) {
5323 gameInfo.boardHeight = appData.NrRanks;
5325 if(appData.holdingsSize >= 0) {
5326 i = appData.holdingsSize;
5327 if(i > gameInfo.boardHeight) i = gameInfo.boardHeight;
5328 gameInfo.holdingsSize = i;
5330 if(gameInfo.holdingsSize) gameInfo.holdingsWidth = 2;
5331 if(BOARD_HEIGHT > BOARD_RANKS || BOARD_WIDTH > BOARD_FILES)
5332 DisplayFatalError(_("Recompile to support this BOARD_RANKS or BOARD_FILES!"), 0, 2);
5334 pawnRow = gameInfo.boardHeight - 7; /* seems to work in all common variants */
5335 if(pawnRow < 1) pawnRow = 1;
5336 if(gameInfo.variant == VariantMakruk) pawnRow = 2;
5338 /* User pieceToChar list overrules defaults */
5339 if(appData.pieceToCharTable != NULL)
5340 SetCharTable(pieceToChar, appData.pieceToCharTable);
5342 for( j=0; j<BOARD_WIDTH; j++ ) { ChessSquare s = EmptySquare;
5344 if(j==BOARD_LEFT-1 || j==BOARD_RGHT)
5345 s = (ChessSquare) 0; /* account holding counts in guard band */
5346 for( i=0; i<BOARD_HEIGHT; i++ )
5347 initialPosition[i][j] = s;
5349 if(j < BOARD_LEFT || j >= BOARD_RGHT || overrule) continue;
5350 initialPosition[0][j] = pieces[0][j-gameInfo.holdingsWidth];
5351 initialPosition[pawnRow][j] = WhitePawn;
5352 initialPosition[BOARD_HEIGHT-pawnRow-1][j] = BlackPawn;
5353 if(gameInfo.variant == VariantXiangqi) {
5355 initialPosition[pawnRow][j] =
5356 initialPosition[BOARD_HEIGHT-pawnRow-1][j] = EmptySquare;
5357 if(j==BOARD_LEFT+1 || j>=BOARD_RGHT-2) {
5358 initialPosition[2][j] = WhiteCannon;
5359 initialPosition[BOARD_HEIGHT-3][j] = BlackCannon;
5363 initialPosition[BOARD_HEIGHT-1][j] = pieces[1][j-gameInfo.holdingsWidth];
5365 if( (gameInfo.variant == VariantShogi) && !overrule ) {
5368 initialPosition[1][j] = WhiteBishop;
5369 initialPosition[BOARD_HEIGHT-2][j] = BlackRook;
5371 initialPosition[1][j] = WhiteRook;
5372 initialPosition[BOARD_HEIGHT-2][j] = BlackBishop;
5375 if( nrCastlingRights == -1) {
5376 /* [HGM] Build normal castling rights (must be done after board sizing!) */
5377 /* This sets default castling rights from none to normal corners */
5378 /* Variants with other castling rights must set them themselves above */
5379 nrCastlingRights = 6;
5381 initialPosition[CASTLING][0] = initialRights[0] = BOARD_RGHT-1;
5382 initialPosition[CASTLING][1] = initialRights[1] = BOARD_LEFT;
5383 initialPosition[CASTLING][2] = initialRights[2] = BOARD_WIDTH>>1;
5384 initialPosition[CASTLING][3] = initialRights[3] = BOARD_RGHT-1;
5385 initialPosition[CASTLING][4] = initialRights[4] = BOARD_LEFT;
5386 initialPosition[CASTLING][5] = initialRights[5] = BOARD_WIDTH>>1;
5389 if(gameInfo.variant == VariantSuper) Prelude(initialPosition);
5390 if(gameInfo.variant == VariantGreat) { // promotion commoners
5391 initialPosition[PieceToNumber(WhiteMan)][BOARD_WIDTH-1] = WhiteMan;
5392 initialPosition[PieceToNumber(WhiteMan)][BOARD_WIDTH-2] = 9;
5393 initialPosition[BOARD_HEIGHT-1-PieceToNumber(WhiteMan)][0] = BlackMan;
5394 initialPosition[BOARD_HEIGHT-1-PieceToNumber(WhiteMan)][1] = 9;
5396 if (appData.debugMode) {
5397 fprintf(debugFP, "shuffleOpenings = %d\n", shuffleOpenings);
5399 if(shuffleOpenings) {
5400 SetUpShuffle(initialPosition, appData.defaultFrcPosition);
5401 startedFromSetupPosition = TRUE;
5403 if(startedFromPositionFile) {
5404 /* [HGM] loadPos: use PositionFile for every new game */
5405 CopyBoard(initialPosition, filePosition);
5406 for(i=0; i<nrCastlingRights; i++)
5407 initialRights[i] = filePosition[CASTLING][i];
5408 startedFromSetupPosition = TRUE;
5411 CopyBoard(boards[0], initialPosition);
5413 if(oldx != gameInfo.boardWidth ||
5414 oldy != gameInfo.boardHeight ||
5415 oldh != gameInfo.holdingsWidth
5417 || oldv == VariantGothic || // For licensing popups
5418 gameInfo.variant == VariantGothic
5421 || oldv == VariantFalcon ||
5422 gameInfo.variant == VariantFalcon
5425 InitDrawingSizes(-2 ,0);
5428 DrawPosition(TRUE, boards[currentMove]);
5432 SendBoard(cps, moveNum)
5433 ChessProgramState *cps;
5436 char message[MSG_SIZ];
5438 if (cps->useSetboard) {
5439 char* fen = PositionToFEN(moveNum, cps->fenOverride);
5440 sprintf(message, "setboard %s\n", fen);
5441 SendToProgram(message, cps);
5447 /* Kludge to set black to move, avoiding the troublesome and now
5448 * deprecated "black" command.
5450 if (!WhiteOnMove(moveNum)) SendToProgram("a2a3\n", cps);
5452 SendToProgram("edit\n", cps);
5453 SendToProgram("#\n", cps);
5454 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
5455 bp = &boards[moveNum][i][BOARD_LEFT];
5456 for (j = BOARD_LEFT; j < BOARD_RGHT; j++, bp++) {
5457 if ((int) *bp < (int) BlackPawn) {
5458 sprintf(message, "%c%c%c\n", PieceToChar(*bp),
5460 if(message[0] == '+' || message[0] == '~') {
5461 sprintf(message, "%c%c%c+\n",
5462 PieceToChar((ChessSquare)(DEMOTED *bp)),
5465 if(cps->alphaRank) { /* [HGM] shogi: translate coords */
5466 message[1] = BOARD_RGHT - 1 - j + '1';
5467 message[2] = BOARD_HEIGHT - 1 - i + 'a';
5469 SendToProgram(message, cps);
5474 SendToProgram("c\n", cps);
5475 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
5476 bp = &boards[moveNum][i][BOARD_LEFT];
5477 for (j = BOARD_LEFT; j < BOARD_RGHT; j++, bp++) {
5478 if (((int) *bp != (int) EmptySquare)
5479 && ((int) *bp >= (int) BlackPawn)) {
5480 sprintf(message, "%c%c%c\n", ToUpper(PieceToChar(*bp)),
5482 if(message[0] == '+' || message[0] == '~') {
5483 sprintf(message, "%c%c%c+\n",
5484 PieceToChar((ChessSquare)(DEMOTED *bp)),
5487 if(cps->alphaRank) { /* [HGM] shogi: translate coords */
5488 message[1] = BOARD_RGHT - 1 - j + '1';
5489 message[2] = BOARD_HEIGHT - 1 - i + 'a';
5491 SendToProgram(message, cps);
5496 SendToProgram(".\n", cps);
5498 setboardSpoiledMachineBlack = 0; /* [HGM] assume WB 4.2.7 already solves this after sending setboard */
5501 static int autoQueen; // [HGM] oneclick
5504 HasPromotionChoice(int fromX, int fromY, int toX, int toY, char *promoChoice)
5506 /* [HGM] rewritten IsPromotion to only flag promotions that offer a choice */
5507 /* [HGM] add Shogi promotions */
5508 int promotionZoneSize=1, highestPromotingPiece = (int)WhitePawn;
5513 if(fromX < BOARD_LEFT || fromX >= BOARD_RGHT) return FALSE; // drop
5514 if(toX < BOARD_LEFT || toX >= BOARD_RGHT) return FALSE; // move into holdings
5516 if(gameMode == EditPosition || gameInfo.variant == VariantXiangqi || // no promotions
5517 !(fromX >=0 && fromY >= 0 && toX >= 0 && toY >= 0) ) // invalid move
5520 piece = boards[currentMove][fromY][fromX];
5521 if(gameInfo.variant == VariantShogi) {
5522 promotionZoneSize = 3;
5523 highestPromotingPiece = (int)WhiteFerz;
5524 } else if(gameInfo.variant == VariantMakruk) {
5525 promotionZoneSize = 3;
5528 // next weed out all moves that do not touch the promotion zone at all
5529 if((int)piece >= BlackPawn) {
5530 if(toY >= promotionZoneSize && fromY >= promotionZoneSize)
5532 highestPromotingPiece = WHITE_TO_BLACK highestPromotingPiece;
5534 if( toY < BOARD_HEIGHT - promotionZoneSize &&
5535 fromY < BOARD_HEIGHT - promotionZoneSize) return FALSE;
5538 if( (int)piece > highestPromotingPiece ) return FALSE; // non-promoting piece
5540 // weed out mandatory Shogi promotions
5541 if(gameInfo.variant == VariantShogi) {
5542 if(piece >= BlackPawn) {
5543 if(toY == 0 && piece == BlackPawn ||
5544 toY == 0 && piece == BlackQueen ||
5545 toY <= 1 && piece == BlackKnight) {
5550 if(toY == BOARD_HEIGHT-1 && piece == WhitePawn ||
5551 toY == BOARD_HEIGHT-1 && piece == WhiteQueen ||
5552 toY >= BOARD_HEIGHT-2 && piece == WhiteKnight) {
5559 // weed out obviously illegal Pawn moves
5560 if(appData.testLegality && (piece == WhitePawn || piece == BlackPawn) ) {
5561 if(toX > fromX+1 || toX < fromX-1) return FALSE; // wide
5562 if(piece == WhitePawn && toY != fromY+1) return FALSE; // deep
5563 if(piece == BlackPawn && toY != fromY-1) return FALSE; // deep
5564 if(fromX != toX && gameInfo.variant == VariantShogi) return FALSE;
5565 // note we are not allowed to test for valid (non-)capture, due to premove
5568 // we either have a choice what to promote to, or (in Shogi) whether to promote
5569 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk) {
5570 *promoChoice = PieceToChar(BlackFerz); // no choice
5573 if(autoQueen) { // predetermined
5574 if(gameInfo.variant == VariantSuicide || gameInfo.variant == VariantLosers)
5575 *promoChoice = PieceToChar(BlackKing); // in Suicide Q is the last thing we want
5576 else *promoChoice = PieceToChar(BlackQueen);
5580 // suppress promotion popup on illegal moves that are not premoves
5581 premove = gameMode == IcsPlayingWhite && !WhiteOnMove(currentMove) ||
5582 gameMode == IcsPlayingBlack && WhiteOnMove(currentMove);
5583 if(appData.testLegality && !premove) {
5584 moveType = LegalityTest(boards[currentMove], PosFlags(currentMove),
5585 fromY, fromX, toY, toX, NULLCHAR);
5586 if(moveType != WhitePromotionQueen && moveType != BlackPromotionQueen &&
5587 moveType != WhitePromotionKnight && moveType != BlackPromotionKnight)
5595 InPalace(row, column)
5597 { /* [HGM] for Xiangqi */
5598 if( (row < 3 || row > BOARD_HEIGHT-4) &&
5599 column < (BOARD_WIDTH + 4)/2 &&
5600 column > (BOARD_WIDTH - 5)/2 ) return TRUE;
5605 PieceForSquare (x, y)
5609 if (x < 0 || x >= BOARD_WIDTH || y < 0 || y >= BOARD_HEIGHT)
5612 return boards[currentMove][y][x];
5616 OKToStartUserMove(x, y)
5619 ChessSquare from_piece;
5622 if (matchMode) return FALSE;
5623 if (gameMode == EditPosition) return TRUE;
5625 if (x >= 0 && y >= 0)
5626 from_piece = boards[currentMove][y][x];
5628 from_piece = EmptySquare;
5630 if (from_piece == EmptySquare) return FALSE;
5632 white_piece = (int)from_piece >= (int)WhitePawn &&
5633 (int)from_piece < (int)BlackPawn; /* [HGM] can be > King! */
5636 case PlayFromGameFile:
5638 case TwoMachinesPlay:
5646 case MachinePlaysWhite:
5647 case IcsPlayingBlack:
5648 if (appData.zippyPlay) return FALSE;
5650 DisplayMoveError(_("You are playing Black"));
5655 case MachinePlaysBlack:
5656 case IcsPlayingWhite:
5657 if (appData.zippyPlay) return FALSE;
5659 DisplayMoveError(_("You are playing White"));
5665 if (!white_piece && WhiteOnMove(currentMove)) {
5666 DisplayMoveError(_("It is White's turn"));
5669 if (white_piece && !WhiteOnMove(currentMove)) {
5670 DisplayMoveError(_("It is Black's turn"));
5673 if (cmailMsgLoaded && (currentMove < cmailOldMove)) {
5674 /* Editing correspondence game history */
5675 /* Could disallow this or prompt for confirmation */
5680 case BeginningOfGame:
5681 if (appData.icsActive) return FALSE;
5682 if (!appData.noChessProgram) {
5684 DisplayMoveError(_("You are playing White"));
5691 if (!white_piece && WhiteOnMove(currentMove)) {
5692 DisplayMoveError(_("It is White's turn"));
5695 if (white_piece && !WhiteOnMove(currentMove)) {
5696 DisplayMoveError(_("It is Black's turn"));
5705 if (currentMove != forwardMostMove && gameMode != AnalyzeMode
5706 && gameMode != EditGame // [HGM] vari: treat as AnalyzeMode
5707 && gameMode != AnalyzeFile && gameMode != Training) {
5708 DisplayMoveError(_("Displayed position is not current"));
5715 OnlyMove(int *x, int *y, Boolean captures) {
5716 DisambiguateClosure cl;
5717 if (appData.zippyPlay) return FALSE;
5719 case MachinePlaysBlack:
5720 case IcsPlayingWhite:
5721 case BeginningOfGame:
5722 if(!WhiteOnMove(currentMove)) return FALSE;
5724 case MachinePlaysWhite:
5725 case IcsPlayingBlack:
5726 if(WhiteOnMove(currentMove)) return FALSE;
5731 cl.pieceIn = EmptySquare;
5736 cl.promoCharIn = NULLCHAR;
5737 Disambiguate(boards[currentMove], PosFlags(currentMove), &cl);
5738 if( cl.kind == NormalMove ||
5739 cl.kind == AmbiguousMove && captures && cl.captures == 1 ||
5740 cl.kind == WhitePromotionQueen || cl.kind == BlackPromotionQueen ||
5741 cl.kind == WhitePromotionKnight || cl.kind == BlackPromotionKnight ||
5742 cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) {
5749 if(cl.kind != ImpossibleMove) return FALSE;
5750 cl.pieceIn = EmptySquare;
5755 cl.promoCharIn = NULLCHAR;
5756 Disambiguate(boards[currentMove], PosFlags(currentMove), &cl);
5757 if( cl.kind == NormalMove ||
5758 cl.kind == AmbiguousMove && captures && cl.captures == 1 ||
5759 cl.kind == WhitePromotionQueen || cl.kind == BlackPromotionQueen ||
5760 cl.kind == WhitePromotionKnight || cl.kind == BlackPromotionKnight ||
5761 cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) {
5766 autoQueen = TRUE; // act as if autoQueen on when we click to-square
5772 FILE *lastLoadGameFP = NULL, *lastLoadPositionFP = NULL;
5773 int lastLoadGameNumber = 0, lastLoadPositionNumber = 0;
5774 int lastLoadGameUseList = FALSE;
5775 char lastLoadGameTitle[MSG_SIZ], lastLoadPositionTitle[MSG_SIZ];
5776 ChessMove lastLoadGameStart = (ChessMove) 0;
5779 UserMoveTest(fromX, fromY, toX, toY, promoChar, captureOwn)
5780 int fromX, fromY, toX, toY;
5785 ChessSquare pdown, pup;
5787 /* Check if the user is playing in turn. This is complicated because we
5788 let the user "pick up" a piece before it is his turn. So the piece he
5789 tried to pick up may have been captured by the time he puts it down!
5790 Therefore we use the color the user is supposed to be playing in this
5791 test, not the color of the piece that is currently on the starting
5792 square---except in EditGame mode, where the user is playing both
5793 sides; fortunately there the capture race can't happen. (It can
5794 now happen in IcsExamining mode, but that's just too bad. The user
5795 will get a somewhat confusing message in that case.)
5799 case PlayFromGameFile:
5801 case TwoMachinesPlay:
5805 /* We switched into a game mode where moves are not accepted,
5806 perhaps while the mouse button was down. */
5807 return ImpossibleMove;
5809 case MachinePlaysWhite:
5810 /* User is moving for Black */
5811 if (WhiteOnMove(currentMove)) {
5812 DisplayMoveError(_("It is White's turn"));
5813 return ImpossibleMove;
5817 case MachinePlaysBlack:
5818 /* User is moving for White */
5819 if (!WhiteOnMove(currentMove)) {
5820 DisplayMoveError(_("It is Black's turn"));
5821 return ImpossibleMove;
5827 case BeginningOfGame:
5830 if ((int) boards[currentMove][fromY][fromX] >= (int) BlackPawn &&
5831 (int) boards[currentMove][fromY][fromX] < (int) EmptySquare) {
5832 /* User is moving for Black */
5833 if (WhiteOnMove(currentMove)) {
5834 DisplayMoveError(_("It is White's turn"));
5835 return ImpossibleMove;
5838 /* User is moving for White */
5839 if (!WhiteOnMove(currentMove)) {
5840 DisplayMoveError(_("It is Black's turn"));
5841 return ImpossibleMove;
5846 case IcsPlayingBlack:
5847 /* User is moving for Black */
5848 if (WhiteOnMove(currentMove)) {
5849 if (!appData.premove) {
5850 DisplayMoveError(_("It is White's turn"));
5851 } else if (toX >= 0 && toY >= 0) {
5854 premoveFromX = fromX;
5855 premoveFromY = fromY;
5856 premovePromoChar = promoChar;
5858 if (appData.debugMode)
5859 fprintf(debugFP, "Got premove: fromX %d,"
5860 "fromY %d, toX %d, toY %d\n",
5861 fromX, fromY, toX, toY);
5863 return ImpossibleMove;
5867 case IcsPlayingWhite:
5868 /* User is moving for White */
5869 if (!WhiteOnMove(currentMove)) {
5870 if (!appData.premove) {
5871 DisplayMoveError(_("It is Black's turn"));
5872 } else if (toX >= 0 && toY >= 0) {
5875 premoveFromX = fromX;
5876 premoveFromY = fromY;
5877 premovePromoChar = promoChar;
5879 if (appData.debugMode)
5880 fprintf(debugFP, "Got premove: fromX %d,"
5881 "fromY %d, toX %d, toY %d\n",
5882 fromX, fromY, toX, toY);
5884 return ImpossibleMove;
5892 /* EditPosition, empty square, or different color piece;
5893 click-click move is possible */
5894 if (toX == -2 || toY == -2) {
5895 boards[0][fromY][fromX] = EmptySquare;
5896 return AmbiguousMove;
5897 } else if (toX >= 0 && toY >= 0) {
5898 boards[0][toY][toX] = boards[0][fromY][fromX];
5899 if(fromX == BOARD_LEFT-2) { // handle 'moves' out of holdings
5900 if(boards[0][fromY][0] != EmptySquare) {
5901 if(boards[0][fromY][1]) boards[0][fromY][1]--;
5902 if(boards[0][fromY][1] == 0) boards[0][fromY][0] = EmptySquare;
5905 if(fromX == BOARD_RGHT+1) {
5906 if(boards[0][fromY][BOARD_WIDTH-1] != EmptySquare) {
5907 if(boards[0][fromY][BOARD_WIDTH-2]) boards[0][fromY][BOARD_WIDTH-2]--;
5908 if(boards[0][fromY][BOARD_WIDTH-2] == 0) boards[0][fromY][BOARD_WIDTH-1] = EmptySquare;
5911 boards[0][fromY][fromX] = EmptySquare;
5912 return AmbiguousMove;
5914 return ImpossibleMove;
5917 if(toX < 0 || toY < 0) return ImpossibleMove;
5918 pdown = boards[currentMove][fromY][fromX];
5919 pup = boards[currentMove][toY][toX];
5921 /* [HGM] If move started in holdings, it means a drop */
5922 if( fromX == BOARD_LEFT-2 || fromX == BOARD_RGHT+1) {
5923 if( pup != EmptySquare ) return ImpossibleMove;
5924 if(appData.testLegality) {
5925 /* it would be more logical if LegalityTest() also figured out
5926 * which drops are legal. For now we forbid pawns on back rank.
5927 * Shogi is on its own here...
5929 if( (pdown == WhitePawn || pdown == BlackPawn) &&
5930 (toY == 0 || toY == BOARD_HEIGHT -1 ) )
5931 return(ImpossibleMove); /* no pawn drops on 1st/8th */
5933 return WhiteDrop; /* Not needed to specify white or black yet */
5936 /* [HGM] always test for legality, to get promotion info */
5937 moveType = LegalityTest(boards[currentMove], PosFlags(currentMove),
5938 fromY, fromX, toY, toX, promoChar);
5939 /* [HGM] but possibly ignore an IllegalMove result */
5940 if (appData.testLegality) {
5941 if (moveType == IllegalMove || moveType == ImpossibleMove) {
5942 DisplayMoveError(_("Illegal move"));
5943 return ImpossibleMove;
5948 /* [HGM] <popupFix> in stead of calling FinishMove directly, this
5949 function is made into one that returns an OK move type if FinishMove
5950 should be called. This to give the calling driver routine the
5951 opportunity to finish the userMove input with a promotion popup,
5952 without bothering the user with this for invalid or illegal moves */
5954 /* FinishMove(moveType, fromX, fromY, toX, toY, promoChar); */
5957 /* Common tail of UserMoveEvent and DropMenuEvent */
5959 FinishMove(moveType, fromX, fromY, toX, toY, promoChar)
5961 int fromX, fromY, toX, toY;
5962 /*char*/int promoChar;
5966 if((gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat) && promoChar != NULLCHAR) {
5967 // [HGM] superchess: suppress promotions to non-available piece
5968 int k = PieceToNumber(CharToPiece(ToUpper(promoChar)));
5969 if(WhiteOnMove(currentMove)) {
5970 if(!boards[currentMove][k][BOARD_WIDTH-2]) return 0;
5972 if(!boards[currentMove][BOARD_HEIGHT-1-k][1]) return 0;
5976 /* [HGM] <popupFix> kludge to avoid having to know the exact promotion
5977 move type in caller when we know the move is a legal promotion */
5978 if(moveType == NormalMove && promoChar)
5979 moveType = PromoCharToMoveType(WhiteOnMove(currentMove), promoChar);
5981 /* [HGM] convert drag-and-drop piece drops to standard form */
5982 if( (fromX == BOARD_LEFT-2 || fromX == BOARD_RGHT+1) && fromY != DROP_RANK ){
5983 moveType = WhiteOnMove(currentMove) ? WhiteDrop : BlackDrop;
5984 if(appData.debugMode) fprintf(debugFP, "Drop move %d, curr=%d, x=%d,y=%d, p=%d\n",
5985 moveType, currentMove, fromX, fromY, boards[currentMove][fromY][fromX]);
5986 // holdings might not be sent yet in ICS play; we have to figure out which piece belongs here
5987 if(fromX == 0) fromY = BOARD_HEIGHT-1 - fromY; // black holdings upside-down
5988 fromX = fromX ? WhitePawn : BlackPawn; // first piece type in selected holdings
5989 while(PieceToChar(fromX) == '.' || PieceToNumber(fromX) != fromY && fromX != (int) EmptySquare) fromX++;
5993 /* [HGM] <popupFix> The following if has been moved here from
5994 UserMoveEvent(). Because it seemed to belong here (why not allow
5995 piece drops in training games?), and because it can only be
5996 performed after it is known to what we promote. */
5997 if (gameMode == Training) {
5998 /* compare the move played on the board to the next move in the
5999 * game. If they match, display the move and the opponent's response.
6000 * If they don't match, display an error message.
6004 CopyBoard(testBoard, boards[currentMove]);
6005 ApplyMove(fromX, fromY, toX, toY, promoChar, testBoard);
6007 if (CompareBoards(testBoard, boards[currentMove+1])) {
6008 ForwardInner(currentMove+1);
6010 /* Autoplay the opponent's response.
6011 * if appData.animate was TRUE when Training mode was entered,
6012 * the response will be animated.
6014 saveAnimate = appData.animate;
6015 appData.animate = animateTraining;
6016 ForwardInner(currentMove+1);
6017 appData.animate = saveAnimate;
6019 /* check for the end of the game */
6020 if (currentMove >= forwardMostMove) {
6021 gameMode = PlayFromGameFile;
6023 SetTrainingModeOff();
6024 DisplayInformation(_("End of game"));
6027 DisplayError(_("Incorrect move"), 0);
6032 /* Ok, now we know that the move is good, so we can kill
6033 the previous line in Analysis Mode */
6034 if ((gameMode == AnalyzeMode || gameMode == EditGame)
6035 && currentMove < forwardMostMove) {
6036 PushTail(currentMove, forwardMostMove); // [HGM] vari: save tail of game
6039 /* If we need the chess program but it's dead, restart it */
6040 ResurrectChessProgram();
6042 /* A user move restarts a paused game*/
6046 thinkOutput[0] = NULLCHAR;
6048 MakeMove(fromX, fromY, toX, toY, promoChar); /*updates forwardMostMove*/
6050 if(Adjudicate(NULL)) return 1; // [HGM] adjudicate: take care of automtic game end
6052 if (gameMode == BeginningOfGame) {
6053 if (appData.noChessProgram) {
6054 gameMode = EditGame;
6058 gameMode = MachinePlaysBlack;
6061 sprintf(buf, "%s vs. %s", gameInfo.white, gameInfo.black);
6063 if (first.sendName) {
6064 sprintf(buf, "name %s\n", gameInfo.white);
6065 SendToProgram(buf, &first);
6072 /* Relay move to ICS or chess engine */
6073 if (appData.icsActive) {
6074 if (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
6075 gameMode == IcsExamining) {
6076 if(userOfferedDraw && (signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
6077 SendToICS(ics_prefix); // [HGM] drawclaim: send caim and move on one line for FICS
6079 SendMoveToICS(moveType, fromX, fromY, toX, toY);
6081 // also send plain move, in case ICS does not understand atomic claims
6082 SendMoveToICS(moveType, fromX, fromY, toX, toY);
6086 if (first.sendTime && (gameMode == BeginningOfGame ||
6087 gameMode == MachinePlaysWhite ||
6088 gameMode == MachinePlaysBlack)) {
6089 SendTimeRemaining(&first, gameMode != MachinePlaysBlack);
6091 if (gameMode != EditGame && gameMode != PlayFromGameFile) {
6092 // [HGM] book: if program might be playing, let it use book
6093 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, FALSE);
6094 first.maybeThinking = TRUE;
6095 } else SendMoveToProgram(forwardMostMove-1, &first);
6096 if (currentMove == cmailOldMove + 1) {
6097 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
6101 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6105 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
6111 if (WhiteOnMove(currentMove)) {
6112 GameEnds(BlackWins, "Black mates", GE_PLAYER);
6114 GameEnds(WhiteWins, "White mates", GE_PLAYER);
6118 GameEnds(GameIsDrawn, "Stalemate", GE_PLAYER);
6123 case MachinePlaysBlack:
6124 case MachinePlaysWhite:
6125 /* disable certain menu options while machine is thinking */
6126 SetMachineThinkingEnables();
6133 userOfferedDraw = FALSE; // [HGM] drawclaim: after move made, and tested for claimable draw
6135 if(bookHit) { // [HGM] book: simulate book reply
6136 static char bookMove[MSG_SIZ]; // a bit generous?
6138 programStats.nodes = programStats.depth = programStats.time =
6139 programStats.score = programStats.got_only_move = 0;
6140 sprintf(programStats.movelist, "%s (xbook)", bookHit);
6142 strcpy(bookMove, "move ");
6143 strcat(bookMove, bookHit);
6144 HandleMachineMove(bookMove, &first);
6150 UserMoveEvent(fromX, fromY, toX, toY, promoChar)
6151 int fromX, fromY, toX, toY;
6154 /* [HGM] This routine was added to allow calling of its two logical
6155 parts from other modules in the old way. Before, UserMoveEvent()
6156 automatically called FinishMove() if the move was OK, and returned
6157 otherwise. I separated the two, in order to make it possible to
6158 slip a promotion popup in between. But that it always needs two
6159 calls, to the first part, (now called UserMoveTest() ), and to
6160 FinishMove if the first part succeeded. Calls that do not need
6161 to do anything in between, can call this routine the old way.
6163 ChessMove moveType = UserMoveTest(fromX, fromY, toX, toY, promoChar, FALSE);
6164 if(appData.debugMode) fprintf(debugFP, "moveType 4 = %d, promochar = %x\n", moveType, promoChar);
6165 if(moveType == AmbiguousMove)
6166 DrawPosition(FALSE, boards[currentMove]);
6167 else if(moveType != ImpossibleMove && moveType != Comment)
6168 FinishMove(moveType, fromX, fromY, toX, toY, promoChar);
6172 Mark(board, flags, kind, rf, ff, rt, ft, closure)
6179 typedef char Markers[BOARD_RANKS][BOARD_FILES];
6180 Markers *m = (Markers *) closure;
6181 if(rf == fromY && ff == fromX)
6182 (*m)[rt][ft] = 1 + (board[rt][ft] != EmptySquare
6183 || kind == WhiteCapturesEnPassant
6184 || kind == BlackCapturesEnPassant);
6185 else if(flags & F_MANDATORY_CAPTURE && board[rt][ft] != EmptySquare) (*m)[rt][ft] = 3;
6189 MarkTargetSquares(int clear)
6192 if(!appData.markers || !appData.highlightDragging ||
6193 !appData.testLegality || gameMode == EditPosition) return;
6195 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) marker[y][x] = 0;
6198 GenLegal(boards[currentMove], PosFlags(currentMove), Mark, (void*) marker);
6199 if(PosFlags(0) & F_MANDATORY_CAPTURE) {
6200 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) if(marker[y][x]>1) capt++;
6202 for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) if(marker[y][x] == 1) marker[y][x] = 0;
6205 DrawPosition(TRUE, NULL);
6208 void LeftClick(ClickType clickType, int xPix, int yPix)
6211 Boolean saveAnimate;
6212 static int second = 0, promotionChoice = 0;
6213 char promoChoice = NULLCHAR;
6215 if(appData.seekGraph && appData.icsActive && loggedOn &&
6216 (gameMode == BeginningOfGame || gameMode == IcsIdle)) {
6217 SeekGraphClick(clickType, xPix, yPix, 0);
6221 if (clickType == Press) ErrorPopDown();
6222 MarkTargetSquares(1);
6224 x = EventToSquare(xPix, BOARD_WIDTH);
6225 y = EventToSquare(yPix, BOARD_HEIGHT);
6226 if (!flipView && y >= 0) {
6227 y = BOARD_HEIGHT - 1 - y;
6229 if (flipView && x >= 0) {
6230 x = BOARD_WIDTH - 1 - x;
6233 if(promotionChoice) { // we are waiting for a click to indicate promotion piece
6234 if(clickType == Release) return; // ignore upclick of click-click destination
6235 promotionChoice = FALSE; // only one chance: if click not OK it is interpreted as cancel
6236 if(appData.debugMode) fprintf(debugFP, "promotion click, x=%d, y=%d\n", x, y);
6237 if(gameInfo.holdingsWidth &&
6238 (WhiteOnMove(currentMove)
6239 ? x == BOARD_WIDTH-1 && y < gameInfo.holdingsSize && y > 0
6240 : x == 0 && y >= BOARD_HEIGHT - gameInfo.holdingsSize && y < BOARD_HEIGHT-1) ) {
6241 // click in right holdings, for determining promotion piece
6242 ChessSquare p = boards[currentMove][y][x];
6243 if(appData.debugMode) fprintf(debugFP, "square contains %d\n", (int)p);
6244 if(p != EmptySquare) {
6245 FinishMove(NormalMove, fromX, fromY, toX, toY, ToLower(PieceToChar(p)));
6250 DrawPosition(FALSE, boards[currentMove]);
6254 /* [HGM] holdings: next 5 lines: ignore all clicks between board and holdings */
6255 if(clickType == Press
6256 && ( x == BOARD_LEFT-1 || x == BOARD_RGHT
6257 || x == BOARD_LEFT-2 && y < BOARD_HEIGHT-gameInfo.holdingsSize
6258 || x == BOARD_RGHT+1 && y >= gameInfo.holdingsSize) )
6261 autoQueen = appData.alwaysPromoteToQueen;
6264 if(!appData.oneClick || !OnlyMove(&x, &y, FALSE)) {
6265 if (clickType == Press) {
6267 if (OKToStartUserMove(x, y)) {
6271 MarkTargetSquares(0);
6272 DragPieceBegin(xPix, yPix);
6273 if (appData.highlightDragging) {
6274 SetHighlights(x, y, -1, -1);
6283 if (clickType == Press && gameMode != EditPosition) {
6288 // ignore off-board to clicks
6289 if(y < 0 || x < 0) return;
6291 /* Check if clicking again on the same color piece */
6292 fromP = boards[currentMove][fromY][fromX];
6293 toP = boards[currentMove][y][x];
6294 frc = gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom;
6295 if ((WhitePawn <= fromP && fromP <= WhiteKing &&
6296 WhitePawn <= toP && toP <= WhiteKing &&
6297 !(fromP == WhiteKing && toP == WhiteRook && frc) &&
6298 !(fromP == WhiteRook && toP == WhiteKing && frc)) ||
6299 (BlackPawn <= fromP && fromP <= BlackKing &&
6300 BlackPawn <= toP && toP <= BlackKing &&
6301 !(fromP == BlackRook && toP == BlackKing && frc) && // allow also RxK as FRC castling
6302 !(fromP == BlackKing && toP == BlackRook && frc))) {
6303 /* Clicked again on same color piece -- changed his mind */
6304 second = (x == fromX && y == fromY);
6305 if(!second || !OnlyMove(&x, &y, TRUE)) {
6306 if (appData.highlightDragging) {
6307 SetHighlights(x, y, -1, -1);
6311 if (OKToStartUserMove(x, y)) {
6314 MarkTargetSquares(0);
6315 DragPieceBegin(xPix, yPix);
6320 // ignore clicks on holdings
6321 if(x < BOARD_LEFT || x >= BOARD_RGHT) return;
6324 if (clickType == Release && x == fromX && y == fromY) {
6325 DragPieceEnd(xPix, yPix);
6326 if (appData.animateDragging) {
6327 /* Undo animation damage if any */
6328 DrawPosition(FALSE, NULL);
6331 /* Second up/down in same square; just abort move */
6336 ClearPremoveHighlights();
6338 /* First upclick in same square; start click-click mode */
6339 SetHighlights(x, y, -1, -1);
6344 /* we now have a different from- and (possibly off-board) to-square */
6345 /* Completed move */
6348 saveAnimate = appData.animate;
6349 if (clickType == Press) {
6350 /* Finish clickclick move */
6351 if (appData.animate || appData.highlightLastMove) {
6352 SetHighlights(fromX, fromY, toX, toY);
6357 /* Finish drag move */
6358 if (appData.highlightLastMove) {
6359 SetHighlights(fromX, fromY, toX, toY);
6363 DragPieceEnd(xPix, yPix);
6364 /* Don't animate move and drag both */
6365 appData.animate = FALSE;
6368 // moves into holding are invalid for now (except in EditPosition, adapting to-square)
6369 if(x >= 0 && x < BOARD_LEFT || x >= BOARD_RGHT) {
6370 ChessSquare piece = boards[currentMove][fromY][fromX];
6371 if(gameMode == EditPosition && piece != EmptySquare &&
6372 fromX >= BOARD_LEFT && fromX < BOARD_RGHT) {
6375 if(x == BOARD_LEFT-2 && piece >= BlackPawn) {
6376 n = PieceToNumber(piece - (int)BlackPawn);
6377 if(n >= gameInfo.holdingsSize) { n = 0; piece = BlackPawn; }
6378 boards[currentMove][BOARD_HEIGHT-1 - n][0] = piece;
6379 boards[currentMove][BOARD_HEIGHT-1 - n][1]++;
6381 if(x == BOARD_RGHT+1 && piece < BlackPawn) {
6382 n = PieceToNumber(piece);
6383 if(n >= gameInfo.holdingsSize) { n = 0; piece = WhitePawn; }
6384 boards[currentMove][n][BOARD_WIDTH-1] = piece;
6385 boards[currentMove][n][BOARD_WIDTH-2]++;
6387 boards[currentMove][fromY][fromX] = EmptySquare;
6391 DrawPosition(TRUE, boards[currentMove]);
6395 // off-board moves should not be highlighted
6396 if(x < 0 || x < 0) ClearHighlights();
6398 if (HasPromotionChoice(fromX, fromY, toX, toY, &promoChoice)) {
6399 SetHighlights(fromX, fromY, toX, toY);
6400 if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat) {
6401 // [HGM] super: promotion to captured piece selected from holdings
6402 ChessSquare p = boards[currentMove][fromY][fromX], q = boards[currentMove][toY][toX];
6403 promotionChoice = TRUE;
6404 // kludge follows to temporarily execute move on display, without promoting yet
6405 boards[currentMove][fromY][fromX] = EmptySquare; // move Pawn to 8th rank
6406 boards[currentMove][toY][toX] = p;
6407 DrawPosition(FALSE, boards[currentMove]);
6408 boards[currentMove][fromY][fromX] = p; // take back, but display stays
6409 boards[currentMove][toY][toX] = q;
6410 DisplayMessage("Click in holdings to choose piece", "");
6415 UserMoveEvent(fromX, fromY, toX, toY, promoChoice);
6416 if (!appData.highlightLastMove || gotPremove) ClearHighlights();
6417 if (gotPremove) SetPremoveHighlights(fromX, fromY, toX, toY);
6420 appData.animate = saveAnimate;
6421 if (appData.animate || appData.animateDragging) {
6422 /* Undo animation damage if needed */
6423 DrawPosition(FALSE, NULL);
6427 int RightClick(ClickType action, int x, int y, int *fromX, int *fromY)
6428 { // front-end-free part taken out of PieceMenuPopup
6429 int whichMenu; int xSqr, ySqr;
6431 if(seekGraphUp) { // [HGM] seekgraph
6432 if(action == Press) SeekGraphClick(Press, x, y, 2); // 2 indicates right-click: no pop-down on miss
6433 if(action == Release) SeekGraphClick(Release, x, y, 2); // and no challenge on hit
6437 if((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)
6438 && !appData.zippyPlay && appData.bgObserve) { // [HGM] bughouse: show background game
6439 if(action == Press) { flipView = !flipView; DrawPosition(TRUE, partnerBoard); partnerUp = TRUE; } else
6440 if(action == Release) { flipView = !flipView; DrawPosition(TRUE, boards[currentMove]); partnerUp = FALSE; }
6444 xSqr = EventToSquare(x, BOARD_WIDTH);
6445 ySqr = EventToSquare(y, BOARD_HEIGHT);
6446 if (action == Release) UnLoadPV(); // [HGM] pv
6447 if (action != Press) return -2; // return code to be ignored
6450 if(xSqr < BOARD_LEFT || xSqr >= BOARD_RGHT) return -1;
\r
6452 if (xSqr == BOARD_LEFT-1 || xSqr == BOARD_RGHT) return -1;
\r
6453 if (xSqr < 0 || ySqr < 0) return -1;
\r
6454 whichMenu = 0; // edit-position menu
6457 if(!appData.icsEngineAnalyze) return -1;
6458 case IcsPlayingWhite:
6459 case IcsPlayingBlack:
6460 if(!appData.zippyPlay) goto noZip;
6463 case MachinePlaysWhite:
6464 case MachinePlaysBlack:
6465 case TwoMachinesPlay: // [HGM] pv: use for showing PV
6466 if (!appData.dropMenu) {
6468 return 2; // flag front-end to grab mouse events
6470 if(gameMode == TwoMachinesPlay || gameMode == AnalyzeMode ||
6471 gameMode == AnalyzeFile || gameMode == IcsObserving) return -1;
6474 if (xSqr < 0 || ySqr < 0) return -1;
6475 if (!appData.dropMenu || appData.testLegality &&
6476 gameInfo.variant != VariantBughouse &&
6477 gameInfo.variant != VariantCrazyhouse) return -1;
6478 whichMenu = 1; // drop menu
6484 if (((*fromX = xSqr) < 0) ||
6485 ((*fromY = ySqr) < 0)) {
6486 *fromX = *fromY = -1;
6490 *fromX = BOARD_WIDTH - 1 - *fromX;
6492 *fromY = BOARD_HEIGHT - 1 - *fromY;
6497 void SendProgramStatsToFrontend( ChessProgramState * cps, ChessProgramStats * cpstats )
6499 // char * hint = lastHint;
6500 FrontEndProgramStats stats;
6502 stats.which = cps == &first ? 0 : 1;
6503 stats.depth = cpstats->depth;
6504 stats.nodes = cpstats->nodes;
6505 stats.score = cpstats->score;
6506 stats.time = cpstats->time;
6507 stats.pv = cpstats->movelist;
6508 stats.hint = lastHint;
6509 stats.an_move_index = 0;
6510 stats.an_move_count = 0;
6512 if( gameMode == AnalyzeMode || gameMode == AnalyzeFile ) {
6513 stats.hint = cpstats->move_name;
6514 stats.an_move_index = cpstats->nr_moves - cpstats->moves_left;
6515 stats.an_move_count = cpstats->nr_moves;
6518 if(stats.pv && stats.pv[0]) strcpy(lastPV[stats.which], stats.pv); // [HGM] pv: remember last PV of each
6520 SetProgramStats( &stats );
6524 Adjudicate(ChessProgramState *cps)
6525 { // [HGM] some adjudications useful with buggy engines
6526 // [HGM] adjudicate: made into separate routine, which now can be called after every move
6527 // In any case it determnes if the game is a claimable draw (filling in EP_STATUS).
6528 // Actually ending the game is now based on the additional internal condition canAdjudicate.
6529 // Only when the game is ended, and the opponent is a computer, this opponent gets the move relayed.
6530 int k, count = 0; static int bare = 1;
6531 ChessProgramState *engineOpponent = (gameMode == TwoMachinesPlay ? cps->other : (cps ? NULL : &first));
6532 Boolean canAdjudicate = !appData.icsActive;
6534 // most tests only when we understand the game, i.e. legality-checking on, and (for the time being) no piece drops
6535 if(gameInfo.holdingsSize == 0 || gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat) {
6536 if( appData.testLegality )
6537 { /* [HGM] Some more adjudications for obstinate engines */
6538 int NrWN=0, NrBN=0, NrWB=0, NrBB=0, NrWR=0, NrBR=0,
6539 NrWQ=0, NrBQ=0, NrW=0, NrK=0, bishopsColor = 0,
6540 NrPieces=0, NrPawns=0, PawnAdvance=0, i, j;
6541 static int moveCount = 6;
6543 char *reason = NULL;
6545 /* Count what is on board. */
6546 for(i=0; i<BOARD_HEIGHT; i++) for(j=BOARD_LEFT; j<BOARD_RGHT; j++)
6547 { ChessSquare p = boards[forwardMostMove][i][j];
6551 { /* count B,N,R and other of each side */
6554 NrK++; break; // [HGM] atomic: count Kings
6558 case WhiteFerz: // [HGM] shatranj: kludge to mke it work in shatranj
6559 bishopsColor |= 1 << ((i^j)&1);
6564 case BlackFerz: // [HGM] shatranj: kludge to mke it work in shatranj
6565 bishopsColor |= 1 << ((i^j)&1);
6580 PawnAdvance += m; NrPawns++;
6582 NrPieces += (p != EmptySquare);
6583 NrW += ((int)p < (int)BlackPawn);
6584 if(gameInfo.variant == VariantXiangqi &&
6585 (p == WhiteFerz || p == WhiteAlfil || p == BlackFerz || p == BlackAlfil)) {
6586 NrPieces--; // [HGM] XQ: do not count purely defensive pieces
6587 NrW -= ((int)p < (int)BlackPawn);
6591 /* Some material-based adjudications that have to be made before stalemate test */
6592 if(gameInfo.variant == VariantAtomic && NrK < 2) {
6593 // [HGM] atomic: stm must have lost his King on previous move, as destroying own K is illegal
6594 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE; // make claimable as if stm is checkmated
6595 if(canAdjudicate && appData.checkMates) {
6597 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets move
6598 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6599 GameEnds( WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins,
6600 "Xboard adjudication: King destroyed", GE_XBOARD );
6605 /* Bare King in Shatranj (loses) or Losers (wins) */
6606 if( NrW == 1 || NrPieces - NrW == 1) {
6607 if( gameInfo.variant == VariantLosers) { // [HGM] losers: bare King wins (stm must have it first)
6608 boards[forwardMostMove][EP_STATUS] = EP_WINS; // mark as win, so it becomes claimable
6609 if(canAdjudicate && appData.checkMates) {
6611 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets to see move
6612 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6613 GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
6614 "Xboard adjudication: Bare king", GE_XBOARD );
6618 if( gameInfo.variant == VariantShatranj && --bare < 0)
6620 boards[forwardMostMove][EP_STATUS] = EP_WINS; // make claimable as win for stm
6621 if(canAdjudicate && appData.checkMates) {
6622 /* but only adjudicate if adjudication enabled */
6624 SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets move
6625 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6626 GameEnds( NrW > 1 ? WhiteWins : NrPieces - NrW > 1 ? BlackWins : GameIsDrawn,
6627 "Xboard adjudication: Bare king", GE_XBOARD );
6634 // don't wait for engine to announce game end if we can judge ourselves
6635 switch (MateTest(boards[forwardMostMove], PosFlags(forwardMostMove)) ) {
6637 if(gameInfo.variant == Variant3Check) { // [HGM] 3check: when in check, test if 3rd time
6638 int i, checkCnt = 0; // (should really be done by making nr of checks part of game state)
6639 for(i=forwardMostMove-2; i>=backwardMostMove; i-=2) {
6640 if(MateTest(boards[i], PosFlags(i)) == MT_CHECK)
6643 reason = "Xboard adjudication: 3rd check";
6644 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE;
6654 reason = "Xboard adjudication: Stalemate";
6655 if((signed char)boards[forwardMostMove][EP_STATUS] != EP_CHECKMATE) { // [HGM] don't touch win through baring or K-capt
6656 boards[forwardMostMove][EP_STATUS] = EP_STALEMATE; // default result for stalemate is draw
6657 if(gameInfo.variant == VariantLosers || gameInfo.variant == VariantGiveaway) // [HGM] losers:
6658 boards[forwardMostMove][EP_STATUS] = EP_WINS; // in these variants stalemated is always a win
6659 else if(gameInfo.variant == VariantSuicide) // in suicide it depends
6660 boards[forwardMostMove][EP_STATUS] = NrW == NrPieces-NrW ? EP_STALEMATE :
6661 ((NrW < NrPieces-NrW) != WhiteOnMove(forwardMostMove) ?
6662 EP_CHECKMATE : EP_WINS);
6663 else if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantXiangqi)
6664 boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE; // and in these variants being stalemated loses
6668 reason = "Xboard adjudication: Checkmate";
6669 boards[forwardMostMove][EP_STATUS] = (gameInfo.variant == VariantLosers ? EP_WINS : EP_CHECKMATE);
6673 switch(i = (signed char)boards[forwardMostMove][EP_STATUS]) {
6675 result = GameIsDrawn; break;
6677 result = WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins; break;
6679 result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins; break;
6681 result = (ChessMove) 0;
6683 if(canAdjudicate && appData.checkMates && result) { // [HGM] mates: adjudicate finished games if requested
6685 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
6686 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6687 GameEnds( result, reason, GE_XBOARD );
6691 /* Next absolutely insufficient mating material. */
6692 if( NrPieces == 2 || gameInfo.variant != VariantXiangqi &&
6693 gameInfo.variant != VariantShatranj && // [HGM] baring will remain possible
6694 (NrPieces == 3 && NrWN+NrBN+NrWB+NrBB == 1 ||
6695 NrPieces == NrBB+NrWB+2 && bishopsColor != 3)) // [HGM] all Bishops (Ferz!) same color
6696 { /* KBK, KNK, KK of KBKB with like Bishops */
6698 /* always flag draws, for judging claims */
6699 boards[forwardMostMove][EP_STATUS] = EP_INSUF_DRAW;
6701 if(canAdjudicate && appData.materialDraws) {
6702 /* but only adjudicate them if adjudication enabled */
6703 if(engineOpponent) {
6704 SendToProgram("force\n", engineOpponent); // suppress reply
6705 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see last move */
6707 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6708 GameEnds( GameIsDrawn, "Xboard adjudication: Insufficient mating material", GE_XBOARD );
6713 /* Then some trivial draws (only adjudicate, cannot be claimed) */
6715 ( NrWR == 1 && NrBR == 1 /* KRKR */
6716 || NrWQ==1 && NrBQ==1 /* KQKQ */
6717 || NrWN==2 || NrBN==2 /* KNNK */
6718 || NrWN+NrWB == 1 && NrBN+NrBB == 1 /* KBKN, KBKB, KNKN */
6720 if(canAdjudicate && --moveCount < 0 && appData.trivialDraws)
6721 { /* if the first 3 moves do not show a tactical win, declare draw */
6722 if(engineOpponent) {
6723 SendToProgram("force\n", engineOpponent); // suppress reply
6724 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
6726 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6727 GameEnds( GameIsDrawn, "Xboard adjudication: Trivial draw", GE_XBOARD );
6730 } else moveCount = 6;
6734 if (appData.debugMode) { int i;
6735 fprintf(debugFP, "repeat test fmm=%d bmm=%d ep=%d, reps=%d\n",
6736 forwardMostMove, backwardMostMove, boards[backwardMostMove][EP_STATUS],
6737 appData.drawRepeats);
6738 for( i=forwardMostMove; i>=backwardMostMove; i-- )
6739 fprintf(debugFP, "%d ep=%d\n", i, (signed char)boards[i][EP_STATUS]);
6743 // Repetition draws and 50-move rule can be applied independently of legality testing
6745 /* Check for rep-draws */
6747 for(k = forwardMostMove-2;
6748 k>=backwardMostMove && k>=forwardMostMove-100 &&
6749 (signed char)boards[k][EP_STATUS] < EP_UNKNOWN &&
6750 (signed char)boards[k+2][EP_STATUS] <= EP_NONE && (signed char)boards[k+1][EP_STATUS] <= EP_NONE;
6753 if(CompareBoards(boards[k], boards[forwardMostMove])) {
6754 /* compare castling rights */
6755 if( boards[forwardMostMove][CASTLING][2] != boards[k][CASTLING][2] &&
6756 (boards[k][CASTLING][0] != NoRights || boards[k][CASTLING][1] != NoRights) )
6757 rights++; /* King lost rights, while rook still had them */
6758 if( boards[forwardMostMove][CASTLING][2] != NoRights ) { /* king has rights */
6759 if( boards[forwardMostMove][CASTLING][0] != boards[k][CASTLING][0] ||
6760 boards[forwardMostMove][CASTLING][1] != boards[k][CASTLING][1] )
6761 rights++; /* but at least one rook lost them */
6763 if( boards[forwardMostMove][CASTLING][5] != boards[k][CASTLING][5] &&
6764 (boards[k][CASTLING][3] != NoRights || boards[k][CASTLING][4] != NoRights) )
6766 if( boards[forwardMostMove][CASTLING][5] != NoRights ) {
6767 if( boards[forwardMostMove][CASTLING][3] != boards[k][CASTLING][3] ||
6768 boards[forwardMostMove][CASTLING][4] != boards[k][CASTLING][4] )
6771 if( canAdjudicate && rights == 0 && ++count > appData.drawRepeats-2
6772 && appData.drawRepeats > 1) {
6773 /* adjudicate after user-specified nr of repeats */
6774 if(engineOpponent) {
6775 SendToProgram("force\n", engineOpponent); // suppress reply
6776 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
6778 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6779 if(gameInfo.variant == VariantXiangqi && appData.testLegality) {
6780 // [HGM] xiangqi: check for forbidden perpetuals
6781 int m, ourPerpetual = 1, hisPerpetual = 1;
6782 for(m=forwardMostMove; m>k; m-=2) {
6783 if(MateTest(boards[m], PosFlags(m)) != MT_CHECK)
6784 ourPerpetual = 0; // the current mover did not always check
6785 if(MateTest(boards[m-1], PosFlags(m-1)) != MT_CHECK)
6786 hisPerpetual = 0; // the opponent did not always check
6788 if(appData.debugMode) fprintf(debugFP, "XQ perpetual test, our=%d, his=%d\n",
6789 ourPerpetual, hisPerpetual);
6790 if(ourPerpetual && !hisPerpetual) { // we are actively checking him: forfeit
6791 GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
6792 "Xboard adjudication: perpetual checking", GE_XBOARD );
6795 if(hisPerpetual && !ourPerpetual) // he is checking us, but did not repeat yet
6796 break; // (or we would have caught him before). Abort repetition-checking loop.
6797 // Now check for perpetual chases
6798 if(!ourPerpetual && !hisPerpetual) { // no perpetual check, test for chase
6799 hisPerpetual = PerpetualChase(k, forwardMostMove);
6800 ourPerpetual = PerpetualChase(k+1, forwardMostMove);
6801 if(ourPerpetual && !hisPerpetual) { // we are actively chasing him: forfeit
6802 GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
6803 "Xboard adjudication: perpetual chasing", GE_XBOARD );
6806 if(hisPerpetual && !ourPerpetual) // he is chasing us, but did not repeat yet
6807 break; // Abort repetition-checking loop.
6809 // if neither of us is checking or chasing all the time, or both are, it is draw
6811 GameEnds( GameIsDrawn, "Xboard adjudication: repetition draw", GE_XBOARD );
6814 if( rights == 0 && count > 1 ) /* occurred 2 or more times before */
6815 boards[forwardMostMove][EP_STATUS] = EP_REP_DRAW;
6819 /* Now we test for 50-move draws. Determine ply count */
6820 count = forwardMostMove;
6821 /* look for last irreversble move */
6822 while( (signed char)boards[count][EP_STATUS] <= EP_NONE && count > backwardMostMove )
6824 /* if we hit starting position, add initial plies */
6825 if( count == backwardMostMove )
6826 count -= initialRulePlies;
6827 count = forwardMostMove - count;
6829 boards[forwardMostMove][EP_STATUS] = EP_RULE_DRAW;
6830 /* this is used to judge if draw claims are legal */
6831 if(canAdjudicate && appData.ruleMoves > 0 && count >= 2*appData.ruleMoves) {
6832 if(engineOpponent) {
6833 SendToProgram("force\n", engineOpponent); // suppress reply
6834 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
6836 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6837 GameEnds( GameIsDrawn, "Xboard adjudication: 50-move rule", GE_XBOARD );
6841 /* if draw offer is pending, treat it as a draw claim
6842 * when draw condition present, to allow engines a way to
6843 * claim draws before making their move to avoid a race
6844 * condition occurring after their move
6846 if((gameMode == TwoMachinesPlay ? second.offeredDraw : userOfferedDraw) || first.offeredDraw ) {
6848 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_RULE_DRAW)
6849 p = "Draw claim: 50-move rule";
6850 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_REP_DRAW)
6851 p = "Draw claim: 3-fold repetition";
6852 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_INSUF_DRAW)
6853 p = "Draw claim: insufficient mating material";
6854 if( p != NULL && canAdjudicate) {
6855 if(engineOpponent) {
6856 SendToProgram("force\n", engineOpponent); // suppress reply
6857 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
6859 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6860 GameEnds( GameIsDrawn, p, GE_XBOARD );
6865 if( canAdjudicate && appData.adjudicateDrawMoves > 0 && forwardMostMove > (2*appData.adjudicateDrawMoves) ) {
6866 if(engineOpponent) {
6867 SendToProgram("force\n", engineOpponent); // suppress reply
6868 SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
6870 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6871 GameEnds( GameIsDrawn, "Xboard adjudication: long game", GE_XBOARD );
6877 char *SendMoveToBookUser(int moveNr, ChessProgramState *cps, int initial)
6878 { // [HGM] book: this routine intercepts moves to simulate book replies
6879 char *bookHit = NULL;
6881 //first determine if the incoming move brings opponent into his book
6882 if(appData.usePolyglotBook && (cps == &first ? !appData.firstHasOwnBookUCI : !appData.secondHasOwnBookUCI))
6883 bookHit = ProbeBook(moveNr+1, appData.polyglotBook); // returns move
6884 if(appData.debugMode) fprintf(debugFP, "book hit = %s\n", bookHit ? bookHit : "(NULL)");
6885 if(bookHit != NULL && !cps->bookSuspend) {
6886 // make sure opponent is not going to reply after receiving move to book position
6887 SendToProgram("force\n", cps);
6888 cps->bookSuspend = TRUE; // flag indicating it has to be restarted
6890 if(!initial) SendMoveToProgram(moveNr, cps); // with hit on initial position there is no move
6891 // now arrange restart after book miss
6893 // after a book hit we never send 'go', and the code after the call to this routine
6894 // has '&& !bookHit' added to suppress potential sending there (based on 'firstMove').
6896 if (cps->useUsermove) sprintf(buf, "usermove "); // sorry, no SAN yet :(
6897 sprintf(buf, "%s\n", bookHit); // force book move into program supposed to play it
6898 SendToProgram(buf, cps);
6899 if(!initial) firstMove = FALSE; // normally we would clear the firstMove condition after return & sending 'go'
6900 } else if(initial) { // 'go' was needed irrespective of firstMove, and it has to be done in this routine
6901 SendToProgram("go\n", cps);
6902 cps->bookSuspend = FALSE; // after a 'go' we are never suspended
6903 } else { // 'go' might be sent based on 'firstMove' after this routine returns
6904 if(cps->bookSuspend && !firstMove) // 'go' needed, and it will not be done after we return
6905 SendToProgram("go\n", cps);
6906 cps->bookSuspend = FALSE; // anyhow, we will not be suspended after a miss
6908 return bookHit; // notify caller of hit, so it can take action to send move to opponent
6912 ChessProgramState *savedState;
6913 void DeferredBookMove(void)
6915 if(savedState->lastPing != savedState->lastPong)
6916 ScheduleDelayedEvent(DeferredBookMove, 10);
6918 HandleMachineMove(savedMessage, savedState);
6922 HandleMachineMove(message, cps)
6924 ChessProgramState *cps;
6926 char machineMove[MSG_SIZ], buf1[MSG_SIZ*10], buf2[MSG_SIZ];
6927 char realname[MSG_SIZ];
6928 int fromX, fromY, toX, toY;
6937 FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book hit
6939 * Kludge to ignore BEL characters
6941 while (*message == '\007') message++;
6944 * [HGM] engine debug message: ignore lines starting with '#' character
6946 if(cps->debug && *message == '#') return;
6949 * Look for book output
6951 if (cps == &first && bookRequested) {
6952 if (message[0] == '\t' || message[0] == ' ') {
6953 /* Part of the book output is here; append it */
6954 strcat(bookOutput, message);
6955 strcat(bookOutput, " \n");
6957 } else if (bookOutput[0] != NULLCHAR) {
6958 /* All of book output has arrived; display it */
6959 char *p = bookOutput;
6960 while (*p != NULLCHAR) {
6961 if (*p == '\t') *p = ' ';
6964 DisplayInformation(bookOutput);
6965 bookRequested = FALSE;
6966 /* Fall through to parse the current output */
6971 * Look for machine move.
6973 if ((sscanf(message, "%s %s %s", buf1, buf2, machineMove) == 3 && strcmp(buf2, "...") == 0) ||
6974 (sscanf(message, "%s %s", buf1, machineMove) == 2 && strcmp(buf1, "move") == 0))
6976 /* This method is only useful on engines that support ping */
6977 if (cps->lastPing != cps->lastPong) {
6978 if (gameMode == BeginningOfGame) {
6979 /* Extra move from before last new; ignore */
6980 if (appData.debugMode) {
6981 fprintf(debugFP, "Ignoring extra move from %s\n", cps->which);
6984 if (appData.debugMode) {
6985 fprintf(debugFP, "Undoing extra move from %s, gameMode %d\n",
6986 cps->which, gameMode);
6989 SendToProgram("undo\n", cps);
6995 case BeginningOfGame:
6996 /* Extra move from before last reset; ignore */
6997 if (appData.debugMode) {
6998 fprintf(debugFP, "Ignoring extra move from %s\n", cps->which);
7005 /* Extra move after we tried to stop. The mode test is
7006 not a reliable way of detecting this problem, but it's
7007 the best we can do on engines that don't support ping.
7009 if (appData.debugMode) {
7010 fprintf(debugFP, "Undoing extra move from %s, gameMode %d\n",
7011 cps->which, gameMode);
7013 SendToProgram("undo\n", cps);
7016 case MachinePlaysWhite:
7017 case IcsPlayingWhite:
7018 machineWhite = TRUE;
7021 case MachinePlaysBlack:
7022 case IcsPlayingBlack:
7023 machineWhite = FALSE;
7026 case TwoMachinesPlay:
7027 machineWhite = (cps->twoMachinesColor[0] == 'w');
7030 if (WhiteOnMove(forwardMostMove) != machineWhite) {
7031 if (appData.debugMode) {
7033 "Ignoring move out of turn by %s, gameMode %d"
7034 ", forwardMost %d\n",
7035 cps->which, gameMode, forwardMostMove);
7040 if (appData.debugMode) { int f = forwardMostMove;
7041 fprintf(debugFP, "machine move %d, castling = %d %d %d %d %d %d\n", f,
7042 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
7043 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
7045 if(cps->alphaRank) AlphaRank(machineMove, 4);
7046 if (!ParseOneMove(machineMove, forwardMostMove, &moveType,
7047 &fromX, &fromY, &toX, &toY, &promoChar)) {
7048 /* Machine move could not be parsed; ignore it. */
7049 sprintf(buf1, _("Illegal move \"%s\" from %s machine"),
7050 machineMove, cps->which);
7051 DisplayError(buf1, 0);
7052 sprintf(buf1, "Xboard: Forfeit due to invalid move: %s (%c%c%c%c) res=%d",
7053 machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, moveType);
7054 if (gameMode == TwoMachinesPlay) {
7055 GameEnds(machineWhite ? BlackWins : WhiteWins,
7061 /* [HGM] Apparently legal, but so far only tested with EP_UNKOWN */
7062 /* So we have to redo legality test with true e.p. status here, */
7063 /* to make sure an illegal e.p. capture does not slip through, */
7064 /* to cause a forfeit on a justified illegal-move complaint */
7065 /* of the opponent. */
7066 if( gameMode==TwoMachinesPlay && appData.testLegality
7067 && fromY != DROP_RANK /* [HGM] temporary; should still add legality test for drops */
7070 moveType = LegalityTest(boards[forwardMostMove], PosFlags(forwardMostMove),
7071 fromY, fromX, toY, toX, promoChar);
7072 if (appData.debugMode) {
7074 for(i=0; i< nrCastlingRights; i++) fprintf(debugFP, "(%d,%d) ",
7075 boards[forwardMostMove][CASTLING][i], castlingRank[i]);
7076 fprintf(debugFP, "castling rights\n");
7078 if(moveType == IllegalMove) {
7079 sprintf(buf1, "Xboard: Forfeit due to illegal move: %s (%c%c%c%c)%c",
7080 machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, 0);
7081 GameEnds(machineWhite ? BlackWins : WhiteWins,
7084 } else if(gameInfo.variant != VariantFischeRandom && gameInfo.variant != VariantCapaRandom)
7085 /* [HGM] Kludge to handle engines that send FRC-style castling
7086 when they shouldn't (like TSCP-Gothic) */
7088 case WhiteASideCastleFR:
7089 case BlackASideCastleFR:
7091 currentMoveString[2]++;
7093 case WhiteHSideCastleFR:
7094 case BlackHSideCastleFR:
7096 currentMoveString[2]--;
7098 default: ; // nothing to do, but suppresses warning of pedantic compilers
7101 hintRequested = FALSE;
7102 lastHint[0] = NULLCHAR;
7103 bookRequested = FALSE;
7104 /* Program may be pondering now */
7105 cps->maybeThinking = TRUE;
7106 if (cps->sendTime == 2) cps->sendTime = 1;
7107 if (cps->offeredDraw) cps->offeredDraw--;
7109 /* currentMoveString is set as a side-effect of ParseOneMove */
7110 strcpy(machineMove, currentMoveString);
7111 strcat(machineMove, "\n");
7112 strcpy(moveList[forwardMostMove], machineMove);
7114 MakeMove(fromX, fromY, toX, toY, promoChar);/*updates forwardMostMove*/
7116 /* [AS] Adjudicate game if needed (note: remember that forwardMostMove now points past the last move) */
7117 if( gameMode == TwoMachinesPlay && adjudicateLossThreshold != 0 && forwardMostMove >= adjudicateLossPlies ) {
7120 while( count < adjudicateLossPlies ) {
7121 int score = pvInfoList[ forwardMostMove - count - 1 ].score;
7124 score = -score; /* Flip score for winning side */
7127 if( score > adjudicateLossThreshold ) {
7134 if( count >= adjudicateLossPlies ) {
7135 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
7137 GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins,
7138 "Xboard adjudication",
7145 if(Adjudicate(cps)) return; // [HGM] adjudicate: for all automatic game ends
7148 if ((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack) &&
7150 if(cps->offeredDraw && (signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
7151 SendToICS(ics_prefix); // [HGM] drawclaim: send caim and move on one line for FICS
7153 SendMoveToICS(moveType, fromX, fromY, toX, toY);
7155 SendMoveToICS(moveType, fromX, fromY, toX, toY);
7157 if(appData.autoKibitz && !appData.icsEngineAnalyze ) { /* [HGM] kibitz: send most-recent PV info to ICS */
7158 char buf[3*MSG_SIZ];
7160 sprintf(buf, "kibitz !!! %+.2f/%d (%.2f sec, %u nodes, %.0f knps) PV=%s\n",
7161 programStats.score / 100.,
7163 programStats.time / 100.,
7164 (unsigned int)programStats.nodes,
7165 (unsigned int)programStats.nodes / (10*abs(programStats.time) + 1.),
7166 programStats.movelist);
7168 if(appData.debugMode) fprintf(debugFP, "nodes = %d, %lld\n", (int) programStats.nodes, programStats.nodes);
7173 /* [AS] Save move info and clear stats for next move */
7174 pvInfoList[ forwardMostMove-1 ].score = programStats.score;
7175 pvInfoList[ forwardMostMove-1 ].depth = programStats.depth;
7176 pvInfoList[ forwardMostMove-1 ].time = programStats.time; // [HGM] PGNtime: take time from engine stats
7177 ClearProgramStats();
7178 thinkOutput[0] = NULLCHAR;
7179 hiddenThinkOutputState = 0;
7182 if (gameMode == TwoMachinesPlay) {
7183 /* [HGM] relaying draw offers moved to after reception of move */
7184 /* and interpreting offer as claim if it brings draw condition */
7185 if (cps->offeredDraw == 1 && cps->other->sendDrawOffers) {
7186 SendToProgram("draw\n", cps->other);
7188 if (cps->other->sendTime) {
7189 SendTimeRemaining(cps->other,
7190 cps->other->twoMachinesColor[0] == 'w');
7192 bookHit = SendMoveToBookUser(forwardMostMove-1, cps->other, FALSE);
7193 if (firstMove && !bookHit) {
7195 if (cps->other->useColors) {
7196 SendToProgram(cps->other->twoMachinesColor, cps->other);
7198 SendToProgram("go\n", cps->other);
7200 cps->other->maybeThinking = TRUE;
7203 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
7205 if (!pausing && appData.ringBellAfterMoves) {
7210 * Reenable menu items that were disabled while
7211 * machine was thinking
7213 if (gameMode != TwoMachinesPlay)
7214 SetUserThinkingEnables();
7216 // [HGM] book: after book hit opponent has received move and is now in force mode
7217 // force the book reply into it, and then fake that it outputted this move by jumping
7218 // back to the beginning of HandleMachineMove, with cps toggled and message set to this move
7220 static char bookMove[MSG_SIZ]; // a bit generous?
7222 strcpy(bookMove, "move ");
7223 strcat(bookMove, bookHit);
7226 programStats.nodes = programStats.depth = programStats.time =
7227 programStats.score = programStats.got_only_move = 0;
7228 sprintf(programStats.movelist, "%s (xbook)", bookHit);
7230 if(cps->lastPing != cps->lastPong) {
7231 savedMessage = message; // args for deferred call
7233 ScheduleDelayedEvent(DeferredBookMove, 10);
7242 /* Set special modes for chess engines. Later something general
7243 * could be added here; for now there is just one kludge feature,
7244 * needed because Crafty 15.10 and earlier don't ignore SIGINT
7245 * when "xboard" is given as an interactive command.
7247 if (strncmp(message, "kibitz Hello from Crafty", 24) == 0) {
7248 cps->useSigint = FALSE;
7249 cps->useSigterm = FALSE;
7251 if (strncmp(message, "feature ", 8) == 0) { // [HGM] moved forward to pre-empt non-compliant commands
7252 ParseFeatures(message+8, cps);
7253 return; // [HGM] This return was missing, causing option features to be recognized as non-compliant commands!
7256 /* [HGM] Allow engine to set up a position. Don't ask me why one would
7257 * want this, I was asked to put it in, and obliged.
7259 if (!strncmp(message, "setboard ", 9)) {
7260 Board initial_position;
7262 GameEnds(GameUnfinished, "Engine aborts game", GE_XBOARD);
7264 if (!ParseFEN(initial_position, &blackPlaysFirst, message + 9)) {
7265 DisplayError(_("Bad FEN received from engine"), 0);
7269 CopyBoard(boards[0], initial_position);
7270 initialRulePlies = FENrulePlies;
7271 if(blackPlaysFirst) gameMode = MachinePlaysWhite;
7272 else gameMode = MachinePlaysBlack;
7273 DrawPosition(FALSE, boards[currentMove]);
7279 * Look for communication commands
7281 if (!strncmp(message, "telluser ", 9)) {
7282 DisplayNote(message + 9);
7285 if (!strncmp(message, "tellusererror ", 14)) {
7287 DisplayError(message + 14, 0);
7290 if (!strncmp(message, "tellopponent ", 13)) {
7291 if (appData.icsActive) {
7293 snprintf(buf1, sizeof(buf1), "%ssay %s\n", ics_prefix, message + 13);
7297 DisplayNote(message + 13);
7301 if (!strncmp(message, "tellothers ", 11)) {
7302 if (appData.icsActive) {
7304 snprintf(buf1, sizeof(buf1), "%swhisper %s\n", ics_prefix, message + 11);
7310 if (!strncmp(message, "tellall ", 8)) {
7311 if (appData.icsActive) {
7313 snprintf(buf1, sizeof(buf1), "%skibitz %s\n", ics_prefix, message + 8);
7317 DisplayNote(message + 8);
7321 if (strncmp(message, "warning", 7) == 0) {
7322 /* Undocumented feature, use tellusererror in new code */
7323 DisplayError(message, 0);
7326 if (sscanf(message, "askuser %s %[^\n]", buf1, buf2) == 2) {
7327 strcpy(realname, cps->tidy);
7328 strcat(realname, " query");
7329 AskQuestion(realname, buf2, buf1, cps->pr);
7332 /* Commands from the engine directly to ICS. We don't allow these to be
7333 * sent until we are logged on. Crafty kibitzes have been known to
7334 * interfere with the login process.
7337 if (!strncmp(message, "tellics ", 8)) {
7338 SendToICS(message + 8);
7342 if (!strncmp(message, "tellicsnoalias ", 15)) {
7343 SendToICS(ics_prefix);
7344 SendToICS(message + 15);
7348 /* The following are for backward compatibility only */
7349 if (!strncmp(message,"whisper",7) || !strncmp(message,"kibitz",6) ||
7350 !strncmp(message,"draw",4) || !strncmp(message,"tell",3)) {
7351 SendToICS(ics_prefix);
7357 if (sscanf(message, "pong %d", &cps->lastPong) == 1) {
7361 * If the move is illegal, cancel it and redraw the board.
7362 * Also deal with other error cases. Matching is rather loose
7363 * here to accommodate engines written before the spec.
7365 if (strncmp(message + 1, "llegal move", 11) == 0 ||
7366 strncmp(message, "Error", 5) == 0) {
7367 if (StrStr(message, "name") ||
7368 StrStr(message, "rating") || StrStr(message, "?") ||
7369 StrStr(message, "result") || StrStr(message, "board") ||
7370 StrStr(message, "bk") || StrStr(message, "computer") ||
7371 StrStr(message, "variant") || StrStr(message, "hint") ||
7372 StrStr(message, "random") || StrStr(message, "depth") ||
7373 StrStr(message, "accepted")) {
7376 if (StrStr(message, "protover")) {
7377 /* Program is responding to input, so it's apparently done
7378 initializing, and this error message indicates it is
7379 protocol version 1. So we don't need to wait any longer
7380 for it to initialize and send feature commands. */
7381 FeatureDone(cps, 1);
7382 cps->protocolVersion = 1;
7385 cps->maybeThinking = FALSE;
7387 if (StrStr(message, "draw")) {
7388 /* Program doesn't have "draw" command */
7389 cps->sendDrawOffers = 0;
7392 if (cps->sendTime != 1 &&
7393 (StrStr(message, "time") || StrStr(message, "otim"))) {
7394 /* Program apparently doesn't have "time" or "otim" command */
7398 if (StrStr(message, "analyze")) {
7399 cps->analysisSupport = FALSE;
7400 cps->analyzing = FALSE;
7402 sprintf(buf2, _("%s does not support analysis"), cps->tidy);
7403 DisplayError(buf2, 0);
7406 if (StrStr(message, "(no matching move)st")) {
7407 /* Special kludge for GNU Chess 4 only */
7408 cps->stKludge = TRUE;
7409 SendTimeControl(cps, movesPerSession, timeControl,
7410 timeIncrement, appData.searchDepth,
7414 if (StrStr(message, "(no matching move)sd")) {
7415 /* Special kludge for GNU Chess 4 only */
7416 cps->sdKludge = TRUE;
7417 SendTimeControl(cps, movesPerSession, timeControl,
7418 timeIncrement, appData.searchDepth,
7422 if (!StrStr(message, "llegal")) {
7425 if (gameMode == BeginningOfGame || gameMode == EndOfGame ||
7426 gameMode == IcsIdle) return;
7427 if (forwardMostMove <= backwardMostMove) return;
7428 if (pausing) PauseEvent();
7429 if(appData.forceIllegal) {
7430 // [HGM] illegal: machine refused move; force position after move into it
7431 SendToProgram("force\n", cps);
7432 if(!cps->useSetboard) { // hideous kludge on kludge, because SendBoard sucks.
7433 // we have a real problem now, as SendBoard will use the a2a3 kludge
7434 // when black is to move, while there might be nothing on a2 or black
7435 // might already have the move. So send the board as if white has the move.
7436 // But first we must change the stm of the engine, as it refused the last move
7437 SendBoard(cps, 0); // always kludgeless, as white is to move on boards[0]
7438 if(WhiteOnMove(forwardMostMove)) {
7439 SendToProgram("a7a6\n", cps); // for the engine black still had the move
7440 SendBoard(cps, forwardMostMove); // kludgeless board
7442 SendToProgram("a2a3\n", cps); // for the engine white still had the move
7443 CopyBoard(boards[forwardMostMove+1], boards[forwardMostMove]);
7444 SendBoard(cps, forwardMostMove+1); // kludgeless board
7446 } else SendBoard(cps, forwardMostMove); // FEN case, also sets stm properly
7447 if(gameMode == MachinePlaysWhite || gameMode == MachinePlaysBlack ||
7448 gameMode == TwoMachinesPlay)
7449 SendToProgram("go\n", cps);
7452 if (gameMode == PlayFromGameFile) {
7453 /* Stop reading this game file */
7454 gameMode = EditGame;
7457 currentMove = forwardMostMove-1;
7458 DisplayMove(currentMove-1); /* before DisplayMoveError */
7459 SwitchClocks(forwardMostMove-1); // [HGM] race
7460 DisplayBothClocks();
7461 sprintf(buf1, _("Illegal move \"%s\" (rejected by %s chess program)"),
7462 parseList[currentMove], cps->which);
7463 DisplayMoveError(buf1);
7464 DrawPosition(FALSE, boards[currentMove]);
7466 /* [HGM] illegal-move claim should forfeit game when Xboard */
7467 /* only passes fully legal moves */
7468 if( appData.testLegality && gameMode == TwoMachinesPlay ) {
7469 GameEnds( cps->twoMachinesColor[0] == 'w' ? BlackWins : WhiteWins,
7470 "False illegal-move claim", GE_XBOARD );
7474 if (strncmp(message, "time", 4) == 0 && StrStr(message, "Illegal")) {
7475 /* Program has a broken "time" command that
7476 outputs a string not ending in newline.
7482 * If chess program startup fails, exit with an error message.
7483 * Attempts to recover here are futile.
7485 if ((StrStr(message, "unknown host") != NULL)
7486 || (StrStr(message, "No remote directory") != NULL)
7487 || (StrStr(message, "not found") != NULL)
7488 || (StrStr(message, "No such file") != NULL)
7489 || (StrStr(message, "can't alloc") != NULL)
7490 || (StrStr(message, "Permission denied") != NULL)) {
7492 cps->maybeThinking = FALSE;
7493 snprintf(buf1, sizeof(buf1), _("Failed to start %s chess program %s on %s: %s\n"),
7494 cps->which, cps->program, cps->host, message);
7495 RemoveInputSource(cps->isr);
7496 DisplayFatalError(buf1, 0, 1);
7501 * Look for hint output
7503 if (sscanf(message, "Hint: %s", buf1) == 1) {
7504 if (cps == &first && hintRequested) {
7505 hintRequested = FALSE;
7506 if (ParseOneMove(buf1, forwardMostMove, &moveType,
7507 &fromX, &fromY, &toX, &toY, &promoChar)) {
7508 (void) CoordsToAlgebraic(boards[forwardMostMove],
7509 PosFlags(forwardMostMove),
7510 fromY, fromX, toY, toX, promoChar, buf1);
7511 snprintf(buf2, sizeof(buf2), _("Hint: %s"), buf1);
7512 DisplayInformation(buf2);
7514 /* Hint move could not be parsed!? */
7515 snprintf(buf2, sizeof(buf2),
7516 _("Illegal hint move \"%s\"\nfrom %s chess program"),
7518 DisplayError(buf2, 0);
7521 strcpy(lastHint, buf1);
7527 * Ignore other messages if game is not in progress
7529 if (gameMode == BeginningOfGame || gameMode == EndOfGame ||
7530 gameMode == IcsIdle || cps->lastPing != cps->lastPong) return;
7533 * look for win, lose, draw, or draw offer
7535 if (strncmp(message, "1-0", 3) == 0) {
7536 char *p, *q, *r = "";
7537 p = strchr(message, '{');
7545 GameEnds(WhiteWins, r, GE_ENGINE1 + (cps != &first)); /* [HGM] pass claimer indication for claim test */
7547 } else if (strncmp(message, "0-1", 3) == 0) {
7548 char *p, *q, *r = "";
7549 p = strchr(message, '{');
7557 /* Kludge for Arasan 4.1 bug */
7558 if (strcmp(r, "Black resigns") == 0) {
7559 GameEnds(WhiteWins, r, GE_ENGINE1 + (cps != &first));
7562 GameEnds(BlackWins, r, GE_ENGINE1 + (cps != &first));
7564 } else if (strncmp(message, "1/2", 3) == 0) {
7565 char *p, *q, *r = "";
7566 p = strchr(message, '{');
7575 GameEnds(GameIsDrawn, r, GE_ENGINE1 + (cps != &first));
7578 } else if (strncmp(message, "White resign", 12) == 0) {
7579 GameEnds(BlackWins, "White resigns", GE_ENGINE1 + (cps != &first));
7581 } else if (strncmp(message, "Black resign", 12) == 0) {
7582 GameEnds(WhiteWins, "Black resigns", GE_ENGINE1 + (cps != &first));
7584 } else if (strncmp(message, "White matches", 13) == 0 ||
7585 strncmp(message, "Black matches", 13) == 0 ) {
7586 /* [HGM] ignore GNUShogi noises */
7588 } else if (strncmp(message, "White", 5) == 0 &&
7589 message[5] != '(' &&
7590 StrStr(message, "Black") == NULL) {
7591 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
7593 } else if (strncmp(message, "Black", 5) == 0 &&
7594 message[5] != '(') {
7595 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
7597 } else if (strcmp(message, "resign") == 0 ||
7598 strcmp(message, "computer resigns") == 0) {
7600 case MachinePlaysBlack:
7601 case IcsPlayingBlack:
7602 GameEnds(WhiteWins, "Black resigns", GE_ENGINE);
7604 case MachinePlaysWhite:
7605 case IcsPlayingWhite:
7606 GameEnds(BlackWins, "White resigns", GE_ENGINE);
7608 case TwoMachinesPlay:
7609 if (cps->twoMachinesColor[0] == 'w')
7610 GameEnds(BlackWins, "White resigns", GE_ENGINE1 + (cps != &first));
7612 GameEnds(WhiteWins, "Black resigns", GE_ENGINE1 + (cps != &first));
7619 } else if (strncmp(message, "opponent mates", 14) == 0) {
7621 case MachinePlaysBlack:
7622 case IcsPlayingBlack:
7623 GameEnds(WhiteWins, "White mates", GE_ENGINE);
7625 case MachinePlaysWhite:
7626 case IcsPlayingWhite:
7627 GameEnds(BlackWins, "Black mates", GE_ENGINE);
7629 case TwoMachinesPlay:
7630 if (cps->twoMachinesColor[0] == 'w')
7631 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
7633 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
7640 } else if (strncmp(message, "computer mates", 14) == 0) {
7642 case MachinePlaysBlack:
7643 case IcsPlayingBlack:
7644 GameEnds(BlackWins, "Black mates", GE_ENGINE1);
7646 case MachinePlaysWhite:
7647 case IcsPlayingWhite:
7648 GameEnds(WhiteWins, "White mates", GE_ENGINE);
7650 case TwoMachinesPlay:
7651 if (cps->twoMachinesColor[0] == 'w')
7652 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
7654 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
7661 } else if (strncmp(message, "checkmate", 9) == 0) {
7662 if (WhiteOnMove(forwardMostMove)) {
7663 GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
7665 GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
7668 } else if (strstr(message, "Draw") != NULL ||
7669 strstr(message, "game is a draw") != NULL) {
7670 GameEnds(GameIsDrawn, "Draw", GE_ENGINE1 + (cps != &first));
7672 } else if (strstr(message, "offer") != NULL &&
7673 strstr(message, "draw") != NULL) {
7675 if (appData.zippyPlay && first.initDone) {
7676 /* Relay offer to ICS */
7677 SendToICS(ics_prefix);
7678 SendToICS("draw\n");
7681 cps->offeredDraw = 2; /* valid until this engine moves twice */
7682 if (gameMode == TwoMachinesPlay) {
7683 if (cps->other->offeredDraw) {
7684 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
7685 /* [HGM] in two-machine mode we delay relaying draw offer */
7686 /* until after we also have move, to see if it is really claim */
7688 } else if (gameMode == MachinePlaysWhite ||
7689 gameMode == MachinePlaysBlack) {
7690 if (userOfferedDraw) {
7691 DisplayInformation(_("Machine accepts your draw offer"));
7692 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
7694 DisplayInformation(_("Machine offers a draw\nSelect Action / Draw to agree"));
7701 * Look for thinking output
7703 if ( appData.showThinking // [HGM] thinking: test all options that cause this output
7704 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp()
7706 int plylev, mvleft, mvtot, curscore, time;
7707 char mvname[MOVE_LEN];
7711 int prefixHint = FALSE;
7712 mvname[0] = NULLCHAR;
7715 case MachinePlaysBlack:
7716 case IcsPlayingBlack:
7717 if (WhiteOnMove(forwardMostMove)) prefixHint = TRUE;
7719 case MachinePlaysWhite:
7720 case IcsPlayingWhite:
7721 if (!WhiteOnMove(forwardMostMove)) prefixHint = TRUE;
7726 case IcsObserving: /* [DM] icsEngineAnalyze */
7727 if (!appData.icsEngineAnalyze) ignore = TRUE;
7729 case TwoMachinesPlay:
7730 if ((cps->twoMachinesColor[0] == 'w') != WhiteOnMove(forwardMostMove)) {
7741 if (sscanf(message, "%d%c %d %d " u64Display " %[^\n]\n",
7742 &plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5) {
7744 if (plyext != ' ' && plyext != '\t') {
7748 /* [AS] Negate score if machine is playing black and reporting absolute scores */
7749 if( cps->scoreIsAbsolute &&
7750 ( gameMode == MachinePlaysBlack ||
7751 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b' ||
7752 gameMode == IcsPlayingBlack || // [HGM] also add other situations where engine should report black POV
7753 (gameMode == AnalyzeMode || gameMode == AnalyzeFile || gameMode == IcsObserving && appData.icsEngineAnalyze) &&
7754 !WhiteOnMove(currentMove)
7757 curscore = -curscore;
7761 programStats.depth = plylev;
7762 programStats.nodes = nodes;
7763 programStats.time = time;
7764 programStats.score = curscore;
7765 programStats.got_only_move = 0;
7767 if(cps->nps >= 0) { /* [HGM] nps: use engine nodes or time to decrement clock */
7770 if(cps->nps == 0) ticklen = 10*time; // use engine reported time
7771 else ticklen = (1000. * u64ToDouble(nodes)) / cps->nps; // convert node count to time
7772 if(WhiteOnMove(forwardMostMove) && (gameMode == MachinePlaysWhite ||
7773 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'w'))
7774 whiteTimeRemaining = timeRemaining[0][forwardMostMove] - ticklen;
7775 if(!WhiteOnMove(forwardMostMove) && (gameMode == MachinePlaysBlack ||
7776 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b'))
7777 blackTimeRemaining = timeRemaining[1][forwardMostMove] - ticklen;
7780 /* Buffer overflow protection */
7781 if (buf1[0] != NULLCHAR) {
7782 if (strlen(buf1) >= sizeof(programStats.movelist)
7783 && appData.debugMode) {
7785 "PV is too long; using the first %u bytes.\n",
7786 (unsigned) sizeof(programStats.movelist) - 1);
7789 safeStrCpy( programStats.movelist, buf1, sizeof(programStats.movelist) );
7791 sprintf(programStats.movelist, " no PV\n");
7794 if (programStats.seen_stat) {
7795 programStats.ok_to_send = 1;
7798 if (strchr(programStats.movelist, '(') != NULL) {
7799 programStats.line_is_book = 1;
7800 programStats.nr_moves = 0;
7801 programStats.moves_left = 0;
7803 programStats.line_is_book = 0;
7806 SendProgramStatsToFrontend( cps, &programStats );
7809 [AS] Protect the thinkOutput buffer from overflow... this
7810 is only useful if buf1 hasn't overflowed first!
7812 sprintf(thinkOutput, "[%d]%c%+.2f %s%s",
7814 (gameMode == TwoMachinesPlay ?
7815 ToUpper(cps->twoMachinesColor[0]) : ' '),
7816 ((double) curscore) / 100.0,
7817 prefixHint ? lastHint : "",
7818 prefixHint ? " " : "" );
7820 if( buf1[0] != NULLCHAR ) {
7821 unsigned max_len = sizeof(thinkOutput) - strlen(thinkOutput) - 1;
7823 if( strlen(buf1) > max_len ) {
7824 if( appData.debugMode) {
7825 fprintf(debugFP,"PV is too long for thinkOutput, truncating.\n");
7827 buf1[max_len+1] = '\0';
7830 strcat( thinkOutput, buf1 );
7833 if (currentMove == forwardMostMove || gameMode == AnalyzeMode
7834 || gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
7835 DisplayMove(currentMove - 1);
7839 } else if ((p=StrStr(message, "(only move)")) != NULL) {
7840 /* crafty (9.25+) says "(only move) <move>"
7841 * if there is only 1 legal move
7843 sscanf(p, "(only move) %s", buf1);
7844 sprintf(thinkOutput, "%s (only move)", buf1);
7845 sprintf(programStats.movelist, "%s (only move)", buf1);
7846 programStats.depth = 1;
7847 programStats.nr_moves = 1;
7848 programStats.moves_left = 1;
7849 programStats.nodes = 1;
7850 programStats.time = 1;
7851 programStats.got_only_move = 1;
7853 /* Not really, but we also use this member to
7854 mean "line isn't going to change" (Crafty
7855 isn't searching, so stats won't change) */
7856 programStats.line_is_book = 1;
7858 SendProgramStatsToFrontend( cps, &programStats );
7860 if (currentMove == forwardMostMove || gameMode==AnalyzeMode ||
7861 gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
7862 DisplayMove(currentMove - 1);
7865 } else if (sscanf(message,"stat01: %d " u64Display " %d %d %d %s",
7866 &time, &nodes, &plylev, &mvleft,
7867 &mvtot, mvname) >= 5) {
7868 /* The stat01: line is from Crafty (9.29+) in response
7869 to the "." command */
7870 programStats.seen_stat = 1;
7871 cps->maybeThinking = TRUE;
7873 if (programStats.got_only_move || !appData.periodicUpdates)
7876 programStats.depth = plylev;
7877 programStats.time = time;
7878 programStats.nodes = nodes;
7879 programStats.moves_left = mvleft;
7880 programStats.nr_moves = mvtot;
7881 strcpy(programStats.move_name, mvname);
7882 programStats.ok_to_send = 1;
7883 programStats.movelist[0] = '\0';
7885 SendProgramStatsToFrontend( cps, &programStats );
7889 } else if (strncmp(message,"++",2) == 0) {
7890 /* Crafty 9.29+ outputs this */
7891 programStats.got_fail = 2;
7894 } else if (strncmp(message,"--",2) == 0) {
7895 /* Crafty 9.29+ outputs this */
7896 programStats.got_fail = 1;
7899 } else if (thinkOutput[0] != NULLCHAR &&
7900 strncmp(message, " ", 4) == 0) {
7901 unsigned message_len;
7904 while (*p && *p == ' ') p++;
7906 message_len = strlen( p );
7908 /* [AS] Avoid buffer overflow */
7909 if( sizeof(thinkOutput) - strlen(thinkOutput) - 1 > message_len ) {
7910 strcat(thinkOutput, " ");
7911 strcat(thinkOutput, p);
7914 if( sizeof(programStats.movelist) - strlen(programStats.movelist) - 1 > message_len ) {
7915 strcat(programStats.movelist, " ");
7916 strcat(programStats.movelist, p);
7919 if (currentMove == forwardMostMove || gameMode==AnalyzeMode ||
7920 gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
7921 DisplayMove(currentMove - 1);
7929 if (sscanf(message, "%d%c %d %d " u64Display " %[^\n]\n",
7930 &plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5)
7932 ChessProgramStats cpstats;
7934 if (plyext != ' ' && plyext != '\t') {
7938 /* [AS] Negate score if machine is playing black and reporting absolute scores */
7939 if( cps->scoreIsAbsolute && ((gameMode == MachinePlaysBlack) || (gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b')) ) {
7940 curscore = -curscore;
7943 cpstats.depth = plylev;
7944 cpstats.nodes = nodes;
7945 cpstats.time = time;
7946 cpstats.score = curscore;
7947 cpstats.got_only_move = 0;
7948 cpstats.movelist[0] = '\0';
7950 if (buf1[0] != NULLCHAR) {
7951 safeStrCpy( cpstats.movelist, buf1, sizeof(cpstats.movelist) );
7954 cpstats.ok_to_send = 0;
7955 cpstats.line_is_book = 0;
7956 cpstats.nr_moves = 0;
7957 cpstats.moves_left = 0;
7959 SendProgramStatsToFrontend( cps, &cpstats );
7966 /* Parse a game score from the character string "game", and
7967 record it as the history of the current game. The game
7968 score is NOT assumed to start from the standard position.
7969 The display is not updated in any way.
7972 ParseGameHistory(game)
7976 int fromX, fromY, toX, toY, boardIndex;
7981 if (appData.debugMode)
7982 fprintf(debugFP, "Parsing game history: %s\n", game);
7984 if (gameInfo.event == NULL) gameInfo.event = StrSave("ICS game");
7985 gameInfo.site = StrSave(appData.icsHost);
7986 gameInfo.date = PGNDate();
7987 gameInfo.round = StrSave("-");
7989 /* Parse out names of players */
7990 while (*game == ' ') game++;
7992 while (*game != ' ') *p++ = *game++;
7994 gameInfo.white = StrSave(buf);
7995 while (*game == ' ') game++;
7997 while (*game != ' ' && *game != '\n') *p++ = *game++;
7999 gameInfo.black = StrSave(buf);
8002 boardIndex = blackPlaysFirst ? 1 : 0;
8005 yyboardindex = boardIndex;
8006 moveType = (ChessMove) yylex();
8008 case IllegalMove: /* maybe suicide chess, etc. */
8009 if (appData.debugMode) {
8010 fprintf(debugFP, "Illegal move from ICS: '%s'\n", yy_text);
8011 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
8012 setbuf(debugFP, NULL);
8014 case WhitePromotionChancellor:
8015 case BlackPromotionChancellor:
8016 case WhitePromotionArchbishop:
8017 case BlackPromotionArchbishop:
8018 case WhitePromotionQueen:
8019 case BlackPromotionQueen:
8020 case WhitePromotionRook:
8021 case BlackPromotionRook:
8022 case WhitePromotionBishop:
8023 case BlackPromotionBishop:
8024 case WhitePromotionKnight:
8025 case BlackPromotionKnight:
8026 case WhitePromotionKing:
8027 case BlackPromotionKing:
8029 case WhiteCapturesEnPassant:
8030 case BlackCapturesEnPassant:
8031 case WhiteKingSideCastle:
8032 case WhiteQueenSideCastle:
8033 case BlackKingSideCastle:
8034 case BlackQueenSideCastle:
8035 case WhiteKingSideCastleWild:
8036 case WhiteQueenSideCastleWild:
8037 case BlackKingSideCastleWild:
8038 case BlackQueenSideCastleWild:
8040 case WhiteHSideCastleFR:
8041 case WhiteASideCastleFR:
8042 case BlackHSideCastleFR:
8043 case BlackASideCastleFR:
8045 fromX = currentMoveString[0] - AAA;
8046 fromY = currentMoveString[1] - ONE;
8047 toX = currentMoveString[2] - AAA;
8048 toY = currentMoveString[3] - ONE;
8049 promoChar = currentMoveString[4];
8053 fromX = moveType == WhiteDrop ?
8054 (int) CharToPiece(ToUpper(currentMoveString[0])) :
8055 (int) CharToPiece(ToLower(currentMoveString[0]));
8057 toX = currentMoveString[2] - AAA;
8058 toY = currentMoveString[3] - ONE;
8059 promoChar = NULLCHAR;
8063 sprintf(buf, _("Ambiguous move in ICS output: \"%s\""), yy_text);
8064 if (appData.debugMode) {
8065 fprintf(debugFP, "Ambiguous move from ICS: '%s'\n", yy_text);
8066 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
8067 setbuf(debugFP, NULL);
8069 DisplayError(buf, 0);
8071 case ImpossibleMove:
8073 sprintf(buf, _("Illegal move in ICS output: \"%s\""), yy_text);
8074 if (appData.debugMode) {
8075 fprintf(debugFP, "Impossible move from ICS: '%s'\n", yy_text);
8076 fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
8077 setbuf(debugFP, NULL);
8079 DisplayError(buf, 0);
8081 case (ChessMove) 0: /* end of file */
8082 if (boardIndex < backwardMostMove) {
8083 /* Oops, gap. How did that happen? */
8084 DisplayError(_("Gap in move list"), 0);
8087 backwardMostMove = blackPlaysFirst ? 1 : 0;
8088 if (boardIndex > forwardMostMove) {
8089 forwardMostMove = boardIndex;
8093 if (boardIndex > (blackPlaysFirst ? 1 : 0)) {
8094 strcat(parseList[boardIndex-1], " ");
8095 strcat(parseList[boardIndex-1], yy_text);
8107 case GameUnfinished:
8108 if (gameMode == IcsExamining) {
8109 if (boardIndex < backwardMostMove) {
8110 /* Oops, gap. How did that happen? */
8113 backwardMostMove = blackPlaysFirst ? 1 : 0;
8116 gameInfo.result = moveType;
8117 p = strchr(yy_text, '{');
8118 if (p == NULL) p = strchr(yy_text, '(');
8121 if (p[0] == '0' || p[0] == '1' || p[0] == '*') p = "";
8123 q = strchr(p, *p == '{' ? '}' : ')');
8124 if (q != NULL) *q = NULLCHAR;
8127 gameInfo.resultDetails = StrSave(p);
8130 if (boardIndex >= forwardMostMove &&
8131 !(gameMode == IcsObserving && ics_gamenum == -1)) {
8132 backwardMostMove = blackPlaysFirst ? 1 : 0;
8135 (void) CoordsToAlgebraic(boards[boardIndex], PosFlags(boardIndex),
8136 fromY, fromX, toY, toX, promoChar,
8137 parseList[boardIndex]);
8138 CopyBoard(boards[boardIndex + 1], boards[boardIndex]);
8139 /* currentMoveString is set as a side-effect of yylex */
8140 strcpy(moveList[boardIndex], currentMoveString);
8141 strcat(moveList[boardIndex], "\n");
8143 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[boardIndex]);
8144 switch (MateTest(boards[boardIndex], PosFlags(boardIndex)) ) {
8150 if(gameInfo.variant != VariantShogi)
8151 strcat(parseList[boardIndex - 1], "+");
8155 strcat(parseList[boardIndex - 1], "#");
8162 /* Apply a move to the given board */
8164 ApplyMove(fromX, fromY, toX, toY, promoChar, board)
8165 int fromX, fromY, toX, toY;
8169 ChessSquare captured = board[toY][toX], piece, king; int p, oldEP = EP_NONE, berolina = 0;
8170 int promoRank = gameInfo.variant == VariantMakruk ? 3 : 1;
8172 /* [HGM] compute & store e.p. status and castling rights for new position */
8173 /* we can always do that 'in place', now pointers to these rights are passed to ApplyMove */
8176 if(gameInfo.variant == VariantBerolina) berolina = EP_BEROLIN_A;
8177 oldEP = (signed char)board[EP_STATUS];
8178 board[EP_STATUS] = EP_NONE;
8180 if( board[toY][toX] != EmptySquare )
8181 board[EP_STATUS] = EP_CAPTURE;
8183 if( board[fromY][fromX] == WhitePawn ) {
8184 if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
8185 board[EP_STATUS] = EP_PAWN_MOVE;
8187 if(toX>BOARD_LEFT && board[toY][toX-1] == BlackPawn &&
8188 gameInfo.variant != VariantBerolina || toX < fromX)
8189 board[EP_STATUS] = toX | berolina;
8190 if(toX<BOARD_RGHT-1 && board[toY][toX+1] == BlackPawn &&
8191 gameInfo.variant != VariantBerolina || toX > fromX)
8192 board[EP_STATUS] = toX;
8195 if( board[fromY][fromX] == BlackPawn ) {
8196 if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
8197 board[EP_STATUS] = EP_PAWN_MOVE;
8198 if( toY-fromY== -2) {
8199 if(toX>BOARD_LEFT && board[toY][toX-1] == WhitePawn &&
8200 gameInfo.variant != VariantBerolina || toX < fromX)
8201 board[EP_STATUS] = toX | berolina;
8202 if(toX<BOARD_RGHT-1 && board[toY][toX+1] == WhitePawn &&
8203 gameInfo.variant != VariantBerolina || toX > fromX)
8204 board[EP_STATUS] = toX;
8208 for(i=0; i<nrCastlingRights; i++) {
8209 if(board[CASTLING][i] == fromX && castlingRank[i] == fromY ||
8210 board[CASTLING][i] == toX && castlingRank[i] == toY
8211 ) board[CASTLING][i] = NoRights; // revoke for moved or captured piece
8216 /* [HGM] In Shatranj and Courier all promotions are to Ferz */
8217 if((gameInfo.variant==VariantShatranj || gameInfo.variant==VariantCourier || gameInfo.variant == VariantMakruk)
8218 && promoChar != 0) promoChar = PieceToChar(WhiteFerz);
8220 if (fromX == toX && fromY == toY) return;
8222 if (fromY == DROP_RANK) {
8224 piece = board[toY][toX] = (ChessSquare) fromX;
8226 piece = board[fromY][fromX]; /* [HGM] remember, for Shogi promotion */
8227 king = piece < (int) BlackPawn ? WhiteKing : BlackKing; /* [HGM] Knightmate simplify testing for castling */
8228 if(gameInfo.variant == VariantKnightmate)
8229 king += (int) WhiteUnicorn - (int) WhiteKing;
8231 /* Code added by Tord: */
8232 /* FRC castling assumed when king captures friendly rook. */
8233 if (board[fromY][fromX] == WhiteKing &&
8234 board[toY][toX] == WhiteRook) {
8235 board[fromY][fromX] = EmptySquare;
8236 board[toY][toX] = EmptySquare;
8238 board[0][BOARD_RGHT-2] = WhiteKing; board[0][BOARD_RGHT-3] = WhiteRook;
8240 board[0][BOARD_LEFT+2] = WhiteKing; board[0][BOARD_LEFT+3] = WhiteRook;
8242 } else if (board[fromY][fromX] == BlackKing &&
8243 board[toY][toX] == BlackRook) {
8244 board[fromY][fromX] = EmptySquare;
8245 board[toY][toX] = EmptySquare;
8247 board[BOARD_HEIGHT-1][BOARD_RGHT-2] = BlackKing; board[BOARD_HEIGHT-1][BOARD_RGHT-3] = BlackRook;
8249 board[BOARD_HEIGHT-1][BOARD_LEFT+2] = BlackKing; board[BOARD_HEIGHT-1][BOARD_LEFT+3] = BlackRook;
8251 /* End of code added by Tord */
8253 } else if (board[fromY][fromX] == king
8254 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
8255 && toY == fromY && toX > fromX+1) {
8256 board[fromY][fromX] = EmptySquare;
8257 board[toY][toX] = king;
8258 board[toY][toX-1] = board[fromY][BOARD_RGHT-1];
8259 board[fromY][BOARD_RGHT-1] = EmptySquare;
8260 } else if (board[fromY][fromX] == king
8261 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
8262 && toY == fromY && toX < fromX-1) {
8263 board[fromY][fromX] = EmptySquare;
8264 board[toY][toX] = king;
8265 board[toY][toX+1] = board[fromY][BOARD_LEFT];
8266 board[fromY][BOARD_LEFT] = EmptySquare;
8267 } else if (board[fromY][fromX] == WhitePawn
8268 && toY >= BOARD_HEIGHT-promoRank
8269 && gameInfo.variant != VariantXiangqi
8271 /* white pawn promotion */
8272 board[toY][toX] = CharToPiece(ToUpper(promoChar));
8273 if (board[toY][toX] == EmptySquare) {
8274 board[toY][toX] = WhiteQueen;
8276 if(gameInfo.variant==VariantBughouse ||
8277 gameInfo.variant==VariantCrazyhouse) /* [HGM] use shadow piece */
8278 board[toY][toX] = (ChessSquare) (PROMOTED board[toY][toX]);
8279 board[fromY][fromX] = EmptySquare;
8280 } else if ((fromY == BOARD_HEIGHT-4)
8282 && gameInfo.variant != VariantXiangqi
8283 && gameInfo.variant != VariantBerolina
8284 && (board[fromY][fromX] == WhitePawn)
8285 && (board[toY][toX] == EmptySquare)) {
8286 board[fromY][fromX] = EmptySquare;
8287 board[toY][toX] = WhitePawn;
8288 captured = board[toY - 1][toX];
8289 board[toY - 1][toX] = EmptySquare;
8290 } else if ((fromY == BOARD_HEIGHT-4)
8292 && gameInfo.variant == VariantBerolina
8293 && (board[fromY][fromX] == WhitePawn)
8294 && (board[toY][toX] == EmptySquare)) {
8295 board[fromY][fromX] = EmptySquare;
8296 board[toY][toX] = WhitePawn;
8297 if(oldEP & EP_BEROLIN_A) {
8298 captured = board[fromY][fromX-1];
8299 board[fromY][fromX-1] = EmptySquare;
8300 }else{ captured = board[fromY][fromX+1];
8301 board[fromY][fromX+1] = EmptySquare;
8303 } else if (board[fromY][fromX] == king
8304 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
8305 && toY == fromY && toX > fromX+1) {
8306 board[fromY][fromX] = EmptySquare;
8307 board[toY][toX] = king;
8308 board[toY][toX-1] = board[fromY][BOARD_RGHT-1];
8309 board[fromY][BOARD_RGHT-1] = EmptySquare;
8310 } else if (board[fromY][fromX] == king
8311 && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
8312 && toY == fromY && toX < fromX-1) {
8313 board[fromY][fromX] = EmptySquare;
8314 board[toY][toX] = king;
8315 board[toY][toX+1] = board[fromY][BOARD_LEFT];
8316 board[fromY][BOARD_LEFT] = EmptySquare;
8317 } else if (fromY == 7 && fromX == 3
8318 && board[fromY][fromX] == BlackKing
8319 && toY == 7 && toX == 5) {
8320 board[fromY][fromX] = EmptySquare;
8321 board[toY][toX] = BlackKing;
8322 board[fromY][7] = EmptySquare;
8323 board[toY][4] = BlackRook;
8324 } else if (fromY == 7 && fromX == 3
8325 && board[fromY][fromX] == BlackKing
8326 && toY == 7 && toX == 1) {
8327 board[fromY][fromX] = EmptySquare;
8328 board[toY][toX] = BlackKing;
8329 board[fromY][0] = EmptySquare;
8330 board[toY][2] = BlackRook;
8331 } else if (board[fromY][fromX] == BlackPawn
8333 && gameInfo.variant != VariantXiangqi
8335 /* black pawn promotion */
8336 board[toY][toX] = CharToPiece(ToLower(promoChar));
8337 if (board[toY][toX] == EmptySquare) {
8338 board[toY][toX] = BlackQueen;
8340 if(gameInfo.variant==VariantBughouse ||
8341 gameInfo.variant==VariantCrazyhouse) /* [HGM] use shadow piece */
8342 board[toY][toX] = (ChessSquare) (PROMOTED board[toY][toX]);
8343 board[fromY][fromX] = EmptySquare;
8344 } else if ((fromY == 3)
8346 && gameInfo.variant != VariantXiangqi
8347 && gameInfo.variant != VariantBerolina
8348 && (board[fromY][fromX] == BlackPawn)
8349 && (board[toY][toX] == EmptySquare)) {
8350 board[fromY][fromX] = EmptySquare;
8351 board[toY][toX] = BlackPawn;
8352 captured = board[toY + 1][toX];
8353 board[toY + 1][toX] = EmptySquare;
8354 } else if ((fromY == 3)
8356 && gameInfo.variant == VariantBerolina
8357 && (board[fromY][fromX] == BlackPawn)
8358 && (board[toY][toX] == EmptySquare)) {
8359 board[fromY][fromX] = EmptySquare;
8360 board[toY][toX] = BlackPawn;
8361 if(oldEP & EP_BEROLIN_A) {
8362 captured = board[fromY][fromX-1];
8363 board[fromY][fromX-1] = EmptySquare;
8364 }else{ captured = board[fromY][fromX+1];
8365 board[fromY][fromX+1] = EmptySquare;
8368 board[toY][toX] = board[fromY][fromX];
8369 board[fromY][fromX] = EmptySquare;
8372 /* [HGM] now we promote for Shogi, if needed */
8373 if(gameInfo.variant == VariantShogi && promoChar == 'q')
8374 board[toY][toX] = (ChessSquare) (PROMOTED piece);
8377 if (gameInfo.holdingsWidth != 0) {
8379 /* !!A lot more code needs to be written to support holdings */
8380 /* [HGM] OK, so I have written it. Holdings are stored in the */
8381 /* penultimate board files, so they are automaticlly stored */
8382 /* in the game history. */
8383 if (fromY == DROP_RANK) {
8384 /* Delete from holdings, by decreasing count */
8385 /* and erasing image if necessary */
8387 if(p < (int) BlackPawn) { /* white drop */
8388 p -= (int)WhitePawn;
8389 p = PieceToNumber((ChessSquare)p);
8390 if(p >= gameInfo.holdingsSize) p = 0;
8391 if(--board[p][BOARD_WIDTH-2] <= 0)
8392 board[p][BOARD_WIDTH-1] = EmptySquare;
8393 if((int)board[p][BOARD_WIDTH-2] < 0)
8394 board[p][BOARD_WIDTH-2] = 0;
8395 } else { /* black drop */
8396 p -= (int)BlackPawn;
8397 p = PieceToNumber((ChessSquare)p);
8398 if(p >= gameInfo.holdingsSize) p = 0;
8399 if(--board[BOARD_HEIGHT-1-p][1] <= 0)
8400 board[BOARD_HEIGHT-1-p][0] = EmptySquare;
8401 if((int)board[BOARD_HEIGHT-1-p][1] < 0)
8402 board[BOARD_HEIGHT-1-p][1] = 0;
8405 if (captured != EmptySquare && gameInfo.holdingsSize > 0
8406 && gameInfo.variant != VariantBughouse ) {
8407 /* [HGM] holdings: Add to holdings, if holdings exist */
8408 if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat) {
8409 // [HGM] superchess: suppress flipping color of captured pieces by reverse pre-flip
8410 captured = (int) captured >= (int) BlackPawn ? BLACK_TO_WHITE captured : WHITE_TO_BLACK captured;
8413 if (p >= (int) BlackPawn) {
8414 p -= (int)BlackPawn;
8415 if(gameInfo.variant == VariantShogi && DEMOTED p >= 0) {
8416 /* in Shogi restore piece to its original first */
8417 captured = (ChessSquare) (DEMOTED captured);
8420 p = PieceToNumber((ChessSquare)p);
8421 if(p >= gameInfo.holdingsSize) { p = 0; captured = BlackPawn; }
8422 board[p][BOARD_WIDTH-2]++;
8423 board[p][BOARD_WIDTH-1] = BLACK_TO_WHITE captured;
8425 p -= (int)WhitePawn;
8426 if(gameInfo.variant == VariantShogi && DEMOTED p >= 0) {
8427 captured = (ChessSquare) (DEMOTED captured);
8430 p = PieceToNumber((ChessSquare)p);
8431 if(p >= gameInfo.holdingsSize) { p = 0; captured = WhitePawn; }
8432 board[BOARD_HEIGHT-1-p][1]++;
8433 board[BOARD_HEIGHT-1-p][0] = WHITE_TO_BLACK captured;
8436 } else if (gameInfo.variant == VariantAtomic) {
8437 if (captured != EmptySquare) {
8439 for (y = toY-1; y <= toY+1; y++) {
8440 for (x = toX-1; x <= toX+1; x++) {
8441 if (y >= 0 && y < BOARD_HEIGHT && x >= BOARD_LEFT && x < BOARD_RGHT &&
8442 board[y][x] != WhitePawn && board[y][x] != BlackPawn) {
8443 board[y][x] = EmptySquare;
8447 board[toY][toX] = EmptySquare;
8450 if(gameInfo.variant == VariantShogi && promoChar != NULLCHAR && promoChar != '=') {
8451 /* [HGM] Shogi promotions */
8452 board[toY][toX] = (ChessSquare) (PROMOTED piece);
8455 if((gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat)
8456 && promoChar != NULLCHAR && gameInfo.holdingsSize) {
8457 // [HGM] superchess: take promotion piece out of holdings
8458 int k = PieceToNumber(CharToPiece(ToUpper(promoChar)));
8459 if((int)piece < (int)BlackPawn) { // determine stm from piece color
8460 if(!--board[k][BOARD_WIDTH-2])
8461 board[k][BOARD_WIDTH-1] = EmptySquare;
8463 if(!--board[BOARD_HEIGHT-1-k][1])
8464 board[BOARD_HEIGHT-1-k][0] = EmptySquare;
8470 /* Updates forwardMostMove */
8472 MakeMove(fromX, fromY, toX, toY, promoChar)
8473 int fromX, fromY, toX, toY;
8476 // forwardMostMove++; // [HGM] bare: moved downstream
8478 if(serverMoves != NULL) { /* [HGM] write moves on file for broadcasting (should be separate routine, really) */
8479 int timeLeft; static int lastLoadFlag=0; int king, piece;
8480 piece = boards[forwardMostMove][fromY][fromX];
8481 king = piece < (int) BlackPawn ? WhiteKing : BlackKing;
8482 if(gameInfo.variant == VariantKnightmate)
8483 king += (int) WhiteUnicorn - (int) WhiteKing;
8484 if(forwardMostMove == 0) {
8486 fprintf(serverMoves, "%s;", second.tidy);
8487 fprintf(serverMoves, "%s;", first.tidy);
8488 if(!blackPlaysFirst)
8489 fprintf(serverMoves, "%s;", second.tidy);
8490 } else fprintf(serverMoves, loadFlag|lastLoadFlag ? ":" : ";");
8491 lastLoadFlag = loadFlag;
8493 fprintf(serverMoves, "%c%c:%c%c", AAA+fromX, ONE+fromY, AAA+toX, ONE+toY);
8494 // print castling suffix
8495 if( toY == fromY && piece == king ) {
8497 fprintf(serverMoves, ":%c%c:%c%c", AAA+BOARD_RGHT-1, ONE+fromY, AAA+toX-1,ONE+toY);
8499 fprintf(serverMoves, ":%c%c:%c%c", AAA+BOARD_LEFT, ONE+fromY, AAA+toX+1,ONE+toY);
8502 if( (boards[forwardMostMove][fromY][fromX] == WhitePawn ||
8503 boards[forwardMostMove][fromY][fromX] == BlackPawn ) &&
8504 boards[forwardMostMove][toY][toX] == EmptySquare
8506 fprintf(serverMoves, ":%c%c:%c%c", AAA+fromX, ONE+fromY, AAA+toX, ONE+fromY);
8508 if(promoChar != NULLCHAR)
8509 fprintf(serverMoves, ":%c:%c%c", promoChar, AAA+toX, ONE+toY);
8511 fprintf(serverMoves, "/%d/%d",
8512 pvInfoList[forwardMostMove].depth, pvInfoList[forwardMostMove].score);
8513 if(forwardMostMove+1 & 1) timeLeft = whiteTimeRemaining/1000;
8514 else timeLeft = blackTimeRemaining/1000;
8515 fprintf(serverMoves, "/%d", timeLeft);
8517 fflush(serverMoves);
8520 if (forwardMostMove+1 > framePtr) { // [HGM] vari: do not run into saved variations
8521 DisplayFatalError(_("Game too long; increase MAX_MOVES and recompile"),
8525 UnLoadPV(); // [HGM] pv: if we are looking at a PV, abort this
8526 if (commentList[forwardMostMove+1] != NULL) {
8527 free(commentList[forwardMostMove+1]);
8528 commentList[forwardMostMove+1] = NULL;
8530 CopyBoard(boards[forwardMostMove+1], boards[forwardMostMove]);
8531 ApplyMove(fromX, fromY, toX, toY, promoChar, boards[forwardMostMove+1]);
8532 // forwardMostMove++; // [HGM] bare: moved to after ApplyMove, to make sure clock interrupt finds complete board
8533 SwitchClocks(forwardMostMove+1); // [HGM] race: incrementing move nr inside
8534 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
8535 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
8536 gameInfo.result = GameUnfinished;
8537 if (gameInfo.resultDetails != NULL) {
8538 free(gameInfo.resultDetails);
8539 gameInfo.resultDetails = NULL;
8541 CoordsToComputerAlgebraic(fromY, fromX, toY, toX, promoChar,
8542 moveList[forwardMostMove - 1]);
8543 (void) CoordsToAlgebraic(boards[forwardMostMove - 1],
8544 PosFlags(forwardMostMove - 1),
8545 fromY, fromX, toY, toX, promoChar,
8546 parseList[forwardMostMove - 1]);
8547 switch (MateTest(boards[forwardMostMove], PosFlags(forwardMostMove)) ) {
8553 if(gameInfo.variant != VariantShogi)
8554 strcat(parseList[forwardMostMove - 1], "+");
8558 strcat(parseList[forwardMostMove - 1], "#");
8561 if (appData.debugMode) {
8562 fprintf(debugFP, "move: %s, parse: %s (%c)\n", moveList[forwardMostMove-1], parseList[forwardMostMove-1], moveList[forwardMostMove-1][4]);
8567 /* Updates currentMove if not pausing */
8569 ShowMove(fromX, fromY, toX, toY)
8571 int instant = (gameMode == PlayFromGameFile) ?
8572 (matchMode || (appData.timeDelay == 0 && !pausing)) : pausing;
8573 if(appData.noGUI) return;
8574 if (!pausing || gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
8576 if (forwardMostMove == currentMove + 1) {
8577 AnimateMove(boards[forwardMostMove - 1],
8578 fromX, fromY, toX, toY);
8580 if (appData.highlightLastMove) {
8581 SetHighlights(fromX, fromY, toX, toY);
8584 currentMove = forwardMostMove;
8587 if (instant) return;
8589 DisplayMove(currentMove - 1);
8590 DrawPosition(FALSE, boards[currentMove]);
8591 DisplayBothClocks();
8592 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
8595 void SendEgtPath(ChessProgramState *cps)
8596 { /* [HGM] EGT: match formats given in feature with those given by user, and send info for each match */
8597 char buf[MSG_SIZ], name[MSG_SIZ], *p;
8599 if((p = cps->egtFormats) == NULL || appData.egtFormats == NULL) return;
8602 char c, *q = name+1, *r, *s;
8604 name[0] = ','; // extract next format name from feature and copy with prefixed ','
8605 while(*p && *p != ',') *q++ = *p++;
8607 if( appData.defaultPathEGTB && appData.defaultPathEGTB[0] &&
8608 strcmp(name, ",nalimov:") == 0 ) {
8609 // take nalimov path from the menu-changeable option first, if it is defined
8610 sprintf(buf, "egtpath nalimov %s\n", appData.defaultPathEGTB);
8611 SendToProgram(buf,cps); // send egtbpath command for nalimov
8613 if( (s = StrStr(appData.egtFormats, name+1)) == appData.egtFormats ||
8614 (s = StrStr(appData.egtFormats, name)) != NULL) {
8615 // format name occurs amongst user-supplied formats, at beginning or immediately after comma
8616 s = r = StrStr(s, ":") + 1; // beginning of path info
8617 while(*r && *r != ',') r++; // path info is everything upto next ';' or end of string
8618 c = *r; *r = 0; // temporarily null-terminate path info
8619 *--q = 0; // strip of trailig ':' from name
8620 sprintf(buf, "egtpath %s %s\n", name+1, s);
8622 SendToProgram(buf,cps); // send egtbpath command for this format
8624 if(*p == ',') p++; // read away comma to position for next format name
8629 InitChessProgram(cps, setup)
8630 ChessProgramState *cps;
8631 int setup; /* [HGM] needed to setup FRC opening position */
8633 char buf[MSG_SIZ], b[MSG_SIZ]; int overruled;
8634 if (appData.noChessProgram) return;
8635 hintRequested = FALSE;
8636 bookRequested = FALSE;
8638 /* [HGM] some new WB protocol commands to configure engine are sent now, if engine supports them */
8639 /* moved to before sending initstring in 4.3.15, so Polyglot can delay UCI 'isready' to recepton of 'new' */
8640 if(cps->memSize) { /* [HGM] memory */
8641 sprintf(buf, "memory %d\n", appData.defaultHashSize + appData.defaultCacheSizeEGTB);
8642 SendToProgram(buf, cps);
8644 SendEgtPath(cps); /* [HGM] EGT */
8645 if(cps->maxCores) { /* [HGM] SMP: (protocol specified must be last settings command before new!) */
8646 sprintf(buf, "cores %d\n", appData.smpCores);
8647 SendToProgram(buf, cps);
8650 SendToProgram(cps->initString, cps);
8651 if (gameInfo.variant != VariantNormal &&
8652 gameInfo.variant != VariantLoadable
8653 /* [HGM] also send variant if board size non-standard */
8654 || gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0
8656 char *v = VariantName(gameInfo.variant);
8657 if (cps->protocolVersion != 1 && StrStr(cps->variants, v) == NULL) {
8658 /* [HGM] in protocol 1 we have to assume all variants valid */
8659 sprintf(buf, _("Variant %s not supported by %s"), v, cps->tidy);
8660 DisplayFatalError(buf, 0, 1);
8664 /* [HGM] make prefix for non-standard board size. Awkward testing... */
8665 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
8666 if( gameInfo.variant == VariantXiangqi )
8667 overruled = gameInfo.boardWidth != 9 || gameInfo.boardHeight != 10 || gameInfo.holdingsSize != 0;
8668 if( gameInfo.variant == VariantShogi )
8669 overruled = gameInfo.boardWidth != 9 || gameInfo.boardHeight != 9 || gameInfo.holdingsSize != 7;
8670 if( gameInfo.variant == VariantBughouse || gameInfo.variant == VariantCrazyhouse )
8671 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 5;
8672 if( gameInfo.variant == VariantCapablanca || gameInfo.variant == VariantCapaRandom ||
8673 gameInfo.variant == VariantGothic || gameInfo.variant == VariantFalcon )
8674 overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
8675 if( gameInfo.variant == VariantCourier )
8676 overruled = gameInfo.boardWidth != 12 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
8677 if( gameInfo.variant == VariantSuper )
8678 overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 8;
8679 if( gameInfo.variant == VariantGreat )
8680 overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 8;
8683 sprintf(b, "%dx%d+%d_%s", gameInfo.boardWidth, gameInfo.boardHeight,
8684 gameInfo.holdingsSize, VariantName(gameInfo.variant)); // cook up sized variant name
8685 /* [HGM] varsize: try first if this defiant size variant is specifically known */
8686 if(StrStr(cps->variants, b) == NULL) {
8687 // specific sized variant not known, check if general sizing allowed
8688 if (cps->protocolVersion != 1) { // for protocol 1 we cannot check and hope for the best
8689 if(StrStr(cps->variants, "boardsize") == NULL) {
8690 sprintf(buf, "Board size %dx%d+%d not supported by %s",
8691 gameInfo.boardWidth, gameInfo.boardHeight, gameInfo.holdingsSize, cps->tidy);
8692 DisplayFatalError(buf, 0, 1);
8695 /* [HGM] here we really should compare with the maximum supported board size */
8698 } else sprintf(b, "%s", VariantName(gameInfo.variant));
8699 sprintf(buf, "variant %s\n", b);
8700 SendToProgram(buf, cps);
8702 currentlyInitializedVariant = gameInfo.variant;
8704 /* [HGM] send opening position in FRC to first engine */
8706 SendToProgram("force\n", cps);
8708 /* engine is now in force mode! Set flag to wake it up after first move. */
8709 setboardSpoiledMachineBlack = 1;
8713 snprintf(buf, sizeof(buf), "ics %s\n", appData.icsActive ? appData.icsHost : "-");
8714 SendToProgram(buf, cps);
8716 cps->maybeThinking = FALSE;
8717 cps->offeredDraw = 0;
8718 if (!appData.icsActive) {
8719 SendTimeControl(cps, movesPerSession, timeControl,
8720 timeIncrement, appData.searchDepth,
8723 if (appData.showThinking
8724 // [HGM] thinking: four options require thinking output to be sent
8725 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp()
8727 SendToProgram("post\n", cps);
8729 SendToProgram("hard\n", cps);
8730 if (!appData.ponderNextMove) {
8731 /* Warning: "easy" is a toggle in GNU Chess, so don't send
8732 it without being sure what state we are in first. "hard"
8733 is not a toggle, so that one is OK.
8735 SendToProgram("easy\n", cps);
8738 sprintf(buf, "ping %d\n", ++cps->lastPing);
8739 SendToProgram(buf, cps);
8741 cps->initDone = TRUE;
8746 StartChessProgram(cps)
8747 ChessProgramState *cps;
8752 if (appData.noChessProgram) return;
8753 cps->initDone = FALSE;
8755 if (strcmp(cps->host, "localhost") == 0) {
8756 err = StartChildProcess(cps->program, cps->dir, &cps->pr);
8757 } else if (*appData.remoteShell == NULLCHAR) {
8758 err = OpenRcmd(cps->host, appData.remoteUser, cps->program, &cps->pr);
8760 if (*appData.remoteUser == NULLCHAR) {
8761 snprintf(buf, sizeof(buf), "%s %s %s", appData.remoteShell, cps->host,
8764 snprintf(buf, sizeof(buf), "%s %s -l %s %s", appData.remoteShell,
8765 cps->host, appData.remoteUser, cps->program);
8767 err = StartChildProcess(buf, "", &cps->pr);
8771 sprintf(buf, _("Startup failure on '%s'"), cps->program);
8772 DisplayFatalError(buf, err, 1);
8778 cps->isr = AddInputSource(cps->pr, TRUE, ReceiveFromProgram, cps);
8779 if (cps->protocolVersion > 1) {
8780 sprintf(buf, "xboard\nprotover %d\n", cps->protocolVersion);
8781 cps->nrOptions = 0; // [HGM] options: clear all engine-specific options
8782 cps->comboCnt = 0; // and values of combo boxes
8783 SendToProgram(buf, cps);
8785 SendToProgram("xboard\n", cps);
8791 TwoMachinesEventIfReady P((void))
8793 if (first.lastPing != first.lastPong) {
8794 DisplayMessage("", _("Waiting for first chess program"));
8795 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10); // [HGM] fast: lowered from 1000
8798 if (second.lastPing != second.lastPong) {
8799 DisplayMessage("", _("Waiting for second chess program"));
8800 ScheduleDelayedEvent(TwoMachinesEventIfReady, 10); // [HGM] fast: lowered from 1000
8808 NextMatchGame P((void))
8810 int index; /* [HGM] autoinc: step load index during match */
8812 if (*appData.loadGameFile != NULLCHAR) {
8813 index = appData.loadGameIndex;
8814 if(index < 0) { // [HGM] autoinc
8815 lastIndex = index = (index == -2 && first.twoMachinesColor[0] == 'b') ? lastIndex : lastIndex+1;
8816 if(appData.rewindIndex > 0 && index > appData.rewindIndex) lastIndex = index = 1;
8818 LoadGameFromFile(appData.loadGameFile,
8820 appData.loadGameFile, FALSE);
8821 } else if (*appData.loadPositionFile != NULLCHAR) {
8822 index = appData.loadPositionIndex;
8823 if(index < 0) { // [HGM] autoinc
8824 lastIndex = index = (index == -2 && first.twoMachinesColor[0] == 'b') ? lastIndex : lastIndex+1;
8825 if(appData.rewindIndex > 0 && index > appData.rewindIndex) lastIndex = index = 1;
8827 LoadPositionFromFile(appData.loadPositionFile,
8829 appData.loadPositionFile);
8831 TwoMachinesEventIfReady();
8834 void UserAdjudicationEvent( int result )
8836 ChessMove gameResult = GameIsDrawn;
8839 gameResult = WhiteWins;
8841 else if( result < 0 ) {
8842 gameResult = BlackWins;
8845 if( gameMode == TwoMachinesPlay ) {
8846 GameEnds( gameResult, "User adjudication", GE_XBOARD );
8851 // [HGM] save: calculate checksum of game to make games easily identifiable
8852 int StringCheckSum(char *s)
8855 if(s==NULL) return 0;
8856 while(*s) i = i*259 + *s++;
8863 for(i=backwardMostMove; i<forwardMostMove; i++) {
8864 sum += pvInfoList[i].depth;
8865 sum += StringCheckSum(parseList[i]);
8866 sum += StringCheckSum(commentList[i]);
8869 if(i>1 && sum==0) sum++; // make sure never zero for non-empty game
8870 return sum + StringCheckSum(commentList[i]);
8871 } // end of save patch
8874 GameEnds(result, resultDetails, whosays)
8876 char *resultDetails;
8879 GameMode nextGameMode;
8883 if(endingGame) return; /* [HGM] crash: forbid recursion */
8886 if (appData.debugMode) {
8887 fprintf(debugFP, "GameEnds(%d, %s, %d)\n",
8888 result, resultDetails ? resultDetails : "(null)", whosays);
8891 fromX = fromY = -1; // [HGM] abort any move the user is entering.
8893 if (appData.icsActive && (whosays == GE_ENGINE || whosays >= GE_ENGINE1)) {
8894 /* If we are playing on ICS, the server decides when the
8895 game is over, but the engine can offer to draw, claim
8899 if (appData.zippyPlay && first.initDone) {
8900 if (result == GameIsDrawn) {
8901 /* In case draw still needs to be claimed */
8902 SendToICS(ics_prefix);
8903 SendToICS("draw\n");
8904 } else if (StrCaseStr(resultDetails, "resign")) {
8905 SendToICS(ics_prefix);
8906 SendToICS("resign\n");
8910 endingGame = 0; /* [HGM] crash */
8914 /* If we're loading the game from a file, stop */
8915 if (whosays == GE_FILE) {
8916 (void) StopLoadGameTimer();
8920 /* Cancel draw offers */
8921 first.offeredDraw = second.offeredDraw = 0;
8923 /* If this is an ICS game, only ICS can really say it's done;
8924 if not, anyone can. */
8925 isIcsGame = (gameMode == IcsPlayingWhite ||
8926 gameMode == IcsPlayingBlack ||
8927 gameMode == IcsObserving ||
8928 gameMode == IcsExamining);
8930 if (!isIcsGame || whosays == GE_ICS) {
8931 /* OK -- not an ICS game, or ICS said it was done */
8933 if (!isIcsGame && !appData.noChessProgram)
8934 SetUserThinkingEnables();
8936 /* [HGM] if a machine claims the game end we verify this claim */
8937 if(gameMode == TwoMachinesPlay && appData.testClaims) {
8938 if(appData.testLegality && whosays >= GE_ENGINE1 ) {
8940 ChessMove trueResult = (ChessMove) -1;
8942 claimer = whosays == GE_ENGINE1 ? /* color of claimer */
8943 first.twoMachinesColor[0] :
8944 second.twoMachinesColor[0] ;
8946 // [HGM] losers: because the logic is becoming a bit hairy, determine true result first
8947 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_CHECKMATE) {
8948 /* [HGM] verify: engine mate claims accepted if they were flagged */
8949 trueResult = WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins;
8951 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_WINS) { // added code for games where being mated is a win
8952 /* [HGM] verify: engine mate claims accepted if they were flagged */
8953 trueResult = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
8955 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_STALEMATE) { // only used to indicate draws now
8956 trueResult = GameIsDrawn; // default; in variants where stalemate loses, Status is CHECKMATE
8959 // now verify win claims, but not in drop games, as we don't understand those yet
8960 if( (gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper
8961 || gameInfo.variant == VariantGreat) &&
8962 (result == WhiteWins && claimer == 'w' ||
8963 result == BlackWins && claimer == 'b' ) ) { // case to verify: engine claims own win
8964 if (appData.debugMode) {
8965 fprintf(debugFP, "result=%d sp=%d move=%d\n",
8966 result, (signed char)boards[forwardMostMove][EP_STATUS], forwardMostMove);
8968 if(result != trueResult) {
8969 sprintf(buf, "False win claim: '%s'", resultDetails);
8970 result = claimer == 'w' ? BlackWins : WhiteWins;
8971 resultDetails = buf;
8974 if( result == GameIsDrawn && (signed char)boards[forwardMostMove][EP_STATUS] > EP_DRAWS
8975 && (forwardMostMove <= backwardMostMove ||
8976 (signed char)boards[forwardMostMove-1][EP_STATUS] > EP_DRAWS ||
8977 (claimer=='b')==(forwardMostMove&1))
8979 /* [HGM] verify: draws that were not flagged are false claims */
8980 sprintf(buf, "False draw claim: '%s'", resultDetails);
8981 result = claimer == 'w' ? BlackWins : WhiteWins;
8982 resultDetails = buf;
8984 /* (Claiming a loss is accepted no questions asked!) */
8986 /* [HGM] bare: don't allow bare King to win */
8987 if((gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat)
8988 && gameInfo.variant != VariantLosers && gameInfo.variant != VariantGiveaway
8989 && gameInfo.variant != VariantSuicide // [HGM] losers: except in losers, of course...
8990 && result != GameIsDrawn)
8991 { int i, j, k=0, color = (result==WhiteWins ? (int)WhitePawn : (int)BlackPawn);
8992 for(j=BOARD_LEFT; j<BOARD_RGHT; j++) for(i=0; i<BOARD_HEIGHT; i++) {
8993 int p = (signed char)boards[forwardMostMove][i][j] - color;
8994 if(p >= 0 && p <= (int)WhiteKing) k++;
8996 if (appData.debugMode) {
8997 fprintf(debugFP, "GE(%d, %s, %d) bare king k=%d color=%d\n",
8998 result, resultDetails ? resultDetails : "(null)", whosays, k, color);
9001 result = GameIsDrawn;
9002 sprintf(buf, "%s but bare king", resultDetails);
9003 resultDetails = buf;
9009 if(serverMoves != NULL && !loadFlag) { char c = '=';
9010 if(result==WhiteWins) c = '+';
9011 if(result==BlackWins) c = '-';
9012 if(resultDetails != NULL)
9013 fprintf(serverMoves, ";%c;%s\n", c, resultDetails);
9015 if (resultDetails != NULL) {
9016 gameInfo.result = result;
9017 gameInfo.resultDetails = StrSave(resultDetails);
9019 /* display last move only if game was not loaded from file */
9020 if ((whosays != GE_FILE) && (currentMove == forwardMostMove))
9021 DisplayMove(currentMove - 1);
9023 if (forwardMostMove != 0) {
9024 if (gameMode != PlayFromGameFile && gameMode != EditGame
9025 && lastSavedGame != GameCheckSum() // [HGM] save: suppress duplicates
9027 if (*appData.saveGameFile != NULLCHAR) {
9028 SaveGameToFile(appData.saveGameFile, TRUE);
9029 } else if (appData.autoSaveGames) {
9032 if (*appData.savePositionFile != NULLCHAR) {
9033 SavePositionToFile(appData.savePositionFile);
9038 /* Tell program how game ended in case it is learning */
9039 /* [HGM] Moved this to after saving the PGN, just in case */
9040 /* engine died and we got here through time loss. In that */
9041 /* case we will get a fatal error writing the pipe, which */
9042 /* would otherwise lose us the PGN. */
9043 /* [HGM] crash: not needed anymore, but doesn't hurt; */
9044 /* output during GameEnds should never be fatal anymore */
9045 if (gameMode == MachinePlaysWhite ||
9046 gameMode == MachinePlaysBlack ||
9047 gameMode == TwoMachinesPlay ||
9048 gameMode == IcsPlayingWhite ||
9049 gameMode == IcsPlayingBlack ||
9050 gameMode == BeginningOfGame) {
9052 sprintf(buf, "result %s {%s}\n", PGNResult(result),
9054 if (first.pr != NoProc) {
9055 SendToProgram(buf, &first);
9057 if (second.pr != NoProc &&
9058 gameMode == TwoMachinesPlay) {
9059 SendToProgram(buf, &second);
9064 if (appData.icsActive) {
9065 if (appData.quietPlay &&
9066 (gameMode == IcsPlayingWhite ||
9067 gameMode == IcsPlayingBlack)) {
9068 SendToICS(ics_prefix);
9069 SendToICS("set shout 1\n");
9071 nextGameMode = IcsIdle;
9072 ics_user_moved = FALSE;
9073 /* clean up premove. It's ugly when the game has ended and the
9074 * premove highlights are still on the board.
9078 ClearPremoveHighlights();
9079 DrawPosition(FALSE, boards[currentMove]);
9081 if (whosays == GE_ICS) {
9084 if (gameMode == IcsPlayingWhite)
9086 else if(gameMode == IcsPlayingBlack)
9090 if (gameMode == IcsPlayingBlack)
9092 else if(gameMode == IcsPlayingWhite)
9099 PlayIcsUnfinishedSound();
9102 } else if (gameMode == EditGame ||
9103 gameMode == PlayFromGameFile ||
9104 gameMode == AnalyzeMode ||
9105 gameMode == AnalyzeFile) {
9106 nextGameMode = gameMode;
9108 nextGameMode = EndOfGame;
9113 nextGameMode = gameMode;
9116 if (appData.noChessProgram) {
9117 gameMode = nextGameMode;
9119 endingGame = 0; /* [HGM] crash */
9124 /* Put first chess program into idle state */
9125 if (first.pr != NoProc &&
9126 (gameMode == MachinePlaysWhite ||
9127 gameMode == MachinePlaysBlack ||
9128 gameMode == TwoMachinesPlay ||
9129 gameMode == IcsPlayingWhite ||
9130 gameMode == IcsPlayingBlack ||
9131 gameMode == BeginningOfGame)) {
9132 SendToProgram("force\n", &first);
9133 if (first.usePing) {
9135 sprintf(buf, "ping %d\n", ++first.lastPing);
9136 SendToProgram(buf, &first);
9139 } else if (result != GameUnfinished || nextGameMode == IcsIdle) {
9140 /* Kill off first chess program */
9141 if (first.isr != NULL)
9142 RemoveInputSource(first.isr);
9145 if (first.pr != NoProc) {
9147 DoSleep( appData.delayBeforeQuit );
9148 SendToProgram("quit\n", &first);
9149 DoSleep( appData.delayAfterQuit );
9150 DestroyChildProcess(first.pr, first.useSigterm);
9155 /* Put second chess program into idle state */
9156 if (second.pr != NoProc &&
9157 gameMode == TwoMachinesPlay) {
9158 SendToProgram("force\n", &second);
9159 if (second.usePing) {
9161 sprintf(buf, "ping %d\n", ++second.lastPing);
9162 SendToProgram(buf, &second);
9165 } else if (result != GameUnfinished || nextGameMode == IcsIdle) {
9166 /* Kill off second chess program */
9167 if (second.isr != NULL)
9168 RemoveInputSource(second.isr);
9171 if (second.pr != NoProc) {
9172 DoSleep( appData.delayBeforeQuit );
9173 SendToProgram("quit\n", &second);
9174 DoSleep( appData.delayAfterQuit );
9175 DestroyChildProcess(second.pr, second.useSigterm);
9180 if (matchMode && gameMode == TwoMachinesPlay) {
9183 if (first.twoMachinesColor[0] == 'w') {
9190 if (first.twoMachinesColor[0] == 'b') {
9199 if (matchGame < appData.matchGames) {
9201 if(appData.sameColorGames <= 1) { /* [HGM] alternate: suppress color swap */
9202 tmp = first.twoMachinesColor;
9203 first.twoMachinesColor = second.twoMachinesColor;
9204 second.twoMachinesColor = tmp;
9206 gameMode = nextGameMode;
9208 if(appData.matchPause>10000 || appData.matchPause<10)
9209 appData.matchPause = 10000; /* [HGM] make pause adjustable */
9210 ScheduleDelayedEvent(NextMatchGame, appData.matchPause);
9211 endingGame = 0; /* [HGM] crash */
9215 gameMode = nextGameMode;
9216 sprintf(buf, _("Match %s vs. %s: final score %d-%d-%d"),
9217 first.tidy, second.tidy,
9218 first.matchWins, second.matchWins,
9219 appData.matchGames - (first.matchWins + second.matchWins));
9220 DisplayFatalError(buf, 0, 0);
9223 if ((gameMode == AnalyzeMode || gameMode == AnalyzeFile) &&
9224 !(nextGameMode == AnalyzeMode || nextGameMode == AnalyzeFile))
9226 gameMode = nextGameMode;
9228 endingGame = 0; /* [HGM] crash */
9231 /* Assumes program was just initialized (initString sent).
9232 Leaves program in force mode. */
9234 FeedMovesToProgram(cps, upto)
9235 ChessProgramState *cps;
9240 if (appData.debugMode)
9241 fprintf(debugFP, "Feeding %smoves %d through %d to %s chess program\n",
9242 startedFromSetupPosition ? "position and " : "",
9243 backwardMostMove, upto, cps->which);
9244 if(currentlyInitializedVariant != gameInfo.variant) { char buf[MSG_SIZ];
9245 // [HGM] variantswitch: make engine aware of new variant
9246 if(cps->protocolVersion > 1 && StrStr(cps->variants, VariantName(gameInfo.variant)) == NULL)
9247 return; // [HGM] refrain from feeding moves altogether if variant is unsupported!
9248 sprintf(buf, "variant %s\n", VariantName(gameInfo.variant));
9249 SendToProgram(buf, cps);
9250 currentlyInitializedVariant = gameInfo.variant;
9252 SendToProgram("force\n", cps);
9253 if (startedFromSetupPosition) {
9254 SendBoard(cps, backwardMostMove);
9255 if (appData.debugMode) {
9256 fprintf(debugFP, "feedMoves\n");
9259 for (i = backwardMostMove; i < upto; i++) {
9260 SendMoveToProgram(i, cps);
9266 ResurrectChessProgram()
9268 /* The chess program may have exited.
9269 If so, restart it and feed it all the moves made so far. */
9271 if (appData.noChessProgram || first.pr != NoProc) return;
9273 StartChessProgram(&first);
9274 InitChessProgram(&first, FALSE);
9275 FeedMovesToProgram(&first, currentMove);
9277 if (!first.sendTime) {
9278 /* can't tell gnuchess what its clock should read,
9279 so we bow to its notion. */
9281 timeRemaining[0][currentMove] = whiteTimeRemaining;
9282 timeRemaining[1][currentMove] = blackTimeRemaining;
9285 if ((gameMode == AnalyzeMode || gameMode == AnalyzeFile ||
9286 appData.icsEngineAnalyze) && first.analysisSupport) {
9287 SendToProgram("analyze\n", &first);
9288 first.analyzing = TRUE;
9301 if (appData.debugMode) {
9302 fprintf(debugFP, "Reset(%d, %d) from gameMode %d\n",
9303 redraw, init, gameMode);
9305 CleanupTail(); // [HGM] vari: delete any stored variations
9306 pausing = pauseExamInvalid = FALSE;
9307 startedFromSetupPosition = blackPlaysFirst = FALSE;
9309 whiteFlag = blackFlag = FALSE;
9310 userOfferedDraw = FALSE;
9311 hintRequested = bookRequested = FALSE;
9312 first.maybeThinking = FALSE;
9313 second.maybeThinking = FALSE;
9314 first.bookSuspend = FALSE; // [HGM] book
9315 second.bookSuspend = FALSE;
9316 thinkOutput[0] = NULLCHAR;
9317 lastHint[0] = NULLCHAR;
9318 ClearGameInfo(&gameInfo);
9319 gameInfo.variant = StringToVariant(appData.variant);
9320 ics_user_moved = ics_clock_paused = FALSE;
9321 ics_getting_history = H_FALSE;
9323 white_holding[0] = black_holding[0] = NULLCHAR;
9324 ClearProgramStats();
9325 opponentKibitzes = FALSE; // [HGM] kibitz: do not reserve space in engine-output window in zippy mode
9329 flipView = appData.flipView;
9330 ClearPremoveHighlights();
9332 alarmSounded = FALSE;
9334 GameEnds((ChessMove) 0, NULL, GE_PLAYER);
9335 if(appData.serverMovesName != NULL) {
9336 /* [HGM] prepare to make moves file for broadcasting */
9337 clock_t t = clock();
9338 if(serverMoves != NULL) fclose(serverMoves);
9339 serverMoves = fopen(appData.serverMovesName, "r");
9340 if(serverMoves != NULL) {
9341 fclose(serverMoves);
9342 /* delay 15 sec before overwriting, so all clients can see end */
9343 while(clock()-t < appData.serverPause*CLOCKS_PER_SEC);
9345 serverMoves = fopen(appData.serverMovesName, "w");
9349 gameMode = BeginningOfGame;
9351 if(appData.icsActive) gameInfo.variant = VariantNormal;
9352 currentMove = forwardMostMove = backwardMostMove = 0;
9353 InitPosition(redraw);
9354 for (i = 0; i < MAX_MOVES; i++) {
9355 if (commentList[i] != NULL) {
9356 free(commentList[i]);
9357 commentList[i] = NULL;
9361 timeRemaining[0][0] = whiteTimeRemaining;
9362 timeRemaining[1][0] = blackTimeRemaining;
9363 if (first.pr == NULL) {
9364 StartChessProgram(&first);
9367 InitChessProgram(&first, startedFromSetupPosition);
9370 DisplayMessage("", "");
9371 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
9372 lastSavedGame = 0; // [HGM] save: make sure next game counts as unsaved
9379 if (!AutoPlayOneMove())
9381 if (matchMode || appData.timeDelay == 0)
9383 if (appData.timeDelay < 0 || gameMode == AnalyzeFile)
9385 StartLoadGameTimer((long)(1000.0 * appData.timeDelay));
9394 int fromX, fromY, toX, toY;
9396 if (appData.debugMode) {
9397 fprintf(debugFP, "AutoPlayOneMove(): current %d\n", currentMove);
9400 if (gameMode != PlayFromGameFile)
9403 if (currentMove >= forwardMostMove) {
9404 gameMode = EditGame;
9407 /* [AS] Clear current move marker at the end of a game */
9408 /* HistorySet(parseList, backwardMostMove, forwardMostMove, -1); */
9413 toX = moveList[currentMove][2] - AAA;
9414 toY = moveList[currentMove][3] - ONE;
9416 if (moveList[currentMove][1] == '@') {
9417 if (appData.highlightLastMove) {
9418 SetHighlights(-1, -1, toX, toY);
9421 fromX = moveList[currentMove][0] - AAA;
9422 fromY = moveList[currentMove][1] - ONE;
9424 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove); /* [AS] */
9426 AnimateMove(boards[currentMove], fromX, fromY, toX, toY);
9428 if (appData.highlightLastMove) {
9429 SetHighlights(fromX, fromY, toX, toY);
9432 DisplayMove(currentMove);
9433 SendMoveToProgram(currentMove++, &first);
9434 DisplayBothClocks();
9435 DrawPosition(FALSE, boards[currentMove]);
9436 // [HGM] PV info: always display, routine tests if empty
9437 DisplayComment(currentMove - 1, commentList[currentMove]);
9443 LoadGameOneMove(readAhead)
9444 ChessMove readAhead;
9446 int fromX = 0, fromY = 0, toX = 0, toY = 0, done;
9447 char promoChar = NULLCHAR;
9452 if (gameMode != PlayFromGameFile && gameMode != AnalyzeFile &&
9453 gameMode != AnalyzeMode && gameMode != Training) {
9458 yyboardindex = forwardMostMove;
9459 if (readAhead != (ChessMove)0) {
9460 moveType = readAhead;
9462 if (gameFileFP == NULL)
9464 moveType = (ChessMove) yylex();
9470 if (appData.debugMode)
9471 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
9474 /* append the comment but don't display it */
9475 AppendComment(currentMove, p, FALSE);
9478 case WhiteCapturesEnPassant:
9479 case BlackCapturesEnPassant:
9480 case WhitePromotionChancellor:
9481 case BlackPromotionChancellor:
9482 case WhitePromotionArchbishop:
9483 case BlackPromotionArchbishop:
9484 case WhitePromotionCentaur:
9485 case BlackPromotionCentaur:
9486 case WhitePromotionQueen:
9487 case BlackPromotionQueen:
9488 case WhitePromotionRook:
9489 case BlackPromotionRook:
9490 case WhitePromotionBishop:
9491 case BlackPromotionBishop:
9492 case WhitePromotionKnight:
9493 case BlackPromotionKnight:
9494 case WhitePromotionKing:
9495 case BlackPromotionKing:
9497 case WhiteKingSideCastle:
9498 case WhiteQueenSideCastle:
9499 case BlackKingSideCastle:
9500 case BlackQueenSideCastle:
9501 case WhiteKingSideCastleWild:
9502 case WhiteQueenSideCastleWild:
9503 case BlackKingSideCastleWild:
9504 case BlackQueenSideCastleWild:
9506 case WhiteHSideCastleFR:
9507 case WhiteASideCastleFR:
9508 case BlackHSideCastleFR:
9509 case BlackASideCastleFR:
9511 if (appData.debugMode)
9512 fprintf(debugFP, "Parsed %s into %s\n", yy_text, currentMoveString);
9513 fromX = currentMoveString[0] - AAA;
9514 fromY = currentMoveString[1] - ONE;
9515 toX = currentMoveString[2] - AAA;
9516 toY = currentMoveString[3] - ONE;
9517 promoChar = currentMoveString[4];
9522 if (appData.debugMode)
9523 fprintf(debugFP, "Parsed %s into %s\n", yy_text, currentMoveString);
9524 fromX = moveType == WhiteDrop ?
9525 (int) CharToPiece(ToUpper(currentMoveString[0])) :
9526 (int) CharToPiece(ToLower(currentMoveString[0]));
9528 toX = currentMoveString[2] - AAA;
9529 toY = currentMoveString[3] - ONE;
9535 case GameUnfinished:
9536 if (appData.debugMode)
9537 fprintf(debugFP, "Parsed game end: %s\n", yy_text);
9538 p = strchr(yy_text, '{');
9539 if (p == NULL) p = strchr(yy_text, '(');
9542 if (p[0] == '0' || p[0] == '1' || p[0] == '*') p = "";
9544 q = strchr(p, *p == '{' ? '}' : ')');
9545 if (q != NULL) *q = NULLCHAR;
9548 GameEnds(moveType, p, GE_FILE);
9550 if (cmailMsgLoaded) {
9552 flipView = WhiteOnMove(currentMove);
9553 if (moveType == GameUnfinished) flipView = !flipView;
9554 if (appData.debugMode)
9555 fprintf(debugFP, "Setting flipView to %d\n", flipView) ;
9559 case (ChessMove) 0: /* end of file */
9560 if (appData.debugMode)
9561 fprintf(debugFP, "Parser hit end of file\n");
9562 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
9568 if (WhiteOnMove(currentMove)) {
9569 GameEnds(BlackWins, "Black mates", GE_FILE);
9571 GameEnds(WhiteWins, "White mates", GE_FILE);
9575 GameEnds(GameIsDrawn, "Stalemate", GE_FILE);
9582 if (lastLoadGameStart == GNUChessGame) {
9583 /* GNUChessGames have numbers, but they aren't move numbers */
9584 if (appData.debugMode)
9585 fprintf(debugFP, "Parser ignoring: '%s' (%d)\n",
9586 yy_text, (int) moveType);
9587 return LoadGameOneMove((ChessMove)0); /* tail recursion */
9589 /* else fall thru */
9594 /* Reached start of next game in file */
9595 if (appData.debugMode)
9596 fprintf(debugFP, "Parsed start of next game: %s\n", yy_text);
9597 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
9603 if (WhiteOnMove(currentMove)) {
9604 GameEnds(BlackWins, "Black mates", GE_FILE);
9606 GameEnds(WhiteWins, "White mates", GE_FILE);
9610 GameEnds(GameIsDrawn, "Stalemate", GE_FILE);
9616 case PositionDiagram: /* should not happen; ignore */
9617 case ElapsedTime: /* ignore */
9618 case NAG: /* ignore */
9619 if (appData.debugMode)
9620 fprintf(debugFP, "Parser ignoring: '%s' (%d)\n",
9621 yy_text, (int) moveType);
9622 return LoadGameOneMove((ChessMove)0); /* tail recursion */
9625 if (appData.testLegality) {
9626 if (appData.debugMode)
9627 fprintf(debugFP, "Parsed IllegalMove: %s\n", yy_text);
9628 sprintf(move, _("Illegal move: %d.%s%s"),
9629 (forwardMostMove / 2) + 1,
9630 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
9631 DisplayError(move, 0);
9634 if (appData.debugMode)
9635 fprintf(debugFP, "Parsed %s into IllegalMove %s\n",
9636 yy_text, currentMoveString);
9637 fromX = currentMoveString[0] - AAA;
9638 fromY = currentMoveString[1] - ONE;
9639 toX = currentMoveString[2] - AAA;
9640 toY = currentMoveString[3] - ONE;
9641 promoChar = currentMoveString[4];
9646 if (appData.debugMode)
9647 fprintf(debugFP, "Parsed AmbiguousMove: %s\n", yy_text);
9648 sprintf(move, _("Ambiguous move: %d.%s%s"),
9649 (forwardMostMove / 2) + 1,
9650 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
9651 DisplayError(move, 0);
9656 case ImpossibleMove:
9657 if (appData.debugMode)
9658 fprintf(debugFP, "Parsed ImpossibleMove (type = %d): %s\n", moveType, yy_text);
9659 sprintf(move, _("Illegal move: %d.%s%s"),
9660 (forwardMostMove / 2) + 1,
9661 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
9662 DisplayError(move, 0);
9668 if (appData.matchMode || (appData.timeDelay == 0 && !pausing)) {
9669 DrawPosition(FALSE, boards[currentMove]);
9670 DisplayBothClocks();
9671 if (!appData.matchMode) // [HGM] PV info: routine tests if empty
9672 DisplayComment(currentMove - 1, commentList[currentMove]);
9674 (void) StopLoadGameTimer();
9676 cmailOldMove = forwardMostMove;
9679 /* currentMoveString is set as a side-effect of yylex */
9680 strcat(currentMoveString, "\n");
9681 strcpy(moveList[forwardMostMove], currentMoveString);
9683 thinkOutput[0] = NULLCHAR;
9684 MakeMove(fromX, fromY, toX, toY, promoChar);
9685 currentMove = forwardMostMove;
9690 /* Load the nth game from the given file */
9692 LoadGameFromFile(filename, n, title, useList)
9696 /*Boolean*/ int useList;
9701 if (strcmp(filename, "-") == 0) {
9705 f = fopen(filename, "rb");
9707 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
9708 DisplayError(buf, errno);
9712 if (fseek(f, 0, 0) == -1) {
9713 /* f is not seekable; probably a pipe */
9716 if (useList && n == 0) {
9717 int error = GameListBuild(f);
9719 DisplayError(_("Cannot build game list"), error);
9720 } else if (!ListEmpty(&gameList) &&
9721 ((ListGame *) gameList.tailPred)->number > 1) {
9722 GameListPopUp(f, title);
9729 return LoadGame(f, n, title, FALSE);
9734 MakeRegisteredMove()
9736 int fromX, fromY, toX, toY;
9738 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
9739 switch (cmailMoveType[lastLoadGameNumber - 1]) {
9742 if (appData.debugMode)
9743 fprintf(debugFP, "Restoring %s for game %d\n",
9744 cmailMove[lastLoadGameNumber - 1], lastLoadGameNumber);
9746 thinkOutput[0] = NULLCHAR;
9747 strcpy(moveList[currentMove], cmailMove[lastLoadGameNumber - 1]);
9748 fromX = cmailMove[lastLoadGameNumber - 1][0] - AAA;
9749 fromY = cmailMove[lastLoadGameNumber - 1][1] - ONE;
9750 toX = cmailMove[lastLoadGameNumber - 1][2] - AAA;
9751 toY = cmailMove[lastLoadGameNumber - 1][3] - ONE;
9752 promoChar = cmailMove[lastLoadGameNumber - 1][4];
9753 MakeMove(fromX, fromY, toX, toY, promoChar);
9754 ShowMove(fromX, fromY, toX, toY);
9756 switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
9763 if (WhiteOnMove(currentMove)) {
9764 GameEnds(BlackWins, "Black mates", GE_PLAYER);
9766 GameEnds(WhiteWins, "White mates", GE_PLAYER);
9771 GameEnds(GameIsDrawn, "Stalemate", GE_PLAYER);
9778 if (WhiteOnMove(currentMove)) {
9779 GameEnds(BlackWins, "White resigns", GE_PLAYER);
9781 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
9786 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
9797 /* Wrapper around LoadGame for use when a Cmail message is loaded */
9799 CmailLoadGame(f, gameNumber, title, useList)
9807 if (gameNumber > nCmailGames) {
9808 DisplayError(_("No more games in this message"), 0);
9811 if (f == lastLoadGameFP) {
9812 int offset = gameNumber - lastLoadGameNumber;
9814 cmailMsg[0] = NULLCHAR;
9815 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
9816 cmailMoveRegistered[lastLoadGameNumber - 1] = FALSE;
9817 nCmailMovesRegistered--;
9819 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
9820 if (cmailResult[lastLoadGameNumber - 1] == CMAIL_NEW_RESULT) {
9821 cmailResult[lastLoadGameNumber - 1] = CMAIL_NOT_RESULT;
9824 if (! RegisterMove()) return FALSE;
9828 retVal = LoadGame(f, gameNumber, title, useList);
9830 /* Make move registered during previous look at this game, if any */
9831 MakeRegisteredMove();
9833 if (cmailCommentList[lastLoadGameNumber - 1] != NULL) {
9834 commentList[currentMove]
9835 = StrSave(cmailCommentList[lastLoadGameNumber - 1]);
9836 DisplayComment(currentMove - 1, commentList[currentMove]);
9842 /* Support for LoadNextGame, LoadPreviousGame, ReloadSameGame */
9847 int gameNumber = lastLoadGameNumber + offset;
9848 if (lastLoadGameFP == NULL) {
9849 DisplayError(_("No game has been loaded yet"), 0);
9852 if (gameNumber <= 0) {
9853 DisplayError(_("Can't back up any further"), 0);
9856 if (cmailMsgLoaded) {
9857 return CmailLoadGame(lastLoadGameFP, gameNumber,
9858 lastLoadGameTitle, lastLoadGameUseList);
9860 return LoadGame(lastLoadGameFP, gameNumber,
9861 lastLoadGameTitle, lastLoadGameUseList);
9867 /* Load the nth game from open file f */
9869 LoadGame(f, gameNumber, title, useList)
9877 int gn = gameNumber;
9878 ListGame *lg = NULL;
9881 GameMode oldGameMode;
9882 VariantClass oldVariant = gameInfo.variant; /* [HGM] PGNvariant */
9884 if (appData.debugMode)
9885 fprintf(debugFP, "LoadGame(): on entry, gameMode %d\n", gameMode);
9887 if (gameMode == Training )
9888 SetTrainingModeOff();
9890 oldGameMode = gameMode;
9891 if (gameMode != BeginningOfGame) {
9896 if (lastLoadGameFP != NULL && lastLoadGameFP != f) {
9897 fclose(lastLoadGameFP);
9901 lg = (ListGame *) ListElem(&gameList, gameNumber-1);
9904 fseek(f, lg->offset, 0);
9905 GameListHighlight(gameNumber);
9909 DisplayError(_("Game number out of range"), 0);
9914 if (fseek(f, 0, 0) == -1) {
9915 if (f == lastLoadGameFP ?
9916 gameNumber == lastLoadGameNumber + 1 :
9920 DisplayError(_("Can't seek on game file"), 0);
9926 lastLoadGameNumber = gameNumber;
9927 strcpy(lastLoadGameTitle, title);
9928 lastLoadGameUseList = useList;
9932 if (lg && lg->gameInfo.white && lg->gameInfo.black) {
9933 snprintf(buf, sizeof(buf), "%s vs. %s", lg->gameInfo.white,
9934 lg->gameInfo.black);
9936 } else if (*title != NULLCHAR) {
9937 if (gameNumber > 1) {
9938 sprintf(buf, "%s %d", title, gameNumber);
9941 DisplayTitle(title);
9945 if (gameMode != AnalyzeFile && gameMode != AnalyzeMode) {
9946 gameMode = PlayFromGameFile;
9950 currentMove = forwardMostMove = backwardMostMove = 0;
9951 CopyBoard(boards[0], initialPosition);
9955 * Skip the first gn-1 games in the file.
9956 * Also skip over anything that precedes an identifiable
9957 * start of game marker, to avoid being confused by
9958 * garbage at the start of the file. Currently
9959 * recognized start of game markers are the move number "1",
9960 * the pattern "gnuchess .* game", the pattern
9961 * "^[#;%] [^ ]* game file", and a PGN tag block.
9962 * A game that starts with one of the latter two patterns
9963 * will also have a move number 1, possibly
9964 * following a position diagram.
9965 * 5-4-02: Let's try being more lenient and allowing a game to
9966 * start with an unnumbered move. Does that break anything?
9968 cm = lastLoadGameStart = (ChessMove) 0;
9970 yyboardindex = forwardMostMove;
9971 cm = (ChessMove) yylex();
9974 if (cmailMsgLoaded) {
9975 nCmailGames = CMAIL_MAX_GAMES - gn;
9978 DisplayError(_("Game not found in file"), 0);
9985 lastLoadGameStart = cm;
9989 switch (lastLoadGameStart) {
9996 gn--; /* count this game */
9997 lastLoadGameStart = cm;
10006 switch (lastLoadGameStart) {
10009 case MoveNumberOne:
10010 case (ChessMove) 0:
10011 gn--; /* count this game */
10012 lastLoadGameStart = cm;
10015 lastLoadGameStart = cm; /* game counted already */
10023 yyboardindex = forwardMostMove;
10024 cm = (ChessMove) yylex();
10025 } while (cm == PGNTag || cm == Comment);
10032 if (cmailMsgLoaded && (CMAIL_MAX_GAMES == lastLoadGameNumber)) {
10033 if ( cmailResult[CMAIL_MAX_GAMES - gn - 1]
10034 != CMAIL_OLD_RESULT) {
10036 cmailResult[ CMAIL_MAX_GAMES
10037 - gn - 1] = CMAIL_OLD_RESULT;
10043 /* Only a NormalMove can be at the start of a game
10044 * without a position diagram. */
10045 if (lastLoadGameStart == (ChessMove) 0) {
10047 lastLoadGameStart = MoveNumberOne;
10056 if (appData.debugMode)
10057 fprintf(debugFP, "Parsed game start '%s' (%d)\n", yy_text, (int) cm);
10059 if (cm == XBoardGame) {
10060 /* Skip any header junk before position diagram and/or move 1 */
10062 yyboardindex = forwardMostMove;
10063 cm = (ChessMove) yylex();
10065 if (cm == (ChessMove) 0 ||
10066 cm == GNUChessGame || cm == XBoardGame) {
10067 /* Empty game; pretend end-of-file and handle later */
10068 cm = (ChessMove) 0;
10072 if (cm == MoveNumberOne || cm == PositionDiagram ||
10073 cm == PGNTag || cm == Comment)
10076 } else if (cm == GNUChessGame) {
10077 if (gameInfo.event != NULL) {
10078 free(gameInfo.event);
10080 gameInfo.event = StrSave(yy_text);
10083 startedFromSetupPosition = FALSE;
10084 while (cm == PGNTag) {
10085 if (appData.debugMode)
10086 fprintf(debugFP, "Parsed PGNTag: %s\n", yy_text);
10087 err = ParsePGNTag(yy_text, &gameInfo);
10088 if (!err) numPGNTags++;
10090 /* [HGM] PGNvariant: automatically switch to variant given in PGN tag */
10091 if(gameInfo.variant != oldVariant) {
10092 startedFromPositionFile = FALSE; /* [HGM] loadPos: variant switch likely makes position invalid */
10093 InitPosition(TRUE);
10094 oldVariant = gameInfo.variant;
10095 if (appData.debugMode)
10096 fprintf(debugFP, "New variant %d\n", (int) oldVariant);
10100 if (gameInfo.fen != NULL) {
10101 Board initial_position;
10102 startedFromSetupPosition = TRUE;
10103 if (!ParseFEN(initial_position, &blackPlaysFirst, gameInfo.fen)) {
10105 DisplayError(_("Bad FEN position in file"), 0);
10108 CopyBoard(boards[0], initial_position);
10109 if (blackPlaysFirst) {
10110 currentMove = forwardMostMove = backwardMostMove = 1;
10111 CopyBoard(boards[1], initial_position);
10112 strcpy(moveList[0], "");
10113 strcpy(parseList[0], "");
10114 timeRemaining[0][1] = whiteTimeRemaining;
10115 timeRemaining[1][1] = blackTimeRemaining;
10116 if (commentList[0] != NULL) {
10117 commentList[1] = commentList[0];
10118 commentList[0] = NULL;
10121 currentMove = forwardMostMove = backwardMostMove = 0;
10123 /* [HGM] copy FEN attributes as well. Bugfix 4.3.14m and 4.3.15e: moved to after 'blackPlaysFirst' */
10125 initialRulePlies = FENrulePlies;
10126 for( i=0; i< nrCastlingRights; i++ )
10127 initialRights[i] = initial_position[CASTLING][i];
10129 yyboardindex = forwardMostMove;
10130 free(gameInfo.fen);
10131 gameInfo.fen = NULL;
10134 yyboardindex = forwardMostMove;
10135 cm = (ChessMove) yylex();
10137 /* Handle comments interspersed among the tags */
10138 while (cm == Comment) {
10140 if (appData.debugMode)
10141 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
10143 AppendComment(currentMove, p, FALSE);
10144 yyboardindex = forwardMostMove;
10145 cm = (ChessMove) yylex();
10149 /* don't rely on existence of Event tag since if game was
10150 * pasted from clipboard the Event tag may not exist
10152 if (numPGNTags > 0){
10154 if (gameInfo.variant == VariantNormal) {
10155 gameInfo.variant = StringToVariant(gameInfo.event);
10158 if( appData.autoDisplayTags ) {
10159 tags = PGNTags(&gameInfo);
10160 TagsPopUp(tags, CmailMsg());
10165 /* Make something up, but don't display it now */
10170 if (cm == PositionDiagram) {
10173 Board initial_position;
10175 if (appData.debugMode)
10176 fprintf(debugFP, "Parsed PositionDiagram: %s\n", yy_text);
10178 if (!startedFromSetupPosition) {
10180 for (i = BOARD_HEIGHT - 1; i >= 0; i--)
10181 for (j = BOARD_LEFT; j < BOARD_RGHT; p++)
10191 initial_position[i][j++] = CharToPiece(*p);
10194 while (*p == ' ' || *p == '\t' ||
10195 *p == '\n' || *p == '\r') p++;
10197 if (strncmp(p, "black", strlen("black"))==0)
10198 blackPlaysFirst = TRUE;
10200 blackPlaysFirst = FALSE;
10201 startedFromSetupPosition = TRUE;
10203 CopyBoard(boards[0], initial_position);
10204 if (blackPlaysFirst) {
10205 currentMove = forwardMostMove = backwardMostMove = 1;
10206 CopyBoard(boards[1], initial_position);
10207 strcpy(moveList[0], "");
10208 strcpy(parseList[0], "");
10209 timeRemaining[0][1] = whiteTimeRemaining;
10210 timeRemaining[1][1] = blackTimeRemaining;
10211 if (commentList[0] != NULL) {
10212 commentList[1] = commentList[0];
10213 commentList[0] = NULL;
10216 currentMove = forwardMostMove = backwardMostMove = 0;
10219 yyboardindex = forwardMostMove;
10220 cm = (ChessMove) yylex();
10223 if (first.pr == NoProc) {
10224 StartChessProgram(&first);
10226 InitChessProgram(&first, FALSE);
10227 SendToProgram("force\n", &first);
10228 if (startedFromSetupPosition) {
10229 SendBoard(&first, forwardMostMove);
10230 if (appData.debugMode) {
10231 fprintf(debugFP, "Load Game\n");
10233 DisplayBothClocks();
10236 /* [HGM] server: flag to write setup moves in broadcast file as one */
10237 loadFlag = appData.suppressLoadMoves;
10239 while (cm == Comment) {
10241 if (appData.debugMode)
10242 fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
10244 AppendComment(currentMove, p, FALSE);
10245 yyboardindex = forwardMostMove;
10246 cm = (ChessMove) yylex();
10249 if ((cm == (ChessMove) 0 && lastLoadGameStart != (ChessMove) 0) ||
10250 cm == WhiteWins || cm == BlackWins ||
10251 cm == GameIsDrawn || cm == GameUnfinished) {
10252 DisplayMessage("", _("No moves in game"));
10253 if (cmailMsgLoaded) {
10254 if (appData.debugMode)
10255 fprintf(debugFP, "Setting flipView to %d.\n", FALSE);
10259 DrawPosition(FALSE, boards[currentMove]);
10260 DisplayBothClocks();
10261 gameMode = EditGame;
10268 // [HGM] PV info: routine tests if comment empty
10269 if (!matchMode && (pausing || appData.timeDelay != 0)) {
10270 DisplayComment(currentMove - 1, commentList[currentMove]);
10272 if (!matchMode && appData.timeDelay != 0)
10273 DrawPosition(FALSE, boards[currentMove]);
10275 if (gameMode == AnalyzeFile || gameMode == AnalyzeMode) {
10276 programStats.ok_to_send = 1;
10279 /* if the first token after the PGN tags is a move
10280 * and not move number 1, retrieve it from the parser
10282 if (cm != MoveNumberOne)
10283 LoadGameOneMove(cm);
10285 /* load the remaining moves from the file */
10286 while (LoadGameOneMove((ChessMove)0)) {
10287 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
10288 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
10291 /* rewind to the start of the game */
10292 currentMove = backwardMostMove;
10294 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
10296 if (oldGameMode == AnalyzeFile ||
10297 oldGameMode == AnalyzeMode) {
10298 AnalyzeFileEvent();
10301 if (matchMode || appData.timeDelay == 0) {
10303 gameMode = EditGame;
10305 } else if (appData.timeDelay > 0) {
10306 AutoPlayGameLoop();
10309 if (appData.debugMode)
10310 fprintf(debugFP, "LoadGame(): on exit, gameMode %d\n", gameMode);
10312 loadFlag = 0; /* [HGM] true game starts */
10316 /* Support for LoadNextPosition, LoadPreviousPosition, ReloadSamePosition */
10318 ReloadPosition(offset)
10321 int positionNumber = lastLoadPositionNumber + offset;
10322 if (lastLoadPositionFP == NULL) {
10323 DisplayError(_("No position has been loaded yet"), 0);
10326 if (positionNumber <= 0) {
10327 DisplayError(_("Can't back up any further"), 0);
10330 return LoadPosition(lastLoadPositionFP, positionNumber,
10331 lastLoadPositionTitle);
10334 /* Load the nth position from the given file */
10336 LoadPositionFromFile(filename, n, title)
10344 if (strcmp(filename, "-") == 0) {
10345 return LoadPosition(stdin, n, "stdin");
10347 f = fopen(filename, "rb");
10349 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
10350 DisplayError(buf, errno);
10353 return LoadPosition(f, n, title);
10358 /* Load the nth position from the given open file, and close it */
10360 LoadPosition(f, positionNumber, title)
10362 int positionNumber;
10365 char *p, line[MSG_SIZ];
10366 Board initial_position;
10367 int i, j, fenMode, pn;
10369 if (gameMode == Training )
10370 SetTrainingModeOff();
10372 if (gameMode != BeginningOfGame) {
10373 Reset(FALSE, TRUE);
10375 if (lastLoadPositionFP != NULL && lastLoadPositionFP != f) {
10376 fclose(lastLoadPositionFP);
10378 if (positionNumber == 0) positionNumber = 1;
10379 lastLoadPositionFP = f;
10380 lastLoadPositionNumber = positionNumber;
10381 strcpy(lastLoadPositionTitle, title);
10382 if (first.pr == NoProc) {
10383 StartChessProgram(&first);
10384 InitChessProgram(&first, FALSE);
10386 pn = positionNumber;
10387 if (positionNumber < 0) {
10388 /* Negative position number means to seek to that byte offset */
10389 if (fseek(f, -positionNumber, 0) == -1) {
10390 DisplayError(_("Can't seek on position file"), 0);
10395 if (fseek(f, 0, 0) == -1) {
10396 if (f == lastLoadPositionFP ?
10397 positionNumber == lastLoadPositionNumber + 1 :
10398 positionNumber == 1) {
10401 DisplayError(_("Can't seek on position file"), 0);
10406 /* See if this file is FEN or old-style xboard */
10407 if (fgets(line, MSG_SIZ, f) == NULL) {
10408 DisplayError(_("Position not found in file"), 0);
10411 // [HGM] FEN can begin with digit, any piece letter valid in this variant, or a + for Shogi promoted pieces
10412 fenMode = line[0] >= '0' && line[0] <= '9' || line[0] == '+' || CharToPiece(line[0]) != EmptySquare;
10415 if (fenMode || line[0] == '#') pn--;
10417 /* skip positions before number pn */
10418 if (fgets(line, MSG_SIZ, f) == NULL) {
10420 DisplayError(_("Position not found in file"), 0);
10423 if (fenMode || line[0] == '#') pn--;
10428 if (!ParseFEN(initial_position, &blackPlaysFirst, line)) {
10429 DisplayError(_("Bad FEN position in file"), 0);
10433 (void) fgets(line, MSG_SIZ, f);
10434 (void) fgets(line, MSG_SIZ, f);
10436 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
10437 (void) fgets(line, MSG_SIZ, f);
10438 for (p = line, j = BOARD_LEFT; j < BOARD_RGHT; p++) {
10441 initial_position[i][j++] = CharToPiece(*p);
10445 blackPlaysFirst = FALSE;
10447 (void) fgets(line, MSG_SIZ, f);
10448 if (strncmp(line, "black", strlen("black"))==0)
10449 blackPlaysFirst = TRUE;
10452 startedFromSetupPosition = TRUE;
10454 SendToProgram("force\n", &first);
10455 CopyBoard(boards[0], initial_position);
10456 if (blackPlaysFirst) {
10457 currentMove = forwardMostMove = backwardMostMove = 1;
10458 strcpy(moveList[0], "");
10459 strcpy(parseList[0], "");
10460 CopyBoard(boards[1], initial_position);
10461 DisplayMessage("", _("Black to play"));
10463 currentMove = forwardMostMove = backwardMostMove = 0;
10464 DisplayMessage("", _("White to play"));
10466 initialRulePlies = FENrulePlies; /* [HGM] copy FEN attributes as well */
10467 SendBoard(&first, forwardMostMove);
10468 if (appData.debugMode) {
10470 for(i=0;i<2;i++){for(j=0;j<6;j++)fprintf(debugFP, " %d", boards[i][CASTLING][j]);fprintf(debugFP,"\n");}
10471 for(j=0;j<6;j++)fprintf(debugFP, " %d", initialRights[j]);fprintf(debugFP,"\n");
10472 fprintf(debugFP, "Load Position\n");
10475 if (positionNumber > 1) {
10476 sprintf(line, "%s %d", title, positionNumber);
10477 DisplayTitle(line);
10479 DisplayTitle(title);
10481 gameMode = EditGame;
10484 timeRemaining[0][1] = whiteTimeRemaining;
10485 timeRemaining[1][1] = blackTimeRemaining;
10486 DrawPosition(FALSE, boards[currentMove]);
10493 CopyPlayerNameIntoFileName(dest, src)
10496 while (*src != NULLCHAR && *src != ',') {
10501 *(*dest)++ = *src++;
10506 char *DefaultFileName(ext)
10509 static char def[MSG_SIZ];
10512 if (gameInfo.white != NULL && gameInfo.white[0] != '-') {
10514 CopyPlayerNameIntoFileName(&p, gameInfo.white);
10516 CopyPlayerNameIntoFileName(&p, gameInfo.black);
10525 /* Save the current game to the given file */
10527 SaveGameToFile(filename, append)
10534 if (strcmp(filename, "-") == 0) {
10535 return SaveGame(stdout, 0, NULL);
10537 f = fopen(filename, append ? "a" : "w");
10539 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
10540 DisplayError(buf, errno);
10543 return SaveGame(f, 0, NULL);
10552 static char buf[MSG_SIZ];
10555 p = strchr(str, ' ');
10556 if (p == NULL) return str;
10557 strncpy(buf, str, p - str);
10558 buf[p - str] = NULLCHAR;
10562 #define PGN_MAX_LINE 75
10564 #define PGN_SIDE_WHITE 0
10565 #define PGN_SIDE_BLACK 1
10568 static int FindFirstMoveOutOfBook( int side )
10572 if( backwardMostMove == 0 && ! startedFromSetupPosition) {
10573 int index = backwardMostMove;
10574 int has_book_hit = 0;
10576 if( (index % 2) != side ) {
10580 while( index < forwardMostMove ) {
10581 /* Check to see if engine is in book */
10582 int depth = pvInfoList[index].depth;
10583 int score = pvInfoList[index].score;
10589 else if( score == 0 && depth == 63 ) {
10590 in_book = 1; /* Zappa */
10592 else if( score == 2 && depth == 99 ) {
10593 in_book = 1; /* Abrok */
10596 has_book_hit += in_book;
10612 void GetOutOfBookInfo( char * buf )
10616 int offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
10618 oob[0] = FindFirstMoveOutOfBook( PGN_SIDE_WHITE );
10619 oob[1] = FindFirstMoveOutOfBook( PGN_SIDE_BLACK );
10623 if( oob[0] >= 0 || oob[1] >= 0 ) {
10624 for( i=0; i<2; i++ ) {
10628 if( i > 0 && oob[0] >= 0 ) {
10629 strcat( buf, " " );
10632 sprintf( buf+strlen(buf), "%d%s. ", (idx - offset)/2 + 1, idx & 1 ? ".." : "" );
10633 sprintf( buf+strlen(buf), "%s%.2f",
10634 pvInfoList[idx].score >= 0 ? "+" : "",
10635 pvInfoList[idx].score / 100.0 );
10641 /* Save game in PGN style and close the file */
10646 int i, offset, linelen, newblock;
10650 int movelen, numlen, blank;
10651 char move_buffer[100]; /* [AS] Buffer for move+PV info */
10653 offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
10655 tm = time((time_t *) NULL);
10657 PrintPGNTags(f, &gameInfo);
10659 if (backwardMostMove > 0 || startedFromSetupPosition) {
10660 char *fen = PositionToFEN(backwardMostMove, NULL);
10661 fprintf(f, "[FEN \"%s\"]\n[SetUp \"1\"]\n", fen);
10662 fprintf(f, "\n{--------------\n");
10663 PrintPosition(f, backwardMostMove);
10664 fprintf(f, "--------------}\n");
10668 /* [AS] Out of book annotation */
10669 if( appData.saveOutOfBookInfo ) {
10672 GetOutOfBookInfo( buf );
10674 if( buf[0] != '\0' ) {
10675 fprintf( f, "[%s \"%s\"]\n", PGN_OUT_OF_BOOK, buf );
10682 i = backwardMostMove;
10686 while (i < forwardMostMove) {
10687 /* Print comments preceding this move */
10688 if (commentList[i] != NULL) {
10689 if (linelen > 0) fprintf(f, "\n");
10690 fprintf(f, "%s", commentList[i]);
10695 /* Format move number */
10696 if ((i % 2) == 0) {
10697 sprintf(numtext, "%d.", (i - offset)/2 + 1);
10700 sprintf(numtext, "%d...", (i - offset)/2 + 1);
10702 numtext[0] = NULLCHAR;
10705 numlen = strlen(numtext);
10708 /* Print move number */
10709 blank = linelen > 0 && numlen > 0;
10710 if (linelen + (blank ? 1 : 0) + numlen > PGN_MAX_LINE) {
10719 fprintf(f, "%s", numtext);
10723 strcpy(move_buffer, SavePart(parseList[i])); // [HGM] pgn: print move via buffer, so it can be edited
10724 movelen = strlen(move_buffer); /* [HGM] pgn: line-break point before move */
10727 blank = linelen > 0 && movelen > 0;
10728 if (linelen + (blank ? 1 : 0) + movelen > PGN_MAX_LINE) {
10737 fprintf(f, "%s", move_buffer);
10738 linelen += movelen;
10740 /* [AS] Add PV info if present */
10741 if( i >= 0 && appData.saveExtendedInfoInPGN && pvInfoList[i].depth > 0 ) {
10742 /* [HGM] add time */
10743 char buf[MSG_SIZ]; int seconds;
10745 seconds = (pvInfoList[i].time+5)/10; // deci-seconds, rounded to nearest
10747 if( seconds <= 0) buf[0] = 0; else
10748 if( seconds < 30 ) sprintf(buf, " %3.1f%c", seconds/10., 0); else {
10749 seconds = (seconds + 4)/10; // round to full seconds
10750 if( seconds < 60 ) sprintf(buf, " %d%c", seconds, 0); else
10751 sprintf(buf, " %d:%02d%c", seconds/60, seconds%60, 0);
10754 sprintf( move_buffer, "{%s%.2f/%d%s}",
10755 pvInfoList[i].score >= 0 ? "+" : "",
10756 pvInfoList[i].score / 100.0,
10757 pvInfoList[i].depth,
10760 movelen = strlen(move_buffer); /* [HGM] pgn: line-break point after move */
10762 /* Print score/depth */
10763 blank = linelen > 0 && movelen > 0;
10764 if (linelen + (blank ? 1 : 0) + movelen > PGN_MAX_LINE) {
10773 fprintf(f, "%s", move_buffer);
10774 linelen += movelen;
10780 /* Start a new line */
10781 if (linelen > 0) fprintf(f, "\n");
10783 /* Print comments after last move */
10784 if (commentList[i] != NULL) {
10785 fprintf(f, "%s\n", commentList[i]);
10789 if (gameInfo.resultDetails != NULL &&
10790 gameInfo.resultDetails[0] != NULLCHAR) {
10791 fprintf(f, "{%s} %s\n\n", gameInfo.resultDetails,
10792 PGNResult(gameInfo.result));
10794 fprintf(f, "%s\n\n", PGNResult(gameInfo.result));
10798 lastSavedGame = GameCheckSum(); // [HGM] save: remember ID of last saved game to prevent double saving
10802 /* Save game in old style and close the file */
10804 SaveGameOldStyle(f)
10810 tm = time((time_t *) NULL);
10812 fprintf(f, "# %s game file -- %s", programName, ctime(&tm));
10815 if (backwardMostMove > 0 || startedFromSetupPosition) {
10816 fprintf(f, "\n[--------------\n");
10817 PrintPosition(f, backwardMostMove);
10818 fprintf(f, "--------------]\n");
10823 i = backwardMostMove;
10824 offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
10826 while (i < forwardMostMove) {
10827 if (commentList[i] != NULL) {
10828 fprintf(f, "[%s]\n", commentList[i]);
10831 if ((i % 2) == 1) {
10832 fprintf(f, "%d. ... %s\n", (i - offset)/2 + 1, parseList[i]);
10835 fprintf(f, "%d. %s ", (i - offset)/2 + 1, parseList[i]);
10837 if (commentList[i] != NULL) {
10841 if (i >= forwardMostMove) {
10845 fprintf(f, "%s\n", parseList[i]);
10850 if (commentList[i] != NULL) {
10851 fprintf(f, "[%s]\n", commentList[i]);
10854 /* This isn't really the old style, but it's close enough */
10855 if (gameInfo.resultDetails != NULL &&
10856 gameInfo.resultDetails[0] != NULLCHAR) {
10857 fprintf(f, "%s (%s)\n\n", PGNResult(gameInfo.result),
10858 gameInfo.resultDetails);
10860 fprintf(f, "%s\n\n", PGNResult(gameInfo.result));
10867 /* Save the current game to open file f and close the file */
10869 SaveGame(f, dummy, dummy2)
10874 if (gameMode == EditPosition) EditPositionDone(TRUE);
10875 lastSavedGame = GameCheckSum(); // [HGM] save: remember ID of last saved game to prevent double saving
10876 if (appData.oldSaveStyle)
10877 return SaveGameOldStyle(f);
10879 return SaveGamePGN(f);
10882 /* Save the current position to the given file */
10884 SavePositionToFile(filename)
10890 if (strcmp(filename, "-") == 0) {
10891 return SavePosition(stdout, 0, NULL);
10893 f = fopen(filename, "a");
10895 snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
10896 DisplayError(buf, errno);
10899 SavePosition(f, 0, NULL);
10905 /* Save the current position to the given open file and close the file */
10907 SavePosition(f, dummy, dummy2)
10915 if (gameMode == EditPosition) EditPositionDone(TRUE);
10916 if (appData.oldSaveStyle) {
10917 tm = time((time_t *) NULL);
10919 fprintf(f, "# %s position file -- %s", programName, ctime(&tm));
10921 fprintf(f, "[--------------\n");
10922 PrintPosition(f, currentMove);
10923 fprintf(f, "--------------]\n");
10925 fen = PositionToFEN(currentMove, NULL);
10926 fprintf(f, "%s\n", fen);
10934 ReloadCmailMsgEvent(unregister)
10938 static char *inFilename = NULL;
10939 static char *outFilename;
10941 struct stat inbuf, outbuf;
10944 /* Any registered moves are unregistered if unregister is set, */
10945 /* i.e. invoked by the signal handler */
10947 for (i = 0; i < CMAIL_MAX_GAMES; i ++) {
10948 cmailMoveRegistered[i] = FALSE;
10949 if (cmailCommentList[i] != NULL) {
10950 free(cmailCommentList[i]);
10951 cmailCommentList[i] = NULL;
10954 nCmailMovesRegistered = 0;
10957 for (i = 0; i < CMAIL_MAX_GAMES; i ++) {
10958 cmailResult[i] = CMAIL_NOT_RESULT;
10962 if (inFilename == NULL) {
10963 /* Because the filenames are static they only get malloced once */
10964 /* and they never get freed */
10965 inFilename = (char *) malloc(strlen(appData.cmailGameName) + 9);
10966 sprintf(inFilename, "%s.game.in", appData.cmailGameName);
10968 outFilename = (char *) malloc(strlen(appData.cmailGameName) + 5);
10969 sprintf(outFilename, "%s.out", appData.cmailGameName);
10972 status = stat(outFilename, &outbuf);
10974 cmailMailedMove = FALSE;
10976 status = stat(inFilename, &inbuf);
10977 cmailMailedMove = (inbuf.st_mtime < outbuf.st_mtime);
10980 /* LoadGameFromFile(CMAIL_MAX_GAMES) with cmailMsgLoaded == TRUE
10981 counts the games, notes how each one terminated, etc.
10983 It would be nice to remove this kludge and instead gather all
10984 the information while building the game list. (And to keep it
10985 in the game list nodes instead of having a bunch of fixed-size
10986 parallel arrays.) Note this will require getting each game's
10987 termination from the PGN tags, as the game list builder does
10988 not process the game moves. --mann
10990 cmailMsgLoaded = TRUE;
10991 LoadGameFromFile(inFilename, CMAIL_MAX_GAMES, "", FALSE);
10993 /* Load first game in the file or popup game menu */
10994 LoadGameFromFile(inFilename, 0, appData.cmailGameName, TRUE);
10996 #endif /* !WIN32 */
11004 char string[MSG_SIZ];
11006 if ( cmailMailedMove
11007 || (cmailResult[lastLoadGameNumber - 1] == CMAIL_OLD_RESULT)) {
11008 return TRUE; /* Allow free viewing */
11011 /* Unregister move to ensure that we don't leave RegisterMove */
11012 /* with the move registered when the conditions for registering no */
11014 if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
11015 cmailMoveRegistered[lastLoadGameNumber - 1] = FALSE;
11016 nCmailMovesRegistered --;
11018 if (cmailCommentList[lastLoadGameNumber - 1] != NULL)
11020 free(cmailCommentList[lastLoadGameNumber - 1]);
11021 cmailCommentList[lastLoadGameNumber - 1] = NULL;
11025 if (cmailOldMove == -1) {
11026 DisplayError(_("You have edited the game history.\nUse Reload Same Game and make your move again."), 0);
11030 if (currentMove > cmailOldMove + 1) {
11031 DisplayError(_("You have entered too many moves.\nBack up to the correct position and try again."), 0);
11035 if (currentMove < cmailOldMove) {
11036 DisplayError(_("Displayed position is not current.\nStep forward to the correct position and try again."), 0);
11040 if (forwardMostMove > currentMove) {
11041 /* Silently truncate extra moves */
11045 if ( (currentMove == cmailOldMove + 1)
11046 || ( (currentMove == cmailOldMove)
11047 && ( (cmailMoveType[lastLoadGameNumber - 1] == CMAIL_ACCEPT)
11048 || (cmailMoveType[lastLoadGameNumber - 1] == CMAIL_RESIGN)))) {
11049 if (gameInfo.result != GameUnfinished) {
11050 cmailResult[lastLoadGameNumber - 1] = CMAIL_NEW_RESULT;
11053 if (commentList[currentMove] != NULL) {
11054 cmailCommentList[lastLoadGameNumber - 1]
11055 = StrSave(commentList[currentMove]);
11057 strcpy(cmailMove[lastLoadGameNumber - 1], moveList[currentMove - 1]);
11059 if (appData.debugMode)
11060 fprintf(debugFP, "Saving %s for game %d\n",
11061 cmailMove[lastLoadGameNumber - 1], lastLoadGameNumber);
11064 "%s.game.out.%d", appData.cmailGameName, lastLoadGameNumber);
11066 f = fopen(string, "w");
11067 if (appData.oldSaveStyle) {
11068 SaveGameOldStyle(f); /* also closes the file */
11070 sprintf(string, "%s.pos.out", appData.cmailGameName);
11071 f = fopen(string, "w");
11072 SavePosition(f, 0, NULL); /* also closes the file */
11074 fprintf(f, "{--------------\n");
11075 PrintPosition(f, currentMove);
11076 fprintf(f, "--------------}\n\n");
11078 SaveGame(f, 0, NULL); /* also closes the file*/
11081 cmailMoveRegistered[lastLoadGameNumber - 1] = TRUE;
11082 nCmailMovesRegistered ++;
11083 } else if (nCmailGames == 1) {
11084 DisplayError(_("You have not made a move yet"), 0);
11095 static char *partCommandString = "cmail -xv%s -remail -game %s 2>&1";
11096 FILE *commandOutput;
11097 char buffer[MSG_SIZ], msg[MSG_SIZ], string[MSG_SIZ];
11098 int nBytes = 0; /* Suppress warnings on uninitialized variables */
11104 if (! cmailMsgLoaded) {
11105 DisplayError(_("The cmail message is not loaded.\nUse Reload CMail Message and make your move again."), 0);
11109 if (nCmailGames == nCmailResults) {
11110 DisplayError(_("No unfinished games"), 0);
11114 #if CMAIL_PROHIBIT_REMAIL
11115 if (cmailMailedMove) {
11116 sprintf(msg, _("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);
11117 DisplayError(msg, 0);
11122 if (! (cmailMailedMove || RegisterMove())) return;
11124 if ( cmailMailedMove
11125 || (nCmailMovesRegistered + nCmailResults == nCmailGames)) {
11126 sprintf(string, partCommandString,
11127 appData.debugMode ? " -v" : "", appData.cmailGameName);
11128 commandOutput = popen(string, "r");
11130 if (commandOutput == NULL) {
11131 DisplayError(_("Failed to invoke cmail"), 0);
11133 for (nBuffers = 0; (! feof(commandOutput)); nBuffers ++) {
11134 nBytes = fread(buffer, 1, MSG_SIZ - 1, commandOutput);
11136 if (nBuffers > 1) {
11137 (void) memcpy(msg, buffer + nBytes, MSG_SIZ - nBytes - 1);
11138 (void) memcpy(msg + MSG_SIZ - nBytes - 1, buffer, nBytes);
11139 nBytes = MSG_SIZ - 1;
11141 (void) memcpy(msg, buffer, nBytes);
11143 *(msg + nBytes) = '\0'; /* \0 for end-of-string*/
11145 if(StrStr(msg, "Mailed cmail message to ") != NULL) {
11146 cmailMailedMove = TRUE; /* Prevent >1 moves */
11149 for (i = 0; i < nCmailGames; i ++) {
11150 if (cmailResult[i] == CMAIL_NOT_RESULT) {
11155 && ( (arcDir = (char *) getenv("CMAIL_ARCDIR"))
11157 sprintf(buffer, "%s/%s.%s.archive",
11159 appData.cmailGameName,
11161 LoadGameFromFile(buffer, 1, buffer, FALSE);
11162 cmailMsgLoaded = FALSE;
11166 DisplayInformation(msg);
11167 pclose(commandOutput);
11170 if ((*cmailMsg) != '\0') {
11171 DisplayInformation(cmailMsg);
11176 #endif /* !WIN32 */
11185 int prependComma = 0;
11187 char string[MSG_SIZ]; /* Space for game-list */
11190 if (!cmailMsgLoaded) return "";
11192 if (cmailMailedMove) {
11193 sprintf(cmailMsg, _("Waiting for reply from opponent\n"));
11195 /* Create a list of games left */
11196 sprintf(string, "[");
11197 for (i = 0; i < nCmailGames; i ++) {
11198 if (! ( cmailMoveRegistered[i]
11199 || (cmailResult[i] == CMAIL_OLD_RESULT))) {
11200 if (prependComma) {
11201 sprintf(number, ",%d", i + 1);
11203 sprintf(number, "%d", i + 1);
11207 strcat(string, number);
11210 strcat(string, "]");
11212 if (nCmailMovesRegistered + nCmailResults == 0) {
11213 switch (nCmailGames) {
11216 _("Still need to make move for game\n"));
11221 _("Still need to make moves for both games\n"));
11226 _("Still need to make moves for all %d games\n"),
11231 switch (nCmailGames - nCmailMovesRegistered - nCmailResults) {
11234 _("Still need to make a move for game %s\n"),
11239 if (nCmailResults == nCmailGames) {
11240 sprintf(cmailMsg, _("No unfinished games\n"));
11242 sprintf(cmailMsg, _("Ready to send mail\n"));
11248 _("Still need to make moves for games %s\n"),
11260 if (gameMode == Training)
11261 SetTrainingModeOff();
11264 cmailMsgLoaded = FALSE;
11265 if (appData.icsActive) {
11266 SendToICS(ics_prefix);
11267 SendToICS("refresh\n");
11277 /* Give up on clean exit */
11281 /* Keep trying for clean exit */
11285 if (appData.icsActive && appData.colorize) Colorize(ColorNone, FALSE);
11287 if (telnetISR != NULL) {
11288 RemoveInputSource(telnetISR);
11290 if (icsPR != NoProc) {
11291 DestroyChildProcess(icsPR, TRUE);
11294 /* [HGM] crash: leave writing PGN and position entirely to GameEnds() */
11295 GameEnds(gameInfo.result, gameInfo.resultDetails==NULL ? "xboard exit" : gameInfo.resultDetails, GE_PLAYER);
11297 /* [HGM] crash: the above GameEnds() is a dud if another one was running */
11298 /* make sure this other one finishes before killing it! */
11299 if(endingGame) { int count = 0;
11300 if(appData.debugMode) fprintf(debugFP, "ExitEvent() during GameEnds(), wait\n");
11301 while(endingGame && count++ < 10) DoSleep(1);
11302 if(appData.debugMode && endingGame) fprintf(debugFP, "GameEnds() seems stuck, proceed exiting\n");
11305 /* Kill off chess programs */
11306 if (first.pr != NoProc) {
11309 DoSleep( appData.delayBeforeQuit );
11310 SendToProgram("quit\n", &first);
11311 DoSleep( appData.delayAfterQuit );
11312 DestroyChildProcess(first.pr, 10 /* [AS] first.useSigterm */ );
11314 if (second.pr != NoProc) {
11315 DoSleep( appData.delayBeforeQuit );
11316 SendToProgram("quit\n", &second);
11317 DoSleep( appData.delayAfterQuit );
11318 DestroyChildProcess(second.pr, 10 /* [AS] second.useSigterm */ );
11320 if (first.isr != NULL) {
11321 RemoveInputSource(first.isr);
11323 if (second.isr != NULL) {
11324 RemoveInputSource(second.isr);
11327 ShutDownFrontEnd();
11334 if (appData.debugMode)
11335 fprintf(debugFP, "PauseEvent(): pausing %d\n", pausing);
11339 if (gameMode == MachinePlaysWhite ||
11340 gameMode == MachinePlaysBlack) {
11343 DisplayBothClocks();
11345 if (gameMode == PlayFromGameFile) {
11346 if (appData.timeDelay >= 0)
11347 AutoPlayGameLoop();
11348 } else if (gameMode == IcsExamining && pauseExamInvalid) {
11349 Reset(FALSE, TRUE);
11350 SendToICS(ics_prefix);
11351 SendToICS("refresh\n");
11352 } else if (currentMove < forwardMostMove) {
11353 ForwardInner(forwardMostMove);
11355 pauseExamInvalid = FALSE;
11357 switch (gameMode) {
11361 pauseExamForwardMostMove = forwardMostMove;
11362 pauseExamInvalid = FALSE;
11365 case IcsPlayingWhite:
11366 case IcsPlayingBlack:
11370 case PlayFromGameFile:
11371 (void) StopLoadGameTimer();
11375 case BeginningOfGame:
11376 if (appData.icsActive) return;
11377 /* else fall through */
11378 case MachinePlaysWhite:
11379 case MachinePlaysBlack:
11380 case TwoMachinesPlay:
11381 if (forwardMostMove == 0)
11382 return; /* don't pause if no one has moved */
11383 if ((gameMode == MachinePlaysWhite &&
11384 !WhiteOnMove(forwardMostMove)) ||
11385 (gameMode == MachinePlaysBlack &&
11386 WhiteOnMove(forwardMostMove))) {
11399 char title[MSG_SIZ];
11401 if (currentMove < 1 || parseList[currentMove - 1][0] == NULLCHAR) {
11402 strcpy(title, _("Edit comment"));
11404 sprintf(title, _("Edit comment on %d.%s%s"), (currentMove - 1) / 2 + 1,
11405 WhiteOnMove(currentMove - 1) ? " " : ".. ",
11406 parseList[currentMove - 1]);
11409 EditCommentPopUp(currentMove, title, commentList[currentMove]);
11416 char *tags = PGNTags(&gameInfo);
11417 EditTagsPopUp(tags);
11424 if (appData.noChessProgram || gameMode == AnalyzeMode)
11427 if (gameMode != AnalyzeFile) {
11428 if (!appData.icsEngineAnalyze) {
11430 if (gameMode != EditGame) return;
11432 ResurrectChessProgram();
11433 SendToProgram("analyze\n", &first);
11434 first.analyzing = TRUE;
11435 /*first.maybeThinking = TRUE;*/
11436 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
11437 EngineOutputPopUp();
11439 if (!appData.icsEngineAnalyze) gameMode = AnalyzeMode;
11444 StartAnalysisClock();
11445 GetTimeMark(&lastNodeCountTime);
11452 if (appData.noChessProgram || gameMode == AnalyzeFile)
11455 if (gameMode != AnalyzeMode) {
11457 if (gameMode != EditGame) return;
11458 ResurrectChessProgram();
11459 SendToProgram("analyze\n", &first);
11460 first.analyzing = TRUE;
11461 /*first.maybeThinking = TRUE;*/
11462 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
11463 EngineOutputPopUp();
11465 gameMode = AnalyzeFile;
11470 StartAnalysisClock();
11471 GetTimeMark(&lastNodeCountTime);
11476 MachineWhiteEvent()
11479 char *bookHit = NULL;
11481 if (appData.noChessProgram || (gameMode == MachinePlaysWhite))
11485 if (gameMode == PlayFromGameFile ||
11486 gameMode == TwoMachinesPlay ||
11487 gameMode == Training ||
11488 gameMode == AnalyzeMode ||
11489 gameMode == EndOfGame)
11492 if (gameMode == EditPosition)
11493 EditPositionDone(TRUE);
11495 if (!WhiteOnMove(currentMove)) {
11496 DisplayError(_("It is not White's turn"), 0);
11500 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile)
11503 if (gameMode == EditGame || gameMode == AnalyzeMode ||
11504 gameMode == AnalyzeFile)
11507 ResurrectChessProgram(); /* in case it isn't running */
11508 if(gameMode == BeginningOfGame) { /* [HGM] time odds: to get right odds in human mode */
11509 gameMode = MachinePlaysWhite;
11512 gameMode = MachinePlaysWhite;
11516 sprintf(buf, "%s vs. %s", gameInfo.white, gameInfo.black);
11518 if (first.sendName) {
11519 sprintf(buf, "name %s\n", gameInfo.black);
11520 SendToProgram(buf, &first);
11522 if (first.sendTime) {
11523 if (first.useColors) {
11524 SendToProgram("black\n", &first); /*gnu kludge*/
11526 SendTimeRemaining(&first, TRUE);
11528 if (first.useColors) {
11529 SendToProgram("white\n", &first); // [HGM] book: send 'go' separately
11531 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: send go or retrieve book move
11532 SetMachineThinkingEnables();
11533 first.maybeThinking = TRUE;
11537 if (appData.autoFlipView && !flipView) {
11538 flipView = !flipView;
11539 DrawPosition(FALSE, NULL);
11540 DisplayBothClocks(); // [HGM] logo: clocks might have to be exchanged;
11543 if(bookHit) { // [HGM] book: simulate book reply
11544 static char bookMove[MSG_SIZ]; // a bit generous?
11546 programStats.nodes = programStats.depth = programStats.time =
11547 programStats.score = programStats.got_only_move = 0;
11548 sprintf(programStats.movelist, "%s (xbook)", bookHit);
11550 strcpy(bookMove, "move ");
11551 strcat(bookMove, bookHit);
11552 HandleMachineMove(bookMove, &first);
11557 MachineBlackEvent()
11560 char *bookHit = NULL;
11562 if (appData.noChessProgram || (gameMode == MachinePlaysBlack))
11566 if (gameMode == PlayFromGameFile ||
11567 gameMode == TwoMachinesPlay ||
11568 gameMode == Training ||
11569 gameMode == AnalyzeMode ||
11570 gameMode == EndOfGame)
11573 if (gameMode == EditPosition)
11574 EditPositionDone(TRUE);
11576 if (WhiteOnMove(currentMove)) {
11577 DisplayError(_("It is not Black's turn"), 0);
11581 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile)
11584 if (gameMode == EditGame || gameMode == AnalyzeMode ||
11585 gameMode == AnalyzeFile)
11588 ResurrectChessProgram(); /* in case it isn't running */
11589 gameMode = MachinePlaysBlack;
11593 sprintf(buf, "%s vs. %s", gameInfo.white, gameInfo.black);
11595 if (first.sendName) {
11596 sprintf(buf, "name %s\n", gameInfo.white);
11597 SendToProgram(buf, &first);
11599 if (first.sendTime) {
11600 if (first.useColors) {
11601 SendToProgram("white\n", &first); /*gnu kludge*/
11603 SendTimeRemaining(&first, FALSE);
11605 if (first.useColors) {
11606 SendToProgram("black\n", &first); // [HGM] book: 'go' sent separately
11608 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: send go or retrieve book move
11609 SetMachineThinkingEnables();
11610 first.maybeThinking = TRUE;
11613 if (appData.autoFlipView && flipView) {
11614 flipView = !flipView;
11615 DrawPosition(FALSE, NULL);
11616 DisplayBothClocks(); // [HGM] logo: clocks might have to be exchanged;
11618 if(bookHit) { // [HGM] book: simulate book reply
11619 static char bookMove[MSG_SIZ]; // a bit generous?
11621 programStats.nodes = programStats.depth = programStats.time =
11622 programStats.score = programStats.got_only_move = 0;
11623 sprintf(programStats.movelist, "%s (xbook)", bookHit);
11625 strcpy(bookMove, "move ");
11626 strcat(bookMove, bookHit);
11627 HandleMachineMove(bookMove, &first);
11633 DisplayTwoMachinesTitle()
11636 if (appData.matchGames > 0) {
11637 if (first.twoMachinesColor[0] == 'w') {
11638 sprintf(buf, "%s vs. %s (%d-%d-%d)",
11639 gameInfo.white, gameInfo.black,
11640 first.matchWins, second.matchWins,
11641 matchGame - 1 - (first.matchWins + second.matchWins));
11643 sprintf(buf, "%s vs. %s (%d-%d-%d)",
11644 gameInfo.white, gameInfo.black,
11645 second.matchWins, first.matchWins,
11646 matchGame - 1 - (first.matchWins + second.matchWins));
11649 sprintf(buf, "%s vs. %s", gameInfo.white, gameInfo.black);
11655 TwoMachinesEvent P((void))
11659 ChessProgramState *onmove;
11660 char *bookHit = NULL;
11662 if (appData.noChessProgram) return;
11664 switch (gameMode) {
11665 case TwoMachinesPlay:
11667 case MachinePlaysWhite:
11668 case MachinePlaysBlack:
11669 if (WhiteOnMove(forwardMostMove) == (gameMode == MachinePlaysWhite)) {
11670 DisplayError(_("Wait until your turn,\nor select Move Now"), 0);
11674 case BeginningOfGame:
11675 case PlayFromGameFile:
11678 if (gameMode != EditGame) return;
11681 EditPositionDone(TRUE);
11692 // forwardMostMove = currentMove;
11693 TruncateGame(); // [HGM] vari: MachineWhite and MachineBlack do this...
11694 ResurrectChessProgram(); /* in case first program isn't running */
11696 if (second.pr == NULL) {
11697 StartChessProgram(&second);
11698 if (second.protocolVersion == 1) {
11699 TwoMachinesEventIfReady();
11701 /* kludge: allow timeout for initial "feature" command */
11703 DisplayMessage("", _("Starting second chess program"));
11704 ScheduleDelayedEvent(TwoMachinesEventIfReady, FEATURE_TIMEOUT);
11708 DisplayMessage("", "");
11709 InitChessProgram(&second, FALSE);
11710 SendToProgram("force\n", &second);
11711 if (startedFromSetupPosition) {
11712 SendBoard(&second, backwardMostMove);
11713 if (appData.debugMode) {
11714 fprintf(debugFP, "Two Machines\n");
11717 for (i = backwardMostMove; i < forwardMostMove; i++) {
11718 SendMoveToProgram(i, &second);
11721 gameMode = TwoMachinesPlay;
11725 DisplayTwoMachinesTitle();
11727 if ((first.twoMachinesColor[0] == 'w') == WhiteOnMove(forwardMostMove)) {
11733 SendToProgram(first.computerString, &first);
11734 if (first.sendName) {
11735 sprintf(buf, "name %s\n", second.tidy);
11736 SendToProgram(buf, &first);
11738 SendToProgram(second.computerString, &second);
11739 if (second.sendName) {
11740 sprintf(buf, "name %s\n", first.tidy);
11741 SendToProgram(buf, &second);
11745 if (!first.sendTime || !second.sendTime) {
11746 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
11747 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
11749 if (onmove->sendTime) {
11750 if (onmove->useColors) {
11751 SendToProgram(onmove->other->twoMachinesColor, onmove); /*gnu kludge*/
11753 SendTimeRemaining(onmove, WhiteOnMove(forwardMostMove));
11755 if (onmove->useColors) {
11756 SendToProgram(onmove->twoMachinesColor, onmove);
11758 bookHit = SendMoveToBookUser(forwardMostMove-1, onmove, TRUE); // [HGM] book: send go or retrieve book move
11759 // SendToProgram("go\n", onmove);
11760 onmove->maybeThinking = TRUE;
11761 SetMachineThinkingEnables();
11765 if(bookHit) { // [HGM] book: simulate book reply
11766 static char bookMove[MSG_SIZ]; // a bit generous?
11768 programStats.nodes = programStats.depth = programStats.time =
11769 programStats.score = programStats.got_only_move = 0;
11770 sprintf(programStats.movelist, "%s (xbook)", bookHit);
11772 strcpy(bookMove, "move ");
11773 strcat(bookMove, bookHit);
11774 savedMessage = bookMove; // args for deferred call
11775 savedState = onmove;
11776 ScheduleDelayedEvent(DeferredBookMove, 1);
11783 if (gameMode == Training) {
11784 SetTrainingModeOff();
11785 gameMode = PlayFromGameFile;
11786 DisplayMessage("", _("Training mode off"));
11788 gameMode = Training;
11789 animateTraining = appData.animate;
11791 /* make sure we are not already at the end of the game */
11792 if (currentMove < forwardMostMove) {
11793 SetTrainingModeOn();
11794 DisplayMessage("", _("Training mode on"));
11796 gameMode = PlayFromGameFile;
11797 DisplayError(_("Already at end of game"), 0);
11806 if (!appData.icsActive) return;
11807 switch (gameMode) {
11808 case IcsPlayingWhite:
11809 case IcsPlayingBlack:
11812 case BeginningOfGame:
11820 EditPositionDone(TRUE);
11833 gameMode = IcsIdle;
11844 switch (gameMode) {
11846 SetTrainingModeOff();
11848 case MachinePlaysWhite:
11849 case MachinePlaysBlack:
11850 case BeginningOfGame:
11851 SendToProgram("force\n", &first);
11852 SetUserThinkingEnables();
11854 case PlayFromGameFile:
11855 (void) StopLoadGameTimer();
11856 if (gameFileFP != NULL) {
11861 EditPositionDone(TRUE);
11866 SendToProgram("force\n", &first);
11868 case TwoMachinesPlay:
11869 GameEnds((ChessMove) 0, NULL, GE_PLAYER);
11870 ResurrectChessProgram();
11871 SetUserThinkingEnables();
11874 ResurrectChessProgram();
11876 case IcsPlayingBlack:
11877 case IcsPlayingWhite:
11878 DisplayError(_("Warning: You are still playing a game"), 0);
11881 DisplayError(_("Warning: You are still observing a game"), 0);
11884 DisplayError(_("Warning: You are still examining a game"), 0);
11895 first.offeredDraw = second.offeredDraw = 0;
11897 if (gameMode == PlayFromGameFile) {
11898 whiteTimeRemaining = timeRemaining[0][currentMove];
11899 blackTimeRemaining = timeRemaining[1][currentMove];
11903 if (gameMode == MachinePlaysWhite ||
11904 gameMode == MachinePlaysBlack ||
11905 gameMode == TwoMachinesPlay ||
11906 gameMode == EndOfGame) {
11907 i = forwardMostMove;
11908 while (i > currentMove) {
11909 SendToProgram("undo\n", &first);
11912 whiteTimeRemaining = timeRemaining[0][currentMove];
11913 blackTimeRemaining = timeRemaining[1][currentMove];
11914 DisplayBothClocks();
11915 if (whiteFlag || blackFlag) {
11916 whiteFlag = blackFlag = 0;
11921 gameMode = EditGame;
11928 EditPositionEvent()
11930 if (gameMode == EditPosition) {
11936 if (gameMode != EditGame) return;
11938 gameMode = EditPosition;
11941 if (currentMove > 0)
11942 CopyBoard(boards[0], boards[currentMove]);
11944 blackPlaysFirst = !WhiteOnMove(currentMove);
11946 currentMove = forwardMostMove = backwardMostMove = 0;
11947 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
11954 /* [DM] icsEngineAnalyze - possible call from other functions */
11955 if (appData.icsEngineAnalyze) {
11956 appData.icsEngineAnalyze = FALSE;
11958 DisplayMessage("",_("Close ICS engine analyze..."));
11960 if (first.analysisSupport && first.analyzing) {
11961 SendToProgram("exit\n", &first);
11962 first.analyzing = FALSE;
11964 thinkOutput[0] = NULLCHAR;
11968 EditPositionDone(Boolean fakeRights)
11970 int king = gameInfo.variant == VariantKnightmate ? WhiteUnicorn : WhiteKing;
11972 startedFromSetupPosition = TRUE;
11973 InitChessProgram(&first, FALSE);
11974 if(fakeRights) { // [HGM] suppress this if we just pasted a FEN.
11975 boards[0][EP_STATUS] = EP_NONE;
11976 boards[0][CASTLING][2] = boards[0][CASTLING][5] = BOARD_WIDTH>>1;
11977 if(boards[0][0][BOARD_WIDTH>>1] == king) {
11978 boards[0][CASTLING][1] = boards[0][0][BOARD_LEFT] == WhiteRook ? 0 : NoRights;
11979 boards[0][CASTLING][0] = boards[0][0][BOARD_RGHT-1] == WhiteRook ? BOARD_RGHT-1 : NoRights;
11980 } else boards[0][CASTLING][2] = NoRights;
11981 if(boards[0][BOARD_HEIGHT-1][BOARD_WIDTH>>1] == WHITE_TO_BLACK king) {
11982 boards[0][CASTLING][4] = boards[0][BOARD_HEIGHT-1][BOARD_LEFT] == BlackRook ? 0 : NoRights;
11983 boards[0][CASTLING][3] = boards[0][BOARD_HEIGHT-1][BOARD_RGHT-1] == BlackRook ? BOARD_RGHT-1 : NoRights;
11984 } else boards[0][CASTLING][5] = NoRights;
11986 SendToProgram("force\n", &first);
11987 if (blackPlaysFirst) {
11988 strcpy(moveList[0], "");
11989 strcpy(parseList[0], "");
11990 currentMove = forwardMostMove = backwardMostMove = 1;
11991 CopyBoard(boards[1], boards[0]);
11993 currentMove = forwardMostMove = backwardMostMove = 0;
11995 SendBoard(&first, forwardMostMove);
11996 if (appData.debugMode) {
11997 fprintf(debugFP, "EditPosDone\n");
12000 timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
12001 timeRemaining[1][forwardMostMove] = blackTimeRemaining;
12002 gameMode = EditGame;
12004 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
12005 ClearHighlights(); /* [AS] */
12008 /* Pause for `ms' milliseconds */
12009 /* !! Ugh, this is a kludge. Fix it sometime. --tpm */
12019 } while (SubtractTimeMarks(&m2, &m1) < ms);
12022 /* !! Ugh, this is a kludge. Fix it sometime. --tpm */
12024 SendMultiLineToICS(buf)
12027 char temp[MSG_SIZ+1], *p;
12034 strncpy(temp, buf, len);
12039 if (*p == '\n' || *p == '\r')
12044 strcat(temp, "\n");
12046 SendToPlayer(temp, strlen(temp));
12050 SetWhiteToPlayEvent()
12052 if (gameMode == EditPosition) {
12053 blackPlaysFirst = FALSE;
12054 DisplayBothClocks(); /* works because currentMove is 0 */
12055 } else if (gameMode == IcsExamining) {
12056 SendToICS(ics_prefix);
12057 SendToICS("tomove white\n");
12062 SetBlackToPlayEvent()
12064 if (gameMode == EditPosition) {
12065 blackPlaysFirst = TRUE;
12066 currentMove = 1; /* kludge */
12067 DisplayBothClocks();
12069 } else if (gameMode == IcsExamining) {
12070 SendToICS(ics_prefix);
12071 SendToICS("tomove black\n");
12076 EditPositionMenuEvent(selection, x, y)
12077 ChessSquare selection;
12081 ChessSquare piece = boards[0][y][x];
12083 if (gameMode != EditPosition && gameMode != IcsExamining) return;
12085 switch (selection) {
12087 if (gameMode == IcsExamining && ics_type == ICS_FICS) {
12088 SendToICS(ics_prefix);
12089 SendToICS("bsetup clear\n");
12090 } else if (gameMode == IcsExamining && ics_type == ICS_ICC) {
12091 SendToICS(ics_prefix);
12092 SendToICS("clearboard\n");
12094 for (x = 0; x < BOARD_WIDTH; x++) { ChessSquare p = EmptySquare;
12095 if(x == BOARD_LEFT-1 || x == BOARD_RGHT) p = (ChessSquare) 0; /* [HGM] holdings */
12096 for (y = 0; y < BOARD_HEIGHT; y++) {
12097 if (gameMode == IcsExamining) {
12098 if (boards[currentMove][y][x] != EmptySquare) {
12099 sprintf(buf, "%sx@%c%c\n", ics_prefix,
12104 boards[0][y][x] = p;
12109 if (gameMode == EditPosition) {
12110 DrawPosition(FALSE, boards[0]);
12115 SetWhiteToPlayEvent();
12119 SetBlackToPlayEvent();
12123 if (gameMode == IcsExamining) {
12124 if (x < BOARD_LEFT || x >= BOARD_RGHT) break; // [HGM] holdings
12125 sprintf(buf, "%sx@%c%c\n", ics_prefix, AAA + x, ONE + y);
12128 if(x < BOARD_LEFT || x >= BOARD_RGHT) {
12129 if(x == BOARD_LEFT-2) {
12130 if(y < BOARD_HEIGHT-1-gameInfo.holdingsSize) break;
12131 boards[0][y][1] = 0;
12133 if(x == BOARD_RGHT+1) {
12134 if(y >= gameInfo.holdingsSize) break;
12135 boards[0][y][BOARD_WIDTH-2] = 0;
12138 boards[0][y][x] = EmptySquare;
12139 DrawPosition(FALSE, boards[0]);
12144 if(piece >= (int)WhitePawn && piece < (int)WhiteMan ||
12145 piece >= (int)BlackPawn && piece < (int)BlackMan ) {
12146 selection = (ChessSquare) (PROMOTED piece);
12147 } else if(piece == EmptySquare) selection = WhiteSilver;
12148 else selection = (ChessSquare)((int)piece - 1);
12152 if(piece > (int)WhiteMan && piece <= (int)WhiteKing ||
12153 piece > (int)BlackMan && piece <= (int)BlackKing ) {
12154 selection = (ChessSquare) (DEMOTED piece);
12155 } else if(piece == EmptySquare) selection = BlackSilver;
12156 else selection = (ChessSquare)((int)piece + 1);
12161 if(gameInfo.variant == VariantShatranj ||
12162 gameInfo.variant == VariantXiangqi ||
12163 gameInfo.variant == VariantCourier ||
12164 gameInfo.variant == VariantMakruk )
12165 selection = (ChessSquare)((int)selection - (int)WhiteQueen + (int)WhiteFerz);
12170 if(gameInfo.variant == VariantXiangqi)
12171 selection = (ChessSquare)((int)selection - (int)WhiteKing + (int)WhiteWazir);
12172 if(gameInfo.variant == VariantKnightmate)
12173 selection = (ChessSquare)((int)selection - (int)WhiteKing + (int)WhiteUnicorn);
12176 if (gameMode == IcsExamining) {
12177 if (x < BOARD_LEFT || x >= BOARD_RGHT) break; // [HGM] holdings
12178 sprintf(buf, "%s%c@%c%c\n", ics_prefix,
12179 PieceToChar(selection), AAA + x, ONE + y);
12182 if(x < BOARD_LEFT || x >= BOARD_RGHT) {
12184 if(x == BOARD_LEFT-2 && selection >= BlackPawn) {
12185 n = PieceToNumber(selection - BlackPawn);
12186 if(n >= gameInfo.holdingsSize) { n = 0; selection = BlackPawn; }
12187 boards[0][BOARD_HEIGHT-1-n][0] = selection;
12188 boards[0][BOARD_HEIGHT-1-n][1]++;
12190 if(x == BOARD_RGHT+1 && selection < BlackPawn) {
12191 n = PieceToNumber(selection);
12192 if(n >= gameInfo.holdingsSize) { n = 0; selection = WhitePawn; }
12193 boards[0][n][BOARD_WIDTH-1] = selection;
12194 boards[0][n][BOARD_WIDTH-2]++;
12197 boards[0][y][x] = selection;
12198 DrawPosition(TRUE, boards[0]);
12206 DropMenuEvent(selection, x, y)
12207 ChessSquare selection;
12210 ChessMove moveType;
12212 switch (gameMode) {
12213 case IcsPlayingWhite:
12214 case MachinePlaysBlack:
12215 if (!WhiteOnMove(currentMove)) {
12216 DisplayMoveError(_("It is Black's turn"));
12219 moveType = WhiteDrop;
12221 case IcsPlayingBlack:
12222 case MachinePlaysWhite:
12223 if (WhiteOnMove(currentMove)) {
12224 DisplayMoveError(_("It is White's turn"));
12227 moveType = BlackDrop;
12230 moveType = WhiteOnMove(currentMove) ? WhiteDrop : BlackDrop;
12236 if (moveType == BlackDrop && selection < BlackPawn) {
12237 selection = (ChessSquare) ((int) selection
12238 + (int) BlackPawn - (int) WhitePawn);
12240 if (boards[currentMove][y][x] != EmptySquare) {
12241 DisplayMoveError(_("That square is occupied"));
12245 FinishMove(moveType, (int) selection, DROP_RANK, x, y, NULLCHAR);
12251 /* Accept a pending offer of any kind from opponent */
12253 if (appData.icsActive) {
12254 SendToICS(ics_prefix);
12255 SendToICS("accept\n");
12256 } else if (cmailMsgLoaded) {
12257 if (currentMove == cmailOldMove &&
12258 commentList[cmailOldMove] != NULL &&
12259 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
12260 "Black offers a draw" : "White offers a draw")) {
12262 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
12263 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_ACCEPT;
12265 DisplayError(_("There is no pending offer on this move"), 0);
12266 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
12269 /* Not used for offers from chess program */
12276 /* Decline a pending offer of any kind from opponent */
12278 if (appData.icsActive) {
12279 SendToICS(ics_prefix);
12280 SendToICS("decline\n");
12281 } else if (cmailMsgLoaded) {
12282 if (currentMove == cmailOldMove &&
12283 commentList[cmailOldMove] != NULL &&
12284 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
12285 "Black offers a draw" : "White offers a draw")) {
12287 AppendComment(cmailOldMove, "Draw declined", TRUE);
12288 DisplayComment(cmailOldMove - 1, "Draw declined");
12291 DisplayError(_("There is no pending offer on this move"), 0);
12294 /* Not used for offers from chess program */
12301 /* Issue ICS rematch command */
12302 if (appData.icsActive) {
12303 SendToICS(ics_prefix);
12304 SendToICS("rematch\n");
12311 /* Call your opponent's flag (claim a win on time) */
12312 if (appData.icsActive) {
12313 SendToICS(ics_prefix);
12314 SendToICS("flag\n");
12316 switch (gameMode) {
12319 case MachinePlaysWhite:
12322 GameEnds(GameIsDrawn, "Both players ran out of time",
12325 GameEnds(BlackWins, "Black wins on time", GE_PLAYER);
12327 DisplayError(_("Your opponent is not out of time"), 0);
12330 case MachinePlaysBlack:
12333 GameEnds(GameIsDrawn, "Both players ran out of time",
12336 GameEnds(WhiteWins, "White wins on time", GE_PLAYER);
12338 DisplayError(_("Your opponent is not out of time"), 0);
12348 /* Offer draw or accept pending draw offer from opponent */
12350 if (appData.icsActive) {
12351 /* Note: tournament rules require draw offers to be
12352 made after you make your move but before you punch
12353 your clock. Currently ICS doesn't let you do that;
12354 instead, you immediately punch your clock after making
12355 a move, but you can offer a draw at any time. */
12357 SendToICS(ics_prefix);
12358 SendToICS("draw\n");
12359 userOfferedDraw = TRUE; // [HGM] drawclaim: also set flag in ICS play
12360 } else if (cmailMsgLoaded) {
12361 if (currentMove == cmailOldMove &&
12362 commentList[cmailOldMove] != NULL &&
12363 StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
12364 "Black offers a draw" : "White offers a draw")) {
12365 GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
12366 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_ACCEPT;
12367 } else if (currentMove == cmailOldMove + 1) {
12368 char *offer = WhiteOnMove(cmailOldMove) ?
12369 "White offers a draw" : "Black offers a draw";
12370 AppendComment(currentMove, offer, TRUE);
12371 DisplayComment(currentMove - 1, offer);
12372 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_DRAW;
12374 DisplayError(_("You must make your move before offering a draw"), 0);
12375 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
12377 } else if (first.offeredDraw) {
12378 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
12380 if (first.sendDrawOffers) {
12381 SendToProgram("draw\n", &first);
12382 userOfferedDraw = TRUE;
12390 /* Offer Adjourn or accept pending Adjourn offer from opponent */
12392 if (appData.icsActive) {
12393 SendToICS(ics_prefix);
12394 SendToICS("adjourn\n");
12396 /* Currently GNU Chess doesn't offer or accept Adjourns */
12404 /* Offer Abort or accept pending Abort offer from opponent */
12406 if (appData.icsActive) {
12407 SendToICS(ics_prefix);
12408 SendToICS("abort\n");
12410 GameEnds(GameUnfinished, "Game aborted", GE_PLAYER);
12417 /* Resign. You can do this even if it's not your turn. */
12419 if (appData.icsActive) {
12420 SendToICS(ics_prefix);
12421 SendToICS("resign\n");
12423 switch (gameMode) {
12424 case MachinePlaysWhite:
12425 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
12427 case MachinePlaysBlack:
12428 GameEnds(BlackWins, "White resigns", GE_PLAYER);
12431 if (cmailMsgLoaded) {
12433 if (WhiteOnMove(cmailOldMove)) {
12434 GameEnds(BlackWins, "White resigns", GE_PLAYER);
12436 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
12438 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_RESIGN;
12449 StopObservingEvent()
12451 /* Stop observing current games */
12452 SendToICS(ics_prefix);
12453 SendToICS("unobserve\n");
12457 StopExaminingEvent()
12459 /* Stop observing current game */
12460 SendToICS(ics_prefix);
12461 SendToICS("unexamine\n");
12465 ForwardInner(target)
12470 if (appData.debugMode)
12471 fprintf(debugFP, "ForwardInner(%d), current %d, forward %d\n",
12472 target, currentMove, forwardMostMove);
12474 if (gameMode == EditPosition)
12477 if (gameMode == PlayFromGameFile && !pausing)
12480 if (gameMode == IcsExamining && pausing)
12481 limit = pauseExamForwardMostMove;
12483 limit = forwardMostMove;
12485 if (target > limit) target = limit;
12487 if (target > 0 && moveList[target - 1][0]) {
12488 int fromX, fromY, toX, toY;
12489 toX = moveList[target - 1][2] - AAA;
12490 toY = moveList[target - 1][3] - ONE;
12491 if (moveList[target - 1][1] == '@') {
12492 if (appData.highlightLastMove) {
12493 SetHighlights(-1, -1, toX, toY);
12496 fromX = moveList[target - 1][0] - AAA;
12497 fromY = moveList[target - 1][1] - ONE;
12498 if (target == currentMove + 1) {
12499 AnimateMove(boards[currentMove], fromX, fromY, toX, toY);
12501 if (appData.highlightLastMove) {
12502 SetHighlights(fromX, fromY, toX, toY);
12506 if (gameMode == EditGame || gameMode == AnalyzeMode ||
12507 gameMode == Training || gameMode == PlayFromGameFile ||
12508 gameMode == AnalyzeFile) {
12509 while (currentMove < target) {
12510 SendMoveToProgram(currentMove++, &first);
12513 currentMove = target;
12516 if (gameMode == EditGame || gameMode == EndOfGame) {
12517 whiteTimeRemaining = timeRemaining[0][currentMove];
12518 blackTimeRemaining = timeRemaining[1][currentMove];
12520 DisplayBothClocks();
12521 DisplayMove(currentMove - 1);
12522 DrawPosition(FALSE, boards[currentMove]);
12523 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
12524 if ( !matchMode && gameMode != Training) { // [HGM] PV info: routine tests if empty
12525 DisplayComment(currentMove - 1, commentList[currentMove]);
12533 if (gameMode == IcsExamining && !pausing) {
12534 SendToICS(ics_prefix);
12535 SendToICS("forward\n");
12537 ForwardInner(currentMove + 1);
12544 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
12545 /* to optimze, we temporarily turn off analysis mode while we feed
12546 * the remaining moves to the engine. Otherwise we get analysis output
12549 if (first.analysisSupport) {
12550 SendToProgram("exit\nforce\n", &first);
12551 first.analyzing = FALSE;
12555 if (gameMode == IcsExamining && !pausing) {
12556 SendToICS(ics_prefix);
12557 SendToICS("forward 999999\n");
12559 ForwardInner(forwardMostMove);
12562 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
12563 /* we have fed all the moves, so reactivate analysis mode */
12564 SendToProgram("analyze\n", &first);
12565 first.analyzing = TRUE;
12566 /*first.maybeThinking = TRUE;*/
12567 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
12572 BackwardInner(target)
12575 int full_redraw = TRUE; /* [AS] Was FALSE, had to change it! */
12577 if (appData.debugMode)
12578 fprintf(debugFP, "BackwardInner(%d), current %d, forward %d\n",
12579 target, currentMove, forwardMostMove);
12581 if (gameMode == EditPosition) return;
12582 if (currentMove <= backwardMostMove) {
12584 DrawPosition(full_redraw, boards[currentMove]);
12587 if (gameMode == PlayFromGameFile && !pausing)
12590 if (moveList[target][0]) {
12591 int fromX, fromY, toX, toY;
12592 toX = moveList[target][2] - AAA;
12593 toY = moveList[target][3] - ONE;
12594 if (moveList[target][1] == '@') {
12595 if (appData.highlightLastMove) {
12596 SetHighlights(-1, -1, toX, toY);
12599 fromX = moveList[target][0] - AAA;
12600 fromY = moveList[target][1] - ONE;
12601 if (target == currentMove - 1) {
12602 AnimateMove(boards[currentMove], toX, toY, fromX, fromY);
12604 if (appData.highlightLastMove) {
12605 SetHighlights(fromX, fromY, toX, toY);
12609 if (gameMode == EditGame || gameMode==AnalyzeMode ||
12610 gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
12611 while (currentMove > target) {
12612 SendToProgram("undo\n", &first);
12616 currentMove = target;
12619 if (gameMode == EditGame || gameMode == EndOfGame) {
12620 whiteTimeRemaining = timeRemaining[0][currentMove];
12621 blackTimeRemaining = timeRemaining[1][currentMove];
12623 DisplayBothClocks();
12624 DisplayMove(currentMove - 1);
12625 DrawPosition(full_redraw, boards[currentMove]);
12626 HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
12627 // [HGM] PV info: routine tests if comment empty
12628 DisplayComment(currentMove - 1, commentList[currentMove]);
12634 if (gameMode == IcsExamining && !pausing) {
12635 SendToICS(ics_prefix);
12636 SendToICS("backward\n");
12638 BackwardInner(currentMove - 1);
12645 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
12646 /* to optimize, we temporarily turn off analysis mode while we undo
12647 * all the moves. Otherwise we get analysis output after each undo.
12649 if (first.analysisSupport) {
12650 SendToProgram("exit\nforce\n", &first);
12651 first.analyzing = FALSE;
12655 if (gameMode == IcsExamining && !pausing) {
12656 SendToICS(ics_prefix);
12657 SendToICS("backward 999999\n");
12659 BackwardInner(backwardMostMove);
12662 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
12663 /* we have fed all the moves, so reactivate analysis mode */
12664 SendToProgram("analyze\n", &first);
12665 first.analyzing = TRUE;
12666 /*first.maybeThinking = TRUE;*/
12667 first.maybeThinking = FALSE; /* avoid killing GNU Chess */
12674 if (gameMode == PlayFromGameFile && !pausing) PauseEvent();
12675 if (to >= forwardMostMove) to = forwardMostMove;
12676 if (to <= backwardMostMove) to = backwardMostMove;
12677 if (to < currentMove) {
12685 RevertEvent(Boolean annotate)
12687 if(PopTail(annotate)) { // [HGM] vari: restore old game tail
12690 if (gameMode != IcsExamining) {
12691 DisplayError(_("You are not examining a game"), 0);
12695 DisplayError(_("You can't revert while pausing"), 0);
12698 SendToICS(ics_prefix);
12699 SendToICS("revert\n");
12705 switch (gameMode) {
12706 case MachinePlaysWhite:
12707 case MachinePlaysBlack:
12708 if (WhiteOnMove(forwardMostMove) == (gameMode == MachinePlaysWhite)) {
12709 DisplayError(_("Wait until your turn,\nor select Move Now"), 0);
12712 if (forwardMostMove < 2) return;
12713 currentMove = forwardMostMove = forwardMostMove - 2;
12714 whiteTimeRemaining = timeRemaining[0][currentMove];
12715 blackTimeRemaining = timeRemaining[1][currentMove];
12716 DisplayBothClocks();
12717 DisplayMove(currentMove - 1);
12718 ClearHighlights();/*!! could figure this out*/
12719 DrawPosition(TRUE, boards[currentMove]); /* [AS] Changed to full redraw! */
12720 SendToProgram("remove\n", &first);
12721 /*first.maybeThinking = TRUE;*/ /* GNU Chess does not ponder here */
12724 case BeginningOfGame:
12728 case IcsPlayingWhite:
12729 case IcsPlayingBlack:
12730 if (WhiteOnMove(forwardMostMove) == (gameMode == IcsPlayingWhite)) {
12731 SendToICS(ics_prefix);
12732 SendToICS("takeback 2\n");
12734 SendToICS(ics_prefix);
12735 SendToICS("takeback 1\n");
12744 ChessProgramState *cps;
12746 switch (gameMode) {
12747 case MachinePlaysWhite:
12748 if (!WhiteOnMove(forwardMostMove)) {
12749 DisplayError(_("It is your turn"), 0);
12754 case MachinePlaysBlack:
12755 if (WhiteOnMove(forwardMostMove)) {
12756 DisplayError(_("It is your turn"), 0);
12761 case TwoMachinesPlay:
12762 if (WhiteOnMove(forwardMostMove) ==
12763 (first.twoMachinesColor[0] == 'w')) {
12769 case BeginningOfGame:
12773 SendToProgram("?\n", cps);
12777 TruncateGameEvent()
12780 if (gameMode != EditGame) return;
12787 CleanupTail(); // [HGM] vari: only keep current variation if we explicitly truncate
12788 if (forwardMostMove > currentMove) {
12789 if (gameInfo.resultDetails != NULL) {
12790 free(gameInfo.resultDetails);
12791 gameInfo.resultDetails = NULL;
12792 gameInfo.result = GameUnfinished;
12794 forwardMostMove = currentMove;
12795 HistorySet(parseList, backwardMostMove, forwardMostMove,
12803 if (appData.noChessProgram) return;
12804 switch (gameMode) {
12805 case MachinePlaysWhite:
12806 if (WhiteOnMove(forwardMostMove)) {
12807 DisplayError(_("Wait until your turn"), 0);
12811 case BeginningOfGame:
12812 case MachinePlaysBlack:
12813 if (!WhiteOnMove(forwardMostMove)) {
12814 DisplayError(_("Wait until your turn"), 0);
12819 DisplayError(_("No hint available"), 0);
12822 SendToProgram("hint\n", &first);
12823 hintRequested = TRUE;
12829 if (appData.noChessProgram) return;
12830 switch (gameMode) {
12831 case MachinePlaysWhite:
12832 if (WhiteOnMove(forwardMostMove)) {
12833 DisplayError(_("Wait until your turn"), 0);
12837 case BeginningOfGame:
12838 case MachinePlaysBlack:
12839 if (!WhiteOnMove(forwardMostMove)) {
12840 DisplayError(_("Wait until your turn"), 0);
12845 EditPositionDone(TRUE);
12847 case TwoMachinesPlay:
12852 SendToProgram("bk\n", &first);
12853 bookOutput[0] = NULLCHAR;
12854 bookRequested = TRUE;
12860 char *tags = PGNTags(&gameInfo);
12861 TagsPopUp(tags, CmailMsg());
12865 /* end button procedures */
12868 PrintPosition(fp, move)
12874 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
12875 for (j = BOARD_LEFT; j < BOARD_RGHT; j++) {
12876 char c = PieceToChar(boards[move][i][j]);
12877 fputc(c == 'x' ? '.' : c, fp);
12878 fputc(j == BOARD_RGHT - 1 ? '\n' : ' ', fp);
12881 if ((gameMode == EditPosition) ? !blackPlaysFirst : (move % 2 == 0))
12882 fprintf(fp, "white to play\n");
12884 fprintf(fp, "black to play\n");
12891 if (gameInfo.white != NULL) {
12892 fprintf(fp, "\t%s vs. %s\n", gameInfo.white, gameInfo.black);
12898 /* Find last component of program's own name, using some heuristics */
12900 TidyProgramName(prog, host, buf)
12901 char *prog, *host, buf[MSG_SIZ];
12904 int local = (strcmp(host, "localhost") == 0);
12905 while (!local && (p = strchr(prog, ';')) != NULL) {
12907 while (*p == ' ') p++;
12910 if (*prog == '"' || *prog == '\'') {
12911 q = strchr(prog + 1, *prog);
12913 q = strchr(prog, ' ');
12915 if (q == NULL) q = prog + strlen(prog);
12917 while (p >= prog && *p != '/' && *p != '\\') p--;
12919 if(p == prog && *p == '"') p++;
12920 if (q - p >= 4 && StrCaseCmp(q - 4, ".exe") == 0) q -= 4;
12921 memcpy(buf, p, q - p);
12922 buf[q - p] = NULLCHAR;
12930 TimeControlTagValue()
12933 if (!appData.clockMode) {
12935 } else if (movesPerSession > 0) {
12936 sprintf(buf, "%d/%ld", movesPerSession, timeControl/1000);
12937 } else if (timeIncrement == 0) {
12938 sprintf(buf, "%ld", timeControl/1000);
12940 sprintf(buf, "%ld+%ld", timeControl/1000, timeIncrement/1000);
12942 return StrSave(buf);
12948 /* This routine is used only for certain modes */
12949 VariantClass v = gameInfo.variant;
12950 ChessMove r = GameUnfinished;
12953 if(gameMode == EditGame) { // [HGM] vari: do not erase result on EditGame
12954 r = gameInfo.result;
12955 p = gameInfo.resultDetails;
12956 gameInfo.resultDetails = NULL;
12958 ClearGameInfo(&gameInfo);
12959 gameInfo.variant = v;
12961 switch (gameMode) {
12962 case MachinePlaysWhite:
12963 gameInfo.event = StrSave( appData.pgnEventHeader );
12964 gameInfo.site = StrSave(HostName());
12965 gameInfo.date = PGNDate();
12966 gameInfo.round = StrSave("-");
12967 gameInfo.white = StrSave(first.tidy);
12968 gameInfo.black = StrSave(UserName());
12969 gameInfo.timeControl = TimeControlTagValue();
12972 case MachinePlaysBlack:
12973 gameInfo.event = StrSave( appData.pgnEventHeader );
12974 gameInfo.site = StrSave(HostName());
12975 gameInfo.date = PGNDate();
12976 gameInfo.round = StrSave("-");
12977 gameInfo.white = StrSave(UserName());
12978 gameInfo.black = StrSave(first.tidy);
12979 gameInfo.timeControl = TimeControlTagValue();
12982 case TwoMachinesPlay:
12983 gameInfo.event = StrSave( appData.pgnEventHeader );
12984 gameInfo.site = StrSave(HostName());
12985 gameInfo.date = PGNDate();
12986 if (matchGame > 0) {
12988 sprintf(buf, "%d", matchGame);
12989 gameInfo.round = StrSave(buf);
12991 gameInfo.round = StrSave("-");
12993 if (first.twoMachinesColor[0] == 'w') {
12994 gameInfo.white = StrSave(first.tidy);
12995 gameInfo.black = StrSave(second.tidy);
12997 gameInfo.white = StrSave(second.tidy);
12998 gameInfo.black = StrSave(first.tidy);
13000 gameInfo.timeControl = TimeControlTagValue();
13004 gameInfo.event = StrSave("Edited game");
13005 gameInfo.site = StrSave(HostName());
13006 gameInfo.date = PGNDate();
13007 gameInfo.round = StrSave("-");
13008 gameInfo.white = StrSave("-");
13009 gameInfo.black = StrSave("-");
13010 gameInfo.result = r;
13011 gameInfo.resultDetails = p;
13015 gameInfo.event = StrSave("Edited position");
13016 gameInfo.site = StrSave(HostName());
13017 gameInfo.date = PGNDate();
13018 gameInfo.round = StrSave("-");
13019 gameInfo.white = StrSave("-");
13020 gameInfo.black = StrSave("-");
13023 case IcsPlayingWhite:
13024 case IcsPlayingBlack:
13029 case PlayFromGameFile:
13030 gameInfo.event = StrSave("Game from non-PGN file");
13031 gameInfo.site = StrSave(HostName());
13032 gameInfo.date = PGNDate();
13033 gameInfo.round = StrSave("-");
13034 gameInfo.white = StrSave("?");
13035 gameInfo.black = StrSave("?");
13044 ReplaceComment(index, text)
13050 while (*text == '\n') text++;
13051 len = strlen(text);
13052 while (len > 0 && text[len - 1] == '\n') len--;
13054 if (commentList[index] != NULL)
13055 free(commentList[index]);
13058 commentList[index] = NULL;
13061 if( *text == '{' && strchr(text, '}') || // [HGM] braces: if certainy malformed, put braces
13062 *text == '[' && strchr(text, ']') || // otherwise hope the user knows what he is doing
13063 *text == '(' && strchr(text, ')')) { // (perhaps check if this parses as comment-only?)
13064 commentList[index] = (char *) malloc(len + 2);
13065 strncpy(commentList[index], text, len);
13066 commentList[index][len] = '\n';
13067 commentList[index][len + 1] = NULLCHAR;
13069 // [HGM] braces: if text does not start with known OK delimiter, put braces around it.
13071 commentList[index] = (char *) malloc(len + 6);
13072 strcpy(commentList[index], "{\n");
13073 strncpy(commentList[index]+2, text, len);
13074 commentList[index][len+2] = NULLCHAR;
13075 while(p = strchr(commentList[index], '}')) *p = ')'; // kill all } to make it one comment
13076 strcat(commentList[index], "\n}\n");
13090 if (ch == '\r') continue;
13092 } while (ch != '\0');
13096 AppendComment(index, text, addBraces)
13099 Boolean addBraces; // [HGM] braces: tells if we should add {}
13104 if(appData.debugMode) fprintf(debugFP, "Append: in='%s' %d\n", text, addBraces); fflush(debugFP);
13105 text = GetInfoFromComment( index, text ); /* [HGM] PV time: strip PV info from comment */
13108 while (*text == '\n') text++;
13109 len = strlen(text);
13110 while (len > 0 && text[len - 1] == '\n') len--;
13112 if (len == 0) return;
13114 if (commentList[index] != NULL) {
13115 old = commentList[index];
13116 oldlen = strlen(old);
13117 while(commentList[index][oldlen-1] == '\n')
13118 commentList[index][--oldlen] = NULLCHAR;
13119 commentList[index] = (char *) malloc(oldlen + len + 6); // might waste 4
13120 strcpy(commentList[index], old);
13122 // [HGM] braces: join "{A\n}\n" + "{\nB}" as "{A\nB\n}"
13123 if(commentList[index][oldlen-1] == '}' && (text[0] == '{' || addBraces)) {
13124 if(addBraces) addBraces = FALSE; else { text++; len--; }
13125 while (*text == '\n') { text++; len--; }
13126 commentList[index][--oldlen] = NULLCHAR;
13128 if(addBraces) strcat(commentList[index], "\n{\n");
13129 else strcat(commentList[index], "\n");
13130 strcat(commentList[index], text);
13131 if(addBraces) strcat(commentList[index], "\n}\n");
13132 else strcat(commentList[index], "\n");
13134 commentList[index] = (char *) malloc(len + 6); // perhaps wastes 4...
13136 strcpy(commentList[index], "{\n");
13137 else commentList[index][0] = NULLCHAR;
13138 strcat(commentList[index], text);
13139 strcat(commentList[index], "\n");
13140 if(addBraces) strcat(commentList[index], "}\n");
13144 static char * FindStr( char * text, char * sub_text )
13146 char * result = strstr( text, sub_text );
13148 if( result != NULL ) {
13149 result += strlen( sub_text );
13155 /* [AS] Try to extract PV info from PGN comment */
13156 /* [HGM] PV time: and then remove it, to prevent it appearing twice */
13157 char *GetInfoFromComment( int index, char * text )
13161 if( text != NULL && index > 0 ) {
13164 int time = -1, sec = 0, deci;
13165 char * s_eval = FindStr( text, "[%eval " );
13166 char * s_emt = FindStr( text, "[%emt " );
13168 if( s_eval != NULL || s_emt != NULL ) {
13172 if( s_eval != NULL ) {
13173 if( sscanf( s_eval, "%d,%d%c", &score, &depth, &delim ) != 3 ) {
13177 if( delim != ']' ) {
13182 if( s_emt != NULL ) {
13187 /* We expect something like: [+|-]nnn.nn/dd */
13190 if(*text != '{') return text; // [HGM] braces: must be normal comment
13192 sep = strchr( text, '/' );
13193 if( sep == NULL || sep < (text+4) ) {
13197 time = -1; sec = -1; deci = -1;
13198 if( sscanf( text+1, "%d.%d/%d %d:%d", &score, &score_lo, &depth, &time, &sec ) != 5 &&
13199 sscanf( text+1, "%d.%d/%d %d.%d", &score, &score_lo, &depth, &time, &deci ) != 5 &&
13200 sscanf( text+1, "%d.%d/%d %d", &score, &score_lo, &depth, &time ) != 4 &&
13201 sscanf( text+1, "%d.%d/%d", &score, &score_lo, &depth ) != 3 ) {
13205 if( score_lo < 0 || score_lo >= 100 ) {
13209 if(sec >= 0) time = 600*time + 10*sec; else
13210 if(deci >= 0) time = 10*time + deci; else time *= 10; // deci-sec
13212 score = score >= 0 ? score*100 + score_lo : score*100 - score_lo;
13214 /* [HGM] PV time: now locate end of PV info */
13215 while( *++sep >= '0' && *sep <= '9'); // strip depth
13217 while( *++sep >= '0' && *sep <= '9'); // strip time
13219 while( *++sep >= '0' && *sep <= '9'); // strip seconds
13221 while( *++sep >= '0' && *sep <= '9'); // strip fractional seconds
13222 while(*sep == ' ') sep++;
13233 pvInfoList[index-1].depth = depth;
13234 pvInfoList[index-1].score = score;
13235 pvInfoList[index-1].time = 10*time; // centi-sec
13236 if(*sep == '}') *sep = 0; else *--sep = '{';
13242 SendToProgram(message, cps)
13244 ChessProgramState *cps;
13246 int count, outCount, error;
13249 if (cps->pr == NULL) return;
13252 if (appData.debugMode) {
13255 fprintf(debugFP, "%ld >%-6s: %s",
13256 SubtractTimeMarks(&now, &programStartTime),
13257 cps->which, message);
13260 count = strlen(message);
13261 outCount = OutputToProcess(cps->pr, message, count, &error);
13262 if (outCount < count && !exiting
13263 && !endingGame) { /* [HGM] crash: to not hang GameEnds() writing to deceased engines */
13264 sprintf(buf, _("Error writing to %s chess program"), cps->which);
13265 if(gameInfo.resultDetails==NULL) { /* [HGM] crash: if game in progress, give reason for abort */
13266 if((signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
13267 gameInfo.result = GameIsDrawn; /* [HGM] accept exit as draw claim */
13268 sprintf(buf, "%s program exits in draw position (%s)", cps->which, cps->program);
13270 gameInfo.result = cps->twoMachinesColor[0]=='w' ? BlackWins : WhiteWins;
13272 gameInfo.resultDetails = StrSave(buf);
13274 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, error, 1); else errorExitStatus = 1;
13279 ReceiveFromProgram(isr, closure, message, count, error)
13280 InputSourceRef isr;
13288 ChessProgramState *cps = (ChessProgramState *)closure;
13290 if (isr != cps->isr) return; /* Killed intentionally */
13294 _("Error: %s chess program (%s) exited unexpectedly"),
13295 cps->which, cps->program);
13296 if(gameInfo.resultDetails==NULL) { /* [HGM] crash: if game in progress, give reason for abort */
13297 if((signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
13298 gameInfo.result = GameIsDrawn; /* [HGM] accept exit as draw claim */
13299 sprintf(buf, _("%s program exits in draw position (%s)"), cps->which, cps->program);
13301 gameInfo.result = cps->twoMachinesColor[0]=='w' ? BlackWins : WhiteWins;
13303 gameInfo.resultDetails = StrSave(buf);
13305 RemoveInputSource(cps->isr);
13306 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, 0, 1); else errorExitStatus = 1;
13309 _("Error reading from %s chess program (%s)"),
13310 cps->which, cps->program);
13311 RemoveInputSource(cps->isr);
13313 /* [AS] Program is misbehaving badly... kill it */
13314 if( count == -2 ) {
13315 DestroyChildProcess( cps->pr, 9 );
13319 if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, error, 1); else errorExitStatus = 1;
13324 if ((end_str = strchr(message, '\r')) != NULL)
13325 *end_str = NULLCHAR;
13326 if ((end_str = strchr(message, '\n')) != NULL)
13327 *end_str = NULLCHAR;
13329 if (appData.debugMode) {
13330 TimeMark now; int print = 1;
13331 char *quote = ""; char c; int i;
13333 if(appData.engineComments != 1) { /* [HGM] debug: decide if protocol-violating output is written */
13334 char start = message[0];
13335 if(start >='A' && start <= 'Z') start += 'a' - 'A'; // be tolerant to capitalizing
13336 if(sscanf(message, "%d%c%d%d%d", &i, &c, &i, &i, &i) != 5 &&
13337 sscanf(message, "move %c", &c)!=1 && sscanf(message, "offer%c", &c)!=1 &&
13338 sscanf(message, "resign%c", &c)!=1 && sscanf(message, "feature %c", &c)!=1 &&
13339 sscanf(message, "error %c", &c)!=1 && sscanf(message, "illegal %c", &c)!=1 &&
13340 sscanf(message, "tell%c", &c)!=1 && sscanf(message, "0-1 %c", &c)!=1 &&
13341 sscanf(message, "1-0 %c", &c)!=1 && sscanf(message, "1/2-1/2 %c", &c)!=1 &&
13342 sscanf(message, "pong %c", &c)!=1 && start != '#')
13343 { quote = "# "; print = (appData.engineComments == 2); }
13344 message[0] = start; // restore original message
13348 fprintf(debugFP, "%ld <%-6s: %s%s\n",
13349 SubtractTimeMarks(&now, &programStartTime), cps->which,
13355 /* [DM] if icsEngineAnalyze is active we block all whisper and kibitz output, because nobody want to see this */
13356 if (appData.icsEngineAnalyze) {
13357 if (strstr(message, "whisper") != NULL ||
13358 strstr(message, "kibitz") != NULL ||
13359 strstr(message, "tellics") != NULL) return;
13362 HandleMachineMove(message, cps);
13367 SendTimeControl(cps, mps, tc, inc, sd, st)
13368 ChessProgramState *cps;
13369 int mps, inc, sd, st;
13375 if( timeControl_2 > 0 ) {
13376 if( (gameMode == MachinePlaysBlack) || (gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b') ) {
13377 tc = timeControl_2;
13380 tc /= cps->timeOdds; /* [HGM] time odds: apply before telling engine */
13381 inc /= cps->timeOdds;
13382 st /= cps->timeOdds;
13384 seconds = (tc / 1000) % 60; /* [HGM] displaced to after applying odds */
13387 /* Set exact time per move, normally using st command */
13388 if (cps->stKludge) {
13389 /* GNU Chess 4 has no st command; uses level in a nonstandard way */
13391 if (seconds == 0) {
13392 sprintf(buf, "level 1 %d\n", st/60);
13394 sprintf(buf, "level 1 %d:%02d\n", st/60, seconds);
13397 sprintf(buf, "st %d\n", st);
13400 /* Set conventional or incremental time control, using level command */
13401 if (seconds == 0) {
13402 /* Note old gnuchess bug -- minutes:seconds used to not work.
13403 Fixed in later versions, but still avoid :seconds
13404 when seconds is 0. */
13405 sprintf(buf, "level %d %ld %d\n", mps, tc/60000, inc/1000);
13407 sprintf(buf, "level %d %ld:%02d %d\n", mps, tc/60000,
13408 seconds, inc/1000);
13411 SendToProgram(buf, cps);
13413 /* Orthoganally (except for GNU Chess 4), limit time to st seconds */
13414 /* Orthogonally, limit search to given depth */
13416 if (cps->sdKludge) {
13417 sprintf(buf, "depth\n%d\n", sd);
13419 sprintf(buf, "sd %d\n", sd);
13421 SendToProgram(buf, cps);
13424 if(cps->nps > 0) { /* [HGM] nps */
13425 if(cps->supportsNPS == FALSE) cps->nps = -1; // don't use if engine explicitly says not supported!
13427 sprintf(buf, "nps %d\n", cps->nps);
13428 SendToProgram(buf, cps);
13433 ChessProgramState *WhitePlayer()
13434 /* [HGM] return pointer to 'first' or 'second', depending on who plays white */
13436 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b' ||
13437 gameMode == BeginningOfGame || gameMode == MachinePlaysBlack)
13443 SendTimeRemaining(cps, machineWhite)
13444 ChessProgramState *cps;
13445 int /*boolean*/ machineWhite;
13447 char message[MSG_SIZ];
13450 /* Note: this routine must be called when the clocks are stopped
13451 or when they have *just* been set or switched; otherwise
13452 it will be off by the time since the current tick started.
13454 if (machineWhite) {
13455 time = whiteTimeRemaining / 10;
13456 otime = blackTimeRemaining / 10;
13458 time = blackTimeRemaining / 10;
13459 otime = whiteTimeRemaining / 10;
13461 /* [HGM] translate opponent's time by time-odds factor */
13462 otime = (otime * cps->other->timeOdds) / cps->timeOdds;
13463 if (appData.debugMode) {
13464 fprintf(debugFP, "time odds: %f %f \n", cps->timeOdds, cps->other->timeOdds);
13467 if (time <= 0) time = 1;
13468 if (otime <= 0) otime = 1;
13470 sprintf(message, "time %ld\n", time);
13471 SendToProgram(message, cps);
13473 sprintf(message, "otim %ld\n", otime);
13474 SendToProgram(message, cps);
13478 BoolFeature(p, name, loc, cps)
13482 ChessProgramState *cps;
13485 int len = strlen(name);
13487 if (strncmp((*p), name, len) == 0 && (*p)[len] == '=') {
13489 sscanf(*p, "%d", &val);
13491 while (**p && **p != ' ') (*p)++;
13492 sprintf(buf, "accepted %s\n", name);
13493 SendToProgram(buf, cps);
13500 IntFeature(p, name, loc, cps)
13504 ChessProgramState *cps;
13507 int len = strlen(name);
13508 if (strncmp((*p), name, len) == 0 && (*p)[len] == '=') {
13510 sscanf(*p, "%d", loc);
13511 while (**p && **p != ' ') (*p)++;
13512 sprintf(buf, "accepted %s\n", name);
13513 SendToProgram(buf, cps);
13520 StringFeature(p, name, loc, cps)
13524 ChessProgramState *cps;
13527 int len = strlen(name);
13528 if (strncmp((*p), name, len) == 0
13529 && (*p)[len] == '=' && (*p)[len+1] == '\"') {
13531 sscanf(*p, "%[^\"]", loc);
13532 while (**p && **p != '\"') (*p)++;
13533 if (**p == '\"') (*p)++;
13534 sprintf(buf, "accepted %s\n", name);
13535 SendToProgram(buf, cps);
13542 ParseOption(Option *opt, ChessProgramState *cps)
13543 // [HGM] options: process the string that defines an engine option, and determine
13544 // name, type, default value, and allowed value range
13546 char *p, *q, buf[MSG_SIZ];
13547 int n, min = (-1)<<31, max = 1<<31, def;
13549 if(p = strstr(opt->name, " -spin ")) {
13550 if((n = sscanf(p, " -spin %d %d %d", &def, &min, &max)) < 3 ) return FALSE;
13551 if(max < min) max = min; // enforce consistency
13552 if(def < min) def = min;
13553 if(def > max) def = max;
13558 } else if((p = strstr(opt->name, " -slider "))) {
13559 // for now -slider is a synonym for -spin, to already provide compatibility with future polyglots
13560 if((n = sscanf(p, " -slider %d %d %d", &def, &min, &max)) < 3 ) return FALSE;
13561 if(max < min) max = min; // enforce consistency
13562 if(def < min) def = min;
13563 if(def > max) def = max;
13567 opt->type = Spin; // Slider;
13568 } else if((p = strstr(opt->name, " -string "))) {
13569 opt->textValue = p+9;
13570 opt->type = TextBox;
13571 } else if((p = strstr(opt->name, " -file "))) {
13572 // for now -file is a synonym for -string, to already provide compatibility with future polyglots
13573 opt->textValue = p+7;
13574 opt->type = TextBox; // FileName;
13575 } else if((p = strstr(opt->name, " -path "))) {
13576 // for now -file is a synonym for -string, to already provide compatibility with future polyglots
13577 opt->textValue = p+7;
13578 opt->type = TextBox; // PathName;
13579 } else if(p = strstr(opt->name, " -check ")) {
13580 if(sscanf(p, " -check %d", &def) < 1) return FALSE;
13581 opt->value = (def != 0);
13582 opt->type = CheckBox;
13583 } else if(p = strstr(opt->name, " -combo ")) {
13584 opt->textValue = (char*) (&cps->comboList[cps->comboCnt]); // cheat with pointer type
13585 cps->comboList[cps->comboCnt++] = q = p+8; // holds possible choices
13586 if(*q == '*') cps->comboList[cps->comboCnt-1]++;
13587 opt->value = n = 0;
13588 while(q = StrStr(q, " /// ")) {
13589 n++; *q = 0; // count choices, and null-terminate each of them
13591 if(*q == '*') { // remember default, which is marked with * prefix
13595 cps->comboList[cps->comboCnt++] = q;
13597 cps->comboList[cps->comboCnt++] = NULL;
13599 opt->type = ComboBox;
13600 } else if(p = strstr(opt->name, " -button")) {
13601 opt->type = Button;
13602 } else if(p = strstr(opt->name, " -save")) {
13603 opt->type = SaveButton;
13604 } else return FALSE;
13605 *p = 0; // terminate option name
13606 // now look if the command-line options define a setting for this engine option.
13607 if(cps->optionSettings && cps->optionSettings[0])
13608 p = strstr(cps->optionSettings, opt->name); else p = NULL;
13609 if(p && (p == cps->optionSettings || p[-1] == ',')) {
13610 sprintf(buf, "option %s", p);
13611 if(p = strstr(buf, ",")) *p = 0;
13613 SendToProgram(buf, cps);
13619 FeatureDone(cps, val)
13620 ChessProgramState* cps;
13623 DelayedEventCallback cb = GetDelayedEvent();
13624 if ((cb == InitBackEnd3 && cps == &first) ||
13625 (cb == TwoMachinesEventIfReady && cps == &second)) {
13626 CancelDelayedEvent();
13627 ScheduleDelayedEvent(cb, val ? 1 : 3600000);
13629 cps->initDone = val;
13632 /* Parse feature command from engine */
13634 ParseFeatures(args, cps)
13636 ChessProgramState *cps;
13644 while (*p == ' ') p++;
13645 if (*p == NULLCHAR) return;
13647 if (BoolFeature(&p, "setboard", &cps->useSetboard, cps)) continue;
13648 if (BoolFeature(&p, "time", &cps->sendTime, cps)) continue;
13649 if (BoolFeature(&p, "draw", &cps->sendDrawOffers, cps)) continue;
13650 if (BoolFeature(&p, "sigint", &cps->useSigint, cps)) continue;
13651 if (BoolFeature(&p, "sigterm", &cps->useSigterm, cps)) continue;
13652 if (BoolFeature(&p, "reuse", &val, cps)) {
13653 /* Engine can disable reuse, but can't enable it if user said no */
13654 if (!val) cps->reuse = FALSE;
13657 if (BoolFeature(&p, "analyze", &cps->analysisSupport, cps)) continue;
13658 if (StringFeature(&p, "myname", &cps->tidy, cps)) {
13659 if (gameMode == TwoMachinesPlay) {
13660 DisplayTwoMachinesTitle();
13666 if (StringFeature(&p, "variants", &cps->variants, cps)) continue;
13667 if (BoolFeature(&p, "san", &cps->useSAN, cps)) continue;
13668 if (BoolFeature(&p, "ping", &cps->usePing, cps)) continue;
13669 if (BoolFeature(&p, "playother", &cps->usePlayother, cps)) continue;
13670 if (BoolFeature(&p, "colors", &cps->useColors, cps)) continue;
13671 if (BoolFeature(&p, "usermove", &cps->useUsermove, cps)) continue;
13672 if (BoolFeature(&p, "ics", &cps->sendICS, cps)) continue;
13673 if (BoolFeature(&p, "name", &cps->sendName, cps)) continue;
13674 if (BoolFeature(&p, "pause", &val, cps)) continue; /* unused at present */
13675 if (IntFeature(&p, "done", &val, cps)) {
13676 FeatureDone(cps, val);
13679 /* Added by Tord: */
13680 if (BoolFeature(&p, "fen960", &cps->useFEN960, cps)) continue;
13681 if (BoolFeature(&p, "oocastle", &cps->useOOCastle, cps)) continue;
13682 /* End of additions by Tord */
13684 /* [HGM] added features: */
13685 if (BoolFeature(&p, "debug", &cps->debug, cps)) continue;
13686 if (BoolFeature(&p, "nps", &cps->supportsNPS, cps)) continue;
13687 if (IntFeature(&p, "level", &cps->maxNrOfSessions, cps)) continue;
13688 if (BoolFeature(&p, "memory", &cps->memSize, cps)) continue;
13689 if (BoolFeature(&p, "smp", &cps->maxCores, cps)) continue;
13690 if (StringFeature(&p, "egt", &cps->egtFormats, cps)) continue;
13691 if (StringFeature(&p, "option", &(cps->option[cps->nrOptions].name), cps)) {
13692 if(!ParseOption(&(cps->option[cps->nrOptions++]), cps)) { // [HGM] options: add option feature
13693 sprintf(buf, "rejected option %s\n", cps->option[--cps->nrOptions].name);
13694 SendToProgram(buf, cps);
13697 if(cps->nrOptions >= MAX_OPTIONS) {
13699 sprintf(buf, "%s engine has too many options\n", cps->which);
13700 DisplayError(buf, 0);
13704 if (BoolFeature(&p, "smp", &cps->maxCores, cps)) continue;
13705 /* End of additions by HGM */
13707 /* unknown feature: complain and skip */
13709 while (*q && *q != '=') q++;
13710 sprintf(buf, "rejected %.*s\n", (int)(q-p), p);
13711 SendToProgram(buf, cps);
13717 while (*p && *p != '\"') p++;
13718 if (*p == '\"') p++;
13720 while (*p && *p != ' ') p++;
13728 PeriodicUpdatesEvent(newState)
13731 if (newState == appData.periodicUpdates)
13734 appData.periodicUpdates=newState;
13736 /* Display type changes, so update it now */
13737 // DisplayAnalysis();
13739 /* Get the ball rolling again... */
13741 AnalysisPeriodicEvent(1);
13742 StartAnalysisClock();
13747 PonderNextMoveEvent(newState)
13750 if (newState == appData.ponderNextMove) return;
13751 if (gameMode == EditPosition) EditPositionDone(TRUE);
13753 SendToProgram("hard\n", &first);
13754 if (gameMode == TwoMachinesPlay) {
13755 SendToProgram("hard\n", &second);
13758 SendToProgram("easy\n", &first);
13759 thinkOutput[0] = NULLCHAR;
13760 if (gameMode == TwoMachinesPlay) {
13761 SendToProgram("easy\n", &second);
13764 appData.ponderNextMove = newState;
13768 NewSettingEvent(option, command, value)
13774 if (gameMode == EditPosition) EditPositionDone(TRUE);
13775 sprintf(buf, "%s%s %d\n", (option ? "option ": ""), command, value);
13776 SendToProgram(buf, &first);
13777 if (gameMode == TwoMachinesPlay) {
13778 SendToProgram(buf, &second);
13783 ShowThinkingEvent()
13784 // [HGM] thinking: this routine is now also called from "Options -> Engine..." popup
13786 static int oldState = 2; // kludge alert! Neither true nor fals, so first time oldState is always updated
13787 int newState = appData.showThinking
13788 // [HGM] thinking: other features now need thinking output as well
13789 || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp();
13791 if (oldState == newState) return;
13792 oldState = newState;
13793 if (gameMode == EditPosition) EditPositionDone(TRUE);
13795 SendToProgram("post\n", &first);
13796 if (gameMode == TwoMachinesPlay) {
13797 SendToProgram("post\n", &second);
13800 SendToProgram("nopost\n", &first);
13801 thinkOutput[0] = NULLCHAR;
13802 if (gameMode == TwoMachinesPlay) {
13803 SendToProgram("nopost\n", &second);
13806 // appData.showThinking = newState; // [HGM] thinking: responsible option should already have be changed when calling this routine!
13810 AskQuestionEvent(title, question, replyPrefix, which)
13811 char *title; char *question; char *replyPrefix; char *which;
13813 ProcRef pr = (which[0] == '1') ? first.pr : second.pr;
13814 if (pr == NoProc) return;
13815 AskQuestion(title, question, replyPrefix, pr);
13819 DisplayMove(moveNumber)
13822 char message[MSG_SIZ];
13824 char cpThinkOutput[MSG_SIZ];
13826 if(appData.noGUI) return; // [HGM] fast: suppress display of moves
13828 if (moveNumber == forwardMostMove - 1 ||
13829 gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
13831 safeStrCpy(cpThinkOutput, thinkOutput, sizeof(cpThinkOutput));
13833 if (strchr(cpThinkOutput, '\n')) {
13834 *strchr(cpThinkOutput, '\n') = NULLCHAR;
13837 *cpThinkOutput = NULLCHAR;
13840 /* [AS] Hide thinking from human user */
13841 if( appData.hideThinkingFromHuman && gameMode != TwoMachinesPlay ) {
13842 *cpThinkOutput = NULLCHAR;
13843 if( thinkOutput[0] != NULLCHAR ) {
13846 for( i=0; i<=hiddenThinkOutputState; i++ ) {
13847 cpThinkOutput[i] = '.';
13849 cpThinkOutput[i] = NULLCHAR;
13850 hiddenThinkOutputState = (hiddenThinkOutputState + 1) % 3;
13854 if (moveNumber == forwardMostMove - 1 &&
13855 gameInfo.resultDetails != NULL) {
13856 if (gameInfo.resultDetails[0] == NULLCHAR) {
13857 sprintf(res, " %s", PGNResult(gameInfo.result));
13859 sprintf(res, " {%s} %s",
13860 gameInfo.resultDetails, PGNResult(gameInfo.result));
13866 if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
13867 DisplayMessage(res, cpThinkOutput);
13869 sprintf(message, "%d.%s%s%s", moveNumber / 2 + 1,
13870 WhiteOnMove(moveNumber) ? " " : ".. ",
13871 parseList[moveNumber], res);
13872 DisplayMessage(message, cpThinkOutput);
13877 DisplayComment(moveNumber, text)
13881 char title[MSG_SIZ];
13882 char buf[8000]; // comment can be long!
13885 if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
13886 strcpy(title, "Comment");
13888 sprintf(title, "Comment on %d.%s%s", moveNumber / 2 + 1,
13889 WhiteOnMove(moveNumber) ? " " : ".. ",
13890 parseList[moveNumber]);
13892 // [HGM] PV info: display PV info together with (or as) comment
13893 if(moveNumber >= 0 && (depth = pvInfoList[moveNumber].depth) > 0) {
13894 if(text == NULL) text = "";
13895 score = pvInfoList[moveNumber].score;
13896 sprintf(buf, "%s%.2f/%d %d\n%s", score>0 ? "+" : "", score/100.,
13897 depth, (pvInfoList[moveNumber].time+50)/100, text);
13900 if (text != NULL && (appData.autoDisplayComment || commentUp))
13901 CommentPopUp(title, text);
13904 /* This routine sends a ^C interrupt to gnuchess, to awaken it if it
13905 * might be busy thinking or pondering. It can be omitted if your
13906 * gnuchess is configured to stop thinking immediately on any user
13907 * input. However, that gnuchess feature depends on the FIONREAD
13908 * ioctl, which does not work properly on some flavors of Unix.
13912 ChessProgramState *cps;
13915 if (!cps->useSigint) return;
13916 if (appData.noChessProgram || (cps->pr == NoProc)) return;
13917 switch (gameMode) {
13918 case MachinePlaysWhite:
13919 case MachinePlaysBlack:
13920 case TwoMachinesPlay:
13921 case IcsPlayingWhite:
13922 case IcsPlayingBlack:
13925 /* Skip if we know it isn't thinking */
13926 if (!cps->maybeThinking) return;
13927 if (appData.debugMode)
13928 fprintf(debugFP, "Interrupting %s\n", cps->which);
13929 InterruptChildProcess(cps->pr);
13930 cps->maybeThinking = FALSE;
13935 #endif /*ATTENTION*/
13941 if (whiteTimeRemaining <= 0) {
13944 if (appData.icsActive) {
13945 if (appData.autoCallFlag &&
13946 gameMode == IcsPlayingBlack && !blackFlag) {
13947 SendToICS(ics_prefix);
13948 SendToICS("flag\n");
13952 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Both flags fell"));
13954 if(gameMode != TwoMachinesPlay) DisplayTitle(_("White's flag fell"));
13955 if (appData.autoCallFlag) {
13956 GameEnds(BlackWins, "Black wins on time", GE_XBOARD);
13963 if (blackTimeRemaining <= 0) {
13966 if (appData.icsActive) {
13967 if (appData.autoCallFlag &&
13968 gameMode == IcsPlayingWhite && !whiteFlag) {
13969 SendToICS(ics_prefix);
13970 SendToICS("flag\n");
13974 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Both flags fell"));
13976 if(gameMode != TwoMachinesPlay) DisplayTitle(_("Black's flag fell"));
13977 if (appData.autoCallFlag) {
13978 GameEnds(WhiteWins, "White wins on time", GE_XBOARD);
13991 if (!appData.clockMode || appData.icsActive || searchTime || // [HGM] st: no inc in st mode
13992 gameMode == PlayFromGameFile || forwardMostMove == 0) return;
13995 * add time to clocks when time control is achieved ([HGM] now also used for increment)
13997 if ( !WhiteOnMove(forwardMostMove) )
13998 /* White made time control */
13999 whiteTimeRemaining += GetTimeQuota((forwardMostMove-1)/2)
14000 /* [HGM] time odds: correct new time quota for time odds! */
14001 / WhitePlayer()->timeOdds;
14003 /* Black made time control */
14004 blackTimeRemaining += GetTimeQuota((forwardMostMove-1)/2)
14005 / WhitePlayer()->other->timeOdds;
14009 DisplayBothClocks()
14011 int wom = gameMode == EditPosition ?
14012 !blackPlaysFirst : WhiteOnMove(currentMove);
14013 DisplayWhiteClock(whiteTimeRemaining, wom);
14014 DisplayBlackClock(blackTimeRemaining, !wom);
14018 /* Timekeeping seems to be a portability nightmare. I think everyone
14019 has ftime(), but I'm really not sure, so I'm including some ifdefs
14020 to use other calls if you don't. Clocks will be less accurate if
14021 you have neither ftime nor gettimeofday.
14024 /* VS 2008 requires the #include outside of the function */
14025 #if !HAVE_GETTIMEOFDAY && HAVE_FTIME
14026 #include <sys/timeb.h>
14029 /* Get the current time as a TimeMark */
14034 #if HAVE_GETTIMEOFDAY
14036 struct timeval timeVal;
14037 struct timezone timeZone;
14039 gettimeofday(&timeVal, &timeZone);
14040 tm->sec = (long) timeVal.tv_sec;
14041 tm->ms = (int) (timeVal.tv_usec / 1000L);
14043 #else /*!HAVE_GETTIMEOFDAY*/
14046 // include <sys/timeb.h> / moved to just above start of function
14047 struct timeb timeB;
14050 tm->sec = (long) timeB.time;
14051 tm->ms = (int) timeB.millitm;
14053 #else /*!HAVE_FTIME && !HAVE_GETTIMEOFDAY*/
14054 tm->sec = (long) time(NULL);
14060 /* Return the difference in milliseconds between two
14061 time marks. We assume the difference will fit in a long!
14064 SubtractTimeMarks(tm2, tm1)
14065 TimeMark *tm2, *tm1;
14067 return 1000L*(tm2->sec - tm1->sec) +
14068 (long) (tm2->ms - tm1->ms);
14073 * Code to manage the game clocks.
14075 * In tournament play, black starts the clock and then white makes a move.
14076 * We give the human user a slight advantage if he is playing white---the
14077 * clocks don't run until he makes his first move, so it takes zero time.
14078 * Also, we don't account for network lag, so we could get out of sync
14079 * with GNU Chess's clock -- but then, referees are always right.
14082 static TimeMark tickStartTM;
14083 static long intendedTickLength;
14086 NextTickLength(timeRemaining)
14087 long timeRemaining;
14089 long nominalTickLength, nextTickLength;
14091 if (timeRemaining > 0L && timeRemaining <= 10000L)
14092 nominalTickLength = 100L;
14094 nominalTickLength = 1000L;
14095 nextTickLength = timeRemaining % nominalTickLength;
14096 if (nextTickLength <= 0) nextTickLength += nominalTickLength;
14098 return nextTickLength;
14101 /* Adjust clock one minute up or down */
14103 AdjustClock(Boolean which, int dir)
14105 if(which) blackTimeRemaining += 60000*dir;
14106 else whiteTimeRemaining += 60000*dir;
14107 DisplayBothClocks();
14110 /* Stop clocks and reset to a fresh time control */
14114 (void) StopClockTimer();
14115 if (appData.icsActive) {
14116 whiteTimeRemaining = blackTimeRemaining = 0;
14117 } else if (searchTime) {
14118 whiteTimeRemaining = 1000*searchTime / WhitePlayer()->timeOdds;
14119 blackTimeRemaining = 1000*searchTime / WhitePlayer()->other->timeOdds;
14120 } else { /* [HGM] correct new time quote for time odds */
14121 whiteTimeRemaining = GetTimeQuota(-1) / WhitePlayer()->timeOdds;
14122 blackTimeRemaining = GetTimeQuota(-1) / WhitePlayer()->other->timeOdds;
14124 if (whiteFlag || blackFlag) {
14126 whiteFlag = blackFlag = FALSE;
14128 DisplayBothClocks();
14131 #define FUDGE 25 /* 25ms = 1/40 sec; should be plenty even for 50 Hz clocks */
14133 /* Decrement running clock by amount of time that has passed */
14137 long timeRemaining;
14138 long lastTickLength, fudge;
14141 if (!appData.clockMode) return;
14142 if (gameMode==AnalyzeMode || gameMode == AnalyzeFile) return;
14146 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
14148 /* Fudge if we woke up a little too soon */
14149 fudge = intendedTickLength - lastTickLength;
14150 if (fudge < 0 || fudge > FUDGE) fudge = 0;
14152 if (WhiteOnMove(forwardMostMove)) {
14153 if(whiteNPS >= 0) lastTickLength = 0;
14154 timeRemaining = whiteTimeRemaining -= lastTickLength;
14155 DisplayWhiteClock(whiteTimeRemaining - fudge,
14156 WhiteOnMove(currentMove < forwardMostMove ? currentMove : forwardMostMove));
14158 if(blackNPS >= 0) lastTickLength = 0;
14159 timeRemaining = blackTimeRemaining -= lastTickLength;
14160 DisplayBlackClock(blackTimeRemaining - fudge,
14161 !WhiteOnMove(currentMove < forwardMostMove ? currentMove : forwardMostMove));
14164 if (CheckFlags()) return;
14167 intendedTickLength = NextTickLength(timeRemaining - fudge) + fudge;
14168 StartClockTimer(intendedTickLength);
14170 /* if the time remaining has fallen below the alarm threshold, sound the
14171 * alarm. if the alarm has sounded and (due to a takeback or time control
14172 * with increment) the time remaining has increased to a level above the
14173 * threshold, reset the alarm so it can sound again.
14176 if (appData.icsActive && appData.icsAlarm) {
14178 /* make sure we are dealing with the user's clock */
14179 if (!( ((gameMode == IcsPlayingWhite) && WhiteOnMove(currentMove)) ||
14180 ((gameMode == IcsPlayingBlack) && !WhiteOnMove(currentMove))
14183 if (alarmSounded && (timeRemaining > appData.icsAlarmTime)) {
14184 alarmSounded = FALSE;
14185 } else if (!alarmSounded && (timeRemaining <= appData.icsAlarmTime)) {
14187 alarmSounded = TRUE;
14193 /* A player has just moved, so stop the previously running
14194 clock and (if in clock mode) start the other one.
14195 We redisplay both clocks in case we're in ICS mode, because
14196 ICS gives us an update to both clocks after every move.
14197 Note that this routine is called *after* forwardMostMove
14198 is updated, so the last fractional tick must be subtracted
14199 from the color that is *not* on move now.
14202 SwitchClocks(int newMoveNr)
14204 long lastTickLength;
14206 int flagged = FALSE;
14210 if (StopClockTimer() && appData.clockMode) {
14211 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
14212 if (!WhiteOnMove(forwardMostMove)) {
14213 if(blackNPS >= 0) lastTickLength = 0;
14214 blackTimeRemaining -= lastTickLength;
14215 /* [HGM] PGNtime: save time for PGN file if engine did not give it */
14216 // if(pvInfoList[forwardMostMove-1].time == -1)
14217 pvInfoList[forwardMostMove-1].time = // use GUI time
14218 (timeRemaining[1][forwardMostMove-1] - blackTimeRemaining)/10;
14220 if(whiteNPS >= 0) lastTickLength = 0;
14221 whiteTimeRemaining -= lastTickLength;
14222 /* [HGM] PGNtime: save time for PGN file if engine did not give it */
14223 // if(pvInfoList[forwardMostMove-1].time == -1)
14224 pvInfoList[forwardMostMove-1].time =
14225 (timeRemaining[0][forwardMostMove-1] - whiteTimeRemaining)/10;
14227 flagged = CheckFlags();
14229 forwardMostMove = newMoveNr; // [HGM] race: change stm when no timer interrupt scheduled
14230 CheckTimeControl();
14232 if (flagged || !appData.clockMode) return;
14234 switch (gameMode) {
14235 case MachinePlaysBlack:
14236 case MachinePlaysWhite:
14237 case BeginningOfGame:
14238 if (pausing) return;
14242 case PlayFromGameFile:
14250 if (searchTime) { // [HGM] st: set clock of player that has to move to max time
14251 if(WhiteOnMove(forwardMostMove))
14252 whiteTimeRemaining = 1000*searchTime / WhitePlayer()->timeOdds;
14253 else blackTimeRemaining = 1000*searchTime / WhitePlayer()->other->timeOdds;
14257 intendedTickLength = NextTickLength(WhiteOnMove(forwardMostMove) ?
14258 whiteTimeRemaining : blackTimeRemaining);
14259 StartClockTimer(intendedTickLength);
14263 /* Stop both clocks */
14267 long lastTickLength;
14270 if (!StopClockTimer()) return;
14271 if (!appData.clockMode) return;
14275 lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
14276 if (WhiteOnMove(forwardMostMove)) {
14277 if(whiteNPS >= 0) lastTickLength = 0;
14278 whiteTimeRemaining -= lastTickLength;
14279 DisplayWhiteClock(whiteTimeRemaining, WhiteOnMove(currentMove));
14281 if(blackNPS >= 0) lastTickLength = 0;
14282 blackTimeRemaining -= lastTickLength;
14283 DisplayBlackClock(blackTimeRemaining, !WhiteOnMove(currentMove));
14288 /* Start clock of player on move. Time may have been reset, so
14289 if clock is already running, stop and restart it. */
14293 (void) StopClockTimer(); /* in case it was running already */
14294 DisplayBothClocks();
14295 if (CheckFlags()) return;
14297 if (!appData.clockMode) return;
14298 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) return;
14300 GetTimeMark(&tickStartTM);
14301 intendedTickLength = NextTickLength(WhiteOnMove(forwardMostMove) ?
14302 whiteTimeRemaining : blackTimeRemaining);
14304 /* [HGM] nps: figure out nps factors, by determining which engine plays white and/or black once and for all */
14305 whiteNPS = blackNPS = -1;
14306 if(gameMode == MachinePlaysWhite || gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w'
14307 || appData.zippyPlay && gameMode == IcsPlayingBlack) // first (perhaps only) engine has white
14308 whiteNPS = first.nps;
14309 if(gameMode == MachinePlaysBlack || gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b'
14310 || appData.zippyPlay && gameMode == IcsPlayingWhite) // first (perhaps only) engine has black
14311 blackNPS = first.nps;
14312 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b') // second only used in Two-Machines mode
14313 whiteNPS = second.nps;
14314 if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w')
14315 blackNPS = second.nps;
14316 if(appData.debugMode) fprintf(debugFP, "nps: w=%d, b=%d\n", whiteNPS, blackNPS);
14318 StartClockTimer(intendedTickLength);
14325 long second, minute, hour, day;
14327 static char buf[32];
14329 if (ms > 0 && ms <= 9900) {
14330 /* convert milliseconds to tenths, rounding up */
14331 double tenths = floor( ((double)(ms + 99L)) / 100.00 );
14333 sprintf(buf, " %03.1f ", tenths/10.0);
14337 /* convert milliseconds to seconds, rounding up */
14338 /* use floating point to avoid strangeness of integer division
14339 with negative dividends on many machines */
14340 second = (long) floor(((double) (ms + 999L)) / 1000.0);
14347 day = second / (60 * 60 * 24);
14348 second = second % (60 * 60 * 24);
14349 hour = second / (60 * 60);
14350 second = second % (60 * 60);
14351 minute = second / 60;
14352 second = second % 60;
14355 sprintf(buf, " %s%ld:%02ld:%02ld:%02ld ",
14356 sign, day, hour, minute, second);
14358 sprintf(buf, " %s%ld:%02ld:%02ld ", sign, hour, minute, second);
14360 sprintf(buf, " %s%2ld:%02ld ", sign, minute, second);
14367 * This is necessary because some C libraries aren't ANSI C compliant yet.
14370 StrStr(string, match)
14371 char *string, *match;
14375 length = strlen(match);
14377 for (i = strlen(string) - length; i >= 0; i--, string++)
14378 if (!strncmp(match, string, length))
14385 StrCaseStr(string, match)
14386 char *string, *match;
14390 length = strlen(match);
14392 for (i = strlen(string) - length; i >= 0; i--, string++) {
14393 for (j = 0; j < length; j++) {
14394 if (ToLower(match[j]) != ToLower(string[j]))
14397 if (j == length) return string;
14411 c1 = ToLower(*s1++);
14412 c2 = ToLower(*s2++);
14413 if (c1 > c2) return 1;
14414 if (c1 < c2) return -1;
14415 if (c1 == NULLCHAR) return 0;
14424 return isupper(c) ? tolower(c) : c;
14432 return islower(c) ? toupper(c) : c;
14434 #endif /* !_amigados */
14442 if ((ret = (char *) malloc(strlen(s) + 1))) {
14449 StrSavePtr(s, savePtr)
14450 char *s, **savePtr;
14455 if ((*savePtr = (char *) malloc(strlen(s) + 1))) {
14456 strcpy(*savePtr, s);
14468 clock = time((time_t *)NULL);
14469 tm = localtime(&clock);
14470 sprintf(buf, "%04d.%02d.%02d",
14471 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
14472 return StrSave(buf);
14477 PositionToFEN(move, overrideCastling)
14479 char *overrideCastling;
14481 int i, j, fromX, fromY, toX, toY;
14488 whiteToPlay = (gameMode == EditPosition) ?
14489 !blackPlaysFirst : (move % 2 == 0);
14492 /* Piece placement data */
14493 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
14495 for (j = BOARD_LEFT; j < BOARD_RGHT; j++) {
14496 if (boards[move][i][j] == EmptySquare) {
14498 } else { ChessSquare piece = boards[move][i][j];
14499 if (emptycount > 0) {
14500 if(emptycount<10) /* [HGM] can be >= 10 */
14501 *p++ = '0' + emptycount;
14502 else { *p++ = '0' + emptycount/10; *p++ = '0' + emptycount%10; }
14505 if(PieceToChar(piece) == '+') {
14506 /* [HGM] write promoted pieces as '+<unpromoted>' (Shogi) */
14508 piece = (ChessSquare)(DEMOTED piece);
14510 *p++ = PieceToChar(piece);
14512 /* [HGM] flag promoted pieces as '<promoted>~' (Crazyhouse) */
14513 p[-1] = PieceToChar((ChessSquare)(DEMOTED piece));
14518 if (emptycount > 0) {
14519 if(emptycount<10) /* [HGM] can be >= 10 */
14520 *p++ = '0' + emptycount;
14521 else { *p++ = '0' + emptycount/10; *p++ = '0' + emptycount%10; }
14528 /* [HGM] print Crazyhouse or Shogi holdings */
14529 if( gameInfo.holdingsWidth ) {
14530 *(p-1) = '['; /* if we wanted to support BFEN, this could be '/' */
14532 for(i=0; i<gameInfo.holdingsSize; i++) { /* white holdings */
14533 piece = boards[move][i][BOARD_WIDTH-1];
14534 if( piece != EmptySquare )
14535 for(j=0; j<(int) boards[move][i][BOARD_WIDTH-2]; j++)
14536 *p++ = PieceToChar(piece);
14538 for(i=0; i<gameInfo.holdingsSize; i++) { /* black holdings */
14539 piece = boards[move][BOARD_HEIGHT-i-1][0];
14540 if( piece != EmptySquare )
14541 for(j=0; j<(int) boards[move][BOARD_HEIGHT-i-1][1]; j++)
14542 *p++ = PieceToChar(piece);
14545 if( q == p ) *p++ = '-';
14551 *p++ = whiteToPlay ? 'w' : 'b';
14554 if(q = overrideCastling) { // [HGM] FRC: override castling & e.p fields for non-compliant engines
14555 while(*p++ = *q++); if(q != overrideCastling+1) p[-1] = ' '; else --p;
14557 if(nrCastlingRights) {
14559 if(gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom) {
14560 /* [HGM] write directly from rights */
14561 if(boards[move][CASTLING][2] != NoRights &&
14562 boards[move][CASTLING][0] != NoRights )
14563 *p++ = boards[move][CASTLING][0] + AAA + 'A' - 'a';
14564 if(boards[move][CASTLING][2] != NoRights &&
14565 boards[move][CASTLING][1] != NoRights )
14566 *p++ = boards[move][CASTLING][1] + AAA + 'A' - 'a';
14567 if(boards[move][CASTLING][5] != NoRights &&
14568 boards[move][CASTLING][3] != NoRights )
14569 *p++ = boards[move][CASTLING][3] + AAA;
14570 if(boards[move][CASTLING][5] != NoRights &&
14571 boards[move][CASTLING][4] != NoRights )
14572 *p++ = boards[move][CASTLING][4] + AAA;
14575 /* [HGM] write true castling rights */
14576 if( nrCastlingRights == 6 ) {
14577 if(boards[move][CASTLING][0] == BOARD_RGHT-1 &&
14578 boards[move][CASTLING][2] != NoRights ) *p++ = 'K';
14579 if(boards[move][CASTLING][1] == BOARD_LEFT &&
14580 boards[move][CASTLING][2] != NoRights ) *p++ = 'Q';
14581 if(boards[move][CASTLING][3] == BOARD_RGHT-1 &&
14582 boards[move][CASTLING][5] != NoRights ) *p++ = 'k';
14583 if(boards[move][CASTLING][4] == BOARD_LEFT &&
14584 boards[move][CASTLING][5] != NoRights ) *p++ = 'q';
14587 if (q == p) *p++ = '-'; /* No castling rights */
14591 if(gameInfo.variant != VariantShogi && gameInfo.variant != VariantXiangqi &&
14592 gameInfo.variant != VariantShatranj && gameInfo.variant != VariantCourier && gameInfo.variant != VariantMakruk ) {
14593 /* En passant target square */
14594 if (move > backwardMostMove) {
14595 fromX = moveList[move - 1][0] - AAA;
14596 fromY = moveList[move - 1][1] - ONE;
14597 toX = moveList[move - 1][2] - AAA;
14598 toY = moveList[move - 1][3] - ONE;
14599 if (fromY == (whiteToPlay ? BOARD_HEIGHT-2 : 1) &&
14600 toY == (whiteToPlay ? BOARD_HEIGHT-4 : 3) &&
14601 boards[move][toY][toX] == (whiteToPlay ? BlackPawn : WhitePawn) &&
14603 /* 2-square pawn move just happened */
14605 *p++ = whiteToPlay ? '6'+BOARD_HEIGHT-8 : '3';
14609 } else if(move == backwardMostMove) {
14610 // [HGM] perhaps we should always do it like this, and forget the above?
14611 if((signed char)boards[move][EP_STATUS] >= 0) {
14612 *p++ = boards[move][EP_STATUS] + AAA;
14613 *p++ = whiteToPlay ? '6'+BOARD_HEIGHT-8 : '3';
14624 /* [HGM] find reversible plies */
14625 { int i = 0, j=move;
14627 if (appData.debugMode) { int k;
14628 fprintf(debugFP, "write FEN 50-move: %d %d %d\n", initialRulePlies, forwardMostMove, backwardMostMove);
14629 for(k=backwardMostMove; k<=forwardMostMove; k++)
14630 fprintf(debugFP, "e%d. p=%d\n", k, (signed char)boards[k][EP_STATUS]);
14634 while(j > backwardMostMove && (signed char)boards[j][EP_STATUS] <= EP_NONE) j--,i++;
14635 if( j == backwardMostMove ) i += initialRulePlies;
14636 sprintf(p, "%d ", i);
14637 p += i>=100 ? 4 : i >= 10 ? 3 : 2;
14639 /* Fullmove number */
14640 sprintf(p, "%d", (move / 2) + 1);
14642 return StrSave(buf);
14646 ParseFEN(board, blackPlaysFirst, fen)
14648 int *blackPlaysFirst;
14658 /* [HGM] by default clear Crazyhouse holdings, if present */
14659 if(gameInfo.holdingsWidth) {
14660 for(i=0; i<BOARD_HEIGHT; i++) {
14661 board[i][0] = EmptySquare; /* black holdings */
14662 board[i][BOARD_WIDTH-1] = EmptySquare; /* white holdings */
14663 board[i][1] = (ChessSquare) 0; /* black counts */
14664 board[i][BOARD_WIDTH-2] = (ChessSquare) 0; /* white counts */
14668 /* Piece placement data */
14669 for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
14672 if (*p == '/' || *p == ' ' || (*p == '[' && i == 0) ) {
14673 if (*p == '/') p++;
14674 emptycount = gameInfo.boardWidth - j;
14675 while (emptycount--)
14676 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
14678 #if(BOARD_FILES >= 10)
14679 } else if(*p=='x' || *p=='X') { /* [HGM] X means 10 */
14680 p++; emptycount=10;
14681 if (j + emptycount > gameInfo.boardWidth) return FALSE;
14682 while (emptycount--)
14683 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
14685 } else if (isdigit(*p)) {
14686 emptycount = *p++ - '0';
14687 while(isdigit(*p)) emptycount = 10*emptycount + *p++ - '0'; /* [HGM] allow > 9 */
14688 if (j + emptycount > gameInfo.boardWidth) return FALSE;
14689 while (emptycount--)
14690 board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
14691 } else if (*p == '+' || isalpha(*p)) {
14692 if (j >= gameInfo.boardWidth) return FALSE;
14694 piece = CharToPiece(*++p);
14695 if(piece == EmptySquare) return FALSE; /* unknown piece */
14696 piece = (ChessSquare) (PROMOTED piece ); p++;
14697 if(PieceToChar(piece) != '+') return FALSE; /* unpromotable piece */
14698 } else piece = CharToPiece(*p++);
14700 if(piece==EmptySquare) return FALSE; /* unknown piece */
14701 if(*p == '~') { /* [HGM] make it a promoted piece for Crazyhouse */
14702 piece = (ChessSquare) (PROMOTED piece);
14703 if(PieceToChar(piece) != '~') return FALSE; /* cannot be a promoted piece */
14706 board[i][(j++)+gameInfo.holdingsWidth] = piece;
14712 while (*p == '/' || *p == ' ') p++;
14714 /* [HGM] look for Crazyhouse holdings here */
14715 while(*p==' ') p++;
14716 if( gameInfo.holdingsWidth && p[-1] == '/' || *p == '[') {
14718 if(*p == '-' ) *p++; /* empty holdings */ else {
14719 if( !gameInfo.holdingsWidth ) return FALSE; /* no room to put holdings! */
14720 /* if we would allow FEN reading to set board size, we would */
14721 /* have to add holdings and shift the board read so far here */
14722 while( (piece = CharToPiece(*p) ) != EmptySquare ) {
14724 if((int) piece >= (int) BlackPawn ) {
14725 i = (int)piece - (int)BlackPawn;
14726 i = PieceToNumber((ChessSquare)i);
14727 if( i >= gameInfo.holdingsSize ) return FALSE;
14728 board[BOARD_HEIGHT-1-i][0] = piece; /* black holdings */
14729 board[BOARD_HEIGHT-1-i][1]++; /* black counts */
14731 i = (int)piece - (int)WhitePawn;
14732 i = PieceToNumber((ChessSquare)i);
14733 if( i >= gameInfo.holdingsSize ) return FALSE;
14734 board[i][BOARD_WIDTH-1] = piece; /* white holdings */
14735 board[i][BOARD_WIDTH-2]++; /* black holdings */
14739 if(*p == ']') *p++;
14742 while(*p == ' ') p++;
14747 *blackPlaysFirst = FALSE;
14750 *blackPlaysFirst = TRUE;
14756 /* [HGM] We NO LONGER ignore the rest of the FEN notation */
14757 /* return the extra info in global variiables */
14759 /* set defaults in case FEN is incomplete */
14760 board[EP_STATUS] = EP_UNKNOWN;
14761 for(i=0; i<nrCastlingRights; i++ ) {
14762 board[CASTLING][i] =
14763 gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom ? NoRights : initialRights[i];
14764 } /* assume possible unless obviously impossible */
14765 if(initialRights[0]!=NoRights && board[castlingRank[0]][initialRights[0]] != WhiteRook) board[CASTLING][0] = NoRights;
14766 if(initialRights[1]!=NoRights && board[castlingRank[1]][initialRights[1]] != WhiteRook) board[CASTLING][1] = NoRights;
14767 if(initialRights[2]!=NoRights && board[castlingRank[2]][initialRights[2]] != WhiteUnicorn
14768 && board[castlingRank[2]][initialRights[2]] != WhiteKing) board[CASTLING][2] = NoRights;
14769 if(initialRights[3]!=NoRights && board[castlingRank[3]][initialRights[3]] != BlackRook) board[CASTLING][3] = NoRights;
14770 if(initialRights[4]!=NoRights && board[castlingRank[4]][initialRights[4]] != BlackRook) board[CASTLING][4] = NoRights;
14771 if(initialRights[5]!=NoRights && board[castlingRank[5]][initialRights[5]] != BlackUnicorn
14772 && board[castlingRank[5]][initialRights[5]] != BlackKing) board[CASTLING][5] = NoRights;
14775 while(*p==' ') p++;
14776 if(nrCastlingRights) {
14777 if(*p=='K' || *p=='Q' || *p=='k' || *p=='q' || *p=='-') {
14778 /* castling indicator present, so default becomes no castlings */
14779 for(i=0; i<nrCastlingRights; i++ ) {
14780 board[CASTLING][i] = NoRights;
14783 while(*p=='K' || *p=='Q' || *p=='k' || *p=='q' || *p=='-' ||
14784 (gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom) &&
14785 ( *p >= 'a' && *p < 'a' + gameInfo.boardWidth) ||
14786 ( *p >= 'A' && *p < 'A' + gameInfo.boardWidth) ) {
14787 char c = *p++; int whiteKingFile=NoRights, blackKingFile=NoRights;
14789 for(i=BOARD_LEFT; i<BOARD_RGHT; i++) {
14790 if(board[BOARD_HEIGHT-1][i] == BlackKing) blackKingFile = i;
14791 if(board[0 ][i] == WhiteKing) whiteKingFile = i;
14793 if(gameInfo.variant == VariantTwoKings || gameInfo.variant == VariantKnightmate)
14794 whiteKingFile = blackKingFile = BOARD_WIDTH >> 1; // for these variant scanning fails
14795 if(whiteKingFile == NoRights || board[0][whiteKingFile] != WhiteUnicorn
14796 && board[0][whiteKingFile] != WhiteKing) whiteKingFile = NoRights;
14797 if(blackKingFile == NoRights || board[BOARD_HEIGHT-1][blackKingFile] != BlackUnicorn
14798 && board[BOARD_HEIGHT-1][blackKingFile] != BlackKing) blackKingFile = NoRights;
14801 for(i=BOARD_RGHT-1; board[0][i]!=WhiteRook && i>whiteKingFile; i--);
14802 board[CASTLING][0] = i != whiteKingFile ? i : NoRights;
14803 board[CASTLING][2] = whiteKingFile;
14806 for(i=BOARD_LEFT; i<BOARD_RGHT && board[0][i]!=WhiteRook && i<whiteKingFile; i++);
14807 board[CASTLING][1] = i != whiteKingFile ? i : NoRights;
14808 board[CASTLING][2] = whiteKingFile;
14811 for(i=BOARD_RGHT-1; board[BOARD_HEIGHT-1][i]!=BlackRook && i>blackKingFile; i--);
14812 board[CASTLING][3] = i != blackKingFile ? i : NoRights;
14813 board[CASTLING][5] = blackKingFile;
14816 for(i=BOARD_LEFT; i<BOARD_RGHT && board[BOARD_HEIGHT-1][i]!=BlackRook && i<blackKingFile; i++);
14817 board[CASTLING][4] = i != blackKingFile ? i : NoRights;
14818 board[CASTLING][5] = blackKingFile;
14821 default: /* FRC castlings */
14822 if(c >= 'a') { /* black rights */
14823 for(i=BOARD_LEFT; i<BOARD_RGHT; i++)
14824 if(board[BOARD_HEIGHT-1][i] == BlackKing) break;
14825 if(i == BOARD_RGHT) break;
14826 board[CASTLING][5] = i;
14828 if(board[BOARD_HEIGHT-1][c] < BlackPawn ||
14829 board[BOARD_HEIGHT-1][c] >= BlackKing ) break;
14831 board[CASTLING][3] = c;
14833 board[CASTLING][4] = c;
14834 } else { /* white rights */
14835 for(i=BOARD_LEFT; i<BOARD_RGHT; i++)
14836 if(board[0][i] == WhiteKing) break;
14837 if(i == BOARD_RGHT) break;
14838 board[CASTLING][2] = i;
14839 c -= AAA - 'a' + 'A';
14840 if(board[0][c] >= WhiteKing) break;
14842 board[CASTLING][0] = c;
14844 board[CASTLING][1] = c;
14848 for(i=0; i<nrCastlingRights; i++)
14849 if(board[CASTLING][i] != NoRights) initialRights[i] = board[CASTLING][i];
14850 if (appData.debugMode) {
14851 fprintf(debugFP, "FEN castling rights:");
14852 for(i=0; i<nrCastlingRights; i++)
14853 fprintf(debugFP, " %d", board[CASTLING][i]);
14854 fprintf(debugFP, "\n");
14857 while(*p==' ') p++;
14860 /* read e.p. field in games that know e.p. capture */
14861 if(gameInfo.variant != VariantShogi && gameInfo.variant != VariantXiangqi &&
14862 gameInfo.variant != VariantShatranj && gameInfo.variant != VariantCourier && gameInfo.variant != VariantMakruk ) {
14864 p++; board[EP_STATUS] = EP_NONE;
14866 char c = *p++ - AAA;
14868 if(c < BOARD_LEFT || c >= BOARD_RGHT) return TRUE;
14869 if(*p >= '0' && *p <='9') *p++;
14870 board[EP_STATUS] = c;
14875 if(sscanf(p, "%d", &i) == 1) {
14876 FENrulePlies = i; /* 50-move ply counter */
14877 /* (The move number is still ignored) */
14884 EditPositionPasteFEN(char *fen)
14887 Board initial_position;
14889 if (!ParseFEN(initial_position, &blackPlaysFirst, fen)) {
14890 DisplayError(_("Bad FEN position in clipboard"), 0);
14893 int savedBlackPlaysFirst = blackPlaysFirst;
14894 EditPositionEvent();
14895 blackPlaysFirst = savedBlackPlaysFirst;
14896 CopyBoard(boards[0], initial_position);
14897 initialRulePlies = FENrulePlies; /* [HGM] copy FEN attributes as well */
14898 EditPositionDone(FALSE); // [HGM] fake: do not fake rights if we had FEN
14899 DisplayBothClocks();
14900 DrawPosition(FALSE, boards[currentMove]);
14905 static char cseq[12] = "\\ ";
14907 Boolean set_cont_sequence(char *new_seq)
14912 // handle bad attempts to set the sequence
14914 return 0; // acceptable error - no debug
14916 len = strlen(new_seq);
14917 ret = (len > 0) && (len < sizeof(cseq));
14919 strcpy(cseq, new_seq);
14920 else if (appData.debugMode)
14921 fprintf(debugFP, "Invalid continuation sequence \"%s\" (maximum length is: %u)\n", new_seq, (unsigned) sizeof(cseq)-1);
14926 reformat a source message so words don't cross the width boundary. internal
14927 newlines are not removed. returns the wrapped size (no null character unless
14928 included in source message). If dest is NULL, only calculate the size required
14929 for the dest buffer. lp argument indicats line position upon entry, and it's
14930 passed back upon exit.
14932 int wrap(char *dest, char *src, int count, int width, int *lp)
14934 int len, i, ansi, cseq_len, line, old_line, old_i, old_len, clen;
14936 cseq_len = strlen(cseq);
14937 old_line = line = *lp;
14938 ansi = len = clen = 0;
14940 for (i=0; i < count; i++)
14942 if (src[i] == '\033')
14945 // if we hit the width, back up
14946 if (!ansi && (line >= width) && src[i] != '\n' && src[i] != ' ')
14948 // store i & len in case the word is too long
14949 old_i = i, old_len = len;
14951 // find the end of the last word
14952 while (i && src[i] != ' ' && src[i] != '\n')
14958 // word too long? restore i & len before splitting it
14959 if ((old_i-i+clen) >= width)
14966 if (i && src[i-1] == ' ')
14969 if (src[i] != ' ' && src[i] != '\n')
14976 // now append the newline and continuation sequence
14981 strncpy(dest+len, cseq, cseq_len);
14989 dest[len] = src[i];
14993 if (src[i] == '\n')
14998 if (dest && appData.debugMode)
15000 fprintf(debugFP, "wrap(count:%d,width:%d,line:%d,len:%d,*lp:%d,src: ",
15001 count, width, line, len, *lp);
15002 show_bytes(debugFP, src, count);
15003 fprintf(debugFP, "\ndest: ");
15004 show_bytes(debugFP, dest, len);
15005 fprintf(debugFP, "\n");
15007 *lp = dest ? line : old_line;
15012 // [HGM] vari: routines for shelving variations
15015 PushTail(int firstMove, int lastMove)
15017 int i, j, nrMoves = lastMove - firstMove;
15019 if(appData.icsActive) { // only in local mode
15020 forwardMostMove = currentMove; // mimic old ICS behavior
15023 if(storedGames >= MAX_VARIATIONS-1) return;
15025 // push current tail of game on stack
15026 savedResult[storedGames] = gameInfo.result;
15027 savedDetails[storedGames] = gameInfo.resultDetails;
15028 gameInfo.resultDetails = NULL;
15029 savedFirst[storedGames] = firstMove;
15030 savedLast [storedGames] = lastMove;
15031 savedFramePtr[storedGames] = framePtr;
15032 framePtr -= nrMoves; // reserve space for the boards
15033 for(i=nrMoves; i>=1; i--) { // copy boards to stack, working downwards, in case of overlap
15034 CopyBoard(boards[framePtr+i], boards[firstMove+i]);
15035 for(j=0; j<MOVE_LEN; j++)
15036 moveList[framePtr+i][j] = moveList[firstMove+i-1][j];
15037 for(j=0; j<2*MOVE_LEN; j++)
15038 parseList[framePtr+i][j] = parseList[firstMove+i-1][j];
15039 timeRemaining[0][framePtr+i] = timeRemaining[0][firstMove+i];
15040 timeRemaining[1][framePtr+i] = timeRemaining[1][firstMove+i];
15041 pvInfoList[framePtr+i] = pvInfoList[firstMove+i-1];
15042 pvInfoList[firstMove+i-1].depth = 0;
15043 commentList[framePtr+i] = commentList[firstMove+i];
15044 commentList[firstMove+i] = NULL;
15048 forwardMostMove = firstMove; // truncate game so we can start variation
15049 if(storedGames == 1) GreyRevert(FALSE);
15053 PopTail(Boolean annotate)
15056 char buf[8000], moveBuf[20];
15058 if(appData.icsActive) return FALSE; // only in local mode
15059 if(!storedGames) return FALSE; // sanity
15060 CommentPopDown(); // make sure no stale variation comments to the destroyed line can remain open
15063 ToNrEvent(savedFirst[storedGames]); // sets currentMove
15064 nrMoves = savedLast[storedGames] - currentMove;
15067 if(!WhiteOnMove(currentMove)) sprintf(buf, "(%d...", currentMove+2>>1);
15068 else strcpy(buf, "(");
15069 for(i=currentMove; i<forwardMostMove; i++) {
15071 sprintf(moveBuf, " %d. %s", i+2>>1, SavePart(parseList[i]));
15072 else sprintf(moveBuf, " %s", SavePart(parseList[i]));
15073 strcat(buf, moveBuf);
15074 if(commentList[i]) { strcat(buf, " "); strcat(buf, commentList[i]); }
15075 if(!--cnt) { strcat(buf, "\n"); cnt = 10; }
15079 for(i=1; i<=nrMoves; i++) { // copy last variation back
15080 CopyBoard(boards[currentMove+i], boards[framePtr+i]);
15081 for(j=0; j<MOVE_LEN; j++)
15082 moveList[currentMove+i-1][j] = moveList[framePtr+i][j];
15083 for(j=0; j<2*MOVE_LEN; j++)
15084 parseList[currentMove+i-1][j] = parseList[framePtr+i][j];
15085 timeRemaining[0][currentMove+i] = timeRemaining[0][framePtr+i];
15086 timeRemaining[1][currentMove+i] = timeRemaining[1][framePtr+i];
15087 pvInfoList[currentMove+i-1] = pvInfoList[framePtr+i];
15088 if(commentList[currentMove+i]) free(commentList[currentMove+i]);
15089 commentList[currentMove+i] = commentList[framePtr+i];
15090 commentList[framePtr+i] = NULL;
15092 if(annotate) AppendComment(currentMove+1, buf, FALSE);
15093 framePtr = savedFramePtr[storedGames];
15094 gameInfo.result = savedResult[storedGames];
15095 if(gameInfo.resultDetails != NULL) {
15096 free(gameInfo.resultDetails);
15098 gameInfo.resultDetails = savedDetails[storedGames];
15099 forwardMostMove = currentMove + nrMoves;
15100 if(storedGames == 0) GreyRevert(TRUE);
15106 { // remove all shelved variations
15108 for(i=0; i<storedGames; i++) {
15109 if(savedDetails[i])
15110 free(savedDetails[i]);
15111 savedDetails[i] = NULL;
15113 for(i=framePtr; i<MAX_MOVES; i++) {
15114 if(commentList[i]) free(commentList[i]);
15115 commentList[i] = NULL;
15117 framePtr = MAX_MOVES-1;
15122 LoadVariation(int index, char *text)
15123 { // [HGM] vari: shelve previous line and load new variation, parsed from text around text[index]
15124 char *p = text, *start = NULL, *end = NULL, wait = NULLCHAR;
15125 int level = 0, move;
15127 if(gameMode != EditGame && gameMode != AnalyzeMode) return;
15128 // first find outermost bracketing variation
15129 while(*p) { // hope I got this right... Non-nesting {} and [] can screen each other and nesting ()
15130 if(!wait) { // while inside [] pr {}, ignore everyting except matching closing ]}
15131 if(*p == '{') wait = '}'; else
15132 if(*p == '[') wait = ']'; else
15133 if(*p == '(' && level++ == 0 && p-text < index) start = p+1;
15134 if(*p == ')' && level > 0 && --level == 0 && p-text > index && end == NULL) end = p-1;
15136 if(*p == wait) wait = NULLCHAR; // closing ]} found
15139 if(!start || !end) return; // no variation found, or syntax error in PGN: ignore click
15140 if(appData.debugMode) fprintf(debugFP, "at move %d load variation '%s'\n", currentMove, start);
15141 end[1] = NULLCHAR; // clip off comment beyond variation
15142 ToNrEvent(currentMove-1);
15143 PushTail(currentMove, forwardMostMove); // shelve main variation. This truncates game
15144 // kludge: use ParsePV() to append variation to game
15145 move = currentMove;
15146 ParsePV(start, TRUE);
15147 forwardMostMove = endPV; endPV = -1; currentMove = move; // cleanup what ParsePV did
15148 ClearPremoveHighlights();
15150 ToNrEvent(currentMove+1);